From 15dada20e670f39186365aafe3cc24ab6c77bd64 Mon Sep 17 00:00:00 2001 From: Sergey Isakov Date: Wed, 4 Sep 2019 13:44:38 +0300 Subject: [PATCH] sync sources wuth new edk2 Signed-off-by: Sergey Isakov --- .gitignore | 1 + BaseTools/Source/C/GNUmakefile | 5 +- BaseTools/Source/C/Makefile | 4 +- Clover.dsc | 2 +- CloverEFI/OsxDxeCore/DxeMain.inf | 2 +- CloverEFI/OsxDxeCore/Image/Image.c | 4 +- CloverEFI/OsxEfiLdr/EfiLdr.inf | 4 +- MdePkg/Include/IndustryStandard/Pci22.h | 146 +- MdePkg/Include/Protocol/Shell.h | 2 +- MdePkg/Include/Uefi/UefiBaseType.h | 14 +- MdePkg/Include/Uefi/UefiBaseType.h.bak | 285 -- MdePkg/Library/BaseLib/BaseLib.inf | 248 +- MdePkg/Library/BaseLib/SafeString.c | 8 +- MdePkg/Library/BaseLib/String.c | 479 +- MdePkg/Library/BaseLib/X64/CpuId.S | 60 - MdePkg/Library/BaseLib/X64/CpuIdEx.S | 62 - MdePkg/Library/BaseLib/X64/DisableCache.S | 39 - MdePkg/Library/BaseLib/X64/DisablePaging64.S | 82 - MdePkg/Library/BaseLib/X64/EnableCache.S | 39 - .../BaseLib/X64/EnableDisableInterrupts.S | 36 - MdePkg/Library/BaseLib/X64/LongJump.S | 54 - MdePkg/Library/BaseLib/X64/RdRand.S | 72 - MdePkg/Library/BaseLib/X64/SetJump.S | 53 - MdePkg/Library/BaseLib/X64/SwitchStack.S | 52 - MdePkg/Library/BaseLib/X64/Thunk16.S | 334 -- .../UefiFileHandleLib/UefiFileHandleLib.c | 48 +- .../MemoryAllocationLib.c | 59 +- .../MemoryAllocationLib.c.bak | 815 ---- MdePkg/MdePkg.dec | 26 - NetworkPkg/Application/VConfig/VConfig.c | 689 +++ NetworkPkg/Application/VConfig/VConfig.inf | 56 + NetworkPkg/Application/VConfig/VConfig.uni | 16 + .../Application/VConfig/VConfigExtra.uni | 14 + .../Application/VConfig/VConfigStrings.uni | 57 + NetworkPkg/ArpDxe/ArpDriver.c | 811 ++++ NetworkPkg/ArpDxe/ArpDriver.h | 334 ++ NetworkPkg/ArpDxe/ArpDxe.inf | 63 + NetworkPkg/ArpDxe/ArpDxe.uni | 18 + NetworkPkg/ArpDxe/ArpDxeExtra.uni | 14 + NetworkPkg/ArpDxe/ArpImpl.c | 1667 +++++++ NetworkPkg/ArpDxe/ArpImpl.h | 770 ++++ NetworkPkg/ArpDxe/ArpMain.c | 739 ++++ NetworkPkg/ArpDxe/ComponentName.c | 219 + NetworkPkg/Dhcp4Dxe/ComponentName.c | 431 ++ NetworkPkg/Dhcp4Dxe/Dhcp4Driver.c | 732 +++ NetworkPkg/Dhcp4Dxe/Dhcp4Driver.h | 146 + NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.inf | 67 + NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.uni | 18 + NetworkPkg/Dhcp4Dxe/Dhcp4DxeExtra.uni | 14 + NetworkPkg/Dhcp4Dxe/Dhcp4Impl.c | 1802 ++++++++ NetworkPkg/Dhcp4Dxe/Dhcp4Impl.h | 206 + NetworkPkg/Dhcp4Dxe/Dhcp4Io.c | 1657 +++++++ NetworkPkg/Dhcp4Dxe/Dhcp4Io.h | 189 + NetworkPkg/Dhcp4Dxe/Dhcp4Option.c | 890 ++++ NetworkPkg/Dhcp4Dxe/Dhcp4Option.h | 228 + NetworkPkg/Dhcp6Dxe/ComponentName.c | 442 ++ NetworkPkg/Dhcp6Dxe/Dhcp6Driver.c | 813 ++++ NetworkPkg/Dhcp6Dxe/Dhcp6Driver.h | 150 + NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.inf | 78 + NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.uni | 17 + NetworkPkg/Dhcp6Dxe/Dhcp6DxeExtra.uni | 14 + NetworkPkg/Dhcp6Dxe/Dhcp6Impl.c | 1220 +++++ NetworkPkg/Dhcp6Dxe/Dhcp6Impl.h | 479 ++ NetworkPkg/Dhcp6Dxe/Dhcp6Io.c | 3168 +++++++++++++ NetworkPkg/Dhcp6Dxe/Dhcp6Io.h | 221 + NetworkPkg/Dhcp6Dxe/Dhcp6Utility.c | 1324 ++++++ NetworkPkg/Dhcp6Dxe/Dhcp6Utility.h | 354 ++ NetworkPkg/DnsDxe/ComponentName.c | 451 ++ NetworkPkg/DnsDxe/DnsDhcp.c | 758 ++++ NetworkPkg/DnsDxe/DnsDhcp.h | 141 + NetworkPkg/DnsDxe/DnsDriver.c | 1532 +++++++ NetworkPkg/DnsDxe/DnsDriver.h | 598 +++ NetworkPkg/DnsDxe/DnsDxe.inf | 72 + NetworkPkg/DnsDxe/DnsDxe.uni | 17 + NetworkPkg/DnsDxe/DnsDxeExtra.uni | 14 + NetworkPkg/DnsDxe/DnsImpl.c | 2243 ++++++++++ NetworkPkg/DnsDxe/DnsImpl.h | 1209 +++++ NetworkPkg/DnsDxe/DnsProtocol.c | 1729 ++++++++ NetworkPkg/DpcDxe/Dpc.c | 341 ++ NetworkPkg/DpcDxe/Dpc.h | 80 + NetworkPkg/DpcDxe/DpcDxe.inf | 46 + NetworkPkg/DpcDxe/DpcDxe.uni | 16 + NetworkPkg/DpcDxe/DpcDxeExtra.uni | 14 + NetworkPkg/HttpBootDxe/HttpBootClient.c | 1306 ++++++ NetworkPkg/HttpBootDxe/HttpBootClient.h | 137 + .../HttpBootDxe/HttpBootComponentName.c | 177 + .../HttpBootDxe/HttpBootComponentName.h | 93 + NetworkPkg/HttpBootDxe/HttpBootConfig.c | 700 +++ NetworkPkg/HttpBootDxe/HttpBootConfig.h | 73 + .../HttpBootDxe/HttpBootConfigNVDataStruc.h | 44 + .../HttpBootDxe/HttpBootConfigStrings.uni | 21 + NetworkPkg/HttpBootDxe/HttpBootConfigVfr.vfr | 49 + NetworkPkg/HttpBootDxe/HttpBootDhcp4.c | 912 ++++ NetworkPkg/HttpBootDxe/HttpBootDhcp4.h | 250 ++ NetworkPkg/HttpBootDxe/HttpBootDhcp6.c | 1032 +++++ NetworkPkg/HttpBootDxe/HttpBootDhcp6.h | 169 + NetworkPkg/HttpBootDxe/HttpBootDxe.c | 1332 ++++++ NetworkPkg/HttpBootDxe/HttpBootDxe.h | 524 +++ NetworkPkg/HttpBootDxe/HttpBootDxe.inf | 99 + NetworkPkg/HttpBootDxe/HttpBootDxe.uni | 18 + NetworkPkg/HttpBootDxe/HttpBootDxeExtra.uni | 14 + NetworkPkg/HttpBootDxe/HttpBootImpl.c | 767 ++++ NetworkPkg/HttpBootDxe/HttpBootImpl.h | 48 + NetworkPkg/HttpBootDxe/HttpBootSupport.c | 1338 ++++++ NetworkPkg/HttpBootDxe/HttpBootSupport.h | 455 ++ NetworkPkg/HttpDxe/ComponentName.c | 132 + NetworkPkg/HttpDxe/ComponentName.h | 92 + NetworkPkg/HttpDxe/HttpDns.c | 409 ++ NetworkPkg/HttpDxe/HttpDns.h | 52 + NetworkPkg/HttpDxe/HttpDriver.c | 1056 +++++ NetworkPkg/HttpDxe/HttpDriver.h | 399 ++ NetworkPkg/HttpDxe/HttpDxe.inf | 79 + NetworkPkg/HttpDxe/HttpDxe.uni | 17 + NetworkPkg/HttpDxe/HttpDxeExtra.uni | 14 + NetworkPkg/HttpDxe/HttpImpl.c | 1669 +++++++ NetworkPkg/HttpDxe/HttpImpl.h | 233 + NetworkPkg/HttpDxe/HttpProto.c | 2196 +++++++++ NetworkPkg/HttpDxe/HttpProto.h | 611 +++ NetworkPkg/HttpDxe/HttpsSupport.c | 1877 ++++++++ NetworkPkg/HttpDxe/HttpsSupport.h | 265 ++ .../HttpUtilitiesDxe/HttpUtilitiesDxe.c | 120 + .../HttpUtilitiesDxe/HttpUtilitiesDxe.h | 116 + .../HttpUtilitiesDxe/HttpUtilitiesDxe.inf | 49 + .../HttpUtilitiesDxe/HttpUtilitiesDxe.uni | 17 + .../HttpUtilitiesDxeExtra.uni | 14 + .../HttpUtilitiesDxe/HttpUtilitiesProtocol.c | 387 ++ NetworkPkg/IScsiDxe/ComponentName.c | 338 ++ NetworkPkg/IScsiDxe/IScsiAuthenticationInfo.c | 61 + NetworkPkg/IScsiDxe/IScsiCHAP.c | 471 ++ NetworkPkg/IScsiDxe/IScsiCHAP.h | 102 + NetworkPkg/IScsiDxe/IScsiConfig.c | 3934 +++++++++++++++++ NetworkPkg/IScsiDxe/IScsiConfig.h | 221 + NetworkPkg/IScsiDxe/IScsiConfigNVDataStruc.h | 232 + NetworkPkg/IScsiDxe/IScsiConfigStrings.uni | 96 + NetworkPkg/IScsiDxe/IScsiConfigVfr.vfr | 399 ++ NetworkPkg/IScsiDxe/IScsiDhcp.c | 568 +++ NetworkPkg/IScsiDxe/IScsiDhcp.h | 49 + NetworkPkg/IScsiDxe/IScsiDhcp6.c | 545 +++ NetworkPkg/IScsiDxe/IScsiDhcp6.h | 65 + NetworkPkg/IScsiDxe/IScsiDns.c | 429 ++ NetworkPkg/IScsiDxe/IScsiDns.h | 53 + NetworkPkg/IScsiDxe/IScsiDriver.c | 1874 ++++++++ NetworkPkg/IScsiDxe/IScsiDriver.h | 814 ++++ NetworkPkg/IScsiDxe/IScsiDxe.inf | 140 + NetworkPkg/IScsiDxe/IScsiDxe.uni | 17 + NetworkPkg/IScsiDxe/IScsiDxeExtra.uni | 14 + NetworkPkg/IScsiDxe/IScsiExtScsiPassThru.c | 419 ++ NetworkPkg/IScsiDxe/IScsiIbft.c | 545 +++ NetworkPkg/IScsiDxe/IScsiIbft.h | 33 + NetworkPkg/IScsiDxe/IScsiImpl.h | 202 + NetworkPkg/IScsiDxe/IScsiInitiatorName.c | 130 + NetworkPkg/IScsiDxe/IScsiMisc.c | 2565 +++++++++++ NetworkPkg/IScsiDxe/IScsiMisc.h | 467 ++ NetworkPkg/IScsiDxe/IScsiProto.c | 3180 +++++++++++++ NetworkPkg/IScsiDxe/IScsiProto.h | 1036 +++++ NetworkPkg/Include/Guid/HttpBootConfigHii.h | 19 + NetworkPkg/Include/Guid/HttpTlsCipherList.h | 32 + NetworkPkg/Include/Guid/IScsiConfigHii.h | 20 + NetworkPkg/Include/Guid/Ip6ConfigHii.h | 19 + NetworkPkg/Include/Guid/TlsAuthConfigHii.h | 20 + NetworkPkg/Include/Guid/TlsAuthentication.h | 24 + .../Guid/WifiConnectionManagerConfigHii.h | 19 + NetworkPkg/Include/Library/DpcLib.h | 53 + NetworkPkg/Include/Library/HttpLib.h | 481 ++ NetworkPkg/Include/Library/IpIoLib.h | 607 +++ NetworkPkg/Include/Library/NetLib.h | 2266 ++++++++++ NetworkPkg/Include/Library/TcpIoLib.h | 247 ++ NetworkPkg/Include/Library/UdpIoLib.h | 363 ++ NetworkPkg/Include/Protocol/Dpc.h | 98 + NetworkPkg/Ip4Dxe/ComponentName.c | 428 ++ NetworkPkg/Ip4Dxe/Ip4Common.c | 306 ++ NetworkPkg/Ip4Dxe/Ip4Common.h | 217 + NetworkPkg/Ip4Dxe/Ip4Config2.vfr | 94 + NetworkPkg/Ip4Dxe/Ip4Config2Impl.c | 2168 +++++++++ NetworkPkg/Ip4Dxe/Ip4Config2Impl.h | 294 ++ NetworkPkg/Ip4Dxe/Ip4Config2Nv.c | 1444 ++++++ NetworkPkg/Ip4Dxe/Ip4Config2Nv.h | 45 + NetworkPkg/Ip4Dxe/Ip4Driver.c | 1069 +++++ NetworkPkg/Ip4Dxe/Ip4Driver.h | 184 + NetworkPkg/Ip4Dxe/Ip4Dxe.inf | 110 + NetworkPkg/Ip4Dxe/Ip4Dxe.uni | 19 + NetworkPkg/Ip4Dxe/Ip4DxeExtra.uni | 14 + NetworkPkg/Ip4Dxe/Ip4DxeStrings.uni | 30 + NetworkPkg/Ip4Dxe/Ip4Icmp.c | 363 ++ NetworkPkg/Ip4Dxe/Ip4Icmp.h | 97 + NetworkPkg/Ip4Dxe/Ip4If.c | 1345 ++++++ NetworkPkg/Ip4Dxe/Ip4If.h | 340 ++ NetworkPkg/Ip4Dxe/Ip4Igmp.c | 615 +++ NetworkPkg/Ip4Dxe/Ip4Igmp.h | 201 + NetworkPkg/Ip4Dxe/Ip4Impl.c | 2330 ++++++++++ NetworkPkg/Ip4Dxe/Ip4Impl.h | 417 ++ NetworkPkg/Ip4Dxe/Ip4Input.c | 1597 +++++++ NetworkPkg/Ip4Dxe/Ip4Input.h | 246 ++ NetworkPkg/Ip4Dxe/Ip4NvData.h | 45 + NetworkPkg/Ip4Dxe/Ip4Option.c | 204 + NetworkPkg/Ip4Dxe/Ip4Option.h | 66 + NetworkPkg/Ip4Dxe/Ip4Output.c | 482 ++ NetworkPkg/Ip4Dxe/Ip4Output.h | 120 + NetworkPkg/Ip4Dxe/Ip4Route.c | 673 +++ NetworkPkg/Ip4Dxe/Ip4Route.h | 225 + NetworkPkg/Ip6Dxe/ComponentName.c | 468 ++ NetworkPkg/Ip6Dxe/Ip6Common.c | 667 +++ NetworkPkg/Ip6Dxe/Ip6Common.h | 312 ++ NetworkPkg/Ip6Dxe/Ip6Config.vfr | 172 + NetworkPkg/Ip6Dxe/Ip6ConfigImpl.c | 2520 +++++++++++ NetworkPkg/Ip6Dxe/Ip6ConfigImpl.h | 307 ++ NetworkPkg/Ip6Dxe/Ip6ConfigNv.c | 2089 +++++++++ NetworkPkg/Ip6Dxe/Ip6ConfigNv.h | 62 + NetworkPkg/Ip6Dxe/Ip6Driver.c | 1054 +++++ NetworkPkg/Ip6Dxe/Ip6Driver.h | 186 + NetworkPkg/Ip6Dxe/Ip6Dxe.inf | 110 + NetworkPkg/Ip6Dxe/Ip6Dxe.uni | 20 + NetworkPkg/Ip6Dxe/Ip6DxeExtra.uni | 14 + NetworkPkg/Ip6Dxe/Ip6DxeStrings.uni | 55 + NetworkPkg/Ip6Dxe/Ip6Icmp.c | 679 +++ NetworkPkg/Ip6Dxe/Ip6Icmp.h | 102 + NetworkPkg/Ip6Dxe/Ip6If.c | 792 ++++ NetworkPkg/Ip6Dxe/Ip6If.h | 261 ++ NetworkPkg/Ip6Dxe/Ip6Impl.c | 1841 ++++++++ NetworkPkg/Ip6Dxe/Ip6Impl.h | 748 ++++ NetworkPkg/Ip6Dxe/Ip6Input.c | 1815 ++++++++ NetworkPkg/Ip6Dxe/Ip6Input.h | 229 + NetworkPkg/Ip6Dxe/Ip6Mld.c | 902 ++++ NetworkPkg/Ip6Dxe/Ip6Mld.h | 192 + NetworkPkg/Ip6Dxe/Ip6Nd.c | 3149 +++++++++++++ NetworkPkg/Ip6Dxe/Ip6Nd.h | 743 ++++ NetworkPkg/Ip6Dxe/Ip6NvData.h | 63 + NetworkPkg/Ip6Dxe/Ip6Option.c | 752 ++++ NetworkPkg/Ip6Dxe/Ip6Option.h | 185 + NetworkPkg/Ip6Dxe/Ip6Output.c | 1085 +++++ NetworkPkg/Ip6Dxe/Ip6Output.h | 135 + NetworkPkg/Ip6Dxe/Ip6Route.c | 629 +++ NetworkPkg/Ip6Dxe/Ip6Route.h | 293 ++ NetworkPkg/Library/DxeDpcLib/DpcLib.c | 94 + NetworkPkg/Library/DxeDpcLib/DxeDpcLib.inf | 40 + NetworkPkg/Library/DxeDpcLib/DxeDpcLib.uni | 16 + NetworkPkg/Library/DxeHttpLib/DxeHttpLib.c | 2084 +++++++++ NetworkPkg/Library/DxeHttpLib/DxeHttpLib.h | 85 + NetworkPkg/Library/DxeHttpLib/DxeHttpLib.inf | 43 + NetworkPkg/Library/DxeHttpLib/DxeHttpLib.uni | 16 + NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.c | 2291 ++++++++++ NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.inf | 47 + NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.uni | 16 + NetworkPkg/Library/DxeNetLib/DxeNetLib.c | 3394 ++++++++++++++ NetworkPkg/Library/DxeNetLib/DxeNetLib.inf | 62 + NetworkPkg/Library/DxeNetLib/DxeNetLib.uni | 16 + NetworkPkg/Library/DxeNetLib/NetBuffer.c | 1890 ++++++++ NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.c | 1011 +++++ .../Library/DxeTcpIoLib/DxeTcpIoLib.inf | 45 + .../Library/DxeTcpIoLib/DxeTcpIoLib.uni | 16 + NetworkPkg/Library/DxeUdpIoLib/DxeUdpIoLib.c | 1119 +++++ .../Library/DxeUdpIoLib/DxeUdpIoLib.inf | 47 + .../Library/DxeUdpIoLib/DxeUpdIoLib.uni | 16 + NetworkPkg/MnpDxe/ComponentName.c | 341 ++ NetworkPkg/MnpDxe/ComponentName.h | 144 + NetworkPkg/MnpDxe/MnpConfig.c | 1939 ++++++++ NetworkPkg/MnpDxe/MnpDriver.c | 683 +++ NetworkPkg/MnpDxe/MnpDriver.h | 268 ++ NetworkPkg/MnpDxe/MnpDxe.inf | 69 + NetworkPkg/MnpDxe/MnpDxe.uni | 18 + NetworkPkg/MnpDxe/MnpDxeExtra.uni | 14 + NetworkPkg/MnpDxe/MnpImpl.h | 898 ++++ NetworkPkg/MnpDxe/MnpIo.c | 1133 +++++ NetworkPkg/MnpDxe/MnpMain.c | 789 ++++ NetworkPkg/MnpDxe/MnpVlan.c | 732 +++ NetworkPkg/MnpDxe/MnpVlan.h | 205 + NetworkPkg/Mtftp4Dxe/ComponentName.c | 425 ++ NetworkPkg/Mtftp4Dxe/Mtftp4Driver.c | 739 ++++ NetworkPkg/Mtftp4Dxe/Mtftp4Driver.h | 131 + NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.inf | 70 + NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.uni | 17 + NetworkPkg/Mtftp4Dxe/Mtftp4DxeExtra.uni | 14 + NetworkPkg/Mtftp4Dxe/Mtftp4Impl.c | 1113 +++++ NetworkPkg/Mtftp4Dxe/Mtftp4Impl.h | 226 + NetworkPkg/Mtftp4Dxe/Mtftp4Option.c | 549 +++ NetworkPkg/Mtftp4Dxe/Mtftp4Option.h | 111 + NetworkPkg/Mtftp4Dxe/Mtftp4Rrq.c | 818 ++++ NetworkPkg/Mtftp4Dxe/Mtftp4Support.c | 663 +++ NetworkPkg/Mtftp4Dxe/Mtftp4Support.h | 198 + NetworkPkg/Mtftp4Dxe/Mtftp4Wrq.c | 529 +++ NetworkPkg/Mtftp6Dxe/ComponentName.c | 424 ++ NetworkPkg/Mtftp6Dxe/Mtftp6Driver.c | 749 ++++ NetworkPkg/Mtftp6Dxe/Mtftp6Driver.h | 146 + NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.inf | 71 + NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.uni | 17 + NetworkPkg/Mtftp6Dxe/Mtftp6DxeExtra.uni | 14 + NetworkPkg/Mtftp6Dxe/Mtftp6Impl.c | 641 +++ NetworkPkg/Mtftp6Dxe/Mtftp6Impl.h | 484 ++ NetworkPkg/Mtftp6Dxe/Mtftp6Option.c | 430 ++ NetworkPkg/Mtftp6Dxe/Mtftp6Option.h | 146 + NetworkPkg/Mtftp6Dxe/Mtftp6Rrq.c | 942 ++++ NetworkPkg/Mtftp6Dxe/Mtftp6Support.c | 1240 ++++++ NetworkPkg/Mtftp6Dxe/Mtftp6Support.h | 353 ++ NetworkPkg/Mtftp6Dxe/Mtftp6Wrq.c | 598 +++ NetworkPkg/Network.dsc.inc | 40 + NetworkPkg/Network.fdf.inc | 60 + NetworkPkg/NetworkComponents.dsc.inc | 61 + NetworkPkg/NetworkDefines.dsc.inc | 118 + NetworkPkg/NetworkLibs.dsc.inc | 20 + NetworkPkg/NetworkPcds.dsc.inc | 16 + NetworkPkg/NetworkPkg.dec | 128 + NetworkPkg/NetworkPkg.dsc | 106 + NetworkPkg/NetworkPkg.uni | 94 + NetworkPkg/NetworkPkgExtra.uni | 13 + NetworkPkg/SnpDxe/Callback.c | 354 ++ NetworkPkg/SnpDxe/ComponentName.c | 430 ++ NetworkPkg/SnpDxe/Get_status.c | 257 ++ NetworkPkg/SnpDxe/Initialize.c | 277 ++ NetworkPkg/SnpDxe/Mcast_ip_to_mac.c | 173 + NetworkPkg/SnpDxe/Nvdata.c | 217 + NetworkPkg/SnpDxe/Receive.c | 251 ++ NetworkPkg/SnpDxe/Receive_filters.c | 478 ++ NetworkPkg/SnpDxe/Reset.c | 130 + NetworkPkg/SnpDxe/Shutdown.c | 146 + NetworkPkg/SnpDxe/Snp.c | 862 ++++ NetworkPkg/SnpDxe/Snp.h | 1033 +++++ NetworkPkg/SnpDxe/SnpDxe.inf | 78 + NetworkPkg/SnpDxe/SnpDxe.uni | 17 + NetworkPkg/SnpDxe/SnpDxeExtra.uni | 14 + NetworkPkg/SnpDxe/Start.c | 162 + NetworkPkg/SnpDxe/Station_address.c | 243 + NetworkPkg/SnpDxe/Statistics.c | 224 + NetworkPkg/SnpDxe/Stop.c | 120 + NetworkPkg/SnpDxe/Transmit.c | 353 ++ NetworkPkg/SnpDxe/WaitForPacket.c | 86 + NetworkPkg/TcpDxe/ComponentName.c | 522 +++ NetworkPkg/TcpDxe/SockImpl.c | 1233 ++++++ NetworkPkg/TcpDxe/SockImpl.h | 115 + NetworkPkg/TcpDxe/SockInterface.c | 1126 +++++ NetworkPkg/TcpDxe/Socket.h | 917 ++++ NetworkPkg/TcpDxe/TcpDispatcher.c | 890 ++++ NetworkPkg/TcpDxe/TcpDriver.c | 995 +++++ NetworkPkg/TcpDxe/TcpDriver.h | 290 ++ NetworkPkg/TcpDxe/TcpDxe.inf | 87 + NetworkPkg/TcpDxe/TcpDxe.uni | 18 + NetworkPkg/TcpDxe/TcpDxeExtra.uni | 14 + NetworkPkg/TcpDxe/TcpFunc.h | 693 +++ NetworkPkg/TcpDxe/TcpInput.c | 1707 +++++++ NetworkPkg/TcpDxe/TcpIo.c | 186 + NetworkPkg/TcpDxe/TcpMain.c | 1106 +++++ NetworkPkg/TcpDxe/TcpMain.h | 777 ++++ NetworkPkg/TcpDxe/TcpMisc.c | 1042 +++++ NetworkPkg/TcpDxe/TcpOption.c | 338 ++ NetworkPkg/TcpDxe/TcpOption.h | 123 + NetworkPkg/TcpDxe/TcpOutput.c | 1251 ++++++ NetworkPkg/TcpDxe/TcpProto.h | 344 ++ NetworkPkg/TcpDxe/TcpTimer.c | 587 +++ .../TlsAuthConfigDxe/TlsAuthConfigDxe.c | 128 + .../TlsAuthConfigDxe/TlsAuthConfigDxe.inf | 68 + .../TlsAuthConfigDxe/TlsAuthConfigDxe.uni | 16 + .../TlsAuthConfigDxeExtra.uni | 14 + .../TlsAuthConfigDxeStrings.uni | 33 + .../TlsAuthConfigDxe/TlsAuthConfigImpl.c | 1571 +++++++ .../TlsAuthConfigDxe/TlsAuthConfigImpl.h | 276 ++ .../TlsAuthConfigDxe/TlsAuthConfigNvData.h | 44 + .../TlsAuthConfigDxe/TlsAuthConfigVfr.vfr | 147 + NetworkPkg/TlsDxe/TlsConfigProtocol.c | 147 + NetworkPkg/TlsDxe/TlsDriver.c | 491 ++ NetworkPkg/TlsDxe/TlsDriver.h | 232 + NetworkPkg/TlsDxe/TlsDxe.inf | 61 + NetworkPkg/TlsDxe/TlsDxe.uni | 19 + NetworkPkg/TlsDxe/TlsDxeExtra.uni | 13 + NetworkPkg/TlsDxe/TlsImpl.c | 349 ++ NetworkPkg/TlsDxe/TlsImpl.h | 306 ++ NetworkPkg/TlsDxe/TlsProtocol.c | 638 +++ NetworkPkg/Udp4Dxe/ComponentName.c | 429 ++ NetworkPkg/Udp4Dxe/Udp4Driver.c | 584 +++ NetworkPkg/Udp4Dxe/Udp4Driver.h | 148 + NetworkPkg/Udp4Dxe/Udp4Dxe.inf | 65 + NetworkPkg/Udp4Dxe/Udp4Dxe.uni | 17 + NetworkPkg/Udp4Dxe/Udp4DxeExtra.uni | 14 + NetworkPkg/Udp4Dxe/Udp4Impl.c | 1908 ++++++++ NetworkPkg/Udp4Dxe/Udp4Impl.h | 689 +++ NetworkPkg/Udp4Dxe/Udp4Main.c | 902 ++++ NetworkPkg/Udp6Dxe/ComponentName.c | 423 ++ NetworkPkg/Udp6Dxe/Udp6Driver.c | 623 +++ NetworkPkg/Udp6Dxe/Udp6Driver.h | 176 + NetworkPkg/Udp6Dxe/Udp6Dxe.inf | 64 + NetworkPkg/Udp6Dxe/Udp6Dxe.uni | 17 + NetworkPkg/Udp6Dxe/Udp6DxeExtra.uni | 14 + NetworkPkg/Udp6Dxe/Udp6Impl.c | 2003 +++++++++ NetworkPkg/Udp6Dxe/Udp6Impl.h | 648 +++ NetworkPkg/Udp6Dxe/Udp6Main.c | 858 ++++ NetworkPkg/UefiPxeBcDxe/ComponentName.c | 352 ++ NetworkPkg/UefiPxeBcDxe/PxeBcBoot.c | 1256 ++++++ NetworkPkg/UefiPxeBcDxe/PxeBcBoot.h | 94 + NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.c | 1756 ++++++++ NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.h | 365 ++ NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.c | 2370 ++++++++++ NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.h | 269 ++ NetworkPkg/UefiPxeBcDxe/PxeBcDriver.c | 1851 ++++++++ NetworkPkg/UefiPxeBcDxe/PxeBcDriver.h | 175 + NetworkPkg/UefiPxeBcDxe/PxeBcImpl.c | 2421 ++++++++++ NetworkPkg/UefiPxeBcDxe/PxeBcImpl.h | 228 + NetworkPkg/UefiPxeBcDxe/PxeBcMtftp.c | 1187 +++++ NetworkPkg/UefiPxeBcDxe/PxeBcMtftp.h | 138 + NetworkPkg/UefiPxeBcDxe/PxeBcSupport.c | 1538 +++++++ NetworkPkg/UefiPxeBcDxe/PxeBcSupport.h | 513 +++ NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.inf | 109 + NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.uni | 18 + NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxeExtra.uni | 14 + NetworkPkg/VlanConfigDxe/ComponentName.c | 164 + NetworkPkg/VlanConfigDxe/VlanConfig.vfr | 72 + NetworkPkg/VlanConfigDxe/VlanConfigDriver.c | 299 ++ NetworkPkg/VlanConfigDxe/VlanConfigDxe.inf | 67 + NetworkPkg/VlanConfigDxe/VlanConfigDxe.uni | 17 + .../VlanConfigDxe/VlanConfigDxeExtra.uni | 14 + NetworkPkg/VlanConfigDxe/VlanConfigImpl.c | 664 +++ NetworkPkg/VlanConfigDxe/VlanConfigImpl.h | 381 ++ NetworkPkg/VlanConfigDxe/VlanConfigNvData.h | 40 + .../VlanConfigDxe/VlanConfigStrings.uni | 31 + .../WifiConnectionManagerDxe/EapContext.h | 22 + .../WifiConnectionManagerDxe.inf | 81 + .../WifiConnectionManagerDxe.vfr | 347 ++ .../WifiConnectionManagerDxeStrings.uni | 103 + .../WifiConnectionMgrComponentName.c | 185 + .../WifiConnectionMgrComponentName.h | 93 + .../WifiConnectionMgrConfig.h | 68 + .../WifiConnectionMgrConfigHii.h | 29 + .../WifiConnectionMgrConfigNVDataStruct.h | 152 + .../WifiConnectionMgrDriver.c | 616 +++ .../WifiConnectionMgrDriverBinding.h | 142 + .../WifiConnectionMgrDxe.h | 321 ++ .../WifiConnectionMgrFileUtil.c | 303 ++ .../WifiConnectionMgrFileUtil.h | 71 + .../WifiConnectionMgrHiiConfigAccess.c | 2017 +++++++++ .../WifiConnectionMgrHiiConfigAccess.h | 241 + .../WifiConnectionMgrImpl.c | 1285 ++++++ .../WifiConnectionMgrImpl.h | 99 + .../WifiConnectionMgrMisc.c | 744 ++++ .../WifiConnectionMgrMisc.h | 262 ++ ShellPkg/Include/Library/ShellLib.h | 4 +- .../UefiHandleParsingLib.c | 17 - .../UefiHandleParsingLib.inf | 17 - .../UefiHandleParsingLib.uni | 17 - .../Library/UefiShellDebug1CommandsLib/Dmem.c | 6 +- .../UefiShellDebug1CommandsLib.inf | 1 - .../UefiShellNetwork1CommandsLib.inf | 1 + .../UefiShellNetwork2CommandsLib.inf | 1 + cbuild.bat | 5 +- rEFIt_UEFI/refit.inf | 1 + 441 files changed, 200955 insertions(+), 2666 deletions(-) delete mode 100644 MdePkg/Include/Uefi/UefiBaseType.h.bak delete mode 100644 MdePkg/Library/BaseLib/X64/CpuId.S delete mode 100644 MdePkg/Library/BaseLib/X64/CpuIdEx.S delete mode 100644 MdePkg/Library/BaseLib/X64/DisableCache.S delete mode 100644 MdePkg/Library/BaseLib/X64/DisablePaging64.S delete mode 100644 MdePkg/Library/BaseLib/X64/EnableCache.S delete mode 100644 MdePkg/Library/BaseLib/X64/EnableDisableInterrupts.S delete mode 100644 MdePkg/Library/BaseLib/X64/LongJump.S delete mode 100644 MdePkg/Library/BaseLib/X64/RdRand.S delete mode 100644 MdePkg/Library/BaseLib/X64/SetJump.S delete mode 100644 MdePkg/Library/BaseLib/X64/SwitchStack.S delete mode 100644 MdePkg/Library/BaseLib/X64/Thunk16.S delete mode 100644 MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c.bak create mode 100644 NetworkPkg/Application/VConfig/VConfig.c create mode 100644 NetworkPkg/Application/VConfig/VConfig.inf create mode 100644 NetworkPkg/Application/VConfig/VConfig.uni create mode 100644 NetworkPkg/Application/VConfig/VConfigExtra.uni create mode 100644 NetworkPkg/Application/VConfig/VConfigStrings.uni create mode 100644 NetworkPkg/ArpDxe/ArpDriver.c create mode 100644 NetworkPkg/ArpDxe/ArpDriver.h create mode 100644 NetworkPkg/ArpDxe/ArpDxe.inf create mode 100644 NetworkPkg/ArpDxe/ArpDxe.uni create mode 100644 NetworkPkg/ArpDxe/ArpDxeExtra.uni create mode 100644 NetworkPkg/ArpDxe/ArpImpl.c create mode 100644 NetworkPkg/ArpDxe/ArpImpl.h create mode 100644 NetworkPkg/ArpDxe/ArpMain.c create mode 100644 NetworkPkg/ArpDxe/ComponentName.c create mode 100644 NetworkPkg/Dhcp4Dxe/ComponentName.c create mode 100644 NetworkPkg/Dhcp4Dxe/Dhcp4Driver.c create mode 100644 NetworkPkg/Dhcp4Dxe/Dhcp4Driver.h create mode 100644 NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.inf create mode 100644 NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.uni create mode 100644 NetworkPkg/Dhcp4Dxe/Dhcp4DxeExtra.uni create mode 100644 NetworkPkg/Dhcp4Dxe/Dhcp4Impl.c create mode 100644 NetworkPkg/Dhcp4Dxe/Dhcp4Impl.h create mode 100644 NetworkPkg/Dhcp4Dxe/Dhcp4Io.c create mode 100644 NetworkPkg/Dhcp4Dxe/Dhcp4Io.h create mode 100644 NetworkPkg/Dhcp4Dxe/Dhcp4Option.c create mode 100644 NetworkPkg/Dhcp4Dxe/Dhcp4Option.h create mode 100644 NetworkPkg/Dhcp6Dxe/ComponentName.c create mode 100644 NetworkPkg/Dhcp6Dxe/Dhcp6Driver.c create mode 100644 NetworkPkg/Dhcp6Dxe/Dhcp6Driver.h create mode 100644 NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.inf create mode 100644 NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.uni create mode 100644 NetworkPkg/Dhcp6Dxe/Dhcp6DxeExtra.uni create mode 100644 NetworkPkg/Dhcp6Dxe/Dhcp6Impl.c create mode 100644 NetworkPkg/Dhcp6Dxe/Dhcp6Impl.h create mode 100644 NetworkPkg/Dhcp6Dxe/Dhcp6Io.c create mode 100644 NetworkPkg/Dhcp6Dxe/Dhcp6Io.h create mode 100644 NetworkPkg/Dhcp6Dxe/Dhcp6Utility.c create mode 100644 NetworkPkg/Dhcp6Dxe/Dhcp6Utility.h create mode 100644 NetworkPkg/DnsDxe/ComponentName.c create mode 100644 NetworkPkg/DnsDxe/DnsDhcp.c create mode 100644 NetworkPkg/DnsDxe/DnsDhcp.h create mode 100644 NetworkPkg/DnsDxe/DnsDriver.c create mode 100644 NetworkPkg/DnsDxe/DnsDriver.h create mode 100644 NetworkPkg/DnsDxe/DnsDxe.inf create mode 100644 NetworkPkg/DnsDxe/DnsDxe.uni create mode 100644 NetworkPkg/DnsDxe/DnsDxeExtra.uni create mode 100644 NetworkPkg/DnsDxe/DnsImpl.c create mode 100644 NetworkPkg/DnsDxe/DnsImpl.h create mode 100644 NetworkPkg/DnsDxe/DnsProtocol.c create mode 100644 NetworkPkg/DpcDxe/Dpc.c create mode 100644 NetworkPkg/DpcDxe/Dpc.h create mode 100644 NetworkPkg/DpcDxe/DpcDxe.inf create mode 100644 NetworkPkg/DpcDxe/DpcDxe.uni create mode 100644 NetworkPkg/DpcDxe/DpcDxeExtra.uni create mode 100644 NetworkPkg/HttpBootDxe/HttpBootClient.c create mode 100644 NetworkPkg/HttpBootDxe/HttpBootClient.h create mode 100644 NetworkPkg/HttpBootDxe/HttpBootComponentName.c create mode 100644 NetworkPkg/HttpBootDxe/HttpBootComponentName.h create mode 100644 NetworkPkg/HttpBootDxe/HttpBootConfig.c create mode 100644 NetworkPkg/HttpBootDxe/HttpBootConfig.h create mode 100644 NetworkPkg/HttpBootDxe/HttpBootConfigNVDataStruc.h create mode 100644 NetworkPkg/HttpBootDxe/HttpBootConfigStrings.uni create mode 100644 NetworkPkg/HttpBootDxe/HttpBootConfigVfr.vfr create mode 100644 NetworkPkg/HttpBootDxe/HttpBootDhcp4.c create mode 100644 NetworkPkg/HttpBootDxe/HttpBootDhcp4.h create mode 100644 NetworkPkg/HttpBootDxe/HttpBootDhcp6.c create mode 100644 NetworkPkg/HttpBootDxe/HttpBootDhcp6.h create mode 100644 NetworkPkg/HttpBootDxe/HttpBootDxe.c create mode 100644 NetworkPkg/HttpBootDxe/HttpBootDxe.h create mode 100644 NetworkPkg/HttpBootDxe/HttpBootDxe.inf create mode 100644 NetworkPkg/HttpBootDxe/HttpBootDxe.uni create mode 100644 NetworkPkg/HttpBootDxe/HttpBootDxeExtra.uni create mode 100644 NetworkPkg/HttpBootDxe/HttpBootImpl.c create mode 100644 NetworkPkg/HttpBootDxe/HttpBootImpl.h create mode 100644 NetworkPkg/HttpBootDxe/HttpBootSupport.c create mode 100644 NetworkPkg/HttpBootDxe/HttpBootSupport.h create mode 100644 NetworkPkg/HttpDxe/ComponentName.c create mode 100644 NetworkPkg/HttpDxe/ComponentName.h create mode 100644 NetworkPkg/HttpDxe/HttpDns.c create mode 100644 NetworkPkg/HttpDxe/HttpDns.h create mode 100644 NetworkPkg/HttpDxe/HttpDriver.c create mode 100644 NetworkPkg/HttpDxe/HttpDriver.h create mode 100644 NetworkPkg/HttpDxe/HttpDxe.inf create mode 100644 NetworkPkg/HttpDxe/HttpDxe.uni create mode 100644 NetworkPkg/HttpDxe/HttpDxeExtra.uni create mode 100644 NetworkPkg/HttpDxe/HttpImpl.c create mode 100644 NetworkPkg/HttpDxe/HttpImpl.h create mode 100644 NetworkPkg/HttpDxe/HttpProto.c create mode 100644 NetworkPkg/HttpDxe/HttpProto.h create mode 100644 NetworkPkg/HttpDxe/HttpsSupport.c create mode 100644 NetworkPkg/HttpDxe/HttpsSupport.h create mode 100644 NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.c create mode 100644 NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.h create mode 100644 NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.inf create mode 100644 NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.uni create mode 100644 NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxeExtra.uni create mode 100644 NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesProtocol.c create mode 100644 NetworkPkg/IScsiDxe/ComponentName.c create mode 100644 NetworkPkg/IScsiDxe/IScsiAuthenticationInfo.c create mode 100644 NetworkPkg/IScsiDxe/IScsiCHAP.c create mode 100644 NetworkPkg/IScsiDxe/IScsiCHAP.h create mode 100644 NetworkPkg/IScsiDxe/IScsiConfig.c create mode 100644 NetworkPkg/IScsiDxe/IScsiConfig.h create mode 100644 NetworkPkg/IScsiDxe/IScsiConfigNVDataStruc.h create mode 100644 NetworkPkg/IScsiDxe/IScsiConfigStrings.uni create mode 100644 NetworkPkg/IScsiDxe/IScsiConfigVfr.vfr create mode 100644 NetworkPkg/IScsiDxe/IScsiDhcp.c create mode 100644 NetworkPkg/IScsiDxe/IScsiDhcp.h create mode 100644 NetworkPkg/IScsiDxe/IScsiDhcp6.c create mode 100644 NetworkPkg/IScsiDxe/IScsiDhcp6.h create mode 100644 NetworkPkg/IScsiDxe/IScsiDns.c create mode 100644 NetworkPkg/IScsiDxe/IScsiDns.h create mode 100644 NetworkPkg/IScsiDxe/IScsiDriver.c create mode 100644 NetworkPkg/IScsiDxe/IScsiDriver.h create mode 100644 NetworkPkg/IScsiDxe/IScsiDxe.inf create mode 100644 NetworkPkg/IScsiDxe/IScsiDxe.uni create mode 100644 NetworkPkg/IScsiDxe/IScsiDxeExtra.uni create mode 100644 NetworkPkg/IScsiDxe/IScsiExtScsiPassThru.c create mode 100644 NetworkPkg/IScsiDxe/IScsiIbft.c create mode 100644 NetworkPkg/IScsiDxe/IScsiIbft.h create mode 100644 NetworkPkg/IScsiDxe/IScsiImpl.h create mode 100644 NetworkPkg/IScsiDxe/IScsiInitiatorName.c create mode 100644 NetworkPkg/IScsiDxe/IScsiMisc.c create mode 100644 NetworkPkg/IScsiDxe/IScsiMisc.h create mode 100644 NetworkPkg/IScsiDxe/IScsiProto.c create mode 100644 NetworkPkg/IScsiDxe/IScsiProto.h create mode 100644 NetworkPkg/Include/Guid/HttpBootConfigHii.h create mode 100644 NetworkPkg/Include/Guid/HttpTlsCipherList.h create mode 100644 NetworkPkg/Include/Guid/IScsiConfigHii.h create mode 100644 NetworkPkg/Include/Guid/Ip6ConfigHii.h create mode 100644 NetworkPkg/Include/Guid/TlsAuthConfigHii.h create mode 100644 NetworkPkg/Include/Guid/TlsAuthentication.h create mode 100644 NetworkPkg/Include/Guid/WifiConnectionManagerConfigHii.h create mode 100644 NetworkPkg/Include/Library/DpcLib.h create mode 100644 NetworkPkg/Include/Library/HttpLib.h create mode 100644 NetworkPkg/Include/Library/IpIoLib.h create mode 100644 NetworkPkg/Include/Library/NetLib.h create mode 100644 NetworkPkg/Include/Library/TcpIoLib.h create mode 100644 NetworkPkg/Include/Library/UdpIoLib.h create mode 100644 NetworkPkg/Include/Protocol/Dpc.h create mode 100644 NetworkPkg/Ip4Dxe/ComponentName.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Common.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Common.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4Config2.vfr create mode 100644 NetworkPkg/Ip4Dxe/Ip4Config2Impl.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Config2Impl.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4Config2Nv.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Config2Nv.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4Driver.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Driver.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4Dxe.inf create mode 100644 NetworkPkg/Ip4Dxe/Ip4Dxe.uni create mode 100644 NetworkPkg/Ip4Dxe/Ip4DxeExtra.uni create mode 100644 NetworkPkg/Ip4Dxe/Ip4DxeStrings.uni create mode 100644 NetworkPkg/Ip4Dxe/Ip4Icmp.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Icmp.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4If.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4If.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4Igmp.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Igmp.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4Impl.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Impl.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4Input.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Input.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4NvData.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4Option.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Option.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4Output.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Output.h create mode 100644 NetworkPkg/Ip4Dxe/Ip4Route.c create mode 100644 NetworkPkg/Ip4Dxe/Ip4Route.h create mode 100644 NetworkPkg/Ip6Dxe/ComponentName.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6Common.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6Common.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6Config.vfr create mode 100644 NetworkPkg/Ip6Dxe/Ip6ConfigImpl.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6ConfigImpl.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6ConfigNv.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6ConfigNv.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6Driver.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6Driver.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6Dxe.inf create mode 100644 NetworkPkg/Ip6Dxe/Ip6Dxe.uni create mode 100644 NetworkPkg/Ip6Dxe/Ip6DxeExtra.uni create mode 100644 NetworkPkg/Ip6Dxe/Ip6DxeStrings.uni create mode 100644 NetworkPkg/Ip6Dxe/Ip6Icmp.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6Icmp.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6If.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6If.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6Impl.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6Impl.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6Input.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6Input.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6Mld.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6Mld.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6Nd.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6Nd.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6NvData.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6Option.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6Option.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6Output.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6Output.h create mode 100644 NetworkPkg/Ip6Dxe/Ip6Route.c create mode 100644 NetworkPkg/Ip6Dxe/Ip6Route.h create mode 100644 NetworkPkg/Library/DxeDpcLib/DpcLib.c create mode 100644 NetworkPkg/Library/DxeDpcLib/DxeDpcLib.inf create mode 100644 NetworkPkg/Library/DxeDpcLib/DxeDpcLib.uni create mode 100644 NetworkPkg/Library/DxeHttpLib/DxeHttpLib.c create mode 100644 NetworkPkg/Library/DxeHttpLib/DxeHttpLib.h create mode 100644 NetworkPkg/Library/DxeHttpLib/DxeHttpLib.inf create mode 100644 NetworkPkg/Library/DxeHttpLib/DxeHttpLib.uni create mode 100644 NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.c create mode 100644 NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.inf create mode 100644 NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.uni create mode 100644 NetworkPkg/Library/DxeNetLib/DxeNetLib.c create mode 100644 NetworkPkg/Library/DxeNetLib/DxeNetLib.inf create mode 100644 NetworkPkg/Library/DxeNetLib/DxeNetLib.uni create mode 100644 NetworkPkg/Library/DxeNetLib/NetBuffer.c create mode 100644 NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.c create mode 100644 NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.inf create mode 100644 NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.uni create mode 100644 NetworkPkg/Library/DxeUdpIoLib/DxeUdpIoLib.c create mode 100644 NetworkPkg/Library/DxeUdpIoLib/DxeUdpIoLib.inf create mode 100644 NetworkPkg/Library/DxeUdpIoLib/DxeUpdIoLib.uni create mode 100644 NetworkPkg/MnpDxe/ComponentName.c create mode 100644 NetworkPkg/MnpDxe/ComponentName.h create mode 100644 NetworkPkg/MnpDxe/MnpConfig.c create mode 100644 NetworkPkg/MnpDxe/MnpDriver.c create mode 100644 NetworkPkg/MnpDxe/MnpDriver.h create mode 100644 NetworkPkg/MnpDxe/MnpDxe.inf create mode 100644 NetworkPkg/MnpDxe/MnpDxe.uni create mode 100644 NetworkPkg/MnpDxe/MnpDxeExtra.uni create mode 100644 NetworkPkg/MnpDxe/MnpImpl.h create mode 100644 NetworkPkg/MnpDxe/MnpIo.c create mode 100644 NetworkPkg/MnpDxe/MnpMain.c create mode 100644 NetworkPkg/MnpDxe/MnpVlan.c create mode 100644 NetworkPkg/MnpDxe/MnpVlan.h create mode 100644 NetworkPkg/Mtftp4Dxe/ComponentName.c create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Driver.c create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Driver.h create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.inf create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.uni create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4DxeExtra.uni create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Impl.c create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Impl.h create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Option.c create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Option.h create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Rrq.c create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Support.c create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Support.h create mode 100644 NetworkPkg/Mtftp4Dxe/Mtftp4Wrq.c create mode 100644 NetworkPkg/Mtftp6Dxe/ComponentName.c create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Driver.c create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Driver.h create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.inf create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.uni create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6DxeExtra.uni create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Impl.c create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Impl.h create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Option.c create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Option.h create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Rrq.c create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Support.c create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Support.h create mode 100644 NetworkPkg/Mtftp6Dxe/Mtftp6Wrq.c create mode 100644 NetworkPkg/Network.dsc.inc create mode 100644 NetworkPkg/Network.fdf.inc create mode 100644 NetworkPkg/NetworkComponents.dsc.inc create mode 100644 NetworkPkg/NetworkDefines.dsc.inc create mode 100644 NetworkPkg/NetworkLibs.dsc.inc create mode 100644 NetworkPkg/NetworkPcds.dsc.inc create mode 100644 NetworkPkg/NetworkPkg.dec create mode 100644 NetworkPkg/NetworkPkg.dsc create mode 100644 NetworkPkg/NetworkPkg.uni create mode 100644 NetworkPkg/NetworkPkgExtra.uni create mode 100644 NetworkPkg/SnpDxe/Callback.c create mode 100644 NetworkPkg/SnpDxe/ComponentName.c create mode 100644 NetworkPkg/SnpDxe/Get_status.c create mode 100644 NetworkPkg/SnpDxe/Initialize.c create mode 100644 NetworkPkg/SnpDxe/Mcast_ip_to_mac.c create mode 100644 NetworkPkg/SnpDxe/Nvdata.c create mode 100644 NetworkPkg/SnpDxe/Receive.c create mode 100644 NetworkPkg/SnpDxe/Receive_filters.c create mode 100644 NetworkPkg/SnpDxe/Reset.c create mode 100644 NetworkPkg/SnpDxe/Shutdown.c create mode 100644 NetworkPkg/SnpDxe/Snp.c create mode 100644 NetworkPkg/SnpDxe/Snp.h create mode 100644 NetworkPkg/SnpDxe/SnpDxe.inf create mode 100644 NetworkPkg/SnpDxe/SnpDxe.uni create mode 100644 NetworkPkg/SnpDxe/SnpDxeExtra.uni create mode 100644 NetworkPkg/SnpDxe/Start.c create mode 100644 NetworkPkg/SnpDxe/Station_address.c create mode 100644 NetworkPkg/SnpDxe/Statistics.c create mode 100644 NetworkPkg/SnpDxe/Stop.c create mode 100644 NetworkPkg/SnpDxe/Transmit.c create mode 100644 NetworkPkg/SnpDxe/WaitForPacket.c create mode 100644 NetworkPkg/TcpDxe/ComponentName.c create mode 100644 NetworkPkg/TcpDxe/SockImpl.c create mode 100644 NetworkPkg/TcpDxe/SockImpl.h create mode 100644 NetworkPkg/TcpDxe/SockInterface.c create mode 100644 NetworkPkg/TcpDxe/Socket.h create mode 100644 NetworkPkg/TcpDxe/TcpDispatcher.c create mode 100644 NetworkPkg/TcpDxe/TcpDriver.c create mode 100644 NetworkPkg/TcpDxe/TcpDriver.h create mode 100644 NetworkPkg/TcpDxe/TcpDxe.inf create mode 100644 NetworkPkg/TcpDxe/TcpDxe.uni create mode 100644 NetworkPkg/TcpDxe/TcpDxeExtra.uni create mode 100644 NetworkPkg/TcpDxe/TcpFunc.h create mode 100644 NetworkPkg/TcpDxe/TcpInput.c create mode 100644 NetworkPkg/TcpDxe/TcpIo.c create mode 100644 NetworkPkg/TcpDxe/TcpMain.c create mode 100644 NetworkPkg/TcpDxe/TcpMain.h create mode 100644 NetworkPkg/TcpDxe/TcpMisc.c create mode 100644 NetworkPkg/TcpDxe/TcpOption.c create mode 100644 NetworkPkg/TcpDxe/TcpOption.h create mode 100644 NetworkPkg/TcpDxe/TcpOutput.c create mode 100644 NetworkPkg/TcpDxe/TcpProto.h create mode 100644 NetworkPkg/TcpDxe/TcpTimer.c create mode 100644 NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.c create mode 100644 NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.inf create mode 100644 NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.uni create mode 100644 NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxeExtra.uni create mode 100644 NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxeStrings.uni create mode 100644 NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c create mode 100644 NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.h create mode 100644 NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigNvData.h create mode 100644 NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigVfr.vfr create mode 100644 NetworkPkg/TlsDxe/TlsConfigProtocol.c create mode 100644 NetworkPkg/TlsDxe/TlsDriver.c create mode 100644 NetworkPkg/TlsDxe/TlsDriver.h create mode 100644 NetworkPkg/TlsDxe/TlsDxe.inf create mode 100644 NetworkPkg/TlsDxe/TlsDxe.uni create mode 100644 NetworkPkg/TlsDxe/TlsDxeExtra.uni create mode 100644 NetworkPkg/TlsDxe/TlsImpl.c create mode 100644 NetworkPkg/TlsDxe/TlsImpl.h create mode 100644 NetworkPkg/TlsDxe/TlsProtocol.c create mode 100644 NetworkPkg/Udp4Dxe/ComponentName.c create mode 100644 NetworkPkg/Udp4Dxe/Udp4Driver.c create mode 100644 NetworkPkg/Udp4Dxe/Udp4Driver.h create mode 100644 NetworkPkg/Udp4Dxe/Udp4Dxe.inf create mode 100644 NetworkPkg/Udp4Dxe/Udp4Dxe.uni create mode 100644 NetworkPkg/Udp4Dxe/Udp4DxeExtra.uni create mode 100644 NetworkPkg/Udp4Dxe/Udp4Impl.c create mode 100644 NetworkPkg/Udp4Dxe/Udp4Impl.h create mode 100644 NetworkPkg/Udp4Dxe/Udp4Main.c create mode 100644 NetworkPkg/Udp6Dxe/ComponentName.c create mode 100644 NetworkPkg/Udp6Dxe/Udp6Driver.c create mode 100644 NetworkPkg/Udp6Dxe/Udp6Driver.h create mode 100644 NetworkPkg/Udp6Dxe/Udp6Dxe.inf create mode 100644 NetworkPkg/Udp6Dxe/Udp6Dxe.uni create mode 100644 NetworkPkg/Udp6Dxe/Udp6DxeExtra.uni create mode 100644 NetworkPkg/Udp6Dxe/Udp6Impl.c create mode 100644 NetworkPkg/Udp6Dxe/Udp6Impl.h create mode 100644 NetworkPkg/Udp6Dxe/Udp6Main.c create mode 100644 NetworkPkg/UefiPxeBcDxe/ComponentName.c create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcBoot.c create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcBoot.h create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.c create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.h create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.c create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.h create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcDriver.c create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcDriver.h create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcImpl.c create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcImpl.h create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcMtftp.c create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcMtftp.h create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcSupport.c create mode 100644 NetworkPkg/UefiPxeBcDxe/PxeBcSupport.h create mode 100644 NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.inf create mode 100644 NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.uni create mode 100644 NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxeExtra.uni create mode 100644 NetworkPkg/VlanConfigDxe/ComponentName.c create mode 100644 NetworkPkg/VlanConfigDxe/VlanConfig.vfr create mode 100644 NetworkPkg/VlanConfigDxe/VlanConfigDriver.c create mode 100644 NetworkPkg/VlanConfigDxe/VlanConfigDxe.inf create mode 100644 NetworkPkg/VlanConfigDxe/VlanConfigDxe.uni create mode 100644 NetworkPkg/VlanConfigDxe/VlanConfigDxeExtra.uni create mode 100644 NetworkPkg/VlanConfigDxe/VlanConfigImpl.c create mode 100644 NetworkPkg/VlanConfigDxe/VlanConfigImpl.h create mode 100644 NetworkPkg/VlanConfigDxe/VlanConfigNvData.h create mode 100644 NetworkPkg/VlanConfigDxe/VlanConfigStrings.uni create mode 100644 NetworkPkg/WifiConnectionManagerDxe/EapContext.h create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxe.inf create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxe.vfr create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxeStrings.uni create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrComponentName.c create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrComponentName.h create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfig.h create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfigHii.h create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfigNVDataStruct.h create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDriver.c create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDriverBinding.h create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDxe.h create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrFileUtil.c create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrFileUtil.h create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrHiiConfigAccess.c create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrHiiConfigAccess.h create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrImpl.c create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrImpl.h create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrMisc.c create mode 100644 NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrMisc.h diff --git a/.gitignore b/.gitignore index 421f5895a..5f804e639 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .DS_Store *.swp *.lock +*.bak *~ # /CloverPackage/ diff --git a/BaseTools/Source/C/GNUmakefile b/BaseTools/Source/C/GNUmakefile index 8b8dc03ae..678338028 100644 --- a/BaseTools/Source/C/GNUmakefile +++ b/BaseTools/Source/C/GNUmakefile @@ -27,8 +27,9 @@ ifndef HOST_ARCH endif ifneq (,$(findstring aarch64,$(uname_m))) HOST_ARCH=AARCH64 - endif - ifneq (,$(findstring arm,$(uname_m))) + else ifneq (,$(findstring arm64,$(uname_m))) + HOST_ARCH=AARCH64 + else ifneq (,$(findstring arm,$(uname_m))) HOST_ARCH=ARM endif ifndef HOST_ARCH diff --git a/BaseTools/Source/C/Makefile b/BaseTools/Source/C/Makefile index c696ec10f..718a7f57f 100644 --- a/BaseTools/Source/C/Makefile +++ b/BaseTools/Source/C/Makefile @@ -69,8 +69,8 @@ clean: .PHONY: cleanall cleanall: - @if defined PYTHON_COMMAND $(PYTHON_COMMAND) $(PYTHON_COMMAND) Makefiles\NmakeSubdirs.py cleanall $(LIBRARIES) $(APPLICATIONS) - @if not defined PYTHON_COMMAND $(PYTHON_HOME)\python.exe $(PYTHON_COMMAND) Makefiles\NmakeSubdirs.py cleanall $(LIBRARIES) $(APPLICATIONS) + @if defined PYTHON_COMMAND $(PYTHON_COMMAND) Makefiles\NmakeSubdirs.py cleanall $(LIBRARIES) $(APPLICATIONS) + @if not defined PYTHON_COMMAND $(PYTHON_HOME)\python.exe Makefiles\NmakeSubdirs.py cleanall $(LIBRARIES) $(APPLICATIONS) !INCLUDE Makefiles\ms.rule diff --git a/Clover.dsc b/Clover.dsc index 0d2c82a22..8fab438e3 100644 --- a/Clover.dsc +++ b/Clover.dsc @@ -104,7 +104,7 @@ SecurityManagementLib|MdeModulePkg/Library/DxeSecurityManagementLib/DxeSecurityManagementLib.inf CapsuleLib|MdeModulePkg/Library/DxeCapsuleLibNull/DxeCapsuleLibNull.inf #PeCoffExtraActionLib|MdePkg/Library/BasePeCoffExtraActionLibNull/BasePeCoffExtraActionLibNull.inf - NetLib|MdeModulePkg/Library/DxeNetLib/DxeNetLib.inf + NetLib|NetworkPkg/Library/DxeNetLib/DxeNetLib.inf # # Platform # diff --git a/CloverEFI/OsxDxeCore/DxeMain.inf b/CloverEFI/OsxDxeCore/DxeMain.inf index fa553fe6a..538f647da 100644 --- a/CloverEFI/OsxDxeCore/DxeMain.inf +++ b/CloverEFI/OsxDxeCore/DxeMain.inf @@ -153,7 +153,7 @@ gEfiSmmBase2ProtocolGuid ## SOMETIMES_CONSUMES [FeaturePcd] - gEfiMdeModulePkgTokenSpaceGuid.PcdFrameworkCompatibilitySupport ## CONSUMES +# gEfiMdeModulePkgTokenSpaceGuid.PcdFrameworkCompatibilitySupport ## CONSUMES [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdLoadFixAddressBootTimeCodePageNumber ## SOMETIMES_CONSUMES diff --git a/CloverEFI/OsxDxeCore/Image/Image.c b/CloverEFI/OsxDxeCore/Image/Image.c index efdf86707..bc8663a8a 100644 --- a/CloverEFI/OsxDxeCore/Image/Image.c +++ b/CloverEFI/OsxDxeCore/Image/Image.c @@ -190,7 +190,7 @@ CoreInitializeImageServices ( mDxeCoreImageMachineType = PeCoffLoaderGetMachineType (Image->Info.ImageBase); gDxeCoreImageHandle = Image->Handle; gDxeCoreLoadedImage = &Image->Info; - +/* if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) { // // Export DXE Core PE Loader functionality for backward compatibility. @@ -202,7 +202,7 @@ CoreInitializeImageServices ( &mLoadPe32PrivateData.Pe32Image ); } - +*/ return Status; } diff --git a/CloverEFI/OsxEfiLdr/EfiLdr.inf b/CloverEFI/OsxEfiLdr/EfiLdr.inf index 60a133c44..d6e8adb25 100644 --- a/CloverEFI/OsxEfiLdr/EfiLdr.inf +++ b/CloverEFI/OsxEfiLdr/EfiLdr.inf @@ -26,8 +26,8 @@ [Packages] CloverPkg.dec MdePkg/MdePkg.dec -# MdeModulePkg/MdeModulePkg.dec - IntelFrameworkModulePkg/IntelFrameworkModulePkg.dec + MdeModulePkg/MdeModulePkg.dec +# IntelFrameworkModulePkg/IntelFrameworkModulePkg.dec [LibraryClasses] BaseLib diff --git a/MdePkg/Include/IndustryStandard/Pci22.h b/MdePkg/Include/IndustryStandard/Pci22.h index 52ded2617..5fd054c4c 100644 --- a/MdePkg/Include/IndustryStandard/Pci22.h +++ b/MdePkg/Include/IndustryStandard/Pci22.h @@ -9,13 +9,7 @@ Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
Copyright (c) 2014 - 2015, Hewlett-Packard Development Company, L.P.
- 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 - http://opensource.org/licenses/bsd-license.php - - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + SPDX-License-Identifier: BSD-2-Clause-Patent **/ @@ -33,74 +27,74 @@ /// Section 6.1, PCI Local Bus Specification, 2.2 /// typedef struct { - UINT16 VendorId; //0 - UINT16 DeviceId; //2 - UINT16 Command; //4 - UINT16 Status; //6 - UINT8 RevisionID; //8 - UINT8 ClassCode[3]; //9 - UINT8 CacheLineSize; //c - UINT8 LatencyTimer; //d - UINT8 HeaderType; //e - UINT8 BIST; //f -} PCI_DEVICE_INDEPENDENT_REGION; //len=0x10 + UINT16 VendorId; + UINT16 DeviceId; + UINT16 Command; + UINT16 Status; + UINT8 RevisionID; + UINT8 ClassCode[3]; + UINT8 CacheLineSize; + UINT8 LatencyTimer; + UINT8 HeaderType; + UINT8 BIST; +} PCI_DEVICE_INDEPENDENT_REGION; /// /// PCI Device header region in PCI Configuration Space /// Section 6.1, PCI Local Bus Specification, 2.2 /// typedef struct { - UINT32 Bar[6]; //0 - UINT32 CISPtr; //18 - UINT16 SubsystemVendorID; //1c - UINT16 SubsystemID; //1e - UINT32 ExpansionRomBar; //20 - UINT8 CapabilityPtr; //24 - UINT8 Reserved1[3]; //25 - UINT32 Reserved2; //28 - UINT8 InterruptLine; //2c - UINT8 InterruptPin; //2d - UINT8 MinGnt; //2e - UINT8 MaxLat; //2f -} PCI_DEVICE_HEADER_TYPE_REGION; //len=0x30 + UINT32 Bar[6]; + UINT32 CISPtr; + UINT16 SubsystemVendorID; + UINT16 SubsystemID; + UINT32 ExpansionRomBar; + UINT8 CapabilityPtr; + UINT8 Reserved1[3]; + UINT32 Reserved2; + UINT8 InterruptLine; + UINT8 InterruptPin; + UINT8 MinGnt; + UINT8 MaxLat; +} PCI_DEVICE_HEADER_TYPE_REGION; /// /// PCI Device Configuration Space /// Section 6.1, PCI Local Bus Specification, 2.2 /// typedef struct { - PCI_DEVICE_INDEPENDENT_REGION Hdr; //0 - PCI_DEVICE_HEADER_TYPE_REGION Device; //10 -} PCI_TYPE00; //len=0x40 + PCI_DEVICE_INDEPENDENT_REGION Hdr; + PCI_DEVICE_HEADER_TYPE_REGION Device; +} PCI_TYPE00; /// /// PCI-PCI Bridge header region in PCI Configuration Space /// Section 3.2, PCI-PCI Bridge Architecture, Version 1.2 /// typedef struct { - UINT32 Bar[2]; //0 - UINT8 PrimaryBus; //8 - UINT8 SecondaryBus; //9 - UINT8 SubordinateBus; //a - UINT8 SecondaryLatencyTimer; //b - UINT8 IoBase; //c - UINT8 IoLimit; //d - UINT16 SecondaryStatus; //e - UINT16 MemoryBase; //10 - UINT16 MemoryLimit; //12 - UINT16 PrefetchableMemoryBase; //14 - UINT16 PrefetchableMemoryLimit; //16 - UINT32 PrefetchableBaseUpper32; //18 - UINT32 PrefetchableLimitUpper32; //1c - UINT16 IoBaseUpper16; //20 - UINT16 IoLimitUpper16; //22 - UINT8 CapabilityPtr; //24 - UINT8 Reserved[3]; //25 - UINT32 ExpansionRomBAR; //28 - UINT8 InterruptLine; //2c - UINT8 InterruptPin; //2d - UINT16 BridgeControl; //2e -} PCI_BRIDGE_CONTROL_REGISTER; //len=0x30 + UINT32 Bar[2]; + UINT8 PrimaryBus; + UINT8 SecondaryBus; + UINT8 SubordinateBus; + UINT8 SecondaryLatencyTimer; + UINT8 IoBase; + UINT8 IoLimit; + UINT16 SecondaryStatus; + UINT16 MemoryBase; + UINT16 MemoryLimit; + UINT16 PrefetchableMemoryBase; + UINT16 PrefetchableMemoryLimit; + UINT32 PrefetchableBaseUpper32; + UINT32 PrefetchableLimitUpper32; + UINT16 IoBaseUpper16; + UINT16 IoLimitUpper16; + UINT8 CapabilityPtr; + UINT8 Reserved[3]; + UINT32 ExpansionRomBAR; + UINT8 InterruptLine; + UINT8 InterruptPin; + UINT16 BridgeControl; +} PCI_BRIDGE_CONTROL_REGISTER; /// /// PCI-to-PCI Bridge Configuration Space @@ -121,26 +115,26 @@ typedef union { /// Section 4.5.1, PC Card Standard. 8.0 /// typedef struct { - UINT32 CardBusSocketReg; ///< Cardus Socket/ExCA Base //0 - UINT8 Cap_Ptr; //4 + UINT32 CardBusSocketReg; ///< Cardus Socket/ExCA Base + UINT8 Cap_Ptr; UINT8 Reserved; - UINT16 SecondaryStatus; ///< Secondary Status //6 - UINT8 PciBusNumber; ///< PCI Bus Number //8 - UINT8 CardBusBusNumber; ///< CardBus Bus Number //9 - UINT8 SubordinateBusNumber; ///< Subordinate Bus Number //a - UINT8 CardBusLatencyTimer; ///< CardBus Latency Timer //b - UINT32 MemoryBase0; ///< Memory Base Register 0 //c - UINT32 MemoryLimit0; ///< Memory Limit Register 0 //10 - UINT32 MemoryBase1; //14 - UINT32 MemoryLimit1; //18 - UINT32 IoBase0; //1c - UINT32 IoLimit0; ///< I/O Base Register 0 //20 - UINT32 IoBase1; ///< I/O Limit Register 0 //24 - UINT32 IoLimit1; //28 - UINT8 InterruptLine; ///< Interrupt Line //2c - UINT8 InterruptPin; ///< Interrupt Pin //2d - UINT16 BridgeControl; ///< Bridge Control //2e -} PCI_CARDBUS_CONTROL_REGISTER; //len=0x30 + UINT16 SecondaryStatus; ///< Secondary Status + UINT8 PciBusNumber; ///< PCI Bus Number + UINT8 CardBusBusNumber; ///< CardBus Bus Number + UINT8 SubordinateBusNumber; ///< Subordinate Bus Number + UINT8 CardBusLatencyTimer; ///< CardBus Latency Timer + UINT32 MemoryBase0; ///< Memory Base Register 0 + UINT32 MemoryLimit0; ///< Memory Limit Register 0 + UINT32 MemoryBase1; + UINT32 MemoryLimit1; + UINT32 IoBase0; + UINT32 IoLimit0; ///< I/O Base Register 0 + UINT32 IoBase1; ///< I/O Limit Register 0 + UINT32 IoLimit1; + UINT8 InterruptLine; ///< Interrupt Line + UINT8 InterruptPin; ///< Interrupt Pin + UINT16 BridgeControl; ///< Bridge Control +} PCI_CARDBUS_CONTROL_REGISTER; // // Definitions of PCI class bytes and manipulation macros. diff --git a/MdePkg/Include/Protocol/Shell.h b/MdePkg/Include/Protocol/Shell.h index d991d96b6..333b98b24 100644 --- a/MdePkg/Include/Protocol/Shell.h +++ b/MdePkg/Include/Protocol/Shell.h @@ -1262,7 +1262,7 @@ extern EFI_GUID gEfiShellProtocolGuid; enum ShellVersion { SHELL_MAJOR_VERSION = 2, - SHELL_MINOR_VERSION = 204 + SHELL_MINOR_VERSION = 205 }; #endif diff --git a/MdePkg/Include/Uefi/UefiBaseType.h b/MdePkg/Include/Uefi/UefiBaseType.h index f8e0b0675..51a6489c5 100644 --- a/MdePkg/Include/Uefi/UefiBaseType.h +++ b/MdePkg/Include/Uefi/UefiBaseType.h @@ -4,7 +4,13 @@ Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
Portions copyright (c) 2011 - 2016, ARM Ltd. All rights reserved.
-SPDX-License-Identifier: BSD-2-Clause-Patent +This program and the accompanying materials are licensed and made available under +the terms and conditions of the BSD License that accompanies this distribution. +The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php. + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ @@ -244,14 +250,14 @@ typedef union { #if defined (MDE_CPU_IA32) #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \ - (((Machine) == EFI_IMAGE_MACHINE_IA32) || ((Machine) == EFI_IMAGE_MACHINE_EBC)) + ((Machine) == EFI_IMAGE_MACHINE_IA32) #define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) ((Machine) == EFI_IMAGE_MACHINE_X64) #elif defined (MDE_CPU_X64) #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \ - (((Machine) == EFI_IMAGE_MACHINE_X64) || ((Machine) == EFI_IMAGE_MACHINE_EBC)) + ((Machine) == EFI_IMAGE_MACHINE_X64) #define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) ((Machine) == EFI_IMAGE_MACHINE_IA32) @@ -264,7 +270,7 @@ typedef union { #elif defined (MDE_CPU_AARCH64) #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \ - (((Machine) == EFI_IMAGE_MACHINE_AARCH64) || ((Machine) == EFI_IMAGE_MACHINE_EBC)) + ((Machine) == EFI_IMAGE_MACHINE_AARCH64) #define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) (FALSE) diff --git a/MdePkg/Include/Uefi/UefiBaseType.h.bak b/MdePkg/Include/Uefi/UefiBaseType.h.bak deleted file mode 100644 index a62f13dd0..000000000 --- a/MdePkg/Include/Uefi/UefiBaseType.h.bak +++ /dev/null @@ -1,285 +0,0 @@ -/** @file - Defines data types and constants introduced in UEFI. - -Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
-Portions copyright (c) 2011 - 2016, ARM Ltd. All rights reserved.
- -SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#ifndef __UEFI_BASETYPE_H__ -#define __UEFI_BASETYPE_H__ - -#include - -// -// Basic data type definitions introduced in UEFI. -// - -/// -/// 128-bit buffer containing a unique identifier value. -/// -typedef GUID EFI_GUID; -/// -/// Function return status for EFI API. -/// -typedef RETURN_STATUS EFI_STATUS; -/// -/// A collection of related interfaces. -/// -typedef VOID *EFI_HANDLE; -/// -/// Handle to an event structure. -/// -typedef VOID *EFI_EVENT; -/// -/// Task priority level. -/// -typedef UINTN EFI_TPL; -/// -/// Logical block address. -/// -typedef UINT64 EFI_LBA; - -/// -/// 64-bit physical memory address. -/// -typedef UINT64 EFI_PHYSICAL_ADDRESS; - -/// -/// 64-bit virtual memory address. -/// -typedef UINT64 EFI_VIRTUAL_ADDRESS; - -/// -/// EFI Time Abstraction: -/// Year: 1900 - 9999 -/// Month: 1 - 12 -/// Day: 1 - 31 -/// Hour: 0 - 23 -/// Minute: 0 - 59 -/// Second: 0 - 59 -/// Nanosecond: 0 - 999,999,999 -/// TimeZone: -1440 to 1440 or 2047 -/// -typedef struct { - UINT16 Year; - UINT8 Month; - UINT8 Day; - UINT8 Hour; - UINT8 Minute; - UINT8 Second; - UINT8 Pad1; - UINT32 Nanosecond; - INT16 TimeZone; - UINT8 Daylight; - UINT8 Pad2; -} EFI_TIME; - - -/// -/// 4-byte buffer. An IPv4 internet protocol address. -/// -typedef IPv4_ADDRESS EFI_IPv4_ADDRESS; - -/// -/// 16-byte buffer. An IPv6 internet protocol address. -/// -typedef IPv6_ADDRESS EFI_IPv6_ADDRESS; - -/// -/// 32-byte buffer containing a network Media Access Control address. -/// -typedef struct { - UINT8 Addr[32]; -} EFI_MAC_ADDRESS; - -/// -/// 16-byte buffer aligned on a 4-byte boundary. -/// An IPv4 or IPv6 internet protocol address. -/// -typedef union { - UINT32 Addr[4]; - EFI_IPv4_ADDRESS v4; - EFI_IPv6_ADDRESS v6; -} EFI_IP_ADDRESS; - - -/// -/// Enumeration of EFI_STATUS. -///@{ -#define EFI_SUCCESS RETURN_SUCCESS -#define EFI_LOAD_ERROR RETURN_LOAD_ERROR -#define EFI_INVALID_PARAMETER RETURN_INVALID_PARAMETER -#define EFI_UNSUPPORTED RETURN_UNSUPPORTED -#define EFI_BAD_BUFFER_SIZE RETURN_BAD_BUFFER_SIZE -#define EFI_BUFFER_TOO_SMALL RETURN_BUFFER_TOO_SMALL -#define EFI_NOT_READY RETURN_NOT_READY -#define EFI_DEVICE_ERROR RETURN_DEVICE_ERROR -#define EFI_WRITE_PROTECTED RETURN_WRITE_PROTECTED -#define EFI_OUT_OF_RESOURCES RETURN_OUT_OF_RESOURCES -#define EFI_VOLUME_CORRUPTED RETURN_VOLUME_CORRUPTED -#define EFI_VOLUME_FULL RETURN_VOLUME_FULL -#define EFI_NO_MEDIA RETURN_NO_MEDIA -#define EFI_MEDIA_CHANGED RETURN_MEDIA_CHANGED -#define EFI_NOT_FOUND RETURN_NOT_FOUND -#define EFI_ACCESS_DENIED RETURN_ACCESS_DENIED -#define EFI_NO_RESPONSE RETURN_NO_RESPONSE -#define EFI_NO_MAPPING RETURN_NO_MAPPING -#define EFI_TIMEOUT RETURN_TIMEOUT -#define EFI_NOT_STARTED RETURN_NOT_STARTED -#define EFI_ALREADY_STARTED RETURN_ALREADY_STARTED -#define EFI_ABORTED RETURN_ABORTED -#define EFI_ICMP_ERROR RETURN_ICMP_ERROR -#define EFI_TFTP_ERROR RETURN_TFTP_ERROR -#define EFI_PROTOCOL_ERROR RETURN_PROTOCOL_ERROR -#define EFI_INCOMPATIBLE_VERSION RETURN_INCOMPATIBLE_VERSION -#define EFI_SECURITY_VIOLATION RETURN_SECURITY_VIOLATION -#define EFI_CRC_ERROR RETURN_CRC_ERROR -#define EFI_END_OF_MEDIA RETURN_END_OF_MEDIA -#define EFI_END_OF_FILE RETURN_END_OF_FILE -#define EFI_INVALID_LANGUAGE RETURN_INVALID_LANGUAGE -#define EFI_COMPROMISED_DATA RETURN_COMPROMISED_DATA -#define EFI_HTTP_ERROR RETURN_HTTP_ERROR - -#define EFI_WARN_UNKNOWN_GLYPH RETURN_WARN_UNKNOWN_GLYPH -#define EFI_WARN_DELETE_FAILURE RETURN_WARN_DELETE_FAILURE -#define EFI_WARN_WRITE_FAILURE RETURN_WARN_WRITE_FAILURE -#define EFI_WARN_BUFFER_TOO_SMALL RETURN_WARN_BUFFER_TOO_SMALL -#define EFI_WARN_STALE_DATA RETURN_WARN_STALE_DATA -#define EFI_WARN_FILE_SYSTEM RETURN_WARN_FILE_SYSTEM -///@} - -/// -/// Define macro to encode the status code. -/// -#define EFIERR(_a) ENCODE_ERROR(_a) - -#define EFI_ERROR(A) RETURN_ERROR(A) - -/// -/// ICMP error definitions -///@{ -#define EFI_NETWORK_UNREACHABLE EFIERR(100) -#define EFI_HOST_UNREACHABLE EFIERR(101) -#define EFI_PROTOCOL_UNREACHABLE EFIERR(102) -#define EFI_PORT_UNREACHABLE EFIERR(103) -///@} - -/// -/// Tcp connection status definitions -///@{ -#define EFI_CONNECTION_FIN EFIERR(104) -#define EFI_CONNECTION_RESET EFIERR(105) -#define EFI_CONNECTION_REFUSED EFIERR(106) -///@} - -// -// The EFI memory allocation functions work in units of EFI_PAGEs that are -// 4KB. This should in no way be confused with the page size of the processor. -// An EFI_PAGE is just the quanta of memory in EFI. -// -#define EFI_PAGE_SIZE SIZE_4KB -#define EFI_PAGE_MASK 0xFFF -#define EFI_PAGE_SHIFT 12 - -/** - Macro that converts a size, in bytes, to a number of EFI_PAGESs. - - @param Size A size in bytes. This parameter is assumed to be type UINTN. - Passing in a parameter that is larger than UINTN may produce - unexpected results. - - @return The number of EFI_PAGESs associated with the number of bytes specified - by Size. - -**/ -#define EFI_SIZE_TO_PAGES(Size) (((Size) >> EFI_PAGE_SHIFT) + (((Size) & EFI_PAGE_MASK) ? 1 : 0)) - -/** - Macro that converts a number of EFI_PAGEs to a size in bytes. - - @param Pages The number of EFI_PAGES. This parameter is assumed to be - type UINTN. Passing in a parameter that is larger than - UINTN may produce unexpected results. - - @return The number of bytes associated with the number of EFI_PAGEs specified - by Pages. - -**/ -#define EFI_PAGES_TO_SIZE(Pages) ((Pages) << EFI_PAGE_SHIFT) - -/// -/// PE32+ Machine type for IA32 UEFI images. -/// -#define EFI_IMAGE_MACHINE_IA32 0x014C - -/// -/// PE32+ Machine type for IA64 UEFI images. -/// -#define EFI_IMAGE_MACHINE_IA64 0x0200 - -/// -/// PE32+ Machine type for EBC UEFI images. -/// -#define EFI_IMAGE_MACHINE_EBC 0x0EBC - -/// -/// PE32+ Machine type for X64 UEFI images. -/// -#define EFI_IMAGE_MACHINE_X64 0x8664 - -/// -/// PE32+ Machine type for ARM mixed ARM and Thumb/Thumb2 images. -/// -#define EFI_IMAGE_MACHINE_ARMTHUMB_MIXED 0x01C2 - -/// -/// PE32+ Machine type for AARCH64 A64 images. -/// -#define EFI_IMAGE_MACHINE_AARCH64 0xAA64 - - -#if defined (MDE_CPU_IA32) - -#define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \ - ((Machine) == EFI_IMAGE_MACHINE_IA32) - -#define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) ((Machine) == EFI_IMAGE_MACHINE_X64) - -#elif defined (MDE_CPU_X64) - -#define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \ - ((Machine) == EFI_IMAGE_MACHINE_X64) - -#define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) ((Machine) == EFI_IMAGE_MACHINE_IA32) - -#elif defined (MDE_CPU_ARM) - -#define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) ((Machine) == EFI_IMAGE_MACHINE_ARMTHUMB_MIXED) - -#define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) (FALSE) - -#elif defined (MDE_CPU_AARCH64) - -#define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \ - ((Machine) == EFI_IMAGE_MACHINE_AARCH64) - -#define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) (FALSE) - -#elif defined (MDE_CPU_EBC) - -/// -/// This is just to make sure you can cross compile with the EBC compiler. -/// It does not make sense to have a PE loader coded in EBC. -/// -#define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) ((Machine) == EFI_IMAGE_MACHINE_EBC) - -#define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) (FALSE) - -#else -#error Unknown Processor Type -#endif - -#endif diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/BaseLib.inf index 2b613579a..3586beb0a 100644 --- a/MdePkg/Library/BaseLib/BaseLib.inf +++ b/MdePkg/Library/BaseLib/BaseLib.inf @@ -5,12 +5,7 @@ # Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.
# Portions copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.
# -# 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 -# http://opensource.org/licenses/bsd-license.php. -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# SPDX-License-Identifier: BSD-2-Clause-Patent # # ## @@ -96,7 +91,6 @@ Ia32/WriteCr0.c | MSFT Ia32/WriteMsr64.c | MSFT Ia32/SwapBytes64.c | MSFT - Ia32/SetJump.c | MSFT Ia32/RRotU64.c | MSFT Ia32/RShiftU64.c | MSFT Ia32/ReadPmc.c | MSFT @@ -140,7 +134,6 @@ Ia32/MultU64x32.c | MSFT Ia32/LShiftU64.c | MSFT Ia32/LRotU64.c | MSFT - Ia32/LongJump.c | MSFT Ia32/Invd.c | MSFT Ia32/FxRestore.c | MSFT Ia32/FxSave.c | MSFT @@ -148,7 +141,6 @@ Ia32/EnablePaging32.c | MSFT Ia32/EnableInterrupts.c | MSFT Ia32/EnableDisableInterrupts.c | MSFT - Ia32/DivU64x64Remainder.nasm| MSFT Ia32/DivU64x32Remainder.c | MSFT Ia32/DivU64x32.c | MSFT Ia32/DisablePaging32.c | MSFT @@ -158,164 +150,40 @@ Ia32/CpuId.c | MSFT Ia32/CpuBreakpoint.c | MSFT Ia32/ARShiftU64.c | MSFT - Ia32/Thunk16.nasm | MSFT - Ia32/EnablePaging64.nasm| MSFT Ia32/EnableCache.c | MSFT Ia32/DisableCache.c | MSFT - Ia32/RdRand.nasm| MSFT - Ia32/Wbinvd.nasm| INTEL - Ia32/WriteMm7.nasm| INTEL - Ia32/WriteMm6.nasm| INTEL - Ia32/WriteMm5.nasm| INTEL - Ia32/WriteMm4.nasm| INTEL - Ia32/WriteMm3.nasm| INTEL - Ia32/WriteMm2.nasm| INTEL - Ia32/WriteMm1.nasm| INTEL - Ia32/WriteMm0.nasm| INTEL - Ia32/WriteLdtr.nasm| INTEL - Ia32/WriteIdtr.nasm| INTEL - Ia32/WriteGdtr.nasm| INTEL - Ia32/WriteDr7.nasm| INTEL - Ia32/WriteDr6.nasm| INTEL - Ia32/WriteDr5.nasm| INTEL - Ia32/WriteDr4.nasm| INTEL - Ia32/WriteDr3.nasm| INTEL - Ia32/WriteDr2.nasm| INTEL - Ia32/WriteDr1.nasm| INTEL - Ia32/WriteDr0.nasm| INTEL - Ia32/WriteCr4.nasm| INTEL - Ia32/WriteCr3.nasm| INTEL - Ia32/WriteCr2.nasm| INTEL - Ia32/WriteCr0.nasm| INTEL - Ia32/WriteMsr64.nasm| INTEL - Ia32/SwapBytes64.nasm| INTEL - Ia32/SetJump.nasm| INTEL - Ia32/RRotU64.nasm| INTEL - Ia32/RShiftU64.nasm| INTEL - Ia32/ReadPmc.nasm| INTEL - Ia32/ReadTsc.nasm| INTEL - Ia32/ReadLdtr.nasm| INTEL - Ia32/ReadIdtr.nasm| INTEL - Ia32/ReadGdtr.nasm| INTEL - Ia32/ReadTr.nasm| INTEL - Ia32/ReadSs.nasm| INTEL - Ia32/ReadGs.nasm| INTEL - Ia32/ReadFs.nasm| INTEL - Ia32/ReadEs.nasm| INTEL - Ia32/ReadDs.nasm| INTEL - Ia32/ReadCs.nasm| INTEL - Ia32/ReadMsr64.nasm| INTEL - Ia32/ReadMm7.nasm| INTEL - Ia32/ReadMm6.nasm| INTEL - Ia32/ReadMm5.nasm| INTEL - Ia32/ReadMm4.nasm| INTEL - Ia32/ReadMm3.nasm| INTEL - Ia32/ReadMm2.nasm| INTEL - Ia32/ReadMm1.nasm| INTEL - Ia32/ReadMm0.nasm| INTEL - Ia32/ReadEflags.nasm| INTEL - Ia32/ReadDr7.nasm| INTEL - Ia32/ReadDr6.nasm| INTEL - Ia32/ReadDr5.nasm| INTEL - Ia32/ReadDr4.nasm| INTEL - Ia32/ReadDr3.nasm| INTEL - Ia32/ReadDr2.nasm| INTEL - Ia32/ReadDr1.nasm| INTEL - Ia32/ReadDr0.nasm| INTEL - Ia32/ReadCr4.nasm| INTEL - Ia32/ReadCr3.nasm| INTEL - Ia32/ReadCr2.nasm| INTEL - Ia32/ReadCr0.nasm| INTEL - Ia32/Mwait.nasm| INTEL - Ia32/Monitor.nasm| INTEL - Ia32/ModU64x32.nasm| INTEL - Ia32/MultU64x64.nasm| INTEL - Ia32/MultU64x32.nasm| INTEL - Ia32/LShiftU64.nasm| INTEL - Ia32/LRotU64.nasm| INTEL - Ia32/LongJump.nasm| INTEL - Ia32/Invd.nasm| INTEL - Ia32/FxRestore.nasm| INTEL - Ia32/FxSave.nasm| INTEL - Ia32/FlushCacheLine.nasm| INTEL - Ia32/EnablePaging32.nasm| INTEL - Ia32/EnableInterrupts.nasm| INTEL - Ia32/EnableDisableInterrupts.nasm| INTEL - Ia32/DivU64x64Remainder.nasm| INTEL - Ia32/DivU64x32Remainder.nasm| INTEL - Ia32/DivU64x32.nasm| INTEL - Ia32/DisablePaging32.nasm| INTEL - Ia32/DisableInterrupts.nasm| INTEL - Ia32/CpuPause.nasm| INTEL - Ia32/CpuIdEx.nasm| INTEL - Ia32/CpuId.nasm| INTEL - Ia32/CpuBreakpoint.nasm| INTEL - Ia32/ARShiftU64.nasm| INTEL - Ia32/Thunk16.nasm | INTEL - Ia32/EnablePaging64.nasm| INTEL - Ia32/EnableCache.nasm| INTEL - Ia32/DisableCache.nasm| INTEL - Ia32/RdRand.nasm| INTEL Ia32/GccInline.c | GCC - Ia32/Thunk16.nasm | GCC - Ia32/Thunk16.S | XCODE + Ia32/Thunk16.nasm Ia32/EnableDisableInterrupts.nasm| GCC - Ia32/EnableDisableInterrupts.S | GCC - Ia32/EnablePaging64.nasm| GCC - Ia32/EnablePaging64.S | GCC + Ia32/EnablePaging64.nasm Ia32/DisablePaging32.nasm| GCC - Ia32/DisablePaging32.S | GCC Ia32/EnablePaging32.nasm| GCC - Ia32/EnablePaging32.S | GCC Ia32/Mwait.nasm| GCC - Ia32/Mwait.S | GCC Ia32/Monitor.nasm| GCC - Ia32/Monitor.S | GCC Ia32/CpuIdEx.nasm| GCC - Ia32/CpuIdEx.S | GCC Ia32/CpuId.nasm| GCC - Ia32/CpuId.S | GCC - Ia32/LongJump.nasm| GCC - Ia32/LongJump.S | GCC - Ia32/SetJump.nasm| GCC - Ia32/SetJump.S | GCC + Ia32/LongJump.nasm + Ia32/SetJump.nasm Ia32/SwapBytes64.nasm| GCC - Ia32/SwapBytes64.S | GCC - Ia32/DivU64x64Remainder.nasm| GCC - Ia32/DivU64x64Remainder.S | GCC + Ia32/DivU64x64Remainder.nasm Ia32/DivU64x32Remainder.nasm| GCC - Ia32/DivU64x32Remainder.S | GCC Ia32/ModU64x32.nasm| GCC - Ia32/ModU64x32.S | GCC Ia32/DivU64x32.nasm| GCC - Ia32/DivU64x32.S | GCC Ia32/MultU64x64.nasm| GCC - Ia32/MultU64x64.S | GCC Ia32/MultU64x32.nasm| GCC - Ia32/MultU64x32.S | GCC Ia32/RRotU64.nasm| GCC - Ia32/RRotU64.S | GCC Ia32/LRotU64.nasm| GCC - Ia32/LRotU64.S | GCC Ia32/ARShiftU64.nasm| GCC - Ia32/ARShiftU64.S | GCC Ia32/RShiftU64.nasm| GCC - Ia32/RShiftU64.S | GCC Ia32/LShiftU64.nasm| GCC - Ia32/LShiftU64.S | GCC Ia32/EnableCache.nasm| GCC - Ia32/EnableCache.S | GCC Ia32/DisableCache.nasm| GCC - Ia32/DisableCache.S | GCC - Ia32/RdRand.nasm| GCC - Ia32/RdRand.S | GCC + Ia32/RdRand.nasm Ia32/DivS64x64Remainder.c Ia32/InternalSwitchStack.c | MSFT - Ia32/InternalSwitchStack.c | INTEL - Ia32/InternalSwitchStack.S | GCC Ia32/InternalSwitchStack.nasm | GCC Ia32/Non-existing.c Unaligned.c @@ -326,7 +194,6 @@ X86ReadGdtr.c X86Msr.c X86MemoryFence.c | MSFT - X86MemoryFence.c | INTEL X86GetInterruptState.c X86FxSave.c X86FxRestore.c @@ -353,15 +220,12 @@ X64/CpuBreakpoint.c | MSFT X64/WriteMsr64.c | MSFT X64/ReadMsr64.c | MSFT - X64/RdRand.nasm| MSFT X64/CpuPause.nasm| MSFT - X64/EnableDisableInterrupts.nasm| MSFT X64/DisableInterrupts.nasm| MSFT X64/EnableInterrupts.nasm| MSFT X64/FlushCacheLine.nasm| MSFT X64/Invd.nasm| MSFT X64/Wbinvd.nasm| MSFT - X64/DisablePaging64.nasm| MSFT X64/Mwait.nasm| MSFT X64/Monitor.nasm| MSFT X64/ReadPmc.nasm| MSFT @@ -423,78 +287,6 @@ X64/ReadCr0.nasm| MSFT X64/ReadEflags.nasm| MSFT - X64/CpuBreakpoint.nasm| INTEL - X64/WriteMsr64.nasm| INTEL - X64/ReadMsr64.nasm| INTEL - X64/RdRand.nasm| INTEL - X64/CpuPause.nasm| INTEL - X64/EnableDisableInterrupts.nasm| INTEL - X64/DisableInterrupts.nasm| INTEL - X64/EnableInterrupts.nasm| INTEL - X64/FlushCacheLine.nasm| INTEL - X64/Invd.nasm| INTEL - X64/Wbinvd.nasm| INTEL - X64/DisablePaging64.nasm| INTEL - X64/Mwait.nasm| INTEL - X64/Monitor.nasm| INTEL - X64/ReadPmc.nasm| INTEL - X64/ReadTsc.nasm| INTEL - X64/WriteMm7.nasm| INTEL - X64/WriteMm6.nasm| INTEL - X64/WriteMm5.nasm| INTEL - X64/WriteMm4.nasm| INTEL - X64/WriteMm3.nasm| INTEL - X64/WriteMm2.nasm| INTEL - X64/WriteMm1.nasm| INTEL - X64/WriteMm0.nasm| INTEL - X64/ReadMm7.nasm| INTEL - X64/ReadMm6.nasm| INTEL - X64/ReadMm5.nasm| INTEL - X64/ReadMm4.nasm| INTEL - X64/ReadMm3.nasm| INTEL - X64/ReadMm2.nasm| INTEL - X64/ReadMm1.nasm| INTEL - X64/ReadMm0.nasm| INTEL - X64/FxRestore.nasm| INTEL - X64/FxSave.nasm| INTEL - X64/WriteLdtr.nasm| INTEL - X64/ReadLdtr.nasm| INTEL - X64/WriteIdtr.nasm| INTEL - X64/ReadIdtr.nasm| INTEL - X64/WriteGdtr.nasm| INTEL - X64/ReadGdtr.nasm| INTEL - X64/ReadTr.nasm| INTEL - X64/ReadSs.nasm| INTEL - X64/ReadGs.nasm| INTEL - X64/ReadFs.nasm| INTEL - X64/ReadEs.nasm| INTEL - X64/ReadDs.nasm| INTEL - X64/ReadCs.nasm| INTEL - X64/WriteDr7.nasm| INTEL - X64/WriteDr6.nasm| INTEL - X64/WriteDr5.nasm| INTEL - X64/WriteDr4.nasm| INTEL - X64/WriteDr3.nasm| INTEL - X64/WriteDr2.nasm| INTEL - X64/WriteDr1.nasm| INTEL - X64/WriteDr0.nasm| INTEL - X64/ReadDr7.nasm| INTEL - X64/ReadDr6.nasm| INTEL - X64/ReadDr5.nasm| INTEL - X64/ReadDr4.nasm| INTEL - X64/ReadDr3.nasm| INTEL - X64/ReadDr2.nasm| INTEL - X64/ReadDr1.nasm| INTEL - X64/ReadDr0.nasm| INTEL - X64/WriteCr4.nasm| INTEL - X64/WriteCr3.nasm| INTEL - X64/WriteCr2.nasm| INTEL - X64/WriteCr0.nasm| INTEL - X64/ReadCr4.nasm| INTEL - X64/ReadCr3.nasm| INTEL - X64/ReadCr2.nasm| INTEL - X64/ReadCr0.nasm| INTEL - X64/ReadEflags.nasm| INTEL X64/Non-existing.c Math64.c @@ -506,7 +298,6 @@ X86ReadGdtr.c X86Msr.c X86MemoryFence.c | MSFT - X86MemoryFence.c | INTEL X86GetInterruptState.c X86FxSave.c X86FxRestore.c @@ -518,27 +309,9 @@ X86PatchInstruction.c X86SpeculationBarrier.c X64/GccInline.c | GCC -# X64/Thunk16.S | XCODE - X64/SwitchStack.nasm| GCC - X64/SwitchStack.S | GCC - X64/SetJump.nasm| GCC - X64/SetJump.S | GCC - X64/LongJump.nasm| GCC - X64/LongJump.S | GCC - X64/EnableDisableInterrupts.nasm| GCC - X64/EnableDisableInterrupts.S | GCC - X64/DisablePaging64.nasm| GCC - X64/DisablePaging64.S | GCC - X64/CpuId.nasm| GCC - X64/CpuId.S | GCC - X64/CpuIdEx.nasm| GCC - X64/CpuIdEx.S | GCC - X64/EnableCache.nasm| GCC - X64/EnableCache.S | GCC - X64/DisableCache.nasm| GCC - X64/DisableCache.S | GCC - X64/RdRand.nasm| GCC - X64/RdRand.S | GCC + X64/EnableDisableInterrupts.nasm + X64/DisablePaging64.nasm + X64/RdRand.nasm ChkStkGcc.c | GCC [Sources.EBC] @@ -621,6 +394,7 @@ gEfiMdePkgTokenSpaceGuid.PcdMaximumAsciiStringLength ## SOMETIMES_CONSUMES gEfiMdePkgTokenSpaceGuid.PcdMaximumUnicodeStringLength ## SOMETIMES_CONSUMES gEfiMdePkgTokenSpaceGuid.PcdControlFlowEnforcementPropertyMask ## SOMETIMES_CONSUMES + gEfiMdePkgTokenSpaceGuid.PcdSpeculationBarrierType ## SOMETIMES_CONSUMES [FeaturePcd] gEfiMdePkgTokenSpaceGuid.PcdVerifyNodeInList ## CONSUMES diff --git a/MdePkg/Library/BaseLib/SafeString.c b/MdePkg/Library/BaseLib/SafeString.c index 8c7897050..284527c7b 100644 --- a/MdePkg/Library/BaseLib/SafeString.c +++ b/MdePkg/Library/BaseLib/SafeString.c @@ -2,13 +2,7 @@ Safe String functions. Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.
- 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 - http://opensource.org/licenses/bsd-license.php. - - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + SPDX-License-Identifier: BSD-2-Clause-Patent **/ diff --git a/MdePkg/Library/BaseLib/String.c b/MdePkg/Library/BaseLib/String.c index cee8f08f1..f4b00ac47 100644 --- a/MdePkg/Library/BaseLib/String.c +++ b/MdePkg/Library/BaseLib/String.c @@ -2,13 +2,7 @@ Unicode and ASCII string primitives. Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
- 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 - http://opensource.org/licenses/bsd-license.php. - - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + SPDX-License-Identifier: BSD-2-Clause-Patent **/ @@ -190,8 +184,7 @@ StrLen ( // ASSERT (Length < PcdGet32 (PcdMaximumUnicodeStringLength)); if (Length >= 100000000ull) { break; - } -// } + } } return Length; } @@ -340,8 +333,7 @@ StrnCmp ( // ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength)); if (Length > 100000000ull) { Length = 100000000ull; - } -// } + } while ((*FirstString != L'\0') && (*SecondString != L'\0') && @@ -855,8 +847,8 @@ StrHexToUint64 ( UINT64 EFIAPI StrHexToUint64 ( - IN CONST CHAR16 *String - ) + IN CONST CHAR16 *String + ) { UINT64 Result; @@ -1161,8 +1153,7 @@ AsciiStrnCpy ( // ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength)); if (Length <= 100000000ull) { Length = 100000000ull; - } -// } + } ReturnValue = Destination; @@ -1215,7 +1206,7 @@ AsciiStrLen ( // ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength)); if (Length == 100000000ull) { break; - } + } // } } return Length; @@ -1483,8 +1474,7 @@ AsciiStrnCmp ( // ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength)); if (Length > 100000000ull) { Length = 100000000ull; - } -// } + } while ((*FirstString != '\0') && (*SecondString != '\0') && @@ -1951,47 +1941,10 @@ AsciiStrToUnicodeStr ( #endif -// -// The basis for Base64 encoding is RFC 4686 https://tools.ietf.org/html/rfc4648 -// -// RFC 4686 has a number of MAY and SHOULD cases. This implementation chooses -// the more restrictive versions for security concerns (see RFC 4686 section 3.3). -// -// A invalid character, if encountered during the decode operation, causes the data -// to be rejected. In addition, the '=' padding character is only allowed at the end -// of the Base64 encoded string. -// -#define BAD_V 99 - STATIC CHAR8 EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; -#if 0 -STATIC UINT8 DecodingTable[] = { - // - // Valid characters ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ - // Also, set '=' as a zero for decoding - // 0 , 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f - BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, // 0 - BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, // 10 - BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, 62, BAD_V, BAD_V, BAD_V, 63, // 20 - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, BAD_V, BAD_V, BAD_V, 0, BAD_V, BAD_V, // 30 - BAD_V, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 40 - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, // 50 - BAD_V, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 60 - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, // 70 - BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, // 80 - BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, // 90 - BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, // a0 - BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, // b0 - BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, // c0 - BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, // d0 - BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, // d0 - BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V, BAD_V // f0 -}; -#endif - /** Convert binary data to a Base64 encoded ascii string based on RFC4648. @@ -2114,175 +2067,343 @@ Base64Encode ( } /** - Convert Base64 ascii string to binary data based on RFC4648. + Decode Base64 ASCII encoded data to 8-bit binary representation, based on + RFC4648. - Produce Null-terminated binary data in the output buffer specified by Destination and DestinationSize. - The binary data is produced by converting the Base64 ascii string specified by Source and SourceLength. + Decoding occurs according to "Table 1: The Base 64 Alphabet" in RFC4648. - @param Source Input ASCII characters - @param SourceLength Number of ASCII characters - @param Destination Pointer to output buffer - @param DestinationSize Caller is responsible for passing in buffer of at least DestinationSize. - Set 0 to get the size needed. Set to bytes stored on return. + Whitespace is ignored at all positions: + - 0x09 ('\t') horizontal tab + - 0x0A ('\n') new line + - 0x0B ('\v') vertical tab + - 0x0C ('\f') form feed + - 0x0D ('\r') carriage return + - 0x20 (' ') space - @retval RETURN_SUCCESS When binary buffer is filled in. - @retval RETURN_INVALID_PARAMETER If Source is NULL or DestinationSize is NULL. - @retval RETURN_INVALID_PARAMETER If SourceLength or DestinationSize is bigger than (MAX_ADDRESS -(UINTN)Destination ). - @retval RETURN_INVALID_PARAMETER If there is any invalid character in input stream. - @retval RETURN_BUFFER_TOO_SMALL If buffer length is smaller than required buffer size. - **/ + The minimum amount of required padding (with ASCII 0x3D, '=') is tolerated + and enforced at the end of the Base64 ASCII encoded data, and only there. + + Other characters outside of the encoding alphabet cause the function to + reject the Base64 ASCII encoded data. + + @param[in] Source Array of CHAR8 elements containing the Base64 + ASCII encoding. May be NULL if SourceSize is + zero. + + @param[in] SourceSize Number of CHAR8 elements in Source. + + @param[out] Destination Array of UINT8 elements receiving the decoded + 8-bit binary representation. Allocated by the + caller. May be NULL if DestinationSize is + zero on input. If NULL, decoding is + performed, but the 8-bit binary + representation is not stored. If non-NULL and + the function returns an error, the contents + of Destination are indeterminate. + + @param[in,out] DestinationSize On input, the number of UINT8 elements that + the caller allocated for Destination. On + output, if the function returns + RETURN_SUCCESS or RETURN_BUFFER_TOO_SMALL, + the number of UINT8 elements that are + required for decoding the Base64 ASCII + representation. If the function returns a + value different from both RETURN_SUCCESS and + RETURN_BUFFER_TOO_SMALL, then DestinationSize + is indeterminate on output. + + @retval RETURN_SUCCESS SourceSize CHAR8 elements at Source have + been decoded to on-output DestinationSize + UINT8 elements at Destination. Note that + RETURN_SUCCESS covers the case when + DestinationSize is zero on input, and + Source decodes to zero bytes (due to + containing at most ignored whitespace). + + @retval RETURN_BUFFER_TOO_SMALL The input value of DestinationSize is not + large enough for decoding SourceSize CHAR8 + elements at Source. The required number of + UINT8 elements has been stored to + DestinationSize. + + @retval RETURN_INVALID_PARAMETER DestinationSize is NULL. + + @retval RETURN_INVALID_PARAMETER Source is NULL, but SourceSize is not zero. + + @retval RETURN_INVALID_PARAMETER Destination is NULL, but DestinationSize is + not zero on input. + + @retval RETURN_INVALID_PARAMETER Source is non-NULL, and (Source + + SourceSize) would wrap around MAX_ADDRESS. + + @retval RETURN_INVALID_PARAMETER Destination is non-NULL, and (Destination + + DestinationSize) would wrap around + MAX_ADDRESS, as specified on input. + + @retval RETURN_INVALID_PARAMETER None of Source and Destination are NULL, + and CHAR8[SourceSize] at Source overlaps + UINT8[DestinationSize] at Destination, as + specified on input. + + @retval RETURN_INVALID_PARAMETER Invalid CHAR8 element encountered in + Source. +**/ #if 0 RETURN_STATUS EFIAPI Base64Decode ( - IN CONST CHAR8 *Source, - IN UINTN SourceLength, - OUT UINT8 *Destination OPTIONAL, - IN OUT UINTN *DestinationSize + IN CONST CHAR8 *Source OPTIONAL, + IN UINTN SourceSize, + OUT UINT8 *Destination OPTIONAL, + IN OUT UINTN *DestinationSize ) { + BOOLEAN PaddingMode; + UINTN SixBitGroupsConsumed; + UINT32 Accumulator; + UINTN OriginalDestinationSize; + UINTN SourceIndex; + CHAR8 SourceChar; + UINT32 Base64Value; + UINT8 DestinationOctet; - UINT32 Value; - CHAR8 Chr; - INTN BufferSize; - UINTN SourceIndex; - UINTN DestinationIndex; - UINTN Index; - UINTN ActualSourceLength; - - // - // Check pointers are not NULL - // - if ((Source == NULL) || (DestinationSize == NULL)) { + if (DestinationSize == NULL) { return RETURN_INVALID_PARAMETER; } // - // Check if SourceLength or DestinationSize is valid + // Check Source array validity. // - if ((SourceLength >= (MAX_ADDRESS - (UINTN)Source)) || (*DestinationSize >= (MAX_ADDRESS - (UINTN)Destination))){ + if (Source == NULL) { + if (SourceSize > 0) { + // + // At least one CHAR8 element at NULL Source. + // + return RETURN_INVALID_PARAMETER; + } + } else if (SourceSize > MAX_ADDRESS - (UINTN)Source) { + // + // Non-NULL Source, but it wraps around. + // return RETURN_INVALID_PARAMETER; } - ActualSourceLength = 0; - BufferSize = 0; + // + // Check Destination array validity. + // + if (Destination == NULL) { + if (*DestinationSize > 0) { + // + // At least one UINT8 element at NULL Destination. + // + return RETURN_INVALID_PARAMETER; + } + } else if (*DestinationSize > MAX_ADDRESS - (UINTN)Destination) { + // + // Non-NULL Destination, but it wraps around. + // + return RETURN_INVALID_PARAMETER; + } // - // Determine the actual number of valid characters in the string. - // All invalid characters except selected white space characters, - // will cause the Base64 string to be rejected. White space to allow - // properly formatted XML will be ignored. + // Check for overlap. // - // See section 3.3 of RFC 4648. + if (Source != NULL && Destination != NULL) { + // + // Both arrays have been provided, and we know from earlier that each array + // is valid in itself. + // + if ((UINTN)Source + SourceSize <= (UINTN)Destination) { + // + // Source array precedes Destination array, OK. + // + } else if ((UINTN)Destination + *DestinationSize <= (UINTN)Source) { + // + // Destination array precedes Source array, OK. + // + } else { + // + // Overlap. + // + return RETURN_INVALID_PARAMETER; + } + } + // - for (SourceIndex = 0; SourceIndex < SourceLength; SourceIndex++) { + // Decoding loop setup. + // + PaddingMode = FALSE; + SixBitGroupsConsumed = 0; + Accumulator = 0; + OriginalDestinationSize = *DestinationSize; + *DestinationSize = 0; + + // + // Decoding loop. + // + for (SourceIndex = 0; SourceIndex < SourceSize; SourceIndex++) { + SourceChar = Source[SourceIndex]; // - // '=' is part of the quantum + // Whitespace is ignored at all positions (regardless of padding mode). // - if (Source[SourceIndex] == '=') { - ActualSourceLength++; - BufferSize--; + if (SourceChar == '\t' || SourceChar == '\n' || SourceChar == '\v' || + SourceChar == '\f' || SourceChar == '\r' || SourceChar == ' ') { + continue; + } + // + // If we're in padding mode, accept another padding character, as long as + // that padding character completes the quantum. This completes case (2) + // from RFC4648, Chapter 4. "Base 64 Encoding": + // + // (2) The final quantum of encoding input is exactly 8 bits; here, the + // final unit of encoded output will be two characters followed by two + // "=" padding characters. + // + if (PaddingMode) { + if (SourceChar == '=' && SixBitGroupsConsumed == 3) { + SixBitGroupsConsumed = 0; + continue; + } + return RETURN_INVALID_PARAMETER; + } + + // + // When not in padding mode, decode Base64Value based on RFC4648, "Table 1: + // The Base 64 Alphabet". + // + if ('A' <= SourceChar && SourceChar <= 'Z') { + Base64Value = SourceChar - 'A'; + } else if ('a' <= SourceChar && SourceChar <= 'z') { + Base64Value = 26 + (SourceChar - 'a'); + } else if ('0' <= SourceChar && SourceChar <= '9') { + Base64Value = 52 + (SourceChar - '0'); + } else if (SourceChar == '+') { + Base64Value = 62; + } else if (SourceChar == '/') { + Base64Value = 63; + } else if (SourceChar == '=') { // - // Only two '=' characters can be valid. + // Enter padding mode. // - if (BufferSize < -2) { + PaddingMode = TRUE; + + if (SixBitGroupsConsumed == 2) { + // + // If we have consumed two 6-bit groups from the current quantum before + // encountering the first padding character, then this is case (2) from + // RFC4648, Chapter 4. "Base 64 Encoding". Bump SixBitGroupsConsumed, + // and we'll enforce another padding character. + // + SixBitGroupsConsumed = 3; + } else if (SixBitGroupsConsumed == 3) { + // + // If we have consumed three 6-bit groups from the current quantum + // before encountering the first padding character, then this is case + // (3) from RFC4648, Chapter 4. "Base 64 Encoding". The quantum is now + // complete. + // + SixBitGroupsConsumed = 0; + } else { + // + // Padding characters are not allowed at the first two positions of a + // quantum. + // return RETURN_INVALID_PARAMETER; } - } - else { - Chr = Source[SourceIndex]; - if (BAD_V != DecodingTable[(UINT8) Chr]) { - // - // The '=' characters are only valid at the end, so any - // valid character after an '=', will be flagged as an error. - // - if (BufferSize < 0) { - return RETURN_INVALID_PARAMETER; - } - ActualSourceLength++; + // + // Wherever in a quantum we enter padding mode, we enforce the padding + // bits pending in the accumulator -- from the last 6-bit group just + // preceding the padding character -- to be zero. Refer to RFC4648, + // Chapter 3.5. "Canonical Encoding". + // + if (Accumulator != 0) { + return RETURN_INVALID_PARAMETER; } - else { - // - // The reset of the decoder will ignore all invalid characters allowed here. - // Ignoring selected white space is useful. In this case, the decoder will - // ignore ' ', '\t', '\n', and '\r'. - // - if ((Chr != ' ') &&(Chr != '\t') &&(Chr != '\n') &&(Chr != '\r')) { - return RETURN_INVALID_PARAMETER; - } - } + // + // Advance to the next source character. + // + continue; + } else { + // + // Other characters outside of the encoding alphabet are rejected. + // + return RETURN_INVALID_PARAMETER; } + + // + // Feed the bits of the current 6-bit group of the quantum to the + // accumulator. + // + Accumulator = (Accumulator << 6) | Base64Value; + SixBitGroupsConsumed++; + switch (SixBitGroupsConsumed) { + case 1: + // + // No octet to spill after consuming the first 6-bit group of the + // quantum; advance to the next source character. + // + continue; + case 2: + // + // 12 bits accumulated (6 pending + 6 new); prepare for spilling an + // octet. 4 bits remain pending. + // + DestinationOctet = (UINT8)(Accumulator >> 4); + Accumulator &= 0xF; + break; + case 3: + // + // 10 bits accumulated (4 pending + 6 new); prepare for spilling an + // octet. 2 bits remain pending. + // + DestinationOctet = (UINT8)(Accumulator >> 2); + Accumulator &= 0x3; + break; + default: + ASSERT (SixBitGroupsConsumed == 4); + // + // 8 bits accumulated (2 pending + 6 new); prepare for spilling an octet. + // The quantum is complete, 0 bits remain pending. + // + DestinationOctet = (UINT8)Accumulator; + Accumulator = 0; + SixBitGroupsConsumed = 0; + break; + } + + // + // Store the decoded octet if there's room left. Increment + // (*DestinationSize) unconditionally. + // + if (*DestinationSize < OriginalDestinationSize) { + ASSERT (Destination != NULL); + Destination[*DestinationSize] = DestinationOctet; + } + (*DestinationSize)++; + + // + // Advance to the next source character. + // } // - // The Base64 character string must be a multiple of 4 character quantums. + // If Source terminates mid-quantum, then Source is invalid. // - if (ActualSourceLength % 4 != 0) { + if (SixBitGroupsConsumed != 0) { return RETURN_INVALID_PARAMETER; } - BufferSize += ActualSourceLength / 4 * 3; - if (BufferSize < 0) { - return RETURN_INVALID_PARAMETER; - } - // - // BufferSize is >= 0 + // Done. // - if ((Destination == NULL) || (*DestinationSize < (UINTN) BufferSize)) { - *DestinationSize = BufferSize; - return RETURN_BUFFER_TOO_SMALL; - } - - // - // If no decodable characters, return a size of zero. RFC 4686 test vector 1. - // - if (ActualSourceLength == 0) { - *DestinationSize = 0; + if (*DestinationSize <= OriginalDestinationSize) { return RETURN_SUCCESS; } - - // - // Input data is verified to be a multiple of 4 valid charcters. Process four - // characters at a time. Uncounted (ie. invalid) characters will be ignored. - // - for (SourceIndex = 0, DestinationIndex = 0; (SourceIndex < SourceLength) && (DestinationIndex < *DestinationSize); ) { - Value = 0; - - // - // Get 24 bits of data from 4 input characters, each character representing 6 bits - // - for (Index = 0; Index < 4; Index++) { - do { - Chr = DecodingTable[(UINT8) Source[SourceIndex++]]; - } while (Chr == BAD_V); - Value <<= 6; - Value |= (UINT32)Chr; - } - - // - // Store 3 bytes of binary data (24 bits) - // - *Destination++ = (UINT8) (Value >> 16); - DestinationIndex++; - - // - // Due to the '=' special cases for the two bytes at the end, - // we have to check the length and not store the padding data - // - if (DestinationIndex++ < *DestinationSize) { - *Destination++ = (UINT8) (Value >> 8); - } - if (DestinationIndex++ < *DestinationSize) { - *Destination++ = (UINT8) Value; - } - } - - return RETURN_SUCCESS; + return RETURN_BUFFER_TOO_SMALL; } #endif diff --git a/MdePkg/Library/BaseLib/X64/CpuId.S b/MdePkg/Library/BaseLib/X64/CpuId.S deleted file mode 100644 index c3d259720..000000000 --- a/MdePkg/Library/BaseLib/X64/CpuId.S +++ /dev/null @@ -1,60 +0,0 @@ -#------------------------------------------------------------------------------ -# -# Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.
-# 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 -# http://opensource.org/licenses/bsd-license.php. -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -# Module Name: -# -# CpuId.S -# -# Abstract: -# -# AsmCpuid function -# -# Notes: -# -#------------------------------------------------------------------------------ - -#------------------------------------------------------------------------------ -# VOID -# EFIAPI -# AsmCpuid ( -# IN UINT32 RegisterInEax, -# OUT UINT32 *RegisterOutEax OPTIONAL, -# OUT UINT32 *RegisterOutEbx OPTIONAL, -# OUT UINT32 *RegisterOutEcx OPTIONAL, -# OUT UINT32 *RegisterOutEdx OPTIONAL -# ) -#------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(AsmCpuid) -ASM_PFX(AsmCpuid): - push %rbx - mov %ecx, %eax - push %rax # save Index on stack - push %rdx - cpuid - test %r9, %r9 - jz L1 - mov %ecx, (%r9) -L1: - pop %rcx - jrcxz L2 - mov %eax, (%rcx) -L2: - mov %r8, %rcx - jrcxz L3 - mov %ebx, (%rcx) -L3: - mov 0x38(%rsp), %rcx - jrcxz L4 - mov %edx, (%rcx) -L4: - pop %rax # restore Index to rax as return value - pop %rbx - ret diff --git a/MdePkg/Library/BaseLib/X64/CpuIdEx.S b/MdePkg/Library/BaseLib/X64/CpuIdEx.S deleted file mode 100644 index d47f53c84..000000000 --- a/MdePkg/Library/BaseLib/X64/CpuIdEx.S +++ /dev/null @@ -1,62 +0,0 @@ -#------------------------------------------------------------------------------ -# -# Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.
-# 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 -# http://opensource.org/licenses/bsd-license.php. -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -# Module Name: -# -# CpuIdEx.S -# -# Abstract: -# -# AsmCpuidEx function -# -# Notes: -# -#------------------------------------------------------------------------------ - -#------------------------------------------------------------------------------ -# UINT32 -# EFIAPI -# AsmCpuidEx ( -# IN UINT32 RegisterInEax, -# IN UINT32 RegisterInEcx, -# OUT UINT32 *RegisterOutEax OPTIONAL, -# OUT UINT32 *RegisterOutEbx OPTIONAL, -# OUT UINT32 *RegisterOutEcx OPTIONAL, -# OUT UINT32 *RegisterOutEdx OPTIONAL -# ) -#------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(AsmCpuidEx) -ASM_PFX(AsmCpuidEx): - push %rbx - movl %ecx,%eax - movl %edx,%ecx - push %rax # save Index on stack - cpuid - mov 0x38(%rsp), %r10 - test %r10, %r10 - jz L1 - mov %ecx,(%r10) -L1: - mov %r8, %rcx - jrcxz L2 - movl %eax,(%rcx) -L2: - mov %r9, %rcx - jrcxz L3 - mov %ebx, (%rcx) -L3: - mov 0x40(%rsp), %rcx - jrcxz L4 - mov %edx, (%rcx) -L4: - pop %rax # restore Index to rax as return value - pop %rbx - ret diff --git a/MdePkg/Library/BaseLib/X64/DisableCache.S b/MdePkg/Library/BaseLib/X64/DisableCache.S deleted file mode 100644 index 970f2f361..000000000 --- a/MdePkg/Library/BaseLib/X64/DisableCache.S +++ /dev/null @@ -1,39 +0,0 @@ -#------------------------------------------------------------------------------ -# -# Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.
-# 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 -# http://opensource.org/licenses/bsd-license.php. -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -# Module Name: -# -# DisableCache.S -# -# Abstract: -# -# Set the CD bit of CR0 to 1, clear the NW bit of CR0 to 0, and flush all caches with a -# WBINVD instruction. -# -# Notes: -# -#------------------------------------------------------------------------------ - -#------------------------------------------------------------------------------ -# VOID -# EFIAPI -# AsmDisableCache ( -# VOID -# ); -#------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(AsmDisableCache) -ASM_PFX(AsmDisableCache): - movq %cr0, %rax - btsq $30, %rax - btrq $29, %rax - movq %rax, %cr0 - wbinvd - ret diff --git a/MdePkg/Library/BaseLib/X64/DisablePaging64.S b/MdePkg/Library/BaseLib/X64/DisablePaging64.S deleted file mode 100644 index 676e9e175..000000000 --- a/MdePkg/Library/BaseLib/X64/DisablePaging64.S +++ /dev/null @@ -1,82 +0,0 @@ -#------------------------------------------------------------------------------ -# -# Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.
-# 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 -# http://opensource.org/licenses/bsd-license.php. -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -# Module Name: -# -# DisablePaging64.S -# -# Abstract: -# -# AsmDisablePaging64 function -# -# Notes: -# -#------------------------------------------------------------------------------ - - - -#------------------------------------------------------------------------------ -# VOID -# EFIAPI -# InternalX86DisablePaging64 ( -# IN UINT16 Cs, -# IN UINT32 EntryPoint, -# IN UINT32 Context1, OPTIONAL -# IN UINT32 Context2, OPTIONAL -# IN UINT32 NewStack -# ); -#------------------------------------------------------------------------------ - -ASM_GLOBAL ASM_PFX(InternalX86DisablePaging64) -ASM_PFX(InternalX86DisablePaging64): - cli - lea L1(%rip), %rsi # rsi <- The start address of transition code - mov 0x28(%rsp), %edi # rdi <- New stack - lea _mTransitionEnd(%rip), %rax # rax <- end of transition code - sub %rsi, %rax # rax <- The size of transition piece code - add $4, %rax # round rax up to the next 4 byte boundary - and $0xfc, %al - sub %rax, %rdi # rdi <- use stack to hold transition code - mov %edi, %r10d # r10 <- The start address of transicition code below 4G - push %rcx # save rcx to stack - mov %rax, %rcx # rcx <- The size of transition piece code - rep - movsb # copy transition code to (new stack - 64byte) below 4G - pop %rcx # restore rcx - - mov %r8d, %esi - mov %r9d, %edi - mov %r10d, %eax - sub $4, %eax - push %rcx # push Cs to stack - push %r10 # push address of transition code on stack - .byte 0x48, 0xcb # retq: Use far return to load CS register from stack - # (Use raw byte code since some GNU assemblers generates incorrect code for "retq") -L1: - mov %eax,%esp # set up new stack - mov %cr0,%rax - btr $0x1f,%eax # clear CR0.PG - mov %rax,%cr0 # disable paging - - mov %edx,%ebx # save EntryPoint to ebx, for rdmsr will overwrite edx - mov $0xc0000080,%ecx - rdmsr - and $0xfe,%ah # clear LME - wrmsr - mov %cr4,%rax - and $0xdf,%al # clear PAE - mov %rax,%cr4 - push %rdi # push Context2 - push %rsi # push Context1 - callq *%rbx # transfer control to EntryPoint - jmp . # no one should get here - -_mTransitionEnd : diff --git a/MdePkg/Library/BaseLib/X64/EnableCache.S b/MdePkg/Library/BaseLib/X64/EnableCache.S deleted file mode 100644 index 9d739603c..000000000 --- a/MdePkg/Library/BaseLib/X64/EnableCache.S +++ /dev/null @@ -1,39 +0,0 @@ -#------------------------------------------------------------------------------ -# -# Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.
-# 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 -# http://opensource.org/licenses/bsd-license.php. -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -# Module Name: -# -# EnableCache.S -# -# Abstract: -# -# Flush all caches with a WBINVD instruction, clear the CD bit of CR0 to 0, and clear -# the NW bit of CR0 to 0 -# -# Notes: -# -#------------------------------------------------------------------------------ - -#------------------------------------------------------------------------------ -# VOID -# EFIAPI -# AsmEnableCache ( -# VOID -# ); -#------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(AsmEnableCache) -ASM_PFX(AsmEnableCache): - wbinvd - movq %cr0, %rax - btrq $30, %rax - btrq $29, %rax - movq %rax, %cr0 - ret diff --git a/MdePkg/Library/BaseLib/X64/EnableDisableInterrupts.S b/MdePkg/Library/BaseLib/X64/EnableDisableInterrupts.S deleted file mode 100644 index f2ff61ecf..000000000 --- a/MdePkg/Library/BaseLib/X64/EnableDisableInterrupts.S +++ /dev/null @@ -1,36 +0,0 @@ -#------------------------------------------------------------------------------ -# -# Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.
-# 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 -# http://opensource.org/licenses/bsd-license.php. -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -# Module Name: -# -# EnableDisableInterrupts.S -# -# Abstract: -# -# EnableDisableInterrupts function -# -# Notes: -# -#------------------------------------------------------------------------------ - - -#------------------------------------------------------------------------------ -# VOID -# EFIAPI -# EnableDisableInterrupts ( -# VOID -# ); -#------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(EnableDisableInterrupts) -ASM_PFX(EnableDisableInterrupts): - sti - cli - ret diff --git a/MdePkg/Library/BaseLib/X64/LongJump.S b/MdePkg/Library/BaseLib/X64/LongJump.S deleted file mode 100644 index f20446fcf..000000000 --- a/MdePkg/Library/BaseLib/X64/LongJump.S +++ /dev/null @@ -1,54 +0,0 @@ -#------------------------------------------------------------------------------ -# -# Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.
-# 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 -# http://opensource.org/licenses/bsd-license.php. -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -# Module Name: -# -# LongJump.S -# -# Abstract: -# -# Implementation of _LongJump() on x64. -# -#------------------------------------------------------------------------------ - -#------------------------------------------------------------------------------ -# VOID -# EFIAPI -# InternalLongJump ( -# IN BASE_LIBRARY_JUMP_BUFFER *JumpBuffer, -# IN UINTN Value -# ); -#------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(InternalLongJump) -ASM_PFX(InternalLongJump): - mov (%rcx), %rbx - mov 0x8(%rcx), %rsp - mov 0x10(%rcx), %rbp - mov 0x18(%rcx), %rdi - mov 0x20(%rcx), %rsi - mov 0x28(%rcx), %r12 - mov 0x30(%rcx), %r13 - mov 0x38(%rcx), %r14 - mov 0x40(%rcx), %r15 - # load non-volatile fp registers - ldmxcsr 0x50(%rcx) - movdqu 0x58(%rcx), %xmm6 - movdqu 0x68(%rcx), %xmm7 - movdqu 0x78(%rcx), %xmm8 - movdqu 0x88(%rcx), %xmm9 - movdqu 0x98(%rcx), %xmm10 - movdqu 0xA8(%rcx), %xmm11 - movdqu 0xB8(%rcx), %xmm12 - movdqu 0xC8(%rcx), %xmm13 - movdqu 0xD8(%rcx), %xmm14 - movdqu 0xE8(%rcx), %xmm15 - mov %rdx, %rax # set return value - jmp *0x48(%rcx) diff --git a/MdePkg/Library/BaseLib/X64/RdRand.S b/MdePkg/Library/BaseLib/X64/RdRand.S deleted file mode 100644 index 55724c086..000000000 --- a/MdePkg/Library/BaseLib/X64/RdRand.S +++ /dev/null @@ -1,72 +0,0 @@ -#------------------------------------------------------------------------------ ; -# Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.
-# 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 -# http://opensource.org/licenses/bsd-license.php. -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -# Module Name: -# -# RdRand.S -# -# Abstract: -# -# Generates random number through CPU RdRand instruction under 64-bit platform. -# -# Notes: -# -#------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -// Generates a 16 bit random number through RDRAND instruction. -// Return TRUE if Rand generated successfully, or FALSE if not. -// -// BOOLEAN EFIAPI InternalX86RdRand16 (UINT16 *Rand); -//------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(InternalX86RdRand16) -ASM_PFX(InternalX86RdRand16): - .byte 0x0f, 0xc7, 0xf0 // rdrand r16: "0f c7 /6 ModRM:r/m(w)" - jc rn16_ok // jmp if CF=1 - xor %rax, %rax // reg=0 if CF=0 - ret // return with failure status -rn16_ok: - mov %ax, (%rcx) - mov $0x1, %rax - ret - -//------------------------------------------------------------------------------ -// Generates a 32 bit random number through RDRAND instruction. -// Return TRUE if Rand generated successfully, or FALSE if not. -// -// BOOLEAN EFIAPI InternalX86RdRand32 (UINT32 *Rand); -//------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(InternalX86RdRand32) -ASM_PFX(InternalX86RdRand32): - .byte 0x0f, 0xc7, 0xf0 // rdrand r32: "0f c7 /6 ModRM:r/m(w)" - jc rn32_ok // jmp if CF=1 - xor %rax, %rax // reg=0 if CF=0 - ret // return with failure status -rn32_ok: - mov %eax, (%rcx) - mov $0x1, %rax - ret - -//------------------------------------------------------------------------------ -// Generates a 64 bit random number through RDRAND instruction. -// Return TRUE if Rand generated successfully, or FALSE if not. -// -// BOOLEAN EFIAPI InternalX86RdRand64 (UINT64 *Rand); -//------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(InternalX86RdRand64) -ASM_PFX(InternalX86RdRand64): - .byte 0x48, 0x0f, 0xc7, 0xf0 // rdrand r64: "REX.W + 0f c7 /6 ModRM:r/m(w)" - jc rn64_ok // jmp if CF=1 - xor %rax, %rax // reg=0 if CF=0 - ret // return with failure status -rn64_ok: - mov %rax, (%rcx) - mov $0x1, %rax - ret diff --git a/MdePkg/Library/BaseLib/X64/SetJump.S b/MdePkg/Library/BaseLib/X64/SetJump.S deleted file mode 100644 index ea9e225a9..000000000 --- a/MdePkg/Library/BaseLib/X64/SetJump.S +++ /dev/null @@ -1,53 +0,0 @@ -#------------------------------------------------------------------------------ -# -# Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.
-# 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 -# http://opensource.org/licenses/bsd-license.php. -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -# Module Name: -# -# SetJump.S -# -# Abstract: -# -# Implementation of SetJump() on x86_64 -# -#------------------------------------------------------------------------------ - -ASM_GLOBAL ASM_PFX(SetJump) -ASM_PFX(SetJump): - push %rcx - add $0xffffffffffffffe0,%rsp - call ASM_PFX(InternalAssertJumpBuffer) - add $0x20,%rsp - pop %rcx - pop %rdx - mov %rbx,(%rcx) - mov %rsp,0x8(%rcx) - mov %rbp,0x10(%rcx) - mov %rdi,0x18(%rcx) - mov %rsi,0x20(%rcx) - mov %r12,0x28(%rcx) - mov %r13,0x30(%rcx) - mov %r14,0x38(%rcx) - mov %r15,0x40(%rcx) - mov %rdx,0x48(%rcx) - # save non-volatile fp registers - stmxcsr 0x50(%rcx) - movdqu %xmm6, 0x58(%rcx) - movdqu %xmm7, 0x68(%rcx) - movdqu %xmm8, 0x78(%rcx) - movdqu %xmm9, 0x88(%rcx) - movdqu %xmm10, 0x98(%rcx) - movdqu %xmm11, 0xA8(%rcx) - movdqu %xmm12, 0xB8(%rcx) - movdqu %xmm13, 0xC8(%rcx) - movdqu %xmm14, 0xD8(%rcx) - movdqu %xmm15, 0xE8(%rcx) - xor %rax,%rax - jmpq *%rdx diff --git a/MdePkg/Library/BaseLib/X64/SwitchStack.S b/MdePkg/Library/BaseLib/X64/SwitchStack.S deleted file mode 100644 index dc8d80e40..000000000 --- a/MdePkg/Library/BaseLib/X64/SwitchStack.S +++ /dev/null @@ -1,52 +0,0 @@ -#------------------------------------------------------------------------------ -# -# Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.
-# 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 -# http://opensource.org/licenses/bsd-license.php. -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -# Module Name: -# -# SwitchStack.S -# -# Abstract: -# -#------------------------------------------------------------------------------ - - -#------------------------------------------------------------------------------ -# Routine Description: -# -# Routine for switching stacks with 2 parameters -# -# Arguments: -# -# (rcx) EntryPoint - Entry point with new stack. -# (rdx) Context1 - Parameter1 for entry point. -# (r8) Context2 - Parameter2 for entry point. -# (r9) NewStack - The pointer to new stack. -# -# Returns: -# -# None -# -#------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(InternalSwitchStack) -ASM_PFX(InternalSwitchStack): - pushq %rbp - movq %rsp, %rbp - - mov %rcx, %rax // Shift registers for new call - mov %rdx, %rcx - mov %r8, %rdx - # - # Reserve space for register parameters (rcx, rdx, r8 & r9) on the stack, - # in case the callee wishes to spill them. - # - lea -0x20(%r9), %rsp - pushq $0 // stop gdb stack unwind - jmp *%rax // call EntryPoint () diff --git a/MdePkg/Library/BaseLib/X64/Thunk16.S b/MdePkg/Library/BaseLib/X64/Thunk16.S deleted file mode 100644 index f592a2868..000000000 --- a/MdePkg/Library/BaseLib/X64/Thunk16.S +++ /dev/null @@ -1,334 +0,0 @@ -#------------------------------------------------------------------------------ -# -# Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.
-# 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 -# http://opensource.org/licenses/bsd-license.php. -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -# Module Name: -# -# Thunk16.S -# -# Abstract: -# -# Real mode thunk -# -#------------------------------------------------------------------------------ - -#include - -ASM_GLOBAL ASM_PFX(m16Start) -ASM_GLOBAL ASM_PFX(m16Size) -ASM_GLOBAL ASM_PFX(mThunk16Attr) -ASM_GLOBAL ASM_PFX(m16Gdt) -ASM_GLOBAL ASM_PFX(m16GdtrBase) -ASM_GLOBAL ASM_PFX(mTransition) -ASM_GLOBAL ASM_PFX(InternalAsmThunk16) - -# define the structure of IA32_REGS -.set _EDI, 0 #size 4 -.set _ESI, 4 #size 4 -.set _EBP, 8 #size 4 -.set _ESP, 12 #size 4 -.set _EBX, 16 #size 4 -.set _EDX, 20 #size 4 -.set _ECX, 24 #size 4 -.set _EAX, 28 #size 4 -.set _DS, 32 #size 2 -.set _ES, 34 #size 2 -.set _FS, 36 #size 2 -.set _GS, 38 #size 2 -.set _EFLAGS, 40 #size 8 -.set _EIP, 48 #size 4 -.set _CS, 52 #size 2 -.set _SS, 54 #size 2 -.set IA32_REGS_SIZE, 56 - - .data - -.set Lm16Size, ASM_PFX(InternalAsmThunk16) - ASM_PFX(m16Start) -ASM_PFX(m16Size): .word Lm16Size -.set LmThunk16Attr, L_ThunkAttr - ASM_PFX(m16Start) -ASM_PFX(mThunk16Attr): .word LmThunk16Attr -.set Lm16Gdt, ASM_PFX(NullSeg) - ASM_PFX(m16Start) -ASM_PFX(m16Gdt): .word Lm16Gdt -.set Lm16GdtrBase, _16GdtrBase - ASM_PFX(m16Start) -ASM_PFX(m16GdtrBase): .word Lm16GdtrBase -.set LmTransition, _EntryPoint - ASM_PFX(m16Start) -ASM_PFX(mTransition): .word LmTransition - - .text - -ASM_PFX(m16Start): - -SavedGdt: .space 10 - -#------------------------------------------------------------------------------ -# _BackFromUserCode() takes control in real mode after 'retf' has been executed -# by user code. It will be shadowed to somewhere in memory below 1MB. -#------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(BackFromUserCode) -ASM_PFX(BackFromUserCode): - # - # The order of saved registers on the stack matches the order they appears - # in IA32_REGS structure. This facilitates wrapper function to extract them - # into that structure. - # - # Some instructions for manipulation of segment registers have to be written - # in opcode since 64-bit MASM prevents accesses to those registers. - # - .byte 0x16 # push ss - .byte 0xe # push cs - .byte 0x66 - call L_Base # push eip -L_Base: - .byte 0x66 - pushq $0 # reserved high order 32 bits of EFlags - .byte 0x66, 0x9c # pushfd actually - cli # disable interrupts - push %gs - push %fs - .byte 6 # push es - .byte 0x1e # push ds - .byte 0x66,0x60 # pushad - .byte 0x66,0xba # mov edx, imm32 -L_ThunkAttr: .space 4 - testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15, %dl - jz L_1 - movl $0x15cd2401,%eax # mov ax, 2401h & int 15h - cli # disable interrupts - jnc L_2 -L_1: - testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL, %dl - jz L_2 - inb $0x92,%al - orb $2,%al - outb %al, $0x92 # deactivate A20M# -L_2: - xorw %ax, %ax # xor eax, eax - movl %ss, %eax # mov ax, ss - lea IA32_REGS_SIZE(%esp), %bp - # - # rsi in the following 2 instructions is indeed bp in 16-bit code - # - movw %bp, (_ESP - IA32_REGS_SIZE)(%rsi) - .byte 0x66 - movl (_EIP - IA32_REGS_SIZE)(%rsi), %ebx - shlw $4,%ax # shl eax, 4 - addw %ax,%bp # add ebp, eax - movw %cs,%ax - shlw $4,%ax - lea (L_64BitCode - L_Base)(%ebx, %eax), %ax - .byte 0x66,0x2e,0x89,0x87 # mov cs:[bx + (L_64Eip - L_Base)], eax - .word L_64Eip - L_Base - .byte 0x66,0xb8 # mov eax, imm32 -L_SavedCr4: .space 4 - movq %rax, %cr4 - # - # rdi in the instruction below is indeed bx in 16-bit code - # - .byte 0x66,0x2e # 2eh is "cs:" segment override - lgdt (SavedGdt - L_Base)(%rdi) - .byte 0x66 - movl $0xc0000080,%ecx - rdmsr - orb $1,%ah - wrmsr - .byte 0x66,0xb8 # mov eax, imm32 -L_SavedCr0: .space 4 - movq %rax, %cr0 - .byte 0x66,0xea # jmp far cs:L_64Bit -L_64Eip: .space 4 -L_SavedCs: .space 2 -L_64BitCode: - .byte 0x90 - .byte 0x48,0xbc # mov rsp, imm64 -L_SavedSp: .space 8 # restore stack - nop - ret - -_EntryPoint: .long ASM_PFX(ToUserCode) - ASM_PFX(m16Start) - .word CODE16 -_16Gdtr: .word GDT_SIZE - 1 -_16GdtrBase: .quad 0 -_16Idtr: .word 0x3ff - .long 0 - -#------------------------------------------------------------------------------ -# _ToUserCode() takes control in real mode before passing control to user code. -# It will be shadowed to somewhere in memory below 1MB. -#------------------------------------------------------------------------------ -ASM_GLOBAL ASM_PFX(ToUserCode) -ASM_PFX(ToUserCode): - movl %edx,%ss # set new segment selectors - movl %edx,%ds - movl %edx,%es - movl %edx,%fs - movl %edx,%gs - .byte 0x66 - movl $0xc0000080,%ecx - movq %rax, %cr0 - rdmsr - andb $0xfe, %ah # $0b11111110 - wrmsr - movq %rbp, %cr4 - movl %esi,%ss # set up 16-bit stack segment - movw %bx,%sp # set up 16-bit stack pointer - .byte 0x66 # make the following call 32-bit - call L_Base1 # push eip -L_Base1: - popw %bp # ebp <- address of L_Base1 - pushq (IA32_REGS_SIZE + 2)(%esp) - lea 0x0c(%rsi), %eax - pushq %rax - lret # execution begins at next instruction -L_RealMode: - .byte 0x66,0x2e # CS and operand size override - lidt (_16Idtr - L_Base1)(%rsi) - .byte 0x66,0x61 # popad - .byte 0x1f # pop ds - .byte 0x7 # pop es - .byte 0x0f, 0xa1 # pop fs - .byte 0x0f, 0xa9 # pop gs - .byte 0x66, 0x9d # popfd - leaw 4(%esp),%sp # skip high order 32 bits of EFlags - .byte 0x66 # make the following retf 32-bit - lret # transfer control to user code - -.set CODE16, ASM_PFX(_16Code) - . -.set DATA16, ASM_PFX(_16Data) - . -.set DATA32, ASM_PFX(_32Data) - . - -ASM_PFX(NullSeg): .quad 0 -ASM_PFX(_16Code): - .word -1 - .word 0 - .byte 0 - .byte 0x9b - .byte 0x8f # 16-bit segment, 4GB limit - .byte 0 -ASM_PFX(_16Data): - .word -1 - .word 0 - .byte 0 - .byte 0x93 - .byte 0x8f # 16-bit segment, 4GB limit - .byte 0 -ASM_PFX(_32Data): - .word -1 - .word 0 - .byte 0 - .byte 0x93 - .byte 0xcf # 16-bit segment, 4GB limit - .byte 0 - -.set GDT_SIZE, . - ASM_PFX(NullSeg) - -#------------------------------------------------------------------------------ -# IA32_REGISTER_SET * -# EFIAPI -# InternalAsmThunk16 ( -# IN IA32_REGISTER_SET *RegisterSet, -# IN OUT VOID *Transition -# ); -#------------------------------------------------------------------------------ - -ASM_GLOBAL ASM_PFX(InternalAsmThunk16) -ASM_PFX(InternalAsmThunk16): - pushq %rbp - pushq %rbx - pushq %rsi - pushq %rdi - - movl %ds, %ebx - pushq %rbx # Save ds segment register on the stack - movl %es, %ebx - pushq %rbx # Save es segment register on the stack - movl %ss, %ebx - pushq %rbx # Save ss segment register on the stack - - .byte 0x0f, 0xa0 #push fs - .byte 0x0f, 0xa8 #push gs - movq %rcx, %rsi - movzwl _SS(%rsi), %r8d - movl _ESP(%rsi), %edi - lea -(IA32_REGS_SIZE + 4)(%edi), %rdi - imul $16, %r8d, %eax - movl %edi,%ebx # ebx <- stack for 16-bit code - pushq $(IA32_REGS_SIZE / 4) - addl %eax,%edi # edi <- linear address of 16-bit stack - popq %rcx - rep - movsl # copy RegSet - lea (L_SavedCr4 - ASM_PFX(m16Start))(%rdx), %ecx - movl %edx,%eax # eax <- transition code address - andl $0xf,%edx - shll $12,%eax # segment address in high order 16 bits - .set LBackFromUserCodeDelta, ASM_PFX(BackFromUserCode) - ASM_PFX(m16Start) - lea (LBackFromUserCodeDelta)(%rdx), %ax - stosl # [edi] <- return address of user code - sgdt 0x60(%rsp) # save GDT stack in argument space - movzwq 0x60(%rsp), %r10 # r10 <- GDT limit - lea ((ASM_PFX(InternalAsmThunk16) - L_SavedCr4) + 0xf)(%rcx), %r11 - andq $0xfffffffffffffff0, %r11 # r11 <- 16-byte aligned shadowed GDT table in real mode buffer - - movw %r10w, (SavedGdt - L_SavedCr4)(%rcx) # save the limit of shadowed GDT table - movq %r11, (SavedGdt - L_SavedCr4 + 0x2)(%rcx) # save the base address of shadowed GDT table - - movq 0x62(%rsp) ,%rsi # rsi <- the original GDT base address - xchg %r10, %rcx # save rcx to r10 and initialize rcx to be the limit of GDT table - incq %rcx # rcx <- the size of memory to copy - xchg %r11, %rdi # save rdi to r11 and initialize rdi to the base address of shadowed GDT table - rep - movsb # perform memory copy to shadow GDT table - movq %r10, %rcx # restore the orignal rcx before memory copy - movq %r11, %rdi # restore the original rdi before memory copy - - sidt 0x50(%rsp) - movq %cr0, %rax - .set LSavedCrDelta, L_SavedCr0 - L_SavedCr4 - movl %eax, (LSavedCrDelta)(%rcx) - andl $0x7ffffffe,%eax # clear PE, PG bits - movq %cr4, %rbp - movl %ebp, (%rcx) # save CR4 in SavedCr4 - andl $0xffffffcf,%ebp # clear PAE, PSE bits - movl %r8d, %esi # esi <- 16-bit stack segment - .byte 0x6a, DATA32 - popq %rdx - lgdt (_16Gdtr - L_SavedCr4)(%rcx) - movl %edx,%ss - pushfq - lea -8(%rdx), %edx - lea L_RetFromRealMode(%rip), %r8 - pushq %r8 - movl %cs, %r8d - movw %r8w, (L_SavedCs - L_SavedCr4)(%rcx) - movq %rsp, (L_SavedSp - L_SavedCr4)(%rcx) - .byte 0xff, 0x69 # jmp (_EntryPoint - L_SavedCr4)(%rcx) - .set Ltemp1, _EntryPoint - L_SavedCr4 - .byte Ltemp1 -L_RetFromRealMode: - popfq - lgdt 0x60(%rsp) # restore protected mode GDTR - lidt 0x50(%rsp) # restore protected mode IDTR - lea -IA32_REGS_SIZE(%rbp), %eax - .byte 0x0f, 0xa9 # pop gs - .byte 0x0f, 0xa1 # pop fs - - popq %rbx - movl %ebx, %ss - popq %rbx - movl %ebx, %es - popq %rbx - movl %ebx, %ds - - popq %rdi - popq %rsi - popq %rbx - popq %rbp - - ret diff --git a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c b/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c index cc229652e..96913c5c0 100644 --- a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c +++ b/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c @@ -2,13 +2,7 @@ Provides interface to EFI_FILE_HANDLE functionality. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
- 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 - http://opensource.org/licenses/bsd-license.php - - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + SPDX-License-Identifier: BSD-2-Clause-Patent **/ @@ -73,7 +67,6 @@ FileHandleGetInfo ( // // error is expected. getting size to allocate // - FileInfoSize += 2; FileInfo = AllocateZeroPool(FileInfoSize); // // now get the information @@ -503,13 +496,7 @@ FileHandleFindFirstFile ( // read in the info about the first file // Status = FileHandleRead (DirHandle, &BufferSize, *Buffer); -// ASSERT(Status != EFI_BUFFER_TOO_SMALL); - if (Status == EFI_BUFFER_TOO_SMALL) { - BufferSize += 2; - FreePool(*Buffer); - *Buffer = AllocateZeroPool(BufferSize); - Status = FileHandleRead (DirHandle, &BufferSize, *Buffer); - } + ASSERT(Status != EFI_BUFFER_TOO_SMALL); if (EFI_ERROR(Status) || BufferSize == 0) { FreePool(*Buffer); *Buffer = NULL; @@ -517,8 +504,6 @@ FileHandleFindFirstFile ( return (EFI_NOT_FOUND); } return (Status); - } else if ((*Buffer)->Size == 0) { - (*Buffer)->Size = BufferSize; } return (EFI_SUCCESS); } @@ -559,7 +544,7 @@ FileHandleFindNextFile( // // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile // - BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE; //Buffer->Size; // + BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE; // // read in the info about the next file @@ -576,8 +561,6 @@ FileHandleFindNextFile( if (BufferSize == 0) { FreePool(Buffer); *NoFile = TRUE; - } else if (Buffer->Size == 0) { - Buffer->Size = BufferSize; } return (EFI_SUCCESS); @@ -831,25 +814,10 @@ FileHandleGetFileName ( Status = EFI_OUT_OF_RESOURCES; break; } else { - // - // Prepare to move to the parent directory. - // Also determine whether CurrentHandle refers to the Root directory. - // - Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0); // // We got info... do we have a name? if yes precede the current path with it... // - if ((StrLen (FileInfo->FileName) == 0) || EFI_ERROR (Status)) { - // - // Both FileInfo->FileName being '\0' and EFI_ERROR() suggest that - // CurrentHandle refers to the Root directory. As this loop ensures - // FullFileName is starting with '\\' at all times, signal success - // and exit the loop. - // While FileInfo->FileName could theoretically be a value other than - // '\0' or '\\', '\\' is guaranteed to be supported by the - // specification and hence its value can safely be ignored. - // - Status = EFI_SUCCESS; + if (StrLen (FileInfo->FileName) == 0) { if (*FullFileName == NULL) { ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL)); *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0); @@ -867,11 +835,15 @@ FileHandleGetFileName ( FreePool(FileInfo); } } - - FileHandleClose(CurrentHandle); // // Move to the parent directory // + Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0); + if (EFI_ERROR (Status)) { + break; + } + + FileHandleClose(CurrentHandle); CurrentHandle = NextHigherHandle; } } else if (Status == EFI_NOT_FOUND) { diff --git a/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c index 06e3863cf..554b6a7e8 100644 --- a/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c @@ -141,15 +141,9 @@ FreePages ( { EFI_STATUS Status; -// ASSERT (Pages != 0); - if (!Pages) { - return; - } + ASSERT (Pages != 0); Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); - // ASSERT_EFI_ERROR (Status); - if (EFI_ERROR (Status)) { - return; - } + ASSERT_EFI_ERROR (Status); } /** @@ -187,10 +181,7 @@ InternalAllocateAlignedPages ( // // Alignment must be a power of two or zero. // -// ASSERT ((Alignment & (Alignment - 1)) == 0); - if ((Alignment & (Alignment - 1)) != 0) { - return NULL; - } + ASSERT ((Alignment & (Alignment - 1)) == 0); if (Pages == 0) { return NULL; @@ -204,10 +195,7 @@ InternalAllocateAlignedPages ( // // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. // - // ASSERT (RealPages > Pages); - if (RealPages < Pages) { - return NULL; - } + ASSERT (RealPages > Pages); Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory); if (EFI_ERROR (Status)) { @@ -220,10 +208,7 @@ InternalAllocateAlignedPages ( // Free first unaligned page(s). // Status = gBS->FreePages (Memory, UnalignedPages); - // ASSERT_EFI_ERROR (Status); - if (EFI_ERROR (Status)) { - return NULL; - } + ASSERT_EFI_ERROR (Status); } Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages); UnalignedPages = RealPages - Pages - UnalignedPages; @@ -232,10 +217,7 @@ InternalAllocateAlignedPages ( // Free last unaligned page(s). // Status = gBS->FreePages (Memory, UnalignedPages); - // ASSERT_EFI_ERROR (Status); - if (EFI_ERROR (Status)) { - return NULL; - } + ASSERT_EFI_ERROR (Status); } } else { // @@ -358,14 +340,11 @@ FreeAlignedPages ( IN UINTN Pages ) { -// EFI_STATUS Status; + EFI_STATUS Status; -// ASSERT (Pages != 0); - if (!Pages || !Buffer) { - return; - } - /* Status = */gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); -// ASSERT_EFI_ERROR (Status); + ASSERT (Pages != 0); + Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); + ASSERT_EFI_ERROR (Status); } /** @@ -581,14 +560,8 @@ InternalAllocateCopyPool ( { VOID *Memory; -// ASSERT (Buffer != NULL); - if (!Buffer) { - return NULL; - } -// ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1)); - if (AllocationSize > (MAX_ADDRESS - (UINTN) Buffer + 1)) { - return NULL; - } + ASSERT (Buffer != NULL); + ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1)); Memory = InternalAllocatePool (PoolType, AllocationSize); if (Memory != NULL) { @@ -834,11 +807,9 @@ FreePool ( IN VOID *Buffer ) { -// EFI_STATUS Status; - if (Buffer) { + EFI_STATUS Status; -/* Status = */gBS->FreePool (Buffer); -// ASSERT_EFI_ERROR (Status); - } + Status = gBS->FreePool (Buffer); + ASSERT_EFI_ERROR (Status); } diff --git a/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c.bak b/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c.bak deleted file mode 100644 index 554b6a7e8..000000000 --- a/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c.bak +++ /dev/null @@ -1,815 +0,0 @@ -/** @file - Support routines for memory allocation routines based - on boot services for Dxe phase drivers. - - Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
- SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - - -#include - - -#include -#include -#include -#include - -/** - Allocates one or more 4KB pages of a certain memory type. - - Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated - buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned. - If there is not enough memory remaining to satisfy the request, then NULL is returned. - - @param MemoryType The type of memory to allocate. - @param Pages The number of 4 KB pages to allocate. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -InternalAllocatePages ( - IN EFI_MEMORY_TYPE MemoryType, - IN UINTN Pages - ) -{ - EFI_STATUS Status; - EFI_PHYSICAL_ADDRESS Memory; - - if (Pages == 0) { - return NULL; - } - - Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory); - if (EFI_ERROR (Status)) { - return NULL; - } - return (VOID *) (UINTN) Memory; -} - -/** - Allocates one or more 4KB pages of type EfiBootServicesData. - - Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the - allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL - is returned. If there is not enough memory remaining to satisfy the request, then NULL is - returned. - - @param Pages The number of 4 KB pages to allocate. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocatePages ( - IN UINTN Pages - ) -{ - return InternalAllocatePages (EfiBootServicesData, Pages); -} - -/** - Allocates one or more 4KB pages of type EfiRuntimeServicesData. - - Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the - allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL - is returned. If there is not enough memory remaining to satisfy the request, then NULL is - returned. - - @param Pages The number of 4 KB pages to allocate. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateRuntimePages ( - IN UINTN Pages - ) -{ - return InternalAllocatePages (EfiRuntimeServicesData, Pages); -} - -/** - Allocates one or more 4KB pages of type EfiReservedMemoryType. - - Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the - allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL - is returned. If there is not enough memory remaining to satisfy the request, then NULL is - returned. - - @param Pages The number of 4 KB pages to allocate. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateReservedPages ( - IN UINTN Pages - ) -{ - return InternalAllocatePages (EfiReservedMemoryType, Pages); -} - -/** - Frees one or more 4KB pages that were previously allocated with one of the page allocation - functions in the Memory Allocation Library. - - Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer - must have been allocated on a previous call to the page allocation services of the Memory - Allocation Library. If it is not possible to free allocated pages, then this function will - perform no actions. - - If Buffer was not allocated with a page allocation function in the Memory Allocation Library, - then ASSERT(). - If Pages is zero, then ASSERT(). - - @param Buffer The pointer to the buffer of pages to free. - @param Pages The number of 4 KB pages to free. - -**/ -VOID -EFIAPI -FreePages ( - IN VOID *Buffer, - IN UINTN Pages - ) -{ - EFI_STATUS Status; - - ASSERT (Pages != 0); - Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); - ASSERT_EFI_ERROR (Status); -} - -/** - Allocates one or more 4KB pages of a certain memory type at a specified alignment. - - Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment - specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. - If there is not enough memory at the specified alignment remaining to satisfy the request, then - NULL is returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). - - @param MemoryType The type of memory to allocate. - @param Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -InternalAllocateAlignedPages ( - IN EFI_MEMORY_TYPE MemoryType, - IN UINTN Pages, - IN UINTN Alignment - ) -{ - EFI_STATUS Status; - EFI_PHYSICAL_ADDRESS Memory; - UINTN AlignedMemory; - UINTN AlignmentMask; - UINTN UnalignedPages; - UINTN RealPages; - - // - // Alignment must be a power of two or zero. - // - ASSERT ((Alignment & (Alignment - 1)) == 0); - - if (Pages == 0) { - return NULL; - } - if (Alignment > EFI_PAGE_SIZE) { - // - // Calculate the total number of pages since alignment is larger than page size. - // - AlignmentMask = Alignment - 1; - RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment); - // - // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. - // - ASSERT (RealPages > Pages); - - Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory); - if (EFI_ERROR (Status)) { - return NULL; - } - AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask; - UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory); - if (UnalignedPages > 0) { - // - // Free first unaligned page(s). - // - Status = gBS->FreePages (Memory, UnalignedPages); - ASSERT_EFI_ERROR (Status); - } - Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages); - UnalignedPages = RealPages - Pages - UnalignedPages; - if (UnalignedPages > 0) { - // - // Free last unaligned page(s). - // - Status = gBS->FreePages (Memory, UnalignedPages); - ASSERT_EFI_ERROR (Status); - } - } else { - // - // Do not over-allocate pages in this case. - // - Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory); - if (EFI_ERROR (Status)) { - return NULL; - } - AlignedMemory = (UINTN) Memory; - } - return (VOID *) AlignedMemory; -} - -/** - Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment. - - Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an - alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is - returned. If there is not enough memory at the specified alignment remaining to satisfy the - request, then NULL is returned. - - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). - - @param Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateAlignedPages ( - IN UINTN Pages, - IN UINTN Alignment - ) -{ - return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment); -} - -/** - Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment. - - Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an - alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is - returned. If there is not enough memory at the specified alignment remaining to satisfy the - request, then NULL is returned. - - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). - - @param Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateAlignedRuntimePages ( - IN UINTN Pages, - IN UINTN Alignment - ) -{ - return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment); -} - -/** - Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment. - - Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an - alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is - returned. If there is not enough memory at the specified alignment remaining to satisfy the - request, then NULL is returned. - - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). - - @param Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateAlignedReservedPages ( - IN UINTN Pages, - IN UINTN Alignment - ) -{ - return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment); -} - -/** - Frees one or more 4KB pages that were previously allocated with one of the aligned page - allocation functions in the Memory Allocation Library. - - Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer - must have been allocated on a previous call to the aligned page allocation services of the Memory - Allocation Library. If it is not possible to free allocated pages, then this function will - perform no actions. - - If Buffer was not allocated with an aligned page allocation function in the Memory Allocation - Library, then ASSERT(). - If Pages is zero, then ASSERT(). - - @param Buffer The pointer to the buffer of pages to free. - @param Pages The number of 4 KB pages to free. - -**/ -VOID -EFIAPI -FreeAlignedPages ( - IN VOID *Buffer, - IN UINTN Pages - ) -{ - EFI_STATUS Status; - - ASSERT (Pages != 0); - Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); - ASSERT_EFI_ERROR (Status); -} - -/** - Allocates a buffer of a certain pool type. - - Allocates the number bytes specified by AllocationSize of a certain pool type and returns a - pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is - returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. - - @param MemoryType The type of memory to allocate. - @param AllocationSize The number of bytes to allocate. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -InternalAllocatePool ( - IN EFI_MEMORY_TYPE MemoryType, - IN UINTN AllocationSize - ) -{ - EFI_STATUS Status; - VOID *Memory; - - Status = gBS->AllocatePool (MemoryType, AllocationSize, &Memory); - if (EFI_ERROR (Status)) { - Memory = NULL; - } - return Memory; -} - -/** - Allocates a buffer of type EfiBootServicesData. - - Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a - pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is - returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. - - @param AllocationSize The number of bytes to allocate. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocatePool ( - IN UINTN AllocationSize - ) -{ - return InternalAllocatePool (EfiBootServicesData, AllocationSize); -} - -/** - Allocates a buffer of type EfiRuntimeServicesData. - - Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns - a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is - returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. - - @param AllocationSize The number of bytes to allocate. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateRuntimePool ( - IN UINTN AllocationSize - ) -{ - return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize); -} - -/** - Allocates a buffer of type EfiReservedMemoryType. - - Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns - a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is - returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. - - @param AllocationSize The number of bytes to allocate. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateReservedPool ( - IN UINTN AllocationSize - ) -{ - return InternalAllocatePool (EfiReservedMemoryType, AllocationSize); -} - -/** - Allocates and zeros a buffer of a certain pool type. - - Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer - with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid - buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, - then NULL is returned. - - @param PoolType The type of memory to allocate. - @param AllocationSize The number of bytes to allocate and zero. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -InternalAllocateZeroPool ( - IN EFI_MEMORY_TYPE PoolType, - IN UINTN AllocationSize - ) -{ - VOID *Memory; - - Memory = InternalAllocatePool (PoolType, AllocationSize); - if (Memory != NULL) { - Memory = ZeroMem (Memory, AllocationSize); - } - return Memory; -} - -/** - Allocates and zeros a buffer of type EfiBootServicesData. - - Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the - buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a - valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the - request, then NULL is returned. - - @param AllocationSize The number of bytes to allocate and zero. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateZeroPool ( - IN UINTN AllocationSize - ) -{ - return InternalAllocateZeroPool (EfiBootServicesData, AllocationSize); -} - -/** - Allocates and zeros a buffer of type EfiRuntimeServicesData. - - Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the - buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a - valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the - request, then NULL is returned. - - @param AllocationSize The number of bytes to allocate and zero. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateRuntimeZeroPool ( - IN UINTN AllocationSize - ) -{ - return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize); -} - -/** - Allocates and zeros a buffer of type EfiReservedMemoryType. - - Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the - buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a - valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the - request, then NULL is returned. - - @param AllocationSize The number of bytes to allocate and zero. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateReservedZeroPool ( - IN UINTN AllocationSize - ) -{ - return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize); -} - -/** - Copies a buffer to an allocated buffer of a certain pool type. - - Allocates the number bytes specified by AllocationSize of a certain pool type, copies - AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the - allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there - is not enough memory remaining to satisfy the request, then NULL is returned. - If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). - - @param PoolType The type of pool to allocate. - @param AllocationSize The number of bytes to allocate and zero. - @param Buffer The buffer to copy to the allocated buffer. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -InternalAllocateCopyPool ( - IN EFI_MEMORY_TYPE PoolType, - IN UINTN AllocationSize, - IN CONST VOID *Buffer - ) -{ - VOID *Memory; - - ASSERT (Buffer != NULL); - ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1)); - - Memory = InternalAllocatePool (PoolType, AllocationSize); - if (Memory != NULL) { - Memory = CopyMem (Memory, Buffer, AllocationSize); - } - return Memory; -} - -/** - Copies a buffer to an allocated buffer of type EfiBootServicesData. - - Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies - AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the - allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there - is not enough memory remaining to satisfy the request, then NULL is returned. - - If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). - - @param AllocationSize The number of bytes to allocate and zero. - @param Buffer The buffer to copy to the allocated buffer. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateCopyPool ( - IN UINTN AllocationSize, - IN CONST VOID *Buffer - ) -{ - return InternalAllocateCopyPool (EfiBootServicesData, AllocationSize, Buffer); -} - -/** - Copies a buffer to an allocated buffer of type EfiRuntimeServicesData. - - Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies - AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the - allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there - is not enough memory remaining to satisfy the request, then NULL is returned. - - If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). - - @param AllocationSize The number of bytes to allocate and zero. - @param Buffer The buffer to copy to the allocated buffer. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateRuntimeCopyPool ( - IN UINTN AllocationSize, - IN CONST VOID *Buffer - ) -{ - return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer); -} - -/** - Copies a buffer to an allocated buffer of type EfiReservedMemoryType. - - Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies - AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the - allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there - is not enough memory remaining to satisfy the request, then NULL is returned. - - If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). - - @param AllocationSize The number of bytes to allocate and zero. - @param Buffer The buffer to copy to the allocated buffer. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateReservedCopyPool ( - IN UINTN AllocationSize, - IN CONST VOID *Buffer - ) -{ - return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer); -} - -/** - Reallocates a buffer of a specified memory type. - - Allocates and zeros the number bytes specified by NewSize from memory of the type - specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and - NewSize bytes are copied from OldBuffer to the newly allocated buffer, and - OldBuffer is freed. A pointer to the newly allocated buffer is returned. - If NewSize is 0, then a valid buffer of 0 size is returned. If there is not - enough memory remaining to satisfy the request, then NULL is returned. - - If the allocation of the new buffer is successful and the smaller of NewSize and OldSize - is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). - - @param PoolType The type of pool to allocate. - @param OldSize The size, in bytes, of OldBuffer. - @param NewSize The size, in bytes, of the buffer to reallocate. - @param OldBuffer The buffer to copy to the allocated buffer. This is an optional - parameter that may be NULL. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -InternalReallocatePool ( - IN EFI_MEMORY_TYPE PoolType, - IN UINTN OldSize, - IN UINTN NewSize, - IN VOID *OldBuffer OPTIONAL - ) -{ - VOID *NewBuffer; - - NewBuffer = InternalAllocateZeroPool (PoolType, NewSize); - if (NewBuffer != NULL && OldBuffer != NULL) { - CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize)); - FreePool (OldBuffer); - } - return NewBuffer; -} - -/** - Reallocates a buffer of type EfiBootServicesData. - - Allocates and zeros the number bytes specified by NewSize from memory of type - EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and - NewSize bytes are copied from OldBuffer to the newly allocated buffer, and - OldBuffer is freed. A pointer to the newly allocated buffer is returned. - If NewSize is 0, then a valid buffer of 0 size is returned. If there is not - enough memory remaining to satisfy the request, then NULL is returned. - - If the allocation of the new buffer is successful and the smaller of NewSize and OldSize - is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). - - @param OldSize The size, in bytes, of OldBuffer. - @param NewSize The size, in bytes, of the buffer to reallocate. - @param OldBuffer The buffer to copy to the allocated buffer. This is an optional - parameter that may be NULL. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -ReallocatePool ( - IN UINTN OldSize, - IN UINTN NewSize, - IN VOID *OldBuffer OPTIONAL - ) -{ - return InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer); -} - -/** - Reallocates a buffer of type EfiRuntimeServicesData. - - Allocates and zeros the number bytes specified by NewSize from memory of type - EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and - NewSize bytes are copied from OldBuffer to the newly allocated buffer, and - OldBuffer is freed. A pointer to the newly allocated buffer is returned. - If NewSize is 0, then a valid buffer of 0 size is returned. If there is not - enough memory remaining to satisfy the request, then NULL is returned. - - If the allocation of the new buffer is successful and the smaller of NewSize and OldSize - is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). - - @param OldSize The size, in bytes, of OldBuffer. - @param NewSize The size, in bytes, of the buffer to reallocate. - @param OldBuffer The buffer to copy to the allocated buffer. This is an optional - parameter that may be NULL. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -ReallocateRuntimePool ( - IN UINTN OldSize, - IN UINTN NewSize, - IN VOID *OldBuffer OPTIONAL - ) -{ - return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer); -} - -/** - Reallocates a buffer of type EfiReservedMemoryType. - - Allocates and zeros the number bytes specified by NewSize from memory of type - EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and - NewSize bytes are copied from OldBuffer to the newly allocated buffer, and - OldBuffer is freed. A pointer to the newly allocated buffer is returned. - If NewSize is 0, then a valid buffer of 0 size is returned. If there is not - enough memory remaining to satisfy the request, then NULL is returned. - - If the allocation of the new buffer is successful and the smaller of NewSize and OldSize - is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). - - @param OldSize The size, in bytes, of OldBuffer. - @param NewSize The size, in bytes, of the buffer to reallocate. - @param OldBuffer The buffer to copy to the allocated buffer. This is an optional - parameter that may be NULL. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -ReallocateReservedPool ( - IN UINTN OldSize, - IN UINTN NewSize, - IN VOID *OldBuffer OPTIONAL - ) -{ - return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer); -} - -/** - Frees a buffer that was previously allocated with one of the pool allocation functions in the - Memory Allocation Library. - - Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the - pool allocation services of the Memory Allocation Library. If it is not possible to free pool - resources, then this function will perform no actions. - - If Buffer was not allocated with a pool allocation function in the Memory Allocation Library, - then ASSERT(). - - @param Buffer The pointer to the buffer to free. - -**/ -VOID -EFIAPI -FreePool ( - IN VOID *Buffer - ) -{ - EFI_STATUS Status; - - Status = gBS->FreePool (Buffer); - ASSERT_EFI_ERROR (Status); -} - diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec index ead69adc7..3fd7d1634 100644 --- a/MdePkg/MdePkg.dec +++ b/MdePkg/MdePkg.dec @@ -324,9 +324,6 @@ ## Include/Guid/Mps.h gEfiMpsTableGuid = { 0xEB9D2D2F, 0x2D88, 0x11D3, { 0x9A, 0x16, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }} - ## Include/Guid/SalSystemTable.h - gEfiSalSystemTableGuid = { 0xEB9D2D32, 0x2D88, 0x11D3, { 0x9A, 0x16, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }} - ## Include/Protocol/AuthenticationInfo.h gEfiAuthenticationChapLocalGuid = { 0xC280C73E, 0x15CA, 0x11DA, { 0xB0, 0xCA, 0x00, 0x10, 0x83, 0xFF, 0xCA, 0x4D }} @@ -1136,29 +1133,6 @@ ## Include/Protocol/LegacyRegion2.h gEfiLegacyRegion2ProtocolGuid = {0x70101eaf, 0x85, 0x440c, {0xb3, 0x56, 0x8e, 0xe3, 0x6f, 0xef, 0x24, 0xf0 } } - ## Include/Protocol/McaInitPmi.h - gEfiSalMcaInitPmiProtocolGuid = { 0xb60dc6e8, 0x3b6f, 0x11d5, {0xaf, 0x9, 0x0, 0xa0, 0xc9, 0x44, 0xa0, 0x5b } } - - ## Include/Protocol/ExtendedSalBootService.h - gEfiExtendedSalBootServiceProtocolGuid = { 0xde0ee9a4, 0x3c7a, 0x44f2, {0xb7, 0x8b, 0xe3, 0xcc, 0xd6, 0x9c, 0x3a, 0xf7 } } - - ## Include/Protocol/ExtendedSalServiceClasses.h - gEfiExtendedSalBaseIoServicesProtocolGuid = { 0x5aea42b5, 0x31e1, 0x4515, {0xbc, 0x31, 0xb8, 0xd5, 0x25, 0x75, 0x65, 0xa6 } } - gEfiExtendedSalStallServicesProtocolGuid = { 0x53a58d06, 0xac27, 0x4d8c, {0xb5, 0xe9, 0xf0, 0x8a, 0x80, 0x65, 0x41, 0x70 } } - gEfiExtendedSalRtcServicesProtocolGuid = { 0x7e97a470, 0xefdb, 0x4d02, {0x8f, 0xce, 0x61, 0x90, 0xd2, 0x7b, 0xa2, 0x96 } } - gEfiExtendedSalVariableServicesProtocolGuid = { 0x4ecb6c53, 0xc641, 0x4370, {0x8c, 0xb2, 0x3b, 0x0e, 0x49, 0x6e, 0x83, 0x78 } } - gEfiExtendedSalMtcServicesProtocolGuid = { 0x899afd18, 0x75e8, 0x408b, {0xa4, 0x1a, 0x6e, 0x2e, 0x7e, 0xcd, 0xf4, 0x54 } } - gEfiExtendedSalResetServicesProtocolGuid = { 0x7d019990, 0x8ce1, 0x46f5, {0xa7, 0x76, 0x3c, 0x51, 0x98, 0x67, 0x6a, 0xa0 } } - gEfiExtendedSalStatusCodeServicesProtocolGuid = { 0xdbd91d, 0x55e9, 0x420f, {0x96, 0x39, 0x5e, 0x9f, 0x84, 0x37, 0xb4, 0x4f } } - gEfiExtendedSalFvBlockServicesProtocolGuid = { 0xa2271df1, 0xbcbb, 0x4f1d, {0x98, 0xa9, 0x06, 0xbc, 0x17, 0x2f, 0x07, 0x1a } } - gEfiExtendedSalMpServicesProtocolGuid = { 0x697d81a2, 0xcf18, 0x4dc0, {0x9e, 0x0d, 0x06, 0x11, 0x3b, 0x61, 0x8a, 0x3f } } - gEfiExtendedSalPalServicesProtocolGuid = { 0xe1cd9d21, 0x0fc2, 0x438d, {0x97, 0x03, 0x04, 0xe6, 0x6d, 0x96, 0x1e, 0x57 } } - gEfiExtendedSalBaseServicesProtocolGuid = { 0xd9e9fa06, 0x0fe0, 0x41c3, {0x96, 0xfb, 0x83, 0x42, 0x5a, 0x33, 0x94, 0xf8 } } - gEfiExtendedSalMcaServicesProtocolGuid = { 0x2a591128, 0x6cc7, 0x42b1, {0x8a, 0xf0, 0x58, 0x93, 0x3b, 0x68, 0x2d, 0xbb } } - gEfiExtendedSalPciServicesProtocolGuid = { 0xa46b1a31, 0xad66, 0x4905, {0x92, 0xf6, 0x2b, 0x46, 0x59, 0xdc, 0x30, 0x63 } } - gEfiExtendedSalCacheServicesProtocolGuid = { 0xedc9494, 0x2743, 0x4ba5, { 0x88, 0x18, 0x0a, 0xef, 0x52, 0x13, 0xf1, 0x88 } } - gEfiExtendedSalMcaLogServicesProtocolGuid = { 0xcb3fd86e, 0x38a3, 0x4c03, {0x9a, 0x5c, 0x90, 0xcf, 0xa3, 0xa2, 0xab, 0x7a } } - # # Protocols defined in PI 1.2.1 # diff --git a/NetworkPkg/Application/VConfig/VConfig.c b/NetworkPkg/Application/VConfig/VConfig.c new file mode 100644 index 000000000..b50b07b97 --- /dev/null +++ b/NetworkPkg/Application/VConfig/VConfig.c @@ -0,0 +1,689 @@ +/** @file + Shell application for VLAN configuration. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +// +// String token ID of VConfig command help message text. +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_STRING_ID mStringVConfigHelpTokenId = STRING_TOKEN (STR_VCONFIG_HELP); + +#define INVALID_NIC_INDEX 0xffff +#define INVALID_VLAN_ID 0xffff + +// +// This is the generated String package data for all .UNI files. +// This data array is ready to be used as input of HiiAddPackages() to +// create a packagelist (which contains Form packages, String packages, etc). +// +extern UINT8 VConfigStrings[]; + +EFI_HANDLE mImageHandle = NULL; +EFI_HII_HANDLE mHiiHandle = NULL; + +SHELL_PARAM_ITEM mParamList[] = { + { + L"-l", + TypeValue + }, + { + L"-a", + TypeMaxValue + }, + { + L"-d", + TypeValue + }, + { + NULL, + TypeMax + } +}; + +/** + Locate the network interface handle buffer. + + @param[out] NumberOfHandles Pointer to the number of handles. + @param[out] HandleBuffer Pointer to the buffer to store the returned handles. + +**/ +VOID +LocateNicHandleBuffer ( + OUT UINTN *NumberOfHandles, + OUT EFI_HANDLE **HandleBuffer + ) +{ + EFI_STATUS Status; + + *NumberOfHandles = 0; + *HandleBuffer = NULL; + + Status = gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiVlanConfigProtocolGuid, + NULL, + NumberOfHandles, + HandleBuffer + ); + if (EFI_ERROR (Status)) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_LOCATE_FAIL), mHiiHandle, Status); + } +} + +/** + Extract the decimal index from the network interface name. + + @param[in] Name Name of the network interface. + + @retval INVALID_NIC_INDEX Failed to extract the network interface index. + @return others The network interface index. + +**/ +UINTN +NicNameToIndex ( + IN CHAR16 *Name + ) +{ + CHAR16 *Str; + + Str = Name + 3; + if ((StrnCmp (Name, L"eth", 3) != 0) || (*Str == 0)) { + return INVALID_NIC_INDEX; + } + + while (*Str != 0) { + if ((*Str < L'0') || (*Str > L'9')) { + return INVALID_NIC_INDEX; + } + + Str++; + } + + return (UINT16) StrDecimalToUintn (Name + 3); +} + +/** + Find network interface device handle by its name. + + @param[in] Name Name of the network interface. + + @retval NULL Cannot find the network interface. + @return others Handle of the network interface. + +**/ +EFI_HANDLE +NicNameToHandle ( + IN CHAR16 *Name + ) +{ + UINTN NumberOfHandles; + EFI_HANDLE *HandleBuffer; + UINTN Index; + EFI_HANDLE Handle; + + // + // Find all NIC handles. + // + LocateNicHandleBuffer (&NumberOfHandles, &HandleBuffer); + if (NumberOfHandles == 0) { + return NULL; + } + + Index = NicNameToIndex (Name); + if (Index >= NumberOfHandles) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_IF), mHiiHandle, Name); + Handle = NULL; + } else { + Handle = HandleBuffer[Index]; + } + + FreePool (HandleBuffer); + return Handle; +} + +/** + Open VlanConfig protocol from a handle. + + @param[in] Handle The handle to open the VlanConfig protocol. + + @return The VlanConfig protocol interface. + +**/ +EFI_VLAN_CONFIG_PROTOCOL * +OpenVlanConfigProtocol ( + IN EFI_HANDLE Handle + ) +{ + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + + VlanConfig = NULL; + gBS->OpenProtocol ( + Handle, + &gEfiVlanConfigProtocolGuid, + (VOID **) &VlanConfig, + mImageHandle, + Handle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + return VlanConfig; +} + +/** + Close VlanConfig protocol of a handle. + + @param[in] Handle The handle to close the VlanConfig protocol. + +**/ +VOID +CloseVlanConfigProtocol ( + IN EFI_HANDLE Handle + ) +{ + gBS->CloseProtocol ( + Handle, + &gEfiVlanConfigProtocolGuid, + mImageHandle, + Handle + ); +} + +/** + Display VLAN configuration of a network interface. + + @param[in] Handle Handle of the network interface. + @param[in] NicIndex Index of the network interface. + +**/ +VOID +ShowNicVlanInfo ( + IN EFI_HANDLE Handle, + IN UINTN NicIndex + ) +{ + CHAR16 *MacStr; + EFI_STATUS Status; + UINTN Index; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + UINT16 NumberOfVlan; + EFI_VLAN_FIND_DATA *VlanData; + + VlanConfig = OpenVlanConfigProtocol (Handle); + if (VlanConfig == NULL) { + return ; + } + + MacStr = NULL; + Status = NetLibGetMacString (Handle, mImageHandle, &MacStr); + if (EFI_ERROR (Status)) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_MAC_FAIL), mHiiHandle, Status); + goto Exit; + } + + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_ETH_MAC), mHiiHandle, NicIndex, MacStr); + + Status = VlanConfig->Find (VlanConfig, NULL, &NumberOfVlan, &VlanData); + if (EFI_ERROR (Status)) { + if (Status == EFI_NOT_FOUND) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_VLAN), mHiiHandle); + } else { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_FIND_FAIL), mHiiHandle, Status); + } + + goto Exit; + } + + for (Index = 0; Index < NumberOfVlan; Index++) { + ShellPrintHiiEx ( + -1, + -1, + NULL, + STRING_TOKEN (STR_VCONFIG_VLAN_DISPLAY), + mHiiHandle, + VlanData[Index].VlanId, + VlanData[Index].Priority + ); + } + + FreePool (VlanData); + +Exit: + CloseVlanConfigProtocol (Handle); + + if (MacStr != NULL) { + FreePool (MacStr); + } +} + +/** + Display the VLAN configuration of all, or a specified network interface. + + @param[in] Name Name of the network interface. If NULL, the VLAN + configuration of all network will be displayed. + +**/ +VOID +DisplayVlan ( + IN CHAR16 *Name OPTIONAL + ) +{ + UINTN NumberOfHandles; + EFI_HANDLE *HandleBuffer; + UINTN Index; + EFI_HANDLE NicHandle; + + if (Name != NULL) { + // + // Display specified NIC + // + NicHandle = NicNameToHandle (Name); + if (NicHandle == NULL) { + return ; + } + + ShowNicVlanInfo (NicHandle, 0); + return ; + } + + // + // Find all NIC handles + // + LocateNicHandleBuffer (&NumberOfHandles, &HandleBuffer); + if (NumberOfHandles == 0) { + return ; + } + + for (Index = 0; Index < NumberOfHandles; Index++) { + ShowNicVlanInfo (HandleBuffer[Index], Index); + } + + FreePool (HandleBuffer); +} + +/** + Convert a NULL-terminated unicode decimal VLAN ID string to VLAN ID. + + @param[in] String Pointer to VLAN ID string from user input. + + @retval Value translated from String, or INVALID_VLAN_ID is string is invalid. + +**/ +UINT16 +StrToVlanId ( + IN CHAR16 *String + ) +{ + CHAR16 *Str; + + if (String == NULL) { + return INVALID_VLAN_ID; + } + + Str = String; + while ((*Str >= '0') && (*Str <= '9')) { + Str++; + } + + if (*Str != 0) { + return INVALID_VLAN_ID; + } + + return (UINT16) StrDecimalToUintn (String); +} + +/** + Add a VLAN device. + + @param[in] ParamStr Parameter string from user input. + +**/ +VOID +AddVlan ( + IN CHAR16 *ParamStr + ) +{ + CHAR16 *Name; + CHAR16 *VlanIdStr; + CHAR16 *PriorityStr; + CHAR16 *StrPtr; + BOOLEAN IsSpace; + UINTN VlanId; + UINTN Priority; + EFI_HANDLE Handle; + EFI_HANDLE VlanHandle; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + EFI_STATUS Status; + + VlanConfig = NULL; + Priority = 0; + + if (ParamStr == NULL) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_IF), mHiiHandle); + return ; + } + + StrPtr = AllocateCopyPool (StrSize (ParamStr), ParamStr); + if (StrPtr == NULL) { + return ; + } + + Name = StrPtr; + VlanIdStr = NULL; + PriorityStr = NULL; + IsSpace = FALSE; + while (*StrPtr != 0) { + if (*StrPtr == L' ') { + *StrPtr = 0; + IsSpace = TRUE; + } else { + if (IsSpace) { + // + // Start of a parameter. + // + if (VlanIdStr == NULL) { + // + // 2nd parameter is VLAN ID. + // + VlanIdStr = StrPtr; + } else if (PriorityStr == NULL) { + // + // 3rd parameter is Priority. + // + PriorityStr = StrPtr; + } else { + // + // Ignore else parameters. + // + break; + } + } + + IsSpace = FALSE; + } + + StrPtr++; + } + + Handle = NicNameToHandle (Name); + if (Handle == NULL) { + goto Exit; + } + + VlanConfig = OpenVlanConfigProtocol (Handle); + if (VlanConfig == NULL) { + goto Exit; + } + + // + // Check VLAN ID. + // + if ((VlanIdStr == NULL) || (*VlanIdStr == 0)) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_VID), mHiiHandle); + goto Exit; + } + + VlanId = StrToVlanId (VlanIdStr); + if (VlanId > 4094) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_VID), mHiiHandle, VlanIdStr); + goto Exit; + } + + // + // Check Priority. + // + if ((PriorityStr != NULL) && (*PriorityStr != 0)) { + Priority = StrDecimalToUintn (PriorityStr); + if (Priority > 7) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_PRIORITY), mHiiHandle, PriorityStr); + goto Exit; + } + } + + // + // Set VLAN + // + Status = VlanConfig->Set (VlanConfig, (UINT16) VlanId, (UINT8) Priority); + if (EFI_ERROR (Status)) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_SET_FAIL), mHiiHandle, Status); + goto Exit; + } + + // + // Connect the VLAN device. + // + VlanHandle = NetLibGetVlanHandle (Handle, (UINT16) VlanId); + if (VlanHandle != NULL) { + gBS->ConnectController (VlanHandle, NULL, NULL, TRUE); + } + + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_SET_SUCCESS), mHiiHandle); + +Exit: + if (VlanConfig != NULL) { + CloseVlanConfigProtocol (Handle); + } + + FreePool (Name); +} + +/** + Remove a VLAN device. + + @param[in] ParamStr Parameter string from user input. + +**/ +VOID +DeleteVlan ( + IN CHAR16 *ParamStr + ) +{ + CHAR16 *Name; + CHAR16 *VlanIdStr; + CHAR16 *StrPtr; + UINTN VlanId; + EFI_HANDLE Handle; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + EFI_STATUS Status; + UINT16 NumberOfVlan; + EFI_VLAN_FIND_DATA *VlanData; + + VlanConfig = NULL; + + if (ParamStr == NULL) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_IF), mHiiHandle); + return ; + } + + StrPtr = AllocateCopyPool (StrSize (ParamStr), ParamStr); + if (StrPtr == NULL) { + return ; + } + + Name = StrPtr; + VlanIdStr = NULL; + while (*StrPtr != 0) { + if (*StrPtr == L'.') { + *StrPtr = 0; + VlanIdStr = StrPtr + 1; + break; + } + + StrPtr++; + } + + Handle = NicNameToHandle (Name); + if (Handle == NULL) { + goto Exit; + } + + VlanConfig = OpenVlanConfigProtocol (Handle); + if (VlanConfig == NULL) { + goto Exit; + } + + // + // Check VLAN ID + // + if (VlanIdStr == NULL || *VlanIdStr == 0) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_VID), mHiiHandle); + goto Exit; + } + + VlanId = StrToVlanId (VlanIdStr); + if (VlanId > 4094) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_VID), mHiiHandle, VlanIdStr); + goto Exit; + } + + // + // Delete VLAN. + // + Status = VlanConfig->Remove (VlanConfig, (UINT16) VlanId); + if (EFI_ERROR (Status)) { + if (Status == EFI_NOT_FOUND) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NOT_FOUND), mHiiHandle); + } else { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_REMOVE_FAIL), mHiiHandle, Status); + } + + goto Exit; + } + + // + // Check whether this is the last VLAN to remove. + // + Status = VlanConfig->Find (VlanConfig, NULL, &NumberOfVlan, &VlanData); + if (EFI_ERROR (Status)) { + // + // This is the last VLAN to remove, try to connect the controller handle. + // + gBS->ConnectController (Handle, NULL, NULL, TRUE); + } else { + FreePool (VlanData); + } + + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_REMOVE_SUCCESS), mHiiHandle); + +Exit: + if (VlanConfig != NULL) { + CloseVlanConfigProtocol (Handle); + } + + FreePool (Name); +} + +/** + The actual entry point for the application. + + @param[in] ImageHandle The firmware allocated handle for the EFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The entry point executed successfully. + @retval other Some error occur when executing this entry point. + +**/ +EFI_STATUS +EFIAPI +VlanConfigMain ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + LIST_ENTRY *List; + CONST CHAR16 *Str; + EFI_HII_PACKAGE_LIST_HEADER *PackageList; + EFI_STATUS Status; + + mImageHandle = ImageHandle; + + // + // Retrieve HII package list from ImageHandle + // + Status = gBS->OpenProtocol ( + ImageHandle, + &gEfiHiiPackageListProtocolGuid, + (VOID **) &PackageList, + ImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Publish HII package list to HII Database. + // + Status = gHiiDatabase->NewPackageList ( + gHiiDatabase, + PackageList, + NULL, + &mHiiHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (mHiiHandle == NULL) { + return EFI_SUCCESS; + } + + List = NULL; + ShellCommandLineParseEx (mParamList, &List, NULL, FALSE, FALSE); + if (List == NULL) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_ARG), mHiiHandle); + goto Exit; + } + + if (ShellCommandLineGetFlag (List, L"-l")) { + Str = ShellCommandLineGetValue (List, L"-l"); + DisplayVlan ((CHAR16 *) Str); + goto Exit; + } + + if (ShellCommandLineGetFlag (List, L"-a")) { + Str = ShellCommandLineGetValue (List, L"-a"); + AddVlan ((CHAR16 *) Str); + goto Exit; + } + + if (ShellCommandLineGetFlag (List, L"-d")) { + Str = ShellCommandLineGetValue (List, L"-d"); + DeleteVlan ((CHAR16 *) Str); + goto Exit; + } + + // + // No valid argument till now. + // + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_ARG), mHiiHandle); + +Exit: + if (List != NULL) { + ShellCommandLineFreeVarList (List); + } + + // + // Remove our string package from HII database. + // + HiiRemovePackages (mHiiHandle); + + return EFI_SUCCESS; +} diff --git a/NetworkPkg/Application/VConfig/VConfig.inf b/NetworkPkg/Application/VConfig/VConfig.inf new file mode 100644 index 000000000..1d7a81201 --- /dev/null +++ b/NetworkPkg/Application/VConfig/VConfig.inf @@ -0,0 +1,56 @@ +## @file +# Shell application VLAN configuration. +# +# It is shell application which is used to get and set VLAN configuration. +# +# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = VConfig + FILE_GUID = 87E36301-0406-44db-AAF3-9E0E591F3725 + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 1.0 + ENTRY_POINT = VlanConfigMain + MODULE_UNI_FILE = VConfig.uni + +# +# +# This flag specifies whether HII resource section is generated into PE image. +# + UEFI_HII_RESOURCE_SECTION = TRUE + +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + VConfigStrings.uni + VConfig.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + ShellPkg/ShellPkg.dec + +[LibraryClasses] + UefiApplicationEntryPoint + UefiBootServicesTableLib + UefiHiiServicesLib + UefiLib + ShellLib + NetLib + MemoryAllocationLib + HiiLib + +[Protocols] + gEfiVlanConfigProtocolGuid ## CONSUMES + gEfiHiiPackageListProtocolGuid ## CONSUMES + +[UserExtensions.TianoCore."ExtraFiles"] + VConfigExtra.uni diff --git a/NetworkPkg/Application/VConfig/VConfig.uni b/NetworkPkg/Application/VConfig/VConfig.uni new file mode 100644 index 000000000..41511e644 --- /dev/null +++ b/NetworkPkg/Application/VConfig/VConfig.uni @@ -0,0 +1,16 @@ +// /** @file +// Shell application VLAN configuration. +// +// It is shell application which is used to get and set VLAN configuration. +// +// Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Shell application VLAN configuration" + +#string STR_MODULE_DESCRIPTION #language en-US "It is shell application which is used to get and set VLAN configuration." + diff --git a/NetworkPkg/Application/VConfig/VConfigExtra.uni b/NetworkPkg/Application/VConfig/VConfigExtra.uni new file mode 100644 index 000000000..1177ae3c9 --- /dev/null +++ b/NetworkPkg/Application/VConfig/VConfigExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// VConfig Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"Vlan Config App" + + diff --git a/NetworkPkg/Application/VConfig/VConfigStrings.uni b/NetworkPkg/Application/VConfig/VConfigStrings.uni new file mode 100644 index 000000000..9caf409f1 --- /dev/null +++ b/NetworkPkg/Application/VConfig/VConfigStrings.uni @@ -0,0 +1,57 @@ +/** @file + String definitions for VLAN configuration Shell application. + + Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#langdef en-US "English" + +#string STR_VCONFIG_LOCATE_FAIL #language en-US "Failed to locate EFI_VLAN_CONFIG_PROTOCOL - %r.\n" +#string STR_VCONFIG_MAC_FAIL #language en-US "Failed to get MAC address - %r.\n" +#string STR_VCONFIG_FIND_FAIL #language en-US "Failed to find VLAN configuration - %r.\n" +#string STR_VCONFIG_SET_FAIL #language en-US "Failed to set VLAN configuration - %r.\n" +#string STR_VCONFIG_REMOVE_FAIL #language en-US "Failed to remove VLAN - %r.\n" +#string STR_VCONFIG_NO_IF #language en-US "Network interface not specified.\n" +#string STR_VCONFIG_NO_VID #language en-US "VLAN ID not specified.\n" +#string STR_VCONFIG_INVALID_IF #language en-US "Invalid network interface - %s.\n" +#string STR_VCONFIG_INVALID_VID #language en-US "Invalid VLAN ID - %s.\n" +#string STR_VCONFIG_INVALID_PRIORITY #language en-US "Invalid VLAN Priority - %s.\n" +#string STR_VCONFIG_NOT_FOUND #language en-US "Cannot find the VLAN device specified.\n" +#string STR_VCONFIG_VLAN_DISPLAY #language en-US " VLAN ID: %4d Priority: %d\n" +#string STR_VCONFIG_NO_VLAN #language en-US " VLAN is not configured.\n" +#string STR_VCONFIG_ETH_MAC #language en-US "eth%d MAC:%s\n" +#string STR_VCONFIG_SET_SUCCESS #language en-US "VLAN device added.\n" +#string STR_VCONFIG_REMOVE_SUCCESS #language en-US "VLAN device removed.\n" +#string STR_VCONFIG_NO_ARG #language en-US "Invalid argument, try "-?" for help.\n" + +#string STR_VCONFIG_HELP #language en-US "" +".TH VConfig 0 "Display or modify VLAN configuration for network interface."\r\n" +".SH NAME\r\n" +"Display or modify VLAN configuration for network interface.\r\n" +".SH SYNOPSIS\r\n" +" \r\n" +"VCONFIG [-?] [-l [IfName]] [-a IfName VlanId [Priority]] [-d IfName.VlanId]\r\n" +".SH OPTIONS\r\n" +" \r\n" +" -l Display VLAN configuration for all or specified interface.\r\n" +" -a Add a VLAN device for the network interface.\r\n" +" -d Delete a VLAN device.\r\n" +" IfName Name of network interface, e.g. eth0, eth1.\r\n" +" VlanId Unique VLAN identifier (0~4094).\r\n" +" Priority 802.1Q priority level (0~7), default 0.\r\n" +".SH EXAMPLES\r\n" +" \r\n" +"Examples:\r\n" +" * To display VLAN configuration:\r\n" +" fs0:\> vconfig -l\r\n" +" fs0:\> vconfig -l eth0\r\n" +"\r\n" +" * To add VLAN device:\r\n" +" fs0:\> vconfig -a eth0 1000\r\n" +" fs0:\> vconfig -a eth0 2000 7\r\n" +"\r\n" +" * To delete VLAN device:\r\n" +" fs0:\> vconfig -d eth0.1000\r\n" diff --git a/NetworkPkg/ArpDxe/ArpDriver.c b/NetworkPkg/ArpDxe/ArpDriver.c new file mode 100644 index 000000000..632d691d7 --- /dev/null +++ b/NetworkPkg/ArpDxe/ArpDriver.c @@ -0,0 +1,811 @@ +/** @file + ARP driver functions. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "ArpDriver.h" +#include "ArpImpl.h" + +EFI_DRIVER_BINDING_PROTOCOL gArpDriverBinding = { + ArpDriverBindingSupported, + ArpDriverBindingStart, + ArpDriverBindingStop, + 0xa, + NULL, + NULL +}; + + +/** + Create and initialize the arp service context data. + + @param[in] ImageHandle The image handle representing the loaded driver + image. + @param[in] ControllerHandle The controller handle the driver binds to. + @param[in, out] ArpService Pointer to the buffer containing the arp service + context data. + + @retval EFI_SUCCESS The arp service context is initialized. + + @retval EFI_UNSUPPORTED The underlayer Snp mode type is not ethernet. + Failed to initialize the service context. + @retval other Failed to initialize the arp service context. + +**/ +EFI_STATUS +ArpCreateService ( + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle, + IN OUT ARP_SERVICE_DATA *ArpService + ) +{ + EFI_STATUS Status; + + ASSERT (ArpService != NULL); + + ArpService->Signature = ARP_SERVICE_DATA_SIGNATURE; + + // + // Init the lists. + // + InitializeListHead (&ArpService->ChildrenList); + InitializeListHead (&ArpService->PendingRequestTable); + InitializeListHead (&ArpService->DeniedCacheTable); + InitializeListHead (&ArpService->ResolvedCacheTable); + + // + // Init the servicebinding protocol members. + // + ArpService->ServiceBinding.CreateChild = ArpServiceBindingCreateChild; + ArpService->ServiceBinding.DestroyChild = ArpServiceBindingDestroyChild; + + // + // Save the handles. + // + ArpService->ImageHandle = ImageHandle; + ArpService->ControllerHandle = ControllerHandle; + + // + // Create a MNP child instance. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + ImageHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + &ArpService->MnpChildHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Open the MNP protocol. + // + Status = gBS->OpenProtocol ( + ArpService->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **)&ArpService->Mnp, + ImageHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ERROR_EXIT; + } + + // + // Get the underlayer Snp mode data. + // + Status = ArpService->Mnp->GetModeData (ArpService->Mnp, NULL, &ArpService->SnpMode); + if ((Status != EFI_NOT_STARTED) && EFI_ERROR (Status)) { + goto ERROR_EXIT; + } + + if (ArpService->SnpMode.IfType != NET_IFTYPE_ETHERNET) { + // + // Only support the ethernet. + // + Status = EFI_UNSUPPORTED; + goto ERROR_EXIT; + } + + // + // Set the Mnp config parameters. + // + ArpService->MnpConfigData.ReceivedQueueTimeoutValue = 0; + ArpService->MnpConfigData.TransmitQueueTimeoutValue = 0; + ArpService->MnpConfigData.ProtocolTypeFilter = ARP_ETHER_PROTO_TYPE; + ArpService->MnpConfigData.EnableUnicastReceive = TRUE; + ArpService->MnpConfigData.EnableMulticastReceive = FALSE; + ArpService->MnpConfigData.EnableBroadcastReceive = TRUE; + ArpService->MnpConfigData.EnablePromiscuousReceive = FALSE; + ArpService->MnpConfigData.FlushQueuesOnReset = TRUE; + ArpService->MnpConfigData.EnableReceiveTimestamps = FALSE; + ArpService->MnpConfigData.DisableBackgroundPolling = FALSE; + + // + // Configure the Mnp child. + // + Status = ArpService->Mnp->Configure (ArpService->Mnp, &ArpService->MnpConfigData); + if (EFI_ERROR (Status)) { + goto ERROR_EXIT; + } + + // + // Create the event used in the RxToken. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + ArpOnFrameRcvd, + ArpService, + &ArpService->RxToken.Event + ); + if (EFI_ERROR (Status)) { + goto ERROR_EXIT; + } + + // + // Create the Arp heartbeat timer. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + ArpTimerHandler, + ArpService, + &ArpService->PeriodicTimer + ); + if (EFI_ERROR (Status)) { + goto ERROR_EXIT; + } + + // + // Start the heartbeat timer. + // + Status = gBS->SetTimer ( + ArpService->PeriodicTimer, + TimerPeriodic, + ARP_PERIODIC_TIMER_INTERVAL + ); + +ERROR_EXIT: + + return Status; +} + + +/** + Clean the arp service context data. + + @param[in, out] ArpService Pointer to the buffer containing the arp service + context data. + + @return None. + +**/ +VOID +ArpCleanService ( + IN OUT ARP_SERVICE_DATA *ArpService + ) +{ + NET_CHECK_SIGNATURE (ArpService, ARP_SERVICE_DATA_SIGNATURE); + + if (ArpService->PeriodicTimer != NULL) { + // + // Cancle and close the PeriodicTimer. + // + gBS->SetTimer (ArpService->PeriodicTimer, TimerCancel, 0); + gBS->CloseEvent (ArpService->PeriodicTimer); + } + + if (ArpService->RxToken.Event != NULL) { + // + // Cancle the RxToken and close the event in the RxToken. + // + ArpService->Mnp->Cancel (ArpService->Mnp, NULL); + gBS->CloseEvent (ArpService->RxToken.Event); + } + + if (ArpService->Mnp != NULL) { + // + // Reset the Mnp child and close the Mnp protocol. + // + ArpService->Mnp->Configure (ArpService->Mnp, NULL); + gBS->CloseProtocol ( + ArpService->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + ArpService->ImageHandle, + ArpService->ControllerHandle + ); + } + + if (ArpService->MnpChildHandle != NULL) { + // + // Destroy the mnp child. + // + NetLibDestroyServiceChild( + ArpService->ControllerHandle, + ArpService->ImageHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + ArpService->MnpChildHandle + ); + } +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +ArpDestroyChildEntryInHandleBuffer ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + ARP_INSTANCE_DATA *Instance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = NET_LIST_USER_STRUCT_S (Entry, ARP_INSTANCE_DATA, List, ARP_INSTANCE_DATA_SIGNATURE); + ServiceBinding = (EFI_SERVICE_BINDING_PROTOCOL *) Context; + + return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle); +} + +/** + Tests to see if this driver supports a given controller. + + If a child device is provided, it further tests to see if this driver supports + creating a handle for the specified child device. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. + This parameter is ignored by device drivers, + and is optional for bus drivers. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver + specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed + by the driver specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by + a different driver or an application that + requires exclusive acces. Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the + driver specified by This. + +**/ +EFI_STATUS +EFIAPI +ArpDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // Test to see if Arp SB is already installed. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiArpServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (Status == EFI_SUCCESS) { + return EFI_ALREADY_STARTED; + } + + // + // Test to see if MNP SB is installed. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + return Status; +} + + +/** + Start this driver on ControllerHandle. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been + moved into this common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed or the system behavior + will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally + aligned EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified + by This must have been called with the same calling parameters, and Supported() + must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. + This parameter is ignored by device drivers, + and is optional for bus drivers. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error. + Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of + resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +ArpDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + ARP_SERVICE_DATA *ArpService; + + // + // Allocate a zero pool for ArpService. + // + ArpService = AllocateZeroPool (sizeof(ARP_SERVICE_DATA)); + if (ArpService == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Initialize the arp service context data. + // + Status = ArpCreateService (This->DriverBindingHandle, ControllerHandle, ArpService); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + // + // Install the ARP service binding protocol. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiArpServiceBindingProtocolGuid, + &ArpService->ServiceBinding, + NULL + ); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + // + // OK, start to receive arp packets from Mnp. + // + Status = ArpService->Mnp->Receive (ArpService->Mnp, &ArpService->RxToken); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + return Status; + +ERROR: + + // + // On error, clean the arp service context data, and free the memory allocated. + // + ArpCleanService (ArpService); + FreePool (ArpService); + + return Status; +} + + +/** + Stop this driver on ControllerHandle. + + Release the control of this controller and remove the IScsi functions. The Stop() + function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed or the system behavior + will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + Not used. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0.Not used. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +ArpDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_STATUS Status; + EFI_HANDLE NicHandle; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + ARP_SERVICE_DATA *ArpService; + LIST_ENTRY *List; + + // + // Get the NicHandle which the arp servicebinding is installed on. + // + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid); + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + // + // Try to get the arp servicebinding protocol on the NicHandle. + // + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiArpServiceBindingProtocolGuid, + (VOID **)&ServiceBinding, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "ArpDriverBindingStop: Open ArpSb failed, %r.\n", Status)); + return EFI_DEVICE_ERROR; + } + + ArpService = ARP_SERVICE_DATA_FROM_THIS (ServiceBinding); + + if (NumberOfChildren != 0) { + // + // NumberOfChildren is not zero, destroy all the ARP children instances. + // + List = &ArpService->ChildrenList; + Status = NetDestroyLinkList ( + List, + ArpDestroyChildEntryInHandleBuffer, + ServiceBinding, + NULL + ); + ASSERT (IsListEmpty (&ArpService->PendingRequestTable)); + ASSERT (IsListEmpty (&ArpService->DeniedCacheTable)); + ASSERT (IsListEmpty (&ArpService->ResolvedCacheTable)); + } else if (IsListEmpty (&ArpService->ChildrenList)) { + // + // Uninstall the ARP ServiceBinding protocol. + // + gBS->UninstallMultipleProtocolInterfaces ( + NicHandle, + &gEfiArpServiceBindingProtocolGuid, + &ArpService->ServiceBinding, + NULL + ); + + // + // Clean the arp servicebinding context data and free the memory allocated. + // + ArpCleanService (ArpService); + + FreePool (ArpService); + } + + return EFI_SUCCESS; +} + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned + in ChildHandle. If ChildHandle is not a pointer to NULL, then the protocol + installs on the existing ChildHandle. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing + UEFI handle, then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +ArpServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ) +{ + EFI_STATUS Status; + ARP_SERVICE_DATA *ArpService; + ARP_INSTANCE_DATA *Instance; + VOID *Mnp; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + ArpService = ARP_SERVICE_DATA_FROM_THIS (This); + + // + // Allocate memory for the instance context data. + // + Instance = AllocateZeroPool (sizeof(ARP_INSTANCE_DATA)); + if (Instance == NULL) { + DEBUG ((EFI_D_ERROR, "ArpSBCreateChild: Failed to allocate memory for Instance.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + // + // Init the instance context data. + // + ArpInitInstance (ArpService, Instance); + + // + // Install the ARP protocol onto the ChildHandle. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiArpProtocolGuid, + (VOID *)&Instance->ArpProto, + NULL + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "ArpSBCreateChild: faild to install ARP protocol, %r.\n", Status)); + + FreePool (Instance); + return Status; + } + + // + // Save the ChildHandle. + // + Instance->Handle = *ChildHandle; + + // + // Open the Managed Network protocol BY_CHILD. + // + Status = gBS->OpenProtocol ( + ArpService->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **) &Mnp, + gArpDriverBinding.DriverBindingHandle, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Insert the instance into children list managed by the arp service context data. + // + InsertTailList (&ArpService->ChildrenList, &Instance->List); + ArpService->ChildrenNumber++; + + gBS->RestoreTPL (OldTpl); + +ERROR: + + if (EFI_ERROR (Status)) { + + gBS->CloseProtocol ( + ArpService->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + gArpDriverBinding.DriverBindingHandle, + Instance->Handle + ); + + gBS->UninstallMultipleProtocolInterfaces ( + Instance->Handle, + &gEfiArpProtocolGuid, + &Instance->ArpProto, + NULL + ); + + // + // Free the allocated memory. + // + FreePool (Instance); + } + + return Status; +} + + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is + being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +ArpServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + EFI_STATUS Status; + ARP_SERVICE_DATA *ArpService; + ARP_INSTANCE_DATA *Instance; + EFI_ARP_PROTOCOL *Arp; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + ArpService = ARP_SERVICE_DATA_FROM_THIS (This); + + // + // Get the arp protocol. + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiArpProtocolGuid, + (VOID **)&Arp, + ArpService->ImageHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Instance = ARP_INSTANCE_DATA_FROM_THIS (Arp); + + if (Instance->InDestroy) { + return EFI_SUCCESS; + } + + // + // Use the InDestroy as a flag to avoid re-entrance. + // + Instance->InDestroy = TRUE; + + // + // Close the Managed Network protocol. + // + gBS->CloseProtocol ( + ArpService->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + gArpDriverBinding.DriverBindingHandle, + ChildHandle + ); + + // + // Uninstall the ARP protocol. + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiArpProtocolGuid, + &Instance->ArpProto, + NULL + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "ArpSBDestroyChild: Failed to uninstall the arp protocol, %r.\n", + Status)); + + Instance->InDestroy = FALSE; + return Status; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (Instance->Configured) { + // + // Delete the related cache entry. + // + ArpDeleteCacheEntry (Instance, FALSE, NULL, TRUE); + + // + // Reset the instance configuration. + // + ArpConfigureInstance (Instance, NULL); + } + + // + // Remove this instance from the ChildrenList. + // + RemoveEntryList (&Instance->List); + ArpService->ChildrenNumber--; + + gBS->RestoreTPL (OldTpl); + + FreePool (Instance); + + return Status; +} + +/** + The entry point for Arp driver which installs the driver binding and component name + protocol on its ImageHandle. + + @param[in] ImageHandle The image handle of the driver. + @param[in] SystemTable The system table. + + @retval EFI_SUCCESS if the driver binding and component name protocols + are successfully + @retval Others Failed to install the protocols. + +**/ +EFI_STATUS +EFIAPI +ArpDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gArpDriverBinding, + ImageHandle, + &gArpComponentName, + &gArpComponentName2 + ); +} + diff --git a/NetworkPkg/ArpDxe/ArpDriver.h b/NetworkPkg/ArpDxe/ArpDriver.h new file mode 100644 index 000000000..0b5b06ee3 --- /dev/null +++ b/NetworkPkg/ArpDxe/ArpDriver.h @@ -0,0 +1,334 @@ +/** @file + ARP driver header file. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ARP_DRIVER_H_ +#define _ARP_DRIVER_H_ + + +#include + +#include +#include +#include + +#include +#include +#include +#include + + +// +// Global variables +// +extern EFI_DRIVER_BINDING_PROTOCOL gArpDriverBinding; +extern EFI_COMPONENT_NAME_PROTOCOL gArpComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gArpComponentName2; + +// +// Function prototypes for the Drivr Binding Protocol +// +/** + Tests to see if this driver supports a given controller. + + If a child device is provided, it further tests to see if this driver supports + creating a handle for the specified child device. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. + This parameter is ignored by device drivers, + and is optional for bus drivers. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver + specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed + by the driver specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by + a different driver or an application that + requires exclusive acces. Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the + driver specified by This. + +**/ +EFI_STATUS +EFIAPI +ArpDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been + moved into this common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed or the system behavior + will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally + aligned EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified + by This must have been called with the same calling parameters, and Supported() + must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. + This parameter is ignored by device drivers, + and is optional for bus drivers. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error. + Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of + resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +ArpDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle. + + Release the control of this controller and remove the IScsi functions. The Stop() + function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed or the system behavior + will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + Not used. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0.Not used. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +ArpDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned + in ChildHandle. If ChildHandle is not a pointer to NULL, then the protocol + installs on the existing ChildHandle. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing + UEFI handle, then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +ArpServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is + being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +ArpServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +ArpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +ArpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + + +#endif + diff --git a/NetworkPkg/ArpDxe/ArpDxe.inf b/NetworkPkg/ArpDxe/ArpDxe.inf new file mode 100644 index 000000000..2ff74895a --- /dev/null +++ b/NetworkPkg/ArpDxe/ArpDxe.inf @@ -0,0 +1,63 @@ +## @file +# This module produces EFI ARP Protocol and EFI ARP Service Binding Protocol. +# +# This module produces EFI ARP Protocol upon EFI MNP Protocol, to provide a generic +# implementation of the Address Resolution Protocol that is described in RFCs 826 +# and 1122. +# +# Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = ArpDxe + MODULE_UNI_FILE = ArpDxe.uni + FILE_GUID = 529D3F93-E8E9-4e73-B1E1-BDF6A9D50113 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = ArpDriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = gArpDriverBinding +# COMPONENT_NAME = gArpComponentName +# COMPONENT_NAME2 = gArpComponentName2 +# + +[Sources] + ArpMain.c + ArpDriver.h + ComponentName.c + ArpImpl.h + ArpImpl.c + ArpDriver.c + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + UefiLib + UefiBootServicesTableLib + UefiDriverEntryPoint + DebugLib + NetLib + DpcLib + +[Protocols] + gEfiArpServiceBindingProtocolGuid ## BY_START + gEfiManagedNetworkServiceBindingProtocolGuid ## TO_START + gEfiArpProtocolGuid ## BY_START + gEfiManagedNetworkProtocolGuid ## TO_START + +[UserExtensions.TianoCore."ExtraFiles"] + ArpDxeExtra.uni diff --git a/NetworkPkg/ArpDxe/ArpDxe.uni b/NetworkPkg/ArpDxe/ArpDxe.uni new file mode 100644 index 000000000..f72063dd4 --- /dev/null +++ b/NetworkPkg/ArpDxe/ArpDxe.uni @@ -0,0 +1,18 @@ +// /** @file +// This module produces EFI ARP Protocol and EFI ARP Service Binding Protocol. +// +// This module produces EFI ARP Protocol upon EFI MNP Protocol, to provide a generic +// implementation of the Address Resolution Protocol that is described in RFCs 826 +// and 1122. +// +// Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "EFI Address Resolution Protocol" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces EFI ARP Protocol using the EFI MNP Protocol to provide a generic implementation of the Address Resolution Protocol that is described in RFCs 826 and 1122." + diff --git a/NetworkPkg/ArpDxe/ArpDxeExtra.uni b/NetworkPkg/ArpDxe/ArpDxeExtra.uni new file mode 100644 index 000000000..be612d002 --- /dev/null +++ b/NetworkPkg/ArpDxe/ArpDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// ArpDxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"ARP DXE Driver" + + diff --git a/NetworkPkg/ArpDxe/ArpImpl.c b/NetworkPkg/ArpDxe/ArpImpl.c new file mode 100644 index 000000000..0e9ef103e --- /dev/null +++ b/NetworkPkg/ArpDxe/ArpImpl.c @@ -0,0 +1,1667 @@ +/** @file + The implementation of the ARP protocol. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "ArpImpl.h" + +// +// Global variable of EFI ARP Protocol Interface. +// +EFI_ARP_PROTOCOL mEfiArpProtocolTemplate = { + ArpConfigure, + ArpAdd, + ArpFind, + ArpDelete, + ArpFlush, + ArpRequest, + ArpCancel +}; + + +/** + Initialize the instance context data. + + @param[in] ArpService Pointer to the arp service context data this + instance belongs to. + @param[out] Instance Pointer to the instance context data. + + @return None. + +**/ +VOID +ArpInitInstance ( + IN ARP_SERVICE_DATA *ArpService, + OUT ARP_INSTANCE_DATA *Instance + ) +{ + NET_CHECK_SIGNATURE (ArpService, ARP_SERVICE_DATA_SIGNATURE); + + Instance->Signature = ARP_INSTANCE_DATA_SIGNATURE; + Instance->ArpService = ArpService; + + CopyMem (&Instance->ArpProto, &mEfiArpProtocolTemplate, sizeof (Instance->ArpProto)); + + Instance->Configured = FALSE; + Instance->InDestroy = FALSE; + + InitializeListHead (&Instance->List); +} + + +/** + Process the Arp packets received from Mnp, the procedure conforms to RFC826. + + @param[in] Context Pointer to the context data registerd to the + Event. + + @return None. + +**/ +VOID +EFIAPI +ArpOnFrameRcvdDpc ( + IN VOID *Context + ) +{ + EFI_STATUS Status; + ARP_SERVICE_DATA *ArpService; + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *RxToken; + EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData; + ARP_HEAD *Head; + ARP_ADDRESS ArpAddress; + ARP_CACHE_ENTRY *CacheEntry; + LIST_ENTRY *Entry; + ARP_INSTANCE_DATA *Instance; + EFI_ARP_CONFIG_DATA *ConfigData; + NET_ARP_ADDRESS SenderAddress[2]; + BOOLEAN ProtoMatched; + BOOLEAN IsTarget; + BOOLEAN MergeFlag; + + ArpService = (ARP_SERVICE_DATA *)Context; + NET_CHECK_SIGNATURE (ArpService, ARP_SERVICE_DATA_SIGNATURE); + + RxToken = &ArpService->RxToken; + + if (RxToken->Status == EFI_ABORTED) { + // + // The Token is aborted, possibly by arp itself, just return and the receiving + // process is stopped. + // + return; + } + + if (EFI_ERROR (RxToken->Status)) { + // + // Restart the receiving if any other error Status occurs. + // + goto RESTART_RECEIVE; + } + + // + // Status is EFI_SUCCESS, process the received frame. + // + RxData = RxToken->Packet.RxData; + // + // Sanity check. + // + if (RxData->DataLength < sizeof (ARP_HEAD)) { + // + // Restart the receiving if packet size is not correct. + // + goto RESTART_RECEIVE; + } + + // + // Convert the byte order of the multi-byte fields. + // + Head = (ARP_HEAD *) RxData->PacketData; + Head->HwType = NTOHS (Head->HwType); + Head->ProtoType = NTOHS (Head->ProtoType); + Head->OpCode = NTOHS (Head->OpCode); + + if (RxData->DataLength < (sizeof (ARP_HEAD) + 2 * Head->HwAddrLen + 2 * Head->ProtoAddrLen)) { + goto RESTART_RECEIVE; + } + + if ((Head->HwType != ArpService->SnpMode.IfType) || + (Head->HwAddrLen != ArpService->SnpMode.HwAddressSize) || + (RxData->ProtocolType != ARP_ETHER_PROTO_TYPE)) { + // + // The hardware type or the hardware address length doesn't match. + // There is a sanity check for the protocol type too. + // + goto RECYCLE_RXDATA; + } + + // + // Set the pointers to the addresses contained in the arp packet. + // + ArpAddress.SenderHwAddr = (UINT8 *)(Head + 1); + ArpAddress.SenderProtoAddr = ArpAddress.SenderHwAddr + Head->HwAddrLen; + ArpAddress.TargetHwAddr = ArpAddress.SenderProtoAddr + Head->ProtoAddrLen; + ArpAddress.TargetProtoAddr = ArpAddress.TargetHwAddr + Head->HwAddrLen; + + SenderAddress[Hardware].Type = Head->HwType; + SenderAddress[Hardware].Length = Head->HwAddrLen; + SenderAddress[Hardware].AddressPtr = ArpAddress.SenderHwAddr; + + SenderAddress[Protocol].Type = Head->ProtoType; + SenderAddress[Protocol].Length = Head->ProtoAddrLen; + SenderAddress[Protocol].AddressPtr = ArpAddress.SenderProtoAddr; + + // + // First, check the denied cache table. + // + CacheEntry = ArpFindDeniedCacheEntry ( + ArpService, + &SenderAddress[Protocol], + &SenderAddress[Hardware] + ); + if (CacheEntry != NULL) { + // + // This address (either hardware or protocol address, or both) is configured to + // be a deny entry, silently skip the normal process. + // + goto RECYCLE_RXDATA; + } + + ProtoMatched = FALSE; + IsTarget = FALSE; + Instance = NULL; + NET_LIST_FOR_EACH (Entry, &ArpService->ChildrenList) { + // + // Iterate all the children. + // + Instance = NET_LIST_USER_STRUCT (Entry, ARP_INSTANCE_DATA, List); + NET_CHECK_SIGNATURE (Instance, ARP_INSTANCE_DATA_SIGNATURE); + ConfigData = &Instance->ConfigData; + + if ((Instance->Configured) && + (Head->ProtoType == ConfigData->SwAddressType) && + (Head->ProtoAddrLen == ConfigData->SwAddressLength)) { + // + // The protocol type is matched for the received arp packet. + // + ProtoMatched = TRUE; + if (0 == CompareMem ( + (VOID *)ArpAddress.TargetProtoAddr, + ConfigData->StationAddress, + ConfigData->SwAddressLength + )) { + // + // The arp driver has the target address required by the received arp packet. + // + IsTarget = TRUE; + break; + } + } + } + + if (!ProtoMatched) { + // + // Protocol type unmatchable, skip. + // + goto RECYCLE_RXDATA; + } + + // + // Check whether the sender's address information is already in the cache. + // + MergeFlag = FALSE; + CacheEntry = ArpFindNextCacheEntryInTable ( + &ArpService->ResolvedCacheTable, + NULL, + ByProtoAddress, + &SenderAddress[Protocol], + NULL + ); + if (CacheEntry != NULL) { + // + // Update the entry with the new information. + // + ArpFillAddressInCacheEntry (CacheEntry, &SenderAddress[Hardware], NULL); + CacheEntry->DecayTime = CacheEntry->DefaultDecayTime; + MergeFlag = TRUE; + } + + if (!IsTarget) { + // + // This arp packet isn't targeted to us, skip now. + // + goto RECYCLE_RXDATA; + } + + if (!MergeFlag) { + // + // Add the triplet + // to the translation table. + // + CacheEntry = ArpFindNextCacheEntryInTable ( + &ArpService->PendingRequestTable, + NULL, + ByProtoAddress, + &SenderAddress[Protocol], + NULL + ); + if (CacheEntry == NULL) { + // + // Allocate a new CacheEntry. + // + CacheEntry = ArpAllocCacheEntry (NULL); + if (CacheEntry == NULL) { + goto RECYCLE_RXDATA; + } + } + + if (!IsListEmpty (&CacheEntry->List)) { + RemoveEntryList (&CacheEntry->List); + } + + // + // Fill the addresses into the CacheEntry. + // + ArpFillAddressInCacheEntry ( + CacheEntry, + &SenderAddress[Hardware], + &SenderAddress[Protocol] + ); + + // + // Inform the user. + // + ArpAddressResolved (CacheEntry, NULL, NULL); + + // + // Add this entry into the ResolvedCacheTable + // + InsertHeadList (&ArpService->ResolvedCacheTable, &CacheEntry->List); + } + + if (Head->OpCode == ARP_OPCODE_REQUEST) { + // + // Send back the ARP Reply. If we reach here, Instance is not NULL and CacheEntry + // is not NULL. + // + ArpSendFrame (Instance, CacheEntry, ARP_OPCODE_REPLY); + } + +RECYCLE_RXDATA: + + // + // Signal Mnp to recycle the RxData. + // + gBS->SignalEvent (RxData->RecycleEvent); + +RESTART_RECEIVE: + + // + // Continue to receive packets from Mnp. + // + Status = ArpService->Mnp->Receive (ArpService->Mnp, RxToken); + + DEBUG_CODE ( + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "ArpOnFrameRcvd: ArpService->Mnp->Receive " + "failed, %r\n.", Status)); + } + ); +} + +/** + Queue ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK. + + @param[in] Event The Event this notify function registered to. + @param[in] Context Pointer to the context data registerd to the + Event. + + @return None. + +**/ +VOID +EFIAPI +ArpOnFrameRcvd ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, ArpOnFrameRcvdDpc, Context); +} + +/** + Process the already sent arp packets. + + @param[in] Context Pointer to the context data registerd to the + Event. + + @return None. + +**/ +VOID +EFIAPI +ArpOnFrameSentDpc ( + IN VOID *Context + ) +{ + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TxToken; + EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData; + + ASSERT (Context != NULL); + + TxToken = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *)Context; + TxData = TxToken->Packet.TxData; + + DEBUG_CODE ( + if (EFI_ERROR (TxToken->Status)) { + DEBUG ((EFI_D_ERROR, "ArpOnFrameSent: TxToken->Status, %r.\n", TxToken->Status)); + } + ); + + // + // Free the allocated memory and close the event. + // + FreePool (TxData->FragmentTable[0].FragmentBuffer); + FreePool (TxData); + gBS->CloseEvent (TxToken->Event); + FreePool (TxToken); +} + +/** + Request ArpOnFrameSentDpc as a DPC at TPL_CALLBACK. + + @param[in] Event The Event this notify function registered to. + @param[in] Context Pointer to the context data registerd to the + Event. + + @return None. + +**/ +VOID +EFIAPI +ArpOnFrameSent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request ArpOnFrameSentDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, ArpOnFrameSentDpc, Context); +} + + +/** + Process the arp cache olding and drive the retrying arp requests. + + @param[in] Event The Event this notify function registered to. + @param[in] Context Pointer to the context data registerd to the + Event. + + @return None. + +**/ +VOID +EFIAPI +ArpTimerHandler ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + ARP_SERVICE_DATA *ArpService; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + LIST_ENTRY *ContextEntry; + ARP_CACHE_ENTRY *CacheEntry; + USER_REQUEST_CONTEXT *RequestContext; + + ASSERT (Context != NULL); + ArpService = (ARP_SERVICE_DATA *)Context; + + // + // Iterate all the pending requests to see whether a retry is needed to send out + // or the request finally fails because the retry time reaches the limitation. + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &ArpService->PendingRequestTable) { + CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List); + + if (CacheEntry->NextRetryTime <= ARP_PERIODIC_TIMER_INTERVAL) { + // + // Timeout, if we can retry more, send out the request again, otherwise abort + // this request. + // + if (CacheEntry->RetryCount == 0) { + // + // Abort this request. + // + ArpAddressResolved (CacheEntry, NULL, NULL); + ASSERT (IsListEmpty (&CacheEntry->UserRequestList)); + + RemoveEntryList (&CacheEntry->List); + FreePool (CacheEntry); + } else { + // + // resend the ARP request. + // + ASSERT (!IsListEmpty(&CacheEntry->UserRequestList)); + + ContextEntry = CacheEntry->UserRequestList.ForwardLink; + RequestContext = NET_LIST_USER_STRUCT (ContextEntry, USER_REQUEST_CONTEXT, List); + + ArpSendFrame (RequestContext->Instance, CacheEntry, ARP_OPCODE_REQUEST); + + CacheEntry->RetryCount--; + CacheEntry->NextRetryTime = RequestContext->Instance->ConfigData.RetryTimeOut; + } + } else { + // + // Update the NextRetryTime. + // + CacheEntry->NextRetryTime -= ARP_PERIODIC_TIMER_INTERVAL; + } + } + + // + // Check the timeouts for the DeniedCacheTable. + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &ArpService->DeniedCacheTable) { + CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List); + ASSERT (IsListEmpty (&CacheEntry->UserRequestList)); + + if (CacheEntry->DefaultDecayTime == 0) { + // + // It's a static entry, skip it. + // + continue; + } + + if (CacheEntry->DecayTime <= ARP_PERIODIC_TIMER_INTERVAL) { + // + // Time out, remove it. + // + RemoveEntryList (&CacheEntry->List); + FreePool (CacheEntry); + } else { + // + // Update the DecayTime. + // + CacheEntry->DecayTime -= ARP_PERIODIC_TIMER_INTERVAL; + } + } + + // + // Check the timeouts for the ResolvedCacheTable. + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &ArpService->ResolvedCacheTable) { + CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List); + ASSERT (IsListEmpty (&CacheEntry->UserRequestList)); + + if (CacheEntry->DefaultDecayTime == 0) { + // + // It's a static entry, skip it. + // + continue; + } + + if (CacheEntry->DecayTime <= ARP_PERIODIC_TIMER_INTERVAL) { + // + // Time out, remove it. + // + RemoveEntryList (&CacheEntry->List); + FreePool (CacheEntry); + } else { + // + // Update the DecayTime. + // + CacheEntry->DecayTime -= ARP_PERIODIC_TIMER_INTERVAL; + } + } +} + + +/** + Match the two NET_ARP_ADDRESSes. + + @param[in] AddressOne Pointer to the first address to match. + @param[in] AddressTwo Pointer to the second address to match. + + @return The two addresses match or not. + +**/ +BOOLEAN +ArpMatchAddress ( + IN NET_ARP_ADDRESS *AddressOne, + IN NET_ARP_ADDRESS *AddressTwo + ) +{ + ASSERT (AddressOne != NULL && AddressTwo != NULL); + + if ((AddressOne->Type != AddressTwo->Type) || + (AddressOne->Length != AddressTwo->Length)) { + // + // Either Type or Length doesn't match. + // + return FALSE; + } + + if ((AddressOne->AddressPtr != NULL) && + (CompareMem ( + AddressOne->AddressPtr, + AddressTwo->AddressPtr, + AddressOne->Length + ) != 0)) { + // + // The address is not the same. + // + return FALSE; + } + + return TRUE; +} + + +/** + Find the CacheEntry which matches the requirements in the specified CacheTable. + + @param[in] CacheTable Pointer to the arp cache table. + @param[in] StartEntry Pointer to the start entry this search begins with + in the cache table. + @param[in] FindOpType The search type. + @param[in] ProtocolAddress Pointer to the protocol address to match. + @param[in] HardwareAddress Pointer to the hardware address to match. + + @return Pointer to the matched arp cache entry, if NULL, no match is found. + +**/ +ARP_CACHE_ENTRY * +ArpFindNextCacheEntryInTable ( + IN LIST_ENTRY *CacheTable, + IN LIST_ENTRY *StartEntry, + IN FIND_OPTYPE FindOpType, + IN NET_ARP_ADDRESS *ProtocolAddress OPTIONAL, + IN NET_ARP_ADDRESS *HardwareAddress OPTIONAL + ) +{ + LIST_ENTRY *Entry; + ARP_CACHE_ENTRY *CacheEntry; + + if (StartEntry == NULL) { + // + // Start from the beginning of the table if no StartEntry is specified. + // + StartEntry = CacheTable; + } + + for (Entry = StartEntry->ForwardLink; Entry != CacheTable; Entry = Entry->ForwardLink) { + CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List); + + if ((FindOpType & MATCH_SW_ADDRESS) != 0) { + // + // Find by the software address. + // + if (!ArpMatchAddress (ProtocolAddress, &CacheEntry->Addresses[Protocol])) { + // + // The ProtocolAddress doesn't match, continue to the next cache entry. + // + continue; + } + } + + if ((FindOpType & MATCH_HW_ADDRESS) != 0) { + // + // Find by the hardware address. + // + if (!ArpMatchAddress (HardwareAddress, &CacheEntry->Addresses[Hardware])) { + // + // The HardwareAddress doesn't match, continue to the next cache entry. + // + continue; + } + } + + // + // The CacheEntry meets the requirements now, return this entry. + // + return CacheEntry; + } + + // + // No matching. + // + return NULL; +} + + +/** + Find the CacheEntry, using ProtocolAddress or HardwareAddress or both, as the keyword, + in the DeniedCacheTable. + + @param[in] ArpService Pointer to the arp service context data. + @param[in] ProtocolAddress Pointer to the protocol address. + @param[in] HardwareAddress Pointer to the hardware address. + + @return Pointer to the matched cache entry, if NULL no match is found. + +**/ +ARP_CACHE_ENTRY * +ArpFindDeniedCacheEntry ( + IN ARP_SERVICE_DATA *ArpService, + IN NET_ARP_ADDRESS *ProtocolAddress OPTIONAL, + IN NET_ARP_ADDRESS *HardwareAddress OPTIONAL + ) +{ + ARP_CACHE_ENTRY *CacheEntry; + + ASSERT ((ProtocolAddress != NULL) || (HardwareAddress != NULL)); + NET_CHECK_SIGNATURE (ArpService, ARP_SERVICE_DATA_SIGNATURE); + + CacheEntry = NULL; + + if ((ProtocolAddress != NULL) && (ProtocolAddress->AddressPtr != NULL)) { + // + // Find the cache entry in the DeniedCacheTable by the protocol address. + // + CacheEntry = ArpFindNextCacheEntryInTable ( + &ArpService->DeniedCacheTable, + NULL, + ByProtoAddress, + ProtocolAddress, + NULL + ); + if (CacheEntry != NULL) { + // + // There is a match. + // + return CacheEntry; + } + } + + if ((HardwareAddress != NULL) && (HardwareAddress->AddressPtr != NULL)) { + // + // Find the cache entry in the DeniedCacheTable by the hardware address. + // + CacheEntry = ArpFindNextCacheEntryInTable ( + &ArpService->DeniedCacheTable, + NULL, + ByHwAddress, + NULL, + HardwareAddress + ); + } + + return CacheEntry; +} + + +/** + Allocate a cache entry and initialize it. + + @param[in] Instance Pointer to the instance context data. + + @return Pointer to the new created cache entry. + +**/ +ARP_CACHE_ENTRY * +ArpAllocCacheEntry ( + IN ARP_INSTANCE_DATA *Instance + ) +{ + ARP_CACHE_ENTRY *CacheEntry; + NET_ARP_ADDRESS *Address; + UINT16 Index; + + // + // Allocate memory for the cache entry. + // + CacheEntry = AllocatePool (sizeof (ARP_CACHE_ENTRY)); + if (CacheEntry == NULL) { + return NULL; + } + + // + // Init the lists. + // + InitializeListHead (&CacheEntry->List); + InitializeListHead (&CacheEntry->UserRequestList); + + for (Index = 0; Index < 2; Index++) { + // + // Init the address pointers to point to the concrete buffer. + // + Address = &CacheEntry->Addresses[Index]; + Address->AddressPtr = Address->Buffer.ProtoAddress; + } + + // + // Zero the hardware address first. + // + ZeroMem (CacheEntry->Addresses[Hardware].AddressPtr, ARP_MAX_HARDWARE_ADDRESS_LEN); + + if (Instance != NULL) { + // + // Inherit the parameters from the instance configuration. + // + CacheEntry->RetryCount = Instance->ConfigData.RetryCount; + CacheEntry->NextRetryTime = Instance->ConfigData.RetryTimeOut; + CacheEntry->DefaultDecayTime = Instance->ConfigData.EntryTimeOut; + CacheEntry->DecayTime = Instance->ConfigData.EntryTimeOut; + } else { + // + // Use the default parameters if this cache entry isn't allocate in a + // instance's scope. + // + CacheEntry->RetryCount = ARP_DEFAULT_RETRY_COUNT; + CacheEntry->NextRetryTime = ARP_DEFAULT_RETRY_INTERVAL; + CacheEntry->DefaultDecayTime = ARP_DEFAULT_TIMEOUT_VALUE; + CacheEntry->DecayTime = ARP_DEFAULT_TIMEOUT_VALUE; + } + + return CacheEntry; +} + + +/** + Turn the CacheEntry into the resolved status. + + @param[in] CacheEntry Pointer to the resolved cache entry. + @param[in] Instance Pointer to the instance context data. + @param[in] UserEvent Pointer to the UserEvent to notify. + + @return The count of notifications sent to the instance. + +**/ +UINTN +ArpAddressResolved ( + IN ARP_CACHE_ENTRY *CacheEntry, + IN ARP_INSTANCE_DATA *Instance OPTIONAL, + IN EFI_EVENT UserEvent OPTIONAL + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + USER_REQUEST_CONTEXT *Context; + UINTN Count; + + Count = 0; + + // + // Iterate all the linked user requests to notify them. + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &CacheEntry->UserRequestList) { + Context = NET_LIST_USER_STRUCT (Entry, USER_REQUEST_CONTEXT, List); + + if (((Instance == NULL) || (Context->Instance == Instance)) && + ((UserEvent == NULL) || (Context->UserRequestEvent == UserEvent))) { + // + // Copy the address to the user-provided buffer and notify the user. + // + CopyMem ( + Context->UserHwAddrBuffer, + CacheEntry->Addresses[Hardware].AddressPtr, + CacheEntry->Addresses[Hardware].Length + ); + gBS->SignalEvent (Context->UserRequestEvent); + + // + // Remove this user request and free the context data. + // + RemoveEntryList (&Context->List); + FreePool (Context); + + Count++; + } + } + + // + // Dispatch the DPCs queued by the NotifyFunction of the Context->UserRequestEvent. + // + DispatchDpc (); + + return Count; +} + + +/** + Fill the addresses in the CacheEntry using the information passed in by + HwAddr and SwAddr. + + @param[in] CacheEntry Pointer to the cache entry. + @param[in] HwAddr Pointer to the software address. + @param[in] SwAddr Pointer to the hardware address. + + @return None. + +**/ +VOID +ArpFillAddressInCacheEntry ( + IN ARP_CACHE_ENTRY *CacheEntry, + IN NET_ARP_ADDRESS *HwAddr OPTIONAL, + IN NET_ARP_ADDRESS *SwAddr OPTIONAL + ) +{ + NET_ARP_ADDRESS *Address[2]; + NET_ARP_ADDRESS *CacheAddress; + UINT32 Index; + + Address[Hardware] = HwAddr; + Address[Protocol] = SwAddr; + + for (Index = 0; Index < 2; Index++) { + if (Address[Index] != NULL) { + // + // Fill the address if the passed in pointer is not NULL. + // + CacheAddress = &CacheEntry->Addresses[Index]; + + CacheAddress->Type = Address[Index]->Type; + CacheAddress->Length = Address[Index]->Length; + + if (Address[Index]->AddressPtr != NULL) { + // + // Copy it if the AddressPtr points to some buffer. + // + CopyMem ( + CacheAddress->AddressPtr, + Address[Index]->AddressPtr, + CacheAddress->Length + ); + } else { + // + // Zero the corresponding address buffer in the CacheEntry. + // + ZeroMem (CacheAddress->AddressPtr, CacheAddress->Length); + } + } + } +} + + +/** + Configure the instance using the ConfigData. ConfigData is already validated. + + @param[in] Instance Pointer to the instance context data to be + configured. + @param[in] ConfigData Pointer to the configuration data used to + configure the instance. + + @retval EFI_SUCCESS The instance is configured with the ConfigData. + @retval EFI_ACCESS_DENIED The instance is already configured and the + ConfigData tries to reset some unchangeable + fields. + @retval EFI_INVALID_PARAMETER The ConfigData provides a non-unicast IPv4 address + when the SwAddressType is IPv4. + @retval EFI_OUT_OF_RESOURCES The instance fails to configure due to memory + limitation. + +**/ +EFI_STATUS +ArpConfigureInstance ( + IN ARP_INSTANCE_DATA *Instance, + IN EFI_ARP_CONFIG_DATA *ConfigData OPTIONAL + ) +{ + EFI_ARP_CONFIG_DATA *OldConfigData; + IP4_ADDR Ip; + + OldConfigData = &Instance->ConfigData; + + if (ConfigData != NULL) { + + if (Instance->Configured) { + // + // The instance is configured, check the unchangeable fields. + // + if ((OldConfigData->SwAddressType != ConfigData->SwAddressType) || + (OldConfigData->SwAddressLength != ConfigData->SwAddressLength) || + (CompareMem ( + OldConfigData->StationAddress, + ConfigData->StationAddress, + OldConfigData->SwAddressLength + ) != 0)) { + // + // Deny the unallowed changes. + // + return EFI_ACCESS_DENIED; + } + } else { + // + // The instance is not configured. + // + + if (ConfigData->SwAddressType == IPV4_ETHER_PROTO_TYPE) { + CopyMem (&Ip, ConfigData->StationAddress, sizeof (IP4_ADDR)); + + if (IP4_IS_UNSPECIFIED (Ip) || IP4_IS_LOCAL_BROADCAST (Ip)) { + // + // The station address should not be zero or broadcast address. + // + return EFI_INVALID_PARAMETER; + } + } + + // + // Save the configuration. + // + CopyMem (OldConfigData, ConfigData, sizeof (*OldConfigData)); + + OldConfigData->StationAddress = AllocatePool (OldConfigData->SwAddressLength); + if (OldConfigData->StationAddress == NULL) { + DEBUG ((EFI_D_ERROR, "ArpConfigInstance: AllocatePool for the StationAddress " + "failed.\n")); + return EFI_OUT_OF_RESOURCES; + } + + // + // Save the StationAddress. + // + CopyMem ( + OldConfigData->StationAddress, + ConfigData->StationAddress, + OldConfigData->SwAddressLength + ); + + // + // Set the state to configured. + // + Instance->Configured = TRUE; + } + + // + // Use the implementation specific values if the following field is zero. + // + OldConfigData->EntryTimeOut = (ConfigData->EntryTimeOut == 0) ? + ARP_DEFAULT_TIMEOUT_VALUE : ConfigData->EntryTimeOut; + + OldConfigData->RetryCount = (ConfigData->RetryCount == 0) ? + ARP_DEFAULT_RETRY_COUNT : ConfigData->RetryCount; + + OldConfigData->RetryTimeOut = (ConfigData->RetryTimeOut == 0) ? + ARP_DEFAULT_RETRY_INTERVAL : ConfigData->RetryTimeOut; + } else { + // + // Reset the configuration. + // + + if (Instance->Configured) { + // + // Cancel the arp requests issued by this instance. + // + Instance->ArpProto.Cancel (&Instance->ArpProto, NULL, NULL); + + // + // Free the buffer previously allocated to hold the station address. + // + FreePool (OldConfigData->StationAddress); + } + + Instance->Configured = FALSE; + } + + return EFI_SUCCESS; +} + + +/** + Send out an arp frame using the CachEntry and the ArpOpCode. + + @param[in] Instance Pointer to the instance context data. + @param[in] CacheEntry Pointer to the configuration data used to + configure the instance. + @param[in] ArpOpCode The opcode used to send out this Arp frame, either + request or reply. + + @return None. + +**/ +VOID +ArpSendFrame ( + IN ARP_INSTANCE_DATA *Instance, + IN ARP_CACHE_ENTRY *CacheEntry, + IN UINT16 ArpOpCode + ) +{ + EFI_STATUS Status; + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TxToken; + EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData; + UINT32 TotalLength; + UINT8 *Packet; + ARP_SERVICE_DATA *ArpService; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + EFI_ARP_CONFIG_DATA *ConfigData; + UINT8 *TmpPtr; + ARP_HEAD *ArpHead; + + ASSERT ((Instance != NULL) && (CacheEntry != NULL)); + + // + // Allocate memory for the TxToken. + // + TxToken = AllocatePool (sizeof(EFI_MANAGED_NETWORK_COMPLETION_TOKEN)); + if (TxToken == NULL) { + DEBUG ((EFI_D_ERROR, "ArpSendFrame: Allocate memory for TxToken failed.\n")); + return; + } + + TxToken->Event = NULL; + TxData = NULL; + Packet = NULL; + + // + // Create the event for this TxToken. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + ArpOnFrameSent, + (VOID *)TxToken, + &TxToken->Event + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "ArpSendFrame: CreateEvent failed for TxToken->Event.\n")); + goto CLEAN_EXIT; + } + + // + // Allocate memory for the TxData used in the TxToken. + // + TxData = AllocatePool (sizeof(EFI_MANAGED_NETWORK_TRANSMIT_DATA)); + if (TxData == NULL) { + DEBUG ((EFI_D_ERROR, "ArpSendFrame: Allocate memory for TxData failed.\n")); + goto CLEAN_EXIT; + } + + ArpService = Instance->ArpService; + SnpMode = &ArpService->SnpMode; + ConfigData = &Instance->ConfigData; + + // + // Calculate the buffer length for this arp frame. + // + TotalLength = SnpMode->MediaHeaderSize + sizeof (ARP_HEAD) + + 2 * (ConfigData->SwAddressLength + SnpMode->HwAddressSize); + + // + // Allocate buffer for the arp frame. + // + Packet = AllocatePool (TotalLength); + if (Packet == NULL) { + DEBUG ((EFI_D_ERROR, "ArpSendFrame: Allocate memory for Packet failed.\n")); + ASSERT (Packet != NULL); + } + + TmpPtr = Packet; + + // + // The destination MAC address. + // + if (ArpOpCode == ARP_OPCODE_REQUEST) { + CopyMem (TmpPtr, &SnpMode->BroadcastAddress, SnpMode->HwAddressSize); + } else { + CopyMem ( + TmpPtr, + CacheEntry->Addresses[Hardware].AddressPtr, + SnpMode->HwAddressSize + ); + } + TmpPtr += SnpMode->HwAddressSize; + + // + // The source MAC address. + // + CopyMem (TmpPtr, &SnpMode->CurrentAddress, SnpMode->HwAddressSize); + TmpPtr += SnpMode->HwAddressSize; + + // + // The ethernet protocol type. + // + *(UINT16 *)TmpPtr = HTONS (ARP_ETHER_PROTO_TYPE); + TmpPtr += 2; + + // + // The ARP Head. + // + ArpHead = (ARP_HEAD *) TmpPtr; + ArpHead->HwType = HTONS ((UINT16)SnpMode->IfType); + ArpHead->ProtoType = HTONS (ConfigData->SwAddressType); + ArpHead->HwAddrLen = (UINT8)SnpMode->HwAddressSize; + ArpHead->ProtoAddrLen = ConfigData->SwAddressLength; + ArpHead->OpCode = HTONS (ArpOpCode); + TmpPtr += sizeof (ARP_HEAD); + + // + // The sender hardware address. + // + CopyMem (TmpPtr, &SnpMode->CurrentAddress, SnpMode->HwAddressSize); + TmpPtr += SnpMode->HwAddressSize; + + // + // The sender protocol address. + // + CopyMem (TmpPtr, ConfigData->StationAddress, ConfigData->SwAddressLength); + TmpPtr += ConfigData->SwAddressLength; + + // + // The target hardware address. + // + CopyMem ( + TmpPtr, + CacheEntry->Addresses[Hardware].AddressPtr, + SnpMode->HwAddressSize + ); + TmpPtr += SnpMode->HwAddressSize; + + // + // The target protocol address. + // + CopyMem ( + TmpPtr, + CacheEntry->Addresses[Protocol].AddressPtr, + ConfigData->SwAddressLength + ); + + // + // Set all the fields of the TxData. + // + TxData->DestinationAddress = NULL; + TxData->SourceAddress = NULL; + TxData->ProtocolType = 0; + TxData->DataLength = TotalLength - SnpMode->MediaHeaderSize; + TxData->HeaderLength = (UINT16) SnpMode->MediaHeaderSize; + TxData->FragmentCount = 1; + + TxData->FragmentTable[0].FragmentBuffer = Packet; + TxData->FragmentTable[0].FragmentLength = TotalLength; + + // + // Associate the TxData with the TxToken. + // + TxToken->Packet.TxData = TxData; + TxToken->Status = EFI_NOT_READY; + + // + // Send out this arp packet by Mnp. + // + Status = ArpService->Mnp->Transmit (ArpService->Mnp, TxToken); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Mnp->Transmit failed, %r.\n", Status)); + goto CLEAN_EXIT; + } + + return; + +CLEAN_EXIT: + + if (Packet != NULL) { + FreePool (Packet); + } + + if (TxData != NULL) { + FreePool (TxData); + } + + if (TxToken->Event != NULL) { + gBS->CloseEvent (TxToken->Event); + } + + FreePool (TxToken); +} + + +/** + Delete the cache entries in the specified CacheTable, using the BySwAddress, + SwAddressType, AddressBuffer combination as the matching key, if Force is TRUE, + the cache is deleted event it's a static entry. + + @param[in] CacheTable Pointer to the cache table to do the deletion. + @param[in] BySwAddress Delete the cache entry by software address or by + hardware address. + @param[in] SwAddressType The software address used to do the deletion. + @param[in] AddressBuffer Pointer to the buffer containing the address to + match for the deletion. + @param[in] Force This deletion is forced or not. + + @return The count of the deleted cache entries. + +**/ +UINTN +ArpDeleteCacheEntryInTable ( + IN LIST_ENTRY *CacheTable, + IN BOOLEAN BySwAddress, + IN UINT16 SwAddressType, + IN UINT8 *AddressBuffer OPTIONAL, + IN BOOLEAN Force + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + ARP_CACHE_ENTRY *CacheEntry; + UINTN Count; + + Count = 0; + + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, CacheTable) { + CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List); + + if ((CacheEntry->DefaultDecayTime == 0) && !Force) { + // + // It's a static entry and we are not forced to delete it, skip. + // + continue; + } + + if (BySwAddress) { + if (SwAddressType == CacheEntry->Addresses[Protocol].Type) { + // + // Protocol address type matched. Check the address. + // + if ((AddressBuffer == NULL) || + (CompareMem ( + AddressBuffer, + CacheEntry->Addresses[Protocol].AddressPtr, + CacheEntry->Addresses[Protocol].Length + ) == 0)) { + // + // Address matched. + // + goto MATCHED; + } + } + } else { + if ((AddressBuffer == NULL) || + (CompareMem ( + AddressBuffer, + CacheEntry->Addresses[Hardware].AddressPtr, + CacheEntry->Addresses[Hardware].Length + ) == 0)) { + // + // Address matched. + // + goto MATCHED; + } + } + + continue; + +MATCHED: + + // + // Delete this entry. + // + RemoveEntryList (&CacheEntry->List); + ASSERT (IsListEmpty (&CacheEntry->UserRequestList)); + FreePool (CacheEntry); + + Count++; + } + + return Count; +} + + +/** + Delete cache entries in all the cache tables. + + @param[in] Instance Pointer to the instance context data. + @param[in] BySwAddress Delete the cache entry by software address or by + hardware address. + @param[in] AddressBuffer Pointer to the buffer containing the address to + match for the deletion. + @param[in] Force This deletion is forced or not. + + @return The count of the deleted cache entries. + +**/ +UINTN +ArpDeleteCacheEntry ( + IN ARP_INSTANCE_DATA *Instance, + IN BOOLEAN BySwAddress, + IN UINT8 *AddressBuffer OPTIONAL, + IN BOOLEAN Force + ) +{ + ARP_SERVICE_DATA *ArpService; + UINTN Count; + + NET_CHECK_SIGNATURE (Instance, ARP_INSTANCE_DATA_SIGNATURE); + + ArpService = Instance->ArpService; + + // + // Delete the cache entries in the DeniedCacheTable. + // + Count = ArpDeleteCacheEntryInTable ( + &ArpService->DeniedCacheTable, + BySwAddress, + Instance->ConfigData.SwAddressType, + AddressBuffer, + Force + ); + + // + // Delete the cache entries inthe ResolvedCacheTable. + // + Count += ArpDeleteCacheEntryInTable ( + &ArpService->ResolvedCacheTable, + BySwAddress, + Instance->ConfigData.SwAddressType, + AddressBuffer, + Force + ); + + return Count; +} + + +/** + Cancel the arp request. + + @param[in] Instance Pointer to the instance context data. + @param[in] TargetSwAddress Pointer to the buffer containing the target + software address to match the arp request. + @param[in] UserEvent The user event used to notify this request + cancellation. + + @return The count of the cancelled requests. + +**/ +UINTN +ArpCancelRequest ( + IN ARP_INSTANCE_DATA *Instance, + IN VOID *TargetSwAddress OPTIONAL, + IN EFI_EVENT UserEvent OPTIONAL + ) +{ + ARP_SERVICE_DATA *ArpService; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + ARP_CACHE_ENTRY *CacheEntry; + UINTN Count; + + NET_CHECK_SIGNATURE (Instance, ARP_INSTANCE_DATA_SIGNATURE); + + ArpService = Instance->ArpService; + + Count = 0; + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &ArpService->PendingRequestTable) { + CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List); + + if ((TargetSwAddress == NULL) || + (CompareMem ( + TargetSwAddress, + CacheEntry->Addresses[Protocol].AddressPtr, + CacheEntry->Addresses[Protocol].Length + ) == 0)) { + // + // This request entry matches the TargetSwAddress or all requests are to be + // cancelled as TargetSwAddress is NULL. + // + Count += ArpAddressResolved (CacheEntry, Instance, UserEvent); + + if (IsListEmpty (&CacheEntry->UserRequestList)) { + // + // No user requests any more, remove this request cache entry. + // + RemoveEntryList (&CacheEntry->List); + FreePool (CacheEntry); + } + } + } + + return Count; +} + + +/** + Find the cache entry in the cache table. + + @param[in] Instance Pointer to the instance context data. + @param[in] BySwAddress Set to TRUE to look for matching software protocol + addresses. Set to FALSE to look for matching + hardware protocol addresses. + @param[in] AddressBuffer Pointer to address buffer. Set to NULL to match + all addresses. + @param[out] EntryLength The size of an entry in the entries buffer. + @param[out] EntryCount The number of ARP cache entries that are found by + the specified criteria. + @param[out] Entries Pointer to the buffer that will receive the ARP + cache entries. + @param[in] Refresh Set to TRUE to refresh the timeout value of the + matching ARP cache entry. + + @retval EFI_SUCCESS The requested ARP cache entries are copied into + the buffer. + @retval EFI_NOT_FOUND No matching entries found. + @retval EFI_OUT_OF_RESOURCE There is a memory allocation failure. + +**/ +EFI_STATUS +ArpFindCacheEntry ( + IN ARP_INSTANCE_DATA *Instance, + IN BOOLEAN BySwAddress, + IN VOID *AddressBuffer OPTIONAL, + OUT UINT32 *EntryLength OPTIONAL, + OUT UINT32 *EntryCount OPTIONAL, + OUT EFI_ARP_FIND_DATA **Entries OPTIONAL, + IN BOOLEAN Refresh + ) +{ + EFI_STATUS Status; + ARP_SERVICE_DATA *ArpService; + NET_ARP_ADDRESS MatchAddress; + FIND_OPTYPE FindOpType; + LIST_ENTRY *StartEntry; + ARP_CACHE_ENTRY *CacheEntry; + NET_MAP FoundEntries; + UINT32 FoundCount; + EFI_ARP_FIND_DATA *FindData; + LIST_ENTRY *CacheTable; + UINT32 FoundEntryLength; + + ArpService = Instance->ArpService; + + // + // Init the FounEntries used to hold the found cache entries. + // + NetMapInit (&FoundEntries); + + // + // Set the MatchAddress. + // + if (BySwAddress) { + MatchAddress.Type = Instance->ConfigData.SwAddressType; + MatchAddress.Length = Instance->ConfigData.SwAddressLength; + FindOpType = ByProtoAddress; + } else { + MatchAddress.Type = ArpService->SnpMode.IfType; + MatchAddress.Length = (UINT8)ArpService->SnpMode.HwAddressSize; + FindOpType = ByHwAddress; + } + + MatchAddress.AddressPtr = AddressBuffer; + + // + // Search the DeniedCacheTable + // + StartEntry = NULL; + while (TRUE) { + // + // Try to find the matched entries in the DeniedCacheTable. + // + CacheEntry = ArpFindNextCacheEntryInTable ( + &ArpService->DeniedCacheTable, + StartEntry, + FindOpType, + &MatchAddress, + &MatchAddress + ); + if (CacheEntry == NULL) { + // + // Once the CacheEntry is NULL, there are no more matches. + // + break; + } + + // + // Insert the found entry into the map. + // + NetMapInsertTail ( + &FoundEntries, + (VOID *)CacheEntry, + (VOID *)&ArpService->DeniedCacheTable + ); + + // + // Let the next search start from this cache entry. + // + StartEntry = &CacheEntry->List; + + if (Refresh) { + // + // Refresh the DecayTime if needed. + // + CacheEntry->DecayTime = CacheEntry->DefaultDecayTime; + } + } + + // + // Search the ResolvedCacheTable + // + StartEntry = NULL; + while (TRUE) { + CacheEntry = ArpFindNextCacheEntryInTable ( + &ArpService->ResolvedCacheTable, + StartEntry, + FindOpType, + &MatchAddress, + &MatchAddress + ); + if (CacheEntry == NULL) { + // + // Once the CacheEntry is NULL, there are no more matches. + // + break; + } + + // + // Insert the found entry into the map. + // + NetMapInsertTail ( + &FoundEntries, + (VOID *)CacheEntry, + (VOID *)&ArpService->ResolvedCacheTable + ); + + // + // Let the next search start from this cache entry. + // + StartEntry = &CacheEntry->List; + + if (Refresh) { + // + // Refresh the DecayTime if needed. + // + CacheEntry->DecayTime = CacheEntry->DefaultDecayTime; + } + } + + Status = EFI_SUCCESS; + + FoundCount = (UINT32) NetMapGetCount (&FoundEntries); + if (FoundCount == 0) { + Status = EFI_NOT_FOUND; + goto CLEAN_EXIT; + } + + // + // Found the entry length, make sure its 8 bytes alignment. + // + FoundEntryLength = (((sizeof (EFI_ARP_FIND_DATA) + Instance->ConfigData.SwAddressLength + + ArpService->SnpMode.HwAddressSize) + 3) & ~(0x3)); + + if (EntryLength != NULL) { + *EntryLength = FoundEntryLength; + } + + if (EntryCount != NULL) { + // + // Return the found entry count. + // + *EntryCount = FoundCount; + } + + if (Entries == NULL) { + goto CLEAN_EXIT; + } + + // + // Allocate buffer to copy the found entries. + // + FindData = AllocatePool (FoundCount * FoundEntryLength); + if (FindData == NULL) { + DEBUG ((EFI_D_ERROR, "ArpFindCacheEntry: Failed to allocate memory.\n")); + Status = EFI_OUT_OF_RESOURCES; + goto CLEAN_EXIT; + } + + // + // Return the address to the user. + // + *Entries = FindData; + + // + // Dump the entries. + // + while (!NetMapIsEmpty (&FoundEntries)) { + // + // Get a cache entry from the map. + // + CacheEntry = NetMapRemoveHead (&FoundEntries, (VOID **)&CacheTable); + + // + // Set the fields in FindData. + // + FindData->Size = FoundEntryLength; + FindData->DenyFlag = (BOOLEAN)(CacheTable == &ArpService->DeniedCacheTable); + FindData->StaticFlag = (BOOLEAN)(CacheEntry->DefaultDecayTime == 0); + FindData->HwAddressType = ArpService->SnpMode.IfType; + FindData->SwAddressType = Instance->ConfigData.SwAddressType; + FindData->HwAddressLength = (UINT8)ArpService->SnpMode.HwAddressSize; + FindData->SwAddressLength = Instance->ConfigData.SwAddressLength; + + // + // Copy the software address. + // + CopyMem ( + FindData + 1, + CacheEntry->Addresses[Protocol].AddressPtr, + FindData->SwAddressLength + ); + + // + // Copy the hardware address. + // + CopyMem ( + (UINT8 *)(FindData + 1) + FindData->SwAddressLength, + CacheEntry->Addresses[Hardware].AddressPtr, + FindData->HwAddressLength + ); + + // + // Slip to the next FindData. + // + FindData = (EFI_ARP_FIND_DATA *)((UINT8 *)FindData + FoundEntryLength); + } + +CLEAN_EXIT: + + NetMapClean (&FoundEntries); + + return Status; +} + diff --git a/NetworkPkg/ArpDxe/ArpImpl.h b/NetworkPkg/ArpDxe/ArpImpl.h new file mode 100644 index 000000000..47eedc1cb --- /dev/null +++ b/NetworkPkg/ArpDxe/ArpImpl.h @@ -0,0 +1,770 @@ +/** @file + EFI Address Resolution Protocol (ARP) Protocol interface header file. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ARP_IMPL_H_ +#define _ARP_IMPL_H_ + + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +// Ethernet protocol type definitions. +// +#define ARP_ETHER_PROTO_TYPE 0x0806 +#define IPV4_ETHER_PROTO_TYPE 0x0800 +#define IPV6_ETHER_PROTO_TYPE 0x86DD + +// +// ARP opcode definitions. +// +#define ARP_OPCODE_REQUEST 0x0001 +#define ARP_OPCODE_REPLY 0x0002 + +// +// ARP timeout, retry count and interval definitions. +// +#define ARP_DEFAULT_TIMEOUT_VALUE (400 * TICKS_PER_SECOND) +#define ARP_DEFAULT_RETRY_COUNT 2 +#define ARP_DEFAULT_RETRY_INTERVAL (5 * TICKS_PER_MS) +#define ARP_PERIODIC_TIMER_INTERVAL (500 * TICKS_PER_MS) + +// +// ARP packet head definition. +// +#pragma pack(1) +typedef struct { + UINT16 HwType; + UINT16 ProtoType; + UINT8 HwAddrLen; + UINT8 ProtoAddrLen; + UINT16 OpCode; +} ARP_HEAD; +#pragma pack() + +// +// ARP Address definition for internal use. +// +typedef struct { + UINT8 *SenderHwAddr; + UINT8 *SenderProtoAddr; + UINT8 *TargetHwAddr; + UINT8 *TargetProtoAddr; +} ARP_ADDRESS; + +#define MATCH_SW_ADDRESS 0x1 +#define MATCH_HW_ADDRESS 0x2 + +// +// Enumeration for the search type. A search type is specified as the keyword to find +// a cache entry in the cache table. +// +typedef enum { + ByNone = 0, + ByProtoAddress = MATCH_SW_ADDRESS, + ByHwAddress = MATCH_HW_ADDRESS, + ByBoth = MATCH_SW_ADDRESS | MATCH_HW_ADDRESS +} FIND_OPTYPE; + +#define ARP_INSTANCE_DATA_SIGNATURE SIGNATURE_32('A', 'R', 'P', 'I') + +/** + Returns a pointer to the ARP_INSTANCE_DATA structure from the input a. + + If the signatures matches, then a pointer to the data structure that contains + a specified field of that data structure is returned. + + @param a Pointer to the field specified by ArpProto within a data + structure of type ARP_INSTANCE_DATA. + +**/ +#define ARP_INSTANCE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + ARP_INSTANCE_DATA, \ + ArpProto, \ + ARP_INSTANCE_DATA_SIGNATURE \ + ) + +typedef struct _ARP_SERVICE_DATA ARP_SERVICE_DATA; + +// +// ARP instance context data structure. +// +typedef struct { + UINT32 Signature; + ARP_SERVICE_DATA *ArpService; + EFI_HANDLE Handle; + EFI_ARP_PROTOCOL ArpProto; + LIST_ENTRY List; + EFI_ARP_CONFIG_DATA ConfigData; + BOOLEAN Configured; + BOOLEAN InDestroy; +} ARP_INSTANCE_DATA; + +#define ARP_SERVICE_DATA_SIGNATURE SIGNATURE_32('A', 'R', 'P', 'S') + +/** + Returns a pointer to the ARP_SERVICE_DATA structure from the input a. + + If the signatures matches, then a pointer to the data structure that contains + a specified field of that data structure is returned. + + @param a Pointer to the field specified by ServiceBinding within + a data structure of type ARP_SERVICE_DATA. + +**/ +#define ARP_SERVICE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + ARP_SERVICE_DATA, \ + ServiceBinding, \ + ARP_SERVICE_DATA_SIGNATURE \ + ) + +// +// ARP service data structure. +// +struct _ARP_SERVICE_DATA { + UINT32 Signature; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + + EFI_HANDLE MnpChildHandle; + EFI_HANDLE ImageHandle; + EFI_HANDLE ControllerHandle; + + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + EFI_MANAGED_NETWORK_CONFIG_DATA MnpConfigData; + EFI_MANAGED_NETWORK_COMPLETION_TOKEN RxToken; + + EFI_SIMPLE_NETWORK_MODE SnpMode; + + UINTN ChildrenNumber; + LIST_ENTRY ChildrenList; + + LIST_ENTRY PendingRequestTable; + LIST_ENTRY DeniedCacheTable; + LIST_ENTRY ResolvedCacheTable; + + EFI_EVENT PeriodicTimer; +}; + +// +// User request context structure. +// +typedef struct { + LIST_ENTRY List; + ARP_INSTANCE_DATA *Instance; + EFI_EVENT UserRequestEvent; + VOID *UserHwAddrBuffer; +} USER_REQUEST_CONTEXT; + +#define ARP_MAX_PROTOCOL_ADDRESS_LEN sizeof(EFI_IP_ADDRESS) +#define ARP_MAX_HARDWARE_ADDRESS_LEN sizeof(EFI_MAC_ADDRESS) + +typedef union { + UINT8 ProtoAddress[ARP_MAX_PROTOCOL_ADDRESS_LEN]; + UINT8 HwAddress[ARP_MAX_HARDWARE_ADDRESS_LEN]; +} NET_ARP_ADDRESS_UNION; + +// +// ARP address structure in an ARP packet. +// +typedef struct { + UINT16 Type; + UINT8 Length; + UINT8 *AddressPtr; + NET_ARP_ADDRESS_UNION Buffer; +} NET_ARP_ADDRESS; + +// +// Enumeration for ARP address type. +// +typedef enum { + Hardware, + Protocol +} ARP_ADDRESS_TYPE; + +// +// ARP cache entry definition. +// +typedef struct { + LIST_ENTRY List; + + UINT32 RetryCount; + UINT32 DefaultDecayTime; + UINT32 DecayTime; + UINT32 NextRetryTime; + + NET_ARP_ADDRESS Addresses[2]; + + LIST_ENTRY UserRequestList; +} ARP_CACHE_ENTRY; + +/** + This function is used to assign a station address to the ARP cache for this instance + of the ARP driver. + + Each ARP instance has one station address. The EFI_ARP_PROTOCOL driver will + respond to ARP requests that match this registered station address. A call to + this function with the ConfigData field set to NULL will reset this ARP instance. + + Once a protocol type and station address have been assigned to this ARP instance, + all the following ARP functions will use this information. Attempting to change + the protocol type or station address to a configured ARP instance will result in errors. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param ConfigData Pointer to the EFI_ARP_CONFIG_DATA structure. + + @retval EFI_SUCCESS The new station address was successfully + registered. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. SwAddressLength is zero when + ConfigData is not NULL. StationAddress is NULL + when ConfigData is not NULL. + @retval EFI_ACCESS_DENIED The SwAddressType, SwAddressLength, or + StationAddress is different from the one that is + already registered. + @retval EFI_OUT_OF_RESOURCES Storage for the new StationAddress could not be + allocated. + +**/ +EFI_STATUS +EFIAPI +ArpConfigure ( + IN EFI_ARP_PROTOCOL *This, + IN EFI_ARP_CONFIG_DATA *ConfigData OPTIONAL + ); + +/** + This function is used to insert entries into the ARP cache. + + ARP cache entries are typically inserted and updated by network protocol drivers + as network traffic is processed. Most ARP cache entries will time out and be + deleted if the network traffic stops. ARP cache entries that were inserted + by the Add() function may be static (will not time out) or dynamic (will time out). + Default ARP cache timeout values are not covered in most network protocol + specifications (although RFC 1122 comes pretty close) and will only be + discussed in general in this specification. The timeout values that are + used in the EFI Sample Implementation should be used only as a guideline. + Final product implementations of the EFI network stack should be tuned for + their expected network environments. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param DenyFlag Set to TRUE if this entry is a deny entry. Set to + FALSE if this entry is a normal entry. + @param TargetSwAddress Pointer to a protocol address to add (or deny). + May be set to NULL if DenyFlag is TRUE. + @param TargetHwAddress Pointer to a hardware address to add (or deny). + May be set to NULL if DenyFlag is TRUE. + @param TimeoutValue Time in 100-ns units that this entry will remain + in the ARP cache. A value of zero means that the + entry is permanent. A nonzero value will override + the one given by Configure() if the entry to be + added is a dynamic entry. + @param Overwrite If TRUE, the matching cache entry will be + overwritten with the supplied parameters. If + FALSE, EFI_ACCESS_DENIED is returned if the + corresponding cache entry already exists. + + @retval EFI_SUCCESS The entry has been added or updated. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. DenyFlag is FALSE and + TargetHwAddress is NULL. DenyFlag is FALSE and + TargetSwAddress is NULL. TargetHwAddress is NULL + and TargetSwAddress is NULL. Both TargetSwAddress + and TargetHwAddress are not NULL when DenyFlag is + TRUE. + @retval EFI_OUT_OF_RESOURCES The new ARP cache entry could not be allocated. + @retval EFI_ACCESS_DENIED The ARP cache entry already exists and Overwrite + is not true. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + +**/ +EFI_STATUS +EFIAPI +ArpAdd ( + IN EFI_ARP_PROTOCOL *This, + IN BOOLEAN DenyFlag, + IN VOID *TargetSwAddress OPTIONAL, + IN VOID *TargetHwAddress OPTIONAL, + IN UINT32 TimeoutValue, + IN BOOLEAN Overwrite + ); + +/** + This function searches the ARP cache for matching entries and allocates a buffer into + which those entries are copied. + + The first part of the allocated buffer is EFI_ARP_FIND_DATA, following which + are protocol address pairs and hardware address pairs. + When finding a specific protocol address (BySwAddress is TRUE and AddressBuffer + is not NULL), the ARP cache timeout for the found entry is reset if Refresh is + set to TRUE. If the found ARP cache entry is a permanent entry, it is not + affected by Refresh. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param BySwAddress Set to TRUE to look for matching software protocol + addresses. Set to FALSE to look for matching + hardware protocol addresses. + @param AddressBuffer Pointer to address buffer. Set to NULL to match + all addresses. + @param EntryLength The size of an entry in the entries buffer. + @param EntryCount The number of ARP cache entries that are found by + the specified criteria. + @param Entries Pointer to the buffer that will receive the ARP + cache entries. + @param Refresh Set to TRUE to refresh the timeout value of the + matching ARP cache entry. + + @retval EFI_SUCCESS The requested ARP cache entries were copied into + the buffer. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. Both EntryCount and EntryLength are + NULL, when Refresh is FALSE. + @retval EFI_NOT_FOUND No matching entries were found. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + +**/ +EFI_STATUS +EFIAPI +ArpFind ( + IN EFI_ARP_PROTOCOL *This, + IN BOOLEAN BySwAddress, + IN VOID *AddressBuffer OPTIONAL, + OUT UINT32 *EntryLength OPTIONAL, + OUT UINT32 *EntryCount OPTIONAL, + OUT EFI_ARP_FIND_DATA **Entries OPTIONAL, + IN BOOLEAN Refresh + ); + +/** + This function removes specified ARP cache entries. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param BySwAddress Set to TRUE to delete matching protocol addresses. + Set to FALSE to delete matching hardware + addresses. + @param AddressBuffer Pointer to the address buffer that is used as a + key to look for the cache entry. Set to NULL to + delete all entries. + + @retval EFI_SUCCESS The entry was removed from the ARP cache. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_FOUND The specified deletion key was not found. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + +**/ +EFI_STATUS +EFIAPI +ArpDelete ( + IN EFI_ARP_PROTOCOL *This, + IN BOOLEAN BySwAddress, + IN VOID *AddressBuffer OPTIONAL + ); + +/** + This function delete all dynamic entries from the ARP cache that match the specified + software protocol type. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + + @retval EFI_SUCCESS The cache has been flushed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_FOUND There are no matching dynamic cache entries. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + +**/ +EFI_STATUS +EFIAPI +ArpFlush ( + IN EFI_ARP_PROTOCOL *This + ); + +/** + This function tries to resolve the TargetSwAddress and optionally returns a + TargetHwAddress if it already exists in the ARP cache. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param TargetSwAddress Pointer to the protocol address to resolve. + @param ResolvedEvent Pointer to the event that will be signaled when + the address is resolved or some error occurs. + @param TargetHwAddress Pointer to the buffer for the resolved hardware + address in network byte order. + + @retval EFI_SUCCESS The data is copied from the ARP cache into the + TargetHwAddress buffer. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. TargetHwAddress is NULL. + @retval EFI_ACCESS_DENIED The requested address is not present in the normal + ARP cache but is present in the deny address list. + Outgoing traffic to that address is forbidden. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + @retval EFI_NOT_READY The request has been started and is not finished. + +**/ +EFI_STATUS +EFIAPI +ArpRequest ( + IN EFI_ARP_PROTOCOL *This, + IN VOID *TargetSwAddress OPTIONAL, + IN EFI_EVENT ResolvedEvent OPTIONAL, + OUT VOID *TargetHwAddress + ); + +/** + This function aborts the previous ARP request (identified by This, TargetSwAddress + and ResolvedEvent) that is issued by EFI_ARP_PROTOCOL.Request(). + + If the request is in the internal ARP request queue, the request is aborted + immediately and its ResolvedEvent is signaled. Only an asynchronous address + request needs to be canceled. If TargeSwAddress and ResolveEvent are both + NULL, all the pending asynchronous requests that have been issued by This + instance will be cancelled and their corresponding events will be signaled. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param TargetSwAddress Pointer to the protocol address in previous + request session. + @param ResolvedEvent Pointer to the event that is used as the + notification event in previous request session. + + @retval EFI_SUCCESS The pending request session(s) is/are aborted and + corresponding event(s) is/are signaled. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. TargetSwAddress is not NULL and + ResolvedEvent is NULL. TargetSwAddress is NULL and + ResolvedEvent is not NULL. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + @retval EFI_NOT_FOUND The request is not issued by + EFI_ARP_PROTOCOL.Request(). + +**/ +EFI_STATUS +EFIAPI +ArpCancel ( + IN EFI_ARP_PROTOCOL *This, + IN VOID *TargetSwAddress OPTIONAL, + IN EFI_EVENT ResolvedEvent OPTIONAL + ); + +/** + Configure the instance using the ConfigData. ConfigData is already validated. + + @param[in] Instance Pointer to the instance context data to be + configured. + @param[in] ConfigData Pointer to the configuration data used to + configure the instance. + + @retval EFI_SUCCESS The instance is configured with the ConfigData. + @retval EFI_ACCESS_DENIED The instance is already configured and the + ConfigData tries to reset some unchangeable + fields. + @retval EFI_INVALID_PARAMETER The ConfigData provides a non-unicast IPv4 address + when the SwAddressType is IPv4. + @retval EFI_OUT_OF_RESOURCES The instance fails to configure due to memory + limitation. + +**/ +EFI_STATUS +ArpConfigureInstance ( + IN ARP_INSTANCE_DATA *Instance, + IN EFI_ARP_CONFIG_DATA *ConfigData OPTIONAL + ); + +/** + Find the CacheEntry, using ProtocolAddress or HardwareAddress or both, as the keyword, + in the DeniedCacheTable. + + @param[in] ArpService Pointer to the arp service context data. + @param[in] ProtocolAddress Pointer to the protocol address. + @param[in] HardwareAddress Pointer to the hardware address. + + @return Pointer to the matched cache entry, if NULL no match is found. + +**/ +ARP_CACHE_ENTRY * +ArpFindDeniedCacheEntry ( + IN ARP_SERVICE_DATA *ArpService, + IN NET_ARP_ADDRESS *ProtocolAddress OPTIONAL, + IN NET_ARP_ADDRESS *HardwareAddress OPTIONAL + ); + +/** + Find the CacheEntry which matches the requirements in the specified CacheTable. + + @param[in] CacheTable Pointer to the arp cache table. + @param[in] StartEntry Pointer to the start entry this search begins with + in the cache table. + @param[in] FindOpType The search type. + @param[in] ProtocolAddress Pointer to the protocol address to match. + @param[in] HardwareAddress Pointer to the hardware address to match. + + @return Pointer to the matched arp cache entry, if NULL, no match is found. + +**/ +ARP_CACHE_ENTRY * +ArpFindNextCacheEntryInTable ( + IN LIST_ENTRY *CacheTable, + IN LIST_ENTRY *StartEntry, + IN FIND_OPTYPE FindOpType, + IN NET_ARP_ADDRESS *ProtocolAddress OPTIONAL, + IN NET_ARP_ADDRESS *HardwareAddress OPTIONAL + ); + +/** + Allocate a cache entry and initialize it. + + @param[in] Instance Pointer to the instance context data. + + @return Pointer to the new created cache entry. + +**/ +ARP_CACHE_ENTRY * +ArpAllocCacheEntry ( + IN ARP_INSTANCE_DATA *Instance + ); + +/** + Fill the addresses in the CacheEntry using the information passed in by + HwAddr and SwAddr. + + @param[in] CacheEntry Pointer to the cache entry. + @param[in] HwAddr Pointer to the software address. + @param[in] SwAddr Pointer to the hardware address. + + @return None. + +**/ +VOID +ArpFillAddressInCacheEntry ( + IN ARP_CACHE_ENTRY *CacheEntry, + IN NET_ARP_ADDRESS *HwAddr OPTIONAL, + IN NET_ARP_ADDRESS *SwAddr OPTIONAL + ); + +/** + Turn the CacheEntry into the resolved status. + + @param[in] CacheEntry Pointer to the resolved cache entry. + @param[in] Instance Pointer to the instance context data. + @param[in] UserEvent Pointer to the UserEvent to notify. + + @return The count of notifications sent to the instance. + +**/ +UINTN +ArpAddressResolved ( + IN ARP_CACHE_ENTRY *CacheEntry, + IN ARP_INSTANCE_DATA *Instance OPTIONAL, + IN EFI_EVENT UserEvent OPTIONAL + ); + +/** + Delete cache entries in all the cache tables. + + @param[in] Instance Pointer to the instance context data. + @param[in] BySwAddress Delete the cache entry by software address or by + hardware address. + @param[in] AddressBuffer Pointer to the buffer containing the address to + match for the deletion. + @param[in] Force This deletion is forced or not. + + @return The count of the deleted cache entries. + +**/ +UINTN +ArpDeleteCacheEntry ( + IN ARP_INSTANCE_DATA *Instance, + IN BOOLEAN BySwAddress, + IN UINT8 *AddressBuffer OPTIONAL, + IN BOOLEAN Force + ); + +/** + Send out an arp frame using the CachEntry and the ArpOpCode. + + @param[in] Instance Pointer to the instance context data. + @param[in] CacheEntry Pointer to the configuration data used to + configure the instance. + @param[in] ArpOpCode The opcode used to send out this Arp frame, either + request or reply. + + @return None. + +**/ +VOID +ArpSendFrame ( + IN ARP_INSTANCE_DATA *Instance, + IN ARP_CACHE_ENTRY *CacheEntry, + IN UINT16 ArpOpCode + ); + +/** + Initialize the instance context data. + + @param[in] ArpService Pointer to the arp service context data this + instance belongs to. + @param[out] Instance Pointer to the instance context data. + + @return None. + +**/ +VOID +ArpInitInstance ( + IN ARP_SERVICE_DATA *ArpService, + OUT ARP_INSTANCE_DATA *Instance + ); + +/** + Process the Arp packets received from Mnp, the procedure conforms to RFC826. + + @param[in] Context Pointer to the context data registerd to the + Event. + + @return None. + +**/ +VOID +EFIAPI +ArpOnFrameRcvdDpc ( + IN VOID *Context + ); + +/** + Queue ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK. + + @param[in] Event The Event this notify function registered to. + @param[in] Context Pointer to the context data registerd to the + Event. + + @return None. + +**/ +VOID +EFIAPI +ArpOnFrameRcvd ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Process the already sent arp packets. + + @param[in] Context Pointer to the context data registerd to the + Event. + + @return None. + +**/ +VOID +EFIAPI +ArpOnFrameSentDpc ( + IN VOID *Context + ); + +/** + Request ArpOnFrameSentDpc as a DPC at TPL_CALLBACK. + + @param[in] Event The Event this notify function registered to. + @param[in] Context Pointer to the context data registerd to the + Event. + + @return None. + +**/ +VOID +EFIAPI +ArpOnFrameSent ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Process the arp cache olding and drive the retrying arp requests. + + @param[in] Event The Event this notify function registered to. + @param[in] Context Pointer to the context data registerd to the + Event. + + @return None. + +**/ +VOID +EFIAPI +ArpTimerHandler ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Cancel the arp request. + + @param[in] Instance Pointer to the instance context data. + @param[in] TargetSwAddress Pointer to the buffer containing the target + software address to match the arp request. + @param[in] UserEvent The user event used to notify this request + cancellation. + + @return The count of the cancelled requests. + +**/ +UINTN +ArpCancelRequest ( + IN ARP_INSTANCE_DATA *Instance, + IN VOID *TargetSwAddress OPTIONAL, + IN EFI_EVENT UserEvent OPTIONAL + ); + +/** + Find the cache entry in the cache table. + + @param[in] Instance Pointer to the instance context data. + @param[in] BySwAddress Set to TRUE to look for matching software protocol + addresses. Set to FALSE to look for matching + hardware protocol addresses. + @param[in] AddressBuffer Pointer to address buffer. Set to NULL to match + all addresses. + @param[out] EntryLength The size of an entry in the entries buffer. + @param[out] EntryCount The number of ARP cache entries that are found by + the specified criteria. + @param[out] Entries Pointer to the buffer that will receive the ARP + cache entries. + @param[in] Refresh Set to TRUE to refresh the timeout value of the + matching ARP cache entry. + + @retval EFI_SUCCESS The requested ARP cache entries are copied into + the buffer. + @retval EFI_NOT_FOUND No matching entries found. + @retval EFI_OUT_OF_RESOURCE There is a memory allocation failure. + +**/ +EFI_STATUS +ArpFindCacheEntry ( + IN ARP_INSTANCE_DATA *Instance, + IN BOOLEAN BySwAddress, + IN VOID *AddressBuffer OPTIONAL, + OUT UINT32 *EntryLength OPTIONAL, + OUT UINT32 *EntryCount OPTIONAL, + OUT EFI_ARP_FIND_DATA **Entries OPTIONAL, + IN BOOLEAN Refresh + ); + +#endif diff --git a/NetworkPkg/ArpDxe/ArpMain.c b/NetworkPkg/ArpDxe/ArpMain.c new file mode 100644 index 000000000..f06121fed --- /dev/null +++ b/NetworkPkg/ArpDxe/ArpMain.c @@ -0,0 +1,739 @@ +/** @file + Implementation of EFI Address Resolution Protocol (ARP) Protocol interface functions. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "ArpImpl.h" + + +/** + This function is used to assign a station address to the ARP cache for this instance + of the ARP driver. + + Each ARP instance has one station address. The EFI_ARP_PROTOCOL driver will + respond to ARP requests that match this registered station address. A call to + this function with the ConfigData field set to NULL will reset this ARP instance. + + Once a protocol type and station address have been assigned to this ARP instance, + all the following ARP functions will use this information. Attempting to change + the protocol type or station address to a configured ARP instance will result in errors. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param ConfigData Pointer to the EFI_ARP_CONFIG_DATA structure. + + @retval EFI_SUCCESS The new station address was successfully + registered. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. SwAddressLength is zero when + ConfigData is not NULL. StationAddress is NULL + when ConfigData is not NULL. + @retval EFI_ACCESS_DENIED The SwAddressType, SwAddressLength, or + StationAddress is different from the one that is + already registered. + @retval EFI_OUT_OF_RESOURCES Storage for the new StationAddress could not be + allocated. + +**/ +EFI_STATUS +EFIAPI +ArpConfigure ( + IN EFI_ARP_PROTOCOL *This, + IN EFI_ARP_CONFIG_DATA *ConfigData OPTIONAL + ) +{ + EFI_STATUS Status; + ARP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + if ((ConfigData != NULL) && + ((ConfigData->SwAddressLength == 0) || + (ConfigData->StationAddress == NULL) || + (ConfigData->SwAddressType <= 1500))) { + return EFI_INVALID_PARAMETER; + } + + Instance = ARP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Configure this instance, the ConfigData has already passed the basic checks. + // + Status = ArpConfigureInstance (Instance, ConfigData); + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + This function is used to insert entries into the ARP cache. + + ARP cache entries are typically inserted and updated by network protocol drivers + as network traffic is processed. Most ARP cache entries will time out and be + deleted if the network traffic stops. ARP cache entries that were inserted + by the Add() function may be static (will not time out) or dynamic (will time out). + Default ARP cache timeout values are not covered in most network protocol + specifications (although RFC 1122 comes pretty close) and will only be + discussed in general in this specification. The timeout values that are + used in the EFI Sample Implementation should be used only as a guideline. + Final product implementations of the EFI network stack should be tuned for + their expected network environments. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param DenyFlag Set to TRUE if this entry is a deny entry. Set to + FALSE if this entry is a normal entry. + @param TargetSwAddress Pointer to a protocol address to add (or deny). + May be set to NULL if DenyFlag is TRUE. + @param TargetHwAddress Pointer to a hardware address to add (or deny). + May be set to NULL if DenyFlag is TRUE. + @param TimeoutValue Time in 100-ns units that this entry will remain + in the ARP cache. A value of zero means that the + entry is permanent. A nonzero value will override + the one given by Configure() if the entry to be + added is a dynamic entry. + @param Overwrite If TRUE, the matching cache entry will be + overwritten with the supplied parameters. If + FALSE, EFI_ACCESS_DENIED is returned if the + corresponding cache entry already exists. + + @retval EFI_SUCCESS The entry has been added or updated. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. DenyFlag is FALSE and + TargetHwAddress is NULL. DenyFlag is FALSE and + TargetSwAddress is NULL. TargetHwAddress is NULL + and TargetSwAddress is NULL. Both TargetSwAddress + and TargetHwAddress are not NULL when DenyFlag is + TRUE. + @retval EFI_OUT_OF_RESOURCES The new ARP cache entry could not be allocated. + @retval EFI_ACCESS_DENIED The ARP cache entry already exists and Overwrite + is not true. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + +**/ +EFI_STATUS +EFIAPI +ArpAdd ( + IN EFI_ARP_PROTOCOL *This, + IN BOOLEAN DenyFlag, + IN VOID *TargetSwAddress OPTIONAL, + IN VOID *TargetHwAddress OPTIONAL, + IN UINT32 TimeoutValue, + IN BOOLEAN Overwrite + ) +{ + EFI_STATUS Status; + ARP_INSTANCE_DATA *Instance; + ARP_SERVICE_DATA *ArpService; + ARP_CACHE_ENTRY *CacheEntry; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + NET_ARP_ADDRESS MatchAddress[2]; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (((!DenyFlag) && ((TargetHwAddress == NULL) || (TargetSwAddress == NULL))) || + (DenyFlag && (TargetHwAddress != NULL) && (TargetSwAddress != NULL)) || + ((TargetHwAddress == NULL) && (TargetSwAddress == NULL))) { + return EFI_INVALID_PARAMETER; + } + + Instance = ARP_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + Status = EFI_SUCCESS; + ArpService = Instance->ArpService; + SnpMode = &Instance->ArpService->SnpMode; + + // + // Fill the hardware address part in the MatchAddress. + // + MatchAddress[Hardware].Type = SnpMode->IfType; + MatchAddress[Hardware].Length = (UINT8) SnpMode->HwAddressSize; + MatchAddress[Hardware].AddressPtr = TargetHwAddress; + + // + // Fill the software address part in the MatchAddress. + // + MatchAddress[Protocol].Type = Instance->ConfigData.SwAddressType; + MatchAddress[Protocol].Length = Instance->ConfigData.SwAddressLength; + MatchAddress[Protocol].AddressPtr = TargetSwAddress; + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // See whether the entry to add exists. Check the DeinedCacheTable first. + // + CacheEntry = ArpFindDeniedCacheEntry ( + ArpService, + &MatchAddress[Protocol], + &MatchAddress[Hardware] + ); + + if (CacheEntry == NULL) { + // + // Check the ResolvedCacheTable + // + CacheEntry = ArpFindNextCacheEntryInTable ( + &ArpService->ResolvedCacheTable, + NULL, + ByBoth, + &MatchAddress[Protocol], + &MatchAddress[Hardware] + ); + } + + if ((CacheEntry != NULL) && !Overwrite) { + // + // The entry to add exists, if not Overwirte, deny this add request. + // + Status = EFI_ACCESS_DENIED; + goto UNLOCK_EXIT; + } + + if ((CacheEntry == NULL) && (TargetSwAddress != NULL)) { + // + // Check whether there are pending requests matching the entry to be added. + // + CacheEntry = ArpFindNextCacheEntryInTable ( + &ArpService->PendingRequestTable, + NULL, + ByProtoAddress, + &MatchAddress[Protocol], + NULL + ); + } + + if (CacheEntry != NULL) { + // + // Remove it from the Table. + // + RemoveEntryList (&CacheEntry->List); + } else { + // + // It's a new entry, allocate memory for the entry. + // + CacheEntry = ArpAllocCacheEntry (Instance); + + if (CacheEntry == NULL) { + DEBUG ((EFI_D_ERROR, "ArpAdd: Failed to allocate pool for CacheEntry.\n")); + Status = EFI_OUT_OF_RESOURCES; + goto UNLOCK_EXIT; + } + } + + // + // Overwrite these parameters. + // + CacheEntry->DefaultDecayTime = TimeoutValue; + CacheEntry->DecayTime = TimeoutValue; + + // + // Fill in the addresses. + // + ArpFillAddressInCacheEntry ( + CacheEntry, + &MatchAddress[Hardware], + &MatchAddress[Protocol] + ); + + // + // Inform the user if there is any. + // + ArpAddressResolved (CacheEntry, NULL, NULL); + + // + // Add this CacheEntry to the corresponding CacheTable. + // + if (DenyFlag) { + InsertHeadList (&ArpService->DeniedCacheTable, &CacheEntry->List); + } else { + InsertHeadList (&ArpService->ResolvedCacheTable, &CacheEntry->List); + } + +UNLOCK_EXIT: + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + This function searches the ARP cache for matching entries and allocates a buffer into + which those entries are copied. + + The first part of the allocated buffer is EFI_ARP_FIND_DATA, following which + are protocol address pairs and hardware address pairs. + When finding a specific protocol address (BySwAddress is TRUE and AddressBuffer + is not NULL), the ARP cache timeout for the found entry is reset if Refresh is + set to TRUE. If the found ARP cache entry is a permanent entry, it is not + affected by Refresh. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param BySwAddress Set to TRUE to look for matching software protocol + addresses. Set to FALSE to look for matching + hardware protocol addresses. + @param AddressBuffer Pointer to address buffer. Set to NULL to match + all addresses. + @param EntryLength The size of an entry in the entries buffer. + @param EntryCount The number of ARP cache entries that are found by + the specified criteria. + @param Entries Pointer to the buffer that will receive the ARP + cache entries. + @param Refresh Set to TRUE to refresh the timeout value of the + matching ARP cache entry. + + @retval EFI_SUCCESS The requested ARP cache entries were copied into + the buffer. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. Both EntryCount and EntryLength are + NULL, when Refresh is FALSE. + @retval EFI_NOT_FOUND No matching entries were found. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + +**/ +EFI_STATUS +EFIAPI +ArpFind ( + IN EFI_ARP_PROTOCOL *This, + IN BOOLEAN BySwAddress, + IN VOID *AddressBuffer OPTIONAL, + OUT UINT32 *EntryLength OPTIONAL, + OUT UINT32 *EntryCount OPTIONAL, + OUT EFI_ARP_FIND_DATA **Entries OPTIONAL, + IN BOOLEAN Refresh + ) +{ + EFI_STATUS Status; + ARP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if ((This == NULL) || + (!Refresh && (EntryCount == NULL) && (EntryLength == NULL)) || + ((Entries != NULL) && ((EntryLength == NULL) || (EntryCount == NULL)))) { + return EFI_INVALID_PARAMETER; + } + + Instance = ARP_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // All the check passed, find the cache entries now. + // + Status = ArpFindCacheEntry ( + Instance, + BySwAddress, + AddressBuffer, + EntryLength, + EntryCount, + Entries, + Refresh + ); + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + This function removes specified ARP cache entries. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param BySwAddress Set to TRUE to delete matching protocol addresses. + Set to FALSE to delete matching hardware + addresses. + @param AddressBuffer Pointer to the address buffer that is used as a + key to look for the cache entry. Set to NULL to + delete all entries. + + @retval EFI_SUCCESS The entry was removed from the ARP cache. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_FOUND The specified deletion key was not found. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + +**/ +EFI_STATUS +EFIAPI +ArpDelete ( + IN EFI_ARP_PROTOCOL *This, + IN BOOLEAN BySwAddress, + IN VOID *AddressBuffer OPTIONAL + ) +{ + ARP_INSTANCE_DATA *Instance; + UINTN Count; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = ARP_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Delete the specified cache entries. + // + Count = ArpDeleteCacheEntry (Instance, BySwAddress, AddressBuffer, TRUE); + + gBS->RestoreTPL (OldTpl); + + return (Count == 0) ? EFI_NOT_FOUND : EFI_SUCCESS; +} + + +/** + This function delete all dynamic entries from the ARP cache that match the specified + software protocol type. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + + @retval EFI_SUCCESS The cache has been flushed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_FOUND There are no matching dynamic cache entries. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + +**/ +EFI_STATUS +EFIAPI +ArpFlush ( + IN EFI_ARP_PROTOCOL *This + ) +{ + ARP_INSTANCE_DATA *Instance; + UINTN Count; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = ARP_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Delete the dynamic entries from the cache table. + // + Count = ArpDeleteCacheEntry (Instance, FALSE, NULL, FALSE); + + gBS->RestoreTPL (OldTpl); + + return (Count == 0) ? EFI_NOT_FOUND : EFI_SUCCESS; +} + + +/** + This function tries to resolve the TargetSwAddress and optionally returns a + TargetHwAddress if it already exists in the ARP cache. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param TargetSwAddress Pointer to the protocol address to resolve. + @param ResolvedEvent Pointer to the event that will be signaled when + the address is resolved or some error occurs. + @param TargetHwAddress Pointer to the buffer for the resolved hardware + address in network byte order. + + @retval EFI_SUCCESS The data is copied from the ARP cache into the + TargetHwAddress buffer. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. TargetHwAddress is NULL. + @retval EFI_ACCESS_DENIED The requested address is not present in the normal + ARP cache but is present in the deny address list. + Outgoing traffic to that address is forbidden. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + @retval EFI_NOT_READY The request has been started and is not finished. + +**/ +EFI_STATUS +EFIAPI +ArpRequest ( + IN EFI_ARP_PROTOCOL *This, + IN VOID *TargetSwAddress OPTIONAL, + IN EFI_EVENT ResolvedEvent OPTIONAL, + OUT VOID *TargetHwAddress + ) +{ + EFI_STATUS Status; + ARP_INSTANCE_DATA *Instance; + ARP_SERVICE_DATA *ArpService; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + ARP_CACHE_ENTRY *CacheEntry; + NET_ARP_ADDRESS HardwareAddress; + NET_ARP_ADDRESS ProtocolAddress; + USER_REQUEST_CONTEXT *RequestContext; + EFI_TPL OldTpl; + + if ((This == NULL) || (TargetHwAddress == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = ARP_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + Status = EFI_SUCCESS; + ArpService = Instance->ArpService; + SnpMode = &ArpService->SnpMode; + + if ((TargetSwAddress == NULL) || + ((Instance->ConfigData.SwAddressType == IPV4_ETHER_PROTO_TYPE) && + IP4_IS_LOCAL_BROADCAST (*((UINT32 *)TargetSwAddress)))) { + // + // Return the hardware broadcast address. + // + CopyMem (TargetHwAddress, &SnpMode->BroadcastAddress, SnpMode->HwAddressSize); + + goto SIGNAL_USER; + } + + if ((Instance->ConfigData.SwAddressType == IPV4_ETHER_PROTO_TYPE) && + IP4_IS_MULTICAST (NTOHL (*((UINT32 *)TargetSwAddress)))) { + // + // If the software address is an IPv4 multicast address, invoke Mnp to + // resolve the address. + // + Status = ArpService->Mnp->McastIpToMac ( + ArpService->Mnp, + FALSE, + TargetSwAddress, + TargetHwAddress + ); + goto SIGNAL_USER; + } + + HardwareAddress.Type = SnpMode->IfType; + HardwareAddress.Length = (UINT8)SnpMode->HwAddressSize; + HardwareAddress.AddressPtr = NULL; + + ProtocolAddress.Type = Instance->ConfigData.SwAddressType; + ProtocolAddress.Length = Instance->ConfigData.SwAddressLength; + ProtocolAddress.AddressPtr = TargetSwAddress; + + // + // Initialize the TargetHwAddrss to a zero address. + // + ZeroMem (TargetHwAddress, SnpMode->HwAddressSize); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Check whether the software address is in the denied table. + // + CacheEntry = ArpFindDeniedCacheEntry (ArpService, &ProtocolAddress, NULL); + if (CacheEntry != NULL) { + Status = EFI_ACCESS_DENIED; + goto UNLOCK_EXIT; + } + + // + // Check whether the software address is already resolved. + // + CacheEntry = ArpFindNextCacheEntryInTable ( + &ArpService->ResolvedCacheTable, + NULL, + ByProtoAddress, + &ProtocolAddress, + NULL + ); + if (CacheEntry != NULL) { + // + // Resolved, copy the address into the user buffer. + // + CopyMem ( + TargetHwAddress, + CacheEntry->Addresses[Hardware].AddressPtr, + CacheEntry->Addresses[Hardware].Length + ); + + goto UNLOCK_EXIT; + } + + if (ResolvedEvent == NULL) { + Status = EFI_NOT_READY; + goto UNLOCK_EXIT; + } + + // + // Create a request context for this arp request. + // + RequestContext = AllocatePool (sizeof(USER_REQUEST_CONTEXT)); + if (RequestContext == NULL) { + DEBUG ((EFI_D_ERROR, "ArpRequest: Allocate memory for RequestContext failed.\n")); + + Status = EFI_OUT_OF_RESOURCES; + goto UNLOCK_EXIT; + } + + RequestContext->Instance = Instance; + RequestContext->UserRequestEvent = ResolvedEvent; + RequestContext->UserHwAddrBuffer = TargetHwAddress; + InitializeListHead (&RequestContext->List); + + // + // Check whether there is a same request. + // + CacheEntry = ArpFindNextCacheEntryInTable ( + &ArpService->PendingRequestTable, + NULL, + ByProtoAddress, + &ProtocolAddress, + NULL + ); + if (CacheEntry != NULL) { + + CacheEntry->NextRetryTime = Instance->ConfigData.RetryTimeOut; + CacheEntry->RetryCount = Instance->ConfigData.RetryCount; + } else { + // + // Allocate a cache entry for this request. + // + CacheEntry = ArpAllocCacheEntry (Instance); + if (CacheEntry == NULL) { + DEBUG ((EFI_D_ERROR, "ArpRequest: Allocate memory for CacheEntry failed.\n")); + FreePool (RequestContext); + + Status = EFI_OUT_OF_RESOURCES; + goto UNLOCK_EXIT; + } + + // + // Fill the software address. + // + ArpFillAddressInCacheEntry (CacheEntry, &HardwareAddress, &ProtocolAddress); + + // + // Add this entry into the PendingRequestTable. + // + InsertTailList (&ArpService->PendingRequestTable, &CacheEntry->List); + } + + // + // Link this request context into the cache entry. + // + InsertHeadList (&CacheEntry->UserRequestList, &RequestContext->List); + + // + // Send out the ARP Request frame. + // + ArpSendFrame (Instance, CacheEntry, ARP_OPCODE_REQUEST); + Status = EFI_NOT_READY; + +UNLOCK_EXIT: + + gBS->RestoreTPL (OldTpl); + +SIGNAL_USER: + + if ((ResolvedEvent != NULL) && (Status == EFI_SUCCESS)) { + gBS->SignalEvent (ResolvedEvent); + + // + // Dispatch the DPC queued by the NotifyFunction of ResolvedEvent. + // + DispatchDpc (); + } + + return Status; +} + + +/** + This function aborts the previous ARP request (identified by This, TargetSwAddress + and ResolvedEvent) that is issued by EFI_ARP_PROTOCOL.Request(). + + If the request is in the internal ARP request queue, the request is aborted + immediately and its ResolvedEvent is signaled. Only an asynchronous address + request needs to be canceled. If TargeSwAddress and ResolveEvent are both + NULL, all the pending asynchronous requests that have been issued by This + instance will be cancelled and their corresponding events will be signaled. + + @param This Pointer to the EFI_ARP_PROTOCOL instance. + @param TargetSwAddress Pointer to the protocol address in previous + request session. + @param ResolvedEvent Pointer to the event that is used as the + notification event in previous request session. + + @retval EFI_SUCCESS The pending request session(s) is/are aborted and + corresponding event(s) is/are signaled. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. TargetSwAddress is not NULL and + ResolvedEvent is NULL. TargetSwAddress is NULL and + ResolvedEvent is not NULL. + @retval EFI_NOT_STARTED The ARP driver instance has not been configured. + @retval EFI_NOT_FOUND The request is not issued by + EFI_ARP_PROTOCOL.Request(). + +**/ +EFI_STATUS +EFIAPI +ArpCancel ( + IN EFI_ARP_PROTOCOL *This, + IN VOID *TargetSwAddress OPTIONAL, + IN EFI_EVENT ResolvedEvent OPTIONAL + ) +{ + ARP_INSTANCE_DATA *Instance; + UINTN Count; + EFI_TPL OldTpl; + + if ((This == NULL) || + ((TargetSwAddress != NULL) && (ResolvedEvent == NULL)) || + ((TargetSwAddress == NULL) && (ResolvedEvent != NULL))) { + return EFI_INVALID_PARAMETER; + } + + Instance = ARP_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Cancel the specified request. + // + Count = ArpCancelRequest (Instance, TargetSwAddress, ResolvedEvent); + + // + // Dispatch the DPCs queued by the NotifyFunction of the events signaled + // by ArpCancleRequest. + // + DispatchDpc (); + + gBS->RestoreTPL (OldTpl); + + return (Count == 0) ? EFI_NOT_FOUND : EFI_SUCCESS; +} diff --git a/NetworkPkg/ArpDxe/ComponentName.c b/NetworkPkg/ArpDxe/ComponentName.c new file mode 100644 index 000000000..bcee1e144 --- /dev/null +++ b/NetworkPkg/ArpDxe/ComponentName.c @@ -0,0 +1,219 @@ +/** @file + UEFI Component Name(2) protocol implementation for ArpDxe driver. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "ArpDriver.h" + + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gArpComponentName = { + ArpComponentNameGetDriverName, + ArpComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gArpComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) ArpComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) ArpComponentNameGetControllerName, + "en" +}; + + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mArpDriverNameTable[] = { + { "eng;en", L"ARP Network Service Driver" }, + { NULL, NULL } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mArpControllerNameTable[] = { + { "eng;en", L"ARP Controller" }, + { NULL, NULL } +}; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +ArpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mArpDriverNameTable, + DriverName, + (BOOLEAN)(This == &gArpComponentName) + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +ArpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_ARP_PROTOCOL *Arp; + + // + // Only provide names for child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiManagedNetworkProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiArpProtocolGuid, + (VOID **)&Arp, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mArpControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gArpComponentName) + ); +} diff --git a/NetworkPkg/Dhcp4Dxe/ComponentName.c b/NetworkPkg/Dhcp4Dxe/ComponentName.c new file mode 100644 index 000000000..7c404fa26 --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/ComponentName.c @@ -0,0 +1,431 @@ +/** @file + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Dhcp4Impl.h" + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DhcpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DhcpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gDhcp4ComponentName = { + DhcpComponentNameGetDriverName, + DhcpComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gDhcp4ComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) DhcpComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) DhcpComponentNameGetControllerName, + "en" +}; + + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mDhcpDriverNameTable[] = { + { + "eng;en", + L"DHCP Protocol Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gDhcpControllerNameTable = NULL; + +CHAR16 *mDhcp4ControllerName[] = { + L"DHCPv4 (State=0, Stopped)", + L"DHCPv4 (State=1, Init)", + L"DHCPv4 (State=2, Selecting)", + L"DHCPv4 (State=3, Requesting)", + L"DHCPv4 (State=4, Bound)", + L"DHCPv4 (State=5, Renewing)", + L"DHCPv4 (State=6, Rebinding)", + L"DHCPv4 (State=7, InitReboot)", + L"DHCPv4 (State=8, Rebooting)" +}; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DhcpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mDhcpDriverNameTable, + DriverName, + (BOOLEAN)(This == &gDhcp4ComponentName) + ); +} + +/** + Update the component name for the Dhcp4 child handle. + + @param Dhcp4[in] A pointer to the EFI_DHCP4_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + @retval EFI_DEVICE_ERROR DHCP is in unknown state. + +**/ +EFI_STATUS +UpdateName ( + IN EFI_DHCP4_PROTOCOL *Dhcp4 + ) +{ + EFI_STATUS Status; + EFI_DHCP4_MODE_DATA Dhcp4ModeData; + + if (Dhcp4 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer. + // + Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4ModeData); + if (EFI_ERROR (Status)) { + return Status; + } + + if (gDhcpControllerNameTable != NULL) { + FreeUnicodeStringTable (gDhcpControllerNameTable); + gDhcpControllerNameTable = NULL; + } + + if (Dhcp4ModeData.State > Dhcp4Rebooting) { + return EFI_DEVICE_ERROR; + } + + Status = AddUnicodeString2 ( + "eng", + gDhcp4ComponentName.SupportedLanguages, + &gDhcpControllerNameTable, + mDhcp4ControllerName[Dhcp4ModeData.State], + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gDhcp4ComponentName2.SupportedLanguages, + &gDhcpControllerNameTable, + mDhcp4ControllerName[Dhcp4ModeData.State], + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DhcpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_DHCP4_PROTOCOL *Dhcp4; + + // + // Only provide names for child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiUdp4ProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiDhcp4ProtocolGuid, + (VOID **)&Dhcp4, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateName (Dhcp4); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gDhcpControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gDhcp4ComponentName) + ); +} diff --git a/NetworkPkg/Dhcp4Dxe/Dhcp4Driver.c b/NetworkPkg/Dhcp4Dxe/Dhcp4Driver.c new file mode 100644 index 000000000..e891b6888 --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/Dhcp4Driver.c @@ -0,0 +1,732 @@ +/** @file + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Dhcp4Impl.h" +#include "Dhcp4Driver.h" + +EFI_DRIVER_BINDING_PROTOCOL gDhcp4DriverBinding = { + Dhcp4DriverBindingSupported, + Dhcp4DriverBindingStart, + Dhcp4DriverBindingStop, + 0xa, + NULL, + NULL +}; + +EFI_SERVICE_BINDING_PROTOCOL mDhcp4ServiceBindingTemplate = { + Dhcp4ServiceBindingCreateChild, + Dhcp4ServiceBindingDestroyChild +}; + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + Entry point of the DHCP driver to install various protocols. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +Dhcp4DriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gDhcp4DriverBinding, + ImageHandle, + &gDhcp4ComponentName, + &gDhcp4ComponentName2 + ); +} + + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device + @retval EFI_ALREADY_STARTED This driver is already running on this device + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Dhcp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + return Status; +} + + + +/** + Configure the default UDP child to receive all the DHCP traffics + on this network interface. + + @param[in] UdpIo The UDP IO to configure + @param[in] Context The context to the function + + @retval EFI_SUCCESS The UDP IO is successfully configured. + @retval Others Failed to configure the UDP child. + +**/ +EFI_STATUS +EFIAPI +DhcpConfigUdpIo ( + IN UDP_IO *UdpIo, + IN VOID *Context + ) +{ + EFI_UDP4_CONFIG_DATA UdpConfigData; + + UdpConfigData.AcceptBroadcast = TRUE; + UdpConfigData.AcceptPromiscuous = FALSE; + UdpConfigData.AcceptAnyPort = FALSE; + UdpConfigData.AllowDuplicatePort = TRUE; + UdpConfigData.TypeOfService = 0; + UdpConfigData.TimeToLive = 64; + UdpConfigData.DoNotFragment = FALSE; + UdpConfigData.ReceiveTimeout = 0; + UdpConfigData.TransmitTimeout = 0; + + UdpConfigData.UseDefaultAddress = FALSE; + UdpConfigData.StationPort = DHCP_CLIENT_PORT; + UdpConfigData.RemotePort = DHCP_SERVER_PORT; + + ZeroMem (&UdpConfigData.StationAddress, sizeof (EFI_IPv4_ADDRESS)); + ZeroMem (&UdpConfigData.SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + ZeroMem (&UdpConfigData.RemoteAddress, sizeof (EFI_IPv4_ADDRESS)); + + return UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfigData);; +} + + + +/** + Destroy the DHCP service. The Dhcp4 service may be partly initialized, + or partly destroyed. If a resource is destroyed, it is marked as so in + case the destroy failed and being called again later. + + @param[in] DhcpSb The DHCP service instance to destroy. + + @retval EFI_SUCCESS Always return success. + +**/ +EFI_STATUS +Dhcp4CloseService ( + IN DHCP_SERVICE *DhcpSb + ) +{ + DhcpCleanLease (DhcpSb); + + if (DhcpSb->UdpIo != NULL) { + UdpIoFreeIo (DhcpSb->UdpIo); + DhcpSb->UdpIo = NULL; + } + + if (DhcpSb->Timer != NULL) { + gBS->SetTimer (DhcpSb->Timer, TimerCancel, 0); + gBS->CloseEvent (DhcpSb->Timer); + + DhcpSb->Timer = NULL; + } + + return EFI_SUCCESS; +} + + + +/** + Create a new DHCP service binding instance for the controller. + + @param[in] Controller The controller to install DHCP service binding + protocol onto + @param[in] ImageHandle The driver's image handle + @param[out] Service The variable to receive the created DHCP service + instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource . + @retval EFI_SUCCESS The DHCP service instance is created. + @retval other Other error occurs. + +**/ +EFI_STATUS +Dhcp4CreateService ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE ImageHandle, + OUT DHCP_SERVICE **Service + ) +{ + DHCP_SERVICE *DhcpSb; + EFI_STATUS Status; + + *Service = NULL; + DhcpSb = AllocateZeroPool (sizeof (DHCP_SERVICE)); + + if (DhcpSb == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + DhcpSb->Signature = DHCP_SERVICE_SIGNATURE; + DhcpSb->ServiceState = DHCP_UNCONFIGED; + DhcpSb->Controller = Controller; + DhcpSb->Image = ImageHandle; + InitializeListHead (&DhcpSb->Children); + DhcpSb->DhcpState = Dhcp4Stopped; + DhcpSb->Xid = NET_RANDOM (NetRandomInitSeed ()); + CopyMem ( + &DhcpSb->ServiceBinding, + &mDhcp4ServiceBindingTemplate, + sizeof (EFI_SERVICE_BINDING_PROTOCOL) + ); + // + // Create various resources, UdpIo, Timer, and get Mac address + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + DhcpOnTimerTick, + DhcpSb, + &DhcpSb->Timer + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + DhcpSb->UdpIo = UdpIoCreateIo ( + Controller, + ImageHandle, + DhcpConfigUdpIo, + UDP_IO_UDP4_VERSION, + NULL + ); + + if (DhcpSb->UdpIo == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + DhcpSb->HwLen = (UINT8) DhcpSb->UdpIo->SnpMode.HwAddressSize; + DhcpSb->HwType = DhcpSb->UdpIo->SnpMode.IfType; + CopyMem (&DhcpSb->Mac, &DhcpSb->UdpIo->SnpMode.CurrentAddress, sizeof (DhcpSb->Mac)); + + *Service = DhcpSb; + return EFI_SUCCESS; + +ON_ERROR: + Dhcp4CloseService (DhcpSb); + FreePool (DhcpSb); + + return Status; +} + + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Dhcp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + DHCP_SERVICE *DhcpSb; + EFI_STATUS Status; + + // + // First: test for the DHCP4 Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDhcp4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + if (Status == EFI_SUCCESS) { + return EFI_ALREADY_STARTED; + } + + Status = Dhcp4CreateService (ControllerHandle, This->DriverBindingHandle, &DhcpSb); + + if (EFI_ERROR (Status)) { + return Status; + } + ASSERT (DhcpSb != NULL); + + // + // Start the receiving + // + Status = UdpIoRecvDatagram (DhcpSb->UdpIo, DhcpInput, DhcpSb, 0); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + Status = gBS->SetTimer (DhcpSb->Timer, TimerPeriodic, TICKS_PER_SECOND); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Install the Dhcp4ServiceBinding Protocol onto ControlerHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiDhcp4ServiceBindingProtocolGuid, + &DhcpSb->ServiceBinding, + NULL + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return Status; + +ON_ERROR: + Dhcp4CloseService (DhcpSb); + FreePool (DhcpSb); + return Status; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +Dhcp4DestroyChildEntry ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + DHCP_PROTOCOL *Instance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = NET_LIST_USER_STRUCT_S (Entry, DHCP_PROTOCOL, Link, DHCP_PROTOCOL_SIGNATURE); + ServiceBinding = (EFI_SERVICE_BINDING_PROTOCOL *) Context; + + return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle); +} + + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +Dhcp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + DHCP_SERVICE *DhcpSb; + EFI_HANDLE NicHandle; + EFI_STATUS Status; + LIST_ENTRY *List; + UINTN ListLength; + + // + // DHCP driver opens UDP child, So, the ControllerHandle is the + // UDP child handle. locate the Nic handle first. + // + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp4ProtocolGuid); + + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiDhcp4ServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + NicHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + DhcpSb = DHCP_SERVICE_FROM_THIS (ServiceBinding); + if (!IsListEmpty (&DhcpSb->Children)) { + // + // Destroy all the children instances before destory the service. + // + List = &DhcpSb->Children; + Status = NetDestroyLinkList ( + List, + Dhcp4DestroyChildEntry, + ServiceBinding, + &ListLength + ); + if (EFI_ERROR (Status) || ListLength != 0) { + Status = EFI_DEVICE_ERROR; + } + } + + if (NumberOfChildren == 0 && !IsListEmpty (&DhcpSb->Children)) { + Status = EFI_DEVICE_ERROR; + } + + if (NumberOfChildren == 0 && IsListEmpty (&DhcpSb->Children)) { + // + // Destroy the service itself if no child instance left. + // + DhcpSb->ServiceState = DHCP_DESTROY; + + gBS->UninstallProtocolInterface ( + NicHandle, + &gEfiDhcp4ServiceBindingProtocolGuid, + ServiceBinding + ); + + Dhcp4CloseService (DhcpSb); + + if (gDhcpControllerNameTable != NULL) { + FreeUnicodeStringTable (gDhcpControllerNameTable); + gDhcpControllerNameTable = NULL; + } + FreePool (DhcpSb); + + Status = EFI_SUCCESS; + } + + return Status; +} + + +/** + Initialize a new DHCP instance. + + @param DhcpSb The dhcp service instance + @param Instance The dhcp instance to initialize + +**/ +VOID +DhcpInitProtocol ( + IN DHCP_SERVICE *DhcpSb, + IN OUT DHCP_PROTOCOL *Instance + ) +{ + Instance->Signature = DHCP_PROTOCOL_SIGNATURE; + CopyMem (&Instance->Dhcp4Protocol, &mDhcp4ProtocolTemplate, sizeof (Instance->Dhcp4Protocol)); + InitializeListHead (&Instance->Link); + Instance->Handle = NULL; + Instance->Service = DhcpSb; + Instance->InDestroy = FALSE; + Instance->CompletionEvent = NULL; + Instance->RenewRebindEvent = NULL; + Instance->Token = NULL; + Instance->UdpIo = NULL; + Instance->ElaspedTime = 0; + NetbufQueInit (&Instance->ResponseQueue); +} + + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +Dhcp4ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ) +{ + DHCP_SERVICE *DhcpSb; + DHCP_PROTOCOL *Instance; + EFI_STATUS Status; + EFI_TPL OldTpl; + VOID *Udp4; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = AllocatePool (sizeof (*Instance)); + + if (Instance == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + DhcpSb = DHCP_SERVICE_FROM_THIS (This); + DhcpInitProtocol (DhcpSb, Instance); + + // + // Install DHCP4 onto ChildHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiDhcp4ProtocolGuid, + &Instance->Dhcp4Protocol, + NULL + ); + + if (EFI_ERROR (Status)) { + FreePool (Instance); + return Status; + } + + Instance->Handle = *ChildHandle; + + // + // Open the Udp4 protocol BY_CHILD. + // + Status = gBS->OpenProtocol ( + DhcpSb->UdpIo->UdpHandle, + &gEfiUdp4ProtocolGuid, + (VOID **) &Udp4, + gDhcp4DriverBinding.DriverBindingHandle, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + gBS->UninstallMultipleProtocolInterfaces ( + Instance->Handle, + &gEfiDhcp4ProtocolGuid, + &Instance->Dhcp4Protocol, + NULL + ); + + FreePool (Instance); + return Status; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&DhcpSb->Children, &Instance->Link); + DhcpSb->NumChildren++; + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; +} + + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Dhcp4ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + DHCP_SERVICE *DhcpSb; + DHCP_PROTOCOL *Instance; + EFI_DHCP4_PROTOCOL *Dhcp; + EFI_TPL OldTpl; + EFI_STATUS Status; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Retrieve the private context data structures + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiDhcp4ProtocolGuid, + (VOID **) &Dhcp, + gDhcp4DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Instance = DHCP_INSTANCE_FROM_THIS (Dhcp); + DhcpSb = DHCP_SERVICE_FROM_THIS (This); + + if (Instance->Service != DhcpSb) { + return EFI_INVALID_PARAMETER; + } + + // + // A child can be destroyed more than once. For example, + // Dhcp4DriverBindingStop will destroy all of its children. + // when caller driver is being stopped, it will destroy the + // dhcp child it opens. + // + if (Instance->InDestroy) { + return EFI_SUCCESS; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Instance->InDestroy = TRUE; + + // + // Close the Udp4 protocol. + // + gBS->CloseProtocol ( + DhcpSb->UdpIo->UdpHandle, + &gEfiUdp4ProtocolGuid, + gDhcp4DriverBinding.DriverBindingHandle, + ChildHandle + ); + + // + // Uninstall the DHCP4 protocol first to enable a top down destruction. + // + gBS->RestoreTPL (OldTpl); + Status = gBS->UninstallProtocolInterface ( + ChildHandle, + &gEfiDhcp4ProtocolGuid, + Dhcp + ); + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + if (EFI_ERROR (Status)) { + Instance->InDestroy = FALSE; + + gBS->RestoreTPL (OldTpl); + return Status; + } + + if (DhcpSb->ActiveChild == Instance) { + DhcpYieldControl (DhcpSb); + } + + RemoveEntryList (&Instance->Link); + DhcpSb->NumChildren--; + + if (Instance->UdpIo != NULL) { + UdpIoCleanIo (Instance->UdpIo); + gBS->CloseProtocol ( + Instance->UdpIo->UdpHandle, + &gEfiUdp4ProtocolGuid, + Instance->Service->Image, + Instance->Handle + ); + UdpIoFreeIo (Instance->UdpIo); + Instance->UdpIo = NULL; + Instance->Token = NULL; + } + + gBS->RestoreTPL (OldTpl); + + FreePool (Instance); + return EFI_SUCCESS; +} diff --git a/NetworkPkg/Dhcp4Dxe/Dhcp4Driver.h b/NetworkPkg/Dhcp4Dxe/Dhcp4Driver.h new file mode 100644 index 000000000..cebde20dc --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/Dhcp4Driver.h @@ -0,0 +1,146 @@ +/** @file + Header for the DHCP4 driver. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_DHCP4_DRIVER_H__ +#define __EFI_DHCP4_DRIVER_H__ + +extern EFI_COMPONENT_NAME_PROTOCOL gDhcp4ComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gDhcp4ComponentName2; +extern EFI_UNICODE_STRING_TABLE *gDhcpControllerNameTable; + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device + @retval EFI_ALREADY_STARTED This driver is already running on this device + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Dhcp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Dhcp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +Dhcp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +Dhcp4ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Dhcp4ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +#endif diff --git a/NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.inf b/NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.inf new file mode 100644 index 000000000..e34aab0a6 --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.inf @@ -0,0 +1,67 @@ +## @file +# This module produces EFI DHCPv4 Protocol and EFI DHCPv4 Service Binding Protocol. +# +# This module produces EFI DHCPv4 Protocol upon EFI UDPv4 Protocol, to provide the +# capability to collect configuration information for the EFI IPv4 Protocol drivers +# and to provide DHCPv4 server and PXE boot server discovery services. +# +# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = Dhcp4Dxe + MODULE_UNI_FILE = Dhcp4Dxe.uni + FILE_GUID = 94734718-0BBC-47fb-96A5-EE7A5AE6A2AD + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = Dhcp4DriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = gDhcp4DriverBinding +# COMPONENT_NAME = gDhcp4ComponentName +# COMPONENT_NAME2 = gDhcp4ComponentName2 +# + +[Sources] + Dhcp4Impl.c + Dhcp4Io.c + Dhcp4Io.h + ComponentName.c + Dhcp4Driver.h + Dhcp4Driver.c + Dhcp4Option.c + Dhcp4Option.h + Dhcp4Impl.h + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + BaseLib + UefiLib + UefiBootServicesTableLib + UefiDriverEntryPoint + DebugLib + NetLib + UdpIoLib + + +[Protocols] + gEfiDhcp4ServiceBindingProtocolGuid ## BY_START + gEfiUdp4ServiceBindingProtocolGuid ## TO_START + gEfiDhcp4ProtocolGuid ## BY_START + gEfiUdp4ProtocolGuid ## TO_START + +[UserExtensions.TianoCore."ExtraFiles"] + Dhcp4DxeExtra.uni diff --git a/NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.uni b/NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.uni new file mode 100644 index 000000000..5405b88d2 --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.uni @@ -0,0 +1,18 @@ +// /** @file +// This module produces EFI DHCPv4 Protocol and EFI DHCPv4 Service Binding Protocol. +// +// This module produces EFI DHCPv4 Protocol upon EFI UDPv4 Protocol, to provide the +// capability to collect configuration information for the EFI IPv4 Protocol drivers +// and to provide DHCPv4 server and PXE boot server discovery services. +// +// Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "EFI DHCPv4 Driver" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces EFI DHCPv4 Protocol using the EFI UDPv4 Protocol, providing the capability to collect configuration information for the EFI IPv4 Protocol drivers and providing DHCPv4 server and PXE boot server discovery services." + diff --git a/NetworkPkg/Dhcp4Dxe/Dhcp4DxeExtra.uni b/NetworkPkg/Dhcp4Dxe/Dhcp4DxeExtra.uni new file mode 100644 index 000000000..ca62553f8 --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/Dhcp4DxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// Dhcp4Dxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"DHCP v4 DXE Driver" + + diff --git a/NetworkPkg/Dhcp4Dxe/Dhcp4Impl.c b/NetworkPkg/Dhcp4Dxe/Dhcp4Impl.c new file mode 100644 index 000000000..0b35bdf4d --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/Dhcp4Impl.c @@ -0,0 +1,1802 @@ +/** @file + This file implement the EFI_DHCP4_PROTOCOL interface. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Dhcp4Impl.h" + +/** + Returns the current operating mode and cached data packet for the EFI DHCPv4 Protocol driver. + + The GetModeData() function returns the current operating mode and cached data + packet for the EFI DHCPv4 Protocol driver. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[out] Dhcp4ModeData Pointer to storage for the EFI_DHCP4_MODE_DATA structure. + + @retval EFI_SUCCESS The mode data was returned. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4GetModeData ( + IN EFI_DHCP4_PROTOCOL *This, + OUT EFI_DHCP4_MODE_DATA *Dhcp4ModeData + ); + +/** + Initializes, changes, or resets the operational settings for the EFI DHCPv4 Protocol driver. + + The Configure() function is used to initialize, change, or reset the operational + settings of the EFI DHCPv4 Protocol driver for the communication device on which + the EFI DHCPv4 Service Binding Protocol is installed. This function can be + successfully called only if both of the following are true: + * This instance of the EFI DHCPv4 Protocol driver is in the Dhcp4Stopped, Dhcp4Init, + Dhcp4InitReboot, or Dhcp4Bound states. + * No other EFI DHCPv4 Protocol driver instance that is controlled by this EFI + DHCPv4 Service Binding Protocol driver instance has configured this EFI DHCPv4 + Protocol driver. + When this driver is in the Dhcp4Stopped state, it can transfer into one of the + following two possible initial states: + * Dhcp4Init + * Dhcp4InitReboot + The driver can transfer into these states by calling Configure() with a non-NULL + Dhcp4CfgData. The driver will transfer into the appropriate state based on the + supplied client network address in the ClientAddress parameter and DHCP options + in the OptionList parameter as described in RFC 2131. + When Configure() is called successfully while Dhcp4CfgData is set to NULL, the + default configuring data will be reset in the EFI DHCPv4 Protocol driver and + the state of the EFI DHCPv4 Protocol driver will not be changed. If one instance + wants to make it possible for another instance to configure the EFI DHCPv4 Protocol + driver, it must call this function with Dhcp4CfgData set to NULL. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[in] Dhcp4CfgData Pointer to the EFI_DHCP4_CONFIG_DATA. + + @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init or + Dhcp4InitReboot state, if the original state of this driver + was Dhcp4Stopped and the value of Dhcp4CfgData was + not NULL. Otherwise, the state was left unchanged. + @retval EFI_ACCESS_DENIED This instance of the EFI DHCPv4 Protocol driver was not in the + Dhcp4Stopped, Dhcp4Init, Dhcp4InitReboot, or Dhcp4Bound state; + Or onother instance of this EFI DHCPv4 Protocol driver is already + in a valid configured state. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Configure ( + IN EFI_DHCP4_PROTOCOL *This, + IN EFI_DHCP4_CONFIG_DATA *Dhcp4CfgData OPTIONAL + ); + +/** + Starts the DHCP configuration process. + + The Start() function starts the DHCP configuration process. This function can + be called only when the EFI DHCPv4 Protocol driver is in the Dhcp4Init or + Dhcp4InitReboot state. + If the DHCP process completes successfully, the state of the EFI DHCPv4 Protocol + driver will be transferred through Dhcp4Selecting and Dhcp4Requesting to the + Dhcp4Bound state. The CompletionEvent will then be signaled if it is not NULL. + If the process aborts, either by the user or by some unexpected network error, + the state is restored to the Dhcp4Init state. The Start() function can be called + again to restart the process. + Refer to RFC 2131 for precise state transitions during this process. At the + time when each event occurs in this process, the callback function that was set + by EFI_DHCP4_PROTOCOL.Configure() will be called and the user can take this + opportunity to control the process. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[in] CompletionEvent If not NULL, indicates the event that will be signaled when the + EFI DHCPv4 Protocol driver is transferred into the + Dhcp4Bound state or when the DHCP process is aborted. + EFI_DHCP4_PROTOCOL.GetModeData() can be called to + check the completion status. If NULL, + EFI_DHCP4_PROTOCOL.Start() will wait until the driver + is transferred into the Dhcp4Bound state or the process fails. + + @retval EFI_SUCCESS The DHCP configuration process has started, or it has completed + when CompletionEvent is NULL. + @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped + state. EFI_DHCP4_PROTOCOL. Configure() needs to be called. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_TIMEOUT The DHCP configuration process failed because no response was + received from the server within the specified timeout value. + @retval EFI_ABORTED The user aborted the DHCP process. + @retval EFI_ALREADY_STARTED Some other EFI DHCPv4 Protocol instance already started the + DHCP process. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Start ( + IN EFI_DHCP4_PROTOCOL *This, + IN EFI_EVENT CompletionEvent OPTIONAL + ); + +/** + Extends the lease time by sending a request packet. + + The RenewRebind() function is used to manually extend the lease time when the + EFI DHCPv4 Protocol driver is in the Dhcp4Bound state and the lease time has + not expired yet. This function will send a request packet to the previously + found server (or to any server when RebindRequest is TRUE) and transfer the + state into the Dhcp4Renewing state (or Dhcp4Rebinding when RebindingRequest is + TRUE). When a response is received, the state is returned to Dhcp4Bound. + If no response is received before the try count is exceeded (the RequestTryCount + field that is specified in EFI_DHCP4_CONFIG_DATA) but before the lease time that + was issued by the previous server expires, the driver will return to the Dhcp4Bound + state and the previous configuration is restored. The outgoing and incoming packets + can be captured by the EFI_DHCP4_CALLBACK function. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[in] RebindRequest If TRUE, this function broadcasts the request packets and enters + the Dhcp4Rebinding state. Otherwise, it sends a unicast + request packet and enters the Dhcp4Renewing state. + @param[in] CompletionEvent If not NULL, this event is signaled when the renew/rebind phase + completes or some error occurs. + EFI_DHCP4_PROTOCOL.GetModeData() can be called to + check the completion status. If NULL, + EFI_DHCP4_PROTOCOL.RenewRebind() will busy-wait + until the DHCP process finishes. + + @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the + Dhcp4Renewing state or is back to the Dhcp4Bound state. + @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped + state. EFI_DHCP4_PROTOCOL.Configure() needs to + be called. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_TIMEOUT There was no response from the server when the try count was + exceeded. + @retval EFI_ACCESS_DENIED The driver is not in the Dhcp4Bound state. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4RenewRebind ( + IN EFI_DHCP4_PROTOCOL *This, + IN BOOLEAN RebindRequest, + IN EFI_EVENT CompletionEvent OPTIONAL + ); + +/** + Releases the current address configuration. + + The Release() function releases the current configured IP address by doing either + of the following: + * Sending a DHCPRELEASE packet when the EFI DHCPv4 Protocol driver is in the + Dhcp4Bound state + * Setting the previously assigned IP address that was provided with the + EFI_DHCP4_PROTOCOL.Configure() function to 0.0.0.0 when the driver is in + Dhcp4InitReboot state + After a successful call to this function, the EFI DHCPv4 Protocol driver returns + to the Dhcp4Init state and any subsequent incoming packets will be discarded silently. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + + @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init phase. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_ACCESS_DENIED The EFI DHCPv4 Protocol driver is not Dhcp4InitReboot state. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Release ( + IN EFI_DHCP4_PROTOCOL *This + ); + +/** + Stops the current address configuration. + + The Stop() function is used to stop the DHCP configuration process. After this + function is called successfully, the EFI DHCPv4 Protocol driver is transferred + into the Dhcp4Stopped state. EFI_DHCP4_PROTOCOL.Configure() needs to be called + before DHCP configuration process can be started again. This function can be + called when the EFI DHCPv4 Protocol driver is in any state. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + + @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Stopped phase. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Stop ( + IN EFI_DHCP4_PROTOCOL *This + ); + +/** + Builds a DHCP packet, given the options to be appended or deleted or replaced. + + The Build() function is used to assemble a new packet from the original packet + by replacing or deleting existing options or appending new options. This function + does not change any state of the EFI DHCPv4 Protocol driver and can be used at + any time. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[in] SeedPacket Initial packet to be used as a base for building new packet. + @param[in] DeleteCount Number of opcodes in the DeleteList. + @param[in] DeleteList List of opcodes to be deleted from the seed packet. + Ignored if DeleteCount is zero. + @param[in] AppendCount Number of entries in the OptionList. + @param[in] AppendList Pointer to a DHCP option list to be appended to SeedPacket. + If SeedPacket also contains options in this list, they are + replaced by new options (except pad option). Ignored if + AppendCount is zero. Type EFI_DHCP4_PACKET_OPTION + @param[out] NewPacket Pointer to storage for the pointer to the new allocated packet. + Use the EFI Boot Service FreePool() on the resulting pointer + when done with the packet. + + @retval EFI_SUCCESS The new packet was built. + @retval EFI_OUT_OF_RESOURCES Storage for the new packet could not be allocated. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Build ( + IN EFI_DHCP4_PROTOCOL *This, + IN EFI_DHCP4_PACKET *SeedPacket, + IN UINT32 DeleteCount, + IN UINT8 *DeleteList OPTIONAL, + IN UINT32 AppendCount, + IN EFI_DHCP4_PACKET_OPTION *AppendList[] OPTIONAL, + OUT EFI_DHCP4_PACKET **NewPacket + ); + +/** + Transmits a DHCP formatted packet and optionally waits for responses. + + The TransmitReceive() function is used to transmit a DHCP packet and optionally + wait for the response from servers. This function does not change the state of + the EFI DHCPv4 Protocol driver and thus can be used at any time. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[in] Token Pointer to the EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN structure. + + @retval EFI_SUCCESS The packet was successfully queued for transmission. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_NOT_READY The previous call to this function has not finished yet. Try to call + this function after collection process completes. + @retval EFI_NO_MAPPING The default station address is not available yet. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval Others Some other unexpected error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4TransmitReceive ( + IN EFI_DHCP4_PROTOCOL *This, + IN EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN *Token + ); + +/** + Parses the packed DHCP option data. + + The Parse() function is used to retrieve the option list from a DHCP packet. + If *OptionCount isn't zero, and there is enough space for all the DHCP options + in the Packet, each element of PacketOptionList is set to point to somewhere in + the Packet->Dhcp4.Option where a new DHCP option begins. If RFC3396 is supported, + the caller should reassemble the parsed DHCP options to get the finial result. + If *OptionCount is zero or there isn't enough space for all of them, the number + of DHCP options in the Packet is returned in OptionCount. + + @param This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param Packet Pointer to packet to be parsed. + @param OptionCount On input, the number of entries in the PacketOptionList. + On output, the number of entries that were written into the + PacketOptionList. + @param PacketOptionList List of packet option entries to be filled in. End option or pad + options are not included. + + @retval EFI_SUCCESS The packet was successfully parsed. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_BUFFER_TOO_SMALL One or more of the following conditions is TRUE: + 1) *OptionCount is smaller than the number of options that + were found in the Packet. + 2) PacketOptionList is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Parse ( + IN EFI_DHCP4_PROTOCOL *This, + IN EFI_DHCP4_PACKET *Packet, + IN OUT UINT32 *OptionCount, + OUT EFI_DHCP4_PACKET_OPTION *PacketOptionList[] OPTIONAL + ); + +EFI_DHCP4_PROTOCOL mDhcp4ProtocolTemplate = { + EfiDhcp4GetModeData, + EfiDhcp4Configure, + EfiDhcp4Start, + EfiDhcp4RenewRebind, + EfiDhcp4Release, + EfiDhcp4Stop, + EfiDhcp4Build, + EfiDhcp4TransmitReceive, + EfiDhcp4Parse +}; + +/** + Returns the current operating mode and cached data packet for the EFI DHCPv4 Protocol driver. + + The GetModeData() function returns the current operating mode and cached data + packet for the EFI DHCPv4 Protocol driver. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[out] Dhcp4ModeData Pointer to storage for the EFI_DHCP4_MODE_DATA structure. + + @retval EFI_SUCCESS The mode data was returned. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4GetModeData ( + IN EFI_DHCP4_PROTOCOL *This, + OUT EFI_DHCP4_MODE_DATA *Dhcp4ModeData + ) +{ + DHCP_PROTOCOL *Instance; + DHCP_SERVICE *DhcpSb; + DHCP_PARAMETER *Para; + EFI_TPL OldTpl; + IP4_ADDR Ip; + + // + // First validate the parameters. + // + if ((This == NULL) || (Dhcp4ModeData == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP_INSTANCE_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + DhcpSb = Instance->Service; + + // + // Caller can use GetModeData to retrieve current DHCP states + // no matter whether it is the active child or not. + // + Dhcp4ModeData->State = (EFI_DHCP4_STATE) DhcpSb->DhcpState; + CopyMem (&Dhcp4ModeData->ConfigData, &DhcpSb->ActiveConfig, sizeof (Dhcp4ModeData->ConfigData)); + CopyMem (&Dhcp4ModeData->ClientMacAddress, &DhcpSb->Mac, sizeof (Dhcp4ModeData->ClientMacAddress)); + + Ip = HTONL (DhcpSb->ClientAddr); + CopyMem (&Dhcp4ModeData->ClientAddress, &Ip, sizeof (EFI_IPv4_ADDRESS)); + + Ip = HTONL (DhcpSb->Netmask); + CopyMem (&Dhcp4ModeData->SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS)); + + Ip = HTONL (DhcpSb->ServerAddr); + CopyMem (&Dhcp4ModeData->ServerAddress, &Ip, sizeof (EFI_IPv4_ADDRESS)); + + Para = DhcpSb->Para; + + if (Para != NULL) { + Ip = HTONL (Para->Router); + CopyMem (&Dhcp4ModeData->RouterAddress, &Ip, sizeof (EFI_IPv4_ADDRESS)); + Dhcp4ModeData->LeaseTime = Para->Lease; + } else { + ZeroMem (&Dhcp4ModeData->RouterAddress, sizeof (EFI_IPv4_ADDRESS)); + Dhcp4ModeData->LeaseTime = 0xffffffff; + } + + Dhcp4ModeData->ReplyPacket = DhcpSb->Selected; + + gBS->RestoreTPL (OldTpl); + return EFI_SUCCESS; +} + + +/** + Free the resource related to the configure parameters. + DHCP driver will make a copy of the user's configure + such as the time out value. + + @param Config The DHCP configure data + +**/ +VOID +DhcpCleanConfigure ( + IN OUT EFI_DHCP4_CONFIG_DATA *Config + ) +{ + UINT32 Index; + + if (Config->DiscoverTimeout != NULL) { + FreePool (Config->DiscoverTimeout); + } + + if (Config->RequestTimeout != NULL) { + FreePool (Config->RequestTimeout); + } + + if (Config->OptionList != NULL) { + for (Index = 0; Index < Config->OptionCount; Index++) { + if (Config->OptionList[Index] != NULL) { + FreePool (Config->OptionList[Index]); + } + } + + FreePool (Config->OptionList); + } + + ZeroMem (Config, sizeof (EFI_DHCP4_CONFIG_DATA)); +} + + +/** + Allocate memory for configure parameter such as timeout value for Dst, + then copy the configure parameter from Src to Dst. + + @param[out] Dst The destination DHCP configure data. + @param[in] Src The source DHCP configure data. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_SUCCESS The configure is copied. + +**/ +EFI_STATUS +DhcpCopyConfigure ( + OUT EFI_DHCP4_CONFIG_DATA *Dst, + IN EFI_DHCP4_CONFIG_DATA *Src + ) +{ + EFI_DHCP4_PACKET_OPTION **DstOptions; + EFI_DHCP4_PACKET_OPTION **SrcOptions; + UINTN Len; + UINT32 Index; + + CopyMem (Dst, Src, sizeof (*Dst)); + Dst->DiscoverTimeout = NULL; + Dst->RequestTimeout = NULL; + Dst->OptionList = NULL; + + // + // Allocate a memory then copy DiscoverTimeout to it + // + if (Src->DiscoverTimeout != NULL) { + Len = Src->DiscoverTryCount * sizeof (UINT32); + Dst->DiscoverTimeout = AllocatePool (Len); + + if (Dst->DiscoverTimeout == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + for (Index = 0; Index < Src->DiscoverTryCount; Index++) { + Dst->DiscoverTimeout[Index] = MAX (Src->DiscoverTimeout[Index], 1); + } + } + + // + // Allocate a memory then copy RequestTimeout to it + // + if (Src->RequestTimeout != NULL) { + Len = Src->RequestTryCount * sizeof (UINT32); + Dst->RequestTimeout = AllocatePool (Len); + + if (Dst->RequestTimeout == NULL) { + goto ON_ERROR; + } + + for (Index = 0; Index < Src->RequestTryCount; Index++) { + Dst->RequestTimeout[Index] = MAX (Src->RequestTimeout[Index], 1); + } + } + + // + // Allocate an array of dhcp option point, then allocate memory + // for each option and copy the source option to it + // + if (Src->OptionList != NULL) { + Len = Src->OptionCount * sizeof (EFI_DHCP4_PACKET_OPTION *); + Dst->OptionList = AllocateZeroPool (Len); + + if (Dst->OptionList == NULL) { + goto ON_ERROR; + } + + DstOptions = Dst->OptionList; + SrcOptions = Src->OptionList; + + for (Index = 0; Index < Src->OptionCount; Index++) { + Len = sizeof (EFI_DHCP4_PACKET_OPTION) + MAX (SrcOptions[Index]->Length - 1, 0); + + DstOptions[Index] = AllocatePool (Len); + + if (DstOptions[Index] == NULL) { + goto ON_ERROR; + } + + CopyMem (DstOptions[Index], SrcOptions[Index], Len); + } + } + + return EFI_SUCCESS; + +ON_ERROR: + DhcpCleanConfigure (Dst); + return EFI_OUT_OF_RESOURCES; +} + + +/** + Give up the control of the DHCP service to let other child + resume. Don't change the service's DHCP state and the Client + address and option list configure as required by RFC2131. + + @param DhcpSb The DHCP service instance. + +**/ +VOID +DhcpYieldControl ( + IN DHCP_SERVICE *DhcpSb + ) +{ + EFI_DHCP4_CONFIG_DATA *Config; + + Config = &DhcpSb->ActiveConfig; + + DhcpSb->ServiceState = DHCP_UNCONFIGED; + DhcpSb->ActiveChild = NULL; + + if (Config->DiscoverTimeout != NULL) { + FreePool (Config->DiscoverTimeout); + + Config->DiscoverTryCount = 0; + Config->DiscoverTimeout = NULL; + } + + if (Config->RequestTimeout != NULL) { + FreePool (Config->RequestTimeout); + + Config->RequestTryCount = 0; + Config->RequestTimeout = NULL; + } + + Config->Dhcp4Callback = NULL; + Config->CallbackContext = NULL; +} + + +/** + Initializes, changes, or resets the operational settings for the EFI DHCPv4 Protocol driver. + + The Configure() function is used to initialize, change, or reset the operational + settings of the EFI DHCPv4 Protocol driver for the communication device on which + the EFI DHCPv4 Service Binding Protocol is installed. This function can be + successfully called only if both of the following are true: + * This instance of the EFI DHCPv4 Protocol driver is in the Dhcp4Stopped, Dhcp4Init, + Dhcp4InitReboot, or Dhcp4Bound states. + * No other EFI DHCPv4 Protocol driver instance that is controlled by this EFI + DHCPv4 Service Binding Protocol driver instance has configured this EFI DHCPv4 + Protocol driver. + When this driver is in the Dhcp4Stopped state, it can transfer into one of the + following two possible initial states: + * Dhcp4Init + * Dhcp4InitReboot + The driver can transfer into these states by calling Configure() with a non-NULL + Dhcp4CfgData. The driver will transfer into the appropriate state based on the + supplied client network address in the ClientAddress parameter and DHCP options + in the OptionList parameter as described in RFC 2131. + When Configure() is called successfully while Dhcp4CfgData is set to NULL, the + default configuring data will be reset in the EFI DHCPv4 Protocol driver and + the state of the EFI DHCPv4 Protocol driver will not be changed. If one instance + wants to make it possible for another instance to configure the EFI DHCPv4 Protocol + driver, it must call this function with Dhcp4CfgData set to NULL. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[in] Dhcp4CfgData Pointer to the EFI_DHCP4_CONFIG_DATA. + + @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init or + Dhcp4InitReboot state, if the original state of this driver + was Dhcp4Stopped and the value of Dhcp4CfgData was + not NULL. Otherwise, the state was left unchanged. + @retval EFI_ACCESS_DENIED This instance of the EFI DHCPv4 Protocol driver was not in the + Dhcp4Stopped, Dhcp4Init, Dhcp4InitReboot, or Dhcp4Bound state; + Or onother instance of this EFI DHCPv4 Protocol driver is already + in a valid configured state. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Configure ( + IN EFI_DHCP4_PROTOCOL *This, + IN EFI_DHCP4_CONFIG_DATA *Dhcp4CfgData OPTIONAL + ) +{ + EFI_DHCP4_CONFIG_DATA *Config; + DHCP_PROTOCOL *Instance; + DHCP_SERVICE *DhcpSb; + EFI_STATUS Status; + EFI_TPL OldTpl; + UINT32 Index; + IP4_ADDR Ip; + + // + // First validate the parameters + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (Dhcp4CfgData != NULL) { + if ((Dhcp4CfgData->DiscoverTryCount != 0) && (Dhcp4CfgData->DiscoverTimeout == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if ((Dhcp4CfgData->RequestTryCount != 0) && (Dhcp4CfgData->RequestTimeout == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if ((Dhcp4CfgData->OptionCount != 0) && (Dhcp4CfgData->OptionList == NULL)) { + return EFI_INVALID_PARAMETER; + } + + CopyMem (&Ip, &Dhcp4CfgData->ClientAddress, sizeof (IP4_ADDR)); + if (IP4_IS_LOCAL_BROADCAST(NTOHL (Ip))) { + return EFI_INVALID_PARAMETER; + } + } + + Instance = DHCP_INSTANCE_FROM_THIS (This); + + if (Instance->Signature != DHCP_PROTOCOL_SIGNATURE) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + DhcpSb = Instance->Service; + Config = &DhcpSb->ActiveConfig; + + Status = EFI_ACCESS_DENIED; + + if ((DhcpSb->DhcpState != Dhcp4Stopped) && + (DhcpSb->DhcpState != Dhcp4Init) && + (DhcpSb->DhcpState != Dhcp4InitReboot) && + (DhcpSb->DhcpState != Dhcp4Bound)) { + + goto ON_EXIT; + } + + if ((DhcpSb->ActiveChild != NULL) && (DhcpSb->ActiveChild != Instance)) { + goto ON_EXIT; + } + + if (Dhcp4CfgData != NULL) { + Status = EFI_OUT_OF_RESOURCES; + DhcpCleanConfigure (Config); + + if (EFI_ERROR (DhcpCopyConfigure (Config, Dhcp4CfgData))) { + goto ON_EXIT; + } + + DhcpSb->UserOptionLen = 0; + + for (Index = 0; Index < Dhcp4CfgData->OptionCount; Index++) { + DhcpSb->UserOptionLen += Dhcp4CfgData->OptionList[Index]->Length + 2; + } + + DhcpSb->ActiveChild = Instance; + + if (DhcpSb->DhcpState == Dhcp4Stopped) { + DhcpSb->ClientAddr = EFI_NTOHL (Dhcp4CfgData->ClientAddress); + + if (DhcpSb->ClientAddr != 0) { + DhcpSb->DhcpState = Dhcp4InitReboot; + } else { + DhcpSb->DhcpState = Dhcp4Init; + } + } + + DhcpSb->ServiceState = DHCP_CONFIGED; + Status = EFI_SUCCESS; + + } else if (DhcpSb->ActiveChild == Instance) { + Status = EFI_SUCCESS; + DhcpYieldControl (DhcpSb); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Starts the DHCP configuration process. + + The Start() function starts the DHCP configuration process. This function can + be called only when the EFI DHCPv4 Protocol driver is in the Dhcp4Init or + Dhcp4InitReboot state. + If the DHCP process completes successfully, the state of the EFI DHCPv4 Protocol + driver will be transferred through Dhcp4Selecting and Dhcp4Requesting to the + Dhcp4Bound state. The CompletionEvent will then be signaled if it is not NULL. + If the process aborts, either by the user or by some unexpected network error, + the state is restored to the Dhcp4Init state. The Start() function can be called + again to restart the process. + Refer to RFC 2131 for precise state transitions during this process. At the + time when each event occurs in this process, the callback function that was set + by EFI_DHCP4_PROTOCOL.Configure() will be called and the user can take this + opportunity to control the process. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[in] CompletionEvent If not NULL, indicates the event that will be signaled when the + EFI DHCPv4 Protocol driver is transferred into the + Dhcp4Bound state or when the DHCP process is aborted. + EFI_DHCP4_PROTOCOL.GetModeData() can be called to + check the completion status. If NULL, + EFI_DHCP4_PROTOCOL.Start() will wait until the driver + is transferred into the Dhcp4Bound state or the process fails. + + @retval EFI_SUCCESS The DHCP configuration process has started, or it has completed + when CompletionEvent is NULL. + @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped + state. EFI_DHCP4_PROTOCOL. Configure() needs to be called. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_TIMEOUT The DHCP configuration process failed because no response was + received from the server within the specified timeout value. + @retval EFI_ABORTED The user aborted the DHCP process. + @retval EFI_ALREADY_STARTED Some other EFI DHCPv4 Protocol instance already started the + DHCP process. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_NO_MEDIA There was a media error. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Start ( + IN EFI_DHCP4_PROTOCOL *This, + IN EFI_EVENT CompletionEvent OPTIONAL + ) +{ + DHCP_PROTOCOL *Instance; + DHCP_SERVICE *DhcpSb; + EFI_STATUS Status; + EFI_TPL OldTpl; + EFI_STATUS MediaStatus; + + // + // First validate the parameters + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP_INSTANCE_FROM_THIS (This); + + if (Instance->Signature != DHCP_PROTOCOL_SIGNATURE) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + DhcpSb = Instance->Service; + + if (DhcpSb->DhcpState == Dhcp4Stopped) { + Status = EFI_NOT_STARTED; + goto ON_ERROR; + } + + if ((DhcpSb->DhcpState != Dhcp4Init) && (DhcpSb->DhcpState != Dhcp4InitReboot)) { + Status = EFI_ALREADY_STARTED; + goto ON_ERROR; + } + + // + // Check Media Satus. + // + MediaStatus = EFI_SUCCESS; + NetLibDetectMediaWaitTimeout (DhcpSb->Controller, DHCP_CHECK_MEDIA_WAITING_TIME, &MediaStatus); + if (MediaStatus != EFI_SUCCESS) { + Status = EFI_NO_MEDIA; + goto ON_ERROR; + } + + DhcpSb->IoStatus = EFI_ALREADY_STARTED; + + if (EFI_ERROR (Status = DhcpInitRequest (DhcpSb))) { + goto ON_ERROR; + } + + + Instance->CompletionEvent = CompletionEvent; + + // + // Restore the TPL now, don't call poll function at TPL_CALLBACK. + // + gBS->RestoreTPL (OldTpl); + + if (CompletionEvent == NULL) { + while (DhcpSb->IoStatus == EFI_ALREADY_STARTED) { + DhcpSb->UdpIo->Protocol.Udp4->Poll (DhcpSb->UdpIo->Protocol.Udp4); + } + + return DhcpSb->IoStatus; + } + + return EFI_SUCCESS; + +ON_ERROR: + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Extends the lease time by sending a request packet. + + The RenewRebind() function is used to manually extend the lease time when the + EFI DHCPv4 Protocol driver is in the Dhcp4Bound state and the lease time has + not expired yet. This function will send a request packet to the previously + found server (or to any server when RebindRequest is TRUE) and transfer the + state into the Dhcp4Renewing state (or Dhcp4Rebinding when RebindingRequest is + TRUE). When a response is received, the state is returned to Dhcp4Bound. + If no response is received before the try count is exceeded (the RequestTryCount + field that is specified in EFI_DHCP4_CONFIG_DATA) but before the lease time that + was issued by the previous server expires, the driver will return to the Dhcp4Bound + state and the previous configuration is restored. The outgoing and incoming packets + can be captured by the EFI_DHCP4_CALLBACK function. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[in] RebindRequest If TRUE, this function broadcasts the request packets and enters + the Dhcp4Rebinding state. Otherwise, it sends a unicast + request packet and enters the Dhcp4Renewing state. + @param[in] CompletionEvent If not NULL, this event is signaled when the renew/rebind phase + completes or some error occurs. + EFI_DHCP4_PROTOCOL.GetModeData() can be called to + check the completion status. If NULL, + EFI_DHCP4_PROTOCOL.RenewRebind() will busy-wait + until the DHCP process finishes. + + @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the + Dhcp4Renewing state or is back to the Dhcp4Bound state. + @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped + state. EFI_DHCP4_PROTOCOL.Configure() needs to + be called. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_TIMEOUT There was no response from the server when the try count was + exceeded. + @retval EFI_ACCESS_DENIED The driver is not in the Dhcp4Bound state. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4RenewRebind ( + IN EFI_DHCP4_PROTOCOL *This, + IN BOOLEAN RebindRequest, + IN EFI_EVENT CompletionEvent OPTIONAL + ) +{ + DHCP_PROTOCOL *Instance; + DHCP_SERVICE *DhcpSb; + EFI_STATUS Status; + EFI_TPL OldTpl; + + // + // First validate the parameters + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP_INSTANCE_FROM_THIS (This); + + if (Instance->Signature != DHCP_PROTOCOL_SIGNATURE) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + DhcpSb = Instance->Service; + + if (DhcpSb->DhcpState == Dhcp4Stopped) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + if (DhcpSb->DhcpState != Dhcp4Bound) { + Status = EFI_ACCESS_DENIED; + goto ON_EXIT; + } + + if (DHCP_IS_BOOTP (DhcpSb->Para)) { + Status = EFI_SUCCESS; + goto ON_EXIT; + } + + // + // Transit the states then send a extra DHCP request + // + if (!RebindRequest) { + DhcpSetState (DhcpSb, Dhcp4Renewing, FALSE); + } else { + DhcpSetState (DhcpSb, Dhcp4Rebinding, FALSE); + } + + // + // Clear initial time to make sure that elapsed-time + // is set to 0 for first REQUEST in renewal process. + // + Instance->ElaspedTime = 0; + + Status = DhcpSendMessage ( + DhcpSb, + DhcpSb->Selected, + DhcpSb->Para, + DHCP_MSG_REQUEST, + (UINT8 *) "Extra renew/rebind by the application" + ); + + if (EFI_ERROR (Status)) { + DhcpSetState (DhcpSb, Dhcp4Bound, FALSE); + goto ON_EXIT; + } + + DhcpSb->ExtraRefresh = TRUE; + DhcpSb->IoStatus = EFI_ALREADY_STARTED; + Instance->RenewRebindEvent = CompletionEvent; + + gBS->RestoreTPL (OldTpl); + + if (CompletionEvent == NULL) { + while (DhcpSb->IoStatus == EFI_ALREADY_STARTED) { + DhcpSb->UdpIo->Protocol.Udp4->Poll (DhcpSb->UdpIo->Protocol.Udp4); + + } + + return DhcpSb->IoStatus; + } + + return EFI_SUCCESS; + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Releases the current address configuration. + + The Release() function releases the current configured IP address by doing either + of the following: + * Sending a DHCPRELEASE packet when the EFI DHCPv4 Protocol driver is in the + Dhcp4Bound state + * Setting the previously assigned IP address that was provided with the + EFI_DHCP4_PROTOCOL.Configure() function to 0.0.0.0 when the driver is in + Dhcp4InitReboot state + After a successful call to this function, the EFI DHCPv4 Protocol driver returns + to the Dhcp4Init state and any subsequent incoming packets will be discarded silently. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + + @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init phase. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_ACCESS_DENIED The EFI DHCPv4 Protocol driver is not Dhcp4InitReboot state. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Release ( + IN EFI_DHCP4_PROTOCOL *This + ) +{ + DHCP_PROTOCOL *Instance; + DHCP_SERVICE *DhcpSb; + EFI_STATUS Status; + EFI_TPL OldTpl; + + // + // First validate the parameters + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP_INSTANCE_FROM_THIS (This); + + if (Instance->Signature != DHCP_PROTOCOL_SIGNATURE) { + return EFI_INVALID_PARAMETER; + } + + Status = EFI_SUCCESS; + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + DhcpSb = Instance->Service; + + if ((DhcpSb->DhcpState != Dhcp4InitReboot) && (DhcpSb->DhcpState != Dhcp4Bound)) { + Status = EFI_ACCESS_DENIED; + goto ON_EXIT; + } + + if (!DHCP_IS_BOOTP (DhcpSb->Para) && (DhcpSb->DhcpState == Dhcp4Bound)) { + Status = DhcpSendMessage ( + DhcpSb, + DhcpSb->Selected, + DhcpSb->Para, + DHCP_MSG_RELEASE, + NULL + ); + + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + } + + DhcpCleanLease (DhcpSb); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Stops the current address configuration. + + The Stop() function is used to stop the DHCP configuration process. After this + function is called successfully, the EFI DHCPv4 Protocol driver is transferred + into the Dhcp4Stopped state. EFI_DHCP4_PROTOCOL.Configure() needs to be called + before DHCP configuration process can be started again. This function can be + called when the EFI DHCPv4 Protocol driver is in any state. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + + @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Stopped phase. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Stop ( + IN EFI_DHCP4_PROTOCOL *This + ) +{ + DHCP_PROTOCOL *Instance; + DHCP_SERVICE *DhcpSb; + EFI_TPL OldTpl; + + // + // First validate the parameters + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP_INSTANCE_FROM_THIS (This); + + if (Instance->Signature != DHCP_PROTOCOL_SIGNATURE) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + DhcpSb = Instance->Service; + + DhcpCleanLease (DhcpSb); + + DhcpSb->DhcpState = Dhcp4Stopped; + DhcpSb->ServiceState = DHCP_UNCONFIGED; + + gBS->RestoreTPL (OldTpl); + return EFI_SUCCESS; +} + + +/** + Builds a DHCP packet, given the options to be appended or deleted or replaced. + + The Build() function is used to assemble a new packet from the original packet + by replacing or deleting existing options or appending new options. This function + does not change any state of the EFI DHCPv4 Protocol driver and can be used at + any time. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[in] SeedPacket Initial packet to be used as a base for building new packet. + @param[in] DeleteCount Number of opcodes in the DeleteList. + @param[in] DeleteList List of opcodes to be deleted from the seed packet. + Ignored if DeleteCount is zero. + @param[in] AppendCount Number of entries in the OptionList. + @param[in] AppendList Pointer to a DHCP option list to be appended to SeedPacket. + If SeedPacket also contains options in this list, they are + replaced by new options (except pad option). Ignored if + AppendCount is zero. Type EFI_DHCP4_PACKET_OPTION + @param[out] NewPacket Pointer to storage for the pointer to the new allocated packet. + Use the EFI Boot Service FreePool() on the resulting pointer + when done with the packet. + + @retval EFI_SUCCESS The new packet was built. + @retval EFI_OUT_OF_RESOURCES Storage for the new packet could not be allocated. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Build ( + IN EFI_DHCP4_PROTOCOL *This, + IN EFI_DHCP4_PACKET *SeedPacket, + IN UINT32 DeleteCount, + IN UINT8 *DeleteList OPTIONAL, + IN UINT32 AppendCount, + IN EFI_DHCP4_PACKET_OPTION *AppendList[] OPTIONAL, + OUT EFI_DHCP4_PACKET **NewPacket + ) +{ + // + // First validate the parameters + // + if ((This == NULL) || (NewPacket == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if ((SeedPacket == NULL) || (SeedPacket->Dhcp4.Magik != DHCP_OPTION_MAGIC) || + EFI_ERROR (DhcpValidateOptions (SeedPacket, NULL))) { + + return EFI_INVALID_PARAMETER; + } + + if (((DeleteCount == 0) && (AppendCount == 0)) || + ((DeleteCount != 0) && (DeleteList == NULL)) || + ((AppendCount != 0) && (AppendList == NULL))) { + + return EFI_INVALID_PARAMETER; + } + + return DhcpBuild ( + SeedPacket, + DeleteCount, + DeleteList, + AppendCount, + AppendList, + NewPacket + ); +} + +/** + Callback by UdpIoCreatePort() when creating UdpIo for this Dhcp4 instance. + + @param[in] UdpIo The UdpIo being created. + @param[in] Context Dhcp4 instance. + + @retval EFI_SUCCESS UdpIo is configured successfully. + @retval EFI_INVALID_PARAMETER Class E IP address is not supported or other parameters + are not valid. + @retval other Other error occurs. +**/ +EFI_STATUS +EFIAPI +Dhcp4InstanceConfigUdpIo ( + IN UDP_IO *UdpIo, + IN VOID *Context + ) +{ + DHCP_PROTOCOL *Instance; + DHCP_SERVICE *DhcpSb; + EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN *Token; + EFI_UDP4_CONFIG_DATA UdpConfigData; + IP4_ADDR ClientAddr; + IP4_ADDR Ip; + INTN Class; + IP4_ADDR SubnetMask; + + Instance = (DHCP_PROTOCOL *) Context; + DhcpSb = Instance->Service; + Token = Instance->Token; + + ZeroMem (&UdpConfigData, sizeof (EFI_UDP4_CONFIG_DATA)); + + UdpConfigData.AcceptBroadcast = TRUE; + UdpConfigData.AllowDuplicatePort = TRUE; + UdpConfigData.TimeToLive = 64; + UdpConfigData.DoNotFragment = TRUE; + + ClientAddr = EFI_NTOHL (Token->Packet->Dhcp4.Header.ClientAddr); + Ip = HTONL (ClientAddr); + CopyMem (&UdpConfigData.StationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS)); + + if (DhcpSb->Netmask == 0) { + // + // The Dhcp4.TransmitReceive() API should be able to used at any time according to + // UEFI spec, while in classless addressing network, the netmask must be explicitly + // provided together with the station address. + // If the DHCP instance haven't be configured with a valid netmask, we could only + // compute it according to the classful addressing rule. + // + Class = NetGetIpClass (ClientAddr); + // + // Class E IP address is not supported here! + // + ASSERT (Class < IP4_ADDR_CLASSE); + if (Class >= IP4_ADDR_CLASSE) { + return EFI_INVALID_PARAMETER; + } + + SubnetMask = gIp4AllMasks[Class << 3]; + } else { + SubnetMask = DhcpSb->Netmask; + } + + Ip = HTONL (SubnetMask); + CopyMem (&UdpConfigData.SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS)); + + if ((Token->ListenPointCount == 0) || (Token->ListenPoints[0].ListenPort == 0)) { + UdpConfigData.StationPort = DHCP_CLIENT_PORT; + } else { + UdpConfigData.StationPort = Token->ListenPoints[0].ListenPort; + } + + return UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfigData); +} + +/** + Create UdpIo for this Dhcp4 instance. + + @param Instance The Dhcp4 instance. + + @retval EFI_SUCCESS UdpIo is created successfully. + @retval EFI_OUT_OF_RESOURCES Fails to create UdpIo because of limited + resources or configuration failure. +**/ +EFI_STATUS +Dhcp4InstanceCreateUdpIo ( + IN OUT DHCP_PROTOCOL *Instance + ) +{ + DHCP_SERVICE *DhcpSb; + EFI_STATUS Status; + VOID *Udp4; + + ASSERT (Instance->Token != NULL); + + DhcpSb = Instance->Service; + Instance->UdpIo = UdpIoCreateIo ( + DhcpSb->Controller, + DhcpSb->Image, + Dhcp4InstanceConfigUdpIo, + UDP_IO_UDP4_VERSION, + Instance + ); + if (Instance->UdpIo == NULL) { + return EFI_OUT_OF_RESOURCES; + } else { + Status = gBS->OpenProtocol ( + Instance->UdpIo->UdpHandle, + &gEfiUdp4ProtocolGuid, + (VOID **) &Udp4, + Instance->Service->Image, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + UdpIoFreeIo (Instance->UdpIo); + Instance->UdpIo = NULL; + } + return Status; + } +} + +/** + Callback of Dhcp packet. Does nothing. + + @param Arg The context. + +**/ +VOID +EFIAPI +DhcpDummyExtFree ( + IN VOID *Arg + ) +{ +} + +/** + Callback of UdpIoRecvDatagram() that handles a Dhcp4 packet. + + Only BOOTP responses will be handled that correspond to the Xid of the request + sent out. The packet will be queued to the response queue. + + @param UdpPacket The Dhcp4 packet. + @param EndPoint Udp4 address pair. + @param IoStatus Status of the input. + @param Context Extra info for the input. + +**/ +VOID +EFIAPI +PxeDhcpInput ( + NET_BUF *UdpPacket, + UDP_END_POINT *EndPoint, + EFI_STATUS IoStatus, + VOID *Context + ) +{ + DHCP_PROTOCOL *Instance; + EFI_DHCP4_HEADER *Head; + NET_BUF *Wrap; + EFI_DHCP4_PACKET *Packet; + EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN *Token; + UINT32 Len; + EFI_STATUS Status; + + Wrap = NULL; + Instance = (DHCP_PROTOCOL *) Context; + Token = Instance->Token; + + // + // Don't restart receive if error occurs or DHCP is destroyed. + // + if (EFI_ERROR (IoStatus)) { + return ; + } + + ASSERT (UdpPacket != NULL); + + // + // Validate the packet received + // + if (UdpPacket->TotalSize < sizeof (EFI_DHCP4_HEADER)) { + goto RESTART; + } + + // + // Copy the DHCP message to a continuous memory block, make the buffer size + // of the EFI_DHCP4_PACKET a multiple of 4-byte. + // + Len = NET_ROUNDUP (sizeof (EFI_DHCP4_PACKET) + UdpPacket->TotalSize - sizeof (EFI_DHCP4_HEADER), 4); + Wrap = NetbufAlloc (Len); + if (Wrap == NULL) { + goto RESTART; + } + + Packet = (EFI_DHCP4_PACKET *) NetbufAllocSpace (Wrap, Len, NET_BUF_TAIL); + ASSERT (Packet != NULL); + + Packet->Size = Len; + Head = &Packet->Dhcp4.Header; + Packet->Length = NetbufCopy (UdpPacket, 0, UdpPacket->TotalSize, (UINT8 *) Head); + + if (Packet->Length != UdpPacket->TotalSize) { + goto RESTART; + } + + // + // Is this packet the answer to our packet? + // + if ((Head->OpCode != BOOTP_REPLY) || + (Head->Xid != Token->Packet->Dhcp4.Header.Xid) || + (CompareMem (&Token->Packet->Dhcp4.Header.ClientHwAddr[0], Head->ClientHwAddr, Head->HwAddrLen) != 0)) { + goto RESTART; + } + + // + // Validate the options and retrieve the interested options + // + if ((Packet->Length > sizeof (EFI_DHCP4_HEADER) + sizeof (UINT32)) && + (Packet->Dhcp4.Magik == DHCP_OPTION_MAGIC) && + EFI_ERROR (DhcpValidateOptions (Packet, NULL))) { + + goto RESTART; + } + + // + // Keep this packet in the ResponseQueue. + // + NET_GET_REF (Wrap); + NetbufQueAppend (&Instance->ResponseQueue, Wrap); + +RESTART: + + NetbufFree (UdpPacket); + + if (Wrap != NULL) { + NetbufFree (Wrap); + } + + Status = UdpIoRecvDatagram (Instance->UdpIo, PxeDhcpInput, Instance, 0); + if (EFI_ERROR (Status)) { + PxeDhcpDone (Instance); + } +} + +/** + Complete a Dhcp4 transaction and signal the upper layer. + + @param Instance Dhcp4 instance. + +**/ +VOID +PxeDhcpDone ( + IN DHCP_PROTOCOL *Instance + ) +{ + EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN *Token; + + Token = Instance->Token; + + Token->ResponseCount = Instance->ResponseQueue.BufNum; + if (Token->ResponseCount != 0) { + Token->ResponseList = (EFI_DHCP4_PACKET *) AllocatePool (Instance->ResponseQueue.BufSize); + if (Token->ResponseList == NULL) { + Token->Status = EFI_OUT_OF_RESOURCES; + goto SIGNAL_USER; + } + + // + // Copy the received DHCP responses. + // + NetbufQueCopy (&Instance->ResponseQueue, 0, Instance->ResponseQueue.BufSize, (UINT8 *) Token->ResponseList); + Token->Status = EFI_SUCCESS; + } else { + Token->ResponseList = NULL; + Token->Status = EFI_TIMEOUT; + } + +SIGNAL_USER: + // + // Clean up the resources dedicated for this transmit receive transaction. + // + NetbufQueFlush (&Instance->ResponseQueue); + UdpIoCleanIo (Instance->UdpIo); + gBS->CloseProtocol ( + Instance->UdpIo->UdpHandle, + &gEfiUdp4ProtocolGuid, + Instance->Service->Image, + Instance->Handle + ); + UdpIoFreeIo (Instance->UdpIo); + Instance->UdpIo = NULL; + Instance->Token = NULL; + + if (Token->CompletionEvent != NULL) { + gBS->SignalEvent (Token->CompletionEvent); + } +} + + +/** + Transmits a DHCP formatted packet and optionally waits for responses. + + The TransmitReceive() function is used to transmit a DHCP packet and optionally + wait for the response from servers. This function does not change the state of + the EFI DHCPv4 Protocol driver and thus can be used at any time. + + @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param[in] Token Pointer to the EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN structure. + + @retval EFI_SUCCESS The packet was successfully queued for transmission. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_NOT_READY The previous call to this function has not finished yet. Try to call + this function after collection process completes. + @retval EFI_NO_MAPPING The default station address is not available yet. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval Others Some other unexpected error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4TransmitReceive ( + IN EFI_DHCP4_PROTOCOL *This, + IN EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN *Token + ) +{ + DHCP_PROTOCOL *Instance; + EFI_TPL OldTpl; + EFI_STATUS Status; + NET_FRAGMENT Frag; + NET_BUF *Wrap; + UDP_END_POINT EndPoint; + IP4_ADDR Ip; + DHCP_SERVICE *DhcpSb; + EFI_IP_ADDRESS Gateway; + IP4_ADDR ClientAddr; + + if ((This == NULL) || (Token == NULL) || (Token->Packet == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP_INSTANCE_FROM_THIS (This); + DhcpSb = Instance->Service; + + if (Instance->Token != NULL) { + // + // The previous call to TransmitReceive is not finished. + // + return EFI_NOT_READY; + } + + if ((Token->Packet->Dhcp4.Magik != DHCP_OPTION_MAGIC) || + (NTOHL (Token->Packet->Dhcp4.Header.Xid) == Instance->Service->Xid) || + (Token->TimeoutValue == 0) || + ((Token->ListenPointCount != 0) && (Token->ListenPoints == NULL)) || + EFI_ERROR (DhcpValidateOptions (Token->Packet, NULL)) || + EFI_IP4_EQUAL (&Token->RemoteAddress, &mZeroIp4Addr) + ) { + // + // The DHCP packet isn't well-formed, the Transaction ID is already used, + // the timeout value is zero, the ListenPoint is invalid, or the + // RemoteAddress is zero. + // + return EFI_INVALID_PARAMETER; + } + + ClientAddr = EFI_NTOHL (Token->Packet->Dhcp4.Header.ClientAddr); + + if (ClientAddr == 0) { + return EFI_NO_MAPPING; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Save the token and the timeout value. + // + Instance->Token = Token; + Instance->Timeout = Token->TimeoutValue; + + // + // Create a UDP IO for this transmit receive transaction. + // + Status = Dhcp4InstanceCreateUdpIo (Instance); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Save the Client Address is sent out + // + CopyMem ( + &DhcpSb->ClientAddressSendOut[0], + &Token->Packet->Dhcp4.Header.ClientHwAddr[0], + Token->Packet->Dhcp4.Header.HwAddrLen + ); + + // + // Wrap the DHCP packet into a net buffer. + // + Frag.Bulk = (UINT8 *) &Token->Packet->Dhcp4; + Frag.Len = Token->Packet->Length; + Wrap = NetbufFromExt (&Frag, 1, 0, 0, DhcpDummyExtFree, NULL); + if (Wrap == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + // + // Set the local address and local port to ZERO. + // + ZeroMem (&EndPoint, sizeof (UDP_END_POINT)); + + // + // Set the destination address and destination port. + // + CopyMem (&Ip, &Token->RemoteAddress, sizeof (EFI_IPv4_ADDRESS)); + EndPoint.RemoteAddr.Addr[0] = NTOHL (Ip); + + if (Token->RemotePort == 0) { + EndPoint.RemotePort = DHCP_SERVER_PORT; + } else { + EndPoint.RemotePort = Token->RemotePort; + } + + // + // Get the gateway. + // + ZeroMem (&Gateway, sizeof (Gateway)); + if (!IP4_NET_EQUAL (ClientAddr, EndPoint.RemoteAddr.Addr[0], DhcpSb->Netmask)) { + CopyMem (&Gateway.v4, &Token->GatewayAddress, sizeof (EFI_IPv4_ADDRESS)); + Gateway.Addr[0] = NTOHL (Gateway.Addr[0]); + } + + // + // Transmit the DHCP packet. + // + Status = UdpIoSendDatagram (Instance->UdpIo, Wrap, &EndPoint, &Gateway, DhcpOnPacketSent, NULL); + if (EFI_ERROR (Status)) { + NetbufFree (Wrap); + goto ON_ERROR; + } + + // + // Start to receive the DHCP response. + // + Status = UdpIoRecvDatagram (Instance->UdpIo, PxeDhcpInput, Instance, 0); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + +ON_ERROR: + + if (EFI_ERROR (Status) && (Instance->UdpIo != NULL)) { + UdpIoCleanIo (Instance->UdpIo); + gBS->CloseProtocol ( + Instance->UdpIo->UdpHandle, + &gEfiUdp4ProtocolGuid, + Instance->Service->Image, + Instance->Handle + ); + UdpIoFreeIo (Instance->UdpIo); + Instance->UdpIo = NULL; + Instance->Token = NULL; + } + + gBS->RestoreTPL (OldTpl); + + if (!EFI_ERROR (Status) && (Token->CompletionEvent == NULL)) { + // + // Keep polling until timeout if no error happens and the CompletionEvent + // is NULL. + // + while (TRUE) { + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + // + // Raise TPL to protect the UDPIO in instance, in case that DhcpOnTimerTick + // free it when timeout. + // + if (Instance->Timeout > 0) { + Instance->UdpIo->Protocol.Udp4->Poll (Instance->UdpIo->Protocol.Udp4); + gBS->RestoreTPL (OldTpl); + } else { + gBS->RestoreTPL (OldTpl); + break; + } + } + } + + return Status; +} + + +/** + Callback function for DhcpIterateOptions. This callback sets the + EFI_DHCP4_PACKET_OPTION array in the DHCP_PARSE_CONTEXT to point + the individual DHCP option in the packet. + + @param[in] Tag The DHCP option type + @param[in] Len Length of the DHCP option data + @param[in] Data The DHCP option data + @param[in] Context The context, to pass several parameters in. + + @retval EFI_SUCCESS It always returns EFI_SUCCESS + +**/ +EFI_STATUS +Dhcp4ParseCheckOption ( + IN UINT8 Tag, + IN UINT8 Len, + IN UINT8 *Data, + IN VOID *Context + ) +{ + DHCP_PARSE_CONTEXT *Parse; + + Parse = (DHCP_PARSE_CONTEXT *) Context; + Parse->Index++; + + if (Parse->Index <= Parse->OptionCount) { + // + // Use BASE_CR to get the memory position of EFI_DHCP4_PACKET_OPTION for + // the EFI_DHCP4_PACKET_OPTION->Data because DhcpIterateOptions only + // pass in the point to option data. + // + Parse->Option[Parse->Index - 1] = BASE_CR (Data, EFI_DHCP4_PACKET_OPTION, Data); + } + + return EFI_SUCCESS; +} + + +/** + Parses the packed DHCP option data. + + The Parse() function is used to retrieve the option list from a DHCP packet. + If *OptionCount isn't zero, and there is enough space for all the DHCP options + in the Packet, each element of PacketOptionList is set to point to somewhere in + the Packet->Dhcp4.Option where a new DHCP option begins. If RFC3396 is supported, + the caller should reassemble the parsed DHCP options to get the finial result. + If *OptionCount is zero or there isn't enough space for all of them, the number + of DHCP options in the Packet is returned in OptionCount. + + @param This Pointer to the EFI_DHCP4_PROTOCOL instance. + @param Packet Pointer to packet to be parsed. + @param OptionCount On input, the number of entries in the PacketOptionList. + On output, the number of entries that were written into the + PacketOptionList. + @param PacketOptionList List of packet option entries to be filled in. End option or pad + options are not included. + + @retval EFI_SUCCESS The packet was successfully parsed. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_BUFFER_TOO_SMALL One or more of the following conditions is TRUE: + 1) *OptionCount is smaller than the number of options that + were found in the Packet. + 2) PacketOptionList is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp4Parse ( + IN EFI_DHCP4_PROTOCOL *This, + IN EFI_DHCP4_PACKET *Packet, + IN OUT UINT32 *OptionCount, + OUT EFI_DHCP4_PACKET_OPTION *PacketOptionList[] OPTIONAL + ) +{ + DHCP_PARSE_CONTEXT Context; + EFI_STATUS Status; + + // + // First validate the parameters + // + if ((This == NULL) || (Packet == NULL) || (OptionCount == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if ((Packet->Size < Packet->Length + 2 * sizeof (UINT32)) || + (Packet->Dhcp4.Magik != DHCP_OPTION_MAGIC) || + EFI_ERROR (DhcpValidateOptions (Packet, NULL))) { + + return EFI_INVALID_PARAMETER; + } + + if ((*OptionCount != 0) && (PacketOptionList == NULL)) { + return EFI_BUFFER_TOO_SMALL; + } + + ZeroMem (PacketOptionList, *OptionCount * sizeof (EFI_DHCP4_PACKET_OPTION *)); + + Context.Option = PacketOptionList; + Context.OptionCount = *OptionCount; + Context.Index = 0; + + Status = DhcpIterateOptions (Packet, Dhcp4ParseCheckOption, &Context); + + if (EFI_ERROR (Status)) { + return Status; + } + + *OptionCount = Context.Index; + + if (Context.Index > Context.OptionCount) { + return EFI_BUFFER_TOO_SMALL; + } + + return EFI_SUCCESS; +} + +/** + Set the elapsed time based on the given instance and the pointer to the + elapsed time option. + + @param[in] Elapsed The pointer to the position to append. + @param[in] Instance The pointer to the Dhcp4 instance. +**/ +VOID +SetElapsedTime ( + IN UINT16 *Elapsed, + IN DHCP_PROTOCOL *Instance + ) +{ + WriteUnaligned16 (Elapsed, HTONS(Instance->ElaspedTime)); +} diff --git a/NetworkPkg/Dhcp4Dxe/Dhcp4Impl.h b/NetworkPkg/Dhcp4Dxe/Dhcp4Impl.h new file mode 100644 index 000000000..8c2cd3dae --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/Dhcp4Impl.h @@ -0,0 +1,206 @@ +/** @file + EFI DHCP protocol implementation. + RFCs supported are: + RFC 2131: Dynamic Host Configuration Protocol + RFC 2132: DHCP Options and BOOTP Vendor Extensions + RFC 1534: Interoperation Between DHCP and BOOTP + RFC 3396: Encoding Long Options in DHCP. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_DHCP4_IMPL_H__ +#define __EFI_DHCP4_IMPL_H__ + + + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct _DHCP_SERVICE DHCP_SERVICE; +typedef struct _DHCP_PROTOCOL DHCP_PROTOCOL; + +#include "Dhcp4Option.h" +#include "Dhcp4Io.h" + +#define DHCP_SERVICE_SIGNATURE SIGNATURE_32 ('D', 'H', 'C', 'P') +#define DHCP_PROTOCOL_SIGNATURE SIGNATURE_32 ('d', 'h', 'c', 'p') + +#define DHCP_CHECK_MEDIA_WAITING_TIME EFI_TIMER_PERIOD_SECONDS(20) + +// +// The state of the DHCP service. It starts as UNCONFIGED. If +// and active child configures the service successfully, it +// goes to CONFIGED. If the active child configures NULL, it +// goes back to UNCONFIGED. It becomes DESTROY if it is (partly) +// destroyed. +// +#define DHCP_UNCONFIGED 0 +#define DHCP_CONFIGED 1 +#define DHCP_DESTROY 2 + + +struct _DHCP_PROTOCOL { + UINT32 Signature; + EFI_DHCP4_PROTOCOL Dhcp4Protocol; + LIST_ENTRY Link; + EFI_HANDLE Handle; + DHCP_SERVICE *Service; + + BOOLEAN InDestroy; + + EFI_EVENT CompletionEvent; + EFI_EVENT RenewRebindEvent; + + EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN *Token; + UDP_IO *UdpIo; // The UDP IO used for TransmitReceive. + UINT32 Timeout; + UINT16 ElaspedTime; + NET_BUF_QUEUE ResponseQueue; +}; + +// +// DHCP driver is specical in that it is a singleton. Although it +// has a service binding, there can be only one active child. +// +struct _DHCP_SERVICE { + UINT32 Signature; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + + INTN ServiceState; // CONFIGED, UNCONFIGED, and DESTROY + + EFI_HANDLE Controller; + EFI_HANDLE Image; + + LIST_ENTRY Children; + UINTN NumChildren; + + INTN DhcpState; + EFI_STATUS IoStatus; // the result of last user operation + UINT32 Xid; + + IP4_ADDR ClientAddr; // lease IP or configured client address + IP4_ADDR Netmask; + IP4_ADDR ServerAddr; + + EFI_DHCP4_PACKET *LastOffer; // The last received offer + EFI_DHCP4_PACKET *Selected; + DHCP_PARAMETER *Para; + + UINT32 Lease; + UINT32 T1; + UINT32 T2; + INTN ExtraRefresh; // This refresh is reqested by user + + UDP_IO *UdpIo; // Udp child receiving all DHCP message + UDP_IO *LeaseIoPort; // Udp child with lease IP + EFI_DHCP4_PACKET *LastPacket; // The last sent packet for retransmission + EFI_MAC_ADDRESS Mac; + UINT8 HwType; + UINT8 HwLen; + UINT8 ClientAddressSendOut[16]; + + DHCP_PROTOCOL *ActiveChild; + EFI_DHCP4_CONFIG_DATA ActiveConfig; + UINT32 UserOptionLen; + + // + // Timer event and various timer + // + EFI_EVENT Timer; + + UINT32 PacketToLive; // Retransmission timer for our packets + UINT32 LastTimeout; // Record the init value of PacketToLive every time + INTN CurRetry; + INTN MaxRetries; + UINT32 LeaseLife; +}; + +typedef struct { + EFI_DHCP4_PACKET_OPTION **Option; + UINT32 OptionCount; + UINT32 Index; +} DHCP_PARSE_CONTEXT; + +#define DHCP_INSTANCE_FROM_THIS(Proto) \ + CR ((Proto), DHCP_PROTOCOL, Dhcp4Protocol, DHCP_PROTOCOL_SIGNATURE) + +#define DHCP_SERVICE_FROM_THIS(Sb) \ + CR ((Sb), DHCP_SERVICE, ServiceBinding, DHCP_SERVICE_SIGNATURE) + +extern EFI_DHCP4_PROTOCOL mDhcp4ProtocolTemplate; + +/** + Give up the control of the DHCP service to let other child + resume. Don't change the service's DHCP state and the Client + address and option list configure as required by RFC2131. + + @param DhcpSb The DHCP service instance. + +**/ +VOID +DhcpYieldControl ( + IN DHCP_SERVICE *DhcpSb + ); + +/** + Complete a Dhcp4 transaction and signal the upper layer. + + @param Instance Dhcp4 instance. + +**/ +VOID +PxeDhcpDone ( + IN DHCP_PROTOCOL *Instance + ); + +/** + Free the resource related to the configure parameters. + DHCP driver will make a copy of the user's configure + such as the time out value. + + @param Config The DHCP configure data + +**/ +VOID +DhcpCleanConfigure ( + IN OUT EFI_DHCP4_CONFIG_DATA *Config + ); + +/** + Callback of Dhcp packet. Does nothing. + + @param Arg The context. + +**/ +VOID +EFIAPI +DhcpDummyExtFree ( + IN VOID *Arg + ); + +/** + Set the elapsed time based on the given instance and the pointer to the + elapsed time option. + + @param[in] Elapsed The pointer to the position to append. + @param[in] Instance The pointer to the Dhcp4 instance. +**/ +VOID +SetElapsedTime ( + IN UINT16 *Elapsed, + IN DHCP_PROTOCOL *Instance + ); + +#endif diff --git a/NetworkPkg/Dhcp4Dxe/Dhcp4Io.c b/NetworkPkg/Dhcp4Dxe/Dhcp4Io.c new file mode 100644 index 000000000..4728b94c5 --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/Dhcp4Io.c @@ -0,0 +1,1657 @@ +/** @file + EFI DHCP protocol implementation. + +Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Dhcp4Impl.h" + +UINT32 mDhcp4DefaultTimeout[4] = { 4, 8, 16, 32 }; + + +/** + Send an initial DISCOVER or REQUEST message according to the + DHCP service's current state. + + @param[in] DhcpSb The DHCP service instance + + @retval EFI_SUCCESS The request has been sent + @retval other Some error occurs when sending the request. + +**/ +EFI_STATUS +DhcpInitRequest ( + IN DHCP_SERVICE *DhcpSb + ) +{ + EFI_STATUS Status; + + ASSERT ((DhcpSb->DhcpState == Dhcp4Init) || (DhcpSb->DhcpState == Dhcp4InitReboot)); + + // + // Clear initial time to make sure that elapsed-time is set to 0 for first Discover or REQUEST message. + // + DhcpSb->ActiveChild->ElaspedTime= 0; + + if (DhcpSb->DhcpState == Dhcp4Init) { + DhcpSetState (DhcpSb, Dhcp4Selecting, FALSE); + Status = DhcpSendMessage (DhcpSb, NULL, NULL, DHCP_MSG_DISCOVER, NULL); + + if (EFI_ERROR (Status)) { + DhcpSb->DhcpState = Dhcp4Init; + return Status; + } + } else { + DhcpSetState (DhcpSb, Dhcp4Rebooting, FALSE); + Status = DhcpSendMessage (DhcpSb, NULL, NULL, DHCP_MSG_REQUEST, NULL); + + if (EFI_ERROR (Status)) { + DhcpSb->DhcpState = Dhcp4InitReboot; + return Status; + } + } + + return EFI_SUCCESS; +} + + +/** + Call user provided callback function, and return the value the + function returns. If the user doesn't provide a callback, a + proper return value is selected to let the caller continue the + normal process. + + @param[in] DhcpSb The DHCP service instance + @param[in] Event The event as defined in the spec + @param[in] Packet The current packet trigger the event + @param[out] NewPacket The user's return new packet + + @retval EFI_NOT_READY Direct the caller to continue collecting the offer. + @retval EFI_SUCCESS The user function returns success. + @retval EFI_ABORTED The user function ask it to abort. + +**/ +EFI_STATUS +DhcpCallUser ( + IN DHCP_SERVICE *DhcpSb, + IN EFI_DHCP4_EVENT Event, + IN EFI_DHCP4_PACKET *Packet, OPTIONAL + OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL + ) +{ + EFI_DHCP4_CONFIG_DATA *Config; + EFI_STATUS Status; + + if (NewPacket != NULL) { + *NewPacket = NULL; + } + + // + // If user doesn't provide the call back function, return the value + // that directs the client to continue the normal process. + // In Dhcp4Selecting EFI_SUCCESS tells the client to stop collecting + // the offers and select a offer, EFI_NOT_READY tells the client to + // collect more offers. + // + Config = &DhcpSb->ActiveConfig; + + if (Config->Dhcp4Callback == NULL) { + if (Event == Dhcp4RcvdOffer) { + return EFI_NOT_READY; + } + + return EFI_SUCCESS; + } + + Status = Config->Dhcp4Callback ( + &DhcpSb->ActiveChild->Dhcp4Protocol, + Config->CallbackContext, + (EFI_DHCP4_STATE) DhcpSb->DhcpState, + Event, + Packet, + NewPacket + ); + + // + // User's callback should only return EFI_SUCCESS, EFI_NOT_READY, + // and EFI_ABORTED. If it returns values other than those, assume + // it to be EFI_ABORTED. + // + if ((Status == EFI_SUCCESS) || (Status == EFI_NOT_READY)) { + return Status; + } + + return EFI_ABORTED; +} + + +/** + Notify the user about the operation result. + + @param DhcpSb DHCP service instance + @param Which Which notify function to signal + +**/ +VOID +DhcpNotifyUser ( + IN DHCP_SERVICE *DhcpSb, + IN INTN Which + ) +{ + DHCP_PROTOCOL *Child; + + if ((Child = DhcpSb->ActiveChild) == NULL) { + return ; + } + + if ((Child->CompletionEvent != NULL) && + ((Which == DHCP_NOTIFY_COMPLETION) || (Which == DHCP_NOTIFY_ALL)) + ) { + + gBS->SignalEvent (Child->CompletionEvent); + Child->CompletionEvent = NULL; + } + + if ((Child->RenewRebindEvent != NULL) && + ((Which == DHCP_NOTIFY_RENEWREBIND) || (Which == DHCP_NOTIFY_ALL)) + ) { + + gBS->SignalEvent (Child->RenewRebindEvent); + Child->RenewRebindEvent = NULL; + } +} + + + +/** + Set the DHCP state. If CallUser is true, it will try to notify + the user before change the state by DhcpNotifyUser. It returns + EFI_ABORTED if the user return EFI_ABORTED, otherwise, it returns + EFI_SUCCESS. If CallUser is FALSE, it isn't necessary to test + the return value of this function. + + @param DhcpSb The DHCP service instance + @param State The new DHCP state to change to + @param CallUser Whether we need to call user + + @retval EFI_SUCCESS The state is changed + @retval EFI_ABORTED The user asks to abort the DHCP process. + +**/ +EFI_STATUS +DhcpSetState ( + IN OUT DHCP_SERVICE *DhcpSb, + IN INTN State, + IN BOOLEAN CallUser + ) +{ + EFI_STATUS Status; + + if (CallUser) { + Status = EFI_SUCCESS; + + if (State == Dhcp4Renewing) { + Status = DhcpCallUser (DhcpSb, Dhcp4EnterRenewing, NULL, NULL); + + } else if (State == Dhcp4Rebinding) { + Status = DhcpCallUser (DhcpSb, Dhcp4EnterRebinding, NULL, NULL); + + } else if (State == Dhcp4Bound) { + Status = DhcpCallUser (DhcpSb, Dhcp4BoundCompleted, NULL, NULL); + + } + + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Update the retransmission timer during the state transition. + // This will clear the retry count. This is also why the rule + // first transit the state, then send packets. + // + if (State == Dhcp4Selecting) { + DhcpSb->MaxRetries = DhcpSb->ActiveConfig.DiscoverTryCount; + } else { + DhcpSb->MaxRetries = DhcpSb->ActiveConfig.RequestTryCount; + } + + if (DhcpSb->MaxRetries == 0) { + DhcpSb->MaxRetries = 4; + } + + DhcpSb->CurRetry = 0; + DhcpSb->PacketToLive = 0; + DhcpSb->LastTimeout = 0; + DhcpSb->DhcpState = State; + return EFI_SUCCESS; +} + + +/** + Set the retransmit timer for the packet. It will select from either + the discover timeouts/request timeouts or the default timeout values. + + @param DhcpSb The DHCP service instance. + +**/ +VOID +DhcpSetTransmitTimer ( + IN OUT DHCP_SERVICE *DhcpSb + ) +{ + UINT32 *Times; + + ASSERT (DhcpSb->MaxRetries > DhcpSb->CurRetry); + + if (DhcpSb->DhcpState == Dhcp4Selecting) { + Times = DhcpSb->ActiveConfig.DiscoverTimeout; + } else { + Times = DhcpSb->ActiveConfig.RequestTimeout; + } + + if (Times == NULL) { + Times = mDhcp4DefaultTimeout; + } + + DhcpSb->PacketToLive = Times[DhcpSb->CurRetry]; + DhcpSb->LastTimeout = DhcpSb->PacketToLive; + + return; +} + +/** + Compute the lease. If the server grants a permanent lease, just + process it as a normal timeout value since the lease will last + more than 100 years. + + @param DhcpSb The DHCP service instance + @param Para The DHCP parameter extracted from the server's + response. +**/ +VOID +DhcpComputeLease ( + IN OUT DHCP_SERVICE *DhcpSb, + IN DHCP_PARAMETER *Para + ) +{ + ASSERT (Para != NULL); + + DhcpSb->Lease = Para->Lease; + DhcpSb->T2 = Para->T2; + DhcpSb->T1 = Para->T1; + + if (DhcpSb->Lease == 0) { + DhcpSb->Lease = DHCP_DEFAULT_LEASE; + } + + if ((DhcpSb->T2 == 0) || (DhcpSb->T2 >= Para->Lease)) { + DhcpSb->T2 = Para->Lease - (Para->Lease >> 3); + } + + if ((DhcpSb->T1 == 0) || (DhcpSb->T1 >= Para->T2)) { + DhcpSb->T1 = DhcpSb->Lease >> 1; + } +} + + +/** + Configure a UDP IO port to use the acquired lease address. + DHCP driver needs this port to unicast packet to the server + such as DHCP release. + + @param[in] UdpIo The UDP IO to configure + @param[in] Context Dhcp service instance. + + @retval EFI_SUCCESS The UDP IO port is successfully configured. + @retval Others It failed to configure the port. + +**/ +EFI_STATUS +EFIAPI +DhcpConfigLeaseIoPort ( + IN UDP_IO *UdpIo, + IN VOID *Context + ) +{ + EFI_UDP4_CONFIG_DATA UdpConfigData; + EFI_IPv4_ADDRESS Subnet; + EFI_IPv4_ADDRESS Gateway; + DHCP_SERVICE *DhcpSb; + EFI_STATUS Status; + IP4_ADDR Ip; + + DhcpSb = (DHCP_SERVICE *) Context; + + UdpConfigData.AcceptBroadcast = FALSE; + UdpConfigData.AcceptPromiscuous = FALSE; + UdpConfigData.AcceptAnyPort = FALSE; + UdpConfigData.AllowDuplicatePort = TRUE; + UdpConfigData.TypeOfService = 0; + UdpConfigData.TimeToLive = 64; + UdpConfigData.DoNotFragment = FALSE; + UdpConfigData.ReceiveTimeout = 1; + UdpConfigData.TransmitTimeout = 0; + + UdpConfigData.UseDefaultAddress = FALSE; + UdpConfigData.StationPort = DHCP_CLIENT_PORT; + UdpConfigData.RemotePort = DHCP_SERVER_PORT; + + Ip = HTONL (DhcpSb->ClientAddr); + CopyMem (&UdpConfigData.StationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS)); + + Ip = HTONL (DhcpSb->Netmask); + CopyMem (&UdpConfigData.SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS)); + + ZeroMem (&UdpConfigData.RemoteAddress, sizeof (EFI_IPv4_ADDRESS)); + + Status = UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfigData); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Add a default route if received from the server. + // + if ((DhcpSb->Para != NULL) && (DhcpSb->Para->Router != 0)) { + ZeroMem (&Subnet, sizeof (EFI_IPv4_ADDRESS)); + + Ip = HTONL (DhcpSb->Para->Router); + CopyMem (&Gateway, &Ip, sizeof (EFI_IPv4_ADDRESS)); + + UdpIo->Protocol.Udp4->Routes (UdpIo->Protocol.Udp4, FALSE, &Subnet, &Subnet, &Gateway); + } + + return EFI_SUCCESS; +} + + +/** + Update the lease states when a new lease is acquired. It will not only + save the acquired the address and lease time, it will also create a UDP + child to provide address resolution for the address. + + @param DhcpSb The DHCP service instance + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_SUCCESS The lease is recorded. + +**/ +EFI_STATUS +DhcpLeaseAcquired ( + IN OUT DHCP_SERVICE *DhcpSb + ) +{ + DhcpSb->ClientAddr = EFI_NTOHL (DhcpSb->Selected->Dhcp4.Header.YourAddr); + + if (DhcpSb->Para != NULL) { + DhcpSb->Netmask = DhcpSb->Para->NetMask; + DhcpSb->ServerAddr = DhcpSb->Para->ServerId; + } + + if (DhcpSb->Netmask == 0) { + return EFI_ABORTED; + } + + if (DhcpSb->LeaseIoPort != NULL) { + UdpIoFreeIo (DhcpSb->LeaseIoPort); + } + + // + // Create a UDP/IP child to provide ARP service for the Leased IP, + // and transmit unicast packet with it as source address. Don't + // start receive on this port, the queued packet will be timeout. + // + DhcpSb->LeaseIoPort = UdpIoCreateIo ( + DhcpSb->Controller, + DhcpSb->Image, + DhcpConfigLeaseIoPort, + UDP_IO_UDP4_VERSION, + DhcpSb + ); + + if (DhcpSb->LeaseIoPort == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if (!DHCP_IS_BOOTP (DhcpSb->Para)) { + DhcpComputeLease (DhcpSb, DhcpSb->Para); + } + + return DhcpSetState (DhcpSb, Dhcp4Bound, TRUE); +} + + +/** + Clean up the DHCP related states, IoStatus isn't reset. + + @param DhcpSb The DHCP instance service. + +**/ +VOID +DhcpCleanLease ( + IN DHCP_SERVICE *DhcpSb + ) +{ + DhcpSb->DhcpState = Dhcp4Init; + DhcpSb->Xid = DhcpSb->Xid + 1; + DhcpSb->ClientAddr = 0; + DhcpSb->Netmask = 0; + DhcpSb->ServerAddr = 0; + + if (DhcpSb->LastOffer != NULL) { + FreePool (DhcpSb->LastOffer); + DhcpSb->LastOffer = NULL; + } + + if (DhcpSb->Selected != NULL) { + FreePool (DhcpSb->Selected); + DhcpSb->Selected = NULL; + } + + if (DhcpSb->Para != NULL) { + FreePool (DhcpSb->Para); + DhcpSb->Para = NULL; + } + + DhcpSb->Lease = 0; + DhcpSb->T1 = 0; + DhcpSb->T2 = 0; + DhcpSb->ExtraRefresh = FALSE; + + if (DhcpSb->LeaseIoPort != NULL) { + UdpIoFreeIo (DhcpSb->LeaseIoPort); + DhcpSb->LeaseIoPort = NULL; + } + + if (DhcpSb->LastPacket != NULL) { + FreePool (DhcpSb->LastPacket); + DhcpSb->LastPacket = NULL; + } + + DhcpSb->PacketToLive = 0; + DhcpSb->LastTimeout = 0; + DhcpSb->CurRetry = 0; + DhcpSb->MaxRetries = 0; + DhcpSb->LeaseLife = 0; + + // + // Clean active config data. + // + DhcpCleanConfigure (&DhcpSb->ActiveConfig); +} + + +/** + Select a offer among all the offers collected. If the offer selected is + of BOOTP, the lease is recorded and user notified. If the offer is of + DHCP, it will request the offer from the server. + + @param[in] DhcpSb The DHCP service instance. + + @retval EFI_SUCCESS One of the offer is selected. + +**/ +EFI_STATUS +DhcpChooseOffer ( + IN DHCP_SERVICE *DhcpSb + ) +{ + EFI_DHCP4_PACKET *Selected; + EFI_DHCP4_PACKET *NewPacket; + EFI_DHCP4_PACKET *TempPacket; + EFI_STATUS Status; + + ASSERT (DhcpSb->LastOffer != NULL); + + // + // User will cache previous offers if he wants to select + // from multiple offers. If user provides an invalid packet, + // use the last offer, otherwise use the provided packet. + // + NewPacket = NULL; + Status = DhcpCallUser (DhcpSb, Dhcp4SelectOffer, DhcpSb->LastOffer, &NewPacket); + + if (EFI_ERROR (Status)) { + return Status; + } + + Selected = DhcpSb->LastOffer; + + if ((NewPacket != NULL) && !EFI_ERROR (DhcpValidateOptions (NewPacket, NULL))) { + TempPacket = (EFI_DHCP4_PACKET *) AllocatePool (NewPacket->Size); + if (TempPacket != NULL) { + CopyMem (TempPacket, NewPacket, NewPacket->Size); + FreePool (Selected); + Selected = TempPacket; + } + } + + DhcpSb->Selected = Selected; + DhcpSb->LastOffer = NULL; + DhcpSb->Para = NULL; + DhcpValidateOptions (Selected, &DhcpSb->Para); + + // + // A bootp offer has been selected, save the lease status, + // enter bound state then notify the user. + // + if (DHCP_IS_BOOTP (DhcpSb->Para)) { + Status = DhcpLeaseAcquired (DhcpSb); + + if (EFI_ERROR (Status)) { + return Status; + } + + DhcpSb->IoStatus = EFI_SUCCESS; + DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_ALL); + return EFI_SUCCESS; + } + + // + // Send a DHCP requests + // + Status = DhcpSetState (DhcpSb, Dhcp4Requesting, TRUE); + + if (EFI_ERROR (Status)) { + return Status; + } + + return DhcpSendMessage (DhcpSb, Selected, DhcpSb->Para, DHCP_MSG_REQUEST, NULL); +} + + +/** + Terminate the current address acquire. All the allocated resources + are released. Be careful when calling this function. A rule related + to this is: only call DhcpEndSession at the highest level, such as + DhcpInput, DhcpOnTimerTick...At the other level, just return error. + + @param[in] DhcpSb The DHCP service instance + @param[in] Status The result of the DHCP process. + +**/ +VOID +DhcpEndSession ( + IN DHCP_SERVICE *DhcpSb, + IN EFI_STATUS Status + ) +{ + if (DHCP_CONNECTED (DhcpSb->DhcpState)) { + DhcpCallUser (DhcpSb, Dhcp4AddressLost, NULL, NULL); + } else { + DhcpCallUser (DhcpSb, Dhcp4Fail, NULL, NULL); + } + + DhcpCleanLease (DhcpSb); + + DhcpSb->IoStatus = Status; + DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_ALL); +} + + +/** + Handle packets in DHCP select state. + + @param[in] DhcpSb The DHCP service instance + @param[in] Packet The DHCP packet received + @param[in] Para The DHCP parameter extracted from the packet. That + is, all the option value that we care. + + @retval EFI_SUCCESS The packet is successfully processed. + @retval Others Some error occured. + +**/ +EFI_STATUS +DhcpHandleSelect ( + IN DHCP_SERVICE *DhcpSb, + IN EFI_DHCP4_PACKET *Packet, + IN DHCP_PARAMETER *Para + ) +{ + EFI_STATUS Status; + + Status = EFI_SUCCESS; + + // + // First validate the message: + // 1. the offer is a unicast + // 2. if it is a DHCP message, it must contains a server ID. + // Don't return a error for these two case otherwise the session is ended. + // + if (!DHCP_IS_BOOTP (Para) && + ((Para->DhcpType != DHCP_MSG_OFFER) || (Para->ServerId == 0)) + ) { + goto ON_EXIT; + } + + // + // Call the user's callback. The action according to the return is as: + // 1. EFI_SUCESS: stop waiting for more offers, select the offer now + // 2. EFI_NOT_READY: wait for more offers + // 3. EFI_ABORTED: abort the address acquiring. + // + Status = DhcpCallUser (DhcpSb, Dhcp4RcvdOffer, Packet, NULL); + + if (Status == EFI_SUCCESS) { + if (DhcpSb->LastOffer != NULL) { + FreePool (DhcpSb->LastOffer); + } + + DhcpSb->LastOffer = Packet; + + return DhcpChooseOffer (DhcpSb); + + } else if (Status == EFI_NOT_READY) { + if (DhcpSb->LastOffer != NULL) { + FreePool (DhcpSb->LastOffer); + } + + DhcpSb->LastOffer = Packet; + + } else if (Status == EFI_ABORTED) { + // + // DhcpInput will end the session upon error return. Remember + // only to call DhcpEndSession at the top level call. + // + goto ON_EXIT; + } + + return EFI_SUCCESS; + +ON_EXIT: + FreePool (Packet); + return Status; +} + + +/** + Handle packets in DHCP request state. + + @param[in] DhcpSb The DHCP service instance + @param[in] Packet The DHCP packet received + @param[in] Para The DHCP parameter extracted from the packet. That + is, all the option value that we care. + + @retval EFI_SUCCESS The packet is successfully processed. + @retval Others Some error occured. + +**/ +EFI_STATUS +DhcpHandleRequest ( + IN DHCP_SERVICE *DhcpSb, + IN EFI_DHCP4_PACKET *Packet, + IN DHCP_PARAMETER *Para + ) +{ + EFI_DHCP4_HEADER *Head; + EFI_DHCP4_HEADER *Selected; + EFI_STATUS Status; + UINT8 *Message; + + ASSERT (!DHCP_IS_BOOTP (DhcpSb->Para)); + + Head = &Packet->Dhcp4.Header; + Selected = &DhcpSb->Selected->Dhcp4.Header; + + // + // Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK. + // + if (DHCP_IS_BOOTP (Para) || + (Para->ServerId != DhcpSb->Para->ServerId) || + ((Para->DhcpType != DHCP_MSG_ACK) && (Para->DhcpType != DHCP_MSG_NAK)) + ) { + + Status = EFI_SUCCESS; + goto ON_EXIT; + } + + // + // Received a NAK, end the session no matter what the user returns + // + Status = EFI_DEVICE_ERROR; + + if (Para->DhcpType == DHCP_MSG_NAK) { + DhcpCallUser (DhcpSb, Dhcp4RcvdNak, Packet, NULL); + goto ON_EXIT; + } + + // + // Check whether the ACK matches the selected offer + // + Message = NULL; + + if (!EFI_IP4_EQUAL (&Head->YourAddr, &Selected->YourAddr)) { + Message = (UINT8 *) "Lease confirmed isn't the same as that in the offer"; + goto REJECT; + } + + Status = DhcpCallUser (DhcpSb, Dhcp4RcvdAck, Packet, NULL); + + if (EFI_ERROR (Status)) { + Message = (UINT8 *) "Lease is denied upon received ACK"; + goto REJECT; + } + + // + // Record the lease, transit to BOUND state, then notify the user + // + Status = DhcpLeaseAcquired (DhcpSb); + + if (EFI_ERROR (Status)) { + Message = (UINT8 *) "Lease is denied upon entering bound"; + goto REJECT; + } + + DhcpSb->IoStatus = EFI_SUCCESS; + DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_COMPLETION); + + FreePool (Packet); + return EFI_SUCCESS; + +REJECT: + DhcpSendMessage (DhcpSb, DhcpSb->Selected, DhcpSb->Para, DHCP_MSG_DECLINE, Message); + +ON_EXIT: + FreePool (Packet); + return Status; +} + + +/** + Handle packets in DHCP renew/rebound state. + + @param[in] DhcpSb The DHCP service instance + @param[in] Packet The DHCP packet received + @param[in] Para The DHCP parameter extracted from the packet. That + is, all the option value that we care. + + @retval EFI_SUCCESS The packet is successfully processed. + @retval Others Some error occured. + +**/ +EFI_STATUS +DhcpHandleRenewRebind ( + IN DHCP_SERVICE *DhcpSb, + IN EFI_DHCP4_PACKET *Packet, + IN DHCP_PARAMETER *Para + ) +{ + EFI_DHCP4_HEADER *Head; + EFI_DHCP4_HEADER *Selected; + EFI_STATUS Status; + + ASSERT (!DHCP_IS_BOOTP (DhcpSb->Para)); + + Head = &Packet->Dhcp4.Header; + Selected = &DhcpSb->Selected->Dhcp4.Header; + + // + // Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK + // + if (DHCP_IS_BOOTP (Para) || + (Para->ServerId != DhcpSb->Para->ServerId) || + ((Para->DhcpType != DHCP_MSG_ACK) && (Para->DhcpType != DHCP_MSG_NAK)) + ) { + + Status = EFI_SUCCESS; + goto ON_EXIT; + } + + // + // Received a NAK, ignore the user's return then terminate the process + // + Status = EFI_DEVICE_ERROR; + + if (Para->DhcpType == DHCP_MSG_NAK) { + DhcpCallUser (DhcpSb, Dhcp4RcvdNak, Packet, NULL); + goto ON_EXIT; + } + + // + // The lease is different from the selected. Don't send a DECLINE + // since it isn't existed in the client's FSM. + // + if (!EFI_IP4_EQUAL (&Head->YourAddr, &Selected->YourAddr)) { + goto ON_EXIT; + } + + Status = DhcpCallUser (DhcpSb, Dhcp4RcvdAck, Packet, NULL); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Record the lease, start timer for T1 and T2, + // + DhcpComputeLease (DhcpSb, Para); + DhcpSb->LeaseLife = 0; + DhcpSetState (DhcpSb, Dhcp4Bound, TRUE); + + if (DhcpSb->ExtraRefresh != 0) { + DhcpSb->ExtraRefresh = FALSE; + + DhcpSb->IoStatus = EFI_SUCCESS; + DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_RENEWREBIND); + } + +ON_EXIT: + FreePool (Packet); + return Status; +} + + +/** + Handle packets in DHCP reboot state. + + @param[in] DhcpSb The DHCP service instance + @param[in] Packet The DHCP packet received + @param[in] Para The DHCP parameter extracted from the packet. That + is, all the option value that we care. + + @retval EFI_SUCCESS The packet is successfully processed. + @retval Others Some error occured. + +**/ +EFI_STATUS +DhcpHandleReboot ( + IN DHCP_SERVICE *DhcpSb, + IN EFI_DHCP4_PACKET *Packet, + IN DHCP_PARAMETER *Para + ) +{ + EFI_DHCP4_HEADER *Head; + EFI_STATUS Status; + + Head = &Packet->Dhcp4.Header; + + // + // Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK + // + if (DHCP_IS_BOOTP (Para) || + ((Para->DhcpType != DHCP_MSG_ACK) && (Para->DhcpType != DHCP_MSG_NAK)) + ) { + + Status = EFI_SUCCESS; + goto ON_EXIT; + } + + // + // If a NAK is received, transit to INIT and try again. + // + if (Para->DhcpType == DHCP_MSG_NAK) { + DhcpCallUser (DhcpSb, Dhcp4RcvdNak, Packet, NULL); + + DhcpSb->ClientAddr = 0; + DhcpSb->DhcpState = Dhcp4Init; + + Status = DhcpInitRequest (DhcpSb); + goto ON_EXIT; + } + + // + // Check whether the ACK matches the selected offer + // + if (EFI_NTOHL (Head->YourAddr) != DhcpSb->ClientAddr) { + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + Status = DhcpCallUser (DhcpSb, Dhcp4RcvdAck, Packet, NULL); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // OK, get the parameter from server, record the lease + // + DhcpSb->Para = AllocateCopyPool (sizeof (DHCP_PARAMETER), Para); + if (DhcpSb->Para == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + DhcpSb->Selected = Packet; + Status = DhcpLeaseAcquired (DhcpSb); + if (EFI_ERROR (Status)) { + return Status; + } + + DhcpSb->IoStatus = EFI_SUCCESS; + DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_COMPLETION); + return EFI_SUCCESS; + +ON_EXIT: + FreePool (Packet); + return Status; +} + + +/** + Handle the received DHCP packets. This function drives the DHCP + state machine. + + @param UdpPacket The UDP packets received. + @param EndPoint The local/remote UDP access point + @param IoStatus The status of the UDP receive + @param Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +DhcpInput ( + NET_BUF *UdpPacket, + UDP_END_POINT *EndPoint, + EFI_STATUS IoStatus, + VOID *Context + ) +{ + DHCP_SERVICE *DhcpSb; + EFI_DHCP4_HEADER *Head; + EFI_DHCP4_PACKET *Packet; + DHCP_PARAMETER *Para; + EFI_STATUS Status; + UINT32 Len; + + Packet = NULL; + DhcpSb = (DHCP_SERVICE *) Context; + + // + // Don't restart receive if error occurs or DHCP is destroyed. + // + if (EFI_ERROR (IoStatus)) { + return ; + } else if (DhcpSb->ServiceState == DHCP_DESTROY) { + NetbufFree (UdpPacket); + return ; + } + + ASSERT (UdpPacket != NULL); + + if (DhcpSb->DhcpState == Dhcp4Stopped) { + goto RESTART; + } + + // + // Validate the packet received + // + if (UdpPacket->TotalSize < sizeof (EFI_DHCP4_HEADER)) { + goto RESTART; + } + + // + // Copy the DHCP message to a continuous memory block + // + Len = sizeof (EFI_DHCP4_PACKET) + UdpPacket->TotalSize - sizeof (EFI_DHCP4_HEADER); + Packet = (EFI_DHCP4_PACKET *) AllocatePool (Len); + + if (Packet == NULL) { + goto RESTART; + } + + Packet->Size = Len; + Head = &Packet->Dhcp4.Header; + Packet->Length = NetbufCopy (UdpPacket, 0, UdpPacket->TotalSize, (UINT8 *) Head); + + if (Packet->Length != UdpPacket->TotalSize) { + goto RESTART; + } + + // + // Is this packet the answer to our packet? + // + if ((Head->OpCode != BOOTP_REPLY) || + (NTOHL (Head->Xid) != DhcpSb->Xid) || + (CompareMem (DhcpSb->ClientAddressSendOut, Head->ClientHwAddr, Head->HwAddrLen) != 0)) { + goto RESTART; + } + + // + // Validate the options and retrieve the interested options + // + Para = NULL; + if ((Packet->Length > sizeof (EFI_DHCP4_HEADER) + sizeof (UINT32)) && + (Packet->Dhcp4.Magik == DHCP_OPTION_MAGIC) && + EFI_ERROR (DhcpValidateOptions (Packet, &Para))) { + + goto RESTART; + } + + // + // Call the handler for each state. The handler should return + // EFI_SUCCESS if the process can go on no matter whether the + // packet is ignored or not. If the return is EFI_ERROR, the + // session will be terminated. Packet's ownership is handled + // over to the handlers. If operation succeeds, the handler + // must notify the user. It isn't necessary to do if EFI_ERROR + // is returned because the DhcpEndSession will notify the user. + // + Status = EFI_SUCCESS; + + switch (DhcpSb->DhcpState) { + case Dhcp4Selecting: + Status = DhcpHandleSelect (DhcpSb, Packet, Para); + break; + + case Dhcp4Requesting: + Status = DhcpHandleRequest (DhcpSb, Packet, Para); + break; + + case Dhcp4InitReboot: + case Dhcp4Init: + case Dhcp4Bound: + // + // Ignore the packet in INITREBOOT, INIT and BOUND states + // + FreePool (Packet); + Status = EFI_SUCCESS; + break; + + case Dhcp4Renewing: + case Dhcp4Rebinding: + Status = DhcpHandleRenewRebind (DhcpSb, Packet, Para); + break; + + case Dhcp4Rebooting: + Status = DhcpHandleReboot (DhcpSb, Packet, Para); + break; + } + + if (Para != NULL) { + FreePool (Para); + } + + Packet = NULL; + + if (EFI_ERROR (Status)) { + NetbufFree (UdpPacket); + UdpIoRecvDatagram (DhcpSb->UdpIo, DhcpInput, DhcpSb, 0); + DhcpEndSession (DhcpSb, Status); + return ; + } + +RESTART: + NetbufFree (UdpPacket); + + if (Packet != NULL) { + FreePool (Packet); + } + + Status = UdpIoRecvDatagram (DhcpSb->UdpIo, DhcpInput, DhcpSb, 0); + + if (EFI_ERROR (Status)) { + DhcpEndSession (DhcpSb, EFI_DEVICE_ERROR); + } +} + +/** + Release the net buffer when packet is sent. + + @param UdpPacket The UDP packets received. + @param EndPoint The local/remote UDP access point + @param IoStatus The status of the UDP receive + @param Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +DhcpOnPacketSent ( + NET_BUF *Packet, + UDP_END_POINT *EndPoint, + EFI_STATUS IoStatus, + VOID *Context + ) +{ + NetbufFree (Packet); +} + + + +/** + Build and transmit a DHCP message according to the current states. + This function implement the Table 5. of RFC 2131. Always transits + the state (as defined in Figure 5. of the same RFC) before sending + a DHCP message. The table is adjusted accordingly. + + @param[in] DhcpSb The DHCP service instance + @param[in] Seed The seed packet which the new packet is based on + @param[in] Para The DHCP parameter of the Seed packet + @param[in] Type The message type to send + @param[in] Msg The human readable message to include in the packet + sent. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources for the packet + @retval EFI_ACCESS_DENIED Failed to transmit the packet through UDP + @retval EFI_SUCCESS The message is sent + @retval other Other error occurs + +**/ +EFI_STATUS +DhcpSendMessage ( + IN DHCP_SERVICE *DhcpSb, + IN EFI_DHCP4_PACKET *Seed, + IN DHCP_PARAMETER *Para, + IN UINT8 Type, + IN UINT8 *Msg + ) +{ + EFI_DHCP4_CONFIG_DATA *Config; + EFI_DHCP4_PACKET *Packet; + EFI_DHCP4_PACKET *NewPacket; + EFI_DHCP4_HEADER *Head; + EFI_DHCP4_HEADER *SeedHead; + UDP_IO *UdpIo; + UDP_END_POINT EndPoint; + NET_BUF *Wrap; + NET_FRAGMENT Frag; + EFI_STATUS Status; + IP4_ADDR IpAddr; + UINT8 *Buf; + UINT16 MaxMsg; + UINT32 Len; + UINT32 Index; + + // + // Allocate a big enough memory block to hold the DHCP packet + // + Len = sizeof (EFI_DHCP4_PACKET) + 128 + DhcpSb->UserOptionLen; + + if (Msg != NULL) { + Len += (UINT32)AsciiStrLen ((CHAR8 *) Msg); + } + + Packet = AllocatePool (Len); + + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Packet->Size = Len; + Packet->Length = sizeof (EFI_DHCP4_HEADER) + sizeof (UINT32); + + // + // Fill in the DHCP header fields + // + Config = &DhcpSb->ActiveConfig; + SeedHead = NULL; + + if (Seed != NULL) { + SeedHead = &Seed->Dhcp4.Header; + } + + Head = &Packet->Dhcp4.Header; + ZeroMem (Head, sizeof (EFI_DHCP4_HEADER)); + + Head->OpCode = BOOTP_REQUEST; + Head->HwType = DhcpSb->HwType; + Head->HwAddrLen = DhcpSb->HwLen; + Head->Xid = HTONL (DhcpSb->Xid); + Head->Reserved = HTONS (0x8000); //Server, broadcast the message please. + + EFI_IP4 (Head->ClientAddr) = HTONL (DhcpSb->ClientAddr); + CopyMem (Head->ClientHwAddr, DhcpSb->Mac.Addr, DhcpSb->HwLen); + + if ((Type == DHCP_MSG_DECLINE) || (Type == DHCP_MSG_RELEASE)) { + Head->Seconds = 0; + } else if ((Type == DHCP_MSG_REQUEST) && (DhcpSb->DhcpState == Dhcp4Requesting)) { + // + // Use the same value as the original DHCPDISCOVER message. + // + Head->Seconds = DhcpSb->LastPacket->Dhcp4.Header.Seconds; + } else { + SetElapsedTime(&Head->Seconds, DhcpSb->ActiveChild); + } + + // + // Append the DHCP message type + // + Packet->Dhcp4.Magik = DHCP_OPTION_MAGIC; + Buf = Packet->Dhcp4.Option; + Buf = DhcpAppendOption (Buf, DHCP4_TAG_MSG_TYPE, 1, &Type); + + // + // Append the serverid option if necessary: + // 1. DHCP decline message + // 2. DHCP release message + // 3. DHCP request to confirm one lease. + // + if ((Type == DHCP_MSG_DECLINE) || (Type == DHCP_MSG_RELEASE) || + ((Type == DHCP_MSG_REQUEST) && (DhcpSb->DhcpState == Dhcp4Requesting)) + ) { + + ASSERT ((Para != NULL) && (Para->ServerId != 0)); + + IpAddr = HTONL (Para->ServerId); + Buf = DhcpAppendOption (Buf, DHCP4_TAG_SERVER_ID, 4, (UINT8 *) &IpAddr); + } + + // + // Append the requested IP option if necessary: + // 1. DHCP request to use the previously allocated address + // 2. DHCP request to confirm one lease + // 3. DHCP decline to decline one lease + // + IpAddr = 0; + + if (Type == DHCP_MSG_REQUEST) { + if (DhcpSb->DhcpState == Dhcp4Rebooting) { + IpAddr = EFI_IP4 (Config->ClientAddress); + + } else if (DhcpSb->DhcpState == Dhcp4Requesting) { + ASSERT (SeedHead != NULL); + IpAddr = EFI_IP4 (SeedHead->YourAddr); + } + + } else if (Type == DHCP_MSG_DECLINE) { + ASSERT (SeedHead != NULL); + IpAddr = EFI_IP4 (SeedHead->YourAddr); + } + + if (IpAddr != 0) { + Buf = DhcpAppendOption (Buf, DHCP4_TAG_REQUEST_IP, 4, (UINT8 *) &IpAddr); + } + + // + // Append the Max Message Length option if it isn't a DECLINE + // or RELEASE to direct the server use large messages instead of + // override the BOOTFILE and SERVER fields in the message head. + // + if ((Type != DHCP_MSG_DECLINE) && (Type != DHCP_MSG_RELEASE)) { + MaxMsg = HTONS (0xFF00); + Buf = DhcpAppendOption (Buf, DHCP4_TAG_MAXMSG, 2, (UINT8 *) &MaxMsg); + } + + // + // Append the user's message if it isn't NULL + // + if (Msg != NULL) { + Len = MIN ((UINT32) AsciiStrLen ((CHAR8 *) Msg), 255); + Buf = DhcpAppendOption (Buf, DHCP4_TAG_MESSAGE, (UINT16) Len, Msg); + } + + // + // Append the user configured options + // + if (DhcpSb->UserOptionLen != 0) { + for (Index = 0; Index < Config->OptionCount; Index++) { + // + // We can't use any option other than the client ID from user + // if it is a DHCP decline or DHCP release . + // + if (((Type == DHCP_MSG_DECLINE) || (Type == DHCP_MSG_RELEASE)) && + (Config->OptionList[Index]->OpCode != DHCP4_TAG_CLIENT_ID)) { + continue; + } + + Buf = DhcpAppendOption ( + Buf, + Config->OptionList[Index]->OpCode, + Config->OptionList[Index]->Length, + Config->OptionList[Index]->Data + ); + } + } + + *(Buf++) = DHCP4_TAG_EOP; + Packet->Length += (UINT32) (Buf - Packet->Dhcp4.Option); + + // + // OK, the message is built, call the user to override it. + // + Status = EFI_SUCCESS; + NewPacket = NULL; + + if (Type == DHCP_MSG_DISCOVER) { + Status = DhcpCallUser (DhcpSb, Dhcp4SendDiscover, Packet, &NewPacket); + + } else if (Type == DHCP_MSG_REQUEST) { + Status = DhcpCallUser (DhcpSb, Dhcp4SendRequest, Packet, &NewPacket); + + } else if (Type == DHCP_MSG_DECLINE) { + Status = DhcpCallUser (DhcpSb, Dhcp4SendDecline, Packet, &NewPacket); + } + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + if (NewPacket != NULL) { + FreePool (Packet); + Packet = NewPacket; + } + + // + // Save the Client Address will be sent out + // + CopyMem ( + &DhcpSb->ClientAddressSendOut[0], + &Packet->Dhcp4.Header.ClientHwAddr[0], + Packet->Dhcp4.Header.HwAddrLen + ); + + // + // Wrap it into a netbuf then send it. + // + Frag.Bulk = (UINT8 *) &Packet->Dhcp4.Header; + Frag.Len = Packet->Length; + Wrap = NetbufFromExt (&Frag, 1, 0, 0, DhcpDummyExtFree, NULL); + + if (Wrap == NULL) { + FreePool (Packet); + return EFI_OUT_OF_RESOURCES; + } + + // + // Save it as the last sent packet for retransmission + // + if (DhcpSb->LastPacket != NULL) { + FreePool (DhcpSb->LastPacket); + } + + DhcpSb->LastPacket = Packet; + DhcpSetTransmitTimer (DhcpSb); + + // + // Broadcast the message, unless we know the server address. + // Use the lease UdpIo port to send the unicast packet. + // + EndPoint.RemoteAddr.Addr[0] = 0xffffffff; + EndPoint.LocalAddr.Addr[0] = 0; + EndPoint.RemotePort = DHCP_SERVER_PORT; + EndPoint.LocalPort = DHCP_CLIENT_PORT; + UdpIo = DhcpSb->UdpIo; + + if ((DhcpSb->DhcpState == Dhcp4Renewing) || (Type == DHCP_MSG_RELEASE)) { + EndPoint.RemoteAddr.Addr[0] = DhcpSb->ServerAddr; + EndPoint.LocalAddr.Addr[0] = DhcpSb->ClientAddr; + UdpIo = DhcpSb->LeaseIoPort; + } + + ASSERT (UdpIo != NULL); + + Status = UdpIoSendDatagram ( + UdpIo, + Wrap, + &EndPoint, + NULL, + DhcpOnPacketSent, + DhcpSb + ); + + if (EFI_ERROR (Status)) { + NetbufFree (Wrap); + return EFI_ACCESS_DENIED; + } + + return EFI_SUCCESS; +} + + +/** + Retransmit a saved packet. Only DISCOVER and REQUEST messages + will be retransmitted. + + @param[in] DhcpSb The DHCP service instance + + @retval EFI_ACCESS_DENIED Failed to transmit packet through UDP port + @retval EFI_SUCCESS The packet is retransmitted. + +**/ +EFI_STATUS +DhcpRetransmit ( + IN DHCP_SERVICE *DhcpSb + ) +{ + UDP_IO *UdpIo; + UDP_END_POINT EndPoint; + NET_BUF *Wrap; + NET_FRAGMENT Frag; + EFI_STATUS Status; + + ASSERT (DhcpSb->LastPacket != NULL); + + // + // For REQUEST message in Dhcp4Requesting state, do not change the secs fields. + // + if (DhcpSb->DhcpState != Dhcp4Requesting) { + SetElapsedTime(&DhcpSb->LastPacket->Dhcp4.Header.Seconds, DhcpSb->ActiveChild); + } + + // + // Wrap it into a netbuf then send it. + // + Frag.Bulk = (UINT8 *) &DhcpSb->LastPacket->Dhcp4.Header; + Frag.Len = DhcpSb->LastPacket->Length; + Wrap = NetbufFromExt (&Frag, 1, 0, 0, DhcpDummyExtFree, NULL); + + if (Wrap == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Broadcast the message, unless we know the server address. + // + EndPoint.RemotePort = DHCP_SERVER_PORT; + EndPoint.LocalPort = DHCP_CLIENT_PORT; + EndPoint.RemoteAddr.Addr[0] = 0xffffffff; + EndPoint.LocalAddr.Addr[0] = 0; + UdpIo = DhcpSb->UdpIo; + + if (DhcpSb->DhcpState == Dhcp4Renewing) { + EndPoint.RemoteAddr.Addr[0] = DhcpSb->ServerAddr; + EndPoint.LocalAddr.Addr[0] = DhcpSb->ClientAddr; + UdpIo = DhcpSb->LeaseIoPort; + } + + ASSERT (UdpIo != NULL); + + Status = UdpIoSendDatagram ( + UdpIo, + Wrap, + &EndPoint, + NULL, + DhcpOnPacketSent, + DhcpSb + ); + + if (EFI_ERROR (Status)) { + NetbufFree (Wrap); + return EFI_ACCESS_DENIED; + } + + return EFI_SUCCESS; +} + + +/** + Each DHCP service has three timer. Two of them are count down timer. + One for the packet retransmission. The other is to collect the offers. + The third timer increaments the lease life which is compared to T1, T2, + and lease to determine the time to renew and rebind the lease. + DhcpOnTimerTick will be called once every second. + + @param[in] Event The timer event + @param[in] Context The context, which is the DHCP service instance. + +**/ +VOID +EFIAPI +DhcpOnTimerTick ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + DHCP_SERVICE *DhcpSb; + DHCP_PROTOCOL *Instance; + EFI_STATUS Status; + + DhcpSb = (DHCP_SERVICE *) Context; + Instance = DhcpSb->ActiveChild; + + // + // 0xffff is the maximum supported value for elapsed time according to RFC. + // + if (Instance != NULL && Instance->ElaspedTime < 0xffff) { + Instance->ElaspedTime++; + } + + // + // Check the retransmit timer + // + if ((DhcpSb->PacketToLive > 0) && (--DhcpSb->PacketToLive == 0)) { + + // + // Select offer at each timeout if any offer received. + // + if (DhcpSb->DhcpState == Dhcp4Selecting && DhcpSb->LastOffer != NULL) { + + Status = DhcpChooseOffer (DhcpSb); + + if (EFI_ERROR(Status)) { + if (DhcpSb->LastOffer != NULL) { + FreePool (DhcpSb->LastOffer); + DhcpSb->LastOffer = NULL; + } + } else { + goto ON_EXIT; + } + } + + if (++DhcpSb->CurRetry < DhcpSb->MaxRetries) { + // + // Still has another try + // + DhcpRetransmit (DhcpSb); + DhcpSetTransmitTimer (DhcpSb); + + } else if (DHCP_CONNECTED (DhcpSb->DhcpState)) { + + // + // Retransmission failed, if the DHCP request is initiated by + // user, adjust the current state according to the lease life. + // Otherwise do nothing to wait the lease to timeout + // + if (DhcpSb->ExtraRefresh != 0) { + Status = EFI_SUCCESS; + + if (DhcpSb->LeaseLife < DhcpSb->T1) { + Status = DhcpSetState (DhcpSb, Dhcp4Bound, FALSE); + + } else if (DhcpSb->LeaseLife < DhcpSb->T2) { + Status = DhcpSetState (DhcpSb, Dhcp4Renewing, FALSE); + + } else if (DhcpSb->LeaseLife < DhcpSb->Lease) { + Status = DhcpSetState (DhcpSb, Dhcp4Rebinding, FALSE); + + } else { + goto END_SESSION; + + } + + DhcpSb->IoStatus = EFI_TIMEOUT; + DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_RENEWREBIND); + } + } else { + goto END_SESSION; + } + } + + // + // If an address has been acquired, check whether need to + // refresh or whether it has expired. + // + if (DHCP_CONNECTED (DhcpSb->DhcpState)) { + DhcpSb->LeaseLife++; + + // + // Don't timeout the lease, only count the life if user is + // requesting extra renew/rebind. Adjust the state after that. + // + if (DhcpSb->ExtraRefresh != 0) { + return ; + } + + if (DhcpSb->LeaseLife == DhcpSb->Lease) { + // + // Lease expires, end the session + // + goto END_SESSION; + + } else if (DhcpSb->LeaseLife == DhcpSb->T2) { + // + // T2 expires, transit to rebinding then send a REQUEST to any server + // + if (EFI_ERROR (DhcpSetState (DhcpSb, Dhcp4Rebinding, TRUE))) { + goto END_SESSION; + } + + if (Instance != NULL) { + Instance->ElaspedTime= 0; + } + + Status = DhcpSendMessage ( + DhcpSb, + DhcpSb->Selected, + DhcpSb->Para, + DHCP_MSG_REQUEST, + NULL + ); + + if (EFI_ERROR (Status)) { + goto END_SESSION; + } + + } else if (DhcpSb->LeaseLife == DhcpSb->T1) { + // + // T1 expires, transit to renewing, then send a REQUEST to the server + // + if (EFI_ERROR (DhcpSetState (DhcpSb, Dhcp4Renewing, TRUE))) { + goto END_SESSION; + } + + if (Instance != NULL) { + Instance->ElaspedTime= 0; + } + + Status = DhcpSendMessage ( + DhcpSb, + DhcpSb->Selected, + DhcpSb->Para, + DHCP_MSG_REQUEST, + NULL + ); + + if (EFI_ERROR (Status)) { + goto END_SESSION; + } + } + } + +ON_EXIT: + // + // Iterate through all the DhcpSb Children. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &DhcpSb->Children) { + Instance = NET_LIST_USER_STRUCT (Entry, DHCP_PROTOCOL, Link); + Instance->Timeout--; + if (Instance->Timeout == 0 && Instance->Token != NULL) { + PxeDhcpDone (Instance); + } + } + + return ; + +END_SESSION: + DhcpEndSession (DhcpSb, EFI_TIMEOUT); + + return ; +} diff --git a/NetworkPkg/Dhcp4Dxe/Dhcp4Io.h b/NetworkPkg/Dhcp4Dxe/Dhcp4Io.h new file mode 100644 index 000000000..01283e5e1 --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/Dhcp4Io.h @@ -0,0 +1,189 @@ +/** @file + The DHCP4 protocol implementation. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_DHCP4_IO_H__ +#define __EFI_DHCP4_IO_H__ + +#include + +#include + +#include +#include +#include +#include + + + +#define DHCP_WAIT_OFFER 3 // Time to wait the offers +#define DHCP_DEFAULT_LEASE 7 * 24 * 60 * 60 // Seven days as default. +#define DHCP_SERVER_PORT 67 +#define DHCP_CLIENT_PORT 68 + +// +// BOOTP header "op" field +// +#define BOOTP_REQUEST 1 +#define BOOTP_REPLY 2 + +// +// DHCP message types +// +#define DHCP_MSG_DISCOVER 1 +#define DHCP_MSG_OFFER 2 +#define DHCP_MSG_REQUEST 3 +#define DHCP_MSG_DECLINE 4 +#define DHCP_MSG_ACK 5 +#define DHCP_MSG_NAK 6 +#define DHCP_MSG_RELEASE 7 +#define DHCP_MSG_INFORM 8 + +// +// DHCP notify user type +// +#define DHCP_NOTIFY_COMPLETION 1 +#define DHCP_NOTIFY_RENEWREBIND 2 +#define DHCP_NOTIFY_ALL 3 + +#define DHCP_IS_BOOTP(Parameter) (((Parameter) == NULL) || ((Parameter)->DhcpType == 0)) + +#define DHCP_CONNECTED(State) \ + (((State) == Dhcp4Bound) || ((State) == (Dhcp4Renewing)) || ((State) == Dhcp4Rebinding)) + +/** + Set the DHCP state. If CallUser is true, it will try to notify + the user before change the state by DhcpNotifyUser. It returns + EFI_ABORTED if the user return EFI_ABORTED, otherwise, it returns + EFI_SUCCESS. If CallUser is FALSE, it isn't necessary to test + the return value of this function. + + @param DhcpSb The DHCP service instance + @param State The new DHCP state to change to + @param CallUser Whether we need to call user + + @retval EFI_SUCCESS The state is changed + @retval EFI_ABORTED The user asks to abort the DHCP process. + +**/ +EFI_STATUS +DhcpSetState ( + IN OUT DHCP_SERVICE *DhcpSb, + IN INTN State, + IN BOOLEAN CallUser + ); + +/** + Build and transmit a DHCP message according to the current states. + This function implement the Table 5. of RFC 2131. Always transits + the state (as defined in Figure 5. of the same RFC) before sending + a DHCP message. The table is adjusted accordingly. + + @param[in] DhcpSb The DHCP service instance + @param[in] Seed The seed packet which the new packet is based on + @param[in] Para The DHCP parameter of the Seed packet + @param[in] Type The message type to send + @param[in] Msg The human readable message to include in the packet + sent. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources for the packet + @retval EFI_ACCESS_DENIED Failed to transmit the packet through UDP + @retval EFI_SUCCESS The message is sent + @retval other Other error occurs + +**/ +EFI_STATUS +DhcpSendMessage ( + IN DHCP_SERVICE *DhcpSb, + IN EFI_DHCP4_PACKET *Seed, + IN DHCP_PARAMETER *Para, + IN UINT8 Type, + IN UINT8 *Msg + ); + +/** + Each DHCP service has three timer. Two of them are count down timer. + One for the packet retransmission. The other is to collect the offers. + The third timer increaments the lease life which is compared to T1, T2, + and lease to determine the time to renew and rebind the lease. + DhcpOnTimerTick will be called once every second. + + @param[in] Event The timer event + @param[in] Context The context, which is the DHCP service instance. + +**/ +VOID +EFIAPI +DhcpOnTimerTick ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Handle the received DHCP packets. This function drives the DHCP + state machine. + + @param UdpPacket The UDP packets received. + @param EndPoint The local/remote UDP access point + @param IoStatus The status of the UDP receive + @param Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +DhcpInput ( + NET_BUF *UdpPacket, + UDP_END_POINT *EndPoint, + EFI_STATUS IoStatus, + VOID *Context + ); + +/** + Send an initial DISCOVER or REQUEST message according to the + DHCP service's current state. + + @param[in] DhcpSb The DHCP service instance + + @retval EFI_SUCCESS The request has been sent + @retval other Some error occurs when sending the request. + +**/ +EFI_STATUS +DhcpInitRequest ( + IN DHCP_SERVICE *DhcpSb + ); + +/** + Clean up the DHCP related states, IoStatus isn't reset. + + @param DhcpSb The DHCP instance service. + +**/ +VOID +DhcpCleanLease ( + IN DHCP_SERVICE *DhcpSb + ); + +/** + Release the net buffer when packet is sent. + + @param UdpPacket The UDP packets received. + @param EndPoint The local/remote UDP access point + @param IoStatus The status of the UDP receive + @param Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +DhcpOnPacketSent ( + NET_BUF *Packet, + UDP_END_POINT *EndPoint, + EFI_STATUS IoStatus, + VOID *Context + ); + +#endif diff --git a/NetworkPkg/Dhcp4Dxe/Dhcp4Option.c b/NetworkPkg/Dhcp4Dxe/Dhcp4Option.c new file mode 100644 index 000000000..3adbf55d0 --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/Dhcp4Option.c @@ -0,0 +1,890 @@ +/** @file + Function to validate, parse, process the DHCP options. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Dhcp4Impl.h" + +/// +/// A list of the format of DHCP Options sorted by option tag +/// to validate a dhcp message. Refere the comments of the +/// DHCP_OPTION_FORMAT structure. +/// +DHCP_OPTION_FORMAT DhcpOptionFormats[] = { + {DHCP4_TAG_NETMASK, DHCP_OPTION_IP, 1, 1 , TRUE}, + {DHCP4_TAG_TIME_OFFSET, DHCP_OPTION_INT32, 1, 1 , FALSE}, + {DHCP4_TAG_ROUTER, DHCP_OPTION_IP, 1, -1 , TRUE}, + {DHCP4_TAG_TIME_SERVER, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_NAME_SERVER, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_DNS_SERVER, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_LOG_SERVER, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_COOKIE_SERVER, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_LPR_SERVER, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_IMPRESS_SERVER, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_RL_SERVER, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_HOSTNAME, DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_BOOTFILE_LEN, DHCP_OPTION_INT16, 1, 1 , FALSE}, + {DHCP4_TAG_DUMP, DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_DOMAINNAME, DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_SWAP_SERVER, DHCP_OPTION_IP, 1, 1 , FALSE}, + {DHCP4_TAG_ROOTPATH, DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_EXTEND_PATH, DHCP_OPTION_INT8, 1, -1 , FALSE}, + + {DHCP4_TAG_IPFORWARD, DHCP_OPTION_SWITCH, 1, 1 , FALSE}, + {DHCP4_TAG_NONLOCAL_SRR, DHCP_OPTION_SWITCH, 1, 1 , FALSE}, + {DHCP4_TAG_POLICY_SRR, DHCP_OPTION_IPPAIR, 1, -1 , FALSE}, + {DHCP4_TAG_EMTU, DHCP_OPTION_INT16, 1, 1 , FALSE}, + {DHCP4_TAG_TTL, DHCP_OPTION_INT8, 1, 1 , FALSE}, + {DHCP4_TAG_PATHMTU_AGE, DHCP_OPTION_INT32, 1, 1 , FALSE}, + {DHCP4_TAG_PATHMTU_PLATEAU,DHCP_OPTION_INT16, 1, -1 , FALSE}, + + {DHCP4_TAG_IFMTU, DHCP_OPTION_INT16, 1, 1 , FALSE}, + {DHCP4_TAG_SUBNET_LOCAL, DHCP_OPTION_SWITCH, 1, 1 , FALSE}, + {DHCP4_TAG_BROADCAST, DHCP_OPTION_IP, 1, 1 , FALSE}, + {DHCP4_TAG_DISCOVER_MASK, DHCP_OPTION_SWITCH, 1, 1 , FALSE}, + {DHCP4_TAG_SUPPLY_MASK, DHCP_OPTION_SWITCH, 1, 1 , FALSE}, + {DHCP4_TAG_DISCOVER_ROUTE, DHCP_OPTION_SWITCH, 1, 1 , FALSE}, + {DHCP4_TAG_ROUTER_SOLICIT, DHCP_OPTION_IP, 1, 1 , FALSE}, + {DHCP4_TAG_STATIC_ROUTE, DHCP_OPTION_IPPAIR, 1, -1 , FALSE}, + + {DHCP4_TAG_TRAILER, DHCP_OPTION_SWITCH, 1, 1 , FALSE}, + {DHCP4_TAG_ARPAGE, DHCP_OPTION_INT32, 1, 1 , FALSE}, + {DHCP4_TAG_ETHER_ENCAP, DHCP_OPTION_SWITCH, 1, 1 , FALSE}, + + {DHCP4_TAG_TCP_TTL, DHCP_OPTION_INT8, 1, 1 , FALSE}, + {DHCP4_TAG_KEEP_INTERVAL, DHCP_OPTION_INT32, 1, 1 , FALSE}, + {DHCP4_TAG_KEEP_GARBAGE, DHCP_OPTION_SWITCH, 1, 1 , FALSE}, + + {DHCP4_TAG_NIS_DOMAIN, DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_NIS_SERVER, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_NTP_SERVER, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_VENDOR, DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_NBNS, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_NBDD, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_NBTYPE, DHCP_OPTION_INT8, 1, 1 , FALSE}, + {DHCP4_TAG_NBSCOPE, DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_XFONT, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_XDM, DHCP_OPTION_IP, 1, -1 , FALSE}, + + {DHCP4_TAG_REQUEST_IP, DHCP_OPTION_IP, 1, 1 , FALSE}, + {DHCP4_TAG_LEASE, DHCP_OPTION_INT32, 1, 1 , TRUE}, + {DHCP4_TAG_OVERLOAD, DHCP_OPTION_INT8, 1, 1 , TRUE}, + {DHCP4_TAG_MSG_TYPE, DHCP_OPTION_INT8, 1, 1 , TRUE}, + {DHCP4_TAG_SERVER_ID, DHCP_OPTION_IP, 1, 1 , TRUE}, + {DHCP4_TAG_PARA_LIST, DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_MESSAGE, DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_MAXMSG, DHCP_OPTION_INT16, 1, 1 , FALSE}, + {DHCP4_TAG_T1, DHCP_OPTION_INT32, 1, 1 , TRUE}, + {DHCP4_TAG_T2, DHCP_OPTION_INT32, 1, 1 , TRUE}, + {DHCP4_TAG_VENDOR_CLASS_ID,DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_CLIENT_ID, DHCP_OPTION_INT8, 2, -1 , FALSE}, + + {DHCP4_TAG_NISPLUS, DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_NISPLUS_SERVER, DHCP_OPTION_IP, 1, -1 , FALSE}, + + {DHCP4_TAG_TFTP, DHCP_OPTION_INT8, 1, -1 , FALSE}, + {DHCP4_TAG_BOOTFILE, DHCP_OPTION_INT8, 1, -1 , FALSE}, + + {DHCP4_TAG_MOBILEIP, DHCP_OPTION_IP, 0, -1 , FALSE}, + {DHCP4_TAG_SMTP, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_POP3, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_NNTP, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_WWW, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_FINGER, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_IRC, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_STTALK, DHCP_OPTION_IP, 1, -1 , FALSE}, + {DHCP4_TAG_STDA, DHCP_OPTION_IP, 1, -1 , FALSE}, + + {DHCP4_TAG_CLASSLESS_ROUTE,DHCP_OPTION_INT8, 5, -1 , FALSE}, +}; + + +/** + Binary search the DhcpOptionFormats array to find the format + information about a specific option. + + @param[in] Tag The option's tag. + + @return The point to the option's format, NULL if not found. + +**/ +DHCP_OPTION_FORMAT * +DhcpFindOptionFormat ( + IN UINT8 Tag + ) +{ + INTN Left; + INTN Right; + INTN Middle; + + Left = 0; + Right = sizeof (DhcpOptionFormats) / sizeof (DHCP_OPTION_FORMAT) - 1; + + while (Right >= Left) { + Middle = (Left + Right) / 2; + + if (Tag == DhcpOptionFormats[Middle].Tag) { + return &DhcpOptionFormats[Middle]; + } + + if (Tag < DhcpOptionFormats[Middle].Tag) { + Right = Middle - 1; + } else { + Left = Middle + 1; + } + } + + return NULL; +} + + +/** + Validate whether a single DHCP option is valid according to its format. + + @param[in] Format The option's format + @param[in] OptValue The value of the option + @param[in] Len The length of the option value + + @retval TRUE The option is valid. + @retval FALSE Otherwise. + +**/ +BOOLEAN +DhcpOptionIsValid ( + IN DHCP_OPTION_FORMAT *Format, + IN UINT8 *OptValue, + IN INTN Len + ) +{ + INTN Unit; + INTN Occur; + INTN Index; + + Unit = 0; + + switch (Format->Type) { + case DHCP_OPTION_SWITCH: + case DHCP_OPTION_INT8: + Unit = 1; + break; + + case DHCP_OPTION_INT16: + Unit = 2; + break; + + case DHCP_OPTION_INT32: + case DHCP_OPTION_IP: + Unit = 4; + break; + + case DHCP_OPTION_IPPAIR: + Unit = 8; + break; + } + + ASSERT (Unit != 0); + + // + // Validate that the option appears in the full units. + // + if ((Len % Unit) != 0) { + return FALSE; + } + + // + // Validate the occurance of the option unit is with in [MinOccur, MaxOccur] + // + Occur = Len / Unit; + + if (((Format->MinOccur != -1) && (Occur < Format->MinOccur)) || + ((Format->MaxOccur != -1) && (Occur > Format->MaxOccur)) + ) { + return FALSE; + } + + // + // If the option is of type switch, only 0/1 are valid values. + // + if (Format->Type == DHCP_OPTION_SWITCH) { + for (Index = 0; Index < Occur; Index++) { + if ((OptValue[Index] != 0) && (OptValue[Index] != 1)) { + return FALSE; + } + } + } + + return TRUE; +} + + +/** + Extract the client interested options, all the parameters are + converted to host byte order. + + @param[in] Tag The DHCP option tag + @param[in] Len The length of the option + @param[in] Data The value of the DHCP option + @param[out] Para The variable to save the interested parameter + + @retval EFI_SUCCESS The DHCP option is successfully extracted. + @retval EFI_INVALID_PARAMETER The DHCP option is mal-formated + +**/ +EFI_STATUS +DhcpGetParameter ( + IN UINT8 Tag, + IN INTN Len, + IN UINT8 *Data, + OUT DHCP_PARAMETER *Para + ) +{ + switch (Tag) { + case DHCP4_TAG_NETMASK: + Para->NetMask = NetGetUint32 (Data); + break; + + case DHCP4_TAG_ROUTER: + // + // Return the first router to consumer which is the preferred one + // + Para->Router = NetGetUint32 (Data); + break; + + case DHCP4_TAG_LEASE: + Para->Lease = NetGetUint32 (Data); + break; + + case DHCP4_TAG_OVERLOAD: + Para->Overload = *Data; + + if ((Para->Overload < 1) || (Para->Overload > 3)) { + return EFI_INVALID_PARAMETER; + } + break; + + case DHCP4_TAG_MSG_TYPE: + Para->DhcpType = *Data; + + if ((Para->DhcpType < 1) || (Para->DhcpType > 9)) { + return EFI_INVALID_PARAMETER; + } + break; + + case DHCP4_TAG_SERVER_ID: + Para->ServerId = NetGetUint32 (Data); + break; + + case DHCP4_TAG_T1: + Para->T1 = NetGetUint32 (Data); + break; + + case DHCP4_TAG_T2: + Para->T2 = NetGetUint32 (Data); + break; + } + + return EFI_SUCCESS; +} + + +/** + Inspect all the options in a single buffer. DHCP options may be contained + in several buffers, such as the BOOTP options filed, boot file or server + name. Each option buffer is required to end with DHCP4_TAG_EOP. + + @param[in] Buffer The buffer which contains DHCP options + @param[in] BufLen The length of the buffer + @param[in] Check The callback function for each option found + @param[in] Context The opaque parameter for the Check + @param[out] Overload Variable to save the value of DHCP4_TAG_OVERLOAD + option. + + @retval EFI_SUCCESS All the options are valid + @retval EFI_INVALID_PARAMETER The options are mal-formated. + +**/ +EFI_STATUS +DhcpIterateBufferOptions ( + IN UINT8 *Buffer, + IN INTN BufLen, + IN DHCP_CHECK_OPTION Check OPTIONAL, + IN VOID *Context, + OUT UINT8 *Overload OPTIONAL + ) +{ + INTN Cur; + UINT8 Tag; + UINT8 Len; + + Cur = 0; + + while (Cur < BufLen) { + Tag = Buffer[Cur]; + + if (Tag == DHCP4_TAG_PAD) { + Cur++; + continue; + } else if (Tag == DHCP4_TAG_EOP) { + return EFI_SUCCESS; + } + + Cur++; + + if (Cur == BufLen) { + return EFI_INVALID_PARAMETER; + } + + Len = Buffer[Cur++]; + + if (Cur + Len > BufLen) { + return EFI_INVALID_PARAMETER; + } + + if ((Tag == DHCP4_TAG_OVERLOAD) && (Overload != NULL)) { + if (Len != 1) { + return EFI_INVALID_PARAMETER; + } + + *Overload = Buffer[Cur]; + } + + if ((Check != NULL) && EFI_ERROR (Check (Tag, Len, Buffer + Cur, Context))) { + return EFI_INVALID_PARAMETER; + } + + Cur += Len; + } + + // + // Each option buffer is expected to end with an EOP + // + return EFI_INVALID_PARAMETER; +} + + +/** + Iterate through a DHCP message to visit each option. First inspect + all the options in the OPTION field. Then if overloaded, inspect + the options in FILENAME and SERVERNAME fields. One option may be + encoded in several places. See RFC 3396 Encoding Long Options in DHCP + + @param[in] Packet The DHCP packet to check the options for + @param[in] Check The callback function to be called for each option + found + @param[in] Context The opaque parameter for Check + + @retval EFI_SUCCESS The DHCP packet's options are well formated + @retval EFI_INVALID_PARAMETER The DHCP packet's options are not well formated + +**/ +EFI_STATUS +DhcpIterateOptions ( + IN EFI_DHCP4_PACKET *Packet, + IN DHCP_CHECK_OPTION Check OPTIONAL, + IN VOID *Context + ) +{ + EFI_STATUS Status; + UINT8 Overload; + + Overload = 0; + + Status = DhcpIterateBufferOptions ( + Packet->Dhcp4.Option, + Packet->Length - sizeof (EFI_DHCP4_HEADER) - sizeof (UINT32), + Check, + Context, + &Overload + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + if ((Overload == DHCP_OVERLOAD_FILENAME) || (Overload == DHCP_OVERLOAD_BOTH)) { + Status = DhcpIterateBufferOptions ( + (UINT8 *) Packet->Dhcp4.Header.BootFileName, + 128, + Check, + Context, + NULL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + } + + if ((Overload == DHCP_OVERLOAD_SVRNAME) || (Overload == DHCP_OVERLOAD_BOTH)) { + Status = DhcpIterateBufferOptions ( + (UINT8 *) Packet->Dhcp4.Header.ServerName, + 64, + Check, + Context, + NULL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + } + + return EFI_SUCCESS; +} + + +/** + Call back function to DhcpIterateOptions to compute each option's + length. It just adds the data length of all the occurances of this + Tag. Context is an array of 256 DHCP_OPTION_COUNT. + + @param[in] Tag The current option to check + @param[in] Len The length of the option data + @param[in] Data The option data + @param[in] Context The context, which is a array of 256 + DHCP_OPTION_COUNT. + + @retval EFI_SUCCESS It always returns EFI_SUCCESS. + +**/ +EFI_STATUS +DhcpGetOptionLen ( + IN UINT8 Tag, + IN UINT8 Len, + IN UINT8 *Data, + IN VOID *Context + ) +{ + DHCP_OPTION_COUNT *OpCount; + + OpCount = (DHCP_OPTION_COUNT *) Context; + OpCount[Tag].Offset = (UINT16) (OpCount[Tag].Offset + Len); + + return EFI_SUCCESS; +} + + +/** + Call back function to DhcpIterateOptions to consolidate each option's + data. There are maybe several occurrence of the same option. + + @param[in] Tag The option to consolidate its data + @param[in] Len The length of option data + @param[in] Data The data of the option's current occurance + @param[in] Context The context, which is DHCP_OPTION_CONTEXT. This + array is just a wrap to pass THREE parameters. + + @retval EFI_SUCCESS It always returns EFI_SUCCESS + +**/ +EFI_STATUS +DhcpFillOption ( + IN UINT8 Tag, + IN UINT8 Len, + IN UINT8 *Data, + IN VOID *Context + ) +{ + DHCP_OPTION_CONTEXT *OptContext; + DHCP_OPTION_COUNT *OptCount; + DHCP_OPTION *Options; + UINT8 *Buf; + UINT8 Index; + + OptContext = (DHCP_OPTION_CONTEXT *) Context; + + OptCount = OptContext->OpCount; + Index = OptCount[Tag].Index; + Options = OptContext->Options; + Buf = OptContext->Buf; + + if (Options[Index].Data == NULL) { + Options[Index].Tag = Tag; + Options[Index].Data = Buf + OptCount[Tag].Offset; + } + + CopyMem (Buf + OptCount[Tag].Offset, Data, Len); + + OptCount[Tag].Offset = (UINT16) (OptCount[Tag].Offset + Len); + Options[Index].Len = (UINT16) (Options[Index].Len + Len); + return EFI_SUCCESS; +} + + +/** + Parse the options of a DHCP packet. It supports RFC 3396: Encoding + Long Options in DHCP. That is, it will combine all the option value + of all the occurances of each option. + A little bit of implemenation: + It adopts the "Key indexed counting" algorithm. First, it allocates + an array of 256 DHCP_OPTION_COUNTs because DHCP option tag is encoded + as a UINT8. It then iterates the DHCP packet to get data length of + each option by calling DhcpIterOptions with DhcpGetOptionLen. Now, it + knows the number of present options and their length. It allocates a + array of DHCP_OPTION and a continuous buffer after the array to put + all the options' data. Each option's data is pointed to by the Data + field in DHCP_OPTION structure. At last, it call DhcpIterateOptions + with DhcpFillOption to fill each option's data to its position in the + buffer. + + @param[in] Packet The DHCP packet to parse the options + @param[out] Count The number of valid dhcp options present in the + packet + @param[out] OptionPoint The array that contains the DHCP options. Caller + should free it. + + @retval EFI_NOT_FOUND Cannot find any option. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory to parse the packet. + @retval EFI_INVALID_PARAMETER The options are mal-formated + @retval EFI_SUCCESS The options are parsed into OptionPoint + +**/ +EFI_STATUS +DhcpParseOption ( + IN EFI_DHCP4_PACKET *Packet, + OUT INTN *Count, + OUT DHCP_OPTION **OptionPoint + ) +{ + DHCP_OPTION_CONTEXT Context; + DHCP_OPTION *Options; + DHCP_OPTION_COUNT *OptCount; + EFI_STATUS Status; + UINT16 TotalLen; + INTN OptNum; + INTN Index; + + ASSERT ((Count != NULL) && (OptionPoint != NULL)); + + // + // First compute how many options and how long each option is + // with the "Key indexed counting" algorithms. + // + OptCount = AllocateZeroPool (DHCP_MAX_OPTIONS * sizeof (DHCP_OPTION_COUNT)); + + if (OptCount == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = DhcpIterateOptions (Packet, DhcpGetOptionLen, OptCount); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Before the loop, Offset is the length of the option. After loop, + // OptCount[Index].Offset specifies the offset into the continuous + // option value buffer to put the data. + // + TotalLen = 0; + OptNum = 0; + + for (Index = 0; Index < DHCP_MAX_OPTIONS; Index++) { + if (OptCount[Index].Offset != 0) { + OptCount[Index].Index = (UINT8) OptNum; + + TotalLen = (UINT16) (TotalLen + OptCount[Index].Offset); + OptCount[Index].Offset = (UINT16) (TotalLen - OptCount[Index].Offset); + + OptNum++; + } + } + + *Count = OptNum; + *OptionPoint = NULL; + + if (OptNum == 0) { + goto ON_EXIT; + } + + // + // Allocate a buffer to hold the DHCP options, and after that, a + // continuous buffer to put all the options' data. + // + Options = AllocateZeroPool ((UINTN) (OptNum * sizeof (DHCP_OPTION)) + TotalLen); + + if (Options == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Context.OpCount = OptCount; + Context.Options = Options; + Context.Buf = (UINT8 *) (Options + OptNum); + + Status = DhcpIterateOptions (Packet, DhcpFillOption, &Context); + + if (EFI_ERROR (Status)) { + FreePool (Options); + goto ON_EXIT; + } + + *OptionPoint = Options; + +ON_EXIT: + FreePool (OptCount); + return Status; +} + + +/** + Validate the packet's options. If necessary, allocate + and fill in the interested parameters. + + @param[in] Packet The packet to validate the options + @param[out] Para The variable to save the DHCP parameters. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory to validate the packet. + @retval EFI_INVALID_PARAMETER The options are mal-formated + @retval EFI_SUCCESS The options are parsed into OptionPoint + +**/ +EFI_STATUS +DhcpValidateOptions ( + IN EFI_DHCP4_PACKET *Packet, + OUT DHCP_PARAMETER **Para OPTIONAL + ) +{ + DHCP_PARAMETER Parameter; + DHCP_OPTION_FORMAT *Format; + DHCP_OPTION *AllOption; + DHCP_OPTION *Option; + EFI_STATUS Status; + BOOLEAN Updated; + INTN Count; + INTN Index; + + if (Para != NULL) { + *Para = NULL; + } + + AllOption = NULL; + + Status = DhcpParseOption (Packet, &Count, &AllOption); + if (EFI_ERROR (Status) || (Count == 0)) { + return Status; + } + ASSERT (AllOption != NULL); + + Updated = FALSE; + ZeroMem (&Parameter, sizeof (Parameter)); + + for (Index = 0; Index < Count; Index++) { + Option = &AllOption[Index]; + + // + // Find the format of the option then validate it. + // + Format = DhcpFindOptionFormat (Option->Tag); + + if (Format == NULL) { + continue; + } + + if (!DhcpOptionIsValid (Format, Option->Data, Option->Len)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + // + // Get the client interested parameters + // + if (Format->Alert && (Para != NULL)) { + Updated = TRUE; + Status = DhcpGetParameter (Option->Tag, Option->Len, Option->Data, &Parameter); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + } + + if (Updated && (Para != NULL)) { + *Para = AllocateCopyPool (sizeof (DHCP_PARAMETER), &Parameter); + if (*Para == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + } + +ON_EXIT: + FreePool (AllOption); + return Status; +} + + + +/** + Append an option to the memory, if the option is longer than + 255 bytes, splits it into several options. + + @param[out] Buf The buffer to append the option to + @param[in] Tag The option's tag + @param[in] DataLen The length of the option's data + @param[in] Data The option's data + + @return The position to append the next option + +**/ +UINT8 * +DhcpAppendOption ( + OUT UINT8 *Buf, + IN UINT8 Tag, + IN UINT16 DataLen, + IN UINT8 *Data + ) +{ + INTN Index; + INTN Len; + + ASSERT (DataLen != 0); + + for (Index = 0; Index < (DataLen + 254) / 255; Index++) { + Len = MIN (255, DataLen - Index * 255); + + *(Buf++) = Tag; + *(Buf++) = (UINT8) Len; + CopyMem (Buf, Data + Index * 255, (UINTN) Len); + + Buf += Len; + } + + return Buf; +} + + +/** + Build a new DHCP packet from a seed packet. Options may be deleted or + appended. The caller should free the NewPacket when finished using it. + + @param[in] SeedPacket The seed packet to start with + @param[in] DeleteCount The number of options to delete + @param[in] DeleteList The options to delete from the packet + @param[in] AppendCount The number of options to append + @param[in] AppendList The options to append to the packet + @param[out] NewPacket The new packet, allocated and built by this + function. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory + @retval EFI_INVALID_PARAMETER The options in SeekPacket are mal-formated + @retval EFI_SUCCESS The packet is build. + +**/ +EFI_STATUS +DhcpBuild ( + IN EFI_DHCP4_PACKET *SeedPacket, + IN UINT32 DeleteCount, + IN UINT8 *DeleteList OPTIONAL, + IN UINT32 AppendCount, + IN EFI_DHCP4_PACKET_OPTION *AppendList[] OPTIONAL, + OUT EFI_DHCP4_PACKET **NewPacket + ) +{ + DHCP_OPTION *Mark; + DHCP_OPTION *SeedOptions; + EFI_DHCP4_PACKET *Packet; + EFI_STATUS Status; + INTN Count; + UINT32 Index; + UINT32 Len; + UINT8 *Buf; + + // + // Use an array of DHCP_OPTION to mark the existance + // and position of each valid options. + // + Mark = AllocatePool (sizeof (DHCP_OPTION) * DHCP_MAX_OPTIONS); + + if (Mark == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + for (Index = 0; Index < DHCP_MAX_OPTIONS; Index++) { + Mark[Index].Tag = (UINT8) Index; + Mark[Index].Len = 0; + } + + // + // Get list of the options from the seed packet, then put + // them to the mark array according to their tags. + // + SeedOptions = NULL; + Status = DhcpParseOption (SeedPacket, &Count, &SeedOptions); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + if (SeedOptions != NULL) { + for (Index = 0; Index < (UINT32) Count; Index++) { + Mark[SeedOptions[Index].Tag] = SeedOptions[Index]; + } + } + + // + // Mark the option's length is zero if it is in the DeleteList. + // + for (Index = 0; Index < DeleteCount; Index++) { + Mark[DeleteList[Index]].Len = 0; + } + + // + // Add or replace the option if it is in the append list. + // + for (Index = 0; Index < AppendCount; Index++) { + Mark[AppendList[Index]->OpCode].Len = AppendList[Index]->Length; + Mark[AppendList[Index]->OpCode].Data = AppendList[Index]->Data; + } + + // + // compute the new packet length. No need to add 1 byte for + // EOP option since EFI_DHCP4_PACKET includes one extra byte + // for option. It is necessary to split the option if it is + // longer than 255 bytes. + // + Len = sizeof (EFI_DHCP4_PACKET); + + for (Index = 0; Index < DHCP_MAX_OPTIONS; Index++) { + if (Mark[Index].Len != 0) { + Len += ((Mark[Index].Len + 254) / 255) * 2 + Mark[Index].Len; + } + } + + Status = EFI_OUT_OF_RESOURCES; + Packet = (EFI_DHCP4_PACKET *) AllocatePool (Len); + + if (Packet == NULL) { + goto ON_ERROR; + } + + Packet->Size = Len; + Packet->Length = 0; + CopyMem (&Packet->Dhcp4.Header, &SeedPacket->Dhcp4.Header, sizeof (Packet->Dhcp4.Header)); + Packet->Dhcp4.Magik = DHCP_OPTION_MAGIC; + Buf = Packet->Dhcp4.Option; + + for (Index = 0; Index < DHCP_MAX_OPTIONS; Index++) { + if (Mark[Index].Len != 0) { + Buf = DhcpAppendOption (Buf, Mark[Index].Tag, Mark[Index].Len, Mark[Index].Data); + } + } + + *(Buf++) = DHCP4_TAG_EOP; + Packet->Length = sizeof (EFI_DHCP4_HEADER) + sizeof (UINT32) + + (UINT32) (Buf - Packet->Dhcp4.Option); + + *NewPacket = Packet; + Status = EFI_SUCCESS; + +ON_ERROR: + if (SeedOptions != NULL) { + FreePool (SeedOptions); + } + + FreePool (Mark); + return Status; +} diff --git a/NetworkPkg/Dhcp4Dxe/Dhcp4Option.h b/NetworkPkg/Dhcp4Dxe/Dhcp4Option.h new file mode 100644 index 000000000..9e2538650 --- /dev/null +++ b/NetworkPkg/Dhcp4Dxe/Dhcp4Option.h @@ -0,0 +1,228 @@ +/** @file + To validate, parse and process the DHCP options. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_DHCP4_OPTION_H__ +#define __EFI_DHCP4_OPTION_H__ + +/// +/// DHCP option tags (types) +/// + +#define DHCP_OPTION_MAGIC 0x63538263 // Network byte order +#define DHCP_MAX_OPTIONS 256 + + +// +// DHCP option types, this is used to validate the DHCP options. +// +#define DHCP_OPTION_SWITCH 1 +#define DHCP_OPTION_INT8 2 +#define DHCP_OPTION_INT16 3 +#define DHCP_OPTION_INT32 4 +#define DHCP_OPTION_IP 5 +#define DHCP_OPTION_IPPAIR 6 + +// +// Value of DHCP overload option +// +#define DHCP_OVERLOAD_FILENAME 1 +#define DHCP_OVERLOAD_SVRNAME 2 +#define DHCP_OVERLOAD_BOTH 3 + +/// +/// The DHCP option structure. This structure extends the EFI_DHCP_OPTION +/// structure to support options longer than 255 bytes, such as classless route. +/// +typedef struct { + UINT8 Tag; + UINT16 Len; + UINT8 *Data; +} DHCP_OPTION; + +/// +/// Structures used to parse the DHCP options with RFC3396 support. +/// +typedef struct { + UINT8 Index; + UINT16 Offset; +} DHCP_OPTION_COUNT; + +typedef struct { + DHCP_OPTION_COUNT *OpCount; + DHCP_OPTION *Options; + UINT8 *Buf; +} DHCP_OPTION_CONTEXT; + +/// +/// The options that matters to DHCP driver itself. The user of +/// DHCP clients may be interested in other options, such as +/// classless route, who can parse the DHCP offer to get them. +/// +typedef struct { + IP4_ADDR NetMask; // DHCP4_TAG_NETMASK + IP4_ADDR Router; // DHCP4_TAG_ROUTER, only the first router is used + + // + // DHCP specific options + // + UINT8 DhcpType; // DHCP4_TAG_MSG_TYPE + UINT8 Overload; // DHCP4_TAG_OVERLOAD + IP4_ADDR ServerId; // DHCP4_TAG_SERVER_ID + UINT32 Lease; // DHCP4_TAG_LEASE + UINT32 T1; // DHCP4_TAG_T1 + UINT32 T2; // DHCP4_TAG_T2 +} DHCP_PARAMETER; + +/// +/// Structure used to describe and validate the format of DHCP options. +/// Type is the options' data type, such as DHCP_OPTION_INT8. MinOccur +/// is the minium occurance of this data type. MaxOccur is defined +/// similarly. If MaxOccur is -1, it means that there is no limit on the +/// maximum occurance. Alert tells whether DHCP client should further +/// inspect the option to parse DHCP_PARAMETER. +/// +typedef struct { + UINT8 Tag; + INTN Type; + INTN MinOccur; + INTN MaxOccur; + BOOLEAN Alert; +} DHCP_OPTION_FORMAT; + +typedef +EFI_STATUS +(*DHCP_CHECK_OPTION) ( + IN UINT8 Tag, + IN UINT8 Len, + IN UINT8 *Data, + IN VOID *Context + ); + +/** + Iterate through a DHCP message to visit each option. First inspect + all the options in the OPTION field. Then if overloaded, inspect + the options in FILENAME and SERVERNAME fields. One option may be + encoded in several places. See RFC 3396 Encoding Long Options in DHCP + + @param[in] Packet The DHCP packet to check the options for + @param[in] Check The callback function to be called for each option + found + @param[in] Context The opaque parameter for Check + + @retval EFI_SUCCESS The DHCP packet's options are well formated + @retval EFI_INVALID_PARAMETER The DHCP packet's options are not well formated + +**/ +EFI_STATUS +DhcpIterateOptions ( + IN EFI_DHCP4_PACKET *Packet, + IN DHCP_CHECK_OPTION Check OPTIONAL, + IN VOID *Context + ); + +/** + Validate the packet's options. If necessary, allocate + and fill in the interested parameters. + + @param[in] Packet The packet to validate the options + @param[out] Para The variable to save the DHCP parameters. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory to validate the packet. + @retval EFI_INVALID_PARAMETER The options are mal-formated + @retval EFI_SUCCESS The options are parsed into OptionPoint + +**/ +EFI_STATUS +DhcpValidateOptions ( + IN EFI_DHCP4_PACKET *Packet, + OUT DHCP_PARAMETER **Para OPTIONAL + ); + +/** + Parse the options of a DHCP packet. It supports RFC 3396: Encoding + Long Options in DHCP. That is, it will combine all the option value + of all the occurances of each option. + A little bit of implemenation: + It adopts the "Key indexed counting" algorithm. First, it allocates + an array of 256 DHCP_OPTION_COUNTs because DHCP option tag is encoded + as a UINT8. It then iterates the DHCP packet to get data length of + each option by calling DhcpIterOptions with DhcpGetOptionLen. Now, it + knows the number of present options and their length. It allocates a + array of DHCP_OPTION and a continuous buffer after the array to put + all the options' data. Each option's data is pointed to by the Data + field in DHCP_OPTION structure. At last, it call DhcpIterateOptions + with DhcpFillOption to fill each option's data to its position in the + buffer. + + @param[in] Packet The DHCP packet to parse the options + @param[out] Count The number of valid dhcp options present in the + packet + @param[out] OptionPoint The array that contains the DHCP options. Caller + should free it. + + @retval EFI_NOT_FOUND Cannot find any option. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory to parse the packet. + @retval EFI_INVALID_PARAMETER The options are mal-formated + @retval EFI_SUCCESS The options are parsed into OptionPoint + +**/ +EFI_STATUS +DhcpParseOption ( + IN EFI_DHCP4_PACKET *Packet, + OUT INTN *Count, + OUT DHCP_OPTION **OptionPoint + ); + +/** + Append an option to the memory, if the option is longer than + 255 bytes, splits it into several options. + + @param[out] Buf The buffer to append the option to + @param[in] Tag The option's tag + @param[in] DataLen The length of the option's data + @param[in] Data The option's data + + @return The position to append the next option + +**/ +UINT8 * +DhcpAppendOption ( + OUT UINT8 *Buf, + IN UINT8 Tag, + IN UINT16 DataLen, + IN UINT8 *Data + ); + +/** + Build a new DHCP packet from a seed packet. Options may be deleted or + appended. The caller should free the NewPacket when finished using it. + + @param[in] SeedPacket The seed packet to start with + @param[in] DeleteCount The number of options to delete + @param[in] DeleteList The options to delete from the packet + @param[in] AppendCount The number of options to append + @param[in] AppendList The options to append to the packet + @param[out] NewPacket The new packet, allocated and built by this + function. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory + @retval EFI_INVALID_PARAMETER The options in SeekPacket are mal-formated + @retval EFI_SUCCESS The packet is build. + +**/ +EFI_STATUS +DhcpBuild ( + IN EFI_DHCP4_PACKET *SeedPacket, + IN UINT32 DeleteCount, + IN UINT8 *DeleteList OPTIONAL, + IN UINT32 AppendCount, + IN EFI_DHCP4_PACKET_OPTION *AppendList[] OPTIONAL, + OUT EFI_DHCP4_PACKET **NewPacket + ); + +#endif diff --git a/NetworkPkg/Dhcp6Dxe/ComponentName.c b/NetworkPkg/Dhcp6Dxe/ComponentName.c new file mode 100644 index 000000000..c5927fbd1 --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/ComponentName.c @@ -0,0 +1,442 @@ +/** @file + UEFI Component Name(2) protocol implementation for Dhcp6 driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Dhcp6Impl.h" + + +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Dhcp6ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is being managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that attempt to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that attempts to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language, from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Dhcp6ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gDhcp6ComponentName = { + Dhcp6ComponentNameGetDriverName, + Dhcp6ComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gDhcp6ComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) Dhcp6ComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) Dhcp6ComponentNameGetControllerName, + "en" +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mDhcp6DriverNameTable[] = { + { + "eng;en", + L"DHCP6 Protocol Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gDhcp6ControllerNameTable = NULL; + +CHAR16 *mDhcp6ControllerName[] = { + L"DHCPv6 (State=0, Init)", + L"DHCPv6 (State=1, Selecting)", + L"DHCPv6 (State=2, Requesting)", + L"DHCPv6 (State=3, Declining)", + L"DHCPv6 (State=4, Confirming)", + L"DHCPv6 (State=5, Releasing)", + L"DHCPv6 (State=6, Bound)", + L"DHCPv6 (State=7, Renewing)", + L"DHCPv6 (State=8, Rebinding)" +}; + +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Dhcp6ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mDhcp6DriverNameTable, + DriverName, + (BOOLEAN)(This == &gDhcp6ComponentName) + ); +} + +/** + Update the component name for the Dhcp6 child handle. + + @param Dhcp6[in] A pointer to the EFI_DHCP6_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateName ( + IN EFI_DHCP6_PROTOCOL *Dhcp6 + ) +{ + EFI_STATUS Status; + EFI_DHCP6_MODE_DATA Dhcp6ModeData; + CHAR16 *HandleName; + + if (Dhcp6 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer. + // + Status = Dhcp6->GetModeData (Dhcp6, &Dhcp6ModeData, NULL); + if (EFI_ERROR (Status)) { + return Status; + } + + if (gDhcp6ControllerNameTable != NULL) { + FreeUnicodeStringTable (gDhcp6ControllerNameTable); + gDhcp6ControllerNameTable = NULL; + } + + if (Dhcp6ModeData.Ia == NULL) { + HandleName = L"DHCPv6 (No configured IA)"; + } else { + if (Dhcp6ModeData.Ia->State > Dhcp6Rebinding) { + return EFI_DEVICE_ERROR; + } + HandleName = mDhcp6ControllerName[Dhcp6ModeData.Ia->State]; + } + + if (Dhcp6ModeData.Ia != NULL) { + FreePool (Dhcp6ModeData.Ia); + } + if (Dhcp6ModeData.ClientId != NULL) { + FreePool (Dhcp6ModeData.ClientId); + } + + Status = AddUnicodeString2 ( + "eng", + gDhcp6ComponentName.SupportedLanguages, + &gDhcp6ControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gDhcp6ComponentName2.SupportedLanguages, + &gDhcp6ControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is being managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in the + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language, from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Dhcp6ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_DHCP6_PROTOCOL *Dhcp6; + + // + // Only provide names for child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiUdp6ProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiDhcp6ProtocolGuid, + (VOID **)&Dhcp6, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateName (Dhcp6); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gDhcp6ControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gDhcp6ComponentName) + ); +} + diff --git a/NetworkPkg/Dhcp6Dxe/Dhcp6Driver.c b/NetworkPkg/Dhcp6Dxe/Dhcp6Driver.c new file mode 100644 index 000000000..2ffdba73a --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/Dhcp6Driver.c @@ -0,0 +1,813 @@ +/** @file + Driver Binding functions and Service Binding functions + implementationfor for Dhcp6 Driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Dhcp6Impl.h" + + +EFI_DRIVER_BINDING_PROTOCOL gDhcp6DriverBinding = { + Dhcp6DriverBindingSupported, + Dhcp6DriverBindingStart, + Dhcp6DriverBindingStop, + 0xa, + NULL, + NULL +}; + +EFI_SERVICE_BINDING_PROTOCOL gDhcp6ServiceBindingTemplate = { + Dhcp6ServiceBindingCreateChild, + Dhcp6ServiceBindingDestroyChild +}; + +/** + Configure the default Udp6Io to receive all the DHCP6 traffic + on this network interface. + + @param[in] UdpIo The pointer to Udp6Io to be configured. + @param[in] Context The pointer to the context. + + @retval EFI_SUCCESS The Udp6Io is successfully configured. + @retval Others Failed to configure the Udp6Io. + +**/ +EFI_STATUS +EFIAPI +Dhcp6ConfigureUdpIo ( + IN UDP_IO *UdpIo, + IN VOID *Context + ) +{ + EFI_UDP6_PROTOCOL *Udp6; + EFI_UDP6_CONFIG_DATA *Config; + + Udp6 = UdpIo->Protocol.Udp6; + Config = &(UdpIo->Config.Udp6); + + ZeroMem (Config, sizeof (EFI_UDP6_CONFIG_DATA)); + + // + // Set Udp6 configure data for the Dhcp6 instance. + // + Config->AcceptPromiscuous = FALSE; + Config->AcceptAnyPort = FALSE; + Config->AllowDuplicatePort = FALSE; + Config->TrafficClass = 0; + Config->HopLimit = 128; + Config->ReceiveTimeout = 0; + Config->TransmitTimeout = 0; + + // + // Configure an endpoint of client(0, 546), server(0, 0), the addresses + // will be overridden later. Note that we MUST not limit RemotePort. + // More details, refer to RFC 3315 section 5.2. + // + Config->StationPort = DHCP6_PORT_CLIENT; + Config->RemotePort = 0; + + return Udp6->Configure (Udp6, Config);; +} + + +/** + Destroy the Dhcp6 service. The Dhcp6 service may be partly initialized, + or partly destroyed. If a resource is destroyed, it is marked as such in + case the destroy failed and being called again later. + + @param[in, out] Service The pointer to Dhcp6 service to be destroyed. + +**/ +VOID +Dhcp6DestroyService ( + IN OUT DHCP6_SERVICE *Service + ) +{ + // + // All children instances should have been already destroyed here. + // + ASSERT (Service->NumOfChild == 0); + + if (Service->ClientId != NULL) { + FreePool (Service->ClientId); + } + + if (Service->UdpIo != NULL) { + UdpIoFreeIo (Service->UdpIo); + } + + FreePool (Service); +} + + +/** + Create a new Dhcp6 service for the Nic controller. + + @param[in] Controller The controller to be installed DHCP6 service + binding protocol. + @param[in] ImageHandle The image handle of the Dhcp6 driver. + @param[out] Service The return pointer of the new Dhcp6 service. + + @retval EFI_SUCCESS The Dhcp6 service is created successfully. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource. + +**/ +EFI_STATUS +Dhcp6CreateService ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE ImageHandle, + OUT DHCP6_SERVICE **Service + ) +{ + DHCP6_SERVICE *Dhcp6Srv; + EFI_STATUS Status; + + *Service = NULL; + Dhcp6Srv = AllocateZeroPool (sizeof (DHCP6_SERVICE)); + + if (Dhcp6Srv == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Open the SNP protocol to get mode data later. + // + Dhcp6Srv->Snp = NULL; + NetLibGetSnpHandle (Controller, &Dhcp6Srv->Snp); + if (Dhcp6Srv->Snp == NULL) { + FreePool (Dhcp6Srv); + return EFI_DEVICE_ERROR; + } + + // + // Initialize the fields of the new Dhcp6 service. + // + Dhcp6Srv->Signature = DHCP6_SERVICE_SIGNATURE; + Dhcp6Srv->Controller = Controller; + Dhcp6Srv->Image = ImageHandle; + Dhcp6Srv->Xid = (0xffffff & NET_RANDOM (NetRandomInitSeed ())); + + CopyMem ( + &Dhcp6Srv->ServiceBinding, + &gDhcp6ServiceBindingTemplate, + sizeof (EFI_SERVICE_BINDING_PROTOCOL) + ); + + // + // Locate Ip6->Ip6Config and store it for get IP6 Duplicate Address Detection transmits. + // + Status = gBS->HandleProtocol ( + Controller, + &gEfiIp6ConfigProtocolGuid, + (VOID **) &Dhcp6Srv->Ip6Cfg + ); + if (EFI_ERROR (Status)) { + FreePool (Dhcp6Srv); + return Status; + } + + // + // Generate client Duid: If SMBIOS system UUID is located, generate DUID in DUID-UUID format. + // Otherwise, in DUID-LLT format. + // + Dhcp6Srv->ClientId = Dhcp6GenerateClientId (Dhcp6Srv->Snp->Mode); + + if (Dhcp6Srv->ClientId == NULL) { + FreePool (Dhcp6Srv); + return EFI_DEVICE_ERROR; + } + + // + // Create an Udp6Io for stateful transmit/receive of each Dhcp6 instance. + // + Dhcp6Srv->UdpIo = UdpIoCreateIo ( + Controller, + ImageHandle, + Dhcp6ConfigureUdpIo, + UDP_IO_UDP6_VERSION, + NULL + ); + + if (Dhcp6Srv->UdpIo == NULL) { + FreePool (Dhcp6Srv->ClientId); + FreePool (Dhcp6Srv); + return EFI_DEVICE_ERROR; + } + + InitializeListHead (&Dhcp6Srv->Child); + + *Service = Dhcp6Srv; + + return EFI_SUCCESS; +} + + +/** + Destroy the Dhcp6 instance and recycle the resources. + + @param[in, out] Instance The pointer to the Dhcp6 instance. + +**/ +VOID +Dhcp6DestroyInstance ( + IN OUT DHCP6_INSTANCE *Instance + ) +{ + // + // Clean up the retry list first. + // + Dhcp6CleanupRetry (Instance, DHCP6_PACKET_ALL); + gBS->CloseEvent (Instance->Timer); + + // + // Clean up the current configure data. + // + if (Instance->Config != NULL) { + Dhcp6CleanupConfigData (Instance->Config); + FreePool (Instance->Config); + } + + // + // Clean up the current Ia. + // + if (Instance->IaCb.Ia != NULL) { + if (Instance->IaCb.Ia->ReplyPacket != NULL) { + FreePool (Instance->IaCb.Ia->ReplyPacket); + } + FreePool (Instance->IaCb.Ia); + } + + if (Instance->Unicast != NULL) { + FreePool (Instance->Unicast); + } + + if (Instance->AdSelect != NULL) { + FreePool (Instance->AdSelect); + } + + FreePool (Instance); +} + + +/** + Create the Dhcp6 instance and initialize it. + + @param[in] Service The pointer to the Dhcp6 service. + @param[out] Instance The pointer to the Dhcp6 instance. + + @retval EFI_SUCCESS The Dhcp6 instance is created. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + +**/ +EFI_STATUS +Dhcp6CreateInstance ( + IN DHCP6_SERVICE *Service, + OUT DHCP6_INSTANCE **Instance + ) +{ + EFI_STATUS Status; + DHCP6_INSTANCE *Dhcp6Ins; + + *Instance = NULL; + Dhcp6Ins = AllocateZeroPool (sizeof (DHCP6_INSTANCE)); + + if (Dhcp6Ins == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Initialize the fields of the new Dhcp6 instance. + // + Dhcp6Ins->Signature = DHCP6_INSTANCE_SIGNATURE; + Dhcp6Ins->UdpSts = EFI_ALREADY_STARTED; + Dhcp6Ins->Service = Service; + Dhcp6Ins->InDestroy = FALSE; + Dhcp6Ins->MediaPresent = TRUE; + + CopyMem ( + &Dhcp6Ins->Dhcp6, + &gDhcp6ProtocolTemplate, + sizeof (EFI_DHCP6_PROTOCOL) + ); + + InitializeListHead (&Dhcp6Ins->TxList); + InitializeListHead (&Dhcp6Ins->InfList); + + // + // There is a timer for each Dhcp6 instance, which is used to track the + // lease time of Ia and the retransmisson time of all sent packets. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + Dhcp6OnTimerTick, + Dhcp6Ins, + &Dhcp6Ins->Timer + ); + + if (EFI_ERROR (Status)) { + FreePool (Dhcp6Ins); + return Status; + } + + *Instance = Dhcp6Ins; + + return EFI_SUCCESS; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +Dhcp6DestroyChildEntry ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + DHCP6_INSTANCE *Instance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = NET_LIST_USER_STRUCT_S (Entry, DHCP6_INSTANCE, Link, DHCP6_INSTANCE_SIGNATURE); + ServiceBinding = (EFI_SERVICE_BINDING_PROTOCOL *) Context; + + return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle); +} + + +/** + Entry point of the DHCP6 driver to install various protocols. + + @param[in] ImageHandle The handle of the UEFI image file. + @param[in] SystemTable The pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval Others Unexpected error occurs. + +**/ +EFI_STATUS +EFIAPI +Dhcp6DriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gDhcp6DriverBinding, + ImageHandle, + &gDhcp6ComponentName, + &gDhcp6ComponentName2 + ); +} + + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be tested. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to be started. + + @retval EFI_SUCCESS This driver supports this device. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Dhcp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return gBS->OpenProtocol ( + ControllerHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); +} + + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be started. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to be started. + + @retval EFI_SUCCESS This driver is installed to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Dhcp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + DHCP6_SERVICE *Service; + + // + // Check the Dhcp6 serivce whether already started. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDhcp6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + // + // Create and initialize the Dhcp6 service. + // + Status = Dhcp6CreateService ( + ControllerHandle, + This->DriverBindingHandle, + &Service + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (Service != NULL); + + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiDhcp6ServiceBindingProtocolGuid, + &Service->ServiceBinding, + NULL + ); + + if (EFI_ERROR (Status)) { + Dhcp6DestroyService (Service); + return Status; + } + + return EFI_SUCCESS; +} + + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +Dhcp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_HANDLE NicHandle; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + DHCP6_SERVICE *Service; + LIST_ENTRY *List; + UINTN ListLength; + + // + // Find and check the Nic handle by the controller handle. + // + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp6ProtocolGuid); + + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiDhcp6ServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + NicHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + Service = DHCP6_SERVICE_FROM_THIS (ServiceBinding); + if (!IsListEmpty (&Service->Child)) { + // + // Destroy all the children instances before destory the service. + // + List = &Service->Child; + Status = NetDestroyLinkList ( + List, + Dhcp6DestroyChildEntry, + ServiceBinding, + &ListLength + ); + if (EFI_ERROR (Status) || ListLength != 0) { + Status = EFI_DEVICE_ERROR; + } + } + + if (NumberOfChildren == 0 && !IsListEmpty (&Service->Child)) { + Status = EFI_DEVICE_ERROR; + } + + if (NumberOfChildren == 0 && IsListEmpty (&Service->Child)) { + // + // Destroy the service itself if no child instance left. + // + Status = gBS->UninstallProtocolInterface ( + NicHandle, + &gEfiDhcp6ServiceBindingProtocolGuid, + ServiceBinding + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Dhcp6DestroyService (Service); + Status = EFI_SUCCESS; + } + +ON_EXIT: + return Status; +} + + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing + UEFI handle, then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +Dhcp6ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + DHCP6_SERVICE *Service; + DHCP6_INSTANCE *Instance; + VOID *Udp6; + + if (This == NULL || ChildHandle == NULL) { + return EFI_INVALID_PARAMETER; + } + + Service = DHCP6_SERVICE_FROM_THIS (This); + + Status = Dhcp6CreateInstance (Service, &Instance); + + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (Instance != NULL); + + // + // Start the timer when the instance is ready to use. + // + Status = gBS->SetTimer ( + Instance->Timer, + TimerPeriodic, + TICKS_PER_SECOND + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Install the DHCP6 protocol onto ChildHandle. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiDhcp6ProtocolGuid, + &Instance->Dhcp6, + NULL + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Instance->Handle = *ChildHandle; + + // + // Open the UDP6 protocol BY_CHILD. + // + Status = gBS->OpenProtocol ( + Service->UdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + (VOID **) &Udp6, + gDhcp6DriverBinding.DriverBindingHandle, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + + if (EFI_ERROR (Status)) { + + gBS->UninstallMultipleProtocolInterfaces ( + Instance->Handle, + &gEfiDhcp6ProtocolGuid, + &Instance->Dhcp6, + NULL + ); + goto ON_ERROR; + } + + // + // Add into the children list of its parent service. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&Service->Child, &Instance->Link); + Service->NumOfChild++; + + gBS->RestoreTPL (OldTpl); + return EFI_SUCCESS; + +ON_ERROR: + + Dhcp6DestroyInstance (Instance); + return Status; +} + + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Dhcp6ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + EFI_DHCP6_PROTOCOL *Dhcp6; + DHCP6_SERVICE *Service; + DHCP6_INSTANCE *Instance; + + if (This == NULL || ChildHandle == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Retrieve the private context data structures + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiDhcp6ProtocolGuid, + (VOID **) &Dhcp6, + gDhcp6DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Instance = DHCP6_INSTANCE_FROM_THIS (Dhcp6); + Service = DHCP6_SERVICE_FROM_THIS (This); + + if (Instance->Service != Service) { + return EFI_INVALID_PARAMETER; + } + + if (Instance->InDestroy) { + return EFI_SUCCESS; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance->InDestroy = TRUE; + + Status = gBS->CloseProtocol ( + Service->UdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + gDhcp6DriverBinding.DriverBindingHandle, + ChildHandle + ); + + if (EFI_ERROR (Status)) { + Instance->InDestroy = FALSE; + gBS->RestoreTPL (OldTpl); + return Status; + } + + // + // Uninstall the MTFTP6 protocol first to enable a top down destruction. + // + gBS->RestoreTPL (OldTpl); + Status = gBS->UninstallProtocolInterface ( + ChildHandle, + &gEfiDhcp6ProtocolGuid, + Dhcp6 + ); + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + if (EFI_ERROR (Status)) { + Instance->InDestroy = FALSE; + gBS->RestoreTPL (OldTpl); + return Status; + } + + // + // Remove it from the children list of its parent service. + // + RemoveEntryList (&Instance->Link); + Service->NumOfChild--; + + gBS->RestoreTPL (OldTpl); + + Dhcp6DestroyInstance (Instance); + return EFI_SUCCESS; +} diff --git a/NetworkPkg/Dhcp6Dxe/Dhcp6Driver.h b/NetworkPkg/Dhcp6Dxe/Dhcp6Driver.h new file mode 100644 index 000000000..a609eddf2 --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/Dhcp6Driver.h @@ -0,0 +1,150 @@ +/** @file + Driver Binding functions and Service Binding functions + declaration for Dhcp6 Driver. + + Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_DHCP6_DRIVER_H__ +#define __EFI_DHCP6_DRIVER_H__ + +#include + +extern EFI_COMPONENT_NAME_PROTOCOL gDhcp6ComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gDhcp6ComponentName2; +extern EFI_UNICODE_STRING_TABLE *gDhcp6ControllerNameTable; + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported(), it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Dhcp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start(), it + must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Dhcp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If the number of + children is zero, stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle. + @retval other This driver was not removed from this device. + +**/ +EFI_STATUS +EFIAPI +Dhcp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +Dhcp6ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Dhcp6ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +#endif diff --git a/NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.inf b/NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.inf new file mode 100644 index 000000000..d4d494ba8 --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.inf @@ -0,0 +1,78 @@ +## @file +# Client-side DHCPv6 services. +# +# This driver produces EFI DHCPv6 Protocol which is used to get IPv6 addresses +# and other configuration parameters from DHCPv6 servers. +# +# (C) Copyright 2015 Hewlett-Packard Development Company, L.P.
+# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = Dhcp6Dxe + FILE_GUID = 95E3669D-34BE-4775-A651-7EA41B69D89E + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = Dhcp6DriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload + MODULE_UNI_FILE = Dhcp6Dxe.uni + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = gDhcp6DriverBinding +# COMPONENT_NAME = gDhcp6ComponentName +# COMPONENT_NAME2 = gDhcp6ComponentName2 +# + +[Sources] + Dhcp6Driver.c + Dhcp6Driver.h + Dhcp6Impl.c + Dhcp6Impl.h + Dhcp6Io.c + Dhcp6Io.h + Dhcp6Utility.c + Dhcp6Utility.h + ComponentName.c + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + UefiLib + BaseLib + BaseMemoryLib + MemoryAllocationLib + UefiDriverEntryPoint + UefiBootServicesTableLib + UefiRuntimeServicesTableLib + DebugLib + NetLib + UdpIoLib + + +[Protocols] + gEfiUdp6ServiceBindingProtocolGuid ## TO_START + gEfiUdp6ProtocolGuid ## TO_START + gEfiDhcp6ServiceBindingProtocolGuid ## BY_START + gEfiDhcp6ProtocolGuid ## BY_START + gEfiIp6ConfigProtocolGuid ## TO_START + +[Guids] + gZeroGuid ## SOMETIMES_CONSUMES ## GUID + +[Pcd] + gEfiNetworkPkgTokenSpaceGuid.PcdDhcp6UidType ## SOMETIMES_CONSUMES + +[UserExtensions.TianoCore."ExtraFiles"] + Dhcp6DxeExtra.uni diff --git a/NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.uni b/NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.uni new file mode 100644 index 000000000..9ee749be2 --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.uni @@ -0,0 +1,17 @@ +// /** @file +// Client-side DHCPv6 services. +// +// This driver produces EFI DHCPv6 Protocol which is used to get IPv6 addresses +// and other configuration parameters from DHCPv6 servers. +// +// Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Client-side DHCPv6 services" + +#string STR_MODULE_DESCRIPTION #language en-US "This driver produces EFI DHCPv6 Protocol which is used to get IPv6 addresses and other configuration parameters from DHCPv6 servers." + diff --git a/NetworkPkg/Dhcp6Dxe/Dhcp6DxeExtra.uni b/NetworkPkg/Dhcp6Dxe/Dhcp6DxeExtra.uni new file mode 100644 index 000000000..1acbe304a --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/Dhcp6DxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// Dhcp6Dxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"DHCP6 DXE" + + diff --git a/NetworkPkg/Dhcp6Dxe/Dhcp6Impl.c b/NetworkPkg/Dhcp6Dxe/Dhcp6Impl.c new file mode 100644 index 000000000..44c340ed0 --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/Dhcp6Impl.c @@ -0,0 +1,1220 @@ +/** @file + This EFI_DHCP6_PROTOCOL interface implementation. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Dhcp6Impl.h" + +// +// Well-known multi-cast address defined in section-24.1 of rfc-3315 +// +// ALL_DHCP_Relay_Agents_and_Servers address: FF02::1:2 +// +EFI_IPv6_ADDRESS mAllDhcpRelayAndServersAddress = {{0xFF, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2}}; + +EFI_DHCP6_PROTOCOL gDhcp6ProtocolTemplate = { + EfiDhcp6GetModeData, + EfiDhcp6Configure, + EfiDhcp6Start, + EfiDhcp6InfoRequest, + EfiDhcp6RenewRebind, + EfiDhcp6Decline, + EfiDhcp6Release, + EfiDhcp6Stop, + EfiDhcp6Parse +}; + +/** + Starts the DHCPv6 standard S.A.R.R. process. + + The Start() function starts the DHCPv6 standard process. This function can + be called only when the state of Dhcp6 instance is in the Dhcp6Init state. + If the DHCP process completes successfully, the state of the Dhcp6 instance + will be transferred through Dhcp6Selecting and Dhcp6Requesting to the + Dhcp6Bound state. + Refer to rfc-3315 for precise state transitions during this process. At the + time when each event occurs in this process, the callback function that was set + by EFI_DHCP6_PROTOCOL.Configure() will be called, and the user can take this + opportunity to control the process. + + @param[in] This The pointer to Dhcp6 protocol. + + @retval EFI_SUCCESS The DHCPv6 standard process has started, or it has + completed when CompletionEvent is NULL. + @retval EFI_ACCESS_DENIED The EFI DHCPv6 Child instance hasn't been configured. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_TIMEOUT The DHCPv6 configuration process failed because no + response was received from the server within the + specified timeout value. + @retval EFI_ABORTED The user aborted the DHCPv6 process. + @retval EFI_ALREADY_STARTED Some other Dhcp6 instance already started the DHCPv6 + standard process. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_NO_MEDIA There was a media error. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Start ( + IN EFI_DHCP6_PROTOCOL *This + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + DHCP6_INSTANCE *Instance; + DHCP6_SERVICE *Service; + EFI_STATUS MediaStatus; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP6_INSTANCE_FROM_THIS (This); + Service = Instance->Service; + + // + // The instance hasn't been configured. + // + if (Instance->Config == NULL) { + return EFI_ACCESS_DENIED; + } + + ASSERT (Instance->IaCb.Ia != NULL); + + // + // The instance has already been started. + // + if (Instance->IaCb.Ia->State != Dhcp6Init) { + return EFI_ALREADY_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Check Media Satus. + // + MediaStatus = EFI_SUCCESS; + NetLibDetectMediaWaitTimeout (Service->Controller, DHCP_CHECK_MEDIA_WAITING_TIME, &MediaStatus); + if (MediaStatus != EFI_SUCCESS) { + Status = EFI_NO_MEDIA; + goto ON_ERROR; + } + + Instance->UdpSts = EFI_ALREADY_STARTED; + + // + // Send the solicit message to start S.A.R.R process. + // + Status = Dhcp6SendSolicitMsg (Instance); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Register receive callback for the stateful exchange process. + // + Status = UdpIoRecvDatagram( + Service->UdpIo, + Dhcp6ReceivePacket, + Service, + 0 + ); + + if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) { + goto ON_ERROR; + } + + gBS->RestoreTPL (OldTpl); + + // + // Poll udp out of the net tpl if synchronous call. + // + if (Instance->Config->IaInfoEvent == NULL) { + + while (Instance->UdpSts == EFI_ALREADY_STARTED) { + Service->UdpIo->Protocol.Udp6->Poll (Service->UdpIo->Protocol.Udp6); + } + return Instance->UdpSts; + } + + return EFI_SUCCESS; + +ON_ERROR: + + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Stops the DHCPv6 standard S.A.R.R. process. + + The Stop() function is used to stop the DHCPv6 standard process. After this + function is called successfully, the state of Dhcp6 instance is transferred + into Dhcp6Init. EFI_DHCP6_PROTOCOL.Configure() needs to be called + before DHCPv6 standard process can be started again. This function can be + called when the Dhcp6 instance is in any state. + + @param[in] This The pointer to the Dhcp6 protocol. + + @retval EFI_SUCCESS The Dhcp6 instance is now in the Dhcp6Init state. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Stop ( + IN EFI_DHCP6_PROTOCOL *This + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + EFI_UDP6_PROTOCOL *Udp6; + DHCP6_INSTANCE *Instance; + DHCP6_SERVICE *Service; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP6_INSTANCE_FROM_THIS (This); + Service = Instance->Service; + Udp6 = Service->UdpIo->Protocol.Udp6; + Status = EFI_SUCCESS; + + // + // The instance hasn't been configured. + // + if (Instance->Config == NULL) { + return Status; + } + + ASSERT (Instance->IaCb.Ia != NULL); + + // + // No valid REPLY message received yet, cleanup this instance directly. + // + if (Instance->IaCb.Ia->State == Dhcp6Init || + Instance->IaCb.Ia->State == Dhcp6Selecting || + Instance->IaCb.Ia->State == Dhcp6Requesting + ) { + goto ON_EXIT; + } + + // + // Release the current ready Ia. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance->UdpSts = EFI_ALREADY_STARTED; + Status = Dhcp6SendReleaseMsg (Instance, Instance->IaCb.Ia); + gBS->RestoreTPL (OldTpl); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Poll udp out of the net tpl if synchoronus call. + // + if (Instance->Config->IaInfoEvent == NULL) { + ASSERT (Udp6 != NULL); + while (Instance->UdpSts == EFI_ALREADY_STARTED) { + Udp6->Poll (Udp6); + } + Status = Instance->UdpSts; + } + +ON_EXIT: + // + // Clean up the session data for the released Ia. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Dhcp6CleanupSession (Instance, EFI_SUCCESS); + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Returns the current operating mode data for the Dhcp6 instance. + + The GetModeData() function returns the current operating mode and + cached data packet for the Dhcp6 instance. + + @param[in] This The pointer to the Dhcp6 protocol. + @param[out] Dhcp6ModeData The pointer to the Dhcp6 mode data. + @param[out] Dhcp6ConfigData The pointer to the Dhcp6 configure data. + + @retval EFI_SUCCESS The mode data was returned. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_ACCESS_DENIED The EFI DHCPv6 Protocol instance was not + configured. +**/ +EFI_STATUS +EFIAPI +EfiDhcp6GetModeData ( + IN EFI_DHCP6_PROTOCOL *This, + OUT EFI_DHCP6_MODE_DATA *Dhcp6ModeData OPTIONAL, + OUT EFI_DHCP6_CONFIG_DATA *Dhcp6ConfigData OPTIONAL + ) +{ + EFI_TPL OldTpl; + EFI_DHCP6_IA *Ia; + DHCP6_INSTANCE *Instance; + DHCP6_SERVICE *Service; + UINT32 IaSize; + UINT32 IdSize; + + if (This == NULL || (Dhcp6ModeData == NULL && Dhcp6ConfigData == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP6_INSTANCE_FROM_THIS (This); + Service = Instance->Service; + + if (Instance->Config == NULL && Dhcp6ConfigData != NULL) { + return EFI_ACCESS_DENIED; + } + + ASSERT (Service->ClientId != NULL); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // User needs a copy of instance config data. + // + if (Dhcp6ConfigData != NULL) { + ZeroMem (Dhcp6ConfigData, sizeof(EFI_DHCP6_CONFIG_DATA)); + // + // Duplicate config data, including all reference buffers. + // + if (EFI_ERROR (Dhcp6CopyConfigData (Dhcp6ConfigData, Instance->Config))) { + goto ON_ERROR; + } + } + + // + // User need a copy of instance mode data. + // + if (Dhcp6ModeData != NULL) { + ZeroMem (Dhcp6ModeData, sizeof (EFI_DHCP6_MODE_DATA)); + // + // Duplicate a copy of EFI_DHCP6_DUID for client Id. + // + IdSize = Service->ClientId->Length + sizeof (Service->ClientId->Length); + + Dhcp6ModeData->ClientId = AllocateZeroPool (IdSize); + if (Dhcp6ModeData->ClientId == NULL) { + goto ON_ERROR; + } + + CopyMem ( + Dhcp6ModeData->ClientId, + Service->ClientId, + IdSize + ); + + Ia = Instance->IaCb.Ia; + if (Ia != NULL) { + // + // Duplicate a copy of EFI_DHCP6_IA for configured Ia. + // + IaSize = sizeof (EFI_DHCP6_IA) + (Ia->IaAddressCount -1) * sizeof (EFI_DHCP6_IA_ADDRESS); + + Dhcp6ModeData->Ia = AllocateZeroPool (IaSize); + if (Dhcp6ModeData->Ia == NULL) { + goto ON_ERROR; + } + + CopyMem ( + Dhcp6ModeData->Ia, + Ia, + IaSize + ); + + // + // Duplicate a copy of reply packet if has. + // + if (Ia->ReplyPacket != NULL) { + Dhcp6ModeData->Ia->ReplyPacket = AllocateZeroPool (Ia->ReplyPacket->Size); + if (Dhcp6ModeData->Ia->ReplyPacket == NULL) { + goto ON_ERROR; + } + CopyMem ( + Dhcp6ModeData->Ia->ReplyPacket, + Ia->ReplyPacket, + Ia->ReplyPacket->Size + ); + } + } + } + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; + +ON_ERROR: + + if (Dhcp6ConfigData != NULL) { + Dhcp6CleanupConfigData (Dhcp6ConfigData); + } + if (Dhcp6ModeData != NULL) { + Dhcp6CleanupModeData (Dhcp6ModeData); + } + gBS->RestoreTPL (OldTpl); + + return EFI_OUT_OF_RESOURCES; +} + + +/** + Initializes, changes, or resets the operational settings for the Dhcp6 instance. + + The Configure() function is used to initialize or clean up the configuration + data of the Dhcp6 instance: + - When Dhcp6CfgData is not NULL and Configure() is called successfully, the + configuration data will be initialized in the Dhcp6 instance, and the state + of the configured IA will be transferred into Dhcp6Init. + - When Dhcp6CfgData is NULL and Configure() is called successfully, the + configuration data will be cleaned up and no IA will be associated with + the Dhcp6 instance. + To update the configuration data for an Dhcp6 instance, the original data + must be cleaned up before setting the new configuration data. + + @param[in] This The pointer to the Dhcp6 protocol + @param[in] Dhcp6CfgData The pointer to the EFI_DHCP6_CONFIG_DATA. + + @retval EFI_SUCCESS The Dhcp6 is configured successfully with the + Dhcp6Init state, or cleaned up the original + configuration setting. + @retval EFI_ACCESS_DENIED The Dhcp6 instance was already configured. + The Dhcp6 instance has already started the + DHCPv6 S.A.R.R when Dhcp6CfgData is NULL. + @retval EFI_INVALID_PARAMETER Some of the parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Configure ( + IN EFI_DHCP6_PROTOCOL *This, + IN EFI_DHCP6_CONFIG_DATA *Dhcp6CfgData OPTIONAL + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + LIST_ENTRY *Entry; + DHCP6_INSTANCE *Other; + DHCP6_INSTANCE *Instance; + DHCP6_SERVICE *Service; + UINTN Index; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP6_INSTANCE_FROM_THIS (This); + Service = Instance->Service; + + // + // Check the parameter of configure data. + // + if (Dhcp6CfgData != NULL) { + if (Dhcp6CfgData->OptionCount > 0 && Dhcp6CfgData->OptionList == NULL) { + return EFI_INVALID_PARAMETER; + } + if (Dhcp6CfgData->OptionList != NULL) { + for (Index = 0; Index < Dhcp6CfgData->OptionCount; Index++) { + if (Dhcp6CfgData->OptionList[Index]->OpCode == Dhcp6OptClientId || + Dhcp6CfgData->OptionList[Index]->OpCode == Dhcp6OptRapidCommit || + Dhcp6CfgData->OptionList[Index]->OpCode == Dhcp6OptReconfigureAccept || + Dhcp6CfgData->OptionList[Index]->OpCode == Dhcp6OptIana || + Dhcp6CfgData->OptionList[Index]->OpCode == Dhcp6OptIata + ) { + return EFI_INVALID_PARAMETER; + } + } + } + + if (Dhcp6CfgData->IaDescriptor.Type != EFI_DHCP6_IA_TYPE_NA && + Dhcp6CfgData->IaDescriptor.Type != EFI_DHCP6_IA_TYPE_TA + ) { + return EFI_INVALID_PARAMETER; + } + + if (Dhcp6CfgData->IaInfoEvent == NULL && Dhcp6CfgData->SolicitRetransmission == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (Dhcp6CfgData->SolicitRetransmission != NULL && + Dhcp6CfgData->SolicitRetransmission->Mrc == 0 && + Dhcp6CfgData->SolicitRetransmission->Mrd == 0 + ) { + return EFI_INVALID_PARAMETER; + } + + // + // Make sure the (IaId, IaType) is unique over all the instances. + // + NET_LIST_FOR_EACH (Entry, &Service->Child) { + Other = NET_LIST_USER_STRUCT (Entry, DHCP6_INSTANCE, Link); + if (Other->IaCb.Ia != NULL && + Other->IaCb.Ia->Descriptor.Type == Dhcp6CfgData->IaDescriptor.Type && + Other->IaCb.Ia->Descriptor.IaId == Dhcp6CfgData->IaDescriptor.IaId + ) { + return EFI_INVALID_PARAMETER; + } + } + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (Dhcp6CfgData != NULL) { + // + // It's not allowed to configure one instance twice without configure null. + // + if (Instance->Config != NULL) { + gBS->RestoreTPL (OldTpl); + return EFI_ACCESS_DENIED; + } + + // + // Duplicate config data including all reference buffers. + // + Instance->Config = AllocateZeroPool (sizeof (EFI_DHCP6_CONFIG_DATA)); + if (Instance->Config == NULL) { + gBS->RestoreTPL (OldTpl); + return EFI_OUT_OF_RESOURCES; + } + + Status = Dhcp6CopyConfigData (Instance->Config, Dhcp6CfgData); + if (EFI_ERROR(Status)) { + FreePool (Instance->Config); + gBS->RestoreTPL (OldTpl); + return EFI_OUT_OF_RESOURCES; + } + + // + // Initialize the Ia descriptor from the config data, and leave the other + // fields of the Ia as default value 0. + // + Instance->IaCb.Ia = AllocateZeroPool (sizeof(EFI_DHCP6_IA)); + if (Instance->IaCb.Ia == NULL) { + Dhcp6CleanupConfigData (Instance->Config); + FreePool (Instance->Config); + gBS->RestoreTPL (OldTpl); + return EFI_OUT_OF_RESOURCES; + } + CopyMem ( + &Instance->IaCb.Ia->Descriptor, + &Dhcp6CfgData->IaDescriptor, + sizeof(EFI_DHCP6_IA_DESCRIPTOR) + ); + + } else { + + if (Instance->Config == NULL) { + ASSERT (Instance->IaCb.Ia == NULL); + gBS->RestoreTPL (OldTpl); + return EFI_SUCCESS; + } + + // + // It's not allowed to configure a started instance as null. + // + if (Instance->IaCb.Ia->State != Dhcp6Init) { + gBS->RestoreTPL (OldTpl); + return EFI_ACCESS_DENIED; + } + + Dhcp6CleanupConfigData (Instance->Config); + FreePool (Instance->Config); + Instance->Config = NULL; + + FreePool (Instance->IaCb.Ia); + Instance->IaCb.Ia = NULL; + } + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; +} + + +/** + Request configuration information without the assignment of any + Ia addresses of the client. + + The InfoRequest() function is used to request configuration information + without the assignment of any IPv6 address of the client. The client sends + out an Information Request packet to obtain the required configuration + information, and DHCPv6 server responds with a Reply packet containing + the information for the client. The received Reply packet will be passed + to the user by ReplyCallback function. If the user returns EFI_NOT_READY from + ReplyCallback, the Dhcp6 instance will continue to receive other Reply + packets unless timeout according to the Retransmission parameter. + Otherwise, the Information Request exchange process will be finished + successfully if user returns EFI_SUCCESS from ReplyCallback. + + @param[in] This The pointer to the Dhcp6 protocol. + @param[in] SendClientId If TRUE, the DHCPv6 protocol instance will build Client + Identifier option and include it into Information Request + packet. Otherwise, Client Identifier option will not be included. + @param[in] OptionRequest The pointer to the buffer of option request options. + @param[in] OptionCount The option number in the OptionList. + @param[in] OptionList The list of appended options. + @param[in] Retransmission The pointer to the retransmission of the message. + @param[in] TimeoutEvent The event of timeout. + @param[in] ReplyCallback The callback function when the reply was received. + @param[in] CallbackContext The pointer to the parameter passed to the callback. + + @retval EFI_SUCCESS The DHCPv6 information request exchange process + completed when TimeoutEvent is NULL. Information + Request packet has been sent to DHCPv6 server when + TimeoutEvent is not NULL. + @retval EFI_NO_RESPONSE The DHCPv6 information request exchange process failed + because of no response, or not all requested-options + are responded by DHCPv6 servers when Timeout happened. + @retval EFI_ABORTED The DHCPv6 information request exchange process was aborted + by user. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6InfoRequest ( + IN EFI_DHCP6_PROTOCOL *This, + IN BOOLEAN SendClientId, + IN EFI_DHCP6_PACKET_OPTION *OptionRequest, + IN UINT32 OptionCount, + IN EFI_DHCP6_PACKET_OPTION *OptionList[] OPTIONAL, + IN EFI_DHCP6_RETRANSMISSION *Retransmission, + IN EFI_EVENT TimeoutEvent OPTIONAL, + IN EFI_DHCP6_INFO_CALLBACK ReplyCallback, + IN VOID *CallbackContext OPTIONAL + ) +{ + EFI_STATUS Status; + DHCP6_INSTANCE *Instance; + DHCP6_SERVICE *Service; + UINTN Index; + EFI_EVENT Timer; + EFI_STATUS TimerStatus; + UINTN GetMappingTimeOut; + + if (This == NULL || OptionRequest == NULL || Retransmission == NULL || ReplyCallback == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (Retransmission != NULL && Retransmission->Mrc == 0 && Retransmission->Mrd == 0) { + return EFI_INVALID_PARAMETER; + } + + if (OptionCount > 0 && OptionList == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (OptionList != NULL) { + for (Index = 0; Index < OptionCount; Index++) { + if (OptionList[Index]->OpCode == Dhcp6OptClientId || OptionList[Index]->OpCode == Dhcp6OptRequestOption) { + return EFI_INVALID_PARAMETER; + } + } + } + + Instance = DHCP6_INSTANCE_FROM_THIS (This); + Service = Instance->Service; + + Status = Dhcp6StartInfoRequest ( + Instance, + SendClientId, + OptionRequest, + OptionCount, + OptionList, + Retransmission, + TimeoutEvent, + ReplyCallback, + CallbackContext + ); + if (Status == EFI_NO_MAPPING) { + // + // The link local address is not ready, wait for some time and restart + // the DHCP6 information request process. + // + Status = Dhcp6GetMappingTimeOut(Service->Ip6Cfg, &GetMappingTimeOut); + if (EFI_ERROR(Status)) { + return Status; + } + + Status = gBS->CreateEvent (EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Timer); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Start the timer, wait for link local address DAD to finish. + // + Status = gBS->SetTimer (Timer, TimerRelative, GetMappingTimeOut); + if (EFI_ERROR (Status)) { + gBS->CloseEvent (Timer); + return Status; + } + + do { + TimerStatus = gBS->CheckEvent (Timer); + if (!EFI_ERROR (TimerStatus)) { + Status = Dhcp6StartInfoRequest ( + Instance, + SendClientId, + OptionRequest, + OptionCount, + OptionList, + Retransmission, + TimeoutEvent, + ReplyCallback, + CallbackContext + ); + } + } while (TimerStatus == EFI_NOT_READY); + + gBS->CloseEvent (Timer); + } + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Poll udp out of the net tpl if synchoronus call. + // + if (TimeoutEvent == NULL) { + + while (Instance->UdpSts == EFI_ALREADY_STARTED) { + Service->UdpIo->Protocol.Udp6->Poll (Service->UdpIo->Protocol.Udp6); + } + return Instance->UdpSts; + } + + return EFI_SUCCESS; +} + + +/** + Manually extend the valid and preferred lifetimes for the IPv6 addresses + of the configured IA and update other configuration parameters by sending a + Renew or Rebind packet. + + The RenewRebind() function is used to manually extend the valid and preferred + lifetimes for the IPv6 addresses of the configured IA, and update other + configuration parameters by sending Renew or Rebind packet. + - When RebindRequest is FALSE and the state of the configured IA is Dhcp6Bound, + it sends Renew packet to the previously DHCPv6 server and transfer the + state of the configured IA to Dhcp6Renewing. If valid Reply packet received, + the state transfers to Dhcp6Bound and the valid and preferred timer restarts. + If fails, the state transfers to Dhcp6Bound, but the timer continues. + - When RebindRequest is TRUE and the state of the configured IA is Dhcp6Bound, + it will send a Rebind packet. If valid Reply packet is received, the state transfers + to Dhcp6Bound and the valid and preferred timer restarts. If it fails, the state + transfers to Dhcp6Init, and the IA can't be used. + + @param[in] This The pointer to the Dhcp6 protocol. + @param[in] RebindRequest If TRUE, Rebind packet will be sent and enter Dhcp6Rebinding state. + Otherwise, Renew packet will be sent and enter Dhcp6Renewing state. + + @retval EFI_SUCCESS The DHCPv6 renew/rebind exchange process has + completed and at least one IPv6 address of the + configured IA has been bound again when + EFI_DHCP6_CONFIG_DATA.IaInfoEvent is NULL. + The EFI DHCPv6 Protocol instance has sent Renew + or Rebind packet when + EFI_DHCP6_CONFIG_DATA.IaInfoEvent is not NULL. + @retval EFI_ACCESS_DENIED The Dhcp6 instance hasn't been configured, or the + state of the configured IA is not in Dhcp6Bound. + @retval EFI_ALREADY_STARTED The state of the configured IA has already entered + Dhcp6Renewing when RebindRequest is FALSE. + The state of the configured IA has already entered + Dhcp6Rebinding when RebindRequest is TRUE. + @retval EFI_ABORTED The DHCPv6 renew/rebind exchange process aborted + by the user. + @retval EFI_NO_RESPONSE The DHCPv6 renew/rebind exchange process failed + because of no response. + @retval EFI_NO_MAPPING No IPv6 address has been bound to the configured + IA after the DHCPv6 renew/rebind exchange process. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6RenewRebind ( + IN EFI_DHCP6_PROTOCOL *This, + IN BOOLEAN RebindRequest + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + DHCP6_INSTANCE *Instance; + DHCP6_SERVICE *Service; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP6_INSTANCE_FROM_THIS (This); + Service = Instance->Service; + + // + // The instance hasn't been configured. + // + if (Instance->Config == NULL) { + return EFI_ACCESS_DENIED; + } + + ASSERT (Instance->IaCb.Ia != NULL); + + // + // The instance has already entered renewing or rebinding state. + // + if ((Instance->IaCb.Ia->State == Dhcp6Rebinding && RebindRequest) || + (Instance->IaCb.Ia->State == Dhcp6Renewing && !RebindRequest) + ) { + return EFI_ALREADY_STARTED; + } + + if (Instance->IaCb.Ia->State != Dhcp6Bound) { + return EFI_ACCESS_DENIED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Instance->UdpSts = EFI_ALREADY_STARTED; + + // + // Send renew/rebind message to start exchange process. + // + Status = Dhcp6SendRenewRebindMsg (Instance, RebindRequest); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Register receive callback for the stateful exchange process. + // + Status = UdpIoRecvDatagram( + Service->UdpIo, + Dhcp6ReceivePacket, + Service, + 0 + ); + + if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) { + goto ON_ERROR; + } + + gBS->RestoreTPL (OldTpl); + + // + // Poll udp out of the net tpl if synchoronus call. + // + if (Instance->Config->IaInfoEvent == NULL) { + + while (Instance->UdpSts == EFI_ALREADY_STARTED) { + Service->UdpIo->Protocol.Udp6->Poll (Service->UdpIo->Protocol.Udp6); + } + return Instance->UdpSts; + } + + return EFI_SUCCESS; + +ON_ERROR: + + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Inform that one or more addresses assigned by a server are already + in use by another node. + + The Decline() function is used to manually decline the assignment of + IPv6 addresses, which have been already used by another node. If all + IPv6 addresses of the configured IA are declined through this function, + the state of the IA will switch through Dhcp6Declining to Dhcp6Init. + Otherwise, the state of the IA will restore to Dhcp6Bound after the + declining process. The Decline() can only be called when the IA is in + Dhcp6Bound state. If the EFI_DHCP6_CONFIG_DATA.IaInfoEvent is NULL, + this function is a blocking operation. It will return after the + declining process finishes, or aborted by user. + + @param[in] This The pointer to EFI_DHCP6_PROTOCOL. + @param[in] AddressCount The number of declining addresses. + @param[in] Addresses The pointer to the buffer stored the declining + addresses. + + @retval EFI_SUCCESS The DHCPv6 decline exchange process completed + when EFI_DHCP6_CONFIG_DATA.IaInfoEvent was NULL. + The Dhcp6 instance sent Decline packet when + EFI_DHCP6_CONFIG_DATA.IaInfoEvent was not NULL. + @retval EFI_ACCESS_DENIED The Dhcp6 instance hasn't been configured, or the + state of the configured IA is not in Dhcp6Bound. + @retval EFI_ABORTED The DHCPv6 decline exchange process aborted by user. + @retval EFI_NOT_FOUND Any specified IPv6 address is not correlated with + the configured IA for this instance. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Decline ( + IN EFI_DHCP6_PROTOCOL *This, + IN UINT32 AddressCount, + IN EFI_IPv6_ADDRESS *Addresses + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + EFI_DHCP6_IA *DecIa; + DHCP6_INSTANCE *Instance; + DHCP6_SERVICE *Service; + + if (This == NULL || AddressCount == 0 || Addresses == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP6_INSTANCE_FROM_THIS (This); + Service = Instance->Service; + + // + // The instance hasn't been configured. + // + if (Instance->Config == NULL) { + return EFI_ACCESS_DENIED; + } + + ASSERT (Instance->IaCb.Ia != NULL); + + if (Instance->IaCb.Ia->State != Dhcp6Bound) { + return EFI_ACCESS_DENIED; + } + + // + // Check whether all the declined addresses belongs to the configured Ia. + // + Status = Dhcp6CheckAddress (Instance->IaCb.Ia, AddressCount, Addresses); + + if (EFI_ERROR(Status)) { + return Status; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Instance->UdpSts = EFI_ALREADY_STARTED; + + // + // Deprive of all the declined addresses from the configured Ia, and create a + // DeclineIa used to create decline message. + // + DecIa = Dhcp6DepriveAddress (Instance->IaCb.Ia, AddressCount, Addresses); + + if (DecIa == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + // + // Send the decline message to start exchange process. + // + Status = Dhcp6SendDeclineMsg (Instance, DecIa); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Register receive callback for the stateful exchange process. + // + Status = UdpIoRecvDatagram( + Service->UdpIo, + Dhcp6ReceivePacket, + Service, + 0 + ); + + if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) { + goto ON_ERROR; + } + + FreePool (DecIa); + gBS->RestoreTPL (OldTpl); + + // + // Poll udp out of the net tpl if synchoronus call. + // + if (Instance->Config->IaInfoEvent == NULL) { + + while (Instance->UdpSts == EFI_ALREADY_STARTED) { + Service->UdpIo->Protocol.Udp6->Poll (Service->UdpIo->Protocol.Udp6); + } + return Instance->UdpSts; + } + + return EFI_SUCCESS; + +ON_ERROR: + + if (DecIa != NULL) { + FreePool (DecIa); + } + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Release one or more addresses associated with the configured Ia + for current instance. + + The Release() function is used to manually release one or more + IPv6 addresses. If AddressCount is zero, it will release all IPv6 + addresses of the configured IA. If all IPv6 addresses of the IA are + released through this function, the state of the IA will switch + through Dhcp6Releasing to Dhcp6Init, otherwise, the state of the + IA will restore to Dhcp6Bound after the releasing process. + The Release() can only be called when the IA is in Dhcp6Bound state. + If the EFI_DHCP6_CONFIG_DATA.IaInfoEvent is NULL, the function is + a blocking operation. It will return after the releasing process + finishes, or is aborted by user. + + @param[in] This The pointer to the Dhcp6 protocol. + @param[in] AddressCount The number of releasing addresses. + @param[in] Addresses The pointer to the buffer stored the releasing + addresses. + + @retval EFI_SUCCESS The DHCPv6 release exchange process + completed when EFI_DHCP6_CONFIG_DATA.IaInfoEvent + was NULL. The Dhcp6 instance was sent Release + packet when EFI_DHCP6_CONFIG_DATA.IaInfoEvent + was not NULL. + @retval EFI_ACCESS_DENIED The Dhcp6 instance hasn't been configured, or the + state of the configured IA is not in Dhcp6Bound. + @retval EFI_ABORTED The DHCPv6 release exchange process aborted by user. + @retval EFI_NOT_FOUND Any specified IPv6 address is not correlated with + the configured IA for this instance. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Release ( + IN EFI_DHCP6_PROTOCOL *This, + IN UINT32 AddressCount, + IN EFI_IPv6_ADDRESS *Addresses + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + EFI_DHCP6_IA *RelIa; + DHCP6_INSTANCE *Instance; + DHCP6_SERVICE *Service; + + if (This == NULL || (AddressCount != 0 && Addresses == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = DHCP6_INSTANCE_FROM_THIS (This); + Service = Instance->Service; + + // + // The instance hasn't been configured. + // + if (Instance->Config == NULL) { + return EFI_ACCESS_DENIED; + } + + ASSERT (Instance->IaCb.Ia != NULL); + + if (Instance->IaCb.Ia->State != Dhcp6Bound) { + return EFI_ACCESS_DENIED; + } + + // + // Check whether all the released addresses belongs to the configured Ia. + // + Status = Dhcp6CheckAddress (Instance->IaCb.Ia, AddressCount, Addresses); + + if (EFI_ERROR(Status)) { + return Status; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Instance->UdpSts = EFI_ALREADY_STARTED; + + // + // Deprive of all the released addresses from the configured Ia, and create a + // ReleaseIa used to create release message. + // + RelIa = Dhcp6DepriveAddress (Instance->IaCb.Ia, AddressCount, Addresses); + + if (RelIa == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + // + // Send the release message to start exchange process. + // + Status = Dhcp6SendReleaseMsg (Instance, RelIa); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Register receive callback for the stateful exchange process. + // + Status = UdpIoRecvDatagram( + Service->UdpIo, + Dhcp6ReceivePacket, + Service, + 0 + ); + + if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) { + goto ON_ERROR; + } + + FreePool (RelIa); + gBS->RestoreTPL (OldTpl); + + // + // Poll udp out of the net tpl if synchoronus call. + // + if (Instance->Config->IaInfoEvent == NULL) { + while (Instance->UdpSts == EFI_ALREADY_STARTED) { + Service->UdpIo->Protocol.Udp6->Poll (Service->UdpIo->Protocol.Udp6); + } + return Instance->UdpSts; + } + + return EFI_SUCCESS; + +ON_ERROR: + + if (RelIa != NULL) { + FreePool (RelIa); + } + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Parse the option data in the Dhcp6 packet. + + The Parse() function is used to retrieve the option list in the DHCPv6 packet. + + @param[in] This The pointer to the Dhcp6 protocol. + @param[in] Packet The pointer to the Dhcp6 packet. + @param[in, out] OptionCount The number of option in the packet. + @param[out] PacketOptionList The array of pointers to each option in the packet. + + @retval EFI_SUCCESS The packet was successfully parsed. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_BUFFER_TOO_SMALL *OptionCount is smaller than the number of options + that were found in the Packet. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Parse ( + IN EFI_DHCP6_PROTOCOL *This, + IN EFI_DHCP6_PACKET *Packet, + IN OUT UINT32 *OptionCount, + OUT EFI_DHCP6_PACKET_OPTION *PacketOptionList[] OPTIONAL + ) +{ + UINT32 OptCnt; + UINT32 OptLen; + UINT16 DataLen; + UINT8 *Start; + UINT8 *End; + + if (This == NULL || Packet == NULL || OptionCount == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (*OptionCount != 0 && PacketOptionList == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (Packet->Length > Packet->Size || Packet->Length < sizeof (EFI_DHCP6_HEADER)) { + return EFI_INVALID_PARAMETER; + } + + // + // The format of Dhcp6 option: + // + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | option-code | option-len (option data) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | option-data | + // | (option-len octets) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + OptCnt = 0; + OptLen = Packet->Length - sizeof (EFI_DHCP6_HEADER); + Start = Packet->Dhcp6.Option; + End = Start + OptLen; + + // + // Calculate the number of option in the packet. + // + while (Start < End) { + DataLen = ((EFI_DHCP6_PACKET_OPTION *) Start)->OpLen; + Start += (NTOHS (DataLen) + 4); + OptCnt++; + } + + // + // It will return buffer too small if pass-in option count is smaller than the + // actual count of options in the packet. + // + if (OptCnt > *OptionCount) { + *OptionCount = OptCnt; + return EFI_BUFFER_TOO_SMALL; + } + + ZeroMem ( + PacketOptionList, + (*OptionCount * sizeof (EFI_DHCP6_PACKET_OPTION *)) + ); + + OptCnt = 0; + Start = Packet->Dhcp6.Option; + + while (Start < End) { + + PacketOptionList[OptCnt] = (EFI_DHCP6_PACKET_OPTION *) Start; + DataLen = ((EFI_DHCP6_PACKET_OPTION *) Start)->OpLen; + Start += (NTOHS (DataLen) + 4); + OptCnt++; + } + + return EFI_SUCCESS; +} + diff --git a/NetworkPkg/Dhcp6Dxe/Dhcp6Impl.h b/NetworkPkg/Dhcp6Dxe/Dhcp6Impl.h new file mode 100644 index 000000000..f88b00ad0 --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/Dhcp6Impl.h @@ -0,0 +1,479 @@ +/** @file + Dhcp6 internal data structure and definition declaration. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_DHCP6_IMPL_H__ +#define __EFI_DHCP6_IMPL_H__ + + +#include + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +typedef struct _DHCP6_IA_CB DHCP6_IA_CB; +typedef struct _DHCP6_INF_CB DHCP6_INF_CB; +typedef struct _DHCP6_TX_CB DHCP6_TX_CB; +typedef struct _DHCP6_SERVICE DHCP6_SERVICE; +typedef struct _DHCP6_INSTANCE DHCP6_INSTANCE; + +#include "Dhcp6Utility.h" +#include "Dhcp6Io.h" +#include "Dhcp6Driver.h" + +#define DHCP6_SERVICE_SIGNATURE SIGNATURE_32 ('D', 'H', '6', 'S') +#define DHCP6_INSTANCE_SIGNATURE SIGNATURE_32 ('D', 'H', '6', 'I') + +#define DHCP6_PACKET_ALL 0 +#define DHCP6_PACKET_STATEFUL 1 +#define DHCP6_PACKET_STATELESS 2 + +#define DHCP6_BASE_PACKET_SIZE 1024 + +#define DHCP6_PORT_CLIENT 546 +#define DHCP6_PORT_SERVER 547 + +#define DHCP_CHECK_MEDIA_WAITING_TIME EFI_TIMER_PERIOD_SECONDS(20) + +#define DHCP6_INSTANCE_FROM_THIS(Instance) CR ((Instance), DHCP6_INSTANCE, Dhcp6, DHCP6_INSTANCE_SIGNATURE) +#define DHCP6_SERVICE_FROM_THIS(Service) CR ((Service), DHCP6_SERVICE, ServiceBinding, DHCP6_SERVICE_SIGNATURE) + +extern EFI_IPv6_ADDRESS mAllDhcpRelayAndServersAddress; +extern EFI_DHCP6_PROTOCOL gDhcp6ProtocolTemplate; + +// +// Control block for each IA. +// +struct _DHCP6_IA_CB { + EFI_DHCP6_IA *Ia; + UINT32 T1; + UINT32 T2; + UINT32 AllExpireTime; + UINT32 LeaseTime; +}; + +// +// Control block for each transmitted message. +// +struct _DHCP6_TX_CB { + LIST_ENTRY Link; + UINT32 Xid; + EFI_DHCP6_PACKET *TxPacket; + EFI_DHCP6_RETRANSMISSION RetryCtl; + UINT32 RetryCnt; + UINT32 RetryExp; + UINT32 RetryLos; + UINT32 TickTime; + UINT16 *Elapsed; + BOOLEAN SolicitRetry; +}; + +// +// Control block for each info-request message. +// +struct _DHCP6_INF_CB { + LIST_ENTRY Link; + UINT32 Xid; + EFI_DHCP6_INFO_CALLBACK ReplyCallback; + VOID *CallbackContext; + EFI_EVENT TimeoutEvent; +}; + +// +// Control block for Dhcp6 instance, it's per configuration data. +// +struct _DHCP6_INSTANCE { + UINT32 Signature; + EFI_HANDLE Handle; + DHCP6_SERVICE *Service; + LIST_ENTRY Link; + EFI_DHCP6_PROTOCOL Dhcp6; + EFI_EVENT Timer; + EFI_DHCP6_CONFIG_DATA *Config; + EFI_DHCP6_IA *CacheIa; + DHCP6_IA_CB IaCb; + LIST_ENTRY TxList; + LIST_ENTRY InfList; + EFI_DHCP6_PACKET *AdSelect; + UINT8 AdPref; + EFI_IPv6_ADDRESS *Unicast; + volatile EFI_STATUS UdpSts; + BOOLEAN InDestroy; + BOOLEAN MediaPresent; + // + // StartTime is used to calculate the 'elapsed-time' option. Refer to RFC3315, + // the elapsed-time is amount of time since the client began its current DHCP transaction. + // + UINT64 StartTime; +}; + +// +// Control block for Dhcp6 service, it's per Nic handle. +// +struct _DHCP6_SERVICE { + UINT32 Signature; + EFI_HANDLE Controller; + EFI_HANDLE Image; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg; + EFI_DHCP6_DUID *ClientId; + UDP_IO *UdpIo; + UINT32 Xid; + LIST_ENTRY Child; + UINTN NumOfChild; +}; + +/** + Starts the DHCPv6 standard S.A.R.R. process. + + The Start() function starts the DHCPv6 standard process. This function can + be called only when the state of Dhcp6 instance is in the Dhcp6Init state. + If the DHCP process completes successfully, the state of the Dhcp6 instance + will be transferred through Dhcp6Selecting and Dhcp6Requesting to the + Dhcp6Bound state. + Refer to rfc-3315 for precise state transitions during this process. At the + time when each event occurs in this process, the callback function that was set + by EFI_DHCP6_PROTOCOL.Configure() will be called and the user can take this + opportunity to control the process. + + @param[in] This The pointer to Dhcp6 protocol. + + @retval EFI_SUCCESS The DHCPv6 standard process has started, or it + completed when CompletionEvent was NULL. + @retval EFI_ACCESS_DENIED The EFI DHCPv6 Child instance hasn't been configured. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_TIMEOUT The DHCPv6 configuration process failed because no + response was received from the server within the + specified timeout value. + @retval EFI_ABORTED The user aborted the DHCPv6 process. + @retval EFI_ALREADY_STARTED Some other Dhcp6 instance already started the DHCPv6 + standard process. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Start ( + IN EFI_DHCP6_PROTOCOL *This + ); + +/** + Stops the DHCPv6 standard S.A.R.R. process. + + The Stop() function is used to stop the DHCPv6 standard process. After this + function is called successfully, the state of Dhcp6 instance is transferred + into the Dhcp6Init. EFI_DHCP6_PROTOCOL.Configure() needs to be called + before DHCPv6 standard process can be started again. This function can be + called when the Dhcp6 instance is in any state. + + @param[in] This The pointer to the Dhcp6 protocol. + + @retval EFI_SUCCESS The Dhcp6 instance is now in the Dhcp6Init state. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Stop ( + IN EFI_DHCP6_PROTOCOL *This + ); + +/** + Returns the current operating mode data for the Dhcp6 instance. + + The GetModeData() function returns the current operating mode and + cached data packet for the Dhcp6 instance. + + @param[in] This The pointer to the Dhcp6 protocol. + @param[out] Dhcp6ModeData The pointer to the Dhcp6 mode data. + @param[out] Dhcp6ConfigData The pointer to the Dhcp6 configure data. + + @retval EFI_SUCCESS The mode data was returned. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_ACCESS_DENIED The EFI DHCPv6 Protocol instance has not + been configured when Dhcp6ConfigData is + not NULL. +**/ +EFI_STATUS +EFIAPI +EfiDhcp6GetModeData ( + IN EFI_DHCP6_PROTOCOL *This, + OUT EFI_DHCP6_MODE_DATA *Dhcp6ModeData OPTIONAL, + OUT EFI_DHCP6_CONFIG_DATA *Dhcp6ConfigData OPTIONAL + ); + +/** + Initializes, changes, or resets the operational settings for the Dhcp6 instance. + + The Configure() function is used to initialize or clean up the configuration + data of the Dhcp6 instance: + - When Dhcp6CfgData is not NULL and Configure() is called successfully, the + configuration data will be initialized in the Dhcp6 instance and the state + of the configured IA will be transferred into Dhcp6Init. + - When Dhcp6CfgData is NULL and Configure() is called successfully, the + configuration data will be cleaned up and no IA will be associated with + the Dhcp6 instance. + To update the configuration data for an Dhcp6 instance, the original data + must be cleaned up before setting the new configuration data. + + @param[in] This The pointer to the Dhcp6 protocol + @param[in] Dhcp6CfgData The pointer to the EFI_DHCP6_CONFIG_DATA. + + @retval EFI_SUCCESS The Dhcp6 is configured successfully with the + Dhcp6Init state, or cleaned up the original + configuration setting. + @retval EFI_ACCESS_DENIED The Dhcp6 instance has been already configured + when Dhcp6CfgData is not NULL. + The Dhcp6 instance has already started the + DHCPv6 S.A.R.R when Dhcp6CfgData is NULL. + @retval EFI_INVALID_PARAMETER Some of the parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Configure ( + IN EFI_DHCP6_PROTOCOL *This, + IN EFI_DHCP6_CONFIG_DATA *Dhcp6CfgData OPTIONAL + ); + +/** + Request configuration information without the assignment of any + Ia addresses of the client. + + The InfoRequest() function is used to request configuration information + without the assignment of any IPv6 address of the client. Client sends + out Information Request packet to obtain the required configuration + information, and DHCPv6 server responds with Reply packet containing + the information for the client. The received Reply packet will be passed + to the user by ReplyCallback function. If user returns EFI_NOT_READY from + ReplyCallback, the Dhcp6 instance will continue to receive other Reply + packets unless timeout according to the Retransmission parameter. + Otherwise, the Information Request exchange process will be finished + successfully if user returns EFI_SUCCESS from ReplyCallback. + + @param[in] This The pointer to the Dhcp6 protocol. + @param[in] SendClientId If TRUE, the DHCPv6 protocol instance will build Client + Identifier option and include it into Information Request + packet. Otherwise, Client Identifier option will not be included. + @param[in] OptionRequest The pointer to the buffer of option request options. + @param[in] OptionCount The option number in the OptionList. + @param[in] OptionList The list of appended options. + @param[in] Retransmission The pointer to the retransmission of the message. + @param[in] TimeoutEvent The event of timeout. + @param[in] ReplyCallback The callback function when a reply was received. + @param[in] CallbackContext The pointer to the parameter passed to the callback. + + @retval EFI_SUCCESS The DHCPv6 information request exchange process + completed when TimeoutEvent is NULL. Information + Request packet has been sent to DHCPv6 server when + TimeoutEvent is not NULL. + @retval EFI_NO_RESPONSE The DHCPv6 information request exchange process failed + because of no response, or not all requested-options + are responded to by DHCPv6 servers when Timeout happened. + @retval EFI_ABORTED The DHCPv6 information request exchange process was aborted + by the user. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6InfoRequest ( + IN EFI_DHCP6_PROTOCOL *This, + IN BOOLEAN SendClientId, + IN EFI_DHCP6_PACKET_OPTION *OptionRequest, + IN UINT32 OptionCount, + IN EFI_DHCP6_PACKET_OPTION *OptionList[] OPTIONAL, + IN EFI_DHCP6_RETRANSMISSION *Retransmission, + IN EFI_EVENT TimeoutEvent OPTIONAL, + IN EFI_DHCP6_INFO_CALLBACK ReplyCallback, + IN VOID *CallbackContext OPTIONAL + ); + +/** + Manually extend the valid and preferred lifetimes for the IPv6 addresses + of the configured IA and update other configuration parameters by sending + Renew or Rebind packet. + + The RenewRebind() function is used to manually extend the valid and preferred + lifetimes for the IPv6 addresses of the configured IA and update other + configuration parameters by sending a Renew or Rebind packet. + - When RebindRequest is FALSE and the state of the configured IA is Dhcp6Bound, + it will send Renew packet to the previously DHCPv6 server and transfer the + state of the configured IA to Dhcp6Renewing. If valid Reply packet received, + the state transfers to Dhcp6Bound and the valid and preferred timer restarts. + If fails, the state transfers to Dhcp6Bound but the timer continues. + - When RebindRequest is TRUE and the state of the configured IA is Dhcp6Bound, + it will send a Rebind packet. If a valid Reply packet is received, the state transfers + to Dhcp6Bound, and the valid and preferred timer restarts. If it fails, the state + transfers to Dhcp6Init, and the IA can't be used. + + @param[in] This The pointer to the Dhcp6 protocol. + @param[in] RebindRequest If TRUE, Rebind packet will be sent and enter Dhcp6Rebinding state. + Otherwise, Renew packet will be sent and enter Dhcp6Renewing state. + + @retval EFI_SUCCESS The DHCPv6 renew/rebind exchange process + completed and at least one IPv6 address of the + configured IA was bound again when + EFI_DHCP6_CONFIG_DATA.IaInfoEvent was NULL. + The EFI DHCPv6 Protocol instance has sent Renew + or Rebind packet when + EFI_DHCP6_CONFIG_DATA.IaInfoEvent is not NULL. + @retval EFI_ACCESS_DENIED The Dhcp6 instance hasn't been configured, or the + state of the configured IA is not in Dhcp6Bound. + @retval EFI_ALREADY_STARTED The state of the configured IA has already entered + Dhcp6Renewing when RebindRequest is FALSE. + The state of the configured IA has already entered + Dhcp6Rebinding when RebindRequest is TRUE. + @retval EFI_ABORTED The DHCPv6 renew/rebind exchange process aborted + by user. + @retval EFI_NO_RESPONSE The DHCPv6 renew/rebind exchange process failed + because of no response. + @retval EFI_NO_MAPPING No IPv6 address has been bound to the configured + IA after the DHCPv6 renew/rebind exchange process. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6RenewRebind ( + IN EFI_DHCP6_PROTOCOL *This, + IN BOOLEAN RebindRequest + ); + +/** + Inform that one or more addresses assigned by a server are already + in use by another node. + + The Decline() function is used to manually decline the assignment of + IPv6 addresses, which have been already used by another node. If all + IPv6 addresses of the configured IA are declined through this function, + the state of the IA will switch through Dhcp6Declining to Dhcp6Init. + Otherwise, the state of the IA will restore to Dhcp6Bound after the + declining process. The Decline() can only be called when the IA is in + Dhcp6Bound state. If the EFI_DHCP6_CONFIG_DATA.IaInfoEvent is NULL, + this function is a blocking operation. It will return after the + declining process finishes, or aborted by user. + + @param[in] This The pointer to the Dhcp6 protocol. + @param[in] AddressCount The number of declining addresses. + @param[in] Addresses The pointer to the buffer stored the declining + addresses. + + @retval EFI_SUCCESS The DHCPv6 decline exchange process completed + when EFI_DHCP6_CONFIG_DATA.IaInfoEvent was NULL. + The Dhcp6 instance has sent Decline packet when + EFI_DHCP6_CONFIG_DATA.IaInfoEvent is not NULL. + @retval EFI_ACCESS_DENIED The Dhcp6 instance hasn't been configured, or the + state of the configured IA is not in Dhcp6Bound. + @retval EFI_ABORTED The DHCPv6 decline exchange process was aborted by the user. + @retval EFI_NOT_FOUND Any specified IPv6 address is not correlated with + the configured IA for this instance. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Decline ( + IN EFI_DHCP6_PROTOCOL *This, + IN UINT32 AddressCount, + IN EFI_IPv6_ADDRESS *Addresses + ); + +/** + Release one or more addresses associated with the configured Ia + for the current instance. + + The Release() function is used to manually release the one or more + IPv6 address. If AddressCount is zero, it will release all IPv6 + addresses of the configured IA. If all IPv6 addresses of the IA are + released through this function, the state of the IA will switch + through Dhcp6Releasing to Dhcp6Init, otherwise, the state of the + IA will restore to Dhcp6Bound after the releasing process. + The Release() can only be called when the IA is in a Dhcp6Bound state. + If the EFI_DHCP6_CONFIG_DATA.IaInfoEvent is NULL, the function is + a blocking operation. It will return after the releasing process + finishes, or aborted by user. + + @param[in] This The pointer to the Dhcp6 protocol. + @param[in] AddressCount The number of releasing addresses. + @param[in] Addresses The pointer to the buffer stored the releasing + addresses. + @retval EFI_SUCCESS The DHCPv6 release exchange process has + completed when EFI_DHCP6_CONFIG_DATA.IaInfoEvent + is NULL. The Dhcp6 instance has sent Release + packet when EFI_DHCP6_CONFIG_DATA.IaInfoEvent + is not NULL. + @retval EFI_ACCESS_DENIED The Dhcp6 instance hasn't been configured, or the + state of the configured IA is not in Dhcp6Bound. + @retval EFI_ABORTED The DHCPv6 release exchange process was aborted by the user. + @retval EFI_NOT_FOUND Any specified IPv6 address is not correlated with + the configured IA for this instance. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Release ( + IN EFI_DHCP6_PROTOCOL *This, + IN UINT32 AddressCount, + IN EFI_IPv6_ADDRESS *Addresses + ); + +/** + Parse the option data in the Dhcp6 packet. + + The Parse() function is used to retrieve the option list in the DHCPv6 packet. + + @param[in] This The pointer to the Dhcp6 protocol. + @param[in] Packet The pointer to the Dhcp6 packet. + @param[in, out] OptionCount The number of option in the packet. + @param[out] PacketOptionList The array of pointers to the each option in the packet. + + @retval EFI_SUCCESS The packet was successfully parsed. + @retval EFI_INVALID_PARAMETER Some parameter is NULL. + @retval EFI_BUFFER_TOO_SMALL *OptionCount is smaller than the number of options + that were found in the Packet. + +**/ +EFI_STATUS +EFIAPI +EfiDhcp6Parse ( + IN EFI_DHCP6_PROTOCOL *This, + IN EFI_DHCP6_PACKET *Packet, + IN OUT UINT32 *OptionCount, + OUT EFI_DHCP6_PACKET_OPTION *PacketOptionList[] OPTIONAL + ); + +#endif diff --git a/NetworkPkg/Dhcp6Dxe/Dhcp6Io.c b/NetworkPkg/Dhcp6Dxe/Dhcp6Io.c new file mode 100644 index 000000000..4f8393cb3 --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/Dhcp6Io.c @@ -0,0 +1,3168 @@ +/** @file + Dhcp6 internal functions implementation. + + (C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Dhcp6Impl.h" + + +/** + Enqueue the packet into the retry list in case of timeout. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] Packet The pointer to the Dhcp6 packet to retry. + @param[in] Elapsed The pointer to the elapsed time value in the packet. + @param[in] RetryCtl The pointer to the transmission control of the packet. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS Successfully enqueued the packet into the retry list according + to its message type. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected message type. + +**/ +EFI_STATUS +Dhcp6EnqueueRetry ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_PACKET *Packet, + IN UINT16 *Elapsed, + IN EFI_DHCP6_RETRANSMISSION *RetryCtl OPTIONAL + ) +{ + DHCP6_TX_CB *TxCb; + DHCP6_IA_CB *IaCb; + + ASSERT (Packet != NULL); + + IaCb = &Instance->IaCb; + TxCb = AllocateZeroPool (sizeof (DHCP6_TX_CB)); + + if (TxCb == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Save tx packet pointer, and it will be destroyed when reply received. + // + TxCb->TxPacket = Packet; + TxCb->Xid = Packet->Dhcp6.Header.TransactionId; + + // + // Save pointer to elapsed-time value so we can update it on retransmits. + // + TxCb->Elapsed = Elapsed; + + // + // Calculate the retransmission according to the the message type. + // + switch (Packet->Dhcp6.Header.MessageType) { + case Dhcp6MsgSolicit: + // + // Calculate the retransmission threshold value for solicit packet. + // Use the default value by rfc-3315 if user doesn't configure. + // + if (RetryCtl == NULL) { + TxCb->RetryCtl.Irt = DHCP6_SOL_IRT; + TxCb->RetryCtl.Mrc = DHCP6_SOL_MRC; + TxCb->RetryCtl.Mrt = DHCP6_SOL_MRT; + TxCb->RetryCtl.Mrd = DHCP6_SOL_MRD; + } else { + TxCb->RetryCtl.Irt = (RetryCtl->Irt != 0) ? RetryCtl->Irt : DHCP6_SOL_IRT; + TxCb->RetryCtl.Mrc = (RetryCtl->Mrc != 0) ? RetryCtl->Mrc : DHCP6_SOL_MRC; + TxCb->RetryCtl.Mrt = (RetryCtl->Mrt != 0) ? RetryCtl->Mrt : DHCP6_SOL_MRT; + TxCb->RetryCtl.Mrd = (RetryCtl->Mrd != 0) ? RetryCtl->Mrd : DHCP6_SOL_MRD; + } + + TxCb->RetryExp = Dhcp6CalculateExpireTime ( + TxCb->RetryCtl.Irt, + TRUE, + FALSE + ); + break; + + case Dhcp6MsgRequest: + // + // Calculate the retransmission threshold value for request packet. + // + TxCb->RetryCtl.Irt = DHCP6_REQ_IRT; + TxCb->RetryCtl.Mrc = DHCP6_REQ_MRC; + TxCb->RetryCtl.Mrt = DHCP6_REQ_MRT; + TxCb->RetryCtl.Mrd = DHCP6_REQ_MRD; + TxCb->RetryExp = Dhcp6CalculateExpireTime ( + TxCb->RetryCtl.Irt, + TRUE, + TRUE + ); + break; + + case Dhcp6MsgConfirm: + // + // Calculate the retransmission threshold value for confirm packet. + // + TxCb->RetryCtl.Irt = DHCP6_CNF_IRT; + TxCb->RetryCtl.Mrc = DHCP6_CNF_MRC; + TxCb->RetryCtl.Mrt = DHCP6_CNF_MRT; + TxCb->RetryCtl.Mrd = DHCP6_CNF_MRD; + TxCb->RetryExp = Dhcp6CalculateExpireTime ( + TxCb->RetryCtl.Irt, + TRUE, + TRUE + ); + break; + + case Dhcp6MsgRenew: + // + // Calculate the retransmission threshold value for renew packet. + // + TxCb->RetryCtl.Irt = DHCP6_REB_IRT; + TxCb->RetryCtl.Mrc = DHCP6_REB_MRC; + TxCb->RetryCtl.Mrt = DHCP6_REB_MRT; + TxCb->RetryCtl.Mrd = IaCb->T2 - IaCb->T1; + TxCb->RetryExp = Dhcp6CalculateExpireTime ( + TxCb->RetryCtl.Irt, + TRUE, + TRUE + ); + break; + + case Dhcp6MsgRebind: + // + // Calculate the retransmission threshold value for rebind packet. + // + TxCb->RetryCtl.Irt = DHCP6_REN_IRT; + TxCb->RetryCtl.Mrc = DHCP6_REN_MRC; + TxCb->RetryCtl.Mrt = DHCP6_REN_MRT; + TxCb->RetryCtl.Mrd = IaCb->AllExpireTime - IaCb->T2; + TxCb->RetryExp = Dhcp6CalculateExpireTime ( + TxCb->RetryCtl.Irt, + TRUE, + TRUE + ); + break; + + case Dhcp6MsgDecline: + // + // Calculate the retransmission threshold value for decline packet. + // + TxCb->RetryCtl.Irt = DHCP6_DEC_IRT; + TxCb->RetryCtl.Mrc = DHCP6_DEC_MRC; + TxCb->RetryCtl.Mrt = DHCP6_DEC_MRT; + TxCb->RetryCtl.Mrd = DHCP6_DEC_MRD; + TxCb->RetryExp = Dhcp6CalculateExpireTime ( + TxCb->RetryCtl.Irt, + TRUE, + TRUE + ); + break; + + case Dhcp6MsgRelease: + // + // Calculate the retransmission threshold value for release packet. + // + TxCb->RetryCtl.Irt = DHCP6_REL_IRT; + TxCb->RetryCtl.Mrc = DHCP6_REL_MRC; + TxCb->RetryCtl.Mrt = DHCP6_REL_MRT; + TxCb->RetryCtl.Mrd = DHCP6_REL_MRD; + TxCb->RetryExp = Dhcp6CalculateExpireTime ( + TxCb->RetryCtl.Irt, + TRUE, + TRUE + ); + break; + + case Dhcp6MsgInfoRequest: + // + // Calculate the retransmission threshold value for info-request packet. + // Use the default value by rfc-3315 if user doesn't configure. + // + if (RetryCtl == NULL) { + TxCb->RetryCtl.Irt = DHCP6_INF_IRT; + TxCb->RetryCtl.Mrc = DHCP6_INF_MRC; + TxCb->RetryCtl.Mrt = DHCP6_INF_MRT; + TxCb->RetryCtl.Mrd = DHCP6_INF_MRD; + } else { + TxCb->RetryCtl.Irt = (RetryCtl->Irt != 0) ? RetryCtl->Irt : DHCP6_INF_IRT; + TxCb->RetryCtl.Mrc = (RetryCtl->Mrc != 0) ? RetryCtl->Mrc : DHCP6_INF_MRC; + TxCb->RetryCtl.Mrt = (RetryCtl->Mrt != 0) ? RetryCtl->Mrt : DHCP6_INF_MRT; + TxCb->RetryCtl.Mrd = (RetryCtl->Mrd != 0) ? RetryCtl->Mrd : DHCP6_INF_MRD; + } + + TxCb->RetryExp = Dhcp6CalculateExpireTime ( + TxCb->RetryCtl.Irt, + TRUE, + TRUE + ); + break; + + default: + // + // Unexpected message type. + // + return EFI_DEVICE_ERROR; + } + + // + // Insert into the retransmit list of the instance. + // + InsertTailList (&Instance->TxList, &TxCb->Link); + + return EFI_SUCCESS; +} + + +/** + Dequeue the packet from retry list if reply received or timeout at last. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] PacketXid The packet transaction id to match. + @param[in] NeedSignal If TRUE, then an timeout event need be signaled when it is existed. + Otherwise, this parameter is ignored. + + @retval EFI_SUCCESS Successfully dequeued the packet into retry list . + @retval EFI_NOT_FOUND There is no xid matched in retry list. + +**/ +EFI_STATUS +Dhcp6DequeueRetry ( + IN DHCP6_INSTANCE *Instance, + IN UINT32 PacketXid, + IN BOOLEAN NeedSignal + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + DHCP6_TX_CB *TxCb; + DHCP6_INF_CB *InfCb; + + // + // Seek the retransmit node in the retransmit list by packet xid. + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->TxList) { + + TxCb = NET_LIST_USER_STRUCT (Entry, DHCP6_TX_CB, Link); + ASSERT(TxCb->TxPacket); + + if (TxCb->Xid == PacketXid) { + + if (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgInfoRequest) { + + // + // Seek the info-request node in the info-request list by packet xid. + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->InfList) { + + InfCb = NET_LIST_USER_STRUCT (Entry, DHCP6_INF_CB, Link); + + if (InfCb->Xid == PacketXid) { + // + // Remove the info-request node, and signal the event if timeout. + // + if (InfCb->TimeoutEvent != NULL && NeedSignal) { + gBS->SignalEvent (InfCb->TimeoutEvent); + } + + RemoveEntryList (&InfCb->Link); + FreePool (InfCb); + } + } + } + // + // Remove the retransmit node. + // + RemoveEntryList (&TxCb->Link); + ASSERT(TxCb->TxPacket); + FreePool (TxCb->TxPacket); + FreePool (TxCb); + return EFI_SUCCESS; + } + } + + return EFI_NOT_FOUND; +} + + +/** + Clean up the specific nodes in the retry list. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] Scope The scope of cleanup nodes. + +**/ +VOID +Dhcp6CleanupRetry ( + IN DHCP6_INSTANCE *Instance, + IN UINT32 Scope + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + DHCP6_TX_CB *TxCb; + DHCP6_INF_CB *InfCb; + + // + // Clean up all the stateful messages from the retransmit list. + // + if (Scope == DHCP6_PACKET_STATEFUL || Scope == DHCP6_PACKET_ALL) { + + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->TxList) { + + TxCb = NET_LIST_USER_STRUCT (Entry, DHCP6_TX_CB, Link); + ASSERT(TxCb->TxPacket); + + if (TxCb->TxPacket->Dhcp6.Header.MessageType != Dhcp6MsgInfoRequest) { + RemoveEntryList (&TxCb->Link); + FreePool (TxCb->TxPacket); + FreePool (TxCb); + } + } + } + + // + // Clean up all the stateless messages from the retransmit list. + // + if (Scope == DHCP6_PACKET_STATELESS || Scope == DHCP6_PACKET_ALL) { + + // + // Clean up all the retransmit list for stateless messages. + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->TxList) { + + TxCb = NET_LIST_USER_STRUCT (Entry, DHCP6_TX_CB, Link); + ASSERT(TxCb->TxPacket); + + if (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgInfoRequest) { + RemoveEntryList (&TxCb->Link); + FreePool (TxCb->TxPacket); + FreePool (TxCb); + } + } + + // + // Clean up all the info-request messages list. + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->InfList) { + + InfCb = NET_LIST_USER_STRUCT (Entry, DHCP6_INF_CB, Link); + + if (InfCb->TimeoutEvent != NULL) { + gBS->SignalEvent (InfCb->TimeoutEvent); + } + RemoveEntryList (&InfCb->Link); + FreePool (InfCb); + } + } +} + +/** + Check whether the TxCb is still a valid control block in the instance's retry list. + + @param[in] Instance The pointer to DHCP6_INSTANCE. + @param[in] TxCb The control block for a transmitted message. + + @retval TRUE The control block is in Instance's retry list. + @retval FALSE The control block is NOT in Instance's retry list. + +**/ +BOOLEAN +Dhcp6IsValidTxCb ( + IN DHCP6_INSTANCE *Instance, + IN DHCP6_TX_CB *TxCb + ) +{ + LIST_ENTRY *Entry; + + NET_LIST_FOR_EACH (Entry, &Instance->TxList) { + if (TxCb == NET_LIST_USER_STRUCT (Entry, DHCP6_TX_CB, Link)) { + return TRUE; + } + } + + return FALSE; +} + +/** + Clean up the session of the instance stateful exchange. + + @param[in, out] Instance The pointer to the Dhcp6 instance. + @param[in] Status The return status from udp. + +**/ +VOID +Dhcp6CleanupSession ( + IN OUT DHCP6_INSTANCE *Instance, + IN EFI_STATUS Status + ) +{ + UINTN Index; + EFI_DHCP6_IA *Ia; + + ASSERT(Instance->Config); + ASSERT(Instance->IaCb.Ia); + + // + // Clean up the retransmit list for stateful messages. + // + Dhcp6CleanupRetry (Instance, DHCP6_PACKET_STATEFUL); + + if (Instance->Unicast != NULL) { + FreePool (Instance->Unicast); + } + + if (Instance->AdSelect != NULL) { + FreePool (Instance->AdSelect); + } + + if (Instance->IaCb.Ia->ReplyPacket != NULL) { + FreePool (Instance->IaCb.Ia->ReplyPacket); + } + + // + // Reinitialize the Ia fields of the instance. + // + Instance->UdpSts = Status; + Instance->AdSelect = NULL; + Instance->AdPref = 0; + Instance->Unicast = NULL; + Instance->IaCb.T1 = 0; + Instance->IaCb.T2 = 0; + Instance->IaCb.AllExpireTime = 0; + Instance->IaCb.LeaseTime = 0; + + // + // Clear start time + // + Instance->StartTime = 0; + + Ia = Instance->IaCb.Ia; + Ia->State = Dhcp6Init; + Ia->ReplyPacket = NULL; + + // + // Set the addresses as zero lifetime, and then the notify + // function in Ip6Config will remove these timeout address. + // + for (Index = 0; Index < Ia->IaAddressCount; Index++) { + Ia->IaAddress[Index].PreferredLifetime = 0; + Ia->IaAddress[Index].ValidLifetime = 0; + } + + // + // + // Signal the Ia information updated event to informal user. + // + if (Instance->Config->IaInfoEvent != NULL) { + gBS->SignalEvent (Instance->Config->IaInfoEvent); + } +} + + +/** + Callback to user when Dhcp6 transmit/receive occurs. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] Event The current Dhcp6 event. + @param[in, out] Packet The pointer to the packet sending or received. + + @retval EFI_SUCCESS The user function returns success. + @retval EFI_NOT_READY Direct the caller to continue collecting the offer. + @retval EFI_ABORTED The user function ask it to abort. + +**/ +EFI_STATUS +EFIAPI +Dhcp6CallbackUser ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_EVENT Event, + IN OUT EFI_DHCP6_PACKET **Packet + ) +{ + EFI_STATUS Status; + EFI_DHCP6_PACKET *NewPacket; + EFI_DHCP6_CALLBACK Callback; + VOID *Context; + + ASSERT (Packet != NULL); + ASSERT (Instance->Config != NULL); + ASSERT (Instance->IaCb.Ia != NULL); + + NewPacket = NULL; + Status = EFI_SUCCESS; + Callback = Instance->Config->Dhcp6Callback; + Context = Instance->Config->CallbackContext; + + // + // Callback to user with the new message if has. + // + if (Callback != NULL) { + + Status = Callback ( + &Instance->Dhcp6, + Context, + Instance->IaCb.Ia->State, + Event, + *Packet, + &NewPacket + ); + // + // Updated the new packet from user to replace the original one. + // + if (NewPacket != NULL) { + ASSERT (*Packet != NULL); + FreePool (*Packet); + *Packet = NewPacket; + } + } + + return Status; +} + + +/** + Update Ia according to the new reply message. + + @param[in, out] Instance The pointer to the Dhcp6 instance. + @param[in] Packet The pointer to reply messages. + + @retval EFI_SUCCESS Updated the Ia information successfully. + @retval EFI_DEVICE_ERROR An unexpected error. + +**/ +EFI_STATUS +Dhcp6UpdateIaInfo ( + IN OUT DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_PACKET *Packet + ) +{ + EFI_STATUS Status; + UINT8 *Option; + UINT8 *IaInnerOpt; + UINT16 IaInnerLen; + UINT16 StsCode; + UINT32 T1; + UINT32 T2; + + ASSERT (Instance->Config != NULL); + // + // If the reply was received in reponse to a solicit with rapid commit option, + // request, renew or rebind message, the client updates the information it has + // recorded about IAs from the IA options contained in the reply message: + // 1. record the T1 and T2 times + // 2. add any new addresses in the IA + // 3. discard any addresses from the IA, that have a valid lifetime of 0 + // 4. update lifetimes for any addresses that alread recorded + // 5. leave unchanged any information about addresses + // + // See details in the section-18.1.8 of rfc-3315. + // + Option = Dhcp6SeekIaOption ( + Packet->Dhcp6.Option, + Packet->Length - sizeof (EFI_DHCP6_HEADER), + &Instance->Config->IaDescriptor + ); + if (Option == NULL) { + return EFI_DEVICE_ERROR; + } + + // + // The format of the IA_NA option is: + // + // 0 1 2 3 + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPTION_IA_NA | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | IAID (4 octets) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | T1 | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | T2 | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | | + // . IA_NA-options . + // . . + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + // The format of the IA_TA option is: + // + // 0 1 2 3 + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPTION_IA_TA | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | IAID (4 octets) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | | + // . IA_TA-options . + // . . + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + // + // sizeof (option-code + option-len + IaId) = 8 + // sizeof (option-code + option-len + IaId + T1) = 12 + // sizeof (option-code + option-len + IaId + T1 + T2) = 16 + // + // The inner options still start with 2 bytes option-code and 2 bytes option-len. + // + if (Instance->Config->IaDescriptor.Type == Dhcp6OptIana) { + T1 = NTOHL (ReadUnaligned32 ((UINT32 *) (Option + 8))); + T2 = NTOHL (ReadUnaligned32 ((UINT32 *) (Option + 12))); + // + // Refer to RFC3155 Chapter 22.4. If a client receives an IA_NA with T1 greater than T2, + // and both T1 and T2 are greater than 0, the client discards the IA_NA option and processes + // the remainder of the message as though the server had not included the invalid IA_NA option. + // + if (T1 > T2 && T2 > 0) { + return EFI_DEVICE_ERROR; + } + IaInnerOpt = Option + 16; + IaInnerLen = (UINT16) (NTOHS (ReadUnaligned16 ((UINT16 *) (Option + 2))) - 12); + } else { + T1 = 0; + T2 = 0; + IaInnerOpt = Option + 8; + IaInnerLen = (UINT16) (NTOHS (ReadUnaligned16 ((UINT16 *) (Option + 2))) - 4); + } + + // + // The format of the Status Code option is: + // + // 0 1 2 3 + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPTION_STATUS_CODE | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | status-code | | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | + // . . + // . status-message . + // . . + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + // + // sizeof (option-code + option-len) = 4 + // + StsCode = Dhcp6StsSuccess; + Option = Dhcp6SeekOption (IaInnerOpt, IaInnerLen, Dhcp6OptStatusCode); + + if (Option != NULL) { + StsCode = NTOHS (ReadUnaligned16 ((UINT16 *) (Option + 4))); + if (StsCode != Dhcp6StsSuccess) { + return EFI_DEVICE_ERROR; + } + } + + // + // Generate control block for the Ia. + // + Status = Dhcp6GenerateIaCb ( + Instance, + IaInnerOpt, + IaInnerLen, + T1, + T2 + ); + + return Status; +} + + + +/** + Seek StatusCode Option in package. A Status Code option may appear in the + options field of a DHCP message and/or in the options field of another option. + See details in section 22.13, RFC3315. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] Packet The pointer to reply messages. + @param[out] Option The pointer to status code option. + + @retval EFI_SUCCESS Seek status code option successfully. + @retval EFI_DEVICE_ERROR An unexpected error. + +**/ +EFI_STATUS +Dhcp6SeekStsOption ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_PACKET *Packet, + OUT UINT8 **Option + ) +{ + UINT8 *IaInnerOpt; + UINT16 IaInnerLen; + UINT16 StsCode; + + // + // Seek StatusCode option directly in DHCP message body. That is, search in + // non-encapsulated option fields. + // + *Option = Dhcp6SeekOption ( + Packet->Dhcp6.Option, + Packet->Length - 4, + Dhcp6OptStatusCode + ); + + if (*Option != NULL) { + StsCode = NTOHS (ReadUnaligned16 ((UINT16 *) (*Option + 4))); + if (StsCode != Dhcp6StsSuccess) { + return EFI_DEVICE_ERROR; + } + } + + // + // Seek in encapsulated options, IA_NA and IA_TA. + // + *Option = Dhcp6SeekIaOption ( + Packet->Dhcp6.Option, + Packet->Length - sizeof (EFI_DHCP6_HEADER), + &Instance->Config->IaDescriptor + ); + if (*Option == NULL) { + return EFI_SUCCESS; + } + + // + // The format of the IA_NA option is: + // + // 0 1 2 3 + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPTION_IA_NA | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | IAID (4 octets) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | T1 | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | T2 | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | | + // . IA_NA-options . + // . . + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + // The format of the IA_TA option is: + // + // 0 1 2 3 + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPTION_IA_TA | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | IAID (4 octets) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | | + // . IA_TA-options . + // . . + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + // + // sizeof (option-code + option-len + IaId) = 8 + // sizeof (option-code + option-len + IaId + T1) = 12 + // sizeof (option-code + option-len + IaId + T1 + T2) = 16 + // + // The inner options still start with 2 bytes option-code and 2 bytes option-len. + // + if (Instance->Config->IaDescriptor.Type == Dhcp6OptIana) { + IaInnerOpt = *Option + 16; + IaInnerLen = (UINT16) (NTOHS (ReadUnaligned16 ((UINT16 *) (*Option + 2))) - 12); + } else { + IaInnerOpt = *Option + 8; + IaInnerLen = (UINT16) (NTOHS (ReadUnaligned16 ((UINT16 *) (*Option + 2))) - 4); + } + + // + // The format of the Status Code option is: + // + // 0 1 2 3 + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPTION_STATUS_CODE | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | status-code | | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | + // . . + // . status-message . + // . . + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + // + // sizeof (option-code + option-len) = 4 + // + *Option = Dhcp6SeekOption (IaInnerOpt, IaInnerLen, Dhcp6OptStatusCode); + if (*Option != NULL) { + StsCode = NTOHS (ReadUnaligned16 ((UINT16 *) (*Option + 4))); + if (StsCode != Dhcp6StsSuccess) { + return EFI_DEVICE_ERROR; + } + } + + return EFI_SUCCESS; +} + + +/** + Transmit Dhcp6 message by udpio. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] Packet The pointer to transmit message. + @param[in] Elapsed The pointer to the elapsed time value to fill in. + + @retval EFI_SUCCESS Successfully transmitted the packet. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Dhcp6TransmitPacket ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_PACKET *Packet, + IN UINT16 *Elapsed + ) +{ + EFI_STATUS Status; + NET_BUF *Wrap; + NET_FRAGMENT Frag; + UDP_END_POINT EndPt; + DHCP6_SERVICE *Service; + + Service = Instance->Service; + + // + // Wrap it into a netbuf then send it. + // + Frag.Bulk = (UINT8 *) &Packet->Dhcp6.Header; + Frag.Len = Packet->Length; + + // + // Do not register free packet here, which will be handled in retry list. + // + Wrap = NetbufFromExt (&Frag, 1, 0, 0, Dhcp6DummyExtFree, NULL); + + if (Wrap == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Multicast the Dhcp6 message, unless get the unicast server address by option. + // + ZeroMem (&EndPt, sizeof (UDP_END_POINT)); + + if (Instance->Unicast != NULL) { + CopyMem ( + &EndPt.RemoteAddr, + Instance->Unicast, + sizeof (EFI_IPv6_ADDRESS) + ); + } else { + CopyMem ( + &EndPt.RemoteAddr, + &mAllDhcpRelayAndServersAddress, + sizeof (EFI_IPv6_ADDRESS) + ); + } + + EndPt.RemotePort = DHCP6_PORT_SERVER; + EndPt.LocalPort = DHCP6_PORT_CLIENT; + + // + // Update the elapsed time value. + // + if (Elapsed != NULL) { + SetElapsedTime (Elapsed, Instance); + } + + // + // Send out the message by the configured Udp6Io. + // + Status = UdpIoSendDatagram ( + Service->UdpIo, + Wrap, + &EndPt, + NULL, + Dhcp6OnTransmitted, + NULL + ); + + if (EFI_ERROR (Status)) { + NetbufFree (Wrap); + return Status; + } + + return EFI_SUCCESS; +} + + +/** + Create the solicit message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + + @retval EFI_SUCCESS Created and sent the solicit message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval Others Failed to send the solicit message. + +**/ +EFI_STATUS +Dhcp6SendSolicitMsg ( + IN DHCP6_INSTANCE *Instance + ) +{ + EFI_STATUS Status; + EFI_DHCP6_PACKET *Packet; + EFI_DHCP6_PACKET_OPTION *UserOpt; + EFI_DHCP6_DUID *ClientId; + DHCP6_SERVICE *Service; + UINT8 *Cursor; + UINT16 *Elapsed; + UINT32 UserLen; + UINTN Index; + UINT16 Length; + + Service = Instance->Service; + ClientId = Service->ClientId; + UserLen = 0; + + ASSERT (Service->ClientId != NULL); + ASSERT (Instance->Config != NULL); + ASSERT (Instance->IaCb.Ia != NULL); + + // + // Calculate the added length of customized option list. + // + for (Index = 0; Index < Instance->Config->OptionCount; Index++) { + UserLen += (NTOHS (Instance->Config->OptionList[Index]->OpLen) + 4); + } + + // + // Create the Dhcp6 packet and initialize commone fields. + // + Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen; + Packet->Length = sizeof (EFI_DHCP6_HEADER); + Packet->Dhcp6.Header.MessageType = Dhcp6MsgSolicit; + Packet->Dhcp6.Header.TransactionId = Service->Xid++; + + // + // Assembly Dhcp6 options for solicit message. + // + Cursor = Packet->Dhcp6.Option; + + Length = HTONS (ClientId->Length); + Cursor = Dhcp6AppendOption ( + Cursor, + HTONS (Dhcp6OptClientId), + Length, + ClientId->Duid + ); + + Cursor = Dhcp6AppendETOption ( + Cursor, + Instance, + &Elapsed + ); + + Cursor = Dhcp6AppendIaOption ( + Cursor, + Instance->IaCb.Ia, + Instance->IaCb.T1, + Instance->IaCb.T2, + Packet->Dhcp6.Header.MessageType + ); + + // + // Append user-defined when configurate Dhcp6 service. + // + for (Index = 0; Index < Instance->Config->OptionCount; Index++) { + + UserOpt = Instance->Config->OptionList[Index]; + Cursor = Dhcp6AppendOption( + Cursor, + UserOpt->OpCode, + UserOpt->OpLen, + UserOpt->Data + ); + } + + // + // Determine the size/length of packet. + // + Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option); + ASSERT (Packet->Size > Packet->Length + 8); + + // + // Callback to user with the packet to be sent and check the user's feedback. + // + Status = Dhcp6CallbackUser (Instance, Dhcp6SendSolicit, &Packet); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Send solicit packet with the state transition from Dhcp6init to + // Dhcp6selecting. + // + Instance->IaCb.Ia->State = Dhcp6Selecting; + // + // Clear initial time for current transaction. + // + Instance->StartTime = 0; + + Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Enqueue the sent packet for the retransmission in case reply timeout. + // + return Dhcp6EnqueueRetry ( + Instance, + Packet, + Elapsed, + Instance->Config->SolicitRetransmission + ); +} + +/** + Configure some parameter to initiate SolicitMsg. + + @param[in] Instance The pointer to the Dhcp6 instance. + + @retval EFI_SUCCESS Created and sent the solicit message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval Others Failed to send the solicit message. + +**/ +EFI_STATUS +Dhcp6InitSolicitMsg ( + IN DHCP6_INSTANCE *Instance + ) +{ + Instance->IaCb.T1 = 0; + Instance->IaCb.T2 = 0; + Instance->IaCb.Ia->IaAddressCount = 0; + + return Dhcp6SendSolicitMsg (Instance); +} + + +/** + Create the request message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + + @retval EFI_SUCCESS Created and sent the request message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others Failed to send the request message. + +**/ +EFI_STATUS +Dhcp6SendRequestMsg ( + IN DHCP6_INSTANCE *Instance + ) +{ + EFI_STATUS Status; + EFI_DHCP6_PACKET *Packet; + EFI_DHCP6_PACKET_OPTION *UserOpt; + EFI_DHCP6_DUID *ClientId; + EFI_DHCP6_DUID *ServerId; + DHCP6_SERVICE *Service; + UINT8 *Option; + UINT8 *Cursor; + UINT16 *Elapsed; + UINT32 UserLen; + UINTN Index; + UINT16 Length; + + ASSERT(Instance->AdSelect != NULL); + ASSERT(Instance->Config != NULL); + ASSERT(Instance->IaCb.Ia != NULL); + ASSERT(Instance->Service != NULL); + + Service = Instance->Service; + ClientId = Service->ClientId; + + ASSERT(ClientId != NULL); + + // + // Get the server Id from the selected advertisement message. + // + Option = Dhcp6SeekOption ( + Instance->AdSelect->Dhcp6.Option, + Instance->AdSelect->Length - 4, + Dhcp6OptServerId + ); + if (Option == NULL) { + return EFI_DEVICE_ERROR; + } + + ServerId = (EFI_DHCP6_DUID *) (Option + 2); + + // + // Calculate the added length of customized option list. + // + UserLen = 0; + for (Index = 0; Index < Instance->Config->OptionCount; Index++) { + UserLen += (NTOHS (Instance->Config->OptionList[Index]->OpLen) + 4); + } + + // + // Create the Dhcp6 packet and initialize commone fields. + // + Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen; + Packet->Length = sizeof (EFI_DHCP6_HEADER); + Packet->Dhcp6.Header.MessageType = Dhcp6MsgRequest; + Packet->Dhcp6.Header.TransactionId = Service->Xid++; + + // + // Assembly Dhcp6 options for request message. + // + Cursor = Packet->Dhcp6.Option; + + Length = HTONS (ClientId->Length); + Cursor = Dhcp6AppendOption ( + Cursor, + HTONS (Dhcp6OptClientId), + Length, + ClientId->Duid + ); + + Cursor = Dhcp6AppendETOption ( + Cursor, + Instance, + &Elapsed + ); + + Cursor = Dhcp6AppendOption ( + Cursor, + HTONS (Dhcp6OptServerId), + ServerId->Length, + ServerId->Duid + ); + + Cursor = Dhcp6AppendIaOption ( + Cursor, + Instance->IaCb.Ia, + Instance->IaCb.T1, + Instance->IaCb.T2, + Packet->Dhcp6.Header.MessageType + ); + + // + // Append user-defined when configurate Dhcp6 service. + // + for (Index = 0; Index < Instance->Config->OptionCount; Index++) { + + UserOpt = Instance->Config->OptionList[Index]; + Cursor = Dhcp6AppendOption( + Cursor, + UserOpt->OpCode, + UserOpt->OpLen, + UserOpt->Data + ); + } + + // + // Determine the size/length of packet. + // + Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option); + ASSERT (Packet->Size > Packet->Length + 8); + + // + // Callback to user with the packet to be sent and check the user's feedback. + // + Status = Dhcp6CallbackUser (Instance, Dhcp6SendRequest, &Packet); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Send request packet with the state transition from Dhcp6selecting to + // Dhcp6requesting. + // + Instance->IaCb.Ia->State = Dhcp6Requesting; + // + // Clear initial time for current transaction. + // + Instance->StartTime = 0; + + Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Enqueue the sent packet for the retransmission in case reply timeout. + // + return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL); +} + + +/** + Create the decline message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] DecIa The pointer to the decline Ia. + + @retval EFI_SUCCESS Created and sent the decline message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others Failed to send the decline message. + +**/ +EFI_STATUS +Dhcp6SendDeclineMsg ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_IA *DecIa + ) +{ + EFI_STATUS Status; + EFI_DHCP6_PACKET *Packet; + EFI_DHCP6_PACKET *LastReply; + EFI_DHCP6_DUID *ClientId; + EFI_DHCP6_DUID *ServerId; + DHCP6_SERVICE *Service; + UINT8 *Option; + UINT8 *Cursor; + UINT16 *Elapsed; + UINT16 Length; + + ASSERT (Instance->Config != NULL); + ASSERT (Instance->IaCb.Ia != NULL); + ASSERT (Instance->Service != NULL); + + Service = Instance->Service; + ClientId = Service->ClientId; + LastReply = Instance->IaCb.Ia->ReplyPacket; + + ASSERT (ClientId != NULL); + ASSERT (LastReply != NULL); + + // + // Get the server Id from the last reply message. + // + Option = Dhcp6SeekOption ( + LastReply->Dhcp6.Option, + LastReply->Length - 4, + Dhcp6OptServerId + ); + if (Option == NULL) { + return EFI_DEVICE_ERROR; + } + + // + // EFI_DHCP6_DUID contains a length field of 2 bytes. + // + ServerId = (EFI_DHCP6_DUID *) (Option + 2); + + // + // Create the Dhcp6 packet and initialize commone fields. + // + Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Packet->Size = DHCP6_BASE_PACKET_SIZE; + Packet->Length = sizeof (EFI_DHCP6_HEADER); + Packet->Dhcp6.Header.MessageType = Dhcp6MsgDecline; + Packet->Dhcp6.Header.TransactionId = Service->Xid++; + + // + // Assembly Dhcp6 options for rebind/renew message. + // + Cursor = Packet->Dhcp6.Option; + + Length = HTONS (ClientId->Length); + Cursor = Dhcp6AppendOption ( + Cursor, + HTONS (Dhcp6OptClientId), + Length, + ClientId->Duid + ); + + Cursor = Dhcp6AppendETOption ( + Cursor, + Instance, + &Elapsed + ); + + Cursor = Dhcp6AppendOption ( + Cursor, + HTONS (Dhcp6OptServerId), + ServerId->Length, + ServerId->Duid + ); + + Cursor = Dhcp6AppendIaOption (Cursor, DecIa, 0, 0, Packet->Dhcp6.Header.MessageType); + + // + // Determine the size/length of packet. + // + Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option); + ASSERT (Packet->Size > Packet->Length + 8); + + // + // Callback to user with the packet to be sent and check the user's feedback. + // + Status = Dhcp6CallbackUser (Instance, Dhcp6SendDecline, &Packet); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Send decline packet with the state transition from Dhcp6bound to + // Dhcp6declining. + // + Instance->IaCb.Ia->State = Dhcp6Declining; + // + // Clear initial time for current transaction. + // + Instance->StartTime = 0; + + Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Enqueue the sent packet for the retransmission in case reply timeout. + // + return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL); +} + + +/** + Create the release message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] RelIa The pointer to the release Ia. + + @retval EFI_SUCCESS Created and sent the release message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others Failed to send the release message. + +**/ +EFI_STATUS +Dhcp6SendReleaseMsg ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_IA *RelIa + ) +{ + EFI_STATUS Status; + EFI_DHCP6_PACKET *Packet; + EFI_DHCP6_PACKET *LastReply; + EFI_DHCP6_DUID *ClientId; + EFI_DHCP6_DUID *ServerId; + DHCP6_SERVICE *Service; + UINT8 *Option; + UINT8 *Cursor; + UINT16 *Elapsed; + UINT16 Length; + + ASSERT(Instance->Config); + ASSERT(Instance->IaCb.Ia); + + Service = Instance->Service; + ClientId = Service->ClientId; + LastReply = Instance->IaCb.Ia->ReplyPacket; + + ASSERT(ClientId); + ASSERT(LastReply); + + // + // Get the server Id from the last reply message. + // + Option = Dhcp6SeekOption ( + LastReply->Dhcp6.Option, + LastReply->Length - 4, + Dhcp6OptServerId + ); + if (Option == NULL) { + return EFI_DEVICE_ERROR; + } + + ServerId = (EFI_DHCP6_DUID *) (Option + 2); + + // + // Create the Dhcp6 packet and initialize commone fields. + // + Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Packet->Size = DHCP6_BASE_PACKET_SIZE; + Packet->Length = sizeof (EFI_DHCP6_HEADER); + Packet->Dhcp6.Header.MessageType = Dhcp6MsgRelease; + Packet->Dhcp6.Header.TransactionId = Service->Xid++; + + // + // Assembly Dhcp6 options for rebind/renew message + // + Cursor = Packet->Dhcp6.Option; + + Length = HTONS (ClientId->Length); + Cursor = Dhcp6AppendOption ( + Cursor, + HTONS (Dhcp6OptClientId), + Length, + ClientId->Duid + ); + + // + // ServerId is extracted from packet, it's network order. + // + Cursor = Dhcp6AppendOption ( + Cursor, + HTONS (Dhcp6OptServerId), + ServerId->Length, + ServerId->Duid + ); + + Cursor = Dhcp6AppendETOption ( + Cursor, + Instance, + &Elapsed + ); + + Cursor = Dhcp6AppendIaOption (Cursor, RelIa, 0, 0, Packet->Dhcp6.Header.MessageType); + + // + // Determine the size/length of packet + // + Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option); + ASSERT (Packet->Size > Packet->Length + 8); + + // + // Callback to user with the packet to be sent and check the user's feedback. + // + Status = Dhcp6CallbackUser (Instance, Dhcp6SendRelease, &Packet); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Send release packet with the state transition from Dhcp6bound to + // Dhcp6releasing. + // + Instance->IaCb.Ia->State = Dhcp6Releasing; + + Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Enqueue the sent packet for the retransmission in case reply timeout. + // + return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL); +} + + +/** + Create the renew/rebind message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] RebindRequest If TRUE, it is a Rebind type message. + Otherwise, it is a Renew type message. + + @retval EFI_SUCCESS Created and sent the renew/rebind message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others Failed to send the renew/rebind message. + +**/ +EFI_STATUS +Dhcp6SendRenewRebindMsg ( + IN DHCP6_INSTANCE *Instance, + IN BOOLEAN RebindRequest + ) +{ + EFI_STATUS Status; + EFI_DHCP6_PACKET *Packet; + EFI_DHCP6_PACKET *LastReply; + EFI_DHCP6_PACKET_OPTION *UserOpt; + EFI_DHCP6_DUID *ClientId; + EFI_DHCP6_DUID *ServerId; + EFI_DHCP6_STATE State; + EFI_DHCP6_EVENT Event; + DHCP6_SERVICE *Service; + UINT8 *Option; + UINT8 *Cursor; + UINT16 *Elapsed; + UINT32 UserLen; + UINTN Index; + UINT16 Length; + + ASSERT(Instance->Config); + ASSERT(Instance->IaCb.Ia); + + Service = Instance->Service; + ClientId = Service->ClientId; + + ASSERT(ClientId); + + // + // Calculate the added length of customized option list. + // + UserLen = 0; + for (Index = 0; Index < Instance->Config->OptionCount; Index++) { + UserLen += (NTOHS (Instance->Config->OptionList[Index]->OpLen) + 4); + } + + // + // Create the Dhcp6 packet and initialize commone fields. + // + Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen; + Packet->Length = sizeof (EFI_DHCP6_HEADER); + Packet->Dhcp6.Header.MessageType = RebindRequest ? Dhcp6MsgRebind : Dhcp6MsgRenew; + Packet->Dhcp6.Header.TransactionId = Service->Xid++; + + // + // Assembly Dhcp6 options for rebind/renew message. + // + Cursor = Packet->Dhcp6.Option; + + Length = HTONS (ClientId->Length); + Cursor = Dhcp6AppendOption ( + Cursor, + HTONS (Dhcp6OptClientId), + Length, + ClientId->Duid + ); + + Cursor = Dhcp6AppendETOption ( + Cursor, + Instance, + &Elapsed + ); + + Cursor = Dhcp6AppendIaOption ( + Cursor, + Instance->IaCb.Ia, + Instance->IaCb.T1, + Instance->IaCb.T2, + Packet->Dhcp6.Header.MessageType + ); + + if (!RebindRequest) { + // + // Get the server Id from the last reply message and + // insert it for rebind request. + // + LastReply = Instance->IaCb.Ia->ReplyPacket; + ASSERT (LastReply); + + Option = Dhcp6SeekOption ( + LastReply->Dhcp6.Option, + LastReply->Length - 4, + Dhcp6OptServerId + ); + if (Option == NULL) { + FreePool (Packet); + return EFI_DEVICE_ERROR; + } + + ServerId = (EFI_DHCP6_DUID *) (Option + 2); + + Cursor = Dhcp6AppendOption ( + Cursor, + HTONS (Dhcp6OptServerId), + ServerId->Length, + ServerId->Duid + ); + } + + // + // Append user-defined when configurate Dhcp6 service. + // + for (Index = 0; Index < Instance->Config->OptionCount; Index++) { + + UserOpt = Instance->Config->OptionList[Index]; + Cursor = Dhcp6AppendOption( + Cursor, + UserOpt->OpCode, + UserOpt->OpLen, + UserOpt->Data + ); + } + + // + // Determine the size/length of packet. + // + Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option); + ASSERT (Packet->Size > Packet->Length + 8); + + // + // Callback to user with the packet to be sent and check the user's feedback. + // + State = (RebindRequest) ? Dhcp6Rebinding : Dhcp6Renewing; + Event = (RebindRequest) ? Dhcp6EnterRebinding : Dhcp6EnterRenewing; + + Status = Dhcp6CallbackUser (Instance, Event, &Packet); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Send renew/rebind packet with the state transition from Dhcp6bound to + // Dhcp6renew/rebind. + // And sync the lease time when send renew/rebind, in case that user send + // renew/rebind actively. + // + Instance->IaCb.Ia->State = State; + Instance->IaCb.LeaseTime = (RebindRequest) ? Instance->IaCb.T2 : Instance->IaCb.T1; + // + // Clear initial time for current transaction. + // + Instance->StartTime = 0; + + Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Enqueue the sent packet for the retransmission in case reply timeout. + // + return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL); +} + +/** + Start the information request process. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] SendClientId If TRUE, the client identifier option will be included in + information request message. Otherwise, the client identifier + option will not be included. + @param[in] OptionRequest The pointer to the option request option. + @param[in] OptionCount The number options in the OptionList. + @param[in] OptionList The array pointers to the appended options. + @param[in] Retransmission The pointer to the retransmission control. + @param[in] TimeoutEvent The event of timeout. + @param[in] ReplyCallback The callback function when the reply was received. + @param[in] CallbackContext The pointer to the parameter passed to the callback. + + @retval EFI_SUCCESS Start the info-request process successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_NO_MAPPING No source address is available for use. + @retval Others Failed to start the info-request process. + +**/ +EFI_STATUS +Dhcp6StartInfoRequest ( + IN DHCP6_INSTANCE *Instance, + IN BOOLEAN SendClientId, + IN EFI_DHCP6_PACKET_OPTION *OptionRequest, + IN UINT32 OptionCount, + IN EFI_DHCP6_PACKET_OPTION *OptionList[] OPTIONAL, + IN EFI_DHCP6_RETRANSMISSION *Retransmission, + IN EFI_EVENT TimeoutEvent OPTIONAL, + IN EFI_DHCP6_INFO_CALLBACK ReplyCallback, + IN VOID *CallbackContext OPTIONAL + ) +{ + EFI_STATUS Status; + DHCP6_INF_CB *InfCb; + DHCP6_SERVICE *Service; + EFI_TPL OldTpl; + + Service = Instance->Service; + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Instance->UdpSts = EFI_ALREADY_STARTED; + // + // Create and initialize the control block for the info-request. + // + InfCb = AllocateZeroPool (sizeof(DHCP6_INF_CB)); + + if (InfCb == NULL) { + gBS->RestoreTPL (OldTpl); + return EFI_OUT_OF_RESOURCES; + } + + InfCb->ReplyCallback = ReplyCallback; + InfCb->CallbackContext = CallbackContext; + InfCb->TimeoutEvent = TimeoutEvent; + + InsertTailList (&Instance->InfList, &InfCb->Link); + + // + // Send the info-request message to start exchange process. + // + Status = Dhcp6SendInfoRequestMsg ( + Instance, + InfCb, + SendClientId, + OptionRequest, + OptionCount, + OptionList, + Retransmission + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Register receive callback for the stateless exchange process. + // + Status = UdpIoRecvDatagram( + Service->UdpIo, + Dhcp6ReceivePacket, + Service, + 0 + ); + + if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) { + goto ON_ERROR; + } + + gBS->RestoreTPL (OldTpl); + return EFI_SUCCESS; + +ON_ERROR: + gBS->RestoreTPL (OldTpl); + RemoveEntryList (&InfCb->Link); + FreePool (InfCb); + + return Status; +} + +/** + Create the information request message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] InfCb The pointer to the information request control block. + @param[in] SendClientId If TRUE, the client identifier option will be included in + information request message. Otherwise, the client identifier + option will not be included. + @param[in] OptionRequest The pointer to the option request option. + @param[in] OptionCount The number options in the OptionList. + @param[in] OptionList The array pointers to the appended options. + @param[in] Retransmission The pointer to the retransmission control. + + @retval EFI_SUCCESS Created and sent the info-request message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval Others Failed to send the info-request message. + +**/ +EFI_STATUS +Dhcp6SendInfoRequestMsg ( + IN DHCP6_INSTANCE *Instance, + IN DHCP6_INF_CB *InfCb, + IN BOOLEAN SendClientId, + IN EFI_DHCP6_PACKET_OPTION *OptionRequest, + IN UINT32 OptionCount, + IN EFI_DHCP6_PACKET_OPTION *OptionList[], + IN EFI_DHCP6_RETRANSMISSION *Retransmission + ) +{ + EFI_STATUS Status; + EFI_DHCP6_PACKET *Packet; + EFI_DHCP6_PACKET_OPTION *UserOpt; + EFI_DHCP6_DUID *ClientId; + DHCP6_SERVICE *Service; + UINT8 *Cursor; + UINT16 *Elapsed; + UINT32 UserLen; + UINTN Index; + UINT16 Length; + + ASSERT(OptionRequest); + + Service = Instance->Service; + ClientId = Service->ClientId; + UserLen = NTOHS (OptionRequest->OpLen) + 4; + + ASSERT(ClientId); + + // + // Calculate the added length of customized option list. + // + for (Index = 0; Index < OptionCount; Index++) { + UserLen += (NTOHS (OptionList[Index]->OpLen) + 4); + } + + // + // Create the Dhcp6 packet and initialize commone fields. + // + Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen; + Packet->Length = sizeof (EFI_DHCP6_HEADER); + Packet->Dhcp6.Header.MessageType = Dhcp6MsgInfoRequest; + Packet->Dhcp6.Header.TransactionId = Service->Xid++; + + InfCb->Xid = Packet->Dhcp6.Header.TransactionId; + + // + // Assembly Dhcp6 options for info-request message. + // + Cursor = Packet->Dhcp6.Option; + + if (SendClientId) { + Length = HTONS (ClientId->Length); + Cursor = Dhcp6AppendOption ( + Cursor, + HTONS (Dhcp6OptClientId), + Length, + ClientId->Duid + ); + } + + Cursor = Dhcp6AppendETOption ( + Cursor, + Instance, + &Elapsed + ); + + Cursor = Dhcp6AppendOption ( + Cursor, + OptionRequest->OpCode, + OptionRequest->OpLen, + OptionRequest->Data + ); + + // + // Append user-defined when configurate Dhcp6 service. + // + for (Index = 0; Index < OptionCount; Index++) { + + UserOpt = OptionList[Index]; + Cursor = Dhcp6AppendOption( + Cursor, + UserOpt->OpCode, + UserOpt->OpLen, + UserOpt->Data + ); + } + + // + // Determine the size/length of packet. + // + Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option); + ASSERT (Packet->Size > Packet->Length + 8); + + // + // Clear initial time for current transaction. + // + Instance->StartTime = 0; + + // + // Send info-request packet with no state. + // + Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Enqueue the sent packet for the retransmission in case reply timeout. + // + return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, Retransmission); +} + + +/** + Create the Confirm message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + + @retval EFI_SUCCESS Created and sent the confirm message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others Failed to send the confirm message. + +**/ +EFI_STATUS +Dhcp6SendConfirmMsg ( + IN DHCP6_INSTANCE *Instance + ) +{ + UINT8 *Cursor; + UINTN Index; + UINT16 Length; + UINT32 UserLen; + EFI_STATUS Status; + DHCP6_SERVICE *Service; + EFI_DHCP6_DUID *ClientId; + EFI_DHCP6_PACKET *Packet; + EFI_DHCP6_PACKET_OPTION *UserOpt; + UINT16 *Elapsed; + + ASSERT (Instance->Config != NULL); + ASSERT (Instance->IaCb.Ia != NULL); + ASSERT (Instance->Service != NULL); + + Service = Instance->Service; + ClientId = Service->ClientId; + ASSERT (ClientId != NULL); + + // + // Calculate the added length of customized option list. + // + UserLen = 0; + for (Index = 0; Index < Instance->Config->OptionCount; Index++) { + UserLen += (NTOHS (Instance->Config->OptionList[Index]->OpLen) + 4); + } + + // + // Create the Dhcp6 packet and initialize common fields. + // + Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen; + Packet->Length = sizeof (EFI_DHCP6_HEADER); + Packet->Dhcp6.Header.MessageType = Dhcp6MsgConfirm; + Packet->Dhcp6.Header.TransactionId = Service->Xid++; + + // + // Assembly Dhcp6 options for solicit message. + // + Cursor = Packet->Dhcp6.Option; + + Length = HTONS (ClientId->Length); + Cursor = Dhcp6AppendOption ( + Cursor, + HTONS (Dhcp6OptClientId), + Length, + ClientId->Duid + ); + + Cursor = Dhcp6AppendETOption ( + Cursor, + Instance, + &Elapsed + ); + + Cursor = Dhcp6AppendIaOption ( + Cursor, + Instance->IaCb.Ia, + Instance->IaCb.T1, + Instance->IaCb.T2, + Packet->Dhcp6.Header.MessageType + ); + + // + // Append user-defined when configurate Dhcp6 service. + // + for (Index = 0; Index < Instance->Config->OptionCount; Index++) { + UserOpt = Instance->Config->OptionList[Index]; + Cursor = Dhcp6AppendOption ( + Cursor, + UserOpt->OpCode, + UserOpt->OpLen, + UserOpt->Data + ); + } + + // + // Determine the size/length of packet. + // + Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option); + ASSERT (Packet->Size > Packet->Length + 8); + + // + // Callback to user with the packet to be sent and check the user's feedback. + // + Status = Dhcp6CallbackUser (Instance, Dhcp6SendConfirm, &Packet); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Send confirm packet with the state transition from Dhcp6Bound to + // Dhcp6Confirming. + // + Instance->IaCb.Ia->State = Dhcp6Confirming; + // + // Clear initial time for current transaction. + // + Instance->StartTime = 0; + + Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed); + + if (EFI_ERROR (Status)) { + FreePool (Packet); + return Status; + } + + // + // Enqueue the sent packet for the retransmission in case reply timeout. + // + return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL); +} + + + +/** + Handle with the Dhcp6 reply message. + + @param[in] Instance The pointer to Dhcp6 instance. + @param[in] Packet The pointer to the Dhcp6 reply message. + + @retval EFI_SUCCESS Processed the reply message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others Failed to process the reply message. + +**/ +EFI_STATUS +Dhcp6HandleReplyMsg ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_PACKET *Packet + ) +{ + EFI_STATUS Status; + UINT8 *Option; + UINT16 StsCode; + + ASSERT (Instance->Config != NULL); + ASSERT (Instance->IaCb.Ia != NULL); + ASSERT (Packet != NULL); + + Status = EFI_SUCCESS; + + if (Packet->Dhcp6.Header.MessageType != Dhcp6MsgReply) { + return EFI_DEVICE_ERROR; + } + + // + // If the client subsequently receives a valid reply message that includes a + // rapid commit option since send a solicit with rapid commit option before, + // preocess the reply message and discard any reply messages received in + // response to the request message. + // See details in the section-17.1.4 of rfc-3315. + // + Option = Dhcp6SeekOption ( + Packet->Dhcp6.Option, + Packet->Length - 4, + Dhcp6OptRapidCommit + ); + + if ((Option != NULL && !Instance->Config->RapidCommit) || (Option == NULL && Instance->Config->RapidCommit)) { + return EFI_DEVICE_ERROR; + } + + // + // As to a valid reply packet in response to a request/renew/rebind packet, + // ignore the packet if not contains the Ia option + // + if (Instance->IaCb.Ia->State == Dhcp6Requesting || + Instance->IaCb.Ia->State == Dhcp6Renewing || + Instance->IaCb.Ia->State == Dhcp6Rebinding + ) { + + Option = Dhcp6SeekIaOption ( + Packet->Dhcp6.Option, + Packet->Length, + &Instance->Config->IaDescriptor + ); + if (Option == NULL) { + return EFI_SUCCESS; + } + } + + // + // Callback to user with the received packet and check the user's feedback. + // + Status = Dhcp6CallbackUser (Instance, Dhcp6RcvdReply, &Packet); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // When receive a valid reply packet in response to a decline/release packet, + // the client considers the decline/release event completed regardless of the + // status code. + // + if (Instance->IaCb.Ia->State == Dhcp6Declining || Instance->IaCb.Ia->State == Dhcp6Releasing) { + + if (Instance->IaCb.Ia->IaAddressCount != 0) { + Instance->IaCb.Ia->State = Dhcp6Bound; + } else { + ASSERT (Instance->IaCb.Ia->ReplyPacket); + FreePool (Instance->IaCb.Ia->ReplyPacket); + Instance->IaCb.Ia->ReplyPacket = NULL; + Instance->IaCb.Ia->State = Dhcp6Init; + } + + // + // For sync, set the success flag out of polling in decline/release. + // + Instance->UdpSts = EFI_SUCCESS; + + // + // For async, signal the Ia event to inform Ia infomation update. + // + if (Instance->Config->IaInfoEvent != NULL) { + gBS->SignalEvent (Instance->Config->IaInfoEvent); + } + + // + // Reset start time for next exchange. + // + Instance->StartTime = 0; + + Status = EFI_SUCCESS; + goto ON_EXIT; + } + + // + // Upon the receipt of a valid reply packet in response to a solicit, request, + // confirm, renew and rebind, the behavior depends on the status code option. + // See the details in the section-18.1.8 of rfc-3315. + // + Option = NULL; + Status = Dhcp6SeekStsOption ( + Instance, + Packet, + &Option + ); + + if (!EFI_ERROR (Status)) { + // + // No status code or no error status code means succeed to reply. + // + Status = Dhcp6UpdateIaInfo (Instance, Packet); + if (!EFI_ERROR (Status)) { + // + // Reset start time for next exchange. + // + Instance->StartTime = 0; + + // + // Set bound state and store the reply packet. + // + if (Instance->IaCb.Ia->ReplyPacket != NULL) { + FreePool (Instance->IaCb.Ia->ReplyPacket); + } + + Instance->IaCb.Ia->ReplyPacket = AllocateZeroPool (Packet->Size); + + if (Instance->IaCb.Ia->ReplyPacket == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + CopyMem (Instance->IaCb.Ia->ReplyPacket, Packet, Packet->Size); + + Instance->IaCb.Ia->State = Dhcp6Bound; + + // + // For sync, set the success flag out of polling in start/renewrebind. + // + Instance->UdpSts = EFI_SUCCESS; + + // + // Maybe this is a new round DHCP process due to some reason, such as NotOnLink + // ReplyMsg for ConfirmMsg should triger new round to acquire new address. In that + // case, clear old address.ValidLifetime and append to new address. Therefore, DHCP + // consumers can be notified to flush old address. + // + Dhcp6AppendCacheIa (Instance); + + // + // For async, signal the Ia event to inform Ia infomation update. + // + if (Instance->Config->IaInfoEvent != NULL) { + gBS->SignalEvent (Instance->Config->IaInfoEvent); + } + } else if (Status == EFI_NOT_FOUND) { + // + // Refer to RFC3315 Chapter 18.1.8, for each IA in the original Renew or Rebind message, + // the client sends a Renew or Rebind if the IA is not in the Reply message. + // Return EFI_SUCCESS so we can continue to restart the Renew/Rebind process. + // + return EFI_SUCCESS; + } + + goto ON_EXIT; + + } else if (Option != NULL) { + // + // Any error status code option is found. + // + StsCode = NTOHS (ReadUnaligned16 ((UINT16 *) (Option + 4))); + switch (StsCode) { + case Dhcp6StsUnspecFail: + // + // It indicates the server is unable to process the message due to an + // unspecified failure condition, so just retry if possible. + // + break; + + case Dhcp6StsUseMulticast: + // + // It indicates the server receives a message via unicast from a client + // to which the server has not sent a unicast option, so retry it by + // multi-cast address. + // + if (Instance->Unicast != NULL) { + FreePool (Instance->Unicast); + Instance->Unicast = NULL; + } + break; + + case Dhcp6StsNotOnLink: + if (Instance->IaCb.Ia->State == Dhcp6Confirming) { + // + // Before initiate new round DHCP, cache the current IA. + // + Status = Dhcp6CacheIa (Instance); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Restart S.A.R.R process to acquire new address. + // + Status = Dhcp6InitSolicitMsg (Instance); + if (EFI_ERROR (Status)) { + return Status; + } + } + break; + + case Dhcp6StsNoBinding: + if (Instance->IaCb.Ia->State == Dhcp6Renewing || Instance->IaCb.Ia->State == Dhcp6Rebinding) { + // + // Refer to RFC3315 Chapter 18.1.8, for each IA in the original Renew or Rebind message, the client + // sends a Request message if the IA contained a Status Code option with the NoBinding status. + // + Status = Dhcp6SendRequestMsg(Instance); + if (EFI_ERROR (Status)) { + return Status; + } + } + break; + + default: + // + // The other status code, just restart solicitation. + // + break; + } + } + + return EFI_SUCCESS; + +ON_EXIT: + + if (!EFI_ERROR(Status)) { + Status = Dhcp6DequeueRetry ( + Instance, + Packet->Dhcp6.Header.TransactionId, + FALSE + ); + } + + return Status; +} + + +/** + Select the appointed Dhcp6 advertisement message. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] AdSelect The pointer to the selected Dhcp6 advertisement message. + + @retval EFI_SUCCESS Selected the right advertisement message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval Others Failed to select the advertise message. + +**/ +EFI_STATUS +Dhcp6SelectAdvertiseMsg ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_PACKET *AdSelect + ) +{ + EFI_STATUS Status; + UINT8 *Option; + + ASSERT (AdSelect != NULL); + + // + // Callback to user with the selected advertisement packet, and the user + // might overwrite it. + // + Status = Dhcp6CallbackUser (Instance, Dhcp6SelectAdvertise, &AdSelect); + + if (EFI_ERROR (Status)) { + return Status; + } + + Instance->AdSelect = AdSelect; + + // + // Dequeue the sent packet for the retransmission since advertisement selected. + // + Status = Dhcp6DequeueRetry ( + Instance, + AdSelect->Dhcp6.Header.TransactionId, + FALSE + ); + + if (EFI_ERROR(Status)) { + return Status; + } + + // + // Check whether there is server unicast option in the selected advertise + // packet, and update it. + // + Option = Dhcp6SeekOption( + AdSelect->Dhcp6.Option, + AdSelect->Length - 4, + Dhcp6OptServerUnicast + ); + + if (Option != NULL) { + + Instance->Unicast = AllocateZeroPool (sizeof(EFI_IPv6_ADDRESS)); + + if (Instance->Unicast == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (Instance->Unicast, Option + 4, sizeof(EFI_IPv6_ADDRESS)); + } + + // + // Update the information of the Ia by the selected advertisement message. + // + Status = Dhcp6UpdateIaInfo (Instance, AdSelect); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Send the request message to continue the S.A.R.R. process. + // + return Dhcp6SendRequestMsg (Instance); +} + + +/** + Handle with the Dhcp6 advertisement message. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] Packet The pointer to the Dhcp6 advertisement message. + + @retval EFI_SUCCESS Processed the advertisement message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others Failed to process the advertise message. + +**/ +EFI_STATUS +Dhcp6HandleAdvertiseMsg ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_PACKET *Packet + ) +{ + EFI_STATUS Status; + UINT8 *Option; + BOOLEAN Timeout; + + ASSERT(Instance->Config); + ASSERT(Instance->IaCb.Ia); + + Timeout = FALSE; + + // + // If the client does receives a valid reply message that includes a rapid + // commit option since a solicit with rapid commit optioin sent before, select + // this reply message. Or else, process the advertise messages as normal. + // See details in the section-17.1.4 of rfc-3315. + // + Option = Dhcp6SeekOption( + Packet->Dhcp6.Option, + Packet->Length - 4, + Dhcp6OptRapidCommit + ); + + if (Option != NULL && Instance->Config->RapidCommit && Packet->Dhcp6.Header.MessageType == Dhcp6MsgReply) { + + return Dhcp6HandleReplyMsg (Instance, Packet); + } + + if (Packet->Dhcp6.Header.MessageType != Dhcp6MsgAdvertise) { + return EFI_DEVICE_ERROR; + } + + // + // Client must ignore any advertise message that includes a status code option + // containing the value noaddrsavail, with the exception that the client may + // display the associated status message to the user. + // See the details in the section-17.1.3 of rfc-3315. + // + Status = Dhcp6SeekStsOption ( + Instance, + Packet, + &Option + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + // + // Callback to user with the received packet and check the user's feedback. + // + Status = Dhcp6CallbackUser (Instance, Dhcp6RcvdAdvertise, &Packet); + + if (!EFI_ERROR (Status)) { + // + // Success means user choose the current advertisement packet. + // + if (Instance->AdSelect != NULL) { + FreePool (Instance->AdSelect); + } + + // + // Store the selected advertisement packet and set a flag. + // + Instance->AdSelect = AllocateZeroPool (Packet->Size); + + if (Instance->AdSelect == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (Instance->AdSelect, Packet, Packet->Size); + + Instance->AdPref = 0xff; + + } else if (Status == EFI_NOT_READY) { + // + // Not_ready means user wants to continue to receive more advertise packets. + // + if (Instance->AdPref == 0xff && Instance->AdSelect == NULL) { + // + // It's a tricky point. The timer routine set adpref as 0xff if the first + // rt timeout and no advertisement received, which means any advertisement + // received will be selected after the first rt. + // + Timeout = TRUE; + } + + // + // Check whether the current packet has a 255 preference option or not. + // Take non-preference option as 0 value. + // + Option = Dhcp6SeekOption( + Packet->Dhcp6.Option, + Packet->Length - 4, + Dhcp6OptPreference + ); + + if (Instance->AdSelect == NULL || (Option != NULL && *(Option + 4) > Instance->AdPref)) { + // + // No advertisements received before or preference is more than other + // advertisements received before. Then store the new packet and the + // preference value. + // + if (Instance->AdSelect != NULL) { + FreePool (Instance->AdSelect); + } + + Instance->AdSelect = AllocateZeroPool (Packet->Size); + + if (Instance->AdSelect == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (Instance->AdSelect, Packet, Packet->Size); + + if (Option != NULL) { + Instance->AdPref = *(Option + 4); + } + } else { + // + // Non-preference and other advertisements received before or current + // preference is less than other advertisements received before. + // Leave the packet alone. + } + + } else { + // + // Other error status means termination. + // + return Status; + } + + // + // Client must collect advertise messages as more as possible until the first + // RT has elapsed, or get a highest preference 255 advertise. + // See details in the section-17.1.2 of rfc-3315. + // + if (Instance->AdPref == 0xff || Timeout) { + Status = Dhcp6SelectAdvertiseMsg (Instance, Instance->AdSelect); + } + + return Status; +} + + +/** + The Dhcp6 stateful exchange process routine. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] Packet The pointer to the received Dhcp6 message. + +**/ +VOID +Dhcp6HandleStateful ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_PACKET *Packet + ) +{ + EFI_STATUS Status; + EFI_DHCP6_DUID *ClientId; + DHCP6_SERVICE *Service; + UINT8 *Option; + + Service = Instance->Service; + ClientId = Service->ClientId; + Status = EFI_SUCCESS; + + if (Instance->Config == NULL) { + goto ON_CONTINUE; + } + + ASSERT (ClientId); + ASSERT (Instance->Config); + ASSERT (Instance->IaCb.Ia); + + // + // Discard the packet if not advertisement or reply packet. + // + if (Packet->Dhcp6.Header.MessageType != Dhcp6MsgAdvertise && Packet->Dhcp6.Header.MessageType != Dhcp6MsgReply) { + goto ON_CONTINUE; + } + + // + // Check whether include client Id or not. + // + Option = Dhcp6SeekOption( + Packet->Dhcp6.Option, + Packet->Length - 4, + Dhcp6OptClientId + ); + + if (Option == NULL || CompareMem (Option + 4, ClientId->Duid, ClientId->Length) != 0) { + goto ON_CONTINUE; + } + + // + // Check whether include server Id or not. + // + Option = Dhcp6SeekOption( + Packet->Dhcp6.Option, + Packet->Length - 4, + Dhcp6OptServerId + ); + + if (Option == NULL) { + goto ON_CONTINUE; + } + + switch (Instance->IaCb.Ia->State) { + case Dhcp6Selecting: + // + // Handle the advertisement message when in the Dhcp6Selecting state. + // Do not need check return status, if failed, just continue to the next. + // + Dhcp6HandleAdvertiseMsg (Instance, Packet); + break; + + case Dhcp6Requesting: + case Dhcp6Confirming: + case Dhcp6Renewing: + case Dhcp6Rebinding: + case Dhcp6Releasing: + case Dhcp6Declining: + // + // Handle the reply message when in the Dhcp6Requesting, Dhcp6Renewing + // Dhcp6Rebinding, Dhcp6Releasing and Dhcp6Declining state. + // If failed here, it should reset the current session. + // + Status = Dhcp6HandleReplyMsg (Instance, Packet); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + break; + default: + // + // Other state has not supported yet. + // + break; + } + +ON_CONTINUE: + // + // Continue to receive the following Dhcp6 message. + // + Status = UdpIoRecvDatagram ( + Service->UdpIo, + Dhcp6ReceivePacket, + Service, + 0 + ); +ON_EXIT: + if (EFI_ERROR (Status)) { + Dhcp6CleanupSession (Instance, Status); + } +} + + +/** + The Dhcp6 stateless exchange process routine. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] Packet The pointer to the received Dhcp6 message. + +**/ +VOID +Dhcp6HandleStateless ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_PACKET *Packet + ) +{ + EFI_STATUS Status; + DHCP6_SERVICE *Service; + DHCP6_INF_CB *InfCb; + UINT8 *Option; + BOOLEAN IsMatched; + + Service = Instance->Service; + Status = EFI_SUCCESS; + IsMatched = FALSE; + InfCb = NULL; + + if (Packet->Dhcp6.Header.MessageType != Dhcp6MsgReply) { + goto ON_EXIT; + } + + // + // Check whether it's a desired Info-request message by Xid. + // + while (!IsListEmpty (&Instance->InfList)) { + InfCb = NET_LIST_HEAD (&Instance->InfList, DHCP6_INF_CB, Link); + if (InfCb->Xid == Packet->Dhcp6.Header.TransactionId) { + IsMatched = TRUE; + break; + } + } + + if (!IsMatched) { + goto ON_EXIT; + } + + // + // Check whether include server Id or not. + // + Option = Dhcp6SeekOption ( + Packet->Dhcp6.Option, + Packet->Length - 4, + Dhcp6OptServerId + ); + + if (Option == NULL) { + goto ON_EXIT; + } + + // + // Callback to user with the received packet and check the user's feedback. + // + Status = InfCb->ReplyCallback ( + &Instance->Dhcp6, + InfCb->CallbackContext, + Packet + ); + + if (Status == EFI_NOT_READY) { + // + // Success or aborted will both stop this info-request exchange process, + // but not ready means user wants to continue to receive reply. + // + goto ON_EXIT; + } + + // + // Dequeue the sent packet from the txlist if the xid matched, and ignore + // if no xid matched. + // + Dhcp6DequeueRetry ( + Instance, + Packet->Dhcp6.Header.TransactionId, + FALSE + ); + + // + // For sync, set the status out of polling for info-request. + // + Instance->UdpSts = Status; + +ON_EXIT: + + Status = UdpIoRecvDatagram ( + Service->UdpIo, + Dhcp6ReceivePacket, + Service, + 0 + ); + + if (EFI_ERROR (Status)) { + Dhcp6CleanupRetry (Instance, DHCP6_PACKET_STATELESS); + } +} + + +/** + The receive callback function for Dhcp6 exchange process. + + @param[in] Udp6Wrap The pointer to the received net buffer. + @param[in] EndPoint The pointer to the udp end point. + @param[in] IoStatus The return status from udp io. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +Dhcp6ReceivePacket ( + IN NET_BUF *Udp6Wrap, + IN UDP_END_POINT *EndPoint, + IN EFI_STATUS IoStatus, + IN VOID *Context + ) +{ + EFI_DHCP6_HEADER *Head; + EFI_DHCP6_PACKET *Packet; + DHCP6_SERVICE *Service; + DHCP6_INSTANCE *Instance; + DHCP6_TX_CB *TxCb; + UINT32 Size; + BOOLEAN IsDispatched; + BOOLEAN IsStateless; + LIST_ENTRY *Entry1; + LIST_ENTRY *Next1; + LIST_ENTRY *Entry2; + LIST_ENTRY *Next2; + EFI_STATUS Status; + + ASSERT (Udp6Wrap != NULL); + ASSERT (Context != NULL); + + Service = (DHCP6_SERVICE *) Context; + Instance = NULL; + Packet = NULL; + IsDispatched = FALSE; + IsStateless = FALSE; + + if (EFI_ERROR (IoStatus)) { + return ; + } + + if (Udp6Wrap->TotalSize < sizeof (EFI_DHCP6_HEADER)) { + goto ON_CONTINUE; + } + + // + // Copy the net buffer received from upd6 to a Dhcp6 packet. + // + Size = sizeof (EFI_DHCP6_PACKET) + Udp6Wrap->TotalSize; + Packet = (EFI_DHCP6_PACKET *) AllocateZeroPool (Size); + + if (Packet == NULL) { + goto ON_CONTINUE; + } + + Packet->Size = Size; + Head = &Packet->Dhcp6.Header; + Packet->Length = NetbufCopy (Udp6Wrap, 0, Udp6Wrap->TotalSize, (UINT8 *) Head); + + if (Packet->Length == 0) { + goto ON_CONTINUE; + } + + // + // Dispatch packet to right instance by transaction id. + // + NET_LIST_FOR_EACH_SAFE (Entry1, Next1, &Service->Child) { + + Instance = NET_LIST_USER_STRUCT (Entry1, DHCP6_INSTANCE, Link); + + NET_LIST_FOR_EACH_SAFE (Entry2, Next2, &Instance->TxList) { + + TxCb = NET_LIST_USER_STRUCT (Entry2, DHCP6_TX_CB, Link); + + if (Packet->Dhcp6.Header.TransactionId == TxCb->Xid) { + // + // Find the corresponding packet in tx list, and check it whether belongs + // to stateful exchange process. + // + if (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgInfoRequest) { + IsStateless = TRUE; + } + IsDispatched = TRUE; + break; + } + } + + if (IsDispatched) { + break; + } + } + + // + // Skip this packet if not dispatched to any instance. + // + if (!IsDispatched) { + goto ON_CONTINUE; + } + + // + // Dispatch the received packet ot the right instance. + // + if (IsStateless) { + Dhcp6HandleStateless (Instance, Packet); + } else { + Dhcp6HandleStateful (Instance, Packet); + } + +ON_CONTINUE: + + if (!IsDispatched) { + Status = UdpIoRecvDatagram ( + Service->UdpIo, + Dhcp6ReceivePacket, + Service, + 0 + ); + if (EFI_ERROR (Status)) { + NET_LIST_FOR_EACH_SAFE (Entry1, Next1, &Service->Child) { + Instance = NET_LIST_USER_STRUCT (Entry1, DHCP6_INSTANCE, Link); + Dhcp6CleanupRetry (Instance, DHCP6_PACKET_ALL); + } + } + } + + NetbufFree (Udp6Wrap); + + if (Packet != NULL) { + FreePool (Packet); + } +} + +/** + Detect Link movement for specified network device. + + This routine will try to invoke Snp->GetStatus() to get the media status. + If media present status switches from unpresent to present, a link movement + is detected. Note that the underlying UNDI driver may not support reporting + media status from GET_STATUS command. If that, fail to detect link movement. + + @param[in] Instance The pointer to DHCP6_INSTANCE. + + @retval TRUE A link movement is detected. + @retval FALSE A link movement is not detected. + +**/ +BOOLEAN +Dhcp6LinkMovDetect ( + IN DHCP6_INSTANCE *Instance + ) +{ + UINT32 InterruptStatus; + BOOLEAN MediaPresent; + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + + ASSERT (Instance != NULL); + Snp = Instance->Service->Snp; + MediaPresent = Instance->MediaPresent; + + // + // Check whether SNP support media detection + // + if (!Snp->Mode->MediaPresentSupported) { + return FALSE; + } + + // + // Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data + // + Status = Snp->GetStatus (Snp, &InterruptStatus, NULL); + if (EFI_ERROR (Status)) { + return FALSE; + } + + Instance->MediaPresent = Snp->Mode->MediaPresent; + // + // Media transimit Unpresent to Present means new link movement is detected. + // + if (!MediaPresent && Instance->MediaPresent) { + return TRUE; + } + return FALSE; +} + + +/** + The timer routine of the Dhcp6 instance for each second. + + @param[in] Event The timer event. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +Dhcp6OnTimerTick ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + DHCP6_INSTANCE *Instance; + DHCP6_TX_CB *TxCb; + DHCP6_IA_CB *IaCb; + UINT32 LossTime; + EFI_STATUS Status; + + ASSERT (Context != NULL); + + Instance = (DHCP6_INSTANCE *) Context; + + // + // 1. Loop the tx list, count live time of every tx packet to check whether + // need re-transmit or not. + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->TxList) { + + TxCb = NET_LIST_USER_STRUCT (Entry, DHCP6_TX_CB, Link); + + TxCb->TickTime++; + + if (TxCb->TickTime > TxCb->RetryExp) { + // + // Handle the first rt in the transmission of solicit specially. + // + if ((TxCb->RetryCnt == 0 || TxCb->SolicitRetry) && TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgSolicit) { + if (Instance->AdSelect == NULL) { + // + // Set adpref as 0xff here to indicate select any advertisement + // afterwards. + // + Instance->AdPref = 0xff; + } else { + // + // Select the advertisement received before. + // + Status = Dhcp6SelectAdvertiseMsg (Instance, Instance->AdSelect); + if (Status == EFI_ABORTED) { + goto ON_CLOSE; + } else if (EFI_ERROR (Status)) { + TxCb->RetryCnt++; + } + return; + } + } + // + // Increase the retry count for the packet and add up the total loss time. + // + TxCb->RetryCnt++; + TxCb->RetryLos += TxCb->RetryExp; + + // + // Check whether overflow the max retry count limit for this packet + // + if (TxCb->RetryCtl.Mrc != 0 && TxCb->RetryCtl.Mrc < TxCb->RetryCnt) { + Status = EFI_NO_RESPONSE; + goto ON_CLOSE; + } + + // + // Check whether overflow the max retry duration for this packet + // + if (TxCb->RetryCtl.Mrd != 0 && TxCb->RetryCtl.Mrd <= TxCb->RetryLos) { + Status = EFI_NO_RESPONSE; + goto ON_CLOSE; + } + + // + // Re-calculate retry expire timeout for the next time. + // + // Firstly, Check the new calculated time whether overflow the max retry + // expire time. + // + TxCb->RetryExp = Dhcp6CalculateExpireTime ( + TxCb->RetryExp, + FALSE, + TRUE + ); + + if (TxCb->RetryCtl.Mrt != 0 && TxCb->RetryCtl.Mrt < TxCb->RetryExp) { + TxCb->RetryExp = Dhcp6CalculateExpireTime ( + TxCb->RetryCtl.Mrt, + TRUE, + TRUE + ); + } + + // + // Secondly, Check the new calculated time whether overflow the max retry + // duration time. + // + LossTime = TxCb->RetryLos + TxCb->RetryExp; + if (TxCb->RetryCtl.Mrd != 0 && TxCb->RetryCtl.Mrd < LossTime) { + TxCb->RetryExp = TxCb->RetryCtl.Mrd - TxCb->RetryLos; + } + + // + // Reset the tick time for the next retransmission + // + TxCb->TickTime = 0; + + // + // Retransmit the last sent packet again. + // + Dhcp6TransmitPacket (Instance, TxCb->TxPacket, TxCb->Elapsed); + TxCb->SolicitRetry = FALSE; + if (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgSolicit) { + TxCb->SolicitRetry = TRUE; + } + } + } + + // + // 2. Check the configured Ia, count lease time of every valid Ia to check + // whether need to renew or rebind this Ia. + // + IaCb = &Instance->IaCb; + + if (Instance->Config == NULL || IaCb->Ia == NULL) { + return; + } + + if (IaCb->Ia->State == Dhcp6Bound || IaCb->Ia->State == Dhcp6Renewing || IaCb->Ia->State == Dhcp6Rebinding) { + + IaCb->LeaseTime++; + + if (IaCb->LeaseTime > IaCb->T2 && IaCb->Ia->State == Dhcp6Bound) { + // + // Exceed t2, send rebind packet to extend the Ia lease. + // + Dhcp6SendRenewRebindMsg (Instance, TRUE); + + } else if (IaCb->LeaseTime > IaCb->T1 && IaCb->Ia->State == Dhcp6Bound) { + + // + // Exceed t1, send renew packet to extend the Ia lease. + // + Dhcp6SendRenewRebindMsg (Instance, FALSE); + } + } + + // + // 3. In any situation when a client may have moved to a new link, the + // client MUST initiate a Confirm/Reply message exchange. + // + if (Dhcp6LinkMovDetect (Instance) && (IaCb->Ia->State == Dhcp6Bound)) { + Dhcp6SendConfirmMsg (Instance); + } + + return; + + ON_CLOSE: + + if (Dhcp6IsValidTxCb (Instance, TxCb) && + TxCb->TxPacket != NULL && + (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgInfoRequest || + TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgRenew || + TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgConfirm) + ) { + // + // The failure of renew/Confirm will still switch to the bound state. + // + if ((TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgRenew) || + (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgConfirm)) { + ASSERT (Instance->IaCb.Ia); + Instance->IaCb.Ia->State = Dhcp6Bound; + } + // + // The failure of info-request will return no response. + // + if (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgInfoRequest) { + Instance->UdpSts = EFI_NO_RESPONSE; + } + Dhcp6DequeueRetry ( + Instance, + TxCb->Xid, + TRUE + ); + } else { + // + // The failure of the others will terminate current state machine if timeout. + // + Dhcp6CleanupSession (Instance, Status); + } +} diff --git a/NetworkPkg/Dhcp6Dxe/Dhcp6Io.h b/NetworkPkg/Dhcp6Dxe/Dhcp6Io.h new file mode 100644 index 000000000..554f0f5e5 --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/Dhcp6Io.h @@ -0,0 +1,221 @@ +/** @file + Dhcp6 internal functions declaration. + + Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_DHCP6_IO_H__ +#define __EFI_DHCP6_IO_H__ + + +/** + Clean up the specific nodes in the retry list. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] Scope The scope of cleanup nodes. + +**/ +VOID +Dhcp6CleanupRetry ( + IN DHCP6_INSTANCE *Instance, + IN UINT32 Scope + ); + +/** + Clean up the session of the instance stateful exchange. + + @param[in, out] Instance The pointer to the Dhcp6 instance. + @param[in] Status The return status from udp. + +**/ +VOID +Dhcp6CleanupSession ( + IN OUT DHCP6_INSTANCE *Instance, + IN EFI_STATUS Status + ); + +/** + Create the solicit message and send it. + + @param[in] Instance The pointer to Dhcp6 instance. + + @retval EFI_SUCCESS Create and send the solicit message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval Others Failed to send the solicit message. + +**/ +EFI_STATUS +Dhcp6SendSolicitMsg ( + IN DHCP6_INSTANCE *Instance + ); + +/** + Create the request message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + + @retval EFI_SUCCESS Create and send the request message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others Failed to send the request message. + +**/ +EFI_STATUS +Dhcp6SendRequestMsg ( + IN DHCP6_INSTANCE *Instance + ); + +/** + Create the renew/rebind message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] RebindRequest If TRUE, it is a Rebind type message. + Otherwise, it is a Renew type message. + + @retval EFI_SUCCESS Create and send the renew/rebind message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others Failed to send the renew/rebind message. + +**/ +EFI_STATUS +Dhcp6SendRenewRebindMsg ( + IN DHCP6_INSTANCE *Instance, + IN BOOLEAN RebindRequest + ); + +/** + Create the decline message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] DecIa The pointer to the decline Ia. + + @retval EFI_SUCCESS Create and send the decline message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others Failed to send the decline message. + +**/ +EFI_STATUS +Dhcp6SendDeclineMsg ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_IA *DecIa + ); + +/** + Create the release message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] RelIa The pointer to the release Ia. + + @retval EFI_SUCCESS Create and send the release message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others Failed to send the release message. + +**/ +EFI_STATUS +Dhcp6SendReleaseMsg ( + IN DHCP6_INSTANCE *Instance, + IN EFI_DHCP6_IA *RelIa + ); + +/** + Start the information request process. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] SendClientId If TRUE, the client identifier option will be included in + information request message. Otherwise, the client identifier + option will not be included. + @param[in] OptionRequest The pointer to the option request option. + @param[in] OptionCount The number options in the OptionList. + @param[in] OptionList The array pointers to the appended options. + @param[in] Retransmission The pointer to the retransmission control. + @param[in] TimeoutEvent The event of timeout. + @param[in] ReplyCallback The callback function when the reply was received. + @param[in] CallbackContext The pointer to the parameter passed to the callback. + + @retval EFI_SUCCESS Start the info-request process successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_NO_MAPPING No source address is available for use. + @retval Others Failed to start the info-request process. + +**/ +EFI_STATUS +Dhcp6StartInfoRequest ( + IN DHCP6_INSTANCE *Instance, + IN BOOLEAN SendClientId, + IN EFI_DHCP6_PACKET_OPTION *OptionRequest, + IN UINT32 OptionCount, + IN EFI_DHCP6_PACKET_OPTION *OptionList[] OPTIONAL, + IN EFI_DHCP6_RETRANSMISSION *Retransmission, + IN EFI_EVENT TimeoutEvent OPTIONAL, + IN EFI_DHCP6_INFO_CALLBACK ReplyCallback, + IN VOID *CallbackContext OPTIONAL + ); + +/** + Create the information request message and send it. + + @param[in] Instance The pointer to the Dhcp6 instance. + @param[in] InfCb The pointer to the information request control block. + @param[in] SendClientId If TRUE, the client identifier option will be included in + information request message. Otherwise, the client identifier + option will not be included. + @param[in] OptionRequest The pointer to the option request option. + @param[in] OptionCount The number options in the OptionList. + @param[in] OptionList The array pointers to the appended options. + @param[in] Retransmission The pointer to the retransmission control. + + @retval EFI_SUCCESS Create and send the info-request message successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval Others Failed to send the info-request message. + +**/ +EFI_STATUS +Dhcp6SendInfoRequestMsg ( + IN DHCP6_INSTANCE *Instance, + IN DHCP6_INF_CB *InfCb, + IN BOOLEAN SendClientId, + IN EFI_DHCP6_PACKET_OPTION *OptionRequest, + IN UINT32 OptionCount, + IN EFI_DHCP6_PACKET_OPTION *OptionList[], + IN EFI_DHCP6_RETRANSMISSION *Retransmission + ); + +/** + The receive callback function for the Dhcp6 exchange process. + + @param[in] Udp6Wrap The pointer to the received net buffer. + @param[in] EndPoint The pointer to the udp end point. + @param[in] IoStatus The return status from udp io. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +Dhcp6ReceivePacket ( + IN NET_BUF *Udp6Wrap, + IN UDP_END_POINT *EndPoint, + IN EFI_STATUS IoStatus, + IN VOID *Context + ); + +/** + The timer routine of the Dhcp6 instance for each second. + + @param[in] Event The timer event. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +Dhcp6OnTimerTick ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +#endif diff --git a/NetworkPkg/Dhcp6Dxe/Dhcp6Utility.c b/NetworkPkg/Dhcp6Dxe/Dhcp6Utility.c new file mode 100644 index 000000000..a8a938211 --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/Dhcp6Utility.c @@ -0,0 +1,1324 @@ +/** @file + Dhcp6 support functions implementation. + + (C) Copyright 2015 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Dhcp6Impl.h" + + +/** + Generate client Duid in the format of Duid-llt. + + @param[in] Mode The pointer to the mode of SNP. + + @retval NULL If it failed to generate a client Id. + @retval others The pointer to the new client id. + +**/ +EFI_DHCP6_DUID * +Dhcp6GenerateClientId ( + IN EFI_SIMPLE_NETWORK_MODE *Mode + ) +{ + EFI_STATUS Status; + EFI_DHCP6_DUID *Duid; + EFI_TIME Time; + UINT32 Stamp; + EFI_GUID Uuid; + + + // + // Attempt to get client Id from variable to keep it constant. + // See details in section-9 of rfc-3315. + // + GetVariable2 (L"ClientId", &gEfiDhcp6ServiceBindingProtocolGuid, (VOID**)&Duid, NULL); + if (Duid != NULL) { + return Duid; + } + + // + // The format of client identifier option: + // + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPTION_CLIENTID | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // . . + // . DUID . + // . (variable length) . + // . . + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + // + // If System UUID is found from SMBIOS Table, use DUID-UUID type. + // + if ((PcdGet8 (PcdDhcp6UidType) == Dhcp6DuidTypeUuid) && !EFI_ERROR (NetLibGetSystemGuid (&Uuid)) && !CompareGuid (&Uuid, &gZeroGuid)) { + // + // + // The format of DUID-UUID: + // + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | DUID-Type (4) | UUID (128 bits) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | + // | | + // | | + // | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + + // + // sizeof (option-len + Duid-type + UUID-size) = 20 bytes + // + Duid = AllocateZeroPool (2 + 2 + sizeof (EFI_GUID)); + if (Duid == NULL) { + return NULL; + } + + // + // sizeof (Duid-type + UUID-size) = 18 bytes + // + Duid->Length = (UINT16) (18); + + // + // Set the Duid-type and copy UUID. + // + WriteUnaligned16 ((UINT16 *) (Duid->Duid), HTONS (Dhcp6DuidTypeUuid)); + + CopyMem (Duid->Duid + 2, &Uuid, sizeof(EFI_GUID)); + + } else { + + // + // + // The format of DUID-LLT: + // + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | Duid type (1) | hardware type (16 bits) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | time (32 bits) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // . . + // . link-layer address (variable length) . + // . . + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + // + // Generate a time stamp of the seconds from 2000/1/1, assume 30day/month. + // + gRT->GetTime (&Time, NULL); + Stamp = (UINT32) + ( + ((((UINT32)(Time.Year - 2000) * 360 + (Time.Month - 1) * 30 + (Time.Day - 1)) * 24 + Time.Hour) * 60 + Time.Minute) * + 60 + + Time.Second + ); + + // + // sizeof (option-len + Duid-type + hardware-type + time) = 10 bytes + // + Duid = AllocateZeroPool (10 + Mode->HwAddressSize); + if (Duid == NULL) { + return NULL; + } + + // + // sizeof (Duid-type + hardware-type + time) = 8 bytes + // + Duid->Length = (UINT16) (Mode->HwAddressSize + 8); + + // + // Set the Duid-type, hardware-type, time and copy the hardware address. + // + WriteUnaligned16 ((UINT16 *) ((UINT8 *) Duid + OFFSET_OF (EFI_DHCP6_DUID, Duid)), HTONS (Dhcp6DuidTypeLlt)); + WriteUnaligned16 ((UINT16 *) ((UINT8 *) Duid + OFFSET_OF (EFI_DHCP6_DUID, Duid) + 2), HTONS (NET_IFTYPE_ETHERNET)); + WriteUnaligned32 ((UINT32 *) ((UINT8 *) Duid + OFFSET_OF (EFI_DHCP6_DUID, Duid) + 4), HTONL (Stamp)); + + CopyMem (Duid->Duid + 8, &Mode->CurrentAddress, Mode->HwAddressSize); + } + + Status = gRT->SetVariable ( + L"ClientId", + &gEfiDhcp6ServiceBindingProtocolGuid, + (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS), + Duid->Length + 2, + (VOID *) Duid + ); + if (EFI_ERROR (Status)) { + FreePool (Duid); + return NULL; + } + + return Duid; +} + + +/** + Copy the Dhcp6 configure data. + + @param[in] DstCfg The pointer to the destination configure data. + @param[in] SorCfg The pointer to the source configure data. + + @retval EFI_SUCCESS Copy the content from SorCfg from DstCfg successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + +**/ +EFI_STATUS +Dhcp6CopyConfigData ( + IN EFI_DHCP6_CONFIG_DATA *DstCfg, + IN EFI_DHCP6_CONFIG_DATA *SorCfg + ) +{ + UINTN Index; + UINTN OptionListSize; + UINTN OptionSize; + + CopyMem (DstCfg, SorCfg, sizeof (EFI_DHCP6_CONFIG_DATA)); + + // + // Allocate another buffer for solicitretransmission, and copy it. + // + if (SorCfg->SolicitRetransmission != NULL) { + + DstCfg->SolicitRetransmission = AllocateZeroPool (sizeof (EFI_DHCP6_RETRANSMISSION)); + + if (DstCfg->SolicitRetransmission == NULL) { + // + // Error will be handled out of this function. + // + return EFI_OUT_OF_RESOURCES; + } + + CopyMem ( + DstCfg->SolicitRetransmission, + SorCfg->SolicitRetransmission, + sizeof (EFI_DHCP6_RETRANSMISSION) + ); + } + + if (SorCfg->OptionList != NULL && SorCfg->OptionCount != 0) { + + OptionListSize = SorCfg->OptionCount * sizeof (EFI_DHCP6_PACKET_OPTION *); + DstCfg->OptionList = AllocateZeroPool (OptionListSize); + + if (DstCfg->OptionList == NULL) { + // + // Error will be handled out of this function. + // + return EFI_OUT_OF_RESOURCES; + } + + for (Index = 0; Index < SorCfg->OptionCount; Index++) { + + OptionSize = NTOHS (SorCfg->OptionList[Index]->OpLen) + 4; + DstCfg->OptionList[Index] = AllocateZeroPool (OptionSize); + + if (DstCfg->OptionList[Index] == NULL) { + // + // Error will be handled out of this function. + // + return EFI_OUT_OF_RESOURCES; + } + + CopyMem ( + DstCfg->OptionList[Index], + SorCfg->OptionList[Index], + OptionSize + ); + } + } + + return EFI_SUCCESS; +} + + +/** + Clean up the configure data. + + @param[in, out] CfgData The pointer to the configure data. + +**/ +VOID +Dhcp6CleanupConfigData ( + IN OUT EFI_DHCP6_CONFIG_DATA *CfgData + ) +{ + UINTN Index; + + ASSERT (CfgData != NULL); + // + // Clean up all fields in config data including the reference buffers, but do + // not free the config data buffer itself. + // + if (CfgData->OptionList != NULL) { + for (Index = 0; Index < CfgData->OptionCount; Index++) { + if (CfgData->OptionList[Index] != NULL) { + FreePool (CfgData->OptionList[Index]); + } + } + FreePool (CfgData->OptionList); + } + + if (CfgData->SolicitRetransmission != NULL) { + FreePool (CfgData->SolicitRetransmission); + } + + ZeroMem (CfgData, sizeof (EFI_DHCP6_CONFIG_DATA)); +} + + +/** + Clean up the mode data. + + @param[in, out] ModeData The pointer to the mode data. + +**/ +VOID +Dhcp6CleanupModeData ( + IN OUT EFI_DHCP6_MODE_DATA *ModeData + ) +{ + ASSERT (ModeData != NULL); + // + // Clean up all fields in mode data including the reference buffers, but do + // not free the mode data buffer itself. + // + if (ModeData->ClientId != NULL) { + FreePool (ModeData->ClientId); + } + + if (ModeData->Ia != NULL) { + + if (ModeData->Ia->ReplyPacket != NULL) { + FreePool (ModeData->Ia->ReplyPacket); + } + FreePool (ModeData->Ia); + } + + ZeroMem (ModeData, sizeof (EFI_DHCP6_MODE_DATA)); +} + + +/** + Calculate the expire time by the algorithm defined in rfc. + + @param[in] Base The base value of the time. + @param[in] IsFirstRt If TRUE, it is the first time to calculate expire time. + @param[in] NeedSigned If TRUE, the the signed factor is needed. + + @return Expire The calculated result for the new expire time. + +**/ +UINT32 +Dhcp6CalculateExpireTime ( + IN UINT32 Base, + IN BOOLEAN IsFirstRt, + IN BOOLEAN NeedSigned + ) +{ + EFI_TIME Time; + BOOLEAN Signed; + UINT32 Seed; + UINT32 Expire; + + // + // Take the 10bits of microsecond in system time as a uniform distribution. + // Take the 10th bit as a flag to determine it's signed or not. + // + gRT->GetTime (&Time, NULL); + Seed = ((Time.Nanosecond >> 10) & DHCP6_10_BIT_MASK); + Signed = (BOOLEAN) ((((Time.Nanosecond >> 9) & 0x01) != 0) ? TRUE : FALSE); + Signed = (BOOLEAN) (NeedSigned ? Signed : FALSE); + + // + // Calculate expire by the following algo: + // 1. base + base * (-0.1 ~ 0) for the first solicit + // 2. base + base * (-0.1 ~ 0.1) for the first other messages + // 3. 2 * base + base * (-0.1 ~ 0.1) for the subsequent all messages + // 4. base + base * (-0.1 ~ 0) for the more than mrt timeout + // + // The (Seed / 0x3ff / 10) is used to a random range (0, 0.1). + // + if (IsFirstRt && Signed) { + + Expire = Base - (UINT32) (Base * Seed / DHCP6_10_BIT_MASK / 10); + + } else if (IsFirstRt && !Signed) { + + Expire = Base + (UINT32) (Base * Seed / DHCP6_10_BIT_MASK / 10); + + } else if (!IsFirstRt && Signed) { + + Expire = 2 * Base - (UINT32) (Base * Seed / DHCP6_10_BIT_MASK / 10); + + } else { + + Expire = 2 * Base + (UINT32) (Base * Seed / DHCP6_10_BIT_MASK / 10); + } + + Expire = (Expire != 0) ? Expire : 1; + + return Expire; +} + + +/** + Calculate the lease time by the algorithm defined in rfc. + + @param[in] IaCb The pointer to the Ia control block. + +**/ +VOID +Dhcp6CalculateLeaseTime ( + IN DHCP6_IA_CB *IaCb + ) +{ + UINT32 MinLt; + UINT32 MaxLt; + UINTN Index; + + ASSERT (IaCb->Ia->IaAddressCount > 0); + + MinLt = (UINT32) (-1); + MaxLt = 0; + + // + // Calculate minlt as min of all valid life time, and maxlt as max of all + // valid life time. + // + for (Index = 0; Index < IaCb->Ia->IaAddressCount; Index++) { + MinLt = MIN (MinLt, IaCb->Ia->IaAddress[Index].ValidLifetime); + MaxLt = MAX (MinLt, IaCb->Ia->IaAddress[Index].ValidLifetime); + } + + // + // Take 50% minlt as t1, and 80% maxlt as t2 if Dhcp6 server doesn't offer + // such information. + // + IaCb->T1 = (IaCb->T1 != 0) ? IaCb->T1 : (UINT32)(MinLt * 5 / 10); + IaCb->T2 = (IaCb->T2 != 0) ? IaCb->T2 : (UINT32)(MinLt * 8 / 10); + IaCb->AllExpireTime = MaxLt; + IaCb->LeaseTime = 0; +} + + +/** + Check whether the addresses are all included by the configured Ia. + + @param[in] Ia The pointer to the Ia. + @param[in] AddressCount The number of addresses. + @param[in] Addresses The pointer to the addresses buffer. + + @retval EFI_SUCCESS The addresses are all included by the configured IA. + @retval EFI_NOT_FOUND The addresses are not included by the configured IA. + +**/ +EFI_STATUS +Dhcp6CheckAddress ( + IN EFI_DHCP6_IA *Ia, + IN UINT32 AddressCount, + IN EFI_IPv6_ADDRESS *Addresses + ) +{ + UINTN Index1; + UINTN Index2; + BOOLEAN Found; + + // + // Check whether the addresses are all included by the configured IA. And it + // will return success if address count is zero, which means all addresses. + // + for (Index1 = 0; Index1 < AddressCount; Index1++) { + + Found = FALSE; + + for (Index2 = 0; Index2 < Ia->IaAddressCount; Index2++) { + + if (CompareMem ( + &Addresses[Index1], + &Ia->IaAddress[Index2], + sizeof (EFI_IPv6_ADDRESS) + ) == 0) { + + Found = TRUE; + break; + } + } + + if (!Found) { + return EFI_NOT_FOUND; + } + } + + return EFI_SUCCESS; +} + + +/** + Deprive the addresses from current Ia, and generate another eliminated Ia. + + @param[in] Ia The pointer to the Ia. + @param[in] AddressCount The number of addresses. + @param[in] Addresses The pointer to the addresses buffer. + + @retval NULL If it failed to generate the deprived Ia. + @retval others The pointer to the deprived Ia. + +**/ +EFI_DHCP6_IA * +Dhcp6DepriveAddress ( + IN EFI_DHCP6_IA *Ia, + IN UINT32 AddressCount, + IN EFI_IPv6_ADDRESS *Addresses + ) +{ + EFI_DHCP6_IA *IaCopy; + UINTN IaCopySize; + UINTN Index1; + UINTN Index2; + BOOLEAN Found; + + if (AddressCount == 0) { + // + // It means release all Ia addresses if address count is zero. + // + AddressCount = Ia->IaAddressCount; + } + + ASSERT (AddressCount != 0); + + IaCopySize = sizeof (EFI_DHCP6_IA) + (AddressCount - 1) * sizeof (EFI_DHCP6_IA_ADDRESS); + IaCopy = AllocateZeroPool (IaCopySize); + + if (IaCopy == NULL) { + return NULL; + } + + if (AddressCount == Ia->IaAddressCount) { + // + // If release all Ia addresses, just copy the configured Ia and then set + // its address count as zero. + // We may decline/release part of addresses at the begining. So it's a + // forwarding step to update address infor for decline/release, while the + // other infor such as Ia state will be updated when receiving reply. + // + CopyMem (IaCopy, Ia, IaCopySize); + Ia->IaAddressCount = 0; + return IaCopy; + } + + CopyMem (IaCopy, Ia, sizeof (EFI_DHCP6_IA)); + + // + // Move the addresses from the Ia of instance to the deprived Ia. + // + for (Index1 = 0; Index1 < AddressCount; Index1++) { + + Found = FALSE; + + for (Index2 = 0; Index2 < Ia->IaAddressCount; Index2++) { + + if (CompareMem ( + &Addresses[Index1], + &Ia->IaAddress[Index2], + sizeof (EFI_IPv6_ADDRESS) + ) == 0) { + // + // Copy the deprived address to the copy of Ia + // + CopyMem ( + &IaCopy->IaAddress[Index1], + &Ia->IaAddress[Index2], + sizeof (EFI_DHCP6_IA_ADDRESS) + ); + // + // Delete the deprived address from the instance Ia + // + if (Index2 + 1 < Ia->IaAddressCount) { + CopyMem ( + &Ia->IaAddress[Index2], + &Ia->IaAddress[Index2 + 1], + (Ia->IaAddressCount - Index2 - 1) * sizeof (EFI_DHCP6_IA_ADDRESS) + ); + } + Found = TRUE; + break; + } + } + ASSERT (Found == TRUE); + } + + Ia->IaAddressCount -= AddressCount; + IaCopy->IaAddressCount = AddressCount; + + return IaCopy; +} + + +/** + The dummy ext buffer free callback routine. + + @param[in] Arg The pointer to the parameter. + +**/ +VOID +EFIAPI +Dhcp6DummyExtFree ( + IN VOID *Arg + ) +{ +} + + +/** + The callback routine once message transmitted. + + @param[in] Wrap The pointer to the received net buffer. + @param[in] EndPoint The pointer to the udp end point. + @param[in] IoStatus The return status from udp io. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +Dhcp6OnTransmitted ( + IN NET_BUF *Wrap, + IN UDP_END_POINT *EndPoint, + IN EFI_STATUS IoStatus, + IN VOID *Context + ) +{ + NetbufFree (Wrap); +} + + +/** + Append the option to Buf, and move Buf to the end. + + @param[in, out] Buf The pointer to the buffer. + @param[in] OptType The option type. + @param[in] OptLen The length of option contents. + @param[in] Data The pointer to the option content. + + @return Buf The position to append the next option. + +**/ +UINT8 * +Dhcp6AppendOption ( + IN OUT UINT8 *Buf, + IN UINT16 OptType, + IN UINT16 OptLen, + IN UINT8 *Data + ) +{ + // + // The format of Dhcp6 option: + // + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | option-code | option-len (option data) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | option-data | + // | (option-len octets) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + ASSERT (OptLen != 0); + + WriteUnaligned16 ((UINT16 *) Buf, OptType); + Buf += 2; + WriteUnaligned16 ((UINT16 *) Buf, OptLen); + Buf += 2; + CopyMem (Buf, Data, NTOHS (OptLen)); + Buf += NTOHS (OptLen); + + return Buf; +} + +/** + Append the appointed IA Address option to Buf, and move Buf to the end. + + @param[in, out] Buf The pointer to the position to append. + @param[in] IaAddr The pointer to the IA Address. + @param[in] MessageType Message type of DHCP6 package. + + @return Buf The position to append the next option. + +**/ +UINT8 * +Dhcp6AppendIaAddrOption ( + IN OUT UINT8 *Buf, + IN EFI_DHCP6_IA_ADDRESS *IaAddr, + IN UINT32 MessageType +) +{ + + // The format of the IA Address option is: + // + // 0 1 2 3 + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPTION_IAADDR | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | | + // | IPv6 address | + // | | + // | | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | preferred-lifetime | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | valid-lifetime | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // . . + // . IAaddr-options . + // . . + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + // + // Fill the value of Ia Address option type + // + WriteUnaligned16 ((UINT16 *) Buf, HTONS (Dhcp6OptIaAddr)); + Buf += 2; + + WriteUnaligned16 ((UINT16 *) Buf, HTONS (sizeof (EFI_DHCP6_IA_ADDRESS))); + Buf += 2; + + CopyMem (Buf, &IaAddr->IpAddress, sizeof(EFI_IPv6_ADDRESS)); + Buf += sizeof(EFI_IPv6_ADDRESS); + + // + // Fill the value of preferred-lifetime and valid-lifetime. + // According to RFC3315 Chapter 18.1.2, the preferred-lifetime and valid-lifetime fields + // should set to 0 when initiate a Confirm message. + // + if (MessageType != Dhcp6MsgConfirm) { + WriteUnaligned32 ((UINT32 *) Buf, HTONL (IaAddr->PreferredLifetime)); + } + Buf += 4; + + if (MessageType != Dhcp6MsgConfirm) { + WriteUnaligned32 ((UINT32 *) Buf, HTONL (IaAddr->ValidLifetime)); + } + Buf += 4; + + return Buf; +} + + +/** + Append the appointed Ia option to Buf, and move Buf to the end. + + @param[in, out] Buf The pointer to the position to append. + @param[in] Ia The pointer to the Ia. + @param[in] T1 The time of T1. + @param[in] T2 The time of T2. + @param[in] MessageType Message type of DHCP6 package. + + @return Buf The position to append the next Ia option. + +**/ +UINT8 * +Dhcp6AppendIaOption ( + IN OUT UINT8 *Buf, + IN EFI_DHCP6_IA *Ia, + IN UINT32 T1, + IN UINT32 T2, + IN UINT32 MessageType + ) +{ + UINT8 *AddrOpt; + UINT16 *Len; + UINTN Index; + + // + // The format of IA_NA and IA_TA option: + // + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPTION_IA_NA | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | IAID (4 octets) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | T1 (only for IA_NA) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | T2 (only for IA_NA) | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | | + // . IA_NA-options/IA_TA-options . + // . . + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + // + // Fill the value of Ia option type + // + WriteUnaligned16 ((UINT16 *) Buf, HTONS (Ia->Descriptor.Type)); + Buf += 2; + + // + // Fill the len of Ia option later, keep the pointer first + // + Len = (UINT16 *) Buf; + Buf += 2; + + // + // Fill the value of iaid + // + WriteUnaligned32 ((UINT32 *) Buf, HTONL (Ia->Descriptor.IaId)); + Buf += 4; + + // + // Fill the value of t1 and t2 if iana, keep it 0xffffffff if no specified. + // + if (Ia->Descriptor.Type == Dhcp6OptIana) { + WriteUnaligned32 ((UINT32 *) Buf, HTONL ((T1 != 0) ? T1 : 0xffffffff)); + Buf += 4; + WriteUnaligned32 ((UINT32 *) Buf, HTONL ((T2 != 0) ? T2 : 0xffffffff)); + Buf += 4; + } + + // + // Fill all the addresses belong to the Ia + // + for (Index = 0; Index < Ia->IaAddressCount; Index++) { + AddrOpt = (UINT8 *) Ia->IaAddress + Index * sizeof (EFI_DHCP6_IA_ADDRESS); + Buf = Dhcp6AppendIaAddrOption (Buf, (EFI_DHCP6_IA_ADDRESS *) AddrOpt, MessageType); + } + + // + // Fill the value of Ia option length + // + *Len = HTONS ((UINT16) (Buf - (UINT8 *) Len - 2)); + + return Buf; +} + +/** + Append the appointed Elapsed time option to Buf, and move Buf to the end. + + @param[in, out] Buf The pointer to the position to append. + @param[in] Instance The pointer to the Dhcp6 instance. + @param[out] Elapsed The pointer to the elapsed time value in + the generated packet. + + @return Buf The position to append the next Ia option. + +**/ +UINT8 * +Dhcp6AppendETOption ( + IN OUT UINT8 *Buf, + IN DHCP6_INSTANCE *Instance, + OUT UINT16 **Elapsed + ) +{ + // + // The format of elapsed time option: + // + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPTION_ELAPSED_TIME | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | elapsed-time | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + // + // Fill the value of elapsed-time option type. + // + WriteUnaligned16 ((UINT16 *) Buf, HTONS (Dhcp6OptElapsedTime)); + Buf += 2; + + // + // Fill the len of elapsed-time option, which is fixed. + // + WriteUnaligned16 ((UINT16 *) Buf, HTONS(2)); + Buf += 2; + + // + // Fill in elapsed time value with 0 value for now. The actual value is + // filled in later just before the packet is transmitted. + // + WriteUnaligned16 ((UINT16 *) Buf, HTONS(0)); + *Elapsed = (UINT16 *) Buf; + Buf += 2; + + return Buf; +} + +/** + Set the elapsed time based on the given instance and the pointer to the + elapsed time option. + + @param[in] Elapsed The pointer to the position to append. + @param[in] Instance The pointer to the Dhcp6 instance. + +**/ +VOID +SetElapsedTime ( + IN UINT16 *Elapsed, + IN DHCP6_INSTANCE *Instance + ) +{ + EFI_TIME Time; + UINT64 CurrentStamp; + UINT64 ElapsedTimeValue; + + // + // Generate a time stamp of the centiseconds from 2000/1/1, assume 30day/month. + // + gRT->GetTime (&Time, NULL); + CurrentStamp = MultU64x32 ( + ((((UINT32)(Time.Year - 2000) * 360 + (Time.Month - 1) * 30 + (Time.Day - 1)) * 24 + Time.Hour) * 60 + Time.Minute) * 60 + Time.Second, + 100 + ) + + DivU64x32( + Time.Nanosecond, + 10000000 + ); + + // + // Sentinel value of 0 means that this is the first DHCP packet that we are + // sending and that we need to initialize the value. First DHCP message + // gets 0 elapsed-time. Otherwise, calculate based on StartTime. + // + if (Instance->StartTime == 0) { + ElapsedTimeValue = 0; + Instance->StartTime = CurrentStamp; + } else { + ElapsedTimeValue = CurrentStamp - Instance->StartTime; + + // + // If elapsed time cannot fit in two bytes, set it to 0xffff. + // + if (ElapsedTimeValue > 0xffff) { + ElapsedTimeValue = 0xffff; + } + } + WriteUnaligned16 (Elapsed, HTONS((UINT16) ElapsedTimeValue)); +} + + +/** + Seek the address of the first byte of the option header. + + @param[in] Buf The pointer to the buffer. + @param[in] SeekLen The length to seek. + @param[in] OptType The option type. + + @retval NULL If it failed to seek the option. + @retval others The position to the option. + +**/ +UINT8 * +Dhcp6SeekOption ( + IN UINT8 *Buf, + IN UINT32 SeekLen, + IN UINT16 OptType + ) +{ + UINT8 *Cursor; + UINT8 *Option; + UINT16 DataLen; + UINT16 OpCode; + + Option = NULL; + Cursor = Buf; + + // + // The format of Dhcp6 option refers to Dhcp6AppendOption(). + // + while (Cursor < Buf + SeekLen) { + OpCode = ReadUnaligned16 ((UINT16 *) Cursor); + if (OpCode == HTONS (OptType)) { + Option = Cursor; + break; + } + DataLen = NTOHS (ReadUnaligned16 ((UINT16 *) (Cursor + 2))); + Cursor += (DataLen + 4); + } + + return Option; +} + + +/** + Seek the address of the first byte of the Ia option header. + + @param[in] Buf The pointer to the buffer. + @param[in] SeekLen The length to seek. + @param[in] IaDesc The pointer to the Ia descriptor. + + @retval NULL If it failed to seek the Ia option. + @retval others The position to the Ia option. + +**/ +UINT8 * +Dhcp6SeekIaOption ( + IN UINT8 *Buf, + IN UINT32 SeekLen, + IN EFI_DHCP6_IA_DESCRIPTOR *IaDesc + ) +{ + UINT8 *Cursor; + UINT8 *Option; + UINT16 DataLen; + UINT16 OpCode; + UINT32 IaId; + + // + // The format of IA_NA and IA_TA option refers to Dhcp6AppendIaOption(). + // + Option = NULL; + Cursor = Buf; + + while (Cursor < Buf + SeekLen) { + OpCode = ReadUnaligned16 ((UINT16 *) Cursor); + IaId = ReadUnaligned32 ((UINT32 *) (Cursor + 4)); + if (OpCode == HTONS (IaDesc->Type) && IaId == HTONL (IaDesc->IaId)) { + Option = Cursor; + break; + } + DataLen = NTOHS (ReadUnaligned16 ((UINT16 *) (Cursor + 2))); + Cursor += (DataLen + 4); + } + + return Option; +} + +/** + Check whether the incoming IPv6 address in IaAddr is one of the maintained + addresses in the IA control blcok. + + @param[in] IaAddr The pointer to the IA Address to be checked. + @param[in] CurrentIa The pointer to the IA in IA control block. + + @retval TRUE Yes, this Address is already in IA control block. + @retval FALSE No, this Address is NOT in IA control block. + +**/ +BOOLEAN +Dhcp6AddrIsInCurrentIa ( + IN EFI_DHCP6_IA_ADDRESS *IaAddr, + IN EFI_DHCP6_IA *CurrentIa + ) +{ + UINT32 Index; + + ASSERT (IaAddr != NULL && CurrentIa != NULL); + + for (Index = 0; Index < CurrentIa->IaAddressCount; Index++) { + if (EFI_IP6_EQUAL(&IaAddr->IpAddress, &CurrentIa->IaAddress[Index].IpAddress)) { + return TRUE; + } + } + return FALSE; +} + +/** + Parse the address option and update the address infomation. + + @param[in] CurrentIa The pointer to the Ia Address in control blcok. + @param[in] IaInnerOpt The pointer to the buffer. + @param[in] IaInnerLen The length to parse. + @param[out] AddrNum The number of addresses. + @param[in, out] AddrBuf The pointer to the address buffer. + +**/ +VOID +Dhcp6ParseAddrOption ( + IN EFI_DHCP6_IA *CurrentIa, + IN UINT8 *IaInnerOpt, + IN UINT16 IaInnerLen, + OUT UINT32 *AddrNum, + IN OUT EFI_DHCP6_IA_ADDRESS *AddrBuf + ) +{ + UINT8 *Cursor; + UINT16 DataLen; + UINT16 OpCode; + UINT32 ValidLt; + UINT32 PreferredLt; + EFI_DHCP6_IA_ADDRESS *IaAddr; + + // + // The format of the IA Address option: + // + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPTION_IAADDR | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | | + // | IPv6 address | + // | | + // | | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | preferred-lifetime | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | valid-lifetime | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // . . + // . IAaddr-options . + // . . + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + // + // Two usage model: + // + // 1. Pass addrbuf == null, to get the addrnum over the Ia inner options. + // 2. Pass addrbuf != null, to resolve the addresses over the Ia inner + // options to the addrbuf. + // + + Cursor = IaInnerOpt; + *AddrNum = 0; + + while (Cursor < IaInnerOpt + IaInnerLen) { + // + // Refer to RFC3315 Chapter 18.1.8, we need to update lifetimes for any addresses in the IA option + // that the client already has recorded in the IA, and discard the Ia address option with 0 valid time. + // + OpCode = ReadUnaligned16 ((UINT16 *) Cursor); + PreferredLt = NTOHL (ReadUnaligned32 ((UINT32 *) (Cursor + 20))); + ValidLt = NTOHL (ReadUnaligned32 ((UINT32 *) (Cursor + 24))); + IaAddr = (EFI_DHCP6_IA_ADDRESS *) (Cursor + 4); + if (OpCode == HTONS (Dhcp6OptIaAddr) && ValidLt >= PreferredLt && + (Dhcp6AddrIsInCurrentIa(IaAddr, CurrentIa) || ValidLt !=0)) { + if (AddrBuf != NULL) { + CopyMem (AddrBuf, IaAddr, sizeof (EFI_DHCP6_IA_ADDRESS)); + AddrBuf->PreferredLifetime = PreferredLt; + AddrBuf->ValidLifetime = ValidLt; + AddrBuf = (EFI_DHCP6_IA_ADDRESS *) ((UINT8 *) AddrBuf + sizeof (EFI_DHCP6_IA_ADDRESS)); + } + (*AddrNum)++; + } + DataLen = NTOHS (ReadUnaligned16 ((UINT16 *) (Cursor + 2))); + Cursor += (DataLen + 4); + } +} + + +/** + Create a control blcok for the Ia according to the corresponding options. + + @param[in] Instance The pointer to DHCP6 Instance. + @param[in] IaInnerOpt The pointer to the inner options in the Ia option. + @param[in] IaInnerLen The length of all the inner options in the Ia option. + @param[in] T1 T1 time in the Ia option. + @param[in] T2 T2 time in the Ia option. + + @retval EFI_NOT_FOUND No valid IA option is found. + @retval EFI_SUCCESS Create an IA control block successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + +**/ +EFI_STATUS +Dhcp6GenerateIaCb ( + IN DHCP6_INSTANCE *Instance, + IN UINT8 *IaInnerOpt, + IN UINT16 IaInnerLen, + IN UINT32 T1, + IN UINT32 T2 + ) +{ + UINT32 AddrNum; + UINT32 IaSize; + EFI_DHCP6_IA *Ia; + + if (Instance->IaCb.Ia == NULL) { + return EFI_DEVICE_ERROR; + } + + // + // Calculate the number of addresses for this Ia, excluding the addresses with + // the value 0 of valid lifetime. + // + Dhcp6ParseAddrOption (Instance->IaCb.Ia, IaInnerOpt, IaInnerLen, &AddrNum, NULL); + + if (AddrNum == 0) { + return EFI_NOT_FOUND; + } + + // + // Allocate for new IA. + // + IaSize = sizeof (EFI_DHCP6_IA) + (AddrNum - 1) * sizeof (EFI_DHCP6_IA_ADDRESS); + Ia = AllocateZeroPool (IaSize); + + if (Ia == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Fill up this new IA fields. + // + Ia->State = Instance->IaCb.Ia->State; + Ia->IaAddressCount = AddrNum; + CopyMem (&Ia->Descriptor, &Instance->Config->IaDescriptor, sizeof (EFI_DHCP6_IA_DESCRIPTOR)); + Dhcp6ParseAddrOption (Instance->IaCb.Ia, IaInnerOpt, IaInnerLen, &AddrNum, Ia->IaAddress); + + // + // Free original IA resource. + // + if (Instance->IaCb.Ia->ReplyPacket != NULL) { + FreePool (Instance->IaCb.Ia->ReplyPacket); + } + FreePool (Instance->IaCb.Ia); + + + ZeroMem (&Instance->IaCb, sizeof (DHCP6_IA_CB)); + + // + // Update IaCb to use new IA. + // + Instance->IaCb.Ia = Ia; + + // + + // Fill in IaCb fields. Such as T1, T2, AllExpireTime and LeaseTime. + // + Instance->IaCb.T1 = T1; + Instance->IaCb.T2 = T2; + Dhcp6CalculateLeaseTime (&Instance->IaCb); + + return EFI_SUCCESS; +} + + +/** + Cache the current IA configuration information. + + @param[in] Instance The pointer to DHCP6 Instance. + + @retval EFI_SUCCESS Cache the current IA successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + +**/ +EFI_STATUS +Dhcp6CacheIa ( + IN DHCP6_INSTANCE *Instance + ) +{ + UINTN IaSize; + EFI_DHCP6_IA *Ia; + + Ia = Instance->IaCb.Ia; + + if ((Instance->CacheIa == NULL) && (Ia != NULL)) { + // + // Cache the current IA. + // + IaSize = sizeof (EFI_DHCP6_IA) + (Ia->IaAddressCount - 1) * sizeof (EFI_DHCP6_IA_ADDRESS); + + Instance->CacheIa = AllocateZeroPool (IaSize); + if (Instance->CacheIa == NULL) { + return EFI_OUT_OF_RESOURCES; + } + CopyMem (Instance->CacheIa, Ia, IaSize); + } + return EFI_SUCCESS; +} + +/** + Append CacheIa to the currrent IA. Meanwhile, clear CacheIa.ValidLifetime to 0. + + @param[in] Instance The pointer to DHCP6 instance. + +**/ +VOID +Dhcp6AppendCacheIa ( + IN DHCP6_INSTANCE *Instance + ) +{ + UINT8 *Ptr; + UINTN Index; + UINTN IaSize; + UINTN NewIaSize; + EFI_DHCP6_IA *Ia; + EFI_DHCP6_IA *NewIa; + EFI_DHCP6_IA *CacheIa; + + Ia = Instance->IaCb.Ia; + CacheIa = Instance->CacheIa; + + if ((CacheIa != NULL) && (CacheIa->IaAddressCount != 0)) { + // + // There are old addresses existing. Merge with current addresses. + // + NewIaSize = sizeof (EFI_DHCP6_IA) + (Ia->IaAddressCount + CacheIa->IaAddressCount - 1) * sizeof (EFI_DHCP6_IA_ADDRESS); + NewIa = AllocateZeroPool (NewIaSize); + if (NewIa == NULL) { + return; + } + + IaSize = sizeof (EFI_DHCP6_IA) + (Ia->IaAddressCount - 1) * sizeof (EFI_DHCP6_IA_ADDRESS); + CopyMem (NewIa, Ia, IaSize); + + // + // Clear old address.ValidLifetime + // + for (Index = 0; Index < CacheIa->IaAddressCount; Index++) { + CacheIa->IaAddress[Index].ValidLifetime = 0; + } + + NewIa->IaAddressCount += CacheIa->IaAddressCount; + Ptr = (UINT8*)&NewIa->IaAddress[Ia->IaAddressCount]; + CopyMem (Ptr, CacheIa->IaAddress, CacheIa->IaAddressCount * sizeof (EFI_DHCP6_IA_ADDRESS)); + + // + // Migrate to the NewIa and free previous. + // + FreePool (Instance->CacheIa); + FreePool (Instance->IaCb.Ia); + Instance->CacheIa = NULL; + Instance->IaCb.Ia = NewIa; + } +} + +/** + Calculate the Dhcp6 get mapping timeout by adding additinal delay to the IP6 DAD transmits count. + + @param[in] Ip6Cfg The pointer to Ip6 config protocol. + @param[out] TimeOut The time out value in 100ns units. + + @retval EFI_INVALID_PARAMETER Input parameters are invalid. + @retval EFI_SUCCESS Calculate the time out value successfully. +**/ +EFI_STATUS +Dhcp6GetMappingTimeOut ( + IN EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg, + OUT UINTN *TimeOut + ) +{ + EFI_STATUS Status; + UINTN DataSize; + EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS DadXmits; + + if (Ip6Cfg == NULL || TimeOut == NULL) { + return EFI_INVALID_PARAMETER; + } + + DataSize = sizeof (EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS); + Status = Ip6Cfg->GetData ( + Ip6Cfg, + Ip6ConfigDataTypeDupAddrDetectTransmits, + &DataSize, + &DadXmits + ); + if (EFI_ERROR (Status)) { + return Status; + } + + *TimeOut = TICKS_PER_SECOND * DadXmits.DupAddrDetectTransmits + DHCP6_DAD_ADDITIONAL_DELAY; + + return EFI_SUCCESS; +} diff --git a/NetworkPkg/Dhcp6Dxe/Dhcp6Utility.h b/NetworkPkg/Dhcp6Dxe/Dhcp6Utility.h new file mode 100644 index 000000000..6a302adf2 --- /dev/null +++ b/NetworkPkg/Dhcp6Dxe/Dhcp6Utility.h @@ -0,0 +1,354 @@ +/** @file + Dhcp6 support functions declaration. + + Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_DHCP6_UTILITY_H__ +#define __EFI_DHCP6_UTILITY_H__ + + +#define DHCP6_10_BIT_MASK 0x3ff +#define DHCP6_DAD_ADDITIONAL_DELAY 30000000 // 3 seconds + +/** + Generate client Duid in the format of Duid-llt. + + @param[in] Mode The pointer to the mode of SNP. + + @retval NULL if failed to generate client Id. + @retval Others The pointer to the new client id. + +**/ +EFI_DHCP6_DUID * +Dhcp6GenerateClientId ( + IN EFI_SIMPLE_NETWORK_MODE *Mode + ); + +/** + Copy the Dhcp6 configure data. + + @param[in] DstCfg The pointer to the destination configure data. + @param[in] SorCfg The pointer to the source configure data. + + @retval EFI_SUCCESS Copy the content from SorCfg from DstCfg successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + +**/ +EFI_STATUS +Dhcp6CopyConfigData ( + IN EFI_DHCP6_CONFIG_DATA *DstCfg, + IN EFI_DHCP6_CONFIG_DATA *SorCfg + ); + +/** + Clean up the configure data. + + @param[in, out] CfgData The pointer to the configure data. + +**/ +VOID +Dhcp6CleanupConfigData ( + IN OUT EFI_DHCP6_CONFIG_DATA *CfgData + ); + +/** + Clean up the mode data. + + @param[in, out] ModeData The pointer to the mode data. + +**/ +VOID +Dhcp6CleanupModeData ( + IN OUT EFI_DHCP6_MODE_DATA *ModeData + ); + +/** + Calculate the expire time by the algorithm defined in rfc. + + @param[in] Base The base value of the time. + @param[in] IsFirstRt If TRUE, it is the first time to calculate expire time. + @param[in] NeedSigned If TRUE, the the signed factor is needed. + + @return Expire The calculated result for the new expire time. + +**/ +UINT32 +Dhcp6CalculateExpireTime ( + IN UINT32 Base, + IN BOOLEAN IsFirstRt, + IN BOOLEAN NeedSigned + ); + +/** + Calculate the lease time by the algorithm defined in rfc. + + @param[in] IaCb The pointer to the Ia control block. + +**/ +VOID +Dhcp6CalculateLeaseTime ( + IN DHCP6_IA_CB *IaCb + ); + +/** + Check whether the addresses are all included by the configured Ia. + + @param[in] Ia The pointer to the Ia. + @param[in] AddressCount The number of addresses. + @param[in] Addresses The pointer to the addresses buffer. + + @retval EFI_SUCCESS The addresses are all included by the configured IA. + @retval EFI_NOT_FOUND The addresses are not included by the configured IA. + +**/ +EFI_STATUS +Dhcp6CheckAddress ( + IN EFI_DHCP6_IA *Ia, + IN UINT32 AddressCount, + IN EFI_IPv6_ADDRESS *Addresses + ); + +/** + Deprive the addresses from current Ia, and generate another eliminated Ia. + + @param[in] Ia The pointer to the Ia. + @param[in] AddressCount The number of addresses. + @param[in] Addresses The pointer to the addresses buffer. + + @retval NULL If failed to generate the deprived Ia. + @retval others The pointer to the deprived Ia. + +**/ +EFI_DHCP6_IA * +Dhcp6DepriveAddress ( + IN EFI_DHCP6_IA *Ia, + IN UINT32 AddressCount, + IN EFI_IPv6_ADDRESS *Addresses + ); + +/** + The dummy ext buffer free callback routine. + + @param[in] Arg The pointer to the parameter. + +**/ +VOID +EFIAPI +Dhcp6DummyExtFree ( + IN VOID *Arg + ); + +/** + The callback routine once message transmitted. + + @param[in] Wrap The pointer to the received net buffer. + @param[in] EndPoint The pointer to the udp end point. + @param[in] IoStatus The return status from udp io. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +Dhcp6OnTransmitted ( + IN NET_BUF *Wrap, + IN UDP_END_POINT *EndPoint, + IN EFI_STATUS IoStatus, + IN VOID *Context + ); + +/** + Append the appointed option to the buf, and move the buf to the end. + + @param[in, out] Buf The pointer to buffer. + @param[in] OptType The option type. + @param[in] OptLen The lenght of option content.s + @param[in] Data The pointer to the option content. + + @return Buf The position to append the next option. + +**/ +UINT8 * +Dhcp6AppendOption ( + IN OUT UINT8 *Buf, + IN UINT16 OptType, + IN UINT16 OptLen, + IN UINT8 *Data + ); + +/** + Append the Ia option to Buf, and move Buf to the end. + + @param[in, out] Buf The pointer to the position to append. + @param[in] Ia The pointer to the Ia. + @param[in] T1 The time of T1. + @param[in] T2 The time of T2. + @param[in] MessageType Message type of DHCP6 package. + + @return Buf The position to append the next Ia option. + +**/ +UINT8 * +Dhcp6AppendIaOption ( + IN OUT UINT8 *Buf, + IN EFI_DHCP6_IA *Ia, + IN UINT32 T1, + IN UINT32 T2, + IN UINT32 MessageType + ); + +/** + Append the appointed Elapsed time option to Buf, and move Buf to the end. + + @param[in, out] Buf The pointer to the position to append. + @param[in] Instance The pointer to the Dhcp6 instance. + @param[out] Elapsed The pointer to the elapsed time value in + the generated packet. + + @return Buf The position to append the next Ia option. + +**/ +UINT8 * +Dhcp6AppendETOption ( + IN OUT UINT8 *Buf, + IN DHCP6_INSTANCE *Instance, + OUT UINT16 **Elapsed + ); + +/** + Set the elapsed time based on the given instance and the pointer to the + elapsed time option. + + @param[in] Elapsed The pointer to the position to append. + @param[in] Instance The pointer to the Dhcp6 instance. +**/ +VOID +SetElapsedTime ( + IN UINT16 *Elapsed, + IN DHCP6_INSTANCE *Instance + ); + +/** + Seek the address of the first byte of the option header. + + @param[in] Buf The pointer to buffer. + @param[in] SeekLen The length to seek. + @param[in] OptType The option type. + + @retval NULL If failed to seek the option. + @retval others The position to the option. + +**/ +UINT8 * +Dhcp6SeekOption ( + IN UINT8 *Buf, + IN UINT32 SeekLen, + IN UINT16 OptType + ); + +/** + Seek the address of the first byte of the Ia option header. + + @param[in] Buf The pointer to the buffer. + @param[in] SeekLen The length to seek. + @param[in] IaDesc The pointer to the Ia descriptor. + + @retval NULL If failed to seek the Ia option. + @retval others The position to the Ia option. + +**/ +UINT8 * +Dhcp6SeekIaOption ( + IN UINT8 *Buf, + IN UINT32 SeekLen, + IN EFI_DHCP6_IA_DESCRIPTOR *IaDesc + ); + +/** + Parse the address option and update the address info. + + @param[in] CurrentIa The pointer to the Ia Address in control blcok. + @param[in] IaInnerOpt The pointer to the buffer. + @param[in] IaInnerLen The length to parse. + @param[out] AddrNum The number of addresses. + @param[in, out] AddrBuf The pointer to the address buffer. + +**/ +VOID +Dhcp6ParseAddrOption ( + IN EFI_DHCP6_IA *CurrentIa, + IN UINT8 *IaInnerOpt, + IN UINT16 IaInnerLen, + OUT UINT32 *AddrNum, + IN OUT EFI_DHCP6_IA_ADDRESS *AddrBuf + ); + +/** + Create a control blcok for the Ia according to the corresponding options. + + @param[in] Instance The pointer to DHCP6 Instance. + @param[in] IaInnerOpt The pointer to the inner options in the Ia option. + @param[in] IaInnerLen The length of all the inner options in the Ia option. + @param[in] T1 T1 time in the Ia option. + @param[in] T2 T2 time in the Ia option. + + @retval EFI_NOT_FOUND No valid IA option is found. + @retval EFI_SUCCESS Create an IA control block successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected error. + +**/ +EFI_STATUS +Dhcp6GenerateIaCb ( + IN DHCP6_INSTANCE *Instance, + IN UINT8 *IaInnerOpt, + IN UINT16 IaInnerLen, + IN UINT32 T1, + IN UINT32 T2 + ); + + +/** + Cache the current IA configuration information. + + @param[in] Instance The pointer to DHCP6 Instance. + + @retval EFI_SUCCESS Cache the current IA successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + +**/ +EFI_STATUS +Dhcp6CacheIa ( + IN DHCP6_INSTANCE *Instance + ); + + +/** + Append CacheIa to the currrent IA. Meanwhile, clear CacheIa.ValidLifetime to 0. + + @param[in] Instance The pointer to DHCP6 instance. + +**/ +VOID +Dhcp6AppendCacheIa ( + IN DHCP6_INSTANCE *Instance + ); + +/** + Calculate the Dhcp6 get mapping timeout by adding additinal delay to the IP6 DAD transmits count. + + @param[in] Ip6Cfg The pointer to Ip6 config protocol. + @param[out] TimeOut The time out value in 100ns units. + + @retval EFI_INVALID_PARAMETER Input parameters are invalid. + @retval EFI_SUCCESS Calculate the time out value successfully. +**/ +EFI_STATUS +Dhcp6GetMappingTimeOut ( + IN EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg, + OUT UINTN *TimeOut + ); +#endif diff --git a/NetworkPkg/DnsDxe/ComponentName.c b/NetworkPkg/DnsDxe/ComponentName.c new file mode 100644 index 000000000..e59c8704f --- /dev/null +++ b/NetworkPkg/DnsDxe/ComponentName.c @@ -0,0 +1,451 @@ +/** @file +Implementation of EFI_COMPONENT_NAME_PROTOCOL and EFI_COMPONENT_NAME2_PROTOCOL protocol. + +Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "DnsImpl.h" + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user-readable name of the EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param Language A pointer to a three-character ISO 639-2 language identifier. + This is the language of the driver name that that the caller + is requesting, and it must match one of the languages specified + in SupportedLanguages. The number of languages supported by a + driver is up to the driver writer. + @param DriverName A pointer to the Unicode string to return. This Unicode string + is the name of the driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by This + and the language specified by Language was returned + in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DnsComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by an EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param ControllerHandle The handle of a controller that the driver specified by + This is managing. This handle specifies the controller + whose name is to be returned. + @param ChildHandle The handle of the child controller to retrieve the name + of. This is an optional parameter that may be NULL. It + will be NULL for device drivers. It will also be NULL + for a bus drivers that wish to retrieve the name of the + bus controller. It will not be NULL for a bus driver + that wishes to retrieve the name of a child controller. + @param Language A pointer to a three character ISO 639-2 language + identifier. This is the language of the controller name + that the caller is requesting, and it must match one + of the languages specified in SupportedLanguages. The + number of languages supported by a driver is up to the + driver writer. + @param ControllerName A pointer to the Unicode string to return. This Unicode + string is the name of the controller specified by + ControllerHandle and ChildHandle in the language specified + by Language, from the point of view of the driver specified + by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in the + language specified by Language for the driver + specified by This was returned in DriverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not currently managing + the controller specified by ControllerHandle and + ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DnsComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + + +/// +/// Component Name Protocol instance +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_COMPONENT_NAME_PROTOCOL gDnsComponentName = { + DnsComponentNameGetDriverName, + DnsComponentNameGetControllerName, + "eng" +}; + +/// +/// Component Name 2 Protocol instance +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_COMPONENT_NAME2_PROTOCOL gDnsComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) DnsComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) DnsComponentNameGetControllerName, + "en" +}; + +/// +/// Table of driver names +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_UNICODE_STRING_TABLE mDnsDriverNameTable[] = { + { "eng;en", (CHAR16 *)L"DNS Network Service Driver" }, + { NULL, NULL } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gDnsControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user-readable name of the EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param Language A pointer to a three-character ISO 639-2 language identifier. + This is the language of the driver name that that the caller + is requesting, and it must match one of the languages specified + in SupportedLanguages. The number of languages supported by a + driver is up to the driver writer. + @param DriverName A pointer to the Unicode string to return. This Unicode string + is the name of the driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by This + and the language specified by Language was returned + in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DnsComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mDnsDriverNameTable, + DriverName, + (BOOLEAN)(This == &gDnsComponentName) + ); +} + +/** + Update the component name for the Dns4 child handle. + + @param Dns4 A pointer to the EFI_DNS4_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateDns4Name ( + EFI_DNS4_PROTOCOL *Dns4 + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[80]; + EFI_DNS4_MODE_DATA ModeData; + + if (Dns4 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer as: + // DNSv4 (StationIp=?, LocalPort=?) + // + Status = Dns4->GetModeData (Dns4, &ModeData); + if (EFI_ERROR (Status)) { + return Status; + } + + UnicodeSPrint ( + HandleName, + sizeof (HandleName), + L"DNSv4 (StationIp=%d.%d.%d.%d, LocalPort=%d)", + ModeData.DnsConfigData.StationIp.Addr[0], + ModeData.DnsConfigData.StationIp.Addr[1], + ModeData.DnsConfigData.StationIp.Addr[2], + ModeData.DnsConfigData.StationIp.Addr[3], + ModeData.DnsConfigData.LocalPort + ); + + if (ModeData.DnsCacheList != NULL) { + FreePool (ModeData.DnsCacheList); + } + if (ModeData.DnsServerList != NULL) { + FreePool (ModeData.DnsServerList); + } + + if (gDnsControllerNameTable != NULL) { + FreeUnicodeStringTable (gDnsControllerNameTable); + gDnsControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gDnsComponentName.SupportedLanguages, + &gDnsControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gDnsComponentName2.SupportedLanguages, + &gDnsControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Update the component name for the Dns6 child handle. + + @param Dns6 A pointer to the EFI_DNS6_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateDns6Name ( + EFI_DNS6_PROTOCOL *Dns6 + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[128]; + EFI_DNS6_MODE_DATA ModeData; + CHAR16 Address[sizeof"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"]; + + if (Dns6 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer as: + // DNSv6 (StationIp=?, LocalPort=?) + // + Status = Dns6->GetModeData (Dns6, &ModeData); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = NetLibIp6ToStr (&ModeData.DnsConfigData.StationIp, Address, sizeof (Address)); + if (EFI_ERROR (Status)) { + return Status; + } + UnicodeSPrint ( + HandleName, + sizeof (HandleName), + L"DNSv6 (StationIp=%s, LocalPort=%d)", + Address, + ModeData.DnsConfigData.LocalPort + ); + + if (ModeData.DnsCacheList != NULL) { + FreePool (ModeData.DnsCacheList); + } + if (ModeData.DnsServerList != NULL) { + FreePool (ModeData.DnsServerList); + } + + if (gDnsControllerNameTable != NULL) { + FreeUnicodeStringTable (gDnsControllerNameTable); + gDnsControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gDnsComponentName.SupportedLanguages, + &gDnsControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gDnsComponentName2.SupportedLanguages, + &gDnsControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by an EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param ControllerHandle The handle of a controller that the driver specified by + This is managing. This handle specifies the controller + whose name is to be returned. + @param ChildHandle The handle of the child controller to retrieve the name + of. This is an optional parameter that may be NULL. It + will be NULL for device drivers. It will also be NULL + for a bus drivers that wish to retrieve the name of the + bus controller. It will not be NULL for a bus driver + that wishes to retrieve the name of a child controller. + @param Language A pointer to a three character ISO 639-2 language + identifier. This is the language of the controller name + that the caller is requesting, and it must match one + of the languages specified in SupportedLanguages. The + number of languages supported by a driver is up to the + driver writer. + @param ControllerName A pointer to the Unicode string to return. This Unicode + string is the name of the controller specified by + ControllerHandle and ChildHandle in the language specified + by Language, from the point of view of the driver specified + by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in the + language specified by Language for the driver + specified by This was returned in DriverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not currently managing + the controller specified by ControllerHandle and + ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DnsComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_DNS4_PROTOCOL *Dns4; + EFI_DNS6_PROTOCOL *Dns6; + + // + // ChildHandle must be NULL for a Device Driver + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiUdp6ProtocolGuid + ); + if (!EFI_ERROR (Status)) { + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiDns6ProtocolGuid, + (VOID **)&Dns6, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateDns6Name (Dns6); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiUdp4ProtocolGuid + ); + if (!EFI_ERROR (Status)) { + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiDns4ProtocolGuid, + (VOID **)&Dns4, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateDns4Name (Dns4); + if (EFI_ERROR (Status)) { + return Status; + } + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gDnsControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gDnsComponentName) + ); +} diff --git a/NetworkPkg/DnsDxe/DnsDhcp.c b/NetworkPkg/DnsDxe/DnsDhcp.c new file mode 100644 index 000000000..9ea9d7c8f --- /dev/null +++ b/NetworkPkg/DnsDxe/DnsDhcp.c @@ -0,0 +1,758 @@ +/** @file +Functions implementation related with DHCPv4/v6 for DNS driver. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "DnsImpl.h" + +/** + This function initialize the DHCP4 message instance. + + This function will pad each item of dhcp4 message packet. + + @param Seed Pointer to the message instance of the DHCP4 packet. + @param InterfaceInfo Pointer to the EFI_IP4_CONFIG2_INTERFACE_INFO instance. + +**/ +VOID +DnsInitSeedPacket ( + OUT EFI_DHCP4_PACKET *Seed, + IN EFI_IP4_CONFIG2_INTERFACE_INFO *InterfaceInfo + ) +{ + EFI_DHCP4_HEADER *Header; + + // + // Get IfType and HwAddressSize from SNP mode data. + // + Seed->Size = sizeof (EFI_DHCP4_PACKET); + Seed->Length = sizeof (Seed->Dhcp4); + Header = &Seed->Dhcp4.Header; + ZeroMem (Header, sizeof (EFI_DHCP4_HEADER)); + Header->OpCode = DHCP4_OPCODE_REQUEST; + Header->HwType = InterfaceInfo->IfType; + Header->HwAddrLen = (UINT8) InterfaceInfo->HwAddressSize; + CopyMem (Header->ClientHwAddr, &(InterfaceInfo->HwAddress), Header->HwAddrLen); + + Seed->Dhcp4.Magik = DHCP4_MAGIC; + Seed->Dhcp4.Option[0] = DHCP4_TAG_EOP; +} + +/** + The common notify function. + + @param[in] Event The event signaled. + @param[in] Context The context. + +**/ +VOID +EFIAPI +DhcpCommonNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + if ((Event == NULL) || (Context == NULL)) { + return ; + } + + *((BOOLEAN *) Context) = TRUE; +} + +/** + Parse the ACK to get required information + + @param Dhcp4 The DHCP4 protocol. + @param Packet Packet waiting for parse. + @param DnsServerInfor The required Dns4 server information. + + @retval EFI_SUCCESS The DNS information is got from the DHCP ACK. + @retval EFI_NO_MAPPING DHCP failed to acquire address and other information. + @retval EFI_DEVICE_ERROR Other errors as indicated. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +ParseDhcp4Ack ( + IN EFI_DHCP4_PROTOCOL *Dhcp4, + IN EFI_DHCP4_PACKET *Packet, + IN DNS4_SERVER_INFOR *DnsServerInfor + ) +{ + EFI_STATUS Status; + UINT32 OptionCount; + EFI_DHCP4_PACKET_OPTION **OptionList; + UINT32 ServerCount; + EFI_IPv4_ADDRESS *ServerList; + UINT32 Index; + UINT32 Count; + + ServerCount = 0; + ServerList = NULL; + + OptionCount = 0; + OptionList = NULL; + + Status = Dhcp4->Parse (Dhcp4, Packet, &OptionCount, OptionList); + if (Status != EFI_BUFFER_TOO_SMALL) { + return EFI_DEVICE_ERROR; + } + + OptionList = AllocatePool (OptionCount * sizeof (EFI_DHCP4_PACKET_OPTION *)); + if (OptionList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = Dhcp4->Parse (Dhcp4, Packet, &OptionCount, OptionList); + if (EFI_ERROR (Status)) { + gBS->FreePool (OptionList); + return EFI_DEVICE_ERROR; + } + + Status = EFI_NOT_FOUND; + + for (Index = 0; Index < OptionCount; Index++) { + // + // Get DNS server addresses + // + if (OptionList[Index]->OpCode == DHCP4_TAG_DNS_SERVER) { + + if (((OptionList[Index]->Length & 0x3) != 0) || (OptionList[Index]->Length == 0)) { + Status = EFI_DEVICE_ERROR; + break; + } + + ServerCount = OptionList[Index]->Length/4; + ServerList = AllocatePool (ServerCount * sizeof (EFI_IPv4_ADDRESS)); + if (ServerList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + for(Count=0; Count < ServerCount; Count++){ + CopyMem (ServerList + Count, &OptionList[Index]->Data[4 * Count], sizeof (EFI_IPv4_ADDRESS)); + } + + *(DnsServerInfor->ServerCount) = ServerCount; + DnsServerInfor->ServerList = ServerList; + + Status = EFI_SUCCESS; + } + } + + gBS->FreePool (OptionList); + + return Status; +} + +/** + EFI_DHCP6_INFO_CALLBACK is provided by the consumer of the EFI DHCPv6 Protocol + instance to intercept events that occurs in the DHCPv6 Information Request + exchange process. + + @param This Pointer to the EFI_DHCP6_PROTOCOL instance that + is used to configure this callback function. + @param Context Pointer to the context that is initialized in + the EFI_DHCP6_PROTOCOL.InfoRequest(). + @param Packet Pointer to Reply packet that has been received. + The EFI DHCPv6 Protocol instance is responsible + for freeing the buffer. + + @retval EFI_SUCCESS The DNS information is got from the DHCP ACK. + @retval EFI_DEVICE_ERROR Other errors as indicated. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. +**/ +EFI_STATUS +EFIAPI +ParseDhcp6Ack ( + IN EFI_DHCP6_PROTOCOL *This, + IN VOID *Context, + IN EFI_DHCP6_PACKET *Packet + ) +{ + EFI_STATUS Status; + UINT32 OptionCount; + EFI_DHCP6_PACKET_OPTION **OptionList; + DNS6_SERVER_INFOR *DnsServerInfor; + UINT32 ServerCount; + EFI_IPv6_ADDRESS *ServerList; + UINT32 Index; + UINT32 Count; + + OptionCount = 0; + ServerCount = 0; + ServerList = NULL; + + Status = This->Parse (This, Packet, &OptionCount, NULL); + if (Status != EFI_BUFFER_TOO_SMALL) { + return EFI_DEVICE_ERROR; + } + + OptionList = AllocateZeroPool (OptionCount * sizeof (EFI_DHCP6_PACKET_OPTION *)); + if (OptionList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = This->Parse (This, Packet, &OptionCount, OptionList); + if (EFI_ERROR (Status)) { + gBS->FreePool (OptionList); + return EFI_DEVICE_ERROR; + } + + DnsServerInfor = (DNS6_SERVER_INFOR *) Context; + + for (Index = 0; Index < OptionCount; Index++) { + OptionList[Index]->OpCode = NTOHS (OptionList[Index]->OpCode); + OptionList[Index]->OpLen = NTOHS (OptionList[Index]->OpLen); + + // + // Get DNS server addresses from this reply packet. + // + if (OptionList[Index]->OpCode == DHCP6_TAG_DNS_SERVER) { + + if (((OptionList[Index]->OpLen & 0xf) != 0) || (OptionList[Index]->OpLen == 0)) { + Status = EFI_DEVICE_ERROR; + gBS->FreePool (OptionList); + return Status; + } + + ServerCount = OptionList[Index]->OpLen/16; + ServerList = AllocatePool (ServerCount * sizeof (EFI_IPv6_ADDRESS)); + if (ServerList == NULL) { + gBS->FreePool (OptionList); + return EFI_OUT_OF_RESOURCES; + } + + for(Count=0; Count < ServerCount; Count++){ + CopyMem (ServerList + Count, &OptionList[Index]->Data[16 * Count], sizeof (EFI_IPv6_ADDRESS)); + } + + *(DnsServerInfor->ServerCount) = ServerCount; + DnsServerInfor->ServerList = ServerList; + } + } + + gBS->FreePool (OptionList); + + return Status; + +} + +/** + Parse the DHCP ACK to get Dns4 server information. + + @param Instance The DNS instance. + @param DnsServerCount Retrieved Dns4 server Ip count. + @param DnsServerList Retrieved Dns4 server Ip list. + + @retval EFI_SUCCESS The Dns4 information is got from the DHCP ACK. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NO_MEDIA There was a media error. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +GetDns4ServerFromDhcp4 ( + IN DNS_INSTANCE *Instance, + OUT UINT32 *DnsServerCount, + OUT EFI_IPv4_ADDRESS **DnsServerList + ) +{ + EFI_STATUS Status; + EFI_HANDLE Image; + EFI_HANDLE Controller; + EFI_STATUS MediaStatus; + EFI_HANDLE MnpChildHandle; + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + EFI_MANAGED_NETWORK_CONFIG_DATA MnpConfigData; + EFI_HANDLE Dhcp4Handle; + EFI_DHCP4_PROTOCOL *Dhcp4; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + UINTN DataSize; + VOID *Data; + EFI_IP4_CONFIG2_INTERFACE_INFO *InterfaceInfo; + EFI_DHCP4_PACKET SeedPacket; + EFI_DHCP4_PACKET_OPTION *ParaList[2]; + DNS4_SERVER_INFOR DnsServerInfor; + + EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token; + BOOLEAN IsDone; + UINTN Index; + + Image = Instance->Service->ImageHandle; + Controller = Instance->Service->ControllerHandle; + + MnpChildHandle = NULL; + Mnp = NULL; + + Dhcp4Handle = NULL; + Dhcp4 = NULL; + + Ip4Config2 = NULL; + DataSize = 0; + Data = NULL; + InterfaceInfo = NULL; + + ZeroMem ((UINT8 *) ParaList, sizeof (ParaList)); + + ZeroMem (&MnpConfigData, sizeof (EFI_MANAGED_NETWORK_CONFIG_DATA)); + + ZeroMem (&DnsServerInfor, sizeof (DNS4_SERVER_INFOR)); + + ZeroMem (&Token, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN)); + + DnsServerInfor.ServerCount = DnsServerCount; + + IsDone = FALSE; + + // + // Check media. + // + MediaStatus = EFI_SUCCESS; + NetLibDetectMediaWaitTimeout (Controller, DNS_CHECK_MEDIA_GET_DHCP_WAITING_TIME, &MediaStatus); + if (MediaStatus != EFI_SUCCESS) { + return EFI_NO_MEDIA; + } + + // + // Create a Mnp child instance, get the protocol and config for it. + // + Status = NetLibCreateServiceChild ( + Controller, + Image, + &gEfiManagedNetworkServiceBindingProtocolGuid, + &MnpChildHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **) &Mnp, + Image, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + MnpConfigData.ReceivedQueueTimeoutValue = 0; + MnpConfigData.TransmitQueueTimeoutValue = 0; + MnpConfigData.ProtocolTypeFilter = IP4_ETHER_PROTO; + MnpConfigData.EnableUnicastReceive = TRUE; + MnpConfigData.EnableMulticastReceive = TRUE; + MnpConfigData.EnableBroadcastReceive = TRUE; + MnpConfigData.EnablePromiscuousReceive = FALSE; + MnpConfigData.FlushQueuesOnReset = TRUE; + MnpConfigData.EnableReceiveTimestamps = FALSE; + MnpConfigData.DisableBackgroundPolling = FALSE; + + Status = Mnp->Configure(Mnp, &MnpConfigData); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Create a DHCP4 child instance and get the protocol. + // + Status = NetLibCreateServiceChild ( + Controller, + Image, + &gEfiDhcp4ServiceBindingProtocolGuid, + &Dhcp4Handle + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = gBS->OpenProtocol ( + Dhcp4Handle, + &gEfiDhcp4ProtocolGuid, + (VOID **) &Dhcp4, + Image, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Get Ip4Config2 instance info. + // + Status = gBS->HandleProtocol (Controller, &gEfiIp4Config2ProtocolGuid, (VOID **) &Ip4Config2); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypeInterfaceInfo, &DataSize, Data); + if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) { + goto ON_EXIT; + } + + Data = AllocateZeroPool (DataSize); + if (Data == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypeInterfaceInfo, &DataSize, Data); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + InterfaceInfo = (EFI_IP4_CONFIG2_INTERFACE_INFO *)Data; + + // + // Build required Token. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + DhcpCommonNotify, + &IsDone, + &Token.CompletionEvent + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + SetMem (&Token.RemoteAddress, sizeof (EFI_IPv4_ADDRESS), 0xff); + + Token.RemotePort = 67; + + Token.ListenPointCount = 1; + + Token.ListenPoints = AllocateZeroPool (Token.ListenPointCount * sizeof (EFI_DHCP4_LISTEN_POINT)); + if (Token.ListenPoints == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + if (Instance->Dns4CfgData.UseDefaultSetting) { + CopyMem (&(Token.ListenPoints[0].ListenAddress), &(InterfaceInfo->StationAddress), sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&(Token.ListenPoints[0].SubnetMask), &(InterfaceInfo->SubnetMask), sizeof (EFI_IPv4_ADDRESS)); + } else { + CopyMem (&(Token.ListenPoints[0].ListenAddress), &(Instance->Dns4CfgData.StationIp), sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&(Token.ListenPoints[0].SubnetMask), &(Instance->Dns4CfgData.SubnetMask), sizeof (EFI_IPv4_ADDRESS)); + } + + Token.ListenPoints[0].ListenPort = 68; + + Token.TimeoutValue = DNS_TIME_TO_GETMAP; + + DnsInitSeedPacket (&SeedPacket, InterfaceInfo); + + ParaList[0] = AllocateZeroPool (sizeof (EFI_DHCP4_PACKET_OPTION)); + if (ParaList[0] == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + ParaList[0]->OpCode = DHCP4_TAG_TYPE; + ParaList[0]->Length = 1; + ParaList[0]->Data[0] = DHCP4_MSG_REQUEST; + + ParaList[1] = AllocateZeroPool (sizeof (EFI_DHCP4_PACKET_OPTION)); + if (ParaList[1] == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + ParaList[1]->OpCode = DHCP4_TAG_PARA_LIST; + ParaList[1]->Length = 1; + ParaList[1]->Data[0] = DHCP4_TAG_DNS_SERVER; + + Status = Dhcp4->Build (Dhcp4, &SeedPacket, 0, NULL, 2, ParaList, &Token.Packet); + + Token.Packet->Dhcp4.Header.Xid = HTONL(NET_RANDOM (NetRandomInitSeed ())); + + Token.Packet->Dhcp4.Header.Reserved = HTONS ((UINT16)0x8000); + + if (Instance->Dns4CfgData.UseDefaultSetting) { + CopyMem (&(Token.Packet->Dhcp4.Header.ClientAddr), &(InterfaceInfo->StationAddress), sizeof (EFI_IPv4_ADDRESS)); + } else { + CopyMem (&(Token.Packet->Dhcp4.Header.ClientAddr), &(Instance->Dns4CfgData.StationIp), sizeof (EFI_IPv4_ADDRESS)); + } + + CopyMem (Token.Packet->Dhcp4.Header.ClientHwAddr, &(InterfaceInfo->HwAddress), InterfaceInfo->HwAddressSize); + + Token.Packet->Dhcp4.Header.HwAddrLen = (UINT8)(InterfaceInfo->HwAddressSize); + + // + // TransmitReceive Token + // + Status = Dhcp4->TransmitReceive (Dhcp4, &Token); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Poll the packet + // + do { + Status = Mnp->Poll (Mnp); + } while (!IsDone); + + // + // Parse the ACK to get required information if received done. + // + if (IsDone && !EFI_ERROR (Token.Status)) { + for (Index = 0; Index < Token.ResponseCount; Index++) { + Status = ParseDhcp4Ack (Dhcp4, &Token.ResponseList[Index], &DnsServerInfor); + if (!EFI_ERROR (Status)) { + break; + } + } + + *DnsServerList = DnsServerInfor.ServerList; + } else { + Status = Token.Status; + } + +ON_EXIT: + + if (Data != NULL) { + FreePool (Data); + } + + for (Index = 0; Index < 2; Index++) { + if (ParaList[Index] != NULL) { + FreePool (ParaList[Index]); + } + } + + if (Token.ListenPoints) { + FreePool (Token.ListenPoints); + } + + if (Token.Packet) { + FreePool (Token.Packet); + } + + if (Token.ResponseList != NULL) { + FreePool (Token.ResponseList); + } + + if (Token.CompletionEvent != NULL) { + gBS->CloseEvent (Token.CompletionEvent); + } + + if (Dhcp4 != NULL) { + Dhcp4->Stop (Dhcp4); + Dhcp4->Configure (Dhcp4, NULL); + + gBS->CloseProtocol ( + Dhcp4Handle, + &gEfiDhcp4ProtocolGuid, + Image, + Controller + ); + } + + if (Dhcp4Handle != NULL) { + NetLibDestroyServiceChild ( + Controller, + Image, + &gEfiDhcp4ServiceBindingProtocolGuid, + Dhcp4Handle + ); + } + + if (Mnp != NULL) { + Mnp->Configure (Mnp, NULL); + + gBS->CloseProtocol ( + MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + Image, + Controller + ); + } + + NetLibDestroyServiceChild ( + Controller, + Image, + &gEfiManagedNetworkServiceBindingProtocolGuid, + MnpChildHandle + ); + + return Status; +} + +/** + Parse the DHCP ACK to get Dns6 server information. + + @param Image The handle of the driver image. + @param Controller The handle of the controller. + @param DnsServerCount Retrieved Dns6 server Ip count. + @param DnsServerList Retrieved Dns6 server Ip list. + + @retval EFI_SUCCESS The Dns6 information is got from the DHCP ACK. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NO_MEDIA There was a media error. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +GetDns6ServerFromDhcp6 ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + OUT UINT32 *DnsServerCount, + OUT EFI_IPv6_ADDRESS **DnsServerList + ) +{ + EFI_HANDLE Dhcp6Handle; + EFI_DHCP6_PROTOCOL *Dhcp6; + EFI_STATUS Status; + EFI_STATUS TimerStatus; + EFI_DHCP6_PACKET_OPTION *Oro; + EFI_DHCP6_RETRANSMISSION InfoReqReXmit; + EFI_EVENT Timer; + EFI_STATUS MediaStatus; + DNS6_SERVER_INFOR DnsServerInfor; + + Dhcp6Handle = NULL; + Dhcp6 = NULL; + Oro = NULL; + Timer = NULL; + + ZeroMem (&DnsServerInfor, sizeof (DNS6_SERVER_INFOR)); + + DnsServerInfor.ServerCount = DnsServerCount; + + // + // Check media status before doing DHCP. + // + MediaStatus = EFI_SUCCESS; + NetLibDetectMediaWaitTimeout (Controller, DNS_CHECK_MEDIA_GET_DHCP_WAITING_TIME, &MediaStatus); + if (MediaStatus != EFI_SUCCESS) { + return EFI_NO_MEDIA; + } + + // + // Create a DHCP6 child instance and get the protocol. + // + Status = NetLibCreateServiceChild ( + Controller, + Image, + &gEfiDhcp6ServiceBindingProtocolGuid, + &Dhcp6Handle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + Dhcp6Handle, + &gEfiDhcp6ProtocolGuid, + (VOID **) &Dhcp6, + Image, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Oro = AllocateZeroPool (sizeof (EFI_DHCP6_PACKET_OPTION) + 1); + if (Oro == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Ask the server to reply with DNS options. + // All members in EFI_DHCP6_PACKET_OPTION are in network order. + // + Oro->OpCode = HTONS (DHCP6_TAG_DNS_REQUEST); + Oro->OpLen = HTONS (2); + Oro->Data[1] = DHCP6_TAG_DNS_SERVER; + + InfoReqReXmit.Irt = 4; + InfoReqReXmit.Mrc = 1; + InfoReqReXmit.Mrt = 10; + InfoReqReXmit.Mrd = 30; + + Status = Dhcp6->InfoRequest ( + Dhcp6, + TRUE, + Oro, + 0, + NULL, + &InfoReqReXmit, + NULL, + ParseDhcp6Ack, + &DnsServerInfor + ); + if (Status == EFI_NO_MAPPING) { + Status = gBS->CreateEvent (EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Timer); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = gBS->SetTimer ( + Timer, + TimerRelative, + DNS_TIME_TO_GETMAP * TICKS_PER_SECOND + ); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + do { + TimerStatus = gBS->CheckEvent (Timer); + if (!EFI_ERROR (TimerStatus)) { + Status = Dhcp6->InfoRequest ( + Dhcp6, + TRUE, + Oro, + 0, + NULL, + &InfoReqReXmit, + NULL, + ParseDhcp6Ack, + &DnsServerInfor + ); + } + } while (TimerStatus == EFI_NOT_READY); + } + + *DnsServerList = DnsServerInfor.ServerList; + +ON_EXIT: + + if (Oro != NULL) { + FreePool (Oro); + } + + if (Timer != NULL) { + gBS->CloseEvent (Timer); + } + + if (Dhcp6 != NULL) { + gBS->CloseProtocol ( + Dhcp6Handle, + &gEfiDhcp6ProtocolGuid, + Image, + Controller + ); + } + + NetLibDestroyServiceChild ( + Controller, + Image, + &gEfiDhcp6ServiceBindingProtocolGuid, + Dhcp6Handle + ); + + return Status; + +} + diff --git a/NetworkPkg/DnsDxe/DnsDhcp.h b/NetworkPkg/DnsDxe/DnsDhcp.h new file mode 100644 index 000000000..7212ba0ce --- /dev/null +++ b/NetworkPkg/DnsDxe/DnsDhcp.h @@ -0,0 +1,141 @@ +/** @file +Functions implementation related with DHCPv4/v6 for DNS driver. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _DNS_DHCP_H_ +#define _DNS_DHCP_H_ + +// +// DHCP DNS related +// +#pragma pack(1) + +#define IP4_ETHER_PROTO 0x0800 + +#define DHCP4_OPCODE_REQUEST 1 +#define DHCP4_MAGIC 0x63538263 /// network byte order +#define DHCP4_TAG_EOP 255 /// End Option + +#define DHCP4_TAG_TYPE 53 +#define DHCP4_MSG_REQUEST 3 +#define DHCP4_MSG_INFORM 8 + +#define DHCP4_TAG_PARA_LIST 55 +#define DHCP4_TAG_DNS_SERVER 6 + + +#define DHCP6_TAG_DNS_REQUEST 6 +#define DHCP6_TAG_DNS_SERVER 23 + +#define DNS_CHECK_MEDIA_GET_DHCP_WAITING_TIME EFI_TIMER_PERIOD_SECONDS(20) + +// +// The required Dns4 server information. +// +typedef struct { + UINT32 *ServerCount; + EFI_IPv4_ADDRESS *ServerList; +} DNS4_SERVER_INFOR; + +// +// The required Dns6 server information. +// +typedef struct { + UINT32 *ServerCount; + EFI_IPv6_ADDRESS *ServerList; +} DNS6_SERVER_INFOR; + +#pragma pack() + +/** + Parse the ACK to get required information + + @param Dhcp4 The DHCP4 protocol. + @param Packet Packet waiting for parse. + @param DnsServerInfor The required Dns4 server information. + + @retval EFI_SUCCESS The DNS information is got from the DHCP ACK. + @retval EFI_NO_MAPPING DHCP failed to acquire address and other information. + @retval EFI_DEVICE_ERROR Other errors as indicated. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +ParseDhcp4Ack ( + IN EFI_DHCP4_PROTOCOL *Dhcp4, + IN EFI_DHCP4_PACKET *Packet, + IN DNS4_SERVER_INFOR *DnsServerInfor + ); + +/** + EFI_DHCP6_INFO_CALLBACK is provided by the consumer of the EFI DHCPv6 Protocol + instance to intercept events that occurs in the DHCPv6 Information Request + exchange process. + + @param This Pointer to the EFI_DHCP6_PROTOCOL instance that + is used to configure this callback function. + @param Context Pointer to the context that is initialized in + the EFI_DHCP6_PROTOCOL.InfoRequest(). + @param Packet Pointer to Reply packet that has been received. + The EFI DHCPv6 Protocol instance is responsible + for freeing the buffer. + + @retval EFI_SUCCESS The DNS information is got from the DHCP ACK. + @retval EFI_DEVICE_ERROR Other errors as indicated. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. +**/ +EFI_STATUS +EFIAPI +ParseDhcp6Ack ( + IN EFI_DHCP6_PROTOCOL *This, + IN VOID *Context, + IN EFI_DHCP6_PACKET *Packet + ); + +/** + Parse the DHCP ACK to get Dns4 server information. + + @param Instance The DNS instance. + @param DnsServerCount Retrieved Dns4 server Ip count. + @param DnsServerList Retrieved Dns4 server Ip list. + + @retval EFI_SUCCESS The Dns4 information is got from the DHCP ACK. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NO_MEDIA There was a media error. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +GetDns4ServerFromDhcp4 ( + IN DNS_INSTANCE *Instance, + OUT UINT32 *DnsServerCount, + OUT EFI_IPv4_ADDRESS **DnsServerList + ); + +/** + Parse the DHCP ACK to get Dns6 server information. + + @param Image The handle of the driver image. + @param Controller The handle of the controller. + @param DnsServerCount Retrieved Dns6 server Ip count. + @param DnsServerList Retrieved Dns6 server Ip list. + + @retval EFI_SUCCESS The Dns6 information is got from the DHCP ACK. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NO_MEDIA There was a media error. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +GetDns6ServerFromDhcp6 ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + OUT UINT32 *DnsServerCount, + OUT EFI_IPv6_ADDRESS **DnsServerList + ); + +#endif diff --git a/NetworkPkg/DnsDxe/DnsDriver.c b/NetworkPkg/DnsDxe/DnsDriver.c new file mode 100644 index 000000000..94d072159 --- /dev/null +++ b/NetworkPkg/DnsDxe/DnsDriver.c @@ -0,0 +1,1532 @@ +/** @file +The driver binding and service binding protocol for DnsDxe driver. + +Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "DnsImpl.h" + +EFI_DRIVER_BINDING_PROTOCOL gDns4DriverBinding = { + Dns4DriverBindingSupported, + Dns4DriverBindingStart, + Dns4DriverBindingStop, + DNS_VERSION, + NULL, + NULL +}; + +EFI_DRIVER_BINDING_PROTOCOL gDns6DriverBinding = { + Dns6DriverBindingSupported, + Dns6DriverBindingStart, + Dns6DriverBindingStop, + DNS_VERSION, + NULL, + NULL +}; + +EFI_SERVICE_BINDING_PROTOCOL mDns4ServiceBinding = { + Dns4ServiceBindingCreateChild, + Dns4ServiceBindingDestroyChild +}; + +EFI_SERVICE_BINDING_PROTOCOL mDns6ServiceBinding = { + Dns6ServiceBindingCreateChild, + Dns6ServiceBindingDestroyChild +}; + +DNS_DRIVER_DATA *mDriverData = NULL; + +/** + Destroy the DNS instance and recycle the resources. + + @param[in] Instance The pointer to the DNS instance. + +**/ +VOID +DnsDestroyInstance ( + IN DNS_INSTANCE *Instance + ) +{ + ZeroMem (&Instance->Dns4CfgData, sizeof (EFI_DNS4_CONFIG_DATA)); + + ZeroMem (&Instance->Dns6CfgData, sizeof (EFI_DNS6_CONFIG_DATA)); + + if (!NetMapIsEmpty (&Instance->Dns4TxTokens)) { + Dns4InstanceCancelToken (Instance, NULL); + } + + if (!NetMapIsEmpty (&Instance->Dns6TxTokens)) { + Dns6InstanceCancelToken (Instance, NULL); + } + + if (Instance->UdpIo!= NULL) { + UdpIoFreeIo (Instance->UdpIo); + } + + FreePool (Instance); +} + +/** + Create the DNS instance and initialize it. + + @param[in] Service The pointer to the DNS service. + @param[out] Instance The pointer to the DNS instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_SUCCESS The DNS instance is created. + +**/ +EFI_STATUS +DnsCreateInstance ( + IN DNS_SERVICE *Service, + OUT DNS_INSTANCE **Instance + ) +{ + DNS_INSTANCE *DnsIns; + + *Instance = NULL; + + DnsIns = AllocateZeroPool (sizeof (DNS_INSTANCE)); + if (DnsIns == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + DnsIns->Signature = DNS_INSTANCE_SIGNATURE; + InitializeListHead (&DnsIns->Link); + DnsIns->State = DNS_STATE_UNCONFIGED; + DnsIns->InDestroy = FALSE; + DnsIns->Service = Service; + + if (Service->IpVersion == IP_VERSION_4) { + CopyMem (&DnsIns->Dns4, &mDns4Protocol, sizeof (DnsIns->Dns4)); + NetMapInit (&DnsIns->Dns4TxTokens); + } else { + CopyMem (&DnsIns->Dns6, &mDns6Protocol, sizeof (DnsIns->Dns6)); + NetMapInit (&DnsIns->Dns6TxTokens); + } + + DnsIns->UdpIo = UdpIoCreateIo ( + Service->ControllerHandle, /// NicHandle + Service->ImageHandle, + DnsConfigNullUdp, + Service->IpVersion, + DnsIns + ); + if (DnsIns->UdpIo == NULL) { + FreePool (DnsIns); + return EFI_OUT_OF_RESOURCES; + } + + *Instance = DnsIns; + + return EFI_SUCCESS; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +DnsDestroyChildEntryInHandleBuffer ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + DNS_INSTANCE *Instance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = NET_LIST_USER_STRUCT_S (Entry, DNS_INSTANCE, Link, DNS_INSTANCE_SIGNATURE); + ServiceBinding = ((DNS_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding; + NumberOfChildren = ((DNS_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren; + ChildHandleBuffer = ((DNS_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer; + + if (!NetIsInHandleBuffer (Instance->ChildHandle, NumberOfChildren, ChildHandleBuffer)) { + return EFI_SUCCESS; + } + + return ServiceBinding->DestroyChild (ServiceBinding, Instance->ChildHandle); +} + +/** + Config a NULL UDP that is used to keep the connection between UDP and DNS. + + Just leave the Udp child unconfigured. When UDP is unloaded, + DNS will be informed with DriverBinding Stop. + + @param UdpIo The UDP_IO to configure + @param Context The opaque parameter to the callback + + @retval EFI_SUCCESS It always return EFI_SUCCESS directly. + +**/ +EFI_STATUS +EFIAPI +DnsConfigNullUdp ( + IN UDP_IO *UdpIo, + IN VOID *Context + ) +{ + return EFI_SUCCESS; +} + +/** + Release all the resource used the DNS service binding instance. + + @param DnsSb The Dns service binding instance. + +**/ +VOID +DnsDestroyService ( + IN DNS_SERVICE *DnsSb + ) +{ + UdpIoFreeIo (DnsSb->ConnectUdp); + + if (DnsSb->TimerToGetMap != NULL){ + gBS->CloseEvent (DnsSb->TimerToGetMap); + } + + if (DnsSb->Timer != NULL){ + gBS->CloseEvent (DnsSb->Timer); + } + + FreePool (DnsSb); +} + +/** + Create then initialize a Dns service binding instance. + + @param Controller The controller to install the DNS service + binding on + @param Image The driver binding image of the DNS driver + @param IpVersion IpVersion for this service + @param Service The variable to receive the created service + binding instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to create the instance. + @retval EFI_DEVICE_ERROR Failed to create a NULL UDP port to keep + connection with UDP. + @retval EFI_SUCCESS The service instance is created for the + controller. + +**/ +EFI_STATUS +DnsCreateService ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE Image, + IN UINT8 IpVersion, + OUT DNS_SERVICE **Service + ) +{ + EFI_STATUS Status; + DNS_SERVICE *DnsSb; + + Status = EFI_SUCCESS; + DnsSb = NULL; + + *Service = NULL; + + DnsSb = AllocateZeroPool (sizeof (DNS_SERVICE)); + if (DnsSb == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + DnsSb->Signature = DNS_SERVICE_SIGNATURE; + + if (IpVersion == IP_VERSION_4) { + DnsSb->ServiceBinding = mDns4ServiceBinding; + } else { + DnsSb->ServiceBinding = mDns6ServiceBinding; + } + + DnsSb->Dns4ChildrenNum = 0; + InitializeListHead (&DnsSb->Dns4ChildrenList); + + DnsSb->Dns6ChildrenNum = 0; + InitializeListHead (&DnsSb->Dns6ChildrenList); + + DnsSb->ControllerHandle = Controller; + DnsSb->ImageHandle = Image; + + DnsSb->TimerToGetMap = NULL; + + DnsSb->Timer = NULL; + + DnsSb->IpVersion = IpVersion; + + // + // Create the timer used to time out the procedure which is used to + // get the default IP address. + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &DnsSb->TimerToGetMap + ); + if (EFI_ERROR (Status)) { + FreePool (DnsSb); + return Status; + } + + // + // Create the timer to retransmit packets. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + DnsOnTimerRetransmit, + DnsSb, + &DnsSb->Timer + ); + if (EFI_ERROR (Status)) { + if (DnsSb->TimerToGetMap != NULL) { + gBS->CloseEvent (DnsSb->TimerToGetMap); + } + FreePool (DnsSb); + return Status; + } + + DnsSb->ConnectUdp = NULL; + DnsSb->ConnectUdp = UdpIoCreateIo ( + Controller, + Image, + DnsConfigNullUdp, + DnsSb->IpVersion, + NULL + ); + if (DnsSb->ConnectUdp == NULL) { + if (DnsSb->TimerToGetMap != NULL) { + gBS->CloseEvent (DnsSb->TimerToGetMap); + } + gBS->CloseEvent (DnsSb->Timer); + FreePool (DnsSb); + return EFI_DEVICE_ERROR; + } + + *Service = DnsSb; + return Status; +} + +/** + Unloads an image. + + @param ImageHandle Handle that identifies the image to be unloaded. + + @retval EFI_SUCCESS The image has been unloaded. + @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle. + +**/ +EFI_STATUS +EFIAPI +DnsUnload ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_STATUS Status; + + LIST_ENTRY *Entry; + DNS4_CACHE *ItemCache4; + DNS4_SERVER_IP *ItemServerIp4; + DNS6_CACHE *ItemCache6; + DNS6_SERVER_IP *ItemServerIp6; + + ItemCache4 = NULL; + ItemServerIp4 = NULL; + ItemCache6 = NULL; + ItemServerIp6 = NULL; + + // + // Disconnect the driver specified by ImageHandle + // + Status = NetLibDefaultUnload(ImageHandle); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Free mDriverData. + // + if (mDriverData != NULL) { + if (mDriverData->Timer != NULL) { + gBS->CloseEvent (mDriverData->Timer); + } + + while (!IsListEmpty (&mDriverData->Dns4CacheList)) { + Entry = NetListRemoveHead (&mDriverData->Dns4CacheList); + ASSERT (Entry != NULL); + ItemCache4 = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink); + FreePool (ItemCache4->DnsCache.HostName); + FreePool (ItemCache4->DnsCache.IpAddress); + FreePool (ItemCache4); + } + + while (!IsListEmpty (&mDriverData->Dns4ServerList)) { + Entry = NetListRemoveHead (&mDriverData->Dns4ServerList); + ASSERT (Entry != NULL); + ItemServerIp4 = NET_LIST_USER_STRUCT (Entry, DNS4_SERVER_IP, AllServerLink); + FreePool (ItemServerIp4); + } + + while (!IsListEmpty (&mDriverData->Dns6CacheList)) { + Entry = NetListRemoveHead (&mDriverData->Dns6CacheList); + ASSERT (Entry != NULL); + ItemCache6 = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink); + FreePool (ItemCache6->DnsCache.HostName); + FreePool (ItemCache6->DnsCache.IpAddress); + FreePool (ItemCache6); + } + + while (!IsListEmpty (&mDriverData->Dns6ServerList)) { + Entry = NetListRemoveHead (&mDriverData->Dns6ServerList); + ASSERT (Entry != NULL); + ItemServerIp6 = NET_LIST_USER_STRUCT (Entry, DNS6_SERVER_IP, AllServerLink); + FreePool (ItemServerIp6); + } + + FreePool (mDriverData); + } + + return Status; +} + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + @param ImageHandle The firmware allocated handle for the UEFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval Others An unexpected error occurred. +**/ +EFI_STATUS +EFIAPI +DnsDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + Status = EFI_SUCCESS; + + // + // Install the Dns4 Driver Binding Protocol. + // + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gDns4DriverBinding, + ImageHandle, + &gDnsComponentName, + &gDnsComponentName2 + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Install the Dns6 Driver Binding Protocol. + // + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gDns6DriverBinding, + NULL, + &gDnsComponentName, + &gDnsComponentName2 + ); + if (EFI_ERROR (Status)) { + goto Error1; + } + + // + // Create the driver data structures. + // + mDriverData = AllocateZeroPool (sizeof (DNS_DRIVER_DATA)); + if (mDriverData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error2; + } + + // + // Create the timer event to update DNS cache list. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + DnsOnTimerUpdate, + NULL, + &mDriverData->Timer + ); + if (EFI_ERROR (Status)) { + goto Error3; + } + + Status = gBS->SetTimer (mDriverData->Timer, TimerPeriodic, TICKS_PER_SECOND); + if (EFI_ERROR (Status)) { + goto Error4; + } + + InitializeListHead (&mDriverData->Dns4CacheList); + InitializeListHead (&mDriverData->Dns4ServerList); + InitializeListHead (&mDriverData->Dns6CacheList); + InitializeListHead (&mDriverData->Dns6ServerList); + + return Status; + + Error4: + gBS->CloseEvent (mDriverData->Timer); + + Error3: + FreePool (mDriverData); + + Error2: + EfiLibUninstallDriverBindingComponentName2 ( + &gDns6DriverBinding, + &gDnsComponentName, + &gDnsComponentName2 + ); + + Error1: + EfiLibUninstallDriverBindingComponentName2 ( + &gDns4DriverBinding, + &gDnsComponentName, + &gDnsComponentName2 + ); + + return Status; +} + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +Dns4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // Test for the Dns4ServiceBinding Protocol. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDns4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + // + // Test for the Udp4ServiceBinding Protocol. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + return Status; +} + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +Dns4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + DNS_SERVICE *DnsSb; + EFI_STATUS Status; + + Status = DnsCreateService (ControllerHandle, This->DriverBindingHandle, IP_VERSION_4, &DnsSb); + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (DnsSb != NULL); + + Status = gBS->SetTimer (DnsSb->Timer, TimerPeriodic, TICKS_PER_SECOND); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Install the Dns4ServiceBinding Protocol onto ControllerHandle. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiDns4ServiceBindingProtocolGuid, + &DnsSb->ServiceBinding, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + DnsDestroyService (DnsSb); + + return Status; +} + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +Dns4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + DNS_SERVICE *DnsSb; + EFI_HANDLE NicHandle; + EFI_STATUS Status; + LIST_ENTRY *List; + DNS_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context; + + // + // DNS driver opens UDP child, So, Controller is a UDP + // child handle. Locate the Nic handle first. Then get the + // DNS private data back. + // + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp4ProtocolGuid); + + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiDns4ServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + NicHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + DnsSb = DNS_SERVICE_FROM_THIS (ServiceBinding); + + if (!IsListEmpty (&DnsSb->Dns4ChildrenList)) { + // + // Destroy the Dns child instance in ChildHandleBuffer. + // + List = &DnsSb->Dns4ChildrenList; + Context.ServiceBinding = ServiceBinding; + Context.NumberOfChildren = NumberOfChildren; + Context.ChildHandleBuffer = ChildHandleBuffer; + Status = NetDestroyLinkList ( + List, + DnsDestroyChildEntryInHandleBuffer, + &Context, + NULL + ); + } + + if (NumberOfChildren == 0 && IsListEmpty (&DnsSb->Dns4ChildrenList)) { + gBS->UninstallProtocolInterface ( + NicHandle, + &gEfiDns4ServiceBindingProtocolGuid, + ServiceBinding + ); + + DnsDestroyService (DnsSb); + + if (gDnsControllerNameTable != NULL) { + FreeUnicodeStringTable (gDnsControllerNameTable); + gDnsControllerNameTable = NULL; + } + + Status = EFI_SUCCESS; + } + + return Status; +} + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +Dns6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // Test for the Dns6ServiceBinding Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDns6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + // + // Test for the Udp6ServiceBinding Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + return Status; +} + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +Dns6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + DNS_SERVICE *DnsSb; + EFI_STATUS Status; + + Status = DnsCreateService (ControllerHandle, This->DriverBindingHandle, IP_VERSION_6, &DnsSb); + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (DnsSb != NULL); + + Status = gBS->SetTimer (DnsSb->Timer, TimerPeriodic, TICKS_PER_SECOND); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Install the Dns6ServiceBinding Protocol onto ControllerHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiDns6ServiceBindingProtocolGuid, + &DnsSb->ServiceBinding, + NULL + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + DnsDestroyService (DnsSb); + + return Status; +} + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +Dns6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + DNS_SERVICE *DnsSb; + EFI_HANDLE NicHandle; + EFI_STATUS Status; + LIST_ENTRY *List; + DNS_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context; + + // + // DNS driver opens UDP child, So, Controller is a UDP + // child handle. Locate the Nic handle first. Then get the + // DNS private data back. + // + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp6ProtocolGuid); + + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiDns6ServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + NicHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + DnsSb = DNS_SERVICE_FROM_THIS (ServiceBinding); + + if (!IsListEmpty (&DnsSb->Dns6ChildrenList)) { + // + // Destroy the Dns child instance in ChildHandleBuffer. + // + List = &DnsSb->Dns6ChildrenList; + Context.ServiceBinding = ServiceBinding; + Context.NumberOfChildren = NumberOfChildren; + Context.ChildHandleBuffer = ChildHandleBuffer; + Status = NetDestroyLinkList ( + List, + DnsDestroyChildEntryInHandleBuffer, + &Context, + NULL + ); + } + + if (NumberOfChildren == 0 && IsListEmpty (&DnsSb->Dns6ChildrenList)) { + gBS->UninstallProtocolInterface ( + NicHandle, + &gEfiDns6ServiceBindingProtocolGuid, + ServiceBinding + ); + + DnsDestroyService (DnsSb); + + if (gDnsControllerNameTable != NULL) { + FreeUnicodeStringTable (gDnsControllerNameTable); + gDnsControllerNameTable = NULL; + } + + Status = EFI_SUCCESS; + } + + return Status; +} + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +Dns4ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ) +{ + DNS_SERVICE *DnsSb; + DNS_INSTANCE *Instance; + EFI_STATUS Status; + EFI_TPL OldTpl; + VOID *Udp4; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + DnsSb = DNS_SERVICE_FROM_THIS (This); + + Status = DnsCreateInstance (DnsSb, &Instance); + if (EFI_ERROR (Status)) { + return Status; + } + ASSERT (Instance != NULL); + + // + // Install the DNS protocol onto ChildHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiDns4ProtocolGuid, + &Instance->Dns4, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Instance->ChildHandle = *ChildHandle; + + // + // Open the Udp4 protocol BY_CHILD. + // + Status = gBS->OpenProtocol ( + DnsSb->ConnectUdp->UdpHandle, + &gEfiUdp4ProtocolGuid, + (VOID **) &Udp4, + gDns4DriverBinding.DriverBindingHandle, + Instance->ChildHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + gBS->UninstallMultipleProtocolInterfaces ( + Instance->ChildHandle, + &gEfiDns4ProtocolGuid, + &Instance->Dns4, + NULL + ); + + goto ON_ERROR; + } + + // + // Open the Udp4 protocol by child. + // + Status = gBS->OpenProtocol ( + Instance->UdpIo->UdpHandle, + &gEfiUdp4ProtocolGuid, + (VOID **) &Udp4, + gDns4DriverBinding.DriverBindingHandle, + Instance->ChildHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + // + // Close the Udp4 protocol. + // + gBS->CloseProtocol ( + DnsSb->ConnectUdp->UdpHandle, + &gEfiUdp4ProtocolGuid, + gDns4DriverBinding.DriverBindingHandle, + ChildHandle + ); + + gBS->UninstallMultipleProtocolInterfaces ( + Instance->ChildHandle, + &gEfiDns4ProtocolGuid, + &Instance->Dns4, + NULL + ); + + goto ON_ERROR; + } + + // + // Add it to the parent's child list. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&DnsSb->Dns4ChildrenList, &Instance->Link); + DnsSb->Dns4ChildrenNum++; + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; + +ON_ERROR: + + DnsDestroyInstance (Instance); + return Status; +} + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Dns4ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + DNS_SERVICE *DnsSb; + DNS_INSTANCE *Instance; + + EFI_DNS4_PROTOCOL *Dns4; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Retrieve the private context data structures + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiDns4ProtocolGuid, + (VOID **) &Dns4, + gDns4DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL4 (Dns4); + DnsSb = DNS_SERVICE_FROM_THIS (This); + + if (Instance->Service != DnsSb) { + return EFI_INVALID_PARAMETER; + } + + if (Instance->InDestroy) { + return EFI_SUCCESS; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance->InDestroy = TRUE; + + // + // Close the Udp4 protocol. + // + gBS->CloseProtocol ( + DnsSb->ConnectUdp->UdpHandle, + &gEfiUdp4ProtocolGuid, + gDns4DriverBinding.DriverBindingHandle, + ChildHandle + ); + + gBS->CloseProtocol ( + Instance->UdpIo->UdpHandle, + &gEfiUdp4ProtocolGuid, + gDns4DriverBinding.DriverBindingHandle, + ChildHandle + ); + + gBS->RestoreTPL (OldTpl); + + // + // Uninstall the DNS protocol first to enable a top down destruction. + // + Status = gBS->UninstallProtocolInterface ( + ChildHandle, + &gEfiDns4ProtocolGuid, + Dns4 + ); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (EFI_ERROR (Status)) { + Instance->InDestroy = FALSE; + gBS->RestoreTPL (OldTpl); + return Status; + } + + RemoveEntryList (&Instance->Link); + DnsSb->Dns4ChildrenNum--; + + gBS->RestoreTPL (OldTpl); + + DnsDestroyInstance (Instance); + return EFI_SUCCESS; +} + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +Dns6ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ) +{ + DNS_SERVICE *DnsSb; + DNS_INSTANCE *Instance; + EFI_STATUS Status; + EFI_TPL OldTpl; + VOID *Udp6; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + DnsSb = DNS_SERVICE_FROM_THIS (This); + + Status = DnsCreateInstance (DnsSb, &Instance); + if (EFI_ERROR (Status)) { + return Status; + } + ASSERT (Instance != NULL); + + // + // Install the DNS protocol onto ChildHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiDns6ProtocolGuid, + &Instance->Dns6, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Instance->ChildHandle = *ChildHandle; + + // + // Open the Udp6 protocol BY_CHILD. + // + Status = gBS->OpenProtocol ( + DnsSb->ConnectUdp->UdpHandle, + &gEfiUdp6ProtocolGuid, + (VOID **) &Udp6, + gDns6DriverBinding.DriverBindingHandle, + Instance->ChildHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + gBS->UninstallMultipleProtocolInterfaces ( + Instance->ChildHandle, + &gEfiDns6ProtocolGuid, + &Instance->Dns6, + NULL + ); + + goto ON_ERROR; + } + + // + // Open the Udp6 protocol by child. + // + Status = gBS->OpenProtocol ( + Instance->UdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + (VOID **) &Udp6, + gDns6DriverBinding.DriverBindingHandle, + Instance->ChildHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + // + // Close the Udp6 protocol. + // + gBS->CloseProtocol ( + DnsSb->ConnectUdp->UdpHandle, + &gEfiUdp6ProtocolGuid, + gDns6DriverBinding.DriverBindingHandle, + ChildHandle + ); + + gBS->UninstallMultipleProtocolInterfaces ( + Instance->ChildHandle, + &gEfiDns6ProtocolGuid, + &Instance->Dns6, + NULL + ); + + goto ON_ERROR; + } + + // + // Add it to the parent's child list. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&DnsSb->Dns6ChildrenList, &Instance->Link); + DnsSb->Dns6ChildrenNum++; + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; + +ON_ERROR: + + DnsDestroyInstance (Instance); + return Status; +} + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Dns6ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + DNS_SERVICE *DnsSb; + DNS_INSTANCE *Instance; + + EFI_DNS6_PROTOCOL *Dns6; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Retrieve the private context data structures + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiDns6ProtocolGuid, + (VOID **) &Dns6, + gDns6DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL6 (Dns6); + DnsSb = DNS_SERVICE_FROM_THIS (This); + + if (Instance->Service != DnsSb) { + return EFI_INVALID_PARAMETER; + } + + if (Instance->InDestroy) { + return EFI_SUCCESS; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance->InDestroy = TRUE; + + // + // Close the Udp6 protocol. + // + gBS->CloseProtocol ( + DnsSb->ConnectUdp->UdpHandle, + &gEfiUdp6ProtocolGuid, + gDns6DriverBinding.DriverBindingHandle, + ChildHandle + ); + + gBS->CloseProtocol ( + Instance->UdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + gDns6DriverBinding.DriverBindingHandle, + ChildHandle + ); + + gBS->RestoreTPL (OldTpl); + + // + // Uninstall the DNS protocol first to enable a top down destruction. + // + Status = gBS->UninstallProtocolInterface ( + ChildHandle, + &gEfiDns6ProtocolGuid, + Dns6 + ); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (EFI_ERROR (Status)) { + Instance->InDestroy = FALSE; + gBS->RestoreTPL (OldTpl); + return Status; + } + + RemoveEntryList (&Instance->Link); + DnsSb->Dns6ChildrenNum--; + + gBS->RestoreTPL (OldTpl); + + DnsDestroyInstance (Instance); + return EFI_SUCCESS; +} diff --git a/NetworkPkg/DnsDxe/DnsDriver.h b/NetworkPkg/DnsDxe/DnsDriver.h new file mode 100644 index 000000000..471b1134d --- /dev/null +++ b/NetworkPkg/DnsDxe/DnsDriver.h @@ -0,0 +1,598 @@ +/** @file +The header files of the driver binding and service binding protocol for DnsDxe driver. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _DNS_DRIVER_H_ +#define _DNS_DRIVER_H_ + +#include +#include + +/// +/// Dns service block +/// +typedef struct _DNS_DRIVER_DATA DNS_DRIVER_DATA; + +/// +/// Dns service block +/// +typedef struct _DNS_SERVICE DNS_SERVICE; + +/// +/// Dns instance block +/// +typedef struct _DNS_INSTANCE DNS_INSTANCE; + +#define DNS_SERVICE_SIGNATURE SIGNATURE_32 ('D', 'N', 'S', 'S') + +#define DNS_INSTANCE_SIGNATURE SIGNATURE_32 ('D', 'N', 'S', 'I') + +struct _DNS_DRIVER_DATA { + EFI_EVENT Timer; /// Ticking timer for DNS cache update. + + LIST_ENTRY Dns4CacheList; + LIST_ENTRY Dns4ServerList; + + LIST_ENTRY Dns6CacheList; + LIST_ENTRY Dns6ServerList; +}; + +struct _DNS_SERVICE { + UINT32 Signature; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + + UINT16 Dns4ChildrenNum; + LIST_ENTRY Dns4ChildrenList; + + UINT16 Dns6ChildrenNum; + LIST_ENTRY Dns6ChildrenList; + + EFI_HANDLE ControllerHandle; + EFI_HANDLE ImageHandle; + + EFI_EVENT TimerToGetMap; + + EFI_EVENT Timer; /// Ticking timer for packet retransmission. + + UINT8 IpVersion; + UDP_IO *ConnectUdp; +}; + +struct _DNS_INSTANCE { + UINT32 Signature; + LIST_ENTRY Link; + + EFI_DNS4_PROTOCOL Dns4; + EFI_DNS6_PROTOCOL Dns6; + + INTN State; + BOOLEAN InDestroy; + + DNS_SERVICE *Service; + EFI_HANDLE ChildHandle; + + EFI_DNS4_CONFIG_DATA Dns4CfgData; + EFI_DNS6_CONFIG_DATA Dns6CfgData; + + EFI_IP_ADDRESS SessionDnsServer; + + NET_MAP Dns4TxTokens; + NET_MAP Dns6TxTokens; + + UDP_IO *UdpIo; +}; + +typedef struct { + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; +} DNS_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT; + +extern DNS_DRIVER_DATA *mDriverData; + +#define DNS_SERVICE_FROM_THIS(a) \ + CR (a, DNS_SERVICE, ServiceBinding, DNS_SERVICE_SIGNATURE) + +#define DNS_INSTANCE_FROM_THIS_PROTOCOL4(a) \ + CR (a, DNS_INSTANCE, Dns4, DNS_INSTANCE_SIGNATURE) + +#define DNS_INSTANCE_FROM_THIS_PROTOCOL6(a) \ + CR (a, DNS_INSTANCE, Dns6, DNS_INSTANCE_SIGNATURE) + + +/** + Destroy the DNS instance and recycle the resources. + + @param[in] Instance The pointer to the DNS instance. + +**/ +VOID +DnsDestroyInstance ( + IN DNS_INSTANCE *Instance + ); + +/** + Create the DNS instance and initialize it. + + @param[in] Service The pointer to the DNS service. + @param[out] Instance The pointer to the DNS instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_SUCCESS The DNS instance is created. + +**/ +EFI_STATUS +DnsCreateInstance ( + IN DNS_SERVICE *Service, + OUT DNS_INSTANCE **Instance + ); + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +DnsDestroyChildEntryInHandleBuffer ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ); + +/** + Config a NULL UDP that is used to keep the connection between UDP and DNS. + + Just leave the Udp child unconfigured. When UDP is unloaded, + DNS will be informed with DriverBinding Stop. + + @param UdpIo The UDP_IO to configure + @param Context The opaque parameter to the callback + + @retval EFI_SUCCESS It always return EFI_SUCCESS directly. + +**/ +EFI_STATUS +EFIAPI +DnsConfigNullUdp ( + IN UDP_IO *UdpIo, + IN VOID *Context + ); + +/** + Release all the resource used the DNS service binding instance. + + @param DnsSb The Dns service binding instance. + +**/ +VOID +DnsDestroyService ( + IN DNS_SERVICE *DnsSb + ); + +/** + Create then initialize a Dns service binding instance. + + @param Controller The controller to install the DNS service + binding on + @param Image The driver binding image of the DNS driver + @param IpVersion IpVersion for this service + @param Service The variable to receive the created service + binding instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to create the instance. + @retval EFI_DEVICE_ERROR Failed to create a NULL UDP port to keep + connection with UDP. + @retval EFI_SUCCESS The service instance is created for the + controller. + +**/ +EFI_STATUS +DnsCreateService ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE Image, + IN UINT8 IpVersion, + OUT DNS_SERVICE **Service + ); + +/** + Unloads an image. + + @param ImageHandle Handle that identifies the image to be unloaded. + + @retval EFI_SUCCESS The image has been unloaded. + @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle. + +**/ +EFI_STATUS +EFIAPI +DnsUnload ( + IN EFI_HANDLE ImageHandle + ); + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + @param ImageHandle The firmware allocated handle for the UEFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval Others An unexpected error occurred. +**/ +EFI_STATUS +EFIAPI +DnsDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ); + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +Dns4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +Dns4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +Dns4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +Dns6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +Dns6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +Dns6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +Dns4ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Dns4ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +Dns6ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Dns6ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + + +#endif diff --git a/NetworkPkg/DnsDxe/DnsDxe.inf b/NetworkPkg/DnsDxe/DnsDxe.inf new file mode 100644 index 000000000..3a66bdd13 --- /dev/null +++ b/NetworkPkg/DnsDxe/DnsDxe.inf @@ -0,0 +1,72 @@ +## @file +# Implementation of EFI_DNS4_PROTOCOL and EFI_DNS6_PROTOCOL interfaces. +# +# Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = DnsDxe + FILE_GUID = b219e140-dffc-11e3-b956-0022681e6906 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = DnsDriverEntryPoint + UNLOAD_IMAGE = DnsUnload + MODULE_UNI_FILE = DnsDxe.uni + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[Sources] + ComponentName.c + DnsDriver.h + DnsDriver.c + DnsImpl.h + DnsImpl.c + DnsProtocol.c + DnsDhcp.h + DnsDhcp.c + + +[LibraryClasses] + BaseLib + UefiLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiRuntimeServicesTableLib + BaseMemoryLib + MemoryAllocationLib + NetLib + DebugLib + DpcLib + PrintLib + UdpIoLib + + +[Protocols] + gEfiDns4ServiceBindingProtocolGuid ## BY_START + gEfiDns4ProtocolGuid ## BY_START + gEfiUdp4ServiceBindingProtocolGuid ## TO_START + gEfiUdp4ProtocolGuid ## TO_START + gEfiDhcp4ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiDhcp4ProtocolGuid ## SOMETIMES_CONSUMES + gEfiIp4Config2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiManagedNetworkServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiManagedNetworkProtocolGuid ## SOMETIMES_CONSUMES + + gEfiDns6ServiceBindingProtocolGuid ## BY_START + gEfiDns6ProtocolGuid ## BY_START + gEfiUdp6ServiceBindingProtocolGuid ## TO_START + gEfiUdp6ProtocolGuid ## TO_START + gEfiDhcp6ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiDhcp6ProtocolGuid ## SOMETIMES_CONSUMES + +[UserExtensions.TianoCore."ExtraFiles"] + DnsDxeExtra.uni + diff --git a/NetworkPkg/DnsDxe/DnsDxe.uni b/NetworkPkg/DnsDxe/DnsDxe.uni new file mode 100644 index 000000000..6e5e38693 --- /dev/null +++ b/NetworkPkg/DnsDxe/DnsDxe.uni @@ -0,0 +1,17 @@ +// /** @file +// UEFI DNS DXE Driver. +// +// This driver provides UEFI 2.5 DNS protocols. It could work with an IPv4 and IPv6 stack. +// +// +// Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "UEFI DNS service" + +#string STR_MODULE_DESCRIPTION #language en-US "This driver provides EFI DNS4 Protocol, EFI DNS6 Protocol, EFI DNS4 Service Binding Protocol and EFI DNS6 Service Binding Protocol. It could work with an IPv4 and IPv6 stack." + diff --git a/NetworkPkg/DnsDxe/DnsDxeExtra.uni b/NetworkPkg/DnsDxe/DnsDxeExtra.uni new file mode 100644 index 000000000..22aef7914 --- /dev/null +++ b/NetworkPkg/DnsDxe/DnsDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// DnsDxe Localized Strings and Content +// +// Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"UEFI DNS DXE" + + diff --git a/NetworkPkg/DnsDxe/DnsImpl.c b/NetworkPkg/DnsDxe/DnsImpl.c new file mode 100644 index 000000000..ca4ef506a --- /dev/null +++ b/NetworkPkg/DnsDxe/DnsImpl.c @@ -0,0 +1,2243 @@ +/** @file +DnsDxe support functions implementation. + +Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "DnsImpl.h" + +/** + Remove TokenEntry from TokenMap. + + @param[in] TokenMap All DNSv4 Token entrys. + @param[in] TokenEntry TokenEntry need to be removed. + + @retval EFI_SUCCESS Remove TokenEntry from TokenMap sucessfully. + @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap. + +**/ +EFI_STATUS +Dns4RemoveTokenEntry ( + IN NET_MAP *TokenMap, + IN DNS4_TOKEN_ENTRY *TokenEntry + ) +{ + NET_MAP_ITEM *Item; + + // + // Find the TokenEntry first. + // + Item = NetMapFindKey (TokenMap, (VOID *) TokenEntry); + + if (Item != NULL) { + // + // Remove the TokenEntry if it's found in the map. + // + NetMapRemoveItem (TokenMap, Item, NULL); + + return EFI_SUCCESS; + } + + return EFI_NOT_FOUND; +} + +/** + Remove TokenEntry from TokenMap. + + @param[in] TokenMap All DNSv6 Token entrys. + @param[in] TokenEntry TokenEntry need to be removed. + + @retval EFI_SUCCESS Remove TokenEntry from TokenMap sucessfully. + @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap. + +**/ +EFI_STATUS +Dns6RemoveTokenEntry ( + IN NET_MAP *TokenMap, + IN DNS6_TOKEN_ENTRY *TokenEntry + ) +{ + NET_MAP_ITEM *Item; + + // + // Find the TokenEntry first. + // + Item = NetMapFindKey (TokenMap, (VOID *) TokenEntry); + + if (Item != NULL) { + // + // Remove the TokenEntry if it's found in the map. + // + NetMapRemoveItem (TokenMap, Item, NULL); + + return EFI_SUCCESS; + } + + return EFI_NOT_FOUND; +} + +/** + This function cancle the token specified by Arg in the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the token to be cancelled. If NULL, all + the tokens in this Map will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token + is not the same as that in the Item, if Arg is not + NULL. + @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is + cancelled. + +**/ +EFI_STATUS +EFIAPI +Dns4CancelTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ) +{ + DNS4_TOKEN_ENTRY *TokenEntry; + NET_BUF *Packet; + UDP_IO *UdpIo; + + if ((Arg != NULL) && (Item->Key != Arg)) { + return EFI_SUCCESS; + } + + if (Item->Value != NULL) { + // + // If the TokenEntry is a transmit TokenEntry, the corresponding Packet is recorded in + // Item->Value. + // + Packet = (NET_BUF *) (Item->Value); + UdpIo = (UDP_IO *) (*((UINTN *) &Packet->ProtoData[0])); + + UdpIoCancelSentDatagram (UdpIo, Packet); + } + + // + // Remove TokenEntry from Dns4TxTokens. + // + TokenEntry = (DNS4_TOKEN_ENTRY *) Item->Key; + if (Dns4RemoveTokenEntry (Map, TokenEntry) == EFI_SUCCESS) { + TokenEntry->Token->Status = EFI_ABORTED; + gBS->SignalEvent (TokenEntry->Token->Event); + DispatchDpc (); + } + + if (Arg != NULL) { + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + +/** + This function cancle the token specified by Arg in the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the token to be cancelled. If NULL, all + the tokens in this Map will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token + is not the same as that in the Item, if Arg is not + NULL. + @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is + cancelled. + +**/ +EFI_STATUS +EFIAPI +Dns6CancelTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ) +{ + DNS6_TOKEN_ENTRY *TokenEntry; + NET_BUF *Packet; + UDP_IO *UdpIo; + + if ((Arg != NULL) && (Item->Key != Arg)) { + return EFI_SUCCESS; + } + + if (Item->Value != NULL) { + // + // If the TokenEntry is a transmit TokenEntry, the corresponding Packet is recorded in + // Item->Value. + // + Packet = (NET_BUF *) (Item->Value); + UdpIo = (UDP_IO *) (*((UINTN *) &Packet->ProtoData[0])); + + UdpIoCancelSentDatagram (UdpIo, Packet); + } + + // + // Remove TokenEntry from Dns6TxTokens. + // + TokenEntry = (DNS6_TOKEN_ENTRY *) Item->Key; + if (Dns6RemoveTokenEntry (Map, TokenEntry) == EFI_SUCCESS) { + TokenEntry->Token->Status = EFI_ABORTED; + gBS->SignalEvent (TokenEntry->Token->Event); + DispatchDpc (); + } + + if (Arg != NULL) { + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + +/** + Get the TokenEntry from the TokensMap. + + @param[in] TokensMap All DNSv4 Token entrys + @param[in] Token Pointer to the token to be get. + @param[out] TokenEntry Pointer to TokenEntry corresponding Token. + + @retval EFI_SUCCESS Get the TokenEntry from the TokensMap sucessfully. + @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap. + +**/ +EFI_STATUS +EFIAPI +GetDns4TokenEntry ( + IN NET_MAP *TokensMap, + IN EFI_DNS4_COMPLETION_TOKEN *Token, + OUT DNS4_TOKEN_ENTRY **TokenEntry + ) +{ + LIST_ENTRY *Entry; + + NET_MAP_ITEM *Item; + + NET_LIST_FOR_EACH (Entry, &TokensMap->Used) { + Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link); + *TokenEntry = (DNS4_TOKEN_ENTRY *) (Item->Key); + if ((*TokenEntry)->Token == Token) { + return EFI_SUCCESS; + } + } + + *TokenEntry = NULL; + + return EFI_NOT_FOUND; +} + +/** + Get the TokenEntry from the TokensMap. + + @param[in] TokensMap All DNSv6 Token entrys + @param[in] Token Pointer to the token to be get. + @param[out] TokenEntry Pointer to TokenEntry corresponding Token. + + @retval EFI_SUCCESS Get the TokenEntry from the TokensMap sucessfully. + @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap. + +**/ +EFI_STATUS +EFIAPI +GetDns6TokenEntry ( + IN NET_MAP *TokensMap, + IN EFI_DNS6_COMPLETION_TOKEN *Token, + OUT DNS6_TOKEN_ENTRY **TokenEntry + ) +{ + LIST_ENTRY *Entry; + + NET_MAP_ITEM *Item; + + NET_LIST_FOR_EACH (Entry, &TokensMap->Used) { + Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link); + *TokenEntry = (DNS6_TOKEN_ENTRY *) (Item->Key); + if ((*TokenEntry)->Token == Token) { + return EFI_SUCCESS; + } + } + + *TokenEntry =NULL; + + return EFI_NOT_FOUND; +} + +/** + Cancel DNS4 tokens from the DNS4 instance. + + @param[in] Instance Pointer to the DNS instance context data. + @param[in] Token Pointer to the token to be canceled. If NULL, all + tokens in this instance will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The Token is cancelled. + @retval EFI_NOT_FOUND The Token is not found. + +**/ +EFI_STATUS +Dns4InstanceCancelToken ( + IN DNS_INSTANCE *Instance, + IN EFI_DNS4_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + DNS4_TOKEN_ENTRY *TokenEntry; + + TokenEntry = NULL; + + if(Token != NULL ) { + Status = GetDns4TokenEntry (&Instance->Dns4TxTokens, Token, &TokenEntry); + if (EFI_ERROR (Status)) { + return Status; + } + } else { + TokenEntry = NULL; + } + + // + // Cancel this TokenEntry from the Dns4TxTokens map. + // + Status = NetMapIterate (&Instance->Dns4TxTokens, Dns4CancelTokens, TokenEntry); + + if ((TokenEntry != NULL) && (Status == EFI_ABORTED)) { + // + // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from + // the Dns4TxTokens and returns success. + // + if (NetMapIsEmpty (&Instance->Dns4TxTokens)) { + Instance->UdpIo->Protocol.Udp4->Cancel (Instance->UdpIo->Protocol.Udp4, &Instance->UdpIo->RecvRequest->Token.Udp4); + } + return EFI_SUCCESS; + } + + ASSERT ((TokenEntry != NULL) || (0 == NetMapGetCount (&Instance->Dns4TxTokens))); + + if (NetMapIsEmpty (&Instance->Dns4TxTokens)) { + Instance->UdpIo->Protocol.Udp4->Cancel (Instance->UdpIo->Protocol.Udp4, &Instance->UdpIo->RecvRequest->Token.Udp4); + } + + return EFI_SUCCESS; +} + +/** + Cancel DNS6 tokens from the DNS6 instance. + + @param[in] Instance Pointer to the DNS instance context data. + @param[in] Token Pointer to the token to be canceled. If NULL, all + tokens in this instance will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The Token is cancelled. + @retval EFI_NOT_FOUND The Token is not found. + +**/ +EFI_STATUS +Dns6InstanceCancelToken ( + IN DNS_INSTANCE *Instance, + IN EFI_DNS6_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + DNS6_TOKEN_ENTRY *TokenEntry; + + TokenEntry = NULL; + + if(Token != NULL ) { + Status = GetDns6TokenEntry (&Instance->Dns6TxTokens, Token, &TokenEntry); + if (EFI_ERROR (Status)) { + return Status; + } + } else { + TokenEntry = NULL; + } + + // + // Cancel this TokenEntry from the Dns6TxTokens map. + // + Status = NetMapIterate (&Instance->Dns6TxTokens, Dns6CancelTokens, TokenEntry); + + if ((TokenEntry != NULL) && (Status == EFI_ABORTED)) { + // + // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from + // the Dns6TxTokens and returns success. + // + if (NetMapIsEmpty (&Instance->Dns6TxTokens)) { + Instance->UdpIo->Protocol.Udp6->Cancel (Instance->UdpIo->Protocol.Udp6, &Instance->UdpIo->RecvRequest->Token.Udp6); + } + return EFI_SUCCESS; + } + + ASSERT ((TokenEntry != NULL) || (0 == NetMapGetCount (&Instance->Dns6TxTokens))); + + if (NetMapIsEmpty (&Instance->Dns6TxTokens)) { + Instance->UdpIo->Protocol.Udp6->Cancel (Instance->UdpIo->Protocol.Udp6, &Instance->UdpIo->RecvRequest->Token.Udp6); + } + + return EFI_SUCCESS; +} + +/** + Free the resource related to the configure parameters. + + @param Config The DNS configure data + +**/ +VOID +Dns4CleanConfigure ( + IN OUT EFI_DNS4_CONFIG_DATA *Config + ) +{ + if (Config->DnsServerList != NULL) { + FreePool (Config->DnsServerList); + } + + ZeroMem (Config, sizeof (EFI_DNS4_CONFIG_DATA)); +} + +/** + Free the resource related to the configure parameters. + + @param Config The DNS configure data + +**/ +VOID +Dns6CleanConfigure ( + IN OUT EFI_DNS6_CONFIG_DATA *Config + ) +{ + if (Config->DnsServerList != NULL) { + FreePool (Config->DnsServerList); + } + + ZeroMem (Config, sizeof (EFI_DNS6_CONFIG_DATA)); +} + +/** + Allocate memory for configure parameter such as timeout value for Dst, + then copy the configure parameter from Src to Dst. + + @param[out] Dst The destination DHCP configure data. + @param[in] Src The source DHCP configure data. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_SUCCESS The configure is copied. + +**/ +EFI_STATUS +Dns4CopyConfigure ( + OUT EFI_DNS4_CONFIG_DATA *Dst, + IN EFI_DNS4_CONFIG_DATA *Src + ) +{ + UINTN Len; + UINT32 Index; + + CopyMem (Dst, Src, sizeof (*Dst)); + Dst->DnsServerList = NULL; + + // + // Allocate a memory then copy DnsServerList to it + // + if (Src->DnsServerList != NULL) { + Len = Src->DnsServerListCount * sizeof (EFI_IPv4_ADDRESS); + Dst->DnsServerList = AllocatePool (Len); + if (Dst->DnsServerList == NULL) { + Dns4CleanConfigure (Dst); + return EFI_OUT_OF_RESOURCES; + } + + for (Index = 0; Index < Src->DnsServerListCount; Index++) { + CopyMem (&Dst->DnsServerList[Index], &Src->DnsServerList[Index], sizeof (EFI_IPv4_ADDRESS)); + } + } + + return EFI_SUCCESS; +} + +/** + Allocate memory for configure parameter such as timeout value for Dst, + then copy the configure parameter from Src to Dst. + + @param[out] Dst The destination DHCP configure data. + @param[in] Src The source DHCP configure data. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_SUCCESS The configure is copied. + +**/ +EFI_STATUS +Dns6CopyConfigure ( + OUT EFI_DNS6_CONFIG_DATA *Dst, + IN EFI_DNS6_CONFIG_DATA *Src + ) +{ + UINTN Len; + UINT32 Index; + + CopyMem (Dst, Src, sizeof (*Dst)); + Dst->DnsServerList = NULL; + + // + // Allocate a memory then copy DnsServerList to it + // + if (Src->DnsServerList != NULL) { + Len = Src->DnsServerCount * sizeof (EFI_IPv6_ADDRESS); + Dst->DnsServerList = AllocatePool (Len); + if (Dst->DnsServerList == NULL) { + Dns6CleanConfigure (Dst); + return EFI_OUT_OF_RESOURCES; + } + + for (Index = 0; Index < Src->DnsServerCount; Index++) { + CopyMem (&Dst->DnsServerList[Index], &Src->DnsServerList[Index], sizeof (EFI_IPv6_ADDRESS)); + } + } + + return EFI_SUCCESS; +} + +/** + Callback of Dns packet. Does nothing. + + @param Arg The context. + +**/ +VOID +EFIAPI +DnsDummyExtFree ( + IN VOID *Arg + ) +{ +} + +/** + Poll the UDP to get the IP4 default address, which may be retrieved + by DHCP. + + The default time out value is 5 seconds. If IP has retrieved the default address, + the UDP is reconfigured. + + @param Instance The DNS instance + @param UdpIo The UDP_IO to poll + @param UdpCfgData The UDP configure data to reconfigure the UDP_IO + + @retval TRUE The default address is retrieved and UDP is reconfigured. + @retval FALSE Some error occured. + +**/ +BOOLEAN +Dns4GetMapping ( + IN DNS_INSTANCE *Instance, + IN UDP_IO *UdpIo, + IN EFI_UDP4_CONFIG_DATA *UdpCfgData + ) +{ + DNS_SERVICE *Service; + EFI_IP4_MODE_DATA Ip4Mode; + EFI_UDP4_PROTOCOL *Udp; + EFI_STATUS Status; + + ASSERT (Instance->Dns4CfgData.UseDefaultSetting); + + Service = Instance->Service; + Udp = UdpIo->Protocol.Udp4; + + Status = gBS->SetTimer ( + Service->TimerToGetMap, + TimerRelative, + DNS_TIME_TO_GETMAP * TICKS_PER_SECOND + ); + if (EFI_ERROR (Status)) { + return FALSE; + } + + while (EFI_ERROR (gBS->CheckEvent (Service->TimerToGetMap))) { + Udp->Poll (Udp); + + if (!EFI_ERROR (Udp->GetModeData (Udp, NULL, &Ip4Mode, NULL, NULL)) && + Ip4Mode.IsConfigured) { + + Udp->Configure (Udp, NULL); + return (BOOLEAN) (Udp->Configure (Udp, UdpCfgData) == EFI_SUCCESS); + } + } + + return FALSE; +} + +/** + Configure the opened Udp6 instance until the corresponding Ip6 instance + has been configured. + + @param Instance The DNS instance + @param UdpIo The UDP_IO to poll + @param UdpCfgData The UDP configure data to reconfigure the UDP_IO + + @retval TRUE Configure the Udp6 instance successfully. + @retval FALSE Some error occured. + +**/ +BOOLEAN +Dns6GetMapping ( + IN DNS_INSTANCE *Instance, + IN UDP_IO *UdpIo, + IN EFI_UDP6_CONFIG_DATA *UdpCfgData + ) +{ + DNS_SERVICE *Service; + EFI_IP6_MODE_DATA Ip6Mode; + EFI_UDP6_PROTOCOL *Udp; + EFI_STATUS Status; + + Service = Instance->Service; + Udp = UdpIo->Protocol.Udp6; + + Status = gBS->SetTimer ( + Service->TimerToGetMap, + TimerRelative, + DNS_TIME_TO_GETMAP * TICKS_PER_SECOND + ); + if (EFI_ERROR (Status)) { + return FALSE; + } + + while (EFI_ERROR (gBS->CheckEvent (Service->TimerToGetMap))) { + Udp->Poll (Udp); + + if (!EFI_ERROR (Udp->GetModeData (Udp, NULL, &Ip6Mode, NULL, NULL))) { + if (Ip6Mode.AddressList != NULL) { + FreePool (Ip6Mode.AddressList); + } + + if (Ip6Mode.GroupTable != NULL) { + FreePool (Ip6Mode.GroupTable); + } + + if (Ip6Mode.RouteTable != NULL) { + FreePool (Ip6Mode.RouteTable); + } + + if (Ip6Mode.NeighborCache != NULL) { + FreePool (Ip6Mode.NeighborCache); + } + + if (Ip6Mode.PrefixTable != NULL) { + FreePool (Ip6Mode.PrefixTable); + } + + if (Ip6Mode.IcmpTypeList != NULL) { + FreePool (Ip6Mode.IcmpTypeList); + } + + if (!Ip6Mode.IsStarted || Ip6Mode.IsConfigured) { + Udp->Configure (Udp, NULL); + if (Udp->Configure (Udp, UdpCfgData) == EFI_SUCCESS) { + return TRUE; + } + } + } + } + + return FALSE; +} + +/** + Configure the UDP. + + @param Instance The DNS session + @param UdpIo The UDP_IO instance + + @retval EFI_SUCCESS The UDP is successfully configured for the + session. + +**/ +EFI_STATUS +Dns4ConfigUdp ( + IN DNS_INSTANCE *Instance, + IN UDP_IO *UdpIo + ) +{ + EFI_DNS4_CONFIG_DATA *Config; + EFI_UDP4_CONFIG_DATA UdpConfig; + EFI_STATUS Status; + + Config = &Instance->Dns4CfgData; + + UdpConfig.AcceptBroadcast = FALSE; + UdpConfig.AcceptPromiscuous = FALSE; + UdpConfig.AcceptAnyPort = FALSE; + UdpConfig.AllowDuplicatePort = FALSE; + UdpConfig.TypeOfService = 0; + UdpConfig.TimeToLive = 128; + UdpConfig.DoNotFragment = FALSE; + UdpConfig.ReceiveTimeout = 0; + UdpConfig.TransmitTimeout = 0; + UdpConfig.UseDefaultAddress = Config->UseDefaultSetting; + UdpConfig.SubnetMask = Config->SubnetMask; + UdpConfig.StationPort = Config->LocalPort; + UdpConfig.RemotePort = DNS_SERVER_PORT; + + CopyMem (&UdpConfig.StationAddress, &Config->StationIp, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&UdpConfig.RemoteAddress, &Instance->SessionDnsServer.v4, sizeof (EFI_IPv4_ADDRESS)); + + Status = UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfig); + + if ((Status == EFI_NO_MAPPING) && Dns4GetMapping (Instance, UdpIo, &UdpConfig)) { + return EFI_SUCCESS; + } + + return Status; +} + +/** + Configure the UDP. + + @param Instance The DNS session + @param UdpIo The UDP_IO instance + + @retval EFI_SUCCESS The UDP is successfully configured for the + session. + +**/ +EFI_STATUS +Dns6ConfigUdp ( + IN DNS_INSTANCE *Instance, + IN UDP_IO *UdpIo + ) +{ + EFI_DNS6_CONFIG_DATA *Config; + EFI_UDP6_CONFIG_DATA UdpConfig; + EFI_STATUS Status; + + Config = &Instance->Dns6CfgData; + + UdpConfig.AcceptPromiscuous = FALSE; + UdpConfig.AcceptAnyPort = FALSE; + UdpConfig.AllowDuplicatePort = FALSE; + UdpConfig.TrafficClass = 0; + UdpConfig.HopLimit = 128; + UdpConfig.ReceiveTimeout = 0; + UdpConfig.TransmitTimeout = 0; + UdpConfig.StationPort = Config->LocalPort; + UdpConfig.RemotePort = DNS_SERVER_PORT; + CopyMem (&UdpConfig.StationAddress, &Config->StationIp, sizeof (EFI_IPv6_ADDRESS)); + CopyMem (&UdpConfig.RemoteAddress, &Instance->SessionDnsServer.v6, sizeof (EFI_IPv6_ADDRESS)); + + Status = UdpIo->Protocol.Udp6->Configure (UdpIo->Protocol.Udp6, &UdpConfig); + + if ((Status == EFI_NO_MAPPING) && Dns6GetMapping (Instance, UdpIo, &UdpConfig)) { + return EFI_SUCCESS; + } + + return Status; +} + +/** + Update Dns4 cache to shared list of caches of all DNSv4 instances. + + @param Dns4CacheList All Dns4 cache list. + @param DeleteFlag If FALSE, this function is to add one entry to the DNS Cache. + If TRUE, this function will delete matching DNS Cache entry. + @param Override If TRUE, the matching DNS cache entry will be overwritten with the supplied parameter. + If FALSE, EFI_ACCESS_DENIED will be returned if the entry to be added is already exists. + @param DnsCacheEntry Entry Pointer to DNS Cache entry. + + @retval EFI_SUCCESS Update Dns4 cache successfully. + @retval Others Failed to update Dns4 cache. + +**/ +EFI_STATUS +EFIAPI +UpdateDns4Cache ( + IN LIST_ENTRY *Dns4CacheList, + IN BOOLEAN DeleteFlag, + IN BOOLEAN Override, + IN EFI_DNS4_CACHE_ENTRY DnsCacheEntry + ) +{ + DNS4_CACHE *NewDnsCache; + DNS4_CACHE *Item; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + + NewDnsCache = NULL; + Item = NULL; + + // + // Search the database for the matching EFI_DNS_CACHE_ENTRY + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, Dns4CacheList) { + Item = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink); + if (StrCmp (DnsCacheEntry.HostName, Item->DnsCache.HostName) == 0 && \ + CompareMem (DnsCacheEntry.IpAddress, Item->DnsCache.IpAddress, sizeof (EFI_IPv4_ADDRESS)) == 0) { + // + // This is the Dns cache entry + // + if (DeleteFlag) { + // + // Delete matching DNS Cache entry + // + RemoveEntryList (&Item->AllCacheLink); + + FreePool (Item->DnsCache.HostName); + FreePool (Item->DnsCache.IpAddress); + FreePool (Item); + + return EFI_SUCCESS; + } else if (Override) { + // + // Update this one + // + Item->DnsCache.Timeout = DnsCacheEntry.Timeout; + + return EFI_SUCCESS; + }else { + return EFI_ACCESS_DENIED; + } + } + } + + // + // Add new one + // + NewDnsCache = AllocatePool (sizeof (DNS4_CACHE)); + if (NewDnsCache == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + InitializeListHead (&NewDnsCache->AllCacheLink); + + NewDnsCache->DnsCache.HostName = AllocatePool (StrSize (DnsCacheEntry.HostName)); + if (NewDnsCache->DnsCache.HostName == NULL) { + FreePool (NewDnsCache); + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (NewDnsCache->DnsCache.HostName, DnsCacheEntry.HostName, StrSize (DnsCacheEntry.HostName)); + + NewDnsCache->DnsCache.IpAddress = AllocatePool (sizeof (EFI_IPv4_ADDRESS)); + if (NewDnsCache->DnsCache.IpAddress == NULL) { + FreePool (NewDnsCache->DnsCache.HostName); + FreePool (NewDnsCache); + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (NewDnsCache->DnsCache.IpAddress, DnsCacheEntry.IpAddress, sizeof (EFI_IPv4_ADDRESS)); + + NewDnsCache->DnsCache.Timeout = DnsCacheEntry.Timeout; + + InsertTailList (Dns4CacheList, &NewDnsCache->AllCacheLink); + + return EFI_SUCCESS; +} + +/** + Update Dns6 cache to shared list of caches of all DNSv6 instances. + + @param Dns6CacheList All Dns6 cache list. + @param DeleteFlag If FALSE, this function is to add one entry to the DNS Cache. + If TRUE, this function will delete matching DNS Cache entry. + @param Override If TRUE, the matching DNS cache entry will be overwritten with the supplied parameter. + If FALSE, EFI_ACCESS_DENIED will be returned if the entry to be added is already exists. + @param DnsCacheEntry Entry Pointer to DNS Cache entry. + + @retval EFI_SUCCESS Update Dns6 cache successfully. + @retval Others Failed to update Dns6 cache. +**/ +EFI_STATUS +EFIAPI +UpdateDns6Cache ( + IN LIST_ENTRY *Dns6CacheList, + IN BOOLEAN DeleteFlag, + IN BOOLEAN Override, + IN EFI_DNS6_CACHE_ENTRY DnsCacheEntry + ) +{ + DNS6_CACHE *NewDnsCache; + DNS6_CACHE *Item; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + + NewDnsCache = NULL; + Item = NULL; + + // + // Search the database for the matching EFI_DNS_CACHE_ENTRY + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, Dns6CacheList) { + Item = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink); + if (StrCmp (DnsCacheEntry.HostName, Item->DnsCache.HostName) == 0 && \ + CompareMem (DnsCacheEntry.IpAddress, Item->DnsCache.IpAddress, sizeof (EFI_IPv6_ADDRESS)) == 0) { + // + // This is the Dns cache entry + // + if (DeleteFlag) { + // + // Delete matching DNS Cache entry + // + RemoveEntryList (&Item->AllCacheLink); + + FreePool (Item->DnsCache.HostName); + FreePool (Item->DnsCache.IpAddress); + FreePool (Item); + + return EFI_SUCCESS; + } else if (Override) { + // + // Update this one + // + Item->DnsCache.Timeout = DnsCacheEntry.Timeout; + + return EFI_SUCCESS; + }else { + return EFI_ACCESS_DENIED; + } + } + } + + // + // Add new one + // + NewDnsCache = AllocatePool (sizeof (DNS6_CACHE)); + if (NewDnsCache == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + InitializeListHead (&NewDnsCache->AllCacheLink); + + NewDnsCache->DnsCache.HostName = AllocatePool (StrSize (DnsCacheEntry.HostName)); + if (NewDnsCache->DnsCache.HostName == NULL) { + FreePool (NewDnsCache); + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (NewDnsCache->DnsCache.HostName, DnsCacheEntry.HostName, StrSize (DnsCacheEntry.HostName)); + + NewDnsCache->DnsCache.IpAddress = AllocatePool (sizeof (EFI_IPv6_ADDRESS)); + if (NewDnsCache->DnsCache.IpAddress == NULL) { + FreePool (NewDnsCache->DnsCache.HostName); + FreePool (NewDnsCache); + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (NewDnsCache->DnsCache.IpAddress, DnsCacheEntry.IpAddress, sizeof (EFI_IPv6_ADDRESS)); + + NewDnsCache->DnsCache.Timeout = DnsCacheEntry.Timeout; + + InsertTailList (Dns6CacheList, &NewDnsCache->AllCacheLink); + + return EFI_SUCCESS; +} + +/** + Add Dns4 ServerIp to common list of addresses of all configured DNSv4 server. + + @param Dns4ServerList Common list of addresses of all configured DNSv4 server. + @param ServerIp DNS server Ip. + + @retval EFI_SUCCESS Add Dns4 ServerIp to common list successfully. + @retval Others Failed to add Dns4 ServerIp to common list. + +**/ +EFI_STATUS +EFIAPI +AddDns4ServerIp ( + IN LIST_ENTRY *Dns4ServerList, + IN EFI_IPv4_ADDRESS ServerIp + ) +{ + DNS4_SERVER_IP *NewServerIp; + DNS4_SERVER_IP *Item; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + + NewServerIp = NULL; + Item = NULL; + + // + // Search the database for the matching ServerIp + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, Dns4ServerList) { + Item = NET_LIST_USER_STRUCT (Entry, DNS4_SERVER_IP, AllServerLink); + if (CompareMem (&Item->Dns4ServerIp, &ServerIp, sizeof (EFI_IPv4_ADDRESS)) == 0) { + // + // Already done. + // + return EFI_SUCCESS; + } + } + + // + // Add new one + // + NewServerIp = AllocatePool (sizeof (DNS4_SERVER_IP)); + if (NewServerIp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + InitializeListHead (&NewServerIp->AllServerLink); + + CopyMem (&NewServerIp->Dns4ServerIp, &ServerIp, sizeof (EFI_IPv4_ADDRESS)); + + InsertTailList (Dns4ServerList, &NewServerIp->AllServerLink); + + return EFI_SUCCESS; +} + +/** + Add Dns6 ServerIp to common list of addresses of all configured DNSv6 server. + + @param Dns6ServerList Common list of addresses of all configured DNSv6 server. + @param ServerIp DNS server Ip. + + @retval EFI_SUCCESS Add Dns6 ServerIp to common list successfully. + @retval Others Failed to add Dns6 ServerIp to common list. + +**/ +EFI_STATUS +EFIAPI +AddDns6ServerIp ( + IN LIST_ENTRY *Dns6ServerList, + IN EFI_IPv6_ADDRESS ServerIp + ) +{ + DNS6_SERVER_IP *NewServerIp; + DNS6_SERVER_IP *Item; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + + NewServerIp = NULL; + Item = NULL; + + // + // Search the database for the matching ServerIp + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, Dns6ServerList) { + Item = NET_LIST_USER_STRUCT (Entry, DNS6_SERVER_IP, AllServerLink); + if (CompareMem (&Item->Dns6ServerIp, &ServerIp, sizeof (EFI_IPv6_ADDRESS)) == 0) { + // + // Already done. + // + return EFI_SUCCESS; + } + } + + // + // Add new one + // + NewServerIp = AllocatePool (sizeof (DNS6_SERVER_IP)); + if (NewServerIp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + InitializeListHead (&NewServerIp->AllServerLink); + + CopyMem (&NewServerIp->Dns6ServerIp, &ServerIp, sizeof (EFI_IPv6_ADDRESS)); + + InsertTailList (Dns6ServerList, &NewServerIp->AllServerLink); + + return EFI_SUCCESS; +} + +/** + Find out whether the response is valid or invalid. + + @param TokensMap All DNS transmittal Tokens entry. + @param Identification Identification for queried packet. + @param Type Type for queried packet. + @param Class Class for queried packet. + @param Item Return corresponding Token entry. + + @retval TRUE The response is valid. + @retval FALSE The response is invalid. + +**/ +BOOLEAN +IsValidDnsResponse ( + IN NET_MAP *TokensMap, + IN UINT16 Identification, + IN UINT16 Type, + IN UINT16 Class, + OUT NET_MAP_ITEM **Item + ) +{ + LIST_ENTRY *Entry; + + NET_BUF *Packet; + UINT8 *TxString; + DNS_HEADER *DnsHeader; + CHAR8 *QueryName; + DNS_QUERY_SECTION *QuerySection; + + NET_LIST_FOR_EACH (Entry, &TokensMap->Used) { + *Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link); + Packet = (NET_BUF *) ((*Item)->Value); + if (Packet == NULL){ + + continue; + } else { + TxString = NetbufGetByte (Packet, 0, NULL); + ASSERT (TxString != NULL); + DnsHeader = (DNS_HEADER *) TxString; + QueryName = (CHAR8 *) (TxString + sizeof (*DnsHeader)); + QuerySection = (DNS_QUERY_SECTION *) (QueryName + AsciiStrLen (QueryName) + 1); + + if (NTOHS (DnsHeader->Identification) == Identification && + NTOHS (QuerySection->Type) == Type && + NTOHS (QuerySection->Class) == Class) { + return TRUE; + } + } + } + + *Item = NULL; + + return FALSE; +} + +/** + Parse Dns Response. + + @param Instance The DNS instance + @param RxString Received buffer. + @param Length Received buffer length. + @param Completed Flag to indicate that Dns response is valid. + + @retval EFI_SUCCESS Parse Dns Response successfully. + @retval Others Failed to parse Dns Response. + +**/ +EFI_STATUS +ParseDnsResponse ( + IN OUT DNS_INSTANCE *Instance, + IN UINT8 *RxString, + IN UINT32 Length, + OUT BOOLEAN *Completed + ) +{ + DNS_HEADER *DnsHeader; + + CHAR8 *QueryName; + UINT32 QueryNameLen; + DNS_QUERY_SECTION *QuerySection; + + CHAR8 *AnswerName; + DNS_ANSWER_SECTION *AnswerSection; + UINT8 *AnswerData; + + NET_MAP_ITEM *Item; + DNS4_TOKEN_ENTRY *Dns4TokenEntry; + DNS6_TOKEN_ENTRY *Dns6TokenEntry; + + UINT32 IpCount; + UINT32 RRCount; + UINT32 AnswerSectionNum; + UINT32 CNameTtl; + + EFI_IPv4_ADDRESS *HostAddr4; + EFI_IPv6_ADDRESS *HostAddr6; + + EFI_DNS4_CACHE_ENTRY *Dns4CacheEntry; + EFI_DNS6_CACHE_ENTRY *Dns6CacheEntry; + + DNS_RESOURCE_RECORD *Dns4RR; + DNS6_RESOURCE_RECORD *Dns6RR; + + EFI_STATUS Status; + UINT32 RemainingLength; + + EFI_TPL OldTpl; + + Item = NULL; + Dns4TokenEntry = NULL; + Dns6TokenEntry = NULL; + + IpCount = 0; + RRCount = 0; + AnswerSectionNum = 0; + CNameTtl = 0; + + HostAddr4 = NULL; + HostAddr6 = NULL; + + Dns4CacheEntry = NULL; + Dns6CacheEntry = NULL; + + Dns4RR = NULL; + Dns6RR = NULL; + + *Completed = TRUE; + Status = EFI_SUCCESS; + RemainingLength = Length; + + // + // Check whether the remaining packet length is avaiable or not. + // + if (RemainingLength <= sizeof (DNS_HEADER)) { + *Completed = FALSE; + return EFI_ABORTED; + } else { + RemainingLength -= sizeof (DNS_HEADER); + } + + // + // Get header + // + DnsHeader = (DNS_HEADER *) RxString; + + DnsHeader->Identification = NTOHS (DnsHeader->Identification); + DnsHeader->Flags.Uint16 = NTOHS (DnsHeader->Flags.Uint16); + DnsHeader->QuestionsNum = NTOHS (DnsHeader->QuestionsNum); + DnsHeader->AnswersNum = NTOHS (DnsHeader->AnswersNum); + DnsHeader->AuthorityNum = NTOHS (DnsHeader->AuthorityNum); + DnsHeader->AditionalNum = NTOHS (DnsHeader->AditionalNum); + + // + // There is always one QuestionsNum in DNS message. The capability to handle more + // than one requires to redesign the message format. Currently, it's not supported. + // + if (DnsHeader->QuestionsNum > 1) { + *Completed = FALSE; + return EFI_UNSUPPORTED; + } + + // + // Get Query name + // + QueryName = (CHAR8 *) (RxString + sizeof (*DnsHeader)); + + QueryNameLen = (UINT32) AsciiStrLen (QueryName) + 1; + + // + // Check whether the remaining packet length is avaiable or not. + // + if (RemainingLength <= QueryNameLen + sizeof (DNS_QUERY_SECTION)) { + *Completed = FALSE; + return EFI_ABORTED; + } else { + RemainingLength -= (QueryNameLen + sizeof (DNS_QUERY_SECTION)); + } + + // + // Get query section + // + QuerySection = (DNS_QUERY_SECTION *) (QueryName + QueryNameLen); + QuerySection->Type = NTOHS (QuerySection->Type); + QuerySection->Class = NTOHS (QuerySection->Class); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Check DnsResponse Validity, if so, also get a valid NET_MAP_ITEM. + // + if (Instance->Service->IpVersion == IP_VERSION_4) { + if (!IsValidDnsResponse ( + &Instance->Dns4TxTokens, + DnsHeader->Identification, + QuerySection->Type, + QuerySection->Class, + &Item + )) { + *Completed = FALSE; + Status = EFI_ABORTED; + goto ON_EXIT; + } + ASSERT (Item != NULL); + Dns4TokenEntry = (DNS4_TOKEN_ENTRY *) (Item->Key); + } else { + if (!IsValidDnsResponse ( + &Instance->Dns6TxTokens, + DnsHeader->Identification, + QuerySection->Type, + QuerySection->Class, + &Item + )) { + *Completed = FALSE; + Status = EFI_ABORTED; + goto ON_EXIT; + } + ASSERT (Item != NULL); + Dns6TokenEntry = (DNS6_TOKEN_ENTRY *) (Item->Key); + } + + // + // Continue Check Some Errors. + // + if (DnsHeader->Flags.Bits.RCode != DNS_FLAGS_RCODE_NO_ERROR || DnsHeader->AnswersNum < 1 || \ + DnsHeader->Flags.Bits.QR != DNS_FLAGS_QR_RESPONSE) { + // + // The domain name referenced in the query does not exist. + // + if (DnsHeader->Flags.Bits.RCode == DNS_FLAGS_RCODE_NAME_ERROR) { + Status = EFI_NOT_FOUND; + } else { + Status = EFI_DEVICE_ERROR; + } + + goto ON_COMPLETE; + } + + // + // Do some buffer allocations. + // + if (Instance->Service->IpVersion == IP_VERSION_4) { + ASSERT (Dns4TokenEntry != NULL); + + if (Dns4TokenEntry->GeneralLookUp) { + // + // It's the GeneralLookUp querying. + // + Dns4TokenEntry->Token->RspData.GLookupData = AllocateZeroPool (sizeof (DNS_RESOURCE_RECORD)); + if (Dns4TokenEntry->Token->RspData.GLookupData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + Dns4TokenEntry->Token->RspData.GLookupData->RRList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (DNS_RESOURCE_RECORD)); + if (Dns4TokenEntry->Token->RspData.GLookupData->RRList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + } else { + // + // It's not the GeneralLookUp querying. Check the Query type. + // + if (QuerySection->Type == DNS_TYPE_A) { + Dns4TokenEntry->Token->RspData.H2AData = AllocateZeroPool (sizeof (DNS_HOST_TO_ADDR_DATA)); + if (Dns4TokenEntry->Token->RspData.H2AData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + Dns4TokenEntry->Token->RspData.H2AData->IpList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (EFI_IPv4_ADDRESS)); + if (Dns4TokenEntry->Token->RspData.H2AData->IpList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + } else { + Status = EFI_UNSUPPORTED; + goto ON_EXIT; + } + } + } else { + ASSERT (Dns6TokenEntry != NULL); + + if (Dns6TokenEntry->GeneralLookUp) { + // + // It's the GeneralLookUp querying. + // + Dns6TokenEntry->Token->RspData.GLookupData = AllocateZeroPool (sizeof (DNS_RESOURCE_RECORD)); + if (Dns6TokenEntry->Token->RspData.GLookupData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + Dns6TokenEntry->Token->RspData.GLookupData->RRList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (DNS_RESOURCE_RECORD)); + if (Dns6TokenEntry->Token->RspData.GLookupData->RRList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + } else { + // + // It's not the GeneralLookUp querying. Check the Query type. + // + if (QuerySection->Type == DNS_TYPE_AAAA) { + Dns6TokenEntry->Token->RspData.H2AData = AllocateZeroPool (sizeof (DNS6_HOST_TO_ADDR_DATA)); + if (Dns6TokenEntry->Token->RspData.H2AData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + Dns6TokenEntry->Token->RspData.H2AData->IpList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (EFI_IPv6_ADDRESS)); + if (Dns6TokenEntry->Token->RspData.H2AData->IpList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + } else { + Status = EFI_UNSUPPORTED; + goto ON_EXIT; + } + } + } + + Status = EFI_NOT_FOUND; + + // + // Get Answer name + // + AnswerName = (CHAR8 *) QuerySection + sizeof (*QuerySection); + + // + // Processing AnswerSection. + // + while (AnswerSectionNum < DnsHeader->AnswersNum) { + // + // Check whether the remaining packet length is avaiable or not. + // + if (RemainingLength <= sizeof (UINT16) + sizeof (DNS_ANSWER_SECTION)) { + *Completed = FALSE; + Status = EFI_ABORTED; + goto ON_EXIT; + } else { + RemainingLength -= (sizeof (UINT16) + sizeof (DNS_ANSWER_SECTION)); + } + + // + // Answer name should be PTR, else EFI_UNSUPPORTED returned. + // + if ((*(UINT8 *) AnswerName & 0xC0) != 0xC0) { + Status = EFI_UNSUPPORTED; + goto ON_EXIT; + } + + // + // Get Answer section. + // + AnswerSection = (DNS_ANSWER_SECTION *) (AnswerName + sizeof (UINT16)); + AnswerSection->Type = NTOHS (AnswerSection->Type); + AnswerSection->Class = NTOHS (AnswerSection->Class); + AnswerSection->Ttl = NTOHL (AnswerSection->Ttl); + AnswerSection->DataLength = NTOHS (AnswerSection->DataLength); + + // + // Check whether the remaining packet length is avaiable or not. + // + if (RemainingLength < AnswerSection->DataLength) { + *Completed = FALSE; + Status = EFI_ABORTED; + goto ON_EXIT; + } else { + RemainingLength -= AnswerSection->DataLength; + } + + // + // Check whether it's the GeneralLookUp querying. + // + if (Instance->Service->IpVersion == IP_VERSION_4 && Dns4TokenEntry->GeneralLookUp) { + Dns4RR = Dns4TokenEntry->Token->RspData.GLookupData->RRList; + AnswerData = (UINT8 *) AnswerSection + sizeof (*AnswerSection); + + // + // Fill the ResourceRecord. + // + Dns4RR[RRCount].QName = AllocateZeroPool (AsciiStrLen (QueryName) + 1); + if (Dns4RR[RRCount].QName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + CopyMem (Dns4RR[RRCount].QName, QueryName, AsciiStrLen (QueryName)); + Dns4RR[RRCount].QType = AnswerSection->Type; + Dns4RR[RRCount].QClass = AnswerSection->Class; + Dns4RR[RRCount].TTL = AnswerSection->Ttl; + Dns4RR[RRCount].DataLength = AnswerSection->DataLength; + Dns4RR[RRCount].RData = AllocateZeroPool (Dns4RR[RRCount].DataLength); + if (Dns4RR[RRCount].RData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + CopyMem (Dns4RR[RRCount].RData, AnswerData, Dns4RR[RRCount].DataLength); + + RRCount ++; + Status = EFI_SUCCESS; + } else if (Instance->Service->IpVersion == IP_VERSION_6 && Dns6TokenEntry->GeneralLookUp) { + Dns6RR = Dns6TokenEntry->Token->RspData.GLookupData->RRList; + AnswerData = (UINT8 *) AnswerSection + sizeof (*AnswerSection); + + // + // Fill the ResourceRecord. + // + Dns6RR[RRCount].QName = AllocateZeroPool (AsciiStrLen (QueryName) + 1); + if (Dns6RR[RRCount].QName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + CopyMem (Dns6RR[RRCount].QName, QueryName, AsciiStrLen (QueryName)); + Dns6RR[RRCount].QType = AnswerSection->Type; + Dns6RR[RRCount].QClass = AnswerSection->Class; + Dns6RR[RRCount].TTL = AnswerSection->Ttl; + Dns6RR[RRCount].DataLength = AnswerSection->DataLength; + Dns6RR[RRCount].RData = AllocateZeroPool (Dns6RR[RRCount].DataLength); + if (Dns6RR[RRCount].RData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + CopyMem (Dns6RR[RRCount].RData, AnswerData, Dns6RR[RRCount].DataLength); + + RRCount ++; + Status = EFI_SUCCESS; + } else { + // + // It's not the GeneralLookUp querying. + // Check the Query type, parse the response packet. + // + switch (AnswerSection->Type) { + case DNS_TYPE_A: + // + // This is address entry, get Data. + // + ASSERT (Dns4TokenEntry != NULL); + + if (AnswerSection->DataLength != 4) { + Status = EFI_ABORTED; + goto ON_EXIT; + } + + HostAddr4 = Dns4TokenEntry->Token->RspData.H2AData->IpList; + AnswerData = (UINT8 *) AnswerSection + sizeof (*AnswerSection); + CopyMem (&HostAddr4[IpCount], AnswerData, sizeof (EFI_IPv4_ADDRESS)); + + // + // Allocate new CacheEntry pool to update DNS cache dynamically. + // + Dns4CacheEntry = AllocateZeroPool (sizeof (EFI_DNS4_CACHE_ENTRY)); + if (Dns4CacheEntry == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + Dns4CacheEntry->HostName = AllocateZeroPool (2 * (StrLen(Dns4TokenEntry->QueryHostName) + 1)); + if (Dns4CacheEntry->HostName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + CopyMem (Dns4CacheEntry->HostName, Dns4TokenEntry->QueryHostName, 2 * (StrLen(Dns4TokenEntry->QueryHostName) + 1)); + Dns4CacheEntry->IpAddress = AllocateZeroPool (sizeof (EFI_IPv4_ADDRESS)); + if (Dns4CacheEntry->IpAddress == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + CopyMem (Dns4CacheEntry->IpAddress, AnswerData, sizeof (EFI_IPv4_ADDRESS)); + + if (CNameTtl != 0 && AnswerSection->Ttl != 0) { + Dns4CacheEntry->Timeout = MIN (CNameTtl, AnswerSection->Ttl); + } else { + Dns4CacheEntry->Timeout = MAX (CNameTtl, AnswerSection->Ttl); + } + + UpdateDns4Cache (&mDriverData->Dns4CacheList, FALSE, TRUE, *Dns4CacheEntry); + + // + // Free allocated CacheEntry pool. + // + FreePool (Dns4CacheEntry->HostName); + Dns4CacheEntry->HostName = NULL; + + FreePool (Dns4CacheEntry->IpAddress); + Dns4CacheEntry->IpAddress = NULL; + + FreePool (Dns4CacheEntry); + Dns4CacheEntry = NULL; + + IpCount ++; + Status = EFI_SUCCESS; + break; + case DNS_TYPE_AAAA: + // + // This is address entry, get Data. + // + ASSERT (Dns6TokenEntry != NULL); + + if (AnswerSection->DataLength != 16) { + Status = EFI_ABORTED; + goto ON_EXIT; + } + + HostAddr6 = Dns6TokenEntry->Token->RspData.H2AData->IpList; + AnswerData = (UINT8 *) AnswerSection + sizeof (*AnswerSection); + CopyMem (&HostAddr6[IpCount], AnswerData, sizeof (EFI_IPv6_ADDRESS)); + + // + // Allocate new CacheEntry pool to update DNS cache dynamically. + // + Dns6CacheEntry = AllocateZeroPool (sizeof (EFI_DNS6_CACHE_ENTRY)); + if (Dns6CacheEntry == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + Dns6CacheEntry->HostName = AllocateZeroPool (2 * (StrLen(Dns6TokenEntry->QueryHostName) + 1)); + if (Dns6CacheEntry->HostName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + CopyMem (Dns6CacheEntry->HostName, Dns6TokenEntry->QueryHostName, 2 * (StrLen(Dns6TokenEntry->QueryHostName) + 1)); + Dns6CacheEntry->IpAddress = AllocateZeroPool (sizeof (EFI_IPv6_ADDRESS)); + if (Dns6CacheEntry->IpAddress == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + CopyMem (Dns6CacheEntry->IpAddress, AnswerData, sizeof (EFI_IPv6_ADDRESS)); + + if (CNameTtl != 0 && AnswerSection->Ttl != 0) { + Dns6CacheEntry->Timeout = MIN (CNameTtl, AnswerSection->Ttl); + } else { + Dns6CacheEntry->Timeout = MAX (CNameTtl, AnswerSection->Ttl); + } + + UpdateDns6Cache (&mDriverData->Dns6CacheList, FALSE, TRUE, *Dns6CacheEntry); + + // + // Free allocated CacheEntry pool. + // + FreePool (Dns6CacheEntry->HostName); + Dns6CacheEntry->HostName = NULL; + + FreePool (Dns6CacheEntry->IpAddress); + Dns6CacheEntry->IpAddress = NULL; + + FreePool (Dns6CacheEntry); + Dns6CacheEntry = NULL; + + IpCount ++; + Status = EFI_SUCCESS; + break; + case DNS_TYPE_CNAME: + // + // According RFC 1034 - 3.6.2, if the query name is an alias, the name server will include the CNAME + // record in the response and restart the query at the domain name specified in the data field of the + // CNAME record. So, just record the TTL value of the CNAME, then skip to parse the next record. + // + CNameTtl = AnswerSection->Ttl; + break; + default: + Status = EFI_UNSUPPORTED; + goto ON_EXIT; + } + } + + // + // Find next one + // + AnswerName = (CHAR8 *) AnswerSection + sizeof (*AnswerSection) + AnswerSection->DataLength; + AnswerSectionNum ++; + } + + if (Instance->Service->IpVersion == IP_VERSION_4) { + ASSERT (Dns4TokenEntry != NULL); + + if (Dns4TokenEntry->GeneralLookUp) { + Dns4TokenEntry->Token->RspData.GLookupData->RRCount = RRCount; + } else { + if (QuerySection->Type == DNS_TYPE_A) { + Dns4TokenEntry->Token->RspData.H2AData->IpCount = IpCount; + } else { + Status = EFI_UNSUPPORTED; + goto ON_EXIT; + } + } + } else { + ASSERT (Dns6TokenEntry != NULL); + + if (Dns6TokenEntry->GeneralLookUp) { + Dns6TokenEntry->Token->RspData.GLookupData->RRCount = RRCount; + } else { + if (QuerySection->Type == DNS_TYPE_AAAA) { + Dns6TokenEntry->Token->RspData.H2AData->IpCount = IpCount; + } else { + Status = EFI_UNSUPPORTED; + goto ON_EXIT; + } + } + } + +ON_COMPLETE: + // + // Parsing is complete, free the sending packet and signal Event here. + // + if (Item != NULL && Item->Value != NULL) { + NetbufFree ((NET_BUF *) (Item->Value)); + } + + if (Instance->Service->IpVersion == IP_VERSION_4) { + ASSERT (Dns4TokenEntry != NULL); + Dns4RemoveTokenEntry (&Instance->Dns4TxTokens, Dns4TokenEntry); + Dns4TokenEntry->Token->Status = Status; + if (Dns4TokenEntry->Token->Event != NULL) { + gBS->SignalEvent (Dns4TokenEntry->Token->Event); + DispatchDpc (); + } + } else { + ASSERT (Dns6TokenEntry != NULL); + Dns6RemoveTokenEntry (&Instance->Dns6TxTokens, Dns6TokenEntry); + Dns6TokenEntry->Token->Status = Status; + if (Dns6TokenEntry->Token->Event != NULL) { + gBS->SignalEvent (Dns6TokenEntry->Token->Event); + DispatchDpc (); + } + } + +ON_EXIT: + // + // Free the allocated buffer if error happen. + // + if (EFI_ERROR (Status)) { + if (Dns4TokenEntry != NULL) { + if (Dns4TokenEntry->GeneralLookUp) { + if (Dns4TokenEntry->Token->RspData.GLookupData != NULL) { + if (Dns4TokenEntry->Token->RspData.GLookupData->RRList != NULL) { + while (RRCount != 0) { + RRCount --; + if (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName != NULL) { + FreePool (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName); + } + + if (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData != NULL) { + FreePool (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData); + } + } + + FreePool (Dns4TokenEntry->Token->RspData.GLookupData->RRList); + } + + FreePool (Dns4TokenEntry->Token->RspData.GLookupData); + } + } else { + if (QuerySection->Type == DNS_TYPE_A && Dns4TokenEntry->Token->RspData.H2AData != NULL) { + if (Dns4TokenEntry->Token->RspData.H2AData->IpList != NULL) { + FreePool (Dns4TokenEntry->Token->RspData.H2AData->IpList); + } + + FreePool (Dns4TokenEntry->Token->RspData.H2AData); + } + } + } + + if (Dns6TokenEntry != NULL) { + if (Dns6TokenEntry->GeneralLookUp) { + if (Dns6TokenEntry->Token->RspData.GLookupData != NULL) { + if (Dns6TokenEntry->Token->RspData.GLookupData->RRList != NULL) { + while (RRCount != 0) { + RRCount --; + if (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName != NULL) { + FreePool (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName); + } + + if (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData != NULL) { + FreePool (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData); + } + } + + FreePool (Dns6TokenEntry->Token->RspData.GLookupData->RRList); + } + + FreePool (Dns6TokenEntry->Token->RspData.GLookupData); + } + } else { + if (QuerySection->Type == DNS_TYPE_AAAA && Dns6TokenEntry->Token->RspData.H2AData != NULL) { + if (Dns6TokenEntry->Token->RspData.H2AData->IpList != NULL) { + FreePool (Dns6TokenEntry->Token->RspData.H2AData->IpList); + } + + FreePool (Dns6TokenEntry->Token->RspData.H2AData); + } + } + } + + if (Dns4CacheEntry != NULL) { + if (Dns4CacheEntry->HostName != NULL) { + FreePool (Dns4CacheEntry->HostName); + } + + if (Dns4CacheEntry->IpAddress != NULL) { + FreePool (Dns4CacheEntry->IpAddress); + } + + FreePool (Dns4CacheEntry); + } + + if (Dns6CacheEntry != NULL) { + if (Dns6CacheEntry->HostName != NULL) { + FreePool (Dns6CacheEntry->HostName); + } + + if (Dns6CacheEntry->IpAddress != NULL) { + FreePool (Dns6CacheEntry->IpAddress); + } + + FreePool (Dns6CacheEntry); + } + } + + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Parse response packet. + + @param Packet The packets received. + @param EndPoint The local/remote UDP access point + @param IoStatus The status of the UDP receive + @param Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +DnsOnPacketReceived ( + NET_BUF *Packet, + UDP_END_POINT *EndPoint, + EFI_STATUS IoStatus, + VOID *Context + ) +{ + DNS_INSTANCE *Instance; + + UINT8 *RcvString; + UINT32 Len; + + BOOLEAN Completed; + + Instance = (DNS_INSTANCE *) Context; + NET_CHECK_SIGNATURE (Instance, DNS_INSTANCE_SIGNATURE); + + RcvString = NULL; + Completed = FALSE; + + if (EFI_ERROR (IoStatus)) { + goto ON_EXIT; + } + + ASSERT (Packet != NULL); + + Len = Packet->TotalSize; + + RcvString = NetbufGetByte (Packet, 0, NULL); + ASSERT (RcvString != NULL); + + // + // Parse Dns Response + // + ParseDnsResponse (Instance, RcvString, Len, &Completed); + +ON_EXIT: + + if (Packet != NULL) { + NetbufFree (Packet); + } + + if (!Completed) { + UdpIoRecvDatagram (Instance->UdpIo, DnsOnPacketReceived, Instance, 0); + } +} + +/** + Release the net buffer when packet is sent. + + @param Packet The packets received. + @param EndPoint The local/remote UDP access point + @param IoStatus The status of the UDP receive + @param Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +DnsOnPacketSent ( + NET_BUF *Packet, + UDP_END_POINT *EndPoint, + EFI_STATUS IoStatus, + VOID *Context + ) +{ + DNS_INSTANCE *Instance; + LIST_ENTRY *Entry; + NET_MAP_ITEM *Item; + DNS4_TOKEN_ENTRY *Dns4TokenEntry; + DNS6_TOKEN_ENTRY *Dns6TokenEntry; + + Dns4TokenEntry = NULL; + Dns6TokenEntry = NULL; + + Instance = (DNS_INSTANCE *) Context; + NET_CHECK_SIGNATURE (Instance, DNS_INSTANCE_SIGNATURE); + + if (Instance->Service->IpVersion == IP_VERSION_4) { + NET_LIST_FOR_EACH (Entry, &Instance->Dns4TxTokens.Used) { + Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link); + if (Packet == (NET_BUF *)(Item->Value)) { + Dns4TokenEntry = ((DNS4_TOKEN_ENTRY *)Item->Key); + Dns4TokenEntry->PacketToLive = Dns4TokenEntry->Token->RetryInterval; + break; + } + } + } else { + NET_LIST_FOR_EACH (Entry, &Instance->Dns6TxTokens.Used) { + Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link); + if (Packet == (NET_BUF *)(Item->Value)) { + Dns6TokenEntry = ((DNS6_TOKEN_ENTRY *)Item->Key); + Dns6TokenEntry->PacketToLive = Dns6TokenEntry->Token->RetryInterval; + break; + } + } + } + + NetbufFree (Packet); +} + +/** + Query request information. + + @param Instance The DNS instance + @param Packet The packet for querying request information. + + @retval EFI_SUCCESS Query request information successfully. + @retval Others Failed to query request information. + +**/ +EFI_STATUS +DoDnsQuery ( + IN DNS_INSTANCE *Instance, + IN NET_BUF *Packet + ) +{ + EFI_STATUS Status; + + // + // Ready to receive the DNS response. + // + if (Instance->UdpIo->RecvRequest == NULL) { + Status = UdpIoRecvDatagram (Instance->UdpIo, DnsOnPacketReceived, Instance, 0); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Transmit the DNS packet. + // + NET_GET_REF (Packet); + + Status = UdpIoSendDatagram (Instance->UdpIo, Packet, NULL, NULL, DnsOnPacketSent, Instance); + + return Status; +} + +/** + Construct the Packet according query section. + + @param Instance The DNS instance + @param QueryName Queried Name + @param Type Queried Type + @param Class Queried Class + @param Packet The packet for query + + @retval EFI_SUCCESS The packet is constructed. + @retval Others Failed to construct the Packet. + +**/ +EFI_STATUS +ConstructDNSQuery ( + IN DNS_INSTANCE *Instance, + IN CHAR8 *QueryName, + IN UINT16 Type, + IN UINT16 Class, + OUT NET_BUF **Packet + ) +{ + NET_FRAGMENT Frag; + DNS_HEADER *DnsHeader; + DNS_QUERY_SECTION *DnsQuery; + + // + // Messages carried by UDP are restricted to 512 bytes (not counting the IP + // or UDP headers). + // + Frag.Bulk = AllocatePool (DNS_MAX_MESSAGE_SIZE * sizeof (UINT8)); + if (Frag.Bulk == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Fill header + // + DnsHeader = (DNS_HEADER *) Frag.Bulk; + DnsHeader->Identification = (UINT16)NET_RANDOM (NetRandomInitSeed()); + DnsHeader->Flags.Uint16 = 0x0000; + DnsHeader->Flags.Bits.RD = 1; + DnsHeader->Flags.Bits.OpCode = DNS_FLAGS_OPCODE_STANDARD; + DnsHeader->Flags.Bits.QR = DNS_FLAGS_QR_QUERY; + DnsHeader->QuestionsNum = 1; + DnsHeader->AnswersNum = 0; + DnsHeader->AuthorityNum = 0; + DnsHeader->AditionalNum = 0; + + DnsHeader->Identification = HTONS (DnsHeader->Identification); + DnsHeader->Flags.Uint16 = HTONS (DnsHeader->Flags.Uint16); + DnsHeader->QuestionsNum = HTONS (DnsHeader->QuestionsNum); + DnsHeader->AnswersNum = HTONS (DnsHeader->AnswersNum); + DnsHeader->AuthorityNum = HTONS (DnsHeader->AuthorityNum); + DnsHeader->AditionalNum = HTONS (DnsHeader->AditionalNum); + + Frag.Len = sizeof (*DnsHeader); + + // + // Fill Query name + // + CopyMem (Frag.Bulk + Frag.Len, QueryName, AsciiStrLen (QueryName)); + Frag.Len = (UINT32) (Frag.Len + AsciiStrLen (QueryName)); + *(Frag.Bulk + Frag.Len) = 0; + Frag.Len ++; + + // + // Rest query section + // + DnsQuery = (DNS_QUERY_SECTION *) (Frag.Bulk + Frag.Len); + + DnsQuery->Type = HTONS (Type); + DnsQuery->Class = HTONS (Class); + + Frag.Len += sizeof (*DnsQuery); + + // + // Wrap the Frag in a net buffer. + // + *Packet = NetbufFromExt (&Frag, 1, 0, 0, DnsDummyExtFree, NULL); + if (*Packet == NULL) { + FreePool (Frag.Bulk); + return EFI_OUT_OF_RESOURCES; + } + + // + // Store the UdpIo in ProtoData. + // + *((UINTN *) &((*Packet)->ProtoData[0])) = (UINTN) (Instance->UdpIo); + + return EFI_SUCCESS; +} + +/** + Retransmit the packet. + + @param Instance The DNS instance + @param Packet Retransmit the packet + + @retval EFI_SUCCESS The packet is retransmitted. + @retval Others Failed to retransmit. + +**/ +EFI_STATUS +DnsRetransmit ( + IN DNS_INSTANCE *Instance, + IN NET_BUF *Packet + ) +{ + EFI_STATUS Status; + + UINT8 *Buffer; + + ASSERT (Packet != NULL); + + // + // Set the requests to the listening port, other packets to the connected port + // + Buffer = NetbufGetByte (Packet, 0, NULL); + ASSERT (Buffer != NULL); + + NET_GET_REF (Packet); + + Status = UdpIoSendDatagram ( + Instance->UdpIo, + Packet, + NULL, + NULL, + DnsOnPacketSent, + Instance + ); + + if (EFI_ERROR (Status)) { + NET_PUT_REF (Packet); + } + + return Status; +} + +/** + The timer ticking function for the DNS services. + + @param Event The ticking event + @param Context The DNS service instance + +**/ +VOID +EFIAPI +DnsOnTimerRetransmit ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + DNS_SERVICE *Service; + + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + + DNS_INSTANCE *Instance; + LIST_ENTRY *EntryNetMap; + NET_MAP_ITEM *ItemNetMap; + DNS4_TOKEN_ENTRY *Dns4TokenEntry; + DNS6_TOKEN_ENTRY *Dns6TokenEntry; + + Dns4TokenEntry = NULL; + Dns6TokenEntry = NULL; + + Service = (DNS_SERVICE *) Context; + + + if (Service->IpVersion == IP_VERSION_4) { + // + // Iterate through all the children of the DNS service instance. Time + // out the packet. If maximum retries reached, clean the Token up. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Service->Dns4ChildrenList) { + Instance = NET_LIST_USER_STRUCT (Entry, DNS_INSTANCE, Link); + + EntryNetMap = Instance->Dns4TxTokens.Used.ForwardLink; + while (EntryNetMap != &Instance->Dns4TxTokens.Used) { + ItemNetMap = NET_LIST_USER_STRUCT (EntryNetMap, NET_MAP_ITEM, Link); + Dns4TokenEntry = (DNS4_TOKEN_ENTRY *)(ItemNetMap->Key); + if (Dns4TokenEntry->PacketToLive == 0 || (--Dns4TokenEntry->PacketToLive > 0)) { + EntryNetMap = EntryNetMap->ForwardLink; + continue; + } + + // + // Retransmit the packet if haven't reach the maxmium retry count, + // otherwise exit the transfer. + // + if (++Dns4TokenEntry->RetryCounting <= Dns4TokenEntry->Token->RetryCount) { + DnsRetransmit (Instance, (NET_BUF *)ItemNetMap->Value); + EntryNetMap = EntryNetMap->ForwardLink; + } else { + // + // Maximum retries reached, clean the Token up. + // + Dns4RemoveTokenEntry (&Instance->Dns4TxTokens, Dns4TokenEntry); + Dns4TokenEntry->Token->Status = EFI_TIMEOUT; + gBS->SignalEvent (Dns4TokenEntry->Token->Event); + DispatchDpc (); + + // + // Free the sending packet. + // + if (ItemNetMap->Value != NULL) { + NetbufFree ((NET_BUF *)(ItemNetMap->Value)); + } + + EntryNetMap = Instance->Dns4TxTokens.Used.ForwardLink; + } + } + } + }else { + // + // Iterate through all the children of the DNS service instance. Time + // out the packet. If maximum retries reached, clean the Token up. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Service->Dns6ChildrenList) { + Instance = NET_LIST_USER_STRUCT (Entry, DNS_INSTANCE, Link); + + EntryNetMap = Instance->Dns6TxTokens.Used.ForwardLink; + while (EntryNetMap != &Instance->Dns6TxTokens.Used) { + ItemNetMap = NET_LIST_USER_STRUCT (EntryNetMap, NET_MAP_ITEM, Link); + Dns6TokenEntry = (DNS6_TOKEN_ENTRY *) (ItemNetMap->Key); + if (Dns6TokenEntry->PacketToLive == 0 || (--Dns6TokenEntry->PacketToLive > 0)) { + EntryNetMap = EntryNetMap->ForwardLink; + continue; + } + + // + // Retransmit the packet if haven't reach the maxmium retry count, + // otherwise exit the transfer. + // + if (++Dns6TokenEntry->RetryCounting <= Dns6TokenEntry->Token->RetryCount) { + DnsRetransmit (Instance, (NET_BUF *) ItemNetMap->Value); + EntryNetMap = EntryNetMap->ForwardLink; + } else { + // + // Maximum retries reached, clean the Token up. + // + Dns6RemoveTokenEntry (&Instance->Dns6TxTokens, Dns6TokenEntry); + Dns6TokenEntry->Token->Status = EFI_TIMEOUT; + gBS->SignalEvent (Dns6TokenEntry->Token->Event); + DispatchDpc (); + + // + // Free the sending packet. + // + if (ItemNetMap->Value != NULL) { + NetbufFree ((NET_BUF *) (ItemNetMap->Value)); + } + + EntryNetMap = Instance->Dns6TxTokens.Used.ForwardLink; + } + } + } + } +} + +/** + The timer ticking function for the DNS driver. + + @param Event The ticking event + @param Context NULL + +**/ +VOID +EFIAPI +DnsOnTimerUpdate ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + DNS4_CACHE *Item4; + DNS6_CACHE *Item6; + + Item4 = NULL; + Item6 = NULL; + + // + // Iterate through all the DNS4 cache list. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns4CacheList) { + Item4 = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink); + Item4->DnsCache.Timeout--; + } + + Entry = mDriverData->Dns4CacheList.ForwardLink; + while (Entry != &mDriverData->Dns4CacheList) { + Item4 = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink); + if (Item4->DnsCache.Timeout == 0) { + RemoveEntryList (&Item4->AllCacheLink); + FreePool (Item4->DnsCache.HostName); + FreePool (Item4->DnsCache.IpAddress); + FreePool (Item4); + Entry = mDriverData->Dns4CacheList.ForwardLink; + } else { + Entry = Entry->ForwardLink; + } + } + + // + // Iterate through all the DNS6 cache list. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns6CacheList) { + Item6 = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink); + Item6->DnsCache.Timeout--; + } + + Entry = mDriverData->Dns6CacheList.ForwardLink; + while (Entry != &mDriverData->Dns6CacheList) { + Item6 = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink); + if (Item6->DnsCache.Timeout == 0) { + RemoveEntryList (&Item6->AllCacheLink); + FreePool (Item6->DnsCache.HostName); + FreePool (Item6->DnsCache.IpAddress); + FreePool (Item6); + Entry = mDriverData->Dns6CacheList.ForwardLink; + } else { + Entry = Entry->ForwardLink; + } + } +} + diff --git a/NetworkPkg/DnsDxe/DnsImpl.h b/NetworkPkg/DnsDxe/DnsImpl.h new file mode 100644 index 000000000..fdab9c75e --- /dev/null +++ b/NetworkPkg/DnsDxe/DnsImpl.h @@ -0,0 +1,1209 @@ +/** @file +DnsDxe support functions implementation. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_DNS_IMPL_H_ +#define __EFI_DNS_IMPL_H_ + +#include + +// +// Libraries classes +// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +// UEFI Driver Model Protocols +// +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include + +#include "DnsDriver.h" +#include "DnsDhcp.h" + +// +// Driver Version +// +#define DNS_VERSION 0x00000000 + +// +// Protocol instances +// +extern EFI_COMPONENT_NAME_PROTOCOL gDnsComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gDnsComponentName2; +extern EFI_UNICODE_STRING_TABLE *gDnsControllerNameTable; + +extern EFI_DRIVER_BINDING_PROTOCOL gDns4DriverBinding; +extern EFI_SERVICE_BINDING_PROTOCOL mDns4ServiceBinding; +extern EFI_DNS4_PROTOCOL mDns4Protocol; + +extern EFI_DRIVER_BINDING_PROTOCOL gDns6DriverBinding; +extern EFI_SERVICE_BINDING_PROTOCOL mDns6ServiceBinding; +extern EFI_DNS6_PROTOCOL mDns6Protocol; + +// +// DNS related +// +#define DNS_SERVER_PORT 53 + +#define DNS_PROTOCOL_UDP EFI_IP_PROTO_UDP +#define DNS_PROTOCOL_TCP EFI_IP_PROTO_TCP + +#define DNS_STATE_UNCONFIGED 0 +#define DNS_STATE_CONFIGED 1 +#define DNS_STATE_DESTROY 2 + +#define DNS_DEFAULT_TIMEOUT 2 + +#define DNS_TIME_TO_GETMAP 5 + +#pragma pack(1) + +typedef union _DNS_FLAGS DNS_FLAGS; + +typedef struct { + LIST_ENTRY AllCacheLink; + EFI_DNS4_CACHE_ENTRY DnsCache; +} DNS4_CACHE; + +typedef struct { + LIST_ENTRY AllCacheLink; + EFI_DNS6_CACHE_ENTRY DnsCache; +} DNS6_CACHE; + +typedef struct { + LIST_ENTRY AllServerLink; + EFI_IPv4_ADDRESS Dns4ServerIp; +} DNS4_SERVER_IP; + +typedef struct { + LIST_ENTRY AllServerLink; + EFI_IPv6_ADDRESS Dns6ServerIp; +} DNS6_SERVER_IP; + +typedef struct { + UINT32 RetryCounting; + UINT32 PacketToLive; + CHAR16 *QueryHostName; + EFI_IPv4_ADDRESS QueryIpAddress; + BOOLEAN GeneralLookUp; + EFI_DNS4_COMPLETION_TOKEN *Token; +} DNS4_TOKEN_ENTRY; + +typedef struct { + UINT32 RetryCounting; + UINT32 PacketToLive; + CHAR16 *QueryHostName; + EFI_IPv6_ADDRESS QueryIpAddress; + BOOLEAN GeneralLookUp; + EFI_DNS6_COMPLETION_TOKEN *Token; +} DNS6_TOKEN_ENTRY; + +union _DNS_FLAGS{ + struct { + UINT16 RCode:4; + UINT16 Zero:3; + UINT16 RA:1; + UINT16 RD:1; + UINT16 TC:1; + UINT16 AA:1; + UINT16 OpCode:4; + UINT16 QR:1; + } Bits; + UINT16 Uint16; +}; + +#define DNS_FLAGS_QR_QUERY 0 +#define DNS_FLAGS_QR_RESPONSE 1 + +#define DNS_FLAGS_OPCODE_STANDARD 0 +#define DNS_FLAGS_OPCODE_INVERSE 1 +#define DNS_FLAGS_OPCODE_SERVER_STATE 2 + +#define DNS_FLAGS_RCODE_NO_ERROR 0 +#define DNS_FLAGS_RCODE_NAME_ERROR 3 + +typedef struct { + UINT16 Identification; + DNS_FLAGS Flags; + UINT16 QuestionsNum; + UINT16 AnswersNum; + UINT16 AuthorityNum; + UINT16 AditionalNum; +} DNS_HEADER; + +typedef struct { + UINT16 Type; + UINT16 Class; +} DNS_QUERY_SECTION; + +typedef struct { + UINT16 Type; + UINT16 Class; + UINT32 Ttl; + UINT16 DataLength; +} DNS_ANSWER_SECTION; + +#define DNS4_DOMAIN L"in-addr.arpa" +#define DNS6_DOMAIN L"IP6.ARPA" + + +#pragma pack() + +/** + Remove TokenEntry from TokenMap. + + @param[in] TokenMap All DNSv4 Token entrys. + @param[in] TokenEntry TokenEntry need to be removed. + + @retval EFI_SUCCESS Remove TokenEntry from TokenMap sucessfully. + @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap. + +**/ +EFI_STATUS +Dns4RemoveTokenEntry ( + IN NET_MAP *TokenMap, + IN DNS4_TOKEN_ENTRY *TokenEntry + ); + +/** + Remove TokenEntry from TokenMap. + + @param[in] TokenMap All DNSv6 Token entrys. + @param[in] TokenEntry TokenEntry need to be removed. + + @retval EFI_SUCCESS Remove TokenEntry from TokenMap sucessfully. + @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap. + +**/ +EFI_STATUS +Dns6RemoveTokenEntry ( + IN NET_MAP *TokenMap, + IN DNS6_TOKEN_ENTRY *TokenEntry + ); + +/** + This function cancle the token specified by Arg in the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the token to be cancelled. If NULL, all + the tokens in this Map will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token + is not the same as that in the Item, if Arg is not + NULL. + @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is + cancelled. + +**/ +EFI_STATUS +EFIAPI +Dns4CancelTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ); + +/** + This function cancle the token specified by Arg in the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the token to be cancelled. If NULL, all + the tokens in this Map will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token + is not the same as that in the Item, if Arg is not + NULL. + @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is + cancelled. + +**/ +EFI_STATUS +EFIAPI +Dns6CancelTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ); + +/** + Get the TokenEntry from the TokensMap. + + @param[in] TokensMap All DNSv4 Token entrys + @param[in] Token Pointer to the token to be get. + @param[out] TokenEntry Pointer to TokenEntry corresponding Token. + + @retval EFI_SUCCESS Get the TokenEntry from the TokensMap sucessfully. + @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap. + +**/ +EFI_STATUS +EFIAPI +GetDns4TokenEntry ( + IN NET_MAP *TokensMap, + IN EFI_DNS4_COMPLETION_TOKEN *Token, + OUT DNS4_TOKEN_ENTRY **TokenEntry + ); + +/** + Get the TokenEntry from the TokensMap. + + @param[in] TokensMap All DNSv6 Token entrys + @param[in] Token Pointer to the token to be get. + @param[out] TokenEntry Pointer to TokenEntry corresponding Token. + + @retval EFI_SUCCESS Get the TokenEntry from the TokensMap sucessfully. + @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap. + +**/ +EFI_STATUS +EFIAPI +GetDns6TokenEntry ( + IN NET_MAP *TokensMap, + IN EFI_DNS6_COMPLETION_TOKEN *Token, + OUT DNS6_TOKEN_ENTRY **TokenEntry + ); + +/** + Cancel DNS4 tokens from the DNS4 instance. + + @param[in] Instance Pointer to the DNS instance context data. + @param[in] Token Pointer to the token to be canceled. If NULL, all + tokens in this instance will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The Token is cancelled. + @retval EFI_NOT_FOUND The Token is not found. + +**/ +EFI_STATUS +Dns4InstanceCancelToken ( + IN DNS_INSTANCE *Instance, + IN EFI_DNS4_COMPLETION_TOKEN *Token + ); + +/** + Cancel DNS6 tokens from the DNS6 instance. + + @param[in] Instance Pointer to the DNS instance context data. + @param[in] Token Pointer to the token to be canceled. If NULL, all + tokens in this instance will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The Token is cancelled. + @retval EFI_NOT_FOUND The Token is not found. + +**/ +EFI_STATUS +Dns6InstanceCancelToken ( + IN DNS_INSTANCE *Instance, + IN EFI_DNS6_COMPLETION_TOKEN *Token + ); + +/** + Free the resource related to the configure parameters. + + @param Config The DNS configure data + +**/ +VOID +Dns4CleanConfigure ( + IN OUT EFI_DNS4_CONFIG_DATA *Config + ); + +/** + Free the resource related to the configure parameters. + + @param Config The DNS configure data + +**/ +VOID +Dns6CleanConfigure ( + IN OUT EFI_DNS6_CONFIG_DATA *Config + ); + +/** + Allocate memory for configure parameter such as timeout value for Dst, + then copy the configure parameter from Src to Dst. + + @param[out] Dst The destination DHCP configure data. + @param[in] Src The source DHCP configure data. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_SUCCESS The configure is copied. + +**/ +EFI_STATUS +Dns4CopyConfigure ( + OUT EFI_DNS4_CONFIG_DATA *Dst, + IN EFI_DNS4_CONFIG_DATA *Src + ); + +/** + Allocate memory for configure parameter such as timeout value for Dst, + then copy the configure parameter from Src to Dst. + + @param[out] Dst The destination DHCP configure data. + @param[in] Src The source DHCP configure data. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_SUCCESS The configure is copied. + +**/ +EFI_STATUS +Dns6CopyConfigure ( + OUT EFI_DNS6_CONFIG_DATA *Dst, + IN EFI_DNS6_CONFIG_DATA *Src + ); + +/** + Callback of Dns packet. Does nothing. + + @param Arg The context. + +**/ +VOID +EFIAPI +DnsDummyExtFree ( + IN VOID *Arg + ); + +/** + Poll the UDP to get the IP4 default address, which may be retrieved + by DHCP. + + The default time out value is 5 seconds. If IP has retrieved the default address, + the UDP is reconfigured. + + @param Instance The DNS instance + @param UdpIo The UDP_IO to poll + @param UdpCfgData The UDP configure data to reconfigure the UDP_IO + + @retval TRUE The default address is retrieved and UDP is reconfigured. + @retval FALSE Some error occured. + +**/ +BOOLEAN +Dns4GetMapping ( + IN DNS_INSTANCE *Instance, + IN UDP_IO *UdpIo, + IN EFI_UDP4_CONFIG_DATA *UdpCfgData + ); + +/** + Configure the opened Udp6 instance until the corresponding Ip6 instance + has been configured. + + @param Instance The DNS instance + @param UdpIo The UDP_IO to poll + @param UdpCfgData The UDP configure data to reconfigure the UDP_IO + + @retval TRUE Configure the Udp6 instance successfully. + @retval FALSE Some error occured. + +**/ +BOOLEAN +Dns6GetMapping ( + IN DNS_INSTANCE *Instance, + IN UDP_IO *UdpIo, + IN EFI_UDP6_CONFIG_DATA *UdpCfgData + ); + +/** + Configure the UDP. + + @param Instance The DNS session + @param UdpIo The UDP_IO instance + + @retval EFI_SUCCESS The UDP is successfully configured for the + session. + +**/ +EFI_STATUS +Dns4ConfigUdp ( + IN DNS_INSTANCE *Instance, + IN UDP_IO *UdpIo + ); + +/** + Configure the UDP. + + @param Instance The DNS session + @param UdpIo The UDP_IO instance + + @retval EFI_SUCCESS The UDP is successfully configured for the + session. + +**/ +EFI_STATUS +Dns6ConfigUdp ( + IN DNS_INSTANCE *Instance, + IN UDP_IO *UdpIo + ); + +/** + Update Dns4 cache to shared list of caches of all DNSv4 instances. + + @param Dns4CacheList All Dns4 cache list. + @param DeleteFlag If FALSE, this function is to add one entry to the DNS Cache. + If TRUE, this function will delete matching DNS Cache entry. + @param Override If TRUE, the matching DNS cache entry will be overwritten with the supplied parameter. + If FALSE, EFI_ACCESS_DENIED will be returned if the entry to be added is already exists. + @param DnsCacheEntry Entry Pointer to DNS Cache entry. + + @retval EFI_SUCCESS Update Dns4 cache successfully. + @retval Others Failed to update Dns4 cache. + +**/ +EFI_STATUS +EFIAPI +UpdateDns4Cache ( + IN LIST_ENTRY *Dns4CacheList, + IN BOOLEAN DeleteFlag, + IN BOOLEAN Override, + IN EFI_DNS4_CACHE_ENTRY DnsCacheEntry + ); + +/** + Update Dns6 cache to shared list of caches of all DNSv6 instances. + + @param Dns6CacheList All Dns6 cache list. + @param DeleteFlag If FALSE, this function is to add one entry to the DNS Cache. + If TRUE, this function will delete matching DNS Cache entry. + @param Override If TRUE, the matching DNS cache entry will be overwritten with the supplied parameter. + If FALSE, EFI_ACCESS_DENIED will be returned if the entry to be added is already exists. + @param DnsCacheEntry Entry Pointer to DNS Cache entry. + + @retval EFI_SUCCESS Update Dns6 cache successfully. + @retval Others Failed to update Dns6 cache. +**/ +EFI_STATUS +EFIAPI +UpdateDns6Cache ( + IN LIST_ENTRY *Dns6CacheList, + IN BOOLEAN DeleteFlag, + IN BOOLEAN Override, + IN EFI_DNS6_CACHE_ENTRY DnsCacheEntry + ); + +/** + Add Dns4 ServerIp to common list of addresses of all configured DNSv4 server. + + @param Dns4ServerList Common list of addresses of all configured DNSv4 server. + @param ServerIp DNS server Ip. + + @retval EFI_SUCCESS Add Dns4 ServerIp to common list successfully. + @retval Others Failed to add Dns4 ServerIp to common list. + +**/ +EFI_STATUS +EFIAPI +AddDns4ServerIp ( + IN LIST_ENTRY *Dns4ServerList, + IN EFI_IPv4_ADDRESS ServerIp + ); + +/** + Add Dns6 ServerIp to common list of addresses of all configured DNSv6 server. + + @param Dns6ServerList Common list of addresses of all configured DNSv6 server. + @param ServerIp DNS server Ip. + + @retval EFI_SUCCESS Add Dns6 ServerIp to common list successfully. + @retval Others Failed to add Dns6 ServerIp to common list. + +**/ +EFI_STATUS +EFIAPI +AddDns6ServerIp ( + IN LIST_ENTRY *Dns6ServerList, + IN EFI_IPv6_ADDRESS ServerIp + ); + +/** + Find out whether the response is valid or invalid. + + @param TokensMap All DNS transmittal Tokens entry. + @param Identification Identification for queried packet. + @param Type Type for queried packet. + @param Class Class for queried packet. + @param Item Return corresponding Token entry. + + @retval TRUE The response is valid. + @retval FALSE The response is invalid. + +**/ +BOOLEAN +IsValidDnsResponse ( + IN NET_MAP *TokensMap, + IN UINT16 Identification, + IN UINT16 Type, + IN UINT16 Class, + OUT NET_MAP_ITEM **Item + ); + +/** + Parse Dns Response. + + @param Instance The DNS instance + @param RxString Received buffer. + @param Length Received buffer length. + @param Completed Flag to indicate that Dns response is valid. + + @retval EFI_SUCCESS Parse Dns Response successfully. + @retval Others Failed to parse Dns Response. + +**/ +EFI_STATUS +ParseDnsResponse ( + IN OUT DNS_INSTANCE *Instance, + IN UINT8 *RxString, + IN UINT32 Length, + OUT BOOLEAN *Completed + ); + +/** + Parse response packet. + + @param Packet The packets received. + @param EndPoint The local/remote UDP access point + @param IoStatus The status of the UDP receive + @param Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +DnsOnPacketReceived ( + NET_BUF *Packet, + UDP_END_POINT *EndPoint, + EFI_STATUS IoStatus, + VOID *Context + ); + +/** + Release the net buffer when packet is sent. + + @param Packet The packets received. + @param EndPoint The local/remote UDP access point + @param IoStatus The status of the UDP receive + @param Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +DnsOnPacketSent ( + NET_BUF *Packet, + UDP_END_POINT *EndPoint, + EFI_STATUS IoStatus, + VOID *Context + ); + +/** + Query request information. + + @param Instance The DNS instance + @param Packet The packet for querying request information. + + @retval EFI_SUCCESS Query request information successfully. + @retval Others Failed to query request information. + +**/ +EFI_STATUS +DoDnsQuery ( + IN DNS_INSTANCE *Instance, + IN NET_BUF *Packet + ); + +/** + Construct the Packet according query section. + + @param Instance The DNS instance + @param QueryName Queried Name + @param Type Queried Type + @param Class Queried Class + @param Packet The packet for query + + @retval EFI_SUCCESS The packet is constructed. + @retval Others Failed to construct the Packet. + +**/ +EFI_STATUS +ConstructDNSQuery ( + IN DNS_INSTANCE *Instance, + IN CHAR8 *QueryName, + IN UINT16 Type, + IN UINT16 Class, + OUT NET_BUF **Packet + ); + +/** + Retransmit the packet. + + @param Instance The DNS instance + @param Packet Retransmit the packet + + @retval EFI_SUCCESS The packet is retransmitted. + @retval Others Failed to retransmit. + +**/ +EFI_STATUS +DnsRetransmit ( + IN DNS_INSTANCE *Instance, + IN NET_BUF *Packet + ); + +/** + The timer ticking function for the DNS service. + + @param Event The ticking event + @param Context The DNS service instance + +**/ +VOID +EFIAPI +DnsOnTimerRetransmit ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + The timer ticking function for the DNS driver. + + @param Event The ticking event + @param Context NULL + +**/ +VOID +EFIAPI +DnsOnTimerUpdate ( + IN EFI_EVENT Event, + IN VOID *Context + ); + + +/** + Retrieve mode data of this DNS instance. + + This function is used to retrieve DNS mode data for this DNS instance. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[out] DnsModeData Point to the mode data. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED When DnsConfigData is queried, no configuration data + is available because this instance has not been + configured. + @retval EFI_INVALID_PARAMETER This is NULL or DnsModeData is NULL. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns4GetModeData ( + IN EFI_DNS4_PROTOCOL *This, + OUT EFI_DNS4_MODE_DATA *DnsModeData + ); + +/** + Configure this DNS instance. + + This function is used to configure DNS mode data for this DNS instance. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] DnsConfigData Point to the Configuration data. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_UNSUPPORTED The designated protocol is not supported. + @retval EFI_INVALID_PARAMTER Thisis NULL. + The StationIp address provided in DnsConfigData is not a + valid unicast. + DnsServerList is NULL while DnsServerListCount + is not ZERO. + DnsServerListCount is ZERO while DnsServerList + is not NULL + @retval EFI_OUT_OF_RESOURCES The DNS instance data or required space could not be + allocated. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The + EFI DNSv4 Protocol instance is not configured. + @retval EFI_ALREADY_STARTED Second call to Configure() with DnsConfigData. To + reconfigure the instance the caller must call Configure() + with NULL first to return driver to unconfigured state. +**/ +EFI_STATUS +EFIAPI +Dns4Configure ( + IN EFI_DNS4_PROTOCOL *This, + IN EFI_DNS4_CONFIG_DATA *DnsConfigData + ); + +/** + Host name to host address translation. + + The HostNameToIp () function is used to translate the host name to host IP address. A + type A query is used to get the one or more IP addresses for this host. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] HostName Host name. + @param[in] Token Point to the completion token to translate host name + to host address. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + HostName is NULL. HostName string is unsupported format. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_NOT_STARTED This instance has not been started. +**/ +EFI_STATUS +EFIAPI +Dns4HostNameToIp ( + IN EFI_DNS4_PROTOCOL *This, + IN CHAR16 *HostName, + IN EFI_DNS4_COMPLETION_TOKEN *Token + ); + +/** + IPv4 address to host name translation also known as Reverse DNS lookup. + + The IpToHostName() function is used to translate the host address to host name. A type PTR + query is used to get the primary name of the host. Support of this function is optional. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] IpAddress Ip Address. + @param[in] Token Point to the completion token to translate host + address to host name. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_UNSUPPORTED This function is not supported. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + IpAddress is not valid IP address . + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_ALREADY_STARTED This Token is being used in another DNS session. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns4IpToHostName ( + IN EFI_DNS4_PROTOCOL *This, + IN EFI_IPv4_ADDRESS IpAddress, + IN EFI_DNS4_COMPLETION_TOKEN *Token + ); + +/** + Retrieve arbitrary information from the DNS server. + + This GeneralLookup() function retrieves arbitrary information from the DNS. The caller + supplies a QNAME, QTYPE, and QCLASS, and all of the matching RRs are returned. All + RR content (e.g., TTL) was returned. The caller need parse the returned RR to get + required information. The function is optional. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] QName Pointer to Query Name. + @param[in] QType Query Type. + @param[in] QClass Query Name. + @param[in] Token Point to the completion token to retrieve arbitrary + information. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_UNSUPPORTED This function is not supported. Or the requested + QType is not supported + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + QName is NULL. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_ALREADY_STARTED This Token is being used in another DNS session. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns4GeneralLookUp ( + IN EFI_DNS4_PROTOCOL *This, + IN CHAR8 *QName, + IN UINT16 QType, + IN UINT16 QClass, + IN EFI_DNS4_COMPLETION_TOKEN *Token + ); + +/** + This function is to update the DNS Cache. + + The UpdateDnsCache() function is used to add/delete/modify DNS cache entry. DNS cache + can be normally dynamically updated after the DNS resolve succeeds. This function + provided capability to manually add/delete/modify the DNS cache. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] DeleteFlag If FALSE, this function is to add one entry to the + DNS Cahce. If TRUE, this function will delete + matching DNS Cache entry. + @param[in] Override If TRUE, the maching DNS cache entry will be + overwritten with the supplied parameter. If FALSE, + EFI_ACCESS_DENIED will be returned if the entry to + be added is already existed. + @param[in] DnsCacheEntry Pointer to DNS Cache entry. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + DnsCacheEntry.HostName is NULL. + DnsCacheEntry.IpAddress is NULL. + DnsCacheEntry.Timeout is zero. + @retval EFI_ACCESS_DENIED The DNS cache entry already exists and Override is + not TRUE. +**/ +EFI_STATUS +EFIAPI +Dns4UpdateDnsCache ( + IN EFI_DNS4_PROTOCOL *This, + IN BOOLEAN DeleteFlag, + IN BOOLEAN Override, + IN EFI_DNS4_CACHE_ENTRY DnsCacheEntry + ); + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications to increase the + rate that data packets are moved between the communications device and the transmit + and receive queues. + In some systems, the periodic timer event in the managed network driver may not poll + the underlying communications device fast enough to transmit and/or receive all data + packets without missing incoming packets or dropping outgoing packets. Drivers and + applications that are experiencing packet loss should try calling the Poll() + function more often. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI DNS Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive + queue. Consider increasing the polling rate. +**/ +EFI_STATUS +EFIAPI +Dns4Poll ( + IN EFI_DNS4_PROTOCOL *This + ); + +/** + Abort an asynchronous DNS operation, including translation between IP and Host, and + general look up behavior. + + The Cancel() function is used to abort a pending resolution request. After calling + this function, Token.Status will be set to EFI_ABORTED and then Token.Event will be + signaled. If the token is not in one of the queues, which usually means that the + asynchronous operation has completed, this function will not signal the token and + EFI_NOT_FOUND is returned. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_DNS4_PROTOCOL.HostNameToIp (), + EFI_DNS4_PROTOCOL.IpToHostName() or + EFI_DNS4_PROTOCOL.GeneralLookup(). + If NULL, all pending tokens are aborted. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI DNS4 Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_FOUND When Token is not NULL, and the asynchronous DNS + operation was not found in the transmit queue. It + was either completed or was not issued by + HostNameToIp(), IpToHostName() or GeneralLookup(). +**/ +EFI_STATUS +EFIAPI +Dns4Cancel ( + IN EFI_DNS4_PROTOCOL *This, + IN EFI_DNS4_COMPLETION_TOKEN *Token + ); + + +/** + Retrieve mode data of this DNS instance. + + This function is used to retrieve DNS mode data for this DNS instance. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[out] DnsModeData Pointer to the caller-allocated storage for the + EFI_DNS6_MODE_DATA data. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED When DnsConfigData is queried, no configuration data + is available because this instance has not been + configured. + @retval EFI_INVALID_PARAMETER This is NULL or DnsModeData is NULL. + @retval EFI_OUT_OF_RESOURCE Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns6GetModeData ( + IN EFI_DNS6_PROTOCOL *This, + OUT EFI_DNS6_MODE_DATA *DnsModeData + ); + +/** + Configure this DNS instance. + + The Configure() function is used to set and change the configuration data for this + EFI DNSv6 Protocol driver instance. Reset the DNS instance if DnsConfigData is NULL. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] DnsConfigData Pointer to the configuration data structure. All associated + storage to be allocated and released by caller. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMTER This is NULL. + The StationIp address provided in DnsConfigData is not zero and not a valid unicast. + DnsServerList is NULL while DnsServerList Count is not ZERO. + DnsServerList Count is ZERO while DnsServerList is not NULL. + @retval EFI_OUT_OF_RESOURCES The DNS instance data or required space could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The + EFI DNSv6 Protocol instance is not configured. + @retval EFI_UNSUPPORTED The designated protocol is not supported. + @retval EFI_ALREADY_STARTED Second call to Configure() with DnsConfigData. To + reconfigure the instance the caller must call Configure() with + NULL first to return driver to unconfigured state. +**/ +EFI_STATUS +EFIAPI +Dns6Configure ( + IN EFI_DNS6_PROTOCOL *This, + IN EFI_DNS6_CONFIG_DATA *DnsConfigData + ); + +/** + Host name to host address translation. + + The HostNameToIp () function is used to translate the host name to host IP address. A + type AAAA query is used to get the one or more IPv6 addresses for this host. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] HostName Host name. + @param[in] Token Point to the completion token to translate host name + to host address. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + HostName is NULL or buffer contained unsupported characters. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_ALREADY_STARTED This Token is being used in another DNS session. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns6HostNameToIp ( + IN EFI_DNS6_PROTOCOL *This, + IN CHAR16 *HostName, + IN EFI_DNS6_COMPLETION_TOKEN *Token + ); + +/** + Host address to host name translation. + + The IpToHostName () function is used to translate the host address to host name. A + type PTR query is used to get the primary name of the host. Implementation can choose + to support this function or not. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] IpAddress Ip Address. + @param[in] Token Point to the completion token to translate host + address to host name. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_UNSUPPORTED This function is not supported. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + IpAddress is not valid IP address. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns6IpToHostName ( + IN EFI_DNS6_PROTOCOL *This, + IN EFI_IPv6_ADDRESS IpAddress, + IN EFI_DNS6_COMPLETION_TOKEN *Token + ); + +/** + This function provides capability to retrieve arbitrary information from the DNS + server. + + This GeneralLookup() function retrieves arbitrary information from the DNS. The caller + supplies a QNAME, QTYPE, and QCLASS, and all of the matching RRs are returned. All + RR content (e.g., TTL) was returned. The caller need parse the returned RR to get + required information. The function is optional. Implementation can choose to support + it or not. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] QName Pointer to Query Name. + @param[in] QType Query Type. + @param[in] QClass Query Name. + @param[in] Token Point to the completion token to retrieve arbitrary + information. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_UNSUPPORTED This function is not supported. Or the requested + QType is not supported + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + QName is NULL. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns6GeneralLookUp ( + IN EFI_DNS6_PROTOCOL *This, + IN CHAR8 *QName, + IN UINT16 QType, + IN UINT16 QClass, + IN EFI_DNS6_COMPLETION_TOKEN *Token + ); + +/** + This function is to update the DNS Cache. + + The UpdateDnsCache() function is used to add/delete/modify DNS cache entry. DNS cache + can be normally dynamically updated after the DNS resolve succeeds. This function + provided capability to manually add/delete/modify the DNS cache. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] DeleteFlag If FALSE, this function is to add one entry to the + DNS Cahce. If TRUE, this function will delete + matching DNS Cache entry. + @param[in] Override If TRUE, the maching DNS cache entry will be + overwritten with the supplied parameter. If FALSE, + EFI_ACCESS_DENIED will be returned if the entry to + be added is already existed. + @param[in] DnsCacheEntry Pointer to DNS Cache entry. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + DnsCacheEntry.HostName is NULL. + DnsCacheEntry.IpAddress is NULL. + DnsCacheEntry.Timeout is zero. + @retval EFI_ACCESS_DENIED The DNS cache entry already exists and Override is + not TRUE. + @retval EFI_OUT_OF_RESOURCE Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns6UpdateDnsCache ( + IN EFI_DNS6_PROTOCOL *This, + IN BOOLEAN DeleteFlag, + IN BOOLEAN Override, + IN EFI_DNS6_CACHE_ENTRY DnsCacheEntry + ); + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications to increase the + rate that data packets are moved between the communications device and the transmit + and receive queues. + + In some systems, the periodic timer event in the managed network driver may not poll + the underlying communications device fast enough to transmit and/or receive all data + packets without missing incoming packets or dropping outgoing packets. Drivers and + applications that are experiencing packet loss should try calling the Poll() + function more often. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI DNS Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NO_MAPPING There is no source address is available for use. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive + queue. Consider increasing the polling rate. +**/ +EFI_STATUS +EFIAPI +Dns6Poll ( + IN EFI_DNS6_PROTOCOL *This + ); + +/** + Abort an asynchronous DNS operation, including translation between IP and Host, and + general look up behavior. + + The Cancel() function is used to abort a pending resolution request. After calling + this function, Token.Status will be set to EFI_ABORTED and then Token.Event will be + signaled. If the token is not in one of the queues, which usually means that the + asynchronous operation has completed, this function will not signal the token and + EFI_NOT_FOUND is returned. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_DNS6_PROTOCOL.HostNameToIp (), + EFI_DNS6_PROTOCOL.IpToHostName() or + EFI_DNS6_PROTOCOL.GeneralLookup(). + If NULL, all pending tokens are aborted. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI DNS6 Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_NOT_FOUND When Token is not NULL, and the asynchronous DNS + operation was not found in the transmit queue. It + was either completed or was not issued by + HostNameToIp(), IpToHostName() or GeneralLookup(). +**/ +EFI_STATUS +EFIAPI +Dns6Cancel ( + IN EFI_DNS6_PROTOCOL *This, + IN EFI_DNS6_COMPLETION_TOKEN *Token + ); + +#endif diff --git a/NetworkPkg/DnsDxe/DnsProtocol.c b/NetworkPkg/DnsDxe/DnsProtocol.c new file mode 100644 index 000000000..9b98e90dd --- /dev/null +++ b/NetworkPkg/DnsDxe/DnsProtocol.c @@ -0,0 +1,1729 @@ +/** @file +Implementation of EFI_DNS4_PROTOCOL and EFI_DNS6_PROTOCOL interfaces. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "DnsImpl.h" + +EFI_DNS4_PROTOCOL mDns4Protocol = { + Dns4GetModeData, + Dns4Configure, + Dns4HostNameToIp, + Dns4IpToHostName, + Dns4GeneralLookUp, + Dns4UpdateDnsCache, + Dns4Poll, + Dns4Cancel +}; + +EFI_DNS6_PROTOCOL mDns6Protocol = { + Dns6GetModeData, + Dns6Configure, + Dns6HostNameToIp, + Dns6IpToHostName, + Dns6GeneralLookUp, + Dns6UpdateDnsCache, + Dns6Poll, + Dns6Cancel +}; + +/** + Retrieve mode data of this DNS instance. + + This function is used to retrieve DNS mode data for this DNS instance. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[out] DnsModeData Point to the mode data. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED When DnsConfigData is queried, no configuration data + is available because this instance has not been + configured. + @retval EFI_INVALID_PARAMETER This is NULL or DnsModeData is NULL. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns4GetModeData ( + IN EFI_DNS4_PROTOCOL *This, + OUT EFI_DNS4_MODE_DATA *DnsModeData + ) +{ + DNS_INSTANCE *Instance; + + EFI_TPL OldTpl; + + UINTN Index; + + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + + DNS4_SERVER_IP *ServerItem; + EFI_IPv4_ADDRESS *ServerList; + DNS4_CACHE *CacheItem; + EFI_DNS4_CACHE_ENTRY *CacheList; + EFI_STATUS Status; + + ServerItem = NULL; + ServerList = NULL; + CacheItem = NULL; + CacheList = NULL; + Status = EFI_SUCCESS; + + + if ((This == NULL) || (DnsModeData == NULL)) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL4 (This); + if (Instance->State == DNS_STATE_UNCONFIGED) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + ZeroMem (DnsModeData, sizeof (EFI_DNS4_MODE_DATA)); + + // + // Get the current configuration data of this instance. + // + Status = Dns4CopyConfigure (&DnsModeData->DnsConfigData, &Instance->Dns4CfgData); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Get the DnsServerCount and DnsServerList + // + Index = 0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns4ServerList) { + Index++; + } + DnsModeData->DnsServerCount = (UINT32) Index; + ServerList = AllocatePool (sizeof (EFI_IPv4_ADDRESS) * DnsModeData->DnsServerCount); + if (ServerList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + Dns4CleanConfigure (&DnsModeData->DnsConfigData); + goto ON_EXIT; + } + + Index = 0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns4ServerList) { + ServerItem = NET_LIST_USER_STRUCT (Entry, DNS4_SERVER_IP, AllServerLink); + CopyMem (ServerList + Index, &ServerItem->Dns4ServerIp, sizeof (EFI_IPv4_ADDRESS)); + Index++; + } + DnsModeData->DnsServerList = ServerList; + + // + // Get the DnsCacheCount and DnsCacheList + // + Index =0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns4CacheList) { + Index++; + } + DnsModeData->DnsCacheCount = (UINT32) Index; + CacheList = AllocatePool (sizeof (EFI_DNS4_CACHE_ENTRY) * DnsModeData->DnsCacheCount); + if (CacheList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + Dns4CleanConfigure (&DnsModeData->DnsConfigData); + FreePool (ServerList); + goto ON_EXIT; + } + + Index =0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns4CacheList) { + CacheItem = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink); + CopyMem (CacheList + Index, &CacheItem->DnsCache, sizeof (EFI_DNS4_CACHE_ENTRY)); + Index++; + } + DnsModeData->DnsCacheList = CacheList; + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Configure this DNS instance. + + This function is used to configure DNS mode data for this DNS instance. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] DnsConfigData Point to the Configuration data. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_UNSUPPORTED The designated protocol is not supported. + @retval EFI_INVALID_PARAMTER Thisis NULL. + The StationIp address provided in DnsConfigData is not a + valid unicast. + DnsServerList is NULL while DnsServerListCount + is not ZERO. + DnsServerListCount is ZERO while DnsServerList + is not NULL + @retval EFI_OUT_OF_RESOURCES The DNS instance data or required space could not be + allocated. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The + EFI DNSv4 Protocol instance is not configured. + @retval EFI_ALREADY_STARTED Second call to Configure() with DnsConfigData. To + reconfigure the instance the caller must call Configure() + with NULL first to return driver to unconfigured state. +**/ +EFI_STATUS +EFIAPI +Dns4Configure ( + IN EFI_DNS4_PROTOCOL *This, + IN EFI_DNS4_CONFIG_DATA *DnsConfigData + ) +{ + EFI_STATUS Status; + DNS_INSTANCE *Instance; + + EFI_TPL OldTpl; + IP4_ADDR Ip; + IP4_ADDR Netmask; + + UINT32 ServerListCount; + EFI_IPv4_ADDRESS *ServerList; + + Status = EFI_SUCCESS; + ServerList = NULL; + + if (This == NULL || + (DnsConfigData != NULL && ((DnsConfigData->DnsServerListCount != 0 && DnsConfigData->DnsServerList == NULL) || + (DnsConfigData->DnsServerListCount == 0 && DnsConfigData->DnsServerList != NULL)))) { + return EFI_INVALID_PARAMETER; + } + + if (DnsConfigData != NULL && DnsConfigData->Protocol != DNS_PROTOCOL_UDP) { + return EFI_UNSUPPORTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL4 (This); + + if (DnsConfigData == NULL) { + ZeroMem (&Instance->SessionDnsServer, sizeof (EFI_IP_ADDRESS)); + + // + // Reset the Instance if ConfigData is NULL + // + if (!NetMapIsEmpty(&Instance->Dns4TxTokens)) { + Dns4InstanceCancelToken(Instance, NULL); + } + + if (Instance->UdpIo != NULL){ + UdpIoCleanIo (Instance->UdpIo); + } + + if (Instance->Dns4CfgData.DnsServerList != NULL) { + FreePool (Instance->Dns4CfgData.DnsServerList); + } + ZeroMem (&Instance->Dns4CfgData, sizeof (EFI_DNS4_CONFIG_DATA)); + + Instance->State = DNS_STATE_UNCONFIGED; + } else { + // + // Configure the parameters for new operation. + // + CopyMem (&Ip, &DnsConfigData->StationIp, sizeof (IP4_ADDR)); + CopyMem (&Netmask, &DnsConfigData->SubnetMask, sizeof (IP4_ADDR)); + + Ip = NTOHL (Ip); + Netmask = NTOHL (Netmask); + + if (!DnsConfigData->UseDefaultSetting && + ((!IP4_IS_VALID_NETMASK (Netmask) || (Netmask != 0 && !NetIp4IsUnicast (Ip, Netmask))))) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + Status = Dns4CopyConfigure (&Instance->Dns4CfgData, DnsConfigData); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + if (DnsConfigData->DnsServerListCount == 0) { + gBS->RestoreTPL (OldTpl); + + // + // The DNS instance will retrieve DNS server from DHCP Server + // + Status = GetDns4ServerFromDhcp4 ( + Instance, + &ServerListCount, + &ServerList + ); + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT(ServerList != NULL); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + CopyMem (&Instance->SessionDnsServer.v4, &ServerList[0], sizeof (EFI_IPv4_ADDRESS)); + } else { + CopyMem (&Instance->SessionDnsServer.v4, &DnsConfigData->DnsServerList[0], sizeof (EFI_IPv4_ADDRESS)); + } + + // + // Config UDP + // + Status = Dns4ConfigUdp (Instance, Instance->UdpIo); + if (EFI_ERROR (Status)) { + if (Instance->Dns4CfgData.DnsServerList != NULL) { + FreePool (Instance->Dns4CfgData.DnsServerList); + Instance->Dns4CfgData.DnsServerList = NULL; + } + goto ON_EXIT; + } + + // + // Add configured DNS server used by this instance to ServerList. + // + Status = AddDns4ServerIp (&mDriverData->Dns4ServerList, Instance->SessionDnsServer.v4); + if (EFI_ERROR (Status)) { + if (Instance->Dns4CfgData.DnsServerList != NULL) { + FreePool (Instance->Dns4CfgData.DnsServerList); + Instance->Dns4CfgData.DnsServerList = NULL; + } + goto ON_EXIT; + } + + Instance->State = DNS_STATE_CONFIGED; + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Host name to host address translation. + + The HostNameToIp () function is used to translate the host name to host IP address. A + type A query is used to get the one or more IP addresses for this host. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] HostName Host name. + @param[in] Token Point to the completion token to translate host name + to host address. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + HostName is NULL. HostName string is unsupported format. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_NOT_STARTED This instance has not been started. +**/ +EFI_STATUS +EFIAPI +Dns4HostNameToIp ( + IN EFI_DNS4_PROTOCOL *This, + IN CHAR16 *HostName, + IN EFI_DNS4_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + + DNS_INSTANCE *Instance; + + EFI_DNS4_CONFIG_DATA *ConfigData; + + UINTN Index; + DNS4_CACHE *Item; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + + CHAR8 *QueryName; + + DNS4_TOKEN_ENTRY *TokenEntry; + NET_BUF *Packet; + + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + Item = NULL; + QueryName = NULL; + TokenEntry = NULL; + Packet = NULL; + + // + // Validate the parameters + // + if ((This == NULL) || (HostName == NULL) || Token == NULL) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL4 (This); + + ConfigData = &(Instance->Dns4CfgData); + + if (Instance->State != DNS_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + Token->Status = EFI_NOT_READY; + + // + // If zero, use the parameter configured through Dns.Configure() interface. + // + if (Token->RetryCount == 0) { + Token->RetryCount = ConfigData->RetryCount; + } + + // + // If zero, use the parameter configured through Dns.Configure() interface. + // + if (Token->RetryInterval == 0) { + Token->RetryInterval = ConfigData->RetryInterval; + } + + // + // Minimum interval of retry is 2 second. If the retry interval is less than 2 second, then use the 2 second. + // + if (Token->RetryInterval < DNS_DEFAULT_TIMEOUT) { + Token->RetryInterval = DNS_DEFAULT_TIMEOUT; + } + + // + // Check cache + // + if (ConfigData->EnableDnsCache) { + Index = 0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns4CacheList) { + Item = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink); + if (StrCmp (HostName, Item->DnsCache.HostName) == 0) { + Index++; + } + } + + if (Index != 0) { + Token->RspData.H2AData = AllocatePool (sizeof (DNS_HOST_TO_ADDR_DATA)); + if (Token->RspData.H2AData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Token->RspData.H2AData->IpCount = (UINT32)Index; + Token->RspData.H2AData->IpList = AllocatePool (sizeof (EFI_IPv4_ADDRESS) * Index); + if (Token->RspData.H2AData->IpList == NULL) { + if (Token->RspData.H2AData != NULL) { + FreePool (Token->RspData.H2AData); + } + + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Index = 0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns4CacheList) { + Item = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink); + if ((UINT32)Index < Token->RspData.H2AData->IpCount && StrCmp (HostName, Item->DnsCache.HostName) == 0) { + CopyMem ((Token->RspData.H2AData->IpList) + Index, Item->DnsCache.IpAddress, sizeof (EFI_IPv4_ADDRESS)); + Index++; + } + } + + Token->Status = EFI_SUCCESS; + + if (Token->Event != NULL) { + gBS->SignalEvent (Token->Event); + DispatchDpc (); + } + + Status = Token->Status; + goto ON_EXIT; + } + } + + // + // Construct DNS TokenEntry. + // + TokenEntry = AllocateZeroPool (sizeof(DNS4_TOKEN_ENTRY)); + if (TokenEntry == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + TokenEntry->PacketToLive = Token->RetryInterval; + TokenEntry->Token = Token; + TokenEntry->QueryHostName = AllocateZeroPool (StrSize (HostName)); + if (TokenEntry->QueryHostName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + CopyMem (TokenEntry->QueryHostName, HostName, StrSize (HostName)); + + // + // Construct QName. + // + QueryName = NetLibCreateDnsQName (TokenEntry->QueryHostName); + if (QueryName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Construct DNS Query Packet. + // + Status = ConstructDNSQuery (Instance, QueryName, DNS_TYPE_A, DNS_CLASS_INET, &Packet); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + ASSERT (Packet != NULL); + + // + // Save the token into the Dns4TxTokens map. + // + Status = NetMapInsertTail (&Instance->Dns4TxTokens, TokenEntry, Packet); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Dns Query Ip + // + Status = DoDnsQuery (Instance, Packet); + if (EFI_ERROR (Status)) { + Dns4RemoveTokenEntry (&Instance->Dns4TxTokens, TokenEntry); + } + +ON_EXIT: + + if (EFI_ERROR (Status)) { + if (TokenEntry != NULL) { + if (TokenEntry->QueryHostName != NULL) { + FreePool (TokenEntry->QueryHostName); + } + + FreePool (TokenEntry); + } + + if (Packet != NULL) { + NetbufFree (Packet); + } + } + + if (QueryName != NULL) { + FreePool (QueryName); + } + + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + IPv4 address to host name translation also known as Reverse DNS lookup. + + The IpToHostName() function is used to translate the host address to host name. A type PTR + query is used to get the primary name of the host. Support of this function is optional. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] IpAddress Ip Address. + @param[in] Token Point to the completion token to translate host + address to host name. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_UNSUPPORTED This function is not supported. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + IpAddress is not valid IP address . + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_ALREADY_STARTED This Token is being used in another DNS session. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns4IpToHostName ( + IN EFI_DNS4_PROTOCOL *This, + IN EFI_IPv4_ADDRESS IpAddress, + IN EFI_DNS4_COMPLETION_TOKEN *Token + ) +{ + return EFI_UNSUPPORTED; +} + +/** + Retrieve arbitrary information from the DNS server. + + This GeneralLookup() function retrieves arbitrary information from the DNS. The caller + supplies a QNAME, QTYPE, and QCLASS, and all of the matching RRs are returned. All + RR content (e.g., TTL) was returned. The caller need parse the returned RR to get + required information. The function is optional. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] QName Pointer to Query Name. + @param[in] QType Query Type. + @param[in] QClass Query Name. + @param[in] Token Point to the completion token to retrieve arbitrary + information. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_UNSUPPORTED This function is not supported. Or the requested + QType is not supported + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + QName is NULL. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_ALREADY_STARTED This Token is being used in another DNS session. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns4GeneralLookUp ( + IN EFI_DNS4_PROTOCOL *This, + IN CHAR8 *QName, + IN UINT16 QType, + IN UINT16 QClass, + IN EFI_DNS4_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + + DNS_INSTANCE *Instance; + + EFI_DNS4_CONFIG_DATA *ConfigData; + + DNS4_TOKEN_ENTRY *TokenEntry; + NET_BUF *Packet; + + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + TokenEntry = NULL; + Packet = NULL; + + // + // Validate the parameters + // + if ((This == NULL) || (QName == NULL) || Token == NULL) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL4 (This); + + ConfigData = &(Instance->Dns4CfgData); + + if (Instance->State != DNS_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + Token->Status = EFI_NOT_READY; + + // + // If zero, use the parameter configured through Dns.Configure() interface. + // + if (Token->RetryCount == 0) { + Token->RetryCount = ConfigData->RetryCount; + } + + // + // If zero, use the parameter configured through Dns.Configure() interface. + // + if (Token->RetryInterval == 0) { + Token->RetryInterval = ConfigData->RetryInterval; + } + + // + // Minimum interval of retry is 2 second. If the retry interval is less than 2 second, then use the 2 second. + // + if (Token->RetryInterval < DNS_DEFAULT_TIMEOUT) { + Token->RetryInterval = DNS_DEFAULT_TIMEOUT; + } + + // + // Construct DNS TokenEntry. + // + TokenEntry = AllocateZeroPool (sizeof(DNS4_TOKEN_ENTRY)); + if (TokenEntry == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + TokenEntry->PacketToLive = Token->RetryInterval; + TokenEntry->GeneralLookUp = TRUE; + TokenEntry->Token = Token; + + // + // Construct DNS Query Packet. + // + Status = ConstructDNSQuery (Instance, QName, QType, QClass, &Packet); + if (EFI_ERROR (Status)) { + if (TokenEntry != NULL) { + FreePool (TokenEntry); + } + + goto ON_EXIT; + } + + ASSERT (Packet != NULL); + + // + // Save the token into the Dns4TxTokens map. + // + Status = NetMapInsertTail (&Instance->Dns4TxTokens, TokenEntry, Packet); + if (EFI_ERROR (Status)) { + if (TokenEntry != NULL) { + FreePool (TokenEntry); + } + + NetbufFree (Packet); + + goto ON_EXIT; + } + + // + // Dns Query Ip + // + Status = DoDnsQuery (Instance, Packet); + if (EFI_ERROR (Status)) { + Dns4RemoveTokenEntry (&Instance->Dns4TxTokens, TokenEntry); + + if (TokenEntry != NULL) { + FreePool (TokenEntry); + } + + NetbufFree (Packet); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + This function is to update the DNS Cache. + + The UpdateDnsCache() function is used to add/delete/modify DNS cache entry. DNS cache + can be normally dynamically updated after the DNS resolve succeeds. This function + provided capability to manually add/delete/modify the DNS cache. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] DeleteFlag If FALSE, this function is to add one entry to the + DNS Cahce. If TRUE, this function will delete + matching DNS Cache entry. + @param[in] Override If TRUE, the maching DNS cache entry will be + overwritten with the supplied parameter. If FALSE, + EFI_ACCESS_DENIED will be returned if the entry to + be added is already existed. + @param[in] DnsCacheEntry Pointer to DNS Cache entry. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + DnsCacheEntry.HostName is NULL. + DnsCacheEntry.IpAddress is NULL. + DnsCacheEntry.Timeout is zero. + @retval EFI_ACCESS_DENIED The DNS cache entry already exists and Override is + not TRUE. +**/ +EFI_STATUS +EFIAPI +Dns4UpdateDnsCache ( + IN EFI_DNS4_PROTOCOL *This, + IN BOOLEAN DeleteFlag, + IN BOOLEAN Override, + IN EFI_DNS4_CACHE_ENTRY DnsCacheEntry + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + + if (DnsCacheEntry.HostName == NULL || DnsCacheEntry.IpAddress == NULL || DnsCacheEntry.Timeout == 0) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Update Dns4Cache here. + // + Status = UpdateDns4Cache (&mDriverData->Dns4CacheList, DeleteFlag, Override, DnsCacheEntry); + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications to increase the + rate that data packets are moved between the communications device and the transmit + and receive queues. + In some systems, the periodic timer event in the managed network driver may not poll + the underlying communications device fast enough to transmit and/or receive all data + packets without missing incoming packets or dropping outgoing packets. Drivers and + applications that are experiencing packet loss should try calling the Poll() + function more often. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI DNS Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive + queue. Consider increasing the polling rate. +**/ +EFI_STATUS +EFIAPI +Dns4Poll ( + IN EFI_DNS4_PROTOCOL *This + ) +{ + DNS_INSTANCE *Instance; + EFI_UDP4_PROTOCOL *Udp; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL4 (This); + + if (Instance->State == DNS_STATE_UNCONFIGED) { + return EFI_NOT_STARTED; + } else if (Instance->State == DNS_STATE_DESTROY) { + return EFI_DEVICE_ERROR; + } + + Udp = Instance->UdpIo->Protocol.Udp4; + + return Udp->Poll (Udp); +} + +/** + Abort an asynchronous DNS operation, including translation between IP and Host, and + general look up behavior. + + The Cancel() function is used to abort a pending resolution request. After calling + this function, Token.Status will be set to EFI_ABORTED and then Token.Event will be + signaled. If the token is not in one of the queues, which usually means that the + asynchronous operation has completed, this function will not signal the token and + EFI_NOT_FOUND is returned. + + @param[in] This Pointer to EFI_DNS4_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_DNS4_PROTOCOL.HostNameToIp (), + EFI_DNS4_PROTOCOL.IpToHostName() or + EFI_DNS4_PROTOCOL.GeneralLookup(). + If NULL, all pending tokens are aborted. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI DNS4 Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_FOUND When Token is not NULL, and the asynchronous DNS + operation was not found in the transmit queue. It + was either completed or was not issued by + HostNameToIp(), IpToHostName() or GeneralLookup(). +**/ +EFI_STATUS +EFIAPI +Dns4Cancel ( + IN EFI_DNS4_PROTOCOL *This, + IN EFI_DNS4_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + DNS_INSTANCE *Instance; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL4 (This); + + if (Instance->State == DNS_STATE_UNCONFIGED) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Cancle the tokens specified by Token for this instance. + // + Status = Dns4InstanceCancelToken (Instance, Token); + + // + // Dispatch the DPC queued by the NotifyFunction of the canceled token's events. + // + DispatchDpc (); + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Retrieve mode data of this DNS instance. + + This function is used to retrieve DNS mode data for this DNS instance. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[out] DnsModeData Pointer to the caller-allocated storage for the + EFI_DNS6_MODE_DATA data. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED When DnsConfigData is queried, no configuration data + is available because this instance has not been + configured. + @retval EFI_INVALID_PARAMETER This is NULL or DnsModeData is NULL. + @retval EFI_OUT_OF_RESOURCE Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns6GetModeData ( + IN EFI_DNS6_PROTOCOL *This, + OUT EFI_DNS6_MODE_DATA *DnsModeData + ) +{ + DNS_INSTANCE *Instance; + + EFI_TPL OldTpl; + + UINTN Index; + + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + + DNS6_SERVER_IP *ServerItem; + EFI_IPv6_ADDRESS *ServerList; + DNS6_CACHE *CacheItem; + EFI_DNS6_CACHE_ENTRY *CacheList; + EFI_STATUS Status; + + ServerItem = NULL; + ServerList = NULL; + CacheItem = NULL; + CacheList = NULL; + Status = EFI_SUCCESS; + + if ((This == NULL) || (DnsModeData == NULL)) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL6 (This); + if (Instance->State == DNS_STATE_UNCONFIGED) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + ZeroMem (DnsModeData, sizeof (EFI_DNS6_MODE_DATA)); + + // + // Get the current configuration data of this instance. + // + Status = Dns6CopyConfigure (&DnsModeData->DnsConfigData, &Instance->Dns6CfgData); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Get the DnsServerCount and DnsServerList + // + Index = 0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns6ServerList) { + Index++; + } + DnsModeData->DnsServerCount = (UINT32) Index; + ServerList = AllocatePool (sizeof(EFI_IPv6_ADDRESS) * DnsModeData->DnsServerCount); + if (ServerList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + Dns6CleanConfigure (&DnsModeData->DnsConfigData); + goto ON_EXIT; + } + + Index = 0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns6ServerList) { + ServerItem = NET_LIST_USER_STRUCT (Entry, DNS6_SERVER_IP, AllServerLink); + CopyMem (ServerList + Index, &ServerItem->Dns6ServerIp, sizeof (EFI_IPv6_ADDRESS)); + Index++; + } + DnsModeData->DnsServerList = ServerList; + + // + // Get the DnsCacheCount and DnsCacheList + // + Index =0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns6CacheList) { + Index++; + } + DnsModeData->DnsCacheCount = (UINT32) Index; + CacheList = AllocatePool (sizeof(EFI_DNS6_CACHE_ENTRY) * DnsModeData->DnsCacheCount); + if (CacheList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + Dns6CleanConfigure (&DnsModeData->DnsConfigData); + FreePool (ServerList); + goto ON_EXIT; + } + + Index =0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns6CacheList) { + CacheItem = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink); + CopyMem (CacheList + Index, &CacheItem->DnsCache, sizeof (EFI_DNS6_CACHE_ENTRY)); + Index++; + } + DnsModeData->DnsCacheList = CacheList; + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Configure this DNS instance. + + The Configure() function is used to set and change the configuration data for this + EFI DNSv6 Protocol driver instance. Reset the DNS instance if DnsConfigData is NULL. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] DnsConfigData Pointer to the configuration data structure. All associated + storage to be allocated and released by caller. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMTER This is NULL. + The StationIp address provided in DnsConfigData is not zero and not a valid unicast. + DnsServerList is NULL while DnsServerList Count is not ZERO. + DnsServerList Count is ZERO while DnsServerList is not NULL. + @retval EFI_OUT_OF_RESOURCES The DNS instance data or required space could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The + EFI DNSv6 Protocol instance is not configured. + @retval EFI_UNSUPPORTED The designated protocol is not supported. + @retval EFI_ALREADY_STARTED Second call to Configure() with DnsConfigData. To + reconfigure the instance the caller must call Configure() with + NULL first to return driver to unconfigured state. +**/ +EFI_STATUS +EFIAPI +Dns6Configure ( + IN EFI_DNS6_PROTOCOL *This, + IN EFI_DNS6_CONFIG_DATA *DnsConfigData + ) +{ + EFI_STATUS Status; + DNS_INSTANCE *Instance; + + EFI_TPL OldTpl; + + UINT32 ServerListCount; + EFI_IPv6_ADDRESS *ServerList; + + Status = EFI_SUCCESS; + ServerList = NULL; + + if (This == NULL || + (DnsConfigData != NULL && ((DnsConfigData->DnsServerCount != 0 && DnsConfigData->DnsServerList == NULL) || + (DnsConfigData->DnsServerCount == 0 && DnsConfigData->DnsServerList != NULL)))) { + return EFI_INVALID_PARAMETER; + } + + if (DnsConfigData != NULL && DnsConfigData->Protocol != DNS_PROTOCOL_UDP) { + return EFI_UNSUPPORTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL6 (This); + + if (DnsConfigData == NULL) { + ZeroMem (&Instance->SessionDnsServer, sizeof (EFI_IP_ADDRESS)); + + // + // Reset the Instance if ConfigData is NULL + // + if (!NetMapIsEmpty(&Instance->Dns6TxTokens)) { + Dns6InstanceCancelToken(Instance, NULL); + } + + if (Instance->UdpIo != NULL){ + UdpIoCleanIo (Instance->UdpIo); + } + + if (Instance->Dns6CfgData.DnsServerList != NULL) { + FreePool (Instance->Dns6CfgData.DnsServerList); + } + ZeroMem (&Instance->Dns6CfgData, sizeof (EFI_DNS6_CONFIG_DATA)); + + Instance->State = DNS_STATE_UNCONFIGED; + } else { + // + // Configure the parameters for new operation. + // + if (!NetIp6IsUnspecifiedAddr (&DnsConfigData->StationIp) && !NetIp6IsValidUnicast (&DnsConfigData->StationIp)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + Status = Dns6CopyConfigure (&Instance->Dns6CfgData, DnsConfigData); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + if (DnsConfigData->DnsServerCount == 0) { + gBS->RestoreTPL (OldTpl); + + // + //The DNS instance will retrieve DNS server from DHCP Server. + // + Status = GetDns6ServerFromDhcp6 ( + Instance->Service->ImageHandle, + Instance->Service->ControllerHandle, + &ServerListCount, + &ServerList + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + ASSERT(ServerList != NULL); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + CopyMem (&Instance->SessionDnsServer.v6, &ServerList[0], sizeof (EFI_IPv6_ADDRESS)); + } else { + CopyMem (&Instance->SessionDnsServer.v6, &DnsConfigData->DnsServerList[0], sizeof (EFI_IPv6_ADDRESS)); + } + + // + // Config UDP + // + gBS->RestoreTPL (OldTpl); + Status = Dns6ConfigUdp (Instance, Instance->UdpIo); + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + if (EFI_ERROR (Status)) { + if (Instance->Dns6CfgData.DnsServerList != NULL) { + FreePool (Instance->Dns6CfgData.DnsServerList); + Instance->Dns6CfgData.DnsServerList = NULL; + } + goto ON_EXIT; + } + + // + // Add configured DNS server used by this instance to ServerList. + // + Status = AddDns6ServerIp (&mDriverData->Dns6ServerList, Instance->SessionDnsServer.v6); + if (EFI_ERROR (Status)) { + if (Instance->Dns6CfgData.DnsServerList != NULL) { + FreePool (Instance->Dns6CfgData.DnsServerList); + Instance->Dns6CfgData.DnsServerList = NULL; + } + goto ON_EXIT; + } + + Instance->State = DNS_STATE_CONFIGED; + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Host name to host address translation. + + The HostNameToIp () function is used to translate the host name to host IP address. A + type AAAA query is used to get the one or more IPv6 addresses for this host. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] HostName Host name. + @param[in] Token Point to the completion token to translate host name + to host address. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + HostName is NULL or buffer contained unsupported characters. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_ALREADY_STARTED This Token is being used in another DNS session. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns6HostNameToIp ( + IN EFI_DNS6_PROTOCOL *This, + IN CHAR16 *HostName, + IN EFI_DNS6_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + + DNS_INSTANCE *Instance; + + EFI_DNS6_CONFIG_DATA *ConfigData; + + UINTN Index; + DNS6_CACHE *Item; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + + CHAR8 *QueryName; + + DNS6_TOKEN_ENTRY *TokenEntry; + NET_BUF *Packet; + + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + Item = NULL; + QueryName = NULL; + TokenEntry = NULL; + Packet = NULL; + + // + // Validate the parameters + // + if ((This == NULL) || (HostName == NULL) || Token == NULL) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL6 (This); + + ConfigData = &(Instance->Dns6CfgData); + + if (Instance->State != DNS_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + Token->Status = EFI_NOT_READY; + + // + // If zero, use the parameter configured through Dns.Configure() interface. + // + if (Token->RetryCount == 0) { + Token->RetryCount = ConfigData->RetryCount; + } + + // + // If zero, use the parameter configured through Dns.Configure() interface. + // + if (Token->RetryInterval == 0) { + Token->RetryInterval = ConfigData->RetryInterval; + } + + // + // Minimum interval of retry is 2 second. If the retry interval is less than 2 second, then use the 2 second. + // + if (Token->RetryInterval < DNS_DEFAULT_TIMEOUT) { + Token->RetryInterval = DNS_DEFAULT_TIMEOUT; + } + + // + // Check cache + // + if (ConfigData->EnableDnsCache) { + Index = 0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns6CacheList) { + Item = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink); + if (StrCmp (HostName, Item->DnsCache.HostName) == 0) { + Index++; + } + } + + if (Index != 0) { + Token->RspData.H2AData = AllocatePool (sizeof (DNS6_HOST_TO_ADDR_DATA)); + if (Token->RspData.H2AData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Token->RspData.H2AData->IpCount = (UINT32)Index; + Token->RspData.H2AData->IpList = AllocatePool (sizeof (EFI_IPv6_ADDRESS) * Index); + if (Token->RspData.H2AData->IpList == NULL) { + if (Token->RspData.H2AData != NULL) { + FreePool (Token->RspData.H2AData); + } + + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Index = 0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns6CacheList) { + Item = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink); + if ((UINT32)Index < Token->RspData.H2AData->IpCount && StrCmp (HostName, Item->DnsCache.HostName) == 0) { + CopyMem ((Token->RspData.H2AData->IpList) + Index, Item->DnsCache.IpAddress, sizeof (EFI_IPv6_ADDRESS)); + Index++; + } + } + + Token->Status = EFI_SUCCESS; + + if (Token->Event != NULL) { + gBS->SignalEvent (Token->Event); + DispatchDpc (); + } + + Status = Token->Status; + goto ON_EXIT; + } + } + + // + // Construct DNS TokenEntry. + // + TokenEntry = AllocateZeroPool (sizeof (DNS6_TOKEN_ENTRY)); + if (TokenEntry == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + TokenEntry->PacketToLive = Token->RetryInterval; + TokenEntry->Token = Token; + TokenEntry->QueryHostName = AllocateZeroPool (StrSize (HostName)); + if (TokenEntry->QueryHostName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + CopyMem (TokenEntry->QueryHostName, HostName, StrSize (HostName)); + + // + // Construct QName. + // + QueryName = NetLibCreateDnsQName (TokenEntry->QueryHostName); + if (QueryName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Construct DNS Query Packet. + // + Status = ConstructDNSQuery (Instance, QueryName, DNS_TYPE_AAAA, DNS_CLASS_INET, &Packet); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + ASSERT (Packet != NULL); + + // + // Save the token into the Dns6TxTokens map. + // + Status = NetMapInsertTail (&Instance->Dns6TxTokens, TokenEntry, Packet); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Dns Query Ip + // + Status = DoDnsQuery (Instance, Packet); + if (EFI_ERROR (Status)) { + Dns6RemoveTokenEntry (&Instance->Dns6TxTokens, TokenEntry); + } + +ON_EXIT: + + if (EFI_ERROR (Status)) { + if (TokenEntry != NULL) { + if (TokenEntry->QueryHostName != NULL) { + FreePool (TokenEntry->QueryHostName); + } + + FreePool (TokenEntry); + } + + if (Packet != NULL) { + NetbufFree (Packet); + } + } + + if (QueryName != NULL) { + FreePool (QueryName); + } + + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Host address to host name translation. + + The IpToHostName () function is used to translate the host address to host name. A + type PTR query is used to get the primary name of the host. Implementation can choose + to support this function or not. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] IpAddress Ip Address. + @param[in] Token Point to the completion token to translate host + address to host name. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_UNSUPPORTED This function is not supported. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + IpAddress is not valid IP address. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns6IpToHostName ( + IN EFI_DNS6_PROTOCOL *This, + IN EFI_IPv6_ADDRESS IpAddress, + IN EFI_DNS6_COMPLETION_TOKEN *Token + ) +{ + return EFI_UNSUPPORTED; +} + +/** + This function provides capability to retrieve arbitrary information from the DNS + server. + + This GeneralLookup() function retrieves arbitrary information from the DNS. The caller + supplies a QNAME, QTYPE, and QCLASS, and all of the matching RRs are returned. All + RR content (e.g., TTL) was returned. The caller need parse the returned RR to get + required information. The function is optional. Implementation can choose to support + it or not. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] QName Pointer to Query Name. + @param[in] QType Query Type. + @param[in] QClass Query Name. + @param[in] Token Point to the completion token to retrieve arbitrary + information. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_UNSUPPORTED This function is not supported. Or the requested + QType is not supported + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + QName is NULL. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns6GeneralLookUp ( + IN EFI_DNS6_PROTOCOL *This, + IN CHAR8 *QName, + IN UINT16 QType, + IN UINT16 QClass, + IN EFI_DNS6_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + + DNS_INSTANCE *Instance; + + EFI_DNS6_CONFIG_DATA *ConfigData; + + DNS6_TOKEN_ENTRY *TokenEntry; + NET_BUF *Packet; + + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + TokenEntry = NULL; + Packet = NULL; + + // + // Validate the parameters + // + if ((This == NULL) || (QName == NULL) || Token == NULL) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL6 (This); + + ConfigData = &(Instance->Dns6CfgData); + + if (Instance->State != DNS_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + Token->Status = EFI_NOT_READY; + + // + // If zero, use the parameter configured through Dns.Configure() interface. + // + if (Token->RetryCount == 0) { + Token->RetryCount = ConfigData->RetryCount; + } + + // + // If zero, use the parameter configured through Dns.Configure() interface. + // + if (Token->RetryInterval == 0) { + Token->RetryInterval = ConfigData->RetryInterval; + } + + // + // Minimum interval of retry is 2 second. If the retry interval is less than 2 second, then use the 2 second. + // + if (Token->RetryInterval < DNS_DEFAULT_TIMEOUT) { + Token->RetryInterval = DNS_DEFAULT_TIMEOUT; + } + + // + // Construct DNS TokenEntry. + // + TokenEntry = AllocateZeroPool (sizeof(DNS6_TOKEN_ENTRY)); + if (TokenEntry == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + TokenEntry->PacketToLive = Token->RetryInterval; + TokenEntry->GeneralLookUp = TRUE; + TokenEntry->Token = Token; + + // + // Construct DNS Query Packet. + // + Status = ConstructDNSQuery (Instance, QName, QType, QClass, &Packet); + if (EFI_ERROR (Status)) { + if (TokenEntry != NULL) { + FreePool (TokenEntry); + } + + goto ON_EXIT; + } + + ASSERT (Packet != NULL); + + // + // Save the token into the Dns6TxTokens map. + // + Status = NetMapInsertTail (&Instance->Dns6TxTokens, TokenEntry, Packet); + if (EFI_ERROR (Status)) { + if (TokenEntry != NULL) { + FreePool (TokenEntry); + } + + NetbufFree (Packet); + + goto ON_EXIT; + } + + // + // Dns Query Ip + // + Status = DoDnsQuery (Instance, Packet); + if (EFI_ERROR (Status)) { + Dns6RemoveTokenEntry (&Instance->Dns6TxTokens, TokenEntry); + + if (TokenEntry != NULL) { + FreePool (TokenEntry); + } + + NetbufFree (Packet); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + This function is to update the DNS Cache. + + The UpdateDnsCache() function is used to add/delete/modify DNS cache entry. DNS cache + can be normally dynamically updated after the DNS resolve succeeds. This function + provided capability to manually add/delete/modify the DNS cache. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] DeleteFlag If FALSE, this function is to add one entry to the + DNS Cahce. If TRUE, this function will delete + matching DNS Cache entry. + @param[in] Override If TRUE, the maching DNS cache entry will be + overwritten with the supplied parameter. If FALSE, + EFI_ACCESS_DENIED will be returned if the entry to + be added is already existed. + @param[in] DnsCacheEntry Pointer to DNS Cache entry. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + DnsCacheEntry.HostName is NULL. + DnsCacheEntry.IpAddress is NULL. + DnsCacheEntry.Timeout is zero. + @retval EFI_ACCESS_DENIED The DNS cache entry already exists and Override is + not TRUE. + @retval EFI_OUT_OF_RESOURCE Failed to allocate needed resources. +**/ +EFI_STATUS +EFIAPI +Dns6UpdateDnsCache ( + IN EFI_DNS6_PROTOCOL *This, + IN BOOLEAN DeleteFlag, + IN BOOLEAN Override, + IN EFI_DNS6_CACHE_ENTRY DnsCacheEntry + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + + if (DnsCacheEntry.HostName == NULL || DnsCacheEntry.IpAddress == NULL || DnsCacheEntry.Timeout == 0) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Update Dns6Cache here. + // + Status = UpdateDns6Cache (&mDriverData->Dns6CacheList, DeleteFlag, Override, DnsCacheEntry); + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications to increase the + rate that data packets are moved between the communications device and the transmit + and receive queues. + + In some systems, the periodic timer event in the managed network driver may not poll + the underlying communications device fast enough to transmit and/or receive all data + packets without missing incoming packets or dropping outgoing packets. Drivers and + applications that are experiencing packet loss should try calling the Poll() + function more often. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI DNS Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NO_MAPPING There is no source address is available for use. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive + queue. Consider increasing the polling rate. +**/ +EFI_STATUS +EFIAPI +Dns6Poll ( + IN EFI_DNS6_PROTOCOL *This + ) +{ + DNS_INSTANCE *Instance; + EFI_UDP6_PROTOCOL *Udp; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL6 (This); + + if (Instance->State == DNS_STATE_UNCONFIGED) { + return EFI_NOT_STARTED; + } else if (Instance->State == DNS_STATE_DESTROY) { + return EFI_DEVICE_ERROR; + } + + Udp = Instance->UdpIo->Protocol.Udp6; + + return Udp->Poll (Udp); +} + +/** + Abort an asynchronous DNS operation, including translation between IP and Host, and + general look up behavior. + + The Cancel() function is used to abort a pending resolution request. After calling + this function, Token.Status will be set to EFI_ABORTED and then Token.Event will be + signaled. If the token is not in one of the queues, which usually means that the + asynchronous operation has completed, this function will not signal the token and + EFI_NOT_FOUND is returned. + + @param[in] This Pointer to EFI_DNS6_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_DNS6_PROTOCOL.HostNameToIp (), + EFI_DNS6_PROTOCOL.IpToHostName() or + EFI_DNS6_PROTOCOL.GeneralLookup(). + If NULL, all pending tokens are aborted. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI DNS6 Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NO_MAPPING There's no source address is available for use. + @retval EFI_NOT_FOUND When Token is not NULL, and the asynchronous DNS + operation was not found in the transmit queue. It + was either completed or was not issued by + HostNameToIp(), IpToHostName() or GeneralLookup(). +**/ +EFI_STATUS +EFIAPI +Dns6Cancel ( + IN EFI_DNS6_PROTOCOL *This, + IN EFI_DNS6_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + DNS_INSTANCE *Instance; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = DNS_INSTANCE_FROM_THIS_PROTOCOL6 (This); + + if (Instance->State == DNS_STATE_UNCONFIGED) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Cancle the tokens specified by Token for this instance. + // + Status = Dns6InstanceCancelToken (Instance, Token); + + // + // Dispatch the DPC queued by the NotifyFunction of the canceled token's events. + // + DispatchDpc (); + + gBS->RestoreTPL (OldTpl); + + return Status; +} + diff --git a/NetworkPkg/DpcDxe/Dpc.c b/NetworkPkg/DpcDxe/Dpc.c new file mode 100644 index 000000000..8a490949d --- /dev/null +++ b/NetworkPkg/DpcDxe/Dpc.c @@ -0,0 +1,341 @@ +/** @file + +Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +Module Name: + + Dpc.c + +Abstract: + + +**/ + +#include "Dpc.h" + +// +// Handle for the EFI_DPC_PROTOCOL instance +// +EFI_HANDLE mDpcHandle = NULL; + +// +// The EFI_DPC_PROTOCOL instances that is installed onto mDpcHandle +// +EFI_DPC_PROTOCOL mDpc = { + DpcQueueDpc, + DpcDispatchDpc +}; + +// +// Global variables used to meaasure the DPC Queue Depths +// +UINTN mDpcQueueDepth = 0; +UINTN mMaxDpcQueueDepth = 0; + +// +// Free list of DPC entries. As DPCs are queued, entries are removed from this +// free list. As DPC entries are dispatched, DPC entries are added to the free list. +// If the free list is empty and a DPC is queued, the free list is grown by allocating +// an additional set of DPC entries. +// +LIST_ENTRY mDpcEntryFreeList = INITIALIZE_LIST_HEAD_VARIABLE(mDpcEntryFreeList); + +// +// An array of DPC queues. A DPC queue is allocated for every leval EFI_TPL value. +// As DPCs are queued, they are added to the end of the linked list. +// As DPCs are dispatched, they are removed from the beginning of the linked list. +// +LIST_ENTRY mDpcQueue[TPL_HIGH_LEVEL + 1]; + +/** + Add a Deferred Procedure Call to the end of the DPC queue. + + @param This Protocol instance pointer. + @param DpcTpl The EFI_TPL that the DPC should be invoked. + @param DpcProcedure Pointer to the DPC's function. + @param DpcContext Pointer to the DPC's context. Passed to DpcProcedure + when DpcProcedure is invoked. + + @retval EFI_SUCCESS The DPC was queued. + @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL. + @retval EFI_INVALID_PARAMETER DpcProcedure is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to + add the DPC to the queue. + +**/ +EFI_STATUS +EFIAPI +DpcQueueDpc ( + IN EFI_DPC_PROTOCOL *This, + IN EFI_TPL DpcTpl, + IN EFI_DPC_PROCEDURE DpcProcedure, + IN VOID *DpcContext OPTIONAL + ) +{ + EFI_STATUS ReturnStatus; + EFI_TPL OriginalTpl; + DPC_ENTRY *DpcEntry; + UINTN Index; + + // + // Make sure DpcTpl is valid + // + if (DpcTpl < TPL_APPLICATION || DpcTpl > TPL_HIGH_LEVEL) { + return EFI_INVALID_PARAMETER; + } + + // + // Make sure DpcProcedure is valid + // + if (DpcProcedure == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Assume this function will succeed + // + ReturnStatus = EFI_SUCCESS; + + // + // Raise the TPL level to TPL_HIGH_LEVEL for DPC list operation and save the + // current TPL value so it can be restored when this function returns. + // + OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL); + + // + // Check to see if there are any entries in the DPC free list + // + if (IsListEmpty (&mDpcEntryFreeList)) { + // + // If the current TPL is greater than TPL_NOTIFY, then memory allocations + // can not be performed, so the free list can not be expanded. In this case + // return EFI_OUT_OF_RESOURCES. + // + if (OriginalTpl > TPL_NOTIFY) { + ReturnStatus = EFI_OUT_OF_RESOURCES; + goto Done; + } + + // + // Add 64 DPC entries to the free list + // + for (Index = 0; Index < 64; Index++) { + // + // Lower the TPL level to perform a memory allocation + // + gBS->RestoreTPL (OriginalTpl); + + // + // Allocate a new DPC entry + // + DpcEntry = AllocatePool (sizeof (DPC_ENTRY)); + + // + // Raise the TPL level back to TPL_HIGH_LEVEL for DPC list operations + // + gBS->RaiseTPL (TPL_HIGH_LEVEL); + + // + // If the allocation of a DPC entry fails, and the free list is empty, + // then return EFI_OUT_OF_RESOURCES. + // + if (DpcEntry == NULL) { + if (IsListEmpty (&mDpcEntryFreeList)) { + ReturnStatus = EFI_OUT_OF_RESOURCES; + goto Done; + } + } + + // + // Add the newly allocated DPC entry to the DPC free list + // + InsertTailList (&mDpcEntryFreeList, &DpcEntry->ListEntry); + } + } + + // + // Retrieve the first node from the free list of DPCs + // + DpcEntry = (DPC_ENTRY *)(GetFirstNode (&mDpcEntryFreeList)); + + // + // Remove the first node from the free list of DPCs + // + RemoveEntryList (&DpcEntry->ListEntry); + + // + // Fill in the DPC entry with the DpcProcedure and DpcContext + // + DpcEntry->DpcProcedure = DpcProcedure; + DpcEntry->DpcContext = DpcContext; + + // + // Add the DPC entry to the end of the list for the specified DplTpl. + // + InsertTailList (&mDpcQueue[DpcTpl], &DpcEntry->ListEntry); + + // + // Increment the measured DPC queue depth across all TPLs + // + mDpcQueueDepth++; + + // + // Measure the maximum DPC queue depth across all TPLs + // + if (mDpcQueueDepth > mMaxDpcQueueDepth) { + mMaxDpcQueueDepth = mDpcQueueDepth; + } + +Done: + // + // Restore the original TPL level when this function was called + // + gBS->RestoreTPL (OriginalTpl); + + return ReturnStatus; +} + +/** + Dispatch the queue of DPCs. ALL DPCs that have been queued with a DpcTpl + value greater than or equal to the current TPL are invoked in the order that + they were queued. DPCs with higher DpcTpl values are invoked before DPCs with + lower DpcTpl values. + + @param This Protocol instance pointer. + + @retval EFI_SUCCESS One or more DPCs were invoked. + @retval EFI_NOT_FOUND No DPCs were invoked. + +**/ +EFI_STATUS +EFIAPI +DpcDispatchDpc ( + IN EFI_DPC_PROTOCOL *This + ) +{ + EFI_STATUS ReturnStatus; + EFI_TPL OriginalTpl; + EFI_TPL Tpl; + DPC_ENTRY *DpcEntry; + + // + // Assume that no DPCs will be invoked + // + ReturnStatus = EFI_NOT_FOUND; + + // + // Raise the TPL level to TPL_HIGH_LEVEL for DPC list operation and save the + // current TPL value so it can be restored when this function returns. + // + OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL); + + // + // Check to see if there are 1 or more DPCs currently queued + // + if (mDpcQueueDepth > 0) { + // + // Loop from TPL_HIGH_LEVEL down to the current TPL value + // + for (Tpl = TPL_HIGH_LEVEL; Tpl >= OriginalTpl; Tpl--) { + // + // Check to see if the DPC queue is empty + // + while (!IsListEmpty (&mDpcQueue[Tpl])) { + // + // Retrieve the first DPC entry from the DPC queue specified by Tpl + // + DpcEntry = (DPC_ENTRY *)(GetFirstNode (&mDpcQueue[Tpl])); + + // + // Remove the first DPC entry from the DPC queue specified by Tpl + // + RemoveEntryList (&DpcEntry->ListEntry); + + // + // Decrement the measured DPC Queue Depth across all TPLs + // + mDpcQueueDepth--; + + // + // Lower the TPL to TPL value of the current DPC queue + // + gBS->RestoreTPL (Tpl); + + // + // Invoke the DPC passing in its context + // + (DpcEntry->DpcProcedure) (DpcEntry->DpcContext); + + // + // At least one DPC has been invoked, so set the return status to EFI_SUCCESS + // + ReturnStatus = EFI_SUCCESS; + + // + // Raise the TPL level back to TPL_HIGH_LEVEL for DPC list operations + // + gBS->RaiseTPL (TPL_HIGH_LEVEL); + + // + // Add the invoked DPC entry to the DPC free list + // + InsertTailList (&mDpcEntryFreeList, &DpcEntry->ListEntry); + } + } + } + + // + // Restore the original TPL level when this function was called + // + gBS->RestoreTPL (OriginalTpl); + + return ReturnStatus; +} + +/** + The entry point for DPC driver which installs the EFI_DPC_PROTOCOL onto a new handle. + + @param ImageHandle The image handle of the driver. + @param SystemTable The system table. + + @retval EFI_SUCCES The DPC queues were initialized and the EFI_DPC_PROTOCOL was + installed onto a new handle. + @retval Others Failed to install EFI_DPC_PROTOCOL. + +**/ +EFI_STATUS +EFIAPI +DpcDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + UINTN Index; + + // + // ASSERT() if the EFI_DPC_PROTOCOL is already present in the handle database + // + ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiDpcProtocolGuid); + + // + // Initialize the DPC queue for all possible TPL values + // + for (Index = 0; Index <= TPL_HIGH_LEVEL; Index++) { + InitializeListHead (&mDpcQueue[Index]); + } + + // + // Install the EFI_DPC_PROTOCOL instance onto a new handle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &mDpcHandle, + &gEfiDpcProtocolGuid, + &mDpc, + NULL + ); + ASSERT_EFI_ERROR (Status); + + return Status; +} diff --git a/NetworkPkg/DpcDxe/Dpc.h b/NetworkPkg/DpcDxe/Dpc.h new file mode 100644 index 000000000..2017a215e --- /dev/null +++ b/NetworkPkg/DpcDxe/Dpc.h @@ -0,0 +1,80 @@ +/** @file + +Copyright (c) 2007, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +Module Name: + + Dpc.h + +Abstract: + + +**/ + +#ifndef _DPC_H_ +#define _DPC_H_ + +#include +#include +#include +#include +#include +#include +#include + +// +// Internal data struture for managing DPCs. A DPC entry is either on the free +// list or on a DPC queue at a specific EFI_TPL. +// +typedef struct { + LIST_ENTRY ListEntry; + EFI_DPC_PROCEDURE DpcProcedure; + VOID *DpcContext; +} DPC_ENTRY; + +/** + Add a Deferred Procedure Call to the end of the DPC queue. + + @param This Protocol instance pointer. + @param DpcTpl The EFI_TPL that the DPC should be invoked. + @param DpcProcedure Pointer to the DPC's function. + @param DpcContext Pointer to the DPC's context. Passed to DpcProcedure + when DpcProcedure is invoked. + + @retval EFI_SUCCESS The DPC was queued. + @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL. + @retval EFI_INVALID_PARAMETER DpcProcedure is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to + add the DPC to the queue. + +**/ +EFI_STATUS +EFIAPI +DpcQueueDpc ( + IN EFI_DPC_PROTOCOL *This, + IN EFI_TPL DpcTpl, + IN EFI_DPC_PROCEDURE DpcProcedure, + IN VOID *DpcContext OPTIONAL + ); + +/** + Dispatch the queue of DPCs. ALL DPCs that have been queued with a DpcTpl + value greater than or equal to the current TPL are invoked in the order that + they were queued. DPCs with higher DpcTpl values are invoked before DPCs with + lower DpcTpl values. + + @param This Protocol instance pointer. + + @retval EFI_SUCCESS One or more DPCs were invoked. + @retval EFI_NOT_FOUND No DPCs were invoked. + +**/ +EFI_STATUS +EFIAPI +DpcDispatchDpc ( + IN EFI_DPC_PROTOCOL *This + ); + +#endif + diff --git a/NetworkPkg/DpcDxe/DpcDxe.inf b/NetworkPkg/DpcDxe/DpcDxe.inf new file mode 100644 index 000000000..716a04d98 --- /dev/null +++ b/NetworkPkg/DpcDxe/DpcDxe.inf @@ -0,0 +1,46 @@ +## @file +# This module produces Deferred Procedure Call Protocol. +# +# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = DpcDxe + MODULE_UNI_FILE = DpcDxe.uni + FILE_GUID = A210F973-229D-4f4d-AA37-9895E6C9EABA + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = DpcDriverEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources] + Dpc.c + Dpc.h + +[Packages] + MdePkg/MdePkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + UefiDriverEntryPoint + BaseLib + DebugLib + UefiBootServicesTableLib + MemoryAllocationLib + +[Protocols] + gEfiDpcProtocolGuid ## PRODUCES + +[Depex] + TRUE +[UserExtensions.TianoCore."ExtraFiles"] + DpcDxeExtra.uni diff --git a/NetworkPkg/DpcDxe/DpcDxe.uni b/NetworkPkg/DpcDxe/DpcDxe.uni new file mode 100644 index 000000000..34ad24b25 --- /dev/null +++ b/NetworkPkg/DpcDxe/DpcDxe.uni @@ -0,0 +1,16 @@ +// /** @file +// This module produces Deferred Procedure Call Protocol. +// +// This module produces Deferred Procedure Call Protocol. +// +// Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Produces Deferred Procedure Call Protocol" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces Deferred Procedure Call Protocol." + diff --git a/NetworkPkg/DpcDxe/DpcDxeExtra.uni b/NetworkPkg/DpcDxe/DpcDxeExtra.uni new file mode 100644 index 000000000..82a93190d --- /dev/null +++ b/NetworkPkg/DpcDxe/DpcDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// DpcDxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"Deferred Procedure Call DXE Driver" + + diff --git a/NetworkPkg/HttpBootDxe/HttpBootClient.c b/NetworkPkg/HttpBootDxe/HttpBootClient.c new file mode 100644 index 000000000..30ac15889 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootClient.c @@ -0,0 +1,1306 @@ +/** @file + Implementation of the boot file download function. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpBootDxe.h" + +/** + Update the device path node to include the boot resource information. + + @param[in] Private The pointer to the driver's private data. + + @retval EFI_SUCCESS Device patch successfully updated. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + @retval Others Unexpected error happened. + +**/ +EFI_STATUS +HttpBootUpdateDevicePath ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + EFI_DEV_PATH *Node; + EFI_DEVICE_PATH_PROTOCOL *TmpIpDevicePath; + EFI_DEVICE_PATH_PROTOCOL *TmpDnsDevicePath; + EFI_DEVICE_PATH_PROTOCOL *NewDevicePath; + UINTN Length; + EFI_STATUS Status; + + TmpIpDevicePath = NULL; + TmpDnsDevicePath = NULL; + + // + // Update the IP node with DHCP assigned information. + // + if (!Private->UsingIpv6) { + Node = AllocateZeroPool (sizeof (IPv4_DEVICE_PATH)); + if (Node == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Node->Ipv4.Header.Type = MESSAGING_DEVICE_PATH; + Node->Ipv4.Header.SubType = MSG_IPv4_DP; + SetDevicePathNodeLength (Node, sizeof (IPv4_DEVICE_PATH)); + CopyMem (&Node->Ipv4.LocalIpAddress, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS)); + Node->Ipv4.RemotePort = Private->Port; + Node->Ipv4.Protocol = EFI_IP_PROTO_TCP; + Node->Ipv4.StaticIpAddress = FALSE; + CopyMem (&Node->Ipv4.GatewayIpAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Node->Ipv4.SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + } else { + Node = AllocateZeroPool (sizeof (IPv6_DEVICE_PATH)); + if (Node == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Node->Ipv6.Header.Type = MESSAGING_DEVICE_PATH; + Node->Ipv6.Header.SubType = MSG_IPv6_DP; + SetDevicePathNodeLength (Node, sizeof (IPv6_DEVICE_PATH)); + Node->Ipv6.PrefixLength = IP6_PREFIX_LENGTH; + Node->Ipv6.RemotePort = Private->Port; + Node->Ipv6.Protocol = EFI_IP_PROTO_TCP; + Node->Ipv6.IpAddressOrigin = 0; + CopyMem (&Node->Ipv6.LocalIpAddress, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS)); + CopyMem (&Node->Ipv6.RemoteIpAddress, &Private->ServerIp.v6, sizeof (EFI_IPv6_ADDRESS)); + CopyMem (&Node->Ipv6.GatewayIpAddress, &Private->GatewayIp.v6, sizeof (EFI_IPv6_ADDRESS)); + } + + TmpIpDevicePath = AppendDevicePathNode (Private->ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (Node); + if (TmpIpDevicePath == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Update the DNS node with DNS server IP list if existed. + // + if (Private->DnsServerIp != NULL) { + Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + sizeof (Node->Dns.IsIPv6) + Private->DnsServerCount * sizeof (EFI_IP_ADDRESS); + Node = AllocatePool (Length); + if (Node == NULL) { + FreePool (TmpIpDevicePath); + return EFI_OUT_OF_RESOURCES; + } + Node->DevPath.Type = MESSAGING_DEVICE_PATH; + Node->DevPath.SubType = MSG_DNS_DP; + SetDevicePathNodeLength (Node, Length); + Node->Dns.IsIPv6 = Private->UsingIpv6 ? 0x01 : 0x00; + CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL) + sizeof (Node->Dns.IsIPv6), Private->DnsServerIp, Private->DnsServerCount * sizeof (EFI_IP_ADDRESS)); + + TmpDnsDevicePath = AppendDevicePathNode (TmpIpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (Node); + FreePool (TmpIpDevicePath); + TmpIpDevicePath = NULL; + if (TmpDnsDevicePath == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + + // + // Update the URI node with the boot file URI. + // + Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + AsciiStrSize (Private->BootFileUri); + Node = AllocatePool (Length); + if (Node == NULL) { + if (TmpIpDevicePath != NULL) { + FreePool (TmpIpDevicePath); + } + if (TmpDnsDevicePath != NULL) { + FreePool (TmpDnsDevicePath); + } + return EFI_OUT_OF_RESOURCES; + } + Node->DevPath.Type = MESSAGING_DEVICE_PATH; + Node->DevPath.SubType = MSG_URI_DP; + SetDevicePathNodeLength (Node, Length); + CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL), Private->BootFileUri, AsciiStrSize (Private->BootFileUri)); + + if (TmpDnsDevicePath != NULL) { + NewDevicePath = AppendDevicePathNode (TmpDnsDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (TmpDnsDevicePath); + } else { + ASSERT (TmpIpDevicePath != NULL); + NewDevicePath = AppendDevicePathNode (TmpIpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (TmpIpDevicePath); + } + FreePool (Node); + if (NewDevicePath == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if (!Private->UsingIpv6) { + // + // Reinstall the device path protocol of the child handle. + // + Status = gBS->ReinstallProtocolInterface ( + Private->Ip4Nic->Controller, + &gEfiDevicePathProtocolGuid, + Private->Ip4Nic->DevicePath, + NewDevicePath + ); + if (EFI_ERROR (Status)) { + return Status; + } + + FreePool (Private->Ip4Nic->DevicePath); + Private->Ip4Nic->DevicePath = NewDevicePath; + } else { + // + // Reinstall the device path protocol of the child handle. + // + Status = gBS->ReinstallProtocolInterface ( + Private->Ip6Nic->Controller, + &gEfiDevicePathProtocolGuid, + Private->Ip6Nic->DevicePath, + NewDevicePath + ); + if (EFI_ERROR (Status)) { + return Status; + } + FreePool (Private->Ip6Nic->DevicePath); + Private->Ip6Nic->DevicePath = NewDevicePath; + } + + return EFI_SUCCESS; +} + +/** + Parse the boot file URI information from the selected Dhcp4 offer packet. + + @param[in] Private The pointer to the driver's private data. + + @retval EFI_SUCCESS Successfully parsed out all the boot information. + @retval Others Failed to parse out the boot information. + +**/ +EFI_STATUS +HttpBootDhcp4ExtractUriInfo ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + HTTP_BOOT_DHCP4_PACKET_CACHE *SelectOffer; + HTTP_BOOT_DHCP4_PACKET_CACHE *HttpOffer; + UINT32 SelectIndex; + UINT32 ProxyIndex; + UINT32 DnsServerIndex; + EFI_DHCP4_PACKET_OPTION *Option; + EFI_STATUS Status; + + ASSERT (Private != NULL); + ASSERT (Private->SelectIndex != 0); + SelectIndex = Private->SelectIndex - 1; + ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM); + + DnsServerIndex = 0; + + Status = EFI_SUCCESS; + + // + // SelectOffer contains the IP address configuration and name server configuration. + // HttpOffer contains the boot file URL. + // + SelectOffer = &Private->OfferBuffer[SelectIndex].Dhcp4; + if (Private->FilePathUri == NULL) { + // + // In Corporate environment, we need a HttpOffer. + // + if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) || + (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns) || + (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) { + HttpOffer = SelectOffer; + } else { + ASSERT (Private->SelectProxyType != HttpOfferTypeMax); + ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0]; + HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp4; + } + Private->BootFileUriParser = HttpOffer->UriParser; + Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data; + } else { + // + // In Home environment the BootFileUri comes from the FilePath. + // + Private->BootFileUriParser = Private->FilePathUriParser; + Private->BootFileUri = Private->FilePathUri; + } + + // + // Check the URI scheme. + // + Status = HttpBootCheckUriScheme (Private->BootFileUri); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "HttpBootDhcp4ExtractUriInfo: %r.\n", Status)); + if (Status == EFI_INVALID_PARAMETER) { + AsciiPrint ("\n Error: Invalid URI address.\n"); + } else if (Status == EFI_ACCESS_DENIED) { + AsciiPrint ("\n Error: Access forbidden, only HTTPS connection is allowed.\n"); + } + return Status; + } + + if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || + (SelectOffer->OfferType == HttpOfferTypeDhcpDns) || + (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) { + Option = SelectOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER]; + ASSERT (Option != NULL); + + // + // Record the Dns Server address list. + // + Private->DnsServerCount = (Option->Length) / sizeof (EFI_IPv4_ADDRESS); + + Private->DnsServerIp = AllocateZeroPool (Private->DnsServerCount * sizeof (EFI_IP_ADDRESS)); + if (Private->DnsServerIp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + for (DnsServerIndex = 0; DnsServerIndex < Private->DnsServerCount; DnsServerIndex++) { + CopyMem (&(Private->DnsServerIp[DnsServerIndex].v4), &(((EFI_IPv4_ADDRESS *) Option->Data)[DnsServerIndex]), sizeof (EFI_IPv4_ADDRESS)); + } + + // + // Configure the default DNS server if server assigned. + // + Status = HttpBootRegisterIp4Dns ( + Private, + Option->Length, + Option->Data + ); + if (EFI_ERROR (Status)) { + FreePool (Private->DnsServerIp); + Private->DnsServerIp = NULL; + return Status; + } + } + + // + // Extract the port from URL, and use default HTTP port 80 if not provided. + // + Status = HttpUrlGetPort ( + Private->BootFileUri, + Private->BootFileUriParser, + &Private->Port + ); + if (EFI_ERROR (Status) || Private->Port == 0) { + Private->Port = 80; + } + + // + // All boot informations are valid here. + // + + // + // Update the device path to include the boot resource information. + // + Status = HttpBootUpdateDevicePath (Private); + if (EFI_ERROR (Status) && Private->DnsServerIp != NULL) { + FreePool (Private->DnsServerIp); + Private->DnsServerIp = NULL; + } + + return Status; +} + +/** + Parse the boot file URI information from the selected Dhcp6 offer packet. + + @param[in] Private The pointer to the driver's private data. + + @retval EFI_SUCCESS Successfully parsed out all the boot information. + @retval Others Failed to parse out the boot information. + +**/ +EFI_STATUS +HttpBootDhcp6ExtractUriInfo ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + HTTP_BOOT_DHCP6_PACKET_CACHE *SelectOffer; + HTTP_BOOT_DHCP6_PACKET_CACHE *HttpOffer; + UINT32 SelectIndex; + UINT32 ProxyIndex; + UINT32 DnsServerIndex; + EFI_DHCP6_PACKET_OPTION *Option; + EFI_IPv6_ADDRESS IpAddr; + CHAR8 *HostName; + UINTN HostNameSize; + CHAR16 *HostNameStr; + EFI_STATUS Status; + + ASSERT (Private != NULL); + ASSERT (Private->SelectIndex != 0); + SelectIndex = Private->SelectIndex - 1; + ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM); + + DnsServerIndex = 0; + + Status = EFI_SUCCESS; + HostName = NULL; + // + // SelectOffer contains the IP address configuration and name server configuration. + // HttpOffer contains the boot file URL. + // + SelectOffer = &Private->OfferBuffer[SelectIndex].Dhcp6; + if (Private->FilePathUri == NULL) { + // + // In Corporate environment, we need a HttpOffer. + // + if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) || + (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns) || + (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) { + HttpOffer = SelectOffer; + } else { + ASSERT (Private->SelectProxyType != HttpOfferTypeMax); + ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0]; + HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp6; + } + Private->BootFileUriParser = HttpOffer->UriParser; + Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data; + } else { + // + // In Home environment the BootFileUri comes from the FilePath. + // + Private->BootFileUriParser = Private->FilePathUriParser; + Private->BootFileUri = Private->FilePathUri; + } + + // + // Check the URI scheme. + // + Status = HttpBootCheckUriScheme (Private->BootFileUri); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "HttpBootDhcp6ExtractUriInfo: %r.\n", Status)); + if (Status == EFI_INVALID_PARAMETER) { + AsciiPrint ("\n Error: Invalid URI address.\n"); + } else if (Status == EFI_ACCESS_DENIED) { + AsciiPrint ("\n Error: Access forbidden, only HTTPS connection is allowed.\n"); + } + return Status; + } + + // + // Set the Local station address to IP layer. + // + Status = HttpBootSetIp6Address (Private); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Register the IPv6 gateway address to the network device. + // + Status = HttpBootSetIp6Gateway (Private); + if (EFI_ERROR (Status)) { + return Status; + } + + if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || + (SelectOffer->OfferType == HttpOfferTypeDhcpDns) || + (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) { + Option = SelectOffer->OptList[HTTP_BOOT_DHCP6_IDX_DNS_SERVER]; + ASSERT (Option != NULL); + + // + // Record the Dns Server address list. + // + Private->DnsServerCount = HTONS (Option->OpLen) / sizeof (EFI_IPv6_ADDRESS); + + Private->DnsServerIp = AllocateZeroPool (Private->DnsServerCount * sizeof (EFI_IP_ADDRESS)); + if (Private->DnsServerIp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + for (DnsServerIndex = 0; DnsServerIndex < Private->DnsServerCount; DnsServerIndex++) { + CopyMem (&(Private->DnsServerIp[DnsServerIndex].v6), &(((EFI_IPv6_ADDRESS *) Option->Data)[DnsServerIndex]), sizeof (EFI_IPv6_ADDRESS)); + } + + // + // Configure the default DNS server if server assigned. + // + Status = HttpBootSetIp6Dns ( + Private, + HTONS (Option->OpLen), + Option->Data + ); + if (EFI_ERROR (Status)) { + goto Error; + } + } + + // + // Extract the HTTP server Ip from URL. This is used to Check route table + // whether can send message to HTTP Server Ip through the GateWay. + // + Status = HttpUrlGetIp6 ( + Private->BootFileUri, + Private->BootFileUriParser, + &IpAddr + ); + + if (EFI_ERROR (Status)) { + // + // The Http server address is expressed by Name Ip, so perform DNS resolution + // + Status = HttpUrlGetHostName ( + Private->BootFileUri, + Private->BootFileUriParser, + &HostName + ); + if (EFI_ERROR (Status)) { + goto Error; + } + + HostNameSize = AsciiStrSize (HostName); + HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16)); + if (HostNameStr == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize); + + if (HostName != NULL) { + FreePool (HostName); + } + + Status = HttpBootDns (Private, HostNameStr, &IpAddr); + FreePool (HostNameStr); + if (EFI_ERROR (Status)) { + AsciiPrint ("\n Error: Could not retrieve the host address from DNS server.\n"); + goto Error; + } + } + + CopyMem (&Private->ServerIp.v6, &IpAddr, sizeof (EFI_IPv6_ADDRESS)); + + // + // Extract the port from URL, and use default HTTP port 80 if not provided. + // + Status = HttpUrlGetPort ( + Private->BootFileUri, + Private->BootFileUriParser, + &Private->Port + ); + if (EFI_ERROR (Status) || Private->Port == 0) { + Private->Port = 80; + } + + // + // All boot informations are valid here. + // + + // + // Update the device path to include the boot resource information. + // + Status = HttpBootUpdateDevicePath (Private); + if (EFI_ERROR (Status)) { + goto Error; + } + + return Status; + +Error: + if (Private->DnsServerIp != NULL) { + FreePool (Private->DnsServerIp); + Private->DnsServerIp = NULL; + } + + return Status; +} + + +/** + Discover all the boot information for boot file. + + @param[in, out] Private The pointer to the driver's private data. + + @retval EFI_SUCCESS Successfully obtained all the boot information . + @retval Others Failed to retrieve the boot information. + +**/ +EFI_STATUS +HttpBootDiscoverBootInfo ( + IN OUT HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + EFI_STATUS Status; + + // + // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and + // other Http boot information. + // + Status = HttpBootDhcp (Private); + if (EFI_ERROR (Status)) { + return Status; + } + + if (!Private->UsingIpv6) { + Status = HttpBootDhcp4ExtractUriInfo (Private); + } else { + Status = HttpBootDhcp6ExtractUriInfo (Private); + } + + return Status; +} + +/** + HttpIo Callback function which will be invoked when specified HTTP_IO_CALLBACK_EVENT happened. + + @param[in] EventType Indicate the Event type that occurs in the current callback. + @param[in] Message HTTP message which will be send to, or just received from HTTP server. + @param[in] Context The Callback Context pointer. + + @retval EFI_SUCCESS Tells the HttpIo to continue the HTTP process. + @retval Others Tells the HttpIo to abort the current HTTP process. +**/ +EFI_STATUS +EFIAPI +HttpBootHttpIoCallback ( + IN HTTP_IO_CALLBACK_EVENT EventType, + IN EFI_HTTP_MESSAGE *Message, + IN VOID *Context + ) +{ + HTTP_BOOT_PRIVATE_DATA *Private; + EFI_STATUS Status; + Private = (HTTP_BOOT_PRIVATE_DATA *) Context; + if (Private->HttpBootCallback != NULL) { + Status = Private->HttpBootCallback->Callback ( + Private->HttpBootCallback, + EventType == HttpIoRequest ? HttpBootHttpRequest : HttpBootHttpResponse, + EventType == HttpIoRequest ? FALSE : TRUE, + sizeof (EFI_HTTP_MESSAGE), + (VOID *) Message + ); + return Status; + } + return EFI_SUCCESS; +} + +/** + Create a HttpIo instance for the file download. + + @param[in] Private The pointer to the driver's private data. + + @retval EFI_SUCCESS Successfully created. + @retval Others Failed to create HttpIo. + +**/ +EFI_STATUS +HttpBootCreateHttpIo ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + HTTP_IO_CONFIG_DATA ConfigData; + EFI_STATUS Status; + EFI_HANDLE ImageHandle; + + ASSERT (Private != NULL); + + ZeroMem (&ConfigData, sizeof (HTTP_IO_CONFIG_DATA)); + if (!Private->UsingIpv6) { + ConfigData.Config4.HttpVersion = HttpVersion11; + ConfigData.Config4.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT; + IP4_COPY_ADDRESS (&ConfigData.Config4.LocalIp, &Private->StationIp.v4); + IP4_COPY_ADDRESS (&ConfigData.Config4.SubnetMask, &Private->SubnetMask.v4); + ImageHandle = Private->Ip4Nic->ImageHandle; + } else { + ConfigData.Config6.HttpVersion = HttpVersion11; + ConfigData.Config6.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT; + IP6_COPY_ADDRESS (&ConfigData.Config6.LocalIp, &Private->StationIp.v6); + ImageHandle = Private->Ip6Nic->ImageHandle; + } + + Status = HttpIoCreateIo ( + ImageHandle, + Private->Controller, + Private->UsingIpv6 ? IP_VERSION_6 : IP_VERSION_4, + &ConfigData, + HttpBootHttpIoCallback, + (VOID *) Private, + &Private->HttpIo + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Private->HttpCreated = TRUE; + return EFI_SUCCESS; +} + +/** + Release all the resource of a cache item. + + @param[in] Cache The pointer to the cache item. + +**/ +VOID +HttpBootFreeCache ( + IN HTTP_BOOT_CACHE_CONTENT *Cache + ) +{ + UINTN Index; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + HTTP_BOOT_ENTITY_DATA *EntityData; + + if (Cache != NULL) { + // + // Free the request data + // + if (Cache->RequestData != NULL) { + if (Cache->RequestData->Url != NULL) { + FreePool (Cache->RequestData->Url); + } + FreePool (Cache->RequestData); + } + + // + // Free the response header + // + if (Cache->ResponseData != NULL) { + if (Cache->ResponseData->Headers != NULL) { + for (Index = 0; Index < Cache->ResponseData->HeaderCount; Index++) { + FreePool (Cache->ResponseData->Headers[Index].FieldName); + FreePool (Cache->ResponseData->Headers[Index].FieldValue); + } + FreePool (Cache->ResponseData->Headers); + } + } + + // + // Free the response body + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Cache->EntityDataList) { + EntityData = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_ENTITY_DATA, Link); + if (EntityData->Block != NULL) { + FreePool (EntityData->Block); + } + RemoveEntryList (&EntityData->Link); + FreePool (EntityData); + } + + FreePool (Cache); + } +} + +/** + Clean up all cached data. + + @param[in] Private The pointer to the driver's private data. + +**/ +VOID +HttpBootFreeCacheList ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + HTTP_BOOT_CACHE_CONTENT *Cache; + + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->CacheList) { + Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link); + RemoveEntryList (&Cache->Link); + HttpBootFreeCache (Cache); + } +} + +/** + Get the file content from cached data. + + @param[in] Private The pointer to the driver's private data. + @param[in] Uri Uri of the file to be retrieved from cache. + @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return + code of EFI_SUCCESS, the amount of data transferred to + Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL, + the size of Buffer required to retrieve the requested file. + @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL, + then the size of the requested file is returned in + BufferSize. + @param[out] ImageType The image type of the downloaded file. + + @retval EFI_SUCCESS Successfully created. + @retval Others Failed to create HttpIo. + +**/ +EFI_STATUS +HttpBootGetFileFromCache ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN CHAR16 *Uri, + IN OUT UINTN *BufferSize, + OUT UINT8 *Buffer, + OUT HTTP_BOOT_IMAGE_TYPE *ImageType + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Entry2; + HTTP_BOOT_CACHE_CONTENT *Cache; + HTTP_BOOT_ENTITY_DATA *EntityData; + UINTN CopyedSize; + + if (Uri == NULL || BufferSize == NULL || Buffer == NULL || ImageType == NULL) { + return EFI_INVALID_PARAMETER; + } + + NET_LIST_FOR_EACH (Entry, &Private->CacheList) { + Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link); + // + // Compare the URI to see whether we already have a cache for this file. + // + if ((Cache->RequestData != NULL) && + (Cache->RequestData->Url != NULL) && + (StrCmp (Uri, Cache->RequestData->Url) == 0)) { + // + // Hit in cache, record image type. + // + *ImageType = Cache->ImageType; + + // + // Check buffer size. + // + if (*BufferSize < Cache->EntityLength) { + *BufferSize = Cache->EntityLength; + return EFI_BUFFER_TOO_SMALL; + } + + // + // Fill data to buffer. + // + CopyedSize = 0; + NET_LIST_FOR_EACH (Entry2, &Cache->EntityDataList) { + EntityData = NET_LIST_USER_STRUCT (Entry2, HTTP_BOOT_ENTITY_DATA, Link); + if (*BufferSize > CopyedSize) { + CopyMem ( + Buffer + CopyedSize, + EntityData->DataStart, + MIN (EntityData->DataLength, *BufferSize - CopyedSize) + ); + CopyedSize += MIN (EntityData->DataLength, *BufferSize - CopyedSize); + } + } + *BufferSize = CopyedSize; + return EFI_SUCCESS; + } + } + + return EFI_NOT_FOUND; +} + +/** + A callback function to intercept events during message parser. + + This function will be invoked during HttpParseMessageBody() with various events type. An error + return status of the callback function will cause the HttpParseMessageBody() aborted. + + @param[in] EventType Event type of this callback call. + @param[in] Data A pointer to data buffer. + @param[in] Length Length in bytes of the Data. + @param[in] Context Callback context set by HttpInitMsgParser(). + + @retval EFI_SUCCESS Continue to parser the message body. + @retval Others Abort the parse. + +**/ +EFI_STATUS +EFIAPI +HttpBootGetBootFileCallback ( + IN HTTP_BODY_PARSE_EVENT EventType, + IN CHAR8 *Data, + IN UINTN Length, + IN VOID *Context + ) +{ + HTTP_BOOT_CALLBACK_DATA *CallbackData; + HTTP_BOOT_ENTITY_DATA *NewEntityData; + EFI_STATUS Status; + EFI_HTTP_BOOT_CALLBACK_PROTOCOL *HttpBootCallback; + + // + // We only care about the entity data. + // + if (EventType != BodyParseEventOnData) { + return EFI_SUCCESS; + } + + CallbackData = (HTTP_BOOT_CALLBACK_DATA *) Context; + HttpBootCallback = CallbackData->Private->HttpBootCallback; + if (HttpBootCallback != NULL) { + Status = HttpBootCallback->Callback ( + HttpBootCallback, + HttpBootHttpEntityBody, + TRUE, + (UINT32)Length, + Data + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + // + // Copy data if caller has provided a buffer. + // + if (CallbackData->BufferSize > CallbackData->CopyedSize) { + CopyMem ( + CallbackData->Buffer + CallbackData->CopyedSize, + Data, + MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize) + ); + CallbackData->CopyedSize += MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize); + } + + // + // The caller doesn't provide a buffer, save the block into cache list. + // + if (CallbackData->Cache != NULL) { + NewEntityData = AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA)); + if (NewEntityData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + if (CallbackData->NewBlock) { + NewEntityData->Block = CallbackData->Block; + CallbackData->Block = NULL; + } + NewEntityData->DataLength = Length; + NewEntityData->DataStart = (UINT8*) Data; + InsertTailList (&CallbackData->Cache->EntityDataList, &NewEntityData->Link); + } + return EFI_SUCCESS; +} + +/** + This function download the boot file by using UEFI HTTP protocol. + + @param[in] Private The pointer to the driver's private data. + @param[in] HeaderOnly Only request the response header, it could save a lot of time if + the caller only want to know the size of the requested file. + @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return + code of EFI_SUCCESS, the amount of data transferred to + Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL, + the size of Buffer required to retrieve the requested file. + @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL, + then the size of the requested file is returned in + BufferSize. + @param[out] ImageType The image type of the downloaded file. + + @retval EFI_SUCCESS The file was loaded. + @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources + @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry. + BufferSize has been updated with the size needed to complete + the request. + @retval Others Unexpected error happened. + +**/ +EFI_STATUS +HttpBootGetBootFile ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN BOOLEAN HeaderOnly, + IN OUT UINTN *BufferSize, + OUT UINT8 *Buffer, + OUT HTTP_BOOT_IMAGE_TYPE *ImageType + ) +{ + EFI_STATUS Status; + EFI_HTTP_STATUS_CODE StatusCode; + CHAR8 *HostName; + EFI_HTTP_REQUEST_DATA *RequestData; + HTTP_IO_RESPONSE_DATA *ResponseData; + HTTP_IO_RESPONSE_DATA ResponseBody; + HTTP_IO *HttpIo; + HTTP_IO_HEADER *HttpIoHeader; + VOID *Parser; + HTTP_BOOT_CALLBACK_DATA Context; + UINTN ContentLength; + HTTP_BOOT_CACHE_CONTENT *Cache; + UINT8 *Block; + UINTN UrlSize; + CHAR16 *Url; + BOOLEAN IdentityMode; + UINTN ReceivedSize; + + ASSERT (Private != NULL); + ASSERT (Private->HttpCreated); + + if (BufferSize == NULL || ImageType == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (*BufferSize != 0 && Buffer == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // First, check whether we already cached the requested Uri. + // + UrlSize = AsciiStrSize (Private->BootFileUri); + Url = AllocatePool (UrlSize * sizeof (CHAR16)); + if (Url == NULL) { + return EFI_OUT_OF_RESOURCES; + } + AsciiStrToUnicodeStrS (Private->BootFileUri, Url, UrlSize); + if (!HeaderOnly && Buffer != NULL) { + Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer, ImageType); + if (Status != EFI_NOT_FOUND) { + FreePool (Url); + return Status; + } + } + + // + // Not found in cache, try to download it through HTTP. + // + + // + // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer. + // + Cache = NULL; + if ((!HeaderOnly) && (*BufferSize == 0)) { + Cache = AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT)); + if (Cache == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR_1; + } + Cache->ImageType = ImageTypeMax; + InitializeListHead (&Cache->EntityDataList); + } + + // + // 2. Send HTTP request message. + // + + // + // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file: + // Host + // Accept + // User-Agent + // + HttpIoHeader = HttpBootCreateHeader (3); + if (HttpIoHeader == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR_2; + } + + // + // Add HTTP header field 1: Host + // + HostName = NULL; + Status = HttpUrlGetHostName ( + Private->BootFileUri, + Private->BootFileUriParser, + &HostName + ); + if (EFI_ERROR (Status)) { + goto ERROR_3; + } + Status = HttpBootSetHeader ( + HttpIoHeader, + HTTP_HEADER_HOST, + HostName + ); + FreePool (HostName); + if (EFI_ERROR (Status)) { + goto ERROR_3; + } + + // + // Add HTTP header field 2: Accept + // + Status = HttpBootSetHeader ( + HttpIoHeader, + HTTP_HEADER_ACCEPT, + "*/*" + ); + if (EFI_ERROR (Status)) { + goto ERROR_3; + } + + // + // Add HTTP header field 3: User-Agent + // + Status = HttpBootSetHeader ( + HttpIoHeader, + HTTP_HEADER_USER_AGENT, + HTTP_USER_AGENT_EFI_HTTP_BOOT + ); + if (EFI_ERROR (Status)) { + goto ERROR_3; + } + + // + // 2.2 Build the rest of HTTP request info. + // + RequestData = AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA)); + if (RequestData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR_3; + } + RequestData->Method = HeaderOnly ? HttpMethodHead : HttpMethodGet; + RequestData->Url = Url; + + // + // 2.3 Record the request info in a temp cache item. + // + if (Cache != NULL) { + Cache->RequestData = RequestData; + } + + // + // 2.4 Send out the request to HTTP server. + // + HttpIo = &Private->HttpIo; + Status = HttpIoSendRequest ( + HttpIo, + RequestData, + HttpIoHeader->HeaderCount, + HttpIoHeader->Headers, + 0, + NULL + ); + if (EFI_ERROR (Status)) { + goto ERROR_4; + } + + // + // 3. Receive HTTP response message. + // + + // + // 3.1 First step, use zero BodyLength to only receive the response headers. + // + ResponseData = AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA)); + if (ResponseData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR_4; + } + Status = HttpIoRecvResponse ( + &Private->HttpIo, + TRUE, + ResponseData + ); + if (EFI_ERROR (Status) || EFI_ERROR (ResponseData->Status)) { + if (EFI_ERROR (ResponseData->Status)) { + StatusCode = HttpIo->RspToken.Message->Data.Response->StatusCode; + HttpBootPrintErrorMessage (StatusCode); + Status = ResponseData->Status; + } + goto ERROR_5; + } + + // + // Check the image type according to server's response. + // + Status = HttpBootCheckImageType ( + Private->BootFileUri, + Private->BootFileUriParser, + ResponseData->HeaderCount, + ResponseData->Headers, + ImageType + ); + if (EFI_ERROR (Status)) { + goto ERROR_5; + } + + // + // 3.2 Cache the response header. + // + if (Cache != NULL) { + Cache->ResponseData = ResponseData; + Cache->ImageType = *ImageType; + } + + // + // 3.3 Init a message-body parser from the header information. + // + Parser = NULL; + Context.NewBlock = FALSE; + Context.Block = NULL; + Context.CopyedSize = 0; + Context.Buffer = Buffer; + Context.BufferSize = *BufferSize; + Context.Cache = Cache; + Context.Private = Private; + Status = HttpInitMsgParser ( + HeaderOnly ? HttpMethodHead : HttpMethodGet, + ResponseData->Response.StatusCode, + ResponseData->HeaderCount, + ResponseData->Headers, + HttpBootGetBootFileCallback, + (VOID*) &Context, + &Parser + ); + if (EFI_ERROR (Status)) { + goto ERROR_6; + } + + // + // 3.4 Continue to receive and parse message-body if needed. + // + Block = NULL; + if (!HeaderOnly) { + // + // 3.4.1, check whether we are in identity transfer-coding. + // + ContentLength = 0; + Status = HttpGetEntityLength (Parser, &ContentLength); + if (!EFI_ERROR (Status)) { + IdentityMode = TRUE; + } else { + IdentityMode = FALSE; + } + + // + // 3.4.2, start the message-body download, the identity and chunked transfer-coding + // is handled in different path here. + // + ZeroMem (&ResponseBody, sizeof (HTTP_IO_RESPONSE_DATA)); + if (IdentityMode) { + // + // In identity transfer-coding there is no need to parse the message body, + // just download the message body to the user provided buffer directly. + // + ReceivedSize = 0; + while (ReceivedSize < ContentLength) { + ResponseBody.Body = (CHAR8*) Buffer + ReceivedSize; + ResponseBody.BodyLength = *BufferSize - ReceivedSize; + Status = HttpIoRecvResponse ( + &Private->HttpIo, + FALSE, + &ResponseBody + ); + if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) { + if (EFI_ERROR (ResponseBody.Status)) { + Status = ResponseBody.Status; + } + goto ERROR_6; + } + ReceivedSize += ResponseBody.BodyLength; + if (Private->HttpBootCallback != NULL) { + Status = Private->HttpBootCallback->Callback ( + Private->HttpBootCallback, + HttpBootHttpEntityBody, + TRUE, + (UINT32)ResponseBody.BodyLength, + ResponseBody.Body + ); + if (EFI_ERROR (Status)) { + goto ERROR_6; + } + } + } + } else { + // + // In "chunked" transfer-coding mode, so we need to parse the received + // data to get the real entity content. + // + Block = NULL; + while (!HttpIsMessageComplete (Parser)) { + // + // Allocate a buffer in Block to hold the message-body. + // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse(). + // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before + // every HttpIoRecvResponse(). + // + if (Block == NULL || Context.BufferSize == 0) { + Block = AllocatePool (HTTP_BOOT_BLOCK_SIZE); + if (Block == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR_6; + } + Context.NewBlock = TRUE; + Context.Block = Block; + } else { + Context.NewBlock = FALSE; + } + + ResponseBody.Body = (CHAR8*) Block; + ResponseBody.BodyLength = HTTP_BOOT_BLOCK_SIZE; + Status = HttpIoRecvResponse ( + &Private->HttpIo, + FALSE, + &ResponseBody + ); + if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) { + if (EFI_ERROR (ResponseBody.Status)) { + Status = ResponseBody.Status; + } + goto ERROR_6; + } + + // + // Parse the new received block of the message-body, the block will be saved in cache. + // + Status = HttpParseMessageBody ( + Parser, + ResponseBody.BodyLength, + ResponseBody.Body + ); + if (EFI_ERROR (Status)) { + goto ERROR_6; + } + } + } + } + + // + // 3.5 Message-body receive & parse is completed, we should be able to get the file size now. + // + Status = HttpGetEntityLength (Parser, &ContentLength); + if (EFI_ERROR (Status)) { + goto ERROR_6; + } + + if (*BufferSize < ContentLength) { + Status = EFI_BUFFER_TOO_SMALL; + } else { + Status = EFI_SUCCESS; + } + *BufferSize = ContentLength; + + // + // 4. Save the cache item to driver's cache list and return. + // + if (Cache != NULL) { + Cache->EntityLength = ContentLength; + InsertTailList (&Private->CacheList, &Cache->Link); + } + + if (Parser != NULL) { + HttpFreeMsgParser (Parser); + } + + return Status; + +ERROR_6: + if (Parser != NULL) { + HttpFreeMsgParser (Parser); + } + if (Context.Block != NULL) { + FreePool (Context.Block); + } + HttpBootFreeCache (Cache); + +ERROR_5: + if (ResponseData != NULL) { + FreePool (ResponseData); + } +ERROR_4: + if (RequestData != NULL) { + FreePool (RequestData); + } +ERROR_3: + HttpBootFreeHeader (HttpIoHeader); +ERROR_2: + if (Cache != NULL) { + FreePool (Cache); + } +ERROR_1: + if (Url != NULL) { + FreePool (Url); + } + + return Status; +} + diff --git a/NetworkPkg/HttpBootDxe/HttpBootClient.h b/NetworkPkg/HttpBootDxe/HttpBootClient.h new file mode 100644 index 000000000..971b2dc80 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootClient.h @@ -0,0 +1,137 @@ +/** @file + Declaration of the boot file download function. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_HTTP_BOOT_HTTP_H__ +#define __EFI_HTTP_BOOT_HTTP_H__ + +#define HTTP_BOOT_REQUEST_TIMEOUT 5000 // 5 seconds in uints of millisecond. +#define HTTP_BOOT_RESPONSE_TIMEOUT 5000 // 5 seconds in uints of millisecond. +#define HTTP_BOOT_BLOCK_SIZE 1500 + + + +#define HTTP_USER_AGENT_EFI_HTTP_BOOT "UefiHttpBoot/1.0" + +// +// Record the data length and start address of a data block. +// +typedef struct { + LIST_ENTRY Link; // Link to the EntityDataList in HTTP_BOOT_CACHE_CONTENT + UINT8 *Block; // If NULL, the data is in previous data block. + UINT8 *DataStart; // Point to somewhere in the Block + UINTN DataLength; +} HTTP_BOOT_ENTITY_DATA; + +// +// Structure for a cache item +// +typedef struct { + LIST_ENTRY Link; // Link to the CacheList in driver's private data. + EFI_HTTP_REQUEST_DATA *RequestData; + HTTP_IO_RESPONSE_DATA *ResponseData; // Not include any message-body data. + HTTP_BOOT_IMAGE_TYPE ImageType; + UINTN EntityLength; + LIST_ENTRY EntityDataList; // Entity data (message-body) +} HTTP_BOOT_CACHE_CONTENT; + +// +// Callback data for HTTP_BODY_PARSER_CALLBACK() +// +typedef struct { + EFI_STATUS Status; + // + // Cache info. + // + HTTP_BOOT_CACHE_CONTENT *Cache; + BOOLEAN NewBlock; + UINT8 *Block; + + // + // Caller provided buffer to load the file in. + // + UINTN CopyedSize; + UINTN BufferSize; + UINT8 *Buffer; + + HTTP_BOOT_PRIVATE_DATA *Private; +} HTTP_BOOT_CALLBACK_DATA; + +/** + Discover all the boot information for boot file. + + @param[in, out] Private The pointer to the driver's private data. + + @retval EFI_SUCCESS Successfully obtained all the boot information . + @retval Others Failed to retrieve the boot information. + +**/ +EFI_STATUS +HttpBootDiscoverBootInfo ( + IN OUT HTTP_BOOT_PRIVATE_DATA *Private + ); + +/** + Create a HttpIo instance for the file download. + + @param[in] Private The pointer to the driver's private data. + + @retval EFI_SUCCESS Successfully created. + @retval Others Failed to create HttpIo. + +**/ +EFI_STATUS +HttpBootCreateHttpIo ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +/** + This function download the boot file by using UEFI HTTP protocol. + + @param[in] Private The pointer to the driver's private data. + @param[in] HeaderOnly Only request the response header, it could save a lot of time if + the caller only want to know the size of the requested file. + @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return + code of EFI_SUCCESS, the amount of data transferred to + Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL, + the size of Buffer required to retrieve the requested file. + @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL, + then the size of the requested file is returned in + BufferSize. + @param[out] ImageType The image type of the downloaded file. + + @retval EFI_SUCCESS The file was loaded. + @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources + @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry. + BufferSize has been updated with the size needed to complete + the request. + @retval Others Unexpected error happened. + +**/ +EFI_STATUS +HttpBootGetBootFile ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN BOOLEAN HeaderOnly, + IN OUT UINTN *BufferSize, + OUT UINT8 *Buffer, + OUT HTTP_BOOT_IMAGE_TYPE *ImageType + ); + +/** + Clean up all cached data. + + @param[in] Private The pointer to the driver's private data. + +**/ +VOID +HttpBootFreeCacheList ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +#endif diff --git a/NetworkPkg/HttpBootDxe/HttpBootComponentName.c b/NetworkPkg/HttpBootDxe/HttpBootComponentName.c new file mode 100644 index 000000000..dbaa944ad --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootComponentName.c @@ -0,0 +1,177 @@ +/** @file + Implementation of EFI_COMPONENT_NAME_PROTOCOL and EFI_COMPONENT_NAME2_PROTOCOL protocol. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpBootDxe.h" + +/// +/// Component Name Protocol instance +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_COMPONENT_NAME_PROTOCOL gHttpBootDxeComponentName = { + (EFI_COMPONENT_NAME_GET_DRIVER_NAME) HttpBootDxeComponentNameGetDriverName, + (EFI_COMPONENT_NAME_GET_CONTROLLER_NAME)HttpBootDxeComponentNameGetControllerName, + "eng" +}; + +/// +/// Component Name 2 Protocol instance +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_COMPONENT_NAME2_PROTOCOL gHttpBootDxeComponentName2 = { + HttpBootDxeComponentNameGetDriverName, + HttpBootDxeComponentNameGetControllerName, + "en" +}; + +/// +/// Table of driver names +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_UNICODE_STRING_TABLE mHttpBootDxeDriverNameTable[] = { + { "eng;en", (CHAR16 *)L"UEFI HTTP Boot Driver" }, + { NULL, NULL } +}; + +/// +/// Table of controller names +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_UNICODE_STRING_TABLE mHttpBootDxeControllerNameTable[] = { + { "eng;en", (CHAR16 *)L"UEFI Http Boot Controller" }, + { NULL, NULL } +}; + +/** + Retrieves a Unicode string that is the user-readable name of the EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param Language A pointer to a three-character ISO 639-2 language identifier. + This is the language of the driver name that that the caller + is requesting, and it must match one of the languages specified + in SupportedLanguages. The number of languages supported by a + driver is up to the driver writer. + @param DriverName A pointer to the Unicode string to return. This Unicode string + is the name of the driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by This + and the language specified by Language was returned + in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +HttpBootDxeComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mHttpBootDxeDriverNameTable, + DriverName, + (BOOLEAN) (This != &gHttpBootDxeComponentName2) + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by an EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param ControllerHandle The handle of a controller that the driver specified by + This is managing. This handle specifies the controller + whose name is to be returned. + @param ChildHandle The handle of the child controller to retrieve the name + of. This is an optional parameter that may be NULL. It + will be NULL for device drivers. It will also be NULL + for a bus drivers that wish to retrieve the name of the + bus controller. It will not be NULL for a bus driver + that wishes to retrieve the name of a child controller. + @param Language A pointer to a three character ISO 639-2 language + identifier. This is the language of the controller name + that the caller is requesting, and it must match one + of the languages specified in SupportedLanguages. The + number of languages supported by a driver is up to the + driver writer. + @param ControllerName A pointer to the Unicode string to return. This Unicode + string is the name of the controller specified by + ControllerHandle and ChildHandle in the language specified + by Language, from the point of view of the driver specified + by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in the + language specified by Language for the driver + specified by This was returned in DriverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not currently managing + the controller specified by ControllerHandle and + ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +HttpBootDxeComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_HANDLE NicHandle; + UINT32 *Id; + + if (ControllerHandle == NULL || ChildHandle != NULL) { + return EFI_UNSUPPORTED; + } + + NicHandle = HttpBootGetNicByIp4Children (ControllerHandle); + if (NicHandle == NULL) { + NicHandle = HttpBootGetNicByIp6Children(ControllerHandle); + if (NicHandle == NULL) { + return EFI_UNSUPPORTED; + } + } + + // + // Try to retrieve the private data by caller ID GUID. + // + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mHttpBootDxeControllerNameTable, + ControllerName, + (BOOLEAN)(This != &gHttpBootDxeComponentName2) + ); + +} diff --git a/NetworkPkg/HttpBootDxe/HttpBootComponentName.h b/NetworkPkg/HttpBootDxe/HttpBootComponentName.h new file mode 100644 index 000000000..f53af8762 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootComponentName.h @@ -0,0 +1,93 @@ +/** @file + Declaration of HTTP boot driver's EFI_COMPONENT_NAME_PROTOCOL and + EFI_COMPONENT_NAME2_PROTOCOL function. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_UEFI_HTTP_BOOT_COM_NAME_H__ +#define __EFI_UEFI_HTTP_BOOT_COM_NAME_H__ + +/** + Retrieves a Unicode string that is the user-readable name of the EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param Language A pointer to a three-character ISO 639-2 language identifier. + This is the language of the driver name that that the caller + is requesting, and it must match one of the languages specified + in SupportedLanguages. The number of languages supported by a + driver is up to the driver writer. + @param DriverName A pointer to the Unicode string to return. This Unicode string + is the name of the driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by This + and the language specified by Language was returned + in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +HttpBootDxeComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by an EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param ControllerHandle The handle of a controller that the driver specified by + This is managing. This handle specifies the controller + whose name is to be returned. + @param ChildHandle The handle of the child controller to retrieve the name + of. This is an optional parameter that may be NULL. It + will be NULL for device drivers. It will also be NULL + for a bus drivers that wish to retrieve the name of the + bus controller. It will not be NULL for a bus driver + that wishes to retrieve the name of a child controller. + @param Language A pointer to a three character ISO 639-2 language + identifier. This is the language of the controller name + that the caller is requesting, and it must match one + of the languages specified in SupportedLanguages. The + number of languages supported by a driver is up to the + driver writer. + @param ControllerName A pointer to the Unicode string to return. This Unicode + string is the name of the controller specified by + ControllerHandle and ChildHandle in the language specified + by Language, from the point of view of the driver specified + by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in the + language specified by Language for the driver + specified by This was returned in DriverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not currently managing + the controller specified by ControllerHandle and + ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +HttpBootDxeComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +#endif diff --git a/NetworkPkg/HttpBootDxe/HttpBootConfig.c b/NetworkPkg/HttpBootDxe/HttpBootConfig.c new file mode 100644 index 000000000..245bd49a6 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootConfig.c @@ -0,0 +1,700 @@ +/** @file + Helper functions for configuring or getting the parameters relating to HTTP Boot. + +Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpBootDxe.h" +#include + +CHAR16 mHttpBootConfigStorageName[] = L"HTTP_BOOT_CONFIG_IFR_NVDATA"; + +/** + Add new boot option for HTTP boot. + + @param[in] Private Pointer to the driver private data. + @param[in] UsingIpv6 Set to TRUE if creating boot option for IPv6. + @param[in] Description The description text of the boot option. + @param[in] Uri The URI string of the boot file. + + @retval EFI_SUCCESS The boot option is created successfully. + @retval Others Failed to create new boot option. + +**/ +EFI_STATUS +HttpBootAddBootOption ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN BOOLEAN UsingIpv6, + IN CHAR16 *Description, + IN CHAR16 *Uri + ) +{ + EFI_DEV_PATH *Node; + EFI_DEVICE_PATH_PROTOCOL *TmpDevicePath; + EFI_DEVICE_PATH_PROTOCOL *NewDevicePath; + UINTN Length; + CHAR8 AsciiUri[URI_STR_MAX_SIZE]; + EFI_STATUS Status; + UINTN Index; + EFI_BOOT_MANAGER_LOAD_OPTION NewOption; + + NewDevicePath = NULL; + Node = NULL; + TmpDevicePath = NULL; + + if (StrLen (Description) == 0) { + return EFI_INVALID_PARAMETER; + } + + // + // Convert the scheme to all lower case. + // + for (Index = 0; Index < StrLen (Uri); Index++) { + if (Uri[Index] == L':') { + break; + } + if (Uri[Index] >= L'A' && Uri[Index] <= L'Z') { + Uri[Index] -= (CHAR16)(L'A' - L'a'); + } + } + + // + // Only accept empty URI, or http and https URI. + // + if ((StrLen (Uri) != 0) && (StrnCmp (Uri, L"http://", 7) != 0) && (StrnCmp (Uri, L"https://", 8) != 0)) { + return EFI_INVALID_PARAMETER; + } + + // + // Create a new device path by appending the IP node and URI node to + // the driver's parent device path + // + if (!UsingIpv6) { + Node = AllocateZeroPool (sizeof (IPv4_DEVICE_PATH)); + if (Node == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + Node->Ipv4.Header.Type = MESSAGING_DEVICE_PATH; + Node->Ipv4.Header.SubType = MSG_IPv4_DP; + SetDevicePathNodeLength (Node, sizeof (IPv4_DEVICE_PATH)); + } else { + Node = AllocateZeroPool (sizeof (IPv6_DEVICE_PATH)); + if (Node == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + Node->Ipv6.Header.Type = MESSAGING_DEVICE_PATH; + Node->Ipv6.Header.SubType = MSG_IPv6_DP; + SetDevicePathNodeLength (Node, sizeof (IPv6_DEVICE_PATH)); + } + TmpDevicePath = AppendDevicePathNode (Private->ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (Node); + if (TmpDevicePath == NULL) { + return EFI_OUT_OF_RESOURCES; + } + // + // Update the URI node with the input boot file URI. + // + UnicodeStrToAsciiStrS (Uri, AsciiUri, sizeof (AsciiUri)); + Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + AsciiStrSize (AsciiUri); + Node = AllocatePool (Length); + if (Node == NULL) { + Status = EFI_OUT_OF_RESOURCES; + FreePool (TmpDevicePath); + goto ON_EXIT; + } + Node->DevPath.Type = MESSAGING_DEVICE_PATH; + Node->DevPath.SubType = MSG_URI_DP; + SetDevicePathNodeLength (Node, Length); + CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL), AsciiUri, AsciiStrSize (AsciiUri)); + NewDevicePath = AppendDevicePathNode (TmpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (Node); + FreePool (TmpDevicePath); + if (NewDevicePath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Add a new load option. + // + Status = EfiBootManagerInitializeLoadOption ( + &NewOption, + LoadOptionNumberUnassigned, + LoadOptionTypeBoot, + LOAD_OPTION_ACTIVE, + Description, + NewDevicePath, + NULL, + 0 + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = EfiBootManagerAddLoadOptionVariable (&NewOption, (UINTN) -1); + EfiBootManagerFreeLoadOption (&NewOption); + +ON_EXIT: + + if (NewDevicePath != NULL) { + FreePool (NewDevicePath); + } + + return Status; +} + +/** + + This function allows the caller to request the current + configuration for one or more named elements. The resulting + string is in format. Also, any and all alternative + configuration strings shall be appended to the end of the + current configuration string. If they are, they must appear + after the current configuration. They must contain the same + routing (GUID, NAME, PATH) as the current configuration string. + They must have an additional description indicating the type of + alternative configuration the string represents, + "ALTCFG=". That (when + converted from Hex UNICODE to binary) is a reference to a + string in the associated string pack. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param[in] Request A null-terminated Unicode string in + format. Note that this + includes the routing information as well as + the configurable name / value pairs. It is + invalid for this string to be in + format. + + @param[out] Progress On return, points to a character in the + Request string. Points to the string's null + terminator if request was successful. Points + to the most recent "&" before the first + failing name / value pair (or the beginning + of the string if the failure is in the first + name / value pair) if the request was not successful. + + @param[out] Results A null-terminated Unicode string in + format which has all values + filled in for the names in the Request string. + String to be allocated by the called function. + + @retval EFI_SUCCESS The Results string is filled with the + values corresponding to all requested + names. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_INVALID_PARAMETER For example, passing in a NULL + for the Request parameter + would result in this type of + error. In this case, the + Progress parameter would be + set to NULL. + + @retval EFI_NOT_FOUND Routing data doesn't match any + known driver. Progress set to the + first character in the routing header. + Note: There is no requirement that the + driver validate the routing data. It + must skip the in order to + process the names. + + @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set + to most recent "&" before the + error or the beginning of the + string. + + @retval EFI_INVALID_PARAMETER Unknown name. Progress points + to the & before the name in + question. + +**/ +EFI_STATUS +EFIAPI +HttpBootFormExtractConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Request, + OUT EFI_STRING *Progress, + OUT EFI_STRING *Results + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + HTTP_BOOT_FORM_CALLBACK_INFO *CallbackInfo; + EFI_STRING ConfigRequestHdr; + EFI_STRING ConfigRequest; + BOOLEAN AllocatedRequest; + UINTN Size; + + if (Progress == NULL || Results == NULL) { + return EFI_INVALID_PARAMETER; + } + + *Progress = Request; + if ((Request != NULL) && !HiiIsConfigHdrMatch (Request, &gHttpBootConfigGuid, mHttpBootConfigStorageName)) { + return EFI_NOT_FOUND; + } + + ConfigRequestHdr = NULL; + ConfigRequest = NULL; + AllocatedRequest = FALSE; + Size = 0; + + CallbackInfo = HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This); + // + // Convert buffer data to by helper function BlockToConfig() + // + BufferSize = sizeof (HTTP_BOOT_CONFIG_IFR_NVDATA); + ZeroMem (&CallbackInfo->HttpBootNvData, BufferSize); + StrCpyS (CallbackInfo->HttpBootNvData.Description, DESCRIPTION_STR_MAX_SIZE / sizeof (CHAR16), HTTP_BOOT_DEFAULT_DESCRIPTION_STR); + + ConfigRequest = Request; + if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) { + // + // Request has no request element, construct full request string. + // Allocate and fill a buffer large enough to hold the template + // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator + // + ConfigRequestHdr = HiiConstructConfigHdr (&gHttpBootConfigGuid, mHttpBootConfigStorageName, CallbackInfo->ChildHandle); + Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16); + ConfigRequest = AllocateZeroPool (Size); + if (ConfigRequest == NULL) { + return EFI_OUT_OF_RESOURCES; + } + AllocatedRequest = TRUE; + UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize); + FreePool (ConfigRequestHdr); + } + + Status = gHiiConfigRouting->BlockToConfig ( + gHiiConfigRouting, + ConfigRequest, + (UINT8 *) &CallbackInfo->HttpBootNvData, + BufferSize, + Results, + Progress + ); + + // + // Free the allocated config request string. + // + if (AllocatedRequest) { + FreePool (ConfigRequest); + ConfigRequest = NULL; + } + // + // Set Progress string to the original request string. + // + if (Request == NULL) { + *Progress = NULL; + } else if (StrStr (Request, L"OFFSET") == NULL) { + *Progress = Request + StrLen (Request); + } + + return Status; +} + +/** + + This function applies changes in a driver's configuration. + Input is a Configuration, which has the routing data for this + driver followed by name / value configuration pairs. The driver + must apply those pairs to its configurable storage. If the + driver's configuration is stored in a linear block of data + and the driver's name / value pairs are in + format, it may use the ConfigToBlock helper function (above) to + simplify the job. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param[in] Configuration A null-terminated Unicode string in + format. + + @param[out] Progress A pointer to a string filled in with the + offset of the most recent '&' before the + first failing name / value pair (or the + beginning of the string if the failure + is in the first name / value pair) or + the terminating NULL if all was + successful. + + @retval EFI_SUCCESS The results have been distributed or are + awaiting distribution. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_INVALID_PARAMETERS Passing in a NULL for the + Results parameter would result + in this type of error. + + @retval EFI_NOT_FOUND Target for the specified routing data + was not found. + +**/ +EFI_STATUS +EFIAPI +HttpBootFormRouteConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Configuration, + OUT EFI_STRING *Progress + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + HTTP_BOOT_FORM_CALLBACK_INFO *CallbackInfo; + HTTP_BOOT_PRIVATE_DATA *Private; + + if (Progress == NULL) { + return EFI_INVALID_PARAMETER; + } + *Progress = Configuration; + + if (Configuration == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Check routing data in . + // Note: there is no name for Name/Value storage, only GUID will be checked + // + if (!HiiIsConfigHdrMatch (Configuration, &gHttpBootConfigGuid, mHttpBootConfigStorageName)) { + return EFI_NOT_FOUND; + } + + CallbackInfo = HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This); + Private = HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_INFO (CallbackInfo); + + BufferSize = sizeof (HTTP_BOOT_CONFIG_IFR_NVDATA); + ZeroMem (&CallbackInfo->HttpBootNvData, BufferSize); + + Status = gHiiConfigRouting->ConfigToBlock ( + gHiiConfigRouting, + Configuration, + (UINT8 *) &CallbackInfo->HttpBootNvData, + &BufferSize, + Progress + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Create a new boot option according to the configuration data. + // + HttpBootAddBootOption ( + Private, + (CallbackInfo->HttpBootNvData.IpVersion == HTTP_BOOT_IP_VERSION_6) ? TRUE : FALSE, + CallbackInfo->HttpBootNvData.Description, + CallbackInfo->HttpBootNvData.Uri + ); + + return EFI_SUCCESS; +} + +/** + + This function is called to provide results data to the driver. + This data consists of a unique key that is used to identify + which data is either being passed back or being asked for. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Action Specifies the type of action taken by the browser. + @param[in] QuestionId A unique value which is sent to the original + exporting driver so that it can identify the type + of data to expect. The format of the data tends to + vary based on the opcode that generated the callback. + @param[in] Type The type of value for the question. + @param[in, out] Value A pointer to the data being sent to the original + exporting driver. + @param[out] ActionRequest On return, points to the action requested by the + callback function. + + @retval EFI_SUCCESS The callback successfully handled the action. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the + variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be saved. + @retval EFI_UNSUPPORTED The specified Action is not supported by the + callback. +**/ +EFI_STATUS +EFIAPI +HttpBootFormCallback ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN EFI_BROWSER_ACTION Action, + IN EFI_QUESTION_ID QuestionId, + IN UINT8 Type, + IN OUT EFI_IFR_TYPE_VALUE *Value, + OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest + ) +{ + EFI_INPUT_KEY Key; + CHAR16 *Uri; + UINTN UriLen; + CHAR8 *AsciiUri; + HTTP_BOOT_FORM_CALLBACK_INFO *CallbackInfo; + EFI_STATUS Status; + + Uri = NULL; + UriLen = 0; + AsciiUri = NULL; + Status = EFI_SUCCESS; + + if (This == NULL || Value == NULL) { + return EFI_INVALID_PARAMETER; + } + + CallbackInfo = HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This); + + if (Action != EFI_BROWSER_ACTION_CHANGING) { + return EFI_UNSUPPORTED; + } + + switch (QuestionId) { + case KEY_INITIATOR_URI: + // + // Get user input URI string + // + Uri = HiiGetString (CallbackInfo->RegisteredHandle, Value->string, NULL); + if(Uri == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // The URI should be either an empty string (for corporate environment) ,or http(s) for home environment. + // Pop up a message box for the unsupported URI. + // + if (StrLen (Uri) != 0) { + UriLen = StrLen (Uri) + 1; + AsciiUri = AllocateZeroPool (UriLen); + if (AsciiUri == NULL) { + FreePool (Uri); + return EFI_OUT_OF_RESOURCES; + } + + UnicodeStrToAsciiStrS (Uri, AsciiUri, UriLen); + + Status = HttpBootCheckUriScheme (AsciiUri); + + if (Status == EFI_INVALID_PARAMETER) { + + DEBUG ((EFI_D_ERROR, "HttpBootFormCallback: %r.\n", Status)); + + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"ERROR: Unsupported URI!", + L"Only supports HTTP and HTTPS", + NULL + ); + } else if (Status == EFI_ACCESS_DENIED) { + + DEBUG ((EFI_D_ERROR, "HttpBootFormCallback: %r.\n", Status)); + + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"ERROR: Unsupported URI!", + L"HTTP is disabled", + NULL + ); + } + } + + if (Uri != NULL) { + FreePool (Uri); + } + + if (AsciiUri != NULL) { + FreePool (AsciiUri); + } + + break; + + default: + break; + } + + return Status; +} + +/** + Initialize the configuration form. + + @param[in] Private Pointer to the driver private data. + + @retval EFI_SUCCESS The configuration form is initialized. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +HttpBootConfigFormInit ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + EFI_STATUS Status; + HTTP_BOOT_FORM_CALLBACK_INFO *CallbackInfo; + VENDOR_DEVICE_PATH VendorDeviceNode; + CHAR16 *MacString; + CHAR16 *OldMenuString; + CHAR16 MenuString[128]; + + CallbackInfo = &Private->CallbackInfo; + + if (CallbackInfo->Initilized) { + return EFI_SUCCESS; + } + + CallbackInfo->Signature = HTTP_BOOT_FORM_CALLBACK_INFO_SIGNATURE; + + // + // Construct device path node for EFI HII Config Access protocol, + // which consists of controller physical device path and one hardware + // vendor guid node. + // + ZeroMem (&VendorDeviceNode, sizeof (VENDOR_DEVICE_PATH)); + VendorDeviceNode.Header.Type = HARDWARE_DEVICE_PATH; + VendorDeviceNode.Header.SubType = HW_VENDOR_DP; + CopyGuid (&VendorDeviceNode.Guid, &gEfiCallerIdGuid); + SetDevicePathNodeLength (&VendorDeviceNode.Header, sizeof (VENDOR_DEVICE_PATH)); + CallbackInfo->HiiVendorDevicePath = AppendDevicePathNode ( + Private->ParentDevicePath, + (EFI_DEVICE_PATH_PROTOCOL *) &VendorDeviceNode + ); + if (CallbackInfo->HiiVendorDevicePath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + CallbackInfo->ConfigAccess.ExtractConfig = HttpBootFormExtractConfig; + CallbackInfo->ConfigAccess.RouteConfig = HttpBootFormRouteConfig; + CallbackInfo->ConfigAccess.Callback = HttpBootFormCallback; + + // + // Install Device Path Protocol and Config Access protocol to driver handle. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &CallbackInfo->ChildHandle, + &gEfiDevicePathProtocolGuid, + CallbackInfo->HiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &CallbackInfo->ConfigAccess, + NULL + ); + if (EFI_ERROR (Status)) { + goto Error; + } + + // + // Publish our HII data. + // + CallbackInfo->RegisteredHandle = HiiAddPackages ( + &gHttpBootConfigGuid, + CallbackInfo->ChildHandle, + HttpBootDxeStrings, + HttpBootConfigVfrBin, + NULL + ); + if (CallbackInfo->RegisteredHandle == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + // + // Append MAC string in the menu help string + // + Status = NetLibGetMacString (Private->Controller, NULL, &MacString); + if (!EFI_ERROR (Status)) { + OldMenuString = HiiGetString ( + CallbackInfo->RegisteredHandle, + STRING_TOKEN (STR_HTTP_BOOT_CONFIG_FORM_HELP), + NULL + ); + UnicodeSPrint (MenuString, 128, L"%s (MAC:%s)", OldMenuString, MacString); + HiiSetString ( + CallbackInfo->RegisteredHandle, + STRING_TOKEN (STR_HTTP_BOOT_CONFIG_FORM_HELP), + MenuString, + NULL + ); + + FreePool (MacString); + FreePool (OldMenuString); + + CallbackInfo->Initilized = TRUE; + return EFI_SUCCESS; + } + +Error: + + HttpBootConfigFormUnload (Private); + return Status; +} + +/** + Unload the configuration form, this includes: delete all the configuration + entries, uninstall the form callback protocol, and free the resources used. + The form will only be unload completely when both IP4 and IP6 stack are stopped. + + @param[in] Private Pointer to the driver private data. + + @retval EFI_SUCCESS The configuration form is unloaded. + @retval Others Failed to unload the form. + +**/ +EFI_STATUS +HttpBootConfigFormUnload ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + HTTP_BOOT_FORM_CALLBACK_INFO *CallbackInfo; + + if (Private->Ip4Nic != NULL || Private->Ip6Nic != NULL) { + // + // Only unload the configuration form when both IP4 and IP6 stack are stopped. + // + return EFI_SUCCESS; + } + + CallbackInfo = &Private->CallbackInfo; + if (CallbackInfo->ChildHandle != NULL) { + // + // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL + // + gBS->UninstallMultipleProtocolInterfaces ( + CallbackInfo->ChildHandle, + &gEfiDevicePathProtocolGuid, + CallbackInfo->HiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &CallbackInfo->ConfigAccess, + NULL + ); + CallbackInfo->ChildHandle = NULL; + } + + if (CallbackInfo->HiiVendorDevicePath != NULL) { + FreePool (CallbackInfo->HiiVendorDevicePath); + CallbackInfo->HiiVendorDevicePath = NULL; + } + + if (CallbackInfo->RegisteredHandle != NULL) { + // + // Remove HII package list + // + HiiRemovePackages (CallbackInfo->RegisteredHandle); + CallbackInfo->RegisteredHandle = NULL; + } + + return EFI_SUCCESS; +} diff --git a/NetworkPkg/HttpBootDxe/HttpBootConfig.h b/NetworkPkg/HttpBootDxe/HttpBootConfig.h new file mode 100644 index 000000000..ce2116e24 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootConfig.h @@ -0,0 +1,73 @@ +/** @file + The header file of functions for configuring or getting the parameters + relating to HTTP Boot. + +Copyright (c) 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _HTTP_BOOT_CONFIG_H_ +#define _HTTP_BOOT_CONFIG_H_ + + +#include "HttpBootConfigNVDataStruc.h" + +typedef struct _HTTP_BOOT_FORM_CALLBACK_INFO HTTP_BOOT_FORM_CALLBACK_INFO; + +extern UINT8 HttpBootDxeStrings[]; +extern UINT8 HttpBootConfigVfrBin[]; + +#pragma pack() + +#define HTTP_BOOT_FORM_CALLBACK_INFO_SIGNATURE SIGNATURE_32 ('H', 'B', 'f', 'c') + +#define HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS(Callback) \ + CR ( \ + Callback, \ + HTTP_BOOT_FORM_CALLBACK_INFO, \ + ConfigAccess, \ + HTTP_BOOT_FORM_CALLBACK_INFO_SIGNATURE \ + ) + +struct _HTTP_BOOT_FORM_CALLBACK_INFO { + UINT32 Signature; + BOOLEAN Initilized; + EFI_HANDLE ChildHandle; + EFI_DEVICE_PATH_PROTOCOL *HiiVendorDevicePath; + EFI_HII_HANDLE RegisteredHandle; + EFI_HII_CONFIG_ACCESS_PROTOCOL ConfigAccess; + HTTP_BOOT_CONFIG_IFR_NVDATA HttpBootNvData; +}; + +/** + Initialize the configuration form. + + @param[in] Private Pointer to the driver private data. + + @retval EFI_SUCCESS The configuration form is initialized. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +HttpBootConfigFormInit ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +/** + Unload the configuration form, this includes: delete all the configuration + entries, uninstall the form callback protocol, and free the resources used. + The form will only be unload completely when both IP4 and IP6 stack are stopped. + + @param[in] Private Pointer to the driver private data. + + @retval EFI_SUCCESS The configuration form is unloaded. + @retval Others Failed to unload the form. + +**/ +EFI_STATUS +HttpBootConfigFormUnload ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +#endif diff --git a/NetworkPkg/HttpBootDxe/HttpBootConfigNVDataStruc.h b/NetworkPkg/HttpBootDxe/HttpBootConfigNVDataStruc.h new file mode 100644 index 000000000..3afa97598 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootConfigNVDataStruc.h @@ -0,0 +1,44 @@ +/** @file + Define NVData structures used by the HTTP Boot configuration component. + +Copyright (c) 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _HTTP_BOOT_NVDATA_STRUC_H_ +#define _HTTP_BOOT_NVDATA_STRUC_H_ + +#include + +#define HTTP_BOOT_IP_VERSION_4 0 +#define HTTP_BOOT_IP_VERSION_6 1 + +// +// Macros used for an IPv4 or an IPv6 address. +// +#define URI_STR_MIN_SIZE 0 +#define URI_STR_MAX_SIZE 255 + +#define DESCRIPTION_STR_MIN_SIZE 6 +#define DESCRIPTION_STR_MAX_SIZE 75 + +#define CONFIGURATION_VARSTORE_ID 0x1234 + +#define FORMID_MAIN_FORM 1 + +#define KEY_INITIATOR_URI 0x101 + +#define HTTP_BOOT_DEFAULT_DESCRIPTION_STR L"UEFI HTTP" + +#pragma pack(1) +typedef struct _HTTP_BOOT_CONFIG_IFR_NVDATA { + UINT8 IpVersion; + UINT8 Padding; + CHAR16 Description[DESCRIPTION_STR_MAX_SIZE]; + CHAR16 Uri[URI_STR_MAX_SIZE]; +} HTTP_BOOT_CONFIG_IFR_NVDATA; +#pragma pack() + + +#endif diff --git a/NetworkPkg/HttpBootDxe/HttpBootConfigStrings.uni b/NetworkPkg/HttpBootDxe/HttpBootConfigStrings.uni new file mode 100644 index 000000000..40abb13d0 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootConfigStrings.uni @@ -0,0 +1,21 @@ +/** @file + String definitions for HTTP Boot configuration. + + Copyright (c) 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#langdef en-US "English" + +#string STR_HTTP_BOOT_CONFIG_FORM_TITLE #language en-US "HTTP Boot Configuration" +#string STR_HTTP_BOOT_CONFIG_FORM_HELP #language en-US "Configure HTTP Boot parameters." +#string STR_HTTP_BOOT_IP_VERSION_PROMPT #language en-US "Internet Protocol" +#string STR_HTTP_BOOT_IP_VERSION_HELP #language en-US "Select the version of Internet Protocol." +#string STR_HTTP_BOOT_IP_VERSION_4 #language en-US "IP4" +#string STR_HTTP_BOOT_IP_VERSION_6 #language en-US "IP6" +#string STR_BOOT_URI_PROMPT #language en-US "Boot URI" +#string STR_BOOT_URI_HELP #language en-US "A new Boot Option will be created according to this Boot URI." +#string STR_BOOT_DESCRIPTION_PROMPT #language en-US "Input the description" +#string STR_NULL_STRING #language en-US "" diff --git a/NetworkPkg/HttpBootDxe/HttpBootConfigVfr.vfr b/NetworkPkg/HttpBootDxe/HttpBootConfigVfr.vfr new file mode 100644 index 000000000..65a60216b --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootConfigVfr.vfr @@ -0,0 +1,49 @@ +/** @file + VFR file used by the HTTP Boot configuration component. + + Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpBootConfigNVDataStruc.h" + + +formset + guid = HTTP_BOOT_CONFIG_GUID, + title = STRING_TOKEN(STR_HTTP_BOOT_CONFIG_FORM_TITLE), + help = STRING_TOKEN(STR_HTTP_BOOT_CONFIG_FORM_HELP), + + varstore HTTP_BOOT_CONFIG_IFR_NVDATA, + name = HTTP_BOOT_CONFIG_IFR_NVDATA, + guid = HTTP_BOOT_CONFIG_GUID; + + form formid = FORMID_MAIN_FORM, + title = STRING_TOKEN(STR_HTTP_BOOT_CONFIG_FORM_TITLE); + + string varid = HTTP_BOOT_CONFIG_IFR_NVDATA.Description, + prompt = STRING_TOKEN(STR_BOOT_DESCRIPTION_PROMPT), + help = STRING_TOKEN(STR_NULL_STRING), + minsize = DESCRIPTION_STR_MIN_SIZE, + maxsize = DESCRIPTION_STR_MAX_SIZE, + endstring; + + oneof varid = HTTP_BOOT_CONFIG_IFR_NVDATA.IpVersion, + prompt = STRING_TOKEN(STR_HTTP_BOOT_IP_VERSION_PROMPT), + help = STRING_TOKEN(STR_HTTP_BOOT_IP_VERSION_HELP), + option text = STRING_TOKEN(STR_HTTP_BOOT_IP_VERSION_4), value = HTTP_BOOT_IP_VERSION_4, flags = DEFAULT; + option text = STRING_TOKEN(STR_HTTP_BOOT_IP_VERSION_6), value = HTTP_BOOT_IP_VERSION_6, flags = 0; + endoneof; + + string varid = HTTP_BOOT_CONFIG_IFR_NVDATA.Uri, + prompt = STRING_TOKEN(STR_BOOT_URI_PROMPT), + help = STRING_TOKEN(STR_BOOT_URI_HELP), + flags = INTERACTIVE, + key = KEY_INITIATOR_URI, + minsize = URI_STR_MIN_SIZE, + maxsize = URI_STR_MAX_SIZE, + endstring; + endform; + +endformset; diff --git a/NetworkPkg/HttpBootDxe/HttpBootDhcp4.c b/NetworkPkg/HttpBootDxe/HttpBootDhcp4.c new file mode 100644 index 000000000..b5dd6e095 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootDhcp4.c @@ -0,0 +1,912 @@ +/** @file + Functions implementation related with DHCPv4 for HTTP boot driver. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpBootDxe.h" + +// +// This is a map from the interested DHCP4 option tags' index to the tag value. +// +UINT8 mInterestedDhcp4Tags[HTTP_BOOT_DHCP4_TAG_INDEX_MAX] = { + DHCP4_TAG_BOOTFILE_LEN, + DHCP4_TAG_OVERLOAD, + DHCP4_TAG_MSG_TYPE, + DHCP4_TAG_SERVER_ID, + DHCP4_TAG_VENDOR_CLASS_ID, + DHCP4_TAG_BOOTFILE, + DHCP4_TAG_DNS_SERVER +}; + +// +// There are 4 times retries with the value of 4, 8, 16 and 32, refers to UEFI 2.5 spec. +// +UINT32 mHttpDhcpTimeout[4] = {4, 8, 16, 32}; + +/** + Build the options buffer for the DHCPv4 request packet. + + @param[in] Private Pointer to HTTP boot driver private data. + @param[out] OptList Pointer to the option pointer array. + @param[in] Buffer Pointer to the buffer to contain the option list. + + @return Index The count of the built-in options. + +**/ +UINT32 +HttpBootBuildDhcp4Options ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + OUT EFI_DHCP4_PACKET_OPTION **OptList, + IN UINT8 *Buffer + ) +{ + HTTP_BOOT_DHCP4_OPTION_ENTRY OptEnt; + UINT16 Value; + UINT32 Index; + + Index = 0; + OptList[0] = (EFI_DHCP4_PACKET_OPTION *) Buffer; + + // + // Append parameter request list option. + // + OptList[Index]->OpCode = DHCP4_TAG_PARA_LIST; + OptList[Index]->Length = 27; + OptEnt.Para = (HTTP_BOOT_DHCP4_OPTION_PARA *) OptList[Index]->Data; + OptEnt.Para->ParaList[0] = DHCP4_TAG_NETMASK; + OptEnt.Para->ParaList[1] = DHCP4_TAG_TIME_OFFSET; + OptEnt.Para->ParaList[2] = DHCP4_TAG_ROUTER; + OptEnt.Para->ParaList[3] = DHCP4_TAG_TIME_SERVER; + OptEnt.Para->ParaList[4] = DHCP4_TAG_NAME_SERVER; + OptEnt.Para->ParaList[5] = DHCP4_TAG_DNS_SERVER; + OptEnt.Para->ParaList[6] = DHCP4_TAG_HOSTNAME; + OptEnt.Para->ParaList[7] = DHCP4_TAG_BOOTFILE_LEN; + OptEnt.Para->ParaList[8] = DHCP4_TAG_DOMAINNAME; + OptEnt.Para->ParaList[9] = DHCP4_TAG_ROOTPATH; + OptEnt.Para->ParaList[10] = DHCP4_TAG_EXTEND_PATH; + OptEnt.Para->ParaList[11] = DHCP4_TAG_EMTU; + OptEnt.Para->ParaList[12] = DHCP4_TAG_TTL; + OptEnt.Para->ParaList[13] = DHCP4_TAG_BROADCAST; + OptEnt.Para->ParaList[14] = DHCP4_TAG_NIS_DOMAIN; + OptEnt.Para->ParaList[15] = DHCP4_TAG_NIS_SERVER; + OptEnt.Para->ParaList[16] = DHCP4_TAG_NTP_SERVER; + OptEnt.Para->ParaList[17] = DHCP4_TAG_VENDOR; + OptEnt.Para->ParaList[18] = DHCP4_TAG_REQUEST_IP; + OptEnt.Para->ParaList[19] = DHCP4_TAG_LEASE; + OptEnt.Para->ParaList[20] = DHCP4_TAG_SERVER_ID; + OptEnt.Para->ParaList[21] = DHCP4_TAG_T1; + OptEnt.Para->ParaList[22] = DHCP4_TAG_T2; + OptEnt.Para->ParaList[23] = DHCP4_TAG_VENDOR_CLASS_ID; + OptEnt.Para->ParaList[25] = DHCP4_TAG_BOOTFILE; + OptEnt.Para->ParaList[26] = DHCP4_TAG_UUID; + Index++; + OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]); + + // + // Append UUID/Guid-based client identifier option + // + OptList[Index]->OpCode = DHCP4_TAG_UUID; + OptList[Index]->Length = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_UUID); + OptEnt.Uuid = (HTTP_BOOT_DHCP4_OPTION_UUID *) OptList[Index]->Data; + OptEnt.Uuid->Type = 0; + if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) OptEnt.Uuid->Guid))) { + // + // Zero the Guid to indicate NOT programable if failed to get system Guid. + // + ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID)); + } + Index++; + OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]); + + // + // Append client network device interface option + // + OptList[Index]->OpCode = DHCP4_TAG_UNDI; + OptList[Index]->Length = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_UNDI); + OptEnt.Undi = (HTTP_BOOT_DHCP4_OPTION_UNDI *) OptList[Index]->Data; + + if (Private->Nii != NULL) { + OptEnt.Undi->Type = Private->Nii->Type; + OptEnt.Undi->MajorVer = Private->Nii->MajorVer; + OptEnt.Undi->MinorVer = Private->Nii->MinorVer; + } else { + OptEnt.Undi->Type = DEFAULT_UNDI_TYPE; + OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR; + OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR; + } + + Index++; + OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]); + + // + // Append client system architecture option + // + OptList[Index]->OpCode = DHCP4_TAG_ARCH; + OptList[Index]->Length = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_ARCH); + OptEnt.Arch = (HTTP_BOOT_DHCP4_OPTION_ARCH *) OptList[Index]->Data; + Value = HTONS (EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE); + CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16)); + Index++; + OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]); + + // + // Append vendor class identify option + // + OptList[Index]->OpCode = DHCP4_TAG_VENDOR_CLASS_ID; + OptList[Index]->Length = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_CLID); + OptEnt.Clid = (HTTP_BOOT_DHCP4_OPTION_CLID *) OptList[Index]->Data; + CopyMem ( + OptEnt.Clid, + DEFAULT_CLASS_ID_DATA, + sizeof (HTTP_BOOT_DHCP4_OPTION_CLID) + ); + HttpBootUintnToAscDecWithFormat ( + EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE, + OptEnt.Clid->ArchitectureType, + sizeof (OptEnt.Clid->ArchitectureType) + ); + + if (Private->Nii != NULL) { + CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName)); + HttpBootUintnToAscDecWithFormat (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor)); + HttpBootUintnToAscDecWithFormat (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor)); + } + + Index++; + + return Index; +} + +/** + Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer. + + @param[in] Buffer Pointer to the option buffer. + @param[in] Length Length of the option buffer. + @param[in] OptTag Tag of the required option. + + @retval NULL Failed to find the required option. + @retval Others The position of the required option. + +**/ +EFI_DHCP4_PACKET_OPTION * +HttpBootParseDhcp4Options ( + IN UINT8 *Buffer, + IN UINT32 Length, + IN UINT8 OptTag + ) +{ + EFI_DHCP4_PACKET_OPTION *Option; + UINT32 Offset; + + Option = (EFI_DHCP4_PACKET_OPTION *) Buffer; + Offset = 0; + + while (Offset < Length && Option->OpCode != DHCP4_TAG_EOP) { + + if (Option->OpCode == OptTag) { + // + // Found the required option. + // + return Option; + } + + // + // Skip the current option to the next. + // + if (Option->OpCode == DHCP4_TAG_PAD) { + Offset++; + } else { + Offset += Option->Length + 2; + } + + Option = (EFI_DHCP4_PACKET_OPTION *) (Buffer + Offset); + } + + return NULL; +} + +/** + Cache the DHCPv4 packet. + + @param[in] Dst Pointer to the cache buffer for DHCPv4 packet. + @param[in] Src Pointer to the DHCPv4 packet to be cached. + + @retval EFI_SUCCESS Packet is copied. + @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet. + +**/ +EFI_STATUS +HttpBootCacheDhcp4Packet ( + IN EFI_DHCP4_PACKET *Dst, + IN EFI_DHCP4_PACKET *Src + ) +{ + if (Dst->Size < Src->Length) { + return EFI_BUFFER_TOO_SMALL; + } + + CopyMem (&Dst->Dhcp4, &Src->Dhcp4, Src->Length); + Dst->Length = Src->Length; + + return EFI_SUCCESS; +} + +/** + Parse the cached DHCPv4 packet, including all the options. + + @param[in] Cache4 Pointer to cached DHCPv4 packet. + + @retval EFI_SUCCESS Parsed the DHCPv4 packet successfully. + @retval EFI_DEVICE_ERROR Failed to parse an invalid packet. + +**/ +EFI_STATUS +HttpBootParseDhcp4Packet ( + IN HTTP_BOOT_DHCP4_PACKET_CACHE *Cache4 + ) +{ + EFI_DHCP4_PACKET *Offer; + EFI_DHCP4_PACKET_OPTION **Options; + UINTN Index; + EFI_DHCP4_PACKET_OPTION *Option; + BOOLEAN IsProxyOffer; + BOOLEAN IsHttpOffer; + BOOLEAN IsDnsOffer; + BOOLEAN IpExpressedUri; + UINT8 *Ptr8; + EFI_STATUS Status; + HTTP_BOOT_OFFER_TYPE OfferType; + EFI_IPv4_ADDRESS IpAddr; + BOOLEAN FileFieldOverloaded; + + IsDnsOffer = FALSE; + IpExpressedUri = FALSE; + IsProxyOffer = FALSE; + IsHttpOffer = FALSE; + FileFieldOverloaded = FALSE; + + ZeroMem (Cache4->OptList, sizeof (Cache4->OptList)); + + Offer = &Cache4->Packet.Offer; + Options = Cache4->OptList; + + // + // Parse DHCPv4 options in this offer, and store the pointers. + // First, try to parse DHCPv4 options from the DHCP optional parameters field. + // + for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) { + Options[Index] = HttpBootParseDhcp4Options ( + Offer->Dhcp4.Option, + GET_OPTION_BUFFER_LEN (Offer), + mInterestedDhcp4Tags[Index] + ); + } + // + // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132. + // If yes, try to parse options from the BootFileName field, then ServerName field. + // + Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_OVERLOAD]; + if (Option != NULL) { + if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_FILE) != 0) { + FileFieldOverloaded = TRUE; + for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) { + if (Options[Index] == NULL) { + Options[Index] = HttpBootParseDhcp4Options ( + (UINT8 *) Offer->Dhcp4.Header.BootFileName, + sizeof (Offer->Dhcp4.Header.BootFileName), + mInterestedDhcp4Tags[Index] + ); + } + } + } + if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_SERVER_NAME) != 0) { + for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) { + if (Options[Index] == NULL) { + Options[Index] = HttpBootParseDhcp4Options ( + (UINT8 *) Offer->Dhcp4.Header.ServerName, + sizeof (Offer->Dhcp4.Header.ServerName), + mInterestedDhcp4Tags[Index] + ); + } + } + } + } + + // + // The offer with "yiaddr" is a proxy offer. + // + if (Offer->Dhcp4.Header.YourAddr.Addr[0] == 0) { + IsProxyOffer = TRUE; + } + + // + // The offer with "HTTPClient" is a Http offer. + // + Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_CLASS_ID]; + if ((Option != NULL) && (Option->Length >= 10) && + (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 10) == 0)) { + IsHttpOffer = TRUE; + } + + // + // The offer with Domain Server is a DNS offer. + // + Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER]; + if (Option != NULL) { + IsDnsOffer = TRUE; + } + + // + // Parse boot file name: + // Boot URI information is provided thru 'file' field in DHCP Header or option 67. + // According to RFC 2132, boot file name should be read from DHCP option 67 (bootfile name) if present. + // Otherwise, read from boot file field in DHCP header. + // + if (Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] != NULL) { + // + // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null + // terminated string. So force to append null terminated character at the end of string. + // + Ptr8 = (UINT8*)&Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data[0]; + Ptr8 += Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Length; + if (*(Ptr8 - 1) != '\0') { + *Ptr8 = '\0'; + } + } else if (!FileFieldOverloaded && Offer->Dhcp4.Header.BootFileName[0] != 0) { + // + // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it. + // Do not count dhcp option header here, or else will destroy the serverhostname. + // + Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *) + (&Offer->Dhcp4.Header.BootFileName[0] - + OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0])); + } + + // + // Http offer must have a boot URI. + // + if (IsHttpOffer && Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] == NULL) { + return EFI_DEVICE_ERROR; + } + + // + // Try to retrieve the IP of HTTP server from URI. + // + if (IsHttpOffer) { + Status = HttpParseUrl ( + (CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data, + (UINT32) AsciiStrLen ((CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data), + FALSE, + &Cache4->UriParser + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + Status = HttpUrlGetIp4 ( + (CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data, + Cache4->UriParser, + &IpAddr + ); + IpExpressedUri = !EFI_ERROR (Status); + } + + // + // Determine offer type of the DHCPv4 packet. + // + if (IsHttpOffer) { + if (IpExpressedUri) { + if (IsProxyOffer) { + OfferType = HttpOfferTypeProxyIpUri; + } else { + OfferType = IsDnsOffer ? HttpOfferTypeDhcpIpUriDns : HttpOfferTypeDhcpIpUri; + } + } else { + if (!IsProxyOffer) { + OfferType = IsDnsOffer ? HttpOfferTypeDhcpNameUriDns : HttpOfferTypeDhcpNameUri; + } else { + OfferType = HttpOfferTypeProxyNameUri; + } + } + + } else { + if (!IsProxyOffer) { + OfferType = IsDnsOffer ? HttpOfferTypeDhcpDns : HttpOfferTypeDhcpOnly; + } else { + if (Cache4->UriParser != NULL) { + FreePool (Cache4->UriParser); + } + return EFI_DEVICE_ERROR; + } + } + + Cache4->OfferType = OfferType; + return EFI_SUCCESS; +} + +/** + Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount. + + @param[in] Private Pointer to HTTP boot driver private data. + @param[in] RcvdOffer Pointer to the received offer packet. + + @retval EFI_SUCCESS Cache and parse the packet successfully. + @retval Others Operation failed. +**/ +EFI_STATUS +HttpBootCacheDhcp4Offer ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN EFI_DHCP4_PACKET *RcvdOffer + ) +{ + HTTP_BOOT_DHCP4_PACKET_CACHE *Cache4; + EFI_DHCP4_PACKET *Offer; + HTTP_BOOT_OFFER_TYPE OfferType; + EFI_STATUS Status; + + ASSERT (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM); + Cache4 = &Private->OfferBuffer[Private->OfferNum].Dhcp4; + Offer = &Cache4->Packet.Offer; + + // + // Cache the content of DHCPv4 packet firstly. + // + Status = HttpBootCacheDhcp4Packet (Offer, RcvdOffer); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Validate the DHCPv4 packet, and parse the options and offer type. + // + if (EFI_ERROR (HttpBootParseDhcp4Packet (Cache4))) { + return EFI_ABORTED; + } + + // + // Determine whether cache the current offer by type, and record OfferIndex and OfferCount. + // + OfferType = Cache4->OfferType; + ASSERT (OfferType < HttpOfferTypeMax); + ASSERT (Private->OfferCount[OfferType] < HTTP_BOOT_OFFER_MAX_NUM); + Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum; + Private->OfferCount[OfferType]++; + Private->OfferNum++; + + return EFI_SUCCESS; +} + +/** + Select an DHCPv4 or DHCP6 offer, and record SelectIndex and SelectProxyType. + + @param[in] Private Pointer to HTTP boot driver private data. + +**/ +VOID +HttpBootSelectDhcpOffer ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + Private->SelectIndex = 0; + Private->SelectProxyType = HttpOfferTypeMax; + + if (Private->FilePathUri != NULL) { + // + // We are in home environment, the URI is already specified. + // Just need to choose a DHCP offer. + // The offer with DNS server address takes priority here. + // + if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1; + + } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1; + + } else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1; + + } else if (Private->OfferCount[HttpOfferTypeDhcpOnly] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1; + + } else if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1; + } + + } else { + // + // We are in corporate environment. + // + // Priority1: HttpOfferTypeDhcpIpUri or HttpOfferTypeDhcpIpUriDns + // Priority2: HttpOfferTypeDhcpNameUriDns + // Priority3: HttpOfferTypeDhcpOnly + HttpOfferTypeProxyIpUri + // Priority4: HttpOfferTypeDhcpDns + HttpOfferTypeProxyIpUri + // Priority5: HttpOfferTypeDhcpDns + HttpOfferTypeProxyNameUri + // Priority6: HttpOfferTypeDhcpDns + HttpOfferTypeDhcpNameUri + // + if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1; + + } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1; + + }else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1; + + } else if (Private->OfferCount[HttpOfferTypeDhcpOnly] > 0 && + Private->OfferCount[HttpOfferTypeProxyIpUri] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1; + Private->SelectProxyType = HttpOfferTypeProxyIpUri; + + } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 && + Private->OfferCount[HttpOfferTypeProxyIpUri] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1; + Private->SelectProxyType = HttpOfferTypeProxyIpUri; + + } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 && + Private->OfferCount[HttpOfferTypeProxyNameUri] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1; + Private->SelectProxyType = HttpOfferTypeProxyNameUri; + + } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 && + Private->OfferCount[HttpOfferTypeDhcpNameUri] > 0) { + + Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1; + Private->SelectProxyType = HttpOfferTypeDhcpNameUri; + } + } +} + + +/** + EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver + to intercept events that occurred in the configuration process. + + @param[in] This Pointer to the EFI DHCPv4 Protocol. + @param[in] Context Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure(). + @param[in] CurrentState The current operational state of the EFI DHCPv4 Protocol driver. + @param[in] Dhcp4Event The event that occurs in the current state, which usually means a + state transition. + @param[in] Packet The DHCPv4 packet that is going to be sent or already received. + @param[out] NewPacket The packet that is used to replace the above Packet. + + @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process. + @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol + driver will continue to wait for more DHCPOFFER packets until the + retry timeout expires. + @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process + and return to the Dhcp4Init or Dhcp4InitReboot state. + +**/ +EFI_STATUS +EFIAPI +HttpBootDhcp4CallBack ( + IN EFI_DHCP4_PROTOCOL *This, + IN VOID *Context, + IN EFI_DHCP4_STATE CurrentState, + IN EFI_DHCP4_EVENT Dhcp4Event, + IN EFI_DHCP4_PACKET *Packet OPTIONAL, + OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL + ) +{ + HTTP_BOOT_PRIVATE_DATA *Private; + EFI_DHCP4_PACKET_OPTION *MaxMsgSize; + UINT16 Value; + EFI_STATUS Status; + BOOLEAN Received; + + if ((Dhcp4Event != Dhcp4SendDiscover) && + (Dhcp4Event != Dhcp4RcvdOffer) && + (Dhcp4Event != Dhcp4SendRequest) && + (Dhcp4Event != Dhcp4RcvdAck) && + (Dhcp4Event != Dhcp4SelectOffer)) { + return EFI_SUCCESS; + } + + Private = (HTTP_BOOT_PRIVATE_DATA *) Context; + + // + // Override the Maximum DHCP Message Size. + // + MaxMsgSize = HttpBootParseDhcp4Options ( + Packet->Dhcp4.Option, + GET_OPTION_BUFFER_LEN (Packet), + DHCP4_TAG_MAXMSG + ); + if (MaxMsgSize != NULL) { + Value = HTONS (HTTP_BOOT_DHCP4_PACKET_MAX_SIZE); + CopyMem (MaxMsgSize->Data, &Value, sizeof (Value)); + } + + // + // Callback to user if any packets sent or received. + // + if (Private->HttpBootCallback != NULL && Dhcp4Event != Dhcp4SelectOffer) { + Received = (BOOLEAN) (Dhcp4Event == Dhcp4RcvdOffer || Dhcp4Event == Dhcp4RcvdAck); + Status = Private->HttpBootCallback->Callback ( + Private->HttpBootCallback, + HttpBootDhcp4, + Received, + Packet->Length, + &Packet->Dhcp4 + ); + if (EFI_ERROR (Status)) { + return EFI_ABORTED; + } + } + + Status = EFI_SUCCESS; + switch (Dhcp4Event) { + case Dhcp4RcvdOffer: + Status = EFI_NOT_READY; + if (Packet->Length > HTTP_BOOT_DHCP4_PACKET_MAX_SIZE) { + // + // Ignore the incoming packets which exceed the maximum length. + // + break; + } + if (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM) { + // + // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record + // the OfferIndex and OfferCount. + // If error happens, just ignore this packet and continue to wait more offer. + // + HttpBootCacheDhcp4Offer (Private, Packet); + } + break; + + case Dhcp4SelectOffer: + // + // Select offer according to the priority in UEFI spec, and record the SelectIndex + // and SelectProxyType. + // + HttpBootSelectDhcpOffer (Private); + + if (Private->SelectIndex == 0) { + Status = EFI_ABORTED; + } else { + *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer; + } + break; + + default: + break; + } + + return Status; +} + +/** + This function will register the IPv4 gateway address to the network device. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + + @retval EFI_SUCCESS The new IP configuration has been configured successfully. + @retval Others Failed to configure the address. + +**/ +EFI_STATUS +HttpBootRegisterIp4Gateway ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + EFI_STATUS Status; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + + ASSERT (!Private->UsingIpv6); + + Ip4Config2 = Private->Ip4Config2; + + // + // Configure the gateway if valid. + // + if (!EFI_IP4_EQUAL (&Private->GatewayIp, &mZeroIp4Addr)) { + Status = Ip4Config2->SetData ( + Ip4Config2, + Ip4Config2DataTypeGateway, + sizeof (EFI_IPv4_ADDRESS), + &Private->GatewayIp + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + return EFI_SUCCESS; +} + +/** + This function will register the default DNS addresses to the network device. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + @param[in] DataLength Size of the buffer pointed to by DnsServerData in bytes. + @param[in] DnsServerData Point a list of DNS server address in an array + of EFI_IPv4_ADDRESS instances. + + @retval EFI_SUCCESS The DNS configuration has been configured successfully. + @retval Others Failed to configure the address. + +**/ +EFI_STATUS +HttpBootRegisterIp4Dns ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN UINTN DataLength, + IN VOID *DnsServerData + ) +{ + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + + ASSERT (!Private->UsingIpv6); + + Ip4Config2 = Private->Ip4Config2; + + return Ip4Config2->SetData ( + Ip4Config2, + Ip4Config2DataTypeDnsServer, + DataLength, + DnsServerData + ); +} + + +/** + This function will switch the IP4 configuration policy to Static. + + @param[in] Private Pointer to HTTP boot driver private data. + + @retval EFI_SUCCESS The policy is already configured to static. + @retval Others Other error as indicated.. + +**/ +EFI_STATUS +HttpBootSetIp4Policy ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + EFI_IP4_CONFIG2_POLICY Policy; + EFI_STATUS Status; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + UINTN DataSize; + + Ip4Config2 = Private->Ip4Config2; + + DataSize = sizeof (EFI_IP4_CONFIG2_POLICY); + Status = Ip4Config2->GetData ( + Ip4Config2, + Ip4Config2DataTypePolicy, + &DataSize, + &Policy + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Policy != Ip4Config2PolicyStatic) { + Policy = Ip4Config2PolicyStatic; + Status= Ip4Config2->SetData ( + Ip4Config2, + Ip4Config2DataTypePolicy, + sizeof (EFI_IP4_CONFIG2_POLICY), + &Policy + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + return EFI_SUCCESS; +} + +/** + Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other Http boot information. + + @param[in] Private Pointer to HTTP boot driver private data. + + @retval EFI_SUCCESS The D.O.R.A process successfully finished. + @retval Others Failed to finish the D.O.R.A process. + +**/ +EFI_STATUS +HttpBootDhcp4Dora ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + EFI_DHCP4_PROTOCOL *Dhcp4; + UINT32 OptCount; + EFI_DHCP4_PACKET_OPTION *OptList[HTTP_BOOT_DHCP4_OPTION_MAX_NUM]; + UINT8 Buffer[HTTP_BOOT_DHCP4_OPTION_MAX_SIZE]; + EFI_DHCP4_CONFIG_DATA Config; + EFI_STATUS Status; + EFI_DHCP4_MODE_DATA Mode; + + Dhcp4 = Private->Dhcp4; + ASSERT (Dhcp4 != NULL); + + Status = HttpBootSetIp4Policy (Private); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Build option list for the request packet. + // + OptCount = HttpBootBuildDhcp4Options (Private, OptList, Buffer); + ASSERT (OptCount > 0); + + ZeroMem (&Config, sizeof(Config)); + Config.OptionCount = OptCount; + Config.OptionList = OptList; + Config.Dhcp4Callback = HttpBootDhcp4CallBack; + Config.CallbackContext = Private; + Config.DiscoverTryCount = HTTP_BOOT_DHCP_RETRIES; + Config.DiscoverTimeout = mHttpDhcpTimeout; + + // + // Configure the DHCPv4 instance for HTTP boot. + // + Status = Dhcp4->Configure (Dhcp4, &Config); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Initialize the record fields for DHCPv4 offer in private data. + // + Private->OfferNum = 0; + ZeroMem (Private->OfferCount, sizeof (Private->OfferCount)); + ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex)); + + // + // Start DHCPv4 D.O.R.A. process to acquire IPv4 address. + // + Status = Dhcp4->Start (Dhcp4, NULL); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Get the acquired IPv4 address and store them. + // + Status = Dhcp4->GetModeData (Dhcp4, &Mode); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + ASSERT (Mode.State == Dhcp4Bound); + CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS)); + + Status = HttpBootRegisterIp4Gateway (Private); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + AsciiPrint ("\n Station IP address is "); + HttpBootShowIp4Addr (&Private->StationIp.v4); + AsciiPrint ("\n"); + +ON_EXIT: + if (EFI_ERROR (Status)) { + Dhcp4->Stop (Dhcp4); + Dhcp4->Configure (Dhcp4, NULL); + } else { + ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA)); + Dhcp4->Configure (Dhcp4, &Config); + } + + return Status; +} diff --git a/NetworkPkg/HttpBootDxe/HttpBootDhcp4.h b/NetworkPkg/HttpBootDxe/HttpBootDhcp4.h new file mode 100644 index 000000000..2c2fa2830 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootDhcp4.h @@ -0,0 +1,250 @@ +/** @file + Functions declaration related with DHCPv4 for HTTP boot driver. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_UEFI_HTTP_BOOT_DHCP4_H__ +#define __EFI_UEFI_HTTP_BOOT_DHCP4_H__ + +#define HTTP_BOOT_DHCP4_OPTION_MAX_NUM 16 +#define HTTP_BOOT_DHCP4_OPTION_MAX_SIZE 312 +#define HTTP_BOOT_DHCP4_PACKET_MAX_SIZE 1472 + +#define HTTP_BOOT_DHCP4_OPCODE_REQUEST 1 +#define HTTP_BOOT_DHCP4_OPCODE_REPLY 2 +#define HTTP_BOOT_DHCP4_MSG_TYPE_REQUEST 3 +#define HTTP_BOOT_DHCP4_MAGIC 0x63538263 // network byte order + +#define HTTP_BOOT_DHCP4_OVERLOAD_FILE 1 +#define HTTP_BOOT_DHCP4_OVERLOAD_SERVER_NAME 2 + +/// +/// HTTP Tag definition that identifies the processor +/// and programming environment of the client system. +/// These identifiers are defined by IETF: +/// http://www.ietf.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml +/// +#if defined (MDE_CPU_IA32) +#define EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE HTTP_CLIENT_ARCH_IA32 +#elif defined (MDE_CPU_X64) +#define EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE HTTP_CLIENT_ARCH_X64 +#elif defined (MDE_CPU_ARM) +#define EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE HTTP_CLIENT_ARCH_ARM +#elif defined (MDE_CPU_AARCH64) +#define EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE HTTP_CLIENT_ARCH_AARCH64 +#elif defined (MDE_CPU_EBC) +#define EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE HTTP_CLIENT_ARCH_EBC +#endif + +/// DHCP offer types among HTTP boot. +/// Dhcp4 and Dhcp6 share this definition, and corresponding +/// relatioinship is as follows: +/// Dhcp4Discover <> Dhcp6Solicit +/// Dhcp4Offer <> Dhcp6Advertise +/// Dhcp4Request <> Dhcp6Request +/// Dhcp4Ack <> DHcp6Reply +/// +typedef enum { + // + // + // + HttpOfferTypeDhcpIpUri, + // + // + // + HttpOfferTypeDhcpIpUriDns, + // + // + // + HttpOfferTypeDhcpNameUriDns, + // + // + // + HttpOfferTypeDhcpDns, + // + // + // + HttpOfferTypeDhcpOnly, + // + // or + // + // + HttpOfferTypeProxyNameUri, + // + // or + // + // + HttpOfferTypeProxyIpUri, + // + // + // + HttpOfferTypeDhcpNameUri, + HttpOfferTypeMax +} HTTP_BOOT_OFFER_TYPE; + +#define HTTP_BOOT_DHCP_RETRIES 4 +#define HTTP_BOOT_OFFER_MAX_NUM 16 + +// The array index of the DHCP4 option tag interested +// +#define HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE_LEN 0 +#define HTTP_BOOT_DHCP4_TAG_INDEX_OVERLOAD 1 +#define HTTP_BOOT_DHCP4_TAG_INDEX_MSG_TYPE 2 +#define HTTP_BOOT_DHCP4_TAG_INDEX_SERVER_ID 3 +#define HTTP_BOOT_DHCP4_TAG_INDEX_CLASS_ID 4 +#define HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE 5 +#define HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER 6 +#define HTTP_BOOT_DHCP4_TAG_INDEX_MAX 7 + +#pragma pack(1) + +typedef struct { + UINT8 ParaList[135]; +} HTTP_BOOT_DHCP4_OPTION_PARA; + +typedef struct { + UINT16 Size; +} HTTP_BOOT_DHCP4_OPTION_MAX_MESG_SIZE; + +typedef struct { + UINT8 Type; + UINT8 MajorVer; + UINT8 MinorVer; +} HTTP_BOOT_DHCP4_OPTION_UNDI; + +typedef struct { + UINT8 Type; +} HTTP_BOOT_DHCP4_OPTION_MESG; + +typedef struct { + UINT16 Type; +} HTTP_BOOT_DHCP4_OPTION_ARCH; + +typedef struct { + UINT8 ClassIdentifier[11]; + UINT8 ArchitecturePrefix[5]; + UINT8 ArchitectureType[5]; + UINT8 Lit3[1]; + UINT8 InterfaceName[4]; + UINT8 Lit4[1]; + UINT8 UndiMajor[3]; + UINT8 UndiMinor[3]; +} HTTP_BOOT_DHCP4_OPTION_CLID; + +typedef struct { + UINT8 Type; + UINT8 Guid[16]; +} HTTP_BOOT_DHCP4_OPTION_UUID; + +typedef struct { + UINT16 Type; + UINT16 Layer; +} HTTP_BOOT_OPTION_BOOT_ITEM; + +#pragma pack() + +typedef union { + HTTP_BOOT_DHCP4_OPTION_PARA *Para; + HTTP_BOOT_DHCP4_OPTION_UNDI *Undi; + HTTP_BOOT_DHCP4_OPTION_ARCH *Arch; + HTTP_BOOT_DHCP4_OPTION_CLID *Clid; + HTTP_BOOT_DHCP4_OPTION_UUID *Uuid; + HTTP_BOOT_DHCP4_OPTION_MESG *Mesg; + HTTP_BOOT_DHCP4_OPTION_MAX_MESG_SIZE *MaxMesgSize; +} HTTP_BOOT_DHCP4_OPTION_ENTRY; + +#define GET_NEXT_DHCP_OPTION(Opt) \ + (EFI_DHCP4_PACKET_OPTION *) ((UINT8 *) (Opt) + \ + sizeof (EFI_DHCP4_PACKET_OPTION) + (Opt)->Length - 1) + +#define GET_OPTION_BUFFER_LEN(Pkt) \ + ((Pkt)->Length - sizeof (EFI_DHCP4_HEADER) - 4) + +#define DEFAULT_CLASS_ID_DATA "HTTPClient:Arch:xxxxx:UNDI:003000" +#define DEFAULT_UNDI_TYPE 1 +#define DEFAULT_UNDI_MAJOR 3 +#define DEFAULT_UNDI_MINOR 0 + +typedef struct { + UINT32 Reserved; +} HTTP_BOOT_VENDOR_OPTION; + +#define HTTP_CACHED_DHCP4_PACKET_MAX_SIZE (OFFSET_OF (EFI_DHCP4_PACKET, Dhcp4) + HTTP_BOOT_DHCP4_PACKET_MAX_SIZE) + +typedef union { + EFI_DHCP4_PACKET Offer; + EFI_DHCP4_PACKET Ack; + UINT8 Buffer[HTTP_CACHED_DHCP4_PACKET_MAX_SIZE]; +} HTTP_BOOT_DHCP4_PACKET; + +typedef struct { + // + // URI component + // + CHAR8 *Scheme; + CHAR8 *Authority; + CHAR8 *Path; + CHAR8 *Query; + CHAR8 *Fragment; /// TODO: may not required in HTTP URL + + CHAR8 *RegName; /// Point to somewhere in Authority + BOOLEAN AddrIsOk; + EFI_IP_ADDRESS Address; + UINT16 Port; +} HTTP_BOOT_URI_CONTENT; + +typedef struct { + HTTP_BOOT_DHCP4_PACKET Packet; + HTTP_BOOT_OFFER_TYPE OfferType; + VOID *UriParser; + EFI_DHCP4_PACKET_OPTION *OptList[HTTP_BOOT_DHCP4_TAG_INDEX_MAX]; +} HTTP_BOOT_DHCP4_PACKET_CACHE; + +/** + Select an DHCPv4 or DHCP6 offer, and record SelectIndex and SelectProxyType. + + @param[in] Private Pointer to HTTP boot driver private data. + +**/ +VOID +HttpBootSelectDhcpOffer ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +/** + Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other Http boot information. + + @param[in] Private Pointer to HTTP_BOOT private data. + + @retval EFI_SUCCESS The D.O.R.A process successfully finished. + @retval Others Failed to finish the D.O.R.A process. + +**/ +EFI_STATUS +HttpBootDhcp4Dora ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +/** + This function will register the default DNS addresses to the network device. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + @param[in] DataLength Size of the buffer pointed to by DnsServerData in bytes. + @param[in] DnsServerData Point a list of DNS server address in an array + of EFI_IPv4_ADDRESS instances. + + @retval EFI_SUCCESS The DNS configuration has been configured successfully. + @retval Others Failed to configure the address. + +**/ +EFI_STATUS +HttpBootRegisterIp4Dns ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN UINTN DataLength, + IN VOID *DnsServerData + ); + +#endif diff --git a/NetworkPkg/HttpBootDxe/HttpBootDhcp6.c b/NetworkPkg/HttpBootDxe/HttpBootDhcp6.c new file mode 100644 index 000000000..8a1dd66ce --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootDhcp6.c @@ -0,0 +1,1032 @@ +/** @file + Functions implementation related with DHCPv6 for HTTP boot driver. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpBootDxe.h" + +/** + Build the options buffer for the DHCPv6 request packet. + + @param[in] Private The pointer to HTTP BOOT driver private data. + @param[out] OptList The pointer to the option pointer array. + @param[in] Buffer The pointer to the buffer to contain the option list. + + @return Index The count of the built-in options. + +**/ +UINT32 +HttpBootBuildDhcp6Options ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + OUT EFI_DHCP6_PACKET_OPTION **OptList, + IN UINT8 *Buffer + ) +{ + HTTP_BOOT_DHCP6_OPTION_ENTRY OptEnt; + UINT16 Value; + UINT32 Index; + + Index = 0; + OptList[0] = (EFI_DHCP6_PACKET_OPTION *) Buffer; + + // + // Append client option request option + // + OptList[Index]->OpCode = HTONS (DHCP6_OPT_ORO); + OptList[Index]->OpLen = HTONS (8); + OptEnt.Oro = (HTTP_BOOT_DHCP6_OPTION_ORO *) OptList[Index]->Data; + OptEnt.Oro->OpCode[0] = HTONS(DHCP6_OPT_BOOT_FILE_URL); + OptEnt.Oro->OpCode[1] = HTONS(DHCP6_OPT_BOOT_FILE_PARAM); + OptEnt.Oro->OpCode[2] = HTONS(DHCP6_OPT_DNS_SERVERS); + OptEnt.Oro->OpCode[3] = HTONS(DHCP6_OPT_VENDOR_CLASS); + Index++; + OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]); + + // + // Append client network device interface option + // + OptList[Index]->OpCode = HTONS (DHCP6_OPT_UNDI); + OptList[Index]->OpLen = HTONS ((UINT16)3); + OptEnt.Undi = (HTTP_BOOT_DHCP6_OPTION_UNDI *) OptList[Index]->Data; + + if (Private->Nii != NULL) { + OptEnt.Undi->Type = Private->Nii->Type; + OptEnt.Undi->MajorVer = Private->Nii->MajorVer; + OptEnt.Undi->MinorVer = Private->Nii->MinorVer; + } else { + OptEnt.Undi->Type = DEFAULT_UNDI_TYPE; + OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR; + OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR; + } + + Index++; + OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]); + + // + // Append client system architecture option + // + OptList[Index]->OpCode = HTONS (DHCP6_OPT_ARCH); + OptList[Index]->OpLen = HTONS ((UINT16) sizeof (HTTP_BOOT_DHCP6_OPTION_ARCH)); + OptEnt.Arch = (HTTP_BOOT_DHCP6_OPTION_ARCH *) OptList[Index]->Data; + Value = HTONS (EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE); + CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16)); + Index++; + OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]); + + // + // Append vendor class identify option. + // + OptList[Index]->OpCode = HTONS (DHCP6_OPT_VENDOR_CLASS); + OptList[Index]->OpLen = HTONS ((UINT16) sizeof (HTTP_BOOT_DHCP6_OPTION_VENDOR_CLASS)); + OptEnt.VendorClass = (HTTP_BOOT_DHCP6_OPTION_VENDOR_CLASS *) OptList[Index]->Data; + OptEnt.VendorClass->Vendor = HTONL (HTTP_BOOT_DHCP6_ENTERPRISE_NUM); + OptEnt.VendorClass->ClassLen = HTONS ((UINT16) sizeof (HTTP_BOOT_CLASS_ID)); + CopyMem ( + &OptEnt.VendorClass->ClassId, + DEFAULT_CLASS_ID_DATA, + sizeof (HTTP_BOOT_CLASS_ID) + ); + HttpBootUintnToAscDecWithFormat ( + EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE, + OptEnt.VendorClass->ClassId.ArchitectureType, + sizeof (OptEnt.VendorClass->ClassId.ArchitectureType) + ); + + if (Private->Nii != NULL) { + CopyMem ( + OptEnt.VendorClass->ClassId.InterfaceName, + Private->Nii->StringId, + sizeof (OptEnt.VendorClass->ClassId.InterfaceName) + ); + HttpBootUintnToAscDecWithFormat ( + Private->Nii->MajorVer, + OptEnt.VendorClass->ClassId.UndiMajor, + sizeof (OptEnt.VendorClass->ClassId.UndiMajor) + ); + HttpBootUintnToAscDecWithFormat ( + Private->Nii->MinorVer, + OptEnt.VendorClass->ClassId.UndiMinor, + sizeof (OptEnt.VendorClass->ClassId.UndiMinor) + ); + } + + Index++; + + return Index; +} + +/** + Parse out a DHCPv6 option by OptTag, and find the position in buffer. + + @param[in] Buffer The pointer to the option buffer. + @param[in] Length Length of the option buffer. + @param[in] OptTag The required option tag. + + @retval NULL Failed to parse the required option. + @retval Others The postion of the required option in buffer. + +**/ +EFI_DHCP6_PACKET_OPTION * +HttpBootParseDhcp6Options ( + IN UINT8 *Buffer, + IN UINT32 Length, + IN UINT16 OptTag + ) +{ + EFI_DHCP6_PACKET_OPTION *Option; + UINT32 Offset; + + Option = (EFI_DHCP6_PACKET_OPTION *) Buffer; + Offset = 0; + + // + // OpLen and OpCode here are both stored in network order. + // + while (Offset < Length) { + + if (NTOHS (Option->OpCode) == OptTag) { + + return Option; + } + + Offset += (NTOHS(Option->OpLen) + 4); + Option = (EFI_DHCP6_PACKET_OPTION *) (Buffer + Offset); + } + + return NULL; + +} + +/** + Parse the cached DHCPv6 packet, including all the options. + + @param[in] Cache6 The pointer to a cached DHCPv6 packet. + + @retval EFI_SUCCESS Parsed the DHCPv6 packet successfully. + @retval EFI_DEVICE_ERROR Failed to parse and invalid the packet. + +**/ +EFI_STATUS +HttpBootParseDhcp6Packet ( + IN HTTP_BOOT_DHCP6_PACKET_CACHE *Cache6 + ) +{ + EFI_DHCP6_PACKET *Offer; + EFI_DHCP6_PACKET_OPTION **Options; + EFI_DHCP6_PACKET_OPTION *Option; + HTTP_BOOT_OFFER_TYPE OfferType; + EFI_IPv6_ADDRESS IpAddr; + BOOLEAN IsProxyOffer; + BOOLEAN IsHttpOffer; + BOOLEAN IsDnsOffer; + BOOLEAN IpExpressedUri; + EFI_STATUS Status; + UINT32 Offset; + UINT32 Length; + + IsDnsOffer = FALSE; + IpExpressedUri = FALSE; + IsProxyOffer = TRUE; + IsHttpOffer = FALSE; + Offer = &Cache6->Packet.Offer; + Options = Cache6->OptList; + + ZeroMem (Cache6->OptList, sizeof (Cache6->OptList)); + + Option = (EFI_DHCP6_PACKET_OPTION *) (Offer->Dhcp6.Option); + Offset = 0; + Length = GET_DHCP6_OPTION_SIZE (Offer); + + // + // OpLen and OpCode here are both stored in network order, since they are from original packet. + // + while (Offset < Length) { + + if (NTOHS (Option->OpCode) == DHCP6_OPT_IA_NA) { + Options[HTTP_BOOT_DHCP6_IDX_IA_NA] = Option; + } else if (NTOHS (Option->OpCode) == DHCP6_OPT_BOOT_FILE_URL) { + // + // The server sends this option to inform the client about an URL to a boot file. + // + Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL] = Option; + } else if (NTOHS (Option->OpCode) == DHCP6_OPT_BOOT_FILE_PARAM) { + Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_PARAM] = Option; + } else if (NTOHS (Option->OpCode) == DHCP6_OPT_VENDOR_CLASS) { + Options[HTTP_BOOT_DHCP6_IDX_VENDOR_CLASS] = Option; + } else if (NTOHS (Option->OpCode) == DHCP6_OPT_DNS_SERVERS) { + Options[HTTP_BOOT_DHCP6_IDX_DNS_SERVER] = Option; + } + + Offset += (NTOHS (Option->OpLen) + 4); + Option = (EFI_DHCP6_PACKET_OPTION *) (Offer->Dhcp6.Option + Offset); + } + // + // The offer with assigned client address is NOT a proxy offer. + // An ia_na option, embeded with valid ia_addr option and a status_code of success. + // + Option = Options[HTTP_BOOT_DHCP6_IDX_IA_NA]; + if (Option != NULL) { + Option = HttpBootParseDhcp6Options ( + Option->Data + 12, + NTOHS (Option->OpLen), + DHCP6_OPT_STATUS_CODE + ); + if ((Option != NULL && Option->Data[0] == 0) || (Option == NULL)) { + IsProxyOffer = FALSE; + } + } + + // + // The offer with "HTTPClient" is a Http offer. + // + Option = Options[HTTP_BOOT_DHCP6_IDX_VENDOR_CLASS]; + + if (Option != NULL && + NTOHS(Option->OpLen) >= 16 && + CompareMem ((Option->Data + 6), DEFAULT_CLASS_ID_DATA, 10) == 0) { + IsHttpOffer = TRUE; + } + + // + // The offer with Domain Server is a DNS offer. + // + Option = Options[HTTP_BOOT_DHCP6_IDX_DNS_SERVER]; + if (Option != NULL) { + IsDnsOffer = TRUE; + } + + // + // Http offer must have a boot URI. + // + if (IsHttpOffer && Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL] == NULL) { + return EFI_DEVICE_ERROR; + } + + // + // Try to retrieve the IP of HTTP server from URI. + // + if (IsHttpOffer) { + Status = HttpParseUrl ( + (CHAR8*) Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data, + (UINT32) AsciiStrLen ((CHAR8*) Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data), + FALSE, + &Cache6->UriParser + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + Status = HttpUrlGetIp6 ( + (CHAR8*) Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data, + Cache6->UriParser, + &IpAddr + ); + IpExpressedUri = !EFI_ERROR (Status); + } + + // + // Determine offer type of the DHCPv6 packet. + // + if (IsHttpOffer) { + if (IpExpressedUri) { + if (IsProxyOffer) { + OfferType = HttpOfferTypeProxyIpUri; + } else { + OfferType = IsDnsOffer ? HttpOfferTypeDhcpIpUriDns : HttpOfferTypeDhcpIpUri; + } + } else { + if (!IsProxyOffer) { + OfferType = IsDnsOffer ? HttpOfferTypeDhcpNameUriDns : HttpOfferTypeDhcpNameUri; + } else { + OfferType = HttpOfferTypeProxyNameUri; + } + } + + } else { + if (!IsProxyOffer) { + OfferType = IsDnsOffer ? HttpOfferTypeDhcpDns : HttpOfferTypeDhcpOnly; + } else { + return EFI_DEVICE_ERROR; + } + } + + Cache6->OfferType = OfferType; + return EFI_SUCCESS; +} + +/** + Cache the DHCPv6 packet. + + @param[in] Dst The pointer to the cache buffer for DHCPv6 packet. + @param[in] Src The pointer to the DHCPv6 packet to be cached. + + @retval EFI_SUCCESS Packet is copied. + @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet. + +**/ +EFI_STATUS +HttpBootCacheDhcp6Packet ( + IN EFI_DHCP6_PACKET *Dst, + IN EFI_DHCP6_PACKET *Src + ) +{ + if (Dst->Size < Src->Length) { + return EFI_BUFFER_TOO_SMALL; + } + + CopyMem (&Dst->Dhcp6, &Src->Dhcp6, Src->Length); + Dst->Length = Src->Length; + + return EFI_SUCCESS; +} + +/** + Cache all the received DHCPv6 offers, and set OfferIndex and OfferCount. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + @param[in] RcvdOffer The pointer to the received offer packet. + + @retval EFI_SUCCESS Cache and parse the packet successfully. + @retval Others Operation failed. + +**/ +EFI_STATUS +HttpBootCacheDhcp6Offer ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN EFI_DHCP6_PACKET *RcvdOffer + ) +{ + HTTP_BOOT_DHCP6_PACKET_CACHE *Cache6; + EFI_DHCP6_PACKET *Offer; + HTTP_BOOT_OFFER_TYPE OfferType; + EFI_STATUS Status; + + Cache6 = &Private->OfferBuffer[Private->OfferNum].Dhcp6; + Offer = &Cache6->Packet.Offer; + + // + // Cache the content of DHCPv6 packet firstly. + // + Status = HttpBootCacheDhcp6Packet(Offer, RcvdOffer); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Validate the DHCPv6 packet, and parse the options and offer type. + // + if (EFI_ERROR (HttpBootParseDhcp6Packet (Cache6))) { + return EFI_ABORTED; + } + + // + // Determine whether cache the current offer by type, and record OfferIndex and OfferCount. + // + OfferType = Cache6->OfferType; + ASSERT (OfferType < HttpOfferTypeMax); + ASSERT (Private->OfferCount[OfferType] < HTTP_BOOT_OFFER_MAX_NUM); + Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum; + Private->OfferCount[OfferType]++; + Private->OfferNum++; + + return EFI_SUCCESS; +} + +/** + EFI_DHCP6_CALLBACK is provided by the consumer of the EFI DHCPv6 Protocol driver + to intercept events that occurred in the configuration process. + + @param[in] This The pointer to the EFI DHCPv6 Protocol. + @param[in] Context The pointer to the context set by EFI_DHCP6_PROTOCOL.Configure(). + @param[in] CurrentState The current operational state of the EFI DHCPv Protocol driver. + @param[in] Dhcp6Event The event that occurs in the current state, which usually means a + state transition. + @param[in] Packet The DHCPv6 packet that is going to be sent or was already received. + @param[out] NewPacket The packet that is used to replace the Packet above. + + @retval EFI_SUCCESS Told the EFI DHCPv6 Protocol driver to continue the DHCP process. + @retval EFI_NOT_READY Only used in the Dhcp6Selecting state. The EFI DHCPv6 Protocol + driver will continue to wait for more packets. + @retval EFI_ABORTED Told the EFI DHCPv6 Protocol driver to abort the current process. + @retval EFI_OUT_OF_RESOURCES There are not enough resources. + +**/ +EFI_STATUS +EFIAPI +HttpBootDhcp6CallBack ( + IN EFI_DHCP6_PROTOCOL *This, + IN VOID *Context, + IN EFI_DHCP6_STATE CurrentState, + IN EFI_DHCP6_EVENT Dhcp6Event, + IN EFI_DHCP6_PACKET *Packet, + OUT EFI_DHCP6_PACKET **NewPacket OPTIONAL + ) +{ + HTTP_BOOT_PRIVATE_DATA *Private; + EFI_DHCP6_PACKET *SelectAd; + EFI_STATUS Status; + BOOLEAN Received; + + if ((Dhcp6Event != Dhcp6SendSolicit) && + (Dhcp6Event != Dhcp6RcvdAdvertise) && + (Dhcp6Event != Dhcp6SendRequest) && + (Dhcp6Event != Dhcp6RcvdReply) && + (Dhcp6Event != Dhcp6SelectAdvertise)) { + return EFI_SUCCESS; + } + + ASSERT (Packet != NULL); + + Private = (HTTP_BOOT_PRIVATE_DATA *) Context; + Status = EFI_SUCCESS; + if (Private->HttpBootCallback != NULL && Dhcp6Event != Dhcp6SelectAdvertise) { + Received = (BOOLEAN) (Dhcp6Event == Dhcp6RcvdAdvertise || Dhcp6Event == Dhcp6RcvdReply); + Status = Private->HttpBootCallback->Callback ( + Private->HttpBootCallback, + HttpBootDhcp6, + Received, + Packet->Length, + &Packet->Dhcp6 + ); + if (EFI_ERROR (Status)) { + return EFI_ABORTED; + } + } + switch (Dhcp6Event) { + + case Dhcp6RcvdAdvertise: + Status = EFI_NOT_READY; + if (Packet->Length > HTTP_BOOT_DHCP6_PACKET_MAX_SIZE) { + // + // Ignore the incoming packets which exceed the maximum length. + // + break; + } + if (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM) { + // + // Cache the dhcp offers to OfferBuffer[] for select later, and record + // the OfferIndex and OfferCount. + // If error happens, just ignore this packet and continue to wait more offer. + // + HttpBootCacheDhcp6Offer (Private, Packet); + } + break; + + case Dhcp6SelectAdvertise: + // + // Select offer by the default policy or by order, and record the SelectIndex + // and SelectProxyType. + // + HttpBootSelectDhcpOffer (Private); + + if (Private->SelectIndex == 0) { + Status = EFI_ABORTED; + } else { + ASSERT (NewPacket != NULL); + SelectAd = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp6.Packet.Offer; + *NewPacket = AllocateZeroPool (SelectAd->Size); + if (*NewPacket == NULL) { + return EFI_OUT_OF_RESOURCES; + } + CopyMem (*NewPacket, SelectAd, SelectAd->Size); + } + break; + + default: + break; + } + + return Status; +} + +/** + Check whether IP driver could route the message which will be sent to ServerIp address. + + This function will check the IP6 route table every 1 seconds until specified timeout is expired, if a valid + route is found in IP6 route table, the address will be filed in GatewayAddr and return. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + @param[in] TimeOutInSecond Timeout value in seconds. + @param[out] GatewayAddr Pointer to store the gateway IP address. + + @retval EFI_SUCCESS Found a valid gateway address successfully. + @retval EFI_TIMEOUT The operation is time out. + @retval Other Unexpect error happened. + +**/ +EFI_STATUS +HttpBootCheckRouteTable ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN UINTN TimeOutInSecond, + OUT EFI_IPv6_ADDRESS *GatewayAddr + ) +{ + EFI_STATUS Status; + EFI_IP6_PROTOCOL *Ip6; + EFI_IP6_MODE_DATA Ip6ModeData; + UINTN Index; + EFI_EVENT TimeOutEvt; + UINTN RetryCount; + BOOLEAN GatewayIsFound; + + ASSERT (GatewayAddr != NULL); + ASSERT (Private != NULL); + + Ip6 = Private->Ip6; + GatewayIsFound = FALSE; + RetryCount = 0; + TimeOutEvt = NULL; + Status = EFI_SUCCESS; + ZeroMem (GatewayAddr, sizeof (EFI_IPv6_ADDRESS)); + + while (TRUE) { + Status = Ip6->GetModeData (Ip6, &Ip6ModeData, NULL, NULL); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Find out the gateway address which can route the message which send to ServerIp. + // + for (Index = 0; Index < Ip6ModeData.RouteCount; Index++) { + if (NetIp6IsNetEqual (&Private->ServerIp.v6, &Ip6ModeData.RouteTable[Index].Destination, Ip6ModeData.RouteTable[Index].PrefixLength)) { + IP6_COPY_ADDRESS (GatewayAddr, &Ip6ModeData.RouteTable[Index].Gateway); + GatewayIsFound = TRUE; + break; + } + } + + if (Ip6ModeData.AddressList != NULL) { + FreePool (Ip6ModeData.AddressList); + } + if (Ip6ModeData.GroupTable != NULL) { + FreePool (Ip6ModeData.GroupTable); + } + if (Ip6ModeData.RouteTable != NULL) { + FreePool (Ip6ModeData.RouteTable); + } + if (Ip6ModeData.NeighborCache != NULL) { + FreePool (Ip6ModeData.NeighborCache); + } + if (Ip6ModeData.PrefixTable != NULL) { + FreePool (Ip6ModeData.PrefixTable); + } + if (Ip6ModeData.IcmpTypeList != NULL) { + FreePool (Ip6ModeData.IcmpTypeList); + } + + if (GatewayIsFound || RetryCount == TimeOutInSecond) { + break; + } + + RetryCount++; + + // + // Delay 1 second then recheck it again. + // + if (TimeOutEvt == NULL) { + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &TimeOutEvt + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + Status = gBS->SetTimer (TimeOutEvt, TimerRelative, TICKS_PER_SECOND); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + while (EFI_ERROR (gBS->CheckEvent (TimeOutEvt))) { + Ip6->Poll (Ip6); + } + } + +ON_EXIT: + if (TimeOutEvt != NULL) { + gBS->CloseEvent (TimeOutEvt); + } + + if (GatewayIsFound) { + Status = EFI_SUCCESS; + } else if (RetryCount == TimeOutInSecond) { + Status = EFI_TIMEOUT; + } + + return Status; +} + +/** + Set the IP6 policy to Automatic. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + + @retval EFI_SUCCESS Switch the IP policy succesfully. + @retval Others Unexpect error happened. + +**/ +EFI_STATUS +HttpBootSetIp6Policy ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + EFI_IP6_CONFIG_POLICY Policy; + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + EFI_STATUS Status; + UINTN DataSize; + + Ip6Config = Private->Ip6Config; + DataSize = sizeof (EFI_IP6_CONFIG_POLICY); + + // + // Get and store the current policy of IP6 driver. + // + Status = Ip6Config->GetData ( + Ip6Config, + Ip6ConfigDataTypePolicy, + &DataSize, + &Policy + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Policy == Ip6ConfigPolicyManual) { + Policy = Ip6ConfigPolicyAutomatic; + Status = Ip6Config->SetData ( + Ip6Config, + Ip6ConfigDataTypePolicy, + sizeof(EFI_IP6_CONFIG_POLICY), + &Policy + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + return EFI_SUCCESS; +} + +/** + This function will register the default DNS addresses to the network device. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + @param[in] DataLength Size of the buffer pointed to by DnsServerData in bytes. + @param[in] DnsServerData Point a list of DNS server address in an array + of EFI_IPv6_ADDRESS instances. + + @retval EFI_SUCCESS The DNS configuration has been configured successfully. + @retval Others Failed to configure the address. + +**/ +EFI_STATUS +HttpBootSetIp6Dns ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN UINTN DataLength, + IN VOID *DnsServerData + ) +{ + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + + ASSERT (Private->UsingIpv6); + + Ip6Config = Private->Ip6Config; + + return Ip6Config->SetData ( + Ip6Config, + Ip6ConfigDataTypeDnsServer, + DataLength, + DnsServerData + ); +} + +/** + This function will register the IPv6 gateway address to the network device. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + + @retval EFI_SUCCESS The new IP configuration has been configured successfully. + @retval Others Failed to configure the address. + +**/ +EFI_STATUS +HttpBootSetIp6Gateway ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + EFI_STATUS Status; + + ASSERT (Private->UsingIpv6); + Ip6Config = Private->Ip6Config; + + // + // Set the default gateway address. + // + if (!Private->NoGateway && !NetIp6IsUnspecifiedAddr (&Private->GatewayIp.v6)) { + Status = Ip6Config->SetData ( + Ip6Config, + Ip6ConfigDataTypeGateway, + sizeof (EFI_IPv6_ADDRESS), + &Private->GatewayIp.v6 + ); + if (EFI_ERROR(Status)) { + return Status; + } + } + + return EFI_SUCCESS; +} + +/** + This function will register the station IP address. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + + @retval EFI_SUCCESS The new IP address has been configured successfully. + @retval Others Failed to configure the address. + +**/ +EFI_STATUS +HttpBootSetIp6Address ( + IN HTTP_BOOT_PRIVATE_DATA *Private +) +{ + EFI_STATUS Status; + EFI_IP6_PROTOCOL *Ip6; + EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg; + EFI_IP6_CONFIG_POLICY Policy; + EFI_IP6_CONFIG_MANUAL_ADDRESS CfgAddr; + EFI_IPv6_ADDRESS *Ip6Addr; + EFI_IPv6_ADDRESS GatewayAddr; + EFI_IP6_CONFIG_DATA Ip6CfgData; + EFI_EVENT MappedEvt; + UINTN DataSize; + BOOLEAN IsAddressOk; + UINTN Index; + + ASSERT (Private->UsingIpv6); + + MappedEvt = NULL; + IsAddressOk = FALSE; + Ip6Addr = NULL; + Ip6Cfg = Private->Ip6Config; + Ip6 = Private->Ip6; + + ZeroMem (&CfgAddr, sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS)); + CopyMem (&CfgAddr, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS)); + ZeroMem (&Ip6CfgData, sizeof (EFI_IP6_CONFIG_DATA)); + + Ip6CfgData.AcceptIcmpErrors = TRUE; + Ip6CfgData.DefaultProtocol = IP6_ICMP; + Ip6CfgData.HopLimit = HTTP_BOOT_DEFAULT_HOPLIMIT; + Ip6CfgData.ReceiveTimeout = HTTP_BOOT_DEFAULT_LIFETIME; + Ip6CfgData.TransmitTimeout = HTTP_BOOT_DEFAULT_LIFETIME; + + Status = Ip6->Configure (Ip6, &Ip6CfgData); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Retrieve the gateway address from IP6 route table. + // + Status = HttpBootCheckRouteTable (Private, HTTP_BOOT_IP6_ROUTE_TABLE_TIMEOUT, &GatewayAddr); + if (EFI_ERROR (Status)) { + Private->NoGateway = TRUE; + } else { + IP6_COPY_ADDRESS (&Private->GatewayIp.v6, &GatewayAddr); + } + + // + // Set the new address by Ip6ConfigProtocol manually. + // + Policy = Ip6ConfigPolicyManual; + Status = Ip6Cfg->SetData ( + Ip6Cfg, + Ip6ConfigDataTypePolicy, + sizeof(EFI_IP6_CONFIG_POLICY), + &Policy + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Create a notify event to set address flag when DAD if IP6 driver succeeded. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpBootCommonNotify, + &IsAddressOk, + &MappedEvt + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Set static host ip6 address. This is a asynchronous process. + // + Status = Ip6Cfg->RegisterDataNotify ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + MappedEvt + ); + if (EFI_ERROR(Status)) { + goto ON_EXIT; + } + + Status = Ip6Cfg->SetData ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS), + &CfgAddr + ); + if (EFI_ERROR (Status) && Status != EFI_NOT_READY) { + goto ON_EXIT; + } else if (Status == EFI_NOT_READY) { + // + // Poll the network until the asynchronous process is finished. + // + while (!IsAddressOk) { + Ip6->Poll (Ip6); + } + // + // Check whether the Ip6 Address setting is successed. + // + DataSize = 0; + Status = Ip6Cfg->GetData ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + &DataSize, + NULL + ); + if (Status != EFI_BUFFER_TOO_SMALL || DataSize == 0) { + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + Ip6Addr = AllocatePool (DataSize); + if (Ip6Addr == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Status = Ip6Cfg->GetData ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + &DataSize, + (VOID *) Ip6Addr + ); + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + for (Index = 0; Index < DataSize / sizeof (EFI_IPv6_ADDRESS); Index ++) { + if (CompareMem (Ip6Addr + Index, &CfgAddr, sizeof (EFI_IPv6_ADDRESS)) == 0) { + break; + } + } + if (Index == DataSize / sizeof (EFI_IPv6_ADDRESS)) { + Status = EFI_ABORTED; + goto ON_EXIT; + } + } + +ON_EXIT: + if (MappedEvt != NULL) { + Ip6Cfg->UnregisterDataNotify ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + MappedEvt + ); + gBS->CloseEvent (MappedEvt); + } + + if (Ip6Addr != NULL) { + FreePool (Ip6Addr); + } + + return Status; +} + +/** + Start the S.A.R.R DHCPv6 process to acquire the IPv6 address and other Http boot information. + + @param[in] Private Pointer to HTTP_BOOT private data. + + @retval EFI_SUCCESS The S.A.R.R process successfully finished. + @retval Others Failed to finish the S.A.R.R process. + +**/ +EFI_STATUS +HttpBootDhcp6Sarr ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + EFI_DHCP6_PROTOCOL *Dhcp6; + EFI_DHCP6_CONFIG_DATA Config; + EFI_DHCP6_MODE_DATA Mode; + EFI_DHCP6_RETRANSMISSION *Retransmit; + EFI_DHCP6_PACKET_OPTION *OptList[HTTP_BOOT_DHCP6_OPTION_MAX_NUM]; + UINT32 OptCount; + UINT8 Buffer[HTTP_BOOT_DHCP6_OPTION_MAX_SIZE]; + EFI_STATUS Status; + + Dhcp6 = Private->Dhcp6; + ASSERT (Dhcp6 != NULL); + + // + // Build options list for the request packet. + // + OptCount = HttpBootBuildDhcp6Options (Private, OptList, Buffer); + ASSERT (OptCount >0); + + Retransmit = AllocateZeroPool (sizeof (EFI_DHCP6_RETRANSMISSION)); + if (Retransmit == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + ZeroMem (&Mode, sizeof (EFI_DHCP6_MODE_DATA)); + ZeroMem (&Config, sizeof (EFI_DHCP6_CONFIG_DATA)); + + Config.OptionCount = OptCount; + Config.OptionList = OptList; + Config.Dhcp6Callback = HttpBootDhcp6CallBack; + Config.CallbackContext = Private; + Config.IaInfoEvent = NULL; + Config.RapidCommit = FALSE; + Config.ReconfigureAccept = FALSE; + Config.IaDescriptor.IaId = NET_RANDOM (NetRandomInitSeed ()); + Config.IaDescriptor.Type = EFI_DHCP6_IA_TYPE_NA; + Config.SolicitRetransmission = Retransmit; + Retransmit->Irt = 4; + Retransmit->Mrc = 4; + Retransmit->Mrt = 32; + Retransmit->Mrd = 60; + + // + // Configure the DHCPv6 instance for HTTP boot. + // + Status = Dhcp6->Configure (Dhcp6, &Config); + FreePool (Retransmit); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + // + // Initialize the record fields for DHCPv6 offer in private data. + // + Private->OfferNum = 0; + Private->SelectIndex = 0; + ZeroMem (Private->OfferCount, sizeof (Private->OfferCount)); + ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex)); + + // + // Start DHCPv6 S.A.R.R. process to acquire IPv6 address. + // + Status = Dhcp6->Start (Dhcp6); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Get the acquired IPv6 address and store them. + // + Status = Dhcp6->GetModeData (Dhcp6, &Mode, NULL); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + ASSERT (Mode.Ia->State == Dhcp6Bound); + CopyMem (&Private->StationIp.v6, &Mode.Ia->IaAddress[0].IpAddress, sizeof (EFI_IPv6_ADDRESS)); + + AsciiPrint ("\n Station IPv6 address is "); + HttpBootShowIp6Addr (&Private->StationIp.v6); + AsciiPrint ("\n"); + +ON_EXIT: + if (EFI_ERROR (Status)) { + Dhcp6->Stop (Dhcp6); + Dhcp6->Configure (Dhcp6, NULL); + } else { + ZeroMem (&Config, sizeof (EFI_DHCP6_CONFIG_DATA)); + Dhcp6->Configure (Dhcp6, &Config); + if (Mode.ClientId != NULL) { + FreePool (Mode.ClientId); + } + if (Mode.Ia != NULL) { + FreePool (Mode.Ia); + } + } + + return Status; + +} + diff --git a/NetworkPkg/HttpBootDxe/HttpBootDhcp6.h b/NetworkPkg/HttpBootDxe/HttpBootDhcp6.h new file mode 100644 index 000000000..e21f272aa --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootDhcp6.h @@ -0,0 +1,169 @@ +/** @file + Functions declaration related with DHCPv6 for HTTP boot driver. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#ifndef __EFI_HTTP_BOOT_DHCP6_H__ +#define __EFI_HTTP_BOOT_DHCP6_H__ + +#define HTTP_BOOT_OFFER_MAX_NUM 16 +#define HTTP_BOOT_DHCP6_OPTION_MAX_NUM 16 +#define HTTP_BOOT_DHCP6_OPTION_MAX_SIZE 312 +#define HTTP_BOOT_DHCP6_PACKET_MAX_SIZE 1472 +#define HTTP_BOOT_IP6_ROUTE_TABLE_TIMEOUT 10 +#define HTTP_BOOT_DEFAULT_HOPLIMIT 64 +#define HTTP_BOOT_DEFAULT_LIFETIME 50000 + +#define HTTP_BOOT_DHCP6_ENTERPRISE_NUM 343 // TODO: IANA TBD: temporarily using Intel's +#define HTTP_BOOT_DHCP6_MAX_BOOT_FILE_SIZE 65535 // It's a limitation of bit length, 65535*512 bytes. + +#define HTTP_BOOT_DHCP6_IDX_IA_NA 0 +#define HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL 1 +#define HTTP_BOOT_DHCP6_IDX_BOOT_FILE_PARAM 2 +#define HTTP_BOOT_DHCP6_IDX_VENDOR_CLASS 3 +#define HTTP_BOOT_DHCP6_IDX_DNS_SERVER 4 +#define HTTP_BOOT_DHCP6_IDX_MAX 5 + +#pragma pack(1) +typedef struct { + UINT16 OpCode[256]; +} HTTP_BOOT_DHCP6_OPTION_ORO; + +typedef struct { + UINT8 Type; + UINT8 MajorVer; + UINT8 MinorVer; +} HTTP_BOOT_DHCP6_OPTION_UNDI; + +typedef struct { + UINT16 Type; +} HTTP_BOOT_DHCP6_OPTION_ARCH; + +typedef struct { + UINT8 ClassIdentifier[11]; + UINT8 ArchitecturePrefix[5]; + UINT8 ArchitectureType[5]; + UINT8 Lit3[1]; + UINT8 InterfaceName[4]; + UINT8 Lit4[1]; + UINT8 UndiMajor[3]; + UINT8 UndiMinor[3]; +} HTTP_BOOT_CLASS_ID; + +typedef struct { + UINT32 Vendor; + UINT16 ClassLen; + HTTP_BOOT_CLASS_ID ClassId; +} HTTP_BOOT_DHCP6_OPTION_VENDOR_CLASS; + +#pragma pack() + +typedef union { + HTTP_BOOT_DHCP6_OPTION_ORO *Oro; + HTTP_BOOT_DHCP6_OPTION_UNDI *Undi; + HTTP_BOOT_DHCP6_OPTION_ARCH *Arch; + HTTP_BOOT_DHCP6_OPTION_VENDOR_CLASS *VendorClass; +} HTTP_BOOT_DHCP6_OPTION_ENTRY; + +#define HTTP_CACHED_DHCP6_PACKET_MAX_SIZE (OFFSET_OF (EFI_DHCP6_PACKET, Dhcp6) + HTTP_BOOT_DHCP6_PACKET_MAX_SIZE) + +typedef union { + EFI_DHCP6_PACKET Offer; + EFI_DHCP6_PACKET Ack; + UINT8 Buffer[HTTP_CACHED_DHCP6_PACKET_MAX_SIZE]; +} HTTP_BOOT_DHCP6_PACKET; + +typedef struct { + HTTP_BOOT_DHCP6_PACKET Packet; + HTTP_BOOT_OFFER_TYPE OfferType; + EFI_DHCP6_PACKET_OPTION *OptList[HTTP_BOOT_DHCP6_IDX_MAX]; + VOID *UriParser; +} HTTP_BOOT_DHCP6_PACKET_CACHE; + +#define GET_NEXT_DHCP6_OPTION(Opt) \ + (EFI_DHCP6_PACKET_OPTION *) ((UINT8 *) (Opt) + \ + sizeof (EFI_DHCP6_PACKET_OPTION) + (NTOHS ((Opt)->OpLen)) - 1) + +#define GET_DHCP6_OPTION_SIZE(Pkt) \ + ((Pkt)->Length - sizeof (EFI_DHCP6_HEADER)) + +/** + Start the S.A.R.R DHCPv6 process to acquire the IPv6 address and other Http boot information. + + @param[in] Private Pointer to HTTP_BOOT private data. + + @retval EFI_SUCCESS The S.A.R.R process successfully finished. + @retval Others Failed to finish the S.A.R.R process. + +**/ +EFI_STATUS +HttpBootDhcp6Sarr ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +/** + Set the IP6 policy to Automatic. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + + @retval EFI_SUCCESS Switch the IP policy succesfully. + @retval Others Unexpect error happened. + +**/ +EFI_STATUS +HttpBootSetIp6Policy ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +/** + This function will register the default DNS addresses to the network device. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + @param[in] DataLength Size of the buffer pointed to by DnsServerData in bytes. + @param[in] DnsServerData Point a list of DNS server address in an array + of EFI_IPv6_ADDRESS instances. + + @retval EFI_SUCCESS The DNS configuration has been configured successfully. + @retval Others Failed to configure the address. + +**/ +EFI_STATUS +HttpBootSetIp6Dns ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN UINTN DataLength, + IN VOID *DnsServerData + ); + +/** + This function will register the IPv6 gateway address to the network device. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + + @retval EFI_SUCCESS The new IP configuration has been configured successfully. + @retval Others Failed to configure the address. + +**/ +EFI_STATUS +HttpBootSetIp6Gateway ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +/** + This function will register the station IP address. + + @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA. + + @retval EFI_SUCCESS The new IP address has been configured successfully. + @retval Others Failed to configure the address. + +**/ +EFI_STATUS +HttpBootSetIp6Address ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +#endif diff --git a/NetworkPkg/HttpBootDxe/HttpBootDxe.c b/NetworkPkg/HttpBootDxe/HttpBootDxe.c new file mode 100644 index 000000000..2efdd9be1 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootDxe.c @@ -0,0 +1,1332 @@ +/** @file + Driver Binding functions implementation for UEFI HTTP boot. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpBootDxe.h" + +/// +/// Driver Binding Protocol instance +/// +EFI_DRIVER_BINDING_PROTOCOL gHttpBootIp4DxeDriverBinding = { + HttpBootIp4DxeDriverBindingSupported, + HttpBootIp4DxeDriverBindingStart, + HttpBootIp4DxeDriverBindingStop, + HTTP_BOOT_DXE_VERSION, + NULL, + NULL +}; + +EFI_DRIVER_BINDING_PROTOCOL gHttpBootIp6DxeDriverBinding = { + HttpBootIp6DxeDriverBindingSupported, + HttpBootIp6DxeDriverBindingStart, + HttpBootIp6DxeDriverBindingStop, + HTTP_BOOT_DXE_VERSION, + NULL, + NULL +}; + + + +/** + Check whether UNDI protocol supports IPv6. + + @param[in] Private Pointer to HTTP_BOOT_PRIVATE_DATA. + @param[out] Ipv6Support TRUE if UNDI supports IPv6. + + @retval EFI_SUCCESS Get the result whether UNDI supports IPv6 by NII or AIP protocol successfully. + @retval EFI_NOT_FOUND Don't know whether UNDI supports IPv6 since NII or AIP is not available. + +**/ +EFI_STATUS +HttpBootCheckIpv6Support ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + OUT BOOLEAN *Ipv6Support + ) +{ + EFI_HANDLE Handle; + EFI_ADAPTER_INFORMATION_PROTOCOL *Aip; + EFI_STATUS Status; + EFI_GUID *InfoTypesBuffer; + UINTN InfoTypeBufferCount; + UINTN TypeIndex; + BOOLEAN Supported; + VOID *InfoBlock; + UINTN InfoBlockSize; + + ASSERT (Private != NULL && Ipv6Support != NULL); + + // + // Check whether the UNDI supports IPv6 by NII protocol. + // + if (Private->Nii != NULL) { + *Ipv6Support = Private->Nii->Ipv6Supported; + return EFI_SUCCESS; + } + + // + // Get the NIC handle by SNP protocol. + // + Handle = NetLibGetSnpHandle (Private->Controller, NULL); + if (Handle == NULL) { + return EFI_NOT_FOUND; + } + + Aip = NULL; + Status = gBS->HandleProtocol ( + Handle, + &gEfiAdapterInformationProtocolGuid, + (VOID *) &Aip + ); + if (EFI_ERROR (Status) || Aip == NULL) { + return EFI_NOT_FOUND; + } + + InfoTypesBuffer = NULL; + InfoTypeBufferCount = 0; + Status = Aip->GetSupportedTypes (Aip, &InfoTypesBuffer, &InfoTypeBufferCount); + if (EFI_ERROR (Status) || InfoTypesBuffer == NULL) { + FreePool (InfoTypesBuffer); + return EFI_NOT_FOUND; + } + + Supported = FALSE; + for (TypeIndex = 0; TypeIndex < InfoTypeBufferCount; TypeIndex++) { + if (CompareGuid (&InfoTypesBuffer[TypeIndex], &gEfiAdapterInfoUndiIpv6SupportGuid)) { + Supported = TRUE; + break; + } + } + + FreePool (InfoTypesBuffer); + if (!Supported) { + return EFI_NOT_FOUND; + } + + // + // We now have adapter information block. + // + InfoBlock = NULL; + InfoBlockSize = 0; + Status = Aip->GetInformation (Aip, &gEfiAdapterInfoUndiIpv6SupportGuid, &InfoBlock, &InfoBlockSize); + if (EFI_ERROR (Status) || InfoBlock == NULL) { + FreePool (InfoBlock); + return EFI_NOT_FOUND; + } + + *Ipv6Support = ((EFI_ADAPTER_INFO_UNDI_IPV6_SUPPORT *) InfoBlock)->Ipv6Support; + FreePool (InfoBlock); + + return EFI_SUCCESS; +} + +/** + Destroy the HTTP child based on IPv4 stack. + + @param[in] This Pointer to the EFI_DRIVER_BINDING_PROTOCOL. + @param[in] Private Pointer to HTTP_BOOT_PRIVATE_DATA. + +**/ +VOID +HttpBootDestroyIp4Children ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + ASSERT (This != NULL); + ASSERT (Private != NULL); + + if (Private->Dhcp4Child != NULL) { + gBS->CloseProtocol ( + Private->Dhcp4Child, + &gEfiDhcp4ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiDhcp4ServiceBindingProtocolGuid, + Private->Dhcp4Child + ); + } + + if (Private->Ip6Nic == NULL && Private->HttpCreated) { + HttpIoDestroyIo (&Private->HttpIo); + Private->HttpCreated = FALSE; + } + + if (Private->Ip4Nic != NULL) { + + gBS->CloseProtocol ( + Private->Controller, + &gEfiCallerIdGuid, + This->DriverBindingHandle, + Private->Ip4Nic->Controller + ); + + gBS->UninstallMultipleProtocolInterfaces ( + Private->Ip4Nic->Controller, + &gEfiLoadFileProtocolGuid, + &Private->Ip4Nic->LoadFile, + &gEfiDevicePathProtocolGuid, + Private->Ip4Nic->DevicePath, + NULL + ); + FreePool (Private->Ip4Nic); + Private->Ip4Nic = NULL; + } + +} + +/** + Destroy the HTTP child based on IPv6 stack. + + @param[in] This Pointer to the EFI_DRIVER_BINDING_PROTOCOL. + @param[in] Private Pointer to HTTP_BOOT_PRIVATE_DATA. + +**/ +VOID +HttpBootDestroyIp6Children ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + ASSERT (This != NULL); + ASSERT (Private != NULL); + + if (Private->Ip6Child != NULL) { + gBS->CloseProtocol ( + Private->Ip6Child, + &gEfiIp6ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiIp6ServiceBindingProtocolGuid, + Private->Ip6Child + ); + } + + if (Private->Dhcp6Child != NULL) { + gBS->CloseProtocol ( + Private->Dhcp6Child, + &gEfiDhcp6ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiDhcp6ServiceBindingProtocolGuid, + Private->Dhcp6Child + ); + } + + if (Private->Ip4Nic == NULL && Private->HttpCreated) { + HttpIoDestroyIo(&Private->HttpIo); + Private->HttpCreated = FALSE; + } + + if (Private->Ip6Nic != NULL) { + + gBS->CloseProtocol ( + Private->Controller, + &gEfiCallerIdGuid, + This->DriverBindingHandle, + Private->Ip6Nic->Controller + ); + + gBS->UninstallMultipleProtocolInterfaces ( + Private->Ip6Nic->Controller, + &gEfiLoadFileProtocolGuid, + &Private->Ip6Nic->LoadFile, + &gEfiDevicePathProtocolGuid, + Private->Ip6Nic->DevicePath, + NULL + ); + FreePool (Private->Ip6Nic); + Private->Ip6Nic = NULL; + } +} + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +HttpBootIp4DxeDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // Try to open the DHCP4, HTTP4 and Device Path protocol. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDhcp4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiHttpServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDevicePathProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + return Status; +} + + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +HttpBootIp4DxeDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + HTTP_BOOT_PRIVATE_DATA *Private; + EFI_DEV_PATH *Node; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + UINT32 *Id; + BOOLEAN FirstStart; + + FirstStart = FALSE; + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (!EFI_ERROR (Status)) { + Private = HTTP_BOOT_PRIVATE_DATA_FROM_ID(Id); + } else { + FirstStart = TRUE; + + // + // Initialize the private data structure. + // + Private = AllocateZeroPool (sizeof (HTTP_BOOT_PRIVATE_DATA)); + if (Private == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Private->Signature = HTTP_BOOT_PRIVATE_DATA_SIGNATURE; + Private->Controller = ControllerHandle; + InitializeListHead (&Private->CacheList); + // + // Get the NII interface if it exists, it's not required. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiNetworkInterfaceIdentifierProtocolGuid_31, + (VOID **) &Private->Nii, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + Private->Nii = NULL; + } + + // + // Open Device Path Protocol to prepare for appending IP and URI node. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDevicePathProtocolGuid, + (VOID **) &Private->ParentDevicePath, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Initialize the HII configuration form. + // + Status = HttpBootConfigFormInit (Private); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Install a protocol with Caller Id Guid to the NIC, this is just to build the relationship between + // NIC handle and the private data. + // + Status = gBS->InstallProtocolInterface ( + &ControllerHandle, + &gEfiCallerIdGuid, + EFI_NATIVE_INTERFACE, + &Private->Id + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + } + + if (Private->Ip4Nic != NULL) { + // + // Already created before + // + return EFI_SUCCESS; + } + + Private->Ip4Nic = AllocateZeroPool (sizeof (HTTP_BOOT_VIRTUAL_NIC)); + if (Private->Ip4Nic == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + Private->Ip4Nic->Private = Private; + Private->Ip4Nic->ImageHandle = This->DriverBindingHandle; + Private->Ip4Nic->Signature = HTTP_BOOT_VIRTUAL_NIC_SIGNATURE; + + // + // Create DHCP4 child instance. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiDhcp4ServiceBindingProtocolGuid, + &Private->Dhcp4Child + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Dhcp4Child, + &gEfiDhcp4ProtocolGuid, + (VOID **) &Private->Dhcp4, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Get the Ip4Config2 protocol, it's required to configure the default gateway address. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiIp4Config2ProtocolGuid, + (VOID **) &Private->Ip4Config2, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Append IPv4 device path node. + // + Node = AllocateZeroPool (sizeof (IPv4_DEVICE_PATH)); + if (Node == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + Node->Ipv4.Header.Type = MESSAGING_DEVICE_PATH; + Node->Ipv4.Header.SubType = MSG_IPv4_DP; + SetDevicePathNodeLength (Node, sizeof (IPv4_DEVICE_PATH)); + Node->Ipv4.StaticIpAddress = FALSE; + DevicePath = AppendDevicePathNode (Private->ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (Node); + if (DevicePath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + // + // Append URI device path node. + // + Node = AllocateZeroPool (sizeof (EFI_DEVICE_PATH_PROTOCOL)); + if (Node == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + Node->DevPath.Type = MESSAGING_DEVICE_PATH; + Node->DevPath.SubType = MSG_URI_DP; + SetDevicePathNodeLength (Node, sizeof (EFI_DEVICE_PATH_PROTOCOL)); + Private->Ip4Nic->DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (Node); + FreePool (DevicePath); + if (Private->Ip4Nic->DevicePath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + // + // Create a child handle for the HTTP boot and install DevPath and Load file protocol on it. + // + CopyMem (&Private->Ip4Nic->LoadFile, &gHttpBootDxeLoadFile, sizeof (EFI_LOAD_FILE_PROTOCOL)); + Status = gBS->InstallMultipleProtocolInterfaces ( + &Private->Ip4Nic->Controller, + &gEfiLoadFileProtocolGuid, + &Private->Ip4Nic->LoadFile, + &gEfiDevicePathProtocolGuid, + Private->Ip4Nic->DevicePath, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Open the Caller Id child to setup a parent-child relationship between + // real NIC handle and the HTTP boot Ipv4 NIC handle. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + This->DriverBindingHandle, + Private->Ip4Nic->Controller, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + if (Private != NULL) { + if (FirstStart) { + gBS->UninstallProtocolInterface ( + ControllerHandle, + &gEfiCallerIdGuid, + &Private->Id + ); + } + + HttpBootDestroyIp4Children (This, Private); + HttpBootConfigFormUnload (Private); + + if (FirstStart) { + FreePool (Private); + } + } + + return Status; +} + + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +HttpBootIp4DxeDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_LOAD_FILE_PROTOCOL *LoadFile; + HTTP_BOOT_PRIVATE_DATA *Private; + EFI_HANDLE NicHandle; + UINT32 *Id; + + // + // Try to get the Load File Protocol from the controller handle. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiLoadFileProtocolGuid, + (VOID **) &LoadFile, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + // + // If failed, try to find the NIC handle for this controller. + // + NicHandle = HttpBootGetNicByIp4Children (ControllerHandle); + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + // + // Try to retrieve the private data by the Caller Id Guid. + // + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + Private = HTTP_BOOT_PRIVATE_DATA_FROM_ID (Id); + } else { + Private = HTTP_BOOT_PRIVATE_DATA_FROM_LOADFILE (LoadFile); + NicHandle = Private->Controller; + } + + // + // Disable the HTTP boot function. + // + Status = HttpBootStop (Private); + if (Status != EFI_SUCCESS && Status != EFI_NOT_STARTED) { + return Status; + } + + // + // Destory all child instance and uninstall protocol interface. + // + HttpBootDestroyIp4Children (This, Private); + + if (Private->Ip4Nic == NULL && Private->Ip6Nic == NULL) { + // + // Release the cached data. + // + HttpBootFreeCacheList (Private); + + // + // Unload the config form. + // + HttpBootConfigFormUnload (Private); + + gBS->UninstallProtocolInterface ( + NicHandle, + &gEfiCallerIdGuid, + &Private->Id + ); + FreePool (Private); + + } + + return EFI_SUCCESS; +} + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +HttpBootIp6DxeDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // Try to open the DHCP6, HTTP and Device Path protocol. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDhcp6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiHttpServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDevicePathProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + return Status; + +} + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +HttpBootIp6DxeDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + HTTP_BOOT_PRIVATE_DATA *Private; + EFI_DEV_PATH *Node; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + UINT32 *Id; + BOOLEAN Ipv6Available; + BOOLEAN FirstStart; + + FirstStart = FALSE; + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (!EFI_ERROR (Status)) { + Private = HTTP_BOOT_PRIVATE_DATA_FROM_ID(Id); + } else { + FirstStart = TRUE; + + // + // Initialize the private data structure. + // + Private = AllocateZeroPool (sizeof (HTTP_BOOT_PRIVATE_DATA)); + if (Private == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Private->Signature = HTTP_BOOT_PRIVATE_DATA_SIGNATURE; + Private->Controller = ControllerHandle; + InitializeListHead (&Private->CacheList); + // + // Get the NII interface if it exists, it's not required. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiNetworkInterfaceIdentifierProtocolGuid_31, + (VOID **) &Private->Nii, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + Private->Nii = NULL; + } + + // + // Open Device Path Protocol to prepare for appending IP and URI node. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDevicePathProtocolGuid, + (VOID **) &Private->ParentDevicePath, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Initialize the HII configuration form. + // + Status = HttpBootConfigFormInit (Private); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Install a protocol with Caller Id Guid to the NIC, this is just to build the relationship between + // NIC handle and the private data. + // + Status = gBS->InstallProtocolInterface ( + &ControllerHandle, + &gEfiCallerIdGuid, + EFI_NATIVE_INTERFACE, + &Private->Id + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + } + + // + // Set IPv6 available flag. + // + Status = HttpBootCheckIpv6Support (Private, &Ipv6Available); + if (EFI_ERROR (Status)) { + // + // Fail to get the data whether UNDI supports IPv6. + // Set default value to TRUE. + // + Ipv6Available = TRUE; + } + + if (!Ipv6Available) { + Status = EFI_UNSUPPORTED; + goto ON_ERROR; + } + + if (Private->Ip6Nic != NULL) { + // + // Already created before + // + return EFI_SUCCESS; + } + + Private->Ip6Nic = AllocateZeroPool (sizeof (HTTP_BOOT_VIRTUAL_NIC)); + if (Private->Ip6Nic == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + Private->Ip6Nic->Private = Private; + Private->Ip6Nic->ImageHandle = This->DriverBindingHandle; + Private->Ip6Nic->Signature = HTTP_BOOT_VIRTUAL_NIC_SIGNATURE; + + // + // Create Dhcp6 child and open Dhcp6 protocol + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiDhcp6ServiceBindingProtocolGuid, + &Private->Dhcp6Child + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Dhcp6Child, + &gEfiDhcp6ProtocolGuid, + (VOID **) &Private->Dhcp6, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create Ip6 child and open Ip6 protocol for background ICMP packets. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiIp6ServiceBindingProtocolGuid, + &Private->Ip6Child + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Ip6Child, + &gEfiIp6ProtocolGuid, + (VOID **) &Private->Ip6, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Locate Ip6Config protocol, it's required to configure the default gateway address. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiIp6ConfigProtocolGuid, + (VOID **) &Private->Ip6Config, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Append IPv6 device path node. + // + Node = AllocateZeroPool (sizeof (IPv6_DEVICE_PATH)); + if (Node == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + Node->Ipv6.Header.Type = MESSAGING_DEVICE_PATH; + Node->Ipv6.Header.SubType = MSG_IPv6_DP; + Node->Ipv6.PrefixLength = IP6_PREFIX_LENGTH; + SetDevicePathNodeLength (Node, sizeof (IPv6_DEVICE_PATH)); + DevicePath = AppendDevicePathNode(Private->ParentDevicePath, (EFI_DEVICE_PATH*) Node); + FreePool(Node); + if (DevicePath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + // + // Append URI device path node. + // + Node = AllocateZeroPool (sizeof (EFI_DEVICE_PATH_PROTOCOL)); + if (Node == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + Node->DevPath.Type = MESSAGING_DEVICE_PATH; + Node->DevPath.SubType = MSG_URI_DP; + SetDevicePathNodeLength (Node, sizeof (EFI_DEVICE_PATH_PROTOCOL)); + Private->Ip6Nic->DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (Node); + FreePool (DevicePath); + if (Private->Ip6Nic->DevicePath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + // + // Create a child handle for the HTTP boot and install DevPath and Load file protocol on it. + // + CopyMem (&Private->Ip6Nic->LoadFile, &gHttpBootDxeLoadFile, sizeof (Private->LoadFile)); + Status = gBS->InstallMultipleProtocolInterfaces ( + &Private->Ip6Nic->Controller, + &gEfiLoadFileProtocolGuid, + &Private->Ip6Nic->LoadFile, + &gEfiDevicePathProtocolGuid, + Private->Ip6Nic->DevicePath, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Open the Caller Id child to setup a parent-child relationship between + // real NIC handle and the HTTP boot child handle. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + This->DriverBindingHandle, + Private->Ip6Nic->Controller, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + if (Private != NULL) { + if (FirstStart) { + gBS->UninstallProtocolInterface ( + ControllerHandle, + &gEfiCallerIdGuid, + &Private->Id + ); + } + + HttpBootDestroyIp6Children(This, Private); + HttpBootConfigFormUnload (Private); + + if (FirstStart) { + FreePool (Private); + } + } + + return Status; +} + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +HttpBootIp6DxeDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_LOAD_FILE_PROTOCOL *LoadFile; + HTTP_BOOT_PRIVATE_DATA *Private; + EFI_HANDLE NicHandle; + UINT32 *Id; + + // + // Try to get the Load File Protocol from the controller handle. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiLoadFileProtocolGuid, + (VOID **) &LoadFile, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + // + // If failed, try to find the NIC handle for this controller. + // + NicHandle = HttpBootGetNicByIp6Children (ControllerHandle); + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + // + // Try to retrieve the private data by the Caller Id Guid. + // + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + Private = HTTP_BOOT_PRIVATE_DATA_FROM_ID (Id); + } else { + Private = HTTP_BOOT_PRIVATE_DATA_FROM_LOADFILE (LoadFile); + NicHandle = Private->Controller; + } + + // + // Disable the HTTP boot function. + // + Status = HttpBootStop (Private); + if (Status != EFI_SUCCESS && Status != EFI_NOT_STARTED) { + return Status; + } + + // + // Destory all child instance and uninstall protocol interface. + // + HttpBootDestroyIp6Children (This, Private); + + if (Private->Ip4Nic == NULL && Private->Ip6Nic == NULL) { + // + // Release the cached data. + // + HttpBootFreeCacheList (Private); + + // + // Unload the config form. + // + HttpBootConfigFormUnload (Private); + + gBS->UninstallProtocolInterface ( + NicHandle, + &gEfiCallerIdGuid, + &Private->Id + ); + FreePool (Private); + + } + + return EFI_SUCCESS; +} +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval Others An unexpected error occurred. + +**/ +EFI_STATUS +EFIAPI +HttpBootDxeDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + // + // Install UEFI Driver Model protocol(s). + // + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gHttpBootIp4DxeDriverBinding, + ImageHandle, + &gHttpBootDxeComponentName, + &gHttpBootDxeComponentName2 + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gHttpBootIp6DxeDriverBinding, + NULL, + &gHttpBootDxeComponentName, + &gHttpBootDxeComponentName2 + ); + if (EFI_ERROR (Status)) { + EfiLibUninstallDriverBindingComponentName2( + &gHttpBootIp4DxeDriverBinding, + &gHttpBootDxeComponentName, + &gHttpBootDxeComponentName2 + ); + } + return Status; +} + diff --git a/NetworkPkg/HttpBootDxe/HttpBootDxe.h b/NetworkPkg/HttpBootDxe/HttpBootDxe.h new file mode 100644 index 000000000..89b7d6aa7 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootDxe.h @@ -0,0 +1,524 @@ +/** @file + UEFI HTTP boot driver's private data structure and interfaces declaration. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_HTTP_BOOT_DXE_H__ +#define __EFI_HTTP_BOOT_DXE_H__ + +#include + +#include +#include + +// +// Libraries +// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +// UEFI Driver Model Protocols +// +#include +#include +#include + +// +// Consumed Protocols +// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +// Produced Protocols +// +#include +#include + +// +// Consumed Guids +// +#include + +// +// Driver Version +// +#define HTTP_BOOT_DXE_VERSION 0xa + +// +// Standard Media Types defined in +// http://www.iana.org/assignments/media-types +// +#define HTTP_CONTENT_TYPE_APP_EFI "application/efi" +#define HTTP_CONTENT_TYPE_APP_IMG "application/vnd.efi-img" +#define HTTP_CONTENT_TYPE_APP_ISO "application/vnd.efi-iso" + +// +// Protocol instances +// +extern EFI_DRIVER_BINDING_PROTOCOL gHttpBootDxeDriverBinding; +extern EFI_COMPONENT_NAME2_PROTOCOL gHttpBootDxeComponentName2; +extern EFI_COMPONENT_NAME_PROTOCOL gHttpBootDxeComponentName; + +// +// Private data structure +// +typedef struct _HTTP_BOOT_PRIVATE_DATA HTTP_BOOT_PRIVATE_DATA; +typedef struct _HTTP_BOOT_VIRTUAL_NIC HTTP_BOOT_VIRTUAL_NIC; + +typedef enum { + ImageTypeEfi, + ImageTypeVirtualCd, + ImageTypeVirtualDisk, + ImageTypeMax +} HTTP_BOOT_IMAGE_TYPE; + +// +// Include files with internal function prototypes +// +#include "HttpBootComponentName.h" +#include "HttpBootDhcp4.h" +#include "HttpBootDhcp6.h" +#include "HttpBootImpl.h" +#include "HttpBootSupport.h" +#include "HttpBootClient.h" +#include "HttpBootConfig.h" + +typedef union { + HTTP_BOOT_DHCP4_PACKET_CACHE Dhcp4; + HTTP_BOOT_DHCP6_PACKET_CACHE Dhcp6; +} HTTP_BOOT_DHCP_PACKET_CACHE; + +struct _HTTP_BOOT_VIRTUAL_NIC { + UINT32 Signature; + EFI_HANDLE Controller; + EFI_HANDLE ImageHandle; + EFI_LOAD_FILE_PROTOCOL LoadFile; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + HTTP_BOOT_PRIVATE_DATA *Private; +}; + +#define HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_INFO(Callback) \ + CR ( \ + Callback, \ + HTTP_BOOT_PRIVATE_DATA, \ + CallbackInfo, \ + HTTP_BOOT_PRIVATE_DATA_SIGNATURE \ + ) + +#define HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL(CallbackProtocol) \ + CR ( \ + CallbackProtocol, \ + HTTP_BOOT_PRIVATE_DATA, \ + LoadFileCallback, \ + HTTP_BOOT_PRIVATE_DATA_SIGNATURE \ + ) + +struct _HTTP_BOOT_PRIVATE_DATA { + UINT32 Signature; + EFI_HANDLE Controller; + + HTTP_BOOT_VIRTUAL_NIC *Ip4Nic; + HTTP_BOOT_VIRTUAL_NIC *Ip6Nic; + + // + // Cousumed children + // + EFI_HANDLE Ip6Child; + EFI_HANDLE Dhcp4Child; + EFI_HANDLE Dhcp6Child; + HTTP_IO HttpIo; + BOOLEAN HttpCreated; + + // + // Consumed protocol + // + EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *Nii; + EFI_IP6_PROTOCOL *Ip6; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + EFI_DHCP4_PROTOCOL *Dhcp4; + EFI_DHCP6_PROTOCOL *Dhcp6; + EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; + + + // + // Produced protocol + // + EFI_LOAD_FILE_PROTOCOL LoadFile; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + UINT32 Id; + EFI_HTTP_BOOT_CALLBACK_PROTOCOL *HttpBootCallback; + EFI_HTTP_BOOT_CALLBACK_PROTOCOL LoadFileCallback; + + // + // Data for the default HTTP Boot callback protocol + // + UINT64 FileSize; + UINT64 ReceivedSize; + UINT32 Percentage; + + // + // HII callback info block + // + HTTP_BOOT_FORM_CALLBACK_INFO CallbackInfo; + + // + // Mode data + // + BOOLEAN UsingIpv6; + BOOLEAN Started; + EFI_IP_ADDRESS StationIp; + EFI_IP_ADDRESS SubnetMask; + EFI_IP_ADDRESS GatewayIp; + EFI_IP_ADDRESS ServerIp; + UINT16 Port; + UINT32 DnsServerCount; + EFI_IP_ADDRESS *DnsServerIp; + + // + // The URI string attempt to download through HTTP, may point to + // the memory in cached DHCP offer, or to the memory in FilePathUri. + // + CHAR8 *BootFileUri; + VOID *BootFileUriParser; + UINTN BootFileSize; + BOOLEAN NoGateway; + HTTP_BOOT_IMAGE_TYPE ImageType; + + // + // URI string extracted from the input FilePath parameter. + // + CHAR8 *FilePathUri; + VOID *FilePathUriParser; + + // + // Cached HTTP data + // + LIST_ENTRY CacheList; + + // + // Cached DHCP offer + // + // OfferIndex records the index of DhcpOffer[] buffer, and OfferCount records the num of each type of offer. + // + // It supposed that + // + // OfferNum: 8 + // OfferBuffer: [ProxyNameUri, DhcpNameUri, DhcpIpUri, ProxyNameUri, ProxyIpUri, DhcpOnly, DhcpIpUri, DhcpNameUriDns] + // (OfferBuffer is 0-based.) + // + // And assume that (DhcpIpUri is the first priority actually.) + // + // SelectIndex: 5 + // SelectProxyType: HttpOfferTypeProxyIpUri + // (SelectIndex is 1-based, and 0 means no one is selected.) + // + // So it should be + // + // DhcpIpUri DhcpNameUriDns DhcpDns DhcpOnly ProxyNameUri ProxyIpUri DhcpNameUri + // OfferCount: [ 2, 1, 0, 1, 2, 1, 1] + // + // OfferIndex: {[ 2, 7, 0, 5, 0, *4, 1] + // [ 6, 0, 0, 0, 3, 0, 0] + // [ 0, 0, 0, 0, 0, 0, 0] + // ... ]} + // (OfferIndex is 0-based.) + // + // + UINT32 SelectIndex; + UINT32 SelectProxyType; + HTTP_BOOT_DHCP_PACKET_CACHE OfferBuffer[HTTP_BOOT_OFFER_MAX_NUM]; + UINT32 OfferNum; + UINT32 OfferCount[HttpOfferTypeMax]; + UINT32 OfferIndex[HttpOfferTypeMax][HTTP_BOOT_OFFER_MAX_NUM]; +}; + +#define HTTP_BOOT_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('H', 'B', 'P', 'D') +#define HTTP_BOOT_VIRTUAL_NIC_SIGNATURE SIGNATURE_32 ('H', 'B', 'V', 'N') +#define HTTP_BOOT_PRIVATE_DATA_FROM_LOADFILE(a) CR (a, HTTP_BOOT_PRIVATE_DATA, LoadFile, HTTP_BOOT_PRIVATE_DATA_SIGNATURE) +#define HTTP_BOOT_PRIVATE_DATA_FROM_ID(a) CR (a, HTTP_BOOT_PRIVATE_DATA, Id, HTTP_BOOT_PRIVATE_DATA_SIGNATURE) +#define HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE(a) CR (a, HTTP_BOOT_VIRTUAL_NIC, LoadFile, HTTP_BOOT_VIRTUAL_NIC_SIGNATURE) +extern EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile; + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +HttpBootIp4DxeDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +HttpBootIp4DxeDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +HttpBootIp4DxeDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +HttpBootIp6DxeDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +HttpBootIp6DxeDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +HttpBootIp6DxeDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); +#endif diff --git a/NetworkPkg/HttpBootDxe/HttpBootDxe.inf b/NetworkPkg/HttpBootDxe/HttpBootDxe.inf new file mode 100644 index 000000000..5beab728d --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootDxe.inf @@ -0,0 +1,99 @@ +## @file +# This modules produce the Load File Protocol for UEFI HTTP boot. +# +# Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = HttpBootDxe + FILE_GUID = ecebcb00-d9c8-11e4-af3d-8cdcd426c973 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = HttpBootDxeDriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload + MODULE_UNI_FILE = HttpBootDxe.uni + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[Sources] + HttpBootConfigNVDataStruc.h + HttpBootDxe.h + HttpBootDxe.c + HttpBootConfig.h + HttpBootConfig.c + HttpBootComponentName.h + HttpBootComponentName.c + HttpBootImpl.h + HttpBootImpl.c + HttpBootDhcp4.h + HttpBootDhcp4.c + HttpBootDhcp6.h + HttpBootDhcp6.c + HttpBootSupport.h + HttpBootSupport.c + HttpBootClient.h + HttpBootClient.c + HttpBootConfigVfr.vfr + HttpBootConfigStrings.uni + +[LibraryClasses] + UefiDriverEntryPoint + UefiBootServicesTableLib + MemoryAllocationLib + BaseLib + UefiLib + DevicePathLib + DebugLib + NetLib + HttpLib + HiiLib + PrintLib + DpcLib + UefiHiiServicesLib + UefiBootManagerLib + +[Protocols] + ## TO_START + ## BY_START + gEfiDevicePathProtocolGuid + + gEfiLoadFileProtocolGuid ## BY_START + gEfiHttpServiceBindingProtocolGuid ## CONSUMES + gEfiHttpProtocolGuid ## CONSUMES + gEfiDhcp4ServiceBindingProtocolGuid ## TO_START + gEfiDhcp4ProtocolGuid ## TO_START + gEfiIp4Config2ProtocolGuid ## TO_START + gEfiDhcp6ServiceBindingProtocolGuid ## TO_START + gEfiDhcp6ProtocolGuid ## TO_START + gEfiDns6ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiDns6ProtocolGuid ## SOMETIMES_CONSUMES + gEfiIp6ServiceBindingProtocolGuid ## TO_START + gEfiIp6ProtocolGuid ## TO_START + gEfiIp6ConfigProtocolGuid ## TO_START + gEfiNetworkInterfaceIdentifierProtocolGuid_31 ## SOMETIMES_CONSUMES + gEfiRamDiskProtocolGuid ## SOMETIMES_CONSUMES + gEfiHiiConfigAccessProtocolGuid ## BY_START + gEfiHttpBootCallbackProtocolGuid ## SOMETIMES_PRODUCES + gEfiAdapterInformationProtocolGuid ## SOMETIMES_CONSUMES + +[Guids] + ## SOMETIMES_CONSUMES ## GUID # HiiIsConfigHdrMatch mHttpBootConfigStorageName + ## SOMETIMES_PRODUCES ## GUID # HiiConstructConfigHdr mHttpBootConfigStorageName + ## SOMETIMES_PRODUCES ## GUID # HiiGetBrowserData mHttpBootConfigStorageName + ## SOMETIMES_CONSUMES ## HII + gHttpBootConfigGuid + gEfiVirtualCdGuid ## SOMETIMES_CONSUMES ## GUID + gEfiVirtualDiskGuid ## SOMETIMES_CONSUMES ## GUID + gEfiAdapterInfoUndiIpv6SupportGuid ## SOMETIMES_CONSUMES ## GUID + +[Pcd] + gEfiNetworkPkgTokenSpaceGuid.PcdAllowHttpConnections ## CONSUMES + +[UserExtensions.TianoCore."ExtraFiles"] + HttpBootDxeExtra.uni diff --git a/NetworkPkg/HttpBootDxe/HttpBootDxe.uni b/NetworkPkg/HttpBootDxe/HttpBootDxe.uni new file mode 100644 index 000000000..68143cc4f --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootDxe.uni @@ -0,0 +1,18 @@ +// /** @file +// UEFI HTTP boot service. +// +// This driver provides EFI Load File Protocol which is used to download +// the boot image from HTTP server. It could work with an IPv4 or IPv6 stack. +// +// +// Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "UEFI HTTP boot service" + +#string STR_MODULE_DESCRIPTION #language en-US "This driver provides EFI Load File Protocol which is used to download the boot image from HTTP server. It could work with an IPv4 or IPv6 stack." + diff --git a/NetworkPkg/HttpBootDxe/HttpBootDxeExtra.uni b/NetworkPkg/HttpBootDxe/HttpBootDxeExtra.uni new file mode 100644 index 000000000..b1b4ed0f0 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// HttpBootDxe Localized Strings and Content +// +// Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"UEFI HTTP BOOT DXE" + + diff --git a/NetworkPkg/HttpBootDxe/HttpBootImpl.c b/NetworkPkg/HttpBootDxe/HttpBootImpl.c new file mode 100644 index 000000000..5eeb1ea2b --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootImpl.c @@ -0,0 +1,767 @@ +/** @file + The implementation of EFI_LOAD_FILE_PROTOCOL for UEFI HTTP boot. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpBootDxe.h" + +/** + Install HTTP Boot Callback Protocol if not installed before. + + @param[in] Private Pointer to HTTP Boot private data. + + @retval EFI_SUCCESS HTTP Boot Callback Protocol installed succesfully. + @retval Others Failed to install HTTP Boot Callback Protocol. + +**/ +EFI_STATUS +HttpBootInstallCallback ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + EFI_STATUS Status; + EFI_HANDLE ControllerHandle; + + if (!Private->UsingIpv6) { + ControllerHandle = Private->Ip4Nic->Controller; + } else { + ControllerHandle = Private->Ip6Nic->Controller; + } + + // + // Check whether gEfiHttpBootCallbackProtocolGuid already installed. + // + Status = gBS->HandleProtocol ( + ControllerHandle, + &gEfiHttpBootCallbackProtocolGuid, + (VOID **) &Private->HttpBootCallback + ); + if (Status == EFI_UNSUPPORTED) { + + CopyMem ( + &Private->LoadFileCallback, + &gHttpBootDxeHttpBootCallback, + sizeof (EFI_HTTP_BOOT_CALLBACK_PROTOCOL) + ); + + // + // Install a default callback if user didn't offer one. + // + Status = gBS->InstallProtocolInterface ( + &ControllerHandle, + &gEfiHttpBootCallbackProtocolGuid, + EFI_NATIVE_INTERFACE, + &Private->LoadFileCallback + ); + if (EFI_ERROR (Status)) { + return Status; + } + Private->HttpBootCallback = &Private->LoadFileCallback; + } + + return EFI_SUCCESS; +} + +/** + Uninstall HTTP Boot Callback Protocol if it's installed by this driver. + + @param[in] Private Pointer to HTTP Boot private data. + +**/ +VOID +HttpBootUninstallCallback ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + if (Private->HttpBootCallback == &Private->LoadFileCallback) { + gBS->UninstallProtocolInterface ( + Private->Controller, + &gEfiHttpBootCallbackProtocolGuid, + &Private->HttpBootCallback + ); + Private->HttpBootCallback = NULL; + } +} + +/** + Enable the use of UEFI HTTP boot function. + + If the driver has already been started but not satisfy the requirement (IP stack and + specified boot file path), this function will stop the driver and start it again. + + @param[in] Private The pointer to the driver's private data. + @param[in] UsingIpv6 Specifies the type of IP addresses that are to be + used during the session that is being started. + Set to TRUE for IPv6, and FALSE for IPv4. + @param[in] FilePath The device specific path of the file to load. + + @retval EFI_SUCCESS HTTP boot was successfully enabled. + @retval EFI_INVALID_PARAMETER Private is NULL or FilePath is NULL. + @retval EFI_INVALID_PARAMETER The FilePath doesn't contain a valid URI device path node. + @retval EFI_ALREADY_STARTED The driver is already in started state. + @retval EFI_OUT_OF_RESOURCES There are not enough resources. + +**/ +EFI_STATUS +HttpBootStart ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN BOOLEAN UsingIpv6, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath + ) +{ + UINTN Index; + EFI_STATUS Status; + CHAR8 *Uri; + + Uri = NULL; + + if (Private == NULL || FilePath == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Check the URI in the input FilePath, in order to see whether it is + // required to boot from a new specified boot file. + // + Status = HttpBootParseFilePath (FilePath, &Uri); + if (EFI_ERROR (Status)) { + return EFI_INVALID_PARAMETER; + } + + // + // Check whether we need to stop and restart the HTTP boot driver. + // + if (Private->Started) { + // + // Restart is needed in 2 cases: + // 1. Http boot driver has already been started but not on the required IP stack. + // 2. The specified boot file URI in FilePath is different with the one we have + // recorded before. + // + if ((UsingIpv6 != Private->UsingIpv6) || + ((Uri != NULL) && (AsciiStrCmp (Private->BootFileUri, Uri) != 0))) { + // + // Restart is required, first stop then continue this start function. + // + Status = HttpBootStop (Private); + if (EFI_ERROR (Status)) { + if (Uri != NULL) { + FreePool (Uri); + } + return Status; + } + } else { + // + // Restart is not required. + // + if (Uri != NULL) { + FreePool (Uri); + } + return EFI_ALREADY_STARTED; + } + } + + // + // Detect whether using ipv6 or not, and set it to the private data. + // + if (UsingIpv6 && Private->Ip6Nic != NULL) { + Private->UsingIpv6 = TRUE; + } else if (!UsingIpv6 && Private->Ip4Nic != NULL) { + Private->UsingIpv6 = FALSE; + } else { + if (Uri != NULL) { + FreePool (Uri); + } + return EFI_UNSUPPORTED; + } + + // + // Record the specified URI and prepare the URI parser if needed. + // + Private->FilePathUri = Uri; + if (Private->FilePathUri != NULL) { + Status = HttpParseUrl ( + Private->FilePathUri, + (UINT32) AsciiStrLen (Private->FilePathUri), + FALSE, + &Private->FilePathUriParser + ); + if (EFI_ERROR (Status)) { + FreePool (Private->FilePathUri); + return Status; + } + } + + // + // Init the content of cached DHCP offer list. + // + ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer)); + if (!Private->UsingIpv6) { + for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) { + Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = HTTP_CACHED_DHCP4_PACKET_MAX_SIZE; + } + } else { + for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) { + Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = HTTP_CACHED_DHCP6_PACKET_MAX_SIZE; + } + } + + if (Private->UsingIpv6) { + // + // Set Ip6 policy to Automatic to start the Ip6 router discovery. + // + Status = HttpBootSetIp6Policy (Private); + if (EFI_ERROR (Status)) { + return Status; + } + } + Private->Started = TRUE; + Print (L"\n>>Start HTTP Boot over IPv%d", Private->UsingIpv6 ? 6 : 4); + + return EFI_SUCCESS; +} + +/** + Attempt to complete a DHCPv4 D.O.R.A or DHCPv6 S.R.A.A sequence to retrieve the boot resource information. + + @param[in] Private The pointer to the driver's private data. + + @retval EFI_SUCCESS Boot info was successfully retrieved. + @retval EFI_INVALID_PARAMETER Private is NULL. + @retval EFI_NOT_STARTED The driver is in stopped state. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpBootDhcp ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + EFI_STATUS Status; + + if (Private == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (!Private->Started) { + return EFI_NOT_STARTED; + } + + Status = EFI_DEVICE_ERROR; + + if (!Private->UsingIpv6) { + // + // Start D.O.R.A process to get a IPv4 address and other boot information. + // + Status = HttpBootDhcp4Dora (Private); + } else { + // + // Start S.A.R.R process to get a IPv6 address and other boot information. + // + Status = HttpBootDhcp6Sarr (Private); + } + + return Status; +} + +/** + Attempt to download the boot file through HTTP message exchange. + + @param[in] Private The pointer to the driver's private data. + @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return + code of EFI_SUCCESS, the amount of data transferred to + Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL, + the size of Buffer required to retrieve the requested file. + @param[in] Buffer The memory buffer to transfer the file to. If Buffer is NULL, + then the size of the requested file is returned in + BufferSize. + @param[out] ImageType The image type of the downloaded file. + + @retval EFI_SUCCESS Boot file was loaded successfully. + @retval EFI_INVALID_PARAMETER Private is NULL, or ImageType is NULL, or BufferSize is NULL. + @retval EFI_INVALID_PARAMETER *BufferSize is not zero, and Buffer is NULL. + @retval EFI_NOT_STARTED The driver is in stopped state. + @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the boot file. BufferSize has + been updated with the size needed to complete the request. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpBootLoadFile ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN OUT UINTN *BufferSize, + IN VOID *Buffer, OPTIONAL + OUT HTTP_BOOT_IMAGE_TYPE *ImageType + ) +{ + EFI_STATUS Status; + + if (Private == NULL || ImageType == NULL || BufferSize == NULL ) { + return EFI_INVALID_PARAMETER; + } + + if (*BufferSize != 0 && Buffer == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (!Private->Started) { + return EFI_NOT_STARTED; + } + + Status = HttpBootInstallCallback (Private); + if (EFI_ERROR(Status)) { + goto ON_EXIT; + } + + if (Private->BootFileUri == NULL) { + // + // Parse the cached offer to get the boot file URL first. + // + Status = HttpBootDiscoverBootInfo (Private); + if (EFI_ERROR (Status)) { + AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n"); + goto ON_EXIT; + } + } + + if (!Private->HttpCreated) { + // + // Create HTTP child. + // + Status = HttpBootCreateHttpIo (Private); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + if (Private->BootFileSize == 0) { + // + // Discover the information about the bootfile if we haven't. + // + + // + // Try to use HTTP HEAD method. + // + Status = HttpBootGetBootFile ( + Private, + TRUE, + &Private->BootFileSize, + NULL, + &Private->ImageType + ); + if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) { + // + // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method. + // + ASSERT (Private->BootFileSize == 0); + Status = HttpBootGetBootFile ( + Private, + FALSE, + &Private->BootFileSize, + NULL, + &Private->ImageType + ); + if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) { + AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n"); + goto ON_EXIT; + } + } + } + + if (*BufferSize < Private->BootFileSize) { + *BufferSize = Private->BootFileSize; + *ImageType = Private->ImageType; + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + + // + // Load the boot file into Buffer + // + Status = HttpBootGetBootFile ( + Private, + FALSE, + BufferSize, + Buffer, + ImageType + ); + +ON_EXIT: + HttpBootUninstallCallback (Private); + + if (EFI_ERROR (Status)) { + if (Status == EFI_ACCESS_DENIED) { + AsciiPrint ("\n Error: Could not establish connection with HTTP server.\n"); + } else if (Status == EFI_BUFFER_TOO_SMALL && Buffer != NULL) { + AsciiPrint ("\n Error: Buffer size is smaller than the requested file.\n"); + } else if (Status == EFI_OUT_OF_RESOURCES) { + AsciiPrint ("\n Error: Could not allocate I/O buffers.\n"); + } else if (Status == EFI_DEVICE_ERROR) { + AsciiPrint ("\n Error: Network device error.\n"); + } else if (Status == EFI_TIMEOUT) { + AsciiPrint ("\n Error: Server response timeout.\n"); + } else if (Status == EFI_ABORTED) { + AsciiPrint ("\n Error: Remote boot cancelled.\n"); + } else if (Status != EFI_BUFFER_TOO_SMALL) { + AsciiPrint ("\n Error: Unexpected network error.\n"); + } + } + + return Status; +} + +/** + Disable the use of UEFI HTTP boot function. + + @param[in] Private The pointer to the driver's private data. + + @retval EFI_SUCCESS HTTP boot was successfully disabled. + @retval EFI_NOT_STARTED The driver is already in stopped state. + @retval EFI_INVALID_PARAMETER Private is NULL. + @retval Others Unexpected error when stop the function. + +**/ +EFI_STATUS +HttpBootStop ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ) +{ + UINTN Index; + + if (Private == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (!Private->Started) { + return EFI_NOT_STARTED; + } + + if (Private->HttpCreated) { + HttpIoDestroyIo (&Private->HttpIo); + Private->HttpCreated = FALSE; + } + + Private->Started = FALSE; + ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS)); + ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS)); + ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS)); + Private->Port = 0; + Private->BootFileUri = NULL; + Private->BootFileUriParser = NULL; + Private->BootFileSize = 0; + Private->SelectIndex = 0; + Private->SelectProxyType = HttpOfferTypeMax; + + if (!Private->UsingIpv6) { + // + // Stop and release the DHCP4 child. + // + Private->Dhcp4->Stop (Private->Dhcp4); + Private->Dhcp4->Configure (Private->Dhcp4, NULL); + + for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) { + if (Private->OfferBuffer[Index].Dhcp4.UriParser) { + HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp4.UriParser); + } + } + } else { + // + // Stop and release the DHCP6 child. + // + Private->Dhcp6->Stop (Private->Dhcp6); + Private->Dhcp6->Configure (Private->Dhcp6, NULL); + + for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) { + if (Private->OfferBuffer[Index].Dhcp6.UriParser) { + HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp6.UriParser); + } + } + } + + if (Private->DnsServerIp != NULL) { + FreePool (Private->DnsServerIp); + Private->DnsServerIp = NULL; + } + + if (Private->FilePathUri!= NULL) { + FreePool (Private->FilePathUri); + HttpUrlFreeParser (Private->FilePathUriParser); + Private->FilePathUri = NULL; + Private->FilePathUriParser = NULL; + } + + ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer)); + Private->OfferNum = 0; + ZeroMem (Private->OfferCount, sizeof (Private->OfferCount)); + ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex)); + + HttpBootFreeCacheList (Private); + + return EFI_SUCCESS; +} + +/** + Causes the driver to load a specified file. + + @param This Protocol instance pointer. + @param FilePath The device specific path of the file to load. + @param BootPolicy If TRUE, indicates that the request originates from the + boot manager is attempting to load FilePath as a boot + selection. If FALSE, then FilePath must match as exact file + to be loaded. + @param BufferSize On input the size of Buffer in bytes. On output with a return + code of EFI_SUCCESS, the amount of data transferred to + Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL, + the size of Buffer required to retrieve the requested file. + @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL, + then the size of the requested file is returned in + BufferSize. + + @retval EFI_SUCCESS The file was loaded. + @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy + @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or + BufferSize is NULL. + @retval EFI_NO_MEDIA No medium was present to load the file. + @retval EFI_DEVICE_ERROR The file was not loaded due to a device error. + @retval EFI_NO_RESPONSE The remote system did not respond. + @retval EFI_NOT_FOUND The file was not found. + @retval EFI_ABORTED The file load process was manually cancelled. + @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry. + BufferSize has been updated with the size needed to complete + the request. + +**/ +EFI_STATUS +EFIAPI +HttpBootDxeLoadFile ( + IN EFI_LOAD_FILE_PROTOCOL *This, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath, + IN BOOLEAN BootPolicy, + IN OUT UINTN *BufferSize, + IN VOID *Buffer OPTIONAL + ) +{ + HTTP_BOOT_PRIVATE_DATA *Private; + HTTP_BOOT_VIRTUAL_NIC *VirtualNic; + EFI_STATUS MediaStatus; + BOOLEAN UsingIpv6; + EFI_STATUS Status; + HTTP_BOOT_IMAGE_TYPE ImageType; + + if (This == NULL || BufferSize == NULL || FilePath == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Only support BootPolicy + // + if (!BootPolicy) { + return EFI_UNSUPPORTED; + } + + VirtualNic = HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This); + Private = VirtualNic->Private; + + // + // Check media status before HTTP boot start + // + MediaStatus = EFI_SUCCESS; + NetLibDetectMediaWaitTimeout (Private->Controller, HTTP_BOOT_CHECK_MEDIA_WAITING_TIME, &MediaStatus); + if (MediaStatus != EFI_SUCCESS) { + AsciiPrint ("\n Error: Could not detect network connection.\n"); + return EFI_NO_MEDIA; + } + + // + // Check whether the virtual nic is using IPv6 or not. + // + UsingIpv6 = FALSE; + if (VirtualNic == Private->Ip6Nic) { + UsingIpv6 = TRUE; + } + + // + // Initialize HTTP boot. + // + Status = HttpBootStart (Private, UsingIpv6, FilePath); + if (Status != EFI_SUCCESS && Status != EFI_ALREADY_STARTED) { + return Status; + } + + // + // Load the boot file. + // + ImageType = ImageTypeMax; + Status = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType); + if (EFI_ERROR (Status)) { + if (Status == EFI_BUFFER_TOO_SMALL && (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk)) { + Status = EFI_WARN_FILE_SYSTEM; + } else if (Status != EFI_BUFFER_TOO_SMALL) { + HttpBootStop (Private); + } + return Status; + } + + // + // Register the RAM Disk to the system if needed. + // + if (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk) { + Status = HttpBootRegisterRamDisk (Private, *BufferSize, Buffer, ImageType); + if (!EFI_ERROR (Status)) { + Status = EFI_WARN_FILE_SYSTEM; + } else { + AsciiPrint ("\n Error: Could not register RAM disk to the system.\n"); + } + } + + // + // Stop the HTTP Boot service after the boot image is downloaded. + // + HttpBootStop (Private); + return Status; +} + +/// +/// Load File Protocol instance +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile = { + HttpBootDxeLoadFile +}; + +/** + Callback function that is invoked when the HTTP Boot driver is about to transmit or has received a + packet. + + This function is invoked when the HTTP Boot driver is about to transmit or has received packet. + Parameters DataType and Received specify the type of event and the format of the buffer pointed + to by Data. Due to the polling nature of UEFI device drivers, this callback function should not + execute for more than 5 ms. + The returned status code determines the behavior of the HTTP Boot driver. + + @param[in] This Pointer to the EFI_HTTP_BOOT_CALLBACK_PROTOCOL instance. + @param[in] DataType The event that occurs in the current state. + @param[in] Received TRUE if the callback is being invoked due to a receive event. + FALSE if the callback is being invoked due to a transmit event. + @param[in] DataLength The length in bytes of the buffer pointed to by Data. + @param[in] Data A pointer to the buffer of data, the data type is specified by + DataType. + + @retval EFI_SUCCESS Tells the HTTP Boot driver to continue the HTTP Boot process. + @retval EFI_ABORTED Tells the HTTP Boot driver to abort the current HTTP Boot process. +**/ +EFI_STATUS +EFIAPI +HttpBootCallback ( + IN EFI_HTTP_BOOT_CALLBACK_PROTOCOL *This, + IN EFI_HTTP_BOOT_CALLBACK_DATA_TYPE DataType, + IN BOOLEAN Received, + IN UINT32 DataLength, + IN VOID *Data OPTIONAL + ) +{ + EFI_HTTP_MESSAGE *HttpMessage; + EFI_HTTP_HEADER *HttpHeader; + HTTP_BOOT_PRIVATE_DATA *Private; + UINT32 Percentage; + + Private = HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL(This); + + switch (DataType) { + case HttpBootDhcp4: + case HttpBootDhcp6: + Print (L"."); + break; + + case HttpBootHttpRequest: + if (Data != NULL) { + HttpMessage = (EFI_HTTP_MESSAGE *) Data; + if (HttpMessage->Data.Request->Method == HttpMethodGet && + HttpMessage->Data.Request->Url != NULL) { + Print (L"\n URI: %s\n", HttpMessage->Data.Request->Url); + } + } + break; + + case HttpBootHttpResponse: + if (Data != NULL) { + HttpMessage = (EFI_HTTP_MESSAGE *) Data; + + if (HttpMessage->Data.Response != NULL) { + if (HttpBootIsHttpRedirectStatusCode (HttpMessage->Data.Response->StatusCode)) { + // + // Server indicates the resource has been redirected to a different URL + // according to the section 6.4 of RFC7231 and the RFC 7538. + // Display the redirect information on the screen. + // + HttpHeader = HttpFindHeader ( + HttpMessage->HeaderCount, + HttpMessage->Headers, + HTTP_HEADER_LOCATION + ); + if (HttpHeader != NULL) { + Print (L"\n HTTP ERROR: Resource Redirected.\n New Location: %a\n", HttpHeader->FieldValue); + } + break; + } + } + + HttpHeader = HttpFindHeader ( + HttpMessage->HeaderCount, + HttpMessage->Headers, + HTTP_HEADER_CONTENT_LENGTH + ); + if (HttpHeader != NULL) { + Private->FileSize = AsciiStrDecimalToUintn (HttpHeader->FieldValue); + Private->ReceivedSize = 0; + Private->Percentage = 0; + } + } + break; + + case HttpBootHttpEntityBody: + if (DataLength != 0) { + if (Private->FileSize != 0) { + // + // We already know the file size, print in percentage format. + // + if (Private->ReceivedSize == 0) { + Print (L" File Size: %lu Bytes\n", Private->FileSize); + } + Private->ReceivedSize += DataLength; + Percentage = (UINT32) DivU64x64Remainder (MultU64x32 (Private->ReceivedSize, 100), Private->FileSize, NULL); + if (Private->Percentage != Percentage) { + Private->Percentage = Percentage; + Print (L"\r Downloading...%d%%", Percentage); + } + } else { + // + // In some case we couldn't get the file size from the HTTP header, so we + // just print the downloaded file size. + // + Private->ReceivedSize += DataLength; + Print (L"\r Downloading...%lu Bytes", Private->ReceivedSize); + } + } + break; + + default: + break; + }; + + return EFI_SUCCESS; +} + +/// +/// HTTP Boot Callback Protocol instance +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_HTTP_BOOT_CALLBACK_PROTOCOL gHttpBootDxeHttpBootCallback = { + HttpBootCallback +}; diff --git a/NetworkPkg/HttpBootDxe/HttpBootImpl.h b/NetworkPkg/HttpBootDxe/HttpBootImpl.h new file mode 100644 index 000000000..7b30583e2 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootImpl.h @@ -0,0 +1,48 @@ +/** @file + The declaration of UEFI HTTP boot function. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#ifndef __EFI_HTTP_BOOT_IMPL_H__ +#define __EFI_HTTP_BOOT_IMPL_H__ + +#define HTTP_BOOT_CHECK_MEDIA_WAITING_TIME EFI_TIMER_PERIOD_SECONDS(20) + +/** + Attempt to complete a DHCPv4 D.O.R.A or DHCPv6 S.R.A.A sequence to retrieve the boot resource information. + + @param[in] Private The pointer to the driver's private data. + + @retval EFI_SUCCESS Boot info was successfully retrieved. + @retval EFI_INVALID_PARAMETER Private is NULL. + @retval EFI_NOT_STARTED The driver is in stopped state. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpBootDhcp ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +/** + Disable the use of UEFI HTTP boot function. + + @param[in] Private The pointer to the driver's private data. + + @retval EFI_SUCCESS HTTP boot was successfully disabled. + @retval EFI_NOT_STARTED The driver is already in stopped state. + @retval EFI_INVALID_PARAMETER Private is NULL. + @retval Others Unexpected error when stop the function. + +**/ +EFI_STATUS +HttpBootStop ( + IN HTTP_BOOT_PRIVATE_DATA *Private + ); + +extern EFI_HTTP_BOOT_CALLBACK_PROTOCOL gHttpBootDxeHttpBootCallback; + +#endif diff --git a/NetworkPkg/HttpBootDxe/HttpBootSupport.c b/NetworkPkg/HttpBootDxe/HttpBootSupport.c new file mode 100644 index 000000000..61814d3b6 --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootSupport.c @@ -0,0 +1,1338 @@ +/** @file + Support functions implementation for UEFI HTTP boot driver. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpBootDxe.h" + + +/** + Get the Nic handle using any child handle in the IPv4 stack. + + @param[in] ControllerHandle Pointer to child handle over IPv4. + + @return NicHandle The pointer to the Nic handle. + @return NULL Can't find the Nic handle. + +**/ +EFI_HANDLE +HttpBootGetNicByIp4Children ( + IN EFI_HANDLE ControllerHandle + ) +{ + EFI_HANDLE NicHandle; + + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiHttpProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp4ProtocolGuid); + if (NicHandle == NULL) { + return NULL; + } + } + + return NicHandle; +} + +/** + Get the Nic handle using any child handle in the IPv6 stack. + + @param[in] ControllerHandle Pointer to child handle over IPv6. + + @return NicHandle The pointer to the Nic handle. + @return NULL Can't find the Nic handle. + +**/ +EFI_HANDLE +HttpBootGetNicByIp6Children ( + IN EFI_HANDLE ControllerHandle + ) +{ + EFI_HANDLE NicHandle; + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiHttpProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp6ProtocolGuid); + if (NicHandle == NULL) { + return NULL; + } + } + + return NicHandle; +} + +/** + This function is to convert UINTN to ASCII string with the required formatting. + + @param[in] Number Numeric value to be converted. + @param[in] Buffer The pointer to the buffer for ASCII string. + @param[in] Length The length of the required format. + +**/ +VOID +HttpBootUintnToAscDecWithFormat ( + IN UINTN Number, + IN UINT8 *Buffer, + IN INTN Length + ) +{ + UINTN Remainder; + + for (; Length > 0; Length--) { + Remainder = Number % 10; + Number /= 10; + Buffer[Length - 1] = (UINT8) ('0' + Remainder); + } +} + +/** + This function is to display the IPv4 address. + + @param[in] Ip The pointer to the IPv4 address. + +**/ +VOID +HttpBootShowIp4Addr ( + IN EFI_IPv4_ADDRESS *Ip + ) +{ + UINTN Index; + + for (Index = 0; Index < 4; Index++) { + AsciiPrint ("%d", Ip->Addr[Index]); + if (Index < 3) { + AsciiPrint ("."); + } + } +} + +/** + This function is to display the IPv6 address. + + @param[in] Ip The pointer to the IPv6 address. + +**/ +VOID +HttpBootShowIp6Addr ( + IN EFI_IPv6_ADDRESS *Ip + ) +{ + UINTN Index; + + for (Index = 0; Index < 16; Index++) { + + if (Ip->Addr[Index] != 0) { + AsciiPrint ("%x", Ip->Addr[Index]); + } + Index++; + if (Index > 15) { + return; + } + if (((Ip->Addr[Index] & 0xf0) == 0) && (Ip->Addr[Index - 1] != 0)) { + AsciiPrint ("0"); + } + AsciiPrint ("%x", Ip->Addr[Index]); + if (Index < 15) { + AsciiPrint (":"); + } + } +} + +/** + This function is to display the HTTP error status. + + @param[in] StatusCode The status code value in HTTP message. + +**/ +VOID +HttpBootPrintErrorMessage ( + EFI_HTTP_STATUS_CODE StatusCode + ) +{ + AsciiPrint ("\n"); + + switch (StatusCode) { + case HTTP_STATUS_300_MULTIPLE_CHOICES: + AsciiPrint ("\n Redirection: 300 Multiple Choices"); + break; + + case HTTP_STATUS_301_MOVED_PERMANENTLY: + AsciiPrint ("\n Redirection: 301 Moved Permanently"); + break; + + case HTTP_STATUS_302_FOUND: + AsciiPrint ("\n Redirection: 302 Found"); + break; + + case HTTP_STATUS_303_SEE_OTHER: + AsciiPrint ("\n Redirection: 303 See Other"); + break; + + case HTTP_STATUS_304_NOT_MODIFIED: + AsciiPrint ("\n Redirection: 304 Not Modified"); + break; + + case HTTP_STATUS_305_USE_PROXY: + AsciiPrint ("\n Redirection: 305 Use Proxy"); + break; + + case HTTP_STATUS_307_TEMPORARY_REDIRECT: + AsciiPrint ("\n Redirection: 307 Temporary Redirect"); + break; + + case HTTP_STATUS_308_PERMANENT_REDIRECT: + AsciiPrint ("\n Redirection: 308 Permanent Redirect"); + break; + + case HTTP_STATUS_400_BAD_REQUEST: + AsciiPrint ("\n Client Error: 400 Bad Request"); + break; + + case HTTP_STATUS_401_UNAUTHORIZED: + AsciiPrint ("\n Client Error: 401 Unauthorized"); + break; + + case HTTP_STATUS_402_PAYMENT_REQUIRED: + AsciiPrint ("\n Client Error: 402 Payment Required"); + break; + + case HTTP_STATUS_403_FORBIDDEN: + AsciiPrint ("\n Client Error: 403 Forbidden"); + break; + + case HTTP_STATUS_404_NOT_FOUND: + AsciiPrint ("\n Client Error: 404 Not Found"); + break; + + case HTTP_STATUS_405_METHOD_NOT_ALLOWED: + AsciiPrint ("\n Client Error: 405 Method Not Allowed"); + break; + + case HTTP_STATUS_406_NOT_ACCEPTABLE: + AsciiPrint ("\n Client Error: 406 Not Acceptable"); + break; + + case HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED: + AsciiPrint ("\n Client Error: 407 Proxy Authentication Required"); + break; + + case HTTP_STATUS_408_REQUEST_TIME_OUT: + AsciiPrint ("\n Client Error: 408 Request Timeout"); + break; + + case HTTP_STATUS_409_CONFLICT: + AsciiPrint ("\n Client Error: 409 Conflict"); + break; + + case HTTP_STATUS_410_GONE: + AsciiPrint ("\n Client Error: 410 Gone"); + break; + + case HTTP_STATUS_411_LENGTH_REQUIRED: + AsciiPrint ("\n Client Error: 411 Length Required"); + break; + + case HTTP_STATUS_412_PRECONDITION_FAILED: + AsciiPrint ("\n Client Error: 412 Precondition Failed"); + break; + + case HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE: + AsciiPrint ("\n Client Error: 413 Request Entity Too Large"); + break; + + case HTTP_STATUS_414_REQUEST_URI_TOO_LARGE: + AsciiPrint ("\n Client Error: 414 Request URI Too Long"); + break; + + case HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE: + AsciiPrint ("\n Client Error: 415 Unsupported Media Type"); + break; + + case HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED: + AsciiPrint ("\n Client Error: 416 Requested Range Not Satisfiable"); + break; + + case HTTP_STATUS_417_EXPECTATION_FAILED: + AsciiPrint ("\n Client Error: 417 Expectation Failed"); + break; + + case HTTP_STATUS_500_INTERNAL_SERVER_ERROR: + AsciiPrint ("\n Server Error: 500 Internal Server Error"); + break; + + case HTTP_STATUS_501_NOT_IMPLEMENTED: + AsciiPrint ("\n Server Error: 501 Not Implemented"); + break; + + case HTTP_STATUS_502_BAD_GATEWAY: + AsciiPrint ("\n Server Error: 502 Bad Gateway"); + break; + + case HTTP_STATUS_503_SERVICE_UNAVAILABLE: + AsciiPrint ("\n Server Error: 503 Service Unavailable"); + break; + + case HTTP_STATUS_504_GATEWAY_TIME_OUT: + AsciiPrint ("\n Server Error: 504 Gateway Timeout"); + break; + + case HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED: + AsciiPrint ("\n Server Error: 505 HTTP Version Not Supported"); + break; + + default: ; + + } +} + +/** + Notify the callback function when an event is triggered. + + @param[in] Event The triggered event. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +HttpBootCommonNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + *((BOOLEAN *) Context) = TRUE; +} + +/** + Retrieve the host address using the EFI_DNS6_PROTOCOL. + + @param[in] Private The pointer to the driver's private data. + @param[in] HostName Pointer to buffer containing hostname. + @param[out] IpAddress On output, pointer to buffer containing IPv6 address. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. +**/ +EFI_STATUS +HttpBootDns ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN CHAR16 *HostName, + OUT EFI_IPv6_ADDRESS *IpAddress + ) +{ + EFI_STATUS Status; + EFI_DNS6_PROTOCOL *Dns6; + EFI_DNS6_CONFIG_DATA Dns6ConfigData; + EFI_DNS6_COMPLETION_TOKEN Token; + EFI_HANDLE Dns6Handle; + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + EFI_IPv6_ADDRESS *DnsServerList; + UINTN DnsServerListCount; + UINTN DataSize; + BOOLEAN IsDone; + + DnsServerList = NULL; + DnsServerListCount = 0; + Dns6 = NULL; + Dns6Handle = NULL; + ZeroMem (&Token, sizeof (EFI_DNS6_COMPLETION_TOKEN)); + + // + // Get DNS server list from EFI IPv6 Configuration protocol. + // + Status = gBS->HandleProtocol (Private->Controller, &gEfiIp6ConfigProtocolGuid, (VOID **) &Ip6Config); + if (!EFI_ERROR (Status)) { + // + // Get the required size. + // + DataSize = 0; + Status = Ip6Config->GetData (Ip6Config, Ip6ConfigDataTypeDnsServer, &DataSize, NULL); + if (Status == EFI_BUFFER_TOO_SMALL) { + DnsServerList = AllocatePool (DataSize); + if (DnsServerList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = Ip6Config->GetData (Ip6Config, Ip6ConfigDataTypeDnsServer, &DataSize, DnsServerList); + if (EFI_ERROR (Status)) { + FreePool (DnsServerList); + DnsServerList = NULL; + } else { + DnsServerListCount = DataSize / sizeof (EFI_IPv6_ADDRESS); + } + } + } + // + // Create a DNSv6 child instance and get the protocol. + // + Status = NetLibCreateServiceChild ( + Private->Controller, + Private->Ip6Nic->ImageHandle, + &gEfiDns6ServiceBindingProtocolGuid, + &Dns6Handle + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status = gBS->OpenProtocol ( + Dns6Handle, + &gEfiDns6ProtocolGuid, + (VOID **) &Dns6, + Private->Ip6Nic->ImageHandle, + Private->Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Configure DNS6 instance for the DNS server address and protocol. + // + ZeroMem (&Dns6ConfigData, sizeof (EFI_DNS6_CONFIG_DATA)); + Dns6ConfigData.DnsServerCount = (UINT32)DnsServerListCount; + Dns6ConfigData.DnsServerList = DnsServerList; + Dns6ConfigData.EnableDnsCache = TRUE; + Dns6ConfigData.Protocol = EFI_IP_PROTO_UDP; + IP6_COPY_ADDRESS (&Dns6ConfigData.StationIp,&Private->StationIp.v6); + Status = Dns6->Configure ( + Dns6, + &Dns6ConfigData + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Token.Status = EFI_NOT_READY; + IsDone = FALSE; + // + // Create event to set the IsDone flag when name resolution is finished. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpBootCommonNotify, + &IsDone, + &Token.Event + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Start asynchronous name resolution. + // + Status = Dns6->HostNameToIp (Dns6, HostName, &Token); + if (EFI_ERROR (Status)) { + goto Exit; + } + + while (!IsDone) { + Dns6->Poll (Dns6); + } + + // + // Name resolution is done, check result. + // + Status = Token.Status; + if (!EFI_ERROR (Status)) { + if (Token.RspData.H2AData == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + if (Token.RspData.H2AData->IpCount == 0 || Token.RspData.H2AData->IpList == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + // + // We just return the first IPv6 address from DNS protocol. + // + IP6_COPY_ADDRESS (IpAddress, Token.RspData.H2AData->IpList); + Status = EFI_SUCCESS; + } +Exit: + + if (Token.Event != NULL) { + gBS->CloseEvent (Token.Event); + } + if (Token.RspData.H2AData != NULL) { + if (Token.RspData.H2AData->IpList != NULL) { + FreePool (Token.RspData.H2AData->IpList); + } + FreePool (Token.RspData.H2AData); + } + + if (Dns6 != NULL) { + Dns6->Configure (Dns6, NULL); + + gBS->CloseProtocol ( + Dns6Handle, + &gEfiDns6ProtocolGuid, + Private->Ip6Nic->ImageHandle, + Private->Controller + ); + } + + if (Dns6Handle != NULL) { + NetLibDestroyServiceChild ( + Private->Controller, + Private->Ip6Nic->ImageHandle, + &gEfiDns6ServiceBindingProtocolGuid, + Dns6Handle + ); + } + + if (DnsServerList != NULL) { + FreePool (DnsServerList); + } + + return Status; +} +/** + Create a HTTP_IO_HEADER to hold the HTTP header items. + + @param[in] MaxHeaderCount The maximun number of HTTP header in this holder. + + @return A pointer of the HTTP header holder or NULL if failed. + +**/ +HTTP_IO_HEADER * +HttpBootCreateHeader ( + UINTN MaxHeaderCount + ) +{ + HTTP_IO_HEADER *HttpIoHeader; + + if (MaxHeaderCount == 0) { + return NULL; + } + + HttpIoHeader = AllocateZeroPool (sizeof (HTTP_IO_HEADER) + MaxHeaderCount * sizeof (EFI_HTTP_HEADER)); + if (HttpIoHeader == NULL) { + return NULL; + } + + HttpIoHeader->MaxHeaderCount = MaxHeaderCount; + HttpIoHeader->Headers = (EFI_HTTP_HEADER *) (HttpIoHeader + 1); + + return HttpIoHeader; +} + +/** + Destroy the HTTP_IO_HEADER and release the resouces. + + @param[in] HttpIoHeader Point to the HTTP header holder to be destroyed. + +**/ +VOID +HttpBootFreeHeader ( + IN HTTP_IO_HEADER *HttpIoHeader + ) +{ + UINTN Index; + + if (HttpIoHeader != NULL) { + if (HttpIoHeader->HeaderCount != 0) { + for (Index = 0; Index < HttpIoHeader->HeaderCount; Index++) { + FreePool (HttpIoHeader->Headers[Index].FieldName); + FreePool (HttpIoHeader->Headers[Index].FieldValue); + } + } + FreePool (HttpIoHeader); + } +} + +/** + Set or update a HTTP header with the field name and corresponding value. + + @param[in] HttpIoHeader Point to the HTTP header holder. + @param[in] FieldName Null terminated string which describes a field name. + @param[in] FieldValue Null terminated string which describes the corresponding field value. + + @retval EFI_SUCCESS The HTTP header has been set or updated. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Insufficient resource to complete the operation. + @retval Other Unexpected error happened. + +**/ +EFI_STATUS +HttpBootSetHeader ( + IN HTTP_IO_HEADER *HttpIoHeader, + IN CHAR8 *FieldName, + IN CHAR8 *FieldValue + ) +{ + EFI_HTTP_HEADER *Header; + UINTN StrSize; + CHAR8 *NewFieldValue; + + if (HttpIoHeader == NULL || FieldName == NULL || FieldValue == NULL) { + return EFI_INVALID_PARAMETER; + } + + Header = HttpFindHeader (HttpIoHeader->HeaderCount, HttpIoHeader->Headers, FieldName); + if (Header == NULL) { + // + // Add a new header. + // + if (HttpIoHeader->HeaderCount >= HttpIoHeader->MaxHeaderCount) { + return EFI_OUT_OF_RESOURCES; + } + Header = &HttpIoHeader->Headers[HttpIoHeader->HeaderCount]; + + StrSize = AsciiStrSize (FieldName); + Header->FieldName = AllocatePool (StrSize); + if (Header->FieldName == NULL) { + return EFI_OUT_OF_RESOURCES; + } + CopyMem (Header->FieldName, FieldName, StrSize); + Header->FieldName[StrSize -1] = '\0'; + + StrSize = AsciiStrSize (FieldValue); + Header->FieldValue = AllocatePool (StrSize); + if (Header->FieldValue == NULL) { + FreePool (Header->FieldName); + return EFI_OUT_OF_RESOURCES; + } + CopyMem (Header->FieldValue, FieldValue, StrSize); + Header->FieldValue[StrSize -1] = '\0'; + + HttpIoHeader->HeaderCount++; + } else { + // + // Update an existing one. + // + StrSize = AsciiStrSize (FieldValue); + NewFieldValue = AllocatePool (StrSize); + if (NewFieldValue == NULL) { + return EFI_OUT_OF_RESOURCES; + } + CopyMem (NewFieldValue, FieldValue, StrSize); + NewFieldValue[StrSize -1] = '\0'; + + if (Header->FieldValue != NULL) { + FreePool (Header->FieldValue); + } + Header->FieldValue = NewFieldValue; + } + + return EFI_SUCCESS; +} + +/** + Notify the callback function when an event is triggered. + + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +HttpIoNotifyDpc ( + IN VOID *Context + ) +{ + *((BOOLEAN *) Context) = TRUE; +} + +/** + Request HttpIoNotifyDpc as a DPC at TPL_CALLBACK. + + @param[in] Event The event signaled. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +HttpIoNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request HttpIoNotifyDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, HttpIoNotifyDpc, Context); +} + +/** + Create a HTTP_IO to access the HTTP service. It will create and configure + a HTTP child handle. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + @param[in] ConfigData The HTTP_IO configuration data. + @param[in] Callback Callback function which will be invoked when specified + HTTP_IO_CALLBACK_EVENT happened. + @param[in] Context The Context data which will be passed to the Callback function. + @param[out] HttpIo The HTTP_IO. + + @retval EFI_SUCCESS The HTTP_IO is created and configured. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Others Failed to create the HTTP_IO or configure it. + +**/ +EFI_STATUS +HttpIoCreateIo ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN UINT8 IpVersion, + IN HTTP_IO_CONFIG_DATA *ConfigData, + IN HTTP_IO_CALLBACK Callback, + IN VOID *Context, + OUT HTTP_IO *HttpIo + ) +{ + EFI_STATUS Status; + EFI_HTTP_CONFIG_DATA HttpConfigData; + EFI_HTTPv4_ACCESS_POINT Http4AccessPoint; + EFI_HTTPv6_ACCESS_POINT Http6AccessPoint; + EFI_HTTP_PROTOCOL *Http; + EFI_EVENT Event; + + if ((Image == NULL) || (Controller == NULL) || (ConfigData == NULL) || (HttpIo == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (IpVersion != IP_VERSION_4 && IpVersion != IP_VERSION_6) { + return EFI_UNSUPPORTED; + } + + ZeroMem (HttpIo, sizeof (HTTP_IO)); + + // + // Create the HTTP child instance and get the HTTP protocol. + // + Status = NetLibCreateServiceChild ( + Controller, + Image, + &gEfiHttpServiceBindingProtocolGuid, + &HttpIo->Handle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + HttpIo->Handle, + &gEfiHttpProtocolGuid, + (VOID **) &Http, + Image, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status) || (Http == NULL)) { + goto ON_ERROR; + } + + // + // Init the configuration data and configure the HTTP child. + // + HttpIo->Image = Image; + HttpIo->Controller = Controller; + HttpIo->IpVersion = IpVersion; + HttpIo->Http = Http; + HttpIo->Callback = Callback; + HttpIo->Context = Context; + + ZeroMem (&HttpConfigData, sizeof (EFI_HTTP_CONFIG_DATA)); + HttpConfigData.HttpVersion = HttpVersion11; + HttpConfigData.TimeOutMillisec = ConfigData->Config4.RequestTimeOut; + if (HttpIo->IpVersion == IP_VERSION_4) { + HttpConfigData.LocalAddressIsIPv6 = FALSE; + + Http4AccessPoint.UseDefaultAddress = ConfigData->Config4.UseDefaultAddress; + Http4AccessPoint.LocalPort = ConfigData->Config4.LocalPort; + IP4_COPY_ADDRESS (&Http4AccessPoint.LocalAddress, &ConfigData->Config4.LocalIp); + IP4_COPY_ADDRESS (&Http4AccessPoint.LocalSubnet, &ConfigData->Config4.SubnetMask); + HttpConfigData.AccessPoint.IPv4Node = &Http4AccessPoint; + } else { + HttpConfigData.LocalAddressIsIPv6 = TRUE; + Http6AccessPoint.LocalPort = ConfigData->Config6.LocalPort; + IP6_COPY_ADDRESS (&Http6AccessPoint.LocalAddress, &ConfigData->Config6.LocalIp); + HttpConfigData.AccessPoint.IPv6Node = &Http6AccessPoint; + } + + Status = Http->Configure (Http, &HttpConfigData); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create events for variuos asynchronous operations. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpIoNotify, + &HttpIo->IsTxDone, + &Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + HttpIo->ReqToken.Event = Event; + HttpIo->ReqToken.Message = &HttpIo->ReqMessage; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpIoNotify, + &HttpIo->IsRxDone, + &Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + HttpIo->RspToken.Event = Event; + HttpIo->RspToken.Message = &HttpIo->RspMessage; + + // + // Create TimeoutEvent for response + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + HttpIo->TimeoutEvent = Event; + + return EFI_SUCCESS; + +ON_ERROR: + HttpIoDestroyIo (HttpIo); + + return Status; +} + +/** + Destroy the HTTP_IO and release the resouces. + + @param[in] HttpIo The HTTP_IO which wraps the HTTP service to be destroyed. + +**/ +VOID +HttpIoDestroyIo ( + IN HTTP_IO *HttpIo + ) +{ + EFI_HTTP_PROTOCOL *Http; + EFI_EVENT Event; + + if (HttpIo == NULL) { + return; + } + + Event = HttpIo->ReqToken.Event; + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + Event = HttpIo->RspToken.Event; + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + Event = HttpIo->TimeoutEvent; + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + Http = HttpIo->Http; + if (Http != NULL) { + Http->Configure (Http, NULL); + gBS->CloseProtocol ( + HttpIo->Handle, + &gEfiHttpProtocolGuid, + HttpIo->Image, + HttpIo->Controller + ); + } + + NetLibDestroyServiceChild ( + HttpIo->Controller, + HttpIo->Image, + &gEfiHttpServiceBindingProtocolGuid, + HttpIo->Handle + ); +} + +/** + Synchronously send a HTTP REQUEST message to the server. + + @param[in] HttpIo The HttpIo wrapping the HTTP service. + @param[in] Request A pointer to storage such data as URL and HTTP method. + @param[in] HeaderCount Number of HTTP header structures in Headers list. + @param[in] Headers Array containing list of HTTP headers. + @param[in] BodyLength Length in bytes of the HTTP body. + @param[in] Body Body associated with the HTTP request. + + @retval EFI_SUCCESS The HTTP request is trasmitted. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpIoSendRequest ( + IN HTTP_IO *HttpIo, + IN EFI_HTTP_REQUEST_DATA *Request, + IN UINTN HeaderCount, + IN EFI_HTTP_HEADER *Headers, + IN UINTN BodyLength, + IN VOID *Body + ) +{ + EFI_STATUS Status; + EFI_HTTP_PROTOCOL *Http; + + if (HttpIo == NULL || HttpIo->Http == NULL) { + return EFI_INVALID_PARAMETER; + } + + HttpIo->ReqToken.Status = EFI_NOT_READY; + HttpIo->ReqToken.Message->Data.Request = Request; + HttpIo->ReqToken.Message->HeaderCount = HeaderCount; + HttpIo->ReqToken.Message->Headers = Headers; + HttpIo->ReqToken.Message->BodyLength = BodyLength; + HttpIo->ReqToken.Message->Body = Body; + + if (HttpIo->Callback != NULL) { + Status = HttpIo->Callback ( + HttpIoRequest, + HttpIo->ReqToken.Message, + HttpIo->Context + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Queue the request token to HTTP instances. + // + Http = HttpIo->Http; + HttpIo->IsTxDone = FALSE; + Status = Http->Request ( + Http, + &HttpIo->ReqToken + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Poll the network until transmit finish. + // + while (!HttpIo->IsTxDone) { + Http->Poll (Http); + } + + return HttpIo->ReqToken.Status; +} + +/** + Synchronously receive a HTTP RESPONSE message from the server. + + @param[in] HttpIo The HttpIo wrapping the HTTP service. + @param[in] RecvMsgHeader TRUE to receive a new HTTP response (from message header). + FALSE to continue receive the previous response message. + @param[out] ResponseData Point to a wrapper of the received response data. + + @retval EFI_SUCCESS The HTTP response is received. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpIoRecvResponse ( + IN HTTP_IO *HttpIo, + IN BOOLEAN RecvMsgHeader, + OUT HTTP_IO_RESPONSE_DATA *ResponseData + ) +{ + EFI_STATUS Status; + EFI_HTTP_PROTOCOL *Http; + + if (HttpIo == NULL || HttpIo->Http == NULL || ResponseData == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Start the timer, and wait Timeout seconds to receive the header packet. + // + Status = gBS->SetTimer (HttpIo->TimeoutEvent, TimerRelative, HTTP_BOOT_RESPONSE_TIMEOUT * TICKS_PER_MS); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Queue the response token to HTTP instances. + // + HttpIo->RspToken.Status = EFI_NOT_READY; + if (RecvMsgHeader) { + HttpIo->RspToken.Message->Data.Response = &ResponseData->Response; + } else { + HttpIo->RspToken.Message->Data.Response = NULL; + } + HttpIo->RspToken.Message->HeaderCount = 0; + HttpIo->RspToken.Message->Headers = NULL; + HttpIo->RspToken.Message->BodyLength = ResponseData->BodyLength; + HttpIo->RspToken.Message->Body = ResponseData->Body; + + Http = HttpIo->Http; + HttpIo->IsRxDone = FALSE; + Status = Http->Response ( + Http, + &HttpIo->RspToken + ); + + if (EFI_ERROR (Status)) { + gBS->SetTimer (HttpIo->TimeoutEvent, TimerCancel, 0); + return Status; + } + + // + // Poll the network until receive finish. + // + while (!HttpIo->IsRxDone && ((HttpIo->TimeoutEvent == NULL) || EFI_ERROR (gBS->CheckEvent (HttpIo->TimeoutEvent)))) { + Http->Poll (Http); + } + + gBS->SetTimer (HttpIo->TimeoutEvent, TimerCancel, 0); + + if (!HttpIo->IsRxDone) { + // + // Timeout occurs, cancel the response token. + // + Http->Cancel (Http, &HttpIo->RspToken); + + Status = EFI_TIMEOUT; + + return Status; + } else { + HttpIo->IsRxDone = FALSE; + } + + if ((HttpIo->Callback != NULL) && + (HttpIo->RspToken.Status == EFI_SUCCESS || HttpIo->RspToken.Status == EFI_HTTP_ERROR)) { + Status = HttpIo->Callback ( + HttpIoResponse, + HttpIo->RspToken.Message, + HttpIo->Context + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Store the received data into the wrapper. + // + ResponseData->Status = HttpIo->RspToken.Status; + ResponseData->HeaderCount = HttpIo->RspToken.Message->HeaderCount; + ResponseData->Headers = HttpIo->RspToken.Message->Headers; + ResponseData->BodyLength = HttpIo->RspToken.Message->BodyLength; + + return Status; +} + +/** + This function checks the HTTP(S) URI scheme. + + @param[in] Uri The pointer to the URI string. + + @retval EFI_SUCCESS The URI scheme is valid. + @retval EFI_INVALID_PARAMETER The URI scheme is not HTTP or HTTPS. + @retval EFI_ACCESS_DENIED HTTP is disabled and the URI is HTTP. + +**/ +EFI_STATUS +HttpBootCheckUriScheme ( + IN CHAR8 *Uri + ) +{ + UINTN Index; + EFI_STATUS Status; + + Status = EFI_SUCCESS; + + // + // Convert the scheme to all lower case. + // + for (Index = 0; Index < AsciiStrLen (Uri); Index++) { + if (Uri[Index] == ':') { + break; + } + if (Uri[Index] >= 'A' && Uri[Index] <= 'Z') { + Uri[Index] -= (CHAR8)('A' - 'a'); + } + } + + // + // Return EFI_INVALID_PARAMETER if the URI is not HTTP or HTTPS. + // + if ((AsciiStrnCmp (Uri, "http://", 7) != 0) && (AsciiStrnCmp (Uri, "https://", 8) != 0)) { + DEBUG ((EFI_D_ERROR, "HttpBootCheckUriScheme: Invalid Uri.\n")); + return EFI_INVALID_PARAMETER; + } + + // + // HTTP is disabled, return EFI_ACCESS_DENIED if the URI is HTTP. + // + if (!PcdGetBool (PcdAllowHttpConnections) && (AsciiStrnCmp (Uri, "http://", 7) == 0)) { + DEBUG ((EFI_D_ERROR, "HttpBootCheckUriScheme: HTTP is disabled.\n")); + return EFI_ACCESS_DENIED; + } + + return Status; +} + +/** + Get the URI address string from the input device path. + + Caller need to free the buffer in the UriAddress pointer. + + @param[in] FilePath Pointer to the device path which contains a URI device path node. + @param[out] UriAddress The URI address string extract from the device path. + + @retval EFI_SUCCESS The URI string is returned. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +HttpBootParseFilePath ( + IN EFI_DEVICE_PATH_PROTOCOL *FilePath, + OUT CHAR8 **UriAddress + ) +{ + EFI_DEVICE_PATH_PROTOCOL *TempDevicePath; + URI_DEVICE_PATH *UriDevicePath; + CHAR8 *Uri; + UINTN UriStrLength; + + if (FilePath == NULL) { + return EFI_INVALID_PARAMETER; + } + + *UriAddress = NULL; + + // + // Extract the URI address from the FilePath + // + TempDevicePath = FilePath; + while (!IsDevicePathEnd (TempDevicePath)) { + if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) && + (DevicePathSubType (TempDevicePath) == MSG_URI_DP)) { + UriDevicePath = (URI_DEVICE_PATH*) TempDevicePath; + // + // UEFI Spec doesn't require the URI to be a NULL-terminated string + // So we allocate a new buffer and always append a '\0' to it. + // + UriStrLength = DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL); + if (UriStrLength == 0) { + // + // return a NULL UriAddress if it's a empty URI device path node. + // + break; + } + Uri = AllocatePool (UriStrLength + 1); + if (Uri == NULL) { + return EFI_OUT_OF_RESOURCES; + } + CopyMem (Uri, UriDevicePath->Uri, DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL)); + Uri[DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL)] = '\0'; + + *UriAddress = Uri; + } + TempDevicePath = NextDevicePathNode (TempDevicePath); + } + + return EFI_SUCCESS; +} + +/** + This function returns the image type according to server replied HTTP message + and also the image's URI info. + + @param[in] Uri The pointer to the image's URI string. + @param[in] UriParser URI Parse result returned by NetHttpParseUrl(). + @param[in] HeaderCount Number of HTTP header structures in Headers list. + @param[in] Headers Array containing list of HTTP headers. + @param[out] ImageType The image type of the downloaded file. + + @retval EFI_SUCCESS The image type is returned in ImageType. + @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL. + @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL. + @retval EFI_NOT_FOUND Failed to identify the image type. + @retval Others Unexpect error happened. + +**/ +EFI_STATUS +HttpBootCheckImageType ( + IN CHAR8 *Uri, + IN VOID *UriParser, + IN UINTN HeaderCount, + IN EFI_HTTP_HEADER *Headers, + OUT HTTP_BOOT_IMAGE_TYPE *ImageType + ) +{ + EFI_STATUS Status; + EFI_HTTP_HEADER *Header; + CHAR8 *FilePath; + CHAR8 *FilePost; + + if (Uri == NULL || UriParser == NULL || ImageType == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (HeaderCount != 0 && Headers == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Determine the image type by the HTTP Content-Type header field first. + // "application/efi" -> EFI Image + // "application/vnd.efi-iso" -> CD/DVD Image + // "application/vnd.efi-img" -> Virtual Disk Image + // + Header = HttpFindHeader (HeaderCount, Headers, HTTP_HEADER_CONTENT_TYPE); + if (Header != NULL) { + if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_EFI) == 0) { + *ImageType = ImageTypeEfi; + return EFI_SUCCESS; + } else if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_ISO) == 0) { + *ImageType = ImageTypeVirtualCd; + return EFI_SUCCESS; + } else if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_IMG) == 0) { + *ImageType = ImageTypeVirtualDisk; + return EFI_SUCCESS; + } + } + + // + // Determine the image type by file extension: + // *.efi -> EFI Image + // *.iso -> CD/DVD Image + // *.img -> Virtual Disk Image + // + Status = HttpUrlGetPath ( + Uri, + UriParser, + &FilePath + ); + if (EFI_ERROR (Status)) { + return Status; + } + + FilePost = FilePath + AsciiStrLen (FilePath) - 4; + if (AsciiStrCmp (FilePost, ".efi") == 0) { + *ImageType = ImageTypeEfi; + } else if (AsciiStrCmp (FilePost, ".iso") == 0) { + *ImageType = ImageTypeVirtualCd; + } else if (AsciiStrCmp (FilePost, ".img") == 0) { + *ImageType = ImageTypeVirtualDisk; + } else { + *ImageType = ImageTypeMax; + } + + FreePool (FilePath); + + return (*ImageType < ImageTypeMax) ? EFI_SUCCESS : EFI_NOT_FOUND; +} + +/** + This function register the RAM disk info to the system. + + @param[in] Private The pointer to the driver's private data. + @param[in] BufferSize The size of Buffer in bytes. + @param[in] Buffer The base address of the RAM disk. + @param[in] ImageType The image type of the file in Buffer. + + @retval EFI_SUCCESS The RAM disk has been registered. + @retval EFI_NOT_FOUND No RAM disk protocol instances were found. + @retval EFI_UNSUPPORTED The ImageType is not supported. + @retval Others Unexpected error happened. + +**/ +EFI_STATUS +HttpBootRegisterRamDisk ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN UINTN BufferSize, + IN VOID *Buffer, + IN HTTP_BOOT_IMAGE_TYPE ImageType + ) +{ + EFI_RAM_DISK_PROTOCOL *RamDisk; + EFI_STATUS Status; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_GUID *RamDiskType; + + ASSERT (Private != NULL); + ASSERT (Buffer != NULL); + ASSERT (BufferSize != 0); + + Status = gBS->LocateProtocol (&gEfiRamDiskProtocolGuid, NULL, (VOID**) &RamDisk); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "HTTP Boot: Couldn't find the RAM Disk protocol - %r\n", Status)); + return Status; + } + + if (ImageType == ImageTypeVirtualCd) { + RamDiskType = &gEfiVirtualCdGuid; + } else if (ImageType == ImageTypeVirtualDisk) { + RamDiskType = &gEfiVirtualDiskGuid; + } else { + return EFI_UNSUPPORTED; + } + + Status = RamDisk->Register ( + (UINTN)Buffer, + (UINT64)BufferSize, + RamDiskType, + Private->UsingIpv6 ? Private->Ip6Nic->DevicePath : Private->Ip4Nic->DevicePath, + &DevicePath + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "HTTP Boot: Failed to register RAM Disk - %r\n", Status)); + } + + return Status; +} + +/** + Indicate if the HTTP status code indicates a redirection. + + @param[in] StatusCode HTTP status code from server. + + @return TRUE if it's redirection. + +**/ +BOOLEAN +HttpBootIsHttpRedirectStatusCode ( + IN EFI_HTTP_STATUS_CODE StatusCode + ) +{ + if (StatusCode == HTTP_STATUS_301_MOVED_PERMANENTLY || + StatusCode == HTTP_STATUS_302_FOUND || + StatusCode == HTTP_STATUS_307_TEMPORARY_REDIRECT || + StatusCode == HTTP_STATUS_308_PERMANENT_REDIRECT) { + return TRUE; + } + + return FALSE; +} diff --git a/NetworkPkg/HttpBootDxe/HttpBootSupport.h b/NetworkPkg/HttpBootDxe/HttpBootSupport.h new file mode 100644 index 000000000..10f62f6ec --- /dev/null +++ b/NetworkPkg/HttpBootDxe/HttpBootSupport.h @@ -0,0 +1,455 @@ +/** @file + Support functions declaration for UEFI HTTP boot driver. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_HTTP_BOOT_SUPPORT_H__ +#define __EFI_HTTP_BOOT_SUPPORT_H__ + +/** + Get the Nic handle using any child handle in the IPv4 stack. + + @param[in] ControllerHandle Pointer to child handle over IPv4. + + @return NicHandle The pointer to the Nic handle. + @return NULL Can't find the Nic handle. + +**/ +EFI_HANDLE +HttpBootGetNicByIp4Children ( + IN EFI_HANDLE ControllerHandle + ); + +/** + Get the Nic handle using any child handle in the IPv6 stack. + + @param[in] ControllerHandle Pointer to child handle over IPv6. + + @return NicHandle The pointer to the Nic handle. + @return NULL Can't find the Nic handle. + +**/ +EFI_HANDLE +HttpBootGetNicByIp6Children ( + IN EFI_HANDLE ControllerHandle + ); + +/** + This function is to convert UINTN to ASCII string with the required formatting. + + @param[in] Number Numeric value to be converted. + @param[in] Buffer The pointer to the buffer for ASCII string. + @param[in] Length The length of the required format. + +**/ +VOID +HttpBootUintnToAscDecWithFormat ( + IN UINTN Number, + IN UINT8 *Buffer, + IN INTN Length + ); + + +/** + This function is to display the IPv4 address. + + @param[in] Ip The pointer to the IPv4 address. + +**/ +VOID +HttpBootShowIp4Addr ( + IN EFI_IPv4_ADDRESS *Ip + ); + +/** + This function is to display the IPv6 address. + + @param[in] Ip The pointer to the IPv6 address. + +**/ +VOID +HttpBootShowIp6Addr ( + IN EFI_IPv6_ADDRESS *Ip + ); + +/** + This function is to display the HTTP error status. + + @param[in] StatusCode The status code value in HTTP message. + +**/ +VOID +HttpBootPrintErrorMessage ( + EFI_HTTP_STATUS_CODE StatusCode + ); + +// +// A wrapper structure to hold the HTTP headers. +// +typedef struct { + UINTN MaxHeaderCount; + UINTN HeaderCount; + EFI_HTTP_HEADER *Headers; +} HTTP_IO_HEADER; + +/** + Create a HTTP_IO_HEADER to hold the HTTP header items. + + @param[in] MaxHeaderCount The maximun number of HTTP header in this holder. + + @return A pointer of the HTTP header holder or NULL if failed. + +**/ +HTTP_IO_HEADER * +HttpBootCreateHeader ( + IN UINTN MaxHeaderCount + ); + +/** + Destroy the HTTP_IO_HEADER and release the resouces. + + @param[in] HttpIoHeader Point to the HTTP header holder to be destroyed. + +**/ +VOID +HttpBootFreeHeader ( + IN HTTP_IO_HEADER *HttpIoHeader + ); + +/** + Set or update a HTTP header with the field name and corresponding value. + + @param[in] HttpIoHeader Point to the HTTP header holder. + @param[in] FieldName Null terminated string which describes a field name. + @param[in] FieldValue Null terminated string which describes the corresponding field value. + + @retval EFI_SUCCESS The HTTP header has been set or updated. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Insufficient resource to complete the operation. + @retval Other Unexpected error happened. + +**/ +EFI_STATUS +HttpBootSetHeader ( + IN HTTP_IO_HEADER *HttpIoHeader, + IN CHAR8 *FieldName, + IN CHAR8 *FieldValue + ); + +/// +/// HTTP_IO_CALLBACK_EVENT +/// +typedef enum { + HttpIoRequest, + HttpIoResponse +} HTTP_IO_CALLBACK_EVENT; + +/** + HttpIo Callback function which will be invoked when specified HTTP_IO_CALLBACK_EVENT happened. + + @param[in] EventType Indicate the Event type that occurs in the current callback. + @param[in] Message HTTP message which will be send to, or just received from HTTP server. + @param[in] Context The Callback Context pointer. + + @retval EFI_SUCCESS Tells the HttpIo to continue the HTTP process. + @retval Others Tells the HttpIo to abort the current HTTP process. +**/ +typedef +EFI_STATUS +(EFIAPI * HTTP_IO_CALLBACK) ( + IN HTTP_IO_CALLBACK_EVENT EventType, + IN EFI_HTTP_MESSAGE *Message, + IN VOID *Context + ); + +// +// HTTP_IO configuration data for IPv4 +// +typedef struct { + EFI_HTTP_VERSION HttpVersion; + UINT32 RequestTimeOut; // In milliseconds. + UINT32 ResponseTimeOut; // In milliseconds. + BOOLEAN UseDefaultAddress; + EFI_IPv4_ADDRESS LocalIp; + EFI_IPv4_ADDRESS SubnetMask; + UINT16 LocalPort; +} HTTP4_IO_CONFIG_DATA; + +// +// HTTP_IO configuration data for IPv6 +// +typedef struct { + EFI_HTTP_VERSION HttpVersion; + UINT32 RequestTimeOut; // In milliseconds. + BOOLEAN UseDefaultAddress; + EFI_IPv6_ADDRESS LocalIp; + UINT16 LocalPort; +} HTTP6_IO_CONFIG_DATA; + + +// +// HTTP_IO configuration +// +typedef union { + HTTP4_IO_CONFIG_DATA Config4; + HTTP6_IO_CONFIG_DATA Config6; +} HTTP_IO_CONFIG_DATA; + +// +// HTTP_IO wrapper of the EFI HTTP service. +// +typedef struct { + UINT8 IpVersion; + EFI_HANDLE Image; + EFI_HANDLE Controller; + EFI_HANDLE Handle; + + EFI_HTTP_PROTOCOL *Http; + + HTTP_IO_CALLBACK Callback; + VOID *Context; + + EFI_HTTP_TOKEN ReqToken; + EFI_HTTP_MESSAGE ReqMessage; + EFI_HTTP_TOKEN RspToken; + EFI_HTTP_MESSAGE RspMessage; + + BOOLEAN IsTxDone; + BOOLEAN IsRxDone; + + EFI_EVENT TimeoutEvent; +} HTTP_IO; + +// +// A wrapper structure to hold the received HTTP response data. +// +typedef struct { + EFI_HTTP_RESPONSE_DATA Response; + UINTN HeaderCount; + EFI_HTTP_HEADER *Headers; + UINTN BodyLength; + CHAR8 *Body; + EFI_STATUS Status; +} HTTP_IO_RESPONSE_DATA; + +/** + Retrieve the host address using the EFI_DNS6_PROTOCOL. + + @param[in] Private The pointer to the driver's private data. + @param[in] HostName Pointer to buffer containing hostname. + @param[out] IpAddress On output, pointer to buffer containing IPv6 address. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. +**/ +EFI_STATUS +HttpBootDns ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN CHAR16 *HostName, + OUT EFI_IPv6_ADDRESS *IpAddress + ); + +/** + Notify the callback function when an event is triggered. + + @param[in] Event The triggered event. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +HttpBootCommonNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Create a HTTP_IO to access the HTTP service. It will create and configure + a HTTP child handle. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + @param[in] ConfigData The HTTP_IO configuration data. + @param[in] Callback Callback function which will be invoked when specified + HTTP_IO_CALLBACK_EVENT happened. + @param[in] Context The Context data which will be passed to the Callback function. + @param[out] HttpIo The HTTP_IO. + + @retval EFI_SUCCESS The HTTP_IO is created and configured. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Others Failed to create the HTTP_IO or configure it. + +**/ +EFI_STATUS +HttpIoCreateIo ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN UINT8 IpVersion, + IN HTTP_IO_CONFIG_DATA *ConfigData, + IN HTTP_IO_CALLBACK Callback, + IN VOID *Context, + OUT HTTP_IO *HttpIo + ); + +/** + Destroy the HTTP_IO and release the resouces. + + @param[in] HttpIo The HTTP_IO which wraps the HTTP service to be destroyed. + +**/ +VOID +HttpIoDestroyIo ( + IN HTTP_IO *HttpIo + ); + +/** + Synchronously send a HTTP REQUEST message to the server. + + @param[in] HttpIo The HttpIo wrapping the HTTP service. + @param[in] Request A pointer to storage such data as URL and HTTP method. + @param[in] HeaderCount Number of HTTP header structures in Headers list. + @param[in] Headers Array containing list of HTTP headers. + @param[in] BodyLength Length in bytes of the HTTP body. + @param[in] Body Body associated with the HTTP request. + + @retval EFI_SUCCESS The HTTP request is trasmitted. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpIoSendRequest ( + IN HTTP_IO *HttpIo, + IN EFI_HTTP_REQUEST_DATA *Request, OPTIONAL + IN UINTN HeaderCount, + IN EFI_HTTP_HEADER *Headers, OPTIONAL + IN UINTN BodyLength, + IN VOID *Body OPTIONAL + ); + +/** + Synchronously receive a HTTP RESPONSE message from the server. + + @param[in] HttpIo The HttpIo wrapping the HTTP service. + @param[in] RecvMsgHeader TRUE to receive a new HTTP response (from message header). + FALSE to continue receive the previous response message. + @param[out] ResponseData Point to a wrapper of the received response data. + + @retval EFI_SUCCESS The HTTP response is received. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpIoRecvResponse ( + IN HTTP_IO *HttpIo, + IN BOOLEAN RecvMsgHeader, + OUT HTTP_IO_RESPONSE_DATA *ResponseData + ); + +/** + This function checks the HTTP(S) URI scheme. + + @param[in] Uri The pointer to the URI string. + + @retval EFI_SUCCESS The URI scheme is valid. + @retval EFI_INVALID_PARAMETER The URI scheme is not HTTP or HTTPS. + @retval EFI_ACCESS_DENIED HTTP is disabled and the URI is HTTP. + +**/ +EFI_STATUS +HttpBootCheckUriScheme ( + IN CHAR8 *Uri + ); + +/** + Get the URI address string from the input device path. + + Caller need to free the buffer in the UriAddress pointer. + + @param[in] FilePath Pointer to the device path which contains a URI device path node. + @param[out] UriAddress The URI address string extract from the device path. + + @retval EFI_SUCCESS The URI string is returned. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +HttpBootParseFilePath ( + IN EFI_DEVICE_PATH_PROTOCOL *FilePath, + OUT CHAR8 **UriAddress + ); + +/** + This function returns the image type according to server replied HTTP message + and also the image's URI info. + + @param[in] Uri The pointer to the image's URI string. + @param[in] UriParser URI Parse result returned by NetHttpParseUrl(). + @param[in] HeaderCount Number of HTTP header structures in Headers list. + @param[in] Headers Array containing list of HTTP headers. + @param[out] ImageType The image type of the downloaded file. + + @retval EFI_SUCCESS The image type is returned in ImageType. + @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL. + @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL. + @retval EFI_NOT_FOUND Failed to identify the image type. + @retval Others Unexpect error happened. + +**/ +EFI_STATUS +HttpBootCheckImageType ( + IN CHAR8 *Uri, + IN VOID *UriParser, + IN UINTN HeaderCount, + IN EFI_HTTP_HEADER *Headers, + OUT HTTP_BOOT_IMAGE_TYPE *ImageType + ); + +/** + This function register the RAM disk info to the system. + + @param[in] Private The pointer to the driver's private data. + @param[in] BufferSize The size of Buffer in bytes. + @param[in] Buffer The base address of the RAM disk. + @param[in] ImageType The image type of the file in Buffer. + + @retval EFI_SUCCESS The RAM disk has been registered. + @retval EFI_NOT_FOUND No RAM disk protocol instances were found. + @retval EFI_UNSUPPORTED The ImageType is not supported. + @retval Others Unexpected error happened. + +**/ +EFI_STATUS +HttpBootRegisterRamDisk ( + IN HTTP_BOOT_PRIVATE_DATA *Private, + IN UINTN BufferSize, + IN VOID *Buffer, + IN HTTP_BOOT_IMAGE_TYPE ImageType + ); + +/** + Indicate if the HTTP status code indicates a redirection. + + @param[in] StatusCode HTTP status code from server. + + @return TRUE if it's redirection. + +**/ +BOOLEAN +HttpBootIsHttpRedirectStatusCode ( + IN EFI_HTTP_STATUS_CODE StatusCode + ); +#endif diff --git a/NetworkPkg/HttpDxe/ComponentName.c b/NetworkPkg/HttpDxe/ComponentName.c new file mode 100644 index 000000000..d51b55ae2 --- /dev/null +++ b/NetworkPkg/HttpDxe/ComponentName.c @@ -0,0 +1,132 @@ +/** @file + Implementation of EFI_COMPONENT_NAME_PROTOCOL and + EFI_COMPONENT_NAME2_PROTOCOL protocol. + + Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpDriver.h" + +/// +/// Component Name Protocol instance +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_COMPONENT_NAME_PROTOCOL gHttpDxeComponentName = { + (EFI_COMPONENT_NAME_GET_DRIVER_NAME) HttpDxeComponentNameGetDriverName, + (EFI_COMPONENT_NAME_GET_CONTROLLER_NAME) HttpDxeComponentNameGetControllerName, + "eng" +}; + +/// +/// Component Name 2 Protocol instance +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_COMPONENT_NAME2_PROTOCOL gHttpDxeComponentName2 = { + HttpDxeComponentNameGetDriverName, + HttpDxeComponentNameGetControllerName, + "en" +}; + +/// +/// Table of driver names +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_UNICODE_STRING_TABLE mHttpDxeDriverNameTable[] = { + { "eng;en", (CHAR16 *) L"HttpDxe" }, + { NULL, NULL } +}; + +/** + Retrieves a Unicode string that is the user-readable name of the EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param Language A pointer to a three-character ISO 639-2 language identifier. + This is the language of the driver name that that the caller + is requesting, and it must match one of the languages specified + in SupportedLanguages. The number of languages supported by a + driver is up to the driver writer. + @param DriverName A pointer to the Unicode string to return. This Unicode string + is the name of the driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by This + and the language specified by Language was returned + in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +HttpDxeComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mHttpDxeDriverNameTable, + DriverName, + (BOOLEAN)(This != &gHttpDxeComponentName2) + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by an EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param ControllerHandle The handle of a controller that the driver specified by + This is managing. This handle specifies the controller + whose name is to be returned. + @param ChildHandle The handle of the child controller to retrieve the name + of. This is an optional parameter that may be NULL. It + will be NULL for device drivers. It will also be NULL + for a bus drivers that wish to retrieve the name of the + bus controller. It will not be NULL for a bus driver + that wishes to retrieve the name of a child controller. + @param Language A pointer to a three character ISO 639-2 language + identifier. This is the language of the controller name + that the caller is requesting, and it must match one + of the languages specified in SupportedLanguages. The + number of languages supported by a driver is up to the + driver writer. + @param ControllerName A pointer to the Unicode string to return. This Unicode + string is the name of the controller specified by + ControllerHandle and ChildHandle in the language specified + by Language, from the point of view of the driver specified + by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in the + language specified by Language for the driver + specified by This was returned in DriverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not currently managing + the controller specified by ControllerHandle and + ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +HttpDxeComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + return EFI_UNSUPPORTED; +} diff --git a/NetworkPkg/HttpDxe/ComponentName.h b/NetworkPkg/HttpDxe/ComponentName.h new file mode 100644 index 000000000..3dc7c4f66 --- /dev/null +++ b/NetworkPkg/HttpDxe/ComponentName.h @@ -0,0 +1,92 @@ +/** @file + Header file for implementation of UEFI Component Name(2) protocol. + +Copyright (c) 2015, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_HTTP_COMPONENT_NAME_H__ +#define __EFI_HTTP_COMPONENT_NAME_H__ + +/** + Retrieves a Unicode string that is the user-readable name of the EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param Language A pointer to a three-character ISO 639-2 language identifier. + This is the language of the driver name that that the caller + is requesting, and it must match one of the languages specified + in SupportedLanguages. The number of languages supported by a + driver is up to the driver writer. + @param DriverName A pointer to the Unicode string to return. This Unicode string + is the name of the driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by This + and the language specified by Language was returned + in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +HttpDxeComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by an EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param ControllerHandle The handle of a controller that the driver specified by + This is managing. This handle specifies the controller + whose name is to be returned. + @param ChildHandle The handle of the child controller to retrieve the name + of. This is an optional parameter that may be NULL. It + will be NULL for device drivers. It will also be NULL + for a bus drivers that wish to retrieve the name of the + bus controller. It will not be NULL for a bus driver + that wishes to retrieve the name of a child controller. + @param Language A pointer to a three character ISO 639-2 language + identifier. This is the language of the controller name + that the caller is requesting, and it must match one + of the languages specified in SupportedLanguages. The + number of languages supported by a driver is up to the + driver writer. + @param ControllerName A pointer to the Unicode string to return. This Unicode + string is the name of the controller specified by + ControllerHandle and ChildHandle in the language specified + by Language, from the point of view of the driver specified + by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in the + language specified by Language for the driver + specified by This was returned in DriverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not currently managing + the controller specified by ControllerHandle and + ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +HttpDxeComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +#endif diff --git a/NetworkPkg/HttpDxe/HttpDns.c b/NetworkPkg/HttpDxe/HttpDns.c new file mode 100644 index 000000000..5f092c00e --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpDns.c @@ -0,0 +1,409 @@ +/** @file + Routines for HttpDxe driver to perform DNS resolution based on UEFI DNS protocols. + +Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpDriver.h" + +/** + Retrieve the host address using the EFI_DNS4_PROTOCOL. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL instance. + @param[in] HostName Pointer to buffer containing hostname. + @param[out] IpAddress On output, pointer to buffer containing IPv4 address. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpDns4 ( + IN HTTP_PROTOCOL *HttpInstance, + IN CHAR16 *HostName, + OUT EFI_IPv4_ADDRESS *IpAddress + ) +{ + EFI_STATUS Status; + EFI_DNS4_PROTOCOL *Dns4; + EFI_DNS4_CONFIG_DATA Dns4CfgData; + EFI_DNS4_COMPLETION_TOKEN Token; + BOOLEAN IsDone; + HTTP_SERVICE *Service; + EFI_HANDLE Dns4Handle; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + UINTN DnsServerListCount; + EFI_IPv4_ADDRESS *DnsServerList; + UINTN DataSize; + + + Service = HttpInstance->Service; + ASSERT (Service != NULL); + + DnsServerList = NULL; + DnsServerListCount = 0; + ZeroMem (&Token, sizeof (EFI_DNS4_COMPLETION_TOKEN)); + + // + // Get DNS server list from EFI IPv4 Configuration II protocol. + // + Status = gBS->HandleProtocol (Service->ControllerHandle, &gEfiIp4Config2ProtocolGuid, (VOID **) &Ip4Config2); + if (!EFI_ERROR (Status)) { + // + // Get the required size. + // + DataSize = 0; + Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypeDnsServer, &DataSize, NULL); + if (Status == EFI_BUFFER_TOO_SMALL) { + DnsServerList = AllocatePool (DataSize); + if (DnsServerList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypeDnsServer, &DataSize, DnsServerList); + if (EFI_ERROR (Status)) { + FreePool (DnsServerList); + DnsServerList = NULL; + } else { + DnsServerListCount = DataSize / sizeof (EFI_IPv4_ADDRESS); + } + } + } + + Dns4Handle = NULL; + Dns4 = NULL; + + // + // Create a DNS child instance and get the protocol. + // + Status = NetLibCreateServiceChild ( + Service->ControllerHandle, + Service->Ip4DriverBindingHandle, + &gEfiDns4ServiceBindingProtocolGuid, + &Dns4Handle + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status = gBS->OpenProtocol ( + Dns4Handle, + &gEfiDns4ProtocolGuid, + (VOID **) &Dns4, + Service->Ip4DriverBindingHandle, + Service->ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Configure DNS4 instance for the DNS server address and protocol. + // + ZeroMem (&Dns4CfgData, sizeof (Dns4CfgData)); + Dns4CfgData.DnsServerListCount = DnsServerListCount; + Dns4CfgData.DnsServerList = DnsServerList; + Dns4CfgData.UseDefaultSetting = HttpInstance->IPv4Node.UseDefaultAddress; + if (!Dns4CfgData.UseDefaultSetting) { + IP4_COPY_ADDRESS (&Dns4CfgData.StationIp, &HttpInstance->IPv4Node.LocalAddress); + IP4_COPY_ADDRESS (&Dns4CfgData.SubnetMask, &HttpInstance->IPv4Node.LocalSubnet); + } + Dns4CfgData.EnableDnsCache = TRUE; + Dns4CfgData.Protocol = EFI_IP_PROTO_UDP; + Status = Dns4->Configure ( + Dns4, + &Dns4CfgData + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Create event to set the is done flag when name resolution is finished. + // + ZeroMem (&Token, sizeof (Token)); + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &IsDone, + &Token.Event + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Start asynchronous name resolution. + // + Token.Status = EFI_NOT_READY; + IsDone = FALSE; + Status = Dns4->HostNameToIp (Dns4, HostName, &Token); + if (EFI_ERROR (Status)) { + goto Exit; + } + + while (!IsDone) { + Dns4->Poll (Dns4); + } + + // + // Name resolution is done, check result. + // + Status = Token.Status; + if (!EFI_ERROR (Status)) { + if (Token.RspData.H2AData == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + if (Token.RspData.H2AData->IpCount == 0 || Token.RspData.H2AData->IpList == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + // + // We just return the first IP address from DNS protocol. + // + IP4_COPY_ADDRESS (IpAddress, Token.RspData.H2AData->IpList); + Status = EFI_SUCCESS; + } + +Exit: + + if (Token.Event != NULL) { + gBS->CloseEvent (Token.Event); + } + if (Token.RspData.H2AData != NULL) { + if (Token.RspData.H2AData->IpList != NULL) { + FreePool (Token.RspData.H2AData->IpList); + } + FreePool (Token.RspData.H2AData); + } + + if (Dns4 != NULL) { + Dns4->Configure (Dns4, NULL); + + gBS->CloseProtocol ( + Dns4Handle, + &gEfiDns4ProtocolGuid, + Service->Ip4DriverBindingHandle, + Service->ControllerHandle + ); + } + + if (Dns4Handle != NULL) { + NetLibDestroyServiceChild ( + Service->ControllerHandle, + Service->Ip4DriverBindingHandle, + &gEfiDns4ServiceBindingProtocolGuid, + Dns4Handle + ); + } + + if (DnsServerList != NULL) { + FreePool (DnsServerList); + } + + return Status; +} + +/** + Retrieve the host address using the EFI_DNS6_PROTOCOL. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL instance. + @param[in] HostName Pointer to buffer containing hostname. + @param[out] IpAddress On output, pointer to buffer containing IPv6 address. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpDns6 ( + IN HTTP_PROTOCOL *HttpInstance, + IN CHAR16 *HostName, + OUT EFI_IPv6_ADDRESS *IpAddress + ) +{ + EFI_STATUS Status; + HTTP_SERVICE *Service; + EFI_DNS6_PROTOCOL *Dns6; + EFI_DNS6_CONFIG_DATA Dns6ConfigData; + EFI_DNS6_COMPLETION_TOKEN Token; + EFI_HANDLE Dns6Handle; + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + EFI_IPv6_ADDRESS *DnsServerList; + UINTN DnsServerListCount; + UINTN DataSize; + BOOLEAN IsDone; + + + Service = HttpInstance->Service; + ASSERT (Service != NULL); + + DnsServerList = NULL; + DnsServerListCount = 0; + Dns6 = NULL; + Dns6Handle = NULL; + ZeroMem (&Token, sizeof (EFI_DNS6_COMPLETION_TOKEN)); + + // + // Get DNS server list from EFI IPv6 Configuration protocol. + // + Status = gBS->HandleProtocol (Service->ControllerHandle, &gEfiIp6ConfigProtocolGuid, (VOID **) &Ip6Config); + if (!EFI_ERROR (Status)) { + // + // Get the required size. + // + DataSize = 0; + Status = Ip6Config->GetData (Ip6Config, Ip6ConfigDataTypeDnsServer, &DataSize, NULL); + if (Status == EFI_BUFFER_TOO_SMALL) { + DnsServerList = AllocatePool (DataSize); + if (DnsServerList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = Ip6Config->GetData (Ip6Config, Ip6ConfigDataTypeDnsServer, &DataSize, DnsServerList); + if (EFI_ERROR (Status)) { + FreePool (DnsServerList); + DnsServerList = NULL; + } else { + DnsServerListCount = DataSize / sizeof (EFI_IPv6_ADDRESS); + } + } + } + + // + // Create a DNSv6 child instance and get the protocol. + // + Status = NetLibCreateServiceChild ( + Service->ControllerHandle, + Service->Ip6DriverBindingHandle, + &gEfiDns6ServiceBindingProtocolGuid, + &Dns6Handle + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status = gBS->OpenProtocol ( + Dns6Handle, + &gEfiDns6ProtocolGuid, + (VOID **) &Dns6, + Service->Ip6DriverBindingHandle, + Service->ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Configure DNS6 instance for the DNS server address and protocol. + // + ZeroMem (&Dns6ConfigData, sizeof (EFI_DNS6_CONFIG_DATA)); + Dns6ConfigData.DnsServerCount = (UINT32)DnsServerListCount; + Dns6ConfigData.DnsServerList = DnsServerList; + Dns6ConfigData.EnableDnsCache = TRUE; + Dns6ConfigData.Protocol = EFI_IP_PROTO_UDP; + IP6_COPY_ADDRESS (&Dns6ConfigData.StationIp, &HttpInstance->Ipv6Node.LocalAddress); + Status = Dns6->Configure ( + Dns6, + &Dns6ConfigData + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Token.Status = EFI_NOT_READY; + IsDone = FALSE; + // + // Create event to set the IsDone flag when name resolution is finished. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &IsDone, + &Token.Event + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Start asynchronous name resolution. + // + Status = Dns6->HostNameToIp (Dns6, HostName, &Token); + if (EFI_ERROR (Status)) { + goto Exit; + } + + while (!IsDone) { + Dns6->Poll (Dns6); + } + + // + // Name resolution is done, check result. + // + Status = Token.Status; + if (!EFI_ERROR (Status)) { + if (Token.RspData.H2AData == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + if (Token.RspData.H2AData->IpCount == 0 || Token.RspData.H2AData->IpList == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + // + // We just return the first IPv6 address from DNS protocol. + // + IP6_COPY_ADDRESS (IpAddress, Token.RspData.H2AData->IpList); + Status = EFI_SUCCESS; + } + +Exit: + + if (Token.Event != NULL) { + gBS->CloseEvent (Token.Event); + } + if (Token.RspData.H2AData != NULL) { + if (Token.RspData.H2AData->IpList != NULL) { + FreePool (Token.RspData.H2AData->IpList); + } + FreePool (Token.RspData.H2AData); + } + + if (Dns6 != NULL) { + Dns6->Configure (Dns6, NULL); + + gBS->CloseProtocol ( + Dns6Handle, + &gEfiDns6ProtocolGuid, + Service->Ip6DriverBindingHandle, + Service->ControllerHandle + ); + } + + if (Dns6Handle != NULL) { + NetLibDestroyServiceChild ( + Service->ControllerHandle, + Service->Ip6DriverBindingHandle, + &gEfiDns6ServiceBindingProtocolGuid, + Dns6Handle + ); + } + + if (DnsServerList != NULL) { + FreePool (DnsServerList); + } + + return Status; +} diff --git a/NetworkPkg/HttpDxe/HttpDns.h b/NetworkPkg/HttpDxe/HttpDns.h new file mode 100644 index 000000000..3ddcbdf39 --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpDns.h @@ -0,0 +1,52 @@ +/** @file + The header file of routines for HttpDxe driver to perform DNS resolution based on UEFI DNS protocols. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_HTTP_DNS_H__ +#define __EFI_HTTP_DNS_H__ + +/** + Retrieve the host address using the EFI_DNS4_PROTOCOL. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL instance. + @param[in] HostName Pointer to buffer containing hostname. + @param[out] IpAddress On output, pointer to buffer containing IPv4 address. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpDns4 ( + IN HTTP_PROTOCOL *HttpInstance, + IN CHAR16 *HostName, + OUT EFI_IPv4_ADDRESS *IpAddress + ); + +/** + Retrieve the host address using the EFI_DNS6_PROTOCOL. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL instance. + @param[in] HostName Pointer to buffer containing hostname. + @param[out] IpAddress On output, pointer to buffer containing IPv6 address. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpDns6 ( + IN HTTP_PROTOCOL *HttpInstance, + IN CHAR16 *HostName, + OUT EFI_IPv6_ADDRESS *IpAddress + ); + +#endif diff --git a/NetworkPkg/HttpDxe/HttpDriver.c b/NetworkPkg/HttpDxe/HttpDriver.c new file mode 100644 index 000000000..7c64d4215 --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpDriver.c @@ -0,0 +1,1056 @@ +/** @file + The driver binding and service binding protocol for HttpDxe driver. + + Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpDriver.h" + +EFI_HTTP_UTILITIES_PROTOCOL *mHttpUtilities = NULL; + +/// +/// Driver Binding Protocol instance +/// +EFI_DRIVER_BINDING_PROTOCOL gHttpDxeIp4DriverBinding = { + HttpDxeIp4DriverBindingSupported, + HttpDxeIp4DriverBindingStart, + HttpDxeIp4DriverBindingStop, + HTTP_DRIVER_VERSION, + NULL, + NULL +}; + +EFI_DRIVER_BINDING_PROTOCOL gHttpDxeIp6DriverBinding = { + HttpDxeIp6DriverBindingSupported, + HttpDxeIp6DriverBindingStart, + HttpDxeIp6DriverBindingStop, + HTTP_DRIVER_VERSION, + NULL, + NULL +}; + + +/** + Create a HTTP driver service binding private instance. + + @param[in] Controller The controller that has TCP4 service binding + installed. + @param[out] ServiceData Point to HTTP driver private instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources. + @retval EFI_SUCCESS A new HTTP driver private instance is created. + +**/ +EFI_STATUS +HttpCreateService ( + IN EFI_HANDLE Controller, + OUT HTTP_SERVICE **ServiceData + ) +{ + HTTP_SERVICE *HttpService; + + ASSERT (ServiceData != NULL); + *ServiceData = NULL; + + HttpService = AllocateZeroPool (sizeof (HTTP_SERVICE)); + if (HttpService == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + HttpService->Signature = HTTP_SERVICE_SIGNATURE; + HttpService->ServiceBinding.CreateChild = HttpServiceBindingCreateChild; + HttpService->ServiceBinding.DestroyChild = HttpServiceBindingDestroyChild; + HttpService->ControllerHandle = Controller; + HttpService->ChildrenNumber = 0; + InitializeListHead (&HttpService->ChildrenList); + + *ServiceData = HttpService; + return EFI_SUCCESS; +} + +/** + Release all the resource used the HTTP service binding instance. + + @param[in] HttpService The HTTP private instance. + @param[in] UsingIpv6 Indicate use TCP4 protocol or TCP6 protocol. + if TRUE, use Tcp6 protocol. + if FALSE, use Tcp4 protocl. +**/ +VOID +HttpCleanService ( + IN HTTP_SERVICE *HttpService, + IN BOOLEAN UsingIpv6 + ) +{ + + if (HttpService == NULL) { + return ; + } + if (!UsingIpv6) { + if (HttpService->Tcp4ChildHandle != NULL) { + gBS->CloseProtocol ( + HttpService->Tcp4ChildHandle, + &gEfiTcp4ProtocolGuid, + HttpService->Ip4DriverBindingHandle, + HttpService->ControllerHandle + ); + + NetLibDestroyServiceChild ( + HttpService->ControllerHandle, + HttpService->Ip4DriverBindingHandle, + &gEfiTcp4ServiceBindingProtocolGuid, + HttpService->Tcp4ChildHandle + ); + + HttpService->Tcp4ChildHandle = NULL; + } + } else { + if (HttpService->Tcp6ChildHandle != NULL) { + gBS->CloseProtocol ( + HttpService->Tcp6ChildHandle, + &gEfiTcp6ProtocolGuid, + HttpService->Ip6DriverBindingHandle, + HttpService->ControllerHandle + ); + + NetLibDestroyServiceChild ( + HttpService->ControllerHandle, + HttpService->Ip6DriverBindingHandle, + &gEfiTcp6ServiceBindingProtocolGuid, + HttpService->Tcp6ChildHandle + ); + + HttpService->Tcp6ChildHandle = NULL; + } + } + +} + +/** + The event process routine when the http utilities protocol is installed + in the system. + + @param[in] Event Not used. + @param[in] Context The pointer to the IP4 config2 instance data or IP6 Config instance data. + +**/ +VOID +EFIAPI +HttpUtilitiesInstalledCallback ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + gBS->LocateProtocol ( + &gEfiHttpUtilitiesProtocolGuid, + NULL, + (VOID **) &mHttpUtilities + ); + + // + // Close the event if Http utilities protocol is loacted. + // + if (mHttpUtilities != NULL && Event != NULL) { + gBS->CloseEvent (Event); + } +} + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + @param ImageHandle The firmware allocated handle for the UEFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval Others An unexpected error occurred. + +**/ +EFI_STATUS +EFIAPI +HttpDxeDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + VOID *Registration; + + gBS->LocateProtocol ( + &gEfiHttpUtilitiesProtocolGuid, + NULL, + (VOID **) &mHttpUtilities + ); + + if (mHttpUtilities == NULL) { + // + // No Http utilities protocol, register a notify. + // + EfiCreateProtocolNotifyEvent ( + &gEfiHttpUtilitiesProtocolGuid, + TPL_CALLBACK, + HttpUtilitiesInstalledCallback, + NULL, + &Registration + ); + } + + // + // Install UEFI Driver Model protocol(s). + // + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gHttpDxeIp4DriverBinding, + ImageHandle, + &gHttpDxeComponentName, + &gHttpDxeComponentName2 + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gHttpDxeIp6DriverBinding, + NULL, + &gHttpDxeComponentName, + &gHttpDxeComponentName2 + ); + if (EFI_ERROR (Status)) { + EfiLibUninstallDriverBindingComponentName2 ( + &gHttpDxeIp4DriverBinding, + &gHttpDxeComponentName, + &gHttpDxeComponentName2 + ); + } + return Status; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_INVALID_PARAMETER Any input parameter is NULL. + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +HttpDestroyChildEntryInHandleBuffer ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + HTTP_PROTOCOL *HttpInstance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + HttpInstance = NET_LIST_USER_STRUCT_S (Entry, HTTP_PROTOCOL, Link, HTTP_PROTOCOL_SIGNATURE); + ServiceBinding = ((HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding; + NumberOfChildren = ((HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren; + ChildHandleBuffer = ((HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer; + + if (!NetIsInHandleBuffer (HttpInstance->Handle, NumberOfChildren, ChildHandleBuffer)) { + return EFI_SUCCESS; + } + + return ServiceBinding->DestroyChild (ServiceBinding, HttpInstance->Handle); +} + +/** + Test to see if this driver supports ControllerHandle. This is the worker function for + HttpDxeIp4(6)DriverBindingSupported. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be tested. + @param[in] RemainingDevicePath Optional parameter used to pick a specific child + device to be started. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +HttpDxeSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL, + IN UINT8 IpVersion + ) +{ + EFI_STATUS Status; + EFI_GUID *TcpServiceBindingProtocolGuid; + + if (IpVersion == IP_VERSION_4) { + TcpServiceBindingProtocolGuid = &gEfiTcp4ServiceBindingProtocolGuid; + } else { + TcpServiceBindingProtocolGuid = &gEfiTcp6ServiceBindingProtocolGuid; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + TcpServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + return EFI_SUCCESS; +} + +/** + Start this driver on ControllerHandle. This is the worker function for + HttpDxeIp4(6)DriverBindingStart. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be started. + @param[in] RemainingDevicePath Optional parameter used to pick a specific child + device to be started. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + + @retval EFI_SUCCESS This driver is installed to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +HttpDxeStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL, + IN UINT8 IpVersion + ) +{ + EFI_STATUS Status; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + HTTP_SERVICE *HttpService; + VOID *Interface; + BOOLEAN UsingIpv6; + + UsingIpv6 = FALSE; + + // + // Test for the Http service binding protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiHttpServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (!EFI_ERROR (Status)) { + HttpService = HTTP_SERVICE_FROM_PROTOCOL (ServiceBinding); + } else { + Status = HttpCreateService (ControllerHandle, &HttpService); + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (HttpService != NULL); + + // + // Install the HttpServiceBinding Protocol onto Controller + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiHttpServiceBindingProtocolGuid, + &HttpService->ServiceBinding, + NULL + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + } + + if (IpVersion == IP_VERSION_4) { + HttpService->Ip4DriverBindingHandle = This->DriverBindingHandle; + + if (HttpService->Tcp4ChildHandle == NULL) { + // + // Create a TCP4 child instance, but do not configure it. This will establish the parent-child relationship. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiTcp4ServiceBindingProtocolGuid, + &HttpService->Tcp4ChildHandle + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + HttpService->Tcp4ChildHandle, + &gEfiTcp4ProtocolGuid, + &Interface, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + } else { + return EFI_ALREADY_STARTED; + } + + } else { + UsingIpv6 = TRUE; + HttpService->Ip6DriverBindingHandle = This->DriverBindingHandle; + + if (HttpService->Tcp6ChildHandle == NULL) { + // + // Create a TCP6 child instance, but do not configure it. This will establish the parent-child relationship. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiTcp6ServiceBindingProtocolGuid, + &HttpService->Tcp6ChildHandle + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + HttpService->Tcp6ChildHandle, + &gEfiTcp6ProtocolGuid, + &Interface, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + } else { + return EFI_ALREADY_STARTED; + } + + } + + return EFI_SUCCESS; + +ON_ERROR: + + if (HttpService != NULL) { + HttpCleanService (HttpService, UsingIpv6); + if (HttpService->Tcp4ChildHandle == NULL && HttpService->Tcp6ChildHandle == NULL) { + FreePool (HttpService); + } + } + + return Status; + + +} + +/** + Stop this driver on ControllerHandle. This is the worker function for + HttpDxeIp4(6)DriverBindingStop. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + @retval EFI_SUCCESS This driver was removed ControllerHandle. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval Others This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +HttpDxeStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer, + IN UINT8 IpVersion + ) +{ + EFI_HANDLE NicHandle; + EFI_STATUS Status; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + HTTP_SERVICE *HttpService; + LIST_ENTRY *List; + HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context; + BOOLEAN UsingIpv6; + + // + // HTTP driver opens TCP4(6) child, So, Controller is a TCP4(6) + // child handle. Locate the Nic handle first. Then get the + // HTTP private data back. + // + if (IpVersion == IP_VERSION_4) { + UsingIpv6 = FALSE; + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiTcp4ProtocolGuid); + } else { + UsingIpv6 = TRUE; + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiTcp6ProtocolGuid); + } + + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiHttpServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + NicHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (!EFI_ERROR (Status)) { + + HttpService = HTTP_SERVICE_FROM_PROTOCOL (ServiceBinding); + + if (NumberOfChildren != 0) { + // + // Destroy the HTTP child instance in ChildHandleBuffer. + // + List = &HttpService->ChildrenList; + Context.ServiceBinding = ServiceBinding; + Context.NumberOfChildren = NumberOfChildren; + Context.ChildHandleBuffer = ChildHandleBuffer; + Status = NetDestroyLinkList ( + List, + HttpDestroyChildEntryInHandleBuffer, + &Context, + NULL + ); + } else { + + HttpCleanService (HttpService, UsingIpv6); + + if (HttpService->Tcp4ChildHandle == NULL && HttpService->Tcp6ChildHandle == NULL) { + gBS->UninstallProtocolInterface ( + NicHandle, + &gEfiHttpServiceBindingProtocolGuid, + ServiceBinding + ); + FreePool (HttpService); + } + Status = EFI_SUCCESS; + } + } + + return Status; + +} + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +HttpDxeIp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return HttpDxeSupported ( + This, + ControllerHandle, + RemainingDevicePath, + IP_VERSION_4 + ); +} + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_ALREADY_STARTED This device is already running on ControllerHandle. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +HttpDxeIp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return HttpDxeStart ( + This, + ControllerHandle, + RemainingDevicePath, + IP_VERSION_4 + ); +} + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +HttpDxeIp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + return HttpDxeStop ( + This, + ControllerHandle, + NumberOfChildren, + ChildHandleBuffer, + IP_VERSION_4 + ); +} + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +HttpDxeIp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return HttpDxeSupported ( + This, + ControllerHandle, + RemainingDevicePath, + IP_VERSION_6 + ); + +} + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_ALREADY_STARTED This device is already running on ControllerHandle. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +HttpDxeIp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return HttpDxeStart ( + This, + ControllerHandle, + RemainingDevicePath, + IP_VERSION_6 + ); +} + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +HttpDxeIp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + return HttpDxeStop ( + This, + ControllerHandle, + NumberOfChildren, + ChildHandleBuffer, + IP_VERSION_6 + ); +} +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER This is NULL, or ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +HttpServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ) +{ + HTTP_SERVICE *HttpService; + HTTP_PROTOCOL *HttpInstance; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + HttpService = HTTP_SERVICE_FROM_PROTOCOL (This); + HttpInstance = AllocateZeroPool (sizeof (HTTP_PROTOCOL)); + if (HttpInstance == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + HttpInstance->Signature = HTTP_PROTOCOL_SIGNATURE; + HttpInstance->Service = HttpService; + HttpInstance->Method = HttpMethodMax; + + CopyMem (&HttpInstance->Http, &mEfiHttpTemplate, sizeof (HttpInstance->Http)); + NetMapInit (&HttpInstance->TxTokens); + NetMapInit (&HttpInstance->RxTokens); + + // + // Install HTTP protocol onto ChildHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiHttpProtocolGuid, + &HttpInstance->Http, + NULL + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + HttpInstance->Handle = *ChildHandle; + + // + // Add it to the HTTP service's child list. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&HttpService->ChildrenList, &HttpInstance->Link); + HttpService->ChildrenNumber++; + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; + +ON_ERROR: + + NetMapClean (&HttpInstance->TxTokens); + NetMapClean (&HttpInstance->RxTokens); + FreePool (HttpInstance); + + return Status; +} + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +HttpServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + HTTP_SERVICE *HttpService; + HTTP_PROTOCOL *HttpInstance; + EFI_HTTP_PROTOCOL *Http; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + HttpService = HTTP_SERVICE_FROM_PROTOCOL (This); + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiHttpProtocolGuid, + (VOID **) &Http, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (Http); + if (HttpInstance->Service != HttpService) { + return EFI_INVALID_PARAMETER; + } + + if (HttpInstance->InDestroy) { + return EFI_SUCCESS; + } + + HttpInstance->InDestroy = TRUE; + + // + // Uninstall the HTTP protocol. + // + Status = gBS->UninstallProtocolInterface ( + ChildHandle, + &gEfiHttpProtocolGuid, + Http + ); + + if (EFI_ERROR (Status)) { + HttpInstance->InDestroy = FALSE; + return Status; + } + + HttpCleanProtocol (HttpInstance); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + RemoveEntryList (&HttpInstance->Link); + HttpService->ChildrenNumber--; + + gBS->RestoreTPL (OldTpl); + + FreePool (HttpInstance); + return EFI_SUCCESS; +} diff --git a/NetworkPkg/HttpDxe/HttpDriver.h b/NetworkPkg/HttpDxe/HttpDriver.h new file mode 100644 index 000000000..bd6f658f3 --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpDriver.h @@ -0,0 +1,399 @@ +/** @file + The header files of the driver binding and service binding protocol for HttpDxe driver. + + Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_HTTP_DRIVER_H__ +#define __EFI_HTTP_DRIVER_H__ + +#include +#include + +// +// Libraries +// +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +// UEFI Driver Model Protocols +// +#include +#include +#include +#include + +// +// Consumed Protocols +// +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +// +// Produced Protocols +// +#include + +#include +#include + +#include + +// +// Driver Version +// +#define HTTP_DRIVER_VERSION 0xa + +// +// Protocol instances +// +extern EFI_DRIVER_BINDING_PROTOCOL gHttpDxeIp4DriverBinding; +extern EFI_DRIVER_BINDING_PROTOCOL gHttpDxeIp6DriverBinding; + +extern EFI_COMPONENT_NAME2_PROTOCOL gHttpDxeComponentName2; +extern EFI_COMPONENT_NAME_PROTOCOL gHttpDxeComponentName; + +extern EFI_HTTP_UTILITIES_PROTOCOL *mHttpUtilities; + +// +// Include files with function prototypes +// +#include "ComponentName.h" +#include "HttpImpl.h" +#include "HttpProto.h" +#include "HttpsSupport.h" +#include "HttpDns.h" + +typedef struct { + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; +} HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT; + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +HttpDxeIp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +HttpDxeIp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +HttpDxeIp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +HttpDxeIp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_ALREADY_STARTED This device is already running on ControllerHandle. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +HttpDxeIp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +HttpDxeIp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER This is NULL, or ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +HttpServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +HttpServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +#endif diff --git a/NetworkPkg/HttpDxe/HttpDxe.inf b/NetworkPkg/HttpDxe/HttpDxe.inf new file mode 100644 index 000000000..4bc80b332 --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpDxe.inf @@ -0,0 +1,79 @@ +## @file +# Implementation of EFI HTTP protocol interfaces. +# +# Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = HttpDxe + FILE_GUID = 2366c20f-e15a-11e3-8bf1-e4115b28bc50 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = HttpDxeDriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload + MODULE_UNI_FILE = HttpDxe.uni + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[Sources] + ComponentName.h + ComponentName.c + HttpDns.h + HttpDns.c + HttpDriver.h + HttpDriver.c + HttpImpl.h + HttpImpl.c + HttpProto.h + HttpProto.c + HttpsSupport.h + HttpsSupport.c + +[LibraryClasses] + UefiDriverEntryPoint + UefiBootServicesTableLib + UefiRuntimeServicesTableLib + MemoryAllocationLib + BaseLib + UefiLib + DebugLib + NetLib + HttpLib + DpcLib + +[Protocols] + gEfiHttpServiceBindingProtocolGuid ## BY_START + gEfiHttpProtocolGuid ## BY_START + gEfiHttpUtilitiesProtocolGuid ## CONSUMES + gEfiTcp4ServiceBindingProtocolGuid ## TO_START + gEfiTcp4ProtocolGuid ## TO_START + gEfiTcp6ServiceBindingProtocolGuid ## TO_START + gEfiTcp6ProtocolGuid ## TO_START + gEfiDns4ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiDns4ProtocolGuid ## SOMETIMES_CONSUMES + gEfiDns6ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiDns6ProtocolGuid ## SOMETIMES_CONSUMES + gEfiIp4Config2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiIp6ConfigProtocolGuid ## SOMETIMES_CONSUMES + gEfiTlsServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiTlsProtocolGuid ## SOMETIMES_CONSUMES + gEfiTlsConfigurationProtocolGuid ## SOMETIMES_CONSUMES + +[Guids] + gEfiTlsCaCertificateGuid ## SOMETIMES_CONSUMES ## Variable:L"TlsCaCertificate" + gEdkiiHttpTlsCipherListGuid ## SOMETIMES_CONSUMES ## Variable:L"HttpTlsCipherList" + gEfiCertX509Guid ## SOMETIMES_CONSUMES ## GUID # Check the cert type + +[Pcd] + gEfiNetworkPkgTokenSpaceGuid.PcdAllowHttpConnections ## CONSUMES + +[UserExtensions.TianoCore."ExtraFiles"] + HttpDxeExtra.uni diff --git a/NetworkPkg/HttpDxe/HttpDxe.uni b/NetworkPkg/HttpDxe/HttpDxe.uni new file mode 100644 index 000000000..e649a884b --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpDxe.uni @@ -0,0 +1,17 @@ +// /** @file +// UEFI HTTP DXE Driver. +// +// This driver provides UEFI 2.5 HTTP protocols. It could work with an IPv4 or IPv6 stack. +// +// +// Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "UEFI HTTP service" + +#string STR_MODULE_DESCRIPTION #language en-US "This driver provides EFI HTTP Protocol and EFI HTTP Service Binding Protocol. It could work with an IPv4 or IPv6 stack." + diff --git a/NetworkPkg/HttpDxe/HttpDxeExtra.uni b/NetworkPkg/HttpDxe/HttpDxeExtra.uni new file mode 100644 index 000000000..c7255d760 --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// HttpDxe Localized Strings and Content +// +// Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"UEFI HTTP DXE" + + diff --git a/NetworkPkg/HttpDxe/HttpImpl.c b/NetworkPkg/HttpDxe/HttpImpl.c new file mode 100644 index 000000000..6b877314b --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpImpl.c @@ -0,0 +1,1669 @@ +/** @file + Implementation of EFI_HTTP_PROTOCOL protocol interfaces. + + Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+ (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpDriver.h" + +EFI_HTTP_PROTOCOL mEfiHttpTemplate = { + EfiHttpGetModeData, + EfiHttpConfigure, + EfiHttpRequest, + EfiHttpCancel, + EfiHttpResponse, + EfiHttpPoll +}; + +/** + Returns the operational parameters for the current HTTP child instance. + + The GetModeData() function is used to read the current mode data (operational + parameters) for this HTTP protocol instance. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + @param[out] HttpConfigData Point to buffer for operational parameters of this + HTTP instance. It is the responsibility of the caller + to allocate the memory for HttpConfigData and + HttpConfigData->AccessPoint.IPv6Node/IPv4Node. In fact, + it is recommended to allocate sufficient memory to record + IPv6Node since it is big enough for all possibilities. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + HttpConfigData is NULL. + HttpConfigData->AccessPoint.IPv4Node or + HttpConfigData->AccessPoint.IPv6Node is NULL. + @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started. + +**/ +EFI_STATUS +EFIAPI +EfiHttpGetModeData ( + IN EFI_HTTP_PROTOCOL *This, + OUT EFI_HTTP_CONFIG_DATA *HttpConfigData + ) +{ + HTTP_PROTOCOL *HttpInstance; + + // + // Check input parameters. + // + if ((This == NULL) || (HttpConfigData == NULL)) { + return EFI_INVALID_PARAMETER; + } + + HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This); + + if ((HttpConfigData->AccessPoint.IPv6Node == NULL) || + (HttpConfigData->AccessPoint.IPv4Node == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) { + return EFI_NOT_STARTED; + } + + HttpConfigData->HttpVersion = HttpInstance->HttpVersion; + HttpConfigData->TimeOutMillisec = HttpInstance->TimeOutMillisec; + HttpConfigData->LocalAddressIsIPv6 = HttpInstance->LocalAddressIsIPv6; + + if (HttpInstance->LocalAddressIsIPv6) { + CopyMem ( + HttpConfigData->AccessPoint.IPv6Node, + &HttpInstance->Ipv6Node, + sizeof (HttpInstance->Ipv6Node) + ); + } else { + CopyMem ( + HttpConfigData->AccessPoint.IPv4Node, + &HttpInstance->IPv4Node, + sizeof (HttpInstance->IPv4Node) + ); + } + + return EFI_SUCCESS; +} + +/** + Initialize or brutally reset the operational parameters for this EFI HTTP instance. + + The Configure() function does the following: + When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring + timeout, local address, port, etc. + When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active + connections with remote hosts, canceling all asynchronous tokens, and flush request + and response buffers without informing the appropriate hosts. + + No other EFI HTTP function can be executed by this instance until the Configure() + function is executed and returns successfully. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + @param[in] HttpConfigData Pointer to the configure data to configure the instance. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + HttpConfigData->LocalAddressIsIPv6 is FALSE and + HttpConfigData->AccessPoint.IPv4Node is NULL. + HttpConfigData->LocalAddressIsIPv6 is TRUE and + HttpConfigData->AccessPoint.IPv6Node is NULL. + @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling + Configure() with NULL to reset it. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when + executing Configure(). + @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported + in the implementation. +**/ +EFI_STATUS +EFIAPI +EfiHttpConfigure ( + IN EFI_HTTP_PROTOCOL *This, + IN EFI_HTTP_CONFIG_DATA *HttpConfigData OPTIONAL + ) +{ + HTTP_PROTOCOL *HttpInstance; + EFI_STATUS Status; + + // + // Check input parameters. + // + if (This == NULL || + (HttpConfigData != NULL && + ((HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv6Node == NULL) || + (!HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv4Node == NULL)))) { + return EFI_INVALID_PARAMETER; + } + + HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This); + ASSERT (HttpInstance->Service != NULL); + + if (HttpConfigData != NULL) { + + if (HttpConfigData->HttpVersion >= HttpVersionUnsupported) { + return EFI_UNSUPPORTED; + } + + // + // Now configure this HTTP instance. + // + if (HttpInstance->State != HTTP_STATE_UNCONFIGED) { + return EFI_ALREADY_STARTED; + } + + HttpInstance->HttpVersion = HttpConfigData->HttpVersion; + HttpInstance->TimeOutMillisec = HttpConfigData->TimeOutMillisec; + HttpInstance->LocalAddressIsIPv6 = HttpConfigData->LocalAddressIsIPv6; + + if (HttpConfigData->LocalAddressIsIPv6) { + CopyMem ( + &HttpInstance->Ipv6Node, + HttpConfigData->AccessPoint.IPv6Node, + sizeof (HttpInstance->Ipv6Node) + ); + } else { + CopyMem ( + &HttpInstance->IPv4Node, + HttpConfigData->AccessPoint.IPv4Node, + sizeof (HttpInstance->IPv4Node) + ); + } + + // + // Creat Tcp child + // + Status = HttpInitProtocol (HttpInstance, HttpInstance->LocalAddressIsIPv6); + if (EFI_ERROR (Status)) { + return Status; + } + + HttpInstance->State = HTTP_STATE_HTTP_CONFIGED; + return EFI_SUCCESS; + + } else { + // + // Reset all the resources related to HttpInsance. + // + HttpCleanProtocol (HttpInstance); + HttpInstance->State = HTTP_STATE_UNCONFIGED; + return EFI_SUCCESS; + } +} + + +/** + The Request() function queues an HTTP request to this HTTP instance. + + Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent + successfully, or if there is an error, Status in token will be updated and Event will + be signaled. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + @param[in] Token Pointer to storage containing HTTP request token. + + @retval EFI_SUCCESS Outgoing data was processed. + @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources. + @retval EFI_UNSUPPORTED The HTTP method is not supported in current + implementation. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token->Message is NULL. + Token->Message->Body is not NULL, + Token->Message->BodyLength is non-zero, and + Token->Message->Data is NULL, but a previous call to + Request()has not been completed successfully. +**/ +EFI_STATUS +EFIAPI +EfiHttpRequest ( + IN EFI_HTTP_PROTOCOL *This, + IN EFI_HTTP_TOKEN *Token + ) +{ + EFI_HTTP_MESSAGE *HttpMsg; + EFI_HTTP_REQUEST_DATA *Request; + VOID *UrlParser; + EFI_STATUS Status; + CHAR8 *HostName; + UINTN HostNameSize; + UINT16 RemotePort; + HTTP_PROTOCOL *HttpInstance; + BOOLEAN Configure; + BOOLEAN ReConfigure; + BOOLEAN TlsConfigure; + CHAR8 *RequestMsg; + CHAR8 *Url; + UINTN UrlLen; + CHAR16 *HostNameStr; + HTTP_TOKEN_WRAP *Wrap; + CHAR8 *FileUrl; + UINTN RequestMsgSize; + EFI_HANDLE ImageHandle; + + // + // Initializations + // + Url = NULL; + UrlParser = NULL; + RemotePort = 0; + HostName = NULL; + RequestMsg = NULL; + HostNameStr = NULL; + Wrap = NULL; + FileUrl = NULL; + TlsConfigure = FALSE; + + if ((This == NULL) || (Token == NULL)) { + return EFI_INVALID_PARAMETER; + } + + HttpMsg = Token->Message; + if (HttpMsg == NULL) { + return EFI_INVALID_PARAMETER; + } + + Request = HttpMsg->Data.Request; + + // + // Only support GET, HEAD, DELETE, PATCH, PUT and POST method in current implementation. + // + if ((Request != NULL) && (Request->Method != HttpMethodGet) && + (Request->Method != HttpMethodHead) && (Request->Method != HttpMethodDelete) && + (Request->Method != HttpMethodPut) && (Request->Method != HttpMethodPost) && + (Request->Method != HttpMethodPatch)) { + return EFI_UNSUPPORTED; + } + + HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This); + + // + // Capture the method into HttpInstance. + // + if (Request != NULL) { + HttpInstance->Method = Request->Method; + } + + if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) { + return EFI_NOT_STARTED; + } + + if (Request == NULL) { + // + // Request would be NULL only for PUT/POST/PATCH operation (in the current implementation) + // + if ((HttpInstance->Method != HttpMethodPut) && + (HttpInstance->Method != HttpMethodPost) && + (HttpInstance->Method != HttpMethodPatch)) { + return EFI_INVALID_PARAMETER; + } + + // + // For PUT/POST/PATCH, we need to have the TCP already configured. Bail out if it is not! + // + if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED) { + return EFI_INVALID_PARAMETER; + } + + // + // We need to have the Message Body for sending the HTTP message across in these cases. + // + if (HttpMsg->Body == NULL || HttpMsg->BodyLength == 0) { + return EFI_INVALID_PARAMETER; + } + + // + // Use existing TCP instance to transmit the packet. + // + Configure = FALSE; + ReConfigure = FALSE; + } else { + // + // Check whether the token already existed. + // + if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTokenExist, Token))) { + return EFI_ACCESS_DENIED; + } + + // + // Parse the URI of the remote host. + // + Url = HttpInstance->Url; + UrlLen = StrLen (Request->Url) + 1; + if (UrlLen > HTTP_URL_BUFFER_LEN) { + Url = AllocateZeroPool (UrlLen); + if (Url == NULL) { + return EFI_OUT_OF_RESOURCES; + } + FreePool (HttpInstance->Url); + HttpInstance->Url = Url; + } + + + UnicodeStrToAsciiStrS (Request->Url, Url, UrlLen); + + // + // From the information in Url, the HTTP instance will + // be able to determine whether to use http or https. + // + HttpInstance->UseHttps = IsHttpsUrl (Url); + + // + // HTTP is disabled, return directly if the URI is not HTTPS. + // + if (!PcdGetBool (PcdAllowHttpConnections) && !(HttpInstance->UseHttps)) { + + DEBUG ((EFI_D_ERROR, "EfiHttpRequest: HTTP is disabled.\n")); + + return EFI_ACCESS_DENIED; + } + + // + // Check whether we need to create Tls child and open the TLS protocol. + // + if (HttpInstance->UseHttps && HttpInstance->TlsChildHandle == NULL) { + // + // Use TlsSb to create Tls child and open the TLS protocol. + // + if (HttpInstance->LocalAddressIsIPv6) { + ImageHandle = HttpInstance->Service->Ip6DriverBindingHandle; + } else { + ImageHandle = HttpInstance->Service->Ip4DriverBindingHandle; + } + + HttpInstance->TlsChildHandle = TlsCreateChild ( + ImageHandle, + &(HttpInstance->TlsSb), + &(HttpInstance->Tls), + &(HttpInstance->TlsConfiguration) + ); + if (HttpInstance->TlsChildHandle == NULL) { + return EFI_DEVICE_ERROR; + } + + TlsConfigure = TRUE; + } + + UrlParser = NULL; + Status = HttpParseUrl (Url, (UINT32) AsciiStrLen (Url), FALSE, &UrlParser); + if (EFI_ERROR (Status)) { + goto Error1; + } + + Status = HttpUrlGetHostName (Url, UrlParser, &HostName); + if (EFI_ERROR (Status)) { + goto Error1; + } + + if (HttpInstance->LocalAddressIsIPv6) { + HostNameSize = AsciiStrSize (HostName); + + if (HostNameSize > 2 && HostName[0] == '[' && HostName[HostNameSize - 2] == ']') { + // + // HostName format is expressed as IPv6, so, remove '[' and ']'. + // + HostNameSize -= 2; + CopyMem (HostName, HostName + 1, HostNameSize - 1); + HostName[HostNameSize - 1] = '\0'; + } + } + + Status = HttpUrlGetPort (Url, UrlParser, &RemotePort); + if (EFI_ERROR (Status)) { + if (HttpInstance->UseHttps) { + RemotePort = HTTPS_DEFAULT_PORT; + } else { + RemotePort = HTTP_DEFAULT_PORT; + } + } + // + // If Configure is TRUE, it indicates the first time to call Request(); + // If ReConfigure is TRUE, it indicates the request URL is not same + // with the previous call to Request(); + // + Configure = TRUE; + ReConfigure = TRUE; + + if (HttpInstance->RemoteHost == NULL) { + // + // Request() is called the first time. + // + ReConfigure = FALSE; + } else { + if ((HttpInstance->RemotePort == RemotePort) && + (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0) && + (!HttpInstance->UseHttps || (HttpInstance->UseHttps && + !TlsConfigure && + HttpInstance->TlsSessionState == EfiTlsSessionDataTransferring))) { + // + // Host Name and port number of the request URL are the same with previous call to Request(). + // If Https protocol used, the corresponding SessionState is EfiTlsSessionDataTransferring. + // Check whether previous TCP packet sent out. + // + + if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTcpNotReady, NULL))) { + // + // Wrap the HTTP token in HTTP_TOKEN_WRAP + // + Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP)); + if (Wrap == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error1; + } + + Wrap->HttpToken = Token; + Wrap->HttpInstance = HttpInstance; + + Status = HttpCreateTcpTxEvent (Wrap); + if (EFI_ERROR (Status)) { + goto Error1; + } + + Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap); + if (EFI_ERROR (Status)) { + goto Error1; + } + + Wrap->TcpWrap.Method = Request->Method; + + FreePool (HostName); + + HttpUrlFreeParser (UrlParser); + + // + // Queue the HTTP token and return. + // + return EFI_SUCCESS; + } else { + // + // Use existing TCP instance to transmit the packet. + // + Configure = FALSE; + ReConfigure = FALSE; + } + } else { + // + // Need close existing TCP instance and create a new TCP instance for data transmit. + // + if (HttpInstance->RemoteHost != NULL) { + FreePool (HttpInstance->RemoteHost); + HttpInstance->RemoteHost = NULL; + HttpInstance->RemotePort = 0; + } + } + } + } + + if (Configure) { + // + // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution. + // + if (!HttpInstance->LocalAddressIsIPv6) { + Status = NetLibAsciiStrToIp4 (HostName, &HttpInstance->RemoteAddr); + } else { + Status = HttpUrlGetIp6 (Url, UrlParser, &HttpInstance->RemoteIpv6Addr); + } + + if (EFI_ERROR (Status)) { + HostNameSize = AsciiStrSize (HostName); + HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16)); + if (HostNameStr == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error1; + } + + AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize); + if (!HttpInstance->LocalAddressIsIPv6) { + Status = HttpDns4 (HttpInstance, HostNameStr, &HttpInstance->RemoteAddr); + } else { + Status = HttpDns6 (HttpInstance, HostNameStr, &HttpInstance->RemoteIpv6Addr); + } + + FreePool (HostNameStr); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Error: Could not retrieve the host address from DNS server.\n")); + goto Error1; + } + } + + // + // Save the RemotePort and RemoteHost. + // + ASSERT (HttpInstance->RemoteHost == NULL); + HttpInstance->RemotePort = RemotePort; + HttpInstance->RemoteHost = HostName; + HostName = NULL; + } + + if (ReConfigure) { + // + // The request URL is different from previous calls to Request(), close existing TCP instance. + // + if (!HttpInstance->LocalAddressIsIPv6) { + ASSERT (HttpInstance->Tcp4 != NULL); + } else { + ASSERT (HttpInstance->Tcp6 != NULL); + } + + if (HttpInstance->UseHttps && !TlsConfigure) { + Status = TlsCloseSession (HttpInstance); + if (EFI_ERROR (Status)) { + goto Error1; + } + + TlsCloseTxRxEvent (HttpInstance); + } + + HttpCloseConnection (HttpInstance); + EfiHttpCancel (This, NULL); + } + + // + // Wrap the HTTP token in HTTP_TOKEN_WRAP + // + Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP)); + if (Wrap == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error1; + } + + Wrap->HttpToken = Token; + Wrap->HttpInstance = HttpInstance; + if (Request != NULL) { + Wrap->TcpWrap.Method = Request->Method; + } + + Status = HttpInitSession ( + HttpInstance, + Wrap, + Configure || ReConfigure, + TlsConfigure + ); + if (EFI_ERROR (Status)) { + goto Error2; + } + + if (!Configure && !ReConfigure && !TlsConfigure) { + // + // For the new HTTP token, create TX TCP token events. + // + Status = HttpCreateTcpTxEvent (Wrap); + if (EFI_ERROR (Status)) { + goto Error1; + } + } + + // + // Create request message. + // + FileUrl = Url; + if (Url != NULL && *FileUrl != '/') { + // + // Convert the absolute-URI to the absolute-path + // + while (*FileUrl != ':') { + FileUrl++; + } + if ((*(FileUrl+1) == '/') && (*(FileUrl+2) == '/')) { + FileUrl += 3; + while (*FileUrl != '/') { + FileUrl++; + } + } else { + Status = EFI_INVALID_PARAMETER; + goto Error3; + } + } + + Status = HttpGenRequestMessage (HttpMsg, FileUrl, &RequestMsg, &RequestMsgSize); + + if (EFI_ERROR (Status) || NULL == RequestMsg) { + goto Error3; + } + + // + // Every request we insert a TxToken and a response call would remove the TxToken. + // In cases of PUT/POST/PATCH, after an initial request-response pair, we would do a + // continuous request without a response call. So, in such cases, where Request + // structure is NULL, we would not insert a TxToken. + // + if (Request != NULL) { + Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap); + if (EFI_ERROR (Status)) { + goto Error4; + } + } + + // + // Transmit the request message. + // + Status = HttpTransmitTcp ( + HttpInstance, + Wrap, + (UINT8*) RequestMsg, + RequestMsgSize + ); + if (EFI_ERROR (Status)) { + goto Error5; + } + + DispatchDpc (); + + if (HostName != NULL) { + FreePool (HostName); + } + + if (UrlParser != NULL) { + HttpUrlFreeParser (UrlParser); + } + + return EFI_SUCCESS; + +Error5: + // + // We would have inserted a TxToken only if Request structure is not NULL. + // Hence check before we do a remove in this error case. + // + if (Request != NULL) { + NetMapRemoveTail (&HttpInstance->TxTokens, NULL); + } + +Error4: + if (RequestMsg != NULL) { + FreePool (RequestMsg); + } + +Error3: + if (HttpInstance->UseHttps) { + TlsCloseSession (HttpInstance); + TlsCloseTxRxEvent (HttpInstance); + } + +Error2: + HttpCloseConnection (HttpInstance); + + HttpCloseTcpConnCloseEvent (HttpInstance); + if (NULL != Wrap->TcpWrap.Tx4Token.CompletionToken.Event) { + gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event); + Wrap->TcpWrap.Tx4Token.CompletionToken.Event = NULL; + } + if (NULL != Wrap->TcpWrap.Tx6Token.CompletionToken.Event) { + gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event); + Wrap->TcpWrap.Tx6Token.CompletionToken.Event = NULL; + } + +Error1: + if (HostName != NULL) { + FreePool (HostName); + } + if (Wrap != NULL) { + FreePool (Wrap); + } + if (UrlParser != NULL) { + HttpUrlFreeParser (UrlParser); + } + + return Status; + +} + +/** + Cancel a user's Token. + + @param[in] Map The HTTP instance's token queue. + @param[in] Item Object container for one HTTP token and token's wrap. + @param[in] Context The user's token to cancel. + + @retval EFI_SUCCESS Continue to check the next Item. + @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled. + +**/ +EFI_STATUS +EFIAPI +HttpCancelTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + EFI_HTTP_TOKEN *Token; + HTTP_TOKEN_WRAP *Wrap; + HTTP_PROTOCOL *HttpInstance; + + Token = (EFI_HTTP_TOKEN *) Context; + + // + // Return EFI_SUCCESS to check the next item in the map if + // this one doesn't match. + // + if ((Token != NULL) && (Token != Item->Key)) { + return EFI_SUCCESS; + } + + Wrap = (HTTP_TOKEN_WRAP *) Item->Value; + ASSERT (Wrap != NULL); + HttpInstance = Wrap->HttpInstance; + + if (!HttpInstance->LocalAddressIsIPv6) { + if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) { + // + // Cancle the Token before close its Event. + // + HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &Wrap->TcpWrap.Rx4Token.CompletionToken); + + // + // Dispatch the DPC queued by the NotifyFunction of the canceled token's events. + // + DispatchDpc (); + } + } else { + if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) { + // + // Cancle the Token before close its Event. + // + HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &Wrap->TcpWrap.Rx6Token.CompletionToken); + + // + // Dispatch the DPC queued by the NotifyFunction of the canceled token's events. + // + DispatchDpc (); + } + } + + // + // If only one item is to be cancel, return EFI_ABORTED to stop + // iterating the map any more. + // + if (Token != NULL) { + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + +/** + Cancel the user's receive/transmit request. It is the worker function of + EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the + token. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[in] Token The token to cancel. If NULL, all token will be + cancelled. + + @retval EFI_SUCCESS The token is cancelled. + @retval EFI_NOT_FOUND The asynchronous request or response token is not found. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpCancel ( + IN HTTP_PROTOCOL *HttpInstance, + IN EFI_HTTP_TOKEN *Token + ) +{ + EFI_STATUS Status; + + // + // First check the tokens queued by EfiHttpRequest(). + // + Status = NetMapIterate (&HttpInstance->TxTokens, HttpCancelTokens, Token); + if (EFI_ERROR (Status)) { + if (Token != NULL) { + if (Status == EFI_ABORTED) { + return EFI_SUCCESS; + } + } else { + return Status; + } + } + + if (!HttpInstance->UseHttps) { + // + // Then check the tokens queued by EfiHttpResponse(), except for Https. + // + Status = NetMapIterate (&HttpInstance->RxTokens, HttpCancelTokens, Token); + if (EFI_ERROR (Status)) { + if (Token != NULL) { + if (Status == EFI_ABORTED) { + return EFI_SUCCESS; + } else { + return EFI_NOT_FOUND; + } + } else { + return Status; + } + } + } else { + if (!HttpInstance->LocalAddressIsIPv6) { + HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsRxToken.CompletionToken); + } else { + HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsRxToken.CompletionToken); + } + } + + return EFI_SUCCESS; +} + + +/** + Abort an asynchronous HTTP request or response token. + + The Cancel() function aborts a pending HTTP request or response transaction. If + Token is not NULL and the token is in transmit or receive queues when it is being + cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will + be signaled. If the token is not in one of the queues, which usually means that the + asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL, + all asynchronous tokens issued by Request() or Response() will be aborted. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + @param[in] Token Point to storage containing HTTP request or response + token. + + @retval EFI_SUCCESS Request and Response queues are successfully flushed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance hasn't been configured. + @retval EFI_NOT_FOUND The asynchronous request or response token is not + found. + @retval EFI_UNSUPPORTED The implementation does not support this function. + +**/ +EFI_STATUS +EFIAPI +EfiHttpCancel ( + IN EFI_HTTP_PROTOCOL *This, + IN EFI_HTTP_TOKEN *Token + ) +{ + HTTP_PROTOCOL *HttpInstance; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This); + + if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) { + return EFI_NOT_STARTED; + } + + return HttpCancel (HttpInstance, Token); + +} + +/** + A callback function to intercept events during message parser. + + This function will be invoked during HttpParseMessageBody() with various events type. An error + return status of the callback function will cause the HttpParseMessageBody() aborted. + + @param[in] EventType Event type of this callback call. + @param[in] Data A pointer to data buffer. + @param[in] Length Length in bytes of the Data. + @param[in] Context Callback context set by HttpInitMsgParser(). + + @retval EFI_SUCCESS Continue to parser the message body. + +**/ +EFI_STATUS +EFIAPI +HttpBodyParserCallback ( + IN HTTP_BODY_PARSE_EVENT EventType, + IN CHAR8 *Data, + IN UINTN Length, + IN VOID *Context + ) +{ + HTTP_CALLBACK_DATA *CallbackData; + HTTP_TOKEN_WRAP *Wrap; + UINTN BodyLength; + CHAR8 *Body; + + if (EventType != BodyParseEventOnComplete) { + return EFI_SUCCESS; + } + + if (Data == NULL || Length != 0 || Context == NULL) { + return EFI_SUCCESS; + } + + CallbackData = (HTTP_CALLBACK_DATA *) Context; + + Wrap = (HTTP_TOKEN_WRAP *) (CallbackData->Wrap); + Body = CallbackData->ParseData; + BodyLength = CallbackData->ParseDataLength; + + if (Data < Body + BodyLength) { + Wrap->HttpInstance->NextMsg = Data; + } else { + Wrap->HttpInstance->NextMsg = NULL; + } + + return EFI_SUCCESS; +} + +/** + The work function of EfiHttpResponse(). + + @param[in] Wrap Pointer to HTTP token's wrap data. + + @retval EFI_SUCCESS Allocation succeeded. + @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources. + @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or + the EFI_HTTP_UTILITIES_PROTOCOL is not available. + +**/ +EFI_STATUS +HttpResponseWorker ( + IN HTTP_TOKEN_WRAP *Wrap + ) +{ + EFI_STATUS Status; + EFI_HTTP_MESSAGE *HttpMsg; + CHAR8 *EndofHeader; + CHAR8 *HttpHeaders; + UINTN SizeofHeaders; + UINTN BufferSize; + UINTN StatusCode; + CHAR8 *Tmp; + CHAR8 *HeaderTmp; + CHAR8 *StatusCodeStr; + UINTN BodyLen; + HTTP_PROTOCOL *HttpInstance; + EFI_HTTP_TOKEN *Token; + NET_MAP_ITEM *Item; + HTTP_TOKEN_WRAP *ValueInItem; + UINTN HdrLen; + NET_FRAGMENT Fragment; + + if (Wrap == NULL || Wrap->HttpInstance == NULL) { + return EFI_INVALID_PARAMETER; + } + + HttpInstance = Wrap->HttpInstance; + Token = Wrap->HttpToken; + HttpMsg = Token->Message; + + HttpInstance->EndofHeader = NULL; + HttpInstance->HttpHeaders = NULL; + HttpMsg->Headers = NULL; + HttpHeaders = NULL; + SizeofHeaders = 0; + BufferSize = 0; + EndofHeader = NULL; + ValueInItem = NULL; + Fragment.Len = 0; + Fragment.Bulk = NULL; + + if (HttpMsg->Data.Response != NULL) { + // + // Check whether we have cached header from previous call. + // + if ((HttpInstance->CacheBody != NULL) && (HttpInstance->NextMsg != NULL)) { + // + // The data is stored at [NextMsg, CacheBody + CacheLen]. + // + HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg; + HttpHeaders = AllocateZeroPool (HdrLen); + if (HttpHeaders == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + CopyMem (HttpHeaders, HttpInstance->NextMsg, HdrLen); + FreePool (HttpInstance->CacheBody); + HttpInstance->CacheBody = NULL; + HttpInstance->NextMsg = NULL; + HttpInstance->CacheOffset = 0; + SizeofHeaders = HdrLen; + BufferSize = HttpInstance->CacheLen; + + // + // Check whether we cached the whole HTTP headers. + // + EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR); + } + + HttpInstance->EndofHeader = &EndofHeader; + HttpInstance->HttpHeaders = &HttpHeaders; + + + if (HttpInstance->TimeoutEvent == NULL) { + // + // Create TimeoutEvent for response + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &HttpInstance->TimeoutEvent + ); + if (EFI_ERROR (Status)) { + goto Error; + } + } + + // + // Start the timer, and wait Timeout seconds to receive the header packet. + // + Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND); + if (EFI_ERROR (Status)) { + goto Error; + } + + Status = HttpTcpReceiveHeader (HttpInstance, &SizeofHeaders, &BufferSize, HttpInstance->TimeoutEvent); + + gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0); + + if (EFI_ERROR (Status)) { + goto Error; + } + + ASSERT (HttpHeaders != NULL); + + // + // Cache the part of body. + // + BodyLen = BufferSize - (EndofHeader - HttpHeaders); + if (BodyLen > 0) { + if (HttpInstance->CacheBody != NULL) { + FreePool (HttpInstance->CacheBody); + } + + HttpInstance->CacheBody = AllocateZeroPool (BodyLen); + if (HttpInstance->CacheBody == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + CopyMem (HttpInstance->CacheBody, EndofHeader, BodyLen); + HttpInstance->CacheLen = BodyLen; + } + + // + // Search for Status Code. + // + StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1; + if (StatusCodeStr == NULL) { + Status = EFI_NOT_READY; + goto Error; + } + + StatusCode = AsciiStrDecimalToUintn (StatusCodeStr); + + // + // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n". + // + Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR); + if (Tmp == NULL) { + Status = EFI_NOT_READY; + goto Error; + } + + // + // We could have response with just a HTTP message and no headers. For Example, + // "100 Continue". In such cases, we would not want to unnecessarily call a Parse + // method. A "\r\n" following Tmp string again would indicate an end. Compare and + // set SizeofHeaders to 0. + // + Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR); + if (CompareMem (Tmp, HTTP_CRLF_STR, AsciiStrLen (HTTP_CRLF_STR)) == 0) { + SizeofHeaders = 0; + } else { + SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders); + } + + HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode); + HttpInstance->StatusCode = StatusCode; + + Status = EFI_NOT_READY; + ValueInItem = NULL; + + // + // In cases of PUT/POST/PATCH, after an initial request-response pair, we would do a + // continuous request without a response call. So, we would not do an insert of + // TxToken. After we have sent the complete file, we will call a response to get + // a final response from server. In such a case, we would not have any TxTokens. + // Hence, check that case before doing a NetMapRemoveHead. + // + if (!NetMapIsEmpty (&HttpInstance->TxTokens)) { + NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem); + if (ValueInItem == NULL) { + goto Error; + } + + // + // The first Tx Token not transmitted yet, insert back and return error. + // + if (!ValueInItem->TcpWrap.IsTxDone) { + goto Error2; + } + } + + if (SizeofHeaders != 0) { + HeaderTmp = AllocateZeroPool (SizeofHeaders); + if (HeaderTmp == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error2; + } + + CopyMem (HeaderTmp, Tmp, SizeofHeaders); + FreePool (HttpHeaders); + HttpHeaders = HeaderTmp; + + // + // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available. + // + if (mHttpUtilities == NULL) { + Status = EFI_NOT_READY; + goto Error2; + } + + // + // Parse the HTTP header into array of key/value pairs. + // + Status = mHttpUtilities->Parse ( + mHttpUtilities, + HttpHeaders, + SizeofHeaders, + &HttpMsg->Headers, + &HttpMsg->HeaderCount + ); + if (EFI_ERROR (Status)) { + goto Error2; + } + + FreePool (HttpHeaders); + HttpHeaders = NULL; + + + // + // Init message-body parser by header information. + // + Status = HttpInitMsgParser ( + HttpInstance->Method, + HttpMsg->Data.Response->StatusCode, + HttpMsg->HeaderCount, + HttpMsg->Headers, + HttpBodyParserCallback, + (VOID *) (&HttpInstance->CallbackData), + &HttpInstance->MsgParser + ); + if (EFI_ERROR (Status)) { + goto Error2; + } + + // + // Check whether we received a complete HTTP message. + // + if (HttpInstance->CacheBody != NULL) { + // + // Record the CallbackData data. + // + HttpInstance->CallbackData.Wrap = (VOID *) Wrap; + HttpInstance->CallbackData.ParseData = (VOID *) HttpInstance->CacheBody; + HttpInstance->CallbackData.ParseDataLength = HttpInstance->CacheLen; + + // + // Parse message with CallbackData data. + // + Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody); + if (EFI_ERROR (Status)) { + goto Error2; + } + } + + if (HttpIsMessageComplete (HttpInstance->MsgParser)) { + // + // Free the MsgParse since we already have a full HTTP message. + // + HttpFreeMsgParser (HttpInstance->MsgParser); + HttpInstance->MsgParser = NULL; + } + } + + if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) { + Status = EFI_SUCCESS; + goto Exit; + } + } + + // + // Receive the response body. + // + BodyLen = 0; + + // + // First check whether we cached some data. + // + if (HttpInstance->CacheBody != NULL) { + // + // Calculate the length of the cached data. + // + if (HttpInstance->NextMsg != NULL) { + // + // We have a cached HTTP message which includes a part of HTTP header of next message. + // + BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset); + } else { + BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset; + } + + if (BodyLen > 0) { + // + // We have some cached data. Just copy the data and return. + // + if (HttpMsg->BodyLength < BodyLen) { + CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength); + HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength; + } else { + // + // Copy all cached data out. + // + CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen); + HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset; + HttpMsg->BodyLength = BodyLen; + + if (HttpInstance->NextMsg == NULL) { + // + // There is no HTTP header of next message. Just free the cache buffer. + // + FreePool (HttpInstance->CacheBody); + HttpInstance->CacheBody = NULL; + HttpInstance->NextMsg = NULL; + HttpInstance->CacheOffset = 0; + } + } + // + // Return since we aready received required data. + // + Status = EFI_SUCCESS; + goto Exit; + } + + if (BodyLen == 0 && HttpInstance->MsgParser == NULL) { + // + // We received a complete HTTP message, and we don't have more data to return to caller. + // + HttpMsg->BodyLength = 0; + Status = EFI_SUCCESS; + goto Exit; + } + } + + ASSERT (HttpInstance->MsgParser != NULL); + + // + // We still need receive more data when there is no cache data and MsgParser is not NULL; + // + if (!HttpInstance->UseHttps) { + Status = HttpTcpReceiveBody (Wrap, HttpMsg); + + if (EFI_ERROR (Status)) { + goto Error2; + } + + } else { + if (HttpInstance->TimeoutEvent == NULL) { + // + // Create TimeoutEvent for response + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &HttpInstance->TimeoutEvent + ); + if (EFI_ERROR (Status)) { + goto Error2; + } + } + + // + // Start the timer, and wait Timeout seconds to receive the body packet. + // + Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND); + if (EFI_ERROR (Status)) { + goto Error2; + } + + Status = HttpsReceive (HttpInstance, &Fragment, HttpInstance->TimeoutEvent); + + gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0); + + if (EFI_ERROR (Status)) { + goto Error2; + } + + // + // Process the received the body packet. + // + HttpMsg->BodyLength = MIN (Fragment.Len, (UINT32) HttpMsg->BodyLength); + + CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength); + + // + // Record the CallbackData data. + // + HttpInstance->CallbackData.Wrap = (VOID *) Wrap; + HttpInstance->CallbackData.ParseData = HttpMsg->Body; + HttpInstance->CallbackData.ParseDataLength = HttpMsg->BodyLength; + + // + // Parse Body with CallbackData data. + // + Status = HttpParseMessageBody ( + HttpInstance->MsgParser, + HttpMsg->BodyLength, + HttpMsg->Body + ); + if (EFI_ERROR (Status)) { + goto Error2; + } + + if (HttpIsMessageComplete (HttpInstance->MsgParser)) { + // + // Free the MsgParse since we already have a full HTTP message. + // + HttpFreeMsgParser (HttpInstance->MsgParser); + HttpInstance->MsgParser = NULL; + } + + // + // Check whether there is the next message header in the HttpMsg->Body. + // + if (HttpInstance->NextMsg != NULL) { + HttpMsg->BodyLength = HttpInstance->NextMsg - (CHAR8 *) HttpMsg->Body; + } + + HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength; + if (HttpInstance->CacheLen != 0) { + if (HttpInstance->CacheBody != NULL) { + FreePool (HttpInstance->CacheBody); + } + + HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen); + if (HttpInstance->CacheBody == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error2; + } + + CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen); + HttpInstance->CacheOffset = 0; + if (HttpInstance->NextMsg != NULL) { + HttpInstance->NextMsg = HttpInstance->CacheBody; + } + } + + if (Fragment.Bulk != NULL) { + FreePool (Fragment.Bulk); + Fragment.Bulk = NULL; + } + + goto Exit; + } + + return Status; + +Exit: + Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken); + if (Item != NULL) { + NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL); + } + + if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) { + Token->Status = EFI_HTTP_ERROR; + } else { + Token->Status = Status; + } + + gBS->SignalEvent (Token->Event); + HttpCloseTcpRxEvent (Wrap); + FreePool (Wrap); + return Status; + +Error2: + if (ValueInItem != NULL) { + NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem); + } + +Error: + Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken); + if (Item != NULL) { + NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL); + } + + if (!HttpInstance->UseHttps) { + HttpTcpTokenCleanup (Wrap); + } else { + FreePool (Wrap); + } + + if (HttpHeaders != NULL) { + FreePool (HttpHeaders); + HttpHeaders = NULL; + } + + if (Fragment.Bulk != NULL) { + FreePool (Fragment.Bulk); + Fragment.Bulk = NULL; + } + + if (HttpMsg->Headers != NULL) { + FreePool (HttpMsg->Headers); + HttpMsg->Headers = NULL; + } + + if (HttpInstance->CacheBody != NULL) { + FreePool (HttpInstance->CacheBody); + HttpInstance->CacheBody = NULL; + } + + if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) { + Token->Status = EFI_HTTP_ERROR; + } else { + Token->Status = Status; + } + + gBS->SignalEvent (Token->Event); + + return Status; + +} + + +/** + The Response() function queues an HTTP response to this HTTP instance, similar to + Receive() function in the EFI TCP driver. When the HTTP response is received successfully, + or if there is an error, Status in token will be updated and Event will be signaled. + + The HTTP driver will queue a receive token to the underlying TCP instance. When data + is received in the underlying TCP instance, the data will be parsed and Token will + be populated with the response data. If the data received from the remote host + contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting + (asynchronously) for more data to be sent from the remote host before signaling + Event in Token. + + It is the responsibility of the caller to allocate a buffer for Body and specify the + size in BodyLength. If the remote host provides a response that contains a content + body, up to BodyLength bytes will be copied from the receive buffer into Body and + BodyLength will be updated with the amount of bytes received and copied to Body. This + allows the client to download a large file in chunks instead of into one contiguous + block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is + non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive + token to underlying TCP instance. If data arrives in the receive buffer, up to + BodyLength bytes of data will be copied to Body. The HTTP driver will then update + BodyLength with the amount of bytes received and copied to Body. + + If the HTTP driver does not have an open underlying TCP connection with the host + specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is + consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain + an open TCP connection between client and host. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + @param[in] Token Pointer to storage containing HTTP response token. + + @retval EFI_SUCCESS Allocation succeeded. + @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been + initialized. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token->Message->Headers is NULL. + Token->Message is NULL. + Token->Message->Body is not NULL, + Token->Message->BodyLength is non-zero, and + Token->Message->Data is NULL, but a previous call to + Response() has not been completed successfully. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources. + @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host + specified by response URL. +**/ +EFI_STATUS +EFIAPI +EfiHttpResponse ( + IN EFI_HTTP_PROTOCOL *This, + IN EFI_HTTP_TOKEN *Token + ) +{ + EFI_STATUS Status; + EFI_HTTP_MESSAGE *HttpMsg; + HTTP_PROTOCOL *HttpInstance; + HTTP_TOKEN_WRAP *Wrap; + + if ((This == NULL) || (Token == NULL)) { + return EFI_INVALID_PARAMETER; + } + + HttpMsg = Token->Message; + if (HttpMsg == NULL) { + return EFI_INVALID_PARAMETER; + } + + HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This); + + if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) { + return EFI_NOT_STARTED; + } + + // + // Check whether the token already existed. + // + if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) { + return EFI_ACCESS_DENIED; + } + + Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP)); + if (Wrap == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Wrap->HttpInstance = HttpInstance; + Wrap->HttpToken = Token; + + // + // Notes: For Https, receive token wrapped in HTTP_TOKEN_WRAP is not used to + // receive the https response. A special TlsRxToken is used for receiving TLS + // related messages. It should be a blocking response. + // + if (!HttpInstance->UseHttps) { + Status = HttpCreateTcpRxEvent (Wrap); + if (EFI_ERROR (Status)) { + goto Error; + } + } + + Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap); + if (EFI_ERROR (Status)) { + goto Error; + } + + // + // If already have pending RxTokens, return directly. + // + if (NetMapGetCount (&HttpInstance->RxTokens) > 1) { + return EFI_SUCCESS; + } + + return HttpResponseWorker (Wrap); + +Error: + if (Wrap != NULL) { + if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) { + gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event); + } + + if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) { + gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event); + } + FreePool (Wrap); + } + + return Status; +} + +/** + The Poll() function can be used by network drivers and applications to increase the + rate that data packets are moved between the communication devices and the transmit + and receive queues. + + In some systems, the periodic timer event in the managed network driver may not poll + the underlying communications device fast enough to transmit and/or receive all data + packets without missing incoming packets or dropping outgoing packets. Drivers and + applications that are experiencing packet loss should try calling the Poll() function + more often. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_READY No incoming or outgoing data is processed. + @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started. + +**/ +EFI_STATUS +EFIAPI +EfiHttpPoll ( + IN EFI_HTTP_PROTOCOL *This + ) +{ + EFI_STATUS Status; + HTTP_PROTOCOL *HttpInstance; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This); + + if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) { + return EFI_NOT_STARTED; + } + + if (HttpInstance->LocalAddressIsIPv6) { + if (HttpInstance->Tcp6 == NULL) { + return EFI_NOT_STARTED; + } + Status = HttpInstance->Tcp6->Poll (HttpInstance->Tcp6); + } else { + if (HttpInstance->Tcp4 == NULL) { + return EFI_NOT_STARTED; + } + Status = HttpInstance->Tcp4->Poll (HttpInstance->Tcp4); + } + + DispatchDpc (); + + return Status; +} diff --git a/NetworkPkg/HttpDxe/HttpImpl.h b/NetworkPkg/HttpDxe/HttpImpl.h new file mode 100644 index 000000000..ec6992f4a --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpImpl.h @@ -0,0 +1,233 @@ +/** @file + The header files of implementation of EFI_HTTP_PROTOCOL protocol interfaces. + + Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_HTTP_IMPL_H__ +#define __EFI_HTTP_IMPL_H__ + +#define HTTP_DEFAULT_PORT 80 +#define HTTP_END_OF_HDR_STR "\r\n\r\n" +#define HTTP_CRLF_STR "\r\n" +#define HTTP_VERSION_STR HTTP_VERSION +#define HTTP_VERSION_CRLF_STR " HTTP/1.1\r\n" +#define HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE 300 + + +/** + Returns the operational parameters for the current HTTP child instance. + + The GetModeData() function is used to read the current mode data (operational + parameters) for this HTTP protocol instance. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + @param[out] HttpConfigData Point to buffer for operational parameters of this + HTTP instance. It is the responsibility of the caller + to allocate the memory for HttpConfigData and + HttpConfigData->AccessPoint.IPv6Node/IPv4Node. In fact, + it is recommended to allocate sufficient memory to record + IPv6Node since it is big enough for all possibilities. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + HttpConfigData is NULL. + HttpConfigData->AccessPoint.IPv4Node or + HttpConfigData->AccessPoint.IPv6Node is NULL. + @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started. + +**/ +EFI_STATUS +EFIAPI +EfiHttpGetModeData ( + IN EFI_HTTP_PROTOCOL *This, + OUT EFI_HTTP_CONFIG_DATA *HttpConfigData + ); + +/** + Initialize or brutally reset the operational parameters for this EFI HTTP instance. + + The Configure() function does the following: + When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring + timeout, local address, port, etc. + When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active + connections with remote hosts, canceling all asynchronous tokens, and flush request + and response buffers without informing the appropriate hosts. + + No other EFI HTTP function can be executed by this instance until the Configure() + function is executed and returns successfully. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + @param[in] HttpConfigData Pointer to the configure data to configure the instance. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + HttpConfigData->LocalAddressIsIPv6 is FALSE and + HttpConfigData->AccessPoint.IPv4Node is NULL. + HttpConfigData->LocalAddressIsIPv6 is TRUE and + HttpConfigData->AccessPoint.IPv6Node is NULL. + @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling + Configure() with NULL to reset it. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when + executing Configure(). + @retval EFI_UNSUPPORTED One or more options in ConfigData are not supported + in the implementation. +**/ +EFI_STATUS +EFIAPI +EfiHttpConfigure ( + IN EFI_HTTP_PROTOCOL *This, + IN EFI_HTTP_CONFIG_DATA *HttpConfigData + ); + +/** + The Request() function queues an HTTP request to this HTTP instance. + + Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent + successfully, or if there is an error, Status in token will be updated and Event will + be signaled. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + @param[in] Token Pointer to storage containing HTTP request token. + + @retval EFI_SUCCESS Outgoing data was processed. + @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources. + @retval EFI_UNSUPPORTED The HTTP method is not supported in current + implementation. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token->Message is NULL. + Token->Message->Body is not NULL, + Token->Message->BodyLength is non-zero, and + Token->Message->Data is NULL, but a previous call to + Request()has not been completed successfully. +**/ +EFI_STATUS +EFIAPI +EfiHttpRequest ( + IN EFI_HTTP_PROTOCOL *This, + IN EFI_HTTP_TOKEN *Token + ); + +/** + Abort an asynchronous HTTP request or response token. + + The Cancel() function aborts a pending HTTP request or response transaction. If + Token is not NULL and the token is in transmit or receive queues when it is being + cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will + be signaled. If the token is not in one of the queues, which usually means that the + asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL, + all asynchronous tokens issued by Request() or Response() will be aborted. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + @param[in] Token Point to storage containing HTTP request or response + token. + + @retval EFI_SUCCESS Request and Response queues are successfully flushed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance hasn't been configured. + @retval EFI_NOT_FOUND The asynchronous request or response token is not + found. + @retval EFI_UNSUPPORTED The implementation does not support this function. +**/ +EFI_STATUS +EFIAPI +EfiHttpCancel ( + IN EFI_HTTP_PROTOCOL *This, + IN EFI_HTTP_TOKEN *Token + ); + +/** + The Response() function queues an HTTP response to this HTTP instance, similar to + Receive() function in the EFI TCP driver. When the HTTP response is received successfully, + or if there is an error, Status in token will be updated and Event will be signaled. + + The HTTP driver will queue a receive token to the underlying TCP instance. When data + is received in the underlying TCP instance, the data will be parsed and Token will + be populated with the response data. If the data received from the remote host + contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting + (asynchronously) for more data to be sent from the remote host before signaling + Event in Token. + + It is the responsibility of the caller to allocate a buffer for Body and specify the + size in BodyLength. If the remote host provides a response that contains a content + body, up to BodyLength bytes will be copied from the receive buffer into Body and + BodyLength will be updated with the amount of bytes received and copied to Body. This + allows the client to download a large file in chunks instead of into one contiguous + block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is + non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive + token to underlying TCP instance. If data arrives in the receive buffer, up to + BodyLength bytes of data will be copied to Body. The HTTP driver will then update + BodyLength with the amount of bytes received and copied to Body. + + If the HTTP driver does not have an open underlying TCP connection with the host + specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is + consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain + an open TCP connection between client and host. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + @param[in] Token Pointer to storage containing HTTP response token. + + @retval EFI_SUCCESS Allocation succeeded. + @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been + initialized. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token->Message->Headers is NULL. + Token->Message is NULL. + Token->Message->Body is not NULL, + Token->Message->BodyLength is non-zero, and + Token->Message->Data is NULL, but a previous call to + Response() has not been completed successfully. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources. + @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host + specified by response URL. +**/ +EFI_STATUS +EFIAPI +EfiHttpResponse ( + IN EFI_HTTP_PROTOCOL *This, + IN EFI_HTTP_TOKEN *Token + ); + +/** + The Poll() function can be used by network drivers and applications to increase the + rate that data packets are moved between the communication devices and the transmit + and receive queues. + + In some systems, the periodic timer event in the managed network driver may not poll + the underlying communications device fast enough to transmit and/or receive all data + packets without missing incoming packets or dropping outgoing packets. Drivers and + applications that are experiencing packet loss should try calling the Poll() function + more often. + + @param[in] This Pointer to EFI_HTTP_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_READY No incoming or outgoing data is processed. + @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started. + +**/ +EFI_STATUS +EFIAPI +EfiHttpPoll ( + IN EFI_HTTP_PROTOCOL *This + ); + +extern EFI_HTTP_PROTOCOL mEfiHttpTemplate; + +#endif diff --git a/NetworkPkg/HttpDxe/HttpProto.c b/NetworkPkg/HttpDxe/HttpProto.c new file mode 100644 index 000000000..bacfbc995 --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpProto.c @@ -0,0 +1,2196 @@ +/** @file + Miscellaneous routines for HttpDxe driver. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpDriver.h" + +/** + The common notify function used in HTTP driver. + + @param[in] Event The event signaled. + @param[in] Context The context. + +**/ +VOID +EFIAPI +HttpCommonNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + if ((Event == NULL) || (Context == NULL)) { + return ; + } + + *((BOOLEAN *) Context) = TRUE; +} + +/** + The notify function associated with Tx4Token for Tcp4->Transmit() or Tx6Token for Tcp6->Transmit(). + + @param[in] Context The context. + +**/ +VOID +EFIAPI +HttpTcpTransmitNotifyDpc ( + IN VOID *Context + ) +{ + HTTP_TOKEN_WRAP *Wrap; + HTTP_PROTOCOL *HttpInstance; + + if (Context == NULL) { + return ; + } + + Wrap = (HTTP_TOKEN_WRAP *) Context; + HttpInstance = Wrap->HttpInstance; + + if (!HttpInstance->LocalAddressIsIPv6) { + Wrap->HttpToken->Status = Wrap->TcpWrap.Tx4Token.CompletionToken.Status; + gBS->SignalEvent (Wrap->HttpToken->Event); + + // + // Free resources. + // + if (Wrap->TcpWrap.Tx4Token.Packet.TxData->FragmentTable[0].FragmentBuffer != NULL) { + FreePool (Wrap->TcpWrap.Tx4Token.Packet.TxData->FragmentTable[0].FragmentBuffer); + } + + if (Wrap->TcpWrap.Tx4Token.CompletionToken.Event != NULL) { + gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event); + } + + } else { + Wrap->HttpToken->Status = Wrap->TcpWrap.Tx6Token.CompletionToken.Status; + gBS->SignalEvent (Wrap->HttpToken->Event); + + // + // Free resources. + // + if (Wrap->TcpWrap.Tx6Token.Packet.TxData->FragmentTable[0].FragmentBuffer != NULL) { + FreePool (Wrap->TcpWrap.Tx6Token.Packet.TxData->FragmentTable[0].FragmentBuffer); + } + + if (Wrap->TcpWrap.Tx6Token.CompletionToken.Event != NULL) { + gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event); + } + } + + + Wrap->TcpWrap.IsTxDone = TRUE; + + // + // Check pending TxTokens and sent out. + // + NetMapIterate (&Wrap->HttpInstance->TxTokens, HttpTcpTransmit, NULL); + +} + +/** + Request HttpTcpTransmitNotifyDpc as a DPC at TPL_CALLBACK. + + @param Event The receive event delivered to TCP for transmit. + @param Context Context for the callback. + +**/ +VOID +EFIAPI +HttpTcpTransmitNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request HttpTcpTransmitNotifyDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, HttpTcpTransmitNotifyDpc, Context); +} + +/** + The notify function associated with Rx4Token for Tcp4->Receive () or Rx6Token for Tcp6->Receive(). + + @param[in] Context The context. + +**/ +VOID +EFIAPI +HttpTcpReceiveNotifyDpc ( + IN VOID *Context + ) +{ + HTTP_TOKEN_WRAP *Wrap; + NET_MAP_ITEM *Item; + UINTN Length; + EFI_STATUS Status; + HTTP_PROTOCOL *HttpInstance; + BOOLEAN UsingIpv6; + + if (Context == NULL) { + return ; + } + + Wrap = (HTTP_TOKEN_WRAP *) Context; + HttpInstance = Wrap->HttpInstance; + UsingIpv6 = HttpInstance->LocalAddressIsIPv6; + + if (UsingIpv6) { + gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event); + Wrap->TcpWrap.Rx6Token.CompletionToken.Event = NULL; + + if (EFI_ERROR (Wrap->TcpWrap.Rx6Token.CompletionToken.Status)) { + DEBUG ((EFI_D_ERROR, "HttpTcpReceiveNotifyDpc: %r!\n", Wrap->TcpWrap.Rx6Token.CompletionToken.Status)); + Wrap->HttpToken->Status = Wrap->TcpWrap.Rx6Token.CompletionToken.Status; + gBS->SignalEvent (Wrap->HttpToken->Event); + + Item = NetMapFindKey (&HttpInstance->RxTokens, Wrap->HttpToken); + if (Item != NULL) { + NetMapRemoveItem (&HttpInstance->RxTokens, Item, NULL); + } + + FreePool (Wrap); + Wrap = NULL; + + return ; + } + + } else { + gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event); + Wrap->TcpWrap.Rx4Token.CompletionToken.Event = NULL; + + if (EFI_ERROR (Wrap->TcpWrap.Rx4Token.CompletionToken.Status)) { + DEBUG ((EFI_D_ERROR, "HttpTcpReceiveNotifyDpc: %r!\n", Wrap->TcpWrap.Rx4Token.CompletionToken.Status)); + Wrap->HttpToken->Status = Wrap->TcpWrap.Rx4Token.CompletionToken.Status; + gBS->SignalEvent (Wrap->HttpToken->Event); + + Item = NetMapFindKey (&HttpInstance->RxTokens, Wrap->HttpToken); + if (Item != NULL) { + NetMapRemoveItem (&HttpInstance->RxTokens, Item, NULL); + } + + FreePool (Wrap); + Wrap = NULL; + + return ; + } + } + + // + // Check whether we receive a complete HTTP message. + // + ASSERT (HttpInstance->MsgParser != NULL); + if (UsingIpv6) { + Length = (UINTN) Wrap->TcpWrap.Rx6Data.FragmentTable[0].FragmentLength; + } else { + Length = (UINTN) Wrap->TcpWrap.Rx4Data.FragmentTable[0].FragmentLength; + } + + // + // Record the CallbackData data. + // + HttpInstance->CallbackData.Wrap = (VOID *) Wrap; + HttpInstance->CallbackData.ParseData = Wrap->HttpToken->Message->Body; + HttpInstance->CallbackData.ParseDataLength = Length; + + // + // Parse Body with CallbackData data. + // + Status = HttpParseMessageBody ( + HttpInstance->MsgParser, + Length, + Wrap->HttpToken->Message->Body + ); + if (EFI_ERROR (Status)) { + return ; + } + + if (HttpIsMessageComplete (HttpInstance->MsgParser)) { + // + // Free the MsgParse since we already have a full HTTP message. + // + HttpFreeMsgParser (HttpInstance->MsgParser); + HttpInstance->MsgParser = NULL; + } + + Wrap->HttpToken->Message->BodyLength = Length; + ASSERT (HttpInstance->CacheBody == NULL); + // + // We receive part of header of next HTTP msg. + // + if (HttpInstance->NextMsg != NULL) { + Wrap->HttpToken->Message->BodyLength = HttpInstance->NextMsg - + (CHAR8 *) Wrap->HttpToken->Message->Body; + HttpInstance->CacheLen = Length - Wrap->HttpToken->Message->BodyLength; + if (HttpInstance->CacheLen != 0) { + HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen); + if (HttpInstance->CacheBody == NULL) { + return ; + } + CopyMem (HttpInstance->CacheBody, HttpInstance->NextMsg, HttpInstance->CacheLen); + HttpInstance->NextMsg = HttpInstance->CacheBody; + HttpInstance->CacheOffset = 0; + } + } + + Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken); + if (Item != NULL) { + NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL); + } + + + Wrap->TcpWrap.IsRxDone = TRUE; + if (UsingIpv6) { + Wrap->HttpToken->Status = Wrap->TcpWrap.Rx6Token.CompletionToken.Status; + } else { + Wrap->HttpToken->Status = Wrap->TcpWrap.Rx4Token.CompletionToken.Status; + } + + + gBS->SignalEvent (Wrap->HttpToken->Event); + + // + // Check pending RxTokens and receive the HTTP message. + // + NetMapIterate (&Wrap->HttpInstance->RxTokens, HttpTcpReceive, NULL); + + FreePool (Wrap); + Wrap = NULL; +} + +/** + Request HttpTcpReceiveNotifyDpc as a DPC at TPL_CALLBACK. + + @param Event The receive event delivered to TCP for receive. + @param Context Context for the callback. + +**/ +VOID +EFIAPI +HttpTcpReceiveNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request HttpTcpTransmitNotifyDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, HttpTcpReceiveNotifyDpc, Context); +} + +/** + Create events for the TCP connection token and TCP close token. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + + @retval EFI_SUCCESS The events are created successfully. + @retval others Other error as indicated. + +**/ +EFI_STATUS +HttpCreateTcpConnCloseEvent ( + IN HTTP_PROTOCOL *HttpInstance + ) +{ + EFI_STATUS Status; + + if (!HttpInstance->LocalAddressIsIPv6) { + // + // Create events for variuos asynchronous operations. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &HttpInstance->IsTcp4ConnDone, + &HttpInstance->Tcp4ConnToken.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + // + // Initialize Tcp4CloseToken + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &HttpInstance->IsTcp4CloseDone, + &HttpInstance->Tcp4CloseToken.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + } else { + // + // Create events for variuos asynchronous operations. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &HttpInstance->IsTcp6ConnDone, + &HttpInstance->Tcp6ConnToken.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + // + // Initialize Tcp6CloseToken + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &HttpInstance->IsTcp6CloseDone, + &HttpInstance->Tcp6CloseToken.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + goto ERROR; + } + } + + return EFI_SUCCESS; + +ERROR: + // + // Error handling + // + HttpCloseTcpConnCloseEvent (HttpInstance); + + return Status; +} + + +/** + Close events in the TCP connection token and TCP close token. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + +**/ +VOID +HttpCloseTcpConnCloseEvent ( + IN HTTP_PROTOCOL *HttpInstance + ) +{ + ASSERT (HttpInstance != NULL); + + if (HttpInstance->LocalAddressIsIPv6) { + if (NULL != HttpInstance->Tcp6ConnToken.CompletionToken.Event) { + gBS->CloseEvent (HttpInstance->Tcp6ConnToken.CompletionToken.Event); + HttpInstance->Tcp6ConnToken.CompletionToken.Event = NULL; + } + + if (NULL != HttpInstance->Tcp6CloseToken.CompletionToken.Event) { + gBS->CloseEvent(HttpInstance->Tcp6CloseToken.CompletionToken.Event); + HttpInstance->Tcp6CloseToken.CompletionToken.Event = NULL; + } + + } else { + if (NULL != HttpInstance->Tcp4ConnToken.CompletionToken.Event) { + gBS->CloseEvent (HttpInstance->Tcp4ConnToken.CompletionToken.Event); + HttpInstance->Tcp4ConnToken.CompletionToken.Event = NULL; + } + + if (NULL != HttpInstance->Tcp4CloseToken.CompletionToken.Event) { + gBS->CloseEvent(HttpInstance->Tcp4CloseToken.CompletionToken.Event); + HttpInstance->Tcp4CloseToken.CompletionToken.Event = NULL; + } + } + +} + +/** + Create event for the TCP transmit token. + + @param[in] Wrap Point to HTTP token's wrap data. + + @retval EFI_SUCCESS The events is created successfully. + @retval others Other error as indicated. + +**/ +EFI_STATUS +HttpCreateTcpTxEvent ( + IN HTTP_TOKEN_WRAP *Wrap + ) +{ + EFI_STATUS Status; + HTTP_PROTOCOL *HttpInstance; + HTTP_TCP_TOKEN_WRAP *TcpWrap; + + HttpInstance = Wrap->HttpInstance; + TcpWrap = &Wrap->TcpWrap; + + if (!HttpInstance->LocalAddressIsIPv6) { + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpTcpTransmitNotify, + Wrap, + &TcpWrap->Tx4Token.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + return Status; + } + + TcpWrap->Tx4Data.Push = TRUE; + TcpWrap->Tx4Data.Urgent = FALSE; + TcpWrap->Tx4Data.FragmentCount = 1; + TcpWrap->Tx4Token.Packet.TxData = &Wrap->TcpWrap.Tx4Data; + TcpWrap->Tx4Token.CompletionToken.Status = EFI_NOT_READY; + + } else { + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpTcpTransmitNotify, + Wrap, + &TcpWrap->Tx6Token.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + return Status; + } + + TcpWrap->Tx6Data.Push = TRUE; + TcpWrap->Tx6Data.Urgent = FALSE; + TcpWrap->Tx6Data.FragmentCount = 1; + TcpWrap->Tx6Token.Packet.TxData = &Wrap->TcpWrap.Tx6Data; + TcpWrap->Tx6Token.CompletionToken.Status =EFI_NOT_READY; + + } + + return EFI_SUCCESS; +} + +/** + Create event for the TCP receive token which is used to receive HTTP header. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + + @retval EFI_SUCCESS The events is created successfully. + @retval others Other error as indicated. + +**/ +EFI_STATUS +HttpCreateTcpRxEventForHeader ( + IN HTTP_PROTOCOL *HttpInstance + ) +{ + EFI_STATUS Status; + + if (!HttpInstance->LocalAddressIsIPv6) { + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &HttpInstance->IsRxDone, + &HttpInstance->Rx4Token.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + return Status; + } + + HttpInstance->Rx4Data.FragmentCount = 1; + HttpInstance->Rx4Token.Packet.RxData = &HttpInstance->Rx4Data; + HttpInstance->Rx4Token.CompletionToken.Status = EFI_NOT_READY; + + } else { + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &HttpInstance->IsRxDone, + &HttpInstance->Rx6Token.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + return Status; + } + + HttpInstance->Rx6Data.FragmentCount =1; + HttpInstance->Rx6Token.Packet.RxData = &HttpInstance->Rx6Data; + HttpInstance->Rx6Token.CompletionToken.Status = EFI_NOT_READY; + + } + + + return EFI_SUCCESS; +} + +/** + Create event for the TCP receive token which is used to receive HTTP body. + + @param[in] Wrap Point to HTTP token's wrap data. + + @retval EFI_SUCCESS The events is created successfully. + @retval others Other error as indicated. + +**/ +EFI_STATUS +HttpCreateTcpRxEvent ( + IN HTTP_TOKEN_WRAP *Wrap + ) +{ + EFI_STATUS Status; + HTTP_PROTOCOL *HttpInstance; + HTTP_TCP_TOKEN_WRAP *TcpWrap; + + HttpInstance = Wrap->HttpInstance; + TcpWrap = &Wrap->TcpWrap; + if (!HttpInstance->LocalAddressIsIPv6) { + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpTcpReceiveNotify, + Wrap, + &TcpWrap->Rx4Token.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + return Status; + } + + TcpWrap->Rx4Data.FragmentCount = 1; + TcpWrap->Rx4Token.Packet.RxData = &Wrap->TcpWrap.Rx4Data; + TcpWrap->Rx4Token.CompletionToken.Status = EFI_NOT_READY; + + } else { + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpTcpReceiveNotify, + Wrap, + &TcpWrap->Rx6Token.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + return Status; + } + + TcpWrap->Rx6Data.FragmentCount = 1; + TcpWrap->Rx6Token.Packet.RxData = &Wrap->TcpWrap.Rx6Data; + TcpWrap->Rx6Token.CompletionToken.Status = EFI_NOT_READY; + } + + return EFI_SUCCESS; +} + +/** + Close Events for Tcp Receive Tokens for HTTP body and HTTP header. + + @param[in] Wrap Pointer to HTTP token's wrap data. + +**/ +VOID +HttpCloseTcpRxEvent ( + IN HTTP_TOKEN_WRAP *Wrap + ) +{ + HTTP_PROTOCOL *HttpInstance; + + ASSERT (Wrap != NULL); + HttpInstance = Wrap->HttpInstance; + + if (HttpInstance->LocalAddressIsIPv6) { + if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) { + gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event); + } + + if (HttpInstance->Rx6Token.CompletionToken.Event != NULL) { + gBS->CloseEvent (HttpInstance->Rx6Token.CompletionToken.Event); + HttpInstance->Rx6Token.CompletionToken.Event = NULL; + } + } else { + if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) { + gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event); + } + + if (HttpInstance->Rx4Token.CompletionToken.Event != NULL) { + gBS->CloseEvent (HttpInstance->Rx4Token.CompletionToken.Event); + HttpInstance->Rx4Token.CompletionToken.Event = NULL; + } + } +} + +/** + Intiialize the HTTP_PROTOCOL structure to the unconfigured state. + + @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[in] IpVersion Indicate us TCP4 protocol or TCP6 protocol. + + @retval EFI_SUCCESS HTTP_PROTOCOL structure is initialized successfully. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpInitProtocol ( + IN OUT HTTP_PROTOCOL *HttpInstance, + IN BOOLEAN IpVersion + ) +{ + EFI_STATUS Status; + VOID *Interface; + BOOLEAN UsingIpv6; + + ASSERT (HttpInstance != NULL); + UsingIpv6 = IpVersion; + + if (!UsingIpv6) { + // + // Create TCP4 child. + // + Status = NetLibCreateServiceChild ( + HttpInstance->Service->ControllerHandle, + HttpInstance->Service->Ip4DriverBindingHandle, + &gEfiTcp4ServiceBindingProtocolGuid, + &HttpInstance->Tcp4ChildHandle + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + HttpInstance->Tcp4ChildHandle, + &gEfiTcp4ProtocolGuid, + (VOID **) &Interface, + HttpInstance->Service->Ip4DriverBindingHandle, + HttpInstance->Service->ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + HttpInstance->Tcp4ChildHandle, + &gEfiTcp4ProtocolGuid, + (VOID **) &HttpInstance->Tcp4, + HttpInstance->Service->Ip4DriverBindingHandle, + HttpInstance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR(Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + HttpInstance->Service->Tcp4ChildHandle, + &gEfiTcp4ProtocolGuid, + (VOID **) &Interface, + HttpInstance->Service->Ip4DriverBindingHandle, + HttpInstance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR(Status)) { + goto ON_ERROR; + } + } else { + // + // Create TCP6 Child. + // + Status = NetLibCreateServiceChild ( + HttpInstance->Service->ControllerHandle, + HttpInstance->Service->Ip6DriverBindingHandle, + &gEfiTcp6ServiceBindingProtocolGuid, + &HttpInstance->Tcp6ChildHandle + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + HttpInstance->Tcp6ChildHandle, + &gEfiTcp6ProtocolGuid, + (VOID **) &Interface, + HttpInstance->Service->Ip6DriverBindingHandle, + HttpInstance->Service->ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + HttpInstance->Tcp6ChildHandle, + &gEfiTcp6ProtocolGuid, + (VOID **) &HttpInstance->Tcp6, + HttpInstance->Service->Ip6DriverBindingHandle, + HttpInstance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + + if (EFI_ERROR(Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + HttpInstance->Service->Tcp6ChildHandle, + &gEfiTcp6ProtocolGuid, + (VOID **) &Interface, + HttpInstance->Service->Ip6DriverBindingHandle, + HttpInstance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + + if (EFI_ERROR(Status)) { + goto ON_ERROR; + } + } + + HttpInstance->Url = AllocateZeroPool (HTTP_URL_BUFFER_LEN); + if (HttpInstance->Url == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + + if (HttpInstance->Tcp4ChildHandle != NULL) { + gBS->CloseProtocol ( + HttpInstance->Tcp4ChildHandle, + &gEfiTcp4ProtocolGuid, + HttpInstance->Service->Ip4DriverBindingHandle, + HttpInstance->Service->ControllerHandle + ); + + gBS->CloseProtocol ( + HttpInstance->Tcp4ChildHandle, + &gEfiTcp4ProtocolGuid, + HttpInstance->Service->Ip4DriverBindingHandle, + HttpInstance->Handle + ); + + NetLibDestroyServiceChild ( + HttpInstance->Service->ControllerHandle, + HttpInstance->Service->Ip4DriverBindingHandle, + &gEfiTcp4ServiceBindingProtocolGuid, + HttpInstance->Tcp4ChildHandle + ); + } + + if (HttpInstance->Service->Tcp4ChildHandle != NULL) { + gBS->CloseProtocol ( + HttpInstance->Service->Tcp4ChildHandle, + &gEfiTcp4ProtocolGuid, + HttpInstance->Service->Ip4DriverBindingHandle, + HttpInstance->Handle + ); + } + + if (HttpInstance->Tcp6ChildHandle != NULL) { + gBS->CloseProtocol ( + HttpInstance->Tcp6ChildHandle, + &gEfiTcp6ProtocolGuid, + HttpInstance->Service->Ip6DriverBindingHandle, + HttpInstance->Service->ControllerHandle + ); + + gBS->CloseProtocol ( + HttpInstance->Tcp6ChildHandle, + &gEfiTcp6ProtocolGuid, + HttpInstance->Service->Ip6DriverBindingHandle, + HttpInstance->Handle + ); + + NetLibDestroyServiceChild ( + HttpInstance->Service->ControllerHandle, + HttpInstance->Service->Ip6DriverBindingHandle, + &gEfiTcp6ServiceBindingProtocolGuid, + HttpInstance->Tcp6ChildHandle + ); + } + + if (HttpInstance->Service->Tcp6ChildHandle != NULL) { + gBS->CloseProtocol ( + HttpInstance->Service->Tcp6ChildHandle, + &gEfiTcp6ProtocolGuid, + HttpInstance->Service->Ip6DriverBindingHandle, + HttpInstance->Handle + ); + } + + return EFI_UNSUPPORTED; + +} + +/** + Clean up the HTTP child, release all the resources used by it. + + @param[in] HttpInstance The HTTP child to clean up. + +**/ +VOID +HttpCleanProtocol ( + IN HTTP_PROTOCOL *HttpInstance + ) +{ + HttpCloseConnection (HttpInstance); + + HttpCloseTcpConnCloseEvent (HttpInstance); + + if (HttpInstance->TimeoutEvent != NULL) { + gBS->CloseEvent (HttpInstance->TimeoutEvent); + HttpInstance->TimeoutEvent = NULL; + } + + if (HttpInstance->CacheBody != NULL) { + FreePool (HttpInstance->CacheBody); + HttpInstance->CacheBody = NULL; + HttpInstance->NextMsg = NULL; + } + + if (HttpInstance->RemoteHost != NULL) { + FreePool (HttpInstance->RemoteHost); + HttpInstance->RemoteHost = NULL; + } + + if (HttpInstance->MsgParser != NULL) { + HttpFreeMsgParser (HttpInstance->MsgParser); + HttpInstance->MsgParser = NULL; + } + + if (HttpInstance->Url != NULL) { + FreePool (HttpInstance->Url); + HttpInstance->Url = NULL; + } + + NetMapClean (&HttpInstance->TxTokens); + NetMapClean (&HttpInstance->RxTokens); + + if (HttpInstance->TlsSb != NULL && HttpInstance->TlsChildHandle != NULL) { + // + // Destroy the TLS instance. + // + HttpInstance->TlsSb->DestroyChild (HttpInstance->TlsSb, HttpInstance->TlsChildHandle); + } + + if (HttpInstance->Tcp4ChildHandle != NULL) { + gBS->CloseProtocol ( + HttpInstance->Tcp4ChildHandle, + &gEfiTcp4ProtocolGuid, + HttpInstance->Service->Ip4DriverBindingHandle, + HttpInstance->Service->ControllerHandle + ); + + gBS->CloseProtocol ( + HttpInstance->Tcp4ChildHandle, + &gEfiTcp4ProtocolGuid, + HttpInstance->Service->Ip4DriverBindingHandle, + HttpInstance->Handle + ); + + NetLibDestroyServiceChild ( + HttpInstance->Service->ControllerHandle, + HttpInstance->Service->Ip4DriverBindingHandle, + &gEfiTcp4ServiceBindingProtocolGuid, + HttpInstance->Tcp4ChildHandle + ); + } + + if (HttpInstance->Service->Tcp4ChildHandle != NULL) { + gBS->CloseProtocol ( + HttpInstance->Service->Tcp4ChildHandle, + &gEfiTcp4ProtocolGuid, + HttpInstance->Service->Ip4DriverBindingHandle, + HttpInstance->Handle + ); + } + + if (HttpInstance->Tcp6ChildHandle != NULL) { + gBS->CloseProtocol ( + HttpInstance->Tcp6ChildHandle, + &gEfiTcp6ProtocolGuid, + HttpInstance->Service->Ip6DriverBindingHandle, + HttpInstance->Service->ControllerHandle + ); + + gBS->CloseProtocol ( + HttpInstance->Tcp6ChildHandle, + &gEfiTcp6ProtocolGuid, + HttpInstance->Service->Ip6DriverBindingHandle, + HttpInstance->Handle + ); + + NetLibDestroyServiceChild ( + HttpInstance->Service->ControllerHandle, + HttpInstance->Service->Ip6DriverBindingHandle, + &gEfiTcp6ServiceBindingProtocolGuid, + HttpInstance->Tcp6ChildHandle + ); + } + + if (HttpInstance->Service->Tcp6ChildHandle != NULL) { + gBS->CloseProtocol ( + HttpInstance->Service->Tcp6ChildHandle, + &gEfiTcp6ProtocolGuid, + HttpInstance->Service->Ip6DriverBindingHandle, + HttpInstance->Handle + ); + } + + TlsCloseTxRxEvent (HttpInstance); +} + +/** + Establish TCP connection with HTTP server. + + @param[in] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS The TCP connection is established. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpCreateConnection ( + IN HTTP_PROTOCOL *HttpInstance + ) +{ + EFI_STATUS Status; + + // + // Connect to Http server + // + if (!HttpInstance->LocalAddressIsIPv6) { + HttpInstance->IsTcp4ConnDone = FALSE; + HttpInstance->Tcp4ConnToken.CompletionToken.Status = EFI_NOT_READY; + Status = HttpInstance->Tcp4->Connect (HttpInstance->Tcp4, &HttpInstance->Tcp4ConnToken); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "HttpCreateConnection: Tcp4->Connect() = %r\n", Status)); + return Status; + } + + while (!HttpInstance->IsTcp4ConnDone) { + HttpInstance->Tcp4->Poll (HttpInstance->Tcp4); + } + + Status = HttpInstance->Tcp4ConnToken.CompletionToken.Status; + + } else { + HttpInstance->IsTcp6ConnDone = FALSE; + HttpInstance->Tcp6ConnToken.CompletionToken.Status = EFI_NOT_READY; + Status = HttpInstance->Tcp6->Connect (HttpInstance->Tcp6, &HttpInstance->Tcp6ConnToken); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "HttpCreateConnection: Tcp6->Connect() = %r\n", Status)); + return Status; + } + + while(!HttpInstance->IsTcp6ConnDone) { + HttpInstance->Tcp6->Poll (HttpInstance->Tcp6); + } + + Status = HttpInstance->Tcp6ConnToken.CompletionToken.Status; + } + + if (!EFI_ERROR (Status)) { + HttpInstance->State = HTTP_STATE_TCP_CONNECTED; + } + + return Status; +} + +/** + Close existing TCP connection. + + @param[in] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS The TCP connection is closed. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpCloseConnection ( + IN HTTP_PROTOCOL *HttpInstance + ) +{ + EFI_STATUS Status; + + if (HttpInstance->State == HTTP_STATE_TCP_CONNECTED) { + + if (HttpInstance->LocalAddressIsIPv6) { + HttpInstance->Tcp6CloseToken.AbortOnClose = TRUE; + HttpInstance->IsTcp6CloseDone = FALSE; + Status = HttpInstance->Tcp6->Close (HttpInstance->Tcp6, &HttpInstance->Tcp6CloseToken); + if (EFI_ERROR (Status)) { + return Status; + } + + while (!HttpInstance->IsTcp6CloseDone) { + HttpInstance->Tcp6->Poll (HttpInstance->Tcp6); + } + + } else { + HttpInstance->Tcp4CloseToken.AbortOnClose = TRUE; + HttpInstance->IsTcp4CloseDone = FALSE; + Status = HttpInstance->Tcp4->Close (HttpInstance->Tcp4, &HttpInstance->Tcp4CloseToken); + if (EFI_ERROR (Status)) { + return Status; + } + + while (!HttpInstance->IsTcp4CloseDone) { + HttpInstance->Tcp4->Poll (HttpInstance->Tcp4); + } + } + + } + + HttpInstance->State = HTTP_STATE_TCP_CLOSED; + return EFI_SUCCESS; +} + +/** + Configure TCP4 protocol child. + + @param[in] HttpInstance The HTTP instance private data. + @param[in] Wrap The HTTP token's wrap data. + + @retval EFI_SUCCESS The TCP4 protocol child is configured. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpConfigureTcp4 ( + IN HTTP_PROTOCOL *HttpInstance, + IN HTTP_TOKEN_WRAP *Wrap + ) +{ + EFI_STATUS Status; + EFI_TCP4_CONFIG_DATA *Tcp4CfgData; + EFI_TCP4_ACCESS_POINT *Tcp4AP; + EFI_TCP4_OPTION *Tcp4Option; + + ASSERT (HttpInstance != NULL); + + + Tcp4CfgData = &HttpInstance->Tcp4CfgData; + ZeroMem (Tcp4CfgData, sizeof (EFI_TCP4_CONFIG_DATA)); + + Tcp4CfgData->TypeOfService = HTTP_TOS_DEAULT; + Tcp4CfgData->TimeToLive = HTTP_TTL_DEAULT; + Tcp4CfgData->ControlOption = &HttpInstance->Tcp4Option; + + Tcp4AP = &Tcp4CfgData->AccessPoint; + Tcp4AP->UseDefaultAddress = HttpInstance->IPv4Node.UseDefaultAddress; + if (!Tcp4AP->UseDefaultAddress) { + IP4_COPY_ADDRESS (&Tcp4AP->StationAddress, &HttpInstance->IPv4Node.LocalAddress); + IP4_COPY_ADDRESS (&Tcp4AP->SubnetMask, &HttpInstance->IPv4Node.LocalSubnet); + } + + Tcp4AP->StationPort = HttpInstance->IPv4Node.LocalPort; + Tcp4AP->RemotePort = HttpInstance->RemotePort; + Tcp4AP->ActiveFlag = TRUE; + IP4_COPY_ADDRESS (&Tcp4AP->RemoteAddress, &HttpInstance->RemoteAddr); + + Tcp4Option = Tcp4CfgData->ControlOption; + Tcp4Option->ReceiveBufferSize = HTTP_BUFFER_SIZE_DEAULT; + Tcp4Option->SendBufferSize = HTTP_BUFFER_SIZE_DEAULT; + Tcp4Option->MaxSynBackLog = HTTP_MAX_SYN_BACK_LOG; + Tcp4Option->ConnectionTimeout = HTTP_CONNECTION_TIMEOUT; + Tcp4Option->DataRetries = HTTP_DATA_RETRIES; + Tcp4Option->FinTimeout = HTTP_FIN_TIMEOUT; + Tcp4Option->KeepAliveProbes = HTTP_KEEP_ALIVE_PROBES; + Tcp4Option->KeepAliveTime = HTTP_KEEP_ALIVE_TIME; + Tcp4Option->KeepAliveInterval = HTTP_KEEP_ALIVE_INTERVAL; + Tcp4Option->EnableNagle = TRUE; + Tcp4CfgData->ControlOption = Tcp4Option; + + Status = HttpInstance->Tcp4->Configure (HttpInstance->Tcp4, Tcp4CfgData); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "HttpConfigureTcp4 - %r\n", Status)); + return Status; + } + + Status = HttpCreateTcpConnCloseEvent (HttpInstance); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = HttpCreateTcpTxEvent (Wrap); + if (EFI_ERROR (Status)) { + return Status; + } + + HttpInstance->State = HTTP_STATE_TCP_CONFIGED; + + return EFI_SUCCESS; +} + +/** + Configure TCP6 protocol child. + + @param[in] HttpInstance The HTTP instance private data. + @param[in] Wrap The HTTP token's wrap data. + + @retval EFI_SUCCESS The TCP6 protocol child is configured. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpConfigureTcp6 ( + IN HTTP_PROTOCOL *HttpInstance, + IN HTTP_TOKEN_WRAP *Wrap + ) +{ + EFI_STATUS Status; + EFI_TCP6_CONFIG_DATA *Tcp6CfgData; + EFI_TCP6_ACCESS_POINT *Tcp6Ap; + EFI_TCP6_OPTION *Tcp6Option; + + ASSERT (HttpInstance != NULL); + + Tcp6CfgData = &HttpInstance->Tcp6CfgData; + ZeroMem (Tcp6CfgData, sizeof (EFI_TCP6_CONFIG_DATA)); + + Tcp6CfgData->TrafficClass = 0; + Tcp6CfgData->HopLimit = 255; + Tcp6CfgData->ControlOption = &HttpInstance->Tcp6Option; + + Tcp6Ap = &Tcp6CfgData->AccessPoint; + Tcp6Ap->ActiveFlag = TRUE; + Tcp6Ap->StationPort = HttpInstance->Ipv6Node.LocalPort; + Tcp6Ap->RemotePort = HttpInstance->RemotePort; + IP6_COPY_ADDRESS (&Tcp6Ap->StationAddress, &HttpInstance->Ipv6Node.LocalAddress); + IP6_COPY_ADDRESS (&Tcp6Ap->RemoteAddress , &HttpInstance->RemoteIpv6Addr); + + Tcp6Option = Tcp6CfgData->ControlOption; + Tcp6Option->ReceiveBufferSize = HTTP_BUFFER_SIZE_DEAULT; + Tcp6Option->SendBufferSize = HTTP_BUFFER_SIZE_DEAULT; + Tcp6Option->MaxSynBackLog = HTTP_MAX_SYN_BACK_LOG; + Tcp6Option->ConnectionTimeout = HTTP_CONNECTION_TIMEOUT; + Tcp6Option->DataRetries = HTTP_DATA_RETRIES; + Tcp6Option->FinTimeout = HTTP_FIN_TIMEOUT; + Tcp6Option->KeepAliveProbes = HTTP_KEEP_ALIVE_PROBES; + Tcp6Option->KeepAliveTime = HTTP_KEEP_ALIVE_TIME; + Tcp6Option->KeepAliveInterval = HTTP_KEEP_ALIVE_INTERVAL; + Tcp6Option->EnableNagle = TRUE; + + Status = HttpInstance->Tcp6->Configure (HttpInstance->Tcp6, Tcp6CfgData); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "HttpConfigureTcp6 - %r\n", Status)); + return Status; + } + + Status = HttpCreateTcpConnCloseEvent (HttpInstance); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = HttpCreateTcpTxEvent (Wrap); + if (EFI_ERROR (Status)) { + return Status; + } + + HttpInstance->State = HTTP_STATE_TCP_CONFIGED; + + return EFI_SUCCESS; + +} + +/** + Check existing TCP connection, if in error state, recover TCP4 connection. Then, + connect one TLS session if required. + + @param[in] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS The TCP connection is established. + @retval EFI_NOT_READY TCP4 protocol child is not created or configured. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpConnectTcp4 ( + IN HTTP_PROTOCOL *HttpInstance + ) +{ + EFI_STATUS Status; + EFI_TCP4_CONNECTION_STATE Tcp4State; + + + if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED || HttpInstance->Tcp4 == NULL) { + return EFI_NOT_READY; + } + + Status = HttpInstance->Tcp4->GetModeData( + HttpInstance->Tcp4, + &Tcp4State, + NULL, + NULL, + NULL, + NULL + ); + if (EFI_ERROR(Status)){ + DEBUG ((EFI_D_ERROR, "Tcp4 GetModeData fail - %x\n", Status)); + return Status; + } + + if (Tcp4State == Tcp4StateEstablished) { + return EFI_SUCCESS; + } else if (Tcp4State > Tcp4StateEstablished ) { + HttpCloseConnection(HttpInstance); + } + + Status = HttpCreateConnection (HttpInstance); + if (EFI_ERROR(Status)){ + DEBUG ((EFI_D_ERROR, "Tcp4 Connection fail - %x\n", Status)); + return Status; + } + + // + // Tls session connection. + // + if (HttpInstance->UseHttps) { + if (HttpInstance->TimeoutEvent == NULL) { + // + // Create TimeoutEvent for TLS connection. + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &HttpInstance->TimeoutEvent + ); + if (EFI_ERROR (Status)) { + TlsCloseTxRxEvent (HttpInstance); + return Status; + } + } + + // + // Start the timer, and wait Timeout seconds for connection. + // + Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_CONNECTION_TIMEOUT * TICKS_PER_SECOND); + if (EFI_ERROR (Status)) { + TlsCloseTxRxEvent (HttpInstance); + return Status; + } + + Status = TlsConnectSession (HttpInstance, HttpInstance->TimeoutEvent); + + gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0); + + if (EFI_ERROR (Status)) { + TlsCloseTxRxEvent (HttpInstance); + return Status; + } + } + + return Status; +} + +/** + Check existing TCP connection, if in error state, recover TCP6 connection. Then, + connect one TLS session if required. + + @param[in] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS The TCP connection is established. + @retval EFI_NOT_READY TCP6 protocol child is not created or configured. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpConnectTcp6 ( + IN HTTP_PROTOCOL *HttpInstance + ) +{ + EFI_STATUS Status; + EFI_TCP6_CONNECTION_STATE Tcp6State; + + if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED || HttpInstance->Tcp6 == NULL) { + return EFI_NOT_READY; + } + + Status = HttpInstance->Tcp6->GetModeData ( + HttpInstance->Tcp6, + &Tcp6State, + NULL, + NULL, + NULL, + NULL + ); + + if (EFI_ERROR(Status)){ + DEBUG ((EFI_D_ERROR, "Tcp6 GetModeData fail - %x\n", Status)); + return Status; + } + + if (Tcp6State == Tcp6StateEstablished) { + return EFI_SUCCESS; + } else if (Tcp6State > Tcp6StateEstablished ) { + HttpCloseConnection(HttpInstance); + } + + Status = HttpCreateConnection (HttpInstance); + if (EFI_ERROR(Status)){ + DEBUG ((EFI_D_ERROR, "Tcp6 Connection fail - %x\n", Status)); + return Status; + } + + // + // Tls session connection. + // + if (HttpInstance->UseHttps) { + if (HttpInstance->TimeoutEvent == NULL) { + // + // Create TimeoutEvent for TLS connection. + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &HttpInstance->TimeoutEvent + ); + if (EFI_ERROR (Status)) { + TlsCloseTxRxEvent (HttpInstance); + return Status; + } + } + + // + // Start the timer, and wait Timeout seconds for connection. + // + Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_CONNECTION_TIMEOUT * TICKS_PER_SECOND); + if (EFI_ERROR (Status)) { + TlsCloseTxRxEvent (HttpInstance); + return Status; + } + + Status = TlsConnectSession (HttpInstance, HttpInstance->TimeoutEvent); + + gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0); + + if (EFI_ERROR (Status)) { + TlsCloseTxRxEvent (HttpInstance); + return Status; + } + } + + return Status; +} + +/** + Initialize Http session. + + @param[in] HttpInstance The HTTP instance private data. + @param[in] Wrap The HTTP token's wrap data. + @param[in] Configure The Flag indicates whether need to initialize session. + @param[in] TlsConfigure The Flag indicates whether it's the new Tls session. + + @retval EFI_SUCCESS The initialization of session is done. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpInitSession ( + IN HTTP_PROTOCOL *HttpInstance, + IN HTTP_TOKEN_WRAP *Wrap, + IN BOOLEAN Configure, + IN BOOLEAN TlsConfigure + ) +{ + EFI_STATUS Status; + ASSERT (HttpInstance != NULL); + + // + // Configure Tls session. + // + if (TlsConfigure) { + Status = TlsConfigureSession (HttpInstance); + if (EFI_ERROR (Status)) { + return Status; + } + } + + if (!HttpInstance->LocalAddressIsIPv6) { + // + // Configure TCP instance. + // + if (Configure) { + Status = HttpConfigureTcp4 (HttpInstance, Wrap); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Connect TCP. + // + Status = HttpConnectTcp4 (HttpInstance); + if (EFI_ERROR (Status)) { + return Status; + } + } else { + // + // Configure TCP instance. + // + if (Configure) { + Status = HttpConfigureTcp6 (HttpInstance, Wrap); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Connect TCP. + // + Status = HttpConnectTcp6 (HttpInstance); + if (EFI_ERROR (Status)) { + return Status; + } + } + + return EFI_SUCCESS; + +} + +/** + Send the HTTP or HTTPS message through TCP4 or TCP6. + + @param[in] HttpInstance The HTTP instance private data. + @param[in] Wrap The HTTP token's wrap data. + @param[in] TxString Buffer containing the HTTP message string. + @param[in] TxStringLen Length of the HTTP message string in bytes. + + @retval EFI_SUCCESS The HTTP message is queued into TCP transmit queue. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpTransmitTcp ( + IN HTTP_PROTOCOL *HttpInstance, + IN HTTP_TOKEN_WRAP *Wrap, + IN UINT8 *TxString, + IN UINTN TxStringLen + ) +{ + EFI_STATUS Status; + EFI_TCP4_IO_TOKEN *Tx4Token; + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP6_IO_TOKEN *Tx6Token; + EFI_TCP6_PROTOCOL *Tcp6; + UINT8 *TlsRecord; + UINT16 PayloadSize; + NET_FRAGMENT TempFragment; + NET_FRAGMENT Fragment; + UINTN RecordCount; + UINTN RemainingLen; + + Status = EFI_SUCCESS; + TlsRecord = NULL; + PayloadSize = 0; + TempFragment.Len = 0; + TempFragment.Bulk = NULL; + Fragment.Len = 0; + Fragment.Bulk = NULL; + RecordCount = 0; + RemainingLen = 0; + + // + // Need to encrypt data. + // + if (HttpInstance->UseHttps) { + // + // Allocate enough buffer for each TLS plaintext records. + // + TlsRecord = AllocateZeroPool (TLS_RECORD_HEADER_LENGTH + TLS_PLAINTEXT_RECORD_MAX_PAYLOAD_LENGTH); + if (TlsRecord == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + // + // Allocate enough buffer for all TLS ciphertext records. + // + RecordCount = TxStringLen / TLS_PLAINTEXT_RECORD_MAX_PAYLOAD_LENGTH + 1; + Fragment.Bulk = AllocateZeroPool (RecordCount * (TLS_RECORD_HEADER_LENGTH + TLS_CIPHERTEXT_RECORD_MAX_PAYLOAD_LENGTH)); + if (Fragment.Bulk == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + // + // Encrypt each TLS plaintext records. + // + RemainingLen = TxStringLen; + while (RemainingLen != 0) { + PayloadSize = (UINT16) MIN (TLS_PLAINTEXT_RECORD_MAX_PAYLOAD_LENGTH, RemainingLen); + + ((TLS_RECORD_HEADER *) TlsRecord)->ContentType = TlsContentTypeApplicationData; + ((TLS_RECORD_HEADER *) TlsRecord)->Version.Major = HttpInstance->TlsConfigData.Version.Major; + ((TLS_RECORD_HEADER *) TlsRecord)->Version.Minor = HttpInstance->TlsConfigData.Version.Minor; + ((TLS_RECORD_HEADER *) TlsRecord)->Length = PayloadSize; + + CopyMem (TlsRecord + TLS_RECORD_HEADER_LENGTH, TxString + (TxStringLen - RemainingLen), PayloadSize); + + Status = TlsProcessMessage ( + HttpInstance, + TlsRecord, + TLS_RECORD_HEADER_LENGTH + PayloadSize, + EfiTlsEncrypt, + &TempFragment + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Record the processed/encrypted Packet. + // + CopyMem (Fragment.Bulk + Fragment.Len, TempFragment.Bulk, TempFragment.Len); + Fragment.Len += TempFragment.Len; + + FreePool (TempFragment.Bulk); + TempFragment.Len = 0; + TempFragment.Bulk = NULL; + + RemainingLen -= (UINTN) PayloadSize; + ZeroMem (TlsRecord, TLS_RECORD_HEADER_LENGTH + TLS_PLAINTEXT_RECORD_MAX_PAYLOAD_LENGTH); + } + + FreePool (TlsRecord); + TlsRecord = NULL; + } + + if (!HttpInstance->LocalAddressIsIPv6) { + Tcp4 = HttpInstance->Tcp4; + Tx4Token = &Wrap->TcpWrap.Tx4Token; + + if (HttpInstance->UseHttps) { + Tx4Token->Packet.TxData->DataLength = Fragment.Len; + Tx4Token->Packet.TxData->FragmentTable[0].FragmentLength = Fragment.Len; + Tx4Token->Packet.TxData->FragmentTable[0].FragmentBuffer = (VOID *) Fragment.Bulk; + } else { + Tx4Token->Packet.TxData->DataLength = (UINT32) TxStringLen; + Tx4Token->Packet.TxData->FragmentTable[0].FragmentLength = (UINT32) TxStringLen; + Tx4Token->Packet.TxData->FragmentTable[0].FragmentBuffer = (VOID *) TxString; + } + + Tx4Token->CompletionToken.Status = EFI_NOT_READY; + + Wrap->TcpWrap.IsTxDone = FALSE; + Status = Tcp4->Transmit (Tcp4, Tx4Token); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Transmit failed: %r\n", Status)); + goto ON_ERROR; + } + + } else { + Tcp6 = HttpInstance->Tcp6; + Tx6Token = &Wrap->TcpWrap.Tx6Token; + + if (HttpInstance->UseHttps) { + Tx6Token->Packet.TxData->DataLength = Fragment.Len; + Tx6Token->Packet.TxData->FragmentTable[0].FragmentLength = Fragment.Len; + Tx6Token->Packet.TxData->FragmentTable[0].FragmentBuffer = (VOID *) Fragment.Bulk; + } else { + Tx6Token->Packet.TxData->DataLength = (UINT32) TxStringLen; + Tx6Token->Packet.TxData->FragmentTable[0].FragmentLength = (UINT32) TxStringLen; + Tx6Token->Packet.TxData->FragmentTable[0].FragmentBuffer = (VOID *) TxString; + } + + Tx6Token->CompletionToken.Status = EFI_NOT_READY; + + Wrap->TcpWrap.IsTxDone = FALSE; + Status = Tcp6->Transmit (Tcp6, Tx6Token); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Transmit failed: %r\n", Status)); + goto ON_ERROR; + } + } + + return Status; + +ON_ERROR: + + if (HttpInstance->UseHttps) { + if (TlsRecord != NULL) { + FreePool (TlsRecord); + TlsRecord = NULL; + } + + if (Fragment.Bulk != NULL) { + FreePool (Fragment.Bulk); + Fragment.Bulk = NULL; + } + } + + return Status; +} + +/** + Check whether the user's token or event has already + been enqueue on HTTP Tx or Rx Token list. + + @param[in] Map The container of either user's transmit or receive + token. + @param[in] Item Current item to check against. + @param[in] Context The Token to check againist. + + @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP + @retval EFI_SUCCESS The current item isn't the same token/event as the + context. + +**/ +EFI_STATUS +EFIAPI +HttpTokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + EFI_HTTP_TOKEN *Token; + EFI_HTTP_TOKEN *TokenInItem; + + Token = (EFI_HTTP_TOKEN *) Context; + TokenInItem = (EFI_HTTP_TOKEN *) Item->Key; + + if (Token == TokenInItem || Token->Event == TokenInItem->Event) { + return EFI_ACCESS_DENIED; + } + + return EFI_SUCCESS; +} + +/** + Check whether the HTTP message associated with Tx4Token or Tx6Token is already sent out. + + @param[in] Map The container of Tx4Token or Tx6Token. + @param[in] Item Current item to check against. + @param[in] Context The Token to check againist. + + @retval EFI_NOT_READY The HTTP message is still queued in the list. + @retval EFI_SUCCESS The HTTP message has been sent out. + +**/ +EFI_STATUS +EFIAPI +HttpTcpNotReady ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + HTTP_TOKEN_WRAP *ValueInItem; + + ValueInItem = (HTTP_TOKEN_WRAP *) Item->Value; + + if (!ValueInItem->TcpWrap.IsTxDone) { + return EFI_NOT_READY; + } + + return EFI_SUCCESS; +} + +/** + Transmit the HTTP or HTTPS mssage by processing the associated HTTP token. + + @param[in] Map The container of Tx4Token or Tx6Token. + @param[in] Item Current item to check against. + @param[in] Context The Token to check againist. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_SUCCESS The HTTP message is queued into TCP transmit + queue. + +**/ +EFI_STATUS +EFIAPI +HttpTcpTransmit ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + HTTP_TOKEN_WRAP *ValueInItem; + EFI_STATUS Status; + CHAR8 *RequestMsg; + CHAR8 *Url; + UINTN UrlSize; + UINTN RequestMsgSize; + + RequestMsg = NULL; + + ValueInItem = (HTTP_TOKEN_WRAP *) Item->Value; + if (ValueInItem->TcpWrap.IsTxDone) { + return EFI_SUCCESS; + } + + // + // Parse the URI of the remote host. + // + UrlSize = StrLen (ValueInItem->HttpToken->Message->Data.Request->Url) + 1; + Url = AllocatePool (UrlSize); + if (Url == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + UnicodeStrToAsciiStrS (ValueInItem->HttpToken->Message->Data.Request->Url, Url, UrlSize); + + // + // Create request message. + // + Status = HttpGenRequestMessage ( + ValueInItem->HttpToken->Message, + Url, + &RequestMsg, + &RequestMsgSize + ); + FreePool (Url); + + if (EFI_ERROR (Status) || NULL == RequestMsg){ + return Status; + } + + ASSERT (RequestMsg != NULL); + + // + // Transmit the request message. + // + Status = HttpTransmitTcp ( + ValueInItem->HttpInstance, + ValueInItem, + (UINT8*) RequestMsg, + RequestMsgSize + ); + FreePool (RequestMsg); + return Status; +} + +/** + Receive the HTTP response by processing the associated HTTP token. + + @param[in] Map The container of Rx4Token or Rx6Token. + @param[in] Item Current item to check against. + @param[in] Context The Token to check againist. + + @retval EFI_SUCCESS The HTTP response is queued into TCP receive + queue. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +HttpTcpReceive ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + // + // Process the queued HTTP response. + // + return HttpResponseWorker ((HTTP_TOKEN_WRAP *) Item->Value); +} + +/** + Receive the HTTP header by processing the associated HTTP token. + + @param[in] HttpInstance The HTTP instance private data. + @param[in, out] SizeofHeaders The HTTP header length. + @param[in, out] BufferSize The size of buffer to cacahe the header message. + @param[in] Timeout The time to wait for receiving the header packet. + + @retval EFI_SUCCESS The HTTP header is received. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpTcpReceiveHeader ( + IN HTTP_PROTOCOL *HttpInstance, + IN OUT UINTN *SizeofHeaders, + IN OUT UINTN *BufferSize, + IN EFI_EVENT Timeout + ) +{ + EFI_STATUS Status; + EFI_TCP4_IO_TOKEN *Rx4Token; + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP6_IO_TOKEN *Rx6Token; + EFI_TCP6_PROTOCOL *Tcp6; + CHAR8 **EndofHeader; + CHAR8 **HttpHeaders; + CHAR8 *Buffer; + NET_FRAGMENT Fragment; + + ASSERT (HttpInstance != NULL); + + EndofHeader = HttpInstance->EndofHeader; + HttpHeaders = HttpInstance->HttpHeaders; + Tcp4 = HttpInstance->Tcp4; + Tcp6 = HttpInstance->Tcp6; + Buffer = NULL; + Rx4Token = NULL; + Rx6Token = NULL; + Fragment.Len = 0; + Fragment.Bulk = NULL; + + if (HttpInstance->LocalAddressIsIPv6) { + ASSERT (Tcp6 != NULL); + } else { + ASSERT (Tcp4 != NULL); + } + + if (!HttpInstance->UseHttps) { + Status = HttpCreateTcpRxEventForHeader (HttpInstance); + if (EFI_ERROR (Status)) { + return Status; + } + } + + if (!HttpInstance->LocalAddressIsIPv6) { + if (!HttpInstance->UseHttps) { + Rx4Token = &HttpInstance->Rx4Token; + Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer = AllocateZeroPool (DEF_BUF_LEN); + if (Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + } + + // + // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL. + // + while (*EndofHeader == NULL) { + if (!HttpInstance->UseHttps) { + HttpInstance->IsRxDone = FALSE; + Rx4Token->Packet.RxData->DataLength = DEF_BUF_LEN; + Rx4Token->Packet.RxData->FragmentTable[0].FragmentLength = DEF_BUF_LEN; + Status = Tcp4->Receive (Tcp4, Rx4Token); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status)); + return Status; + } + + while (!HttpInstance->IsRxDone && ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) { + Tcp4->Poll (Tcp4); + } + + if (!HttpInstance->IsRxDone) { + // + // Cancle the Token before close its Event. + // + Tcp4->Cancel (HttpInstance->Tcp4, &Rx4Token->CompletionToken); + gBS->CloseEvent (Rx4Token->CompletionToken.Event); + Rx4Token->CompletionToken.Status = EFI_TIMEOUT; + } + + Status = Rx4Token->CompletionToken.Status; + if (EFI_ERROR (Status)) { + return Status; + } + + Fragment.Len = Rx4Token->Packet.RxData->FragmentTable[0].FragmentLength; + Fragment.Bulk = (UINT8 *) Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer; + } else { + if (Fragment.Bulk != NULL) { + FreePool (Fragment.Bulk); + Fragment.Bulk = NULL; + } + + Status = HttpsReceive (HttpInstance, &Fragment, Timeout); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status)); + return Status; + } + } + + // + // Append the response string along with a Null-terminator. + // + *BufferSize = *SizeofHeaders + Fragment.Len; + Buffer = AllocatePool (*BufferSize + 1); + if (Buffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + if (*HttpHeaders != NULL) { + CopyMem (Buffer, *HttpHeaders, *SizeofHeaders); + FreePool (*HttpHeaders); + } + + CopyMem ( + Buffer + *SizeofHeaders, + Fragment.Bulk, + Fragment.Len + ); + *(Buffer + *BufferSize) = '\0'; + *HttpHeaders = Buffer; + *SizeofHeaders = *BufferSize; + + // + // Check whether we received end of HTTP headers. + // + *EndofHeader = AsciiStrStr (*HttpHeaders, HTTP_END_OF_HDR_STR); + }; + + // + // Free the buffer. + // + if (Rx4Token != NULL && Rx4Token->Packet.RxData != NULL && Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) { + FreePool (Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer); + Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL; + Fragment.Bulk = NULL; + } + + if (Fragment.Bulk != NULL) { + FreePool (Fragment.Bulk); + Fragment.Bulk = NULL; + } + } else { + if (!HttpInstance->UseHttps) { + Rx6Token = &HttpInstance->Rx6Token; + Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer = AllocateZeroPool (DEF_BUF_LEN); + if (Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + } + + // + // Receive the HTTP headers only when EFI_HTTP_RESPONSE_DATA is not NULL. + // + while (*EndofHeader == NULL) { + if (!HttpInstance->UseHttps) { + HttpInstance->IsRxDone = FALSE; + Rx6Token->Packet.RxData->DataLength = DEF_BUF_LEN; + Rx6Token->Packet.RxData->FragmentTable[0].FragmentLength = DEF_BUF_LEN; + Status = Tcp6->Receive (Tcp6, Rx6Token); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Tcp6 receive failed: %r\n", Status)); + return Status; + } + + while (!HttpInstance->IsRxDone && ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) { + Tcp6->Poll (Tcp6); + } + + if (!HttpInstance->IsRxDone) { + // + // Cancle the Token before close its Event. + // + Tcp6->Cancel (HttpInstance->Tcp6, &Rx6Token->CompletionToken); + gBS->CloseEvent (Rx6Token->CompletionToken.Event); + Rx6Token->CompletionToken.Status = EFI_TIMEOUT; + } + + Status = Rx6Token->CompletionToken.Status; + if (EFI_ERROR (Status)) { + return Status; + } + + Fragment.Len = Rx6Token->Packet.RxData->FragmentTable[0].FragmentLength; + Fragment.Bulk = (UINT8 *) Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer; + } else { + if (Fragment.Bulk != NULL) { + FreePool (Fragment.Bulk); + Fragment.Bulk = NULL; + } + + Status = HttpsReceive (HttpInstance, &Fragment, Timeout); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Tcp6 receive failed: %r\n", Status)); + return Status; + } + } + + // + // Append the response string along with a Null-terminator. + // + *BufferSize = *SizeofHeaders + Fragment.Len; + Buffer = AllocatePool (*BufferSize + 1); + if (Buffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + if (*HttpHeaders != NULL) { + CopyMem (Buffer, *HttpHeaders, *SizeofHeaders); + FreePool (*HttpHeaders); + } + + CopyMem ( + Buffer + *SizeofHeaders, + Fragment.Bulk, + Fragment.Len + ); + *(Buffer + *BufferSize) = '\0'; + *HttpHeaders = Buffer; + *SizeofHeaders = *BufferSize; + + // + // Check whether we received end of HTTP headers. + // + *EndofHeader = AsciiStrStr (*HttpHeaders, HTTP_END_OF_HDR_STR); + }; + + // + // Free the buffer. + // + if (Rx6Token != NULL && Rx6Token->Packet.RxData != NULL && Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) { + FreePool (Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer); + Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL; + Fragment.Bulk = NULL; + } + + if (Fragment.Bulk != NULL) { + FreePool (Fragment.Bulk); + Fragment.Bulk = NULL; + } + } + + // + // Skip the CRLF after the HTTP headers. + // + *EndofHeader = *EndofHeader + AsciiStrLen (HTTP_END_OF_HDR_STR); + + *SizeofHeaders = *EndofHeader - *HttpHeaders; + + return EFI_SUCCESS; +} + +/** + Receive the HTTP body by processing the associated HTTP token. + + @param[in] Wrap The HTTP token's wrap data. + @param[in] HttpMsg The HTTP message data. + + @retval EFI_SUCCESS The HTTP body is received. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpTcpReceiveBody ( + IN HTTP_TOKEN_WRAP *Wrap, + IN EFI_HTTP_MESSAGE *HttpMsg + ) +{ + EFI_STATUS Status; + HTTP_PROTOCOL *HttpInstance; + EFI_TCP6_PROTOCOL *Tcp6; + EFI_TCP6_IO_TOKEN *Rx6Token; + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP4_IO_TOKEN *Rx4Token; + + HttpInstance = Wrap->HttpInstance; + Tcp4 = HttpInstance->Tcp4; + Tcp6 = HttpInstance->Tcp6; + Rx4Token = NULL; + Rx6Token = NULL; + + if (HttpInstance->LocalAddressIsIPv6) { + ASSERT (Tcp6 != NULL); + } else { + ASSERT (Tcp4 != NULL); + } + + if (HttpInstance->LocalAddressIsIPv6) { + Rx6Token = &Wrap->TcpWrap.Rx6Token; + Rx6Token ->Packet.RxData->DataLength = (UINT32) MIN (MAX_UINT32, HttpMsg->BodyLength); + Rx6Token ->Packet.RxData->FragmentTable[0].FragmentLength = (UINT32) MIN (MAX_UINT32, HttpMsg->BodyLength); + Rx6Token ->Packet.RxData->FragmentTable[0].FragmentBuffer = (VOID *) HttpMsg->Body; + Rx6Token->CompletionToken.Status = EFI_NOT_READY; + + Status = Tcp6->Receive (Tcp6, Rx6Token); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Tcp6 receive failed: %r\n", Status)); + return Status; + } + } else { + Rx4Token = &Wrap->TcpWrap.Rx4Token; + Rx4Token->Packet.RxData->DataLength = (UINT32) MIN (MAX_UINT32, HttpMsg->BodyLength); + Rx4Token->Packet.RxData->FragmentTable[0].FragmentLength = (UINT32) MIN (MAX_UINT32, HttpMsg->BodyLength); + Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer = (VOID *) HttpMsg->Body; + + Rx4Token->CompletionToken.Status = EFI_NOT_READY; + Status = Tcp4->Receive (Tcp4, Rx4Token); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Tcp4 receive failed: %r\n", Status)); + return Status; + } + } + + return EFI_SUCCESS; + +} + +/** + Clean up Tcp Tokens while the Tcp transmission error occurs. + + @param[in] Wrap Pointer to HTTP token's wrap data. + +**/ +VOID +HttpTcpTokenCleanup ( + IN HTTP_TOKEN_WRAP *Wrap + ) +{ + HTTP_PROTOCOL *HttpInstance; + EFI_TCP4_IO_TOKEN *Rx4Token; + EFI_TCP6_IO_TOKEN *Rx6Token; + + ASSERT (Wrap != NULL); + HttpInstance = Wrap->HttpInstance; + Rx4Token = NULL; + Rx6Token = NULL; + + if (HttpInstance->LocalAddressIsIPv6) { + Rx6Token = &Wrap->TcpWrap.Rx6Token; + + if (Rx6Token->CompletionToken.Event != NULL) { + gBS->CloseEvent (Rx6Token->CompletionToken.Event); + Rx6Token->CompletionToken.Event = NULL; + } + + FreePool (Wrap); + + Rx6Token = &HttpInstance->Rx6Token; + + if (Rx6Token->CompletionToken.Event != NULL) { + gBS->CloseEvent (Rx6Token->CompletionToken.Event); + Rx6Token->CompletionToken.Event = NULL; + } + + if (Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) { + FreePool (Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer); + Rx6Token->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL; + } + + } else { + Rx4Token = &Wrap->TcpWrap.Rx4Token; + + if (Rx4Token->CompletionToken.Event != NULL) { + gBS->CloseEvent (Rx4Token->CompletionToken.Event); + Rx4Token->CompletionToken.Event = NULL; + } + + FreePool (Wrap); + + Rx4Token = &HttpInstance->Rx4Token; + + if (Rx4Token->CompletionToken.Event != NULL) { + gBS->CloseEvent (Rx4Token->CompletionToken.Event); + Rx4Token->CompletionToken.Event = NULL; + } + + + if (Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) { + FreePool (Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer); + Rx4Token->Packet.RxData->FragmentTable[0].FragmentBuffer = NULL; + } + } + +} diff --git a/NetworkPkg/HttpDxe/HttpProto.h b/NetworkPkg/HttpDxe/HttpProto.h new file mode 100644 index 000000000..6e1f51748 --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpProto.h @@ -0,0 +1,611 @@ +/** @file + The header files of miscellaneous routines for HttpDxe driver. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_HTTP_PROTO_H__ +#define __EFI_HTTP_PROTO_H__ + +#define DEF_BUF_LEN 2048 + +#define HTTP_SERVICE_SIGNATURE SIGNATURE_32('H', 't', 't', 'S') + +#define HTTP_SERVICE_FROM_PROTOCOL(a) \ + CR ( \ + (a), \ + HTTP_SERVICE, \ + ServiceBinding, \ + HTTP_SERVICE_SIGNATURE \ + ) + + +// +// The state of HTTP protocol. It starts from UNCONFIGED. +// +#define HTTP_STATE_UNCONFIGED 0 +#define HTTP_STATE_HTTP_CONFIGED 1 +#define HTTP_STATE_TCP_CONFIGED 2 +#define HTTP_STATE_TCP_UNCONFIGED 3 +#define HTTP_STATE_TCP_CONNECTED 4 +#define HTTP_STATE_TCP_CLOSED 5 + +// +// TCP configured data. +// +#define HTTP_TOS_DEAULT 8 +#define HTTP_TTL_DEAULT 255 +#define HTTP_BUFFER_SIZE_DEAULT 65535 +#define HTTP_MAX_SYN_BACK_LOG 5 +#define HTTP_CONNECTION_TIMEOUT 60 +#define HTTP_RESPONSE_TIMEOUT 5 +#define HTTP_DATA_RETRIES 12 +#define HTTP_FIN_TIMEOUT 2 +#define HTTP_KEEP_ALIVE_PROBES 6 +#define HTTP_KEEP_ALIVE_TIME 7200 +#define HTTP_KEEP_ALIVE_INTERVAL 30 + +#define HTTP_URL_BUFFER_LEN 4096 + +typedef struct _HTTP_SERVICE { + UINT32 Signature; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + EFI_HANDLE Ip4DriverBindingHandle; + EFI_HANDLE Ip6DriverBindingHandle; + EFI_HANDLE ControllerHandle; + EFI_HANDLE Tcp4ChildHandle; + EFI_HANDLE Tcp6ChildHandle; + LIST_ENTRY ChildrenList; + UINTN ChildrenNumber; + INTN State; +} HTTP_SERVICE; + +typedef struct { + EFI_TCP4_IO_TOKEN Tx4Token; + EFI_TCP4_TRANSMIT_DATA Tx4Data; + EFI_TCP6_IO_TOKEN Tx6Token; + EFI_TCP6_TRANSMIT_DATA Tx6Data; + EFI_TCP4_IO_TOKEN Rx4Token; + EFI_TCP4_RECEIVE_DATA Rx4Data; + EFI_TCP6_IO_TOKEN Rx6Token; + EFI_TCP6_RECEIVE_DATA Rx6Data; + BOOLEAN IsTxDone; + BOOLEAN IsRxDone; + UINTN BodyLen; + EFI_HTTP_METHOD Method; +} HTTP_TCP_TOKEN_WRAP; + +typedef struct { + EFI_TLS_VERSION Version; + EFI_TLS_CONNECTION_END ConnectionEnd; + EFI_TLS_VERIFY VerifyMethod; + EFI_TLS_SESSION_STATE SessionState; +} TLS_CONFIG_DATA; + +// +// Callback data for HTTP_PARSER_CALLBACK() +// +typedef struct { + UINTN ParseDataLength; + VOID *ParseData; + VOID *Wrap; +} HTTP_CALLBACK_DATA; + +typedef struct _HTTP_PROTOCOL { + UINT32 Signature; + EFI_HTTP_PROTOCOL Http; + EFI_HANDLE Handle; + HTTP_SERVICE *Service; + LIST_ENTRY Link; // Link to all HTTP instance from the service. + BOOLEAN InDestroy; + INTN State; + EFI_HTTP_METHOD Method; + + UINTN StatusCode; + + EFI_EVENT TimeoutEvent; + + EFI_HANDLE Tcp4ChildHandle; + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP4_CONFIG_DATA Tcp4CfgData; + EFI_TCP4_OPTION Tcp4Option; + + EFI_TCP4_CONNECTION_TOKEN Tcp4ConnToken; + BOOLEAN IsTcp4ConnDone; + EFI_TCP4_CLOSE_TOKEN Tcp4CloseToken; + BOOLEAN IsTcp4CloseDone; + CHAR8 *RemoteHost; + UINT16 RemotePort; + EFI_IPv4_ADDRESS RemoteAddr; + + EFI_HANDLE Tcp6ChildHandle; + EFI_TCP6_PROTOCOL *Tcp6; + EFI_TCP6_CONFIG_DATA Tcp6CfgData; + EFI_TCP6_OPTION Tcp6Option; + + EFI_TCP6_CONNECTION_TOKEN Tcp6ConnToken; + BOOLEAN IsTcp6ConnDone; + EFI_TCP6_CLOSE_TOKEN Tcp6CloseToken; + BOOLEAN IsTcp6CloseDone; + EFI_IPv6_ADDRESS RemoteIpv6Addr; + + // + // Rx4Token or Rx6Token used for receiving HTTP header. + // + EFI_TCP4_IO_TOKEN Rx4Token; + EFI_TCP4_RECEIVE_DATA Rx4Data; + EFI_TCP6_IO_TOKEN Rx6Token; + EFI_TCP6_RECEIVE_DATA Rx6Data; + BOOLEAN IsRxDone; + + CHAR8 **EndofHeader; + CHAR8 **HttpHeaders; + CHAR8 *CacheBody; + CHAR8 *NextMsg; + UINTN CacheLen; + UINTN CacheOffset; + + // + // HTTP message-body parser. + // + VOID *MsgParser; + HTTP_CALLBACK_DATA CallbackData; + + EFI_HTTP_VERSION HttpVersion; + UINT32 TimeOutMillisec; + BOOLEAN LocalAddressIsIPv6; + + EFI_HTTPv4_ACCESS_POINT IPv4Node; + EFI_HTTPv6_ACCESS_POINT Ipv6Node; + + NET_MAP TxTokens; + NET_MAP RxTokens; + + CHAR8 *Url; + + // + // Https Support + // + BOOLEAN UseHttps; + + EFI_SERVICE_BINDING_PROTOCOL *TlsSb; + EFI_HANDLE TlsChildHandle; /// Tls ChildHandle + TLS_CONFIG_DATA TlsConfigData; + EFI_TLS_PROTOCOL *Tls; + EFI_TLS_CONFIGURATION_PROTOCOL *TlsConfiguration; + EFI_TLS_SESSION_STATE TlsSessionState; + + // + // TlsTxData used for transmitting TLS related messages. + // + EFI_TCP4_IO_TOKEN Tcp4TlsTxToken; + EFI_TCP4_TRANSMIT_DATA Tcp4TlsTxData; + EFI_TCP6_IO_TOKEN Tcp6TlsTxToken; + EFI_TCP6_TRANSMIT_DATA Tcp6TlsTxData; + BOOLEAN TlsIsTxDone; + + // + // TlsRxData used for receiving TLS related messages. + // + EFI_TCP4_IO_TOKEN Tcp4TlsRxToken; + EFI_TCP4_RECEIVE_DATA Tcp4TlsRxData; + EFI_TCP6_IO_TOKEN Tcp6TlsRxToken; + EFI_TCP6_RECEIVE_DATA Tcp6TlsRxData; + BOOLEAN TlsIsRxDone; +} HTTP_PROTOCOL; + +typedef struct { + EFI_HTTP_TOKEN *HttpToken; + HTTP_PROTOCOL *HttpInstance; + HTTP_TCP_TOKEN_WRAP TcpWrap; +} HTTP_TOKEN_WRAP; + + +#define HTTP_PROTOCOL_SIGNATURE SIGNATURE_32('H', 't', 't', 'P') + +#define HTTP_INSTANCE_FROM_PROTOCOL(a) \ + CR ( \ + (a), \ + HTTP_PROTOCOL, \ + Http, \ + HTTP_PROTOCOL_SIGNATURE \ + ) + +/** + The common notify function used in HTTP driver. + + @param[in] Event The event signaled. + @param[in] Context The context. + +**/ +VOID +EFIAPI +HttpCommonNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Create events for the TCP connection token and TCP close token. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + + @retval EFI_SUCCESS The events are created successfully. + @retval others Other error as indicated. + +**/ +EFI_STATUS +HttpCreateTcpConnCloseEvent ( + IN HTTP_PROTOCOL *HttpInstance + ); + +/** + Close events in the TCP connection token and TCP close token. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + +**/ +VOID +HttpCloseTcpConnCloseEvent ( + IN HTTP_PROTOCOL *HttpInstance + ); + +/** + Create event for the TCP transmit token. + + @param[in] Wrap Point to HTTP token's wrap data. + + @retval EFI_SUCCESS The events is created successfully. + @retval others Other error as indicated. + +**/ +EFI_STATUS +HttpCreateTcpTxEvent ( + IN HTTP_TOKEN_WRAP *Wrap + ); + +/** + Create event for the TCP receive token which is used to receive HTTP header. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + + @retval EFI_SUCCESS The events is created successfully. + @retval others Other error as indicated. + +**/ +EFI_STATUS +HttpCreateTcpRxEventForHeader ( + IN HTTP_PROTOCOL *HttpInstance + ); + +/** + Create event for the TCP receive token which is used to receive HTTP body. + + @param[in] Wrap Point to HTTP token's wrap data. + + @retval EFI_SUCCESS The events is created successfully. + @retval others Other error as indicated. + +**/ +EFI_STATUS +HttpCreateTcpRxEvent ( + IN HTTP_TOKEN_WRAP *Wrap + ); + +/** + Close Events for Tcp Receive Tokens for HTTP body and HTTP header. + + @param[in] Wrap Pointer to HTTP token's wrap data. + +**/ +VOID +HttpCloseTcpRxEvent ( + IN HTTP_TOKEN_WRAP *Wrap + ); + +/** + Intiialize the HTTP_PROTOCOL structure to the unconfigured state. + + @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[in] IpVersion Indicate us TCP4 protocol or TCP6 protocol. + + @retval EFI_SUCCESS HTTP_PROTOCOL structure is initialized successfully. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpInitProtocol ( + IN OUT HTTP_PROTOCOL *HttpInstance, + IN BOOLEAN IpVersion + ); + +/** + Clean up the HTTP child, release all the resources used by it. + + @param[in] HttpInstance The HTTP child to clean up. + +**/ +VOID +HttpCleanProtocol ( + IN HTTP_PROTOCOL *HttpInstance + ); + +/** + Establish TCP connection with HTTP server. + + @param[in] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS The TCP connection is established. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpCreateConnection ( + IN HTTP_PROTOCOL *HttpInstance + ); + +/** + Close existing TCP connection. + + @param[in] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS The TCP connection is closed. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpCloseConnection ( + IN HTTP_PROTOCOL *HttpInstance + ); + +/** + Configure TCP4 protocol child. + + @param[in] HttpInstance The HTTP instance private data. + @param[in] Wrap The HTTP token's wrap data. + + @retval EFI_SUCCESS The TCP4 protocol child is configured. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpConfigureTcp4 ( + IN HTTP_PROTOCOL *HttpInstance, + IN HTTP_TOKEN_WRAP *Wrap + ); + +/** + Configure TCP6 protocol child. + + @param[in] HttpInstance The HTTP instance private data. + @param[in] Wrap The HTTP token's wrap data. + + @retval EFI_SUCCESS The TCP6 protocol child is configured. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpConfigureTcp6 ( + IN HTTP_PROTOCOL *HttpInstance, + IN HTTP_TOKEN_WRAP *Wrap + ); + +/** + Check existing TCP connection, if in error state, recover TCP4 connection. Then, + connect one TLS session if required. + + @param[in] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS The TCP connection is established. + @retval EFI_NOT_READY TCP4 protocol child is not created or configured. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpConnectTcp4 ( + IN HTTP_PROTOCOL *HttpInstance + ); + +/** + Check existing TCP connection, if in error state, recover TCP6 connection. Then, + connect one TLS session if required. + + @param[in] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS The TCP connection is established. + @retval EFI_NOT_READY TCP6 protocol child is not created or configured. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpConnectTcp6 ( + IN HTTP_PROTOCOL *HttpInstance + ); + +/** + Send the HTTP or HTTPS message through TCP4 or TCP6. + + @param[in] HttpInstance The HTTP instance private data. + @param[in] Wrap The HTTP token's wrap data. + @param[in] TxString Buffer containing the HTTP message string. + @param[in] TxStringLen Length of the HTTP message string in bytes. + + @retval EFI_SUCCESS The HTTP message is queued into TCP transmit queue. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpTransmitTcp ( + IN HTTP_PROTOCOL *HttpInstance, + IN HTTP_TOKEN_WRAP *Wrap, + IN UINT8 *TxString, + IN UINTN TxStringLen + ); + +/** + Check whether the user's token or event has already + been enqueue on HTTP Tx or Rx Token list. + + @param[in] Map The container of either user's transmit or receive + token. + @param[in] Item Current item to check against. + @param[in] Context The Token to check againist. + + @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP + @retval EFI_SUCCESS The current item isn't the same token/event as the + context. + +**/ +EFI_STATUS +EFIAPI +HttpTokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ); + +/** + Check whether the HTTP message associated with TxToken or Tx6Token is already sent out. + + @param[in] Map The container of TxToken. + @param[in] Item Current item to check against. + @param[in] Context The Token to check againist. + + @retval EFI_NOT_READY The HTTP message is still queued in the list. + @retval EFI_SUCCESS The HTTP message has been sent out. + +**/ +EFI_STATUS +EFIAPI +HttpTcpNotReady ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ); + +/** + Initialize Http session. + + @param[in] HttpInstance The HTTP instance private data. + @param[in] Wrap The HTTP token's wrap data. + @param[in] Configure The Flag indicates whether need to initialize session. + @param[in] TlsConfigure The Flag indicates whether it's the new Tls session. + + @retval EFI_SUCCESS The initialization of session is done. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpInitSession ( + IN HTTP_PROTOCOL *HttpInstance, + IN HTTP_TOKEN_WRAP *Wrap, + IN BOOLEAN Configure, + IN BOOLEAN TlsConfigure + ); + +/** + Transmit the HTTP or HTTPS mssage by processing the associated HTTP token. + + @param[in] Map The container of TxToken or Tx6Token. + @param[in] Item Current item to check against. + @param[in] Context The Token to check againist. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_SUCCESS The HTTP message is queued into TCP transmit + queue. + +**/ +EFI_STATUS +EFIAPI +HttpTcpTransmit ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ); + +/** + Receive the HTTP response by processing the associated HTTP token. + + @param[in] Map The container of Rx4Token or Rx6Token. + @param[in] Item Current item to check against. + @param[in] Context The Token to check againist. + + @retval EFI_SUCCESS The HTTP response is queued into TCP receive + queue. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +HttpTcpReceive ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ); + +/** + Receive the HTTP header by processing the associated HTTP token. + + @param[in] HttpInstance The HTTP instance private data. + @param[in, out] SizeofHeaders The HTTP header length. + @param[in, out] BufferSize The size of buffer to cacahe the header message. + @param[in] Timeout The time to wait for receiving the header packet. + + @retval EFI_SUCCESS The HTTP header is received. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +HttpTcpReceiveHeader ( + IN HTTP_PROTOCOL *HttpInstance, + IN OUT UINTN *SizeofHeaders, + IN OUT UINTN *BufferSize, + IN EFI_EVENT Timeout + ); + +/** + Receive the HTTP body by processing the associated HTTP token. + + @param[in] Wrap The HTTP token's wrap data. + @param[in] HttpMsg The HTTP message data. + + @retval EFI_SUCCESS The HTTP body is received. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +HttpTcpReceiveBody ( + IN HTTP_TOKEN_WRAP *Wrap, + IN EFI_HTTP_MESSAGE *HttpMsg + ); + +/** + Clean up Tcp Tokens while the Tcp transmission error occurs. + + @param[in] Wrap Pointer to HTTP token's wrap data. + +**/ +VOID +HttpTcpTokenCleanup ( + IN HTTP_TOKEN_WRAP *Wrap + ); + +/** + The work function of EfiHttpResponse(). + + @param[in] Wrap Pointer to HTTP token's wrap data. + + @retval EFI_SUCCESS Allocation succeeded. + @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources. + @retval EFI_NOT_READY Can't find a corresponding TxToken. + +**/ +EFI_STATUS +HttpResponseWorker ( + IN HTTP_TOKEN_WRAP *Wrap + ); + +#endif diff --git a/NetworkPkg/HttpDxe/HttpsSupport.c b/NetworkPkg/HttpDxe/HttpsSupport.c new file mode 100644 index 000000000..988bbcbce --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpsSupport.c @@ -0,0 +1,1877 @@ +/** @file + Miscellaneous routines specific to Https for HttpDxe driver. + +Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpDriver.h" + +/** + Returns the first occurrence of a Null-terminated ASCII sub-string in a Null-terminated + ASCII string and ignore case during the search process. + + This function scans the contents of the ASCII string specified by String + and returns the first occurrence of SearchString and ignore case during the search process. + If SearchString is not found in String, then NULL is returned. If the length of SearchString + is zero, then String is returned. + + If String is NULL, then ASSERT(). + If SearchString is NULL, then ASSERT(). + + @param[in] String A pointer to a Null-terminated ASCII string. + @param[in] SearchString A pointer to a Null-terminated ASCII string to search for. + + @retval NULL If the SearchString does not appear in String. + @retval others If there is a match return the first occurrence of SearchingString. + If the length of SearchString is zero,return String. + +**/ +CHAR8 * +AsciiStrCaseStr ( + IN CONST CHAR8 *String, + IN CONST CHAR8 *SearchString + ) +{ + CONST CHAR8 *FirstMatch; + CONST CHAR8 *SearchStringTmp; + + CHAR8 Src; + CHAR8 Dst; + + // + // ASSERT both strings are less long than PcdMaximumAsciiStringLength + // + ASSERT (AsciiStrSize (String) != 0); + ASSERT (AsciiStrSize (SearchString) != 0); + + if (*SearchString == '\0') { + return (CHAR8 *) String; + } + + while (*String != '\0') { + SearchStringTmp = SearchString; + FirstMatch = String; + + while ((*SearchStringTmp != '\0') + && (*String != '\0')) { + Src = *String; + Dst = *SearchStringTmp; + + if ((Src >= 'A') && (Src <= 'Z')) { + Src += ('a' - 'A'); + } + + if ((Dst >= 'A') && (Dst <= 'Z')) { + Dst += ('a' - 'A'); + } + + if (Src != Dst) { + break; + } + + String++; + SearchStringTmp++; + } + + if (*SearchStringTmp == '\0') { + return (CHAR8 *) FirstMatch; + } + + String = FirstMatch + 1; + } + + return NULL; +} + +/** + The callback function to free the net buffer list. + + @param[in] Arg The opaque parameter. + +**/ +VOID +EFIAPI +FreeNbufList ( + IN VOID *Arg + ) +{ + ASSERT (Arg != NULL); + + NetbufFreeList ((LIST_ENTRY *) Arg); + FreePool (Arg); +} + +/** + Check whether the Url is from Https. + + @param[in] Url The pointer to a HTTP or HTTPS URL string. + + @retval TRUE The Url is from HTTPS. + @retval FALSE The Url is from HTTP. + +**/ +BOOLEAN +IsHttpsUrl ( + IN CHAR8 *Url + ) +{ + CHAR8 *Tmp; + + Tmp = NULL; + + Tmp = AsciiStrCaseStr (Url, HTTPS_FLAG); + if (Tmp != NULL && Tmp == Url) { + return TRUE; + } + + return FALSE; +} + +/** + Creates a Tls child handle, open EFI_TLS_PROTOCOL and EFI_TLS_CONFIGURATION_PROTOCOL. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[out] TlsSb Pointer to the TLS SERVICE_BINDING_PROTOCOL. + @param[out] TlsProto Pointer to the EFI_TLS_PROTOCOL instance. + @param[out] TlsConfiguration Pointer to the EFI_TLS_CONFIGURATION_PROTOCOL instance. + + @return The child handle with opened EFI_TLS_PROTOCOL and EFI_TLS_CONFIGURATION_PROTOCOL. + +**/ +EFI_HANDLE +EFIAPI +TlsCreateChild ( + IN EFI_HANDLE ImageHandle, + OUT EFI_SERVICE_BINDING_PROTOCOL **TlsSb, + OUT EFI_TLS_PROTOCOL **TlsProto, + OUT EFI_TLS_CONFIGURATION_PROTOCOL **TlsConfiguration + ) +{ + EFI_STATUS Status; + EFI_HANDLE TlsChildHandle; + + TlsChildHandle = 0; + + // + // Locate TlsServiceBinding protocol. + // + gBS->LocateProtocol ( + &gEfiTlsServiceBindingProtocolGuid, + NULL, + (VOID **) TlsSb + ); + if (*TlsSb == NULL) { + return NULL; + } + + Status = (*TlsSb)->CreateChild (*TlsSb, &TlsChildHandle); + if (EFI_ERROR (Status)) { + return NULL; + } + + Status = gBS->OpenProtocol ( + TlsChildHandle, + &gEfiTlsProtocolGuid, + (VOID **) TlsProto, + ImageHandle, + TlsChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + (*TlsSb)->DestroyChild (*TlsSb, TlsChildHandle); + return NULL; + } + + Status = gBS->OpenProtocol ( + TlsChildHandle, + &gEfiTlsConfigurationProtocolGuid, + (VOID **) TlsConfiguration, + ImageHandle, + TlsChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + (*TlsSb)->DestroyChild (*TlsSb, TlsChildHandle); + return NULL; + } + + return TlsChildHandle; +} + +/** + Create event for the TLS receive and transmit tokens which are used to receive and + transmit TLS related messages. + + @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure. + + @retval EFI_SUCCESS The events are created successfully. + @retval others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsCreateTxRxEvent ( + IN OUT HTTP_PROTOCOL *HttpInstance + ) +{ + EFI_STATUS Status; + + if (!HttpInstance->LocalAddressIsIPv6) { + // + // For Tcp4TlsTxToken. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &HttpInstance->TlsIsTxDone, + &HttpInstance->Tcp4TlsTxToken.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + HttpInstance->Tcp4TlsTxData.Push = TRUE; + HttpInstance->Tcp4TlsTxData.Urgent = FALSE; + HttpInstance->Tcp4TlsTxData.DataLength = 0; + HttpInstance->Tcp4TlsTxData.FragmentCount = 1; + HttpInstance->Tcp4TlsTxData.FragmentTable[0].FragmentLength = HttpInstance->Tcp4TlsTxData.DataLength; + HttpInstance->Tcp4TlsTxData.FragmentTable[0].FragmentBuffer = NULL; + HttpInstance->Tcp4TlsTxToken.Packet.TxData = &HttpInstance->Tcp4TlsTxData; + HttpInstance->Tcp4TlsTxToken.CompletionToken.Status = EFI_NOT_READY; + + // + // For Tcp4TlsRxToken. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &HttpInstance->TlsIsRxDone, + &HttpInstance->Tcp4TlsRxToken.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + HttpInstance->Tcp4TlsRxData.DataLength = 0; + HttpInstance->Tcp4TlsRxData.FragmentCount = 1; + HttpInstance->Tcp4TlsRxData.FragmentTable[0].FragmentLength = HttpInstance->Tcp4TlsRxData.DataLength ; + HttpInstance->Tcp4TlsRxData.FragmentTable[0].FragmentBuffer = NULL; + HttpInstance->Tcp4TlsRxToken.Packet.RxData = &HttpInstance->Tcp4TlsRxData; + HttpInstance->Tcp4TlsRxToken.CompletionToken.Status = EFI_NOT_READY; + } else { + // + // For Tcp6TlsTxToken. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &HttpInstance->TlsIsTxDone, + &HttpInstance->Tcp6TlsTxToken.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + HttpInstance->Tcp6TlsTxData.Push = TRUE; + HttpInstance->Tcp6TlsTxData.Urgent = FALSE; + HttpInstance->Tcp6TlsTxData.DataLength = 0; + HttpInstance->Tcp6TlsTxData.FragmentCount = 1; + HttpInstance->Tcp6TlsTxData.FragmentTable[0].FragmentLength = HttpInstance->Tcp6TlsTxData.DataLength; + HttpInstance->Tcp6TlsTxData.FragmentTable[0].FragmentBuffer = NULL; + HttpInstance->Tcp6TlsTxToken.Packet.TxData = &HttpInstance->Tcp6TlsTxData; + HttpInstance->Tcp6TlsTxToken.CompletionToken.Status = EFI_NOT_READY; + + // + // For Tcp6TlsRxToken. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + HttpCommonNotify, + &HttpInstance->TlsIsRxDone, + &HttpInstance->Tcp6TlsRxToken.CompletionToken.Event + ); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + HttpInstance->Tcp6TlsRxData.DataLength = 0; + HttpInstance->Tcp6TlsRxData.FragmentCount = 1; + HttpInstance->Tcp6TlsRxData.FragmentTable[0].FragmentLength = HttpInstance->Tcp6TlsRxData.DataLength ; + HttpInstance->Tcp6TlsRxData.FragmentTable[0].FragmentBuffer = NULL; + HttpInstance->Tcp6TlsRxToken.Packet.RxData = &HttpInstance->Tcp6TlsRxData; + HttpInstance->Tcp6TlsRxToken.CompletionToken.Status = EFI_NOT_READY; + } + + return Status; + +ERROR: + // + // Error handling + // + TlsCloseTxRxEvent (HttpInstance); + + return Status; +} + +/** + Close events in the TlsTxToken and TlsRxToken. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + +**/ +VOID +EFIAPI +TlsCloseTxRxEvent ( + IN HTTP_PROTOCOL *HttpInstance + ) +{ + ASSERT (HttpInstance != NULL); + if (!HttpInstance->LocalAddressIsIPv6) { + if (NULL != HttpInstance->Tcp4TlsTxToken.CompletionToken.Event) { + gBS->CloseEvent(HttpInstance->Tcp4TlsTxToken.CompletionToken.Event); + HttpInstance->Tcp4TlsTxToken.CompletionToken.Event = NULL; + } + + if (NULL != HttpInstance->Tcp4TlsRxToken.CompletionToken.Event) { + gBS->CloseEvent (HttpInstance->Tcp4TlsRxToken.CompletionToken.Event); + HttpInstance->Tcp4TlsRxToken.CompletionToken.Event = NULL; + } + } else { + if (NULL != HttpInstance->Tcp6TlsTxToken.CompletionToken.Event) { + gBS->CloseEvent(HttpInstance->Tcp6TlsTxToken.CompletionToken.Event); + HttpInstance->Tcp6TlsTxToken.CompletionToken.Event = NULL; + } + + if (NULL != HttpInstance->Tcp6TlsRxToken.CompletionToken.Event) { + gBS->CloseEvent (HttpInstance->Tcp6TlsRxToken.CompletionToken.Event); + HttpInstance->Tcp6TlsRxToken.CompletionToken.Event = NULL; + } + } +} + +/** + Read the TlsCaCertificate variable and configure it. + + @param[in, out] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS TlsCaCertificate is configured. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_NOT_FOUND Fail to get 'TlsCaCertificate' variable. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +TlsConfigCertificate ( + IN OUT HTTP_PROTOCOL *HttpInstance + ) +{ + EFI_STATUS Status; + UINT8 *CACert; + UINTN CACertSize; + UINT32 Index; + EFI_SIGNATURE_LIST *CertList; + EFI_SIGNATURE_DATA *Cert; + UINTN CertArraySizeInBytes; + UINTN CertCount; + UINT32 ItemDataSize; + + CACert = NULL; + CACertSize = 0; + + // + // Try to read the TlsCaCertificate variable. + // + Status = gRT->GetVariable ( + EFI_TLS_CA_CERTIFICATE_VARIABLE, + &gEfiTlsCaCertificateGuid, + NULL, + &CACertSize, + NULL + ); + + if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) { + return Status; + } + + // + // Allocate buffer and read the config variable. + // + CACert = AllocatePool (CACertSize); + if (CACert == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = gRT->GetVariable ( + EFI_TLS_CA_CERTIFICATE_VARIABLE, + &gEfiTlsCaCertificateGuid, + NULL, + &CACertSize, + CACert + ); + if (EFI_ERROR (Status)) { + // + // GetVariable still error or the variable is corrupted. + // + goto FreeCACert; + } + + ASSERT (CACert != NULL); + + // + // Sanity check + // + Status = EFI_INVALID_PARAMETER; + CertCount = 0; + ItemDataSize = (UINT32) CACertSize; + while (ItemDataSize > 0) { + if (ItemDataSize < sizeof (EFI_SIGNATURE_LIST)) { + DEBUG ((DEBUG_ERROR, "%a: truncated EFI_SIGNATURE_LIST header\n", + __FUNCTION__)); + goto FreeCACert; + } + + CertList = (EFI_SIGNATURE_LIST *) (CACert + (CACertSize - ItemDataSize)); + + if (CertList->SignatureListSize < sizeof (EFI_SIGNATURE_LIST)) { + DEBUG ((DEBUG_ERROR, + "%a: SignatureListSize too small for EFI_SIGNATURE_LIST\n", + __FUNCTION__)); + goto FreeCACert; + } + + if (CertList->SignatureListSize > ItemDataSize) { + DEBUG ((DEBUG_ERROR, "%a: truncated EFI_SIGNATURE_LIST body\n", + __FUNCTION__)); + goto FreeCACert; + } + + if (!CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) { + DEBUG ((DEBUG_ERROR, "%a: only X509 certificates are supported\n", + __FUNCTION__)); + Status = EFI_UNSUPPORTED; + goto FreeCACert; + } + + if (CertList->SignatureHeaderSize != 0) { + DEBUG ((DEBUG_ERROR, "%a: SignatureHeaderSize must be 0 for X509\n", + __FUNCTION__)); + goto FreeCACert; + } + + if (CertList->SignatureSize < sizeof (EFI_SIGNATURE_DATA)) { + DEBUG ((DEBUG_ERROR, + "%a: SignatureSize too small for EFI_SIGNATURE_DATA\n", __FUNCTION__)); + goto FreeCACert; + } + + CertArraySizeInBytes = (CertList->SignatureListSize - + sizeof (EFI_SIGNATURE_LIST)); + if (CertArraySizeInBytes % CertList->SignatureSize != 0) { + DEBUG ((DEBUG_ERROR, + "%a: EFI_SIGNATURE_DATA array not a multiple of SignatureSize\n", + __FUNCTION__)); + goto FreeCACert; + } + + CertCount += CertArraySizeInBytes / CertList->SignatureSize; + ItemDataSize -= CertList->SignatureListSize; + } + if (CertCount == 0) { + DEBUG ((DEBUG_ERROR, "%a: no X509 certificates provided\n", __FUNCTION__)); + goto FreeCACert; + } + + // + // Enumerate all data and erasing the target item. + // + ItemDataSize = (UINT32) CACertSize; + CertList = (EFI_SIGNATURE_LIST *) CACert; + while ((ItemDataSize > 0) && (ItemDataSize >= CertList->SignatureListSize)) { + Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize); + CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize; + for (Index = 0; Index < CertCount; Index++) { + // + // EfiTlsConfigDataTypeCACertificate + // + Status = HttpInstance->TlsConfiguration->SetData ( + HttpInstance->TlsConfiguration, + EfiTlsConfigDataTypeCACertificate, + Cert->SignatureData, + CertList->SignatureSize - sizeof (Cert->SignatureOwner) + ); + if (EFI_ERROR (Status)) { + goto FreeCACert; + } + + Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize); + } + + ItemDataSize -= CertList->SignatureListSize; + CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize); + } + +FreeCACert: + FreePool (CACert); + return Status; +} + +/** + Read the HttpTlsCipherList variable and configure it for HTTPS session. + + @param[in, out] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS The prefered HTTP TLS CipherList is configured. + @retval EFI_NOT_FOUND Fail to get 'HttpTlsCipherList' variable. + @retval EFI_INVALID_PARAMETER The contents of variable are invalid. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + + @retval Others Other error as indicated. + +**/ +EFI_STATUS +TlsConfigCipherList ( + IN OUT HTTP_PROTOCOL *HttpInstance + ) +{ + EFI_STATUS Status; + UINT8 *CipherList; + UINTN CipherListSize; + + CipherList = NULL; + CipherListSize = 0; + + // + // Try to read the HttpTlsCipherList variable. + // + Status = gRT->GetVariable ( + EDKII_HTTP_TLS_CIPHER_LIST_VARIABLE, + &gEdkiiHttpTlsCipherListGuid, + NULL, + &CipherListSize, + NULL + ); + ASSERT (EFI_ERROR (Status)); + if (Status != EFI_BUFFER_TOO_SMALL) { + return Status; + } + + if (CipherListSize % sizeof (EFI_TLS_CIPHER) != 0) { + return EFI_INVALID_PARAMETER; + } + + // + // Allocate buffer and read the config variable. + // + CipherList = AllocatePool (CipherListSize); + if (CipherList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = gRT->GetVariable ( + EDKII_HTTP_TLS_CIPHER_LIST_VARIABLE, + &gEdkiiHttpTlsCipherListGuid, + NULL, + &CipherListSize, + CipherList + ); + if (EFI_ERROR (Status)) { + // + // GetVariable still error or the variable is corrupted. + // + goto ON_EXIT; + } + + ASSERT (CipherList != NULL); + + Status = HttpInstance->Tls->SetSessionData ( + HttpInstance->Tls, + EfiTlsCipherList, + CipherList, + CipherListSize + ); + +ON_EXIT: + FreePool (CipherList); + + return Status; +} + +/** + Configure TLS session data. + + @param[in, out] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS TLS session data is configured. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsConfigureSession ( + IN OUT HTTP_PROTOCOL *HttpInstance + ) +{ + EFI_STATUS Status; + + // + // TlsConfigData initialization + // + HttpInstance->TlsConfigData.ConnectionEnd = EfiTlsClient; + HttpInstance->TlsConfigData.VerifyMethod = EFI_TLS_VERIFY_PEER; + HttpInstance->TlsConfigData.SessionState = EfiTlsSessionNotStarted; + + // + // EfiTlsConnectionEnd, + // EfiTlsVerifyMethod + // EfiTlsSessionState + // + Status = HttpInstance->Tls->SetSessionData ( + HttpInstance->Tls, + EfiTlsConnectionEnd, + &(HttpInstance->TlsConfigData.ConnectionEnd), + sizeof (EFI_TLS_CONNECTION_END) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = HttpInstance->Tls->SetSessionData ( + HttpInstance->Tls, + EfiTlsVerifyMethod, + &HttpInstance->TlsConfigData.VerifyMethod, + sizeof (EFI_TLS_VERIFY) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = HttpInstance->Tls->SetSessionData ( + HttpInstance->Tls, + EfiTlsSessionState, + &(HttpInstance->TlsConfigData.SessionState), + sizeof (EFI_TLS_SESSION_STATE) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Tls Cipher List + // + Status = TlsConfigCipherList (HttpInstance); + if (EFI_ERROR (Status) && Status != EFI_NOT_FOUND) { + DEBUG ((EFI_D_ERROR, "TlsConfigCipherList: return %r error.\n", Status)); + return Status; + } + + // + // Tls Config Certificate + // + Status = TlsConfigCertificate (HttpInstance); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "TLS Certificate Config Error!\n")); + return Status; + } + + // + // TlsCreateTxRxEvent + // + Status = TlsCreateTxRxEvent (HttpInstance); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + return Status; + +ERROR: + TlsCloseTxRxEvent (HttpInstance); + + return Status; +} + +/** + Transmit the Packet by processing the associated HTTPS token. + + @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[in] Packet The packet to transmit. + + @retval EFI_SUCCESS The packet is transmitted. + @retval EFI_INVALID_PARAMETER HttpInstance is NULL or Packet is NULL. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsCommonTransmit ( + IN OUT HTTP_PROTOCOL *HttpInstance, + IN NET_BUF *Packet + ) +{ + EFI_STATUS Status; + VOID *Data; + UINTN Size; + + if ((HttpInstance == NULL) || (Packet == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (!HttpInstance->LocalAddressIsIPv6) { + Size = sizeof (EFI_TCP4_TRANSMIT_DATA) + + (Packet->BlockOpNum - 1) * sizeof (EFI_TCP4_FRAGMENT_DATA); + } else { + Size = sizeof (EFI_TCP6_TRANSMIT_DATA) + + (Packet->BlockOpNum - 1) * sizeof (EFI_TCP6_FRAGMENT_DATA); + } + + Data = AllocatePool (Size); + if (Data == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if (!HttpInstance->LocalAddressIsIPv6) { + ((EFI_TCP4_TRANSMIT_DATA *) Data)->Push = TRUE; + ((EFI_TCP4_TRANSMIT_DATA *) Data)->Urgent = FALSE; + ((EFI_TCP4_TRANSMIT_DATA *) Data)->DataLength = Packet->TotalSize; + + // + // Build the fragment table. + // + ((EFI_TCP4_TRANSMIT_DATA *) Data)->FragmentCount = Packet->BlockOpNum; + + NetbufBuildExt ( + Packet, + (NET_FRAGMENT *) &((EFI_TCP4_TRANSMIT_DATA *) Data)->FragmentTable[0], + &((EFI_TCP4_TRANSMIT_DATA *) Data)->FragmentCount + ); + + HttpInstance->Tcp4TlsTxToken.Packet.TxData = (EFI_TCP4_TRANSMIT_DATA *) Data; + + Status = EFI_DEVICE_ERROR; + + // + // Transmit the packet. + // + Status = HttpInstance->Tcp4->Transmit (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsTxToken); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + while (!HttpInstance->TlsIsTxDone) { + HttpInstance->Tcp4->Poll (HttpInstance->Tcp4); + } + + HttpInstance->TlsIsTxDone = FALSE; + Status = HttpInstance->Tcp4TlsTxToken.CompletionToken.Status; + } else { + ((EFI_TCP6_TRANSMIT_DATA *) Data)->Push = TRUE; + ((EFI_TCP6_TRANSMIT_DATA *) Data)->Urgent = FALSE; + ((EFI_TCP6_TRANSMIT_DATA *) Data)->DataLength = Packet->TotalSize; + + // + // Build the fragment table. + // + ((EFI_TCP6_TRANSMIT_DATA *) Data)->FragmentCount = Packet->BlockOpNum; + + NetbufBuildExt ( + Packet, + (NET_FRAGMENT *) &((EFI_TCP6_TRANSMIT_DATA *) Data)->FragmentTable[0], + &((EFI_TCP6_TRANSMIT_DATA *) Data)->FragmentCount + ); + + HttpInstance->Tcp6TlsTxToken.Packet.TxData = (EFI_TCP6_TRANSMIT_DATA *) Data; + + Status = EFI_DEVICE_ERROR; + + // + // Transmit the packet. + // + Status = HttpInstance->Tcp6->Transmit (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsTxToken); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + while (!HttpInstance->TlsIsTxDone) { + HttpInstance->Tcp6->Poll (HttpInstance->Tcp6); + } + + HttpInstance->TlsIsTxDone = FALSE; + Status = HttpInstance->Tcp6TlsTxToken.CompletionToken.Status; + } + +ON_EXIT: + FreePool (Data); + + return Status; +} + +/** + Receive the Packet by processing the associated HTTPS token. + + @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[in] Packet The packet to transmit. + @param[in] Timeout The time to wait for connection done. + + @retval EFI_SUCCESS The Packet is received. + @retval EFI_INVALID_PARAMETER HttpInstance is NULL or Packet is NULL. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_TIMEOUT The operation is time out. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsCommonReceive ( + IN OUT HTTP_PROTOCOL *HttpInstance, + IN NET_BUF *Packet, + IN EFI_EVENT Timeout + ) +{ + EFI_TCP4_RECEIVE_DATA *Tcp4RxData; + EFI_TCP6_RECEIVE_DATA *Tcp6RxData; + EFI_STATUS Status; + NET_FRAGMENT *Fragment; + UINT32 FragmentCount; + UINT32 CurrentFragment; + + Tcp4RxData = NULL; + Tcp6RxData = NULL; + + if ((HttpInstance == NULL) || (Packet == NULL)) { + return EFI_INVALID_PARAMETER; + } + + FragmentCount = Packet->BlockOpNum; + Fragment = AllocatePool (FragmentCount * sizeof (NET_FRAGMENT)); + if (Fragment == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Build the fragment table. + // + NetbufBuildExt (Packet, Fragment, &FragmentCount); + + if (!HttpInstance->LocalAddressIsIPv6) { + Tcp4RxData = HttpInstance->Tcp4TlsRxToken.Packet.RxData; + if (Tcp4RxData == NULL) { + return EFI_INVALID_PARAMETER; + } + Tcp4RxData->FragmentCount = 1; + } else { + Tcp6RxData = HttpInstance->Tcp6TlsRxToken.Packet.RxData; + if (Tcp6RxData == NULL) { + return EFI_INVALID_PARAMETER; + } + Tcp6RxData->FragmentCount = 1; + } + + CurrentFragment = 0; + Status = EFI_SUCCESS; + + while (CurrentFragment < FragmentCount) { + if (!HttpInstance->LocalAddressIsIPv6) { + Tcp4RxData->DataLength = Fragment[CurrentFragment].Len; + Tcp4RxData->FragmentTable[0].FragmentLength = Fragment[CurrentFragment].Len; + Tcp4RxData->FragmentTable[0].FragmentBuffer = Fragment[CurrentFragment].Bulk; + Status = HttpInstance->Tcp4->Receive (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsRxToken); + } else { + Tcp6RxData->DataLength = Fragment[CurrentFragment].Len; + Tcp6RxData->FragmentTable[0].FragmentLength = Fragment[CurrentFragment].Len; + Tcp6RxData->FragmentTable[0].FragmentBuffer = Fragment[CurrentFragment].Bulk; + Status = HttpInstance->Tcp6->Receive (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsRxToken); + } + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + while (!HttpInstance->TlsIsRxDone && ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) { + // + // Poll until some data is received or an error occurs. + // + if (!HttpInstance->LocalAddressIsIPv6) { + HttpInstance->Tcp4->Poll (HttpInstance->Tcp4); + } else { + HttpInstance->Tcp6->Poll (HttpInstance->Tcp6); + } + } + + if (!HttpInstance->TlsIsRxDone) { + // + // Timeout occurs, cancel the receive request. + // + if (!HttpInstance->LocalAddressIsIPv6) { + HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsRxToken.CompletionToken); + } else { + HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsRxToken.CompletionToken); + } + + Status = EFI_TIMEOUT; + goto ON_EXIT; + } else { + HttpInstance->TlsIsRxDone = FALSE; + } + + if (!HttpInstance->LocalAddressIsIPv6) { + Status = HttpInstance->Tcp4TlsRxToken.CompletionToken.Status; + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Fragment[CurrentFragment].Len -= Tcp4RxData->FragmentTable[0].FragmentLength; + if (Fragment[CurrentFragment].Len == 0) { + CurrentFragment++; + } else { + Fragment[CurrentFragment].Bulk += Tcp4RxData->FragmentTable[0].FragmentLength; + } + } else { + Status = HttpInstance->Tcp6TlsRxToken.CompletionToken.Status; + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Fragment[CurrentFragment].Len -= Tcp6RxData->FragmentTable[0].FragmentLength; + if (Fragment[CurrentFragment].Len == 0) { + CurrentFragment++; + } else { + Fragment[CurrentFragment].Bulk += Tcp6RxData->FragmentTable[0].FragmentLength; + } + } + } + +ON_EXIT: + + if (Fragment != NULL) { + FreePool (Fragment); + } + + return Status; +} + +/** + Receive one TLS PDU. An TLS PDU contains an TLS record header and it's + corresponding record data. These two parts will be put into two blocks of buffers in the + net buffer. + + @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[out] Pdu The received TLS PDU. + @param[in] Timeout The time to wait for connection done. + + @retval EFI_SUCCESS An TLS PDU is received. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_PROTOCOL_ERROR An unexpected TLS packet was received. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsReceiveOnePdu ( + IN OUT HTTP_PROTOCOL *HttpInstance, + OUT NET_BUF **Pdu, + IN EFI_EVENT Timeout + ) +{ + EFI_STATUS Status; + + LIST_ENTRY *NbufList; + + UINT32 Len; + + NET_BUF *PduHdr; + UINT8 *Header; + TLS_RECORD_HEADER RecordHeader; + + NET_BUF *DataSeg; + + NbufList = NULL; + PduHdr = NULL; + Header = NULL; + DataSeg = NULL; + + NbufList = AllocatePool (sizeof (LIST_ENTRY)); + if (NbufList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + InitializeListHead (NbufList); + + // + // Allocate buffer to receive one TLS header. + // + Len = TLS_RECORD_HEADER_LENGTH; + PduHdr = NetbufAlloc (Len); + if (PduHdr == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Header = NetbufAllocSpace (PduHdr, Len, NET_BUF_TAIL); + if (Header == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // First step, receive one TLS header. + // + Status = TlsCommonReceive (HttpInstance, PduHdr, Timeout); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + RecordHeader = *(TLS_RECORD_HEADER *) Header; + if ((RecordHeader.ContentType == TlsContentTypeHandshake || + RecordHeader.ContentType == TlsContentTypeAlert || + RecordHeader.ContentType == TlsContentTypeChangeCipherSpec || + RecordHeader.ContentType == TlsContentTypeApplicationData) && + (RecordHeader.Version.Major == 0x03) && /// Major versions are same. + (RecordHeader.Version.Minor == TLS10_PROTOCOL_VERSION_MINOR || + RecordHeader.Version.Minor ==TLS11_PROTOCOL_VERSION_MINOR || + RecordHeader.Version.Minor == TLS12_PROTOCOL_VERSION_MINOR) + ) { + InsertTailList (NbufList, &PduHdr->List); + } else { + Status = EFI_PROTOCOL_ERROR; + goto ON_EXIT; + } + + Len = SwapBytes16(RecordHeader.Length); + if (Len == 0) { + // + // No TLS payload. + // + goto FORM_PDU; + } + + // + // Allocate buffer to receive one TLS payload. + // + DataSeg = NetbufAlloc (Len); + if (DataSeg == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + NetbufAllocSpace (DataSeg, Len, NET_BUF_TAIL); + + // + // Second step, receive one TLS payload. + // + Status = TlsCommonReceive (HttpInstance, DataSeg, Timeout); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + InsertTailList (NbufList, &DataSeg->List); + +FORM_PDU: + // + // Form the PDU from a list of PDU. + // + *Pdu = NetbufFromBufList (NbufList, 0, 0, FreeNbufList, NbufList); + if (*Pdu == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } + +ON_EXIT: + + if (EFI_ERROR (Status)) { + // + // Free the Nbufs in this NbufList and the NbufList itself. + // + FreeNbufList (NbufList); + } + + return Status; +} + +/** + Connect one TLS session by finishing the TLS handshake process. + + @param[in] HttpInstance The HTTP instance private data. + @param[in] Timeout The time to wait for connection done. + + @retval EFI_SUCCESS The TLS session is established. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_ABORTED TLS session state is incorrect. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsConnectSession ( + IN HTTP_PROTOCOL *HttpInstance, + IN EFI_EVENT Timeout + ) +{ + EFI_STATUS Status; + UINT8 *BufferOut; + UINTN BufferOutSize; + NET_BUF *PacketOut; + UINT8 *DataOut; + NET_BUF *Pdu; + UINT8 *BufferIn; + UINTN BufferInSize; + UINT8 *GetSessionDataBuffer; + UINTN GetSessionDataBufferSize; + + BufferOut = NULL; + PacketOut = NULL; + DataOut = NULL; + Pdu = NULL; + BufferIn = NULL; + + // + // Initialize TLS state. + // + HttpInstance->TlsSessionState = EfiTlsSessionNotStarted; + Status = HttpInstance->Tls->SetSessionData ( + HttpInstance->Tls, + EfiTlsSessionState, + &(HttpInstance->TlsSessionState), + sizeof (EFI_TLS_SESSION_STATE) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Create ClientHello + // + BufferOutSize = DEF_BUF_LEN; + BufferOut = AllocateZeroPool (BufferOutSize); + if (BufferOut == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->BuildResponsePacket ( + HttpInstance->Tls, + NULL, + 0, + BufferOut, + &BufferOutSize + ); + if (Status == EFI_BUFFER_TOO_SMALL) { + FreePool (BufferOut); + BufferOut = AllocateZeroPool (BufferOutSize); + if (BufferOut == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->BuildResponsePacket ( + HttpInstance->Tls, + NULL, + 0, + BufferOut, + &BufferOutSize + ); + } + if (EFI_ERROR (Status)) { + FreePool (BufferOut); + return Status; + } + + // + // Transmit ClientHello + // + PacketOut = NetbufAlloc ((UINT32) BufferOutSize); + DataOut = NetbufAllocSpace (PacketOut, (UINT32) BufferOutSize, NET_BUF_TAIL); + if (DataOut == NULL) { + FreePool (BufferOut); + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (DataOut, BufferOut, BufferOutSize); + Status = TlsCommonTransmit (HttpInstance, PacketOut); + + FreePool (BufferOut); + NetbufFree (PacketOut); + + if (EFI_ERROR (Status)) { + return Status; + } + + while(HttpInstance->TlsSessionState != EfiTlsSessionDataTransferring && \ + ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) { + // + // Receive one TLS record. + // + Status = TlsReceiveOnePdu (HttpInstance, &Pdu, Timeout); + if (EFI_ERROR (Status)) { + return Status; + } + + BufferInSize = Pdu->TotalSize; + BufferIn = AllocateZeroPool (BufferInSize); + if (BufferIn == NULL) { + NetbufFree (Pdu); + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + NetbufCopy (Pdu, 0, (UINT32)BufferInSize, BufferIn); + + NetbufFree (Pdu); + + // + // Handle Receive data. + // + BufferOutSize = DEF_BUF_LEN; + BufferOut = AllocateZeroPool (BufferOutSize); + if (BufferOut == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->BuildResponsePacket ( + HttpInstance->Tls, + BufferIn, + BufferInSize, + BufferOut, + &BufferOutSize + ); + if (Status == EFI_BUFFER_TOO_SMALL) { + FreePool (BufferOut); + BufferOut = AllocateZeroPool (BufferOutSize); + if (BufferOut == NULL) { + FreePool (BufferIn); + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->BuildResponsePacket ( + HttpInstance->Tls, + BufferIn, + BufferInSize, + BufferOut, + &BufferOutSize + ); + } + + FreePool (BufferIn); + + if (EFI_ERROR (Status)) { + FreePool (BufferOut); + return Status; + } + + if (BufferOutSize != 0) { + // + // Transmit the response packet. + // + PacketOut = NetbufAlloc ((UINT32) BufferOutSize); + DataOut = NetbufAllocSpace (PacketOut, (UINT32) BufferOutSize, NET_BUF_TAIL); + if (DataOut == NULL) { + FreePool (BufferOut); + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (DataOut, BufferOut, BufferOutSize); + + Status = TlsCommonTransmit (HttpInstance, PacketOut); + + NetbufFree (PacketOut); + + if (EFI_ERROR (Status)) { + FreePool (BufferOut); + return Status; + } + } + + FreePool (BufferOut); + + // + // Get the session state, then decide whether need to continue handle received packet. + // + GetSessionDataBufferSize = DEF_BUF_LEN; + GetSessionDataBuffer = AllocateZeroPool (GetSessionDataBufferSize); + if (GetSessionDataBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->GetSessionData ( + HttpInstance->Tls, + EfiTlsSessionState, + GetSessionDataBuffer, + &GetSessionDataBufferSize + ); + if (Status == EFI_BUFFER_TOO_SMALL) { + FreePool (GetSessionDataBuffer); + GetSessionDataBuffer = AllocateZeroPool (GetSessionDataBufferSize); + if (GetSessionDataBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->GetSessionData ( + HttpInstance->Tls, + EfiTlsSessionState, + GetSessionDataBuffer, + &GetSessionDataBufferSize + ); + } + if (EFI_ERROR (Status)) { + FreePool(GetSessionDataBuffer); + return Status; + } + + ASSERT(GetSessionDataBufferSize == sizeof (EFI_TLS_SESSION_STATE)); + HttpInstance->TlsSessionState = *(EFI_TLS_SESSION_STATE *) GetSessionDataBuffer; + + FreePool (GetSessionDataBuffer); + + if(HttpInstance->TlsSessionState == EfiTlsSessionError) { + return EFI_ABORTED; + } + } + + if (HttpInstance->TlsSessionState != EfiTlsSessionDataTransferring) { + Status = EFI_ABORTED; + } + + return Status; +} + +/** + Close the TLS session and send out the close notification message. + + @param[in] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS The TLS session is closed. + @retval EFI_INVALID_PARAMETER HttpInstance is NULL. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsCloseSession ( + IN HTTP_PROTOCOL *HttpInstance + ) +{ + EFI_STATUS Status; + + UINT8 *BufferOut; + UINTN BufferOutSize; + + NET_BUF *PacketOut; + UINT8 *DataOut; + + Status = EFI_SUCCESS; + BufferOut = NULL; + PacketOut = NULL; + DataOut = NULL; + + if (HttpInstance == NULL) { + return EFI_INVALID_PARAMETER; + } + + HttpInstance->TlsSessionState = EfiTlsSessionClosing; + + Status = HttpInstance->Tls->SetSessionData ( + HttpInstance->Tls, + EfiTlsSessionState, + &(HttpInstance->TlsSessionState), + sizeof (EFI_TLS_SESSION_STATE) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + BufferOutSize = DEF_BUF_LEN; + BufferOut = AllocateZeroPool (BufferOutSize); + if (BufferOut == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->BuildResponsePacket ( + HttpInstance->Tls, + NULL, + 0, + BufferOut, + &BufferOutSize + ); + if (Status == EFI_BUFFER_TOO_SMALL) { + FreePool (BufferOut); + BufferOut = AllocateZeroPool (BufferOutSize); + if (BufferOut == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->BuildResponsePacket ( + HttpInstance->Tls, + NULL, + 0, + BufferOut, + &BufferOutSize + ); + } + + if (EFI_ERROR (Status)) { + FreePool (BufferOut); + return Status; + } + + PacketOut = NetbufAlloc ((UINT32) BufferOutSize); + DataOut = NetbufAllocSpace (PacketOut, (UINT32) BufferOutSize, NET_BUF_TAIL); + if (DataOut == NULL) { + FreePool (BufferOut); + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (DataOut, BufferOut, BufferOutSize); + + Status = TlsCommonTransmit (HttpInstance, PacketOut); + + FreePool (BufferOut); + NetbufFree (PacketOut); + + return Status; +} + +/** + Process one message according to the CryptMode. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[in] Message Pointer to the message buffer needed to processed. + If ProcessMode is EfiTlsEncrypt, the message contain the TLS + header and plain text TLS APP payload. + If ProcessMode is EfiTlsDecrypt, the message contain the TLS + header and cipher text TLS APP payload. + @param[in] MessageSize Pointer to the message buffer size. + @param[in] ProcessMode Process mode. + @param[in, out] Fragment Only one Fragment returned after the Message is + processed successfully. + If ProcessMode is EfiTlsEncrypt, the fragment contain the TLS + header and cipher text TLS APP payload. + If ProcessMode is EfiTlsDecrypt, the fragment contain the TLS + header and plain text TLS APP payload. + + @retval EFI_SUCCESS Message is processed successfully. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsProcessMessage ( + IN HTTP_PROTOCOL *HttpInstance, + IN UINT8 *Message, + IN UINTN MessageSize, + IN EFI_TLS_CRYPT_MODE ProcessMode, + IN OUT NET_FRAGMENT *Fragment + ) +{ + EFI_STATUS Status; + UINT8 *Buffer; + UINT32 BufferSize; + UINT32 BytesCopied; + EFI_TLS_FRAGMENT_DATA *FragmentTable; + UINT32 FragmentCount; + EFI_TLS_FRAGMENT_DATA *OriginalFragmentTable; + UINTN Index; + + Status = EFI_SUCCESS; + Buffer = NULL; + BufferSize = 0; + BytesCopied = 0; + FragmentTable = NULL; + OriginalFragmentTable = NULL; + + // + // Rebuild fragment table from BufferIn. + // + FragmentCount = 1; + FragmentTable = AllocateZeroPool (FragmentCount * sizeof (EFI_TLS_FRAGMENT_DATA)); + if (FragmentTable == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + FragmentTable->FragmentLength = (UINT32) MessageSize; + FragmentTable->FragmentBuffer = Message; + + // + // Record the original FragmentTable. + // + OriginalFragmentTable = FragmentTable; + + // + // Process the Message. + // + Status = HttpInstance->Tls->ProcessPacket ( + HttpInstance->Tls, + &FragmentTable, + &FragmentCount, + ProcessMode + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Calculate the size according to FragmentTable. + // + for (Index = 0; Index < FragmentCount; Index++) { + BufferSize += FragmentTable[Index].FragmentLength; + } + + // + // Allocate buffer for processed data. + // + Buffer = AllocateZeroPool (BufferSize); + if (Buffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Copy the new FragmentTable buffer into Buffer. + // + for (Index = 0; Index < FragmentCount; Index++) { + CopyMem ( + (Buffer + BytesCopied), + FragmentTable[Index].FragmentBuffer, + FragmentTable[Index].FragmentLength + ); + BytesCopied += FragmentTable[Index].FragmentLength; + + // + // Free the FragmentBuffer since it has been copied. + // + FreePool (FragmentTable[Index].FragmentBuffer); + } + + Fragment->Len = BufferSize; + Fragment->Bulk = Buffer; + +ON_EXIT: + + if (OriginalFragmentTable != NULL) { + if( FragmentTable == OriginalFragmentTable) { + FragmentTable = NULL; + } + FreePool (OriginalFragmentTable); + OriginalFragmentTable = NULL; + } + + // + // Caller has the responsibility to free the FragmentTable. + // + if (FragmentTable != NULL) { + FreePool (FragmentTable); + FragmentTable = NULL; + } + + return Status; +} + +/** + Receive one fragment decrypted from one TLS record. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[in, out] Fragment The received Fragment. + @param[in] Timeout The time to wait for connection done. + + @retval EFI_SUCCESS One fragment is received. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_ABORTED Something wrong decryption the message. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +HttpsReceive ( + IN HTTP_PROTOCOL *HttpInstance, + IN OUT NET_FRAGMENT *Fragment, + IN EFI_EVENT Timeout + ) +{ + EFI_STATUS Status; + NET_BUF *Pdu; + TLS_RECORD_HEADER RecordHeader; + UINT8 *BufferIn; + UINTN BufferInSize; + NET_FRAGMENT TempFragment; + UINT8 *BufferOut; + UINTN BufferOutSize; + NET_BUF *PacketOut; + UINT8 *DataOut; + UINT8 *GetSessionDataBuffer; + UINTN GetSessionDataBufferSize; + + Status = EFI_SUCCESS; + Pdu = NULL; + BufferIn = NULL; + BufferInSize = 0; + BufferOut = NULL; + BufferOutSize = 0; + PacketOut = NULL; + DataOut = NULL; + GetSessionDataBuffer = NULL; + GetSessionDataBufferSize = 0; + + // + // Receive only one TLS record + // + Status = TlsReceiveOnePdu (HttpInstance, &Pdu, Timeout); + if (EFI_ERROR (Status)) { + return Status; + } + + BufferInSize = Pdu->TotalSize; + BufferIn = AllocateZeroPool (BufferInSize); + if (BufferIn == NULL) { + Status = EFI_OUT_OF_RESOURCES; + NetbufFree (Pdu); + return Status; + } + + NetbufCopy (Pdu, 0, (UINT32) BufferInSize, BufferIn); + + NetbufFree (Pdu); + + // + // Handle Receive data. + // + RecordHeader = *(TLS_RECORD_HEADER *) BufferIn; + + if ((RecordHeader.ContentType == TlsContentTypeApplicationData) && + (RecordHeader.Version.Major == 0x03) && + (RecordHeader.Version.Minor == TLS10_PROTOCOL_VERSION_MINOR || + RecordHeader.Version.Minor == TLS11_PROTOCOL_VERSION_MINOR || + RecordHeader.Version.Minor == TLS12_PROTOCOL_VERSION_MINOR) + ) { + // + // Decrypt Packet. + // + Status = TlsProcessMessage ( + HttpInstance, + BufferIn, + BufferInSize, + EfiTlsDecrypt, + &TempFragment + ); + + FreePool (BufferIn); + + if (EFI_ERROR (Status)) { + if (Status == EFI_ABORTED) { + // + // Something wrong decryption the message. + // BuildResponsePacket() will be called to generate Error Alert message and send it out. + // + BufferOutSize = DEF_BUF_LEN; + BufferOut = AllocateZeroPool (BufferOutSize); + if (BufferOut == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->BuildResponsePacket ( + HttpInstance->Tls, + NULL, + 0, + BufferOut, + &BufferOutSize + ); + if (Status == EFI_BUFFER_TOO_SMALL) { + FreePool (BufferOut); + BufferOut = AllocateZeroPool (BufferOutSize); + if (BufferOut == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->BuildResponsePacket ( + HttpInstance->Tls, + NULL, + 0, + BufferOut, + &BufferOutSize + ); + } + if (EFI_ERROR (Status)) { + FreePool(BufferOut); + return Status; + } + + if (BufferOutSize != 0) { + PacketOut = NetbufAlloc ((UINT32)BufferOutSize); + DataOut = NetbufAllocSpace (PacketOut, (UINT32) BufferOutSize, NET_BUF_TAIL); + if (DataOut == NULL) { + FreePool (BufferOut); + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (DataOut, BufferOut, BufferOutSize); + + Status = TlsCommonTransmit (HttpInstance, PacketOut); + + NetbufFree (PacketOut); + } + + FreePool(BufferOut); + + if (EFI_ERROR (Status)) { + return Status; + } + + return EFI_ABORTED; + } + + return Status; + } + + // + // Parsing buffer. + // + ASSERT (((TLS_RECORD_HEADER *) (TempFragment.Bulk))->ContentType == TlsContentTypeApplicationData); + + BufferInSize = ((TLS_RECORD_HEADER *) (TempFragment.Bulk))->Length; + BufferIn = AllocateZeroPool (BufferInSize); + if (BufferIn == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + CopyMem (BufferIn, TempFragment.Bulk + TLS_RECORD_HEADER_LENGTH, BufferInSize); + + // + // Free the buffer in TempFragment. + // + FreePool (TempFragment.Bulk); + + } else if ((RecordHeader.ContentType == TlsContentTypeAlert) && + (RecordHeader.Version.Major == 0x03) && + (RecordHeader.Version.Minor == TLS10_PROTOCOL_VERSION_MINOR || + RecordHeader.Version.Minor == TLS11_PROTOCOL_VERSION_MINOR || + RecordHeader.Version.Minor == TLS12_PROTOCOL_VERSION_MINOR) + ) { + BufferOutSize = DEF_BUF_LEN; + BufferOut = AllocateZeroPool (BufferOutSize); + if (BufferOut == NULL) { + FreePool (BufferIn); + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->BuildResponsePacket ( + HttpInstance->Tls, + BufferIn, + BufferInSize, + BufferOut, + &BufferOutSize + ); + if (Status == EFI_BUFFER_TOO_SMALL) { + FreePool (BufferOut); + BufferOut = AllocateZeroPool (BufferOutSize); + if (BufferOut == NULL) { + FreePool (BufferIn); + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->BuildResponsePacket ( + HttpInstance->Tls, + BufferIn, + BufferInSize, + BufferOut, + &BufferOutSize + ); + } + + FreePool (BufferIn); + + if (EFI_ERROR (Status)) { + FreePool (BufferOut); + return Status; + } + + if (BufferOutSize != 0) { + PacketOut = NetbufAlloc ((UINT32) BufferOutSize); + DataOut = NetbufAllocSpace (PacketOut, (UINT32) BufferOutSize, NET_BUF_TAIL); + if (DataOut == NULL) { + FreePool (BufferOut); + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (DataOut, BufferOut, BufferOutSize); + + Status = TlsCommonTransmit (HttpInstance, PacketOut); + + NetbufFree (PacketOut); + } + + FreePool (BufferOut); + + // + // Get the session state. + // + GetSessionDataBufferSize = DEF_BUF_LEN; + GetSessionDataBuffer = AllocateZeroPool (GetSessionDataBufferSize); + if (GetSessionDataBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->GetSessionData ( + HttpInstance->Tls, + EfiTlsSessionState, + GetSessionDataBuffer, + &GetSessionDataBufferSize + ); + if (Status == EFI_BUFFER_TOO_SMALL) { + FreePool (GetSessionDataBuffer); + GetSessionDataBuffer = AllocateZeroPool (GetSessionDataBufferSize); + if (GetSessionDataBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = HttpInstance->Tls->GetSessionData ( + HttpInstance->Tls, + EfiTlsSessionState, + GetSessionDataBuffer, + &GetSessionDataBufferSize + ); + } + if (EFI_ERROR (Status)) { + FreePool (GetSessionDataBuffer); + return Status; + } + + ASSERT(GetSessionDataBufferSize == sizeof (EFI_TLS_SESSION_STATE)); + HttpInstance->TlsSessionState = *(EFI_TLS_SESSION_STATE *) GetSessionDataBuffer; + + FreePool (GetSessionDataBuffer); + + if(HttpInstance->TlsSessionState == EfiTlsSessionError) { + DEBUG ((EFI_D_ERROR, "TLS Session State Error!\n")); + return EFI_ABORTED; + } + + BufferIn = NULL; + BufferInSize = 0; + } + + Fragment->Bulk = BufferIn; + Fragment->Len = (UINT32) BufferInSize; + + return Status; +} + diff --git a/NetworkPkg/HttpDxe/HttpsSupport.h b/NetworkPkg/HttpDxe/HttpsSupport.h new file mode 100644 index 000000000..0e16968cd --- /dev/null +++ b/NetworkPkg/HttpDxe/HttpsSupport.h @@ -0,0 +1,265 @@ +/** @file + The header files of miscellaneous routines specific to Https for HttpDxe driver. + +Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_HTTPS_SUPPORT_H__ +#define __EFI_HTTPS_SUPPORT_H__ + +#define HTTPS_DEFAULT_PORT 443 + +#define HTTPS_FLAG "https://" + +/** + Check whether the Url is from Https. + + @param[in] Url The pointer to a HTTP or HTTPS URL string. + + @retval TRUE The Url is from HTTPS. + @retval FALSE The Url is from HTTP. + +**/ +BOOLEAN +IsHttpsUrl ( + IN CHAR8 *Url + ); + +/** + Creates a Tls child handle, open EFI_TLS_PROTOCOL and EFI_TLS_CONFIGURATION_PROTOCOL. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[out] TlsSb Pointer to the TLS SERVICE_BINDING_PROTOCOL. + @param[out] TlsProto Pointer to the EFI_TLS_PROTOCOL instance. + @param[out] TlsConfiguration Pointer to the EFI_TLS_CONFIGURATION_PROTOCOL instance. + + @return The child handle with opened EFI_TLS_PROTOCOL and EFI_TLS_CONFIGURATION_PROTOCOL. + +**/ +EFI_HANDLE +EFIAPI +TlsCreateChild ( + IN EFI_HANDLE ImageHandle, + OUT EFI_SERVICE_BINDING_PROTOCOL **TlsSb, + OUT EFI_TLS_PROTOCOL **TlsProto, + OUT EFI_TLS_CONFIGURATION_PROTOCOL **TlsConfiguration + ); + +/** + Create event for the TLS receive and transmit tokens which are used to receive and + transmit TLS related messages. + + @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure. + + @retval EFI_SUCCESS The events are created successfully. + @retval others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsCreateTxRxEvent ( + IN OUT HTTP_PROTOCOL *HttpInstance + ); + +/** + Close events in the TlsTxToken and TlsRxToken. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + +**/ +VOID +EFIAPI +TlsCloseTxRxEvent ( + IN HTTP_PROTOCOL *HttpInstance + ); + +/** + Read the TlsCaCertificate variable and configure it. + + @param[in, out] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS TlsCaCertificate is configured. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_NOT_FOUND Fail to get "TlsCaCertificate" variable. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +TlsConfigCertificate ( + IN OUT HTTP_PROTOCOL *HttpInstance + ); + +/** + Configure TLS session data. + + @param[in, out] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS TLS session data is configured. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsConfigureSession ( + IN OUT HTTP_PROTOCOL *HttpInstance + ); + +/** + Transmit the Packet by processing the associated HTTPS token. + + @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[in] Packet The packet to transmit. + + @retval EFI_SUCCESS The packet is transmitted. + @retval EFI_INVALID_PARAMETER HttpInstance is NULL or Packet is NULL. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsCommonTransmit ( + IN OUT HTTP_PROTOCOL *HttpInstance, + IN NET_BUF *Packet + ); + +/** + Receive the Packet by processing the associated HTTPS token. + + @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[in] Packet The packet to transmit. + @param[in] Timeout The time to wait for connection done. + + @retval EFI_SUCCESS The Packet is received. + @retval EFI_INVALID_PARAMETER HttpInstance is NULL or Packet is NULL. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_TIMEOUT The operation is time out. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsCommonReceive ( + IN OUT HTTP_PROTOCOL *HttpInstance, + IN NET_BUF *Packet, + IN EFI_EVENT Timeout + ); + +/** + Receive one TLS PDU. An TLS PDU contains an TLS record header and it's + corresponding record data. These two parts will be put into two blocks of buffers in the + net buffer. + + @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[out] Pdu The received TLS PDU. + @param[in] Timeout The time to wait for connection done. + + @retval EFI_SUCCESS An TLS PDU is received. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_PROTOCOL_ERROR An unexpected TLS packet was received. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsReceiveOnePdu ( + IN OUT HTTP_PROTOCOL *HttpInstance, + OUT NET_BUF **Pdu, + IN EFI_EVENT Timeout + ); + +/** + Connect one TLS session by finishing the TLS handshake process. + + @param[in] HttpInstance The HTTP instance private data. + @param[in] Timeout The time to wait for connection done. + + @retval EFI_SUCCESS The TLS session is established. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_ABORTED TLS session state is incorrect. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsConnectSession ( + IN HTTP_PROTOCOL *HttpInstance, + IN EFI_EVENT Timeout + ); + +/** + Close the TLS session and send out the close notification message. + + @param[in] HttpInstance The HTTP instance private data. + + @retval EFI_SUCCESS The TLS session is closed. + @retval EFI_INVALID_PARAMETER HttpInstance is NULL or Packet is NULL. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsCloseSession ( + IN HTTP_PROTOCOL *HttpInstance + ); + +/** + Process one message according to the CryptMode. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[in] Message Pointer to the message buffer needed to processed. + If ProcessMode is EfiTlsEncrypt, the message contain the TLS + header and plain text TLS APP payload. + If ProcessMode is EfiTlsDecrypt, the message contain the TLS + header and cipher text TLS APP payload. + @param[in] MessageSize Pointer to the message buffer size. + @param[in] ProcessMode Process mode. + @param[in, out] Fragment Only one Fragment returned after the Message is + processed successfully. + If ProcessMode is EfiTlsEncrypt, the fragment contain the TLS + header and cipher text TLS APP payload. + If ProcessMode is EfiTlsDecrypt, the fragment contain the TLS + header and plain text TLS APP payload. + + @retval EFI_SUCCESS Message is processed successfully. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TlsProcessMessage ( + IN HTTP_PROTOCOL *HttpInstance, + IN UINT8 *Message, + IN UINTN MessageSize, + IN EFI_TLS_CRYPT_MODE ProcessMode, + IN OUT NET_FRAGMENT *Fragment + ); + +/** + Receive one fragment decrypted from one TLS record. + + @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure. + @param[in, out] Fragment The received Fragment. + @param[in] Timeout The time to wait for connection done. + + @retval EFI_SUCCESS One fragment is received. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_ABORTED Something wrong decryption the message. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +HttpsReceive ( + IN HTTP_PROTOCOL *HttpInstance, + IN OUT NET_FRAGMENT *Fragment, + IN EFI_EVENT Timeout + ); + +#endif + diff --git a/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.c b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.c new file mode 100644 index 000000000..4c1e44d24 --- /dev/null +++ b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.c @@ -0,0 +1,120 @@ +/** @file + The DriverEntryPoint and Unload for HttpUtilities driver. + + Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpUtilitiesDxe.h" + + +/** + Unloads an image. + + @param ImageHandle Handle that identifies the image to be unloaded. + + @retval EFI_SUCCESS The image has been unloaded. + @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle. + +**/ +EFI_STATUS +EFIAPI +HttpUtilitiesDxeUnload ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_STATUS Status; + UINTN HandleNum; + EFI_HANDLE *HandleBuffer; + UINT32 Index; + EFI_HTTP_UTILITIES_PROTOCOL *HttpUtilitiesProtocol; + + + HandleBuffer = NULL; + + // + // Locate all the handles with HttpUtilities protocol. + // + Status = gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiHttpUtilitiesProtocolGuid, + NULL, + &HandleNum, + &HandleBuffer + ); + if (EFI_ERROR (Status)) { + return Status; + } + + for (Index = 0; Index < HandleNum; Index++) { + // + // Firstly, find HttpUtilitiesProtocol interface + // + Status = gBS->OpenProtocol ( + HandleBuffer[Index], + &gEfiHttpUtilitiesProtocolGuid, + (VOID **) &HttpUtilitiesProtocol, + ImageHandle, + NULL, + EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Then, uninstall HttpUtilities interface + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + HandleBuffer[Index], + &gEfiHttpUtilitiesProtocolGuid, HttpUtilitiesProtocol, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + return EFI_SUCCESS; +} + + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + @param ImageHandle The firmware allocated handle for the UEFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval Others An unexpected error occurred. +**/ +EFI_STATUS +EFIAPI +HttpUtilitiesDxeDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + EFI_HANDLE Handle; + + Handle = NULL; + + // + // Install the HttpUtilities Protocol onto Handle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &Handle, + &gEfiHttpUtilitiesProtocolGuid, + &mHttpUtilitiesProtocol, + NULL + ); + + return Status; +} + diff --git a/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.h b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.h new file mode 100644 index 000000000..37dad6bc0 --- /dev/null +++ b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.h @@ -0,0 +1,116 @@ +/** @file + The header files of Http Utilities functions for HttpUtilities driver. + + Copyright (c) 2015, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_HTTP_UTILITIES_DXE_H__ +#define __EFI_HTTP_UTILITIES_DXE_H__ + +#include + +// +// Libraries +// +#include +#include +#include +#include +#include +#include +#include + +// +// Consumed Protocols +// +#include +#include + +// +// Protocol instances +// +extern EFI_HTTP_UTILITIES_PROTOCOL mHttpUtilitiesProtocol; + +/** + Create HTTP header based on a combination of seed header, fields + to delete, and fields to append. + + The Build() function is used to manage the headers portion of an + HTTP message by providing the ability to add, remove, or replace + HTTP headers. + + @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance. + @param[in] SeedMessageSize Size of the initial HTTP header. This can be zero. + @param[in] SeedMessage Initial HTTP header to be used as a base for + building a new HTTP header. If NULL, + SeedMessageSize is ignored. + @param[in] DeleteCount Number of null-terminated HTTP header field names + in DeleteList. + @param[in] DeleteList List of null-terminated HTTP header field names to + remove from SeedMessage. Only the field names are + in this list because the field values are irrelevant + to this operation. + @param[in] AppendCount Number of header fields in AppendList. + @param[in] AppendList List of HTTP headers to populate NewMessage with. + If SeedMessage is not NULL, AppendList will be + appended to the existing list from SeedMessage in + NewMessage. + @param[out] NewMessageSize Pointer to number of header fields in NewMessage. + @param[out] NewMessage Pointer to a new list of HTTP headers based on. + + @retval EFI_SUCCESS Add, remove, and replace operations succeeded. + @retval EFI_OUT_OF_RESOURCES Could not allocate memory for NewMessage. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. +**/ +EFI_STATUS +EFIAPI +HttpUtilitiesBuild ( + IN EFI_HTTP_UTILITIES_PROTOCOL *This, + IN UINTN SeedMessageSize, + IN VOID *SeedMessage, OPTIONAL + IN UINTN DeleteCount, + IN CHAR8 *DeleteList[], OPTIONAL + IN UINTN AppendCount, + IN EFI_HTTP_HEADER *AppendList[], OPTIONAL + OUT UINTN *NewMessageSize, + OUT VOID **NewMessage + ); + + +/** + Parses HTTP header and produces an array of key/value pairs. + + The Parse() function is used to transform data stored in HttpHeader + into a list of fields paired with their corresponding values. + + @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance. + @param[in] HttpMessage Contains raw unformatted HTTP header string. + @param[in] HttpMessageSize Size of HTTP header. + @param[out] HeaderFields Array of key/value header pairs. + @param[out] FieldCount Number of headers in HeaderFields. + + @retval EFI_SUCCESS Allocation succeeded. + @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been + initialized. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + HttpMessage is NULL. + HeaderFields is NULL. + FieldCount is NULL. +**/ +EFI_STATUS +EFIAPI +HttpUtilitiesParse ( + IN EFI_HTTP_UTILITIES_PROTOCOL *This, + IN CHAR8 *HttpMessage, + IN UINTN HttpMessageSize, + OUT EFI_HTTP_HEADER **HeaderFields, + OUT UINTN *FieldCount + ); + +#endif diff --git a/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.inf b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.inf new file mode 100644 index 000000000..e12c3cc92 --- /dev/null +++ b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.inf @@ -0,0 +1,49 @@ +## @file +# Implementation of EFI Http Utilities Protocol interfaces. +# +# Copyright (c) 2015, Intel Corporation. All rights reserved.
+# (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = HttpUtilitiesDxe + FILE_GUID = 22ea234f-e72a-11e4-91f9-28d2447c4829 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = HttpUtilitiesDxeDriverEntryPoint + UNLOAD_IMAGE = HttpUtilitiesDxeUnload + MODULE_UNI_FILE = HttpUtilitiesDxe.uni + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[Sources] + HttpUtilitiesDxe.h + HttpUtilitiesDxe.c + HttpUtilitiesProtocol.c + +[LibraryClasses] + UefiDriverEntryPoint + UefiBootServicesTableLib + MemoryAllocationLib + BaseMemoryLib + BaseLib + UefiLib + DebugLib + HttpLib + +[Protocols] + gEfiHttpUtilitiesProtocolGuid ## PRODUCES + +[Depex] + TRUE + +[UserExtensions.TianoCore."ExtraFiles"] + HttpUtilitiesDxeExtra.uni diff --git a/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.uni b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.uni new file mode 100644 index 000000000..ed8315148 --- /dev/null +++ b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.uni @@ -0,0 +1,17 @@ +// /** @file +// This module produces Http Utilities Protocol. +// +// This module produces Http Utilities Protocol. +// +// +// Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Produces Http Utilities Protocol" + +#string STR_MODULE_DESCRIPTION #language en-US "This module provides Http Utilities Protocol." + diff --git a/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxeExtra.uni b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxeExtra.uni new file mode 100644 index 000000000..908e3a4b4 --- /dev/null +++ b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// HttpUtilitiesDxe Localized Strings and Content +// +// Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"UEFI HTTP UTILITIES DXE" + + diff --git a/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesProtocol.c b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesProtocol.c new file mode 100644 index 000000000..bd4df90e0 --- /dev/null +++ b/NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesProtocol.c @@ -0,0 +1,387 @@ +/** @file + Implementation of EFI_HTTP_PROTOCOL protocol interfaces. + + Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "HttpUtilitiesDxe.h" + +EFI_HTTP_UTILITIES_PROTOCOL mHttpUtilitiesProtocol = { + HttpUtilitiesBuild, + HttpUtilitiesParse +}; + + +/** + Create HTTP header based on a combination of seed header, fields + to delete, and fields to append. + + The Build() function is used to manage the headers portion of an + HTTP message by providing the ability to add, remove, or replace + HTTP headers. + + @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance. + @param[in] SeedMessageSize Size of the initial HTTP header. This can be zero. + @param[in] SeedMessage Initial HTTP header to be used as a base for + building a new HTTP header. If NULL, + SeedMessageSize is ignored. + @param[in] DeleteCount Number of null-terminated HTTP header field names + in DeleteList. + @param[in] DeleteList List of null-terminated HTTP header field names to + remove from SeedMessage. Only the field names are + in this list because the field values are irrelevant + to this operation. + @param[in] AppendCount Number of header fields in AppendList. + @param[in] AppendList List of HTTP headers to populate NewMessage with. + If SeedMessage is not NULL, AppendList will be + appended to the existing list from SeedMessage in + NewMessage. + @param[out] NewMessageSize Pointer to number of header fields in NewMessage. + @param[out] NewMessage Pointer to a new list of HTTP headers based on. + + @retval EFI_SUCCESS Add, remove, and replace operations succeeded. + @retval EFI_OUT_OF_RESOURCES Could not allocate memory for NewMessage. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. +**/ +EFI_STATUS +EFIAPI +HttpUtilitiesBuild ( + IN EFI_HTTP_UTILITIES_PROTOCOL *This, + IN UINTN SeedMessageSize, + IN VOID *SeedMessage, OPTIONAL + IN UINTN DeleteCount, + IN CHAR8 *DeleteList[], OPTIONAL + IN UINTN AppendCount, + IN EFI_HTTP_HEADER *AppendList[], OPTIONAL + OUT UINTN *NewMessageSize, + OUT VOID **NewMessage + ) +{ + EFI_STATUS Status; + EFI_HTTP_HEADER *SeedHeaderFields; + UINTN SeedFieldCount; + UINTN Index; + EFI_HTTP_HEADER *TempHeaderFields; + UINTN TempFieldCount; + EFI_HTTP_HEADER *NewHeaderFields; + UINTN NewFieldCount; + EFI_HTTP_HEADER *HttpHeader; + UINTN StrLength; + UINT8 *NewMessagePtr; + + SeedHeaderFields = NULL; + SeedFieldCount = 0; + TempHeaderFields = NULL; + TempFieldCount = 0; + NewHeaderFields = NULL; + NewFieldCount = 0; + + HttpHeader = NULL; + StrLength = 0; + NewMessagePtr = NULL; + *NewMessageSize = 0; + Status = EFI_SUCCESS; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (SeedMessage != NULL) { + Status = This->Parse ( + This, + SeedMessage, + SeedMessageSize, + &SeedHeaderFields, + &SeedFieldCount + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + // + // Handle DeleteList + // + if (SeedFieldCount != 0 && DeleteCount != 0) { + TempHeaderFields = AllocateZeroPool (SeedFieldCount * sizeof(EFI_HTTP_HEADER)); + if (TempHeaderFields == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + for (Index = 0, TempFieldCount = 0; Index < SeedFieldCount; Index++) { + // + // Check whether each SeedHeaderFields member is in DeleteList + // + if (HttpIsValidHttpHeader( DeleteList, DeleteCount, SeedHeaderFields[Index].FieldName)) { + Status = HttpSetFieldNameAndValue ( + &TempHeaderFields[TempFieldCount], + SeedHeaderFields[Index].FieldName, + SeedHeaderFields[Index].FieldValue + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + TempFieldCount++; + } + } + } else { + TempHeaderFields = SeedHeaderFields; + TempFieldCount = SeedFieldCount; + } + + // + // Handle AppendList + // + NewHeaderFields = AllocateZeroPool ((TempFieldCount + AppendCount) * sizeof (EFI_HTTP_HEADER)); + if (NewHeaderFields == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + for (Index = 0; Index < TempFieldCount; Index++) { + Status = HttpSetFieldNameAndValue ( + &NewHeaderFields[Index], + TempHeaderFields[Index].FieldName, + TempHeaderFields[Index].FieldValue + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + NewFieldCount = TempFieldCount; + + for (Index = 0; Index < AppendCount; Index++) { + HttpHeader = HttpFindHeader (NewFieldCount, NewHeaderFields, AppendList[Index]->FieldName); + if (HttpHeader != NULL) { + Status = HttpSetFieldNameAndValue ( + HttpHeader, + AppendList[Index]->FieldName, + AppendList[Index]->FieldValue + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } else { + Status = HttpSetFieldNameAndValue ( + &NewHeaderFields[NewFieldCount], + AppendList[Index]->FieldName, + AppendList[Index]->FieldValue + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + NewFieldCount++; + } + } + + // + // Calculate NewMessageSize, then build NewMessage + // + for (Index = 0; Index < NewFieldCount; Index++) { + HttpHeader = &NewHeaderFields[Index]; + + StrLength = AsciiStrLen (HttpHeader->FieldName); + *NewMessageSize += StrLength; + + StrLength = sizeof(": ") - 1; + *NewMessageSize += StrLength; + + StrLength = AsciiStrLen (HttpHeader->FieldValue); + *NewMessageSize += StrLength; + + StrLength = sizeof("\r\n") - 1; + *NewMessageSize += StrLength; + } + StrLength = sizeof("\r\n") - 1; + *NewMessageSize += StrLength; + + *NewMessage = AllocateZeroPool (*NewMessageSize); + if (*NewMessage == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + NewMessagePtr = (UINT8 *)(*NewMessage); + + for (Index = 0; Index < NewFieldCount; Index++) { + HttpHeader = &NewHeaderFields[Index]; + + StrLength = AsciiStrLen (HttpHeader->FieldName); + CopyMem (NewMessagePtr, HttpHeader->FieldName, StrLength); + NewMessagePtr += StrLength; + + StrLength = sizeof(": ") - 1; + CopyMem (NewMessagePtr, ": ", StrLength); + NewMessagePtr += StrLength; + + StrLength = AsciiStrLen (HttpHeader->FieldValue); + CopyMem (NewMessagePtr, HttpHeader->FieldValue, StrLength); + NewMessagePtr += StrLength; + + StrLength = sizeof("\r\n") - 1; + CopyMem (NewMessagePtr, "\r\n", StrLength); + NewMessagePtr += StrLength; + } + StrLength = sizeof("\r\n") - 1; + CopyMem (NewMessagePtr, "\r\n", StrLength); + NewMessagePtr += StrLength; + + ASSERT (*NewMessageSize == (UINTN)NewMessagePtr - (UINTN)(*NewMessage)); + + // + // Free allocated buffer + // +ON_EXIT: + if (SeedHeaderFields != NULL) { + HttpFreeHeaderFields(SeedHeaderFields, SeedFieldCount); + } + + if (TempHeaderFields != NULL) { + HttpFreeHeaderFields(TempHeaderFields, TempFieldCount); + } + + if (NewHeaderFields != NULL) { + HttpFreeHeaderFields(NewHeaderFields, NewFieldCount); + } + + return Status; +} + + +/** + Parses HTTP header and produces an array of key/value pairs. + + The Parse() function is used to transform data stored in HttpHeader + into a list of fields paired with their corresponding values. + + @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance. + @param[in] HttpMessage Contains raw unformatted HTTP header string. + @param[in] HttpMessageSize Size of HTTP header. + @param[out] HeaderFields Array of key/value header pairs. + @param[out] FieldCount Number of headers in HeaderFields. + + @retval EFI_SUCCESS Allocation succeeded. + @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been + initialized. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + HttpMessage is NULL. + HeaderFields is NULL. + FieldCount is NULL. +**/ +EFI_STATUS +EFIAPI +HttpUtilitiesParse ( + IN EFI_HTTP_UTILITIES_PROTOCOL *This, + IN CHAR8 *HttpMessage, + IN UINTN HttpMessageSize, + OUT EFI_HTTP_HEADER **HeaderFields, + OUT UINTN *FieldCount + ) +{ + EFI_STATUS Status; + CHAR8 *TempHttpMessage; + CHAR8 *Token; + CHAR8 *NextToken; + CHAR8 *FieldName; + CHAR8 *FieldValue; + UINTN Index; + UINTN HttpBufferSize; + + Status = EFI_SUCCESS; + TempHttpMessage = NULL; + Token = NULL; + NextToken = NULL; + FieldName = NULL; + FieldValue = NULL; + Index = 0; + + if (This == NULL || HttpMessage == NULL || HeaderFields == NULL || FieldCount == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Append the http response string along with a Null-terminator. + // + HttpBufferSize = HttpMessageSize + 1; + TempHttpMessage = AllocatePool (HttpBufferSize); + if (TempHttpMessage == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (TempHttpMessage, HttpMessage, HttpMessageSize); + *(TempHttpMessage + HttpMessageSize) = '\0'; + + // + // Get header number + // + *FieldCount = 0; + Token = TempHttpMessage; + while (TRUE) { + FieldName = NULL; + FieldValue = NULL; + NextToken = HttpGetFieldNameAndValue (Token, &FieldName, &FieldValue); + Token = NextToken; + if (FieldName == NULL || FieldValue == NULL) { + break; + } + + (*FieldCount)++; + } + + if (*FieldCount == 0) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + // + // Allocate buffer for header + // + *HeaderFields = AllocateZeroPool ((*FieldCount) * sizeof(EFI_HTTP_HEADER)); + if (*HeaderFields == NULL) { + *FieldCount = 0; + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + CopyMem (TempHttpMessage, HttpMessage, HttpMessageSize); + + // + // Set Field and Value to each header + // + Token = TempHttpMessage; + while (Index < *FieldCount) { + FieldName = NULL; + FieldValue = NULL; + NextToken = HttpGetFieldNameAndValue (Token, &FieldName, &FieldValue); + Token = NextToken; + if (FieldName == NULL || FieldValue == NULL) { + break; + } + + Status = HttpSetFieldNameAndValue (&(*HeaderFields)[Index], FieldName, FieldValue); + if (EFI_ERROR (Status)) { + *FieldCount = 0; + HttpFreeHeaderFields (*HeaderFields, Index); + goto ON_EXIT; + } + + Index++; + } + + // + // Free allocated buffer + // +ON_EXIT: + if (TempHttpMessage != NULL) { + FreePool (TempHttpMessage); + } + + return Status; +} diff --git a/NetworkPkg/IScsiDxe/ComponentName.c b/NetworkPkg/IScsiDxe/ComponentName.c new file mode 100644 index 000000000..77e6f3858 --- /dev/null +++ b/NetworkPkg/IScsiDxe/ComponentName.c @@ -0,0 +1,338 @@ +/** @file + UEFI Component Name(2) protocol implementation for iSCSI. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gIScsiComponentName = { + IScsiComponentNameGetDriverName, + IScsiComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gIScsiComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) IScsiComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) IScsiComponentNameGetControllerName, + "en" +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mIScsiDriverNameTable[] = { + { + "eng;en", + L"iSCSI Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gIScsiControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +IScsiComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mIScsiDriverNameTable, + DriverName, + (BOOLEAN) (This == &gIScsiComponentName) + ); +} + +/** + Update the component name for the iSCSI instance. + + @param[in] IScsiExtScsiPassThru A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance. + @param[in] Ipv6Flag TRUE if IP6 network stack is used. + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + @retval EFI_UNSUPPORTED Can't get the corresponding NIC info from the Controller handle. + +**/ +EFI_STATUS +UpdateName ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *IScsiExtScsiPassThru, + IN BOOLEAN Ipv6Flag + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[80]; + ISCSI_DRIVER_DATA *Private; + UINT8 NicIndex; + + if (IScsiExtScsiPassThru == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (IScsiExtScsiPassThru); + NicIndex = Private->Session->ConfigData->NicIndex; + + UnicodeSPrint ( + HandleName, + sizeof (HandleName), + L"iSCSI (%s, NicIndex=%d)", + Ipv6Flag ? L"IPv6" : L"IPv4", + NicIndex + ); + + if (gIScsiControllerNameTable != NULL) { + FreeUnicodeStringTable (gIScsiControllerNameTable); + gIScsiControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gIScsiComponentName.SupportedLanguages, + &gIScsiControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gIScsiComponentName2.SupportedLanguages, + &gIScsiControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language, from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +IScsiComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + + EFI_HANDLE IScsiController; + BOOLEAN Ipv6Flag; + EFI_GUID *IScsiPrivateGuid; + ISCSI_PRIVATE_PROTOCOL *IScsiIdentifier; + + EFI_EXT_SCSI_PASS_THRU_PROTOCOL *IScsiExtScsiPassThru; + + if (ControllerHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Get the handle of the controller we are controling. + // + IScsiController = NetLibGetNicHandle (ControllerHandle, &gEfiTcp4ProtocolGuid); + if (IScsiController != NULL) { + IScsiPrivateGuid = &gIScsiV4PrivateGuid; + Ipv6Flag = FALSE; + } else { + IScsiController = NetLibGetNicHandle (ControllerHandle, &gEfiTcp6ProtocolGuid); + if (IScsiController != NULL) { + IScsiPrivateGuid = &gIScsiV6PrivateGuid; + Ipv6Flag = TRUE; + } else { + return EFI_UNSUPPORTED; + } + } + + Status = gBS->OpenProtocol ( + IScsiController, + IScsiPrivateGuid, + (VOID **) &IScsiIdentifier, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if(ChildHandle != NULL) { + if(!Ipv6Flag) { + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiTcp4ProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + } else { + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiTcp6ProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiExtScsiPassThruProtocolGuid, + (VOID **)&IScsiExtScsiPassThru, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateName (IScsiExtScsiPassThru, Ipv6Flag); + if (EFI_ERROR (Status)) { + return Status; + } + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gIScsiControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gIScsiComponentName) + ); +} diff --git a/NetworkPkg/IScsiDxe/IScsiAuthenticationInfo.c b/NetworkPkg/IScsiDxe/IScsiAuthenticationInfo.c new file mode 100644 index 000000000..aeff64615 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiAuthenticationInfo.c @@ -0,0 +1,61 @@ +/** @file + Implementation for EFI_AUTHENTICATION_INFO_PROTOCOL. Currently it is a + dummy support. + +Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + +EFI_AUTHENTICATION_INFO_PROTOCOL gIScsiAuthenticationInfo = { + IScsiGetAuthenticationInfo, + IScsiSetAuthenticationInfo +}; + +/** + Retrieves the authentication information associated with a particular controller handle. + + @param[in] This Pointer to the EFI_AUTHENTICATION_INFO_PROTOCOL. + @param[in] ControllerHandle Handle to the Controller. + @param[out] Buffer Pointer to the authentication information. This function is + responsible for allocating the buffer and it is the caller's + responsibility to free buffer when the caller is finished with buffer. + + @retval EFI_DEVICE_ERROR The authentication information could not be + retrieved due to a hardware error. + +**/ +EFI_STATUS +EFIAPI +IScsiGetAuthenticationInfo ( + IN EFI_AUTHENTICATION_INFO_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + OUT VOID **Buffer + ) +{ + return EFI_DEVICE_ERROR; +} + +/** + Set the authentication information for a given controller handle. + + @param[in] This Pointer to the EFI_AUTHENTICATION_INFO_PROTOCOL. + @param[in] ControllerHandle Handle to the Controller. + @param[in] Buffer Pointer to the authentication information. + + @retval EFI_UNSUPPORTED If the platform policies do not allow setting of + the authentication information. + +**/ +EFI_STATUS +EFIAPI +IScsiSetAuthenticationInfo ( + IN EFI_AUTHENTICATION_INFO_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN VOID *Buffer + ) +{ + return EFI_UNSUPPORTED; +} diff --git a/NetworkPkg/IScsiDxe/IScsiCHAP.c b/NetworkPkg/IScsiDxe/IScsiCHAP.c new file mode 100644 index 000000000..627882af0 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiCHAP.c @@ -0,0 +1,471 @@ +/** @file + This file is for Challenge-Handshake Authentication Protocol (CHAP) Configuration. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + +/** + Initator calculates its own expected hash value. + + @param[in] ChapIdentifier iSCSI CHAP identifier sent by authenticator. + @param[in] ChapSecret iSCSI CHAP secret of the authenticator. + @param[in] SecretLength The length of iSCSI CHAP secret. + @param[in] ChapChallenge The challenge message sent by authenticator. + @param[in] ChallengeLength The length of iSCSI CHAP challenge message. + @param[out] ChapResponse The calculation of the expected hash value. + + @retval EFI_SUCCESS The expected hash value was calculatedly successfully. + @retval EFI_PROTOCOL_ERROR The length of the secret should be at least the + length of the hash value for the hashing algorithm chosen. + @retval EFI_PROTOCOL_ERROR MD5 hash operation fail. + @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete MD5. + +**/ +EFI_STATUS +IScsiCHAPCalculateResponse ( + IN UINT32 ChapIdentifier, + IN CHAR8 *ChapSecret, + IN UINT32 SecretLength, + IN UINT8 *ChapChallenge, + IN UINT32 ChallengeLength, + OUT UINT8 *ChapResponse + ) +{ + UINTN Md5ContextSize; + VOID *Md5Ctx; + CHAR8 IdByte[1]; + EFI_STATUS Status; + + if (SecretLength < ISCSI_CHAP_SECRET_MIN_LEN) { + return EFI_PROTOCOL_ERROR; + } + + Md5ContextSize = Md5GetContextSize (); + Md5Ctx = AllocatePool (Md5ContextSize); + if (Md5Ctx == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = EFI_PROTOCOL_ERROR; + + if (!Md5Init (Md5Ctx)) { + goto Exit; + } + + // + // Hash Identifier - Only calculate 1 byte data (RFC1994) + // + IdByte[0] = (CHAR8) ChapIdentifier; + if (!Md5Update (Md5Ctx, IdByte, 1)) { + goto Exit; + } + + // + // Hash Secret + // + if (!Md5Update (Md5Ctx, ChapSecret, SecretLength)) { + goto Exit; + } + + // + // Hash Challenge received from Target + // + if (!Md5Update (Md5Ctx, ChapChallenge, ChallengeLength)) { + goto Exit; + } + + if (Md5Final (Md5Ctx, ChapResponse)) { + Status = EFI_SUCCESS; + } + +Exit: + FreePool (Md5Ctx); + return Status; +} + +/** + The initator checks the CHAP response replied by target against its own + calculation of the expected hash value. + + @param[in] AuthData iSCSI CHAP authentication data. + @param[in] TargetResponse The response from target. + + @retval EFI_SUCCESS The response from target passed authentication. + @retval EFI_SECURITY_VIOLATION The response from target was not expected value. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiCHAPAuthTarget ( + IN ISCSI_CHAP_AUTH_DATA *AuthData, + IN UINT8 *TargetResponse + ) +{ + EFI_STATUS Status; + UINT32 SecretSize; + UINT8 VerifyRsp[ISCSI_CHAP_RSP_LEN]; + + Status = EFI_SUCCESS; + + SecretSize = (UINT32) AsciiStrLen (AuthData->AuthConfig->ReverseCHAPSecret); + Status = IScsiCHAPCalculateResponse ( + AuthData->OutIdentifier, + AuthData->AuthConfig->ReverseCHAPSecret, + SecretSize, + AuthData->OutChallenge, + AuthData->OutChallengeLength, + VerifyRsp + ); + + if (CompareMem (VerifyRsp, TargetResponse, ISCSI_CHAP_RSP_LEN) != 0) { + Status = EFI_SECURITY_VIOLATION; + } + + return Status; +} + + +/** + This function checks the received iSCSI Login Response during the security + negotiation stage. + + @param[in] Conn The iSCSI connection. + + @retval EFI_SUCCESS The Login Response passed the CHAP validation. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_PROTOCOL_ERROR Some kind of protocol error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiCHAPOnRspReceived ( + IN ISCSI_CONNECTION *Conn + ) +{ + EFI_STATUS Status; + ISCSI_SESSION *Session; + ISCSI_CHAP_AUTH_DATA *AuthData; + CHAR8 *Value; + UINT8 *Data; + UINT32 Len; + LIST_ENTRY *KeyValueList; + UINTN Algorithm; + CHAR8 *Identifier; + CHAR8 *Challenge; + CHAR8 *Name; + CHAR8 *Response; + UINT8 TargetRsp[ISCSI_CHAP_RSP_LEN]; + UINT32 RspLen; + UINTN Result; + + ASSERT (Conn->CurrentStage == ISCSI_SECURITY_NEGOTIATION); + ASSERT (Conn->RspQue.BufNum != 0); + + Session = Conn->Session; + AuthData = &Session->AuthData.CHAP; + Len = Conn->RspQue.BufSize; + Data = AllocateZeroPool (Len); + if (Data == NULL) { + return EFI_OUT_OF_RESOURCES; + } + // + // Copy the data in case the data spans over multiple PDUs. + // + NetbufQueCopy (&Conn->RspQue, 0, Len, Data); + + // + // Build the key-value list from the data segment of the Login Response. + // + KeyValueList = IScsiBuildKeyValueList ((CHAR8 *) Data, Len); + if (KeyValueList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Status = EFI_PROTOCOL_ERROR; + + switch (Conn->AuthStep) { + case ISCSI_AUTH_INITIAL: + // + // The first Login Response. + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG); + if (Value == NULL) { + goto ON_EXIT; + } + + Result = IScsiNetNtoi (Value); + if (Result > 0xFFFF) { + goto ON_EXIT; + } + + Session->TargetPortalGroupTag = (UINT16) Result; + + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_AUTH_METHOD); + if (Value == NULL) { + goto ON_EXIT; + } + // + // Initiator mandates CHAP authentication but target replies without "CHAP", or + // initiator suggets "None" but target replies with some kind of auth method. + // + if (Session->AuthType == ISCSI_AUTH_TYPE_NONE) { + if (AsciiStrCmp (Value, ISCSI_KEY_VALUE_NONE) != 0) { + goto ON_EXIT; + } + } else if (Session->AuthType == ISCSI_AUTH_TYPE_CHAP) { + if (AsciiStrCmp (Value, ISCSI_AUTH_METHOD_CHAP) != 0) { + goto ON_EXIT; + } + } else { + goto ON_EXIT; + } + + // + // Transit to CHAP step one. + // + Conn->AuthStep = ISCSI_CHAP_STEP_ONE; + Status = EFI_SUCCESS; + break; + + case ISCSI_CHAP_STEP_TWO: + // + // The Target replies with CHAP_A= CHAP_I= CHAP_C= + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_CHAP_ALGORITHM); + if (Value == NULL) { + goto ON_EXIT; + } + + Algorithm = IScsiNetNtoi (Value); + if (Algorithm != ISCSI_CHAP_ALGORITHM_MD5) { + // + // Unsupported algorithm is chosen by target. + // + goto ON_EXIT; + } + + Identifier = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_CHAP_IDENTIFIER); + if (Identifier == NULL) { + goto ON_EXIT; + } + + Challenge = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_CHAP_CHALLENGE); + if (Challenge == NULL) { + goto ON_EXIT; + } + // + // Process the CHAP identifier and CHAP Challenge from Target. + // Calculate Response value. + // + Result = IScsiNetNtoi (Identifier); + if (Result > 0xFF) { + goto ON_EXIT; + } + + AuthData->InIdentifier = (UINT32) Result; + AuthData->InChallengeLength = ISCSI_CHAP_AUTH_MAX_LEN; + IScsiHexToBin ((UINT8 *) AuthData->InChallenge, &AuthData->InChallengeLength, Challenge); + Status = IScsiCHAPCalculateResponse ( + AuthData->InIdentifier, + AuthData->AuthConfig->CHAPSecret, + (UINT32) AsciiStrLen (AuthData->AuthConfig->CHAPSecret), + AuthData->InChallenge, + AuthData->InChallengeLength, + AuthData->CHAPResponse + ); + + // + // Transit to next step. + // + Conn->AuthStep = ISCSI_CHAP_STEP_THREE; + break; + + case ISCSI_CHAP_STEP_THREE: + // + // One way CHAP authentication and the target would like to + // authenticate us. + // + Status = EFI_SUCCESS; + break; + + case ISCSI_CHAP_STEP_FOUR: + ASSERT (AuthData->AuthConfig->CHAPType == ISCSI_CHAP_MUTUAL); + // + // The forth step, CHAP_N= CHAP_R= is received from Target. + // + Name = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_CHAP_NAME); + if (Name == NULL) { + goto ON_EXIT; + } + + Response = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_CHAP_RESPONSE); + if (Response == NULL) { + goto ON_EXIT; + } + + RspLen = ISCSI_CHAP_RSP_LEN; + IScsiHexToBin (TargetRsp, &RspLen, Response); + + // + // Check the CHAP Name and Response replied by Target. + // + Status = IScsiCHAPAuthTarget (AuthData, TargetRsp); + break; + + default: + break; + } + +ON_EXIT: + + if (KeyValueList != NULL) { + IScsiFreeKeyValueList (KeyValueList); + } + + FreePool (Data); + + return Status; +} + + +/** + This function fills the CHAP authentication information into the login PDU + during the security negotiation stage in the iSCSI connection login. + + @param[in] Conn The iSCSI connection. + @param[in, out] Pdu The PDU to send out. + + @retval EFI_SUCCESS All check passed and the phase-related CHAP + authentication info is filled into the iSCSI PDU. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_PROTOCOL_ERROR Some kind of protocol error occurred. + +**/ +EFI_STATUS +IScsiCHAPToSendReq ( + IN ISCSI_CONNECTION *Conn, + IN OUT NET_BUF *Pdu + ) +{ + EFI_STATUS Status; + ISCSI_SESSION *Session; + ISCSI_LOGIN_REQUEST *LoginReq; + ISCSI_CHAP_AUTH_DATA *AuthData; + CHAR8 *Value; + CHAR8 ValueStr[256]; + CHAR8 *Response; + UINT32 RspLen; + CHAR8 *Challenge; + UINT32 ChallengeLen; + + ASSERT (Conn->CurrentStage == ISCSI_SECURITY_NEGOTIATION); + + Session = Conn->Session; + AuthData = &Session->AuthData.CHAP; + LoginReq = (ISCSI_LOGIN_REQUEST *) NetbufGetByte (Pdu, 0, 0); + if (LoginReq == NULL) { + return EFI_PROTOCOL_ERROR; + } + Status = EFI_SUCCESS; + + RspLen = 2 * ISCSI_CHAP_RSP_LEN + 3; + Response = AllocateZeroPool (RspLen); + if (Response == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + ChallengeLen = 2 * ISCSI_CHAP_RSP_LEN + 3; + Challenge = AllocateZeroPool (ChallengeLen); + if (Challenge == NULL) { + FreePool (Response); + return EFI_OUT_OF_RESOURCES; + } + + switch (Conn->AuthStep) { + case ISCSI_AUTH_INITIAL: + // + // It's the initial Login Request. Fill in the key=value pairs mandatory + // for the initial Login Request. + // + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_INITIATOR_NAME, mPrivate->InitiatorName); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_SESSION_TYPE, "Normal"); + IScsiAddKeyValuePair ( + Pdu, + ISCSI_KEY_TARGET_NAME, + Session->ConfigData->SessionConfigData.TargetName + ); + + if (Session->AuthType == ISCSI_AUTH_TYPE_NONE) { + Value = ISCSI_KEY_VALUE_NONE; + ISCSI_SET_FLAG (LoginReq, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT); + } else { + Value = ISCSI_AUTH_METHOD_CHAP; + } + + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_AUTH_METHOD, Value); + + break; + + case ISCSI_CHAP_STEP_ONE: + // + // First step, send the Login Request with CHAP_A= key-value pair. + // + AsciiSPrint (ValueStr, sizeof (ValueStr), "%d", ISCSI_CHAP_ALGORITHM_MD5); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_CHAP_ALGORITHM, ValueStr); + + Conn->AuthStep = ISCSI_CHAP_STEP_TWO; + break; + + case ISCSI_CHAP_STEP_THREE: + // + // Third step, send the Login Request with CHAP_N= CHAP_R= or + // CHAP_N= CHAP_R= CHAP_I= CHAP_C= if target authentication is + // required too. + // + // CHAP_N= + // + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_CHAP_NAME, (CHAR8 *) &AuthData->AuthConfig->CHAPName); + // + // CHAP_R= + // + IScsiBinToHex ((UINT8 *) AuthData->CHAPResponse, ISCSI_CHAP_RSP_LEN, Response, &RspLen); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_CHAP_RESPONSE, Response); + + if (AuthData->AuthConfig->CHAPType == ISCSI_CHAP_MUTUAL) { + // + // CHAP_I= + // + IScsiGenRandom ((UINT8 *) &AuthData->OutIdentifier, 1); + AsciiSPrint (ValueStr, sizeof (ValueStr), "%d", AuthData->OutIdentifier); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_CHAP_IDENTIFIER, ValueStr); + // + // CHAP_C= + // + IScsiGenRandom ((UINT8 *) AuthData->OutChallenge, ISCSI_CHAP_RSP_LEN); + AuthData->OutChallengeLength = ISCSI_CHAP_RSP_LEN; + IScsiBinToHex ((UINT8 *) AuthData->OutChallenge, ISCSI_CHAP_RSP_LEN, Challenge, &ChallengeLen); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_CHAP_CHALLENGE, Challenge); + + Conn->AuthStep = ISCSI_CHAP_STEP_FOUR; + } + // + // Set the stage transition flag. + // + ISCSI_SET_FLAG (LoginReq, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT); + break; + + default: + Status = EFI_PROTOCOL_ERROR; + break; + } + + FreePool (Response); + FreePool (Challenge); + + return Status; +} diff --git a/NetworkPkg/IScsiDxe/IScsiCHAP.h b/NetworkPkg/IScsiDxe/IScsiCHAP.h new file mode 100644 index 000000000..140bba0dc --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiCHAP.h @@ -0,0 +1,102 @@ +/** @file + The header file of CHAP configuration. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ISCSI_CHAP_H_ +#define _ISCSI_CHAP_H_ + +#define ISCSI_AUTH_METHOD_CHAP "CHAP" + +#define ISCSI_KEY_CHAP_ALGORITHM "CHAP_A" +#define ISCSI_KEY_CHAP_IDENTIFIER "CHAP_I" +#define ISCSI_KEY_CHAP_CHALLENGE "CHAP_C" +#define ISCSI_KEY_CHAP_NAME "CHAP_N" +#define ISCSI_KEY_CHAP_RESPONSE "CHAP_R" + +#define ISCSI_CHAP_ALGORITHM_MD5 5 + +#define ISCSI_CHAP_AUTH_MAX_LEN 1024 +/// +/// MD5_HASHSIZE +/// +#define ISCSI_CHAP_RSP_LEN 16 + +#define ISCSI_CHAP_STEP_ONE 1 +#define ISCSI_CHAP_STEP_TWO 2 +#define ISCSI_CHAP_STEP_THREE 3 +#define ISCSI_CHAP_STEP_FOUR 4 + + +#pragma pack(1) + +typedef struct _ISCSI_CHAP_AUTH_CONFIG_NVDATA { + UINT8 CHAPType; + CHAR8 CHAPName[ISCSI_CHAP_NAME_STORAGE]; + CHAR8 CHAPSecret[ISCSI_CHAP_SECRET_STORAGE]; + CHAR8 ReverseCHAPName[ISCSI_CHAP_NAME_STORAGE]; + CHAR8 ReverseCHAPSecret[ISCSI_CHAP_SECRET_STORAGE]; +} ISCSI_CHAP_AUTH_CONFIG_NVDATA; + +#pragma pack() + +/// +/// ISCSI CHAP Authentication Data +/// +typedef struct _ISCSI_CHAP_AUTH_DATA { + ISCSI_CHAP_AUTH_CONFIG_NVDATA *AuthConfig; + UINT32 InIdentifier; + UINT8 InChallenge[ISCSI_CHAP_AUTH_MAX_LEN]; + UINT32 InChallengeLength; + // + // Calculated CHAP Response (CHAP_R) value. + // + UINT8 CHAPResponse[ISCSI_CHAP_RSP_LEN]; + + // + // Auth-data to be sent out for mutual authentication. + // + UINT32 OutIdentifier; + UINT8 OutChallenge[ISCSI_CHAP_AUTH_MAX_LEN]; + UINT32 OutChallengeLength; +} ISCSI_CHAP_AUTH_DATA; + +/** + This function checks the received iSCSI Login Response during the security + negotiation stage. + + @param[in] Conn The iSCSI connection. + + @retval EFI_SUCCESS The Login Response passed the CHAP validation. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_PROTOCOL_ERROR Some kind of protocol error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiCHAPOnRspReceived ( + IN ISCSI_CONNECTION *Conn + ); +/** + This function fills the CHAP authentication information into the login PDU + during the security negotiation stage in the iSCSI connection login. + + @param[in] Conn The iSCSI connection. + @param[in, out] Pdu The PDU to send out. + + @retval EFI_SUCCESS All check passed and the phase-related CHAP + authentication info is filled into the iSCSI PDU. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_PROTOCOL_ERROR Some kind of protocol error occurred. + +**/ +EFI_STATUS +IScsiCHAPToSendReq ( + IN ISCSI_CONNECTION *Conn, + IN OUT NET_BUF *Pdu + ); + +#endif diff --git a/NetworkPkg/IScsiDxe/IScsiConfig.c b/NetworkPkg/IScsiDxe/IScsiConfig.c new file mode 100644 index 000000000..b876da7f5 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiConfig.c @@ -0,0 +1,3934 @@ +/** @file + Helper functions for configuring or getting the parameters relating to iSCSI. + +Copyright (c) 2004 - 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + +CHAR16 mVendorStorageName[] = L"ISCSI_CONFIG_IFR_NVDATA"; +ISCSI_FORM_CALLBACK_INFO *mCallbackInfo = NULL; + +HII_VENDOR_DEVICE_PATH mIScsiHiiVendorDevicePath = { + { + { + HARDWARE_DEVICE_PATH, + HW_VENDOR_DP, + { + (UINT8) (sizeof (VENDOR_DEVICE_PATH)), + (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8) + } + }, + ISCSI_CONFIG_GUID + }, + { + END_DEVICE_PATH_TYPE, + END_ENTIRE_DEVICE_PATH_SUBTYPE, + { + (UINT8) (END_DEVICE_PATH_LENGTH), + (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8) + } + } +}; + + +/** + Convert the IP address into a dotted string. + + @param[in] Ip The IP address. + @param[in] Ipv6Flag Indicates whether the IP address is version 4 or version 6. + @param[out] Str The formatted IP string. + +**/ +VOID +IScsiIpToStr ( + IN EFI_IP_ADDRESS *Ip, + IN BOOLEAN Ipv6Flag, + OUT CHAR16 *Str + ) +{ + EFI_IPv4_ADDRESS *Ip4; + EFI_IPv6_ADDRESS *Ip6; + UINTN Index; + BOOLEAN Short; + UINTN Number; + CHAR16 FormatString[8]; + + if (!Ipv6Flag) { + Ip4 = &Ip->v4; + + UnicodeSPrint ( + Str, + (UINTN) 2 * IP4_STR_MAX_SIZE, + L"%d.%d.%d.%d", + (UINTN) Ip4->Addr[0], + (UINTN) Ip4->Addr[1], + (UINTN) Ip4->Addr[2], + (UINTN) Ip4->Addr[3] + ); + + return ; + } + + Ip6 = &Ip->v6; + Short = FALSE; + + for (Index = 0; Index < 15; Index = Index + 2) { + if (!Short && + Index % 2 == 0 && + Ip6->Addr[Index] == 0 && + Ip6->Addr[Index + 1] == 0 + ) { + // + // Deal with the case of ::. + // + if (Index == 0) { + *Str = L':'; + *(Str + 1) = L':'; + Str = Str + 2; + } else { + *Str = L':'; + Str = Str + 1; + } + + while ((Index < 15) && (Ip6->Addr[Index] == 0) && (Ip6->Addr[Index + 1] == 0)) { + Index = Index + 2; + } + + Short = TRUE; + + if (Index == 16) { + // + // :: is at the end of the address. + // + *Str = L'\0'; + break; + } + } + + ASSERT (Index < 15); + + if (Ip6->Addr[Index] == 0) { + Number = UnicodeSPrint (Str, 2 * IP_STR_MAX_SIZE, L"%x:", (UINTN) Ip6->Addr[Index + 1]); + } else { + if (Ip6->Addr[Index + 1] < 0x10) { + CopyMem (FormatString, L"%x0%x:", StrSize (L"%x0%x:")); + } else { + CopyMem (FormatString, L"%x%x:", StrSize (L"%x%x:")); + } + + Number = UnicodeSPrint ( + Str, + 2 * IP_STR_MAX_SIZE, + (CONST CHAR16 *) FormatString, + (UINTN) Ip6->Addr[Index], + (UINTN) Ip6->Addr[Index + 1] + ); + } + + Str = Str + Number; + + if (Index + 2 == 16) { + *Str = L'\0'; + if (*(Str - 1) == L':') { + *(Str - 1) = L'\0'; + } + } + } +} + +/** + Check whether the input IP address is valid. + + @param[in] Ip The IP address. + @param[in] IpMode Indicates iSCSI running on IP4 or IP6 stack. + + @retval TRUE The input IP address is valid. + @retval FALSE Otherwise + +**/ +BOOLEAN +IpIsUnicast ( + IN EFI_IP_ADDRESS *Ip, + IN UINT8 IpMode + ) +{ + if (IpMode == IP_MODE_IP4) { + if (IP4_IS_UNSPECIFIED (NTOHL (Ip->Addr[0])) || IP4_IS_LOCAL_BROADCAST (NTOHL (Ip->Addr[0]))) { + return FALSE; + } + return TRUE; + } else if (IpMode == IP_MODE_IP6) { + return NetIp6IsValidUnicast (&Ip->v6); + } else { + DEBUG ((DEBUG_ERROR, "IpMode %d is invalid when configuring the iSCSI target IP!\n", IpMode)); + return FALSE; + } +} + +/** + Parse IsId in string format and convert it to binary. + + @param[in] String The buffer of the string to be parsed. + @param[in, out] IsId The buffer to store IsId. + + @retval EFI_SUCCESS The operation finished successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + +**/ +EFI_STATUS +IScsiParseIsIdFromString ( + IN CONST CHAR16 *String, + IN OUT UINT8 *IsId + ) +{ + UINT8 Index; + CHAR16 *IsIdStr; + CHAR16 TempStr[3]; + UINTN NodeVal; + CHAR16 PortString[ISCSI_NAME_IFR_MAX_SIZE]; + EFI_INPUT_KEY Key; + + if ((String == NULL) || (IsId == NULL)) { + return EFI_INVALID_PARAMETER; + } + + IsIdStr = (CHAR16 *) String; + + if (StrLen (IsIdStr) != 6 && StrLen (IsIdStr) != 12) { + UnicodeSPrint ( + PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Error! Only last 3 bytes are configurable, please input 6 hex numbers for last 3 bytes only or 12 hex numbers for full SSID!\n" + ); + + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + PortString, + NULL + ); + + return EFI_INVALID_PARAMETER; + } + + if (StrLen (IsIdStr) == 12) { + IsIdStr += 6; + } + + for (Index = 3; Index < 6; Index++) { + CopyMem (TempStr, IsIdStr, sizeof (TempStr)); + TempStr[2] = L'\0'; + + // + // Convert the string to IsId. StrHexToUintn stops at the first character + // that is not a valid hex character, '\0' here. + // + NodeVal = StrHexToUintn (TempStr); + + IsId[Index] = (UINT8) NodeVal; + + IsIdStr = IsIdStr + 2; + } + + return EFI_SUCCESS; +} + +/** + Convert IsId from binary to string format. + + @param[out] String The buffer to store the converted string. + @param[in] IsId The buffer to store IsId. + + @retval EFI_SUCCESS The string converted successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + +**/ +EFI_STATUS +IScsiConvertIsIdToString ( + OUT CHAR16 *String, + IN UINT8 *IsId + ) +{ + UINT8 Index; + UINTN Number; + + if ((String == NULL) || (IsId == NULL)) { + return EFI_INVALID_PARAMETER; + } + + for (Index = 0; Index < 6; Index++) { + if (IsId[Index] <= 0xF) { + Number = UnicodeSPrint ( + String, + 2 * ISID_CONFIGURABLE_STORAGE, + L"0%X", + (UINTN) IsId[Index] + ); + } else { + Number = UnicodeSPrint ( + String, + 2 * ISID_CONFIGURABLE_STORAGE, + L"%X", + (UINTN) IsId[Index] + ); + + } + + String = String + Number; + } + + *String = L'\0'; + + return EFI_SUCCESS; +} + +/** + Get the Offset value specified by the input String. + + @param[in] Configuration A null-terminated Unicode string in + format. + @param[in] String The string is "&OFFSET=". + @param[out] Value The Offset value. + + @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary + structures. + @retval EFI_SUCCESS Value of is outputted in Number + successfully. + +**/ +EFI_STATUS +IScsiGetValue ( + IN CONST EFI_STRING Configuration, + IN CHAR16 *String, + OUT UINTN *Value + ) +{ + CHAR16 *StringPtr; + CHAR16 *TmpPtr; + CHAR16 *Str; + CHAR16 TmpStr[2]; + UINTN Length; + UINTN Len; + UINTN Index; + UINT8 *Buf; + UINT8 DigitUint8; + EFI_STATUS Status; + + // + // Get Value. + // + Buf = NULL; + StringPtr = StrStr (Configuration, String); + ASSERT(StringPtr != NULL); + StringPtr += StrLen (String); + TmpPtr = StringPtr; + + while (*StringPtr != L'\0' && *StringPtr != L'&') { + StringPtr ++; + } + Length = StringPtr - TmpPtr; + Len = Length + 1; + + Str = AllocateZeroPool (Len * sizeof (CHAR16)); + if (Str == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + CopyMem (Str, TmpPtr, Len * sizeof (CHAR16)); + *(Str + Length) = L'\0'; + + Len = (Len + 1) / 2; + Buf = (UINT8 *) AllocateZeroPool (Len); + if (Buf == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + ZeroMem (TmpStr, sizeof (TmpStr)); + for (Index = 0; Index < Length; Index ++) { + TmpStr[0] = Str[Length - Index - 1]; + DigitUint8 = (UINT8) StrHexToUint64 (TmpStr); + if ((Index & 1) == 0) { + Buf [Index/2] = DigitUint8; + } else { + Buf [Index/2] = (UINT8) ((DigitUint8 << 4) + Buf [Index/2]); + } + } + + *Value = 0; + CopyMem ( + Value, + Buf, + (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN) + ); + + FreePool (Buf); + Status = EFI_SUCCESS; + +Exit: + if (Str != NULL) { + FreePool (Str); + } + + return Status; +} + +/** + Get the attempt config data from global structure by the ConfigIndex. + + @param[in] AttemptConfigIndex The unique index indicates the attempt. + + @return Pointer to the attempt config data. + @retval NULL The attempt configuration data cannot be found. + +**/ +ISCSI_ATTEMPT_CONFIG_NVDATA * +IScsiConfigGetAttemptByConfigIndex ( + IN UINT8 AttemptConfigIndex + ) +{ + LIST_ENTRY *Entry; + ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt; + + NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) { + Attempt = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + if (Attempt->AttemptConfigIndex == AttemptConfigIndex) { + return Attempt; + } + } + + return NULL; +} + + +/** + Get the existing attempt config data from global structure by the NicIndex. + + @param[in] NewAttempt The created new attempt + @param[in] IScsiMode The IScsi Mode of the new attempt, Enabled or + Enabled for MPIO. + + @return Pointer to the existing attempt config data which + has the same NICIndex as the new created attempt. + @retval NULL The attempt with NicIndex does not exist. + +**/ +ISCSI_ATTEMPT_CONFIG_NVDATA * +IScsiConfigGetAttemptByNic ( + IN ISCSI_ATTEMPT_CONFIG_NVDATA *NewAttempt, + IN UINT8 IScsiMode + ) +{ + LIST_ENTRY *Entry; + ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt; + + NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) { + Attempt = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + if (Attempt != NewAttempt && Attempt->NicIndex == NewAttempt->NicIndex && + Attempt->SessionConfigData.Enabled == IScsiMode) { + return Attempt; + } + } + + return NULL; +} + +/** + Extract the Index of the attempt list. + + @param[in] AttemptNameList The Name list of the Attempts. + @param[out] AttemptIndexList The Index list of the Attempts. + @param[in] IsAddAttempts If TRUE, Indicates add one or more attempts. + If FALSE, Indicates delete attempts or change attempt order. + + @retval EFI_SUCCESS The Attempt list is valid. + @retval EFI_INVALID_PARAMETERS The Attempt List is invalid. + +**/ +EFI_STATUS +IScsiGetAttemptIndexList ( + IN CHAR16 *AttemptNameList, + OUT UINT8 *AttemptIndexList, + IN BOOLEAN IsAddAttempts +) +{ + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + CHAR16 *AttemptStr; + UINT8 AttemptIndex; + UINTN Len; + UINTN Index; + + Index = 0; + + if ((AttemptNameList == NULL) || (*AttemptNameList == L'\0')) { + return EFI_INVALID_PARAMETER; + } + + AttemptStr = AttemptNameList; + Len = StrLen (L"attempt:"); + + while (*AttemptStr != L'\0') { + AttemptStr = StrStr (AttemptStr, L"attempt:"); + if (AttemptStr == NULL) { + return EFI_INVALID_PARAMETER; + } + AttemptStr += Len; + AttemptIndex = (UINT8)(*AttemptStr - L'0'); + AttemptConfigData = IScsiConfigGetAttemptByConfigIndex (AttemptIndex); + if (IsAddAttempts) { + if ((AttemptConfigData != NULL) || ((AttemptIndex) > PcdGet8 (PcdMaxIScsiAttemptNumber))) { + return EFI_INVALID_PARAMETER; + } + } else { + if (AttemptConfigData == NULL) { + return EFI_INVALID_PARAMETER; + } + } + + AttemptIndexList[Index] = AttemptIndex; + Index ++; + AttemptStr += 2; + } + return EFI_SUCCESS; +} + +/** + Convert the iSCSI configuration data into the IFR data. + + @param[in] Attempt The iSCSI attempt config data. + @param[in, out] IfrNvData The IFR nv data. + +**/ +VOID +IScsiConvertAttemptConfigDataToIfrNvData ( + IN ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt, + IN OUT ISCSI_CONFIG_IFR_NVDATA *IfrNvData + ) +{ + ISCSI_SESSION_CONFIG_NVDATA *SessionConfigData; + ISCSI_CHAP_AUTH_CONFIG_NVDATA *AuthConfigData; + EFI_IP_ADDRESS Ip; + BOOLEAN DnsMode; + + // + // Normal session configuration parameters. + // + SessionConfigData = &Attempt->SessionConfigData; + IfrNvData->Enabled = SessionConfigData->Enabled; + IfrNvData->IpMode = SessionConfigData->IpMode; + DnsMode = SessionConfigData->DnsMode; + + IfrNvData->InitiatorInfoFromDhcp = SessionConfigData->InitiatorInfoFromDhcp; + IfrNvData->TargetInfoFromDhcp = SessionConfigData->TargetInfoFromDhcp; + IfrNvData->TargetPort = SessionConfigData->TargetPort; + + if (IfrNvData->IpMode == IP_MODE_IP4) { + CopyMem (&Ip.v4, &SessionConfigData->LocalIp, sizeof (EFI_IPv4_ADDRESS)); + IScsiIpToStr (&Ip, FALSE, IfrNvData->LocalIp); + CopyMem (&Ip.v4, &SessionConfigData->SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + IScsiIpToStr (&Ip, FALSE, IfrNvData->SubnetMask); + CopyMem (&Ip.v4, &SessionConfigData->Gateway, sizeof (EFI_IPv4_ADDRESS)); + IScsiIpToStr (&Ip, FALSE, IfrNvData->Gateway); + ZeroMem (IfrNvData->TargetIp, sizeof (IfrNvData->TargetIp)); + if (SessionConfigData->TargetIp.v4.Addr[0] != '\0') { + CopyMem (&Ip.v4, &SessionConfigData->TargetIp, sizeof (EFI_IPv4_ADDRESS)); + IScsiIpToStr (&Ip, FALSE, IfrNvData->TargetIp); + } + + } else if (IfrNvData->IpMode == IP_MODE_IP6) { + ZeroMem (IfrNvData->TargetIp, sizeof (IfrNvData->TargetIp)); + if (SessionConfigData->TargetIp.v6.Addr[0] != '\0') { + IP6_COPY_ADDRESS (&Ip.v6, &SessionConfigData->TargetIp); + IScsiIpToStr (&Ip, TRUE, IfrNvData->TargetIp); + } + } + + AsciiStrToUnicodeStrS ( + SessionConfigData->TargetName, + IfrNvData->TargetName, + sizeof (IfrNvData->TargetName) / sizeof (IfrNvData->TargetName[0]) + ); + + if (DnsMode) { + AsciiStrToUnicodeStrS ( + SessionConfigData->TargetUrl, + IfrNvData->TargetIp, + sizeof (IfrNvData->TargetIp) / sizeof (IfrNvData->TargetIp[0]) + ); + } + + IScsiLunToUnicodeStr (SessionConfigData->BootLun, IfrNvData->BootLun); + IScsiConvertIsIdToString (IfrNvData->IsId, SessionConfigData->IsId); + + IfrNvData->ConnectRetryCount = SessionConfigData->ConnectRetryCount; + IfrNvData->ConnectTimeout = SessionConfigData->ConnectTimeout; + + // + // Authentication parameters. + // + IfrNvData->AuthenticationType = Attempt->AuthenticationType; + + if (IfrNvData->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) { + AuthConfigData = &Attempt->AuthConfigData.CHAP; + IfrNvData->CHAPType = AuthConfigData->CHAPType; + AsciiStrToUnicodeStrS ( + AuthConfigData->CHAPName, + IfrNvData->CHAPName, + sizeof (IfrNvData->CHAPName) / sizeof (IfrNvData->CHAPName[0]) + ); + AsciiStrToUnicodeStrS ( + AuthConfigData->CHAPSecret, + IfrNvData->CHAPSecret, + sizeof (IfrNvData->CHAPSecret) / sizeof (IfrNvData->CHAPSecret[0]) + ); + AsciiStrToUnicodeStrS ( + AuthConfigData->ReverseCHAPName, + IfrNvData->ReverseCHAPName, + sizeof (IfrNvData->ReverseCHAPName) / sizeof (IfrNvData->ReverseCHAPName[0]) + ); + AsciiStrToUnicodeStrS ( + AuthConfigData->ReverseCHAPSecret, + IfrNvData->ReverseCHAPSecret, + sizeof (IfrNvData->ReverseCHAPSecret) / sizeof (IfrNvData->ReverseCHAPSecret[0]) + ); + } + + // + // Other parameters. + // + AsciiStrToUnicodeStrS ( + Attempt->AttemptName, + IfrNvData->AttemptName, + sizeof (IfrNvData->AttemptName) / sizeof (IfrNvData->AttemptName[0]) + ); +} + +/** + Convert the iSCSI configuration data into the IFR data Which will be used + to extract the iSCSI Keyword configuration in format. + + @param[in, out] IfrNvData The IFR nv data. + +**/ +VOID +EFIAPI +IScsiConvertAttemptConfigDataToIfrNvDataByKeyword ( + IN OUT ISCSI_CONFIG_IFR_NVDATA *IfrNvData + ) +{ + LIST_ENTRY *Entry; + ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt; + ISCSI_SESSION_CONFIG_NVDATA *SessionConfigData; + ISCSI_CHAP_AUTH_CONFIG_NVDATA *AuthConfigData; + CHAR16 AttemptNameList[ATTEMPT_NAME_LIST_SIZE]; + ISCSI_NIC_INFO *NicInfo; + CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN]; + EFI_IP_ADDRESS Ip; + UINTN Index; + UINTN StringLen; + + NicInfo = NULL; + ZeroMem (AttemptNameList, sizeof (AttemptNameList)); + + if ((mPrivate != NULL) && (mPrivate->AttemptCount != 0)) { + NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) { + Attempt = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + // + // Normal session configuration parameters. + // + SessionConfigData = &Attempt->SessionConfigData; + + ASSERT ((Attempt->AttemptConfigIndex > 0) && (Attempt->AttemptConfigIndex <= FixedPcdGet8 (PcdMaxIScsiAttemptNumber))); + Index = Attempt->AttemptConfigIndex - 1; + + // + // Save the attempt to AttemptNameList as Attempt:1 Attempt:2 + // + AsciiStrToUnicodeStrS ( + Attempt->AttemptName, + AttemptNameList + StrLen (AttemptNameList), + ATTEMPT_NAME_LIST_SIZE - StrLen (AttemptNameList) + ); + + StringLen = StrLen (AttemptNameList); + ASSERT (StringLen > 2); + *(AttemptNameList + StringLen - 2) = L':'; + *(AttemptNameList + StringLen) = L' '; + + AsciiStrToUnicodeStrS ( + Attempt->AttemptName, + IfrNvData->ISCSIAttemptName + ATTEMPT_NAME_SIZE * Index, + ATTEMPT_NAME_LIST_SIZE - ATTEMPT_NAME_SIZE * Index + ); + + IfrNvData->ISCSIBootEnableList[Index] = SessionConfigData->Enabled; + IfrNvData->ISCSIIpAddressTypeList[Index] = SessionConfigData->IpMode; + + IfrNvData->ISCSIInitiatorInfoViaDHCP[Index] = SessionConfigData->InitiatorInfoFromDhcp; + IfrNvData->ISCSITargetInfoViaDHCP[Index] = SessionConfigData->TargetInfoFromDhcp; + IfrNvData->ISCSIConnectRetry[Index] = SessionConfigData->ConnectRetryCount; + IfrNvData->ISCSIConnectTimeout[Index] = SessionConfigData->ConnectTimeout; + IfrNvData->ISCSITargetTcpPort[Index] = SessionConfigData->TargetPort; + + if (SessionConfigData->IpMode == IP_MODE_IP4) { + CopyMem (&Ip.v4, &SessionConfigData->LocalIp, sizeof (EFI_IPv4_ADDRESS)); + IScsiIpToStr (&Ip, FALSE, IfrNvData->Keyword[Index].ISCSIInitiatorIpAddress); + CopyMem (&Ip.v4, &SessionConfigData->SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + IScsiIpToStr (&Ip, FALSE, IfrNvData->Keyword[Index].ISCSIInitiatorNetmask); + CopyMem (&Ip.v4, &SessionConfigData->Gateway, sizeof (EFI_IPv4_ADDRESS)); + IScsiIpToStr (&Ip, FALSE, IfrNvData->Keyword[Index].ISCSIInitiatorGateway); + if (SessionConfigData->TargetIp.v4.Addr[0] != '\0') { + CopyMem (&Ip.v4, &SessionConfigData->TargetIp, sizeof (EFI_IPv4_ADDRESS)); + IScsiIpToStr (&Ip, FALSE, IfrNvData->Keyword[Index].ISCSITargetIpAddress); + } + } else if (SessionConfigData->IpMode == IP_MODE_IP6) { + ZeroMem (IfrNvData->Keyword[Index].ISCSITargetIpAddress, sizeof (IfrNvData->TargetIp)); + if (SessionConfigData->TargetIp.v6.Addr[0] != '\0') { + IP6_COPY_ADDRESS (&Ip.v6, &SessionConfigData->TargetIp); + IScsiIpToStr (&Ip, TRUE, IfrNvData->Keyword[Index].ISCSITargetIpAddress); + } + } + + AsciiStrToUnicodeStrS ( + SessionConfigData->TargetName, + IfrNvData->Keyword[Index].ISCSITargetName, + ISCSI_NAME_MAX_SIZE + ); + + if (SessionConfigData->DnsMode) { + AsciiStrToUnicodeStrS ( + SessionConfigData->TargetUrl, + IfrNvData->Keyword[Index].ISCSITargetIpAddress, + sizeof (IfrNvData->Keyword[Index].ISCSITargetIpAddress) / sizeof (IfrNvData->Keyword[Index].ISCSITargetIpAddress[0]) + ); + } + + IScsiLunToUnicodeStr (SessionConfigData->BootLun, IfrNvData->Keyword[Index].ISCSILun); + IScsiConvertIsIdToString (IfrNvData->Keyword[Index].ISCSIIsId, SessionConfigData->IsId); + + IfrNvData->ISCSIAuthenticationMethod[Index] = Attempt->AuthenticationType; + + if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) { + AuthConfigData = &Attempt->AuthConfigData.CHAP; + IfrNvData->ISCSIChapType[Index] = AuthConfigData->CHAPType; + AsciiStrToUnicodeStrS ( + AuthConfigData->CHAPName, + IfrNvData->Keyword[Index].ISCSIChapUsername, + ISCSI_CHAP_NAME_STORAGE + ); + + AsciiStrToUnicodeStrS ( + AuthConfigData->CHAPSecret, + IfrNvData->Keyword[Index].ISCSIChapSecret, + ISCSI_CHAP_SECRET_STORAGE + ); + + AsciiStrToUnicodeStrS ( + AuthConfigData->ReverseCHAPName, + IfrNvData->Keyword[Index].ISCSIReverseChapUsername, + ISCSI_CHAP_NAME_STORAGE + ); + + AsciiStrToUnicodeStrS ( + AuthConfigData->ReverseCHAPSecret, + IfrNvData->Keyword[Index].ISCSIReverseChapSecret, + ISCSI_CHAP_SECRET_STORAGE + ); + } + } + CopyMem(IfrNvData->ISCSIDisplayAttemptList, AttemptNameList, ATTEMPT_NAME_LIST_SIZE); + + ZeroMem (IfrNvData->ISCSIMacAddr, sizeof (IfrNvData->ISCSIMacAddr)); + NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) { + NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link); + IScsiMacAddrToStr ( + &NicInfo->PermanentAddress, + NicInfo->HwAddressSize, + NicInfo->VlanId, + MacString + ); + CopyMem ( + IfrNvData->ISCSIMacAddr + StrLen (IfrNvData->ISCSIMacAddr), + MacString, + StrLen (MacString) * sizeof (CHAR16) + ); + + *(IfrNvData->ISCSIMacAddr + StrLen (IfrNvData->ISCSIMacAddr)) = L'/'; + } + + StringLen = StrLen (IfrNvData->ISCSIMacAddr); + if (StringLen > 0) { + *(IfrNvData->ISCSIMacAddr + StringLen - 1) = L'\0'; + } + } +} + +/** + Convert the IFR data to iSCSI configuration data. + + @param[in] IfrNvData Point to ISCSI_CONFIG_IFR_NVDATA. + @param[in, out] Attempt The iSCSI attempt config data. + + @retval EFI_INVALID_PARAMETER Any input or configured parameter is invalid. + @retval EFI_NOT_FOUND Cannot find the corresponding variable. + @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources. + @retval EFI_ABORTED The operation is aborted. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +IScsiConvertIfrNvDataToAttemptConfigData ( + IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData, + IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt + ) +{ + EFI_IP_ADDRESS HostIp; + EFI_IP_ADDRESS SubnetMask; + EFI_IP_ADDRESS Gateway; + CHAR16 *MacString; + CHAR16 *AttemptName1; + CHAR16 *AttemptName2; + ISCSI_ATTEMPT_CONFIG_NVDATA *ExistAttempt; + ISCSI_ATTEMPT_CONFIG_NVDATA *SameNicAttempt; + CHAR16 IScsiMode[64]; + CHAR16 IpMode[64]; + ISCSI_NIC_INFO *NicInfo; + EFI_INPUT_KEY Key; + UINT8 *AttemptConfigOrder; + UINTN AttemptConfigOrderSize; + UINT8 *AttemptOrderTmp; + UINTN TotalNumber; + EFI_STATUS Status; + + if (IfrNvData == NULL || Attempt == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Update those fields which don't have INTERACTIVE attribute. + // + Attempt->SessionConfigData.ConnectRetryCount = IfrNvData->ConnectRetryCount; + Attempt->SessionConfigData.ConnectTimeout = IfrNvData->ConnectTimeout; + Attempt->SessionConfigData.IpMode = IfrNvData->IpMode; + + if (IfrNvData->IpMode < IP_MODE_AUTOCONFIG) { + Attempt->SessionConfigData.InitiatorInfoFromDhcp = IfrNvData->InitiatorInfoFromDhcp; + Attempt->SessionConfigData.TargetPort = IfrNvData->TargetPort; + + if (Attempt->SessionConfigData.TargetPort == 0) { + Attempt->SessionConfigData.TargetPort = ISCSI_WELL_KNOWN_PORT; + } + + Attempt->SessionConfigData.TargetInfoFromDhcp = IfrNvData->TargetInfoFromDhcp; + } + + Attempt->AuthenticationType = IfrNvData->AuthenticationType; + + if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) { + Attempt->AuthConfigData.CHAP.CHAPType = IfrNvData->CHAPType; + } + + // + // Only do full parameter validation if iSCSI is enabled on this device. + // + if (IfrNvData->Enabled != ISCSI_DISABLED) { + if (Attempt->SessionConfigData.ConnectTimeout < CONNECT_MIN_TIMEOUT) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Connection Establishing Timeout is less than minimum value 100ms.", + NULL + ); + + return EFI_INVALID_PARAMETER; + } + + // + // Validate the address configuration of the Initiator if DHCP isn't + // deployed. + // + if (!Attempt->SessionConfigData.InitiatorInfoFromDhcp) { + CopyMem (&HostIp.v4, &Attempt->SessionConfigData.LocalIp, sizeof (HostIp.v4)); + CopyMem (&SubnetMask.v4, &Attempt->SessionConfigData.SubnetMask, sizeof (SubnetMask.v4)); + CopyMem (&Gateway.v4, &Attempt->SessionConfigData.Gateway, sizeof (Gateway.v4)); + + if ((Gateway.Addr[0] != 0)) { + if (SubnetMask.Addr[0] == 0) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Gateway address is set but subnet mask is zero.", + NULL + ); + + return EFI_INVALID_PARAMETER; + } else if (!IP4_NET_EQUAL (HostIp.Addr[0], Gateway.Addr[0], SubnetMask.Addr[0])) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Local IP and Gateway are not in the same subnet.", + NULL + ); + + return EFI_INVALID_PARAMETER; + } + } + } + // + // Validate target configuration if DHCP isn't deployed. + // + if (!Attempt->SessionConfigData.TargetInfoFromDhcp && Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) { + if (!Attempt->SessionConfigData.DnsMode) { + if (!IpIsUnicast (&Attempt->SessionConfigData.TargetIp, IfrNvData->IpMode)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Target IP is invalid!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + } else { + if (Attempt->SessionConfigData.TargetUrl[0] == '\0') { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"iSCSI target Url should not be NULL!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + } + + // + // Validate iSCSI target name configuration again: + // The format of iSCSI target name is already verified in IScsiFormCallback() when + // user input the name; here we only check the case user does not input the name. + // + if (Attempt->SessionConfigData.TargetName[0] == '\0') { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"iSCSI target name is NULL!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + } + + // + // Validate the authentication info. + // + if (IfrNvData->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) { + if ((IfrNvData->CHAPName[0] == '\0') || (IfrNvData->CHAPSecret[0] == '\0')) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"CHAP Name or CHAP Secret is invalid!", + NULL + ); + + return EFI_INVALID_PARAMETER; + } + + if ((IfrNvData->CHAPType == ISCSI_CHAP_MUTUAL) && + ((IfrNvData->ReverseCHAPName[0] == '\0') || (IfrNvData->ReverseCHAPSecret[0] == '\0')) + ) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Reverse CHAP Name or Reverse CHAP Secret is invalid!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + } + + // + // Check whether this attempt uses NIC which is already used by existing attempt. + // + SameNicAttempt = IScsiConfigGetAttemptByNic (Attempt, IfrNvData->Enabled); + if (SameNicAttempt != NULL) { + AttemptName1 = (CHAR16 *) AllocateZeroPool (ATTEMPT_NAME_SIZE * sizeof (CHAR16)); + if (AttemptName1 == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + AttemptName2 = (CHAR16 *) AllocateZeroPool (ATTEMPT_NAME_SIZE * sizeof (CHAR16)); + if (AttemptName2 == NULL) { + FreePool (AttemptName1); + return EFI_OUT_OF_RESOURCES; + } + + AsciiStrToUnicodeStrS (Attempt->AttemptName, AttemptName1, ATTEMPT_NAME_SIZE); + AsciiStrToUnicodeStrS (SameNicAttempt->AttemptName, AttemptName2, ATTEMPT_NAME_SIZE); + + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Warning! Attempt \"%s\" uses same NIC as Attempt \"%s\".", + AttemptName1, + AttemptName2 + ); + + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + mPrivate->PortString, + NULL + ); + + FreePool (AttemptName1); + FreePool (AttemptName2); + } + } + + // + // Update the iSCSI Mode data and record it in attempt help info. + // + if (IfrNvData->Enabled == ISCSI_DISABLED) { + UnicodeSPrint (IScsiMode, 64, L"Disabled"); + } else if (IfrNvData->Enabled == ISCSI_ENABLED) { + UnicodeSPrint (IScsiMode, 64, L"Enabled"); + } else if (IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO) { + UnicodeSPrint (IScsiMode, 64, L"Enabled for MPIO"); + } + + if (IfrNvData->IpMode == IP_MODE_IP4) { + UnicodeSPrint (IpMode, 64, L"IP4"); + } else if (IfrNvData->IpMode == IP_MODE_IP6) { + UnicodeSPrint (IpMode, 64, L"IP6"); + } else if (IfrNvData->IpMode == IP_MODE_AUTOCONFIG) { + UnicodeSPrint (IpMode, 64, L"Autoconfigure"); + } + + NicInfo = IScsiGetNicInfoByIndex (Attempt->NicIndex); + if (NicInfo == NULL) { + return EFI_NOT_FOUND; + } + + MacString = (CHAR16 *) AllocateZeroPool (ISCSI_MAX_MAC_STRING_LEN * sizeof (CHAR16)); + if (MacString == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + AsciiStrToUnicodeStrS (Attempt->MacString, MacString, ISCSI_MAX_MAC_STRING_LEN); + + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"MAC: %s, PFA: Bus %d | Dev %d | Func %d, iSCSI mode: %s, IP version: %s", + MacString, + NicInfo->BusNumber, + NicInfo->DeviceNumber, + NicInfo->FunctionNumber, + IScsiMode, + IpMode + ); + + Attempt->AttemptTitleHelpToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + Attempt->AttemptTitleHelpToken, + mPrivate->PortString, + NULL + ); + if (Attempt->AttemptTitleHelpToken == 0) { + FreePool (MacString); + return EFI_OUT_OF_RESOURCES; + } + + // + // Check whether this attempt is an existing one. + // + ExistAttempt = IScsiConfigGetAttemptByConfigIndex (Attempt->AttemptConfigIndex); + if (ExistAttempt != NULL) { + ASSERT (ExistAttempt == Attempt); + + if (IfrNvData->Enabled == ISCSI_DISABLED && + Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) { + + // + // User updates the Attempt from "Enabled"/"Enabled for MPIO" to "Disabled". + // + if (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) { + if (mPrivate->MpioCount < 1) { + return EFI_ABORTED; + } + + if (--mPrivate->MpioCount == 0) { + mPrivate->EnableMpio = FALSE; + } + } else if (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED) { + if (mPrivate->SinglePathCount < 1) { + return EFI_ABORTED; + } + mPrivate->SinglePathCount--; + } + + } else if (IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO && + Attempt->SessionConfigData.Enabled == ISCSI_ENABLED) { + // + // User updates the Attempt from "Enabled" to "Enabled for MPIO". + // + if (mPrivate->SinglePathCount < 1) { + return EFI_ABORTED; + } + + mPrivate->EnableMpio = TRUE; + mPrivate->MpioCount++; + mPrivate->SinglePathCount--; + + } else if (IfrNvData->Enabled == ISCSI_ENABLED && + Attempt->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) { + // + // User updates the Attempt from "Enabled for MPIO" to "Enabled". + // + if (mPrivate->MpioCount < 1) { + return EFI_ABORTED; + } + + if (--mPrivate->MpioCount == 0) { + mPrivate->EnableMpio = FALSE; + } + mPrivate->SinglePathCount++; + + } else if (IfrNvData->Enabled != ISCSI_DISABLED && + Attempt->SessionConfigData.Enabled == ISCSI_DISABLED) { + // + // User updates the Attempt from "Disabled" to "Enabled"/"Enabled for MPIO". + // + if (IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO) { + mPrivate->EnableMpio = TRUE; + mPrivate->MpioCount++; + + } else if (IfrNvData->Enabled == ISCSI_ENABLED) { + mPrivate->SinglePathCount++; + } + } + + } else if (ExistAttempt == NULL) { + // + // When a new attempt is created, pointer of the attempt is saved to + // mCallbackInfo->Current in IScsiConfigProcessDefault. If input Attempt + // does not match any existing attempt, it should be a new created attempt. + // Save it to system now. + // + + // + // Save current order number for this attempt. + // + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"AttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + + TotalNumber = AttemptConfigOrderSize / sizeof (UINT8); + TotalNumber++; + + // + // Append the new created attempt order to the end. + // + AttemptOrderTmp = AllocateZeroPool (TotalNumber * sizeof (UINT8)); + if (AttemptOrderTmp == NULL) { + if (AttemptConfigOrder != NULL) { + FreePool (AttemptConfigOrder); + } + return EFI_OUT_OF_RESOURCES; + } + + if (AttemptConfigOrder != NULL) { + CopyMem (AttemptOrderTmp, AttemptConfigOrder, AttemptConfigOrderSize); + FreePool (AttemptConfigOrder); + } + + AttemptOrderTmp[TotalNumber - 1] = Attempt->AttemptConfigIndex; + AttemptConfigOrder = AttemptOrderTmp; + AttemptConfigOrderSize = TotalNumber * sizeof (UINT8); + + Status = gRT->SetVariable ( + L"AttemptOrder", + &gIScsiConfigGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE, + AttemptConfigOrderSize, + AttemptConfigOrder + ); + FreePool (AttemptConfigOrder); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Insert new created attempt to array. + // + InsertTailList (&mPrivate->AttemptConfigs, &Attempt->Link); + mPrivate->AttemptCount++; + + if (IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO) { + // + // This new Attempt is enabled for MPIO; enable the multipath mode. + // + mPrivate->EnableMpio = TRUE; + mPrivate->MpioCount++; + } else if (IfrNvData->Enabled == ISCSI_ENABLED) { + mPrivate->SinglePathCount++; + } + + IScsiConfigUpdateAttempt (); + } + Attempt->SessionConfigData.Enabled = IfrNvData->Enabled; + + // + // Record the user configuration information in NVR. + // + UnicodeSPrint (mPrivate->PortString, (UINTN) ISCSI_NAME_IFR_MAX_SIZE, L"Attempt %d", Attempt->AttemptConfigIndex); + + FreePool (MacString); + + return gRT->SetVariable ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + ISCSI_CONFIG_VAR_ATTR, + sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA), + Attempt + ); +} + +/** + Convert the IFR data configured by keyword to iSCSI configuration data. + + @param[in] IfrNvData Point to ISCSI_CONFIG_IFR_NVDATA. + @param[in] OffSet The offset of the variable to the configuration structure. + + @retval EFI_INVALID_PARAMETER Any input or configured parameter is invalid. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +IScsiConvertlfrNvDataToAttemptConfigDataByKeyword ( + IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData, + IN UINTN OffSet + ) +{ + ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt; + UINT8 AttemptIndex; + UINT8 Index; + UINT8 ChapSecretLen; + UINT8 ReverseChapSecretLen; + CHAR16 *AttemptName1; + CHAR16 *AttemptName2; + ISCSI_ATTEMPT_CONFIG_NVDATA *SameNicAttempt; + CHAR8 LunString[ISCSI_LUN_STR_MAX_LEN]; + CHAR8 IScsiName[ISCSI_NAME_MAX_SIZE]; + CHAR8 IpString[IP_STR_MAX_SIZE]; + EFI_IP_ADDRESS HostIp; + EFI_IP_ADDRESS SubnetMask; + EFI_IP_ADDRESS Gateway; + EFI_INPUT_KEY Key; + UINT64 Lun; + EFI_STATUS Status; + + Attempt = NULL; + ZeroMem (IScsiName, sizeof (IScsiName)); + + if (OffSet < ATTEMPT_BOOTENABLE_VAR_OFFSET) { + return EFI_SUCCESS; + + } else if ((OffSet >= ATTEMPT_BOOTENABLE_VAR_OFFSET) && (OffSet < ATTEMPT_ADDRESS_TYPE_VAR_OFFSET)) { + AttemptIndex = (UINT8) ((OffSet - ATTEMPT_BOOTENABLE_VAR_OFFSET) + 1); + Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex); + if (Attempt == NULL) { + return EFI_INVALID_PARAMETER; + } + IfrNvData->Enabled = IfrNvData->ISCSIBootEnableList[AttemptIndex - 1]; + // + // Validate the configuration of attempt. + // + if (IfrNvData->Enabled != ISCSI_DISABLED) { + // + // Check whether this attempt uses NIC which is already used by existing attempt. + // + SameNicAttempt = IScsiConfigGetAttemptByNic (Attempt, IfrNvData->Enabled); + if (SameNicAttempt != NULL) { + AttemptName1 = (CHAR16 *) AllocateZeroPool (ATTEMPT_NAME_SIZE * sizeof (CHAR16)); + if (AttemptName1 == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + AttemptName2 = (CHAR16 *) AllocateZeroPool (ATTEMPT_NAME_SIZE * sizeof (CHAR16)); + if (AttemptName2 == NULL) { + FreePool (AttemptName1); + return EFI_OUT_OF_RESOURCES; + } + + AsciiStrToUnicodeStrS (Attempt->AttemptName, AttemptName1, ATTEMPT_NAME_SIZE); + AsciiStrToUnicodeStrS (SameNicAttempt->AttemptName, AttemptName2, ATTEMPT_NAME_SIZE); + + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Warning! \"%s\" uses same NIC as Attempt \"%s\".", + AttemptName1, + AttemptName2 + ); + + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + mPrivate->PortString, + NULL + ); + + FreePool (AttemptName1); + FreePool (AttemptName2); + } + } + + if (IfrNvData->Enabled == ISCSI_DISABLED && + Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) { + + // + // User updates the Attempt from "Enabled"/"Enabled for MPIO" to "Disabled". + // + if (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) { + if (mPrivate->MpioCount < 1) { + return EFI_ABORTED; + } + + if (--mPrivate->MpioCount == 0) { + mPrivate->EnableMpio = FALSE; + } + } else if (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED) { + if (mPrivate->SinglePathCount < 1) { + return EFI_ABORTED; + } + mPrivate->SinglePathCount--; + } + + } else if (IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO && + Attempt->SessionConfigData.Enabled == ISCSI_ENABLED) { + // + // User updates the Attempt from "Enabled" to "Enabled for MPIO". + // + if (mPrivate->SinglePathCount < 1) { + return EFI_ABORTED; + } + + mPrivate->EnableMpio = TRUE; + mPrivate->MpioCount++; + mPrivate->SinglePathCount--; + + } else if (IfrNvData->Enabled == ISCSI_ENABLED && + Attempt->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) { + // + // User updates the Attempt from "Enabled for MPIO" to "Enabled". + // + if (mPrivate->MpioCount < 1) { + return EFI_ABORTED; + } + + if (--mPrivate->MpioCount == 0) { + mPrivate->EnableMpio = FALSE; + } + mPrivate->SinglePathCount++; + + } else if (IfrNvData->Enabled != ISCSI_DISABLED && + Attempt->SessionConfigData.Enabled == ISCSI_DISABLED) { + // + // User updates the Attempt from "Disabled" to "Enabled"/"Enabled for MPIO". + // + if (IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO) { + mPrivate->EnableMpio = TRUE; + mPrivate->MpioCount++; + + } else if (IfrNvData->Enabled == ISCSI_ENABLED) { + mPrivate->SinglePathCount++; + } + } + Attempt->SessionConfigData.Enabled = IfrNvData->Enabled; + + } else if ((OffSet >= ATTEMPT_ADDRESS_TYPE_VAR_OFFSET) && (OffSet < ATTEMPT_CONNECT_RETRY_VAR_OFFSET)) { + AttemptIndex = (UINT8) ((OffSet - ATTEMPT_ADDRESS_TYPE_VAR_OFFSET) + 1); + Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex); + if (Attempt == NULL) { + return EFI_INVALID_PARAMETER; + } + Attempt->SessionConfigData.IpMode = IfrNvData->ISCSIIpAddressTypeList[AttemptIndex - 1]; + if (Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) { + Attempt->AutoConfigureMode = 0; + } + + } else if ((OffSet >= ATTEMPT_CONNECT_RETRY_VAR_OFFSET) && (OffSet < ATTEMPT_CONNECT_TIMEOUT_VAR_OFFSET)) { + AttemptIndex = (UINT8) ((OffSet - ATTEMPT_CONNECT_RETRY_VAR_OFFSET) + 1); + Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex); + if (Attempt == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (IfrNvData->ISCSIConnectRetry[AttemptIndex - 1] > CONNECT_MAX_RETRY) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"The minimum value is 0 and the maximum is 16. 0 means no retry.", + NULL + ); + return EFI_INVALID_PARAMETER; + } + Attempt->SessionConfigData.ConnectRetryCount = IfrNvData->ISCSIConnectRetry[AttemptIndex - 1]; + + } else if ((OffSet >= ATTEMPT_CONNECT_TIMEOUT_VAR_OFFSET) && (OffSet < ATTEMPT_INITIATOR_VIA_DHCP_VAR_OFFSET)) { + AttemptIndex = (UINT8) ((OffSet - ATTEMPT_CONNECT_TIMEOUT_VAR_OFFSET) / 2 + 1); + Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex); + if (Attempt == NULL) { + return EFI_INVALID_PARAMETER; + } + + if ((IfrNvData->ISCSIConnectTimeout[AttemptIndex - 1] < CONNECT_MIN_TIMEOUT) || + (IfrNvData->ISCSIConnectTimeout[AttemptIndex - 1] > CONNECT_MAX_TIMEOUT)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"The minimum value is 100 milliseconds and the maximum is 20 seconds.", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + Attempt->SessionConfigData.ConnectTimeout = IfrNvData->ISCSIConnectTimeout[AttemptIndex - 1]; + if (Attempt->SessionConfigData.ConnectTimeout == 0) { + Attempt->SessionConfigData.ConnectTimeout = CONNECT_DEFAULT_TIMEOUT; + } + + } else if ((OffSet >= ATTEMPT_INITIATOR_VIA_DHCP_VAR_OFFSET) && (OffSet < ATTEMPT_TARGET_VIA_DHCP_VAR_OFFSET)) { + AttemptIndex = (UINT8) ((OffSet - ATTEMPT_INITIATOR_VIA_DHCP_VAR_OFFSET) + 1); + Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex); + if (Attempt == NULL) { + return EFI_INVALID_PARAMETER; + } + Attempt->SessionConfigData.InitiatorInfoFromDhcp = IfrNvData->ISCSIInitiatorInfoViaDHCP[AttemptIndex - 1]; + + } else if ((OffSet >= ATTEMPT_TARGET_VIA_DHCP_VAR_OFFSET) && (OffSet < ATTEMPT_TARGET_TCP_PORT_VAR_OFFSET)) { + AttemptIndex = (UINT8) ((OffSet - ATTEMPT_TARGET_VIA_DHCP_VAR_OFFSET) + 1); + Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex); + if (Attempt == NULL) { + return EFI_INVALID_PARAMETER; + } + + if ((Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) && (Attempt->SessionConfigData.InitiatorInfoFromDhcp)) { + Attempt->SessionConfigData.TargetInfoFromDhcp = IfrNvData->ISCSITargetInfoViaDHCP[AttemptIndex - 1]; + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of IpMode or Enable DHCP!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + } else if ((OffSet >= ATTEMPT_TARGET_TCP_PORT_VAR_OFFSET) && (OffSet < ATTEMPT_AUTHENTICATION_METHOD_VAR_OFFSET)) { + AttemptIndex = (UINT8) ((OffSet - ATTEMPT_TARGET_TCP_PORT_VAR_OFFSET) / 2 + 1); + Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex); + if (Attempt == NULL) { + return EFI_INVALID_PARAMETER; + } + if ((Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) && (!Attempt->SessionConfigData.TargetInfoFromDhcp)) { + Attempt->SessionConfigData.TargetPort = IfrNvData->ISCSITargetTcpPort[AttemptIndex - 1]; + if (Attempt->SessionConfigData.TargetPort == 0) { + Attempt->SessionConfigData.TargetPort = ISCSI_WELL_KNOWN_PORT; + } + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of IpMode or Target Via DHCP!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + } else if ((OffSet >= ATTEMPT_AUTHENTICATION_METHOD_VAR_OFFSET) && (OffSet < ATTEMPT_CHARTYPE_VAR_OFFSET)) { + AttemptIndex = (UINT8) ((OffSet - ATTEMPT_AUTHENTICATION_METHOD_VAR_OFFSET) + 1); + Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex); + if (Attempt == NULL) { + return EFI_INVALID_PARAMETER; + } + + Attempt->AuthenticationType = IfrNvData->ISCSIAuthenticationMethod[AttemptIndex - 1]; + + } else if ((OffSet >= ATTEMPT_CHARTYPE_VAR_OFFSET) && (OffSet < ATTEMPT_ISID_VAR_OFFSET)) { + AttemptIndex = (UINT8) ((OffSet - ATTEMPT_CHARTYPE_VAR_OFFSET) + 1); + Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex); + if (Attempt == NULL) { + return EFI_INVALID_PARAMETER; + } + if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) { + Attempt->AuthConfigData.CHAP.CHAPType = IfrNvData->ISCSIChapType[AttemptIndex - 1]; + } + + } else if (OffSet >= ATTEMPT_ISID_VAR_OFFSET) { + Index = (UINT8) ((OffSet - ATTEMPT_ISID_VAR_OFFSET) / sizeof (KEYWORD_STR)); + AttemptIndex = Index + 1; + Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex); + if (Attempt == NULL) { + return EFI_INVALID_PARAMETER; + } + + OffSet = OffSet - Index * sizeof (KEYWORD_STR); + + if ((OffSet >= ATTEMPT_ISID_VAR_OFFSET) && (OffSet < ATTEMPT_INITIATOR_IP_ADDRESS_VAR_OFFSET)) { + IScsiParseIsIdFromString (IfrNvData->Keyword[Index].ISCSIIsId, Attempt->SessionConfigData.IsId); + + } else if ((OffSet >= ATTEMPT_INITIATOR_IP_ADDRESS_VAR_OFFSET) && (OffSet < ATTEMPT_INITIATOR_NET_MASK_VAR_OFFSET)) { + if ((Attempt->SessionConfigData.IpMode == IP_MODE_IP4) && (!Attempt->SessionConfigData.InitiatorInfoFromDhcp)) { + // + // Config Local ip + // + Status = NetLibStrToIp4 (IfrNvData->Keyword[Index].ISCSIInitiatorIpAddress, &HostIp.v4); + if (EFI_ERROR (Status) || ((Attempt->SessionConfigData.SubnetMask.Addr[0] != 0) && + !NetIp4IsUnicast (NTOHL (HostIp.Addr[0]), NTOHL(*(UINT32*)Attempt->SessionConfigData.SubnetMask.Addr)))) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid IP address!", + NULL + ); + return EFI_INVALID_PARAMETER; + } else { + CopyMem (&Attempt->SessionConfigData.LocalIp, &HostIp.v4, sizeof (HostIp.v4)); + } + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of IpMode or Enable DHCP!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + } else if ((OffSet >= ATTEMPT_INITIATOR_NET_MASK_VAR_OFFSET) && (OffSet < ATTEMPT_INITIATOR_GATE_WAY_VAR_OFFSET)) { + if ((Attempt->SessionConfigData.IpMode == IP_MODE_IP4) && (!Attempt->SessionConfigData.InitiatorInfoFromDhcp)) { + Status = NetLibStrToIp4 (IfrNvData->Keyword[Index].ISCSIInitiatorNetmask, &SubnetMask.v4); + if (EFI_ERROR (Status) || ((SubnetMask.Addr[0] != 0) && (IScsiGetSubnetMaskPrefixLength (&SubnetMask.v4) == 0))) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Subnet Mask!", + NULL + ); + return EFI_INVALID_PARAMETER; + } else { + CopyMem (&Attempt->SessionConfigData.SubnetMask, &SubnetMask.v4, sizeof (SubnetMask.v4)); + } + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of IpMode or Enable DHCP!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + } else if ((OffSet >= ATTEMPT_INITIATOR_GATE_WAY_VAR_OFFSET) && (OffSet < ATTEMPT_TARGET_NAME_VAR_OFFSET)) { + if ((Attempt->SessionConfigData.IpMode == IP_MODE_IP4) && (!Attempt->SessionConfigData.InitiatorInfoFromDhcp)) { + Status = NetLibStrToIp4 (IfrNvData->Keyword[Index].ISCSIInitiatorGateway, &Gateway.v4); + if (EFI_ERROR (Status) || + ((Gateway.Addr[0] != 0) && (Attempt->SessionConfigData.SubnetMask.Addr[0] != 0) && + !NetIp4IsUnicast (NTOHL (Gateway.Addr[0]), NTOHL(*(UINT32*)Attempt->SessionConfigData.SubnetMask.Addr)))) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Gateway!", + NULL + ); + return EFI_INVALID_PARAMETER; + } else { + CopyMem (&Attempt->SessionConfigData.Gateway, &Gateway.v4, sizeof (Gateway.v4)); + } + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of IpMode or Enable DHCP!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + } else if ((OffSet >= ATTEMPT_TARGET_NAME_VAR_OFFSET) && (OffSet < ATTEMPT_TARGET_IP_ADDRESS_VAR_OFFSET)) { + if ((Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) && (!Attempt->SessionConfigData.TargetInfoFromDhcp)) { + UnicodeStrToAsciiStrS (IfrNvData->Keyword[Index].ISCSITargetName, IScsiName, ISCSI_NAME_MAX_SIZE); + Status = IScsiNormalizeName (IScsiName, AsciiStrLen (IScsiName)); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid iSCSI Name!", + NULL + ); + } else { + AsciiStrCpyS (Attempt->SessionConfigData.TargetName, ISCSI_NAME_MAX_SIZE, IScsiName); + } + if (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) { + if (Attempt->SessionConfigData.TargetName[0] == L'\0') { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"iSCSI target name is NULL!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + } + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of IpMode or Target Via DHCP!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + } else if ((OffSet >= ATTEMPT_TARGET_IP_ADDRESS_VAR_OFFSET) && (OffSet < ATTEMPT_LUN_VAR_OFFSET)) { + if ((Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) && (!Attempt->SessionConfigData.TargetInfoFromDhcp)) { + UnicodeStrToAsciiStrS (IfrNvData->Keyword[Index].ISCSITargetIpAddress, IpString, sizeof (IpString)); + Status = IScsiAsciiStrToIp (IpString, Attempt->SessionConfigData.IpMode, &HostIp); + if (EFI_ERROR (Status) || !IpIsUnicast (&HostIp, Attempt->SessionConfigData.IpMode)) { + Attempt->SessionConfigData.DnsMode = TRUE; + ZeroMem (&Attempt->SessionConfigData.TargetIp, sizeof (Attempt->SessionConfigData.TargetIp)); + UnicodeStrToAsciiStrS (IfrNvData->Keyword[Index].ISCSITargetIpAddress, Attempt->SessionConfigData.TargetUrl, ISCSI_NAME_MAX_SIZE); + } else { + Attempt->SessionConfigData.DnsMode = FALSE; + CopyMem (&Attempt->SessionConfigData.TargetIp, &HostIp, sizeof (HostIp)); + } + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of IpMode or Target Via DHCP!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + } else if ((OffSet >= ATTEMPT_LUN_VAR_OFFSET) && (OffSet < ATTEMPT_CHAR_USER_NAME_VAR_OFFSET)) { + if ((Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) && (Attempt->SessionConfigData.TargetInfoFromDhcp == 0)) { + // + // Config LUN. + // + UnicodeStrToAsciiStrS (IfrNvData->Keyword[Index].ISCSILun, LunString, ISCSI_LUN_STR_MAX_LEN); + Status = IScsiAsciiStrToLun (LunString, (UINT8 *) &Lun); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid LUN string, Examples are: 4752-3A4F-6b7e-2F99, 6734-9-156f-127, 4186-9!", + NULL + ); + } else { + CopyMem (&Attempt->SessionConfigData.BootLun, &Lun, sizeof (Lun)); + } + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of IpMode or Target Via DHCP!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + } else if ((OffSet >= ATTEMPT_CHAR_USER_NAME_VAR_OFFSET) && (OffSet < ATTEMPT_CHAR_SECRET_VAR_OFFSET)) { + if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) { + UnicodeStrToAsciiStrS ( + IfrNvData->Keyword[Index].ISCSIChapUsername, + Attempt->AuthConfigData.CHAP.CHAPName, + ISCSI_CHAP_NAME_STORAGE + ); + + if (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) { + if (IfrNvData->Keyword[Index].ISCSIChapUsername[0] == L'\0') { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"CHAP Name is invalid!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + } + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of AuthenticationType!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + } else if ((OffSet >= ATTEMPT_CHAR_SECRET_VAR_OFFSET) && (OffSet < ATTEMPT_CHAR_REVERSE_USER_NAME_VAR_OFFSET)) { + if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) { + ChapSecretLen = (UINT8)StrLen (IfrNvData->Keyword[Index].ISCSIChapSecret); + UnicodeStrToAsciiStrS ( + IfrNvData->Keyword[Index].ISCSIChapSecret, + Attempt->AuthConfigData.CHAP.CHAPSecret, + ISCSI_CHAP_SECRET_STORAGE + ); + + if (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) { + if ((ChapSecretLen < ISCSI_CHAP_SECRET_MIN_LEN) || (ChapSecretLen > ISCSI_CHAP_SECRET_MAX_LEN)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"The Chap Secret minimum length is 12 bytes and the maximum length is 16 bytes.", + NULL + ); + return EFI_INVALID_PARAMETER; + } + } + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of AuthenticationType!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + } else if ((OffSet >= ATTEMPT_CHAR_REVERSE_USER_NAME_VAR_OFFSET) && (OffSet < ATTEMPT_CHAR_REVERSE_SECRET_VAR_OFFSET)) { + if (Attempt->AuthConfigData.CHAP.CHAPType == ISCSI_CHAP_MUTUAL) { + UnicodeStrToAsciiStrS ( + IfrNvData->Keyword[Index].ISCSIReverseChapUsername, + Attempt->AuthConfigData.CHAP.ReverseCHAPName, + ISCSI_CHAP_NAME_STORAGE + ); + if (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) { + if (IfrNvData->Keyword[Index].ISCSIReverseChapUsername[0] == L'\0') { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Reverse CHAP Name is invalid!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + } + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of AuthenticationType or Chap Type!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + + } else if (OffSet >= ATTEMPT_CHAR_REVERSE_SECRET_VAR_OFFSET) { + if (Attempt->AuthConfigData.CHAP.CHAPType == ISCSI_CHAP_MUTUAL) { + ReverseChapSecretLen = (UINT8)StrLen (IfrNvData->Keyword[Index].ISCSIReverseChapSecret); + UnicodeStrToAsciiStrS ( + IfrNvData->Keyword[Index].ISCSIReverseChapSecret, + Attempt->AuthConfigData.CHAP.ReverseCHAPSecret, + ISCSI_CHAP_SECRET_STORAGE + ); + + if (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) { + if ((ReverseChapSecretLen < ISCSI_CHAP_SECRET_MIN_LEN) || (ReverseChapSecretLen > ISCSI_CHAP_SECRET_MAX_LEN)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"The Reverse CHAP Secret minimum length is 12 bytes and the maximum length is 16 bytes.", + NULL + ); + return EFI_INVALID_PARAMETER; + } + } + } else { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Configuration, Check value of AuthenticationType or Chap Type!", + NULL + ); + return EFI_INVALID_PARAMETER; + } + } + } + + + + // + // Record the user configuration information in NVR. + // + ASSERT (Attempt != NULL); + UnicodeSPrint (mPrivate->PortString, (UINTN) ISCSI_NAME_IFR_MAX_SIZE, L"Attempt %d", Attempt->AttemptConfigIndex); + return gRT->SetVariable ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + ISCSI_CONFIG_VAR_ATTR, + sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA), + Attempt + ); + +} + +/** + Create Hii Extend Label OpCode as the start opcode and end opcode. It is + a help function. + + @param[in] StartLabelNumber The number of start label. + @param[out] StartOpCodeHandle Points to the start opcode handle. + @param[out] StartLabel Points to the created start opcode. + @param[out] EndOpCodeHandle Points to the end opcode handle. + @param[out] EndLabel Points to the created end opcode. + + @retval EFI_OUT_OF_RESOURCES Do not have sufficient resource to finish this + operation. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +IScsiCreateOpCode ( + IN UINT16 StartLabelNumber, + OUT VOID **StartOpCodeHandle, + OUT EFI_IFR_GUID_LABEL **StartLabel, + OUT VOID **EndOpCodeHandle, + OUT EFI_IFR_GUID_LABEL **EndLabel + ) +{ + EFI_STATUS Status; + EFI_IFR_GUID_LABEL *InternalStartLabel; + EFI_IFR_GUID_LABEL *InternalEndLabel; + + if (StartOpCodeHandle == NULL || StartLabel == NULL || EndOpCodeHandle == NULL || EndLabel == NULL) { + return EFI_INVALID_PARAMETER; + } + + *StartOpCodeHandle = NULL; + *EndOpCodeHandle = NULL; + Status = EFI_OUT_OF_RESOURCES; + + // + // Initialize the container for dynamic opcodes. + // + *StartOpCodeHandle = HiiAllocateOpCodeHandle (); + if (*StartOpCodeHandle == NULL) { + return Status; + } + + *EndOpCodeHandle = HiiAllocateOpCodeHandle (); + if (*EndOpCodeHandle == NULL) { + goto Exit; + } + + // + // Create Hii Extend Label OpCode as the start opcode. + // + InternalStartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + *StartOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + if (InternalStartLabel == NULL) { + goto Exit; + } + + InternalStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + InternalStartLabel->Number = StartLabelNumber; + + // + // Create Hii Extend Label OpCode as the end opcode. + // + InternalEndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + *EndOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + if (InternalEndLabel == NULL) { + goto Exit; + } + + InternalEndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + InternalEndLabel->Number = LABEL_END; + + *StartLabel = InternalStartLabel; + *EndLabel = InternalEndLabel; + + return EFI_SUCCESS; + +Exit: + + if (*StartOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (*StartOpCodeHandle); + } + + if (*EndOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (*EndOpCodeHandle); + } + return Status; +} + +/** + Update the MAIN form to display the configured attempts. + +**/ +VOID +IScsiConfigUpdateAttempt ( + VOID + ) +{ + LIST_ENTRY *Entry; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + VOID *StartOpCodeHandle; + EFI_IFR_GUID_LABEL *StartLabel; + VOID *EndOpCodeHandle; + EFI_IFR_GUID_LABEL *EndLabel; + EFI_STATUS Status; + + Status = IScsiCreateOpCode ( + ATTEMPT_ENTRY_LABEL, + &StartOpCodeHandle, + &StartLabel, + &EndOpCodeHandle, + &EndLabel + ); + if (EFI_ERROR (Status)) { + return ; + } + + NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) { + AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + if (AttemptConfigData->Actived == ISCSI_ACTIVE_ENABLED) { + // + // Update Attempt Help Info. + // + UnicodeSPrint (mPrivate->PortString, (UINTN) ISCSI_NAME_IFR_MAX_SIZE, L"Attempt %d", (UINTN) AttemptConfigData->AttemptConfigIndex); + AttemptConfigData->AttemptTitleToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + mPrivate->PortString, + NULL + ); + if (AttemptConfigData->AttemptTitleToken == 0) { + return ; + } + + HiiCreateGotoOpCode ( + StartOpCodeHandle, // Container for dynamic created opcodes + FORMID_ATTEMPT_FORM, // Form ID + AttemptConfigData->AttemptTitleToken, // Prompt text + AttemptConfigData->AttemptTitleHelpToken, // Help text + EFI_IFR_FLAG_CALLBACK, // Question flag + (UINT16) (KEY_ATTEMPT_ENTRY_BASE + AttemptConfigData->AttemptConfigIndex) // Question ID + ); + } + } + + HiiUpdateForm ( + mCallbackInfo->RegisteredHandle, // HII handle + &gIScsiConfigGuid, // Formset GUID + FORMID_MAIN_FORM, // Form ID + StartOpCodeHandle, // Label for where to insert opcodes + EndOpCodeHandle // Replace data + ); + + HiiFreeOpCodeHandle (StartOpCodeHandle); + HiiFreeOpCodeHandle (EndOpCodeHandle); +} + +/** + Callback function when user presses "Add an Attempt". + + @retval EFI_OUT_OF_RESOURCES Does not have sufficient resources to finish this + operation. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +IScsiConfigAddAttempt ( + VOID + ) +{ + LIST_ENTRY *Entry; + ISCSI_NIC_INFO *NicInfo; + EFI_STRING_ID PortTitleToken; + EFI_STRING_ID PortTitleHelpToken; + CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN]; + EFI_STATUS Status; + VOID *StartOpCodeHandle; + EFI_IFR_GUID_LABEL *StartLabel; + VOID *EndOpCodeHandle; + EFI_IFR_GUID_LABEL *EndLabel; + + Status = IScsiCreateOpCode ( + MAC_ENTRY_LABEL, + &StartOpCodeHandle, + &StartLabel, + &EndOpCodeHandle, + &EndLabel + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Ask user to select a MAC for this attempt. + // + NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) { + NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link); + IScsiMacAddrToStr ( + &NicInfo->PermanentAddress, + NicInfo->HwAddressSize, + NicInfo->VlanId, + MacString + ); + + UnicodeSPrint (mPrivate->PortString, (UINTN) ISCSI_NAME_IFR_MAX_SIZE, L"MAC %s", MacString); + PortTitleToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + mPrivate->PortString, + NULL + ); + if (PortTitleToken == 0) { + Status = EFI_INVALID_PARAMETER; + goto Exit; + } + + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"PFA: Bus %d | Dev %d | Func %d", + NicInfo->BusNumber, + NicInfo->DeviceNumber, + NicInfo->FunctionNumber + ); + PortTitleHelpToken = HiiSetString (mCallbackInfo->RegisteredHandle, 0, mPrivate->PortString, NULL); + if (PortTitleHelpToken == 0) { + Status = EFI_INVALID_PARAMETER; + goto Exit; + } + + HiiCreateGotoOpCode ( + StartOpCodeHandle, // Container for dynamic created opcodes + FORMID_ATTEMPT_FORM, + PortTitleToken, + PortTitleHelpToken, + EFI_IFR_FLAG_CALLBACK, // Question flag + (UINT16) (KEY_MAC_ENTRY_BASE + NicInfo->NicIndex) + ); + } + + Status = HiiUpdateForm ( + mCallbackInfo->RegisteredHandle, // HII handle + &gIScsiConfigGuid, // Formset GUID + FORMID_MAC_FORM, // Form ID + StartOpCodeHandle, // Label for where to insert opcodes + EndOpCodeHandle // Replace data + ); + +Exit: + HiiFreeOpCodeHandle (StartOpCodeHandle); + HiiFreeOpCodeHandle (EndOpCodeHandle); + + return Status; +} + +/** + Add the attempts by keyword 'iSCSIAddAttempts', you can use this keyword with + value 'attempt:1 attempt:2' etc to add one or more attempts once. This is different + with IScsiConfigAddAttempt function which is used to add attempt by UI configuration. + + @param[in] AttemptList The new attempt List will be added. + + @retval EFI_SUCCESS The operation to add attempt list successfully. + @retval EFI_INVALID_PARAMETER Any parameter is invalid. + @retval EFI_NOT_FOUND Cannot find the corresponding variable. + @retval EFI_OUT_OF_RESOURCES Fail to finish the operation due to lack of + resources. + +**/ +EFI_STATUS +IScsiConfigAddAttemptsByKeywords ( + IN UINT8 *AttemptList + ) +{ + UINT8 Index; + UINT8 Number; + UINTN TotalNumber; + UINT8 Nic; + UINT8 *AttemptConfigOrder; + UINTN AttemptConfigOrderSize; + UINT8 *AttemptConfigOrderTmp; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + ISCSI_NIC_INFO *NicInfo; + CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN]; + CHAR16 IScsiMode[64]; + CHAR16 IpMode[64]; + EFI_STATUS Status; + + Nic = mPrivate->CurrentNic; + NicInfo = IScsiGetNicInfoByIndex (Nic); + if (NicInfo == NULL) { + return EFI_NOT_FOUND; + } + + // + // The MAC info will be recorded in Config Data. + // + IScsiMacAddrToStr ( + &NicInfo->PermanentAddress, + NicInfo->HwAddressSize, + NicInfo->VlanId, + MacString + ); + + for (Index = 0; Index < PcdGet8 (PcdMaxIScsiAttemptNumber); Index++) { + if (AttemptList[Index] == 0) { + continue; + } + + // + // Add the attempt. + // + Number = AttemptList[Index]; + + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Attempt %d", + Number + ); + + GetVariable2 ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + (VOID**)&AttemptConfigData, + NULL + ); + if (AttemptConfigData == NULL || AttemptConfigData->Actived == ISCSI_ACTIVE_ENABLED) { + return EFI_INVALID_PARAMETER; + } + + AttemptConfigData->Actived = ISCSI_ACTIVE_ENABLED; + AttemptConfigData->NicIndex = NicInfo->NicIndex; + UnicodeStrToAsciiStrS (MacString, AttemptConfigData->MacString, ISCSI_MAX_MAC_STRING_LEN); + + // + // Generate OUI-format ISID based on MAC address. + // + CopyMem (AttemptConfigData->SessionConfigData.IsId, &NicInfo->PermanentAddress, 6); + AttemptConfigData->SessionConfigData.IsId[0] = + (UINT8) (AttemptConfigData->SessionConfigData.IsId[0] & 0x3F); + + // + // Configure the iSCSI Mode and IpMode to default. + // Add Attempt Help Info. + // + UnicodeSPrint (IScsiMode, 64, L"Disabled"); + UnicodeSPrint (IpMode, 64, L"IP4"); + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"MAC: %s, PFA: Bus %d | Dev %d | Func %d, iSCSI mode: %s, IP version: %s", + MacString, + NicInfo->BusNumber, + NicInfo->DeviceNumber, + NicInfo->FunctionNumber, + IScsiMode, + IpMode + ); + + AttemptConfigData->AttemptTitleHelpToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + mPrivate->PortString, + NULL + ); + if (AttemptConfigData->AttemptTitleHelpToken == 0) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Get current Attempt order and number. + // + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"AttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + TotalNumber = AttemptConfigOrderSize / sizeof (UINT8); + TotalNumber++; + + // + // Append the new created attempt order to the end. + // + AttemptConfigOrderTmp = AllocateZeroPool (TotalNumber * sizeof (UINT8)); + if (AttemptConfigOrderTmp == NULL) { + if (AttemptConfigOrder != NULL) { + FreePool (AttemptConfigOrder); + } + return EFI_OUT_OF_RESOURCES; + } + if (AttemptConfigOrder != NULL) { + CopyMem (AttemptConfigOrderTmp, AttemptConfigOrder, AttemptConfigOrderSize); + FreePool (AttemptConfigOrder); + } + + AttemptConfigOrderTmp[TotalNumber - 1] = Number; + AttemptConfigOrder = AttemptConfigOrderTmp; + AttemptConfigOrderSize = TotalNumber * sizeof (UINT8); + + Status = gRT->SetVariable ( + L"AttemptOrder", + &gIScsiConfigGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE, + AttemptConfigOrderSize, + AttemptConfigOrder + ); + FreePool (AttemptConfigOrder); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Record the attempt in global link list. + // + InsertTailList (&mPrivate->AttemptConfigs, &AttemptConfigData->Link); + mPrivate->AttemptCount++; + UnicodeSPrint (mPrivate->PortString, (UINTN) ISCSI_NAME_IFR_MAX_SIZE, L"Attempt %d", AttemptConfigData->AttemptConfigIndex); + gRT->SetVariable ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + ISCSI_CONFIG_VAR_ATTR, + sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA), + AttemptConfigData + ); + + } + + return EFI_SUCCESS; +} + +/** + Callback function when user presses "Commit Changes and Exit" in Delete Attempts or Delete Attempts by Keyword. + + @param[in] IfrNvData The IFR NV data. + + @retval EFI_NOT_FOUND Cannot find the corresponding variable. + @retval EFI_SUCCESS The operation is completed successfully. + @retval EFI_ABOTRED This operation is aborted cause of error + configuration. + @retval EFI_OUT_OF_RESOURCES Fail to finish the operation due to lack of + resources. + +**/ +EFI_STATUS +IScsiConfigDeleteAttempts ( + IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData + ) +{ + EFI_STATUS Status; + UINTN Index; + UINTN NewIndex; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + UINT8 *AttemptConfigOrder; + UINTN AttemptConfigOrderSize; + UINT8 *AttemptNewOrder; + UINT8 AttemptConfigIndex; + UINT32 Attribute; + UINTN Total; + UINTN NewTotal; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + ISCSI_SESSION_CONFIG_NVDATA *ConfigData; + + Index = 0; + + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"AttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + if ((AttemptConfigOrder == NULL) || (AttemptConfigOrderSize == 0)) { + return EFI_NOT_FOUND; + } + + AttemptNewOrder = AllocateZeroPool (AttemptConfigOrderSize); + if (AttemptNewOrder == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + Total = AttemptConfigOrderSize / sizeof (UINT8); + NewTotal = Total; + + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &mPrivate->AttemptConfigs) { + if (IfrNvData->DeleteAttemptList[Index] == 0) { + Index++; + continue; + } + + // + // Delete the attempt. + // + + AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + + // + // Remove this attempt from UI configured attempt list. + // + RemoveEntryList (&AttemptConfigData->Link); + mPrivate->AttemptCount--; + + if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) { + if (mPrivate->MpioCount < 1) { + Status = EFI_ABORTED; + goto Error; + } + + // + // No more attempt is enabled for MPIO. Transit the iSCSI mode to single path. + // + if (--mPrivate->MpioCount == 0) { + mPrivate->EnableMpio = FALSE; + } + } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED) { + if (mPrivate->SinglePathCount < 1) { + Status = EFI_ABORTED; + goto Error; + } + + mPrivate->SinglePathCount--; + } + + AttemptConfigIndex = AttemptConfigData->AttemptConfigIndex; + FreePool (AttemptConfigData); + + // + // Create a new Attempt + // + AttemptConfigData = AllocateZeroPool (sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA)); + if (AttemptConfigData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + ConfigData = &AttemptConfigData->SessionConfigData; + ConfigData->TargetPort = ISCSI_WELL_KNOWN_PORT; + ConfigData->ConnectTimeout = CONNECT_DEFAULT_TIMEOUT; + ConfigData->ConnectRetryCount = CONNECT_MIN_RETRY; + + AttemptConfigData->AuthenticationType = ISCSI_AUTH_TYPE_CHAP; + AttemptConfigData->AuthConfigData.CHAP.CHAPType = ISCSI_CHAP_UNI; + // + // Configure the Attempt index and set variable. + // + AttemptConfigData->AttemptConfigIndex = AttemptConfigIndex; + + // + // Set the attempt name to default. + // + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Attempt %d", + (UINTN) AttemptConfigData->AttemptConfigIndex + ); + UnicodeStrToAsciiStrS (mPrivate->PortString, AttemptConfigData->AttemptName, ATTEMPT_NAME_SIZE); + gRT->SetVariable ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + ISCSI_CONFIG_VAR_ATTR, + sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA), + AttemptConfigData + ); + + // + // Mark the attempt order in NVR to be deleted - 0. + // + for (NewIndex = 0; NewIndex < Total; NewIndex++) { + if (AttemptConfigOrder[NewIndex] == AttemptConfigData->AttemptConfigIndex) { + AttemptConfigOrder[NewIndex] = 0; + break; + } + } + + NewTotal--; + if (mCallbackInfo->Current == AttemptConfigData) { + mCallbackInfo->Current = NULL; + } + FreePool (AttemptConfigData); + + // + // Check next Attempt. + // + Index++; + } + + // + // Construct AttemptNewOrder. + // + for (Index = 0, NewIndex = 0; Index < Total; Index++) { + if (AttemptConfigOrder[Index] != 0) { + AttemptNewOrder[NewIndex] = AttemptConfigOrder[Index]; + NewIndex++; + } + } + + Attribute = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE; + + // + // Update AttemptOrder in NVR. + // + Status = gRT->SetVariable ( + L"AttemptOrder", + &gIScsiConfigGuid, + Attribute, + NewTotal * sizeof (UINT8), + AttemptNewOrder + ); + +Error: + if (AttemptConfigOrder != NULL) { + FreePool (AttemptConfigOrder); + } + + if (AttemptNewOrder != NULL) { + FreePool (AttemptNewOrder); + } + + return Status; +} + + +/** + Callback function when user presses "Delete Attempts". + + @param[in] IfrNvData The IFR nv data. + + @retval EFI_INVALID_PARAMETER Any parameter is invalid. + @retval EFI_BUFFER_TOO_SMALL The buffer in UpdateData is too small. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +IScsiConfigDisplayDeleteAttempts ( + IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData + ) +{ + + UINT8 *AttemptConfigOrder; + UINTN AttemptConfigOrderSize; + LIST_ENTRY *Entry; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + UINT8 Index; + VOID *StartOpCodeHandle; + EFI_IFR_GUID_LABEL *StartLabel; + VOID *EndOpCodeHandle; + EFI_IFR_GUID_LABEL *EndLabel; + EFI_STATUS Status; + + Status = IScsiCreateOpCode ( + DELETE_ENTRY_LABEL, + &StartOpCodeHandle, + &StartLabel, + &EndOpCodeHandle, + &EndLabel + ); + if (EFI_ERROR (Status)) { + return Status; + } + + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"AttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + if (AttemptConfigOrder != NULL) { + // + // Create the check box opcode to be deleted. + // + Index = 0; + + NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) { + AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + IfrNvData->DeleteAttemptList[Index] = 0x00; + + HiiCreateCheckBoxOpCode( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_DEL_QUESTION_ID + Index), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_DEL_VAR_OFFSET + Index), + AttemptConfigData->AttemptTitleToken, + AttemptConfigData->AttemptTitleHelpToken, + 0, + 0, + NULL + ); + + Index++; + + if (Index == ISCSI_MAX_ATTEMPTS_NUM) { + break; + } + } + + FreePool (AttemptConfigOrder); + } + + Status = HiiUpdateForm ( + mCallbackInfo->RegisteredHandle, // HII handle + &gIScsiConfigGuid, // Formset GUID + FORMID_DELETE_FORM, // Form ID + StartOpCodeHandle, // Label for where to insert opcodes + EndOpCodeHandle // Replace data + ); + + HiiFreeOpCodeHandle (StartOpCodeHandle); + HiiFreeOpCodeHandle (EndOpCodeHandle); + + return Status; +} + + +/** + Callback function when user presses "Change Attempt Order". + + @retval EFI_INVALID_PARAMETER Any parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Does not have sufficient resources to finish this + operation. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +IScsiConfigDisplayOrderAttempts ( + VOID + ) +{ + EFI_STATUS Status; + UINT8 Index; + LIST_ENTRY *Entry; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + VOID *StartOpCodeHandle; + EFI_IFR_GUID_LABEL *StartLabel; + VOID *EndOpCodeHandle; + EFI_IFR_GUID_LABEL *EndLabel; + VOID *OptionsOpCodeHandle; + + Status = IScsiCreateOpCode ( + ORDER_ENTRY_LABEL, + &StartOpCodeHandle, + &StartLabel, + &EndOpCodeHandle, + &EndLabel + ); + if (EFI_ERROR (Status)) { + return Status; + } + ASSERT (StartOpCodeHandle != NULL); + + OptionsOpCodeHandle = NULL; + + // + // If no attempt to be ordered, update the original form and exit. + // + if (mPrivate->AttemptCount == 0) { + goto Exit; + } + + // + // Create Option OpCode. + // + OptionsOpCodeHandle = HiiAllocateOpCodeHandle (); + if (OptionsOpCodeHandle == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + Index = 0; + + NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) { + AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + HiiCreateOneOfOptionOpCode ( + OptionsOpCodeHandle, + AttemptConfigData->AttemptTitleToken, + 0, + EFI_IFR_NUMERIC_SIZE_1, + AttemptConfigData->AttemptConfigIndex + ); + Index++; + } + + ASSERT (Index == mPrivate->AttemptCount); + + HiiCreateOrderedListOpCode ( + StartOpCodeHandle, // Container for dynamic created opcodes + DYNAMIC_ORDERED_LIST_QUESTION_ID, // Question ID + CONFIGURATION_VARSTORE_ID, // VarStore ID + DYNAMIC_ORDERED_LIST_VAR_OFFSET, // Offset in Buffer Storage + STRING_TOKEN (STR_ORDER_ATTEMPT_ENTRY), // Question prompt text + STRING_TOKEN (STR_ORDER_ATTEMPT_ENTRY), // Question help text + 0, // Question flag + EFI_IFR_UNIQUE_SET, // Ordered list flag, e.g. EFI_IFR_UNIQUE_SET + EFI_IFR_NUMERIC_SIZE_1, // Data type of Question value + ISCSI_MAX_ATTEMPTS_NUM, // Maximum container + OptionsOpCodeHandle, // Option Opcode list + NULL // Default Opcode is NULL + ); + +Exit: + Status = HiiUpdateForm ( + mCallbackInfo->RegisteredHandle, // HII handle + &gIScsiConfigGuid, // Formset GUID + FORMID_ORDER_FORM, // Form ID + StartOpCodeHandle, // Label for where to insert opcodes + EndOpCodeHandle // Replace data + ); + +Error: + HiiFreeOpCodeHandle (StartOpCodeHandle); + HiiFreeOpCodeHandle (EndOpCodeHandle); + if (OptionsOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (OptionsOpCodeHandle); + } + + return Status; +} + +/** + Callback function when user presses "Commit Changes and Exit" in Change Attempt Order or Change Attempt Order by Keyword. + + @param[in] IfrNvData The IFR nv data. + + @retval EFI_OUT_OF_RESOURCES Does not have sufficient resources to finish this + operation. + @retval EFI_NOT_FOUND Cannot find the corresponding variable. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +IScsiConfigOrderAttempts ( + IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData + ) +{ + EFI_STATUS Status; + UINTN Index; + UINTN Indexj; + UINT8 AttemptConfigIndex; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + UINT8 *AttemptConfigOrder; + UINT8 *AttemptConfigOrderTmp; + UINTN AttemptConfigOrderSize; + + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"AttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + if (AttemptConfigOrder == NULL) { + return EFI_NOT_FOUND; + } + + AttemptConfigOrderTmp = AllocateZeroPool (AttemptConfigOrderSize); + if (AttemptConfigOrderTmp == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + for (Index = 0; Index < ISCSI_MAX_ATTEMPTS_NUM; Index++) { + // + // The real content ends with 0. + // + if (IfrNvData->DynamicOrderedList[Index] == 0) { + break; + } + + AttemptConfigIndex = IfrNvData->DynamicOrderedList[Index]; + AttemptConfigData = IScsiConfigGetAttemptByConfigIndex (AttemptConfigIndex); + if (AttemptConfigData == NULL) { + Status = EFI_NOT_FOUND; + goto Exit; + } + + // + // Reorder the Attempt List. + // + RemoveEntryList (&AttemptConfigData->Link); + InsertTailList (&mPrivate->AttemptConfigs, &AttemptConfigData->Link); + + AttemptConfigOrderTmp[Index] = AttemptConfigIndex; + + // + // Mark it to be deleted - 0. + // + for (Indexj = 0; Indexj < AttemptConfigOrderSize / sizeof (UINT8); Indexj++) { + if (AttemptConfigOrder[Indexj] == AttemptConfigIndex) { + AttemptConfigOrder[Indexj] = 0; + break; + } + } + } + + // + // Adjust the attempt order in NVR. + // + for (; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) { + for (Indexj = 0; Indexj < AttemptConfigOrderSize / sizeof (UINT8); Indexj++) { + if (AttemptConfigOrder[Indexj] != 0) { + AttemptConfigOrderTmp[Index] = AttemptConfigOrder[Indexj]; + AttemptConfigOrder[Indexj] = 0; + continue; + } + } + } + + Status = gRT->SetVariable ( + L"AttemptOrder", + &gIScsiConfigGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE, + AttemptConfigOrderSize, + AttemptConfigOrderTmp + ); + +Exit: + if (AttemptConfigOrderTmp != NULL) { + FreePool (AttemptConfigOrderTmp); + } + + FreePool (AttemptConfigOrder); + return Status; +} + + +/** + Callback function when a user presses "Attempt *" or when a user selects a NIC to + create the new attempt. + + @param[in] KeyValue A unique value which is sent to the original + exporting driver so that it can identify the type + of data to expect. + @param[in] IfrNvData The IFR nv data. + + @retval EFI_OUT_OF_RESOURCES Does not have sufficient resources to finish this + operation. + @retval EFI_NOT_FOUND Cannot find the corresponding variable. + @retval EFI_UNSUPPORTED Can not create more attempts. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +IScsiConfigProcessDefault ( + IN EFI_QUESTION_ID KeyValue, + IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData + ) +{ + BOOLEAN NewAttempt; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + UINT8 CurrentAttemptConfigIndex; + ISCSI_NIC_INFO *NicInfo; + UINT8 NicIndex; + CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN]; + UINT8 *AttemptConfigOrder; + UINTN AttemptConfigOrderSize; + UINTN Index; + EFI_INPUT_KEY Key; + + AttemptConfigData = NULL; + // + // Is User creating a new attempt? + // + NewAttempt = FALSE; + + if ((KeyValue >= KEY_MAC_ENTRY_BASE) && + (KeyValue <= (UINT16) (mPrivate->MaxNic + KEY_MAC_ENTRY_BASE))) { + // + // User has pressed "Add an Attempt" and then selects a NIC. + // + NewAttempt = TRUE; + } else if ((KeyValue >= KEY_ATTEMPT_ENTRY_BASE) && + (KeyValue < (ISCSI_MAX_ATTEMPTS_NUM + KEY_ATTEMPT_ENTRY_BASE))) { + + // + // User has pressed "Attempt *". + // + NewAttempt = FALSE; + } else { + // + // Don't process anything. + // + return EFI_SUCCESS; + } + + if (NewAttempt) { + // + // Determine which NIC user has selected for the new created attempt. + // + NicIndex = (UINT8) (KeyValue - KEY_MAC_ENTRY_BASE); + NicInfo = IScsiGetNicInfoByIndex (NicIndex); + if (NicInfo == NULL) { + return EFI_NOT_FOUND; + } + + // + // Create an attempt following the initialized attempt order. + // + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"InitialAttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + + if (AttemptConfigOrder == NULL) { + return EFI_NOT_FOUND; + } + + for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) { + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Attempt %d", + (UINTN) AttemptConfigOrder[Index] + ); + GetVariable2 ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + (VOID**)&AttemptConfigData, + NULL + ); + if (AttemptConfigData == NULL || AttemptConfigData->Actived == ISCSI_ACTIVE_ENABLED) { + continue; + } + + break; + } + + if (Index > PcdGet8 (PcdMaxIScsiAttemptNumber)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Can not create more attempts, Please configure the PcdMaxIScsiAttemptNumber if needed!", + NULL + ); + return EFI_UNSUPPORTED; + } + + if (AttemptConfigOrder != NULL) { + FreePool (AttemptConfigOrder); + } + + // + // Record the MAC info in Config Data. + // + IScsiMacAddrToStr ( + &NicInfo->PermanentAddress, + NicInfo->HwAddressSize, + NicInfo->VlanId, + MacString + ); + + ASSERT (AttemptConfigData != NULL); + UnicodeStrToAsciiStrS (MacString, AttemptConfigData->MacString, sizeof (AttemptConfigData->MacString)); + AttemptConfigData->NicIndex = NicIndex; + AttemptConfigData->Actived = ISCSI_ACTIVE_ENABLED; + + // + // Generate OUI-format ISID based on MAC address. + // + CopyMem (AttemptConfigData->SessionConfigData.IsId, &NicInfo->PermanentAddress, 6); + AttemptConfigData->SessionConfigData.IsId[0] = + (UINT8) (AttemptConfigData->SessionConfigData.IsId[0] & 0x3F); + + // + // Add the help info for the new attempt. + // + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"MAC: %s, PFA: Bus %d | Dev %d | Func %d", + MacString, + NicInfo->BusNumber, + NicInfo->DeviceNumber, + NicInfo->FunctionNumber + ); + + AttemptConfigData->AttemptTitleHelpToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + mPrivate->PortString, + NULL + ); + if (AttemptConfigData->AttemptTitleHelpToken == 0) { + FreePool (AttemptConfigData); + return EFI_OUT_OF_RESOURCES; + } + + } else { + // + // Determine which Attempt user has selected to configure. + // Get the attempt configuration data. + // + CurrentAttemptConfigIndex = (UINT8) (KeyValue - KEY_ATTEMPT_ENTRY_BASE); + + AttemptConfigData = IScsiConfigGetAttemptByConfigIndex (CurrentAttemptConfigIndex); + if (AttemptConfigData == NULL) { + DEBUG ((DEBUG_ERROR, "Corresponding configuration data can not be retrieved!\n")); + return EFI_NOT_FOUND; + } + } + + // + // Clear the old IFR data to avoid sharing it with other attempts. + // + if (IfrNvData->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) { + ZeroMem (IfrNvData->CHAPName, sizeof (IfrNvData->CHAPName)); + ZeroMem (IfrNvData->CHAPSecret, sizeof (IfrNvData->CHAPSecret)); + ZeroMem (IfrNvData->ReverseCHAPName, sizeof (IfrNvData->ReverseCHAPName)); + ZeroMem (IfrNvData->ReverseCHAPSecret, sizeof (IfrNvData->ReverseCHAPSecret)); + } + + IScsiConvertAttemptConfigDataToIfrNvData (AttemptConfigData, IfrNvData); + + // + // Update current attempt to be a new created attempt or an existing attempt. + // + mCallbackInfo->Current = AttemptConfigData; + + return EFI_SUCCESS; +} + + +/** + + This function allows the caller to request the current + configuration for one or more named elements. The resulting + string is in format. Also, any and all alternative + configuration strings shall be appended to the end of the + current configuration string. If they are, they must appear + after the current configuration. They must contain the same + routing (GUID, NAME, PATH) as the current configuration string. + They must have an additional description indicating the type of + alternative configuration the string represents, + "ALTCFG=". That (when + converted from Hex UNICODE to binary) is a reference to a + string in the associated string pack. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param[in] Request A null-terminated Unicode string in + format. Note that this + includes the routing information as well as + the configurable name / value pairs. It is + invalid for this string to be in + format. + + @param[out] Progress On return, points to a character in the + Request string. Points to the string's null + terminator if request was successful. Points + to the most recent "&" before the first + failing name / value pair (or the beginning + of the string if the failure is in the first + name / value pair) if the request was not successful. + + @param[out] Results A null-terminated Unicode string in + format which has all values + filled in for the names in the Request string. + String to be allocated by the called function. + + @retval EFI_SUCCESS The Results string is filled with the + values corresponding to all requested + names. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_INVALID_PARAMETER For example, passing in a NULL + for the Request parameter + would result in this type of + error. In this case, the + Progress parameter would be + set to NULL. + + @retval EFI_NOT_FOUND Routing data doesn't match any + known driver. Progress set to the + first character in the routing header. + Note: There is no requirement that the + driver validate the routing data. It + must skip the in order to + process the names. + + @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set + to most recent "&" before the + error or the beginning of the + string. + + @retval EFI_INVALID_PARAMETER Unknown name. Progress points + to the & before the name in + question. + +**/ +EFI_STATUS +EFIAPI +IScsiFormExtractConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Request, + OUT EFI_STRING *Progress, + OUT EFI_STRING *Results + ) +{ + EFI_STATUS Status; + CHAR8 *InitiatorName; + UINTN BufferSize; + ISCSI_CONFIG_IFR_NVDATA *IfrNvData; + ISCSI_FORM_CALLBACK_INFO *Private; + EFI_STRING ConfigRequestHdr; + EFI_STRING ConfigRequest; + BOOLEAN AllocatedRequest; + UINTN Size; + + if (This == NULL || Progress == NULL || Results == NULL) { + return EFI_INVALID_PARAMETER; + } + + *Progress = Request; + if ((Request != NULL) && !HiiIsConfigHdrMatch (Request, &gIScsiConfigGuid, mVendorStorageName)) { + return EFI_NOT_FOUND; + } + + ConfigRequestHdr = NULL; + ConfigRequest = NULL; + AllocatedRequest = FALSE; + Size = 0; + + Private = ISCSI_FORM_CALLBACK_INFO_FROM_FORM_CALLBACK (This); + IfrNvData = AllocateZeroPool (sizeof (ISCSI_CONFIG_IFR_NVDATA)); + if (IfrNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + + if (Private->Current!= NULL) { + IScsiConvertAttemptConfigDataToIfrNvData (Private->Current, IfrNvData); + } + + // + // Extract all AttemptConfigData to Keyword stroage of IfrNvData. + // + IScsiConvertAttemptConfigDataToIfrNvDataByKeyword (IfrNvData); + + BufferSize = ISCSI_NAME_MAX_SIZE; + InitiatorName = (CHAR8 *) AllocateZeroPool (BufferSize); + if (InitiatorName == NULL) { + FreePool (IfrNvData); + return EFI_OUT_OF_RESOURCES; + } + + Status = gIScsiInitiatorName.Get (&gIScsiInitiatorName, &BufferSize, InitiatorName); + if (EFI_ERROR (Status)) { + IfrNvData->InitiatorName[0] = L'\0'; + } else { + AsciiStrToUnicodeStrS ( + InitiatorName, + IfrNvData->InitiatorName, + sizeof (IfrNvData->InitiatorName) / sizeof (IfrNvData->InitiatorName[0]) + ); + } + + // + // Convert buffer data to by helper function BlockToConfig(). + // + BufferSize = sizeof (ISCSI_CONFIG_IFR_NVDATA); + ConfigRequest = Request; + if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) { + // + // Request has no request element, construct full request string. + // Allocate and fill a buffer large enough to hold the template + // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator + // + ConfigRequestHdr = HiiConstructConfigHdr (&gIScsiConfigGuid, mVendorStorageName, Private->DriverHandle); + Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16); + ConfigRequest = AllocateZeroPool (Size); + if (ConfigRequest == NULL) { + FreePool (IfrNvData); + FreePool (InitiatorName); + return EFI_OUT_OF_RESOURCES; + } + AllocatedRequest = TRUE; + UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize); + FreePool (ConfigRequestHdr); + } + + Status = gHiiConfigRouting->BlockToConfig ( + gHiiConfigRouting, + ConfigRequest, + (UINT8 *) IfrNvData, + BufferSize, + Results, + Progress + ); + FreePool (IfrNvData); + FreePool (InitiatorName); + + // + // Free the allocated config request string. + // + if (AllocatedRequest) { + FreePool (ConfigRequest); + ConfigRequest = NULL; + } + // + // Set Progress string to the original request string. + // + if (Request == NULL) { + *Progress = NULL; + } else if (StrStr (Request, L"OFFSET") == NULL) { + *Progress = Request + StrLen (Request); + } + + return Status; +} + + +/** + + This function applies changes in a driver's configuration. + Input is a Configuration, which has the routing data for this + driver followed by name / value configuration pairs. The driver + must apply those pairs to its configurable storage. If the + driver's configuration is stored in a linear block of data + and the driver's name / value pairs are in + format, it may use the ConfigToBlock helper function (above) to + simplify the job. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param[in] Configuration A null-terminated Unicode string in + format. + + @param[out] Progress A pointer to a string filled in with the + offset of the most recent '&' before the + first failing name / value pair (or the + beginning of the string if the failure + is in the first name / value pair) or + the terminating NULL if all was + successful. + + @retval EFI_SUCCESS The results have been distributed or are + awaiting distribution. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_INVALID_PARAMETERS Passing in a NULL for the + Results parameter would result + in this type of error. + + @retval EFI_NOT_FOUND Target for the specified routing data + was not found. + +**/ +EFI_STATUS +EFIAPI +IScsiFormRouteConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Configuration, + OUT EFI_STRING *Progress + ) +{ + EFI_STATUS Status; + ISCSI_CONFIG_IFR_NVDATA *IfrNvData; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + ISCSI_NIC_INFO *NicInfo; + EFI_INPUT_KEY Key; + CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN]; + CHAR8 *InitiatorName; + UINT8 *AttemptList; + UINTN BufferSize; + UINTN OffSet; + UINTN Index; + UINTN Index2; + + Index = 0; + Index2 = 0; + NicInfo = NULL; + AttemptList = NULL; + Status = EFI_SUCCESS; + + if (This == NULL || Configuration == NULL || Progress == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Check routing data in . + // Note: if only one Storage is used, then this checking could be skipped. + // + if (!HiiIsConfigHdrMatch (Configuration, &gIScsiConfigGuid, mVendorStorageName)) { + *Progress = Configuration; + return EFI_NOT_FOUND; + } + + IfrNvData = AllocateZeroPool (sizeof (ISCSI_CONFIG_IFR_NVDATA)); + if (IfrNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + BufferSize = ISCSI_NAME_MAX_SIZE; + InitiatorName = (CHAR8 *) AllocateZeroPool (BufferSize); + if (InitiatorName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + // + // Convert to buffer data by helper function ConfigToBlock(). + // + BufferSize = sizeof (ISCSI_CONFIG_IFR_NVDATA); + Status = gHiiConfigRouting->ConfigToBlock ( + gHiiConfigRouting, + Configuration, + (UINT8 *) IfrNvData, + &BufferSize, + Progress + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + if (IfrNvData->InitiatorName[0] != L'\0') { + UnicodeStrToAsciiStrS (IfrNvData->InitiatorName, InitiatorName, ISCSI_NAME_MAX_SIZE); + BufferSize = AsciiStrSize (InitiatorName); + + Status = gIScsiInitiatorName.Set (&gIScsiInitiatorName, &BufferSize, InitiatorName); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid iSCSI Name!", + NULL + ); + goto Exit; + } + } else { + Status = IScsiGetValue (Configuration, L"&OFFSET=", &OffSet); + if (EFI_ERROR (Status)) { + goto Exit; + } + + if (OffSet >= ATTEMPT_MAC_ADDR_VAR_OFFSET) { + Status = gIScsiInitiatorName.Get (&gIScsiInitiatorName, &BufferSize, InitiatorName); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Error: please configure iSCSI initiator name first!", + NULL + ); + goto Exit; + } + } else { + goto Exit; + } + + if (IfrNvData->ISCSIAddAttemptList[0] != L'\0') { + Status =IScsiGetAttemptIndexList (IfrNvData->ISCSIAddAttemptList, IfrNvData->AddAttemptList, TRUE); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Error: The add attempt list is invalid", + NULL + ); + goto Exit; + } + + Status = IScsiConfigAddAttemptsByKeywords (IfrNvData->AddAttemptList); + if (EFI_ERROR (Status)) { + goto Exit; + } + + } else if (IfrNvData->ISCSIDeleteAttemptList[0] != L'\0') { + AttemptList =(UINT8 *) AllocateZeroPool ((ISCSI_MAX_ATTEMPTS_NUM + 1) * sizeof (UINT8)); + if (AttemptList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + Status = IScsiGetAttemptIndexList (IfrNvData->ISCSIDeleteAttemptList, AttemptList, FALSE); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Error: The delete attempt list is invalid", + NULL + ); + goto Exit; + } + + // + // Mark the attempt which will be delete in the global list. + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &mPrivate->AttemptConfigs) { + AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + while (AttemptList[Index] != 0) { + if (AttemptConfigData->AttemptConfigIndex == AttemptList[Index]) { + IfrNvData->DeleteAttemptList[Index2] = 1; + break; + } + Index ++; + } + Index2 ++; + Index = 0; + } + + Status = IScsiConfigDeleteAttempts (IfrNvData); + if (EFI_ERROR (Status)) { + goto Exit; + } + + FreePool (AttemptList); + + } else if (IfrNvData->ISCSIAttemptOrder[0] != L'\0') { + Status = IScsiGetAttemptIndexList (IfrNvData->ISCSIAttemptOrder, IfrNvData->DynamicOrderedList, FALSE); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Error: The new attempt order list is invalid", + NULL + ); + goto Exit; + } + + Status = IScsiConfigOrderAttempts (IfrNvData); + if (EFI_ERROR (Status)) { + goto Exit; + } + + } else if (IfrNvData->ISCSIMacAddr[0] != L'\0') { + NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) { + NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link); + IScsiMacAddrToStr ( + &NicInfo->PermanentAddress, + NicInfo->HwAddressSize, + NicInfo->VlanId, + MacString + ); + if (!StrCmp(MacString, IfrNvData->ISCSIMacAddr)) { + mPrivate->CurrentNic = NicInfo->NicIndex; + break; + } + } + + if ((NicInfo == NULL) || (NicInfo->NicIndex == 0)) { + Status = EFI_NOT_FOUND; + goto Exit; + } + + } else { + Status = IScsiConvertlfrNvDataToAttemptConfigDataByKeyword (IfrNvData, OffSet); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + } + + IScsiConfigUpdateAttempt (); + +Exit: + if (InitiatorName != NULL) { + FreePool (InitiatorName); + } + + if (IfrNvData != NULL) { + FreePool (IfrNvData); + } + + return Status; +} + +/** + + This function is called to provide results data to the driver. + This data consists of a unique key that is used to identify + which data is either being passed back or being asked for. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Action Specifies the type of action taken by the browser. + @param[in] QuestionId A unique value which is sent to the original + exporting driver so that it can identify the type + of data to expect. The format of the data tends to + vary based on the opcode that generated the callback. + @param[in] Type The type of value for the question. + @param[in, out] Value A pointer to the data being sent to the original + exporting driver. + @param[out] ActionRequest On return, points to the action requested by the + callback function. + + @retval EFI_SUCCESS The callback successfully handled the action. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the + variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be saved. + @retval EFI_UNSUPPORTED The specified Action is not supported by the + callback. +**/ +EFI_STATUS +EFIAPI +IScsiFormCallback ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN EFI_BROWSER_ACTION Action, + IN EFI_QUESTION_ID QuestionId, + IN UINT8 Type, + IN OUT EFI_IFR_TYPE_VALUE *Value, + OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest + ) +{ + ISCSI_FORM_CALLBACK_INFO *Private; + UINTN BufferSize; + CHAR8 *IScsiName; + CHAR8 IpString[ISCSI_NAME_MAX_SIZE]; + CHAR8 LunString[ISCSI_LUN_STR_MAX_LEN]; + UINT64 Lun; + EFI_IP_ADDRESS HostIp; + EFI_IP_ADDRESS SubnetMask; + EFI_IP_ADDRESS Gateway; + ISCSI_CONFIG_IFR_NVDATA *IfrNvData; + ISCSI_CONFIG_IFR_NVDATA OldIfrNvData; + EFI_STATUS Status; + EFI_INPUT_KEY Key; + ISCSI_NIC_INFO *NicInfo; + + NicInfo = NULL; + + if ((Action == EFI_BROWSER_ACTION_FORM_OPEN) || (Action == EFI_BROWSER_ACTION_FORM_CLOSE)) { + // + // Do nothing for UEFI OPEN/CLOSE Action + // + return EFI_SUCCESS; + } + + if ((Action != EFI_BROWSER_ACTION_CHANGING) && (Action != EFI_BROWSER_ACTION_CHANGED)) { + // + // All other type return unsupported. + // + return EFI_UNSUPPORTED; + } + + if ((Value == NULL) || (ActionRequest == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Private = ISCSI_FORM_CALLBACK_INFO_FROM_FORM_CALLBACK (This); + + // + // Retrieve uncommitted data from Browser + // + + BufferSize = sizeof (ISCSI_CONFIG_IFR_NVDATA); + IfrNvData = AllocateZeroPool (BufferSize); + if (IfrNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + IScsiName = (CHAR8 *) AllocateZeroPool (ISCSI_NAME_MAX_SIZE); + if (IScsiName == NULL) { + FreePool (IfrNvData); + return EFI_OUT_OF_RESOURCES; + } + + Status = EFI_SUCCESS; + + ZeroMem (&OldIfrNvData, BufferSize); + + HiiGetBrowserData (NULL, NULL, BufferSize, (UINT8 *) IfrNvData); + + CopyMem (&OldIfrNvData, IfrNvData, BufferSize); + + if (Action == EFI_BROWSER_ACTION_CHANGING) { + switch (QuestionId) { + case KEY_ADD_ATTEMPT: + // + // Check whether iSCSI initiator name is configured already. + // + mPrivate->InitiatorNameLength = ISCSI_NAME_MAX_SIZE; + Status = gIScsiInitiatorName.Get ( + &gIScsiInitiatorName, + &mPrivate->InitiatorNameLength, + mPrivate->InitiatorName + ); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Error: please configure iSCSI initiator name first!", + NULL + ); + break; + } + + Status = IScsiConfigAddAttempt (); + break; + + case KEY_DELETE_ATTEMPT: + CopyMem ( + OldIfrNvData.DeleteAttemptList, + IfrNvData->DeleteAttemptList, + sizeof (IfrNvData->DeleteAttemptList) + ); + Status = IScsiConfigDisplayDeleteAttempts (IfrNvData); + break; + + case KEY_ORDER_ATTEMPT_CONFIG: + // + // Order the attempt according to user input. + // + CopyMem ( + OldIfrNvData.DynamicOrderedList, + IfrNvData->DynamicOrderedList, + sizeof (IfrNvData->DynamicOrderedList) + ); + IScsiConfigDisplayOrderAttempts (); + break; + + default: + Status = IScsiConfigProcessDefault (QuestionId, IfrNvData); + break; + } + } else if (Action == EFI_BROWSER_ACTION_CHANGED) { + switch (QuestionId) { + case KEY_INITIATOR_NAME: + UnicodeStrToAsciiStrS (IfrNvData->InitiatorName, IScsiName, ISCSI_NAME_MAX_SIZE); + BufferSize = AsciiStrSize (IScsiName); + + Status = gIScsiInitiatorName.Set (&gIScsiInitiatorName, &BufferSize, IScsiName); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid iSCSI Name!", + NULL + ); + } + + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY; + break; + + case KEY_SAVE_ATTEMPT_CONFIG: + Status = IScsiConvertIfrNvDataToAttemptConfigData (IfrNvData, Private->Current); + if (EFI_ERROR (Status)) { + break; + } + + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT; + break; + + case KEY_SAVE_ORDER_CHANGES: + // + // Sync the Attempt Order to NVR. + // + Status = IScsiConfigOrderAttempts (IfrNvData); + if (EFI_ERROR (Status)) { + break; + } + + IScsiConfigUpdateAttempt (); + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT; + break; + + case KEY_IGNORE_ORDER_CHANGES: + CopyMem ( + IfrNvData->DynamicOrderedList, + OldIfrNvData.DynamicOrderedList, + sizeof (IfrNvData->DynamicOrderedList) + ); + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT; + break; + + case KEY_SAVE_DELETE_ATTEMPT: + // + // Delete the Attempt Order from NVR + // + Status = IScsiConfigDeleteAttempts (IfrNvData); + if (EFI_ERROR (Status)) { + break; + } + + IScsiConfigUpdateAttempt (); + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT; + break; + + case KEY_IGNORE_DELETE_ATTEMPT: + CopyMem ( + IfrNvData->DeleteAttemptList, + OldIfrNvData.DeleteAttemptList, + sizeof (IfrNvData->DeleteAttemptList) + ); + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT; + break; + + case KEY_IP_MODE: + switch (Value->u8) { + case IP_MODE_IP6: + NicInfo = IScsiGetNicInfoByIndex (Private->Current->NicIndex); + if(NicInfo == NULL) { + break; + } + + if(!NicInfo->Ipv6Available) { + // + // Current NIC doesn't Support IPv6, hence use IPv4. + // + IfrNvData->IpMode = IP_MODE_IP4; + + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Current NIC doesn't Support IPv6!", + NULL + ); + } + + case IP_MODE_IP4: + ZeroMem (IfrNvData->LocalIp, sizeof (IfrNvData->LocalIp)); + ZeroMem (IfrNvData->SubnetMask, sizeof (IfrNvData->SubnetMask)); + ZeroMem (IfrNvData->Gateway, sizeof (IfrNvData->Gateway)); + ZeroMem (IfrNvData->TargetIp, sizeof (IfrNvData->TargetIp)); + Private->Current->AutoConfigureMode = 0; + ZeroMem (&Private->Current->SessionConfigData.LocalIp, sizeof (EFI_IP_ADDRESS)); + ZeroMem (&Private->Current->SessionConfigData.SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + ZeroMem (&Private->Current->SessionConfigData.Gateway, sizeof (EFI_IP_ADDRESS)); + ZeroMem (&Private->Current->SessionConfigData.TargetIp, sizeof (EFI_IP_ADDRESS)); + + break; + } + + break; + + case KEY_LOCAL_IP: + Status = NetLibStrToIp4 (IfrNvData->LocalIp, &HostIp.v4); + if (EFI_ERROR (Status) || + ((Private->Current->SessionConfigData.SubnetMask.Addr[0] != 0) && + !NetIp4IsUnicast (NTOHL (HostIp.Addr[0]), NTOHL(*(UINT32*)Private->Current->SessionConfigData.SubnetMask.Addr)))) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid IP address!", + NULL + ); + + Status = EFI_INVALID_PARAMETER; + } else { + CopyMem (&Private->Current->SessionConfigData.LocalIp, &HostIp.v4, sizeof (HostIp.v4)); + } + + break; + + case KEY_SUBNET_MASK: + Status = NetLibStrToIp4 (IfrNvData->SubnetMask, &SubnetMask.v4); + if (EFI_ERROR (Status) || ((SubnetMask.Addr[0] != 0) && (IScsiGetSubnetMaskPrefixLength (&SubnetMask.v4) == 0))) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Subnet Mask!", + NULL + ); + + Status = EFI_INVALID_PARAMETER; + } else { + CopyMem (&Private->Current->SessionConfigData.SubnetMask, &SubnetMask.v4, sizeof (SubnetMask.v4)); + } + + break; + + case KEY_GATE_WAY: + Status = NetLibStrToIp4 (IfrNvData->Gateway, &Gateway.v4); + if (EFI_ERROR (Status) || + ((Gateway.Addr[0] != 0) && + (Private->Current->SessionConfigData.SubnetMask.Addr[0] != 0) && + !NetIp4IsUnicast (NTOHL (Gateway.Addr[0]), NTOHL(*(UINT32*)Private->Current->SessionConfigData.SubnetMask.Addr)))) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Gateway!", + NULL + ); + Status = EFI_INVALID_PARAMETER; + } else { + CopyMem (&Private->Current->SessionConfigData.Gateway, &Gateway.v4, sizeof (Gateway.v4)); + } + + break; + + case KEY_TARGET_IP: + UnicodeStrToAsciiStrS (IfrNvData->TargetIp, IpString, sizeof (IpString)); + Status = IScsiAsciiStrToIp (IpString, IfrNvData->IpMode, &HostIp); + if (EFI_ERROR (Status) || !IpIsUnicast (&HostIp, IfrNvData->IpMode)) { + // + // The target is expressed in URL format or an invalid Ip address, just save. + // + Private->Current->SessionConfigData.DnsMode = TRUE; + ZeroMem (&Private->Current->SessionConfigData.TargetIp, sizeof (Private->Current->SessionConfigData.TargetIp)); + UnicodeStrToAsciiStrS (IfrNvData->TargetIp, Private->Current->SessionConfigData.TargetUrl, ISCSI_NAME_MAX_SIZE); + } else { + Private->Current->SessionConfigData.DnsMode = FALSE; + CopyMem (&Private->Current->SessionConfigData.TargetIp, &HostIp, sizeof (HostIp)); + } + + break; + + case KEY_TARGET_NAME: + UnicodeStrToAsciiStrS (IfrNvData->TargetName, IScsiName, ISCSI_NAME_MAX_SIZE); + Status = IScsiNormalizeName (IScsiName, AsciiStrLen (IScsiName)); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid iSCSI Name!", + NULL + ); + } else { + AsciiStrCpyS (Private->Current->SessionConfigData.TargetName, ISCSI_NAME_MAX_SIZE, IScsiName); + } + + break; + + case KEY_DHCP_ENABLE: + if (IfrNvData->InitiatorInfoFromDhcp == 0) { + IfrNvData->TargetInfoFromDhcp = 0; + } + + break; + + case KEY_BOOT_LUN: + UnicodeStrToAsciiStrS (IfrNvData->BootLun, LunString, sizeof (LunString)); + Status = IScsiAsciiStrToLun (LunString, (UINT8 *) &Lun); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid LUN string!", + NULL + ); + } else { + CopyMem (Private->Current->SessionConfigData.BootLun, &Lun, sizeof (Lun)); + } + + break; + + case KEY_AUTH_TYPE: + switch (Value->u8) { + case ISCSI_AUTH_TYPE_CHAP: + IfrNvData->CHAPType = ISCSI_CHAP_UNI; + break; + default: + break; + } + + break; + + case KEY_CHAP_NAME: + UnicodeStrToAsciiStrS ( + IfrNvData->CHAPName, + Private->Current->AuthConfigData.CHAP.CHAPName, + sizeof (Private->Current->AuthConfigData.CHAP.CHAPName) + ); + break; + + case KEY_CHAP_SECRET: + UnicodeStrToAsciiStrS ( + IfrNvData->CHAPSecret, + Private->Current->AuthConfigData.CHAP.CHAPSecret, + sizeof (Private->Current->AuthConfigData.CHAP.CHAPSecret) + ); + break; + + case KEY_REVERSE_CHAP_NAME: + UnicodeStrToAsciiStrS ( + IfrNvData->ReverseCHAPName, + Private->Current->AuthConfigData.CHAP.ReverseCHAPName, + sizeof (Private->Current->AuthConfigData.CHAP.ReverseCHAPName) + ); + break; + + case KEY_REVERSE_CHAP_SECRET: + UnicodeStrToAsciiStrS ( + IfrNvData->ReverseCHAPSecret, + Private->Current->AuthConfigData.CHAP.ReverseCHAPSecret, + sizeof (Private->Current->AuthConfigData.CHAP.ReverseCHAPSecret) + ); + break; + + case KEY_CONFIG_ISID: + IScsiParseIsIdFromString (IfrNvData->IsId, Private->Current->SessionConfigData.IsId); + IScsiConvertIsIdToString (IfrNvData->IsId, Private->Current->SessionConfigData.IsId); + + break; + + default: + break; + } + } + + if (!EFI_ERROR (Status)) { + // + // Pass changed uncommitted data back to Form Browser. + // + BufferSize = sizeof (ISCSI_CONFIG_IFR_NVDATA); + HiiSetBrowserData (NULL, NULL, BufferSize, (UINT8 *) IfrNvData, NULL); + } + + FreePool (IfrNvData); + FreePool (IScsiName); + + return Status; +} + + +/** + Initialize the iSCSI configuration form. + + @param[in] DriverBindingHandle The iSCSI driverbinding handle. + + @retval EFI_SUCCESS The iSCSI configuration form is initialized. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +IScsiConfigFormInit ( + IN EFI_HANDLE DriverBindingHandle + ) +{ + EFI_STATUS Status; + ISCSI_FORM_CALLBACK_INFO *CallbackInfo; + + CallbackInfo = (ISCSI_FORM_CALLBACK_INFO *) AllocateZeroPool (sizeof (ISCSI_FORM_CALLBACK_INFO)); + if (CallbackInfo == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + CallbackInfo->Signature = ISCSI_FORM_CALLBACK_INFO_SIGNATURE; + CallbackInfo->Current = NULL; + + CallbackInfo->ConfigAccess.ExtractConfig = IScsiFormExtractConfig; + CallbackInfo->ConfigAccess.RouteConfig = IScsiFormRouteConfig; + CallbackInfo->ConfigAccess.Callback = IScsiFormCallback; + + // + // Install Device Path Protocol and Config Access protocol to driver handle. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &CallbackInfo->DriverHandle, + &gEfiDevicePathProtocolGuid, + &mIScsiHiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &CallbackInfo->ConfigAccess, + NULL + ); + ASSERT_EFI_ERROR (Status); + + // + // Publish our HII data. + // + CallbackInfo->RegisteredHandle = HiiAddPackages ( + &gIScsiConfigGuid, + CallbackInfo->DriverHandle, + IScsiDxeStrings, + IScsiConfigVfrBin, + NULL + ); + if (CallbackInfo->RegisteredHandle == NULL) { + gBS->UninstallMultipleProtocolInterfaces ( + &CallbackInfo->DriverHandle, + &gEfiDevicePathProtocolGuid, + &mIScsiHiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &CallbackInfo->ConfigAccess, + NULL + ); + FreePool(CallbackInfo); + return EFI_OUT_OF_RESOURCES; + } + + mCallbackInfo = CallbackInfo; + + return EFI_SUCCESS; +} + + +/** + Unload the iSCSI configuration form, this includes: delete all the iSCSI + configuration entries, uninstall the form callback protocol, and + free the resources used. + + @param[in] DriverBindingHandle The iSCSI driverbinding handle. + + @retval EFI_SUCCESS The iSCSI configuration form is unloaded. + @retval Others Failed to unload the form. + +**/ +EFI_STATUS +IScsiConfigFormUnload ( + IN EFI_HANDLE DriverBindingHandle + ) +{ + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + ISCSI_NIC_INFO *NicInfo; + LIST_ENTRY *Entry; + EFI_STATUS Status; + + while (!IsListEmpty (&mPrivate->AttemptConfigs)) { + Entry = NetListRemoveHead (&mPrivate->AttemptConfigs); + AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + FreePool (AttemptConfigData); + mPrivate->AttemptCount--; + } + + ASSERT (mPrivate->AttemptCount == 0); + + while (!IsListEmpty (&mPrivate->NicInfoList)) { + Entry = NetListRemoveHead (&mPrivate->NicInfoList); + NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link); + FreePool (NicInfo); + mPrivate->NicCount--; + } + + ASSERT (mPrivate->NicCount == 0); + + FreePool (mPrivate); + mPrivate = NULL; + + // + // Remove HII package list. + // + HiiRemovePackages (mCallbackInfo->RegisteredHandle); + + // + // Uninstall Device Path Protocol and Config Access protocol. + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + mCallbackInfo->DriverHandle, + &gEfiDevicePathProtocolGuid, + &mIScsiHiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &mCallbackInfo->ConfigAccess, + NULL + ); + + FreePool (mCallbackInfo); + + return Status; +} diff --git a/NetworkPkg/IScsiDxe/IScsiConfig.h b/NetworkPkg/IScsiDxe/IScsiConfig.h new file mode 100644 index 000000000..2125351ce --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiConfig.h @@ -0,0 +1,221 @@ +/** @file + The header file of functions for configuring or getting the parameters + relating to iSCSI. + +Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ISCSI_CONFIG_H_ +#define _ISCSI_CONFIG_H_ + +#include "IScsiConfigNVDataStruc.h" + +typedef struct _ISCSI_FORM_CALLBACK_INFO ISCSI_FORM_CALLBACK_INFO; + +extern UINT8 IScsiConfigVfrBin[]; +extern UINT8 IScsiDxeStrings[]; +extern ISCSI_FORM_CALLBACK_INFO *mCallbackInfo; + + +#define VAR_OFFSET(Field) \ + ((UINT16) ((UINTN) &(((ISCSI_CONFIG_IFR_NVDATA *) 0)->Field))) + +#define QUESTION_ID(Field) \ + ((UINT16) (VAR_OFFSET (Field) + CONFIG_OPTION_OFFSET)) + +#define DYNAMIC_ONE_OF_VAR_OFFSET VAR_OFFSET (Enabled) +#define DYNAMIC_ORDERED_LIST_QUESTION_ID QUESTION_ID (DynamicOrderedList) +#define DYNAMIC_ORDERED_LIST_VAR_OFFSET VAR_OFFSET (DynamicOrderedList) +#define ATTEMPT_DEL_QUESTION_ID QUESTION_ID (DeleteAttemptList) +#define ATTEMPT_DEL_VAR_OFFSET VAR_OFFSET (DeleteAttemptList) +#define ATTEMPT_ADD_QUESTION_ID QUESTION_ID (AddAttemptList) +#define ATTEMPT_ADD_VAR_OFFSET VAR_OFFSET (AddAttemptList) + +// +// Define QuestionId and OffSet for Keywords. +// +#define ATTEMPT_MAC_ADDR_VAR_OFFSET VAR_OFFSET (ISCSIMacAddr) +#define ATTEMPT_ATTEMPT_NAME_QUESTION_ID QUESTION_ID (ISCSIAttemptName) +#define ATTEMPT_ATTEMPT_NAME_VAR_OFFSET VAR_OFFSET (ISCSIAttemptName) +#define ATTEMPT_BOOTENABLE_QUESTION_ID QUESTION_ID (ISCSIBootEnableList) +#define ATTEMPT_BOOTENABLE_VAR_OFFSET VAR_OFFSET (ISCSIBootEnableList) +#define ATTEMPT_ADDRESS_TYPE_QUESTION_ID QUESTION_ID (ISCSIIpAddressTypeList) +#define ATTEMPT_ADDRESS_TYPE_VAR_OFFSET VAR_OFFSET (ISCSIIpAddressTypeList) +#define ATTEMPT_CONNECT_RETRY_QUESTION_ID QUESTION_ID (ISCSIConnectRetry) +#define ATTEMPT_CONNECT_RETRY_VAR_OFFSET VAR_OFFSET (ISCSIConnectRetry) +#define ATTEMPT_CONNECT_TIMEOUT_QUESTION_ID QUESTION_ID (ISCSIConnectTimeout) +#define ATTEMPT_CONNECT_TIMEOUT_VAR_OFFSET VAR_OFFSET (ISCSIConnectTimeout) +#define ATTEMPT_ISID_QUESTION_ID QUESTION_ID (Keyword->ISCSIIsId) +#define ATTEMPT_ISID_VAR_OFFSET VAR_OFFSET (Keyword->ISCSIIsId) +#define ATTEMPT_INITIATOR_VIA_DHCP_QUESTION_ID QUESTION_ID (ISCSIInitiatorInfoViaDHCP) +#define ATTEMPT_INITIATOR_VIA_DHCP_VAR_OFFSET VAR_OFFSET (ISCSIInitiatorInfoViaDHCP) +#define ATTEMPT_INITIATOR_IP_ADDRESS_QUESTION_ID QUESTION_ID (Keyword->ISCSIInitiatorIpAddress) +#define ATTEMPT_INITIATOR_IP_ADDRESS_VAR_OFFSET VAR_OFFSET (Keyword->ISCSIInitiatorIpAddress) +#define ATTEMPT_INITIATOR_NET_MASK_QUESTION_ID QUESTION_ID (Keyword->ISCSIInitiatorNetmask) +#define ATTEMPT_INITIATOR_NET_MASK_VAR_OFFSET VAR_OFFSET (Keyword->ISCSIInitiatorNetmask) +#define ATTEMPT_INITIATOR_GATE_WAY_QUESTION_ID QUESTION_ID (Keyword->ISCSIInitiatorGateway) +#define ATTEMPT_INITIATOR_GATE_WAY_VAR_OFFSET VAR_OFFSET (Keyword->ISCSIInitiatorGateway) +#define ATTEMPT_TARGET_VIA_DHCP_QUESTION_ID QUESTION_ID (ISCSITargetInfoViaDHCP) +#define ATTEMPT_TARGET_VIA_DHCP_VAR_OFFSET VAR_OFFSET (ISCSITargetInfoViaDHCP) +#define ATTEMPT_TARGET_NAME_QUESTION_ID QUESTION_ID (Keyword->ISCSITargetName) +#define ATTEMPT_TARGET_NAME_VAR_OFFSET VAR_OFFSET (Keyword->ISCSITargetName) +#define ATTEMPT_TARGET_IP_ADDRESS_QUESTION_ID QUESTION_ID (Keyword->ISCSITargetIpAddress) +#define ATTEMPT_TARGET_IP_ADDRESS_VAR_OFFSET VAR_OFFSET (Keyword->ISCSITargetIpAddress) +#define ATTEMPT_TARGET_TCP_PORT_QUESTION_ID QUESTION_ID (ISCSITargetTcpPort) +#define ATTEMPT_TARGET_TCP_PORT_VAR_OFFSET VAR_OFFSET (ISCSITargetTcpPort) +#define ATTEMPT_LUN_QUESTION_ID QUESTION_ID (Keyword->ISCSILun) +#define ATTEMPT_LUN_VAR_OFFSET VAR_OFFSET (Keyword->ISCSILun) +#define ATTEMPT_AUTHENTICATION_METHOD_QUESTION_ID QUESTION_ID (ISCSIAuthenticationMethod) +#define ATTEMPT_AUTHENTICATION_METHOD_VAR_OFFSET VAR_OFFSET (ISCSIAuthenticationMethod) +#define ATTEMPT_CHARTYPE_QUESTION_ID QUESTION_ID (ISCSIChapType) +#define ATTEMPT_CHARTYPE_VAR_OFFSET VAR_OFFSET (ISCSIChapType) +#define ATTEMPT_CHAR_USER_NAME_QUESTION_ID QUESTION_ID (Keyword->ISCSIChapUsername) +#define ATTEMPT_CHAR_USER_NAME_VAR_OFFSET VAR_OFFSET (Keyword->ISCSIChapUsername) +#define ATTEMPT_CHAR_SECRET_QUESTION_ID QUESTION_ID (Keyword->ISCSIChapSecret) +#define ATTEMPT_CHAR_SECRET_VAR_OFFSET VAR_OFFSET (Keyword->ISCSIChapSecret) +#define ATTEMPT_CHAR_REVERSE_USER_NAME_QUESTION_ID QUESTION_ID (Keyword->ISCSIReverseChapUsername) +#define ATTEMPT_CHAR_REVERSE_USER_NAME_VAR_OFFSET VAR_OFFSET (Keyword->ISCSIReverseChapUsername) +#define ATTEMPT_CHAR_REVERSE_SECRET_QUESTION_ID QUESTION_ID (Keyword->ISCSIReverseChapSecret) +#define ATTEMPT_CHAR_REVERSE_SECRET_VAR_OFFSET VAR_OFFSET (Keyword->ISCSIReverseChapSecret) + + +#define ISCSI_INITATOR_NAME_VAR_NAME L"I_NAME" + +#define ISCSI_CONFIG_VAR_ATTR (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE) + +#define ISCSI_FORM_CALLBACK_INFO_SIGNATURE SIGNATURE_32 ('I', 'f', 'c', 'i') + +#define ISCSI_FORM_CALLBACK_INFO_FROM_FORM_CALLBACK(Callback) \ + CR ( \ + Callback, \ + ISCSI_FORM_CALLBACK_INFO, \ + ConfigAccess, \ + ISCSI_FORM_CALLBACK_INFO_SIGNATURE \ + ) + +#pragma pack(1) +struct _ISCSI_ATTEMPT_CONFIG_NVDATA { + LIST_ENTRY Link; + UINT8 NicIndex; + UINT8 AttemptConfigIndex; + BOOLEAN DhcpSuccess; + BOOLEAN ValidiBFTPath; + BOOLEAN ValidPath; + UINT8 AutoConfigureMode; + EFI_STRING_ID AttemptTitleToken; + EFI_STRING_ID AttemptTitleHelpToken; + CHAR8 AttemptName[ATTEMPT_NAME_SIZE]; + CHAR8 MacString[ISCSI_MAX_MAC_STRING_LEN]; + EFI_IP_ADDRESS PrimaryDns; + EFI_IP_ADDRESS SecondaryDns; + EFI_IP_ADDRESS DhcpServer; + ISCSI_SESSION_CONFIG_NVDATA SessionConfigData; + UINT8 AuthenticationType; + union { + ISCSI_CHAP_AUTH_CONFIG_NVDATA CHAP; + } AuthConfigData; + BOOLEAN AutoConfigureSuccess; + UINT8 Actived; +}; + +/// +/// HII specific Vendor Device Path definition. +/// +typedef struct { + VENDOR_DEVICE_PATH VendorDevicePath; + EFI_DEVICE_PATH_PROTOCOL End; +} HII_VENDOR_DEVICE_PATH; + +#pragma pack() + +struct _ISCSI_FORM_CALLBACK_INFO { + UINT32 Signature; + EFI_HANDLE DriverHandle; + EFI_HII_CONFIG_ACCESS_PROTOCOL ConfigAccess; + UINT16 *KeyList; + VOID *FormBuffer; + EFI_HII_HANDLE RegisteredHandle; + ISCSI_ATTEMPT_CONFIG_NVDATA *Current; +}; + +/** + Create Hii Extend Label OpCode as the start opcode and end opcode. It is + a help function. + + @param[in] StartLabelNumber The number of start label. + @param[out] StartOpCodeHandle Points to the start opcode handle. + @param[out] StartLabel Points to the created start opcode. + @param[out] EndOpCodeHandle Points to the end opcode handle. + @param[out] EndLabel Points to the created end opcode. + + @retval EFI_OUT_OF_RESOURCES Do not have sufficient resource to finish this + operation. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +IScsiCreateOpCode ( + IN UINT16 StartLabelNumber, + OUT VOID **StartOpCodeHandle, + OUT EFI_IFR_GUID_LABEL **StartLabel, + OUT VOID **EndOpCodeHandle, + OUT EFI_IFR_GUID_LABEL **EndLabel + ); + +/** + Initialize the iSCSI configuration form. + + @param[in] DriverBindingHandle The iSCSI driverbinding handle. + + @retval EFI_SUCCESS The iSCSI configuration form is initialized. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +IScsiConfigFormInit ( + IN EFI_HANDLE DriverBindingHandle + ); + +/** + Unload the iSCSI configuration form, this includes: delete all the iSCSI + configuration entries, uninstall the form callback protocol, and + free the resources used. + + @param[in] DriverBindingHandle The iSCSI driverbinding handle. + + @retval EFI_SUCCESS The iSCSI configuration form is unloaded. + @retval Others Failed to unload the form. + +**/ +EFI_STATUS +IScsiConfigFormUnload ( + IN EFI_HANDLE DriverBindingHandle + ); + +/** + Update the MAIN form to display the configured attempts. + +**/ +VOID +IScsiConfigUpdateAttempt ( + VOID + ); + +/** + Get the attempt config data from global structure by the ConfigIndex. + + @param[in] AttemptConfigIndex The unique index indicates the attempt. + + @return Pointer to the attempt config data. + @retval NULL The attempt configuration data can not be found. + +**/ +ISCSI_ATTEMPT_CONFIG_NVDATA * +IScsiConfigGetAttemptByConfigIndex ( + IN UINT8 AttemptConfigIndex + ); + +#endif diff --git a/NetworkPkg/IScsiDxe/IScsiConfigNVDataStruc.h b/NetworkPkg/IScsiDxe/IScsiConfigNVDataStruc.h new file mode 100644 index 000000000..688fb03bf --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiConfigNVDataStruc.h @@ -0,0 +1,232 @@ +/** @file + Define NVData structures used by the iSCSI configuration component. + +Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ISCSI_NVDATASTRUC_H_ +#define _ISCSI_NVDATASTRUC_H_ + +#include + +#define VAR_EQ_TEST_NAME 0x100 +#define CONFIGURATION_VARSTORE_ID 0x6666 + +#define FORMID_MAIN_FORM 1 +#define FORMID_MAC_FORM 2 +#define FORMID_ATTEMPT_FORM 3 +#define FORMID_ORDER_FORM 4 +#define FORMID_DELETE_FORM 5 + +#define ISCSI_MAX_ATTEMPTS_NUM FixedPcdGet8 (PcdMaxIScsiAttemptNumber) + +#define ISCSI_NAME_IFR_MIN_SIZE 4 +#define ISCSI_NAME_IFR_MAX_SIZE 223 +#define ISCSI_NAME_MAX_SIZE 224 + +#define ATTEMPT_NAME_LIST_SIZE 96 +#define ATTEMPT_NAME_SIZE 12 + +#define CONNECT_MIN_RETRY 0 +#define CONNECT_MAX_RETRY 16 + +#define CONNECT_MIN_TIMEOUT 100 +#define CONNECT_MAX_TIMEOUT 20000 +#define CONNECT_DEFAULT_TIMEOUT 1000 + +#define ISCSI_ACTIVE_DISABLED 0 +#define ISCSI_ACTIVE_ENABLED 1 + +#define ISCSI_DISABLED 0 +#define ISCSI_ENABLED 1 +#define ISCSI_ENABLED_FOR_MPIO 2 + +#define IP_MODE_IP4 0 +#define IP_MODE_IP6 1 +#define IP_MODE_AUTOCONFIG 2 + +#define ISCSI_AUTH_TYPE_NONE 0 +#define ISCSI_AUTH_TYPE_CHAP 1 +#define ISCSI_AUTH_TYPE_KRB 2 + +#define IP4_MIN_SIZE 7 +#define IP4_MAX_SIZE 15 +#define IP4_STR_MAX_SIZE 16 + +// +// Macros used for an IPv4 or an IPv6 address. +// +#define IP_MIN_SIZE 2 +#define IP_MAX_SIZE 39 +#define IP_STR_MAX_SIZE 40 + +#define LUN_MIN_SIZE 1 +#define LUN_MAX_SIZE 20 +#define ISCSI_LUN_STR_MAX_LEN 21 + +#define ISCSI_CHAP_UNI 0 +#define ISCSI_CHAP_MUTUAL 1 + +#define TARGET_PORT_MIN_NUM 0 +#define TARGET_PORT_MAX_NUM 65535 +#define LABEL_END 0xffff + +#define KEY_INITIATOR_NAME 0x101 +#define KEY_DHCP_ENABLE 0x102 +#define KEY_LOCAL_IP 0x103 +#define KEY_SUBNET_MASK 0x104 +#define KEY_GATE_WAY 0x105 +#define KEY_TARGET_IP 0x106 +#define KEY_CHAP_NAME 0x107 +#define KEY_CHAP_SECRET 0x108 +#define KEY_REVERSE_CHAP_NAME 0x109 +#define KEY_REVERSE_CHAP_SECRET 0x10a +#define KEY_SAVE_CHANGES 0x10b +#define KEY_TARGET_NAME 0x10c +#define KEY_BOOT_LUN 0x10d + +#define KEY_ADD_ATTEMPT 0x10e +#define KEY_SAVE_ATTEMPT_CONFIG 0x10f +#define KEY_ORDER_ATTEMPT_CONFIG 0x110 +#define KEY_SAVE_ORDER_CHANGES 0x111 +#define KEY_IGNORE_ORDER_CHANGES 0x112 +#define KEY_ATTEMPT_NAME 0x113 +#define KEY_SAVE_DELETE_ATTEMPT 0x114 +#define KEY_IGNORE_DELETE_ATTEMPT 0x115 +#define KEY_DELETE_ATTEMPT 0x116 + +#define KEY_IP_MODE 0x11c +#define KEY_AUTH_TYPE 0x11d +#define KEY_CONFIG_ISID 0x11e + +#define ATTEMPT_ENTRY_LABEL 0x9000 +#define KEY_ATTEMPT_ENTRY_BASE 0xa000 +#define KEY_DE_ATTEMPT_ENTRY_BASE 0xb000 + +#define KEY_DEVICE_ENTRY_BASE 0x1000 +#define KEY_MAC_ENTRY_BASE 0x2000 +#define MAC_ENTRY_LABEL 0x3000 +#define ORDER_ENTRY_LABEL 0x4000 +#define DELETE_ENTRY_LABEL 0x5000 +#define KEYWORD_ENTRY_LABEL 0x6000 +#define CONFIG_OPTION_OFFSET 0x9000 + +#define ISCSI_CHAP_SECRET_MIN_LEN 12 +#define ISCSI_CHAP_SECRET_MAX_LEN 16 +// +// ISCSI_CHAP_SECRET_STORAGE = ISCSI_CHAP_SECRET_MAX_LEN + sizeof (NULL-Terminator) +// +#define ISCSI_CHAP_SECRET_STORAGE 17 + +#define ISCSI_CHAP_NAME_MAX_LEN 126 +#define ISCSI_CHAP_NAME_STORAGE 127 + +#define KERBEROS_SECRET_MIN_LEN 12 +#define KERBEROS_SECRET_MAX_LEN 16 +#define KERBEROS_SECRET_STORAGE 17 +#define KERBEROS_NAME_MAX_LEN 96 +#define KERBEROS_KDC_PORT_MIN_NUM 0 +#define KERBEROS_KDC_PORT_MAX_NUM 65535 + +#define ISID_CONFIGURABLE_MIN_LEN 6 +#define ISID_CONFIGURABLE_MAX_LEN 12 +#define ISID_CONFIGURABLE_STORAGE 13 + +// +// sizeof (EFI_MAC_ADDRESS) * 3 +// +#define ISCSI_MAX_MAC_STRING_LEN 96 + +/// +/// Macro used for target Url. +/// +#define ISCSI_TARGET_URI_MIN_SIZE 0 +#define ISCSI_TARGET_URI_MAX_SIZE 255 + +#pragma pack(1) + +// +// Used by keyword. +// +typedef struct { + CHAR16 ISCSIIsId[ISID_CONFIGURABLE_STORAGE]; + CHAR16 ISCSIInitiatorIpAddress[IP4_STR_MAX_SIZE]; + CHAR16 ISCSIInitiatorNetmask[IP4_STR_MAX_SIZE]; + CHAR16 ISCSIInitiatorGateway[IP4_STR_MAX_SIZE]; + CHAR16 ISCSITargetName[ISCSI_NAME_MAX_SIZE]; + CHAR16 ISCSITargetIpAddress[ISCSI_TARGET_URI_MAX_SIZE]; + CHAR16 ISCSILun[ISCSI_LUN_STR_MAX_LEN]; + CHAR16 ISCSIChapUsername[ISCSI_CHAP_NAME_STORAGE]; + CHAR16 ISCSIChapSecret[ISCSI_CHAP_SECRET_STORAGE]; + CHAR16 ISCSIReverseChapUsername[ISCSI_CHAP_NAME_STORAGE]; + CHAR16 ISCSIReverseChapSecret[ISCSI_CHAP_SECRET_STORAGE]; +} KEYWORD_STR; + +typedef struct _ISCSI_CONFIG_IFR_NVDATA { + CHAR16 InitiatorName[ISCSI_NAME_MAX_SIZE]; + CHAR16 AttemptName[ATTEMPT_NAME_SIZE]; + UINT8 Enabled; + UINT8 IpMode; + + UINT8 ConnectRetryCount; + UINT8 Padding1; + UINT16 ConnectTimeout; // Timeout value in milliseconds. + + UINT8 InitiatorInfoFromDhcp; + UINT8 TargetInfoFromDhcp; + CHAR16 LocalIp[IP4_STR_MAX_SIZE]; + CHAR16 SubnetMask[IP4_STR_MAX_SIZE]; + CHAR16 Gateway[IP4_STR_MAX_SIZE]; + + CHAR16 TargetName[ISCSI_NAME_MAX_SIZE]; + CHAR16 TargetIp[ISCSI_TARGET_URI_MAX_SIZE]; + UINT16 TargetPort; + CHAR16 BootLun[ISCSI_LUN_STR_MAX_LEN]; + + UINT8 AuthenticationType; + + UINT8 CHAPType; + CHAR16 CHAPName[ISCSI_CHAP_NAME_STORAGE]; + CHAR16 CHAPSecret[ISCSI_CHAP_SECRET_STORAGE]; + CHAR16 ReverseCHAPName[ISCSI_CHAP_NAME_STORAGE]; + CHAR16 ReverseCHAPSecret[ISCSI_CHAP_SECRET_STORAGE]; + + BOOLEAN MutualRequired; + UINT8 Padding2; + CHAR16 KerberosUserName[KERBEROS_NAME_MAX_LEN]; + CHAR16 KerberosUserSecret[KERBEROS_SECRET_STORAGE]; + CHAR16 KerberosKDCName[KERBEROS_NAME_MAX_LEN]; + CHAR16 KerberosKDCRealm[KERBEROS_NAME_MAX_LEN]; + CHAR16 KerberosKDCIp[IP_STR_MAX_SIZE]; + UINT16 KerberosKDCPort; + + UINT8 DynamicOrderedList[ISCSI_MAX_ATTEMPTS_NUM]; + UINT8 DeleteAttemptList[ISCSI_MAX_ATTEMPTS_NUM]; + UINT8 AddAttemptList[ISCSI_MAX_ATTEMPTS_NUM]; + CHAR16 IsId[ISID_CONFIGURABLE_STORAGE]; + + // + // This will be used by keywords. + // + CHAR16 ISCSIMacAddr[ISCSI_MAX_MAC_STRING_LEN]; + CHAR16 ISCSIAttemptOrder[ATTEMPT_NAME_LIST_SIZE]; + CHAR16 ISCSIAddAttemptList[ATTEMPT_NAME_LIST_SIZE]; + CHAR16 ISCSIDeleteAttemptList[ATTEMPT_NAME_LIST_SIZE]; + CHAR16 ISCSIDisplayAttemptList[ATTEMPT_NAME_LIST_SIZE]; + CHAR16 ISCSIAttemptName[ATTEMPT_NAME_LIST_SIZE]; + UINT8 ISCSIBootEnableList[ISCSI_MAX_ATTEMPTS_NUM]; + UINT8 ISCSIIpAddressTypeList[ISCSI_MAX_ATTEMPTS_NUM]; + UINT8 ISCSIConnectRetry[ISCSI_MAX_ATTEMPTS_NUM]; + UINT16 ISCSIConnectTimeout[ISCSI_MAX_ATTEMPTS_NUM]; + UINT8 ISCSIInitiatorInfoViaDHCP[ISCSI_MAX_ATTEMPTS_NUM]; + UINT8 ISCSITargetInfoViaDHCP[ISCSI_MAX_ATTEMPTS_NUM]; + UINT16 ISCSITargetTcpPort[ISCSI_MAX_ATTEMPTS_NUM]; + UINT8 ISCSIAuthenticationMethod[ISCSI_MAX_ATTEMPTS_NUM]; + UINT8 ISCSIChapType[ISCSI_MAX_ATTEMPTS_NUM]; + KEYWORD_STR Keyword[ISCSI_MAX_ATTEMPTS_NUM]; +} ISCSI_CONFIG_IFR_NVDATA; +#pragma pack() + +#endif diff --git a/NetworkPkg/IScsiDxe/IScsiConfigStrings.uni b/NetworkPkg/IScsiDxe/IScsiConfigStrings.uni new file mode 100644 index 000000000..70dfff99b --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiConfigStrings.uni @@ -0,0 +1,96 @@ +// *++ +// +// Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// Module Name: +// +// IScsiConfigStrings.uni +// +// Abstract: +// +// String definitions for iSCSI configuration. +// +// Revision History: +// +// --*/ + +/=# + +#langdef en-US "English" +#langdef x-UEFI-ns "UefiNameSpace" + +#string STR_ISCSI_CONFIG_FORM_TITLE #language en-US "iSCSI Configuration" +#string STR_ISCSI_CONFIG_FORM_HELP #language en-US "Configure the iSCSI parameters." +#string STR_ISCSI_MAIN_FORM_TITLE #language en-US "iSCSI Configuration" +#string STR_ISCSI_MAC_FORM_TITLE #language en-US "MAC Selection" +#string STR_ISCSI_CONFIG_INIT_NAME #language en-US "iSCSI Initiator Name" + #language x-UEFI-ns "iSCSIInitiatorName" +#string STR_ISCSI_CONFIG_INIT_NAME_HELP #language en-US "The worldwide unique name of iSCSI Initiator. Only IQN format is accepted." +#string STR_ISCSI_ATTEMPT_NAME #language en-US "iSCSI Attempt Name" +#string STR_ISCSI_ATTEMPT_NAME_HELP #language en-US "The human name defined for this attempt." +#string STR_ISCSI_CONFIG_RETRY #language en-US "Connection Retry Count" +#string STR_ISCSI_CONFIG_RETRY_HELP #language en-US "The minimum value is 0 and the maximum is 16. 0 means no retry." +#string STR_ISCSI_CONFIG_TIMEOUT #language en-US "Connection Establishing Timeout" +#string STR_ISCSI_CONFIG_TIMEOUT_HELP #language en-US "The timeout value in milliseconds. The minimum value is 100 milliseconds and the maximum is 20 seconds." +#string STR_ADD_ATTEMPT_ENTRY #language en-US "Add an Attempt" +#string STR_ISCSI_ATTEMPT_FORM_TITLE #language en-US "Attempt Configuration" +#string STR_DEL_ATTEMPT_ENTRY #language en-US "Delete Attempts" +#string STR_DEL_ATTEMPT_ENTRY_HELP #language en-US "Delete one or more attempts" +#string STR_ORDER_ATTEMPT_ENTRY #language en-US "Change Attempt Order" +#string STR_ISCSI_MODE_PROMPT #language en-US "iSCSI Mode" +#string STR_ISCSI_MODE_HELP #language en-US "Disabled, Enabled, Enabled for MPIO" +#string STR_ISCSI_MODE_DISABLED #language en-US "Disabled" +#string STR_ISCSI_MODE_ENABLED #language en-US "Enabled" +#string STR_ISCSI_MODE_ENABLED_FOR_MPIO #language en-US "Enabled for MPIO" +#string STR_IP_MODE_PROMPT #language en-US "Internet Protocol" +#string STR_IP_MODE_HELP #language en-US "Initiator IP address is system assigned in IP6 mode. In Autoconfigure mode, iSCSI driver will attempt to connect iSCSI target via IPv4 stack, if failed then attempt IPv6 stack." +#string STR_IP_MODE_IP4 #language en-US "IP4" +#string STR_IP_MODE_IP6 #language en-US "IP6" +#string STR_IP_MODE_AUTOCONFIG #language en-US "Autoconfigure" +#string STR_AUTHEN_TYPE_PROMPT #language en-US "Authentication Type" +#string STR_AUTHEN_TYPE_HELP #language en-US "Authentication method: CHAP, Kerberos, or None" +#string STR_AUTHEN_TYPE_CHAP #language en-US "CHAP" +#string STR_AUTHEN_TYPE_KERBEROS #language en-US "Kerberos" +#string STR_AUTHEN_TYPE_NONE #language en-US "None" +#string STR_ISCSI_LOCAL_IP_ADDRESS #language en-US " Initiator IP Address" +#string STR_ISCSI_LOCAL_MASK #language en-US " Initiator Subnet Mask" +#string STR_ISCSI_LOCAL_GATEWAY #language en-US " Gateway" +#string STR_ISCSI_IP_ADDRESS_HELP #language en-US "Enter IP address in dotted-decimal notation." +#string STR_ISCSI_TARGET_NAME #language en-US " Target Name" +#string STR_ISCSI_TARGET_NAME_HELP #language en-US "The worldwide unique name of the target. Only iqn. format is accepted." +#string STR_ISCSI_TARGET_ADDRESS #language en-US " Target Address" +#string STR_ISCSI_TARGET_ADDRESS_HELP #language en-US "Enter Target address in IPv4,IPv6 or URL format.You need to configure DNS server address in advance if input a URL string." +#string STR_ISCSI_TARGET_PORT #language en-US " Target Port" +#string STR_ISCSI_BOOT_LUN #language en-US " Boot LUN" +#string STR_ISCSI_BOOT_LUN_HELP #language en-US "Hexadecimal representation of the LU number. Examples are: 4752-3A4F-6b7e-2F99, 6734-9-156f-127, 4186-9" +#string STR_ISCSI_ENABLE_DHCP #language en-US "Enable DHCP" +#string STR_ISCSI_ENABLE_DHCP_ON_TARGET #language en-US "Get target info via DHCP" +#string STR_CHAP_TYPE_PROMPT #language en-US " CHAP Type" +#string STR_CHAP_TYPE_HELP #language en-US "None, One way CHAP or mutual CHAP" +#string STR_CHAP_TYPE_UNI #language en-US "One way" +#string STR_CHAP_TYPE_MUTUAL #language en-US "Mutual" +#string STR_ISCSI_CHAP_NAME #language en-US " CHAP Name" +#string STR_ISCSI_CHAP_SECRET #language en-US " CHAP Secret" +#string STR_ISCSI_CHAP_SECRET_HELP #language en-US "The minimum length is 12 bytes and the maximum length is 16 bytes." +#string STR_ISCSI_REVERSE_CHAP_NAME #language en-US " Reverse CHAP Name" +#string STR_ISCSI_REVERSE_CHAP_SECRET #language en-US " Reverse CHAP Secret" +#string STR_RETURN_MAIN_FORM #language en-US "Back to Previous Page" +#string STR_SAVE_CHANGES #language en-US "Save Changes" +#string STR_SAVE_CHANGES_HELP #language en-US "Must reboot system manually for changes to take place." +#string STR_NULL #language en-US "" +#string STR_SAVE_AND_EXIT #language en-US "Commit Changes and Exit" +#string STR_NO_SAVE_AND_EXIT #language en-US "Discard Changes and Exit" +#string STR_ISCSI_CONFIG_ISID #language en-US "ISID" +#string STR_ISCSI_CONFIG_ISID_HELP #language en-US "OUI-format ISID in 6 bytes, default value are derived from MAC address. Only last 3 bytes are configurable. Example: update 0ABBCCDDEEFF to 0ABBCCF07901 by input F07901." +#string STR_ISCSI_MAC_PROMPT #language en-US "Configure the mac address for the attempt" + #language x-UEFI-ns "iSCSIMacAddr" +#string STR_ISCSI_ADD_ATTEMPTS #language en-US "Add Attempts" + #language x-UEFI-ns "iSCSIAddAttempts" +#string STR_ISCSI_DELETE_ATTEMPTS #language en-US "Delete Attempts" + #language x-UEFI-ns "iSCSIDeleteAttempts" +#string STR_ISCSI_DISPLAY_ATTEMPTS #language en-US "Display Attempts" + #language x-UEFI-ns "iSCSIDisplayAttemptList" +#string STR_ISCSI_ATTEMPT_ORDER #language en-US "New Attempt Order" + #language x-UEFI-ns "iSCSIAttemptOrder" +#string STR_ISCSI_ISID_HELP #language en-US "The iSCSI ISID. Default value are derived from MAC address. Only last 3 bytes are configurable." diff --git a/NetworkPkg/IScsiDxe/IScsiConfigVfr.vfr b/NetworkPkg/IScsiDxe/IScsiConfigVfr.vfr new file mode 100644 index 000000000..e53c1e5f0 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiConfigVfr.vfr @@ -0,0 +1,399 @@ +/** @file + VFR file used by the iSCSI configuration component. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiConfigNVDataStruc.h" + +#define EFI_NETWORK_DEVICE_CLASS 0x04 + +formset + guid = ISCSI_CONFIG_GUID, + title = STRING_TOKEN(STR_ISCSI_CONFIG_FORM_TITLE), + help = STRING_TOKEN(STR_ISCSI_CONFIG_FORM_HELP), + + varstore ISCSI_CONFIG_IFR_NVDATA, + varid = CONFIGURATION_VARSTORE_ID, + name = ISCSI_CONFIG_IFR_NVDATA, + guid = ISCSI_CONFIG_GUID; + + form formid = FORMID_MAIN_FORM, + title = STRING_TOKEN(STR_ISCSI_MAIN_FORM_TITLE); + + string varid = ISCSI_CONFIG_IFR_NVDATA.InitiatorName, + prompt = STRING_TOKEN(STR_ISCSI_CONFIG_INIT_NAME), + help = STRING_TOKEN(STR_ISCSI_CONFIG_INIT_NAME_HELP), + flags = INTERACTIVE, + key = KEY_INITIATOR_NAME, + minsize = ISCSI_NAME_IFR_MIN_SIZE, + maxsize = ISCSI_NAME_IFR_MAX_SIZE, + endstring; + + subtitle text = STRING_TOKEN(STR_NULL); + + goto FORMID_MAC_FORM, + prompt = STRING_TOKEN(STR_ADD_ATTEMPT_ENTRY), + help = STRING_TOKEN(STR_ADD_ATTEMPT_ENTRY), + flags = INTERACTIVE, + key = KEY_ADD_ATTEMPT; + + label ATTEMPT_ENTRY_LABEL; + label LABEL_END; + + subtitle text = STRING_TOKEN(STR_NULL); + + goto FORMID_DELETE_FORM, + prompt = STRING_TOKEN (STR_DEL_ATTEMPT_ENTRY), + help = STRING_TOKEN (STR_DEL_ATTEMPT_ENTRY_HELP), + flags = INTERACTIVE, + key = KEY_DELETE_ATTEMPT; + + subtitle text = STRING_TOKEN(STR_NULL); + + goto FORMID_ORDER_FORM, + prompt = STRING_TOKEN (STR_ORDER_ATTEMPT_ENTRY), + help = STRING_TOKEN (STR_ORDER_ATTEMPT_ENTRY), + flags = INTERACTIVE, + key = KEY_ORDER_ATTEMPT_CONFIG; + + subtitle text = STRING_TOKEN(STR_NULL); + + endform; + + form formid = FORMID_MAC_FORM, + title = STRING_TOKEN(STR_ISCSI_MAC_FORM_TITLE); + + label MAC_ENTRY_LABEL; + label LABEL_END; + + endform; + + form formid = FORMID_ORDER_FORM, + title = STRING_TOKEN(STR_ORDER_ATTEMPT_ENTRY); + + label ORDER_ENTRY_LABEL; + label LABEL_END; + + text + help = STRING_TOKEN (STR_SAVE_AND_EXIT), + text = STRING_TOKEN (STR_SAVE_AND_EXIT), + flags = INTERACTIVE, + key = KEY_SAVE_ORDER_CHANGES; + + text + help = STRING_TOKEN (STR_NO_SAVE_AND_EXIT), + text = STRING_TOKEN (STR_NO_SAVE_AND_EXIT), + flags = INTERACTIVE, + key = KEY_IGNORE_ORDER_CHANGES; + endform; + + form formid = FORMID_DELETE_FORM, + title = STRING_TOKEN(STR_DEL_ATTEMPT_ENTRY); + + label DELETE_ENTRY_LABEL; + label LABEL_END; + + text + help = STRING_TOKEN (STR_SAVE_AND_EXIT), + text = STRING_TOKEN (STR_SAVE_AND_EXIT), + flags = INTERACTIVE, + key = KEY_SAVE_DELETE_ATTEMPT; + + text + help = STRING_TOKEN (STR_NO_SAVE_AND_EXIT), + text = STRING_TOKEN (STR_NO_SAVE_AND_EXIT), + flags = INTERACTIVE, + key = KEY_IGNORE_DELETE_ATTEMPT; + endform; + + form formid = FORMID_ATTEMPT_FORM, + title = STRING_TOKEN(STR_ISCSI_ATTEMPT_FORM_TITLE); + + string varid = ISCSI_CONFIG_IFR_NVDATA.AttemptName, + prompt = STRING_TOKEN(STR_ISCSI_ATTEMPT_NAME), + help = STRING_TOKEN(STR_ISCSI_ATTEMPT_NAME_HELP), + flags = READ_ONLY, + key = KEY_ATTEMPT_NAME, + minsize = 0, + maxsize = ATTEMPT_NAME_SIZE, + endstring; + + subtitle text = STRING_TOKEN(STR_NULL); + + oneof varid = ISCSI_CONFIG_IFR_NVDATA.Enabled, + prompt = STRING_TOKEN(STR_ISCSI_MODE_PROMPT), + help = STRING_TOKEN(STR_ISCSI_MODE_HELP), + option text = STRING_TOKEN(STR_ISCSI_MODE_DISABLED), value = ISCSI_DISABLED, flags = DEFAULT; + option text = STRING_TOKEN(STR_ISCSI_MODE_ENABLED), value = ISCSI_ENABLED, flags = 0; + option text = STRING_TOKEN(STR_ISCSI_MODE_ENABLED_FOR_MPIO), value = ISCSI_ENABLED_FOR_MPIO, flags = 0; + endoneof; + + subtitle text = STRING_TOKEN(STR_NULL); + + oneof varid = ISCSI_CONFIG_IFR_NVDATA.IpMode, + questionid = KEY_IP_MODE, + prompt = STRING_TOKEN(STR_IP_MODE_PROMPT), + help = STRING_TOKEN(STR_IP_MODE_HELP), + option text = STRING_TOKEN(STR_IP_MODE_IP4), value = IP_MODE_IP4, flags = INTERACTIVE; + option text = STRING_TOKEN(STR_IP_MODE_IP6), value = IP_MODE_IP6, flags = INTERACTIVE; + option text = STRING_TOKEN(STR_IP_MODE_AUTOCONFIG), value = IP_MODE_AUTOCONFIG, flags = INTERACTIVE; + endoneof; + + subtitle text = STRING_TOKEN(STR_NULL); + + numeric varid = ISCSI_CONFIG_IFR_NVDATA.ConnectRetryCount, + prompt = STRING_TOKEN(STR_ISCSI_CONFIG_RETRY), + help = STRING_TOKEN(STR_ISCSI_CONFIG_RETRY_HELP), + flags = 0, + minimum = CONNECT_MIN_RETRY, + maximum = CONNECT_MAX_RETRY, + step = 0, + endnumeric; + + numeric varid = ISCSI_CONFIG_IFR_NVDATA.ConnectTimeout, + prompt = STRING_TOKEN(STR_ISCSI_CONFIG_TIMEOUT), + help = STRING_TOKEN(STR_ISCSI_CONFIG_TIMEOUT_HELP), + flags = 0, + minimum = CONNECT_MIN_TIMEOUT, + maximum = CONNECT_MAX_TIMEOUT, + step = 0, + default = CONNECT_DEFAULT_TIMEOUT, + endnumeric; + + subtitle text = STRING_TOKEN(STR_NULL); + + string varid = ISCSI_CONFIG_IFR_NVDATA.IsId, + prompt = STRING_TOKEN(STR_ISCSI_CONFIG_ISID), + help = STRING_TOKEN(STR_ISCSI_CONFIG_ISID_HELP), + flags = INTERACTIVE, + key = KEY_CONFIG_ISID, + minsize = ISID_CONFIGURABLE_MIN_LEN, + maxsize = ISID_CONFIGURABLE_MAX_LEN, + endstring; + + subtitle text = STRING_TOKEN(STR_NULL); + + suppressif ideqval ISCSI_CONFIG_IFR_NVDATA.IpMode == IP_MODE_AUTOCONFIG; + checkbox varid = ISCSI_CONFIG_IFR_NVDATA.InitiatorInfoFromDhcp, + prompt = STRING_TOKEN(STR_ISCSI_ENABLE_DHCP), + help = STRING_TOKEN(STR_ISCSI_ENABLE_DHCP), + flags = INTERACTIVE, + key = KEY_DHCP_ENABLE, + endcheckbox; + endif; + + suppressif ideqval ISCSI_CONFIG_IFR_NVDATA.IpMode == IP_MODE_IP6 OR + ideqval ISCSI_CONFIG_IFR_NVDATA.IpMode == IP_MODE_AUTOCONFIG; + + grayoutif ideqval ISCSI_CONFIG_IFR_NVDATA.InitiatorInfoFromDhcp == 0x01; + string varid = ISCSI_CONFIG_IFR_NVDATA.LocalIp, + prompt = STRING_TOKEN(STR_ISCSI_LOCAL_IP_ADDRESS), + help = STRING_TOKEN(STR_ISCSI_IP_ADDRESS_HELP), + flags = INTERACTIVE, + key = KEY_LOCAL_IP, + minsize = IP4_MIN_SIZE, + maxsize = IP4_MAX_SIZE, + endstring; + + string varid = ISCSI_CONFIG_IFR_NVDATA.SubnetMask, + prompt = STRING_TOKEN(STR_ISCSI_LOCAL_MASK), + help = STRING_TOKEN(STR_ISCSI_IP_ADDRESS_HELP), + flags = INTERACTIVE, + key = KEY_SUBNET_MASK, + minsize = IP4_MIN_SIZE, + maxsize = IP4_MAX_SIZE, + endstring; + + string varid = ISCSI_CONFIG_IFR_NVDATA.Gateway, + prompt = STRING_TOKEN(STR_ISCSI_LOCAL_GATEWAY), + help = STRING_TOKEN(STR_ISCSI_IP_ADDRESS_HELP), + flags = INTERACTIVE, + key = KEY_GATE_WAY, + minsize = IP4_MIN_SIZE, + maxsize = IP4_MAX_SIZE, + endstring; + endif; + + endif; + + suppressif ideqval ISCSI_CONFIG_IFR_NVDATA.IpMode == IP_MODE_AUTOCONFIG; + subtitle text = STRING_TOKEN(STR_NULL); + endif; + + suppressif ideqval ISCSI_CONFIG_IFR_NVDATA.IpMode == IP_MODE_AUTOCONFIG OR + ideqval ISCSI_CONFIG_IFR_NVDATA.InitiatorInfoFromDhcp == 0x00; + checkbox varid = ISCSI_CONFIG_IFR_NVDATA.TargetInfoFromDhcp, + prompt = STRING_TOKEN(STR_ISCSI_ENABLE_DHCP_ON_TARGET), + help = STRING_TOKEN(STR_ISCSI_ENABLE_DHCP_ON_TARGET), + flags = 0, + endcheckbox; + endif; + + suppressif ideqval ISCSI_CONFIG_IFR_NVDATA.IpMode == IP_MODE_AUTOCONFIG OR + ideqval ISCSI_CONFIG_IFR_NVDATA.TargetInfoFromDhcp == 0x01; + + string varid = ISCSI_CONFIG_IFR_NVDATA.TargetName, + prompt = STRING_TOKEN(STR_ISCSI_TARGET_NAME), + help = STRING_TOKEN(STR_ISCSI_TARGET_NAME_HELP), + flags = INTERACTIVE, + key = KEY_TARGET_NAME, + minsize = ISCSI_NAME_IFR_MIN_SIZE, + maxsize = ISCSI_NAME_IFR_MAX_SIZE, + endstring; + + string varid = ISCSI_CONFIG_IFR_NVDATA.TargetIp, + prompt = STRING_TOKEN(STR_ISCSI_TARGET_ADDRESS), + help = STRING_TOKEN(STR_ISCSI_TARGET_ADDRESS_HELP), + flags = INTERACTIVE, + key = KEY_TARGET_IP, + minsize = ISCSI_TARGET_URI_MIN_SIZE, + maxsize = ISCSI_TARGET_URI_MAX_SIZE, + endstring; + + numeric varid = ISCSI_CONFIG_IFR_NVDATA.TargetPort, + prompt = STRING_TOKEN(STR_ISCSI_TARGET_PORT), + help = STRING_TOKEN(STR_ISCSI_TARGET_PORT), + flags = 0, + minimum = TARGET_PORT_MIN_NUM, + maximum = TARGET_PORT_MAX_NUM, + step = 0, + endnumeric; + + string varid = ISCSI_CONFIG_IFR_NVDATA.BootLun, + prompt = STRING_TOKEN(STR_ISCSI_BOOT_LUN), + help = STRING_TOKEN(STR_ISCSI_BOOT_LUN_HELP), + flags = INTERACTIVE, + key = KEY_BOOT_LUN, + minsize = LUN_MIN_SIZE, + maxsize = LUN_MAX_SIZE, + endstring; + + endif; + + suppressif ideqval ISCSI_CONFIG_IFR_NVDATA.IpMode == IP_MODE_AUTOCONFIG; + subtitle text = STRING_TOKEN(STR_NULL); + endif; + + oneof varid = ISCSI_CONFIG_IFR_NVDATA.AuthenticationType, + questionid = KEY_AUTH_TYPE, + prompt = STRING_TOKEN(STR_AUTHEN_TYPE_PROMPT), + help = STRING_TOKEN(STR_AUTHEN_TYPE_HELP), + option text = STRING_TOKEN(STR_AUTHEN_TYPE_CHAP), value = ISCSI_AUTH_TYPE_CHAP, flags = 0; + option text = STRING_TOKEN(STR_AUTHEN_TYPE_NONE), value = ISCSI_AUTH_TYPE_NONE, flags = DEFAULT; + endoneof; + + suppressif NOT ideqval ISCSI_CONFIG_IFR_NVDATA.AuthenticationType == ISCSI_AUTH_TYPE_CHAP; + oneof varid = ISCSI_CONFIG_IFR_NVDATA.CHAPType, + prompt = STRING_TOKEN(STR_CHAP_TYPE_PROMPT), + help = STRING_TOKEN(STR_CHAP_TYPE_HELP), + option text = STRING_TOKEN(STR_CHAP_TYPE_UNI), value = ISCSI_CHAP_UNI, flags = 0; + option text = STRING_TOKEN(STR_CHAP_TYPE_MUTUAL), value = ISCSI_CHAP_MUTUAL, flags = DEFAULT; + endoneof; + endif; + + suppressif NOT ideqval ISCSI_CONFIG_IFR_NVDATA.AuthenticationType == ISCSI_AUTH_TYPE_CHAP; + string varid = ISCSI_CONFIG_IFR_NVDATA.CHAPName, + prompt = STRING_TOKEN(STR_ISCSI_CHAP_NAME), + help = STRING_TOKEN(STR_ISCSI_CHAP_NAME), + flags = INTERACTIVE, + key = KEY_CHAP_NAME, + minsize = 0, + maxsize = ISCSI_CHAP_NAME_MAX_LEN, + endstring; + + string varid = ISCSI_CONFIG_IFR_NVDATA.CHAPSecret, + prompt = STRING_TOKEN(STR_ISCSI_CHAP_SECRET), + help = STRING_TOKEN(STR_ISCSI_CHAP_SECRET_HELP), + flags = INTERACTIVE, + key = KEY_CHAP_SECRET, + minsize = ISCSI_CHAP_SECRET_MIN_LEN, + maxsize = ISCSI_CHAP_SECRET_MAX_LEN, + endstring; + + endif; + + suppressif NOT ideqval ISCSI_CONFIG_IFR_NVDATA.AuthenticationType == ISCSI_AUTH_TYPE_CHAP OR + NOT ideqval ISCSI_CONFIG_IFR_NVDATA.CHAPType == ISCSI_CHAP_MUTUAL; + + string varid = ISCSI_CONFIG_IFR_NVDATA.ReverseCHAPName, + prompt = STRING_TOKEN(STR_ISCSI_REVERSE_CHAP_NAME), + help = STRING_TOKEN(STR_ISCSI_REVERSE_CHAP_NAME), + flags = INTERACTIVE, + key = KEY_REVERSE_CHAP_NAME, + minsize = 0, + maxsize = ISCSI_CHAP_NAME_MAX_LEN, + endstring; + + string varid = ISCSI_CONFIG_IFR_NVDATA.ReverseCHAPSecret, + prompt = STRING_TOKEN(STR_ISCSI_REVERSE_CHAP_SECRET), + help = STRING_TOKEN(STR_ISCSI_CHAP_SECRET_HELP), + flags = INTERACTIVE, + key = KEY_REVERSE_CHAP_SECRET, + minsize = ISCSI_CHAP_SECRET_MIN_LEN, + maxsize = ISCSI_CHAP_SECRET_MAX_LEN, + endstring; + + endif; + + suppressif TRUE; + + string varid = ISCSI_CONFIG_IFR_NVDATA.ISCSIMacAddr, + prompt = STRING_TOKEN(STR_ISCSI_MAC_PROMPT), + help = STRING_TOKEN(STR_ISCSI_MAC_PROMPT), + minsize = 0, + maxsize = ISCSI_MAX_MAC_STRING_LEN, + endstring; + + string varid = ISCSI_CONFIG_IFR_NVDATA.ISCSIAttemptOrder, + prompt = STRING_TOKEN(STR_ISCSI_ATTEMPT_ORDER), + help = STRING_TOKEN(STR_ISCSI_ATTEMPT_ORDER), + minsize = 0, + maxsize = ATTEMPT_NAME_LIST_SIZE, + endstring; + + string varid = ISCSI_CONFIG_IFR_NVDATA.ISCSIAddAttemptList, + prompt = STRING_TOKEN(STR_ISCSI_ADD_ATTEMPTS), + help = STRING_TOKEN(STR_ISCSI_ADD_ATTEMPTS), + minsize = 0, + maxsize = ATTEMPT_NAME_LIST_SIZE, + endstring; + + string varid = ISCSI_CONFIG_IFR_NVDATA.ISCSIDeleteAttemptList, + prompt = STRING_TOKEN(STR_ISCSI_DELETE_ATTEMPTS), + help = STRING_TOKEN(STR_ISCSI_DELETE_ATTEMPTS), + minsize = 0, + maxsize = ATTEMPT_NAME_LIST_SIZE, + endstring; + + string varid = ISCSI_CONFIG_IFR_NVDATA.ISCSIDisplayAttemptList, + prompt = STRING_TOKEN(STR_ISCSI_DISPLAY_ATTEMPTS), + help = STRING_TOKEN(STR_ISCSI_DISPLAY_ATTEMPTS), + flags = READ_ONLY, + minsize = 0, + maxsize = ATTEMPT_NAME_LIST_SIZE, + endstring; + + label KEYWORD_ENTRY_LABEL; + label LABEL_END; + endif; + + subtitle text = STRING_TOKEN(STR_NULL); + + text + help = STRING_TOKEN (STR_SAVE_CHANGES_HELP), + text = STRING_TOKEN (STR_SAVE_CHANGES), + flags = INTERACTIVE, + key = KEY_SAVE_ATTEMPT_CONFIG; + + goto FORMID_MAIN_FORM, + prompt = STRING_TOKEN (STR_RETURN_MAIN_FORM), + help = STRING_TOKEN (STR_RETURN_MAIN_FORM), + flags = 0; + + endform; + +endformset; + diff --git a/NetworkPkg/IScsiDxe/IScsiDhcp.c b/NetworkPkg/IScsiDxe/IScsiDhcp.c new file mode 100644 index 000000000..d8c9fff6c --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiDhcp.c @@ -0,0 +1,568 @@ +/** @file + iSCSI DHCP4 related configuration routines. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + + +/** + Extract the Root Path option and get the required target information. + + @param[in] RootPath The RootPath. + @param[in] Length Length of the RootPath option payload. + @param[in, out] ConfigData The iSCSI attempt configuration data read + from a nonvolatile device. + + @retval EFI_SUCCESS All required information is extracted from the RootPath option. + @retval EFI_NOT_FOUND The RootPath is not an iSCSI RootPath. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_INVALID_PARAMETER The RootPath is malformatted. + +**/ +EFI_STATUS +IScsiDhcpExtractRootPath ( + IN CHAR8 *RootPath, + IN UINT8 Length, + IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA *ConfigData + ) +{ + EFI_STATUS Status; + UINT8 IScsiRootPathIdLen; + CHAR8 *TmpStr; + ISCSI_ROOT_PATH_FIELD Fields[RP_FIELD_IDX_MAX]; + ISCSI_ROOT_PATH_FIELD *Field; + UINT32 FieldIndex; + UINT8 Index; + ISCSI_SESSION_CONFIG_NVDATA *ConfigNvData; + EFI_IP_ADDRESS Ip; + UINT8 IpMode; + + ConfigNvData = &ConfigData->SessionConfigData; + + // + // "iscsi:"":"":"":"":" + // + IScsiRootPathIdLen = (UINT8) AsciiStrLen (ISCSI_ROOT_PATH_ID); + + if ((Length <= IScsiRootPathIdLen) || (CompareMem (RootPath, ISCSI_ROOT_PATH_ID, IScsiRootPathIdLen) != 0)) { + return EFI_NOT_FOUND; + } + // + // Skip the iSCSI RootPath ID "iscsi:". + // + RootPath += IScsiRootPathIdLen; + Length = (UINT8) (Length - IScsiRootPathIdLen); + + TmpStr = (CHAR8 *) AllocatePool (Length + 1); + if (TmpStr == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (TmpStr, RootPath, Length); + TmpStr[Length] = '\0'; + + Index = 0; + FieldIndex = RP_FIELD_IDX_SERVERNAME; + ZeroMem (&Fields[0], sizeof (Fields)); + + // + // Extract the fields in the Root Path option string. + // + for (FieldIndex = RP_FIELD_IDX_SERVERNAME; (FieldIndex < RP_FIELD_IDX_MAX) && (Index < Length); FieldIndex++) { + if (TmpStr[Index] != ISCSI_ROOT_PATH_FIELD_DELIMITER) { + Fields[FieldIndex].Str = &TmpStr[Index]; + } + + while ((TmpStr[Index] != ISCSI_ROOT_PATH_FIELD_DELIMITER) && (Index < Length)) { + Index++; + } + + if (TmpStr[Index] == ISCSI_ROOT_PATH_FIELD_DELIMITER) { + if (FieldIndex != RP_FIELD_IDX_TARGETNAME) { + TmpStr[Index] = '\0'; + Index++; + } + + if (Fields[FieldIndex].Str != NULL) { + Fields[FieldIndex].Len = (UINT8) AsciiStrLen (Fields[FieldIndex].Str); + } + } + } + + if (FieldIndex != RP_FIELD_IDX_MAX) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + if ((Fields[RP_FIELD_IDX_SERVERNAME].Str == NULL) || + (Fields[RP_FIELD_IDX_TARGETNAME].Str == NULL) || + (Fields[RP_FIELD_IDX_PROTOCOL].Len > 1) + ) { + + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + // + // Get the IP address of the target. + // + Field = &Fields[RP_FIELD_IDX_SERVERNAME]; + + if (ConfigNvData->IpMode < IP_MODE_AUTOCONFIG) { + IpMode = ConfigNvData->IpMode; + } else { + IpMode = ConfigData->AutoConfigureMode; + } + + // + // Server name is expressed as domain name, just save it. + // + if ((!NET_IS_DIGIT (*(Field->Str))) && (*(Field->Str) != '[')) { + ConfigNvData->DnsMode = TRUE; + if (Field->Len > sizeof (ConfigNvData->TargetUrl)) { + return EFI_INVALID_PARAMETER; + } + CopyMem (&ConfigNvData->TargetUrl, Field->Str, Field->Len); + ConfigNvData->TargetUrl[Field->Len + 1] = '\0'; + } else { + ConfigNvData->DnsMode = FALSE; + ZeroMem(ConfigNvData->TargetUrl, sizeof (ConfigNvData->TargetUrl)); + Status = IScsiAsciiStrToIp (Field->Str, IpMode, &Ip); + CopyMem (&ConfigNvData->TargetIp, &Ip, sizeof (EFI_IP_ADDRESS)); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + // + // Check the protocol type. + // + Field = &Fields[RP_FIELD_IDX_PROTOCOL]; + if ((Field->Str != NULL) && ((*(Field->Str) - '0') != EFI_IP_PROTO_TCP)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + // + // Get the port of the iSCSI target. + // + Field = &Fields[RP_FIELD_IDX_PORT]; + if (Field->Str != NULL) { + ConfigNvData->TargetPort = (UINT16) AsciiStrDecimalToUintn (Field->Str); + } else { + ConfigNvData->TargetPort = ISCSI_WELL_KNOWN_PORT; + } + // + // Get the LUN. + // + Field = &Fields[RP_FIELD_IDX_LUN]; + if (Field->Str != NULL) { + Status = IScsiAsciiStrToLun (Field->Str, ConfigNvData->BootLun); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } else { + ZeroMem (ConfigNvData->BootLun, sizeof (ConfigNvData->BootLun)); + } + // + // Get the target iSCSI Name. + // + Field = &Fields[RP_FIELD_IDX_TARGETNAME]; + + if (AsciiStrLen (Field->Str) > ISCSI_NAME_MAX_SIZE - 1) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + // + // Validate the iSCSI name. + // + Status = IScsiNormalizeName (Field->Str, AsciiStrLen (Field->Str)); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + AsciiStrCpyS (ConfigNvData->TargetName, ISCSI_NAME_MAX_SIZE, Field->Str); + +ON_EXIT: + + FreePool (TmpStr); + + return Status; +} + +/** + The callback function registerd to the DHCP4 instance that is used to select + the qualified DHCP OFFER. + + @param[in] This The DHCP4 protocol. + @param[in] Context The context set when configuring the DHCP4 protocol. + @param[in] CurrentState The current state of the DHCP4 protocol. + @param[in] Dhcp4Event The event occurs in the current state. + @param[in] Packet The DHCP packet that is to be sent or was already received. + @param[out] NewPacket The packet used to replace the above Packet. + + @retval EFI_SUCCESS Either the DHCP OFFER is qualified or we're not intereseted + in the Dhcp4Event. + @retval EFI_NOT_READY The DHCP OFFER packet doesn't match our requirements. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +IScsiDhcpSelectOffer ( + IN EFI_DHCP4_PROTOCOL *This, + IN VOID *Context, + IN EFI_DHCP4_STATE CurrentState, + IN EFI_DHCP4_EVENT Dhcp4Event, + IN EFI_DHCP4_PACKET *Packet, OPTIONAL + OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL + ) +{ + EFI_STATUS Status; + UINT32 OptionCount; + EFI_DHCP4_PACKET_OPTION **OptionList; + UINT32 Index; + + if ((Dhcp4Event != Dhcp4RcvdOffer) && (Dhcp4Event != Dhcp4SelectOffer)) { + return EFI_SUCCESS; + } + + OptionCount = 0; + + Status = This->Parse (This, Packet, &OptionCount, NULL); + if (Status != EFI_BUFFER_TOO_SMALL) { + return EFI_NOT_READY; + } + + OptionList = AllocatePool (OptionCount * sizeof (EFI_DHCP4_PACKET_OPTION *)); + if (OptionList == NULL) { + return EFI_NOT_READY; + } + + Status = This->Parse (This, Packet, &OptionCount, OptionList); + if (EFI_ERROR (Status)) { + FreePool (OptionList); + return EFI_NOT_READY; + } + + for (Index = 0; Index < OptionCount; Index++) { + if (OptionList[Index]->OpCode != DHCP4_TAG_ROOTPATH) { + continue; + } + + Status = IScsiDhcpExtractRootPath ( + (CHAR8 *) &OptionList[Index]->Data[0], + OptionList[Index]->Length, + (ISCSI_ATTEMPT_CONFIG_NVDATA *) Context + ); + + break; + } + + if (Index == OptionCount) { + Status = EFI_NOT_READY; + } + + FreePool (OptionList); + + return Status; +} + +/** + Parse the DHCP ACK to get the address configuration and DNS information. + + @param[in] Dhcp4 The DHCP4 protocol. + @param[in, out] ConfigData The session configuration data. + + @retval EFI_SUCCESS The DNS information is got from the DHCP ACK. + @retval EFI_NO_MAPPING DHCP failed to acquire address and other information. + @retval EFI_INVALID_PARAMETER The DHCP ACK's DNS option is malformatted. + @retval EFI_DEVICE_ERROR Other errors as indicated. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +IScsiParseDhcpAck ( + IN EFI_DHCP4_PROTOCOL *Dhcp4, + IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA *ConfigData + ) +{ + EFI_STATUS Status; + EFI_DHCP4_MODE_DATA Dhcp4ModeData; + UINT32 OptionCount; + EFI_DHCP4_PACKET_OPTION **OptionList; + UINT32 Index; + ISCSI_SESSION_CONFIG_NVDATA *NvData; + + Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4ModeData); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Dhcp4ModeData.State != Dhcp4Bound) { + return EFI_NO_MAPPING; + } + + NvData = &ConfigData->SessionConfigData; + + CopyMem (&NvData->LocalIp, &Dhcp4ModeData.ClientAddress, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&NvData->SubnetMask, &Dhcp4ModeData.SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&NvData->Gateway, &Dhcp4ModeData.RouterAddress, sizeof (EFI_IPv4_ADDRESS)); + + OptionCount = 0; + OptionList = NULL; + + Status = Dhcp4->Parse (Dhcp4, Dhcp4ModeData.ReplyPacket, &OptionCount, OptionList); + if (Status != EFI_BUFFER_TOO_SMALL) { + return EFI_DEVICE_ERROR; + } + + OptionList = AllocatePool (OptionCount * sizeof (EFI_DHCP4_PACKET_OPTION *)); + if (OptionList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = Dhcp4->Parse (Dhcp4, Dhcp4ModeData.ReplyPacket, &OptionCount, OptionList); + if (EFI_ERROR (Status)) { + FreePool (OptionList); + return EFI_DEVICE_ERROR; + } + + for (Index = 0; Index < OptionCount; Index++) { + // + // Get DNS server addresses and DHCP server address from this offer. + // + if (OptionList[Index]->OpCode == DHCP4_TAG_DNS_SERVER) { + + if (((OptionList[Index]->Length & 0x3) != 0) || (OptionList[Index]->Length == 0)) { + Status = EFI_INVALID_PARAMETER; + break; + } + // + // Primary DNS server address. + // + CopyMem (&ConfigData->PrimaryDns, &OptionList[Index]->Data[0], sizeof (EFI_IPv4_ADDRESS)); + + if (OptionList[Index]->Length > 4) { + // + // Secondary DNS server address. + // + CopyMem (&ConfigData->SecondaryDns, &OptionList[Index]->Data[4], sizeof (EFI_IPv4_ADDRESS)); + } + } else if (OptionList[Index]->OpCode == DHCP4_TAG_SERVER_ID) { + if (OptionList[Index]->Length != 4) { + Status = EFI_INVALID_PARAMETER; + break; + } + + CopyMem (&ConfigData->DhcpServer, &OptionList[Index]->Data[0], sizeof (EFI_IPv4_ADDRESS)); + } + } + + FreePool (OptionList); + + return Status; +} + +/** + This function will switch the IP4 configuration policy to Static. + + @param[in] Ip4Config2 Pointer to the IP4 configuration protocol. + + @retval EFI_SUCCESS The policy is already configured to static. + @retval Others Other error as indicated. + +**/ +EFI_STATUS +IScsiSetIp4Policy ( + IN EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2 + ) +{ + EFI_IP4_CONFIG2_POLICY Policy; + EFI_STATUS Status; + UINTN DataSize; + + DataSize = sizeof (EFI_IP4_CONFIG2_POLICY); + Status = Ip4Config2->GetData ( + Ip4Config2, + Ip4Config2DataTypePolicy, + &DataSize, + &Policy + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Policy != Ip4Config2PolicyStatic) { + Policy = Ip4Config2PolicyStatic; + Status= Ip4Config2->SetData ( + Ip4Config2, + Ip4Config2DataTypePolicy, + sizeof (EFI_IP4_CONFIG2_POLICY), + &Policy + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + return EFI_SUCCESS; +} + +/** + Parse the DHCP ACK to get the address configuration and DNS information. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + @param[in, out] ConfigData The attempt configuration data. + + @retval EFI_SUCCESS The DNS information is got from the DHCP ACK. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NO_MEDIA There was a media error. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiDoDhcp ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA *ConfigData + ) +{ + EFI_HANDLE Dhcp4Handle; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + EFI_DHCP4_PROTOCOL *Dhcp4; + EFI_STATUS Status; + EFI_DHCP4_PACKET_OPTION *ParaList; + EFI_DHCP4_CONFIG_DATA Dhcp4ConfigData; + ISCSI_SESSION_CONFIG_NVDATA *NvData; + EFI_STATUS MediaStatus; + + Dhcp4Handle = NULL; + Ip4Config2 = NULL; + Dhcp4 = NULL; + ParaList = NULL; + + // + // Check media status before doing DHCP. + // + MediaStatus = EFI_SUCCESS; + NetLibDetectMediaWaitTimeout (Controller, ISCSI_CHECK_MEDIA_GET_DHCP_WAITING_TIME, &MediaStatus); + if (MediaStatus!= EFI_SUCCESS) { + AsciiPrint ("\n Error: Could not detect network connection.\n"); + return EFI_NO_MEDIA; + } + + // + // DHCP4 service allows only one of its children to be configured in + // the active state, If the DHCP4 D.O.R.A started by IP4 auto + // configuration and has not been completed, the Dhcp4 state machine + // will not be in the right state for the iSCSI to start a new round D.O.R.A. + // So, we need to switch it's policy to static. + // + Status = gBS->HandleProtocol (Controller, &gEfiIp4Config2ProtocolGuid, (VOID **) &Ip4Config2); + if (!EFI_ERROR (Status)) { + Status = IScsiSetIp4Policy (Ip4Config2); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Create a DHCP4 child instance and get the protocol. + // + Status = NetLibCreateServiceChild ( + Controller, + Image, + &gEfiDhcp4ServiceBindingProtocolGuid, + &Dhcp4Handle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + Dhcp4Handle, + &gEfiDhcp4ProtocolGuid, + (VOID **) &Dhcp4, + Image, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + NvData = &ConfigData->SessionConfigData; + + ParaList = AllocatePool (sizeof (EFI_DHCP4_PACKET_OPTION) + 3); + if (ParaList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Ask the server to reply with Netmask, Router, DNS, and RootPath options. + // + ParaList->OpCode = DHCP4_TAG_PARA_LIST; + ParaList->Length = (UINT8) (NvData->TargetInfoFromDhcp ? 4 : 3); + ParaList->Data[0] = DHCP4_TAG_NETMASK; + ParaList->Data[1] = DHCP4_TAG_ROUTER; + ParaList->Data[2] = DHCP4_TAG_DNS_SERVER; + ParaList->Data[3] = DHCP4_TAG_ROOTPATH; + + ZeroMem (&Dhcp4ConfigData, sizeof (EFI_DHCP4_CONFIG_DATA)); + Dhcp4ConfigData.OptionCount = 1; + Dhcp4ConfigData.OptionList = &ParaList; + + if (NvData->TargetInfoFromDhcp) { + // + // Use callback to select an offer that contains target information. + // + Dhcp4ConfigData.Dhcp4Callback = IScsiDhcpSelectOffer; + Dhcp4ConfigData.CallbackContext = ConfigData; + } + + Status = Dhcp4->Configure (Dhcp4, &Dhcp4ConfigData); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = Dhcp4->Start (Dhcp4, NULL); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + // + // Parse the ACK to get required information. + // + Status = IScsiParseDhcpAck (Dhcp4, ConfigData); + +ON_EXIT: + + if (ParaList != NULL) { + FreePool (ParaList); + } + + if (Dhcp4 != NULL) { + Dhcp4->Stop (Dhcp4); + Dhcp4->Configure (Dhcp4, NULL); + + gBS->CloseProtocol ( + Dhcp4Handle, + &gEfiDhcp4ProtocolGuid, + Image, + Controller + ); + } + + NetLibDestroyServiceChild ( + Controller, + Image, + &gEfiDhcp4ServiceBindingProtocolGuid, + Dhcp4Handle + ); + + return Status; +} diff --git a/NetworkPkg/IScsiDxe/IScsiDhcp.h b/NetworkPkg/IScsiDxe/IScsiDhcp.h new file mode 100644 index 000000000..69b501d63 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiDhcp.h @@ -0,0 +1,49 @@ +/** @file + The head file of iSCSI DHCP4 related configuration routines. + +Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ISCSI_DHCP_H_ +#define _ISCSI_DHCP_H_ + +#define ISCSI_ROOT_PATH_ID "iscsi:" +#define ISCSI_ROOT_PATH_FIELD_DELIMITER ':' + +#define RP_FIELD_IDX_SERVERNAME 0 +#define RP_FIELD_IDX_PROTOCOL 1 +#define RP_FIELD_IDX_PORT 2 +#define RP_FIELD_IDX_LUN 3 +#define RP_FIELD_IDX_TARGETNAME 4 +#define RP_FIELD_IDX_MAX 5 + +typedef struct _ISCSI_ATTEMPT_CONFIG_NVDATA ISCSI_ATTEMPT_CONFIG_NVDATA; + +typedef struct _ISCSI_ROOT_PATH_FIELD { + CHAR8 *Str; + UINT8 Len; +} ISCSI_ROOT_PATH_FIELD; + +/** + Parse the DHCP ACK to get the address configuration and DNS information. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + @param[in, out] ConfigData The attempt configuration data. + + @retval EFI_SUCCESS The DNS information is got from the DHCP ACK. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NO_MEDIA There was a media error. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiDoDhcp ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA *ConfigData + ); + +#endif diff --git a/NetworkPkg/IScsiDxe/IScsiDhcp6.c b/NetworkPkg/IScsiDxe/IScsiDhcp6.c new file mode 100644 index 000000000..86a872ade --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiDhcp6.c @@ -0,0 +1,545 @@ +/** @file + iSCSI DHCP6 related configuration routines. + +Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + + +/** + Extract the Root Path option and get the required target information from + Boot File Uniform Resource Locator (URL) Option. + + @param[in] RootPath The RootPath string. + @param[in] Length Length of the RootPath option payload. + @param[in, out] ConfigData The iSCSI session configuration data read from + nonvolatile device. + + @retval EFI_SUCCESS All required information is extracted from the + RootPath option. + @retval EFI_NOT_FOUND The RootPath is not an iSCSI RootPath. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_INVALID_PARAMETER The RootPath is malformatted. + +**/ +EFI_STATUS +IScsiDhcp6ExtractRootPath ( + IN CHAR8 *RootPath, + IN UINT16 Length, + IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA *ConfigData + ) +{ + EFI_STATUS Status; + UINT16 IScsiRootPathIdLen; + CHAR8 *TmpStr; + ISCSI_ROOT_PATH_FIELD Fields[RP_FIELD_IDX_MAX]; + ISCSI_ROOT_PATH_FIELD *Field; + UINT32 FieldIndex; + UINT8 Index; + ISCSI_SESSION_CONFIG_NVDATA *ConfigNvData; + EFI_IP_ADDRESS Ip; + UINT8 IpMode; + + ConfigNvData = &ConfigData->SessionConfigData; + ConfigNvData->DnsMode = FALSE; + // + // "iscsi:"":"":"":"":" + // + IScsiRootPathIdLen = (UINT16) AsciiStrLen (ISCSI_ROOT_PATH_ID); + + if ((Length <= IScsiRootPathIdLen) || + (CompareMem (RootPath, ISCSI_ROOT_PATH_ID, IScsiRootPathIdLen) != 0)) { + return EFI_NOT_FOUND; + } + // + // Skip the iSCSI RootPath ID "iscsi:". + // + RootPath = RootPath + IScsiRootPathIdLen; + Length = (UINT16) (Length - IScsiRootPathIdLen); + + TmpStr = (CHAR8 *) AllocatePool (Length + 1); + if (TmpStr == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (TmpStr, RootPath, Length); + TmpStr[Length] = '\0'; + + Index = 0; + FieldIndex = 0; + ZeroMem (&Fields[0], sizeof (Fields)); + + // + // Extract SERVERNAME field in the Root Path option. + // + if (TmpStr[Index] != ISCSI_ROOT_PATH_ADDR_START_DELIMITER) { + // + // The servername is expressed as domain name. + // + ConfigNvData->DnsMode = TRUE; + } else { + Index++; + } + + Fields[RP_FIELD_IDX_SERVERNAME].Str = &TmpStr[Index]; + + if (!ConfigNvData->DnsMode) { + while ((TmpStr[Index] != ISCSI_ROOT_PATH_ADDR_END_DELIMITER)&& (Index < Length)) { + Index++; + } + + // + // Skip ']' and ':'. + // + TmpStr[Index] = '\0'; + Index += 2; + } else { + while ((TmpStr[Index] != ISCSI_ROOT_PATH_FIELD_DELIMITER) && (Index < Length)) { + Index++; + } + // + // Skip ':'. + // + TmpStr[Index] = '\0'; + Index += 1; + } + + Fields[RP_FIELD_IDX_SERVERNAME].Len = (UINT8) AsciiStrLen (Fields[RP_FIELD_IDX_SERVERNAME].Str); + + // + // Extract others fields in the Root Path option string. + // + for (FieldIndex = 1; (FieldIndex < RP_FIELD_IDX_MAX) && (Index < Length); FieldIndex++) { + + if (TmpStr[Index] != ISCSI_ROOT_PATH_FIELD_DELIMITER) { + Fields[FieldIndex].Str = &TmpStr[Index]; + } + + while ((TmpStr[Index] != ISCSI_ROOT_PATH_FIELD_DELIMITER) && (Index < Length)) { + Index++; + } + + if (TmpStr[Index] == ISCSI_ROOT_PATH_FIELD_DELIMITER) { + if (FieldIndex != RP_FIELD_IDX_TARGETNAME) { + TmpStr[Index] = '\0'; + Index++; + } + + if (Fields[FieldIndex].Str != NULL) { + Fields[FieldIndex].Len = (UINT8) AsciiStrLen (Fields[FieldIndex].Str); + } + } + } + + if (FieldIndex != RP_FIELD_IDX_MAX) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + if ((Fields[RP_FIELD_IDX_SERVERNAME].Str == NULL) || + (Fields[RP_FIELD_IDX_TARGETNAME].Str == NULL) || + (Fields[RP_FIELD_IDX_PROTOCOL].Len > 1) + ) { + + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + // + // Get the IP address of the target. + // + Field = &Fields[RP_FIELD_IDX_SERVERNAME]; + if (ConfigNvData->IpMode < IP_MODE_AUTOCONFIG) { + IpMode = ConfigNvData->IpMode; + } else { + IpMode = ConfigData->AutoConfigureMode; + } + + // + // Server name is expressed as domain name, just save it. + // + if (ConfigNvData->DnsMode) { + if (Field->Len > sizeof (ConfigNvData->TargetUrl)) { + return EFI_INVALID_PARAMETER; + } + CopyMem (&ConfigNvData->TargetUrl, Field->Str, Field->Len); + ConfigNvData->TargetUrl[Field->Len + 1] = '\0'; + } else { + ZeroMem(&ConfigNvData->TargetUrl, sizeof (ConfigNvData->TargetUrl)); + Status = IScsiAsciiStrToIp (Field->Str, IpMode, &Ip); + CopyMem (&ConfigNvData->TargetIp, &Ip, sizeof (EFI_IP_ADDRESS)); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + // + // Check the protocol type. + // + Field = &Fields[RP_FIELD_IDX_PROTOCOL]; + if ((Field->Str != NULL) && ((*(Field->Str) - '0') != EFI_IP_PROTO_TCP)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + // + // Get the port of the iSCSI target. + // + Field = &Fields[RP_FIELD_IDX_PORT]; + if (Field->Str != NULL) { + ConfigNvData->TargetPort = (UINT16) AsciiStrDecimalToUintn (Field->Str); + } else { + ConfigNvData->TargetPort = ISCSI_WELL_KNOWN_PORT; + } + // + // Get the LUN. + // + Field = &Fields[RP_FIELD_IDX_LUN]; + if (Field->Str != NULL) { + Status = IScsiAsciiStrToLun (Field->Str, ConfigNvData->BootLun); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } else { + ZeroMem (ConfigNvData->BootLun, sizeof (ConfigNvData->BootLun)); + } + // + // Get the target iSCSI Name. + // + Field = &Fields[RP_FIELD_IDX_TARGETNAME]; + + if (AsciiStrLen (Field->Str) > ISCSI_NAME_MAX_SIZE - 1) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + // + // Validate the iSCSI name. + // + Status = IScsiNormalizeName (Field->Str, AsciiStrLen (Field->Str)); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + AsciiStrCpyS (ConfigNvData->TargetName, ISCSI_NAME_MAX_SIZE, Field->Str); + +ON_EXIT: + + FreePool (TmpStr); + + return Status; +} + +/** + EFI_DHCP6_INFO_CALLBACK is provided by the consumer of the EFI DHCPv6 Protocol + instance to intercept events that occurs in the DHCPv6 Information Request + exchange process. + + @param[in] This Pointer to the EFI_DHCP6_PROTOCOL instance that + is used to configure this callback function. + @param[in] Context Pointer to the context that is initialized in + the EFI_DHCP6_PROTOCOL.InfoRequest(). + @param[in] Packet Pointer to Reply packet that has been received. + The EFI DHCPv6 Protocol instance is responsible + for freeing the buffer. + + @retval EFI_SUCCESS Tell the EFI DHCPv6 Protocol instance to finish + Information Request exchange process. + @retval EFI_NOT_READY Tell the EFI DHCPv6 Protocol instance to continue + Information Request exchange process. + @retval EFI_ABORTED Tell the EFI DHCPv6 Protocol instance to abort + the Information Request exchange process. + @retval EFI_UNSUPPORTED Tell the EFI DHCPv6 Protocol instance to finish + the Information Request exchange process because some + request information are not received. + +**/ +EFI_STATUS +EFIAPI +IScsiDhcp6ParseReply ( + IN EFI_DHCP6_PROTOCOL *This, + IN VOID *Context, + IN EFI_DHCP6_PACKET *Packet + ) +{ + EFI_STATUS Status; + UINT32 Index; + UINT32 OptionCount; + EFI_DHCP6_PACKET_OPTION *BootFileOpt; + EFI_DHCP6_PACKET_OPTION **OptionList; + ISCSI_ATTEMPT_CONFIG_NVDATA *ConfigData; + UINT16 ParaLen; + + OptionCount = 0; + BootFileOpt = NULL; + + Status = This->Parse (This, Packet, &OptionCount, NULL); + if (Status != EFI_BUFFER_TOO_SMALL) { + return EFI_NOT_READY; + } + + OptionList = AllocateZeroPool (OptionCount * sizeof (EFI_DHCP6_PACKET_OPTION *)); + if (OptionList == NULL) { + return EFI_NOT_READY; + } + + Status = This->Parse (This, Packet, &OptionCount, OptionList); + if (EFI_ERROR (Status)) { + Status = EFI_NOT_READY; + goto Exit; + } + + ConfigData = (ISCSI_ATTEMPT_CONFIG_NVDATA *) Context; + + for (Index = 0; Index < OptionCount; Index++) { + OptionList[Index]->OpCode = NTOHS (OptionList[Index]->OpCode); + OptionList[Index]->OpLen = NTOHS (OptionList[Index]->OpLen); + + // + // Get DNS server addresses from this reply packet. + // + if (OptionList[Index]->OpCode == DHCP6_OPT_DNS_SERVERS) { + + if (((OptionList[Index]->OpLen & 0xf) != 0) || (OptionList[Index]->OpLen == 0)) { + Status = EFI_UNSUPPORTED; + goto Exit; + } + // + // Primary DNS server address. + // + CopyMem (&ConfigData->PrimaryDns, &OptionList[Index]->Data[0], sizeof (EFI_IPv6_ADDRESS)); + + if (OptionList[Index]->OpLen > 16) { + // + // Secondary DNS server address + // + CopyMem (&ConfigData->SecondaryDns, &OptionList[Index]->Data[16], sizeof (EFI_IPv6_ADDRESS)); + } + + } else if (OptionList[Index]->OpCode == DHCP6_OPT_BOOT_FILE_URL) { + // + // The server sends this option to inform the client about an URL to a boot file. + // + BootFileOpt = OptionList[Index]; + } else if (OptionList[Index]->OpCode == DHCP6_OPT_BOOT_FILE_PARAM) { + // + // The server sends this option to inform the client about DHCP6 server address. + // + if (OptionList[Index]->OpLen < 18) { + Status = EFI_UNSUPPORTED; + goto Exit; + } + // + // Check param-len 1, should be 16 bytes. + // + CopyMem (&ParaLen, &OptionList[Index]->Data[0], sizeof (UINT16)); + if (NTOHS (ParaLen) != 16) { + Status = EFI_UNSUPPORTED; + goto Exit; + } + + CopyMem (&ConfigData->DhcpServer, &OptionList[Index]->Data[2], sizeof (EFI_IPv6_ADDRESS)); + } + } + + if (BootFileOpt == NULL) { + Status = EFI_UNSUPPORTED; + goto Exit; + } + + // + // Get iSCSI root path from Boot File Uniform Resource Locator (URL) Option + // + Status = IScsiDhcp6ExtractRootPath ( + (CHAR8 *) BootFileOpt->Data, + BootFileOpt->OpLen, + ConfigData + ); + +Exit: + + FreePool (OptionList); + return Status; +} + + +/** + Parse the DHCP ACK to get the address configuration and DNS information. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller; + @param[in, out] ConfigData The attempt configuration data. + + @retval EFI_SUCCESS The DNS information is got from the DHCP ACK. + @retval EFI_NO_MAPPING DHCP failed to acquire address and other + information. + @retval EFI_INVALID_PARAMETER The DHCP ACK's DNS option is malformatted. + @retval EFI_DEVICE_ERROR Some unexpected error occurred. + @retval EFI_OUT_OF_RESOURCES There is no sufficient resource to finish the + operation. + @retval EFI_NO_MEDIA There was a media error. + +**/ +EFI_STATUS +IScsiDoDhcp6 ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA *ConfigData + ) +{ + EFI_HANDLE Dhcp6Handle; + EFI_DHCP6_PROTOCOL *Dhcp6; + EFI_STATUS Status; + EFI_STATUS TimerStatus; + EFI_DHCP6_PACKET_OPTION *Oro; + EFI_DHCP6_RETRANSMISSION InfoReqReXmit; + EFI_EVENT Timer; + EFI_STATUS MediaStatus; + + // + // Check media status before doing DHCP. + // + MediaStatus = EFI_SUCCESS; + NetLibDetectMediaWaitTimeout (Controller, ISCSI_CHECK_MEDIA_GET_DHCP_WAITING_TIME, &MediaStatus); + if (MediaStatus != EFI_SUCCESS) { + AsciiPrint ("\n Error: Could not detect network connection.\n"); + return EFI_NO_MEDIA; + } + + // + // iSCSI will only request target info from DHCPv6 server. + // + if (!ConfigData->SessionConfigData.TargetInfoFromDhcp) { + return EFI_SUCCESS; + } + + Dhcp6Handle = NULL; + Dhcp6 = NULL; + Oro = NULL; + Timer = NULL; + + // + // Create a DHCP6 child instance and get the protocol. + // + Status = NetLibCreateServiceChild ( + Controller, + Image, + &gEfiDhcp6ServiceBindingProtocolGuid, + &Dhcp6Handle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + Dhcp6Handle, + &gEfiDhcp6ProtocolGuid, + (VOID **) &Dhcp6, + Image, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Oro = AllocateZeroPool (sizeof (EFI_DHCP6_PACKET_OPTION) + 5); + if (Oro == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Ask the server to reply with DNS and Boot File URL options by info request. + // All members in EFI_DHCP6_PACKET_OPTION are in network order. + // + Oro->OpCode = HTONS (DHCP6_OPT_ORO); + Oro->OpLen = HTONS (2 * 3); + Oro->Data[1] = DHCP6_OPT_DNS_SERVERS; + Oro->Data[3] = DHCP6_OPT_BOOT_FILE_URL; + Oro->Data[5] = DHCP6_OPT_BOOT_FILE_PARAM; + + InfoReqReXmit.Irt = 4; + InfoReqReXmit.Mrc = 1; + InfoReqReXmit.Mrt = 10; + InfoReqReXmit.Mrd = 30; + + Status = Dhcp6->InfoRequest ( + Dhcp6, + TRUE, + Oro, + 0, + NULL, + &InfoReqReXmit, + NULL, + IScsiDhcp6ParseReply, + ConfigData + ); + if (Status == EFI_NO_MAPPING) { + Status = gBS->CreateEvent (EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Timer); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = gBS->SetTimer ( + Timer, + TimerRelative, + ISCSI_GET_MAPPING_TIMEOUT + ); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + do { + + TimerStatus = gBS->CheckEvent (Timer); + + if (!EFI_ERROR (TimerStatus)) { + Status = Dhcp6->InfoRequest ( + Dhcp6, + TRUE, + Oro, + 0, + NULL, + &InfoReqReXmit, + NULL, + IScsiDhcp6ParseReply, + ConfigData + ); + } + + } while (TimerStatus == EFI_NOT_READY); + + } + +ON_EXIT: + + if (Oro != NULL) { + FreePool (Oro); + } + + if (Timer != NULL) { + gBS->CloseEvent (Timer); + } + + if (Dhcp6 != NULL) { + gBS->CloseProtocol ( + Dhcp6Handle, + &gEfiDhcp6ProtocolGuid, + Image, + Controller + ); + } + + NetLibDestroyServiceChild ( + Controller, + Image, + &gEfiDhcp6ServiceBindingProtocolGuid, + Dhcp6Handle + ); + + return Status; +} + diff --git a/NetworkPkg/IScsiDxe/IScsiDhcp6.h b/NetworkPkg/IScsiDxe/IScsiDhcp6.h new file mode 100644 index 000000000..666338c7d --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiDhcp6.h @@ -0,0 +1,65 @@ +/** @file + The header file of iSCSI DHCP6 related configuration routines. + +Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ISCSI_DHCP6_H_ +#define _ISCSI_DHCP6_H_ + +#define ISCSI_ROOT_PATH_ID "iscsi:" +#define ISCSI_ROOT_PATH_FIELD_DELIMITER ':' +#define ISCSI_ROOT_PATH_ADDR_START_DELIMITER '[' +#define ISCSI_ROOT_PATH_ADDR_END_DELIMITER ']' + + +/** + Extract the Root Path option and get the required target information from + Boot File Uniform Resource Locator (URL) Option. + + @param[in] RootPath The RootPath string. + @param[in] Length Length of the RootPath option payload. + @param[in, out] ConfigData The iSCSI session configuration data read from + nonvolatile device. + + @retval EFI_SUCCESS All required information is extracted from the + RootPath option. + @retval EFI_NOT_FOUND The RootPath is not an iSCSI RootPath. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_INVALID_PARAMETER The RootPath is malformatted. + +**/ +EFI_STATUS +IScsiDhcp6ExtractRootPath ( + IN CHAR8 *RootPath, + IN UINT16 Length, + IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA *ConfigData + ); + +/** + Parse the DHCP ACK to get the address configuration and DNS information. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller; + @param[in, out] ConfigData The attempt configuration data. + + @retval EFI_SUCCESS The DNS information is got from the DHCP ACK. + @retval EFI_NO_MAPPING DHCP failed to acquire address and other + information. + @retval EFI_INVALID_PARAMETER The DHCP ACK's DNS option is malformatted. + @retval EFI_DEVICE_ERROR Some unexpected error happened. + @retval EFI_OUT_OF_RESOURCES There is no sufficient resource to finish the + operation. + @retval EFI_NO_MEDIA There was a media error. + +**/ +EFI_STATUS +IScsiDoDhcp6 ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA *ConfigData + ); + +#endif diff --git a/NetworkPkg/IScsiDxe/IScsiDns.c b/NetworkPkg/IScsiDxe/IScsiDns.c new file mode 100644 index 000000000..f407c2bbc --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiDns.c @@ -0,0 +1,429 @@ +/** @file + Perform DNS resolution based on UEFI DNS protocols. + +Copyright (c) 2017, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + +/** + Notify the callback function when an event is triggered. + + @param[in] Event The triggered event. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +IScsiCommonNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + *((BOOLEAN *) Context) = TRUE; +} + +/** + Retrieve the host address using the EFI_DNS4_PROTOCOL. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + @param[in, out] NvData The Session config data structure. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiDns4 ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN OUT ISCSI_SESSION_CONFIG_NVDATA *NvData + ) +{ + EFI_STATUS Status; + EFI_DNS4_PROTOCOL *Dns4; + EFI_DNS4_CONFIG_DATA Dns4CfgData; + EFI_DNS4_COMPLETION_TOKEN Token; + BOOLEAN IsDone; + EFI_HANDLE Dns4Handle; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + EFI_IPv4_ADDRESS *DnsServerList; + UINTN DnsServerListCount; + UINTN DataSize; + CHAR16 *HostName; + + DnsServerList = NULL; + DnsServerListCount = 0; + Dns4Handle = NULL; + Dns4 = NULL; + ZeroMem (&Token, sizeof (EFI_DNS4_COMPLETION_TOKEN)); + + // + // Get DNS server list from EFI IPv4 Configuration II protocol. + // + Status = gBS->HandleProtocol (Controller, &gEfiIp4Config2ProtocolGuid, (VOID **) &Ip4Config2); + if (!EFI_ERROR (Status)) { + // + // Get the required size. + // + DataSize = 0; + Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypeDnsServer, &DataSize, NULL); + if (Status == EFI_BUFFER_TOO_SMALL) { + DnsServerList = AllocatePool (DataSize); + if (DnsServerList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypeDnsServer, &DataSize, DnsServerList); + if (EFI_ERROR (Status)) { + FreePool (DnsServerList); + DnsServerList = NULL; + } else { + DnsServerListCount = DataSize / sizeof (EFI_IPv4_ADDRESS); + } + } + } + + + // + // Create a DNS child instance and get the protocol. + // + Status = NetLibCreateServiceChild ( + Controller, + Image, + &gEfiDns4ServiceBindingProtocolGuid, + &Dns4Handle + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status = gBS->OpenProtocol ( + Dns4Handle, + &gEfiDns4ProtocolGuid, + (VOID **) &Dns4, + Image, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Configure DNS4 instance for the DNS server address and protocol. + // + ZeroMem (&Dns4CfgData, sizeof (Dns4CfgData)); + Dns4CfgData.DnsServerListCount = DnsServerListCount; + Dns4CfgData.DnsServerList = DnsServerList; + Dns4CfgData.EnableDnsCache = TRUE; + IP4_COPY_ADDRESS (&Dns4CfgData.StationIp, &NvData->LocalIp); + IP4_COPY_ADDRESS (&Dns4CfgData.SubnetMask, &NvData->SubnetMask); + Dns4CfgData.Protocol = EFI_IP_PROTO_UDP; + Status = Dns4->Configure ( + Dns4, + &Dns4CfgData + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Create event to set the is done flag when name resolution is finished. + // + ZeroMem (&Token, sizeof (Token)); + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + IScsiCommonNotify, + &IsDone, + &Token.Event + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Start asynchronous name resolution. + // + Token.Status = EFI_NOT_READY; + IsDone = FALSE; + + HostName = (CHAR16 *) AllocateZeroPool (ISCSI_NAME_MAX_SIZE); + if (HostName == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + AsciiStrToUnicodeStrS ( + NvData->TargetUrl, + HostName, + ISCSI_NAME_MAX_SIZE + ); + + Status = Dns4->HostNameToIp (Dns4, HostName, &Token); + if (EFI_ERROR (Status)) { + goto Exit; + } + + while (!IsDone) { + Dns4->Poll (Dns4); + } + + // + // Name resolution is done, check result. + // + Status = Token.Status; + if (!EFI_ERROR (Status)) { + if (Token.RspData.H2AData == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + if (Token.RspData.H2AData->IpCount == 0 || Token.RspData.H2AData->IpList == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + // + // We just return the first IP address from DNS protocol. + // + IP4_COPY_ADDRESS (&NvData->TargetIp.v4, Token.RspData.H2AData->IpList); + Status = EFI_SUCCESS; + } + +Exit: + + if (Token.Event != NULL) { + gBS->CloseEvent (Token.Event); + } + if (Token.RspData.H2AData != NULL) { + if (Token.RspData.H2AData->IpList != NULL) { + FreePool (Token.RspData.H2AData->IpList); + } + FreePool (Token.RspData.H2AData); + } + + if (Dns4 != NULL) { + Dns4->Configure (Dns4, NULL); + + gBS->CloseProtocol ( + Dns4Handle, + &gEfiDns4ProtocolGuid, + Image, + Controller + ); + } + + if (Dns4Handle != NULL) { + NetLibDestroyServiceChild ( + Controller, + Image, + &gEfiDns4ServiceBindingProtocolGuid, + Dns4Handle + ); + } + + return Status; +} + +/** + Retrieve the host address using the EFI_DNS6_PROTOCOL. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + @param[in, out] NvData The Session config data structure. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiDns6 ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN OUT ISCSI_SESSION_CONFIG_NVDATA *NvData + ) +{ + EFI_STATUS Status; + EFI_DNS6_PROTOCOL *Dns6; + EFI_DNS6_CONFIG_DATA Dns6ConfigData; + EFI_DNS6_COMPLETION_TOKEN Token; + EFI_HANDLE Dns6Handle; + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + EFI_IPv6_ADDRESS *DnsServerList; + UINTN DnsServerListCount; + UINTN DataSize; + BOOLEAN IsDone; + CHAR16 *HostName; + + DnsServerList = NULL; + DnsServerListCount = 0; + Dns6 = NULL; + Dns6Handle = NULL; + ZeroMem (&Token, sizeof (EFI_DNS6_COMPLETION_TOKEN)); + + // + // Get DNS server list from EFI IPv6 Configuration protocol. + // + Status = gBS->HandleProtocol (Controller, &gEfiIp6ConfigProtocolGuid, (VOID **) &Ip6Config); + if (!EFI_ERROR (Status)) { + // + // Get the required size. + // + DataSize = 0; + Status = Ip6Config->GetData (Ip6Config, Ip6ConfigDataTypeDnsServer, &DataSize, NULL); + if (Status == EFI_BUFFER_TOO_SMALL) { + DnsServerList = AllocatePool (DataSize); + if (DnsServerList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = Ip6Config->GetData (Ip6Config, Ip6ConfigDataTypeDnsServer, &DataSize, DnsServerList); + if (EFI_ERROR (Status)) { + FreePool (DnsServerList); + DnsServerList = NULL; + } else { + DnsServerListCount = DataSize / sizeof (EFI_IPv6_ADDRESS); + } + } + } + + // + // Create a DNSv6 child instance and get the protocol. + // + Status = NetLibCreateServiceChild ( + Controller, + Image, + &gEfiDns6ServiceBindingProtocolGuid, + &Dns6Handle + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status = gBS->OpenProtocol ( + Dns6Handle, + &gEfiDns6ProtocolGuid, + (VOID **) &Dns6, + Image, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Configure DNS6 instance for the DNS server address and protocol. + // + ZeroMem (&Dns6ConfigData, sizeof (EFI_DNS6_CONFIG_DATA)); + Dns6ConfigData.DnsServerCount = (UINT32)DnsServerListCount; + Dns6ConfigData.DnsServerList = DnsServerList; + Dns6ConfigData.EnableDnsCache = TRUE; + Dns6ConfigData.Protocol = EFI_IP_PROTO_UDP; + Status = Dns6->Configure ( + Dns6, + &Dns6ConfigData + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Token.Status = EFI_NOT_READY; + IsDone = FALSE; + // + // Create event to set the IsDone flag when name resolution is finished. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + IScsiCommonNotify, + &IsDone, + &Token.Event + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Start asynchronous name resolution. + // + HostName = (CHAR16 *) AllocateZeroPool (ISCSI_NAME_MAX_SIZE); + if (HostName == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + AsciiStrToUnicodeStrS ( + NvData->TargetUrl, + HostName, + ISCSI_NAME_MAX_SIZE + ); + Status = Dns6->HostNameToIp (Dns6, HostName, &Token); + if (EFI_ERROR (Status)) { + goto Exit; + } + + while (!IsDone) { + Dns6->Poll (Dns6); + } + + // + // Name resolution is done, check result. + // + Status = Token.Status; + if (!EFI_ERROR (Status)) { + if (Token.RspData.H2AData == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + if (Token.RspData.H2AData->IpCount == 0 || Token.RspData.H2AData->IpList == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + // + // We just return the first IPv6 address from DNS protocol. + // + IP6_COPY_ADDRESS (&NvData->TargetIp.v6, Token.RspData.H2AData->IpList); + Status = EFI_SUCCESS; + } + +Exit: + + if (Token.Event != NULL) { + gBS->CloseEvent (Token.Event); + } + if (Token.RspData.H2AData != NULL) { + if (Token.RspData.H2AData->IpList != NULL) { + FreePool (Token.RspData.H2AData->IpList); + } + FreePool (Token.RspData.H2AData); + } + + if (Dns6 != NULL) { + Dns6->Configure (Dns6, NULL); + + gBS->CloseProtocol ( + Dns6Handle, + &gEfiDns6ProtocolGuid, + Image, + Controller + ); + } + + if (Dns6Handle != NULL) { + NetLibDestroyServiceChild ( + Controller, + Image, + &gEfiDns6ServiceBindingProtocolGuid, + Dns6Handle + ); + } + + return Status; +} + diff --git a/NetworkPkg/IScsiDxe/IScsiDns.h b/NetworkPkg/IScsiDxe/IScsiDns.h new file mode 100644 index 000000000..2e0230974 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiDns.h @@ -0,0 +1,53 @@ +/** @file + The header file of routines for IScsi driver to perform DNS + resolution based on UEFI DNS protocols. + +Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ISCSI_DNS_H_ +#define _ISCSI_DNS_H_ + +/** + Retrieve the host address using the EFI_DNS4_PROTOCOL. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + @param[in, out] NvData The Session config data structure. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiDns4 ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN OUT ISCSI_SESSION_CONFIG_NVDATA *NvData + ); + +/** + Retrieve the host address using the EFI_DNS6_PROTOCOL. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + @param[in, out] NvData The Session config data structure. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiDns6 ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN OUT ISCSI_SESSION_CONFIG_NVDATA *NvData + ); + +#endif diff --git a/NetworkPkg/IScsiDxe/IScsiDriver.c b/NetworkPkg/IScsiDxe/IScsiDriver.c new file mode 100644 index 000000000..94c9b228c --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiDriver.c @@ -0,0 +1,1874 @@ +/** @file + The entry point of IScsi driver. + +Copyright (c) 2019, NVIDIA Corporation. All rights reserved. +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2017 Hewlett Packard Enterprise Development LP
+ +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + +EFI_DRIVER_BINDING_PROTOCOL gIScsiIp4DriverBinding = { + IScsiIp4DriverBindingSupported, + IScsiIp4DriverBindingStart, + IScsiIp4DriverBindingStop, + 0xa, + NULL, + NULL +}; + +EFI_DRIVER_BINDING_PROTOCOL gIScsiIp6DriverBinding = { + IScsiIp6DriverBindingSupported, + IScsiIp6DriverBindingStart, + IScsiIp6DriverBindingStop, + 0xa, + NULL, + NULL +}; + +EFI_GUID gIScsiV4PrivateGuid = ISCSI_V4_PRIVATE_GUID; +EFI_GUID gIScsiV6PrivateGuid = ISCSI_V6_PRIVATE_GUID; +ISCSI_PRIVATE_DATA *mPrivate = NULL; + +/** + Tests to see if this driver supports the RemainingDevicePath. + + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The RemainingDevicePath is supported or NULL. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +IScsiIsDevicePathSupported ( + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_DEVICE_PATH_PROTOCOL *CurrentDevicePath; + + CurrentDevicePath = RemainingDevicePath; + if (CurrentDevicePath != NULL) { + while (!IsDevicePathEnd (CurrentDevicePath)) { + if ((CurrentDevicePath->Type == MESSAGING_DEVICE_PATH) && (CurrentDevicePath->SubType == MSG_ISCSI_DP)) { + return EFI_SUCCESS; + } + + CurrentDevicePath = NextDevicePathNode (CurrentDevicePath); + } + + return EFI_UNSUPPORTED; + } + + return EFI_SUCCESS; +} + +/** + Check whether an iSCSI HBA adapter already installs an AIP instance with + network boot policy matching the value specified in PcdIScsiAIPNetworkBootPolicy. + If yes, return EFI_SUCCESS. + + @retval EFI_SUCCESS Found an AIP with matching network boot policy. + @retval EFI_NOT_FOUND AIP is unavailable or the network boot policy + not matched. +**/ +EFI_STATUS +IScsiCheckAip ( + VOID + ) +{ + UINTN AipHandleCount; + EFI_HANDLE *AipHandleBuffer; + UINTN AipIndex; + EFI_ADAPTER_INFORMATION_PROTOCOL *Aip; + EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtScsiPassThru; + EFI_GUID *InfoTypesBuffer; + UINTN InfoTypeBufferCount; + UINTN TypeIndex; + VOID *InfoBlock; + UINTN InfoBlockSize; + BOOLEAN Supported; + EFI_ADAPTER_INFO_NETWORK_BOOT *NetworkBoot; + EFI_STATUS Status; + UINT8 NetworkBootPolicy; + + // + // Check any AIP instances exist in system. + // + AipHandleCount = 0; + AipHandleBuffer = NULL; + Status = gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiAdapterInformationProtocolGuid, + NULL, + &AipHandleCount, + &AipHandleBuffer + ); + if (EFI_ERROR (Status) || AipHandleCount == 0) { + return EFI_NOT_FOUND; + } + + ASSERT (AipHandleBuffer != NULL); + + InfoBlock = NULL; + + for (AipIndex = 0; AipIndex < AipHandleCount; AipIndex++) { + Status = gBS->HandleProtocol ( + AipHandleBuffer[AipIndex], + &gEfiAdapterInformationProtocolGuid, + (VOID *) &Aip + ); + ASSERT_EFI_ERROR (Status); + ASSERT (Aip != NULL); + + Status = gBS->HandleProtocol ( + AipHandleBuffer[AipIndex], + &gEfiExtScsiPassThruProtocolGuid, + (VOID *) &ExtScsiPassThru + ); + if (EFI_ERROR (Status) || ExtScsiPassThru == NULL) { + continue; + } + + InfoTypesBuffer = NULL; + InfoTypeBufferCount = 0; + Status = Aip->GetSupportedTypes (Aip, &InfoTypesBuffer, &InfoTypeBufferCount); + if (EFI_ERROR (Status) || InfoTypesBuffer == NULL) { + continue; + } + // + // Check whether the AIP instance has Network boot information block. + // + Supported = FALSE; + for (TypeIndex = 0; TypeIndex < InfoTypeBufferCount; TypeIndex++) { + if (CompareGuid (&InfoTypesBuffer[TypeIndex], &gEfiAdapterInfoNetworkBootGuid)) { + Supported = TRUE; + break; + } + } + + FreePool (InfoTypesBuffer); + if (!Supported) { + continue; + } + + // + // We now have network boot information block. + // + InfoBlock = NULL; + InfoBlockSize = 0; + Status = Aip->GetInformation (Aip, &gEfiAdapterInfoNetworkBootGuid, &InfoBlock, &InfoBlockSize); + if (EFI_ERROR (Status) || InfoBlock == NULL) { + continue; + } + + // + // Check whether the network boot policy matches. + // + NetworkBoot = (EFI_ADAPTER_INFO_NETWORK_BOOT *) InfoBlock; + NetworkBootPolicy = PcdGet8 (PcdIScsiAIPNetworkBootPolicy); + + if (NetworkBootPolicy == STOP_UEFI_ISCSI_IF_HBA_INSTALL_AIP) { + Status = EFI_SUCCESS; + goto Exit; + } + if (((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_IP4) != 0 && + !NetworkBoot->iScsiIpv4BootCapablity) || + ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_IP6) != 0 && + !NetworkBoot->iScsiIpv6BootCapablity) || + ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_OFFLOAD) != 0 && + !NetworkBoot->OffloadCapability) || + ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_MPIO) != 0 && + !NetworkBoot->iScsiMpioCapability) || + ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_CONFIGURED_IP4) != 0 && + !NetworkBoot->iScsiIpv4Boot) || + ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_CONFIGURED_IP6) != 0 && + !NetworkBoot->iScsiIpv6Boot)) { + FreePool (InfoBlock); + continue; + } + + Status = EFI_SUCCESS; + goto Exit; + } + + Status = EFI_NOT_FOUND; + +Exit: + if (InfoBlock != NULL) { + FreePool (InfoBlock); + } + if (AipHandleBuffer != NULL) { + FreePool (AipHandleBuffer); + } + return Status; +} + +/** + Tests to see if this driver supports a given controller. This is the worker function for + IScsiIp4(6)DriverBindingSupported. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +IScsiSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL, + IN UINT8 IpVersion + ) +{ + EFI_STATUS Status; + EFI_GUID *IScsiServiceBindingGuid; + EFI_GUID *TcpServiceBindingGuid; + EFI_GUID *DhcpServiceBindingGuid; + EFI_GUID *DnsServiceBindingGuid; + + if (IpVersion == IP_VERSION_4) { + IScsiServiceBindingGuid = &gIScsiV4PrivateGuid; + TcpServiceBindingGuid = &gEfiTcp4ServiceBindingProtocolGuid; + DhcpServiceBindingGuid = &gEfiDhcp4ServiceBindingProtocolGuid; + DnsServiceBindingGuid = &gEfiDns4ServiceBindingProtocolGuid; + + } else { + IScsiServiceBindingGuid = &gIScsiV6PrivateGuid; + TcpServiceBindingGuid = &gEfiTcp6ServiceBindingProtocolGuid; + DhcpServiceBindingGuid = &gEfiDhcp6ServiceBindingProtocolGuid; + DnsServiceBindingGuid = &gEfiDns6ServiceBindingProtocolGuid; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + IScsiServiceBindingGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + TcpServiceBindingGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Status = IScsiIsDevicePathSupported (RemainingDevicePath); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + if (IScsiDhcpIsConfigured (ControllerHandle, IpVersion)) { + Status = gBS->OpenProtocol ( + ControllerHandle, + DhcpServiceBindingGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + } + + if (IScsiDnsIsConfigured (ControllerHandle)) { + Status = gBS->OpenProtocol ( + ControllerHandle, + DnsServiceBindingGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + } + + return EFI_SUCCESS; +} + + +/** + Start to manage the controller. This is the worker function for + IScsiIp4(6)DriverBindingStart. + + @param[in] Image Handle of the image. + @param[in] ControllerHandle Handle of the controller. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + @retval EFI_SUCCES This driver was started. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_NOT_FOUND There is no sufficient information to establish + the iScsi session. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_DEVICE_ERROR Failed to get TCP connection device path. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the Handle + because its interfaces are being used. + +**/ +EFI_STATUS +IScsiStart ( + IN EFI_HANDLE Image, + IN EFI_HANDLE ControllerHandle, + IN UINT8 IpVersion + ) +{ + EFI_STATUS Status; + ISCSI_DRIVER_DATA *Private; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + ISCSI_SESSION *Session; + UINT8 Index; + EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExistIScsiExtScsiPassThru; + ISCSI_DRIVER_DATA *ExistPrivate; + UINT8 *AttemptConfigOrder; + UINTN AttemptConfigOrderSize; + UINT8 BootSelected; + EFI_HANDLE *HandleBuffer; + UINTN NumberOfHandles; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_GUID *IScsiPrivateGuid; + EFI_GUID *TcpServiceBindingGuid; + BOOLEAN NeedUpdate; + VOID *Interface; + EFI_GUID *ProtocolGuid; + UINT8 NetworkBootPolicy; + ISCSI_SESSION_CONFIG_NVDATA *NvData; + + // + // Test to see if iSCSI driver supports the given controller. + // + + if (IpVersion == IP_VERSION_4) { + IScsiPrivateGuid = &gIScsiV4PrivateGuid; + TcpServiceBindingGuid = &gEfiTcp4ServiceBindingProtocolGuid; + ProtocolGuid = &gEfiTcp4ProtocolGuid; + } else if (IpVersion == IP_VERSION_6) { + IScsiPrivateGuid = &gIScsiV6PrivateGuid; + TcpServiceBindingGuid = &gEfiTcp6ServiceBindingProtocolGuid; + ProtocolGuid = &gEfiTcp6ProtocolGuid; + } else { + return EFI_INVALID_PARAMETER; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + IScsiPrivateGuid, + NULL, + Image, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + TcpServiceBindingGuid, + NULL, + Image, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + NetworkBootPolicy = PcdGet8 (PcdIScsiAIPNetworkBootPolicy); + if (NetworkBootPolicy == ALWAYS_USE_ISCSI_HBA_AND_IGNORE_UEFI_ISCSI) { + return EFI_ABORTED; + } + + if (NetworkBootPolicy != ALWAYS_USE_UEFI_ISCSI_AND_IGNORE_ISCSI_HBA) { + // + // Check existing iSCSI AIP. + // + Status = IScsiCheckAip (); + if (!EFI_ERROR (Status)) { + // + // Find iSCSI AIP with specified network boot policy. return EFI_ABORTED. + // + return EFI_ABORTED; + } + } + + // + // Record the incoming NIC info. + // + Status = IScsiAddNic (ControllerHandle, Image); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Create the instance private data. + // + Private = IScsiCreateDriverData (Image, ControllerHandle); + if (Private == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Create a underlayer child instance, but not need to configure it. Just open ChildHandle + // via BY_DRIVER. That is, establishing the relationship between ControllerHandle and ChildHandle. + // Therefore, when DisconnectController(), especially VLAN virtual controller handle, + // IScsiDriverBindingStop() will be called. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + Image, + TcpServiceBindingGuid, + &Private->ChildHandle + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->ChildHandle, /// Default Tcp child + ProtocolGuid, + &Interface, + Image, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Always install private protocol no matter what happens later. We need to + // keep the relationship between ControllerHandle and ChildHandle. + // + Status = gBS->InstallProtocolInterface ( + &ControllerHandle, + IScsiPrivateGuid, + EFI_NATIVE_INTERFACE, + &Private->IScsiIdentifier + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + if (IpVersion == IP_VERSION_4) { + mPrivate->Ipv6Flag = FALSE; + } else { + mPrivate->Ipv6Flag = TRUE; + } + + // + // Get the current iSCSI configuration data. + // + Status = IScsiGetConfigData (Private); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // If there is already a successul attempt, check whether this attempt is the + // first "enabled for MPIO" attempt. If not, still try the first attempt. + // In single path mode, try all attempts. + // + ExistPrivate = NULL; + Status = EFI_NOT_FOUND; + + if (mPrivate->OneSessionEstablished && mPrivate->EnableMpio) { + AttemptConfigData = NULL; + NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) { + AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) { + break; + } + } + + if (AttemptConfigData == NULL) { + goto ON_ERROR; + } + + if (AttemptConfigData->AttemptConfigIndex == mPrivate->BootSelectedIndex) { + goto ON_EXIT; + } + + // + // Uninstall the original ExtScsiPassThru first. + // + + // + // Locate all ExtScsiPassThru protocol instances. + // + Status = gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiExtScsiPassThruProtocolGuid, + NULL, + &NumberOfHandles, + &HandleBuffer + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Find ExtScsiPassThru protocol instance produced by this driver. + // + ExistIScsiExtScsiPassThru = NULL; + for (Index = 0; Index < NumberOfHandles && ExistIScsiExtScsiPassThru == NULL; Index++) { + Status = gBS->HandleProtocol ( + HandleBuffer[Index], + &gEfiDevicePathProtocolGuid, + (VOID **) &DevicePath + ); + if (EFI_ERROR (Status)) { + continue; + } + + while (!IsDevicePathEnd (DevicePath)) { + if ((DevicePath->Type == MESSAGING_DEVICE_PATH) && (DevicePath->SubType == MSG_MAC_ADDR_DP)) { + // + // Get the ExtScsiPassThru protocol instance. + // + Status = gBS->HandleProtocol ( + HandleBuffer[Index], + &gEfiExtScsiPassThruProtocolGuid, + (VOID **) &ExistIScsiExtScsiPassThru + ); + ASSERT_EFI_ERROR (Status); + break; + } + + DevicePath = NextDevicePathNode (DevicePath); + } + } + + FreePool (HandleBuffer); + + if (ExistIScsiExtScsiPassThru == NULL) { + Status = EFI_NOT_FOUND; + goto ON_ERROR; + } + + ExistPrivate = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (ExistIScsiExtScsiPassThru); + + Status = gBS->UninstallProtocolInterface ( + ExistPrivate->ExtScsiPassThruHandle, + &gEfiExtScsiPassThruProtocolGuid, + &ExistPrivate->IScsiExtScsiPassThru + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + } + + // + // Install the Ext SCSI PASS THRU protocol. + // + Status = gBS->InstallProtocolInterface ( + &Private->ExtScsiPassThruHandle, + &gEfiExtScsiPassThruProtocolGuid, + EFI_NATIVE_INTERFACE, + &Private->IScsiExtScsiPassThru + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + BootSelected = 0; + + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &mPrivate->AttemptConfigs) { + AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + // + // Don't process the attempt that does not associate with the current NIC or + // this attempt is disabled or established. + // + if (AttemptConfigData->NicIndex != mPrivate->CurrentNic || + AttemptConfigData->SessionConfigData.Enabled == ISCSI_DISABLED || + AttemptConfigData->ValidPath) { + continue; + } + + // + // In multipath mode, don't process attempts configured for single path. + // In default single path mode, don't process attempts configured for multipath. + // + if ((mPrivate->EnableMpio && + AttemptConfigData->SessionConfigData.Enabled != ISCSI_ENABLED_FOR_MPIO) || + (!mPrivate->EnableMpio && + AttemptConfigData->SessionConfigData.Enabled != ISCSI_ENABLED)) { + continue; + } + + // + // Don't process the attempt that fails to get the init/target information from DHCP. + // + if (AttemptConfigData->SessionConfigData.InitiatorInfoFromDhcp && + !AttemptConfigData->DhcpSuccess) { + if (!mPrivate->EnableMpio && mPrivate->ValidSinglePathCount > 0) { + mPrivate->ValidSinglePathCount--; + } + continue; + } + + // + // Don't process the autoconfigure path if it is already established. + // + if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG && + AttemptConfigData->AutoConfigureSuccess) { + continue; + } + + // + // Don't process the attempt if its IP mode is not in the current IP version. + // + if (!mPrivate->Ipv6Flag) { + if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP6) { + continue; + } + if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG && + AttemptConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6) { + continue; + } + } else { + if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP4) { + continue; + } + if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG && + AttemptConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP4) { + continue; + } + } + + // + // Fill in the Session and init it. + // + Session = (ISCSI_SESSION *) AllocateZeroPool (sizeof (ISCSI_SESSION)); + if (Session == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + Session->Private = Private; + Session->ConfigData = AttemptConfigData; + Session->AuthType = AttemptConfigData->AuthenticationType; + + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Attempt %d", + (UINTN) AttemptConfigData->AttemptConfigIndex + ); + + if (Session->AuthType == ISCSI_AUTH_TYPE_CHAP) { + Session->AuthData.CHAP.AuthConfig = &AttemptConfigData->AuthConfigData.CHAP; + } + + IScsiSessionInit (Session, FALSE); + + // + // Try to login and create an iSCSI session according to the configuration. + // + Status = IScsiSessionLogin (Session); + if (Status == EFI_MEDIA_CHANGED) { + // + // The specified target is not available, and the redirection information is + // received. Login the session again with the updated target address. + // + Status = IScsiSessionLogin (Session); + } else if (Status == EFI_NOT_READY) { + Status = IScsiSessionReLogin (Session); + } + + // + // Restore the origial user setting which specifies the proxy/virtual iSCSI target to NV region. + // + NvData = &AttemptConfigData->SessionConfigData; + if (NvData->RedirectFlag) { + NvData->TargetPort = NvData->OriginalTargetPort; + CopyMem (&NvData->TargetIp, &NvData->OriginalTargetIp, sizeof (EFI_IP_ADDRESS)); + NvData->RedirectFlag = FALSE; + + gRT->SetVariable ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + ISCSI_CONFIG_VAR_ATTR, + sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA), + AttemptConfigData + ); + } + + if (EFI_ERROR (Status)) { + // + // In Single path mode, only the successful attempt will be recorded in iBFT; + // in multi-path mode, all the attempt entries in MPIO will be recorded in iBFT. + // + if (!mPrivate->EnableMpio && mPrivate->ValidSinglePathCount > 0) { + mPrivate->ValidSinglePathCount--; + } + + FreePool (Session); + + } else { + AttemptConfigData->ValidPath = TRUE; + + // + // Do not record the attempt in iBFT if it login with KRB5. + // TODO: record KRB5 attempt information in the iSCSI device path. + // + if (Session->AuthType == ISCSI_AUTH_TYPE_KRB) { + if (!mPrivate->EnableMpio && mPrivate->ValidSinglePathCount > 0) { + mPrivate->ValidSinglePathCount--; + } + + AttemptConfigData->ValidiBFTPath = FALSE; + } else { + AttemptConfigData->ValidiBFTPath = TRUE; + } + + // + // IScsi session success. Update the attempt state to NVR. + // + if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG) { + AttemptConfigData->AutoConfigureSuccess = TRUE; + } + + gRT->SetVariable ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + ISCSI_CONFIG_VAR_ATTR, + sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA), + AttemptConfigData + ); + + // + // Select the first login session. Abort others. + // + if (Private->Session == NULL) { + Private->Session = Session; + BootSelected = AttemptConfigData->AttemptConfigIndex; + // + // Don't validate other attempt in multipath mode if one is success. + // + if (mPrivate->EnableMpio) { + break; + } + } else { + IScsiSessionAbort (Session); + FreePool (Session); + } + } + } + + // + // All attempts configured for this driver instance are not valid. + // + if (Private->Session == NULL) { + Status = gBS->UninstallProtocolInterface ( + Private->ExtScsiPassThruHandle, + &gEfiExtScsiPassThruProtocolGuid, + &Private->IScsiExtScsiPassThru + ); + ASSERT_EFI_ERROR (Status); + Private->ExtScsiPassThruHandle = NULL; + + // + // Reinstall the original ExtScsiPassThru back. + // + if (mPrivate->OneSessionEstablished && ExistPrivate != NULL) { + Status = gBS->InstallProtocolInterface ( + &ExistPrivate->ExtScsiPassThruHandle, + &gEfiExtScsiPassThruProtocolGuid, + EFI_NATIVE_INTERFACE, + &ExistPrivate->IScsiExtScsiPassThru + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + goto ON_EXIT; + } + + Status = EFI_NOT_FOUND; + + goto ON_ERROR; + } + + NeedUpdate = TRUE; + // + // More than one attempt successes. + // + if (Private->Session != NULL && mPrivate->OneSessionEstablished) { + + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"AttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + if (AttemptConfigOrder == NULL) { + goto ON_ERROR; + } + for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) { + if (AttemptConfigOrder[Index] == mPrivate->BootSelectedIndex || + AttemptConfigOrder[Index] == BootSelected) { + break; + } + } + + if (mPrivate->EnableMpio) { + // + // Use the attempt in earlier order. Abort the later one in MPIO. + // + if (AttemptConfigOrder[Index] == mPrivate->BootSelectedIndex) { + IScsiSessionAbort (Private->Session); + FreePool (Private->Session); + Private->Session = NULL; + gBS->UninstallProtocolInterface ( + Private->ExtScsiPassThruHandle, + &gEfiExtScsiPassThruProtocolGuid, + &Private->IScsiExtScsiPassThru + ); + Private->ExtScsiPassThruHandle = NULL; + + // + // Reinstall the original ExtScsiPassThru back. + // + Status = gBS->InstallProtocolInterface ( + &ExistPrivate->ExtScsiPassThruHandle, + &gEfiExtScsiPassThruProtocolGuid, + EFI_NATIVE_INTERFACE, + &ExistPrivate->IScsiExtScsiPassThru + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + goto ON_EXIT; + } else { + if (AttemptConfigOrder[Index] != BootSelected) { + goto ON_ERROR; + } + mPrivate->BootSelectedIndex = BootSelected; + // + // Clear the resource in ExistPrivate. + // + gBS->UninstallProtocolInterface ( + ExistPrivate->Controller, + IScsiPrivateGuid, + &ExistPrivate->IScsiIdentifier + ); + + IScsiRemoveNic (ExistPrivate->Controller); + if (ExistPrivate->Session != NULL) { + IScsiSessionAbort (ExistPrivate->Session); + } + + if (ExistPrivate->DevicePath != NULL) { + Status = gBS->UninstallProtocolInterface ( + ExistPrivate->ExtScsiPassThruHandle, + &gEfiDevicePathProtocolGuid, + ExistPrivate->DevicePath + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + FreePool (ExistPrivate->DevicePath); + } + + gBS->CloseEvent (ExistPrivate->ExitBootServiceEvent); + FreePool (ExistPrivate); + + } + } else { + // + // Use the attempt in earlier order as boot selected in single path mode. + // + if (AttemptConfigOrder[Index] == mPrivate->BootSelectedIndex) { + NeedUpdate = FALSE; + } + } + + } + + if (NeedUpdate) { + mPrivate->OneSessionEstablished = TRUE; + mPrivate->BootSelectedIndex = BootSelected; + } + + // + // Duplicate the Session's tcp connection device path. The source port field + // will be set to zero as one iSCSI session is comprised of several iSCSI + // connections. + // + Private->DevicePath = IScsiGetTcpConnDevicePath (Private->Session); + if (Private->DevicePath == NULL) { + Status = EFI_DEVICE_ERROR; + goto ON_ERROR; + } + // + // Install the updated device path onto the ExtScsiPassThruHandle. + // + Status = gBS->InstallProtocolInterface ( + &Private->ExtScsiPassThruHandle, + &gEfiDevicePathProtocolGuid, + EFI_NATIVE_INTERFACE, + Private->DevicePath + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // ISCSI children should share the default Tcp child, just open the default Tcp child via BY_CHILD_CONTROLLER. + // + Status = gBS->OpenProtocol ( + Private->ChildHandle, /// Default Tcp child + ProtocolGuid, + &Interface, + Image, + Private->ExtScsiPassThruHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + gBS->UninstallMultipleProtocolInterfaces ( + Private->ExtScsiPassThruHandle, + &gEfiExtScsiPassThruProtocolGuid, + &Private->IScsiExtScsiPassThru, + &gEfiDevicePathProtocolGuid, + Private->DevicePath, + NULL + ); + + goto ON_ERROR; + } + +ON_EXIT: + + // + // Update/Publish the iSCSI Boot Firmware Table. + // + if (mPrivate->BootSelectedIndex != 0) { + IScsiPublishIbft (); + } + + return EFI_SUCCESS; + +ON_ERROR: + + if (Private->Session != NULL) { + IScsiSessionAbort (Private->Session); + } + + return Status; +} + +/** + Stops a device controller or a bus controller. This is the worker function for + IScsiIp4(6)DriverBindingStop. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the Handle + because its interfaces are being used. + +**/ +EFI_STATUS +EFIAPI +IScsiStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL, + IN UINT8 IpVersion + ) +{ + EFI_HANDLE IScsiController; + EFI_STATUS Status; + ISCSI_PRIVATE_PROTOCOL *IScsiIdentifier; + ISCSI_DRIVER_DATA *Private; + EFI_EXT_SCSI_PASS_THRU_PROTOCOL *PassThru; + ISCSI_CONNECTION *Conn; + EFI_GUID *ProtocolGuid; + EFI_GUID *TcpServiceBindingGuid; + EFI_GUID *TcpProtocolGuid; + + + if (NumberOfChildren != 0) { + // + // We should have only one child. + // + Status = gBS->OpenProtocol ( + ChildHandleBuffer[0], + &gEfiExtScsiPassThruProtocolGuid, + (VOID **) &PassThru, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru); + Conn = NET_LIST_HEAD (&Private->Session->Conns, ISCSI_CONNECTION, Link); + + // + // Previously the TCP protocol is opened BY_CHILD_CONTROLLER. Just close + // the protocol here, but do not uninstall the device path protocol and + // EXT SCSI PASS THRU protocol installed on ExtScsiPassThruHandle. + // + if (IpVersion == IP_VERSION_4) { + ProtocolGuid = &gEfiTcp4ProtocolGuid; + } else { + ProtocolGuid = &gEfiTcp6ProtocolGuid; + } + + gBS->CloseProtocol ( + Private->ChildHandle, + ProtocolGuid, + Private->Image, + Private->ExtScsiPassThruHandle + ); + + gBS->CloseProtocol ( + Conn->TcpIo.Handle, + ProtocolGuid, + Private->Image, + Private->ExtScsiPassThruHandle + ); + + return EFI_SUCCESS; + } + + // + // Get the handle of the controller we are controling. + // + if (IpVersion == IP_VERSION_4) { + ProtocolGuid = &gIScsiV4PrivateGuid; + TcpProtocolGuid = &gEfiTcp4ProtocolGuid; + TcpServiceBindingGuid = &gEfiTcp4ServiceBindingProtocolGuid; + } else { + ProtocolGuid = &gIScsiV6PrivateGuid; + TcpProtocolGuid = &gEfiTcp6ProtocolGuid; + TcpServiceBindingGuid = &gEfiTcp6ServiceBindingProtocolGuid; + } + IScsiController = NetLibGetNicHandle (ControllerHandle, TcpProtocolGuid); + if (IScsiController == NULL) { + return EFI_SUCCESS; + } + + Status = gBS->OpenProtocol ( + IScsiController, + ProtocolGuid, + (VOID **) &IScsiIdentifier, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + Private = ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier); + ASSERT (Private != NULL); + + if (Private->ChildHandle != NULL) { + Status = gBS->CloseProtocol ( + Private->ChildHandle, + TcpProtocolGuid, + This->DriverBindingHandle, + IScsiController + ); + + ASSERT (!EFI_ERROR (Status)); + + Status = NetLibDestroyServiceChild ( + IScsiController, + This->DriverBindingHandle, + TcpServiceBindingGuid, + Private->ChildHandle + ); + + ASSERT (!EFI_ERROR (Status)); + } + + gBS->UninstallProtocolInterface ( + IScsiController, + ProtocolGuid, + &Private->IScsiIdentifier + ); + + // + // Remove this NIC. + // + IScsiRemoveNic (IScsiController); + + // + // Update the iSCSI Boot Firware Table. + // + IScsiPublishIbft (); + + if (Private->Session != NULL) { + IScsiSessionAbort (Private->Session); + } + + Status = IScsiCleanDriverData (Private); + + if (EFI_ERROR (Status)) { + return Status; + } + + return EFI_SUCCESS; +} + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Since ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +IScsiIp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return IScsiSupported ( + This, + ControllerHandle, + RemainingDevicePath, + IP_VERSION_4 + ); +} + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error. Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failed to start the device. + +**/ +EFI_STATUS +EFIAPI +IScsiIp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + Status = IScsiStart (This->DriverBindingHandle, ControllerHandle, IP_VERSION_4); + if (Status == EFI_ALREADY_STARTED) { + Status = EFI_SUCCESS; + } + + return Status; +} + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +IScsiIp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + return IScsiStop ( + This, + ControllerHandle, + NumberOfChildren, + ChildHandleBuffer, + IP_VERSION_4 + ); +} + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Since ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +IScsiIp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return IScsiSupported ( + This, + ControllerHandle, + RemainingDevicePath, + IP_VERSION_6 + ); +} + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error. Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failed to start the device. + +**/ +EFI_STATUS +EFIAPI +IScsiIp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + Status = IScsiStart (This->DriverBindingHandle, ControllerHandle, IP_VERSION_6); + if (Status == EFI_ALREADY_STARTED) { + Status = EFI_SUCCESS; + } + + return Status; +} + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +IScsiIp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + return IScsiStop ( + This, + ControllerHandle, + NumberOfChildren, + ChildHandleBuffer, + IP_VERSION_6 + ); +} + +/** + Unload the iSCSI driver. + + @param[in] ImageHandle The handle of the driver image. + + @retval EFI_SUCCESS The driver is unloaded. + @retval EFI_DEVICE_ERROR An unexpected error occurred. + +**/ +EFI_STATUS +EFIAPI +IScsiUnload ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_STATUS Status; + UINTN DeviceHandleCount; + EFI_HANDLE *DeviceHandleBuffer; + UINTN Index; + EFI_COMPONENT_NAME_PROTOCOL *ComponentName; + EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2; + + // + // Try to disonnect the driver from the devices it's controlling. + // + Status = gBS->LocateHandleBuffer ( + AllHandles, + NULL, + NULL, + &DeviceHandleCount, + &DeviceHandleBuffer + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Disconnect the iSCSI4 driver from the controlled device. + // + for (Index = 0; Index < DeviceHandleCount; Index++) { + Status = IScsiTestManagedDevice ( + DeviceHandleBuffer[Index], + gIScsiIp4DriverBinding.DriverBindingHandle, + &gEfiTcp4ProtocolGuid) + ; + if (EFI_ERROR (Status)) { + continue; + } + Status = gBS->DisconnectController ( + DeviceHandleBuffer[Index], + gIScsiIp4DriverBinding.DriverBindingHandle, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + // + // Disconnect the iSCSI6 driver from the controlled device. + // + for (Index = 0; Index < DeviceHandleCount; Index++) { + Status = IScsiTestManagedDevice ( + DeviceHandleBuffer[Index], + gIScsiIp6DriverBinding.DriverBindingHandle, + &gEfiTcp6ProtocolGuid + ); + if (EFI_ERROR (Status)) { + continue; + } + Status = gBS->DisconnectController ( + DeviceHandleBuffer[Index], + gIScsiIp6DriverBinding.DriverBindingHandle, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + // + // Unload the iSCSI configuration form. + // + Status = IScsiConfigFormUnload (gIScsiIp4DriverBinding.DriverBindingHandle); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Uninstall the protocols installed by iSCSI driver. + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + ImageHandle, + &gEfiAuthenticationInfoProtocolGuid, + &gIScsiAuthenticationInfo, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + if (gIScsiControllerNameTable!= NULL) { + Status = FreeUnicodeStringTable (gIScsiControllerNameTable); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + gIScsiControllerNameTable = NULL; + } + + // + // Uninstall the ComponentName and ComponentName2 protocol from iSCSI4 driver binding handle + // if it has been installed. + // + Status = gBS->HandleProtocol ( + gIScsiIp4DriverBinding.DriverBindingHandle, + &gEfiComponentNameProtocolGuid, + (VOID **) &ComponentName + ); + if (!EFI_ERROR (Status)) { + Status = gBS->UninstallMultipleProtocolInterfaces ( + gIScsiIp4DriverBinding.DriverBindingHandle, + &gEfiComponentNameProtocolGuid, + ComponentName, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + Status = gBS->HandleProtocol ( + gIScsiIp4DriverBinding.DriverBindingHandle, + &gEfiComponentName2ProtocolGuid, + (VOID **) &ComponentName2 + ); + if (!EFI_ERROR (Status)) { + gBS->UninstallMultipleProtocolInterfaces ( + gIScsiIp4DriverBinding.DriverBindingHandle, + &gEfiComponentName2ProtocolGuid, + ComponentName2, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + // + // Uninstall the ComponentName and ComponentName2 protocol from iSCSI6 driver binding handle + // if it has been installed. + // + Status = gBS->HandleProtocol ( + gIScsiIp6DriverBinding.DriverBindingHandle, + &gEfiComponentNameProtocolGuid, + (VOID **) &ComponentName + ); + if (!EFI_ERROR (Status)) { + Status = gBS->UninstallMultipleProtocolInterfaces ( + gIScsiIp6DriverBinding.DriverBindingHandle, + &gEfiComponentNameProtocolGuid, + ComponentName, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + Status = gBS->HandleProtocol ( + gIScsiIp6DriverBinding.DriverBindingHandle, + &gEfiComponentName2ProtocolGuid, + (VOID **) &ComponentName2 + ); + if (!EFI_ERROR (Status)) { + gBS->UninstallMultipleProtocolInterfaces ( + gIScsiIp6DriverBinding.DriverBindingHandle, + &gEfiComponentName2ProtocolGuid, + ComponentName2, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + // + // Uninstall the IScsiInitiatorNameProtocol and all the driver binding protocols. + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + gIScsiIp4DriverBinding.DriverBindingHandle, + &gEfiDriverBindingProtocolGuid, + &gIScsiIp4DriverBinding, + &gEfiIScsiInitiatorNameProtocolGuid, + &gIScsiInitiatorName, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = gBS->UninstallMultipleProtocolInterfaces ( + gIScsiIp6DriverBinding.DriverBindingHandle, + &gEfiDriverBindingProtocolGuid, + &gIScsiIp6DriverBinding, + NULL + ); + +ON_EXIT: + + if (DeviceHandleBuffer != NULL) { + FreePool (DeviceHandleBuffer); + } + + return Status; +} + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + The entry point for iSCSI driver which initializes the global variables and + installs the driver binding, component name protocol, iSCSI initiator name + protocol and Authentication Info protocol on its image. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +IScsiDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + EFI_ISCSI_INITIATOR_NAME_PROTOCOL *IScsiInitiatorName; + EFI_AUTHENTICATION_INFO_PROTOCOL *AuthenticationInfo; + + // + // There should be only one EFI_ISCSI_INITIATOR_NAME_PROTOCOL. + // + Status = gBS->LocateProtocol ( + &gEfiIScsiInitiatorNameProtocolGuid, + NULL, + (VOID **) &IScsiInitiatorName + ); + if (!EFI_ERROR (Status)) { + return EFI_ACCESS_DENIED; + } + + // + // Initialize the EFI Driver Library. + // + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gIScsiIp4DriverBinding, + ImageHandle, + &gIScsiComponentName, + &gIScsiComponentName2 + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gIScsiIp6DriverBinding, + NULL, + &gIScsiComponentName, + &gIScsiComponentName2 + ); + if (EFI_ERROR (Status)) { + goto Error1; + } + + // + // Install the iSCSI Initiator Name Protocol. + // + Status = gBS->InstallProtocolInterface ( + &ImageHandle, + &gEfiIScsiInitiatorNameProtocolGuid, + EFI_NATIVE_INTERFACE, + &gIScsiInitiatorName + ); + if (EFI_ERROR (Status)) { + goto Error2; + } + + // + // Create the private data structures. + // + mPrivate = AllocateZeroPool (sizeof (ISCSI_PRIVATE_DATA)); + if (mPrivate == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error3; + } + + InitializeListHead (&mPrivate->NicInfoList); + InitializeListHead (&mPrivate->AttemptConfigs); + + // + // Initialize the configuration form of iSCSI. + // + Status = IScsiConfigFormInit (gIScsiIp4DriverBinding.DriverBindingHandle); + if (EFI_ERROR (Status)) { + goto Error4; + } + + // + // Create the Maximum Attempts. + // + Status = IScsiCreateAttempts (PcdGet8 (PcdMaxIScsiAttemptNumber)); + if (EFI_ERROR (Status)) { + goto Error5; + } + + // + // Create Keywords for all the Attempts. + // + Status = IScsiCreateKeywords (PcdGet8 (PcdMaxIScsiAttemptNumber)); + if (EFI_ERROR (Status)) { + goto Error6; + } + + // + // There should be only one EFI_AUTHENTICATION_INFO_PROTOCOL. If already exists, + // do not produce the protocol instance. + // + Status = gBS->LocateProtocol ( + &gEfiAuthenticationInfoProtocolGuid, + NULL, + (VOID **) &AuthenticationInfo + ); + if (Status == EFI_NOT_FOUND) { + Status = gBS->InstallProtocolInterface ( + &ImageHandle, + &gEfiAuthenticationInfoProtocolGuid, + EFI_NATIVE_INTERFACE, + &gIScsiAuthenticationInfo + ); + if (EFI_ERROR (Status)) { + goto Error6; + } + } + + return EFI_SUCCESS; + +Error6: + IScsiCleanAttemptVariable (); + +Error5: + IScsiConfigFormUnload (gIScsiIp4DriverBinding.DriverBindingHandle); + +Error4: + if (mPrivate != NULL) { + FreePool (mPrivate); + mPrivate = NULL; + } + +Error3: + gBS->UninstallMultipleProtocolInterfaces ( + ImageHandle, + &gEfiIScsiInitiatorNameProtocolGuid, + &gIScsiInitiatorName, + NULL + ); + +Error2: + EfiLibUninstallDriverBindingComponentName2 ( + &gIScsiIp6DriverBinding, + &gIScsiComponentName, + &gIScsiComponentName2 + ); + +Error1: + EfiLibUninstallDriverBindingComponentName2 ( + &gIScsiIp4DriverBinding, + &gIScsiComponentName, + &gIScsiComponentName2 + ); + + return Status; +} + diff --git a/NetworkPkg/IScsiDxe/IScsiDriver.h b/NetworkPkg/IScsiDxe/IScsiDriver.h new file mode 100644 index 000000000..070dbbad7 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiDriver.h @@ -0,0 +1,814 @@ +/** @file + The header file of IScsiDriver.c. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2017 Hewlett Packard Enterprise Development LP
+ +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ISCSI_DRIVER_H_ +#define _ISCSI_DRIVER_H_ + +#define ISCSI_V4_PRIVATE_GUID \ + { \ + 0xfa3cde4c, 0x87c2, 0x427d, { 0xae, 0xde, 0x7d, 0xd0, 0x96, 0xc8, 0x8c, 0x58 } \ + } + +#define ISCSI_V6_PRIVATE_GUID \ + { \ + 0x28be27e5, 0x66cc, 0x4a31, { 0xa3, 0x15, 0xdb, 0x14, 0xc3, 0x74, 0x4d, 0x85 } \ + } + +#define ISCSI_INITIATOR_NAME_VAR_NAME L"I_NAME" + +#define IP_MODE_AUTOCONFIG_IP4 3 +#define IP_MODE_AUTOCONFIG_IP6 4 +#define ALWAYS_USE_UEFI_ISCSI_AND_IGNORE_ISCSI_HBA 0x00 +#define STOP_UEFI_ISCSI_IF_HBA_INSTALL_AIP 0x01 +#define STOP_UEFI_ISCSI_IF_AIP_SUPPORT_IP4 0x02 +#define STOP_UEFI_ISCSI_IF_AIP_SUPPORT_IP6 0x04 +#define STOP_UEFI_ISCSI_IF_AIP_SUPPORT_OFFLOAD 0x08 +#define STOP_UEFI_ISCSI_IF_AIP_SUPPORT_MPIO 0x10 +#define STOP_UEFI_ISCSI_IF_AIP_CONFIGURED_IP4 0x20 +#define STOP_UEFI_ISCSI_IF_AIP_CONFIGURED_IP6 0x40 +#define ALWAYS_USE_ISCSI_HBA_AND_IGNORE_UEFI_ISCSI 0xFF + +extern EFI_COMPONENT_NAME2_PROTOCOL gIScsiComponentName2; +extern EFI_COMPONENT_NAME_PROTOCOL gIScsiComponentName; +extern EFI_UNICODE_STRING_TABLE *gIScsiControllerNameTable; +extern EFI_ISCSI_INITIATOR_NAME_PROTOCOL gIScsiInitiatorName; +extern EFI_AUTHENTICATION_INFO_PROTOCOL gIScsiAuthenticationInfo; +extern EFI_EXT_SCSI_PASS_THRU_PROTOCOL gIScsiExtScsiPassThruProtocolTemplate; +extern EFI_GUID gIScsiV4PrivateGuid; +extern EFI_GUID gIScsiV6PrivateGuid; + +typedef struct { + CHAR16 PortString[ISCSI_NAME_IFR_MAX_SIZE]; + LIST_ENTRY NicInfoList; + UINT8 NicCount; + UINT8 CurrentNic; + UINT8 MaxNic; + BOOLEAN Ipv6Flag; + BOOLEAN OneSessionEstablished; + BOOLEAN EnableMpio; + UINT8 MpioCount; // The number of attempts in MPIO. + UINT8 Krb5MpioCount; // The number of attempts login with KRB5 in MPIO. + UINT8 SinglePathCount; // The number of single path attempts. + UINT8 ValidSinglePathCount; // The number of valid single path attempts. + UINT8 BootSelectedIndex; + UINT8 AttemptCount; + LIST_ENTRY AttemptConfigs; // User configured Attempt list. + CHAR8 InitiatorName[ISCSI_NAME_MAX_SIZE]; + UINTN InitiatorNameLength; +} ISCSI_PRIVATE_DATA; + +extern ISCSI_PRIVATE_DATA *mPrivate; + +typedef struct { + LIST_ENTRY Link; + UINT32 HwAddressSize; + EFI_MAC_ADDRESS PermanentAddress; + UINT8 NicIndex; + UINT16 VlanId; + UINTN BusNumber; + UINTN DeviceNumber; + UINTN FunctionNumber; + BOOLEAN Ipv6Available; +} ISCSI_NIC_INFO; + +typedef struct _ISCSI_PRIVATE_PROTOCOL { + UINT32 Reserved; +} ISCSI_PRIVATE_PROTOCOL; + +// +// EFI Driver Binding Protocol for iSCSI driver. +// + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Since ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +IScsiIp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error. Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failed to start the device. + +**/ +EFI_STATUS +EFIAPI +IScsiIp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +IScsiIp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Since ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +IScsiIp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error. Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failed to start the device. + +**/ +EFI_STATUS +EFIAPI +IScsiIp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +IScsiIp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +// +// EFI Component Name(2) Protocol for iSCSI driver. +// + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +IScsiComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is + determined by the driver writer. Language is + specified inRFC 4646 or ISO 639-2 language code + format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +IScsiComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +// +// EFI iSCSI Initiator Name Protocol for iSCSI driver. +// + +/** + Retrieves the current set value of iSCSI Initiator Name. + + @param[in] This Pointer to the EFI_ISCSI_INITIATOR_NAME_PROTOCOL + instance. + @param[in, out] BufferSize Size of the buffer in bytes pointed to by Buffer / + Actual size of the variable data buffer. + @param[out] Buffer Pointer to the buffer for data to be read. + + @retval EFI_SUCCESS Data was successfully retrieved into the provided + buffer and the BufferSize was sufficient to handle + the iSCSI initiator name. + @retval EFI_BUFFER_TOO_SMALL BufferSize is too small for the result. BufferSize + will be updated with the size required to complete + the request. Buffer will not be affected. + @retval EFI_INVALID_PARAMETER BufferSize is NULL. BufferSize and Buffer will not + be affected. + @retval EFI_INVALID_PARAMETER Buffer is NULL. BufferSize and Buffer will not be + affected. + @retval EFI_DEVICE_ERROR The iSCSI initiator name could not be retrieved + due to a hardware error. + +**/ +EFI_STATUS +EFIAPI +IScsiGetInitiatorName ( + IN EFI_ISCSI_INITIATOR_NAME_PROTOCOL *This, + IN OUT UINTN *BufferSize, + OUT VOID *Buffer + ); + +/** + Sets the iSSI Initiator Name. + + @param[in] This Pointer to the EFI_ISCSI_INITIATOR_NAME_PROTOCOL + instance. + @param[in, out] BufferSize Size of the buffer in bytes pointed to by Buffer. + @param[in] Buffer Pointer to the buffer for data to be written. + + @retval EFI_SUCCESS Data was successfully stored by the protocol. + @retval EFI_UNSUPPORTED Platform policies do not allow for data to be + written. + @retval EFI_INVALID_PARAMETER BufferSize exceeds the maximum allowed limit. + BufferSize will be updated with the maximum size + required to complete the request. + @retval EFI_INVALID_PARAMETER Buffersize is NULL. BufferSize and Buffer will not + be affected. + @retval EFI_INVALID_PARAMETER Buffer is NULL. BufferSize and Buffer will not be + affected. + @retval EFI_DEVICE_ERROR The data could not be stored due to a hardware + error. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the data + @retval EFI_PROTOCOL_ERROR Input iSCSI initiator name does not adhere to RFC + 3720 + +**/ +EFI_STATUS +EFIAPI +IScsiSetInitiatorName ( + IN EFI_ISCSI_INITIATOR_NAME_PROTOCOL *This, + IN OUT UINTN *BufferSize, + IN VOID *Buffer + ); + +// +// EFI_AUTHENTICATION_INFO_PROTOCOL for iSCSI driver. +// + +/** + Retrieves the authentication information associated with a particular controller handle. + + @param[in] This Pointer to the EFI_AUTHENTICATION_INFO_PROTOCOL. + @param[in] ControllerHandle Handle to the Controller. + @param[out] Buffer Pointer to the authentication information. This function is + responsible for allocating the buffer and it is the caller's + responsibility to free buffer when the caller is finished with buffer. + + @retval EFI_DEVICE_ERROR The authentication information could not be + retrieved due to a hardware error. + +**/ +EFI_STATUS +EFIAPI +IScsiGetAuthenticationInfo ( + IN EFI_AUTHENTICATION_INFO_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + OUT VOID **Buffer + ); + +/** + Set the authentication information for a given controller handle. + + @param[in] This Pointer to the EFI_AUTHENTICATION_INFO_PROTOCOL. + @param[in] ControllerHandle Handle to the Controller. + @param[in] Buffer Pointer to the authentication information. + + @retval EFI_UNSUPPORTED If the platform policies do not allow setting of + the authentication information. + +**/ +EFI_STATUS +EFIAPI +IScsiSetAuthenticationInfo ( + IN EFI_AUTHENTICATION_INFO_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN VOID *Buffer + ); + +// +// EFI_EXT_SCSI_PASS_THRU_PROTOCOL for iSCSI driver. +// + +/** + Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. + This function supports both blocking I/O and nonblocking I/O. The blocking I/O + functionality is required, and the nonblocking I/O functionality is optional. + + @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance. + @param[in] Target The Target is an array of size TARGET_MAX_BYTES and it + represents the id of the SCSI device to send the SCSI + Request Packet. Each transport driver may choose to + utilize a subset of this size to suit the needs + of transport target representation. For example, a + Fibre Channel driver may use only 8 bytes (WWN) + to represent an FC target. + @param[in] Lun The LUN of the SCSI device to send the SCSI Request Packet. + @param[in, out] Packet A pointer to the SCSI Request Packet to send to the + SCSI device specified by Target and Lun. + @param[in] Event If nonblocking I/O is not supported then Event is ignored, + and blocking I/O is performed. If Event is NULL, then + blocking I/O is performed. If Event is not NULL and non + blocking I/O is supported, then nonblocking I/O is performed, + and Event will be signaled when the SCSI Request Packet + completes. + + @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For + bi-directional commands, InTransferLength bytes + were transferred from InDataBuffer. + For write and bi-directional commands, OutTransferLength + bytes were transferred by OutDataBuffer. + @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. + The number of bytes that could be transferred is + returned in InTransferLength. For write and + bi-directional commands, OutTransferLength bytes + were transferred by OutDataBuffer. + @retval EFI_NOT_READY The SCSI Request Packet could not be sent because + there are too many SCSI Request Packets already + queued. The caller may retry later. + @retval EFI_DEVICE_ERROR A device error occurred while attempting to send + the SCSI Request Packet. + @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket + are invalid. + @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet + is not supported by the host adapter. + This includes the case of Bi-directional SCSI + commands not supported by the implementation. + The SCSI Request Packet was not sent, + so no additional status information is available. + @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI + Request Packet to execute. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruFunction ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN UINT8 *Target, + IN UINT64 Lun, + IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet, + IN EFI_EVENT Event OPTIONAL + ); + +/** + Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on + a SCSI channel. These can either be the list SCSI devices that are actually + present on the SCSI channel, or the list of legal Target Ids and LUNs for the + SCSI channel. Regardless, the caller of this function must probe the Target ID + and LUN returned to see if a SCSI device is actually present at that location + on the SCSI channel. + + @param[in] This The EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance. + @param[in, out] Target On input, a pointer to the Target ID of a SCSI + device present on the SCSI channel. On output, a + pointer to the Target ID of the next SCSI device + present on a SCSI channel. An input value of + 0xFFFFFFFF retrieves the Target ID of the first + SCSI device present on a SCSI channel. + @param[in, out] Lun On input, a pointer to the LUN of a SCSI device + present on the SCSI channel. On output, a pointer + to the LUN of the next SCSI device present on a + SCSI channel. + + @retval EFI_SUCCESS The Target ID and Lun of the next SCSI device on + the SCSI channel was returned in Target and Lun. + @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI + channel. + @retval EFI_INVALID_PARAMETER Target is not 0xFFFFFFFF,and Target and Lun were + not returned on a previous call to + GetNextDevice(). + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruGetNextTargetLun ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN OUT UINT8 **Target, + IN OUT UINT64 *Lun + ); + +/** + Allocate and build a device path node for a SCSI device on a SCSI channel. + + @param[in] This Protocol instance pointer. + @param[in] Target The Target ID of the SCSI device for which a + device path node is to be allocated and built. + @param[in] Lun The LUN of the SCSI device for which a device + path node is to be allocated and built. + @param[in, out] DevicePath A pointer to a single device path node that + describes the SCSI device specified by Target and + Lun. This function is responsible for allocating + the buffer DevicePath with the boot service + AllocatePool(). It is the caller's + responsibility to free DevicePath when the caller + is finished with DevicePath. + + @retval EFI_SUCCESS The device path node that describes the SCSI + device specified by Target and Lun was allocated + and returned in DevicePath. + @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does + not exist on the SCSI channel. + @retval EFI_INVALID_PARAMETER DevicePath is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate + DevicePath. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruBuildDevicePath ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN UINT8 *Target, + IN UINT64 Lun, + IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath + ); + +/** + Translate a device path node to a Target ID and LUN. + + @param[in] This Protocol instance pointer. + @param[in] DevicePath A pointer to the device path node that describes + a SCSI device on the SCSI channel. + @param[out] Target A pointer to the Target ID of a SCSI device on + the SCSI channel. + @param[out] Lun A pointer to the LUN of a SCSI device on the SCSI + channel. + + @retval EFI_SUCCESS DevicePath was successfully translated to a + Target ID and LUN, and they were returned in + Target and Lun. + @retval EFI_INVALID_PARAMETER DevicePath/Target/Lun is NULL. + @retval EFI_UNSUPPORTED This driver does not support the device path node + type in DevicePath. + @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target + ID and LUN does not exist. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruGetTargetLun ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, + OUT UINT8 **Target, + OUT UINT64 *Lun + ); + +/** + Resets a SCSI channel.This operation resets all the SCSI devices connected to + the SCSI channel. + + @param[in] This Protocol instance pointer. + + @retval EFI_UNSUPPORTED It is not supported. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruResetChannel ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This + ); + +/** + Resets a SCSI device that is connected to a SCSI channel. + + @param[in] This Protocol instance pointer. + @param[in] Target The Target ID of the SCSI device to reset. + @param[in] Lun The LUN of the SCSI device to reset. + + @retval EFI_UNSUPPORTED It is not supported. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruResetTargetLun ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN UINT8 *Target, + IN UINT64 Lun + ); + +/** + Retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. + + @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL + instance. + @param[in, out] Target (TARGET_MAX_BYTES) of a SCSI device present on + the SCSI channel. On output, a pointer to the + Target ID (an array of TARGET_MAX_BYTES) of the + next SCSI device present on a SCSI channel. + An input value of 0xF(all bytes in the array are 0xF) + in the Target array retrieves the Target ID of the + first SCSI device present on a SCSI channel. + + @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI + channel was returned in Target. + @retval EFI_INVALID_PARAMETER Target or Lun is NULL. + @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not + returned on a previous call to GetNextTarget(). + @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruGetNextTarget ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN OUT UINT8 **Target + ); + +#endif diff --git a/NetworkPkg/IScsiDxe/IScsiDxe.inf b/NetworkPkg/IScsiDxe/IScsiDxe.inf new file mode 100644 index 000000000..0ffb340ce --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiDxe.inf @@ -0,0 +1,140 @@ +## @file +# Client-side iSCSI service. +# +# The iSCSI driver provides iSCSI service in the preboot environment and supports +# booting over iSCSI. This driver supports both IPv4 and IPv6 network stack. +# +# Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = IScsiDxe + FILE_GUID = 86CDDF93-4872-4597-8AF9-A35AE4D3725F + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = IScsiDriverEntryPoint + UNLOAD_IMAGE = IScsiUnload + MODULE_UNI_FILE = IScsiDxe.uni + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# +# DRIVER_BINDING = gIScsiDriverBinding +# COMPONENT_NAME = gIScsiComponentName +# COMPONENT_NAME2 = gIScsiComponentName2 +# + + +[Sources] + ComponentName.c + IScsiAuthenticationInfo.c + IScsiCHAP.h + IScsiCHAP.c + IScsiConfig.c + IScsiConfig.h + IScsiConfigNVDataStruc.h + IScsiConfigStrings.uni + IScsiConfigVfr.vfr + IScsiDhcp.c + IScsiDhcp.h + IScsiDhcp6.c + IScsiDhcp6.h + IScsiDns.c + IScsiDns.h + IScsiDriver.c + IScsiDriver.h + IScsiExtScsiPassThru.c + IScsiIbft.c + IScsiIbft.h + IScsiInitiatorName.c + IScsiImpl.h + IScsiMisc.c + IScsiMisc.h + IScsiProto.c + IScsiProto.h + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + CryptoPkg/CryptoPkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + DevicePathLib + HiiLib + MemoryAllocationLib + NetLib + TcpIoLib + PrintLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiLib + UefiRuntimeServicesTableLib + UefiHiiServicesLib + BaseCryptLib + +[Protocols] + gEfiAcpiTableProtocolGuid ## SOMETIMES_CONSUMES ## SystemTable + gEfiDriverBindingProtocolGuid ## SOMETIMES_PRODUCES + gEfiPciIoProtocolGuid ## SOMETIMES_CONSUMES + gEfiDhcp4ProtocolGuid ## SOMETIMES_CONSUMES + gEfiDhcp6ProtocolGuid ## SOMETIMES_CONSUMES + gEfiDhcp4ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiDhcp6ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiDns4ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiDns4ProtocolGuid ## SOMETIMES_CONSUMES + gEfiDns6ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiDns6ProtocolGuid ## SOMETIMES_CONSUMES + gEfiIp4Config2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiIp6ConfigProtocolGuid ## SOMETIMES_CONSUMES + gEfiTcp4ProtocolGuid ## TO_START + gEfiTcp6ProtocolGuid ## TO_START + gEfiTcp4ServiceBindingProtocolGuid ## TO_START + gEfiTcp6ServiceBindingProtocolGuid ## TO_START + gEfiExtScsiPassThruProtocolGuid ## BY_START + gEfiHiiConfigAccessProtocolGuid ## PRODUCES + ## TO_START + ## PRODUCES + gEfiDevicePathProtocolGuid + ## PRODUCES + ## UNDEFINED # Variable + gEfiIScsiInitiatorNameProtocolGuid + ## PRODUCES + gEfiAuthenticationInfoProtocolGuid + ## SOMETIMES_CONSUMES + gEfiAdapterInformationProtocolGuid + gEfiNetworkInterfaceIdentifierProtocolGuid_31 ## SOMETIMES_CONSUMES + +[Guids] + gEfiEventExitBootServicesGuid ## SOMETIMES_CONSUMES ## Event + gEfiIfrTianoGuid ## SOMETIMES_PRODUCES ## UNDEFINED + gEfiAcpiTableGuid ## SOMETIMES_CONSUMES ## SystemTable + gEfiAcpi10TableGuid ## SOMETIMES_CONSUMES ## SystemTable + gEfiAdapterInfoNetworkBootGuid ## SOMETIMES_CONSUMES ## UNDEFINED + gEfiAdapterInfoUndiIpv6SupportGuid ## SOMETIMES_CONSUMES ## GUID + + ## SOMETIMES_PRODUCES ## Variable:L"AttemptOrder" + ## SOMETIMES_CONSUMES ## Variable:L"AttemptOrder" + ## SOMETIMES_PRODUCES ## Variable:L"InitialAttemptOrder" + ## SOMETIMES_CONSUMES ## Variable:L"InitialAttemptOrder" + ## SOMETIMES_CONSUMES ## UNDEFINED # HiiIsConfigHdrMatch mVendorStorageName + ## SOMETIMES_PRODUCES ## UNDEFINED # HiiConstructConfigHdr mVendorStorageName + ## SOMETIMES_PRODUCES ## UNDEFINED # HiiGetBrowserData mVendorStorageName + ## SOMETIMES_CONSUMES ## UNDEFINED # HiiSetBrowserData mVendorStorageName + ## SOMETIMES_CONSUMES ## HII + gIScsiConfigGuid + +[Pcd] + gEfiNetworkPkgTokenSpaceGuid.PcdIScsiAIPNetworkBootPolicy ## CONSUMES + gEfiNetworkPkgTokenSpaceGuid.PcdMaxIScsiAttemptNumber ## CONSUMES + +[UserExtensions.TianoCore."ExtraFiles"] + IScsiDxeExtra.uni diff --git a/NetworkPkg/IScsiDxe/IScsiDxe.uni b/NetworkPkg/IScsiDxe/IScsiDxe.uni new file mode 100644 index 000000000..ec016a087 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiDxe.uni @@ -0,0 +1,17 @@ +// /** @file +// Client-side iSCSI service. +// +// The iSCSI driver provides iSCSI service in the preboot environment and supports +// booting over iSCSI. +// +// Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Client-side iSCSI service" + +#string STR_MODULE_DESCRIPTION #language en-US "The iSCSI driver provides iSCSI service in the preboot environment and supports booting over iSCSI." + diff --git a/NetworkPkg/IScsiDxe/IScsiDxeExtra.uni b/NetworkPkg/IScsiDxe/IScsiDxeExtra.uni new file mode 100644 index 000000000..08703940a --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// IScsiDxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"iSCSI DXE" + + diff --git a/NetworkPkg/IScsiDxe/IScsiExtScsiPassThru.c b/NetworkPkg/IScsiDxe/IScsiExtScsiPassThru.c new file mode 100644 index 000000000..06fcf92bb --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiExtScsiPassThru.c @@ -0,0 +1,419 @@ +/** @file + The implementation of EFI_EXT_SCSI_PASS_THRU_PROTOCOL. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + +EFI_EXT_SCSI_PASS_THRU_PROTOCOL gIScsiExtScsiPassThruProtocolTemplate = { + NULL, + IScsiExtScsiPassThruFunction, + IScsiExtScsiPassThruGetNextTargetLun, + IScsiExtScsiPassThruBuildDevicePath, + IScsiExtScsiPassThruGetTargetLun, + IScsiExtScsiPassThruResetChannel, + IScsiExtScsiPassThruResetTargetLun, + IScsiExtScsiPassThruGetNextTarget +}; + + +/** + Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. + This function supports both blocking I/O and nonblocking I/O. The blocking I/O + functionality is required, and the nonblocking I/O functionality is optional. + + @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance. + @param[in] Target The Target is an array of size TARGET_MAX_BYTES and it + represents the id of the SCSI device to send the SCSI + Request Packet. Each transport driver may choose to + utilize a subset of this size to suit the needs + of transport target representation. For example, a + Fibre Channel driver may use only 8 bytes (WWN) + to represent an FC target. + @param[in] Lun The LUN of the SCSI device to send the SCSI Request Packet. + @param[in, out] Packet A pointer to the SCSI Request Packet to send to the + SCSI device specified by Target and Lun. + @param[in] Event If nonblocking I/O is not supported then Event is ignored, + and blocking I/O is performed. If Event is NULL, then + blocking I/O is performed. If Event is not NULL and non + blocking I/O is supported, then nonblocking I/O is performed, + and Event will be signaled when the SCSI Request Packet + completes. + + @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For + bi-directional commands, InTransferLength bytes + were transferred from InDataBuffer. + For write and bi-directional commands, OutTransferLength + bytes were transferred by OutDataBuffer. + @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. + The number of bytes that could be transferred is + returned in InTransferLength. For write and + bi-directional commands, OutTransferLength bytes + were transferred by OutDataBuffer. + @retval EFI_NOT_READY The SCSI Request Packet could not be sent because + there are too many SCSI Request Packets already + queued. The caller may retry later. + @retval EFI_DEVICE_ERROR A device error occurred while attempting to send + the SCSI Request Packet. + @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket, + are invalid. + @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet + is not supported by the host adapter. + This includes the case of Bi-directional SCSI + commands not supported by the implementation. + The SCSI Request Packet was not sent, + so no additional status information is available. + @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI + Request Packet to execute. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruFunction ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN UINT8 *Target, + IN UINT64 Lun, + IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet, + IN EFI_EVENT Event OPTIONAL + ) +{ + EFI_STATUS Status; + ISCSI_DRIVER_DATA *Private; + + if (Target[0] != 0) { + return EFI_INVALID_PARAMETER; + } + + if ((Packet == NULL) || (Packet->Cdb == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Status = IScsiExecuteScsiCommand (This, Target, Lun, Packet); + if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_READY)) { + // + // Try to reinstate the session and re-execute the Scsi command. + // + Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This); + if (EFI_ERROR (IScsiSessionReinstatement (Private->Session))) { + return EFI_DEVICE_ERROR; + } + + Status = IScsiExecuteScsiCommand (This, Target, Lun, Packet); + } + + return Status; +} + + +/** + Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on + a SCSI channel. These can either be the list SCSI devices that are actually + present on the SCSI channel, or the list of legal Target Ids and LUNs for the + SCSI channel. Regardless, the caller of this function must probe the Target ID + and LUN returned to see if a SCSI device is actually present at that location + on the SCSI channel. + + @param[in] This The EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance. + @param[in, out] Target On input, a pointer to the Target ID of a SCSI + device present on the SCSI channel. On output, a + pointer to the Target ID of the next SCSI device + present on a SCSI channel. An input value of + 0xFFFFFFFF retrieves the Target ID of the first + SCSI device present on a SCSI channel. + @param[in, out] Lun On input, a pointer to the LUN of a SCSI device + present on the SCSI channel. On output, a pointer + to the LUN of the next SCSI device present on a + SCSI channel. + + @retval EFI_SUCCESS The Target ID and Lun of the next SCSI device on + the SCSI channel was returned in Target and Lun. + @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI + channel. + @retval EFI_INVALID_PARAMETER Target is not 0xFFFFFFFF,and Target and Lun were + not returned on a previous call to + GetNextDevice(). + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruGetNextTargetLun ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN OUT UINT8 **Target, + IN OUT UINT64 *Lun + ) +{ + ISCSI_DRIVER_DATA *Private; + ISCSI_SESSION_CONFIG_NVDATA *ConfigNvData; + UINT8 TargetId[TARGET_MAX_BYTES]; + + Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This); + ConfigNvData = &Private->Session->ConfigData->SessionConfigData; + + if ((*Target)[0] == 0 && (CompareMem (Lun, ConfigNvData->BootLun, sizeof (UINT64)) == 0)) { + // + // Only one pair per iSCSI Driver instance. + // + return EFI_NOT_FOUND; + } + + SetMem (TargetId, TARGET_MAX_BYTES, 0xFF); + if (CompareMem (*Target, TargetId, TARGET_MAX_BYTES) == 0) { + (*Target)[0] = 0; + CopyMem (Lun, ConfigNvData->BootLun, sizeof (UINT64)); + + return EFI_SUCCESS; + } + + return EFI_INVALID_PARAMETER; +} + + +/** + Allocate and build a device path node for a SCSI device on a SCSI channel. + + @param[in] This Protocol instance pointer. + @param[in] Target The Target ID of the SCSI device for which a + device path node is to be allocated and built. + @param[in] Lun The LUN of the SCSI device for which a device + path node is to be allocated and built. + @param[in, out] DevicePath A pointer to a single device path node that + describes the SCSI device specified by Target and + Lun. This function is responsible for allocating + the buffer DevicePath with the boot service + AllocatePool(). It is the caller's + responsibility to free DevicePath when the caller + is finished with DevicePath. + + @retval EFI_SUCCESS The device path node that describes the SCSI + device specified by Target and Lun was allocated + and returned in DevicePath. + @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does + not exist on the SCSI channel. + @retval EFI_INVALID_PARAMETER DevicePath is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate + DevicePath. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruBuildDevicePath ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN UINT8 *Target, + IN UINT64 Lun, + IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath + ) +{ + ISCSI_DRIVER_DATA *Private; + ISCSI_SESSION *Session; + ISCSI_SESSION_CONFIG_NVDATA *ConfigNvData; + ISCSI_CHAP_AUTH_CONFIG_NVDATA *AuthConfig; + EFI_DEV_PATH *Node; + UINTN DevPathNodeLen; + + if (DevicePath == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (Target[0] != 0) { + return EFI_NOT_FOUND; + } + + Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This); + Session = Private->Session; + ConfigNvData = &Session->ConfigData->SessionConfigData; + AuthConfig = Session->AuthData.CHAP.AuthConfig; + + if (CompareMem (&Lun, ConfigNvData->BootLun, sizeof (UINT64)) != 0) { + return EFI_NOT_FOUND; + } + + DevPathNodeLen = sizeof (ISCSI_DEVICE_PATH) + AsciiStrLen (ConfigNvData->TargetName) + 1; + Node = AllocateZeroPool (DevPathNodeLen); + if (Node == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Node->DevPath.Type = MESSAGING_DEVICE_PATH; + Node->DevPath.SubType = MSG_ISCSI_DP; + SetDevicePathNodeLength (&Node->DevPath, DevPathNodeLen); + + // + // 0 for TCP, others are reserved. + // + Node->Iscsi.NetworkProtocol = 0; + + Node->Iscsi.LoginOption = 0; + + switch (Session->AuthType) { + case ISCSI_AUTH_TYPE_NONE: + Node->Iscsi.LoginOption |= 0x0800; + break; + + case ISCSI_AUTH_TYPE_CHAP: + // + // Bit12: 0=CHAP_BI, 1=CHAP_UNI + // + if (AuthConfig->CHAPType == ISCSI_CHAP_UNI) { + Node->Iscsi.LoginOption |= 0x1000; + } + break; + + default: + break; + } + + CopyMem (&Node->Iscsi.Lun, ConfigNvData->BootLun, sizeof (UINT64)); + Node->Iscsi.TargetPortalGroupTag = Session->TargetPortalGroupTag; + AsciiStrCpyS ((CHAR8 *) Node + sizeof (ISCSI_DEVICE_PATH), AsciiStrLen (ConfigNvData->TargetName) + 1, ConfigNvData->TargetName); + + *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Node; + + return EFI_SUCCESS; +} + + +/** + Translate a device path node to a Target ID and LUN. + + @param[in] This Protocol instance pointer. + @param[in] DevicePath A pointer to the device path node that describes + a SCSI device on the SCSI channel. + @param[out] Target A pointer to the Target ID of a SCSI device on + the SCSI channel. + @param[out] Lun A pointer to the LUN of a SCSI device on the SCSI + channel. + + @retval EFI_SUCCESS DevicePath was successfully translated to a + Target ID and LUN, and they were returned in + Target and Lun. + @retval EFI_INVALID_PARAMETER DevicePath/Target/Lun is NULL. + @retval EFI_UNSUPPORTED This driver does not support the device path node + type in DevicePath. + @retval EFI_NOT_FOUND A valid translation does not exist from DevicePath + to a TargetID and LUN. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruGetTargetLun ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, + OUT UINT8 **Target, + OUT UINT64 *Lun + ) +{ + ISCSI_DRIVER_DATA *Private; + ISCSI_SESSION_CONFIG_NVDATA *ConfigNvData; + + if ((DevicePath == NULL) || (Target == NULL) || (Lun == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if ((DevicePath->Type != MESSAGING_DEVICE_PATH) || + (DevicePath->SubType != MSG_ISCSI_DP) || + (DevicePathNodeLength (DevicePath) <= sizeof (ISCSI_DEVICE_PATH)) + ) { + return EFI_UNSUPPORTED; + } + + Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (This); + ConfigNvData = &Private->Session->ConfigData->SessionConfigData; + + SetMem (*Target, TARGET_MAX_BYTES, 0xFF); + (*Target)[0] = 0; + + if (AsciiStrCmp (ConfigNvData->TargetName, (CHAR8 *) DevicePath + sizeof (ISCSI_DEVICE_PATH)) != 0) { + return EFI_UNSUPPORTED; + } + + CopyMem (Lun, ConfigNvData->BootLun, sizeof (UINT64)); + + return EFI_SUCCESS; +} + + +/** + Resets a SCSI channel. This operation resets all the SCSI devices connected to + the SCSI channel. + + @param[in] This Protocol instance pointer. + + @retval EFI_UNSUPPORTED It is not supported. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruResetChannel ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This + ) +{ + return EFI_UNSUPPORTED; +} + + +/** + Resets a SCSI device that is connected to a SCSI channel. + + @param[in] This Protocol instance pointer. + @param[in] Target The Target ID of the SCSI device to reset. + @param[in] Lun The LUN of the SCSI device to reset. + + @retval EFI_UNSUPPORTED It is not supported. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruResetTargetLun ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN UINT8 *Target, + IN UINT64 Lun + ) +{ + return EFI_UNSUPPORTED; +} + +/** + Retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. + + @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL + instance. + @param[in, out] Target (TARGET_MAX_BYTES) of a SCSI device present on + the SCSI channel. On output, a pointer to the + Target ID (an array of TARGET_MAX_BYTES) of the + next SCSI device present on a SCSI channel. + An input value of 0xF(all bytes in the array are 0xF) + in the Target array retrieves the Target ID of the + first SCSI device present on a SCSI channel. + + @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI + channel was returned in Target. + @retval EFI_INVALID_PARAMETER Target or Lun is NULL. + @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not + returned on a previous call to GetNextTarget(). + @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel. + +**/ +EFI_STATUS +EFIAPI +IScsiExtScsiPassThruGetNextTarget ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This, + IN OUT UINT8 **Target + ) +{ + UINT8 TargetId[TARGET_MAX_BYTES]; + + SetMem (TargetId, TARGET_MAX_BYTES, 0xFF); + + if (CompareMem (*Target, TargetId, TARGET_MAX_BYTES) == 0) { + (*Target)[0] = 0; + return EFI_SUCCESS; + } else if ((*Target)[0] == 0) { + return EFI_NOT_FOUND; + } else { + return EFI_INVALID_PARAMETER; + } +} + diff --git a/NetworkPkg/IScsiDxe/IScsiIbft.c b/NetworkPkg/IScsiDxe/IScsiIbft.c new file mode 100644 index 000000000..b2f82cb9b --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiIbft.c @@ -0,0 +1,545 @@ +/** @file + Implementation for iSCSI Boot Firmware Table publication. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + +BOOLEAN mIbftInstalled = FALSE; +UINTN mTableKey; + +/** + Initialize the header of the iSCSI Boot Firmware Table. + + @param[out] Header The header of the iSCSI Boot Firmware Table. + @param[in] OemId The OEM ID. + @param[in] OemTableId The OEM table ID for the iBFT. + +**/ +VOID +IScsiInitIbfTableHeader ( + OUT EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER *Header, + IN UINT8 *OemId, + IN UINT64 *OemTableId + ) +{ + Header->Signature = EFI_ACPI_3_0_ISCSI_BOOT_FIRMWARE_TABLE_SIGNATURE; + Header->Length = IBFT_HEAP_OFFSET; + Header->Revision = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_REVISION; + Header->Checksum = 0; + + CopyMem (Header->OemId, OemId, sizeof (Header->OemId)); + CopyMem (&Header->OemTableId, OemTableId, sizeof (UINT64)); +} + + +/** + Initialize the control section of the iSCSI Boot Firmware Table. + + @param[in] Table The ACPI table. + +**/ +VOID +IScsiInitControlSection ( + IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER *Table + ) +{ + EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE *Control; + UINTN NumOffset; + + Control = (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE *) (Table + 1); + + Control->Header.StructureId = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_ID; + Control->Header.Version = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_VERSION; + Control->Header.Length = (UINT16) sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE); + + // + // If in multipathing mode, enable the Boot Failover Flag. + // If in single path mode, disable it. Mix-model is not allowed. + // + // BUGBUG: if Boot Failover Flag is set to 1, the OS installer cannot + // find the iSCSI mapped disk. So still keep not set for single path mode. + // + if (mPrivate->EnableMpio) { + Control->Header.Flags = 0; + NumOffset = 2 * (mPrivate->MpioCount - mPrivate->Krb5MpioCount); + } else { + NumOffset = 2 * mPrivate->ValidSinglePathCount; + } + + // + // Each attempt occupies two offsets: one for the NIC section; + // the other for the Target section. + // + if (NumOffset > 4) { + // + // Need expand the control section if more than 2 NIC/Target attempts + // exist. + // + Control->Header.Length = (UINT16) (Control->Header.Length + (NumOffset - 4) * sizeof (UINT16)); + } +} + + +/** + Add one item into the heap. + + @param[in, out] Heap On input, the current address of the heap. On output, the address of + the heap after the item is added. + @param[in] Data The data to add into the heap. + @param[in] Len Length of the Data in byte. + +**/ +VOID +IScsiAddHeapItem ( + IN OUT UINT8 **Heap, + IN VOID *Data, + IN UINTN Len + ) +{ + // + // Add one byte for the NULL delimiter. + // + *Heap -= Len + 1; + + CopyMem (*Heap, Data, Len); + *(*Heap + Len) = 0; +} + + +/** + Fill the Initiator section of the iSCSI Boot Firmware Table. + + @param[in] Table The ACPI table. + @param[in, out] Heap The heap. + +**/ +VOID +IScsiFillInitiatorSection ( + IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER *Table, + IN OUT UINT8 **Heap + ) +{ + EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE *Control; + EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE *Initiator; + + Control = (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE *) (Table + 1); + + // + // Initiator section immediately follows the control section. + // + Initiator = (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE *) + ((UINT8 *) Control + IBFT_ROUNDUP (Control->Header.Length)); + + Control->InitiatorOffset = (UINT16) ((UINTN) Initiator - (UINTN) Table); + + Initiator->Header.StructureId = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_ID; + Initiator->Header.Version = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_VERSION; + Initiator->Header.Length = (UINT16) sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE); + Initiator->Header.Flags = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_FLAG_BLOCK_VALID | + EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_FLAG_BOOT_SELECTED; + + // + // Fill the iSCSI Initiator Name into the heap. + // + IScsiAddHeapItem (Heap, mPrivate->InitiatorName, mPrivate->InitiatorNameLength - 1); + + Initiator->IScsiNameLength = (UINT16) (mPrivate->InitiatorNameLength - 1); + Initiator->IScsiNameOffset = (UINT16) ((UINTN) *Heap - (UINTN) Table); +} + + +/** + Map the v4 IP address into v6 IP address. + + @param[in] V4 The v4 IP address. + @param[out] V6 The v6 IP address. + +**/ +VOID +IScsiMapV4ToV6Addr ( + IN EFI_IPv4_ADDRESS *V4, + OUT EFI_IPv6_ADDRESS *V6 + ) +{ + UINTN Index; + + ZeroMem (V6, sizeof (EFI_IPv6_ADDRESS)); + + V6->Addr[10] = 0xff; + V6->Addr[11] = 0xff; + + for (Index = 0; Index < 4; Index++) { + V6->Addr[12 + Index] = V4->Addr[Index]; + } +} + + +/** + Fill the NIC and target sections in iSCSI Boot Firmware Table. + + @param[in] Table The buffer of the ACPI table. + @param[in, out] Heap The heap buffer used to store the variable length + parameters such as iSCSI name. + +**/ +VOID +IScsiFillNICAndTargetSections ( + IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER *Table, + IN OUT UINT8 **Heap + ) +{ + EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE *Control; + EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE *Nic; + EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE *Target; + ISCSI_SESSION_CONFIG_NVDATA *NvData; + ISCSI_CHAP_AUTH_CONFIG_NVDATA *AuthConfig; + UINT16 *SectionOffset; + UINTN Index; + UINT16 Length; + LIST_ENTRY *Entry; + ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt; + ISCSI_NIC_INFO *NicInfo; + BOOLEAN Flag; + + // + // Get the offset of the first Nic and Target section. + // + Control = (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE *) (Table + 1); + Nic = (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE *) ((UINTN) Table + + Control->InitiatorOffset + IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE))); + Target = (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE *) ((UINTN) Nic + + IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE))); + + SectionOffset = &Control->NIC0Offset; + + Index = 0; + Flag = TRUE; + + NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) { + if (Index == 0) { + // + // First entry should be boot selected entry. + // + Attempt = IScsiConfigGetAttemptByConfigIndex (mPrivate->BootSelectedIndex); + if (Attempt == NULL) { + // + // First boot selected entry can not be found. + // + break; + } + + ASSERT (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED); + + } else { + if (Index == 1 && Flag) { + Entry = mPrivate->AttemptConfigs.ForwardLink; + Flag = FALSE; + } + + Attempt = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + if (Attempt->AttemptConfigIndex == mPrivate->BootSelectedIndex) { + continue; + } + } + + if (Attempt->SessionConfigData.Enabled == ISCSI_DISABLED) { + continue; + } + + // + // Krb5 attempt will not be recorded in iBFT. + // + if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_KRB) { + continue; + } + + // + // If multipath mode is enabled, only the attempts in MPIO will be recorded in iBFT. + // + if (mPrivate->EnableMpio && Attempt->SessionConfigData.Enabled != ISCSI_ENABLED_FOR_MPIO) { + continue; + } + + // + // Only the valid attempts will be recorded. + // + if (!Attempt->ValidiBFTPath) { + continue; + } + + NvData = &Attempt->SessionConfigData; + AuthConfig = &Attempt->AuthConfigData.CHAP; + + // + // Fill the Nic section. + // + + Nic->Header.StructureId = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_ID; + Nic->Header.Version = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_VERSION; + Nic->Header.Length = (UINT16) sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE); + Nic->Header.Index = (UINT8) Index; + Nic->Header.Flags = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BLOCK_VALID | + EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_GLOBAL; + + if (Index == 0) { + Nic->Header.Flags |= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BOOT_SELECTED; + } + + if (NvData->InitiatorInfoFromDhcp) { + Nic->Origin = IpPrefixOriginDhcp; + } else { + Nic->Origin = IpPrefixOriginManual; + } + + if (NvData->IpMode == IP_MODE_IP4 || NvData->IpMode == IP_MODE_AUTOCONFIG) { + // + // Get the subnet mask prefix length. + // + Nic->SubnetMaskPrefixLength = IScsiGetSubnetMaskPrefixLength (&NvData->SubnetMask); + + // + // Map the various v4 addresses into v6 addresses. + // + IScsiMapV4ToV6Addr (&NvData->LocalIp.v4, &Nic->Ip); + IScsiMapV4ToV6Addr (&NvData->Gateway.v4, &Nic->Gateway); + IScsiMapV4ToV6Addr (&Attempt->PrimaryDns.v4, &Nic->PrimaryDns); + IScsiMapV4ToV6Addr (&Attempt->SecondaryDns.v4, &Nic->SecondaryDns); + IScsiMapV4ToV6Addr (&Attempt->DhcpServer.v4, &Nic->DhcpServer); + + } else if (NvData->IpMode == IP_MODE_IP6 || NvData->IpMode == IP_MODE_AUTOCONFIG) { + + Nic->SubnetMaskPrefixLength = NvData->PrefixLength; + CopyMem (&Nic->Ip, &NvData->LocalIp, sizeof (EFI_IPv6_ADDRESS)); + CopyMem (&Nic->Gateway, &NvData->Gateway, sizeof (EFI_IPv6_ADDRESS)); + CopyMem (&Nic->PrimaryDns, &Attempt->PrimaryDns, sizeof (EFI_IPv6_ADDRESS)); + CopyMem (&Nic->SecondaryDns, &Attempt->SecondaryDns, sizeof (EFI_IPv6_ADDRESS)); + CopyMem (&Nic->DhcpServer, &Attempt->DhcpServer, sizeof (EFI_IPv6_ADDRESS)); + + } else { + ASSERT (FALSE); + } + + // + // Get Nic Info: VLAN tag, Mac address, PCI location. + // + NicInfo = IScsiGetNicInfoByIndex (Attempt->NicIndex); + ASSERT (NicInfo != NULL); + + Nic->VLanTag = NicInfo->VlanId; + CopyMem (Nic->Mac, &NicInfo->PermanentAddress, sizeof (Nic->Mac)); + Nic->PciLocation = (UINT16) ((NicInfo->BusNumber << 8) | + (NicInfo->DeviceNumber << 3) | NicInfo->FunctionNumber); + *SectionOffset = (UINT16) ((UINTN) Nic - (UINTN) Table); + SectionOffset++; + + // + // Fill the Target section. + // + + Target->Header.StructureId = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_ID; + Target->Header.Version = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_VERSION; + Target->Header.Length = (UINT16) sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE); + Target->Header.Index = (UINT8) Index; + Target->Header.Flags = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_FLAG_BLOCK_VALID; + + if (Index == 0) { + Target->Header.Flags |= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_FLAG_BOOT_SELECTED; + } + + Target->Port = NvData->TargetPort; + + if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) { + if (AuthConfig->CHAPType == ISCSI_CHAP_UNI) { + Target->CHAPType = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_CHAP_TYPE_CHAP; + } else if (AuthConfig->CHAPType == ISCSI_CHAP_MUTUAL) { + Target->CHAPType = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_CHAP_TYPE_MUTUAL_CHAP; + } + } else if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_NONE) { + Target->CHAPType = EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_CHAP_TYPE_NO_CHAP; + } + + Target->NicIndex = (UINT8) Index; + + if (NvData->IpMode == IP_MODE_IP4 || NvData->IpMode == IP_MODE_AUTOCONFIG) { + IScsiMapV4ToV6Addr (&NvData->TargetIp.v4, &Target->Ip); + } else if (NvData->IpMode == IP_MODE_IP6 || NvData->IpMode == IP_MODE_AUTOCONFIG) { + CopyMem (&Target->Ip, &NvData->TargetIp, sizeof (EFI_IPv6_ADDRESS)); + } else { + ASSERT (FALSE); + } + + CopyMem (Target->BootLun, NvData->BootLun, sizeof (Target->BootLun)); + + // + // Target iSCSI Name, CHAP name/secret, reverse CHAP name/secret. + // + Length = (UINT16) AsciiStrLen (NvData->TargetName); + IScsiAddHeapItem (Heap, NvData->TargetName, Length); + + Target->IScsiNameLength = Length; + Target->IScsiNameOffset = (UINT16) ((UINTN) *Heap - (UINTN) Table); + + if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) { + // + // CHAP Name + // + Length = (UINT16) AsciiStrLen (AuthConfig->CHAPName); + IScsiAddHeapItem (Heap, AuthConfig->CHAPName, Length); + Target->CHAPNameLength = Length; + Target->CHAPNameOffset = (UINT16) ((UINTN) *Heap - (UINTN) Table); + + // + // CHAP Secret + // + Length = (UINT16) AsciiStrLen (AuthConfig->CHAPSecret); + IScsiAddHeapItem (Heap, AuthConfig->CHAPSecret, Length); + Target->CHAPSecretLength = Length; + Target->CHAPSecretOffset = (UINT16) ((UINTN) *Heap - (UINTN) Table); + + if (Target->CHAPType == EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_CHAP_TYPE_MUTUAL_CHAP) { + // + // Reverse CHAP Name. + // + Length = (UINT16) AsciiStrLen (AuthConfig->ReverseCHAPName); + IScsiAddHeapItem (Heap, AuthConfig->ReverseCHAPName, Length); + Target->ReverseCHAPNameLength = Length; + Target->ReverseCHAPNameOffset = (UINT16) ((UINTN) *Heap - (UINTN) Table); + + // + // Reverse CHAP Secret. + // + Length = (UINT16) AsciiStrLen (AuthConfig->ReverseCHAPSecret); + IScsiAddHeapItem (Heap, AuthConfig->ReverseCHAPSecret, Length); + Target->ReverseCHAPSecretLength = Length; + Target->ReverseCHAPSecretOffset = (UINT16) ((UINTN) *Heap - (UINTN) Table); + } + } + + *SectionOffset = (UINT16) ((UINTN) Target - (UINTN) Table); + SectionOffset++; + + // + // Advance to the next NIC/Target pair. + // + Nic = (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE *) ((UINTN) Target + + IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE))); + Target = (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE *) ((UINTN) Nic + + IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE))); + + Index++; + } +} + + +/** + Publish and remove the iSCSI Boot Firmware Table according to the iSCSI + session status. + +**/ +VOID +IScsiPublishIbft ( + IN VOID + ) +{ + EFI_STATUS Status; + EFI_ACPI_TABLE_PROTOCOL *AcpiTableProtocol; + EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER *Table; + EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp; + EFI_ACPI_DESCRIPTION_HEADER *Rsdt; + EFI_ACPI_DESCRIPTION_HEADER *Xsdt; + UINT8 *Heap; + UINT8 Checksum; + + Rsdt = NULL; + Xsdt = NULL; + + Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **) &AcpiTableProtocol); + if (EFI_ERROR (Status)) { + return ; + } + + // + // Find ACPI table RSD_PTR from the system table. + // + Status = EfiGetSystemConfigurationTable (&gEfiAcpiTableGuid, (VOID **) &Rsdp); + if (EFI_ERROR (Status)) { + Status = EfiGetSystemConfigurationTable (&gEfiAcpi10TableGuid, (VOID **) &Rsdp); + } + + if (EFI_ERROR (Status) || (Rsdp == NULL)) { + return ; + } else if (Rsdp->Revision >= EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER_REVISION && Rsdp->XsdtAddress != 0) { + Xsdt = (EFI_ACPI_DESCRIPTION_HEADER *) (UINTN) Rsdp->XsdtAddress; + } else if (Rsdp->RsdtAddress != 0) { + Rsdt = (EFI_ACPI_DESCRIPTION_HEADER *) (UINTN) Rsdp->RsdtAddress; + } + + if ((Xsdt == NULL) && (Rsdt == NULL)) { + return ; + } + + if (mIbftInstalled) { + Status = AcpiTableProtocol->UninstallAcpiTable ( + AcpiTableProtocol, + mTableKey + ); + if (EFI_ERROR (Status)) { + return ; + } + mIbftInstalled = FALSE; + } + + // + // If there is no valid attempt configuration, just return. + // + if ((!mPrivate->EnableMpio && mPrivate->ValidSinglePathCount == 0) || + (mPrivate->EnableMpio && mPrivate->MpioCount <= mPrivate->Krb5MpioCount)) { + return ; + } + + // + // Allocate 4k bytes to hold the ACPI table. + // + Table = AllocateZeroPool (IBFT_MAX_SIZE); + if (Table == NULL) { + return ; + } + + Heap = (UINT8 *) Table + IBFT_HEAP_OFFSET; + + // + // Fill in the various section of the iSCSI Boot Firmware Table. + // + if (Rsdp->Revision >= EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER_REVISION) { + IScsiInitIbfTableHeader (Table, Xsdt->OemId, &Xsdt->OemTableId); + } else { + IScsiInitIbfTableHeader (Table, Rsdt->OemId, &Rsdt->OemTableId); + } + + IScsiInitControlSection (Table); + IScsiFillInitiatorSection (Table, &Heap); + IScsiFillNICAndTargetSections (Table, &Heap); + + Checksum = CalculateCheckSum8((UINT8 *)Table, Table->Length); + Table->Checksum = Checksum; + + // + // Install or update the iBFT table. + // + Status = AcpiTableProtocol->InstallAcpiTable ( + AcpiTableProtocol, + Table, + Table->Length, + &mTableKey + ); + if (EFI_ERROR(Status)) { + return; + } + + mIbftInstalled = TRUE; + FreePool (Table); +} diff --git a/NetworkPkg/IScsiDxe/IScsiIbft.h b/NetworkPkg/IScsiDxe/IScsiIbft.h new file mode 100644 index 000000000..98eb56e5c --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiIbft.h @@ -0,0 +1,33 @@ +/** @file + Some extra definitions for iBFT. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ISCSI_IBFT_H_ +#define _ISCSI_IBFT_H_ + +#include +#include +#include +#include + +#define IBFT_TABLE_VAR_NAME L"iBFT" +#define IBFT_MAX_SIZE 4096 +#define IBFT_HEAP_OFFSET 2048 + +#define IBFT_ROUNDUP(size) NET_ROUNDUP ((size), EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_STRUCTURE_ALIGNMENT) + +/** + Publish and remove the iSCSI Boot Firmware Table according to the iSCSI + session status. + +**/ +VOID +IScsiPublishIbft ( + IN VOID + ); + +#endif diff --git a/NetworkPkg/IScsiDxe/IScsiImpl.h b/NetworkPkg/IScsiDxe/IScsiImpl.h new file mode 100644 index 000000000..387ab9765 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiImpl.h @@ -0,0 +1,202 @@ +/** @file + The shared head file for iSCSI driver. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ISCSI_IMPL_H_ +#define _ISCSI_IMPL_H_ + +#include + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "IScsiConfigNVDataStruc.h" +#include "IScsiDriver.h" +#include "IScsiProto.h" +#include "IScsiCHAP.h" +#include "IScsiDhcp.h" +#include "IScsiDhcp6.h" + +#include "IScsiIbft.h" +#include "IScsiMisc.h" +#include "IScsiDns.h" +#include "IScsiConfig.h" + +#define ISCSI_AUTH_INITIAL 0 + +#define ISCSI_SESSION_SIGNATURE SIGNATURE_32 ('I', 'S', 'S', 'N') +/// +/// 10 seconds +/// +#define ISCSI_GET_MAPPING_TIMEOUT 100000000U +/// +/// 3 seconds +/// +#define ISCSI_WAIT_IPSEC_TIMEOUT 30000000U + +struct _ISCSI_SESSION { + UINT32 Signature; + + ISCSI_DRIVER_DATA *Private; + ISCSI_ATTEMPT_CONFIG_NVDATA *ConfigData; + + UINT8 AuthType; + union { + ISCSI_CHAP_AUTH_DATA CHAP; + } AuthData; + + UINT8 State; + + UINT8 Isid[6]; + UINT16 Tsih; + + UINT32 CmdSN; + UINT32 ExpCmdSN; + UINT32 MaxCmdSN; + + UINT32 InitiatorTaskTag; + UINT16 NextCid; + + LIST_ENTRY Conns; + UINT32 NumConns; + + LIST_ENTRY TcbList; + + // + // Session-wide parameters + // + UINT16 TargetPortalGroupTag; + UINT32 MaxConnections; + BOOLEAN InitialR2T; + BOOLEAN ImmediateData; + UINT32 MaxBurstLength; + UINT32 FirstBurstLength; + UINT32 DefaultTime2Wait; + UINT32 DefaultTime2Retain; + UINT16 MaxOutstandingR2T; + BOOLEAN DataPDUInOrder; + BOOLEAN DataSequenceInOrder; + UINT8 ErrorRecoveryLevel; +}; + +#define ISCSI_CONNECTION_SIGNATURE SIGNATURE_32 ('I', 'S', 'C', 'N') + +struct _ISCSI_CONNECTION { + UINT32 Signature; + LIST_ENTRY Link; + + EFI_EVENT TimeoutEvent; + + ISCSI_SESSION *Session; + + UINT8 State; + UINT8 CurrentStage; + UINT8 NextStage; + + UINT8 AuthStep; + + BOOLEAN PartialReqSent; + BOOLEAN PartialRspRcvd; + + BOOLEAN TransitInitiated; + BOOLEAN ParamNegotiated; + + UINT16 Cid; + UINT32 ExpStatSN; + + // + // Queues... + // + NET_BUF_QUEUE RspQue; + + BOOLEAN Ipv6Flag; + TCP_IO TcpIo; + + // + // Connection-only parameters. + // + UINT32 MaxRecvDataSegmentLength; + ISCSI_DIGEST_TYPE HeaderDigest; + ISCSI_DIGEST_TYPE DataDigest; +}; + +#define ISCSI_DRIVER_DATA_SIGNATURE SIGNATURE_32 ('I', 'S', 'D', 'A') + +#define ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU(PassThru) \ + CR ( \ + PassThru, \ + ISCSI_DRIVER_DATA, \ + IScsiExtScsiPassThru, \ + ISCSI_DRIVER_DATA_SIGNATURE \ + ) + +#define ISCSI_DRIVER_DATA_FROM_IDENTIFIER(Identifier) \ + CR ( \ + Identifier, \ + ISCSI_DRIVER_DATA, \ + IScsiIdentifier, \ + ISCSI_DRIVER_DATA_SIGNATURE \ + ) + +struct _ISCSI_DRIVER_DATA { + UINT32 Signature; + EFI_HANDLE Image; + EFI_HANDLE Controller; + ISCSI_PRIVATE_PROTOCOL IScsiIdentifier; + + EFI_EVENT ExitBootServiceEvent; + + EFI_EXT_SCSI_PASS_THRU_PROTOCOL IScsiExtScsiPassThru; + EFI_EXT_SCSI_PASS_THRU_MODE ExtScsiPassThruMode; + EFI_HANDLE ExtScsiPassThruHandle; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_HANDLE ChildHandle; + ISCSI_SESSION *Session; +}; + +#endif diff --git a/NetworkPkg/IScsiDxe/IScsiInitiatorName.c b/NetworkPkg/IScsiDxe/IScsiInitiatorName.c new file mode 100644 index 000000000..0d332e5c6 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiInitiatorName.c @@ -0,0 +1,130 @@ +/** @file + Implementation for EFI iSCSI Initiator Name Protocol. + +Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + +EFI_ISCSI_INITIATOR_NAME_PROTOCOL gIScsiInitiatorName = { + IScsiGetInitiatorName, + IScsiSetInitiatorName +}; + + +/** + Retrieves the current set value of iSCSI Initiator Name. + + @param[in] This Pointer to the EFI_ISCSI_INITIATOR_NAME_PROTOCOL + instance. + @param[in, out] BufferSize Size of the buffer in bytes pointed to by Buffer / + Actual size of the variable data buffer. + @param[out] Buffer Pointer to the buffer for data to be read. + The data is a null-terminated UTF-8 encoded string. + The maximum length is 223 characters, including the null-terminator. + + @retval EFI_SUCCESS Data was successfully retrieved into the provided + buffer and the BufferSize was sufficient to handle + the iSCSI initiator name. + @retval EFI_BUFFER_TOO_SMALL BufferSize is too small for the result. BufferSize + will be updated with the size required to complete + the request. Buffer will not be affected. + @retval EFI_INVALID_PARAMETER BufferSize is NULL. BufferSize and Buffer will not + be affected. + @retval EFI_INVALID_PARAMETER Buffer is NULL. BufferSize and Buffer will not be + affected. + @retval EFI_DEVICE_ERROR The iSCSI initiator name could not be retrieved + due to a hardware error. + +**/ +EFI_STATUS +EFIAPI +IScsiGetInitiatorName ( + IN EFI_ISCSI_INITIATOR_NAME_PROTOCOL *This, + IN OUT UINTN *BufferSize, + OUT VOID *Buffer + ) +{ + EFI_STATUS Status; + + if ((BufferSize == NULL) || (Buffer == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Status = gRT->GetVariable ( + ISCSI_INITIATOR_NAME_VAR_NAME, + &gEfiIScsiInitiatorNameProtocolGuid, + NULL, + BufferSize, + Buffer + ); + + return Status; +} + + +/** + Sets the iSSI Initiator Name. + + @param[in] This Pointer to the EFI_ISCSI_INITIATOR_NAME_PROTOCOL + instance. + @param[in, out] BufferSize Size of the buffer in bytes pointed to by Buffer. + @param[in] Buffer Pointer to the buffer for data to be written. + The data is a null-terminated UTF-8 encoded string. + The maximum length is 223 characters, including the null-terminator. + + @retval EFI_SUCCESS Data was successfully stored by the protocol. + @retval EFI_UNSUPPORTED Platform policies do not allow for data to be + written. + @retval EFI_INVALID_PARAMETER BufferSize exceeds the maximum allowed limit. + BufferSize will be updated with the maximum size + required to complete the request. + @retval EFI_INVALID_PARAMETER Buffersize is NULL. BufferSize and Buffer will not + be affected. + @retval EFI_INVALID_PARAMETER Buffer is NULL. BufferSize and Buffer will not be + affected. + @retval EFI_DEVICE_ERROR The data could not be stored due to a hardware + error. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the data + @retval EFI_PROTOCOL_ERROR Input iSCSI initiator name does not adhere to RFC + 3720 + +**/ +EFI_STATUS +EFIAPI +IScsiSetInitiatorName ( + IN EFI_ISCSI_INITIATOR_NAME_PROTOCOL *This, + IN OUT UINTN *BufferSize, + IN VOID *Buffer + ) +{ + EFI_STATUS Status; + + if ((BufferSize == NULL) || (Buffer == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (*BufferSize > ISCSI_NAME_MAX_SIZE) { + *BufferSize = ISCSI_NAME_MAX_SIZE; + return EFI_INVALID_PARAMETER; + } + // + // Only support iqn iSCSI names. + // + Status = IScsiNormalizeName ((CHAR8 *) Buffer, *BufferSize - 1); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gRT->SetVariable ( + ISCSI_INITIATOR_NAME_VAR_NAME, + &gEfiIScsiInitiatorNameProtocolGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS, + *BufferSize, + Buffer + ); + + return Status; +} diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.c b/NetworkPkg/IScsiDxe/IScsiMisc.c new file mode 100644 index 000000000..38ad67917 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiMisc.c @@ -0,0 +1,2565 @@ +/** @file + Miscellaneous routines for iSCSI driver. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 IScsiHexString[] = "0123456789ABCDEFabcdef"; + +/** + Removes (trims) specified leading and trailing characters from a string. + + @param[in, out] Str Pointer to the null-terminated string to be trimmed. + On return, Str will hold the trimmed string. + + @param[in] CharC Character will be trimmed from str. + +**/ +VOID +IScsiStrTrim ( + IN OUT CHAR16 *Str, + IN CHAR16 CharC + ) +{ + CHAR16 *Pointer1; + CHAR16 *Pointer2; + + if (*Str == 0) { + return ; + } + + // + // Trim off the leading and trailing characters c + // + for (Pointer1 = Str; (*Pointer1 != 0) && (*Pointer1 == CharC); Pointer1++) { + ; + } + + Pointer2 = Str; + if (Pointer2 == Pointer1) { + while (*Pointer1 != 0) { + Pointer2++; + Pointer1++; + } + } else { + while (*Pointer1 != 0) { + *Pointer2 = *Pointer1; + Pointer1++; + Pointer2++; + } + *Pointer2 = 0; + } + + + for (Pointer1 = Str + StrLen(Str) - 1; Pointer1 >= Str && *Pointer1 == CharC; Pointer1--) { + ; + } + if (Pointer1 != Str + StrLen(Str) - 1) { + *(Pointer1 + 1) = 0; + } +} + +/** + Calculate the prefix length of the IPv4 subnet mask. + + @param[in] SubnetMask The IPv4 subnet mask. + + @return The prefix length of the subnet mask. + @retval 0 Other errors as indicated. + +**/ +UINT8 +IScsiGetSubnetMaskPrefixLength ( + IN EFI_IPv4_ADDRESS *SubnetMask + ) +{ + UINT8 Len; + UINT32 ReverseMask; + + // + // The SubnetMask is in network byte order. + // + ReverseMask = (SubnetMask->Addr[0] << 24) | (SubnetMask->Addr[1] << 16) | (SubnetMask->Addr[2] << 8) | (SubnetMask->Addr[3]); + + // + // Reverse it. + // + ReverseMask = ~ReverseMask; + + if ((ReverseMask & (ReverseMask + 1)) != 0) { + return 0; + } + + Len = 0; + + while (ReverseMask != 0) { + ReverseMask = ReverseMask >> 1; + Len++; + } + + return (UINT8) (32 - Len); +} + + +/** + Convert the hexadecimal encoded LUN string into the 64-bit LUN. + + @param[in] Str The hexadecimal encoded LUN string. + @param[out] Lun Storage to return the 64-bit LUN. + + @retval EFI_SUCCESS The 64-bit LUN is stored in Lun. + @retval EFI_INVALID_PARAMETER The string is malformatted. + +**/ +EFI_STATUS +IScsiAsciiStrToLun ( + IN CHAR8 *Str, + OUT UINT8 *Lun + ) +{ + UINTN Index, IndexValue, IndexNum, SizeStr; + CHAR8 TemStr[2]; + UINT8 TemValue; + UINT16 Value[4]; + + ZeroMem (Lun, 8); + ZeroMem (TemStr, 2); + ZeroMem ((UINT8 *) Value, sizeof (Value)); + SizeStr = AsciiStrLen (Str); + IndexValue = 0; + IndexNum = 0; + + for (Index = 0; Index < SizeStr; Index ++) { + TemStr[0] = Str[Index]; + TemValue = (UINT8) AsciiStrHexToUint64 (TemStr); + if (TemValue == 0 && TemStr[0] != '0') { + if ((TemStr[0] != '-') || (IndexNum == 0)) { + // + // Invalid Lun Char. + // + return EFI_INVALID_PARAMETER; + } + } + + if ((TemValue == 0) && (TemStr[0] == '-')) { + // + // Next Lun value. + // + if (++IndexValue >= 4) { + // + // Max 4 Lun value. + // + return EFI_INVALID_PARAMETER; + } + // + // Restart str index for the next lun value. + // + IndexNum = 0; + continue; + } + + if (++IndexNum > 4) { + // + // Each Lun Str can't exceed size 4, because it will be as UINT16 value. + // + return EFI_INVALID_PARAMETER; + } + + // + // Combine UINT16 value. + // + Value[IndexValue] = (UINT16) ((Value[IndexValue] << 4) + TemValue); + } + + for (Index = 0; Index <= IndexValue; Index ++) { + *((UINT16 *) &Lun[Index * 2]) = HTONS (Value[Index]); + } + + return EFI_SUCCESS; +} + +/** + Convert the 64-bit LUN into the hexadecimal encoded LUN string. + + @param[in] Lun The 64-bit LUN. + @param[out] Str The storage to return the hexadecimal encoded LUN string. + +**/ +VOID +IScsiLunToUnicodeStr ( + IN UINT8 *Lun, + OUT CHAR16 *Str + ) +{ + UINTN Index; + CHAR16 *TempStr; + + TempStr = Str; + + for (Index = 0; Index < 4; Index++) { + + if ((Lun[2 * Index] | Lun[2 * Index + 1]) == 0) { + CopyMem (TempStr, L"0-", sizeof (L"0-")); + } else { + TempStr[0] = (CHAR16) IScsiHexString[Lun[2 * Index] >> 4]; + TempStr[1] = (CHAR16) IScsiHexString[Lun[2 * Index] & 0x0F]; + TempStr[2] = (CHAR16) IScsiHexString[Lun[2 * Index + 1] >> 4]; + TempStr[3] = (CHAR16) IScsiHexString[Lun[2 * Index + 1] & 0x0F]; + TempStr[4] = L'-'; + TempStr[5] = 0; + + IScsiStrTrim (TempStr, L'0'); + } + + TempStr += StrLen (TempStr); + } + // + // Remove the last '-' + // + ASSERT (StrLen(Str) >= 1); + Str[StrLen (Str) - 1] = 0; + + for (Index = StrLen (Str) - 1; Index > 1; Index = Index - 2) { + if ((Str[Index] == L'0') && (Str[Index - 1] == L'-')) { + Str[Index - 1] = 0; + } else { + break; + } + } +} + +/** + Convert the formatted IP address into the binary IP address. + + @param[in] Str The UNICODE string. + @param[in] IpMode Indicates whether the IP address is v4 or v6. + @param[out] Ip The storage to return the ASCII string. + + @retval EFI_SUCCESS The binary IP address is returned in Ip. + @retval EFI_INVALID_PARAMETER The IP string is malformatted or IpMode is + invalid. + +**/ +EFI_STATUS +IScsiAsciiStrToIp ( + IN CHAR8 *Str, + IN UINT8 IpMode, + OUT EFI_IP_ADDRESS *Ip + ) +{ + EFI_STATUS Status; + + if (IpMode == IP_MODE_IP4 || IpMode == IP_MODE_AUTOCONFIG_IP4) { + return NetLibAsciiStrToIp4 (Str, &Ip->v4); + + } else if (IpMode == IP_MODE_IP6 || IpMode == IP_MODE_AUTOCONFIG_IP6) { + return NetLibAsciiStrToIp6 (Str, &Ip->v6); + + } else if (IpMode == IP_MODE_AUTOCONFIG) { + Status = NetLibAsciiStrToIp4 (Str, &Ip->v4); + if (!EFI_ERROR (Status)) { + return Status; + } + return NetLibAsciiStrToIp6 (Str, &Ip->v6); + + } + + return EFI_INVALID_PARAMETER; +} + +/** + Convert the mac address into a hexadecimal encoded "-" seperated string. + + @param[in] Mac The mac address. + @param[in] Len Length in bytes of the mac address. + @param[in] VlanId VLAN ID of the network device. + @param[out] Str The storage to return the mac string. + +**/ +VOID +IScsiMacAddrToStr ( + IN EFI_MAC_ADDRESS *Mac, + IN UINT32 Len, + IN UINT16 VlanId, + OUT CHAR16 *Str + ) +{ + UINT32 Index; + CHAR16 *String; + + for (Index = 0; Index < Len; Index++) { + Str[3 * Index] = (CHAR16) IScsiHexString[(Mac->Addr[Index] >> 4) & 0x0F]; + Str[3 * Index + 1] = (CHAR16) IScsiHexString[Mac->Addr[Index] & 0x0F]; + Str[3 * Index + 2] = L':'; + } + + String = &Str[3 * Index - 1] ; + if (VlanId != 0) { + String += UnicodeSPrint (String, 6 * sizeof (CHAR16), L"\\%04x", (UINTN) VlanId); + } + + *String = L'\0'; +} + +/** + Convert the binary encoded buffer into a hexadecimal encoded string. + + @param[in] BinBuffer The buffer containing the binary data. + @param[in] BinLength Length of the binary buffer. + @param[in, out] HexStr Pointer to the string. + @param[in, out] HexLength The length of the string. + + @retval EFI_SUCCESS The binary data is converted to the hexadecimal string + and the length of the string is updated. + @retval EFI_BUFFER_TOO_SMALL The string is too small. + @retval EFI_INVALID_PARAMETER The IP string is malformatted. + +**/ +EFI_STATUS +IScsiBinToHex ( + IN UINT8 *BinBuffer, + IN UINT32 BinLength, + IN OUT CHAR8 *HexStr, + IN OUT UINT32 *HexLength + ) +{ + UINTN Index; + + if ((HexStr == NULL) || (BinBuffer == NULL) || (BinLength == 0)) { + return EFI_INVALID_PARAMETER; + } + + if (((*HexLength) - 3) < BinLength * 2) { + *HexLength = BinLength * 2 + 3; + return EFI_BUFFER_TOO_SMALL; + } + + *HexLength = BinLength * 2 + 3; + // + // Prefix for Hex String. + // + HexStr[0] = '0'; + HexStr[1] = 'x'; + + for (Index = 0; Index < BinLength; Index++) { + HexStr[Index * 2 + 2] = IScsiHexString[BinBuffer[Index] >> 4]; + HexStr[Index * 2 + 3] = IScsiHexString[BinBuffer[Index] & 0xf]; + } + + HexStr[Index * 2 + 2] = '\0'; + + return EFI_SUCCESS; +} + + +/** + Convert the hexadecimal string into a binary encoded buffer. + + @param[in, out] BinBuffer The binary buffer. + @param[in, out] BinLength Length of the binary buffer. + @param[in] HexStr The hexadecimal string. + + @retval EFI_SUCCESS The hexadecimal string is converted into a binary + encoded buffer. + @retval EFI_BUFFER_TOO_SMALL The binary buffer is too small to hold the converted data. + +**/ +EFI_STATUS +IScsiHexToBin ( + IN OUT UINT8 *BinBuffer, + IN OUT UINT32 *BinLength, + IN CHAR8 *HexStr + ) +{ + UINTN Index; + UINTN Length; + UINT8 Digit; + CHAR8 TemStr[2]; + + ZeroMem (TemStr, sizeof (TemStr)); + + // + // Find out how many hex characters the string has. + // + if ((HexStr[0] == '0') && ((HexStr[1] == 'x') || (HexStr[1] == 'X'))) { + HexStr += 2; + } + + Length = AsciiStrLen (HexStr); + + for (Index = 0; Index < Length; Index ++) { + TemStr[0] = HexStr[Index]; + Digit = (UINT8) AsciiStrHexToUint64 (TemStr); + if (Digit == 0 && TemStr[0] != '0') { + // + // Invalid Lun Char. + // + break; + } + if ((Index & 1) == 0) { + BinBuffer [Index/2] = Digit; + } else { + BinBuffer [Index/2] = (UINT8) ((BinBuffer [Index/2] << 4) + Digit); + } + } + + *BinLength = (UINT32) ((Index + 1)/2); + + return EFI_SUCCESS; +} + + +/** + Convert the decimal-constant string or hex-constant string into a numerical value. + + @param[in] Str String in decimal or hex. + + @return The numerical value. + +**/ +UINTN +IScsiNetNtoi ( + IN CHAR8 *Str + ) +{ + if ((Str[0] == '0') && ((Str[1] == 'x') || (Str[1] == 'X'))) { + Str += 2; + + return AsciiStrHexToUintn (Str); + } + + return AsciiStrDecimalToUintn (Str); +} + + +/** + Generate random numbers. + + @param[in, out] Rand The buffer to contain random numbers. + @param[in] RandLength The length of the Rand buffer. + +**/ +VOID +IScsiGenRandom ( + IN OUT UINT8 *Rand, + IN UINTN RandLength + ) +{ + UINT32 Random; + + while (RandLength > 0) { + Random = NET_RANDOM (NetRandomInitSeed ()); + *Rand++ = (UINT8) (Random); + RandLength--; + } +} + + +/** + Check whether UNDI protocol supports IPv6. + + @param[in] ControllerHandle Controller handle. + @param[in] Image Handle of the image. + @param[out] Ipv6Support TRUE if UNDI supports IPv6. + + @retval EFI_SUCCESS Get the result whether UNDI supports IPv6 by NII or AIP protocol successfully. + @retval EFI_NOT_FOUND Don't know whether UNDI supports IPv6 since NII or AIP is not available. + +**/ +EFI_STATUS +IScsiCheckIpv6Support ( + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE Image, + OUT BOOLEAN *Ipv6Support + ) +{ + EFI_HANDLE Handle; + EFI_ADAPTER_INFORMATION_PROTOCOL *Aip; + EFI_STATUS Status; + EFI_GUID *InfoTypesBuffer; + UINTN InfoTypeBufferCount; + UINTN TypeIndex; + BOOLEAN Supported; + VOID *InfoBlock; + UINTN InfoBlockSize; + + EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *Nii; + + ASSERT (Ipv6Support != NULL); + + // + // Check whether the UNDI supports IPv6 by NII protocol. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiNetworkInterfaceIdentifierProtocolGuid_31, + (VOID **) &Nii, + Image, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (Status == EFI_SUCCESS) { + *Ipv6Support = Nii->Ipv6Supported; + return EFI_SUCCESS; + } + + // + // Get the NIC handle by SNP protocol. + // + Handle = NetLibGetSnpHandle (ControllerHandle, NULL); + if (Handle == NULL) { + return EFI_NOT_FOUND; + } + + Aip = NULL; + Status = gBS->HandleProtocol ( + Handle, + &gEfiAdapterInformationProtocolGuid, + (VOID *) &Aip + ); + if (EFI_ERROR (Status) || Aip == NULL) { + return EFI_NOT_FOUND; + } + + InfoTypesBuffer = NULL; + InfoTypeBufferCount = 0; + Status = Aip->GetSupportedTypes (Aip, &InfoTypesBuffer, &InfoTypeBufferCount); + if (EFI_ERROR (Status) || InfoTypesBuffer == NULL) { + FreePool (InfoTypesBuffer); + return EFI_NOT_FOUND; + } + + Supported = FALSE; + for (TypeIndex = 0; TypeIndex < InfoTypeBufferCount; TypeIndex++) { + if (CompareGuid (&InfoTypesBuffer[TypeIndex], &gEfiAdapterInfoUndiIpv6SupportGuid)) { + Supported = TRUE; + break; + } + } + + FreePool (InfoTypesBuffer); + if (!Supported) { + return EFI_NOT_FOUND; + } + + // + // We now have adapter information block. + // + InfoBlock = NULL; + InfoBlockSize = 0; + Status = Aip->GetInformation (Aip, &gEfiAdapterInfoUndiIpv6SupportGuid, &InfoBlock, &InfoBlockSize); + if (EFI_ERROR (Status) || InfoBlock == NULL) { + FreePool (InfoBlock); + return EFI_NOT_FOUND; + } + + *Ipv6Support = ((EFI_ADAPTER_INFO_UNDI_IPV6_SUPPORT *) InfoBlock)->Ipv6Support; + FreePool (InfoBlock); + + return EFI_SUCCESS; +} + +/** + Record the NIC info in global structure. + + @param[in] Controller The handle of the controller. + @param[in] Image Handle of the image. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_OUT_OF_RESOURCES Do not have sufficient resources to finish this + operation. + +**/ +EFI_STATUS +IScsiAddNic ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE Image + ) +{ + EFI_STATUS Status; + ISCSI_NIC_INFO *NicInfo; + LIST_ENTRY *Entry; + EFI_MAC_ADDRESS MacAddr; + UINTN HwAddressSize; + UINT16 VlanId; + + // + // Get MAC address of this network device. + // + Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Get VLAN ID of this network device. + // + VlanId = NetLibGetVlanId (Controller); + + // + // Check whether the NIC info already exists. Return directly if so. + // + NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) { + NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link); + if (NicInfo->HwAddressSize == HwAddressSize && + CompareMem (&NicInfo->PermanentAddress, MacAddr.Addr, HwAddressSize) == 0 && + NicInfo->VlanId == VlanId) { + mPrivate->CurrentNic = NicInfo->NicIndex; + + // + // Set IPv6 available flag. + // + Status = IScsiCheckIpv6Support (Controller, Image, &NicInfo->Ipv6Available); + if (EFI_ERROR (Status)) { + // + // Fail to get the data whether UNDI supports IPv6. + // Set default value to TRUE. + // + NicInfo->Ipv6Available = TRUE; + } + + return EFI_SUCCESS; + } + + if (mPrivate->MaxNic < NicInfo->NicIndex) { + mPrivate->MaxNic = NicInfo->NicIndex; + } + } + + // + // Record the NIC info in private structure. + // + NicInfo = AllocateZeroPool (sizeof (ISCSI_NIC_INFO)); + if (NicInfo == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (&NicInfo->PermanentAddress, MacAddr.Addr, HwAddressSize); + NicInfo->HwAddressSize = (UINT32) HwAddressSize; + NicInfo->VlanId = VlanId; + NicInfo->NicIndex = (UINT8) (mPrivate->MaxNic + 1); + mPrivate->MaxNic = NicInfo->NicIndex; + + // + // Set IPv6 available flag. + // + Status = IScsiCheckIpv6Support (Controller, Image, &NicInfo->Ipv6Available); + if (EFI_ERROR (Status)) { + // + // Fail to get the data whether UNDI supports IPv6. + // Set default value to TRUE. + // + NicInfo->Ipv6Available = TRUE; + } + + // + // Get the PCI location. + // + IScsiGetNICPciLocation ( + Controller, + &NicInfo->BusNumber, + &NicInfo->DeviceNumber, + &NicInfo->FunctionNumber + ); + + InsertTailList (&mPrivate->NicInfoList, &NicInfo->Link); + mPrivate->NicCount++; + + mPrivate->CurrentNic = NicInfo->NicIndex; + return EFI_SUCCESS; +} + + +/** + Delete the recorded NIC info from global structure. Also delete corresponding + attempts. + + @param[in] Controller The handle of the controller. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_NOT_FOUND The NIC info to be deleted is not recorded. + +**/ +EFI_STATUS +IScsiRemoveNic ( + IN EFI_HANDLE Controller + ) +{ + EFI_STATUS Status; + ISCSI_NIC_INFO *NicInfo; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + ISCSI_NIC_INFO *ThisNic; + EFI_MAC_ADDRESS MacAddr; + UINTN HwAddressSize; + UINT16 VlanId; + + // + // Get MAC address of this network device. + // + Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Get VLAN ID of this network device. + // + VlanId = NetLibGetVlanId (Controller); + + // + // Check whether the NIC information exists. + // + ThisNic = NULL; + + NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) { + NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link); + if (NicInfo->HwAddressSize == HwAddressSize && + CompareMem (&NicInfo->PermanentAddress, MacAddr.Addr, HwAddressSize) == 0 && + NicInfo->VlanId == VlanId) { + + ThisNic = NicInfo; + break; + } + } + + if (ThisNic == NULL) { + return EFI_NOT_FOUND; + } + + mPrivate->CurrentNic = ThisNic->NicIndex; + + RemoveEntryList (&ThisNic->Link); + FreePool (ThisNic); + mPrivate->NicCount--; + + // + // Remove all attempts related to this NIC. + // + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &mPrivate->AttemptConfigs) { + AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link); + if (AttemptConfigData->NicIndex == mPrivate->CurrentNic) { + RemoveEntryList (&AttemptConfigData->Link); + mPrivate->AttemptCount--; + + if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO && mPrivate->MpioCount > 0) { + if (--mPrivate->MpioCount == 0) { + mPrivate->EnableMpio = FALSE; + } + + if (AttemptConfigData->AuthenticationType == ISCSI_AUTH_TYPE_KRB && mPrivate->Krb5MpioCount > 0) { + mPrivate->Krb5MpioCount--; + } + + } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED && mPrivate->SinglePathCount > 0) { + mPrivate->SinglePathCount--; + + if (mPrivate->ValidSinglePathCount > 0) { + mPrivate->ValidSinglePathCount--; + } + } + + FreePool (AttemptConfigData); + } + } + + return EFI_SUCCESS; +} + +/** + Create and initialize the Attempts. + + @param[in] AttemptNum The number of Attempts will be created. + + @retval EFI_SUCCESS The Attempts have been created successfully. + @retval Others Failed to create the Attempt. + +**/ +EFI_STATUS +IScsiCreateAttempts ( + IN UINTN AttemptNum +) +{ + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + ISCSI_SESSION_CONFIG_NVDATA *ConfigData; + UINT8 *AttemptConfigOrder; + UINTN AttemptConfigOrderSize; + UINT8 *AttemptOrderTmp; + UINTN TotalNumber; + UINT8 Index; + EFI_STATUS Status; + + for (Index = 1; Index <= AttemptNum; Index ++) { + // + // Get the initialized attempt order. This is used to essure creating attempts by order. + // + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"InitialAttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + TotalNumber = AttemptConfigOrderSize / sizeof (UINT8); + if (TotalNumber == AttemptNum) { + Status = EFI_SUCCESS; + break; + } + TotalNumber++; + + // + // Append the new created attempt to the end. + // + AttemptOrderTmp = AllocateZeroPool (TotalNumber * sizeof (UINT8)); + if (AttemptOrderTmp == NULL) { + if (AttemptConfigOrder != NULL) { + FreePool (AttemptConfigOrder); + } + return EFI_OUT_OF_RESOURCES; + } + + if (AttemptConfigOrder != NULL) { + CopyMem (AttemptOrderTmp, AttemptConfigOrder, AttemptConfigOrderSize); + FreePool (AttemptConfigOrder); + } + + AttemptOrderTmp[TotalNumber - 1] = Index; + AttemptConfigOrder = AttemptOrderTmp; + AttemptConfigOrderSize = TotalNumber * sizeof (UINT8); + + Status = gRT->SetVariable ( + L"InitialAttemptOrder", + &gIScsiConfigGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE, + AttemptConfigOrderSize, + AttemptConfigOrder + ); + FreePool (AttemptConfigOrder); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, + "%a: Failed to set 'InitialAttemptOrder' with Guid (%g): " + "%r\n", + __FUNCTION__, &gIScsiConfigGuid, Status)); + return Status; + } + + // + // Create new Attempt + // + AttemptConfigData = AllocateZeroPool (sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA)); + if (AttemptConfigData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + ConfigData = &AttemptConfigData->SessionConfigData; + ConfigData->TargetPort = ISCSI_WELL_KNOWN_PORT; + ConfigData->ConnectTimeout = CONNECT_DEFAULT_TIMEOUT; + ConfigData->ConnectRetryCount = CONNECT_MIN_RETRY; + + AttemptConfigData->AuthenticationType = ISCSI_AUTH_TYPE_CHAP; + AttemptConfigData->AuthConfigData.CHAP.CHAPType = ISCSI_CHAP_UNI; + // + // Configure the Attempt index and set variable. + // + AttemptConfigData->AttemptConfigIndex = Index; + + // + // Set the attempt name according to the order. + // + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Attempt %d", + (UINTN) AttemptConfigData->AttemptConfigIndex + ); + UnicodeStrToAsciiStrS (mPrivate->PortString, AttemptConfigData->AttemptName, ATTEMPT_NAME_SIZE); + + Status = gRT->SetVariable ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + ISCSI_CONFIG_VAR_ATTR, + sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA), + AttemptConfigData + ); + FreePool (AttemptConfigData); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, + "%a: Failed to set variable (mPrivate->PortString) with Guid (%g): " + "%r\n", + __FUNCTION__, &gEfiIScsiInitiatorNameProtocolGuid, Status)); + return Status; + } + } + + return EFI_SUCCESS; +} + +/** + Create the iSCSI configuration Keywords for each attempt. You can find the keywords + defined in the "x-UEFI-ns" namespace (http://www.uefi.org/confignamespace). + + @param[in] KeywordNum The number Sets of Keywords will be created. + + @retval EFI_SUCCESS The operation is completed. + @retval Others Failed to create the Keywords. + +**/ +EFI_STATUS +IScsiCreateKeywords ( + IN UINTN KeywordNum +) +{ + VOID *StartOpCodeHandle; + EFI_IFR_GUID_LABEL *StartLabel; + VOID *EndOpCodeHandle; + EFI_IFR_GUID_LABEL *EndLabel; + UINTN Index; + EFI_STRING_ID StringToken; + CHAR16 StringId[64]; + CHAR16 KeywordId[32]; + EFI_STATUS Status; + + Status = IScsiCreateOpCode ( + KEYWORD_ENTRY_LABEL, + &StartOpCodeHandle, + &StartLabel, + &EndOpCodeHandle, + &EndLabel + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + + for (Index = 1; Index <= KeywordNum; Index ++) { + // + // Create iSCSIAttemptName Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_ATTEMPTT_NAME_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIAttemptName:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_ATTEMPT_NAME_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_ATTEMPT_NAME_VAR_OFFSET + ATTEMPT_NAME_SIZE * (Index - 1) * sizeof (CHAR16)), + StringToken, + StringToken, + EFI_IFR_FLAG_READ_ONLY, + 0, + 0, + ATTEMPT_NAME_SIZE, + NULL + ); + + // + // Create iSCSIBootEnable Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_MODE_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIBootEnable:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateNumericOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_BOOTENABLE_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_BOOTENABLE_VAR_OFFSET + (Index - 1)), + StringToken, + StringToken, + 0, + EFI_IFR_NUMERIC_SIZE_1, + 0, + 2, + 0, + NULL + ); + + // + // Create iSCSIIpAddressType Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_IP_MODE_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIIpAddressType:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateNumericOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_ADDRESS_TYPE_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_ADDRESS_TYPE_VAR_OFFSET + (Index - 1)), + StringToken, + StringToken, + 0, + EFI_IFR_NUMERIC_SIZE_1, + 0, + 2, + 0, + NULL + ); + + // + // Create iSCSIConnectRetry Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CONNECT_RETRY_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIConnectRetry:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateNumericOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_CONNECT_RETRY_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_CONNECT_RETRY_VAR_OFFSET + (Index - 1)), + StringToken, + StringToken, + 0, + EFI_IFR_NUMERIC_SIZE_1, + 0, + 16, + 0, + NULL + ); + + // + // Create iSCSIConnectTimeout Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CONNECT_TIMEOUT_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIConnectTimeout:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateNumericOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_CONNECT_TIMEOUT_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_CONNECT_TIMEOUT_VAR_OFFSET + 2 * (Index - 1)), + StringToken, + StringToken, + 0, + EFI_IFR_NUMERIC_SIZE_2, + CONNECT_MIN_TIMEOUT, + CONNECT_MAX_TIMEOUT, + 0, + NULL + ); + + // + // Create ISID Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_ISID_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIISID:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_ISID_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_ISID_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)), + StringToken, + STRING_TOKEN (STR_ISCSI_ISID_HELP), + 0, + 0, + ISID_CONFIGURABLE_MIN_LEN, + ISID_CONFIGURABLE_STORAGE, + NULL + ); + + // + // Create iSCSIInitiatorInfoViaDHCP Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_INITIATOR_VIA_DHCP_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIInitiatorInfoViaDHCP:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateNumericOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_INITIATOR_VIA_DHCP_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_INITIATOR_VIA_DHCP_VAR_OFFSET + (Index - 1)), + StringToken, + StringToken, + 0, + 0, + 0, + 1, + 0, + NULL + ); + + // + // Create iSCSIInitiatorIpAddress Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_INITIATOR_IP_ADDRESS_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIInitiatorIpAddress:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_INITIATOR_IP_ADDRESS_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_INITIATOR_IP_ADDRESS_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)), + StringToken, + StringToken, + 0, + 0, + IP4_MIN_SIZE, + IP4_STR_MAX_SIZE, + NULL + ); + + // + // Create iSCSIInitiatorNetmask Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_INITIATOR_NET_MASK_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIInitiatorNetmask:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_INITIATOR_NET_MASK_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_INITIATOR_NET_MASK_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)), + StringToken, + StringToken, + 0, + 0, + IP4_MIN_SIZE, + IP4_STR_MAX_SIZE, + NULL + ); + + // + // Create iSCSIInitiatorGateway Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_INITIATOR_GATE_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIInitiatorGateway:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_INITIATOR_GATE_WAY_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_INITIATOR_GATE_WAY_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)), + StringToken, + StringToken, + 0, + 0, + IP4_MIN_SIZE, + IP4_STR_MAX_SIZE, + NULL + ); + + // + // Create iSCSITargetInfoViaDHCP Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_TARGET_VIA_DHCP_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSITargetInfoViaDHCP:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateNumericOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_TARGET_VIA_DHCP_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_TARGET_VIA_DHCP_VAR_OFFSET + (Index - 1)), + StringToken, + StringToken, + 0, + 0, + 0, + 1, + 0, + NULL + ); + + // + // Create iSCSITargetTcpPort Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_TARGET_TCP_PORT_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSITargetTcpPort:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateNumericOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_TARGET_TCP_PORT_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_TARGET_TCP_PORT_VAR_OFFSET + 2 * (Index - 1)), + StringToken, + StringToken, + 0, + EFI_IFR_NUMERIC_SIZE_2, + TARGET_PORT_MIN_NUM, + TARGET_PORT_MAX_NUM, + 0, + NULL + ); + + // + // Create iSCSITargetName Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_TARGET_NAME_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSITargetName:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_TARGET_NAME_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_TARGET_NAME_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)), + StringToken, + StringToken, + 0, + 0, + ISCSI_NAME_IFR_MIN_SIZE, + ISCSI_NAME_IFR_MAX_SIZE, + NULL + ); + + // + // Create iSCSITargetIpAddress Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_TARGET_IP_ADDRESS_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSITargetIpAddress:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_TARGET_IP_ADDRESS_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_TARGET_IP_ADDRESS_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)), + StringToken, + StringToken, + 0, + 0, + IP_MIN_SIZE, + IP_STR_MAX_SIZE, + NULL + ); + + // + // Create iSCSILUN Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_LUN_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSILUN:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_LUN_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_LUN_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)), + StringToken, + StringToken, + 0, + 0, + LUN_MIN_SIZE, + LUN_MAX_SIZE, + NULL + ); + + // + // Create iSCSIAuthenticationMethod Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_AUTHENTICATION_METHOD_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIAuthenticationMethod:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateNumericOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_AUTHENTICATION_METHOD_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_AUTHENTICATION_METHOD_VAR_OFFSET + (Index - 1)), + StringToken, + StringToken, + 0, + 0, + 0, + 1, + 0, + NULL + ); + + // + // Create iSCSIChapType Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CHARTYPE_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIChapType:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateNumericOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_CHARTYPE_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_CHARTYPE_VAR_OFFSET + (Index - 1)), + StringToken, + StringToken, + 0, + 0, + 0, + 1, + 0, + NULL + ); + + // + // Create iSCSIChapUsername Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CHAR_USER_NAME_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIChapUsername:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_CHAR_USER_NAME_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_CHAR_USER_NAME_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)), + StringToken, + StringToken, + 0, + 0, + 0, + ISCSI_CHAP_NAME_MAX_LEN, + NULL + ); + + // + // Create iSCSIChapSecret Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CHAR_SECRET_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIChapSecret:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_CHAR_SECRET_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_CHAR_SECRET_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)), + StringToken, + StringToken, + 0, + 0, + ISCSI_CHAP_SECRET_MIN_LEN, + ISCSI_CHAP_SECRET_MAX_LEN, + NULL + ); + + // + // Create iSCSIReverseChapUsername Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CHAR_REVERSE_USER_NAME_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIReverseChapUsername:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_CHAR_REVERSE_USER_NAME_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_CHAR_REVERSE_USER_NAME_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)), + StringToken, + StringToken, + 0, + 0, + 0, + ISCSI_CHAP_NAME_MAX_LEN, + NULL + ); + + // + // Create iSCSIReverseChapSecret Keyword. + // + UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CHAR_REVERSE_SECRET_PROMPT%d", Index); + StringToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + StringId, + NULL + ); + UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIReverseChapSecret:%d", Index); + HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns"); + HiiCreateStringOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (ATTEMPT_CHAR_REVERSE_SECRET_QUESTION_ID + (Index - 1)), + CONFIGURATION_VARSTORE_ID, + (UINT16) (ATTEMPT_CHAR_REVERSE_SECRET_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)), + StringToken, + StringToken, + 0, + 0, + ISCSI_CHAP_SECRET_MIN_LEN, + ISCSI_CHAP_SECRET_MAX_LEN, + NULL + ); + } + + Status = HiiUpdateForm ( + mCallbackInfo->RegisteredHandle, // HII handle + &gIScsiConfigGuid, // Formset GUID + FORMID_ATTEMPT_FORM, // Form ID + StartOpCodeHandle, // Label for where to insert opcodes + EndOpCodeHandle // Replace data + ); + + HiiFreeOpCodeHandle (StartOpCodeHandle); + HiiFreeOpCodeHandle (EndOpCodeHandle); + + return Status; +} + +/** + + Free the attempt configure data variable. + +**/ +VOID +IScsiCleanAttemptVariable ( + IN VOID +) +{ + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + UINT8 *AttemptConfigOrder; + UINTN AttemptConfigOrderSize; + UINTN Index; + + // + // Get the initialized attempt order. + // + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"InitialAttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + if (AttemptConfigOrder == NULL || AttemptConfigOrderSize == 0) { + return; + } + + for (Index = 1; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) { + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Attempt %d", + Index + ); + + GetVariable2 ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + (VOID**)&AttemptConfigData, + NULL + ); + + if (AttemptConfigData != NULL) { + gRT->SetVariable ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + 0, + 0, + NULL + ); + } + } + return; +} + +/** + Get the recorded NIC info from global structure by the Index. + + @param[in] NicIndex The index indicates the position of NIC info. + + @return Pointer to the NIC info, or NULL if not found. + +**/ +ISCSI_NIC_INFO * +IScsiGetNicInfoByIndex ( + IN UINT8 NicIndex + ) +{ + LIST_ENTRY *Entry; + ISCSI_NIC_INFO *NicInfo; + + NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) { + NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link); + if (NicInfo->NicIndex == NicIndex) { + return NicInfo; + } + } + + return NULL; +} + + +/** + Get the NIC's PCI location and return it according to the composited + format defined in iSCSI Boot Firmware Table. + + @param[in] Controller The handle of the controller. + @param[out] Bus The bus number. + @param[out] Device The device number. + @param[out] Function The function number. + + @return The composited representation of the NIC PCI location. + +**/ +UINT16 +IScsiGetNICPciLocation ( + IN EFI_HANDLE Controller, + OUT UINTN *Bus, + OUT UINTN *Device, + OUT UINTN *Function + ) +{ + EFI_STATUS Status; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_HANDLE PciIoHandle; + EFI_PCI_IO_PROTOCOL *PciIo; + UINTN Segment; + + Status = gBS->HandleProtocol ( + Controller, + &gEfiDevicePathProtocolGuid, + (VOID **) &DevicePath + ); + if (EFI_ERROR (Status)) { + return 0; + } + + Status = gBS->LocateDevicePath ( + &gEfiPciIoProtocolGuid, + &DevicePath, + &PciIoHandle + ); + if (EFI_ERROR (Status)) { + return 0; + } + + Status = gBS->HandleProtocol (PciIoHandle, &gEfiPciIoProtocolGuid, (VOID **) &PciIo); + if (EFI_ERROR (Status)) { + return 0; + } + + Status = PciIo->GetLocation (PciIo, &Segment, Bus, Device, Function); + if (EFI_ERROR (Status)) { + return 0; + } + + return (UINT16) ((*Bus << 8) | (*Device << 3) | *Function); +} + + +/** + Read the EFI variable (VendorGuid/Name) and return a dynamically allocated + buffer, and the size of the buffer. If failure, return NULL. + + @param[in] Name String part of EFI variable name. + @param[in] VendorGuid GUID part of EFI variable name. + @param[out] VariableSize Returns the size of the EFI variable that was read. + + @return Dynamically allocated memory that contains a copy of the EFI variable. + @return Caller is responsible freeing the buffer. + @retval NULL Variable was not read. + +**/ +VOID * +IScsiGetVariableAndSize ( + IN CHAR16 *Name, + IN EFI_GUID *VendorGuid, + OUT UINTN *VariableSize + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + VOID *Buffer; + + Buffer = NULL; + + // + // Pass in a zero size buffer to find the required buffer size. + // + BufferSize = 0; + Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer); + if (Status == EFI_BUFFER_TOO_SMALL) { + // + // Allocate the buffer to return + // + Buffer = AllocateZeroPool (BufferSize); + if (Buffer == NULL) { + return NULL; + } + // + // Read variable into the allocated buffer. + // + Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer); + if (EFI_ERROR (Status)) { + BufferSize = 0; + } + } + + *VariableSize = BufferSize; + return Buffer; +} + + +/** + Create the iSCSI driver data. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + + @return The iSCSI driver data created. + @retval NULL Other errors as indicated. + +**/ +ISCSI_DRIVER_DATA * +IScsiCreateDriverData ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller + ) +{ + ISCSI_DRIVER_DATA *Private; + EFI_STATUS Status; + + Private = AllocateZeroPool (sizeof (ISCSI_DRIVER_DATA)); + if (Private == NULL) { + return NULL; + } + + Private->Signature = ISCSI_DRIVER_DATA_SIGNATURE; + Private->Image = Image; + Private->Controller = Controller; + Private->Session = NULL; + + // + // Create an event to be signaled when the BS to RT transition is triggerd so + // as to abort the iSCSI session. + // + Status = gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + IScsiOnExitBootService, + Private, + &gEfiEventExitBootServicesGuid, + &Private->ExitBootServiceEvent + ); + if (EFI_ERROR (Status)) { + FreePool (Private); + return NULL; + } + + Private->ExtScsiPassThruHandle = NULL; + CopyMem(&Private->IScsiExtScsiPassThru, &gIScsiExtScsiPassThruProtocolTemplate, sizeof(EFI_EXT_SCSI_PASS_THRU_PROTOCOL)); + + // + // 0 is designated to the TargetId, so use another value for the AdapterId. + // + Private->ExtScsiPassThruMode.AdapterId = 2; + Private->ExtScsiPassThruMode.Attributes = EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL; + Private->ExtScsiPassThruMode.IoAlign = 4; + Private->IScsiExtScsiPassThru.Mode = &Private->ExtScsiPassThruMode; + + return Private; +} + + +/** + Clean the iSCSI driver data. + + @param[in] Private The iSCSI driver data. + + @retval EFI_SUCCESS The clean operation is successful. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiCleanDriverData ( + IN ISCSI_DRIVER_DATA *Private + ) +{ + EFI_STATUS Status; + + Status = EFI_SUCCESS; + + if (Private->DevicePath != NULL) { + Status = gBS->UninstallProtocolInterface ( + Private->ExtScsiPassThruHandle, + &gEfiDevicePathProtocolGuid, + Private->DevicePath + ); + if (EFI_ERROR (Status)) { + goto EXIT; + } + + FreePool (Private->DevicePath); + } + + if (Private->ExtScsiPassThruHandle != NULL) { + Status = gBS->UninstallProtocolInterface ( + Private->ExtScsiPassThruHandle, + &gEfiExtScsiPassThruProtocolGuid, + &Private->IScsiExtScsiPassThru + ); + if (!EFI_ERROR (Status)) { + mPrivate->OneSessionEstablished = FALSE; + } + } + +EXIT: + if (Private->ExitBootServiceEvent != NULL) { + gBS->CloseEvent (Private->ExitBootServiceEvent); + } + + mCallbackInfo->Current = NULL; + + FreePool (Private); + return Status; +} + +/** + Check wheather the Controller handle is configured to use DHCP protocol. + + @param[in] Controller The handle of the controller. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + @retval TRUE The handle of the controller need the Dhcp protocol. + @retval FALSE The handle of the controller does not need the Dhcp protocol. + +**/ +BOOLEAN +IScsiDhcpIsConfigured ( + IN EFI_HANDLE Controller, + IN UINT8 IpVersion + ) +{ + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptTmp; + UINT8 *AttemptConfigOrder; + UINTN AttemptConfigOrderSize; + UINTN Index; + EFI_STATUS Status; + EFI_MAC_ADDRESS MacAddr; + UINTN HwAddressSize; + UINT16 VlanId; + CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN]; + CHAR16 AttemptMacString[ISCSI_MAX_MAC_STRING_LEN]; + CHAR16 AttemptName[ISCSI_NAME_IFR_MAX_SIZE]; + + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"AttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + if (AttemptConfigOrder == NULL || AttemptConfigOrderSize == 0) { + return FALSE; + } + + // + // Get MAC address of this network device. + // + Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize); + if(EFI_ERROR (Status)) { + return FALSE; + } + // + // Get VLAN ID of this network device. + // + VlanId = NetLibGetVlanId (Controller); + IScsiMacAddrToStr (&MacAddr, (UINT32) HwAddressSize, VlanId, MacString); + + for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) { + UnicodeSPrint ( + AttemptName, + (UINTN) 128, + L"Attempt %d", + (UINTN) AttemptConfigOrder[Index] + ); + Status = GetVariable2 ( + AttemptName, + &gEfiIScsiInitiatorNameProtocolGuid, + (VOID**)&AttemptTmp, + NULL + ); + if(AttemptTmp == NULL || EFI_ERROR (Status)) { + continue; + } + + ASSERT (AttemptConfigOrder[Index] == AttemptTmp->AttemptConfigIndex); + + if (AttemptTmp->SessionConfigData.Enabled == ISCSI_DISABLED) { + FreePool (AttemptTmp); + continue; + } + + if (AttemptTmp->SessionConfigData.IpMode != IP_MODE_AUTOCONFIG && + AttemptTmp->SessionConfigData.IpMode != ((IpVersion == IP_VERSION_4) ? IP_MODE_IP4 : IP_MODE_IP6)) { + FreePool (AttemptTmp); + continue; + } + + AsciiStrToUnicodeStrS (AttemptTmp->MacString, AttemptMacString, sizeof (AttemptMacString) / sizeof (AttemptMacString[0])); + + if (AttemptTmp->Actived == ISCSI_ACTIVE_DISABLED || StrCmp (MacString, AttemptMacString)) { + continue; + } + + if(AttemptTmp->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG || + AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp == TRUE || + AttemptTmp->SessionConfigData.TargetInfoFromDhcp == TRUE) { + FreePool (AttemptTmp); + FreePool (AttemptConfigOrder); + return TRUE; + } + + FreePool (AttemptTmp); + } + + FreePool (AttemptConfigOrder); + return FALSE; +} + +/** + Check whether the Controller handle is configured to use DNS protocol. + + @param[in] Controller The handle of the controller. + + @retval TRUE The handle of the controller need the Dns protocol. + @retval FALSE The handle of the controller does not need the Dns protocol. + +**/ +BOOLEAN +IScsiDnsIsConfigured ( + IN EFI_HANDLE Controller + ) +{ + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptTmp; + UINT8 *AttemptConfigOrder; + UINTN AttemptConfigOrderSize; + UINTN Index; + EFI_STATUS Status; + EFI_MAC_ADDRESS MacAddr; + UINTN HwAddressSize; + UINT16 VlanId; + CHAR16 AttemptMacString[ISCSI_MAX_MAC_STRING_LEN]; + CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN]; + CHAR16 AttemptName[ISCSI_NAME_IFR_MAX_SIZE]; + + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"AttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + if (AttemptConfigOrder == NULL || AttemptConfigOrderSize == 0) { + return FALSE; + } + + // + // Get MAC address of this network device. + // + Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize); + if(EFI_ERROR (Status)) { + return FALSE; + } + // + // Get VLAN ID of this network device. + // + VlanId = NetLibGetVlanId (Controller); + IScsiMacAddrToStr (&MacAddr, (UINT32) HwAddressSize, VlanId, MacString); + + for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) { + UnicodeSPrint ( + AttemptName, + (UINTN) 128, + L"Attempt %d", + (UINTN) AttemptConfigOrder[Index] + ); + + Status = GetVariable2 ( + AttemptName, + &gEfiIScsiInitiatorNameProtocolGuid, + (VOID**)&AttemptTmp, + NULL + ); + if(AttemptTmp == NULL || EFI_ERROR (Status)) { + continue; + } + + ASSERT (AttemptConfigOrder[Index] == AttemptTmp->AttemptConfigIndex); + + AsciiStrToUnicodeStrS (AttemptTmp->MacString, AttemptMacString, sizeof (AttemptMacString) / sizeof (AttemptMacString[0])); + + if (AttemptTmp->SessionConfigData.Enabled == ISCSI_DISABLED || StrCmp (MacString, AttemptMacString)) { + FreePool (AttemptTmp); + continue; + } + + if (AttemptTmp->SessionConfigData.DnsMode || AttemptTmp->SessionConfigData.TargetInfoFromDhcp) { + FreePool (AttemptTmp); + FreePool (AttemptConfigOrder); + return TRUE; + } else { + FreePool (AttemptTmp); + continue; + } + + } + + FreePool (AttemptConfigOrder); + return FALSE; + +} + +/** + Get the various configuration data. + + @param[in] Private The iSCSI driver data. + + @retval EFI_SUCCESS The configuration data is retrieved. + @retval EFI_NOT_FOUND This iSCSI driver is not configured yet. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +IScsiGetConfigData ( + IN ISCSI_DRIVER_DATA *Private + ) +{ + EFI_STATUS Status; + CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN]; + CHAR16 AttemptMacString[ISCSI_MAX_MAC_STRING_LEN]; + UINTN Index; + ISCSI_NIC_INFO *NicInfo; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData; + ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptTmp; + UINT8 *AttemptConfigOrder; + UINTN AttemptConfigOrderSize; + CHAR16 IScsiMode[64]; + CHAR16 IpMode[64]; + + // + // There should be at least one attempt configured. + // + AttemptConfigOrder = IScsiGetVariableAndSize ( + L"AttemptOrder", + &gIScsiConfigGuid, + &AttemptConfigOrderSize + ); + if (AttemptConfigOrder == NULL || AttemptConfigOrderSize == 0) { + return EFI_NOT_FOUND; + } + + // + // Get the iSCSI Initiator Name. + // + mPrivate->InitiatorNameLength = ISCSI_NAME_MAX_SIZE; + Status = gIScsiInitiatorName.Get ( + &gIScsiInitiatorName, + &mPrivate->InitiatorNameLength, + mPrivate->InitiatorName + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Get the normal configuration. + // + for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) { + + // + // Check whether the attempt exists in AttemptConfig. + // + AttemptTmp = IScsiConfigGetAttemptByConfigIndex (AttemptConfigOrder[Index]); + if (AttemptTmp != NULL && AttemptTmp->SessionConfigData.Enabled == ISCSI_DISABLED) { + continue; + } else if (AttemptTmp != NULL && AttemptTmp->SessionConfigData.Enabled != ISCSI_DISABLED) { + // + // Check the autoconfig path to see whether it should be retried. + // + if (AttemptTmp->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG && + !AttemptTmp->AutoConfigureSuccess) { + if (mPrivate->Ipv6Flag && + AttemptTmp->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6) { + // + // Autoconfigure for IP6 already attempted but failed. Do not try again. + // + continue; + } else if (!mPrivate->Ipv6Flag && + AttemptTmp->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP4) { + // + // Autoconfigure for IP4 already attempted but failed. Do not try again. + // + continue; + } else { + // + // Try another approach for this autoconfigure path. + // + AttemptTmp->AutoConfigureMode = + (UINT8) (mPrivate->Ipv6Flag ? IP_MODE_AUTOCONFIG_IP6 : IP_MODE_AUTOCONFIG_IP4); + AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp = TRUE; + AttemptTmp->SessionConfigData.TargetInfoFromDhcp = TRUE; + AttemptTmp->DhcpSuccess = FALSE; + + // + // Get some information from the dhcp server. + // + if (!mPrivate->Ipv6Flag) { + Status = IScsiDoDhcp (Private->Image, Private->Controller, AttemptTmp); + if (!EFI_ERROR (Status)) { + AttemptTmp->DhcpSuccess = TRUE; + } + } else { + Status = IScsiDoDhcp6 (Private->Image, Private->Controller, AttemptTmp); + if (!EFI_ERROR (Status)) { + AttemptTmp->DhcpSuccess = TRUE; + } + } + + // + // Refresh the state of this attempt to NVR. + // + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Attempt %d", + (UINTN) AttemptTmp->AttemptConfigIndex + ); + + gRT->SetVariable ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + ISCSI_CONFIG_VAR_ATTR, + sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA), + AttemptTmp + ); + + continue; + } + } else if (AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp && + !AttemptTmp->ValidPath && + AttemptTmp->NicIndex == mPrivate->CurrentNic) { + // + // If the attempt associates with the current NIC, we can + // get DHCP information for already added, but failed, attempt. + // + AttemptTmp->DhcpSuccess = FALSE; + if (!mPrivate->Ipv6Flag && (AttemptTmp->SessionConfigData.IpMode == IP_MODE_IP4)) { + Status = IScsiDoDhcp (Private->Image, Private->Controller, AttemptTmp); + if (!EFI_ERROR (Status)) { + AttemptTmp->DhcpSuccess = TRUE; + } + } else if (mPrivate->Ipv6Flag && (AttemptTmp->SessionConfigData.IpMode == IP_MODE_IP6)) { + Status = IScsiDoDhcp6 (Private->Image, Private->Controller, AttemptTmp); + if (!EFI_ERROR (Status)) { + AttemptTmp->DhcpSuccess = TRUE; + } + } + + // + // Refresh the state of this attempt to NVR. + // + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Attempt %d", + (UINTN) AttemptTmp->AttemptConfigIndex + ); + + gRT->SetVariable ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + ISCSI_CONFIG_VAR_ATTR, + sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA), + AttemptTmp + ); + + continue; + + } else { + continue; + } + } + + // + // This attempt does not exist in AttemptConfig. Try to add a new one. + // + + NicInfo = IScsiGetNicInfoByIndex (mPrivate->CurrentNic); + ASSERT (NicInfo != NULL); + IScsiMacAddrToStr (&NicInfo->PermanentAddress, NicInfo->HwAddressSize, NicInfo->VlanId, MacString); + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Attempt %d", + (UINTN) AttemptConfigOrder[Index] + ); + + GetVariable2 ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + (VOID**)&AttemptConfigData, + NULL + ); + AsciiStrToUnicodeStrS (AttemptConfigData->MacString, AttemptMacString, sizeof (AttemptMacString) / sizeof (AttemptMacString[0])); + + if (AttemptConfigData == NULL || AttemptConfigData->Actived == ISCSI_ACTIVE_DISABLED || + StrCmp (MacString, AttemptMacString)) { + continue; + } + + ASSERT (AttemptConfigOrder[Index] == AttemptConfigData->AttemptConfigIndex); + + AttemptConfigData->NicIndex = NicInfo->NicIndex; + AttemptConfigData->DhcpSuccess = FALSE; + AttemptConfigData->ValidiBFTPath = (BOOLEAN) (mPrivate->EnableMpio ? TRUE : FALSE); + AttemptConfigData->ValidPath = FALSE; + + if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG) { + AttemptConfigData->SessionConfigData.InitiatorInfoFromDhcp = TRUE; + AttemptConfigData->SessionConfigData.TargetInfoFromDhcp = TRUE; + + AttemptConfigData->AutoConfigureMode = + (UINT8) (mPrivate->Ipv6Flag ? IP_MODE_AUTOCONFIG_IP6 : IP_MODE_AUTOCONFIG_IP4); + AttemptConfigData->AutoConfigureSuccess = FALSE; + } + + // + // Get some information from dhcp server. + // + if (AttemptConfigData->SessionConfigData.Enabled != ISCSI_DISABLED && + AttemptConfigData->SessionConfigData.InitiatorInfoFromDhcp) { + + if (!mPrivate->Ipv6Flag && + (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP4 || + AttemptConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP4)) { + Status = IScsiDoDhcp (Private->Image, Private->Controller, AttemptConfigData); + if (!EFI_ERROR (Status)) { + AttemptConfigData->DhcpSuccess = TRUE; + } + } else if (mPrivate->Ipv6Flag && + (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP6 || + AttemptConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6)) { + Status = IScsiDoDhcp6 (Private->Image, Private->Controller, AttemptConfigData); + if (!EFI_ERROR (Status)) { + AttemptConfigData->DhcpSuccess = TRUE; + } + } + + // + // Refresh the state of this attempt to NVR. + // + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"Attempt %d", + (UINTN) AttemptConfigData->AttemptConfigIndex + ); + + gRT->SetVariable ( + mPrivate->PortString, + &gEfiIScsiInitiatorNameProtocolGuid, + ISCSI_CONFIG_VAR_ATTR, + sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA), + AttemptConfigData + ); + } + + // + // Update Attempt Help Info. + // + + if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_DISABLED) { + UnicodeSPrint (IScsiMode, 64, L"Disabled"); + } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED) { + UnicodeSPrint (IScsiMode, 64, L"Enabled"); + } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) { + UnicodeSPrint (IScsiMode, 64, L"Enabled for MPIO"); + } + + if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP4) { + UnicodeSPrint (IpMode, 64, L"IP4"); + } else if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP6) { + UnicodeSPrint (IpMode, 64, L"IP6"); + } else if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG) { + UnicodeSPrint (IpMode, 64, L"Autoconfigure"); + } + + UnicodeSPrint ( + mPrivate->PortString, + (UINTN) ISCSI_NAME_IFR_MAX_SIZE, + L"MAC: %s, PFA: Bus %d | Dev %d | Func %d, iSCSI mode: %s, IP version: %s", + MacString, + NicInfo->BusNumber, + NicInfo->DeviceNumber, + NicInfo->FunctionNumber, + IScsiMode, + IpMode + ); + + AttemptConfigData->AttemptTitleHelpToken = HiiSetString ( + mCallbackInfo->RegisteredHandle, + 0, + mPrivate->PortString, + NULL + ); + if (AttemptConfigData->AttemptTitleHelpToken == 0) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Record the attempt in global link list. + // + InsertTailList (&mPrivate->AttemptConfigs, &AttemptConfigData->Link); + mPrivate->AttemptCount++; + + if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) { + mPrivate->MpioCount++; + mPrivate->EnableMpio = TRUE; + + if (AttemptConfigData->AuthenticationType == ISCSI_AUTH_TYPE_KRB) { + mPrivate->Krb5MpioCount++; + } + } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED) { + mPrivate->SinglePathCount++; + } + } + + // + // Reorder the AttemptConfig by the configured order. + // + for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) { + AttemptConfigData = IScsiConfigGetAttemptByConfigIndex (AttemptConfigOrder[Index]); + if (AttemptConfigData == NULL) { + continue; + } + + RemoveEntryList (&AttemptConfigData->Link); + InsertTailList (&mPrivate->AttemptConfigs, &AttemptConfigData->Link); + } + + // + // Update the Main Form. + // + IScsiConfigUpdateAttempt (); + + FreePool (AttemptConfigOrder); + + // + // There should be at least one attempt configuration. + // + if (!mPrivate->EnableMpio) { + if (mPrivate->SinglePathCount == 0) { + return EFI_NOT_FOUND; + } + mPrivate->ValidSinglePathCount = mPrivate->SinglePathCount; + } + + return EFI_SUCCESS; +} + + +/** + Get the device path of the iSCSI tcp connection and update it. + + @param Session The iSCSI session. + + @return The updated device path. + @retval NULL Other errors as indicated. + +**/ +EFI_DEVICE_PATH_PROTOCOL * +IScsiGetTcpConnDevicePath ( + IN ISCSI_SESSION *Session + ) +{ + ISCSI_CONNECTION *Conn; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_STATUS Status; + EFI_DEV_PATH *DPathNode; + UINTN PathLen; + + if (Session->State != SESSION_STATE_LOGGED_IN) { + return NULL; + } + + Conn = NET_LIST_USER_STRUCT_S ( + Session->Conns.ForwardLink, + ISCSI_CONNECTION, + Link, + ISCSI_CONNECTION_SIGNATURE + ); + + Status = gBS->HandleProtocol ( + Conn->TcpIo.Handle, + &gEfiDevicePathProtocolGuid, + (VOID **) &DevicePath + ); + if (EFI_ERROR (Status)) { + return NULL; + } + // + // Duplicate it. + // + DevicePath = DuplicateDevicePath (DevicePath); + if (DevicePath == NULL) { + return NULL; + } + + DPathNode = (EFI_DEV_PATH *) DevicePath; + + while (!IsDevicePathEnd (&DPathNode->DevPath)) { + if (DevicePathType (&DPathNode->DevPath) == MESSAGING_DEVICE_PATH) { + if (!Conn->Ipv6Flag && DevicePathSubType (&DPathNode->DevPath) == MSG_IPv4_DP) { + DPathNode->Ipv4.LocalPort = 0; + + DPathNode->Ipv4.StaticIpAddress = + (BOOLEAN) (!Session->ConfigData->SessionConfigData.InitiatorInfoFromDhcp); + + // + // Add a judgement here to support previous versions of IPv4_DEVICE_PATH. + // In previous versions of IPv4_DEVICE_PATH, GatewayIpAddress and SubnetMask + // do not exist. + // In new version of IPv4_DEVICE_PATH, structcure length is 27. + // + + PathLen = DevicePathNodeLength (&DPathNode->Ipv4); + + if (PathLen == IP4_NODE_LEN_NEW_VERSIONS) { + + IP4_COPY_ADDRESS ( + &DPathNode->Ipv4.GatewayIpAddress, + &Session->ConfigData->SessionConfigData.Gateway + ); + + IP4_COPY_ADDRESS ( + &DPathNode->Ipv4.SubnetMask, + &Session->ConfigData->SessionConfigData.SubnetMask + ); + } + + break; + } else if (Conn->Ipv6Flag && DevicePathSubType (&DPathNode->DevPath) == MSG_IPv6_DP) { + DPathNode->Ipv6.LocalPort = 0; + + // + // Add a judgement here to support previous versions of IPv6_DEVICE_PATH. + // In previous versions of IPv6_DEVICE_PATH, IpAddressOrigin, PrefixLength + // and GatewayIpAddress do not exist. + // In new version of IPv6_DEVICE_PATH, structure length is 60, while in + // old versions, the length is 43. + // + + PathLen = DevicePathNodeLength (&DPathNode->Ipv6); + + if (PathLen == IP6_NODE_LEN_NEW_VERSIONS ) { + + DPathNode->Ipv6.IpAddressOrigin = 0; + DPathNode->Ipv6.PrefixLength = IP6_PREFIX_LENGTH; + ZeroMem (&DPathNode->Ipv6.GatewayIpAddress, sizeof (EFI_IPv6_ADDRESS)); + } + else if (PathLen == IP6_NODE_LEN_OLD_VERSIONS) { + + // + // StaticIPAddress is a field in old versions of IPv6_DEVICE_PATH, while ignored in new + // version. Set StaticIPAddress through its' offset in old IPv6_DEVICE_PATH. + // + *((UINT8 *)(&DPathNode->Ipv6) + IP6_OLD_IPADDRESS_OFFSET) = + (BOOLEAN) (!Session->ConfigData->SessionConfigData.InitiatorInfoFromDhcp); + } + + break; + } + } + + DPathNode = (EFI_DEV_PATH *) NextDevicePathNode (&DPathNode->DevPath); + } + + return DevicePath; +} + + +/** + Abort the session when the transition from BS to RT is initiated. + + @param[in] Event The event signaled. + @param[in] Context The iSCSI driver data. + +**/ +VOID +EFIAPI +IScsiOnExitBootService ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + ISCSI_DRIVER_DATA *Private; + + Private = (ISCSI_DRIVER_DATA *) Context; + + gBS->CloseEvent (Private->ExitBootServiceEvent); + Private->ExitBootServiceEvent = NULL; + + if (Private->Session != NULL) { + IScsiSessionAbort (Private->Session); + } +} + +/** + Tests whether a controller handle is being managed by IScsi driver. + + This function tests whether the driver specified by DriverBindingHandle is + currently managing the controller specified by ControllerHandle. This test + is performed by evaluating if the the protocol specified by ProtocolGuid is + present on ControllerHandle and is was opened by DriverBindingHandle and Nic + Device handle with an attribute of EFI_OPEN_PROTOCOL_BY_DRIVER. + If ProtocolGuid is NULL, then ASSERT(). + + @param ControllerHandle A handle for a controller to test. + @param DriverBindingHandle Specifies the driver binding handle for the + driver. + @param ProtocolGuid Specifies the protocol that the driver specified + by DriverBindingHandle opens in its Start() + function. + + @retval EFI_SUCCESS ControllerHandle is managed by the driver + specified by DriverBindingHandle. + @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver + specified by DriverBindingHandle. + +**/ +EFI_STATUS +EFIAPI +IScsiTestManagedDevice ( + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE DriverBindingHandle, + IN EFI_GUID *ProtocolGuid + ) +{ + EFI_STATUS Status; + VOID *ManagedInterface; + EFI_HANDLE NicControllerHandle; + + ASSERT (ProtocolGuid != NULL); + + NicControllerHandle = NetLibGetNicHandle (ControllerHandle, ProtocolGuid); + if (NicControllerHandle == NULL) { + return EFI_UNSUPPORTED; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + (EFI_GUID *) ProtocolGuid, + &ManagedInterface, + DriverBindingHandle, + NicControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (!EFI_ERROR (Status)) { + gBS->CloseProtocol ( + ControllerHandle, + (EFI_GUID *) ProtocolGuid, + DriverBindingHandle, + NicControllerHandle + ); + return EFI_UNSUPPORTED; + } + + if (Status != EFI_ALREADY_STARTED) { + return EFI_UNSUPPORTED; + } + + return EFI_SUCCESS; +} diff --git a/NetworkPkg/IScsiDxe/IScsiMisc.h b/NetworkPkg/IScsiDxe/IScsiMisc.h new file mode 100644 index 000000000..92f42e140 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiMisc.h @@ -0,0 +1,467 @@ +/** @file + Miscellaneous definitions for iSCSI driver. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ISCSI_MISC_H_ +#define _ISCSI_MISC_H_ + +typedef struct _ISCSI_DRIVER_DATA ISCSI_DRIVER_DATA; + +/// +/// IPv4 Device Path Node Length +/// +#define IP4_NODE_LEN_NEW_VERSIONS 27 + +/// +/// IPv6 Device Path Node Length +/// +#define IP6_NODE_LEN_OLD_VERSIONS 43 +#define IP6_NODE_LEN_NEW_VERSIONS 60 + +/// +/// The ignored field StaticIpAddress's offset in old IPv6 Device Path +/// +#define IP6_OLD_IPADDRESS_OFFSET 42 + + +#pragma pack(1) +typedef struct _ISCSI_SESSION_CONFIG_NVDATA { + UINT16 TargetPort; + UINT8 Enabled; + UINT8 IpMode; + + EFI_IP_ADDRESS LocalIp; + EFI_IPv4_ADDRESS SubnetMask; + EFI_IP_ADDRESS Gateway; + + BOOLEAN InitiatorInfoFromDhcp; + BOOLEAN TargetInfoFromDhcp; + + CHAR8 TargetName[ISCSI_NAME_MAX_SIZE]; + EFI_IP_ADDRESS TargetIp; + UINT8 PrefixLength; + UINT8 BootLun[8]; + + UINT16 ConnectTimeout; ///< timout value in milliseconds. + UINT8 ConnectRetryCount; + UINT8 IsId[6]; + + BOOLEAN RedirectFlag; + UINT16 OriginalTargetPort; // The port of proxy/virtual target. + EFI_IP_ADDRESS OriginalTargetIp; // The address of proxy/virtual target. + + BOOLEAN DnsMode; // Flag indicate whether the Target address is expressed as URL format. + CHAR8 TargetUrl[ISCSI_TARGET_URI_MAX_SIZE]; + +} ISCSI_SESSION_CONFIG_NVDATA; +#pragma pack() + +/** + Calculate the prefix length of the IPv4 subnet mask. + + @param[in] SubnetMask The IPv4 subnet mask. + + @return The prefix length of the subnet mask. + @retval 0 Other errors as indicated. + +**/ +UINT8 +IScsiGetSubnetMaskPrefixLength ( + IN EFI_IPv4_ADDRESS *SubnetMask + ); + +/** + Convert the hexadecimal encoded LUN string into the 64-bit LUN. + + @param[in] Str The hexadecimal encoded LUN string. + @param[out] Lun Storage to return the 64-bit LUN. + + @retval EFI_SUCCESS The 64-bit LUN is stored in Lun. + @retval EFI_INVALID_PARAMETER The string is malformatted. + +**/ +EFI_STATUS +IScsiAsciiStrToLun ( + IN CHAR8 *Str, + OUT UINT8 *Lun + ); + +/** + Convert the 64-bit LUN into the hexadecimal encoded LUN string. + + @param[in] Lun The 64-bit LUN. + @param[out] String The storage to return the hexadecimal encoded LUN string. + +**/ +VOID +IScsiLunToUnicodeStr ( + IN UINT8 *Lun, + OUT CHAR16 *String + ); + +/** + Convert the mac address into a hexadecimal encoded "-" seperated string. + + @param[in] Mac The mac address. + @param[in] Len Length in bytes of the mac address. + @param[in] VlanId VLAN ID of the network device. + @param[out] Str The storage to return the mac string. + +**/ +VOID +IScsiMacAddrToStr ( + IN EFI_MAC_ADDRESS *Mac, + IN UINT32 Len, + IN UINT16 VlanId, + OUT CHAR16 *Str + ); + +/** + Convert the formatted IP address into the binary IP address. + + @param[in] Str The UNICODE string. + @param[in] IpMode Indicates whether the IP address is v4 or v6. + @param[out] Ip The storage to return the ASCII string. + + @retval EFI_SUCCESS The binary IP address is returned in Ip. + @retval EFI_INVALID_PARAMETER The IP string is malformatted or IpMode is + invalid. + +**/ +EFI_STATUS +IScsiAsciiStrToIp ( + IN CHAR8 *Str, + IN UINT8 IpMode, + OUT EFI_IP_ADDRESS *Ip + ); + +/** + Convert the binary encoded buffer into a hexadecimal encoded string. + + @param[in] BinBuffer The buffer containing the binary data. + @param[in] BinLength Length of the binary buffer. + @param[in, out] HexStr Pointer to the string. + @param[in, out] HexLength The length of the string. + + @retval EFI_SUCCESS The binary data is converted to the hexadecimal string + and the length of the string is updated. + @retval EFI_BUFFER_TOO_SMALL The string is too small. + @retval EFI_INVALID_PARAMETER The IP string is malformatted. + +**/ +EFI_STATUS +IScsiBinToHex ( + IN UINT8 *BinBuffer, + IN UINT32 BinLength, + IN OUT CHAR8 *HexStr, + IN OUT UINT32 *HexLength + ); + +/** + Convert the hexadecimal string into a binary encoded buffer. + + @param[in, out] BinBuffer The binary buffer. + @param[in, out] BinLength Length of the binary buffer. + @param[in] HexStr The hexadecimal string. + + @retval EFI_SUCCESS The hexadecimal string is converted into a binary + encoded buffer. + @retval EFI_BUFFER_TOO_SMALL The binary buffer is too small to hold the converted data. + +**/ +EFI_STATUS +IScsiHexToBin ( + IN OUT UINT8 *BinBuffer, + IN OUT UINT32 *BinLength, + IN CHAR8 *HexStr + ); + + +/** + Convert the decimal-constant string or hex-constant string into a numerical value. + + @param[in] Str String in decimal or hex. + + @return The numerical value. + +**/ +UINTN +IScsiNetNtoi ( + IN CHAR8 *Str + ); + +/** + Generate random numbers. + + @param[in, out] Rand The buffer to contain random numbers. + @param[in] RandLength The length of the Rand buffer. + +**/ +VOID +IScsiGenRandom ( + IN OUT UINT8 *Rand, + IN UINTN RandLength + ); + +/** + Record the NIC information in a global structure. + + @param[in] Controller The handle of the controller. + @param[in] Image Handle of the image. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_OUT_OF_RESOURCES Do not have sufficient resource to finish this + operation. + +**/ +EFI_STATUS +IScsiAddNic ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE Image + ); + +/** + Delete the recorded NIC information from a global structure. Also delete corresponding + attempts. + + @param[in] Controller The handle of the controller. + + @retval EFI_SUCCESS The operation completed. + @retval EFI_NOT_FOUND The NIC information to be deleted is not recorded. + +**/ +EFI_STATUS +IScsiRemoveNic ( + IN EFI_HANDLE Controller + ); + +/** + Create and initialize the Attempts. + + @param[in] AttemptNum The number of Attempts will be created. + + @retval EFI_SUCCESS The Attempts have been created successfully. + @retval Others Failed to create the Attempt. + +**/ +EFI_STATUS +IScsiCreateAttempts ( + IN UINTN AttemptNum + ); + +/** + Create the iSCSI configuration Keywords for each attempt. + + @param[in] KeywordNum The number Sets of Keywords will be created. + + @retval EFI_SUCCESS The operation is completed. + @retval Others Failed to create the Keywords. + +**/ +EFI_STATUS +IScsiCreateKeywords ( + IN UINTN KeywordNum + ); + +/** + + Free the attempt configure data variable. + +**/ +VOID +IScsiCleanAttemptVariable ( + IN VOID + ); + +/** + Get the recorded NIC information from a global structure by the Index. + + @param[in] NicIndex The index indicates the position of NIC info. + + @return Pointer to the NIC info or NULL if not found. + +**/ +ISCSI_NIC_INFO * +IScsiGetNicInfoByIndex ( + IN UINT8 NicIndex + ); + + +/** + Get the NIC's PCI location and return it according to the composited + format defined in iSCSI Boot Firmware Table. + + @param[in] Controller The handle of the controller. + @param[out] Bus The bus number. + @param[out] Device The device number. + @param[out] Function The function number. + + @return The composited representation of the NIC PCI location. + +**/ +UINT16 +IScsiGetNICPciLocation ( + IN EFI_HANDLE Controller, + OUT UINTN *Bus, + OUT UINTN *Device, + OUT UINTN *Function + ); + +/** + Read the EFI variable (VendorGuid/Name) and return a dynamically allocated + buffer, and the size of the buffer. If failure, return NULL. + + @param[in] Name String part of EFI variable name. + @param[in] VendorGuid GUID part of EFI variable name. + @param[out] VariableSize Returns the size of the EFI variable that was read. + + @return Dynamically allocated memory that contains a copy of the EFI variable. + @return Caller is responsible freeing the buffer. + @retval NULL Variable was not read. + +**/ +VOID * +IScsiGetVariableAndSize ( + IN CHAR16 *Name, + IN EFI_GUID *VendorGuid, + OUT UINTN *VariableSize + ); + +/** + Create the iSCSI driver data. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + + @return The iSCSI driver data created. + @retval NULL Other errors as indicated. + +**/ +ISCSI_DRIVER_DATA * +IScsiCreateDriverData ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller + ); + +/** + Clean the iSCSI driver data. + + @param[in] Private The iSCSI driver data. + + @retval EFI_SUCCES The clean operation is successful. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiCleanDriverData ( + IN ISCSI_DRIVER_DATA *Private + ); + +/** + Check wheather the Controller handle is configured to use DHCP protocol. + + @param[in] Controller The handle of the controller. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + @retval TRUE The handle of the controller need the Dhcp protocol. + @retval FALSE The handle of the controller does not need the Dhcp protocol. + +**/ +BOOLEAN +IScsiDhcpIsConfigured ( + IN EFI_HANDLE Controller, + IN UINT8 IpVersion + ); + +/** + Check wheather the Controller handle is configured to use DNS protocol. + + @param[in] Controller The handle of the controller. + + @retval TRUE The handle of the controller need the DNS protocol. + @retval FALSE The handle of the controller does not need the DNS protocol. + +**/ +BOOLEAN +IScsiDnsIsConfigured ( + IN EFI_HANDLE Controller + ); + +/** + Get the various configuration data of this iSCSI instance. + + @param[in] Private The iSCSI driver data. + + @retval EFI_SUCCESS Obtained the configuration of this instance. + @retval EFI_ABORTED The operation was aborted. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiGetConfigData ( + IN ISCSI_DRIVER_DATA *Private + ); + +/** + Get the device path of the iSCSI tcp connection and update it. + + @param[in] Session The iSCSI session data. + + @return The updated device path. + @retval NULL Other errors as indicated. + +**/ +EFI_DEVICE_PATH_PROTOCOL * +IScsiGetTcpConnDevicePath ( + IN ISCSI_SESSION *Session + ); + +/** + Abort the session when the transition from BS to RT is initiated. + + @param[in] Event The event signaled. + @param[in] Context The iSCSI driver data. + +**/ +VOID +EFIAPI +IScsiOnExitBootService ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Tests whether a controller handle is being managed by IScsi driver. + + This function tests whether the driver specified by DriverBindingHandle is + currently managing the controller specified by ControllerHandle. This test + is performed by evaluating if the the protocol specified by ProtocolGuid is + present on ControllerHandle and is was opened by DriverBindingHandle and Nic + Device handle with an attribute of EFI_OPEN_PROTOCOL_BY_DRIVER. + If ProtocolGuid is NULL, then ASSERT(). + + @param ControllerHandle A handle for a controller to test. + @param DriverBindingHandle Specifies the driver binding handle for the + driver. + @param ProtocolGuid Specifies the protocol that the driver specified + by DriverBindingHandle opens in its Start() + function. + + @retval EFI_SUCCESS ControllerHandle is managed by the driver + specified by DriverBindingHandle. + @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver + specified by DriverBindingHandle. + +**/ +EFI_STATUS +EFIAPI +IScsiTestManagedDevice ( + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE DriverBindingHandle, + IN EFI_GUID *ProtocolGuid + ); +#endif diff --git a/NetworkPkg/IScsiDxe/IScsiProto.c b/NetworkPkg/IScsiDxe/IScsiProto.c new file mode 100644 index 000000000..6fbf973e3 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiProto.c @@ -0,0 +1,3180 @@ +/** @file + The implementation of iSCSI protocol based on RFC3720. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "IScsiImpl.h" + +UINT32 mDataSegPad = 0; + +/** + Attach the iSCSI connection to the iSCSI session. + + @param[in, out] Session The iSCSI session. + @param[in, out] Conn The iSCSI connection. + +**/ +VOID +IScsiAttatchConnection ( + IN OUT ISCSI_SESSION *Session, + IN OUT ISCSI_CONNECTION *Conn + ) +{ + InsertTailList (&Session->Conns, &Conn->Link); + Conn->Session = Session; + Session->NumConns++; +} + +/** + Detach the iSCSI connection from the session it belongs to. + + @param[in, out] Conn The iSCSI connection. + +**/ +VOID +IScsiDetatchConnection ( + IN OUT ISCSI_CONNECTION *Conn + ) +{ + RemoveEntryList (&Conn->Link); + Conn->Session->NumConns--; + Conn->Session = NULL; +} + + +/** + Check the sequence number according to RFC3720. + + @param[in, out] ExpSN The currently expected sequence number. + @param[in] NewSN The sequence number to check. + + @retval EFI_SUCCESS The check passed and the ExpSN is increased. + @retval EFI_NOT_READY Response was sent due to a retransmission request. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred. + +**/ +EFI_STATUS +IScsiCheckSN ( + IN OUT UINT32 *ExpSN, + IN UINT32 NewSN + ) +{ + if (!ISCSI_SEQ_EQ (NewSN, *ExpSN)) { + if (ISCSI_SEQ_LT (NewSN, *ExpSN)) { + // + // Duplicate + // + return EFI_NOT_READY; + } else { + return EFI_PROTOCOL_ERROR; + } + } else { + // + // Advance the ExpSN + // + (*ExpSN)++; + return EFI_SUCCESS; + } +} + + +/** + Update the sequence numbers for the iSCSI command. + + @param[in, out] Session The iSCSI session. + @param[in] MaxCmdSN Maximum CmdSN from the target. + @param[in] ExpCmdSN Next expected CmdSN from the target. + +**/ +VOID +IScsiUpdateCmdSN ( + IN OUT ISCSI_SESSION *Session, + IN UINT32 MaxCmdSN, + IN UINT32 ExpCmdSN + ) +{ + if (ISCSI_SEQ_LT (MaxCmdSN, ExpCmdSN - 1)) { + return ; + } + + if (ISCSI_SEQ_GT (MaxCmdSN, Session->MaxCmdSN)) { + Session->MaxCmdSN = MaxCmdSN; + } + + if (ISCSI_SEQ_GT (ExpCmdSN, Session->ExpCmdSN)) { + Session->ExpCmdSN = ExpCmdSN; + } +} + + +/** + This function does the iSCSI connection login. + + @param[in, out] Conn The iSCSI connection to login. + @param Timeout The timeout value in millisecond. + + @retval EFI_SUCCESS The iSCSI connection is logged into the iSCSI target. + @retval EFI_TIMEOUT Timeout occurred during the login procedure. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiConnLogin ( + IN OUT ISCSI_CONNECTION *Conn, + IN UINT16 Timeout + ) +{ + EFI_STATUS Status; + + // + // Start the timer, and wait Timeout seconds to establish the TCP connection. + // + Status = gBS->SetTimer ( + Conn->TimeoutEvent, + TimerRelative, + MultU64x32 (Timeout, TICKS_PER_MS) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Try to establish the tcp connection. + // + Status = TcpIoConnect (&Conn->TcpIo, Conn->TimeoutEvent); + gBS->SetTimer (Conn->TimeoutEvent, TimerCancel, 0); + + if (EFI_ERROR (Status)) { + return Status; + } + + Conn->State = CONN_STATE_IN_LOGIN; + + // + // Connection is established, start the iSCSI Login. + // + do { + Status = IScsiSendLoginReq (Conn); + if (EFI_ERROR (Status)) { + break; + } + + Status = IScsiReceiveLoginRsp (Conn); + if (EFI_ERROR (Status)) { + break; + } + } while (Conn->CurrentStage != ISCSI_FULL_FEATURE_PHASE); + + return Status; +} + + +/** + Reset the iSCSI connection. + + @param[in, out] Conn The iSCSI connection to reset. + +**/ +VOID +IScsiConnReset ( + IN OUT ISCSI_CONNECTION *Conn + ) +{ + TcpIoReset (&Conn->TcpIo); +} + + +/** + Create a TCP connection for the iSCSI session. + + @param[in] Session Points to the iSCSI session. + + @return The newly created iSCSI connection. + +**/ +ISCSI_CONNECTION * +IScsiCreateConnection ( + IN ISCSI_SESSION *Session + ) +{ + ISCSI_DRIVER_DATA *Private; + ISCSI_SESSION_CONFIG_NVDATA *NvData; + ISCSI_CONNECTION *Conn; + TCP_IO_CONFIG_DATA TcpIoConfig; + TCP4_IO_CONFIG_DATA *Tcp4IoConfig; + TCP6_IO_CONFIG_DATA *Tcp6IoConfig; + EFI_STATUS Status; + + Private = Session->Private; + NvData = &Session->ConfigData->SessionConfigData; + + Conn = AllocateZeroPool (sizeof (ISCSI_CONNECTION)); + if (Conn == NULL) { + return NULL; + } + + Conn->Signature = ISCSI_CONNECTION_SIGNATURE; + Conn->State = CONN_STATE_FREE; + Conn->CurrentStage = ISCSI_SECURITY_NEGOTIATION; + Conn->NextStage = ISCSI_LOGIN_OPERATIONAL_NEGOTIATION; + Conn->AuthStep = ISCSI_AUTH_INITIAL; + Conn->ExpStatSN = 0; + Conn->PartialReqSent = FALSE; + Conn->PartialRspRcvd = FALSE; + Conn->ParamNegotiated = FALSE; + Conn->Cid = Session->NextCid++; + Conn->Ipv6Flag = NvData->IpMode == IP_MODE_IP6 || Session->ConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6; + + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &Conn->TimeoutEvent + ); + if (EFI_ERROR (Status)) { + FreePool (Conn); + return NULL; + } + + NetbufQueInit (&Conn->RspQue); + + // + // Set the default connection-only parameters. + // + Conn->MaxRecvDataSegmentLength = DEFAULT_MAX_RECV_DATA_SEG_LEN; + Conn->HeaderDigest = IScsiDigestNone; + Conn->DataDigest = IScsiDigestNone; + + if (NvData->DnsMode) { + // + // perform dns process if target address expressed by domain name. + // + if (!Conn->Ipv6Flag) { + Status = IScsiDns4 (Private->Image, Private->Controller, NvData); + } else { + Status = IScsiDns6 (Private->Image, Private->Controller, NvData); + } + + if (EFI_ERROR(Status)) { + DEBUG ((EFI_D_ERROR, "The configuration of Target address or DNS server address is invalid!\n")); + FreePool (Conn); + return NULL; + } + } + + if (!Conn->Ipv6Flag) { + Tcp4IoConfig = &TcpIoConfig.Tcp4IoConfigData; + + CopyMem (&Tcp4IoConfig->LocalIp, &NvData->LocalIp, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Tcp4IoConfig->SubnetMask, &NvData->SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Tcp4IoConfig->Gateway, &NvData->Gateway, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Tcp4IoConfig->RemoteIp, &NvData->TargetIp, sizeof (EFI_IPv4_ADDRESS)); + + Tcp4IoConfig->RemotePort = NvData->TargetPort; + Tcp4IoConfig->ActiveFlag = TRUE; + Tcp4IoConfig->StationPort = 0; + } else { + Tcp6IoConfig = &TcpIoConfig.Tcp6IoConfigData; + + CopyMem (&Tcp6IoConfig->RemoteIp, &NvData->TargetIp, sizeof (EFI_IPv6_ADDRESS)); + Tcp6IoConfig->RemotePort = NvData->TargetPort; + Tcp6IoConfig->ActiveFlag = TRUE; + Tcp6IoConfig->StationPort = 0; + } + + // + // Create the TCP IO for this connection. + // + Status = TcpIoCreateSocket ( + Private->Image, + Private->Controller, + (UINT8) (!Conn->Ipv6Flag ? TCP_VERSION_4: TCP_VERSION_6), + &TcpIoConfig, + &Conn->TcpIo + ); + if (EFI_ERROR (Status)) { + gBS->CloseEvent (Conn->TimeoutEvent); + FreePool (Conn); + Conn = NULL; + } + + return Conn; +} + + +/** + Destroy an iSCSI connection. + + @param[in] Conn The connection to destroy. + +**/ +VOID +IScsiDestroyConnection ( + IN ISCSI_CONNECTION *Conn + ) +{ + TcpIoDestroySocket (&Conn->TcpIo); + + NetbufQueFlush (&Conn->RspQue); + gBS->CloseEvent (Conn->TimeoutEvent); + FreePool (Conn); +} + +/** + Retrieve the IPv6 Address/Prefix/Gateway from the established TCP connection, these informations + will be filled in the iSCSI Boot Firmware Table. + + @param[in] Conn The connection used in the iSCSI login phase. + + @retval EFI_SUCCESS Get the NIC information successfully. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiGetIp6NicInfo ( + IN ISCSI_CONNECTION *Conn + ) +{ + ISCSI_SESSION_CONFIG_NVDATA *NvData; + EFI_TCP6_PROTOCOL *Tcp6; + EFI_IP6_MODE_DATA Ip6ModeData; + EFI_STATUS Status; + EFI_IPv6_ADDRESS *TargetIp; + UINTN Index; + UINT8 SubnetPrefixLength; + UINTN RouteEntry; + + NvData = &Conn->Session->ConfigData->SessionConfigData; + TargetIp = &NvData->TargetIp.v6; + Tcp6 = Conn->TcpIo.Tcp.Tcp6; + + ZeroMem (&Ip6ModeData, sizeof (EFI_IP6_MODE_DATA)); + Status = Tcp6->GetModeData ( + Tcp6, + NULL, + NULL, + &Ip6ModeData, + NULL, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (!Ip6ModeData.IsConfigured) { + Status = EFI_ABORTED; + goto ON_EXIT; + } + + IP6_COPY_ADDRESS (&NvData->LocalIp, &Ip6ModeData.ConfigData.StationAddress); + + NvData->PrefixLength = 0; + for (Index = 0; Index < Ip6ModeData.AddressCount; Index++) { + if (EFI_IP6_EQUAL (&NvData->LocalIp.v6, &Ip6ModeData.AddressList[Index].Address)) { + NvData->PrefixLength = Ip6ModeData.AddressList[Index].PrefixLength; + break; + } + } + + SubnetPrefixLength = 0; + RouteEntry = Ip6ModeData.RouteCount; + for (Index = 0; Index < Ip6ModeData.RouteCount; Index++) { + if (NetIp6IsNetEqual (TargetIp, &Ip6ModeData.RouteTable[Index].Destination, Ip6ModeData.RouteTable[Index].PrefixLength)) { + if (SubnetPrefixLength < Ip6ModeData.RouteTable[Index].PrefixLength) { + SubnetPrefixLength = Ip6ModeData.RouteTable[Index].PrefixLength; + RouteEntry = Index; + } + } + } + if (RouteEntry != Ip6ModeData.RouteCount) { + IP6_COPY_ADDRESS (&NvData->Gateway, &Ip6ModeData.RouteTable[RouteEntry].Gateway); + } + +ON_EXIT: + if (Ip6ModeData.AddressList != NULL) { + FreePool (Ip6ModeData.AddressList); + } + if (Ip6ModeData.GroupTable!= NULL) { + FreePool (Ip6ModeData.GroupTable); + } + if (Ip6ModeData.RouteTable!= NULL) { + FreePool (Ip6ModeData.RouteTable); + } + if (Ip6ModeData.NeighborCache!= NULL) { + FreePool (Ip6ModeData.NeighborCache); + } + if (Ip6ModeData.PrefixTable!= NULL) { + FreePool (Ip6ModeData.PrefixTable); + } + if (Ip6ModeData.IcmpTypeList!= NULL) { + FreePool (Ip6ModeData.IcmpTypeList); + } + + return Status; +} + +/** + Login the iSCSI session. + + @param[in] Session The iSCSI session. + + @retval EFI_SUCCESS The iSCSI session login procedure finished. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NO_MEDIA There was a media error. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiSessionLogin ( + IN ISCSI_SESSION *Session + ) +{ + EFI_STATUS Status; + ISCSI_CONNECTION *Conn; + VOID *Tcp; + EFI_GUID *ProtocolGuid; + UINT8 RetryCount; + EFI_STATUS MediaStatus; + + // + // Check media status before session login. + // + MediaStatus = EFI_SUCCESS; + NetLibDetectMediaWaitTimeout (Session->Private->Controller, ISCSI_CHECK_MEDIA_LOGIN_WAITING_TIME, &MediaStatus); + if (MediaStatus != EFI_SUCCESS) { + return EFI_NO_MEDIA; + } + + // + // Set session identifier + // + CopyMem (Session->Isid, Session->ConfigData->SessionConfigData.IsId, 6); + + RetryCount = 0; + + do { + // + // Create a connection for the session. + // + Conn = IScsiCreateConnection (Session); + if (Conn == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + IScsiAttatchConnection (Session, Conn); + + // + // Login througth the newly created connection. + // + Status = IScsiConnLogin (Conn, Session->ConfigData->SessionConfigData.ConnectTimeout); + if (EFI_ERROR (Status)) { + IScsiConnReset (Conn); + IScsiDetatchConnection (Conn); + IScsiDestroyConnection (Conn); + } + + if (Status != EFI_TIMEOUT) { + break; + } + + RetryCount++; + } while (RetryCount <= Session->ConfigData->SessionConfigData.ConnectRetryCount); + + if (!EFI_ERROR (Status)) { + Session->State = SESSION_STATE_LOGGED_IN; + + if (!Conn->Ipv6Flag) { + ProtocolGuid = &gEfiTcp4ProtocolGuid; + } else { + ProtocolGuid = &gEfiTcp6ProtocolGuid; + } + + Status = gBS->OpenProtocol ( + Conn->TcpIo.Handle, + ProtocolGuid, + (VOID **) &Tcp, + Session->Private->Image, + Session->Private->ExtScsiPassThruHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + + ASSERT_EFI_ERROR (Status); + + if (Conn->Ipv6Flag) { + Status = IScsiGetIp6NicInfo (Conn); + } + } + + return Status; +} + + +/** + Wait for IPsec negotiation, then try to login the iSCSI session again. + + @param[in] Session The iSCSI session. + + @retval EFI_SUCCESS The iSCSI session login procedure finished. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred. + +**/ +EFI_STATUS +IScsiSessionReLogin ( + IN ISCSI_SESSION *Session + ) +{ + + EFI_STATUS Status; + EFI_STATUS TimerStatus; + EFI_EVENT Timer; + + Status = gBS->CreateEvent (EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Timer); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->SetTimer ( + Timer, + TimerRelative, + ISCSI_WAIT_IPSEC_TIMEOUT + ); + + if (EFI_ERROR (Status)) { + gBS->CloseEvent (Timer); + return Status; + } + + do { + + TimerStatus = gBS->CheckEvent (Timer); + + if (!EFI_ERROR (TimerStatus)) { + Status = IScsiSessionLogin (Session); + } + + } while (TimerStatus == EFI_NOT_READY); + + gBS->CloseEvent (Timer); + return Status; +} + + +/** + Build and send the iSCSI login request to the iSCSI target according to + the current login stage. + + @param[in] Conn The connection in the iSCSI login phase. + + @retval EFI_SUCCESS The iSCSI login request PDU is built and sent on this + connection. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_DEVICE_ERROR Some kind of device error occurred. + +**/ +EFI_STATUS +IScsiSendLoginReq ( + IN ISCSI_CONNECTION *Conn + ) +{ + NET_BUF *Pdu; + EFI_STATUS Status; + + // + // Build the Login Request PDU. + // + Pdu = IScsiPrepareLoginReq (Conn); + if (Pdu == NULL) { + return EFI_DEVICE_ERROR; + } + // + // Send it to the iSCSI target. + // + Status = TcpIoTransmit (&Conn->TcpIo, Pdu); + + NetbufFree (Pdu); + + return Status; +} + + +/** + Receive and process the iSCSI login response. + + @param[in] Conn The connection in the iSCSI login phase. + + @retval EFI_SUCCESS The iSCSI login response PDU is received and processed. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiReceiveLoginRsp ( + IN ISCSI_CONNECTION *Conn + ) +{ + EFI_STATUS Status; + NET_BUF *Pdu; + + Pdu = NULL; + + // + // Receive the iSCSI login response. + // + Status = IScsiReceivePdu (Conn, &Pdu, NULL, FALSE, FALSE, NULL); + if (EFI_ERROR (Status)) { + return Status; + } + ASSERT (Pdu != NULL); + + // + // A Login Response is received; process it. + // + Status = IScsiProcessLoginRsp (Conn, Pdu); + + NetbufFree (Pdu); + + return Status; +} + + +/** + Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU. + The DataSegmentLength and the actual size of the net buffer containing this PDU will be + updated. + + @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will + be added to. + @param[in] Key The key name string. + @param[in] Value The value string. + + @retval EFI_SUCCESS The key-value pair is added to the PDU's data segment and + the correspondence length fields are updated. + @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value + pair. + @retval EFI_PROTOCOL_ERROR There is no such data in the net buffer. +**/ +EFI_STATUS +IScsiAddKeyValuePair ( + IN OUT NET_BUF *Pdu, + IN CHAR8 *Key, + IN CHAR8 *Value + ) +{ + UINT32 DataSegLen; + UINT32 KeyLen; + UINT32 ValueLen; + UINT32 TotalLen; + ISCSI_LOGIN_REQUEST *LoginReq; + CHAR8 *Data; + + LoginReq = (ISCSI_LOGIN_REQUEST *) NetbufGetByte (Pdu, 0, NULL); + if (LoginReq == NULL) { + return EFI_PROTOCOL_ERROR; + } + DataSegLen = NTOH24 (LoginReq->DataSegmentLength); + + KeyLen = (UINT32) AsciiStrLen (Key); + ValueLen = (UINT32) AsciiStrLen (Value); + + // + // 1 byte for the key value separator '=' and 1 byte for the null + // delimiter after the value. + // + TotalLen = KeyLen + 1 + ValueLen + 1; + + // + // Allocate the space for the key-value pair. + // + Data = (CHAR8 *) NetbufAllocSpace (Pdu, TotalLen, NET_BUF_TAIL); + if (Data == NULL) { + return EFI_OUT_OF_RESOURCES; + } + // + // Add the key. + // + CopyMem (Data, Key, KeyLen); + Data += KeyLen; + + *Data = '='; + Data++; + + // + // Add the value. + // + CopyMem (Data, Value, ValueLen); + Data += ValueLen; + + *Data = '\0'; + + // + // Update the DataSegmentLength + // + ISCSI_SET_DATASEG_LEN (LoginReq, DataSegLen + TotalLen); + + return EFI_SUCCESS; +} + + +/** + Prepare the iSCSI login request to be sent according to the current login status. + + @param[in, out] Conn The connection in the iSCSI login phase. + + @return The pointer to the net buffer containing the iSCSI login request built. + @retval NULL Other errors as indicated. + +**/ +NET_BUF * +IScsiPrepareLoginReq ( + IN OUT ISCSI_CONNECTION *Conn + ) +{ + ISCSI_SESSION *Session; + NET_BUF *Nbuf; + ISCSI_LOGIN_REQUEST *LoginReq; + EFI_STATUS Status; + + Session = Conn->Session; + + Nbuf = NetbufAlloc (sizeof (ISCSI_LOGIN_REQUEST) + DEFAULT_MAX_RECV_DATA_SEG_LEN); + if (Nbuf == NULL) { + return NULL; + } + + LoginReq = (ISCSI_LOGIN_REQUEST *) NetbufAllocSpace (Nbuf, sizeof (ISCSI_LOGIN_REQUEST), NET_BUF_TAIL); + if (LoginReq == NULL) { + NetbufFree (Nbuf); + return NULL; + } + ZeroMem (LoginReq, sizeof (ISCSI_LOGIN_REQUEST)); + + // + // Init the login request pdu + // + ISCSI_SET_OPCODE (LoginReq, ISCSI_OPCODE_LOGIN_REQ, ISCSI_REQ_IMMEDIATE); + ISCSI_SET_STAGES (LoginReq, Conn->CurrentStage, Conn->NextStage); + LoginReq->VersionMax = ISCSI_VERSION_MAX; + LoginReq->VersionMin = ISCSI_VERSION_MIN; + LoginReq->Tsih = HTONS (Session->Tsih); + LoginReq->InitiatorTaskTag = HTONL (Session->InitiatorTaskTag); + LoginReq->Cid = HTONS (Conn->Cid); + LoginReq->CmdSN = HTONL (Session->CmdSN); + + // + // For the first Login Request on a coonection this is ExpStatSN for the + // old connection, and this field is only valid if the Login Request restarts + // a connection. + // For subsequent Login Requests it is used to acknowledge the Login Responses + // with their increasing StatSN values. + // + LoginReq->ExpStatSN = HTONL (Conn->ExpStatSN); + CopyMem (LoginReq->Isid, Session->Isid, sizeof (LoginReq->Isid)); + + if (Conn->PartialRspRcvd) { + // + // A partial response. The initiator must send an empty Login Request. + // + return Nbuf; + } + + Status = EFI_SUCCESS; + + switch (Conn->CurrentStage) { + case ISCSI_SECURITY_NEGOTIATION: + // + // Both none authentication and CHAP authentication share the CHAP path. + // + // + if (Session->AuthType != ISCSI_AUTH_TYPE_KRB) { + Status = IScsiCHAPToSendReq (Conn, Nbuf); + } + + break; + + case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION: + // + // Only negotiate the parameter once. + // + if (!Conn->ParamNegotiated) { + IScsiFillOpParams (Conn, Nbuf); + } + + ISCSI_SET_FLAG (LoginReq, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT); + break; + + default: + // + // An error occurs... + // + Status = EFI_DEVICE_ERROR; + break; + } + + if (EFI_ERROR (Status)) { + NetbufFree (Nbuf); + Nbuf = NULL; + } else { + // + // Pad the data segment if needed. + // + IScsiPadSegment (Nbuf, ISCSI_GET_DATASEG_LEN (LoginReq)); + // + // Check whether we will issue the stage transition signal? + // + Conn->TransitInitiated = ISCSI_FLAG_ON (LoginReq, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT); + } + + return Nbuf; +} + + +/** + Process the iSCSI Login Response. + + @param[in, out] Conn The connection on which the iSCSI login response is received. + @param[in, out] Pdu The iSCSI login response PDU. + + @retval EFI_SUCCESS The iSCSI login response PDU is processed, and all checks are passed. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred. + @retval EFI_MEDIA_CHANGED Target is redirected. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiProcessLoginRsp ( + IN OUT ISCSI_CONNECTION *Conn, + IN OUT NET_BUF *Pdu + ) +{ + EFI_STATUS Status; + ISCSI_SESSION *Session; + ISCSI_LOGIN_RESPONSE *LoginRsp; + BOOLEAN Transit; + BOOLEAN Continue; + UINT8 CurrentStage; + UINT8 NextStage; + UINT8 *DataSeg; + UINT32 DataSegLen; + + Status = EFI_SUCCESS; + Session = Conn->Session; + + LoginRsp = (ISCSI_LOGIN_RESPONSE *) NetbufGetByte (Pdu, 0, NULL); + if (LoginRsp == NULL) { + return EFI_PROTOCOL_ERROR; + } + if (!ISCSI_CHECK_OPCODE (LoginRsp, ISCSI_OPCODE_LOGIN_RSP)) { + // + // It is not a Login Response. + // + return EFI_PROTOCOL_ERROR; + } + // + // Get the data segment, if any. + // + DataSegLen = ISCSI_GET_DATASEG_LEN (LoginRsp); + if (DataSegLen != 0) { + DataSeg = NetbufGetByte (Pdu, sizeof (ISCSI_LOGIN_RESPONSE), NULL); + } else { + DataSeg = NULL; + } + // + // Check the status class in the login response PDU. + // + switch (LoginRsp->StatusClass) { + case ISCSI_LOGIN_STATUS_SUCCESS: + // + // Just break here; the response and the data segment will be processed later. + // + break; + + case ISCSI_LOGIN_STATUS_REDIRECTION: + // + // The target may be moved to a different address. + // + if (DataSeg == NULL) { + return EFI_PROTOCOL_ERROR; + } + // + // Process the TargetAddress key-value strings in the data segment to update the + // target address info. + // + Status = IScsiUpdateTargetAddress (Session, (CHAR8 *) DataSeg, DataSegLen); + if (EFI_ERROR (Status)) { + return Status; + } + // + // Session will be restarted on this error status because the Target is + // redirected by this Login Response. + // + return EFI_MEDIA_CHANGED; + + default: + // + // Initiator Error, Target Error, or any other undefined error code. + // + return EFI_PROTOCOL_ERROR; + } + // + // The status is success; extract the wanted fields from the header segment. + // + Transit = ISCSI_FLAG_ON (LoginRsp, ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT); + Continue = ISCSI_FLAG_ON (LoginRsp, ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE); + + CurrentStage = ISCSI_GET_CURRENT_STAGE (LoginRsp); + NextStage = ISCSI_GET_NEXT_STAGE (LoginRsp); + + LoginRsp->InitiatorTaskTag = NTOHL (LoginRsp->InitiatorTaskTag); + + if ((Transit && Continue) || + (CurrentStage != Conn->CurrentStage) || + (!Conn->TransitInitiated && Transit) || + (Transit && (NextStage != Conn->NextStage)) || + (CompareMem (Session->Isid, LoginRsp->Isid, sizeof (LoginRsp->Isid)) != 0) || + (LoginRsp->InitiatorTaskTag != Session->InitiatorTaskTag) + ) { + // + // A Login Response with the C bit set to 1 MUST have the T bit set to 0. + // The CSG in the Login Response MUST be the same with the I-end of this connection. + // The T bit can't be 1 if the last Login Response sent by the initiator doesn't + // initiate the transistion. + // The NSG MUST be the same with the I-end of this connection if Transit is required. + // The ISID in the Login Response MUST be the same with this session. + // + return EFI_PROTOCOL_ERROR; + } + + LoginRsp->StatSN = NTOHL (LoginRsp->StatSN); + LoginRsp->ExpCmdSN = NTOHL (LoginRsp->ExpCmdSN); + LoginRsp->MaxCmdSN = NTOHL (LoginRsp->MaxCmdSN); + + if ((Conn->CurrentStage == ISCSI_SECURITY_NEGOTIATION) && (Conn->AuthStep == ISCSI_AUTH_INITIAL)) { + // + // If the Login Request is a leading Login Request, the target MUST use + // the value presented in CmdSN as the target value for ExpCmdSN. + // + if ((Session->State == SESSION_STATE_FREE) && (Session->CmdSN != LoginRsp->ExpCmdSN)) { + return EFI_PROTOCOL_ERROR; + } + + // + // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN + // and ExpCmdSN. + // + Conn->ExpStatSN = LoginRsp->StatSN + 1; + Session->MaxCmdSN = LoginRsp->MaxCmdSN; + Session->ExpCmdSN = LoginRsp->ExpCmdSN; + } else { + // + // Check the StatSN of this PDU. + // + Status = IScsiCheckSN (&Conn->ExpStatSN, LoginRsp->StatSN); + if (!EFI_ERROR (Status)) { + // + // Update the MaxCmdSN and ExpCmdSN. + // + IScsiUpdateCmdSN (Session, LoginRsp->MaxCmdSN, LoginRsp->ExpCmdSN); + } else { + return Status; + } + } + // + // Trim off the header segment. + // + NetbufTrim (Pdu, sizeof (ISCSI_LOGIN_RESPONSE), NET_BUF_HEAD); + + // + // Queue this login response first in case it's a partial response so that + // later when the full response list is received we can combine these scattered + // responses' data segment and then process it. + // + NET_GET_REF (Pdu); + NetbufQueAppend (&Conn->RspQue, Pdu); + + Conn->PartialRspRcvd = Continue; + if (Continue) { + // + // It is a partial response; must wait for another or more Request/Response + // conversations to get the full response. + // + return EFI_SUCCESS; + } + + switch (CurrentStage) { + case ISCSI_SECURITY_NEGOTIATION: + // + // In security negotiation stage, let CHAP module handle it. + // + if (Session->AuthType != ISCSI_AUTH_TYPE_KRB) { + Status = IScsiCHAPOnRspReceived (Conn); + } + break; + + case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION: + // + // Response received with negotiation response on iSCSI parameters: check them. + // + Status = IScsiCheckOpParams (Conn); + if (!EFI_ERROR (Status)) { + Conn->ParamNegotiated = TRUE; + } + + break; + + default: + // + // Should never get here. + // + Status = EFI_PROTOCOL_ERROR; + break; + } + + if (Transit && (Status == EFI_SUCCESS)) { + // + // Do the state transition. + // + Conn->CurrentStage = Conn->NextStage; + + if (Conn->CurrentStage == ISCSI_LOGIN_OPERATIONAL_NEGOTIATION) { + Conn->NextStage = ISCSI_FULL_FEATURE_PHASE; + } else { + // + // CurrentStage is iSCSI Full Feature. It is the Login-Final Response; + // get the TSIH from the Login Response. + // + Session->Tsih = NTOHS (LoginRsp->Tsih); + } + } + // + // Flush the response(s) received. + // + NetbufQueFlush (&Conn->RspQue); + + return Status; +} + + +/** + Updated the target information according the data received in the iSCSI + login response with an target redirection status. + + @param[in, out] Session The iSCSI session. + @param[in] Data The data segment that should contain the + TargetAddress key-value list. + @param[in] Len Length of the data. + + @retval EFI_SUCCESS The target address is updated. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NOT_FOUND The TargetAddress key is not found. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiUpdateTargetAddress ( + IN OUT ISCSI_SESSION *Session, + IN CHAR8 *Data, + IN UINT32 Len + ) +{ + LIST_ENTRY *KeyValueList; + CHAR8 *TargetAddress; + CHAR8 *IpStr; + EFI_STATUS Status; + UINTN Number; + UINT8 IpMode; + ISCSI_SESSION_CONFIG_NVDATA *NvData; + + KeyValueList = IScsiBuildKeyValueList (Data, Len); + if (KeyValueList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = EFI_NOT_FOUND; + NvData = &Session->ConfigData->SessionConfigData; + + while (TRUE) { + TargetAddress = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_TARGET_ADDRESS); + if (TargetAddress == NULL) { + break; + } + + // + // RFC 3720 defines format of the TargetAddress=domainname[:port][,portal-group-tag] + // The domainname can be specified as either a DNS host name, adotted-decimal IPv4 address, + // or a bracketed IPv6 address as specified in [RFC2732]. + // + if (NET_IS_DIGIT (TargetAddress[0])) { + // + // The domainname of the target is presented in a dotted-decimal IPv4 address format. + // + IpStr = TargetAddress; + + while ((*TargetAddress != '\0') && (*TargetAddress != ':') && (*TargetAddress != ',')) { + // + // NULL, ':', or ',' ends the IPv4 string. + // + TargetAddress++; + } + } else if (*TargetAddress == ISCSI_REDIRECT_ADDR_START_DELIMITER){ + // + // The domainname of the target is presented in a bracketed IPv6 address format. + // + TargetAddress ++; + IpStr = TargetAddress; + while ((*TargetAddress != '\0') && (*TargetAddress != ISCSI_REDIRECT_ADDR_END_DELIMITER)) { + // + // ']' ends the IPv6 string. + // + TargetAddress++; + } + + if (*TargetAddress != ISCSI_REDIRECT_ADDR_END_DELIMITER) { + continue; + } + + *TargetAddress = '\0'; + TargetAddress ++; + + } else { + // + // The domainname of the target is presented in the format of a DNS host name. + // + IpStr = TargetAddress; + + while ((*TargetAddress != '\0') && (*TargetAddress != ':') && (*TargetAddress != ',')) { + TargetAddress++; + } + NvData->DnsMode = TRUE; + } + + // + // Save the origial user setting which specifies the proxy/virtual iSCSI target. + // + NvData->OriginalTargetPort = NvData->TargetPort; + + if (*TargetAddress == ',') { + // + // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent + // as the result of a redirection. + // + continue; + } else if (*TargetAddress == ':') { + *TargetAddress = '\0'; + + TargetAddress++; + + Number = AsciiStrDecimalToUintn (TargetAddress); + if (Number > 0xFFFF) { + continue; + } else { + NvData->TargetPort = (UINT16) Number; + } + } else { + // + // The string only contains the Target address. Use the well-known port. + // + NvData->TargetPort = ISCSI_WELL_KNOWN_PORT; + } + + // + // Save the origial user setting which specifies the proxy/virtual iSCSI target. + // + CopyMem (&NvData->OriginalTargetIp, &NvData->TargetIp, sizeof (EFI_IP_ADDRESS)); + + // + // Update the target IP address. + // + if (NvData->IpMode < IP_MODE_AUTOCONFIG) { + IpMode = NvData->IpMode; + } else { + IpMode = Session->ConfigData->AutoConfigureMode; + } + + if (NvData->DnsMode) { + // + // Target address is expressed as URL format, just save it and + // do DNS resolution when creating a TCP connection. + // + if (AsciiStrSize (IpStr) > sizeof (Session->ConfigData->SessionConfigData.TargetUrl)){ + return EFI_INVALID_PARAMETER; + } + CopyMem (&Session->ConfigData->SessionConfigData.TargetUrl, IpStr, AsciiStrSize (IpStr)); + } else { + Status = IScsiAsciiStrToIp ( + IpStr, + IpMode, + &Session->ConfigData->SessionConfigData.TargetIp + ); + + if (EFI_ERROR (Status)) { + continue; + } else { + NvData->RedirectFlag = TRUE; + break; + } + } + } + + IScsiFreeKeyValueList (KeyValueList); + + return Status; +} + + +/** + The callback function to free the net buffer list. + + @param[in] Arg The opaque parameter. + +**/ +VOID +EFIAPI +IScsiFreeNbufList ( + VOID *Arg + ) +{ + ASSERT (Arg != NULL); + + NetbufFreeList ((LIST_ENTRY *) Arg); + FreePool (Arg); +} + + +/** + The callback function called in NetBufFree; it does nothing. + + @param[in] Arg The opaque parameter. + +**/ +VOID +EFIAPI +IScsiNbufExtFree ( + VOID *Arg + ) +{ +} + + +/** + Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and + an optional data segment. The two parts will be put into two blocks of buffers in the + net buffer. The digest check will be conducted in this function if needed and the digests + will be trimmed from the PDU buffer. + + @param[in] Conn The iSCSI connection to receive data from. + @param[out] Pdu The received iSCSI pdu. + @param[in] Context The context used to describe information on the caller provided + buffer to receive data segment of the iSCSI pdu. It is optional. + @param[in] HeaderDigest Whether there will be header digest received. + @param[in] DataDigest Whether there will be data digest. + @param[in] TimeoutEvent The timeout event. It is optional. + + @retval EFI_SUCCESS An iSCSI pdu is received. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiReceivePdu ( + IN ISCSI_CONNECTION *Conn, + OUT NET_BUF **Pdu, + IN ISCSI_IN_BUFFER_CONTEXT *Context, OPTIONAL + IN BOOLEAN HeaderDigest, + IN BOOLEAN DataDigest, + IN EFI_EVENT TimeoutEvent OPTIONAL + ) +{ + LIST_ENTRY *NbufList; + UINT32 Len; + NET_BUF *PduHdr; + UINT8 *Header; + EFI_STATUS Status; + UINT32 PadLen; + UINT32 InDataOffset; + NET_FRAGMENT Fragment[2]; + UINT32 FragmentCount; + NET_BUF *DataSeg; + UINT32 PadAndCRC32[2]; + + NbufList = AllocatePool (sizeof (LIST_ENTRY)); + if (NbufList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + InitializeListHead (NbufList); + + // + // The header digest will be received together with the PDU header, if exists. + // + Len = sizeof (ISCSI_BASIC_HEADER) + (HeaderDigest ? sizeof (UINT32) : 0); + PduHdr = NetbufAlloc (Len); + if (PduHdr == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Header = NetbufAllocSpace (PduHdr, Len, NET_BUF_TAIL); + if (Header == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + InsertTailList (NbufList, &PduHdr->List); + + // + // First step, receive the BHS of the PDU. + // + Status = TcpIoReceive (&Conn->TcpIo, PduHdr, FALSE, TimeoutEvent); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + if (HeaderDigest) { + // + // TODO: check the header-digest. + // + // + // Trim off the digest. + // + NetbufTrim (PduHdr, sizeof (UINT32), NET_BUF_TAIL); + } + + Len = ISCSI_GET_DATASEG_LEN (Header); + if (Len == 0) { + // + // No data segment. + // + goto FORM_PDU; + } + // + // Get the length of the padding bytes of the data segment. + // + PadLen = ISCSI_GET_PAD_LEN (Len); + + switch (ISCSI_GET_OPCODE (Header)) { + case ISCSI_OPCODE_SCSI_DATA_IN: + // + // To reduce memory copy overhead, try to use the buffer described by Context + // if the PDU is an iSCSI SCSI data. + // + InDataOffset = ISCSI_GET_BUFFER_OFFSET (Header); + if ((Context == NULL) || ((InDataOffset + Len) > Context->InDataLen)) { + Status = EFI_PROTOCOL_ERROR; + goto ON_EXIT; + } + + Fragment[0].Len = Len; + Fragment[0].Bulk = Context->InData + InDataOffset; + + if (DataDigest || (PadLen != 0)) { + // + // The data segment is padded. Use two fragments to receive it: + // the first to receive the useful data; the second to receive the padding. + // + Fragment[1].Len = PadLen + (DataDigest ? sizeof (UINT32) : 0); + Fragment[1].Bulk = (UINT8 *)PadAndCRC32 + (4 - PadLen); + + FragmentCount = 2; + } else { + FragmentCount = 1; + } + + DataSeg = NetbufFromExt (&Fragment[0], FragmentCount, 0, 0, IScsiNbufExtFree, NULL); + if (DataSeg == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + break; + + case ISCSI_OPCODE_SCSI_RSP: + case ISCSI_OPCODE_NOP_IN: + case ISCSI_OPCODE_LOGIN_RSP: + case ISCSI_OPCODE_TEXT_RSP: + case ISCSI_OPCODE_ASYNC_MSG: + case ISCSI_OPCODE_REJECT: + case ISCSI_OPCODE_VENDOR_T0: + case ISCSI_OPCODE_VENDOR_T1: + case ISCSI_OPCODE_VENDOR_T2: + // + // Allocate buffer to receive the data segment. + // + Len += PadLen + (DataDigest ? sizeof (UINT32) : 0); + DataSeg = NetbufAlloc (Len); + if (DataSeg == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + NetbufAllocSpace (DataSeg, Len, NET_BUF_TAIL); + break; + + default: + Status = EFI_PROTOCOL_ERROR; + goto ON_EXIT; + } + + InsertTailList (NbufList, &DataSeg->List); + + // + // Receive the data segment with the data digest, if any. + // + Status = TcpIoReceive (&Conn->TcpIo, DataSeg, FALSE, TimeoutEvent); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + if (DataDigest) { + // + // TODO: Check the data digest. + // + NetbufTrim (DataSeg, sizeof (UINT32), NET_BUF_TAIL); + } + + if (PadLen != 0) { + // + // Trim off the padding bytes in the data segment. + // + NetbufTrim (DataSeg, PadLen, NET_BUF_TAIL); + } + +FORM_PDU: + // + // Form the pdu from a list of pdu segments. + // + *Pdu = NetbufFromBufList (NbufList, 0, 0, IScsiFreeNbufList, NbufList); + if (*Pdu == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } + +ON_EXIT: + + if (EFI_ERROR (Status)) { + // + // Free the Nbufs in this NbufList and the NbufList itself. + // + IScsiFreeNbufList (NbufList); + } + + return Status; +} + + +/** + Check and get the result of the parameter negotiation. + + @param[in, out] Conn The connection in iSCSI login. + + @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +IScsiCheckOpParams ( + IN OUT ISCSI_CONNECTION *Conn + ) +{ + EFI_STATUS Status; + LIST_ENTRY *KeyValueList; + CHAR8 *Data; + UINT32 Len; + ISCSI_SESSION *Session; + CHAR8 *Value; + UINTN NumericValue; + + ASSERT (Conn->RspQue.BufNum != 0); + + Session = Conn->Session; + + Len = Conn->RspQue.BufSize; + Data = AllocatePool (Len); + if (Data == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + NetbufQueCopy (&Conn->RspQue, 0, Len, (UINT8 *) Data); + + Status = EFI_PROTOCOL_ERROR; + + // + // Extract the Key-Value pairs into a list. + // + KeyValueList = IScsiBuildKeyValueList (Data, Len); + if (KeyValueList == NULL) { + FreePool (Data); + return Status; + } + // + // HeaderDigest + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_HEADER_DIGEST); + if (Value == NULL) { + goto ON_ERROR; + } + + if (AsciiStrCmp (Value, "CRC32") == 0) { + if (Conn->HeaderDigest != IScsiDigestCRC32) { + goto ON_ERROR; + } + } else if (AsciiStrCmp (Value, ISCSI_KEY_VALUE_NONE) == 0) { + Conn->HeaderDigest = IScsiDigestNone; + } else { + goto ON_ERROR; + } + // + // DataDigest + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DATA_DIGEST); + if (Value == NULL) { + goto ON_ERROR; + } + + if (AsciiStrCmp (Value, "CRC32") == 0) { + if (Conn->DataDigest != IScsiDigestCRC32) { + goto ON_ERROR; + } + } else if (AsciiStrCmp (Value, ISCSI_KEY_VALUE_NONE) == 0) { + Conn->DataDigest = IScsiDigestNone; + } else { + goto ON_ERROR; + } + // + // ErrorRecoveryLevel: result fuction is Minimum. + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_ERROR_RECOVERY_LEVEL); + if (Value == NULL) { + goto ON_ERROR; + } + + NumericValue = IScsiNetNtoi (Value); + if (NumericValue > 2) { + goto ON_ERROR; + } + + Session->ErrorRecoveryLevel = (UINT8) MIN (Session->ErrorRecoveryLevel, NumericValue); + + // + // InitialR2T: result function is OR. + // + if (!Session->InitialR2T) { + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_INITIAL_R2T); + if (Value == NULL) { + goto ON_ERROR; + } + + Session->InitialR2T = (BOOLEAN) (AsciiStrCmp (Value, "Yes") == 0); + } + + // + // ImmediateData: result function is AND. + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_IMMEDIATE_DATA); + if (Value == NULL) { + goto ON_ERROR; + } + + Session->ImmediateData = (BOOLEAN) (Session->ImmediateData && (BOOLEAN) (AsciiStrCmp (Value, "Yes") == 0)); + + // + // MaxRecvDataSegmentLength is declarative. + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH); + if (Value != NULL) { + Conn->MaxRecvDataSegmentLength = (UINT32) IScsiNetNtoi (Value); + } + // + // MaxBurstLength: result funtion is Mininum. + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_MAX_BURST_LENGTH); + if (Value == NULL) { + goto ON_ERROR; + } + + NumericValue = IScsiNetNtoi (Value); + Session->MaxBurstLength = (UINT32) MIN (Session->MaxBurstLength, NumericValue); + + // + // FirstBurstLength: result function is Minimum. Irrelevant when InitialR2T=Yes and + // ImmediateData=No. + // + if (!(Session->InitialR2T && !Session->ImmediateData)) { + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_FIRST_BURST_LENGTH); + if (Value == NULL) { + goto ON_ERROR; + } + + NumericValue = IScsiNetNtoi (Value); + Session->FirstBurstLength = (UINT32) MIN (Session->FirstBurstLength, NumericValue); + } + + // + // MaxConnections: result function is Minimum. + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_MAX_CONNECTIONS); + if (Value == NULL) { + goto ON_ERROR; + } + + NumericValue = IScsiNetNtoi (Value); + if ((NumericValue == 0) || (NumericValue > 65535)) { + goto ON_ERROR; + } + + Session->MaxConnections = (UINT32) MIN (Session->MaxConnections, NumericValue); + + // + // DataPDUInOrder: result function is OR. + // + if (!Session->DataPDUInOrder) { + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DATA_PDU_IN_ORDER); + if (Value == NULL) { + goto ON_ERROR; + } + + Session->DataPDUInOrder = (BOOLEAN) (AsciiStrCmp (Value, "Yes") == 0); + } + + // + // DataSequenceInorder: result function is OR. + // + if (!Session->DataSequenceInOrder) { + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER); + if (Value == NULL) { + goto ON_ERROR; + } + + Session->DataSequenceInOrder = (BOOLEAN) (AsciiStrCmp (Value, "Yes") == 0); + } + + // + // DefaultTime2Wait: result function is Maximum. + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DEFAULT_TIME2WAIT); + if (Value == NULL) { + goto ON_ERROR; + } + + NumericValue = IScsiNetNtoi (Value); + if (NumericValue == 0) { + Session->DefaultTime2Wait = 0; + } else if (NumericValue > 3600) { + goto ON_ERROR; + } else { + Session->DefaultTime2Wait = (UINT32) MAX (Session->DefaultTime2Wait, NumericValue); + } + // + // DefaultTime2Retain: result function is Minimum. + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DEFAULT_TIME2RETAIN); + if (Value == NULL) { + goto ON_ERROR; + } + + NumericValue = IScsiNetNtoi (Value); + if (NumericValue == 0) { + Session->DefaultTime2Retain = 0; + } else if (NumericValue > 3600) { + goto ON_ERROR; + } else { + Session->DefaultTime2Retain = (UINT32) MIN (Session->DefaultTime2Retain, NumericValue); + } + // + // MaxOutstandingR2T: result function is Minimum. + // + Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_MAX_OUTSTANDING_R2T); + if (Value == NULL) { + goto ON_ERROR; + } + + NumericValue = IScsiNetNtoi (Value); + if ((NumericValue == 0) || (NumericValue > 65535)) { + goto ON_ERROR; + } + + Session->MaxOutstandingR2T = (UINT16) MIN (Session->MaxOutstandingR2T, NumericValue); + + // + // Remove declarative key-value pairs, if any. + // + IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_SESSION_TYPE); + IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_TARGET_ALIAS); + IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG); + + + // + // Remove the key-value that may not needed for result function is OR. + // + IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_INITIAL_R2T); + IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DATA_PDU_IN_ORDER); + IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER); + + // + // Remove irrelevant parameter, if any. + // + if (Session->InitialR2T && !Session->ImmediateData) { + IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_FIRST_BURST_LENGTH); + } + + if (IsListEmpty (KeyValueList)) { + // + // Succeed if no more keys in the list. + // + Status = EFI_SUCCESS; + } + +ON_ERROR: + + IScsiFreeKeyValueList (KeyValueList); + + FreePool (Data); + + return Status; +} + + +/** + Fill the operational parameters. + + @param[in] Conn The connection in iSCSI login. + @param[in, out] Pdu The iSCSI login request PDU to fill the parameters. + +**/ +VOID +IScsiFillOpParams ( + IN ISCSI_CONNECTION *Conn, + IN OUT NET_BUF *Pdu + ) +{ + ISCSI_SESSION *Session; + CHAR8 Value[256]; + + Session = Conn->Session; + + AsciiSPrint (Value, sizeof (Value), "%a", (Conn->HeaderDigest == IScsiDigestCRC32) ? "None,CRC32" : "None"); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_HEADER_DIGEST, Value); + + AsciiSPrint (Value, sizeof (Value), "%a", (Conn->DataDigest == IScsiDigestCRC32) ? "None,CRC32" : "None"); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_DATA_DIGEST, Value); + + AsciiSPrint (Value, sizeof (Value), "%d", Session->ErrorRecoveryLevel); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_ERROR_RECOVERY_LEVEL, Value); + + AsciiSPrint (Value, sizeof (Value), "%a", Session->InitialR2T ? "Yes" : "No"); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_INITIAL_R2T, Value); + + AsciiSPrint (Value, sizeof (Value), "%a", Session->ImmediateData ? "Yes" : "No"); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_IMMEDIATE_DATA, Value); + + AsciiSPrint (Value, sizeof (Value), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH, Value); + + AsciiSPrint (Value, sizeof (Value), "%d", Session->MaxBurstLength); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_MAX_BURST_LENGTH, Value); + + AsciiSPrint (Value, sizeof (Value), "%d", Session->FirstBurstLength); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_FIRST_BURST_LENGTH, Value); + + AsciiSPrint (Value, sizeof (Value), "%d", Session->MaxConnections); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_MAX_CONNECTIONS, Value); + + AsciiSPrint (Value, sizeof (Value), "%a", Session->DataPDUInOrder ? "Yes" : "No"); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_DATA_PDU_IN_ORDER, Value); + + AsciiSPrint (Value, sizeof (Value), "%a", Session->DataSequenceInOrder ? "Yes" : "No"); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER, Value); + + AsciiSPrint (Value, sizeof (Value), "%d", Session->DefaultTime2Wait); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_DEFAULT_TIME2WAIT, Value); + + AsciiSPrint (Value, sizeof (Value), "%d", Session->DefaultTime2Retain); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_DEFAULT_TIME2RETAIN, Value); + + AsciiSPrint (Value, sizeof (Value), "%d", Session->MaxOutstandingR2T); + IScsiAddKeyValuePair (Pdu, ISCSI_KEY_MAX_OUTSTANDING_R2T, Value); +} + + +/** + Pad the iSCSI AHS or data segment to an integer number of 4 byte words. + + @param[in, out] Pdu The iSCSI pdu which contains segments to pad. + @param[in] Len The length of the last segment in the PDU. + + @retval EFI_SUCCESS The segment is padded or there is no need to pad it. + @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the + padding bytes. +**/ +EFI_STATUS +IScsiPadSegment ( + IN OUT NET_BUF *Pdu, + IN UINT32 Len + ) +{ + UINT32 PadLen; + UINT8 *Data; + + PadLen = ISCSI_GET_PAD_LEN (Len); + + if (PadLen != 0) { + Data = NetbufAllocSpace (Pdu, PadLen, NET_BUF_TAIL); + if (Data == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + ZeroMem (Data, PadLen); + } + + return EFI_SUCCESS; +} + + +/** + Build a key-value list from the data segment. + + @param[in] Data The data segment containing the key-value pairs. + @param[in] Len Length of the data segment. + + @return The key-value list. + @retval NULL Other errors as indicated. + +**/ +LIST_ENTRY * +IScsiBuildKeyValueList ( + IN CHAR8 *Data, + IN UINT32 Len + ) +{ + LIST_ENTRY *ListHead; + ISCSI_KEY_VALUE_PAIR *KeyValuePair; + + ListHead = AllocatePool (sizeof (LIST_ENTRY)); + if (ListHead == NULL) { + return NULL; + } + + InitializeListHead (ListHead); + + while (Len > 0) { + KeyValuePair = AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR)); + if (KeyValuePair == NULL) { + goto ON_ERROR; + } + + InitializeListHead (&KeyValuePair->List); + + KeyValuePair->Key = Data; + + while ((Len > 0) && (*Data != '=')) { + Len--; + Data++; + } + + if (*Data == '=') { + *Data = '\0'; + + Data++; + Len--; + } else { + FreePool (KeyValuePair); + goto ON_ERROR; + } + + KeyValuePair->Value = Data; + + InsertTailList (ListHead, &KeyValuePair->List);; + + Data += AsciiStrLen (KeyValuePair->Value) + 1; + Len -= (UINT32) AsciiStrLen (KeyValuePair->Value) + 1; + } + + return ListHead; + +ON_ERROR: + + IScsiFreeKeyValueList (ListHead); + + return NULL; +} + + +/** + Get the value string by the key name from the key-value list. If found, + the key-value entry will be removed from the list. + + @param[in, out] KeyValueList The key-value list. + @param[in] Key The key name to find. + + @return The value string. + @retval NULL The key value pair cannot be found. + +**/ +CHAR8 * +IScsiGetValueByKeyFromList ( + IN OUT LIST_ENTRY *KeyValueList, + IN CHAR8 *Key + ) +{ + LIST_ENTRY *Entry; + ISCSI_KEY_VALUE_PAIR *KeyValuePair; + CHAR8 *Value; + + Value = NULL; + + NET_LIST_FOR_EACH (Entry, KeyValueList) { + KeyValuePair = NET_LIST_USER_STRUCT (Entry, ISCSI_KEY_VALUE_PAIR, List); + + if (AsciiStrCmp (KeyValuePair->Key, Key) == 0) { + Value = KeyValuePair->Value; + + RemoveEntryList (&KeyValuePair->List); + FreePool (KeyValuePair); + break; + } + } + + return Value; +} + + +/** + Free the key-value list. + + @param[in] KeyValueList The key-value list. + +**/ +VOID +IScsiFreeKeyValueList ( + IN LIST_ENTRY *KeyValueList + ) +{ + LIST_ENTRY *Entry; + ISCSI_KEY_VALUE_PAIR *KeyValuePair; + + while (!IsListEmpty (KeyValueList)) { + Entry = NetListRemoveHead (KeyValueList); + KeyValuePair = NET_LIST_USER_STRUCT (Entry, ISCSI_KEY_VALUE_PAIR, List); + + FreePool (KeyValuePair); + } + + FreePool (KeyValueList); +} + + +/** + Normalize the iSCSI name according to RFC. + + @param[in, out] Name The iSCSI name. + @param[in] Len Length of the iSCSI name. + + @retval EFI_SUCCESS The iSCSI name is valid and normalized. + @retval EFI_PROTOCOL_ERROR The iSCSI name is malformatted or not in the IQN format. + +**/ +EFI_STATUS +IScsiNormalizeName ( + IN OUT CHAR8 *Name, + IN UINTN Len + ) +{ + UINTN Index; + + for (Index = 0; Index < Len; Index++) { + if (NET_IS_UPPER_CASE_CHAR (Name[Index])) { + // + // Convert the upper-case characters to lower-case ones. + // + Name[Index] = (CHAR8) (Name[Index] - 'A' + 'a'); + } + + if (!NET_IS_LOWER_CASE_CHAR (Name[Index]) && + !NET_IS_DIGIT (Name[Index]) && + (Name[Index] != '-') && + (Name[Index] != '.') && + (Name[Index] != ':') + ) { + // + // ASCII dash, dot, colon lower-case characters and digit characters + // are allowed. + // + return EFI_PROTOCOL_ERROR; + } + } + + if ((Len < 4) || (CompareMem (Name, "iqn.", 4) != 0)) { + // + // Only IQN format is accepted now. + // + return EFI_PROTOCOL_ERROR; + } + + return EFI_SUCCESS; +} + + +/** + Create an iSCSI task control block. + + @param[in] Conn The connection on which the task control block will be created. + @param[out] Tcb The newly created task control block. + + @retval EFI_SUCCESS The task control block is created. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NOT_READY The target cannot accept new commands. + +**/ +EFI_STATUS +IScsiNewTcb ( + IN ISCSI_CONNECTION *Conn, + OUT ISCSI_TCB **Tcb + ) +{ + ISCSI_SESSION *Session; + ISCSI_TCB *NewTcb; + + ASSERT (Tcb != NULL); + + Session = Conn->Session; + + if (ISCSI_SEQ_GT (Session->CmdSN, Session->MaxCmdSN)) { + return EFI_NOT_READY; + } + + NewTcb = AllocateZeroPool (sizeof (ISCSI_TCB)); + if (NewTcb == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + InitializeListHead (&NewTcb->Link); + + NewTcb->SoFarInOrder = TRUE; + NewTcb->InitiatorTaskTag = Session->InitiatorTaskTag; + NewTcb->CmdSN = Session->CmdSN; + NewTcb->Conn = Conn; + + InsertTailList (&Session->TcbList, &NewTcb->Link); + + // + // Advance the initiator task tag. + // + Session->InitiatorTaskTag++; + Session->CmdSN++; + + *Tcb = NewTcb; + + return EFI_SUCCESS; +} + + +/** + Delete the tcb from the connection and destroy it. + + @param[in] Tcb The tcb to delete. + +**/ +VOID +IScsiDelTcb ( + IN ISCSI_TCB *Tcb + ) +{ + RemoveEntryList (&Tcb->Link); + + FreePool (Tcb); +} + + +/** + Create a data segment, pad it, and calculate the CRC if needed. + + @param[in] Data The data to fill into the data segment. + @param[in] Len Length of the data. + @param[in] DataDigest Whether to calculate CRC for this data segment. + + @return The net buffer wrapping the data segment. + +**/ +NET_BUF * +IScsiNewDataSegment ( + IN UINT8 *Data, + IN UINT32 Len, + IN BOOLEAN DataDigest + ) +{ + NET_FRAGMENT Fragment[2]; + UINT32 FragmentCount; + UINT32 PadLen; + NET_BUF *DataSeg; + + Fragment[0].Len = Len; + Fragment[0].Bulk = Data; + + PadLen = ISCSI_GET_PAD_LEN (Len); + if (PadLen != 0) { + Fragment[1].Len = PadLen; + Fragment[1].Bulk = (UINT8 *) &mDataSegPad; + + FragmentCount = 2; + } else { + FragmentCount = 1; + } + + DataSeg = NetbufFromExt (&Fragment[0], FragmentCount, 0, 0, IScsiNbufExtFree, NULL); + + return DataSeg; +} + + +/** + Create a iSCSI SCSI command PDU to encapsulate the command issued + by SCSI through the EXT SCSI PASS THRU Protocol. + + @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command. + @param[in] Lun The LUN. + @param[in] Tcb The tcb associated with this SCSI command. + + @return The created iSCSI SCSI command PDU. + @retval NULL Other errors as indicated. + +**/ +NET_BUF * +IScsiNewScsiCmdPdu ( + IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet, + IN UINT64 Lun, + IN ISCSI_TCB *Tcb + ) +{ + LIST_ENTRY *NbufList; + NET_BUF *Pdu; + NET_BUF *PduHeader; + NET_BUF *DataSeg; + SCSI_COMMAND *ScsiCmd; + UINT8 AHSLength; + UINT32 Length; + ISCSI_ADDITIONAL_HEADER *Header; + ISCSI_BI_EXP_READ_DATA_LEN_AHS *BiExpReadDataLenAHS; + ISCSI_SESSION *Session; + UINT32 ImmediateDataLen; + + AHSLength = 0; + + if (Packet->DataDirection == DataBi) { + // + // Bidirectional Read/Write command, the bidirectional expected + // read data length AHS is required. + // + AHSLength += sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS); + } + + if (Packet->CdbLength > 16) { + // + // The CDB exceeds 16 bytes. An extended CDB AHS is required. + // + AHSLength = (UINT8) (AHSLength + ISCSI_ROUNDUP (Packet->CdbLength - 16) + sizeof (ISCSI_ADDITIONAL_HEADER)); + } + + Length = sizeof (SCSI_COMMAND) + AHSLength; + PduHeader = NetbufAlloc (Length); + if (PduHeader == NULL) { + return NULL; + } + + ScsiCmd = (SCSI_COMMAND *) NetbufAllocSpace (PduHeader, Length, NET_BUF_TAIL); + if (ScsiCmd == NULL) { + NetbufFree (PduHeader); + return NULL; + } + Header = (ISCSI_ADDITIONAL_HEADER *) (ScsiCmd + 1); + + ZeroMem (ScsiCmd, Length); + + ISCSI_SET_OPCODE (ScsiCmd, ISCSI_OPCODE_SCSI_CMD, 0); + ISCSI_SET_FLAG (ScsiCmd, ISCSI_TASK_ATTR_SIMPLE); + + // + // Set the READ/WRITE flags according to the IO type of this request. + // + switch (Packet->DataDirection) { + case DataIn: + ISCSI_SET_FLAG (ScsiCmd, SCSI_CMD_PDU_FLAG_READ); + ScsiCmd->ExpDataXferLength = NTOHL (Packet->InTransferLength); + break; + + case DataOut: + ISCSI_SET_FLAG (ScsiCmd, SCSI_CMD_PDU_FLAG_WRITE); + ScsiCmd->ExpDataXferLength = NTOHL (Packet->OutTransferLength); + break; + + case DataBi: + ISCSI_SET_FLAG (ScsiCmd, SCSI_CMD_PDU_FLAG_READ | SCSI_CMD_PDU_FLAG_WRITE); + ScsiCmd->ExpDataXferLength = NTOHL (Packet->OutTransferLength); + + // + // Fill the bidirectional expected read data length AHS. + // + BiExpReadDataLenAHS = (ISCSI_BI_EXP_READ_DATA_LEN_AHS *) Header; + Header = (ISCSI_ADDITIONAL_HEADER *) (BiExpReadDataLenAHS + 1); + + BiExpReadDataLenAHS->Length = NTOHS (5); + BiExpReadDataLenAHS->Type = ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN; + BiExpReadDataLenAHS->ExpReadDataLength = NTOHL (Packet->InTransferLength); + + break; + } + + ScsiCmd->TotalAHSLength = AHSLength; + CopyMem (ScsiCmd->Lun, &Lun, sizeof (ScsiCmd->Lun)); + ScsiCmd->InitiatorTaskTag = NTOHL (Tcb->InitiatorTaskTag); + ScsiCmd->CmdSN = NTOHL (Tcb->CmdSN); + ScsiCmd->ExpStatSN = NTOHL (Tcb->Conn->ExpStatSN); + + CopyMem (ScsiCmd->Cdb, Packet->Cdb, sizeof (ScsiCmd->Cdb)); + + if (Packet->CdbLength > 16) { + Header->Length = NTOHS ((UINT16) (Packet->CdbLength - 15)); + Header->Type = ISCSI_AHS_TYPE_EXT_CDB; + + CopyMem (Header + 1, (UINT8 *) Packet->Cdb + 16, Packet->CdbLength - 16); + } + + Pdu = PduHeader; + Session = Tcb->Conn->Session; + ImmediateDataLen = 0; + + if (Session->ImmediateData && (Packet->OutTransferLength != 0)) { + // + // Send immediate data in this SCSI Command PDU. The length of the immeidate + // data is the minimum of FirstBurstLength, the data length to be xfered, and + // the MaxRecvdataSegmentLength on this connection. + // + ImmediateDataLen = MIN (Session->FirstBurstLength, Packet->OutTransferLength); + ImmediateDataLen = MIN (ImmediateDataLen, Tcb->Conn->MaxRecvDataSegmentLength); + + // + // Update the data segment length in the PDU header. + // + ISCSI_SET_DATASEG_LEN (ScsiCmd, ImmediateDataLen); + + // + // Create the data segment. + // + DataSeg = IScsiNewDataSegment ((UINT8 *) Packet->OutDataBuffer, ImmediateDataLen, FALSE); + if (DataSeg == NULL) { + NetbufFree (PduHeader); + Pdu = NULL; + goto ON_EXIT; + } + + NbufList = AllocatePool (sizeof (LIST_ENTRY)); + if (NbufList == NULL) { + NetbufFree (PduHeader); + NetbufFree (DataSeg); + + Pdu = NULL; + goto ON_EXIT; + } + + InitializeListHead (NbufList); + InsertTailList (NbufList, &PduHeader->List); + InsertTailList (NbufList, &DataSeg->List); + + Pdu = NetbufFromBufList (NbufList, 0, 0, IScsiFreeNbufList, NbufList); + if (Pdu == NULL) { + IScsiFreeNbufList (NbufList); + } + } + + if (Session->InitialR2T || + (ImmediateDataLen == Session->FirstBurstLength) || + (ImmediateDataLen == Packet->OutTransferLength) + ) { + // + // Unsolicited data out sequence is not allowed, + // or FirstBustLength data is already sent out by immediate data, + // or all the OUT data accompany this SCSI packet are sent as + // immediate data. The final flag should be set on this SCSI Command + // PDU. + // + ISCSI_SET_FLAG (ScsiCmd, ISCSI_BHS_FLAG_FINAL); + } + +ON_EXIT: + + return Pdu; +} + + +/** + Create a new iSCSI SCSI Data Out PDU. + + @param[in] Data The data to put into the Data Out PDU. + @param[in] Len Length of the data. + @param[in] DataSN The DataSN of the Data Out PDU. + @param[in] Tcb The task control block of this Data Out PDU. + @param[in] Lun The LUN. + + @return The net buffer wrapping the Data Out PDU. + @retval NULL Other errors as indicated. + +**/ +NET_BUF * +IScsiNewDataOutPdu ( + IN UINT8 *Data, + IN UINT32 Len, + IN UINT32 DataSN, + IN ISCSI_TCB *Tcb, + IN UINT64 Lun + ) +{ + LIST_ENTRY *NbufList; + NET_BUF *PduHdr; + NET_BUF *DataSeg; + NET_BUF *Pdu; + ISCSI_SCSI_DATA_OUT *DataOutHdr; + ISCSI_XFER_CONTEXT *XferContext; + + NbufList = AllocatePool (sizeof (LIST_ENTRY)); + if (NbufList == NULL) { + return NULL; + } + + InitializeListHead (NbufList); + + // + // Allocate memory for the BHS. + // + PduHdr = NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT)); + if (PduHdr == NULL) { + FreePool (NbufList); + return NULL; + } + // + // Insert the BHS into the buffer list. + // + InsertTailList (NbufList, &PduHdr->List); + + DataOutHdr = (ISCSI_SCSI_DATA_OUT *) NetbufAllocSpace (PduHdr, sizeof (ISCSI_SCSI_DATA_OUT), NET_BUF_TAIL); + if (DataOutHdr == NULL) { + IScsiFreeNbufList (NbufList); + return NULL; + } + XferContext = &Tcb->XferContext; + + ZeroMem (DataOutHdr, sizeof (ISCSI_SCSI_DATA_OUT)); + + // + // Set the flags and fields of the Data Out PDU BHS. + // + ISCSI_SET_OPCODE (DataOutHdr, ISCSI_OPCODE_SCSI_DATA_OUT, 0); + ISCSI_SET_DATASEG_LEN (DataOutHdr, Len); + + DataOutHdr->InitiatorTaskTag = HTONL (Tcb->InitiatorTaskTag); + DataOutHdr->TargetTransferTag = HTONL (XferContext->TargetTransferTag); + DataOutHdr->ExpStatSN = HTONL (Tcb->Conn->ExpStatSN); + DataOutHdr->DataSN = HTONL (DataSN); + DataOutHdr->BufferOffset = HTONL (XferContext->Offset); + + if (XferContext->TargetTransferTag != ISCSI_RESERVED_TAG) { + CopyMem (&DataOutHdr->Lun, &Lun, sizeof (DataOutHdr->Lun)); + } + // + // Build the data segment for this Data Out PDU. + // + DataSeg = IScsiNewDataSegment (Data, Len, FALSE); + if (DataSeg == NULL) { + IScsiFreeNbufList (NbufList); + return NULL; + } + // + // Put the data segment into the buffer list and combine it with the BHS + // into a full Data Out PDU. + // + InsertTailList (NbufList, &DataSeg->List); + Pdu = NetbufFromBufList (NbufList, 0, 0, IScsiFreeNbufList, NbufList); + if (Pdu == NULL) { + IScsiFreeNbufList (NbufList); + } + + return Pdu; +} + + +/** + Generate a consecutive sequence of iSCSI SCSI Data Out PDUs. + + @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs. + @param[in] Tcb The task control block of the data to send out. + @param[in] Lun The LUN the data will be sent to. + + @return A list of net buffers with each of them wrapping an iSCSI SCSI Data Out PDU. + @retval NULL Other errors as indicated. + +**/ +LIST_ENTRY * +IScsiGenerateDataOutPduSequence ( + IN UINT8 *Data, + IN ISCSI_TCB *Tcb, + IN UINT64 Lun + ) +{ + LIST_ENTRY *PduList; + UINT32 DataSN; + UINT32 DataLen; + NET_BUF *DataOutPdu; + ISCSI_CONNECTION *Conn; + ISCSI_XFER_CONTEXT *XferContext; + UINT8 *DataOutPacket; + + PduList = AllocatePool (sizeof (LIST_ENTRY)); + if (PduList == NULL) { + return NULL; + } + + InitializeListHead (PduList); + + DataSN = 0; + Conn = Tcb->Conn; + DataOutPdu = NULL; + XferContext = &Tcb->XferContext; + + while (XferContext->DesiredLength > 0) { + // + // Determine the length of data this Data Out PDU can carry. + // + DataLen = MIN (XferContext->DesiredLength, Conn->MaxRecvDataSegmentLength); + + // + // Create a Data Out PDU. + // + DataOutPdu = IScsiNewDataOutPdu (Data, DataLen, DataSN, Tcb, Lun); + if (DataOutPdu == NULL) { + IScsiFreeNbufList (PduList); + PduList = NULL; + + goto ON_EXIT; + } + + InsertTailList (PduList, &DataOutPdu->List); + + // + // Update the context and DataSN. + // + Data += DataLen; + XferContext->Offset += DataLen; + XferContext->DesiredLength -= DataLen; + DataSN++; + } + // + // Set the F bit for the last data out PDU in this sequence. + // + DataOutPacket = NetbufGetByte (DataOutPdu, 0, NULL); + if (DataOutPacket == NULL) { + IScsiFreeNbufList (PduList); + PduList = NULL; + goto ON_EXIT; + } + + ISCSI_SET_FLAG (DataOutPacket, ISCSI_BHS_FLAG_FINAL); + +ON_EXIT: + + return PduList; +} + +/** + Send the Data in a sequence of Data Out PDUs one by one. + + @param[in] Data The data to carry by Data Out PDUs. + @param[in] Lun The LUN the data will be sent to. + @param[in] Tcb The task control block. + + @retval EFI_SUCCES The data is sent out to the LUN. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiSendDataOutPduSequence ( + IN UINT8 *Data, + IN UINT64 Lun, + IN ISCSI_TCB *Tcb + ) +{ + LIST_ENTRY *DataOutPduList; + LIST_ENTRY *Entry; + NET_BUF *Pdu; + EFI_STATUS Status; + + // + // Generate the Data Out PDU sequence. + // + DataOutPduList = IScsiGenerateDataOutPduSequence (Data, Tcb, Lun); + if (DataOutPduList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = EFI_SUCCESS; + + // + // Send the Data Out PDU's one by one. + // + NET_LIST_FOR_EACH (Entry, DataOutPduList) { + Pdu = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + + Status = TcpIoTransmit (&Tcb->Conn->TcpIo, Pdu); + + if (EFI_ERROR (Status)) { + break; + } + } + + IScsiFreeNbufList (DataOutPduList); + + return Status; +} + + +/** + Process the received iSCSI SCSI Data In PDU. + + @param[in] Pdu The Data In PDU received. + @param[in] Tcb The task control block. + @param[in, out] Packet The EXT SCSI PASS THRU request packet. + + @retval EFI_SUCCES The check on the Data IN PDU is passed and some update + actions are taken. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred. + @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiOnDataInRcvd ( + IN NET_BUF *Pdu, + IN ISCSI_TCB *Tcb, + IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet + ) +{ + ISCSI_SCSI_DATA_IN *DataInHdr; + EFI_STATUS Status; + + DataInHdr = (ISCSI_SCSI_DATA_IN *) NetbufGetByte (Pdu, 0, NULL); + if (DataInHdr == NULL) { + return EFI_PROTOCOL_ERROR; + } + + DataInHdr->InitiatorTaskTag = NTOHL (DataInHdr->InitiatorTaskTag); + DataInHdr->ExpCmdSN = NTOHL (DataInHdr->ExpCmdSN); + DataInHdr->MaxCmdSN = NTOHL (DataInHdr->MaxCmdSN); + DataInHdr->DataSN = NTOHL (DataInHdr->DataSN); + + // + // Check the DataSN. + // + Status = IScsiCheckSN (&Tcb->ExpDataSN, DataInHdr->DataSN); + if (EFI_ERROR (Status)) { + return Status; + } + + if (DataInHdr->InitiatorTaskTag != Tcb->InitiatorTaskTag) { + return EFI_PROTOCOL_ERROR; + } + // + // Update the command related sequence numbers. + // + IScsiUpdateCmdSN (Tcb->Conn->Session, DataInHdr->MaxCmdSN, DataInHdr->ExpCmdSN); + + if (ISCSI_FLAG_ON (DataInHdr, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID)) { + if (!ISCSI_FLAG_ON (DataInHdr, ISCSI_BHS_FLAG_FINAL)) { + // + // The S bit is on but the F bit is off. + // + return EFI_PROTOCOL_ERROR; + } + + Tcb->StatusXferd = TRUE; + + if (ISCSI_FLAG_ON (DataInHdr, SCSI_DATA_IN_PDU_FLAG_OVERFLOW | SCSI_DATA_IN_PDU_FLAG_UNDERFLOW)) { + // + // Underflow and Overflow are mutual flags. + // + return EFI_PROTOCOL_ERROR; + } + // + // S bit is on, the StatSN is valid. + // + Status = IScsiCheckSN (&Tcb->Conn->ExpStatSN, NTOHL (DataInHdr->StatSN)); + if (EFI_ERROR (Status)) { + return Status; + } + + Packet->HostAdapterStatus = 0; + Packet->TargetStatus = DataInHdr->Status; + + if (ISCSI_FLAG_ON (DataInHdr, SCSI_RSP_PDU_FLAG_OVERFLOW)) { + Packet->InTransferLength += NTOHL (DataInHdr->ResidualCount); + Status = EFI_BAD_BUFFER_SIZE; + } + + if (ISCSI_FLAG_ON (DataInHdr, SCSI_RSP_PDU_FLAG_UNDERFLOW)) { + Packet->InTransferLength -= NTOHL (DataInHdr->ResidualCount); + } + } + + return Status; +} + + +/** + Process the received iSCSI R2T PDU. + + @param[in] Pdu The R2T PDU received. + @param[in] Tcb The task control block. + @param[in] Lun The Lun. + @param[in, out] Packet The EXT SCSI PASS THRU request packet. + + @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiOnR2TRcvd ( + IN NET_BUF *Pdu, + IN ISCSI_TCB *Tcb, + IN UINT64 Lun, + IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet + ) +{ + ISCSI_READY_TO_TRANSFER *R2THdr; + EFI_STATUS Status; + ISCSI_XFER_CONTEXT *XferContext; + UINT8 *Data; + + R2THdr = (ISCSI_READY_TO_TRANSFER *) NetbufGetByte (Pdu, 0, NULL); + if (R2THdr == NULL) { + return EFI_PROTOCOL_ERROR; + } + + R2THdr->InitiatorTaskTag = NTOHL (R2THdr->InitiatorTaskTag); + R2THdr->TargetTransferTag = NTOHL (R2THdr->TargetTransferTag); + R2THdr->StatSN = NTOHL (R2THdr->StatSN); + R2THdr->R2TSeqNum = NTOHL (R2THdr->R2TSeqNum); + R2THdr->BufferOffset = NTOHL (R2THdr->BufferOffset); + R2THdr->DesiredDataTransferLength = NTOHL (R2THdr->DesiredDataTransferLength); + + if ((R2THdr->InitiatorTaskTag != Tcb->InitiatorTaskTag) || !ISCSI_SEQ_EQ (R2THdr->StatSN, Tcb->Conn->ExpStatSN)) { + return EFI_PROTOCOL_ERROR;; + } + // + // Check the sequence number. + // + Status = IScsiCheckSN (&Tcb->ExpDataSN, R2THdr->R2TSeqNum); + if (EFI_ERROR (Status)) { + return Status; + } + + XferContext = &Tcb->XferContext; + XferContext->TargetTransferTag = R2THdr->TargetTransferTag; + XferContext->Offset = R2THdr->BufferOffset; + XferContext->DesiredLength = R2THdr->DesiredDataTransferLength; + + if (((XferContext->Offset + XferContext->DesiredLength) > Packet->OutTransferLength) || + (XferContext->DesiredLength > Tcb->Conn->Session->MaxBurstLength) + ) { + return EFI_PROTOCOL_ERROR; + } + // + // Send the data solicited by this R2T. + // + Data = (UINT8 *) Packet->OutDataBuffer + XferContext->Offset; + Status = IScsiSendDataOutPduSequence (Data, Lun, Tcb); + + return Status; +} + + +/** + Process the received iSCSI SCSI Response PDU. + + @param[in] Pdu The Response PDU received. + @param[in] Tcb The task control block. + @param[in, out] Packet The EXT SCSI PASS THRU request packet. + + @retval EFI_SUCCES The Response PDU is processed. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred. + @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiOnScsiRspRcvd ( + IN NET_BUF *Pdu, + IN ISCSI_TCB *Tcb, + IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet + ) +{ + SCSI_RESPONSE *ScsiRspHdr; + ISCSI_SENSE_DATA *SenseData; + EFI_STATUS Status; + UINT32 DataSegLen; + + ScsiRspHdr = (SCSI_RESPONSE *) NetbufGetByte (Pdu, 0, NULL); + if (ScsiRspHdr == NULL) { + return EFI_PROTOCOL_ERROR; + } + + ScsiRspHdr->InitiatorTaskTag = NTOHL (ScsiRspHdr->InitiatorTaskTag); + if (ScsiRspHdr->InitiatorTaskTag != Tcb->InitiatorTaskTag) { + return EFI_PROTOCOL_ERROR; + } + + ScsiRspHdr->StatSN = NTOHL (ScsiRspHdr->StatSN); + + Status = IScsiCheckSN (&Tcb->Conn->ExpStatSN, ScsiRspHdr->StatSN); + if (EFI_ERROR (Status)) { + return Status; + } + + ScsiRspHdr->MaxCmdSN = NTOHL (ScsiRspHdr->MaxCmdSN); + ScsiRspHdr->ExpCmdSN = NTOHL (ScsiRspHdr->ExpCmdSN); + IScsiUpdateCmdSN (Tcb->Conn->Session, ScsiRspHdr->MaxCmdSN, ScsiRspHdr->ExpCmdSN); + + Tcb->StatusXferd = TRUE; + + Packet->HostAdapterStatus = ScsiRspHdr->Response; + if (Packet->HostAdapterStatus != ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET) { + return EFI_SUCCESS; + } + + Packet->TargetStatus = ScsiRspHdr->Status; + + if (ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW | SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW) || + ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_OVERFLOW | SCSI_RSP_PDU_FLAG_UNDERFLOW) + ) { + return EFI_PROTOCOL_ERROR; + } + + if (ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW)) { + Packet->InTransferLength += NTOHL (ScsiRspHdr->BiReadResidualCount); + Status = EFI_BAD_BUFFER_SIZE; + } + + if (ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW)) { + Packet->InTransferLength -= NTOHL (ScsiRspHdr->BiReadResidualCount); + } + + if (ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_OVERFLOW)) { + if (Packet->DataDirection == DataIn) { + Packet->InTransferLength += NTOHL (ScsiRspHdr->ResidualCount); + } else { + Packet->OutTransferLength += NTOHL (ScsiRspHdr->ResidualCount); + } + + Status = EFI_BAD_BUFFER_SIZE; + } + + if (ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_UNDERFLOW)) { + if (Packet->DataDirection == DataIn) { + Packet->InTransferLength -= NTOHL (ScsiRspHdr->ResidualCount); + } else { + Packet->OutTransferLength -= NTOHL (ScsiRspHdr->ResidualCount); + } + } + + DataSegLen = ISCSI_GET_DATASEG_LEN (ScsiRspHdr); + if (DataSegLen != 0) { + SenseData = (ISCSI_SENSE_DATA *) NetbufGetByte (Pdu, sizeof (SCSI_RESPONSE), NULL); + if (SenseData == NULL) { + return EFI_PROTOCOL_ERROR; + } + + SenseData->Length = NTOHS (SenseData->Length); + + Packet->SenseDataLength = (UINT8) MIN (SenseData->Length, Packet->SenseDataLength); + if (Packet->SenseDataLength != 0) { + CopyMem (Packet->SenseData, &SenseData->Data[0], Packet->SenseDataLength); + } + } else { + Packet->SenseDataLength = 0; + } + + return Status; +} + + +/** + Process the received NOP In PDU. + + @param[in] Pdu The NOP In PDU received. + @param[in] Tcb The task control block. + + @retval EFI_SUCCES The NOP In PDU is processed and the related sequence + numbers are updated. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred. + +**/ +EFI_STATUS +IScsiOnNopInRcvd ( + IN NET_BUF *Pdu, + IN ISCSI_TCB *Tcb + ) +{ + ISCSI_NOP_IN *NopInHdr; + EFI_STATUS Status; + + NopInHdr = (ISCSI_NOP_IN *) NetbufGetByte (Pdu, 0, NULL); + if (NopInHdr == NULL) { + return EFI_PROTOCOL_ERROR; + } + + NopInHdr->StatSN = NTOHL (NopInHdr->StatSN); + NopInHdr->ExpCmdSN = NTOHL (NopInHdr->ExpCmdSN); + NopInHdr->MaxCmdSN = NTOHL (NopInHdr->MaxCmdSN); + + if (NopInHdr->InitiatorTaskTag == ISCSI_RESERVED_TAG) { + if (NopInHdr->StatSN != Tcb->Conn->ExpStatSN) { + return EFI_PROTOCOL_ERROR; + } + } else { + Status = IScsiCheckSN (&Tcb->Conn->ExpStatSN, NopInHdr->StatSN); + if (EFI_ERROR (Status)) { + return Status; + } + } + + IScsiUpdateCmdSN (Tcb->Conn->Session, NopInHdr->MaxCmdSN, NopInHdr->ExpCmdSN); + + return EFI_SUCCESS; +} + + +/** + Execute the SCSI command issued through the EXT SCSI PASS THRU protocol. + + @param[in] PassThru The EXT SCSI PASS THRU protocol. + @param[in] Target The target ID. + @param[in] Lun The LUN. + @param[in, out] Packet The request packet containing IO request, SCSI command + buffer and buffers to read/write. + + @retval EFI_SUCCES The SCSI command is executed and the result is updated to + the Packet. + @retval EFI_DEVICE_ERROR Session state was not as required. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_PROTOCOL_ERROR There is no such data in the net buffer. + @retval EFI_NOT_READY The target can not accept new commands. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiExecuteScsiCommand ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *PassThru, + IN UINT8 *Target, + IN UINT64 Lun, + IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet + ) +{ + EFI_STATUS Status; + ISCSI_DRIVER_DATA *Private; + ISCSI_SESSION *Session; + EFI_EVENT TimeoutEvent; + ISCSI_CONNECTION *Conn; + ISCSI_TCB *Tcb; + NET_BUF *Pdu; + ISCSI_XFER_CONTEXT *XferContext; + UINT8 *Data; + ISCSI_IN_BUFFER_CONTEXT InBufferContext; + UINT64 Timeout; + UINT8 *PduHdr; + + Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru); + Session = Private->Session; + Status = EFI_SUCCESS; + Tcb = NULL; + TimeoutEvent = NULL; + Timeout = 0; + + if (Session->State != SESSION_STATE_LOGGED_IN) { + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + Conn = NET_LIST_USER_STRUCT_S ( + Session->Conns.ForwardLink, + ISCSI_CONNECTION, + Link, + ISCSI_CONNECTION_SIGNATURE + ); + + if (Packet->Timeout != 0) { + Timeout = MultU64x32 (Packet->Timeout, 4); + } + + Status = IScsiNewTcb (Conn, &Tcb); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + // + // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU. + // + Pdu = IScsiNewScsiCmdPdu (Packet, Lun, Tcb); + if (Pdu == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + XferContext = &Tcb->XferContext; + PduHdr = NetbufGetByte (Pdu, 0, NULL); + if (PduHdr == NULL) { + Status = EFI_PROTOCOL_ERROR; + NetbufFree (Pdu); + goto ON_EXIT; + } + XferContext->Offset = ISCSI_GET_DATASEG_LEN (PduHdr); + + // + // Transmit the SCSI Command PDU. + // + Status = TcpIoTransmit (&Conn->TcpIo, Pdu); + + NetbufFree (Pdu); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + if (!Session->InitialR2T && + (XferContext->Offset < Session->FirstBurstLength) && + (XferContext->Offset < Packet->OutTransferLength) + ) { + // + // Unsolicited Data-Out sequence is allowed. There is remaining SCSI + // OUT data, and the limit of FirstBurstLength is not reached. + // + XferContext->TargetTransferTag = ISCSI_RESERVED_TAG; + XferContext->DesiredLength = MIN ( + Session->FirstBurstLength, + Packet->OutTransferLength - XferContext->Offset + ); + + Data = (UINT8 *) Packet->OutDataBuffer + XferContext->Offset; + Status = IScsiSendDataOutPduSequence (Data, Lun, Tcb); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + InBufferContext.InData = (UINT8 *) Packet->InDataBuffer; + InBufferContext.InDataLen = Packet->InTransferLength; + + while (!Tcb->StatusXferd) { + // + // Start the timeout timer. + // + if (Timeout != 0) { + Status = gBS->SetTimer (Conn->TimeoutEvent, TimerRelative, Timeout); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + TimeoutEvent = Conn->TimeoutEvent; + } + + // + // Try to receive PDU from target. + // + Status = IScsiReceivePdu (Conn, &Pdu, &InBufferContext, FALSE, FALSE, TimeoutEvent); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + PduHdr = NetbufGetByte (Pdu, 0, NULL); + if (PduHdr == NULL) { + Status = EFI_PROTOCOL_ERROR; + NetbufFree (Pdu); + goto ON_EXIT; + } + switch (ISCSI_GET_OPCODE (PduHdr)) { + case ISCSI_OPCODE_SCSI_DATA_IN: + Status = IScsiOnDataInRcvd (Pdu, Tcb, Packet); + break; + + case ISCSI_OPCODE_R2T: + Status = IScsiOnR2TRcvd (Pdu, Tcb, Lun, Packet); + break; + + case ISCSI_OPCODE_SCSI_RSP: + Status = IScsiOnScsiRspRcvd (Pdu, Tcb, Packet); + break; + + case ISCSI_OPCODE_NOP_IN: + Status = IScsiOnNopInRcvd (Pdu, Tcb); + break; + + case ISCSI_OPCODE_VENDOR_T0: + case ISCSI_OPCODE_VENDOR_T1: + case ISCSI_OPCODE_VENDOR_T2: + // + // These messages are vendor specific. Skip them. + // + break; + + default: + Status = EFI_PROTOCOL_ERROR; + break; + } + + NetbufFree (Pdu); + + if (EFI_ERROR (Status)) { + break; + } + } + +ON_EXIT: + + if (TimeoutEvent != NULL) { + gBS->SetTimer (TimeoutEvent, TimerCancel, 0); + } + + if (Tcb != NULL) { + IScsiDelTcb (Tcb); + } + + return Status; +} + + +/** + Reinstate the session on some error. + + @param[in] Session The iSCSI session + + @retval EFI_SUCCESS The session is reinstated from some error. + @retval Other Reinstatement failed. + +**/ +EFI_STATUS +IScsiSessionReinstatement ( + IN ISCSI_SESSION *Session + ) +{ + EFI_STATUS Status; + + ASSERT (Session->State != SESSION_STATE_FREE); + + // + // Abort the session and re-init it. + // + IScsiSessionAbort (Session); + IScsiSessionInit (Session, TRUE); + + // + // Login again. + // + Status = IScsiSessionLogin (Session); + + return Status; +} + + +/** + Initialize some session parameters before login. + + @param[in, out] Session The iSCSI session. + @param[in] Recovery Whether the request is from a fresh new start or recovery. + +**/ +VOID +IScsiSessionInit ( + IN OUT ISCSI_SESSION *Session, + IN BOOLEAN Recovery + ) +{ + if (!Recovery) { + Session->Signature = ISCSI_SESSION_SIGNATURE; + Session->State = SESSION_STATE_FREE; + + InitializeListHead (&Session->Conns); + InitializeListHead (&Session->TcbList); + } + + Session->Tsih = 0; + + Session->CmdSN = 1; + Session->InitiatorTaskTag = 1; + Session->NextCid = 1; + + Session->TargetPortalGroupTag = 0; + Session->MaxConnections = ISCSI_MAX_CONNS_PER_SESSION; + Session->InitialR2T = FALSE; + Session->ImmediateData = TRUE; + Session->MaxBurstLength = 262144; + Session->FirstBurstLength = MAX_RECV_DATA_SEG_LEN_IN_FFP; + Session->DefaultTime2Wait = 2; + Session->DefaultTime2Retain = 20; + Session->MaxOutstandingR2T = DEFAULT_MAX_OUTSTANDING_R2T; + Session->DataPDUInOrder = TRUE; + Session->DataSequenceInOrder = TRUE; + Session->ErrorRecoveryLevel = 0; +} + + +/** + Abort the iSCSI session. That is, reset all the connection(s), and free the + resources. + + @param[in, out] Session The iSCSI session. + +**/ +VOID +IScsiSessionAbort ( + IN OUT ISCSI_SESSION *Session + ) +{ + ISCSI_CONNECTION *Conn; + EFI_GUID *ProtocolGuid; + + if (Session->State != SESSION_STATE_LOGGED_IN) { + return ; + } + + ASSERT (!IsListEmpty (&Session->Conns)); + + while (!IsListEmpty (&Session->Conns)) { + Conn = NET_LIST_USER_STRUCT_S ( + Session->Conns.ForwardLink, + ISCSI_CONNECTION, + Link, + ISCSI_CONNECTION_SIGNATURE + ); + if (!Conn->Ipv6Flag) { + ProtocolGuid = &gEfiTcp4ProtocolGuid; + } else { + ProtocolGuid = &gEfiTcp6ProtocolGuid; + } + + gBS->CloseProtocol ( + Conn->TcpIo.Handle, + ProtocolGuid, + Session->Private->Image, + Session->Private->ExtScsiPassThruHandle + ); + + IScsiConnReset (Conn); + + IScsiDetatchConnection (Conn); + IScsiDestroyConnection (Conn); + } + + Session->State = SESSION_STATE_FAILED; + + return ; +} diff --git a/NetworkPkg/IScsiDxe/IScsiProto.h b/NetworkPkg/IScsiDxe/IScsiProto.h new file mode 100644 index 000000000..34b682b68 --- /dev/null +++ b/NetworkPkg/IScsiDxe/IScsiProto.h @@ -0,0 +1,1036 @@ +/** @file + The header file of iSCSI Protocol that defines many specific data structures. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _ISCSI_PROTO_H_ +#define _ISCSI_PROTO_H_ + +// +// RFC 1982 Serial Number Arithmetic, SERIAL_BITS = 32 +// +#define ISCSI_SEQ_EQ(s1, s2) ((s1) == (s2)) +#define ISCSI_SEQ_LT(s1, s2) \ + ( \ + (((INT32) (s1) < (INT32) (s2)) && (s2 - s1) < ((UINT32) 1 << 31)) || \ + (((INT32) (s1) > (INT32) (s2)) && (s1 - s2) > ((UINT32) 1 << 31)) \ + ) +#define ISCSI_SEQ_GT(s1, s2) \ + ( \ + (((INT32) (s1) < (INT32) (s2)) && (s2 - s1) > ((UINT32) 1 << 31)) || \ + (((INT32) (s1) > (INT32) (s2)) && (s1 - s2) < ((UINT32) 1 << 31)) \ + ) + +#define ISCSI_WELL_KNOWN_PORT 3260 +#define ISCSI_MAX_CONNS_PER_SESSION 1 + +#define DEFAULT_MAX_RECV_DATA_SEG_LEN 8192 +#define MAX_RECV_DATA_SEG_LEN_IN_FFP 65536 +#define DEFAULT_MAX_OUTSTANDING_R2T 1 + +#define ISCSI_VERSION_MAX 0x00 +#define ISCSI_VERSION_MIN 0x00 + +#define ISCSI_CHECK_MEDIA_LOGIN_WAITING_TIME EFI_TIMER_PERIOD_SECONDS(20) +#define ISCSI_CHECK_MEDIA_GET_DHCP_WAITING_TIME EFI_TIMER_PERIOD_SECONDS(20) + +#define ISCSI_REDIRECT_ADDR_START_DELIMITER '[' +#define ISCSI_REDIRECT_ADDR_END_DELIMITER ']' + +#define ISCSI_KEY_AUTH_METHOD "AuthMethod" +#define ISCSI_KEY_HEADER_DIGEST "HeaderDigest" +#define ISCSI_KEY_DATA_DIGEST "DataDigest" +#define ISCSI_KEY_MAX_CONNECTIONS "MaxConnections" +#define ISCSI_KEY_TARGET_NAME "TargetName" +#define ISCSI_KEY_INITIATOR_NAME "InitiatorName" +#define ISCSI_KEY_TARGET_ALIAS "TargetAlias" +#define ISCSI_KEY_INITIATOR_ALIAS "InitiatorAlias" +#define ISCSI_KEY_TARGET_ADDRESS "TargetAddress" +#define ISCSI_KEY_INITIAL_R2T "InitialR2T" +#define ISCSI_KEY_IMMEDIATE_DATA "ImmediateData" +#define ISCSI_KEY_TARGET_PORTAL_GROUP_TAG "TargetPortalGroupTag" +#define ISCSI_KEY_MAX_BURST_LENGTH "MaxBurstLength" +#define ISCSI_KEY_FIRST_BURST_LENGTH "FirstBurstLength" +#define ISCSI_KEY_DEFAULT_TIME2WAIT "DefaultTime2Wait" +#define ISCSI_KEY_DEFAULT_TIME2RETAIN "DefaultTime2Retain" +#define ISCSI_KEY_MAX_OUTSTANDING_R2T "MaxOutstandingR2T" +#define ISCSI_KEY_DATA_PDU_IN_ORDER "DataPDUInOrder" +#define ISCSI_KEY_DATA_SEQUENCE_IN_ORDER "DataSequenceInOrder" +#define ISCSI_KEY_ERROR_RECOVERY_LEVEL "ErrorRecoveryLevel" +#define ISCSI_KEY_SESSION_TYPE "SessionType" +#define ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH "MaxRecvDataSegmentLength" + +#define ISCSI_KEY_VALUE_NONE "None" + +/// +/// connection state for initiator +/// + +#define CONN_STATE_FREE 0 +#define CONN_STATE_XPT_WAIT 1 +#define CONN_STATE_IN_LOGIN 2 +#define CONN_STATE_LOGGED_IN 3 +#define CONN_STATE_IN_LOGOUT 4 +#define CONN_STATE_LOGOUT_REQUESTED 5 +#define CONN_STATE_CLEANUP_WAIT 6 +#define CONN_STATE_IN_CLEANUP 7 + +/// +/// session state for initiator +/// +#define SESSION_STATE_FREE 0 +#define SESSION_STATE_LOGGED_IN 1 +#define SESSION_STATE_FAILED 2 + +#define ISCSI_RESERVED_TAG 0xffffffff + +#define ISCSI_REQ_IMMEDIATE 0x40 +#define ISCSI_OPCODE_MASK 0x3F + +#define ISCSI_SET_OPCODE(PduHdr, Op, Flgs) ((((ISCSI_BASIC_HEADER *) (PduHdr))->OpCode) = ((Op) | (Flgs))) +#define ISCSI_GET_OPCODE(PduHdr) ((((ISCSI_BASIC_HEADER *) (PduHdr))->OpCode) & ISCSI_OPCODE_MASK) +#define ISCSI_CHECK_OPCODE(PduHdr, Op) ((((PduHdr)->OpCode) & ISCSI_OPCODE_MASK) == (Op)) +#define ISCSI_IMMEDIATE_ON(PduHdr) ((PduHdr)->OpCode & ISCSI_REQ_IMMEDIATE) +#define ISCSI_SET_FLAG(PduHdr, Flag) (((ISCSI_BASIC_HEADER *) (PduHdr))->Flags |= (BOOLEAN)(Flag)) +#define ISCSI_CLEAR_FLAG(PduHdr, Flag) (((ISCSI_BASIC_HEADER *) (PduHdr))->Flags &= ~(Flag)) +#define ISCSI_FLAG_ON(PduHdr, Flag) ((BOOLEAN) ((((ISCSI_BASIC_HEADER *) (PduHdr))->Flags & (Flag)) == (Flag))) +#define ISCSI_SET_STAGES(PduHdr, Cur, Nxt) ((PduHdr)->Flags = (UINT8) ((PduHdr)->Flags | ((Cur) << 2 | (Nxt)))) +#define ISCSI_GET_CURRENT_STAGE(PduHdr) ((UINT8) (((PduHdr)->Flags >> 2) & 0x3)) +#define ISCSI_GET_NEXT_STAGE(PduHdr) ((UINT8) (((PduHdr)->Flags) & 0x3)) + +#define ISCSI_GET_PAD_LEN(DataLen) ((~(DataLen) + 1) & 0x3) +#define ISCSI_ROUNDUP(DataLen) (((DataLen) + 3) &~(0x3)) + +#define HTON24(Dst, Src) \ + do { \ + (Dst)[0] = (UINT8) ((UINT8) ((Src) >> 16) & 0xFF); \ + (Dst)[1] = (UINT8) ((UINT8) ((Src) >> 8) & 0xFF); \ + (Dst)[2] = (UINT8) ((UINT8) (Src) & 0xFF); \ + } while (0); + +#define NTOH24(src) (((src)[0] << 16) | ((src)[1] << 8) | ((src)[2])) + +#define ISCSI_GET_DATASEG_LEN(PduHdr) NTOH24 (((ISCSI_BASIC_HEADER *) (PduHdr))->DataSegmentLength) +#define ISCSI_SET_DATASEG_LEN(PduHdr, Len) HTON24 (((ISCSI_BASIC_HEADER *) (PduHdr))->DataSegmentLength, (Len)) +#define ISCSI_GET_BUFFER_OFFSET(PduHdr) NTOHL (((ISCSI_SCSI_DATA_IN *) (PduHdr))->BufferOffset) + +// +// Initiator opcodes. +// +#define ISCSI_OPCODE_NOP_OUT 0x00 +#define ISCSI_OPCODE_SCSI_CMD 0x01 +#define ISCSI_OPCODE_SCSI_TMF_REQ 0x02 +#define ISCSI_OPCODE_LOGIN_REQ 0x03 +#define ISCSI_OPCODE_TEXT_REQ 0x04 +#define ISCSI_OPCODE_SCSI_DATA_OUT 0x05 +#define ISCSI_OPCODE_LOGOUT_REQ 0x06 +#define ISCSI_OPCODE_SNACK_REQ 0x10 +#define ISCSI_OPCODE_VENDOR_I0 0x1c +#define ISCSI_OPCODE_VENDOR_I1 0x1d +#define ISCSI_OPCODE_VENDOR_I2 0x1e + +// +// Target opcodes. +// +#define ISCSI_OPCODE_NOP_IN 0x20 +#define ISCSI_OPCODE_SCSI_RSP 0x21 +#define ISCSI_OPCODE_SCSI_TMF_RSP 0x22 +#define ISCSI_OPCODE_LOGIN_RSP 0x23 +#define ISCSI_OPCODE_TEXT_RSP 0x24 +#define ISCSI_OPCODE_SCSI_DATA_IN 0x25 +#define ISCSI_OPCODE_LOGOUT_RSP 0x26 +#define ISCSI_OPCODE_R2T 0x31 +#define ISCSI_OPCODE_ASYNC_MSG 0x32 +#define ISCSI_OPCODE_VENDOR_T0 0x3c +#define ISCSI_OPCODE_VENDOR_T1 0x3d +#define ISCSI_OPCODE_VENDOR_T2 0x3e +#define ISCSI_OPCODE_REJECT 0x3f + +#define ISCSI_BHS_FLAG_FINAL 0x80 + +// +// Defined AHS types, others are reserved. +// +#define ISCSI_AHS_TYPE_EXT_CDB 0x1 +#define ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN 0x2 + +#define SCSI_CMD_PDU_FLAG_READ 0x40 +#define SCSI_CMD_PDU_FLAG_WRITE 0x20 + +#define ISCSI_CMD_PDU_TASK_ATTR_MASK 0x07 + +// +// task attributes +// +#define ISCSI_TASK_ATTR_UNTAGGED 0x00 +#define ISCSI_TASK_ATTR_SIMPLE 0x01 +#define ISCSI_TASK_ATTR_ORDERD 0x02 +#define ISCSI_TASK_ATTR_HOQ 0x03 +#define ISCSI_TASK_ATTR_ACA 0x04 + +// +// Flag bit definitions in SCSI response. +// +#define SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW 0x10 +#define SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW 0x08 +#define SCSI_RSP_PDU_FLAG_OVERFLOW 0x04 +#define SCSI_RSP_PDU_FLAG_UNDERFLOW 0x02 + +// +// iSCSI service response codes. +// +#define ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET 0x00 +#define ISCSI_SERVICE_RSP_TARGET_FAILURE 0x01 + +#define ISCSI_TMF_RSP_PDU_RSP_FUNCTION_COMPLETE 0 +#define ISCSI_TMF_RSP_PDU_RSP_TASK_NOT_EXIST 1 +#define ISCSI_TMF_RSP_PDU_RSP_LUN_NOT_EXIST 2 +#define ISCSI_TMF_RSP_PDU_RSP_TASK_STILL_ALLEGIANT 3 +#define ISCSI_TMF_RSP_PDU_RSP_TASK_REASSGIN_NOT_SUPPORTED 4 +#define ISCSI_TMF_RSP_PDU_RSP_NOT_SUPPORTED 5 +#define ISCSI_TMF_RSP_PDU_RSP_FUNCTION_AHTH_FAILED 6 +#define ISCSI_TMF_RSP_PDU_RSP_FUNCTION_REJECTED 255 + +#define SCSI_DATA_IN_PDU_FLAG_ACKKNOWLEDGE 0x40 +#define SCSI_DATA_IN_PDU_FLAG_OVERFLOW SCSI_RSP_PDU_FLAG_OVERFLOW +#define SCSI_DATA_IN_PDU_FLAG_UNDERFLOW SCSI_RSP_PDU_FLAG_UNDERFLOW +#define SCSI_DATA_IN_PDU_FLAG_STATUS_VALID 0x01 + +#define ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT 0x80 +#define ISCSI_LOGIN_REQ_PDU_FLAG_CONTINUE 0x40 + +#define ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT +#define ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE ISCSI_LOGIN_REQ_PDU_FLAG_CONTINUE + +#define ISCSI_LOGIN_STATUS_SUCCESS 0 +#define ISCSI_LOGIN_STATUS_REDIRECTION 1 +#define ISCSI_LOGIN_STATUS_INITIATOR_ERROR 2 +#define ISCSI_LOGIN_STATUS_TARGET_ERROR 3 + +#define ISCSI_LOGOUT_REASON_CLOSE_SESSION 0 +#define ISCSI_LOGOUT_REASON_CLOSE_CONNECTION 1 +#define ISCSI_LOGOUT_REASON_REMOVE_CONNECTION_FOR_RECOVERY 2 + +#define ISCSI_LOGOUT_RESPONSE_SESSION_CLOSED_SUCCESS 0 +#define ISCSI_LOGOUT_RESPONSE_CID_NOT_FOUND 1 +#define ISCSI_LOGOUT_RESPONSE_RECOVERY_NOT_SUPPORTED 2 +#define ISCSI_LOGOUT_RESPONSE_CLEANUP_FAILED 3 + +#define ISCSI_SNACK_REQUEST_TYPE_DATA_OR_R2T 0 +#define ISCSI_SNACK_REQUEST_TYPE_STATUS 1 +#define ISCSI_SNACK_REQUEST_TYPE_DATA_ACK 2 +#define ISCSI_SNACK_REQUEST_TYPE_RDATA 3 + +#define ISCSI_SECURITY_NEGOTIATION 0 +#define ISCSI_LOGIN_OPERATIONAL_NEGOTIATION 1 +#define ISCSI_FULL_FEATURE_PHASE 3 + +typedef struct _ISCSI_SESSION ISCSI_SESSION; +typedef struct _ISCSI_CONNECTION ISCSI_CONNECTION; + +typedef enum { + DataIn = 0, + DataOut = 1, + DataBi = 2 +} DATA_DIRECTION; + +/// +/// iSCSI Basic Header Segment +/// +typedef struct _ISCSI_BASIC_HEADER { + UINT8 OpCode; + UINT8 Flags; + UINT16 OpCodeSpecific1; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Lun[8]; + UINT32 InitiatorTaskTag; + UINT32 OpCodeSpecific2[7]; +} ISCSI_BASIC_HEADER; + +typedef struct _ISCSI_ADDTIONAL_HEADER { + UINT16 Length; + UINT8 Type; + UINT8 TypeSpecific[1]; +} ISCSI_ADDITIONAL_HEADER; + +typedef struct _ISCSI_BI_EXP_READ_DATA_LEN_AHS { + UINT16 Length; + UINT8 Type; + UINT8 Reserved; + UINT32 ExpReadDataLength; +} ISCSI_BI_EXP_READ_DATA_LEN_AHS; + +/// +/// SCSI Command +/// +typedef struct _SCSI_COMMAND { + UINT8 OpCode; + UINT8 Flags; + UINT16 Reserved; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Lun[8]; + UINT32 InitiatorTaskTag; + UINT32 ExpDataXferLength; + UINT32 CmdSN; + UINT32 ExpStatSN; + UINT8 Cdb[16]; +} SCSI_COMMAND; + +/// +/// SCSI Response +/// +typedef struct _SCSI_RESPONSE { + UINT8 OpCode; + UINT8 Flags; + UINT8 Response; + UINT8 Status; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Reserved[8]; + UINT32 InitiatorTaskTag; + UINT32 SNACKTag; + UINT32 StatSN; + UINT32 ExpCmdSN; + UINT32 MaxCmdSN; + UINT32 ExpDataSN; + UINT32 BiReadResidualCount; + UINT32 ResidualCount; +} SCSI_RESPONSE; + +typedef struct _ISCSI_SENSE_DATA { + UINT16 Length; + UINT8 Data[2]; +} ISCSI_SENSE_DATA; + +/// +/// iSCSI Task Managment Function Request. +/// +typedef struct _ISCSI_TMF_REQUEST { + UINT8 OpCode; + UINT8 Fuction; + UINT16 Reserved1; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Lun[8]; + UINT32 InitiatorTaskTag; + UINT32 ReferencedTaskTag; + UINT32 CmdSN; + UINT32 ExpStatSN; + UINT32 RefCmdSN; + UINT32 ExpDataSN; + UINT32 Reserved2[2]; +} ISCSI_TMF_REQUEST; + +/// +/// iSCSI Task Management Function Response. +/// +typedef struct _ISCSI_TMF_RESPONSE { + UINT8 OpCode; + UINT8 Reserved1; + UINT8 Response; + UINT8 Reserved2; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT32 Reserver3[2]; + UINT32 InitiatorTaskTag; + UINT32 Reserved4; + UINT32 StatSN; + UINT32 ExpCmdSN; + UINT32 MaxCmdSN; + UINT32 Reserved[3]; +} ISCSI_TMF_RESPONSE; + +/// +/// SCSI Data-Out +/// +typedef struct _ISCSI_SCSI_DATA_OUT { + UINT8 OpCode; + UINT8 Reserved1[3]; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Lun[8]; + UINT32 InitiatorTaskTag; + UINT32 TargetTransferTag; + UINT32 Reserved2; + UINT32 ExpStatSN; + UINT32 Reserved3; + UINT32 DataSN; + UINT32 BufferOffset; + UINT32 Reserved4; +} ISCSI_SCSI_DATA_OUT; + +/// +/// SCSI Data-In +/// +typedef struct _ISCSI_SCSI_DATA_IN { + UINT8 OpCode; + UINT8 Flags; + UINT8 Reserved1; + UINT8 Status; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Lun[8]; + UINT32 InitiatorTaskTag; + UINT32 TargetTransferTag; + UINT32 StatSN; + UINT32 ExpCmdSN; + UINT32 MaxCmdSN; + UINT32 DataSN; + UINT32 BufferOffset; + UINT32 ResidualCount; +} ISCSI_SCSI_DATA_IN; + +/// +/// Ready To Transfer. +/// +typedef struct _ISCSI_READY_TO_TRANSFER { + UINT8 OpCode; + UINT8 Reserved1[3]; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Lun[8]; + UINT32 InitiatorTaskTag; + UINT32 TargetTransferTag; + UINT32 StatSN; + UINT32 ExpCmdSN; + UINT32 MaxCmdSN; + UINT32 R2TSeqNum; + UINT32 BufferOffset; + UINT32 DesiredDataTransferLength; +} ISCSI_READY_TO_TRANSFER; + +typedef struct _ISCSI_ASYNC_MESSAGE { + UINT8 OpCode; + UINT8 Reserved1[8]; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Lun[8]; + UINT32 InitiatorTaskTag; + UINT32 Reserved2; + UINT32 StatSN; + UINT32 ExpCmdSN; + UINT32 MaxCmdSN; + UINT8 AsyncEvent; + UINT8 AsyncVCode; + UINT16 Parameter1; + UINT16 Parameter2; + UINT16 Parameter3; + UINT32 Reserved3; +} ISCSI_ASYNC_MESSAGE; + +/// +/// Login Request. +/// +typedef struct _ISCSI_LOGIN_REQUEST { + UINT8 OpCode; + UINT8 Flags; + UINT8 VersionMax; + UINT8 VersionMin; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Isid[6]; + UINT16 Tsih; + UINT32 InitiatorTaskTag; + UINT16 Cid; + UINT16 Reserved1; + UINT32 CmdSN; + UINT32 ExpStatSN; + UINT32 Reserved2[4]; +} ISCSI_LOGIN_REQUEST; + +/// +/// Login Response. +/// +typedef struct _ISCSI_LOGIN_RESPONSE { + UINT8 OpCode; + UINT8 Flags; + UINT8 VersionMax; + UINT8 VersionActive; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Isid[6]; + UINT16 Tsih; + UINT32 InitiatorTaskTag; + UINT32 Reserved1; + UINT32 StatSN; + UINT32 ExpCmdSN; + UINT32 MaxCmdSN; + UINT8 StatusClass; + UINT8 StatusDetail; + UINT8 Reserved2[10]; +} ISCSI_LOGIN_RESPONSE; + +/// +/// Logout Request. +/// +typedef struct _ISCSI_LOGOUT_REQUEST { + UINT8 OpCode; + UINT8 ReasonCode; + UINT16 Reserved1; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT32 Reserved2[2]; + UINT32 InitiatorTaskTag; + UINT16 Cid; + UINT16 Reserved3; + UINT32 CmdSN; + UINT32 ExpStatSN; + UINT32 Reserved4[4]; +} ISCSI_LOGOUT_REQUEST; + +/// +/// Logout Response. +/// +typedef struct _ISCSI_LOGOUT_RESPONSE { + UINT8 OpCode; + UINT8 Reserved1; + UINT8 Response; + UINT8 Reserved2; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT32 Reserved3[2]; + UINT32 InitiatorTaskTag; + UINT32 Reserved4; + UINT32 StatSN; + UINT32 ExpCmdSN; + UINT32 MaxCmdSN; + UINT32 Reserved5; + UINT16 Time2Wait; + UINT16 Time2Retain; + UINT32 Reserved6; +} ISCSI_LOGOUT_RESPONSE; + +/// +/// SNACK Request. +/// +typedef struct _ISCSI_SNACK_REQUEST { + UINT8 OpCode; + UINT8 Type; + UINT16 Reserved1; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Lun[8]; + UINT32 InitiatorTaskTag; + UINT32 TargetTransferTag; + UINT32 Reserved2; + UINT32 ExpStatSN; + UINT32 Reserved[2]; + UINT32 BegRun; + UINT32 RunLength; +} ISCSI_SNACK_REQUEST; + +/// +/// Reject. +/// +typedef struct _ISCSI_REJECT { + UINT8 OpCode; + UINT8 Reserved1; + UINT8 Reason; + UINT8 Reserved2; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT32 Reserved3[2]; + UINT32 InitiatorTaskTag; + UINT32 Reserved4; + UINT32 StatSN; + UINT32 ExpCmdSN; + UINT32 MaxCmdSN; + UINT32 DataSN; + UINT32 Reserved5[2]; +} ISCSI_REJECT; + +/// +/// NOP-Out. +/// +typedef struct _ISCSI_NOP_OUT { + UINT8 OpCode; + UINT8 Reserved1[3]; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Lun[8]; + UINT32 InitiatorTaskTag; + UINT32 TargetTransferTag; + UINT32 CmdSN; + UINT32 ExpStatSN; + UINT32 Reserved2[4]; +} ISCSI_NOP_OUT; + +/// +/// NOP-In. +/// +typedef struct _ISCSI_NOP_IN { + UINT8 OpCode; + UINT8 Reserved1[3]; + UINT8 TotalAHSLength; + UINT8 DataSegmentLength[3]; + UINT8 Lun[8]; + UINT32 InitiatorTaskTag; + UINT32 TargetTransferTag; + UINT32 StatSN; + UINT32 ExpCmdSN; + UINT32 MaxCmdSN; + UINT32 Reserved2[3]; +} ISCSI_NOP_IN; + +typedef enum { + IScsiDigestNone, + IScsiDigestCRC32 +} ISCSI_DIGEST_TYPE; + +typedef struct _ISCSI_XFER_CONTEXT { + UINT32 TargetTransferTag; + UINT32 Offset; + UINT32 DesiredLength; + UINT32 ExpDataSN; +} ISCSI_XFER_CONTEXT; + +typedef struct _ISCSI_IN_BUFFER_CONTEXT { + UINT8 *InData; + UINT32 InDataLen; +} ISCSI_IN_BUFFER_CONTEXT; + +typedef struct _ISCSI_TCB { + LIST_ENTRY Link; + + BOOLEAN SoFarInOrder; + UINT32 ExpDataSN; + BOOLEAN FbitReceived; + BOOLEAN StatusXferd; + UINT32 ActiveR2Ts; + UINT32 Response; + CHAR8 *Reason; + UINT32 InitiatorTaskTag; + UINT32 CmdSN; + UINT32 SNACKTag; + + ISCSI_XFER_CONTEXT XferContext; + + ISCSI_CONNECTION *Conn; +} ISCSI_TCB; + +typedef struct _ISCSI_KEY_VALUE_PAIR { + LIST_ENTRY List; + + CHAR8 *Key; + CHAR8 *Value; +} ISCSI_KEY_VALUE_PAIR; + +/** + Attach the iSCSI connection to the iSCSI session. + + @param[in, out] Session The iSCSI session. + @param[in, out] Conn The iSCSI connection. + +**/ +VOID +IScsiAttatchConnection ( + IN OUT ISCSI_SESSION *Session, + IN OUT ISCSI_CONNECTION *Conn + ); + +/** + Detach the iSCSI connection from the session it belongs to. + + @param[in, out] Conn The iSCSI connection. + +**/ +VOID +IScsiDetatchConnection ( + IN OUT ISCSI_CONNECTION *Conn + ); + +/** + This function performs the iSCSI connection login. + + @param[in, out] Conn The iSCSI connection to login. + @param Timeout The timeout value in milliseconds. + + @retval EFI_SUCCESS The iSCSI connection is logged into the iSCSI target. + @retval EFI_TIMEOUT Timeout occurred during the login procedure. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiConnLogin ( + IN OUT ISCSI_CONNECTION *Conn, + IN UINT16 Timeout + ); + +/** + Create a TCP connection for the iSCSI session. + + @param[in] Session Points to the iSCSI session. + + @return The newly created iSCSI connection. + +**/ +ISCSI_CONNECTION * +IScsiCreateConnection ( + IN ISCSI_SESSION *Session + ); + +/** + Destroy an iSCSI connection. + + @param[in] Conn The connection to destroy. + +**/ +VOID +IScsiDestroyConnection ( + IN ISCSI_CONNECTION *Conn + ); + +/** + Login the iSCSI session. + + @param[in] Session The iSCSI session + + @retval EFI_SUCCESS The iSCSI session login procedure finished. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NO_MEDIA There was a media error. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiSessionLogin ( + IN ISCSI_SESSION *Session + ); + +/** + Wait for IPsec negotiation, then try to login the iSCSI session again. + + @param[in] Session The iSCSI session + + @retval EFI_SUCCESS The iSCSI session login procedure finished. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened. + +**/ +EFI_STATUS +IScsiSessionReLogin ( + IN ISCSI_SESSION *Session + ); + +/** + Build and send the iSCSI login request to the iSCSI target according to + the current login stage. + + @param[in] Conn The connection in the iSCSI login phase. + + @retval EFI_SUCCESS The iSCSI login request PDU is built and sent on this + connection. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_DEVICE_ERROR Some kind of device error happened. + +**/ +EFI_STATUS +IScsiSendLoginReq ( + IN ISCSI_CONNECTION *Conn + ); + +/** + Receive and process the iSCSI login response. + + @param[in] Conn The connection in the iSCSI login phase. + + @retval EFI_SUCCESS The iSCSI login response PDU is received and processed. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiReceiveLoginRsp ( + IN ISCSI_CONNECTION *Conn + ); + +/** + Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU. + The DataSegmentLength and the actual size of the net buffer containing this PDU will be + updated. + + @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will + be added to. + @param[in] Key The key name string. + @param[in] Value The value string. + + @retval EFI_SUCCESS The key-valu pair is added to the PDU's datasegment and + the correspondence length fields are updated. + @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value + pair. +**/ +EFI_STATUS +IScsiAddKeyValuePair ( + IN OUT NET_BUF *Pdu, + IN CHAR8 *Key, + IN CHAR8 *Value + ); + +/** + Prepare the iSCSI login request to be sent according to the current login status. + + @param[in, out] Conn The connection in the iSCSI login phase. + + @return The pointer to the net buffer containing the iSCSI login request built. + @retval NULL Other errors as indicated. + +**/ +NET_BUF * +IScsiPrepareLoginReq ( + IN OUT ISCSI_CONNECTION *Conn + ); + +/** + Process the iSCSI Login Response. + + @param[in, out] Conn The connection on which the iSCSI login response is received. + @param[in, out] Pdu The iSCSI login response PDU. + + @retval EFI_SUCCESS The iSCSI login response PDU is processed and all check are passed. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error happened. + @retval EFI_MEDIA_CHANGED Target is redirected. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiProcessLoginRsp ( + IN OUT ISCSI_CONNECTION *Conn, + IN OUT NET_BUF *Pdu + ); + +/** + Updated the target information according the data received in the iSCSI + login response with an target redirection status. + + @param[in, out] Session The iSCSI session. + @param[in] Data The data segment which should contain the + TargetAddress key-value list. + @param[in] Len Length of the data. + + @retval EFI_SUCCESS The target address is updated. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NOT_FOUND The TargetAddress key is not found. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiUpdateTargetAddress ( + IN OUT ISCSI_SESSION *Session, + IN CHAR8 *Data, + IN UINT32 Len + ); + +/** + The callback function to free the net buffer list. + + @param[in] Arg The opaque parameter. + +**/ +VOID +EFIAPI +IScsiFreeNbufList ( + VOID *Arg + ); + +/** + Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and + an optional data segment. The two parts will be put into two blocks of buffers in the + net buffer. The digest check will be conducted in this function if needed and the digests + will be trimmed from the PDU buffer. + + @param[in] Conn The iSCSI connection to receive data from. + @param[out] Pdu The received iSCSI pdu. + @param[in] Context The context used to describe information on the caller provided + buffer to receive data segment of the iSCSI pdu, it's optional. + @param[in] HeaderDigest Whether there will be header digest received. + @param[in] DataDigest Whether there will be data digest. + @param[in] TimeoutEvent The timeout event, it's optional. + + @retval EFI_SUCCESS An iSCSI pdu is received. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiReceivePdu ( + IN ISCSI_CONNECTION *Conn, + OUT NET_BUF **Pdu, + IN ISCSI_IN_BUFFER_CONTEXT *Context, OPTIONAL + IN BOOLEAN HeaderDigest, + IN BOOLEAN DataDigest, + IN EFI_EVENT TimeoutEvent OPTIONAL + ); + +/** + Check and get the result of the parameter negotiation. + + @param[in, out] Conn The connection in iSCSI login. + + @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished. + @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +IScsiCheckOpParams ( + IN OUT ISCSI_CONNECTION *Conn + ); + +/** + Fill the operational parameters. + + @param[in] Conn The connection in iSCSI login. + @param[in, out] Pdu The iSCSI login request PDU to fill the parameters. + +**/ +VOID +IScsiFillOpParams ( + IN ISCSI_CONNECTION *Conn, + IN OUT NET_BUF *Pdu + ); + +/** + Pad the iSCSI AHS or data segment to an integer number of 4 byte words. + + @param[in, out] Pdu The iSCSI pdu which contains segments to pad. + @param[in] Len The length of the last semgnet in the PDU. + + @retval EFI_SUCCESS The segment is padded or no need to pad it. + @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the + padding bytes. +**/ +EFI_STATUS +IScsiPadSegment ( + IN OUT NET_BUF *Pdu, + IN UINT32 Len + ); + +/** + Build a key-value list from the data segment. + + @param[in] Data The data segment containing the key-value pairs. + @param[in] Len Length of the data segment. + + @return The key-value list. + @retval NULL Other errors as indicated. + +**/ +LIST_ENTRY * +IScsiBuildKeyValueList ( + IN CHAR8 *Data, + IN UINT32 Len + ); + +/** + Get the value string by the key name from the key-value list. If found, + the key-value entry will be removed from the list. + + @param[in, out] KeyValueList The key-value list. + @param[in] Key The key name to find. + + @return The value string. + @retval NULL The key value pair can not be found. + +**/ +CHAR8 * +IScsiGetValueByKeyFromList ( + IN OUT LIST_ENTRY *KeyValueList, + IN CHAR8 *Key + ); + +/** + Free the key-value list. + + @param[in] KeyValueList The key-value list. + +**/ +VOID +IScsiFreeKeyValueList ( + IN LIST_ENTRY *KeyValueList + ); + +/** + Normalize the iSCSI name according to RFC. + + @param[in, out] Name The iSCSI name. + @param[in] Len length of the iSCSI name. + + @retval EFI_SUCCESS The iSCSI name is valid and normalized. + @retval EFI_PROTOCOL_ERROR The iSCSI name is mal-formatted or not in the IQN format. + +**/ +EFI_STATUS +IScsiNormalizeName ( + IN OUT CHAR8 *Name, + IN UINTN Len + ); + +/** + Execute the SCSI command issued through the EXT SCSI PASS THRU protocol. + + @param[in] PassThru The EXT SCSI PASS THRU protocol. + @param[in] Target The target ID. + @param[in] Lun The LUN. + @param[in, out] Packet The request packet containing IO request, SCSI command + buffer and buffers to read/write. + + @retval EFI_SUCCES The SCSI command is executed and the result is updated to + the Packet. + @retval EFI_DEVICE_ERROR Session state was not as required. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NOT_READY The target can not accept new commands. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +IScsiExecuteScsiCommand ( + IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *PassThru, + IN UINT8 *Target, + IN UINT64 Lun, + IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet + ); + +/** + Reinstate the session on some error. + + @param[in] Session The iSCSI session + + @retval EFI_SUCCES The session is reinstated from some error. + @retval Other Reinstatement failed. + +**/ +EFI_STATUS +IScsiSessionReinstatement ( + IN ISCSI_SESSION *Session + ); + +/** + Initialize some session parameters before login. + + @param[in, out] Session The iSCSI session. + @param[in] Recovery Whether the request is from a fresh new start or recovery. + +**/ +VOID +IScsiSessionInit ( + IN OUT ISCSI_SESSION *Session, + IN BOOLEAN Recovery + ); + +/** + Abort the iSCSI session, that is, reset all the connection and free the + resources. + + @param[in, out] Session The iSCSI session. + +**/ +VOID +IScsiSessionAbort ( + IN OUT ISCSI_SESSION *Session + ); + +#endif diff --git a/NetworkPkg/Include/Guid/HttpBootConfigHii.h b/NetworkPkg/Include/Guid/HttpBootConfigHii.h new file mode 100644 index 000000000..88fd42256 --- /dev/null +++ b/NetworkPkg/Include/Guid/HttpBootConfigHii.h @@ -0,0 +1,19 @@ +/** @file + GUIDs used as HII FormSet and HII Package list GUID in HTTP boot driver. + +Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __HTTP_BOOT_HII_GUID_H__ +#define __HTTP_BOOT_HII_GUID_H__ + +#define HTTP_BOOT_CONFIG_GUID \ + { \ + 0x4d20583a, 0x7765, 0x4e7a, { 0x8a, 0x67, 0xdc, 0xde, 0x74, 0xee, 0x3e, 0xc5 } \ + } + +extern EFI_GUID gHttpBootConfigGuid; + +#endif diff --git a/NetworkPkg/Include/Guid/HttpTlsCipherList.h b/NetworkPkg/Include/Guid/HttpTlsCipherList.h new file mode 100644 index 000000000..1dffe9497 --- /dev/null +++ b/NetworkPkg/Include/Guid/HttpTlsCipherList.h @@ -0,0 +1,32 @@ +/** @file + This file defines the HttpTlsCipherList variable for HTTPS to configure Tls Cipher List. + +Copyright (c) 2018 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __HTTP_TLS_CIPHER_LIST_H__ +#define __HTTP_TLS_CIPHER_LIST_H__ + +// +// Private Variable for HTTPS to configure Tls Cipher List. +// The valid contents of variable must follow the TLS CipherList format defined in RFC 5246. +// The valid length of variable must be an integral multiple of 2. +// For example, if below cipher suites are preferred: +// CipherSuite TLS_RSA_WITH_AES_128_CBC_SHA256 = {0x00,0x3C} +// CipherSuite TLS_RSA_WITH_AES_256_CBC_SHA256 = {0x00,0x3D} +// Then, the contents of variable should be: +// {0x00,0x3C,0x00,0x3D} +// +#define EDKII_HTTP_TLS_CIPHER_LIST_GUID \ + { \ + 0x46ddb415, 0x5244, 0x49c7, { 0x93, 0x74, 0xf0, 0xe2, 0x98, 0xe7, 0xd3, 0x86 } \ + } + +#define EDKII_HTTP_TLS_CIPHER_LIST_VARIABLE L"HttpTlsCipherList" + +extern EFI_GUID gEdkiiHttpTlsCipherListGuid; + +#endif + diff --git a/NetworkPkg/Include/Guid/IScsiConfigHii.h b/NetworkPkg/Include/Guid/IScsiConfigHii.h new file mode 100644 index 000000000..d63ef208b --- /dev/null +++ b/NetworkPkg/Include/Guid/IScsiConfigHii.h @@ -0,0 +1,20 @@ +/** @file + GUIDs used as HII FormSet and HII Package list GUID in IScsiConfig driver + that supports IP4 and IP6 both. + +Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __ISCSI_CONFIG_HII_GUID_H__ +#define __ISCSI_CONFIG_HII_GUID_H__ + +#define ISCSI_CONFIG_GUID \ + { \ + 0x4b47d616, 0xa8d6, 0x4552, { 0x9d, 0x44, 0xcc, 0xad, 0x2e, 0xf, 0x4c, 0xf9 } \ + } + +extern EFI_GUID gIScsiConfigGuid; + +#endif diff --git a/NetworkPkg/Include/Guid/Ip6ConfigHii.h b/NetworkPkg/Include/Guid/Ip6ConfigHii.h new file mode 100644 index 000000000..da26d0306 --- /dev/null +++ b/NetworkPkg/Include/Guid/Ip6ConfigHii.h @@ -0,0 +1,19 @@ +/** @file + GUIDs used as HII FormSet and HII Package list GUID in Ip6Config driver. + +Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __IP6_CONFIG_HII_GUID_H__ +#define __IP6_CONFIG_HII_GUID_H__ + +#define IP6_CONFIG_NVDATA_GUID \ + { \ + 0x2eea107, 0x98db, 0x400e, { 0x98, 0x30, 0x46, 0xa, 0x15, 0x42, 0xd7, 0x99 } \ + } + +extern EFI_GUID gIp6ConfigNvDataGuid; + +#endif diff --git a/NetworkPkg/Include/Guid/TlsAuthConfigHii.h b/NetworkPkg/Include/Guid/TlsAuthConfigHii.h new file mode 100644 index 000000000..fd2de001a --- /dev/null +++ b/NetworkPkg/Include/Guid/TlsAuthConfigHii.h @@ -0,0 +1,20 @@ +/** @file + GUIDs used as HII FormSet and HII Package list GUID in TlsAuthConfigDxe driver. + +Copyright (c) 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __TLS_AUTH_CONFIG_HII_GUID_H__ +#define __TLS_AUTH_CONFIG_HII_GUID_H__ + +#define TLS_AUTH_CONFIG_GUID \ + { \ + 0xb0eae4f8, 0x9a04, 0x4c6d, { 0xa7, 0x48, 0x79, 0x3d, 0xaa, 0xf, 0x65, 0xdf } \ + } + +extern EFI_GUID gTlsAuthConfigGuid; + +#endif + diff --git a/NetworkPkg/Include/Guid/TlsAuthentication.h b/NetworkPkg/Include/Guid/TlsAuthentication.h new file mode 100644 index 000000000..e6646a65e --- /dev/null +++ b/NetworkPkg/Include/Guid/TlsAuthentication.h @@ -0,0 +1,24 @@ +/** @file + This file defines TlsCaCertificate variable. + +Copyright (c) 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __TLS_AUTHENTICATION_H__ +#define __TLS_AUTHENTICATION_H__ + +// Private variable for CA Certificate configuration +// +#define EFI_TLS_CA_CERTIFICATE_GUID \ + { \ + 0xfd2340D0, 0x3dab, 0x4349, { 0xa6, 0xc7, 0x3b, 0x4f, 0x12, 0xb4, 0x8e, 0xae } \ + } + +#define EFI_TLS_CA_CERTIFICATE_VARIABLE L"TlsCaCertificate" + +extern EFI_GUID gEfiTlsCaCertificateGuid; + +#endif + diff --git a/NetworkPkg/Include/Guid/WifiConnectionManagerConfigHii.h b/NetworkPkg/Include/Guid/WifiConnectionManagerConfigHii.h new file mode 100644 index 000000000..6d2b1344b --- /dev/null +++ b/NetworkPkg/Include/Guid/WifiConnectionManagerConfigHii.h @@ -0,0 +1,19 @@ +/** @file + GUIDs used as HII FormSet and HII Package list GUID in WiFi Connection Manager. + +Copyright (c) 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __WIFI_CONNECTION_MANAGER_HII_GUID_H__ +#define __WIFI_CONNECTION_MANAGER_HII_GUID_H__ + +#define WIFI_CONNECTION_MANAGER_CONFIG_GUID \ + { \ + 0x9f94d327, 0x0b18, 0x4245, { 0x8f, 0xf2, 0x83, 0x2e, 0x30, 0xd, 0x2c, 0xef } \ + } + +extern EFI_GUID gWifiConfigGuid; + +#endif diff --git a/NetworkPkg/Include/Library/DpcLib.h b/NetworkPkg/Include/Library/DpcLib.h new file mode 100644 index 000000000..a70af1d12 --- /dev/null +++ b/NetworkPkg/Include/Library/DpcLib.h @@ -0,0 +1,53 @@ +/** @file + DpcLib.h. + +Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _DPC_LIB_H_ +#define _DPC_LIB_H_ + +#include + +/** + Add a Deferred Procedure Call to the end of the DPC queue. + + @param[in] DpcTpl The EFI_TPL that the DPC should invoke. + @param[in] DpcProcedure The pointer to the DPC's function. + @param[in] DpcContext The pointer to the DPC's context. Passed to DpcProcedure + when DpcProcedure is invoked. + + @retval EFI_SUCCESS The DPC was queued. + @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL. + @retval EFI_INVALID_PARAMETER DpcProcedure is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to + add the DPC to the queue. + +**/ +EFI_STATUS +EFIAPI +QueueDpc ( + IN EFI_TPL DpcTpl, + IN EFI_DPC_PROCEDURE DpcProcedure, + IN VOID *DpcContext OPTIONAL + ); + +/** + Dispatch the queue of DPCs. All DPCs that have been queued with a DpcTpl + value greater than or equal to the current TPL are invoked in the order that + they were queued. DPCs with higher DpcTpl values are invoked before DPCs with + lower DpcTpl values. + + @retval EFI_SUCCESS One or more DPCs were invoked. + @retval EFI_NOT_FOUND No DPCs were invoked. + +**/ +EFI_STATUS +EFIAPI +DispatchDpc ( + VOID + ); + +#endif diff --git a/NetworkPkg/Include/Library/HttpLib.h b/NetworkPkg/Include/Library/HttpLib.h new file mode 100644 index 000000000..93100eb35 --- /dev/null +++ b/NetworkPkg/Include/Library/HttpLib.h @@ -0,0 +1,481 @@ +/** @file + This library is used to share code between UEFI network stack modules. + It provides the helper routines to parse the HTTP message byte stream. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _HTTP_LIB_H_ +#define _HTTP_LIB_H_ + +#include + + +/** + Decode a percent-encoded URI component to the ASCII character. + + Decode the input component in Buffer according to RFC 3986. The caller is responsible to make + sure ResultBuffer points to a buffer with size equal or greater than ((AsciiStrSize (Buffer)) + in bytes. + + @param[in] Buffer The pointer to a percent-encoded URI component. + @param[in] BufferLength Length of Buffer in bytes. + @param[out] ResultBuffer Point to the buffer to store the decode result. + @param[out] ResultLength Length of decoded string in ResultBuffer in bytes. + + @retval EFI_SUCCESS Successfully decoded the URI. + @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string. + +**/ +EFI_STATUS +EFIAPI +UriPercentDecode ( + IN CHAR8 *Buffer, + IN UINT32 BufferLength, + OUT CHAR8 *ResultBuffer, + OUT UINT32 *ResultLength + ); + +/** + Create a URL parser for the input URL string. + + This function will parse and dereference the input HTTP URL into it components. The original + content of the URL won't be modified and the result will be returned in UrlParser, which can + be used in other functions like NetHttpUrlGetHostName(). It is the caller's responsibility to + free the buffer returned in *UrlParser by HttpUrlFreeParser(). + + @param[in] Url The pointer to a HTTP URL string. + @param[in] Length Length of Url in bytes. + @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not. + @param[out] UrlParser Pointer to the returned buffer to store the parse result. + + @retval EFI_SUCCESS Successfully dereferenced the HTTP URL. + @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpParseUrl ( + IN CHAR8 *Url, + IN UINT32 Length, + IN BOOLEAN IsConnectMethod, + OUT VOID **UrlParser + ); + +/** + Get the Hostname from a HTTP URL. + + This function will return the HostName according to the Url and previous parse result ,and + it is the caller's responsibility to free the buffer returned in *HostName. + + @param[in] Url The pointer to a HTTP URL string. + @param[in] UrlParser URL Parse result returned by NetHttpParseUrl(). + @param[out] HostName Pointer to a buffer to store the HostName. + + @retval EFI_SUCCESS Successfully get the required component. + @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid. + @retval EFI_NOT_FOUND No hostName component in the URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpUrlGetHostName ( + IN CHAR8 *Url, + IN VOID *UrlParser, + OUT CHAR8 **HostName + ); + +/** + Get the IPv4 address from a HTTP URL. + + This function will return the IPv4 address according to the Url and previous parse result. + + @param[in] Url The pointer to a HTTP URL string. + @param[in] UrlParser URL Parse result returned by NetHttpParseUrl(). + @param[out] Ip4Address Pointer to a buffer to store the IP address. + + @retval EFI_SUCCESS Successfully get the required component. + @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid. + @retval EFI_NOT_FOUND No IPv4 address component in the URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpUrlGetIp4 ( + IN CHAR8 *Url, + IN VOID *UrlParser, + OUT EFI_IPv4_ADDRESS *Ip4Address + ); + +/** + Get the IPv6 address from a HTTP URL. + + This function will return the IPv6 address according to the Url and previous parse result. + + @param[in] Url The pointer to a HTTP URL string. + @param[in] UrlParser URL Parse result returned by NetHttpParseUrl(). + @param[out] Ip6Address Pointer to a buffer to store the IP address. + + @retval EFI_SUCCESS Successfully get the required component. + @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid. + @retval EFI_NOT_FOUND No IPv6 address component in the URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpUrlGetIp6 ( + IN CHAR8 *Url, + IN VOID *UrlParser, + OUT EFI_IPv6_ADDRESS *Ip6Address + ); + +/** + Get the port number from a HTTP URL. + + This function will return the port number according to the Url and previous parse result. + + @param[in] Url The pointer to a HTTP URL string. + @param[in] UrlParser URL Parse result returned by NetHttpParseUrl(). + @param[out] Port Pointer to a buffer to store the port number. + + @retval EFI_SUCCESS Successfully get the required component. + @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid. + @retval EFI_NOT_FOUND No port number in the URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpUrlGetPort ( + IN CHAR8 *Url, + IN VOID *UrlParser, + OUT UINT16 *Port + ); + +/** + Get the Path from a HTTP URL. + + This function will return the Path according to the Url and previous parse result,and + it is the caller's responsibility to free the buffer returned in *Path. + + @param[in] Url The pointer to a HTTP URL string. + @param[in] UrlParser URL Parse result returned by NetHttpParseUrl(). + @param[out] Path Pointer to a buffer to store the Path. + + @retval EFI_SUCCESS Successfully get the required component. + @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid. + @retval EFI_NOT_FOUND No hostName component in the URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpUrlGetPath ( + IN CHAR8 *Url, + IN VOID *UrlParser, + OUT CHAR8 **Path + ); + +/** + Release the resource of the URL parser. + + @param[in] UrlParser Pointer to the parser. + +**/ +VOID +EFIAPI +HttpUrlFreeParser ( + IN VOID *UrlParser + ); + +// +// HTTP body parser interface. +// + +typedef enum { + // + // Part of entity data. + // Length of entity body in Data. + // + BodyParseEventOnData, + // + // End of message body. + // Length is 0 and Data points to next byte after the end of the message. + // + BodyParseEventOnComplete +} HTTP_BODY_PARSE_EVENT; + +/** + A callback function to intercept events during message parser. + + This function will be invoked during HttpParseMessageBody() with various events type. An error + return status of the callback function will cause the HttpParseMessageBody() aborted. + + @param[in] EventType Event type of this callback call. + @param[in] Data A pointer to data buffer. + @param[in] Length Length in bytes of the Data. + @param[in] Context Callback context set by HttpInitMsgParser(). + + @retval EFI_SUCCESS Continue to parser the message body. + @retval Others Abort the parse. + +**/ +typedef +EFI_STATUS +(EFIAPI *HTTP_BODY_PARSER_CALLBACK) ( + IN HTTP_BODY_PARSE_EVENT EventType, + IN CHAR8 *Data, + IN UINTN Length, + IN VOID *Context +); + +/** + Initialize a HTTP message-body parser. + + This function will create and initialize a HTTP message parser according to caller provided HTTP message + header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser(). + + @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message. + @param[in] StatusCode Response status code returned by the remote host. + @param[in] HeaderCount Number of HTTP header structures in Headers. + @param[in] Headers Array containing list of HTTP headers. + @param[in] Callback Callback function that is invoked when parsing the HTTP message-body, + set to NULL to ignore all events. + @param[in] Context Pointer to the context that will be passed to Callback. + @param[out] MsgParser Pointer to the returned buffer to store the message parser. + + @retval EFI_SUCCESS Successfully initialized the parser. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL. + @retval Others Failed to initialize the parser. + +**/ +EFI_STATUS +EFIAPI +HttpInitMsgParser ( + IN EFI_HTTP_METHOD Method, + IN EFI_HTTP_STATUS_CODE StatusCode, + IN UINTN HeaderCount, + IN EFI_HTTP_HEADER *Headers, + IN HTTP_BODY_PARSER_CALLBACK Callback, + IN VOID *Context, + OUT VOID **MsgParser + ); + +/** + Parse message body. + + Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially. + + @param[in, out] MsgParser Pointer to the message parser. + @param[in] BodyLength Length in bytes of the Body. + @param[in] Body Pointer to the buffer of the message-body to be parsed. + + @retval EFI_SUCCESS Successfully parse the message-body. + @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0. + @retval EFI_ABORTED Operation aborted. + @retval Other Error happened while parsing message body. + +**/ +EFI_STATUS +EFIAPI +HttpParseMessageBody ( + IN OUT VOID *MsgParser, + IN UINTN BodyLength, + IN CHAR8 *Body + ); + +/** + Check whether the message-body is complete or not. + + @param[in] MsgParser Pointer to the message parser. + + @retval TRUE Message-body is complete. + @retval FALSE Message-body is not complete. + +**/ +BOOLEAN +EFIAPI +HttpIsMessageComplete ( + IN VOID *MsgParser + ); + +/** + Get the content length of the entity. + + Note that in trunk transfer, the entity length is not valid until the whole message body is received. + + @param[in] MsgParser Pointer to the message parser. + @param[out] ContentLength Pointer to store the length of the entity. + + @retval EFI_SUCCESS Successfully to get the entity length. + @retval EFI_NOT_READY Entity length is not valid yet. + @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL. + +**/ +EFI_STATUS +EFIAPI +HttpGetEntityLength ( + IN VOID *MsgParser, + OUT UINTN *ContentLength + ); + +/** + Release the resource of the message parser. + + @param[in] MsgParser Pointer to the message parser. + +**/ +VOID +EFIAPI +HttpFreeMsgParser ( + IN VOID *MsgParser + ); + + +/** + Find a specified header field according to the field name. + + @param[in] HeaderCount Number of HTTP header structures in Headers list. + @param[in] Headers Array containing list of HTTP headers. + @param[in] FieldName Null terminated string which describes a field name. + + @return Pointer to the found header or NULL. + +**/ +EFI_HTTP_HEADER * +EFIAPI +HttpFindHeader ( + IN UINTN HeaderCount, + IN EFI_HTTP_HEADER *Headers, + IN CHAR8 *FieldName + ); + +/** + Set FieldName and FieldValue into specified HttpHeader. + + @param[in,out] HttpHeader Specified HttpHeader. + @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string. + @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string. + + + @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully. + @retval EFI_INVALID_PARAMETER The parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + +**/ +EFI_STATUS +EFIAPI +HttpSetFieldNameAndValue ( + IN OUT EFI_HTTP_HEADER *HttpHeader, + IN CONST CHAR8 *FieldName, + IN CONST CHAR8 *FieldValue + ); + +/** + Get one key/value header pair from the raw string. + + @param[in] String Pointer to the raw string. + @param[out] FieldName Points directly to field name within 'HttpHeader'. + @param[out] FieldValue Points directly to field value within 'HttpHeader'. + + @return Pointer to the next raw string. + @return NULL if no key/value header pair from this raw string. + +**/ +CHAR8 * +EFIAPI +HttpGetFieldNameAndValue ( + IN CHAR8 *String, + OUT CHAR8 **FieldName, + OUT CHAR8 **FieldValue + ); + +/** + Free existing HeaderFields. + + @param[in] HeaderFields Pointer to array of key/value header pairs waiting for free. + @param[in] FieldCount The number of header pairs in HeaderFields. + +**/ +VOID +EFIAPI +HttpFreeHeaderFields ( + IN EFI_HTTP_HEADER *HeaderFields, + IN UINTN FieldCount + ); + +/** + Generate HTTP request message. + + This function will allocate memory for the whole HTTP message and generate a + well formatted HTTP Request message in it, include the Request-Line, header + fields and also the message body. It is the caller's responsibility to free + the buffer returned in *RequestMsg. + + @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which + contains the required information to generate + the HTTP request message. + @param[in] Url The URL of a remote host. + @param[out] RequestMsg Pointer to the created HTTP request message. + NULL if any error occured. + @param[out] RequestMsgSize Size of the RequestMsg (in bytes). + + @retval EFI_SUCCESS If HTTP request string was created successfully. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_INVALID_PARAMETER The input arguments are invalid. + +**/ +EFI_STATUS +EFIAPI +HttpGenRequestMessage ( + IN CONST EFI_HTTP_MESSAGE *Message, + IN CONST CHAR8 *Url, + OUT CHAR8 **RequestMsg, + OUT UINTN *RequestMsgSize + ); + +/** + Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined + in UEFI 2.5 specification. + + @param[in] StatusCode The status code value in HTTP message. + + @return Value defined in EFI_HTTP_STATUS_CODE . + +**/ +EFI_HTTP_STATUS_CODE +EFIAPI +HttpMappingToStatusCode ( + IN UINTN StatusCode + ); + +/** + Check whether header field called FieldName is in DeleteList. + + @param[in] DeleteList Pointer to array of key/value header pairs. + @param[in] DeleteCount The number of header pairs. + @param[in] FieldName Pointer to header field's name. + + @return TRUE if FieldName is not in DeleteList, that means this header field is valid. + @return FALSE if FieldName is in DeleteList, that means this header field is invalid. + +**/ +BOOLEAN +EFIAPI +HttpIsValidHttpHeader ( + IN CHAR8 *DeleteList[], + IN UINTN DeleteCount, + IN CHAR8 *FieldName + ); + + +#endif + diff --git a/NetworkPkg/Include/Library/IpIoLib.h b/NetworkPkg/Include/Library/IpIoLib.h new file mode 100644 index 000000000..2d6e090f7 --- /dev/null +++ b/NetworkPkg/Include/Library/IpIoLib.h @@ -0,0 +1,607 @@ +/** @file + This library is only intended to be used by UEFI network stack modules. + It provides the combined IpIo layer on the EFI IP4 Protocol and EFI IP6 protocol. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _IP_IO_H_ +#define _IP_IO_H_ + +#include +#include + +#include + +// +// type and code define for ICMP protocol error +// from IP +// +#define ICMP_TYPE_UNREACH 3 +#define ICMP_TYPE_TIMXCEED 11 +#define ICMP_TYPE_PARAMPROB 12 +#define ICMP_TYPE_SOURCEQUENCH 4 + +#define ICMP_CODE_UNREACH_NET 0 +#define ICMP_CODE_UNREACH_HOST 1 +#define ICMP_CODE_UNREACH_PROTOCOL 2 +#define ICMP_CODE_UNREACH_PORT 3 +#define ICMP_CODE_UNREACH_NEEDFRAG 4 +#define ICMP_CODE_UNREACH_SRCFAIL 5 +#define ICMP_CODE_UNREACH_NET_UNKNOWN 6 +#define ICMP_CODE_UNREACH_HOST_UNKNOWN 7 +#define ICMP_CODE_UNREACH_ISOLATED 8 +#define ICMP_CODE_UNREACH_NET_PROHIB 9 +#define ICMP_CODE_UNREACH_HOST_PROHIB 10 +#define ICMP_CODE_UNREACH_TOSNET 11 +#define ICMP_CODE_UNREACH_TOSHOST 12 + +/** + Get the IP header length from the struct EFI_IP4_HEADER. HeaderLength is + Internet header length in 32-bit words, so HeaderLength<<2 is the real + length of IP header. + + @param[out] HdrPtr A pointer to EFI_IP4_HEADER. + + @return The IP header length. +**/ +#define EFI_IP4_HEADER_LEN(HdrPtr) ((HdrPtr)->HeaderLength << 2) + +/** + To types of ICMP error which consist of ICMP header, IP header and original + datagram's data, get length from sum of ICMP header length, IP header length + and first 64 bits of datagram's data length. + + @param[in] IpHdr A pointer to EFI_IP4_HEADER. + + @return The ICMP error length. +**/ +#define ICMP_ERRLEN(IpHdr) \ + (sizeof(IP4_ICMP_HEAD) + EFI_IP4_HEADER_LEN(IpHdr) + 8) + +/** + Get the packet header from NET_BUF. + + @param[out] Buf A pointer to NET_BUF. + @param[in] Type Header type. + + @return The pointer to packet header. +**/ +#define NET_PROTO_HDR(Buf, Type) ((Type *) ((Buf)->BlockOp[0].Head)) + + +extern EFI_IP4_CONFIG_DATA mIp4IoDefaultIpConfigData; +extern EFI_IP6_CONFIG_DATA mIp6IoDefaultIpConfigData; + + +/// +/// This error will be delivered to the +/// listening transportation layer protocol +/// that consumes IpIO. +/// + +#define ICMP_ERR_UNREACH_NET 0 +#define ICMP_ERR_UNREACH_HOST 1 +#define ICMP_ERR_UNREACH_PROTOCOL 2 +#define ICMP_ERR_UNREACH_PORT 3 +#define ICMP_ERR_MSGSIZE 4 +#define ICMP_ERR_UNREACH_SRCFAIL 5 +#define ICMP_ERR_TIMXCEED_INTRANS 6 +#define ICMP_ERR_TIMXCEED_REASS 7 +#define ICMP_ERR_QUENCH 8 +#define ICMP_ERR_PARAMPROB 9 + +#define ICMP6_ERR_UNREACH_NET 0 +#define ICMP6_ERR_UNREACH_HOST 1 +#define ICMP6_ERR_UNREACH_PROTOCOL 2 +#define ICMP6_ERR_UNREACH_PORT 3 +#define ICMP6_ERR_PACKAGE_TOOBIG 4 +#define ICMP6_ERR_TIMXCEED_HOPLIMIT 5 +#define ICMP6_ERR_TIMXCEED_REASS 6 +#define ICMP6_ERR_PARAMPROB_HEADER 7 +#define ICMP6_ERR_PARAMPROB_NEXHEADER 8 +#define ICMP6_ERR_PARAMPROB_IPV6OPTION 9 + +/// +/// The helper struct for IpIoGetIcmpErrStatus(). It is for internal use only. +/// +typedef struct { + BOOLEAN IsHard; + BOOLEAN Notify; +} ICMP_ERROR_INFO; + +typedef union { + EFI_IP4_COMPLETION_TOKEN Ip4Token; + EFI_IP6_COMPLETION_TOKEN Ip6Token; +} IP_IO_IP_COMPLETION_TOKEN; + +typedef union { + EFI_IP4_TRANSMIT_DATA Ip4TxData; + EFI_IP6_TRANSMIT_DATA Ip6TxData; +} IP_IO_IP_TX_DATA; + +typedef union { + EFI_IP4_RECEIVE_DATA Ip4RxData; + EFI_IP6_RECEIVE_DATA Ip6RxData; +} IP_IO_IP_RX_DATA; + +typedef union { + EFI_IP4_OVERRIDE_DATA Ip4OverrideData; + EFI_IP6_OVERRIDE_DATA Ip6OverrideData; +} IP_IO_OVERRIDE; + +typedef union { + EFI_IP4_CONFIG_DATA Ip4CfgData; + EFI_IP6_CONFIG_DATA Ip6CfgData; +} IP_IO_IP_CONFIG_DATA; + +typedef union { + EFI_IP4_HEADER *Ip4Hdr; + EFI_IP6_HEADER *Ip6Hdr; +} IP_IO_IP_HEADER; + +typedef union { + IP4_ADDR SubnetMask; + UINT8 PrefixLength; +} IP_IO_IP_MASK; + +typedef union { + EFI_IP4_PROTOCOL *Ip4; + EFI_IP6_PROTOCOL *Ip6; +} IP_IO_IP_PROTOCOL; + +/// +/// The IP session for an IP receive packet. +/// +typedef struct _EFI_NET_SESSION_DATA { + EFI_IP_ADDRESS Source; ///< Source IP of the received packet. + EFI_IP_ADDRESS Dest; ///< Destination IP of the received packet. + IP_IO_IP_HEADER IpHdr; ///< IP header of the received packet. + UINT32 IpHdrLen; ///< IP header length of the received packet. + ///< For IPv6, it includes the IP6 header + ///< length and extension header length. For + ///< IPv4, it includes the IP4 header length + ///< and options length. + UINT8 IpVersion; ///< The IP version of the received packet. +} EFI_NET_SESSION_DATA; + +/** + The prototype is called back when an IP packet is received. + + @param[in] Status The result of the receive request. + @param[in] IcmpErr Valid when Status is EFI_ICMP_ERROR. + @param[in] NetSession The IP session for the received packet. + @param[in] Pkt The packet received. + @param[in] Context The data provided by the user for the received packet when + the callback is registered in IP_IO_OPEN_DATA::RcvdContext. + +**/ +typedef +VOID +(EFIAPI *PKT_RCVD_NOTIFY) ( + IN EFI_STATUS Status, + IN UINT8 IcmpErr, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Pkt, + IN VOID *Context + ); + +/** + The prototype is called back when an IP packet is sent. + + @param[in] Status Result of the IP packet being sent. + @param[in] Context The data provided by user for the received packet when + the callback is registered in IP_IO_OPEN_DATA::SndContext. + @param[in] Sender A Union type to specify a pointer of EFI_IP4_PROTOCOL + or EFI_IP6_PROTOCOL. + @param[in] NotifyData The Context data specified when calling IpIoSend() + +**/ +typedef +VOID +(EFIAPI *PKT_SENT_NOTIFY) ( + IN EFI_STATUS Status, + IN VOID *Context, + IN IP_IO_IP_PROTOCOL Sender, + IN VOID *NotifyData + ); + +/// +/// This data structure wraps Ip4/Ip6 instances. The IpIo Library uses it for all +/// Ip4/Ip6 operations. +/// +typedef struct _IP_IO { + /// + /// The node used to link this IpIo to the active IpIo list. + /// + LIST_ENTRY Entry; + + /// + /// The list used to maintain the IP instance for different sending purpose. + /// + LIST_ENTRY IpList; + + EFI_HANDLE Controller; + EFI_HANDLE Image; + EFI_HANDLE ChildHandle; + // + // The IP instance consumed by this IP_IO + // + IP_IO_IP_PROTOCOL Ip; + BOOLEAN IsConfigured; + + /// + /// Some ip configuration data can be changed. + /// + UINT8 Protocol; + + /// + /// Token and event used to get data from IP. + /// + IP_IO_IP_COMPLETION_TOKEN RcvToken; + + /// + /// List entry used to link the token passed to IP_IO. + /// + LIST_ENTRY PendingSndList; + + // + // User interface used to get notify from IP_IO + // + VOID *RcvdContext; ///< See IP_IO_OPEN_DATA::RcvdContext. + VOID *SndContext; ///< See IP_IO_OPEN_DATA::SndContext. + PKT_RCVD_NOTIFY PktRcvdNotify; ///< See IP_IO_OPEN_DATA::PktRcvdNotify. + PKT_SENT_NOTIFY PktSentNotify; ///< See IP_IO_OPEN_DATA::PktSentNotify. + UINT8 IpVersion; + IP4_ADDR StationIp; + IP4_ADDR SubnetMask; +} IP_IO; + +/// +/// The struct is for the user to pass IP configuration and callbacks to IP_IO. +/// It is used by IpIoOpen(). +/// +typedef struct _IP_IO_OPEN_DATA { + IP_IO_IP_CONFIG_DATA IpConfigData; ///< Configuration of the IP instance. + VOID *RcvdContext; ///< Context data used by receive callback. + VOID *SndContext; ///< Context data used by send callback. + PKT_RCVD_NOTIFY PktRcvdNotify; ///< Receive callback. + PKT_SENT_NOTIFY PktSentNotify; ///< Send callback. +} IP_IO_OPEN_DATA; + +/// +/// Internal struct book-keeping send request of IP_IO. +/// +/// An IP_IO_SEND_ENTRY will be created each time a send request is issued to +/// IP_IO via IpIoSend(). +/// +typedef struct _IP_IO_SEND_ENTRY { + LIST_ENTRY Entry; + IP_IO *IpIo; + VOID *Context; + VOID *NotifyData; + IP_IO_IP_PROTOCOL Ip; + NET_BUF *Pkt; + IP_IO_IP_COMPLETION_TOKEN SndToken; +} IP_IO_SEND_ENTRY; + +/// +/// The IP_IO_IP_INFO is used in IpIoSend() to override the default IP instance +/// in IP_IO. +/// +typedef struct _IP_IO_IP_INFO { + EFI_IP_ADDRESS Addr; + IP_IO_IP_MASK PreMask; + LIST_ENTRY Entry; + EFI_HANDLE ChildHandle; + IP_IO_IP_PROTOCOL Ip; + IP_IO_IP_COMPLETION_TOKEN DummyRcvToken; + INTN RefCnt; + UINT8 IpVersion; +} IP_IO_IP_INFO; + +/** + Create a new IP_IO instance. + + If IpVersion is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + This function uses IP4/IP6 service binding protocol in Controller to create + an IP4/IP6 child (aka IP4/IP6 instance). + + @param[in] Image The image handle of the driver or application that + consumes IP_IO. + @param[in] Controller The controller handle that has IP4 or IP6 service + binding protocol installed. + @param[in] IpVersion The version of the IP protocol to use, either + IPv4 or IPv6. + + @return The pointer to a newly created IP_IO instance, or NULL if failed. + +**/ +IP_IO * +EFIAPI +IpIoCreate ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN UINT8 IpVersion + ); + +/** + Destroy an IP_IO instance. + + This function is paired with IpIoCreate(). The IP_IO will be closed first. + Resource will be freed afterwards. See IpIoClose(). + + @param[in, out] IpIo The pointer to the IP_IO instance that needs to be + destroyed. + + @retval EFI_SUCCESS The IP_IO instance was destroyed successfully. + @retval Others An error condition occurred. + +**/ +EFI_STATUS +EFIAPI +IpIoDestroy ( + IN OUT IP_IO *IpIo + ); + +/** + Stop an IP_IO instance. + + If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + This function is paired with IpIoOpen(). The IP_IO will be unconfigured, and all + pending send/receive tokens will be canceled. + + @param[in, out] IpIo The pointer to the IP_IO instance that needs to stop. + + @retval EFI_SUCCESS The IP_IO instance stopped successfully. + @retval EFI_INVALID_PARAMETER Invalid input parameter. + @retval Others Anrror condition occurred. + +**/ +EFI_STATUS +EFIAPI +IpIoStop ( + IN OUT IP_IO *IpIo + ); + +/** + Open an IP_IO instance for use. + + If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + This function is called after IpIoCreate(). It is used for configuring the IP + instance and register the callbacks and their context data for sending and + receiving IP packets. + + @param[in, out] IpIo The pointer to an IP_IO instance that needs + to open. + @param[in] OpenData The configuration data and callbacks for + the IP_IO instance. + + @retval EFI_SUCCESS The IP_IO instance opened with OpenData + successfully. + @retval EFI_ACCESS_DENIED The IP_IO instance is configured, avoid to + reopen it. + @retval EFI_UNSUPPORTED IPv4 RawData mode is no supported. + @retval EFI_INVALID_PARAMETER Invalid input parameter. + @retval Others Error condition occurred. + +**/ +EFI_STATUS +EFIAPI +IpIoOpen ( + IN OUT IP_IO *IpIo, + IN IP_IO_OPEN_DATA *OpenData + ); + +/** + Send out an IP packet. + + This function is called after IpIoOpen(). The data to be sent is wrapped in + Pkt. The IP instance wrapped in IpIo is used for sending by default but can be + overriden by Sender. Other sending configs, like source address and gateway + address etc., are specified in OverrideData. + + @param[in, out] IpIo Pointer to an IP_IO instance used for sending IP + packet. + @param[in, out] Pkt Pointer to the IP packet to be sent. + @param[in] Sender The IP protocol instance used for sending. + @param[in] Context Optional context data. + @param[in] NotifyData Optional notify data. + @param[in] Dest The destination IP address to send this packet to. + This parameter is optional when using IPv6. + @param[in] OverrideData The data to override some configuration of the IP + instance used for sending. + + @retval EFI_SUCCESS The operation is completed successfully. + @retval EFI_INVALID_PARAMETER The input parameter is not correct. + @retval EFI_NOT_STARTED The IpIo is not configured. + @retval EFI_OUT_OF_RESOURCES Failed due to resource limit. + @retval Others Error condition occurred. + +**/ +EFI_STATUS +EFIAPI +IpIoSend ( + IN OUT IP_IO *IpIo, + IN OUT NET_BUF *Pkt, + IN IP_IO_IP_INFO *Sender OPTIONAL, + IN VOID *Context OPTIONAL, + IN VOID *NotifyData OPTIONAL, + IN EFI_IP_ADDRESS *Dest OPTIONAL, + IN IP_IO_OVERRIDE *OverrideData OPTIONAL + ); + +/** + Cancel the IP transmit token that wraps this Packet. + + If IpIo is NULL, then ASSERT(). + If Packet is NULL, then ASSERT(). + + @param[in] IpIo The pointer to the IP_IO instance. + @param[in] Packet The pointer to the packet of NET_BUF to cancel. + +**/ +VOID +EFIAPI +IpIoCancelTxToken ( + IN IP_IO *IpIo, + IN VOID *Packet + ); + +/** + Add a new IP instance for sending data. + + If IpIo is NULL, then ASSERT(). + If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + The function is used to add the IP_IO to the IP_IO sending list. The caller + can later use IpIoFindSender() to get the IP_IO and call IpIoSend() to send + data. + + @param[in, out] IpIo The pointer to an IP_IO instance to add a new IP + instance for sending purposes. + + @return The pointer to the created IP_IO_IP_INFO structure; NULL if failed. + +**/ +IP_IO_IP_INFO * +EFIAPI +IpIoAddIp ( + IN OUT IP_IO *IpIo + ); + +/** + Configure the IP instance of this IpInfo and start the receiving if IpConfigData + is not NULL. + + If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + @param[in, out] IpInfo The pointer to the IP_IO_IP_INFO instance. + @param[in, out] IpConfigData The IP4 or IP6 configure data used to configure + the IP instance. If NULL, the IP instance is reset. + If UseDefaultAddress is set to TRUE, and the configure + operation succeeds, the default address information + is written back in this IpConfigData. + + @retval EFI_SUCCESS The IP instance of this IpInfo was configured successfully, + or there is no need to reconfigure it. + @retval Others The configuration failed. + +**/ +EFI_STATUS +EFIAPI +IpIoConfigIp ( + IN OUT IP_IO_IP_INFO *IpInfo, + IN OUT VOID *IpConfigData OPTIONAL + ); + +/** + Destroy an IP instance maintained in IpIo->IpList for + sending purpose. + + If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + This function pairs with IpIoAddIp(). The IpInfo is previously created by + IpIoAddIp(). The IP_IO_IP_INFO::RefCnt is decremented and the IP instance + will be dstroyed if the RefCnt is zero. + + @param[in] IpIo The pointer to the IP_IO instance. + @param[in] IpInfo The pointer to the IpInfo to be removed. + +**/ +VOID +EFIAPI +IpIoRemoveIp ( + IN IP_IO *IpIo, + IN IP_IO_IP_INFO *IpInfo + ); + +/** + Find the first IP protocol maintained in IpIo whose local + address is the same as Src. + + This function is called when the caller needs the IpIo to send data to the + specified Src. The IpIo was added previously by IpIoAddIp(). + + @param[in, out] IpIo The pointer to the pointer of the IP_IO instance. + @param[in] IpVersion The version of the IP protocol to use, either + IPv4 or IPv6. + @param[in] Src The local IP address. + + @return The pointer to the IP protocol can be used for sending purpose and its local + address is the same with Src. NULL if failed. + +**/ +IP_IO_IP_INFO * +EFIAPI +IpIoFindSender ( + IN OUT IP_IO **IpIo, + IN UINT8 IpVersion, + IN EFI_IP_ADDRESS *Src + ); + +/** + Get the ICMP error map information. + + The ErrorStatus will be returned. The IsHard and Notify are optional. If they + are not NULL, this routine will fill them. + + @param[in] IcmpError IcmpError Type. + @param[in] IpVersion The version of the IP protocol to use, + either IPv4 or IPv6. + @param[out] IsHard If TRUE, indicates that it is a hard error. + @param[out] Notify If TRUE, SockError needs to be notified. + + @retval EFI_UNSUPPORTED Unrecognizable ICMP error code + @return The ICMP Error Status, such as EFI_NETWORK_UNREACHABLE. + +**/ +EFI_STATUS +EFIAPI +IpIoGetIcmpErrStatus ( + IN UINT8 IcmpError, + IN UINT8 IpVersion, + OUT BOOLEAN *IsHard OPTIONAL, + OUT BOOLEAN *Notify OPTIONAL + ); + +/** + Refresh the remote peer's Neighbor Cache entries. + + This function is called when the caller needs the IpIo to refresh the existing + IPv6 neighbor cache entries since the neighbor is considered reachable by the + node has recently received a confirmation that packets sent recently to the + neighbor were received by its IP layer. + + @param[in] IpIo The pointer to an IP_IO instance + @param[in] Neighbor The IP address of the neighbor + @param[in] Timeout The time in 100-ns units that this entry will + remain in the neighbor cache. A value of + zero means that the entry is permanent. + A value of non-zero means that the entry is + dynamic and will be deleted after Timeout. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The IpIo is not configured. + @retval EFI_INVALID_PARAMETER The Neighbor Address is invalid. + @retval EFI_NOT_FOUND The neighbor cache entry is not in the + neighbor table. + @retval EFI_UNSUPPORTED IP version is IPv4, which doesn't support neighbor cache refresh. + @retval EFI_OUT_OF_RESOURCES Failed due to resource limitations. + +**/ +EFI_STATUS +EFIAPI +IpIoRefreshNeighbor ( + IN IP_IO *IpIo, + IN EFI_IP_ADDRESS *Neighbor, + IN UINT32 Timeout + ); + +#endif + diff --git a/NetworkPkg/Include/Library/NetLib.h b/NetworkPkg/Include/Library/NetLib.h new file mode 100644 index 000000000..786382a1f --- /dev/null +++ b/NetworkPkg/Include/Library/NetLib.h @@ -0,0 +1,2266 @@ +/** @file + This library is only intended to be used by UEFI network stack modules. + It provides basic functions for the UEFI network stack. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _NET_LIB_H_ +#define _NET_LIB_H_ + +#include + +#include +#include + +typedef UINT32 IP4_ADDR; +typedef UINT32 TCP_SEQNO; +typedef UINT16 TCP_PORTNO; + + +#define NET_ETHER_ADDR_LEN 6 +#define NET_IFTYPE_ETHERNET 0x01 + +#define NET_VLAN_TAG_LEN 4 +#define ETHER_TYPE_VLAN 0x8100 + +#define EFI_IP_PROTO_UDP 0x11 +#define EFI_IP_PROTO_TCP 0x06 +#define EFI_IP_PROTO_ICMP 0x01 +#define IP4_PROTO_IGMP 0x02 +#define IP6_ICMP 58 +#define DNS_MAX_NAME_SIZE 255 +#define DNS_MAX_MESSAGE_SIZE 512 + +// +// The address classification +// +#define IP4_ADDR_CLASSA 1 // Deprecated +#define IP4_ADDR_CLASSB 2 // Deprecated +#define IP4_ADDR_CLASSC 3 // Deprecated +#define IP4_ADDR_CLASSD 4 +#define IP4_ADDR_CLASSE 5 + +#define IP4_MASK_NUM 33 +#define IP6_PREFIX_NUM 129 + +#define IP4_MASK_MAX 32 +#define IP6_PREFIX_MAX 128 + +#define IP6_HOP_BY_HOP 0 +#define IP6_DESTINATION 60 +#define IP6_ROUTING 43 +#define IP6_FRAGMENT 44 +#define IP6_AH 51 +#define IP6_ESP 50 +#define IP6_NO_NEXT_HEADER 59 + +#define IP_VERSION_4 4 +#define IP_VERSION_6 6 + +#define IP6_PREFIX_LENGTH 64 + +// +// DNS QTYPE values +// +#define DNS_TYPE_A 1 +#define DNS_TYPE_NS 2 +#define DNS_TYPE_CNAME 5 +#define DNS_TYPE_SOA 6 +#define DNS_TYPE_WKS 11 +#define DNS_TYPE_PTR 12 +#define DNS_TYPE_HINFO 13 +#define DNS_TYPE_MINFO 14 +#define DNS_TYPE_MX 15 +#define DNS_TYPE_TXT 16 +#define DNS_TYPE_AAAA 28 +#define DNS_TYPE_SRV_RR 33 +#define DNS_TYPE_AXFR 252 +#define DNS_TYPE_MAILB 253 +#define DNS_TYPE_ANY 255 + +// +// DNS QCLASS values +// +#define DNS_CLASS_INET 1 +#define DNS_CLASS_CH 3 +#define DNS_CLASS_HS 4 +#define DNS_CLASS_ANY 255 + +// +// Number of 100ns units time Interval for network media state detect +// +#define MEDIA_STATE_DETECT_TIME_INTERVAL 1000000U + + +#pragma pack(1) + +// +// Ethernet head definition +// +typedef struct { + UINT8 DstMac [NET_ETHER_ADDR_LEN]; + UINT8 SrcMac [NET_ETHER_ADDR_LEN]; + UINT16 EtherType; +} ETHER_HEAD; + +// +// 802.1Q VLAN Tag Control Information +// +typedef union { + struct { + UINT16 Vid : 12; // Unique VLAN identifier (0 to 4094) + UINT16 Cfi : 1; // Canonical Format Indicator + UINT16 Priority : 3; // 802.1Q priority level (0 to 7) + } Bits; + UINT16 Uint16; +} VLAN_TCI; + +#define VLAN_TCI_CFI_CANONICAL_MAC 0 +#define VLAN_TCI_CFI_NON_CANONICAL_MAC 1 + +// +// The EFI_IP4_HEADER is hard to use because the source and +// destination address are defined as EFI_IPv4_ADDRESS, which +// is a structure. Two structures can't be compared or masked +// directly. This is why there is an internal representation. +// +typedef struct { + UINT8 HeadLen : 4; + UINT8 Ver : 4; + UINT8 Tos; + UINT16 TotalLen; + UINT16 Id; + UINT16 Fragment; + UINT8 Ttl; + UINT8 Protocol; + UINT16 Checksum; + IP4_ADDR Src; + IP4_ADDR Dst; +} IP4_HEAD; + + +// +// ICMP head definition. Each ICMP message is categorized as either an error +// message or query message. Two message types have their own head format. +// +typedef struct { + UINT8 Type; + UINT8 Code; + UINT16 Checksum; +} IP4_ICMP_HEAD; + +typedef struct { + IP4_ICMP_HEAD Head; + UINT32 Fourth; // 4th filed of the head, it depends on Type. + IP4_HEAD IpHead; +} IP4_ICMP_ERROR_HEAD; + +typedef struct { + IP4_ICMP_HEAD Head; + UINT16 Id; + UINT16 Seq; +} IP4_ICMP_QUERY_HEAD; + +typedef struct { + UINT8 Type; + UINT8 Code; + UINT16 Checksum; +} IP6_ICMP_HEAD; + +typedef struct { + IP6_ICMP_HEAD Head; + UINT32 Fourth; + EFI_IP6_HEADER IpHead; +} IP6_ICMP_ERROR_HEAD; + +typedef struct { + IP6_ICMP_HEAD Head; + UINT32 Fourth; +} IP6_ICMP_INFORMATION_HEAD; + +// +// UDP header definition +// +typedef struct { + UINT16 SrcPort; + UINT16 DstPort; + UINT16 Length; + UINT16 Checksum; +} EFI_UDP_HEADER; + +// +// TCP header definition +// +typedef struct { + TCP_PORTNO SrcPort; + TCP_PORTNO DstPort; + TCP_SEQNO Seq; + TCP_SEQNO Ack; + UINT8 Res : 4; + UINT8 HeadLen : 4; + UINT8 Flag; + UINT16 Wnd; + UINT16 Checksum; + UINT16 Urg; +} TCP_HEAD; + +#pragma pack() + +#define NET_MAC_EQUAL(pMac1, pMac2, Len) \ + (CompareMem ((pMac1), (pMac2), Len) == 0) + +#define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \ + (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len))) + +#define NTOHL(x) SwapBytes32 (x) + +#define HTONL(x) NTOHL(x) + +#define NTOHS(x) SwapBytes16 (x) + +#define HTONS(x) NTOHS(x) +#define NTOHLL(x) SwapBytes64 (x) +#define HTONLL(x) NTOHLL(x) +#define NTOHLLL(x) Ip6Swap128 (x) +#define HTONLLL(x) NTOHLLL(x) + +// +// Test the IP's attribute, All the IPs are in host byte order. +// +#define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000) +#define IP4_IS_UNSPECIFIED(Ip) ((Ip) == 0) +#define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF) +#define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask))) +#define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != (IP4_MASK_MAX + 1)) + +#define IP6_IS_MULTICAST(Ip6) (((Ip6)->Addr[0]) == 0xFF) + +// +// Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address. +// +#define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr)) +#define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp)))) +#define EFI_IP4_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0) + +#define EFI_IP6_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv6_ADDRESS)) == 0) + +#define IP4_COPY_ADDRESS(Dest, Src) (CopyMem ((Dest), (Src), sizeof (EFI_IPv4_ADDRESS))) +#define IP6_COPY_ADDRESS(Dest, Src) (CopyMem ((Dest), (Src), sizeof (EFI_IPv6_ADDRESS))) +#define IP6_COPY_LINK_ADDRESS(Mac1, Mac2) (CopyMem ((Mac1), (Mac2), sizeof (EFI_MAC_ADDRESS))) + +// +// The debug level definition. This value is also used as the +// syslog's severity level. Don't change it. +// +#define NETDEBUG_LEVEL_TRACE 5 +#define NETDEBUG_LEVEL_WARNING 4 +#define NETDEBUG_LEVEL_ERROR 3 + +// +// Network debug message is sent out as syslog packet. +// +#define NET_SYSLOG_FACILITY 16 // Syslog local facility local use +#define NET_SYSLOG_PACKET_LEN 512 +#define NET_SYSLOG_TX_TIMEOUT (500 * 1000 * 10) // 500ms +#define NET_DEBUG_MSG_LEN 470 // 512 - (ether+ip4+udp4 head length) + +// +// The debug output expects the ASCII format string, Use %a to print ASCII +// string, and %s to print UNICODE string. PrintArg must be enclosed in (). +// For example: NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name)); +// +#define NET_DEBUG_TRACE(Module, PrintArg) \ + NetDebugOutput ( \ + NETDEBUG_LEVEL_TRACE, \ + Module, \ + __FILE__, \ + __LINE__, \ + NetDebugASPrint PrintArg \ + ) + +#define NET_DEBUG_WARNING(Module, PrintArg) \ + NetDebugOutput ( \ + NETDEBUG_LEVEL_WARNING, \ + Module, \ + __FILE__, \ + __LINE__, \ + NetDebugASPrint PrintArg \ + ) + +#define NET_DEBUG_ERROR(Module, PrintArg) \ + NetDebugOutput ( \ + NETDEBUG_LEVEL_ERROR, \ + Module, \ + __FILE__, \ + __LINE__, \ + NetDebugASPrint PrintArg \ + ) + +/** + Allocate a buffer, then format the message to it. This is a + help function for the NET_DEBUG_XXX macros. The PrintArg of + these macros treats the variable length print parameters as a + single parameter, and pass it to the NetDebugASPrint. For + example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name)) + if extracted to: + + NetDebugOutput ( + NETDEBUG_LEVEL_TRACE, + "Tcp", + __FILE__, + __LINE__, + NetDebugASPrint ("State transit to %a\n", Name) + ) + + @param Format The ASCII format string. + @param ... The variable length parameter whose format is determined + by the Format string. + + @return The buffer containing the formatted message, + or NULL if memory allocation failed. + +**/ +CHAR8 * +EFIAPI +NetDebugASPrint ( + IN CHAR8 *Format, + ... + ); + +/** + Builds an UDP4 syslog packet and send it using SNP. + + 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 severity level of the message. + @param Module The Module that generates the log. + @param File The file that contains the log. + @param Line The exact line that contains the log. + @param Message The user message to log. + + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet + @retval EFI_SUCCESS The log is discard because that it is more verbose + than the mNetDebugLevelMax. Or, it has been sent out. +**/ +EFI_STATUS +EFIAPI +NetDebugOutput ( + IN UINT32 Level, + IN UINT8 *Module, + IN UINT8 *File, + IN UINT32 Line, + IN UINT8 *Message + ); + + +/** + Return the length of the mask. + + Return the length of the mask. Valid values are 0 to 32. + If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM. + NetMask is in the host byte order. + + @param[in] NetMask The netmask to get the length from. + + @return The length of the netmask, or IP4_MASK_NUM (33) if the mask is invalid. + +**/ +INTN +EFIAPI +NetGetMaskLength ( + IN IP4_ADDR NetMask + ); + +/** + 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. + If the address belong to class B, return IP4_ADDR_CLASSB. + The address of class C starts with 110. + If the address belong to class C, return IP4_ADDR_CLASSC. + The address of class D starts with 1110. + If the address belong to class D, return IP4_ADDR_CLASSD. + The address of class E starts with 1111. + If the address belong to class E, return IP4_ADDR_CLASSE. + + + @param[in] Addr The address to get the class from. + + @return IP address class, such as IP4_ADDR_CLASSA. + +**/ +INTN +EFIAPI +NetGetIpClass ( + IN IP4_ADDR Addr + ); + +/** + Check whether the IP is a valid unicast address according to + the netmask. + + 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), or a 32bit NetMask (all 0xFF) is used for special network environment (e.g. + PPP link). + + @param[in] Ip The IP to check against. + @param[in] NetMask The mask of the IP. + + @return TRUE if IP is a valid unicast address on the network, otherwise FALSE. + +**/ +BOOLEAN +EFIAPI +NetIp4IsUnicast ( + IN IP4_ADDR Ip, + IN IP4_ADDR NetMask + ); + +/** + Check whether the incoming IPv6 address is a valid unicast address. + + ASSERT if Ip6 is NULL. + + If the address is a multicast address has binary 0xFF at the start, it is not + a valid unicast address. If the address is unspecified ::, it is not a valid + unicast address to be assigned to any node. If the address is loopback address + ::1, it is also not a valid unicast address to be assigned to any physical + interface. + + @param[in] Ip6 The IPv6 address to check against. + + @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE. + +**/ +BOOLEAN +EFIAPI +NetIp6IsValidUnicast ( + IN EFI_IPv6_ADDRESS *Ip6 + ); + + +/** + Check whether the incoming Ipv6 address is the unspecified address or not. + + ASSERT if Ip6 is NULL. + + @param[in] Ip6 - Ip6 address, in network order. + + @retval TRUE - Yes, incoming Ipv6 address is the unspecified address. + @retval FALSE - The incoming Ipv6 address is not the unspecified address + +**/ +BOOLEAN +EFIAPI +NetIp6IsUnspecifiedAddr ( + IN EFI_IPv6_ADDRESS *Ip6 + ); + +/** + Check whether the incoming Ipv6 address is a link-local address. + + ASSERT if Ip6 is NULL. + + @param[in] Ip6 - Ip6 address, in network order. + + @retval TRUE - The incoming Ipv6 address is a link-local address. + @retval FALSE - The incoming Ipv6 address is not a link-local address. + +**/ +BOOLEAN +EFIAPI +NetIp6IsLinkLocalAddr ( + IN EFI_IPv6_ADDRESS *Ip6 + ); + +/** + Check whether the Ipv6 address1 and address2 are on the connected network. + + ASSERT if Ip1 or Ip2 is NULL. + ASSERT if PrefixLength exceeds or equals to IP6_PREFIX_MAX. + + @param[in] Ip1 - Ip6 address1, in network order. + @param[in] Ip2 - Ip6 address2, in network order. + @param[in] PrefixLength - The prefix length of the checking net. + + @retval TRUE - Yes, the Ipv6 address1 and address2 are connected. + @retval FALSE - No the Ipv6 address1 and address2 are not connected. + +**/ +BOOLEAN +EFIAPI +NetIp6IsNetEqual ( + EFI_IPv6_ADDRESS *Ip1, + EFI_IPv6_ADDRESS *Ip2, + UINT8 PrefixLength + ); + +/** + Switches the endianess of an IPv6 address. + + ASSERT if Ip6 is NULL. + + This function swaps the bytes in a 128-bit IPv6 address to switch the value + from little endian to big endian or vice versa. The byte swapped value is + returned. + + @param Ip6 Points to an IPv6 address. + + @return The byte swapped IPv6 address. + +**/ +EFI_IPv6_ADDRESS * +EFIAPI +Ip6Swap128 ( + EFI_IPv6_ADDRESS *Ip6 + ); + +extern IP4_ADDR gIp4AllMasks[IP4_MASK_NUM]; + + +extern EFI_IPv4_ADDRESS mZeroIp4Addr; + +#define NET_IS_DIGIT(Ch) (('0' <= (Ch)) && ((Ch) <= '9')) +#define NET_IS_HEX(Ch) ((('0' <= (Ch)) && ((Ch) <= '9')) || (('A' <= (Ch)) && ((Ch) <= 'F')) || (('a' <= (Ch)) && ((Ch) <= 'f'))) +#define NET_ROUNDUP(size, unit) (((size) + (unit) - 1) & (~((unit) - 1))) +#define NET_IS_LOWER_CASE_CHAR(Ch) (('a' <= (Ch)) && ((Ch) <= 'z')) +#define NET_IS_UPPER_CASE_CHAR(Ch) (('A' <= (Ch)) && ((Ch) <= 'Z')) + +#define TICKS_PER_MS 10000U +#define TICKS_PER_SECOND 10000000U + +#define NET_RANDOM(Seed) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL) + +/** + Extract a UINT32 from a byte stream. + + ASSERT if Buf is NULL. + + This function copies a UINT32 from a byte stream, and then converts it from Network + byte order to host byte order. Use this function to avoid alignment error. + + @param[in] Buf The buffer to extract the UINT32. + + @return The UINT32 extracted. + +**/ +UINT32 +EFIAPI +NetGetUint32 ( + IN UINT8 *Buf + ); + +/** + Puts a UINT32 into the byte stream in network byte order. + + ASSERT if Buf is NULL. + + Converts a UINT32 from host byte order to network byte order, then copies it to the + byte stream. + + @param[in, out] Buf The buffer in which to put the UINT32. + @param[in] Data The data to be converted and put into the byte stream. + +**/ +VOID +EFIAPI +NetPutUint32 ( + IN OUT UINT8 *Buf, + IN UINT32 Data + ); + +/** + Initialize a random seed using current time and monotonic count. + + 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. + +**/ +UINT32 +EFIAPI +NetRandomInitSeed ( + VOID + ); + + +#define NET_LIST_USER_STRUCT(Entry, Type, Field) \ + BASE_CR(Entry, Type, Field) + +#define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \ + CR(Entry, Type, Field, Sig) + +// +// Iterate through the double linked list. It is NOT delete safe +// +#define NET_LIST_FOR_EACH(Entry, ListHead) \ + for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink) + +// +// Iterate through the double linked list. This is delete-safe. +// Don't touch NextEntry. Also, don't use this macro if list +// entries other than the Entry may be deleted when processing +// the current Entry. +// +#define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \ + for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \ + Entry != (ListHead); \ + Entry = NextEntry, NextEntry = Entry->ForwardLink \ + ) + +// +// Make sure the list isn't empty before getting the first/last record. +// +#define NET_LIST_HEAD(ListHead, Type, Field) \ + NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field) + +#define NET_LIST_TAIL(ListHead, Type, Field) \ + NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field) + + +/** + Remove the first node entry on the list, and return the removed node entry. + + Removes the first node entry from a doubly linked list. It is up to the caller of + this function to release the memory used by the first node, if that is required. On + exit, the removed node is returned. + + If Head is NULL, then ASSERT(). + If Head was not initialized, then ASSERT(). + If PcdMaximumLinkedListLength is not zero, and the number of nodes in the + linked list including the head node is greater than or equal to PcdMaximumLinkedListLength, + then ASSERT(). + + @param[in, out] Head The list header. + + @return The first node entry that is removed from the list, NULL if the list is empty. + +**/ +LIST_ENTRY * +EFIAPI +NetListRemoveHead ( + IN OUT LIST_ENTRY *Head + ); + +/** + Remove the last node entry on the list and return the removed node entry. + + Removes the last node entry from a doubly linked list. It is up to the caller of + this function to release the memory used by the first node, if that is required. On + exit, the removed node is returned. + + If Head is NULL, then ASSERT(). + If Head was not initialized, then ASSERT(). + If PcdMaximumLinkedListLength is not zero, and the number of nodes in the + linked list including the head node is greater than or equal to PcdMaximumLinkedListLength, + then ASSERT(). + + @param[in, out] Head The list head. + + @return The last node entry that is removed from the list, NULL if the list is empty. + +**/ +LIST_ENTRY * +EFIAPI +NetListRemoveTail ( + IN OUT LIST_ENTRY *Head + ); + +/** + Insert a new node entry after a designated node entry of a doubly linked list. + + ASSERT if PrevEntry or NewEntry is NULL. + + Inserts a new node entry designated by NewEntry after the node entry designated by PrevEntry + of the doubly linked list. + + @param[in, out] PrevEntry The entry after which to insert. + @param[in, out] NewEntry The new entry to insert. + +**/ +VOID +EFIAPI +NetListInsertAfter ( + IN OUT LIST_ENTRY *PrevEntry, + IN OUT LIST_ENTRY *NewEntry + ); + +/** + Insert a new node entry before a designated node entry of a doubly linked list. + + ASSERT if PostEntry or NewEntry is NULL. + + Inserts a new node entry designated by NewEntry before the node entry designated by PostEntry + of the doubly linked list. + + @param[in, out] PostEntry The entry to insert before. + @param[in, out] NewEntry The new entry to insert. + +**/ +VOID +EFIAPI +NetListInsertBefore ( + IN OUT LIST_ENTRY *PostEntry, + IN OUT LIST_ENTRY *NewEntry + ); + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +typedef +EFI_STATUS +(EFIAPI *NET_DESTROY_LINK_LIST_CALLBACK) ( + IN LIST_ENTRY *Entry, + IN VOID *Context OPTIONAL + ); + +/** + Safe destroy nodes in a linked list, and return the length of the list after all possible operations finished. + + Destroy network children 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 + ); + +/** + 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 + ); + +// +// Object container: EFI network stack spec defines various kinds of +// tokens. The drivers can share code to manage those objects. +// +typedef struct { + LIST_ENTRY Link; + VOID *Key; + VOID *Value; +} NET_MAP_ITEM; + +typedef struct { + LIST_ENTRY Used; + LIST_ENTRY Recycled; + UINTN Count; +} NET_MAP; + +#define NET_MAP_INCREAMENT 64 + +/** + Initialize the netmap. Netmap is a reposity to keep the pairs. + + Initialize the forward and backward links of two head nodes donated by Map->Used + and Map->Recycled of two doubly linked lists. + Initializes the count of the pairs in the netmap to zero. + + If Map is NULL, then ASSERT(). + If the address of Map->Used is NULL, then ASSERT(). + If the address of Map->Recycled is NULl, then ASSERT(). + + @param[in, out] Map The netmap to initialize. + +**/ +VOID +EFIAPI +NetMapInit ( + IN OUT NET_MAP *Map + ); + +/** + To clean up the netmap, that is, release allocated memories. + + Removes all nodes of the Used doubly linked list and frees memory of all related netmap items. + Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items. + The number of the pairs in the netmap is set to zero. + + If Map is NULL, then ASSERT(). + + @param[in, out] Map The netmap to clean up. + +**/ +VOID +EFIAPI +NetMapClean ( + IN OUT NET_MAP *Map + ); + +/** + Test whether the netmap is empty and return true if it is. + + If the number of the pairs in the netmap is zero, return TRUE. + + If Map is NULL, then ASSERT(). + + @param[in] Map The net map to test. + + @return TRUE if the netmap is empty, otherwise FALSE. + +**/ +BOOLEAN +EFIAPI +NetMapIsEmpty ( + IN NET_MAP *Map + ); + +/** + Return the number of the pairs in the netmap. + + If Map is NULL, then ASSERT(). + + @param[in] Map The netmap to get the entry number. + + @return The entry number in the netmap. + +**/ +UINTN +EFIAPI +NetMapGetCount ( + IN NET_MAP *Map + ); + +/** + Allocate an item to save the pair to the head of the netmap. + + Allocate an item to save the pair and add corresponding node entry + to the beginning of the Used doubly linked list. The number of the + pairs in the netmap increase by 1. + + If Map is NULL, then ASSERT(). + If Key is NULL, then ASSERT(). + + @param[in, out] Map The netmap to insert into. + @param[in] Key The user's key. + @param[in] Value The user's value for the key. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item. + @retval EFI_SUCCESS The item is inserted to the head. + +**/ +EFI_STATUS +EFIAPI +NetMapInsertHead ( + IN OUT NET_MAP *Map, + IN VOID *Key, + IN VOID *Value OPTIONAL + ); + +/** + Allocate an item to save the pair to the tail of the netmap. + + Allocate an item to save the pair and add corresponding node entry + to the tail of the Used doubly linked list. The number of the + pairs in the netmap increase by 1. + + If Map is NULL, then ASSERT(). + If Key is NULL, then ASSERT(). + + @param[in, out] Map The netmap to insert into. + @param[in] Key The user's key. + @param[in] Value The user's value for the key. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item. + @retval EFI_SUCCESS The item is inserted to the tail. + +**/ +EFI_STATUS +EFIAPI +NetMapInsertTail ( + IN OUT NET_MAP *Map, + IN VOID *Key, + IN VOID *Value OPTIONAL + ); + +/** + Finds the key in the netmap and returns the point to the item containing the Key. + + Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every + item with the key to search. It returns the point to the item contains the Key if found. + + If Map is NULL, then ASSERT(). + If Key is NULL, then ASSERT(). + + @param[in] Map The netmap to search within. + @param[in] Key The key to search. + + @return The point to the item contains the Key, or NULL if Key isn't in the map. + +**/ +NET_MAP_ITEM * +EFIAPI +NetMapFindKey ( + IN NET_MAP *Map, + IN VOID *Key + ); + +/** + Remove the node entry of the item from the netmap and return the key of the removed item. + + Remove the node entry of the item from the Used doubly linked list of the netmap. + The number of the pairs in the netmap decrease by 1. Then add the node + entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL, + Value will point to the value of the item. It returns the key of the removed item. + + If Map is NULL, then ASSERT(). + If Item is NULL, then ASSERT(). + if item in not in the netmap, then ASSERT(). + + @param[in, out] Map The netmap to remove the item from. + @param[in, out] Item The item to remove. + @param[out] Value The variable to receive the value if not NULL. + + @return The key of the removed item. + +**/ +VOID * +EFIAPI +NetMapRemoveItem ( + IN OUT NET_MAP *Map, + IN OUT NET_MAP_ITEM *Item, + OUT VOID **Value OPTIONAL + ); + +/** + Remove the first node entry on the netmap and return the key of the removed item. + + Remove the first node entry from the Used doubly linked list of the netmap. + The number of the pairs in the netmap decrease by 1. Then add the node + entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL, + parameter Value will point to the value of the item. It returns the key of the removed item. + + If Map is NULL, then ASSERT(). + If the Used doubly linked list is empty, then ASSERT(). + + @param[in, out] Map The netmap to remove the head from. + @param[out] Value The variable to receive the value if not NULL. + + @return The key of the item removed. + +**/ +VOID * +EFIAPI +NetMapRemoveHead ( + IN OUT NET_MAP *Map, + OUT VOID **Value OPTIONAL + ); + +/** + Remove the last node entry on the netmap and return the key of the removed item. + + Remove the last node entry from the Used doubly linked list of the netmap. + The number of the pairs in the netmap decrease by 1. Then add the node + entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL, + parameter Value will point to the value of the item. It returns the key of the removed item. + + If Map is NULL, then ASSERT(). + If the Used doubly linked list is empty, then ASSERT(). + + @param[in, out] Map The netmap to remove the tail from. + @param[out] Value The variable to receive the value if not NULL. + + @return The key of the item removed. + +**/ +VOID * +EFIAPI +NetMapRemoveTail ( + IN OUT NET_MAP *Map, + OUT VOID **Value OPTIONAL + ); + +typedef +EFI_STATUS +(EFIAPI *NET_MAP_CALLBACK) ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg + ); + +/** + Iterate through the netmap and call CallBack for each item. + + 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. + + If Map is NULL, then ASSERT(). + If CallBack is NULL, then ASSERT(). + + @param[in] Map The Map to iterate through. + @param[in] CallBack The callback function to call for each item. + @param[in] Arg The opaque parameter to the callback. + + @retval EFI_SUCCESS There is no item in the netmap, or CallBack for each item + returns EFI_SUCCESS. + @retval Others It returns the CallBack's last return value. + +**/ +EFI_STATUS +EFIAPI +NetMapIterate ( + IN NET_MAP *Map, + IN NET_MAP_CALLBACK CallBack, + IN VOID *Arg OPTIONAL + ); + + +// +// Helper functions to implement driver binding and service binding protocols. +// +/** + Create a child of the service that is identified by ServiceBindingGuid. + + Get the ServiceBinding Protocol first, then use it to create a child. + + If ServiceBindingGuid is NULL, then ASSERT(). + If ChildHandle is NULL, then ASSERT(). + + @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, out] ChildHandle The handle to receive the created child. + + @retval EFI_SUCCESS The child was successfully created. + @retval Others Failed to create the child. + +**/ +EFI_STATUS +EFIAPI +NetLibCreateServiceChild ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE Image, + IN EFI_GUID *ServiceBindingGuid, + IN OUT EFI_HANDLE *ChildHandle + ); + +/** + Destroy a child of the service that is identified by ServiceBindingGuid. + + Get the ServiceBinding Protocol first, then use it to destroy a child. + + If ServiceBindingGuid is NULL, then ASSERT(). + + @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 destroy. + + @retval EFI_SUCCESS The child was destroyed. + @retval Others Failed to destroy the child. + +**/ +EFI_STATUS +EFIAPI +NetLibDestroyServiceChild ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE Image, + IN EFI_GUID *ServiceBindingGuid, + IN EFI_HANDLE ChildHandle + ); + +/** + Get handle with Simple Network Protocol installed on it. + + There should be MNP Service Binding Protocol installed on the input ServiceHandle. + If Simple Network Protocol is already installed on the ServiceHandle, the + ServiceHandle will be returned. If SNP is not installed on the ServiceHandle, + try to find its parent handle with SNP installed. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + @param[out] Snp The pointer to store the address of the SNP instance. + This is an optional parameter that may be NULL. + + @return The SNP handle, or NULL if not found. + +**/ +EFI_HANDLE +EFIAPI +NetLibGetSnpHandle ( + IN EFI_HANDLE ServiceHandle, + OUT EFI_SIMPLE_NETWORK_PROTOCOL **Snp OPTIONAL + ); + +/** + Retrieve VLAN ID of a VLAN device handle. + + Search VLAN device path node in Device Path of specified ServiceHandle and + return its VLAN ID. If no VLAN device path node found, then this ServiceHandle + is not a VLAN device handle, and 0 will be returned. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + + @return VLAN ID of the device handle, or 0 if not a VLAN device. + +**/ +UINT16 +EFIAPI +NetLibGetVlanId ( + IN EFI_HANDLE ServiceHandle + ); + +/** + Find VLAN device handle with specified VLAN ID. + + The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle. + This function will append VLAN device path node to the parent device path, + and then use LocateDevicePath() to find the correct VLAN device handle. + + @param[in] ControllerHandle The handle where network service binding protocols are + installed on. + @param[in] VlanId The configured VLAN ID for the VLAN device. + + @return The VLAN device handle, or NULL if not found. + +**/ +EFI_HANDLE +EFIAPI +NetLibGetVlanHandle ( + IN EFI_HANDLE ControllerHandle, + IN UINT16 VlanId + ); + +/** + Get MAC address associated with the network service handle. + + If MacAddress is NULL, then ASSERT(). + If AddressSize is NULL, then ASSERT(). + + There should be MNP Service Binding Protocol installed on the input ServiceHandle. + If SNP is installed on the ServiceHandle or its parent handle, MAC address will + be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + @param[out] MacAddress The pointer to store the returned MAC address. + @param[out] AddressSize The length of returned MAC address. + + @retval EFI_SUCCESS MAC address was returned successfully. + @retval Others Failed to get SNP mode data. + +**/ +EFI_STATUS +EFIAPI +NetLibGetMacAddress ( + IN EFI_HANDLE ServiceHandle, + OUT EFI_MAC_ADDRESS *MacAddress, + OUT UINTN *AddressSize + ); + +/** + Convert MAC address of the NIC associated with specified Service Binding Handle + to a unicode string. Callers are responsible for freeing the string storage. + + If MacString is NULL, then ASSERT(). + + Locate simple network protocol associated with the Service Binding Handle and + get the mac address from SNP. Then convert the mac address into a unicode + string. It takes 2 unicode characters to represent a 1 byte binary buffer. + Plus one unicode character for the null-terminator. + + @param[in] ServiceHandle The handle where network service binding protocol is + installed. + @param[in] ImageHandle The image handle used to act as the agent handle to + 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. + + @retval EFI_SUCCESS Converted the mac address a unicode string successfully. + @retval EFI_OUT_OF_RESOURCES There are not enough memory resources. + @retval Others Failed to open the simple network protocol. + +**/ +EFI_STATUS +EFIAPI +NetLibGetMacString ( + IN EFI_HANDLE ServiceHandle, + IN EFI_HANDLE ImageHandle, OPTIONAL + OUT CHAR16 **MacString + ); + +/** + Detect media status for specified network device. + + If MediaPresent is NULL, then ASSERT(). + + The underlying UNDI driver may or may not support reporting media status from + GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine + will try to invoke Snp->GetStatus() to get the media status. If media is already + present, it returns directly. If media is not present, it will stop SNP and then + restart SNP to get the latest media status. This provides an opportunity to get + the correct media status for old UNDI driver, which doesn't support reporting + media status from GET_STATUS command. + Note: there are two limitations for the current algorithm: + 1) For UNDI with this capability, when the cable is not attached, there will + be an redundant Stop/Start() process. + 2) for UNDI without this capability, in case that network cable is attached when + Snp->Initialize() is invoked while network cable is unattached later, + NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer + apps to wait for timeout time. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed. + @param[out] MediaPresent The pointer to store the media status. + + @retval EFI_SUCCESS Media detection success. + @retval EFI_INVALID_PARAMETER ServiceHandle is not a valid network device handle. + @retval EFI_UNSUPPORTED The network device does not support media detection. + @retval EFI_DEVICE_ERROR SNP is in an unknown state. + +**/ +EFI_STATUS +EFIAPI +NetLibDetectMedia ( + IN EFI_HANDLE ServiceHandle, + OUT BOOLEAN *MediaPresent + ); + +/** + Detect media state for a network device. This routine will wait for a period of time at + a specified checking interval when a certain network is under connecting until connection + process finishes or timeout. If Aip protocol is supported by low layer drivers, three kinds + of media states can be detected: EFI_SUCCESS, EFI_NOT_READY and EFI_NO_MEDIA, represents + connected state, connecting state and no media state respectively. When function detects + the current state is EFI_NOT_READY, it will loop to wait for next time's check until state + turns to be EFI_SUCCESS or EFI_NO_MEDIA. If Aip protocol is not supported, function will + call NetLibDetectMedia() and return state directly. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + @param[in] Timeout The maximum number of 100ns units to wait when network + is connecting. Zero value means detect once and return + immediately. + @param[out] MediaState The pointer to the detected media state. + + @retval EFI_SUCCESS Media detection success. + @retval EFI_INVALID_PARAMETER ServiceHandle is not a valid network device handle or + MediaState pointer is NULL. + @retval EFI_DEVICE_ERROR A device error occurred. + @retval EFI_TIMEOUT Network is connecting but timeout. + +**/ +EFI_STATUS +EFIAPI +NetLibDetectMediaWaitTimeout ( + IN EFI_HANDLE ServiceHandle, + IN UINT64 Timeout, + OUT EFI_STATUS *MediaState + ); + + +/** + Create an IPv4 device path node. + + If Node is NULL, then ASSERT(). + + 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 information from parameters to make up the whole IPv4 device path node. + + @param[in, out] Node The pointer to the IPv4 device path node. + @param[in] Controller The controller handle. + @param[in] LocalIp The local IPv4 address. + @param[in] LocalPort The local port. + @param[in] RemoteIp The remote IPv4 address. + @param[in] RemotePort The remote port. + @param[in] Protocol The protocol type in the IP header. + @param[in] UseDefaultAddress Whether this instance is using default address or not. + +**/ +VOID +EFIAPI +NetLibCreateIPv4DPathNode ( + IN OUT IPv4_DEVICE_PATH *Node, + IN EFI_HANDLE Controller, + IN IP4_ADDR LocalIp, + IN UINT16 LocalPort, + IN IP4_ADDR RemoteIp, + IN UINT16 RemotePort, + IN UINT16 Protocol, + IN BOOLEAN UseDefaultAddress + ); + +/** + Create an IPv6 device path node. + + If Node is NULL, then ASSERT(). + If LocalIp is NULL, then ASSERT(). + If RemoteIp is NULL, then ASSERT(). + + The header type of IPv6 device path node is MESSAGING_DEVICE_PATH. + The header subtype of IPv6 device path node is MSG_IPv6_DP. + The length of the IPv6 device path node in bytes is 43. + Get other information from parameters to make up the whole IPv6 device path node. + + @param[in, out] Node The pointer to the IPv6 device path node. + @param[in] Controller The controller handle. + @param[in] LocalIp The local IPv6 address. + @param[in] LocalPort The local port. + @param[in] RemoteIp The remote IPv6 address. + @param[in] RemotePort The remote port. + @param[in] Protocol The protocol type in the IP header. + +**/ +VOID +EFIAPI +NetLibCreateIPv6DPathNode ( + IN OUT IPv6_DEVICE_PATH *Node, + IN EFI_HANDLE Controller, + IN EFI_IPv6_ADDRESS *LocalIp, + IN UINT16 LocalPort, + IN EFI_IPv6_ADDRESS *RemoteIp, + IN UINT16 RemotePort, + IN UINT16 Protocol + ); + + +/** + Find the UNDI/SNP handle from controller and protocol GUID. + + If ProtocolGuid is NULL, then ASSERT(). + + For example, IP will open an MNP child to transmit/receive + packets. When MNP is stopped, IP should also be stopped. IP + needs to find its own private data that is related the IP's + service binding instance that is installed on the UNDI/SNP handle. + The controller is then either an MNP or an ARP child handle. Note that + IP opens these handles using BY_DRIVER. Use that information to get the + UNDI/SNP handle. + + @param[in] Controller The protocol handle to check. + @param[in] ProtocolGuid The protocol that is related with the handle. + + @return The UNDI/SNP handle or NULL for errors. + +**/ +EFI_HANDLE +EFIAPI +NetLibGetNicHandle ( + IN EFI_HANDLE Controller, + IN EFI_GUID *ProtocolGuid + ); + +/** + This is the default unload handle for all the network drivers. + + Disconnect the driver specified by ImageHandle from all the devices in the handle database. + Uninstall all the protocols installed in the driver entry point. + + @param[in] ImageHandle The drivers' driver image. + + @retval EFI_SUCCESS The image is unloaded. + @retval Others Failed to unload the image. + +**/ +EFI_STATUS +EFIAPI +NetLibDefaultUnload ( + IN EFI_HANDLE ImageHandle + ); + +/** + Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS. + + @param[in] String The pointer to the Ascii string. + @param[out] Ip4Address The pointer to the converted IPv4 address. + + @retval EFI_SUCCESS Converted to an IPv4 address successfully. + @retval EFI_INVALID_PARAMETER The string is malformatted, or Ip4Address is NULL. + +**/ +EFI_STATUS +EFIAPI +NetLibAsciiStrToIp4 ( + IN CONST CHAR8 *String, + OUT EFI_IPv4_ADDRESS *Ip4Address + ); + +/** + Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of 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 Converted to an IPv6 address successfully. + @retval EFI_INVALID_PARAMETER The string is malformatted, or Ip6Address is NULL. + +**/ +EFI_STATUS +EFIAPI +NetLibAsciiStrToIp6 ( + IN CONST CHAR8 *String, + OUT EFI_IPv6_ADDRESS *Ip6Address + ); + +/** + Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS. + + @param[in] String The pointer to the Ascii string. + @param[out] Ip4Address The pointer to the converted IPv4 address. + + @retval EFI_SUCCESS Converted to an IPv4 address successfully. + @retval EFI_INVALID_PARAMETER The string is mal-formatted or Ip4Address is NULL. + +**/ +EFI_STATUS +EFIAPI +NetLibStrToIp4 ( + IN CONST CHAR16 *String, + OUT EFI_IPv4_ADDRESS *Ip4Address + ); + +/** + Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of + 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 Converted to an IPv6 address successfully. + @retval EFI_INVALID_PARAMETER The string is malformatted or Ip6Address is NULL. + +**/ +EFI_STATUS +EFIAPI +NetLibStrToIp6 ( + IN CONST CHAR16 *String, + OUT EFI_IPv6_ADDRESS *Ip6Address + ); + +/** + Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length. + 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. + @param[out] Ip6Address The pointer to the converted IPv6 address. + @param[out] PrefixLength The pointer to the converted prefix length. + + @retval EFI_SUCCESS Converted to an IPv6 address successfully. + @retval EFI_INVALID_PARAMETER The string is malformatted, or Ip6Address is NULL. + +**/ +EFI_STATUS +EFIAPI +NetLibStrToIp6andPrefix ( + IN CONST CHAR16 *String, + OUT EFI_IPv6_ADDRESS *Ip6Address, + OUT UINT8 *PrefixLength + ); + +/** + + 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 + ); + +// +// Various signatures +// +#define NET_BUF_SIGNATURE SIGNATURE_32 ('n', 'b', 'u', 'f') +#define NET_VECTOR_SIGNATURE SIGNATURE_32 ('n', 'v', 'e', 'c') +#define NET_QUE_SIGNATURE SIGNATURE_32 ('n', 'b', 'q', 'u') + + +#define NET_PROTO_DATA 64 // Opaque buffer for protocols +#define NET_BUF_HEAD 1 // Trim or allocate space from head +#define NET_BUF_TAIL 0 // Trim or allocate space from tail +#define NET_VECTOR_OWN_FIRST 0x01 // We allocated the 1st block in the vector + +#define NET_CHECK_SIGNATURE(PData, SIGNATURE) \ + ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE))) + +// +// Single memory block in the vector. +// +typedef struct { + UINT32 Len; // The block's length + UINT8 *Bulk; // The block's Data +} NET_BLOCK; + +typedef VOID (EFIAPI *NET_VECTOR_EXT_FREE) (VOID *Arg); + +// +//NET_VECTOR contains several blocks to hold all packet's +//fragments and other house-keeping stuff for sharing. It +//doesn't specify the where actual packet fragment begins. +// +typedef struct { + UINT32 Signature; + INTN RefCnt; // Reference count to share NET_VECTOR. + NET_VECTOR_EXT_FREE Free; // external function to free NET_VECTOR + VOID *Arg; // opaque argument to Free + UINT32 Flag; // Flags, NET_VECTOR_OWN_FIRST + UINT32 Len; // Total length of the associated BLOCKs + + UINT32 BlockNum; + NET_BLOCK Block[1]; +} NET_VECTOR; + +// +//NET_BLOCK_OP operates on the NET_BLOCK. It specifies +//where the actual fragment begins and ends +// +typedef struct { + UINT8 *BlockHead; // Block's head, or the smallest valid Head + UINT8 *BlockTail; // Block's tail. BlockTail-BlockHead=block length + UINT8 *Head; // 1st byte of the data in the block + UINT8 *Tail; // Tail of the data in the block, Tail-Head=Size + UINT32 Size; // The size of the data +} NET_BLOCK_OP; + +typedef union { + IP4_HEAD *Ip4; + EFI_IP6_HEADER *Ip6; +} NET_IP_HEAD; + +// +//NET_BUF is the buffer manage structure used by the +//network stack. Every network packet may be fragmented. The Vector points to +//memory blocks used by each fragment, and BlockOp +//specifies where each fragment begins and ends. +// +//It also contains an opaque area for the protocol to store +//per-packet information. Protocol must be careful not +//to overwrite the members after that. +// +typedef struct { + UINT32 Signature; + INTN RefCnt; + LIST_ENTRY List; // The List this NET_BUF is on + + NET_IP_HEAD Ip; // Network layer header, for fast access + TCP_HEAD *Tcp; // Transport layer header, for fast access + EFI_UDP_HEADER *Udp; // User Datagram Protocol header + UINT8 ProtoData [NET_PROTO_DATA]; //Protocol specific data + + NET_VECTOR *Vector; // The vector containing the packet + + UINT32 BlockOpNum; // Total number of BlockOp in the buffer + UINT32 TotalSize; // Total size of the actual packet + NET_BLOCK_OP BlockOp[1]; // Specify the position of actual packet +} NET_BUF; + +// +//A queue of NET_BUFs. It is a thin extension of +//NET_BUF functions. +// +typedef struct { + UINT32 Signature; + INTN RefCnt; + LIST_ENTRY List; // The List this buffer queue is on + + LIST_ENTRY BufList; // list of queued buffers + UINT32 BufSize; // total length of DATA in the buffers + UINT32 BufNum; // total number of buffers on the chain +} NET_BUF_QUEUE; + +// +// Pseudo header for TCP and UDP checksum +// +#pragma pack(1) +typedef struct { + IP4_ADDR SrcIp; + IP4_ADDR DstIp; + UINT8 Reserved; + UINT8 Protocol; + UINT16 Len; +} NET_PSEUDO_HDR; + +typedef struct { + EFI_IPv6_ADDRESS SrcIp; + EFI_IPv6_ADDRESS DstIp; + UINT32 Len; + UINT32 Reserved:24; + UINT32 NextHeader:8; +} NET_IP6_PSEUDO_HDR; +#pragma pack() + +// +// The fragment entry table used in network interfaces. This is +// the same as NET_BLOCK now. Use two different to distinguish +// the two in case that NET_BLOCK be enhanced later. +// +typedef struct { + UINT32 Len; + UINT8 *Bulk; +} NET_FRAGMENT; + +#define NET_GET_REF(PData) ((PData)->RefCnt++) +#define NET_PUT_REF(PData) ((PData)->RefCnt--) +#define NETBUF_FROM_PROTODATA(Info) BASE_CR((Info), NET_BUF, ProtoData) + +#define NET_BUF_SHARED(Buf) \ + (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1)) + +#define NET_VECTOR_SIZE(BlockNum) \ + (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK)) + +#define NET_BUF_SIZE(BlockOpNum) \ + (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP)) + +#define NET_HEADSPACE(BlockOp) \ + ((UINTN)((BlockOp)->Head) - (UINTN)((BlockOp)->BlockHead)) + +#define NET_TAILSPACE(BlockOp) \ + ((UINTN)((BlockOp)->BlockTail) - (UINTN)((BlockOp)->Tail)) + +/** + Allocate a single block NET_BUF. Upon allocation, all the + free space is in the tail room. + + @param[in] Len The length of the block. + + @return The pointer to the allocated NET_BUF, or NULL if the + allocation failed due to resource limitations. + +**/ +NET_BUF * +EFIAPI +NetbufAlloc ( + IN UINT32 Len + ); + +/** + Free the net buffer and its associated NET_VECTOR. + + Decrease the reference count of the net buffer by one. Free the associated net + vector and itself if the reference count of the net buffer is decreased to 0. + The net vector free operation decreases the reference count of the net + vector by one, and performs the resource free operation when the reference count + of the net vector is 0. + + @param[in] Nbuf The pointer to the NET_BUF to be freed. + +**/ +VOID +EFIAPI +NetbufFree ( + IN NET_BUF *Nbuf + ); + +/** + Get the index of NET_BLOCK_OP that contains the byte at Offset in the net + buffer. + + For example, this function can be used to retrieve the IP header in the packet. It + also can be used to get the fragment that contains the byte used + mainly by the library implementation itself. + + @param[in] Nbuf The pointer to the net buffer. + @param[in] Offset The offset of the byte. + @param[out] Index Index of the NET_BLOCK_OP that contains the byte at + Offset. + + @return The pointer to the Offset'th byte of data in the net buffer, or NULL + if there is no such data in the net buffer. + +**/ +UINT8 * +EFIAPI +NetbufGetByte ( + IN NET_BUF *Nbuf, + IN UINT32 Offset, + OUT UINT32 *Index OPTIONAL + ); + +/** + Create a copy of the net buffer that shares the associated net vector. + + The reference count of the newly created net buffer is set to 1. The reference + count of the associated net vector is increased by one. + + @param[in] Nbuf The pointer to the net buffer to be cloned. + + @return The pointer to the cloned net buffer, or NULL if the + allocation failed due to resource limitations. + +**/ +NET_BUF * +EFIAPI +NetbufClone ( + IN NET_BUF *Nbuf + ); + +/** + Create a duplicated copy of the net buffer with data copied and HeadSpace + bytes of head space reserved. + + The duplicated net buffer will allocate its own memory to hold the data of the + source net buffer. + + @param[in] Nbuf The pointer to the net buffer to be duplicated from. + @param[in, out] Duplicate The pointer to the net buffer to duplicate to. If + NULL, a new net buffer is allocated. + @param[in] HeadSpace The length of the head space to reserve. + + @return The pointer to the duplicated net buffer, or NULL if + the allocation failed due to resource limitations. + +**/ +NET_BUF * +EFIAPI +NetbufDuplicate ( + IN NET_BUF *Nbuf, + IN OUT NET_BUF *Duplicate OPTIONAL, + IN UINT32 HeadSpace + ); + +/** + Create a NET_BUF structure which contains Len byte data of Nbuf starting from + Offset. + + A new NET_BUF structure will be created but the associated data in NET_VECTOR + is shared. This function exists to perform IP packet fragmentation. + + @param[in] Nbuf The pointer to the net buffer to be extracted. + @param[in] Offset Starting point of the data to be included in the new + net buffer. + @param[in] Len The bytes of data to be included in the new net buffer. + @param[in] HeadSpace The bytes of the head space to reserve for the protocol header. + + @return The pointer to the cloned net buffer, or NULL if the + allocation failed due to resource limitations. + +**/ +NET_BUF * +EFIAPI +NetbufGetFragment ( + IN NET_BUF *Nbuf, + IN UINT32 Offset, + IN UINT32 Len, + IN UINT32 HeadSpace + ); + +/** + Reserve some space in the header room of the net buffer. + + Upon allocation, all the space is in the tail room of the buffer. Call this + function to move space to the header room. This function is quite limited + in that it can only reserve space from the first block of an empty NET_BUF not + built from the external. However, it should be enough for the network stack. + + @param[in, out] Nbuf The pointer to the net buffer. + @param[in] Len The length of buffer to be reserved from the header. + +**/ +VOID +EFIAPI +NetbufReserve ( + IN OUT NET_BUF *Nbuf, + IN UINT32 Len + ); + +/** + Allocate Len bytes of space from the header or tail of the buffer. + + @param[in, out] Nbuf The pointer to the net buffer. + @param[in] Len The length of the buffer to be allocated. + @param[in] FromHead The flag to indicate whether to reserve the data + from head (TRUE) or tail (FALSE). + + @return The pointer to the first byte of the allocated buffer, + or NULL, if there is no sufficient space. + +**/ +UINT8* +EFIAPI +NetbufAllocSpace ( + IN OUT NET_BUF *Nbuf, + IN UINT32 Len, + IN BOOLEAN FromHead + ); + +/** + Trim Len bytes from the header or the tail of the net buffer. + + @param[in, out] Nbuf The pointer to the net buffer. + @param[in] Len The length of the data to be trimmed. + @param[in] FromHead The flag to indicate whether trim data is from the + head (TRUE) or the tail (FALSE). + + @return The length of the actual trimmed data, which may be less + than Len if the TotalSize of Nbuf is less than Len. + +**/ +UINT32 +EFIAPI +NetbufTrim ( + IN OUT NET_BUF *Nbuf, + IN UINT32 Len, + IN BOOLEAN FromHead + ); + +/** + Copy Len bytes of data from the specific offset of the net buffer to the + destination memory. + + The Len bytes of data may cross several fragments of the net buffer. + + @param[in] Nbuf The pointer to the net buffer. + @param[in] Offset The sequence number of the first byte to copy. + @param[in] Len The length of the data to copy. + @param[in] Dest The destination of the data to copy to. + + @return The length of the actual copied data, or 0 if the offset + specified exceeds the total size of net buffer. + +**/ +UINT32 +EFIAPI +NetbufCopy ( + IN NET_BUF *Nbuf, + IN UINT32 Offset, + IN UINT32 Len, + IN UINT8 *Dest + ); + +/** + Build a NET_BUF from external blocks. + + A new NET_BUF structure will be created from external blocks. An additional block + of memory will be allocated to hold reserved HeadSpace bytes of header room + and existing HeadLen bytes of header, but the external blocks are shared by the + net buffer to avoid data copying. + + @param[in] ExtFragment The pointer to the data block. + @param[in] ExtNum The number of the data blocks. + @param[in] HeadSpace The head space to be reserved. + @param[in] HeadLen The length of the protocol header. The function + pulls this amount of data into a linear block. + @param[in] ExtFree The pointer to the caller-provided free function. + @param[in] Arg The argument passed to ExtFree when ExtFree is + called. + + @return The pointer to the net buffer built from the data blocks, + or NULL if the allocation failed due to resource + limit. + +**/ +NET_BUF * +EFIAPI +NetbufFromExt ( + IN NET_FRAGMENT *ExtFragment, + IN UINT32 ExtNum, + IN UINT32 HeadSpace, + IN UINT32 HeadLen, + IN NET_VECTOR_EXT_FREE ExtFree, + IN VOID *Arg OPTIONAL + ); + +/** + Build a fragment table to contain the fragments in the net buffer. This is the + opposite operation of the NetbufFromExt. + + @param[in] Nbuf Points to the net buffer. + @param[in, out] ExtFragment The pointer to the data block. + @param[in, out] ExtNum The number of the data blocks. + + @retval EFI_BUFFER_TOO_SMALL The number of non-empty blocks is bigger than + ExtNum. + @retval EFI_SUCCESS The fragment table was built successfully. + +**/ +EFI_STATUS +EFIAPI +NetbufBuildExt ( + IN NET_BUF *Nbuf, + IN OUT NET_FRAGMENT *ExtFragment, + IN OUT UINT32 *ExtNum + ); + +/** + Build a net buffer from a list of net buffers. + + All the fragments will be collected from the list of NEW_BUF, and then a new + net buffer will be created through NetbufFromExt. + + @param[in] BufList A List of the net buffer. + @param[in] HeadSpace The head space to be reserved. + @param[in] HeaderLen The length of the protocol header. The function + pulls this amount of data into a linear block. + @param[in] ExtFree The pointer to the caller provided free function. + @param[in] Arg The argument passed to ExtFree when ExtFree is called. + + @return The pointer to the net buffer built from the list of net + buffers. + +**/ +NET_BUF * +EFIAPI +NetbufFromBufList ( + IN LIST_ENTRY *BufList, + IN UINT32 HeadSpace, + IN UINT32 HeaderLen, + IN NET_VECTOR_EXT_FREE ExtFree, + IN VOID *Arg OPTIONAL + ); + +/** + Free a list of net buffers. + + @param[in, out] Head The pointer to the head of linked net buffers. + +**/ +VOID +EFIAPI +NetbufFreeList ( + IN OUT LIST_ENTRY *Head + ); + +/** + Initiate the net buffer queue. + + @param[in, out] NbufQue The pointer to the net buffer queue to be initialized. + +**/ +VOID +EFIAPI +NetbufQueInit ( + IN OUT NET_BUF_QUEUE *NbufQue + ); + +/** + Allocate and initialize a net buffer queue. + + @return The pointer to the allocated net buffer queue, or NULL if the + allocation failed due to resource limit. + +**/ +NET_BUF_QUEUE * +EFIAPI +NetbufQueAlloc ( + VOID + ); + +/** + Free a net buffer queue. + + Decrease the reference count of the net buffer queue by one. The real resource + free operation isn't performed until the reference count of the net buffer + queue is decreased to 0. + + @param[in] NbufQue The pointer to the net buffer queue to be freed. + +**/ +VOID +EFIAPI +NetbufQueFree ( + IN NET_BUF_QUEUE *NbufQue + ); + +/** + Remove a net buffer from the head in the specific queue and return it. + + @param[in, out] NbufQue The pointer to the net buffer queue. + + @return The pointer to the net buffer removed from the specific queue, + or NULL if there is no net buffer in the specific queue. + +**/ +NET_BUF * +EFIAPI +NetbufQueRemove ( + IN OUT NET_BUF_QUEUE *NbufQue + ); + +/** + Append a net buffer to the net buffer queue. + + @param[in, out] NbufQue The pointer to the net buffer queue. + @param[in, out] Nbuf The pointer to the net buffer to be appended. + +**/ +VOID +EFIAPI +NetbufQueAppend ( + IN OUT NET_BUF_QUEUE *NbufQue, + IN OUT NET_BUF *Nbuf + ); + +/** + Copy Len bytes of data from the net buffer queue at the specific offset to the + destination memory. + + The copying operation is the same as NetbufCopy, but applies to the net buffer + queue instead of the net buffer. + + @param[in] NbufQue The pointer to the net buffer queue. + @param[in] Offset The sequence number of the first byte to copy. + @param[in] Len The length of the data to copy. + @param[out] Dest The destination of the data to copy to. + + @return The length of the actual copied data, or 0 if the offset + specified exceeds the total size of net buffer queue. + +**/ +UINT32 +EFIAPI +NetbufQueCopy ( + IN NET_BUF_QUEUE *NbufQue, + IN UINT32 Offset, + IN UINT32 Len, + OUT UINT8 *Dest + ); + +/** + Trim Len bytes of data from the buffer queue and free any net buffer + that is completely trimmed. + + The trimming operation is the same as NetbufTrim but applies to the net buffer + queue instead of the net buffer. + + @param[in, out] NbufQue The pointer to the net buffer queue. + @param[in] Len The length of the data to trim. + + @return The actual length of the data trimmed. + +**/ +UINT32 +EFIAPI +NetbufQueTrim ( + IN OUT NET_BUF_QUEUE *NbufQue, + IN UINT32 Len + ); + + +/** + Flush the net buffer queue. + + @param[in, out] NbufQue The pointer to the queue to be flushed. + +**/ +VOID +EFIAPI +NetbufQueFlush ( + IN OUT NET_BUF_QUEUE *NbufQue + ); + +/** + Compute the checksum for a bulk of data. + + @param[in] Bulk The pointer to the data. + @param[in] Len The length of the data, in bytes. + + @return The computed checksum. + +**/ +UINT16 +EFIAPI +NetblockChecksum ( + IN UINT8 *Bulk, + IN UINT32 Len + ); + +/** + Add two checksums. + + @param[in] Checksum1 The first checksum to be added. + @param[in] Checksum2 The second checksum to be added. + + @return The new checksum. + +**/ +UINT16 +EFIAPI +NetAddChecksum ( + IN UINT16 Checksum1, + IN UINT16 Checksum2 + ); + +/** + Compute the checksum for a NET_BUF. + + @param[in] Nbuf The pointer to the net buffer. + + @return The computed checksum. + +**/ +UINT16 +EFIAPI +NetbufChecksum ( + IN NET_BUF *Nbuf + ); + +/** + Compute the checksum for TCP/UDP pseudo header. + + Src and Dst are in network byte order, and Len is in host byte order. + + @param[in] Src The source address of the packet. + @param[in] Dst The destination address of the packet. + @param[in] Proto The protocol type of the packet. + @param[in] Len The length of the packet. + + @return The computed checksum. + +**/ +UINT16 +EFIAPI +NetPseudoHeadChecksum ( + IN IP4_ADDR Src, + IN IP4_ADDR Dst, + IN UINT8 Proto, + IN UINT16 Len + ); + +/** + Compute the checksum for the TCP6/UDP6 pseudo header. + + Src and Dst are in network byte order, and Len is in host byte order. + + @param[in] Src The source address of the packet. + @param[in] Dst The destination address of the packet. + @param[in] NextHeader The protocol type of the packet. + @param[in] Len The length of the packet. + + @return The computed checksum. + +**/ +UINT16 +EFIAPI +NetIp6PseudoHeadChecksum ( + IN EFI_IPv6_ADDRESS *Src, + IN EFI_IPv6_ADDRESS *Dst, + IN UINT8 NextHeader, + IN UINT32 Len + ); + +/** + The function frees the net buffer which allocated by the IP protocol. It releases + only the net buffer and doesn't call the external free function. + + This function should be called after finishing the process of mIpSec->ProcessExt() + for outbound traffic. The (EFI_IPSEC2_PROTOCOL)->ProcessExt() allocates a new + buffer for the ESP, so there needs a function to free the old net buffer. + + @param[in] Nbuf The network buffer to be freed. + +**/ +VOID +NetIpSecNetbufFree ( + NET_BUF *Nbuf + ); + +/** + This function obtains the system guid from the smbios table. + + If SystemGuid is NULL, then ASSERT(). + + @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 + ); + +/** + Create Dns QName according the queried domain name. + + If DomainName is NULL, then ASSERT(). + + 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 + ); + +#endif diff --git a/NetworkPkg/Include/Library/TcpIoLib.h b/NetworkPkg/Include/Library/TcpIoLib.h new file mode 100644 index 000000000..63872f615 --- /dev/null +++ b/NetworkPkg/Include/Library/TcpIoLib.h @@ -0,0 +1,247 @@ +/** @file + This library is used to share code between UEFI network stack modules. + It provides the helper routines to access TCP service. + +Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _TCP_IO_H_ +#define _TCP_IO_H_ + + +#include +#include + +#include + +#define TCP_VERSION_4 IP_VERSION_4 +#define TCP_VERSION_6 IP_VERSION_6 + +/// +/// 10 seconds +/// +#define TCP_GET_MAPPING_TIMEOUT 100000000U + + +typedef struct { + EFI_IPv4_ADDRESS LocalIp; + EFI_IPv4_ADDRESS SubnetMask; + EFI_IPv4_ADDRESS Gateway; + + UINT16 StationPort; + EFI_IPv4_ADDRESS RemoteIp; + UINT16 RemotePort; + BOOLEAN ActiveFlag; +} TCP4_IO_CONFIG_DATA; + +typedef struct { + UINT16 StationPort; + EFI_IPv6_ADDRESS RemoteIp; + UINT16 RemotePort; + BOOLEAN ActiveFlag; +} TCP6_IO_CONFIG_DATA; + +typedef union { + TCP4_IO_CONFIG_DATA Tcp4IoConfigData; + TCP6_IO_CONFIG_DATA Tcp6IoConfigData; +} TCP_IO_CONFIG_DATA; + +typedef union { + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP6_PROTOCOL *Tcp6; +} TCP_IO_PROTOCOL; + +typedef union { + EFI_TCP4_CONNECTION_TOKEN Tcp4Token; + EFI_TCP6_CONNECTION_TOKEN Tcp6Token; +} TCP_IO_CONNECTION_TOKEN; + +typedef union { + EFI_TCP4_IO_TOKEN Tcp4Token; + EFI_TCP6_IO_TOKEN Tcp6Token; +} TCP_IO_IO_TOKEN; + +typedef union { + EFI_TCP4_CLOSE_TOKEN Tcp4Token; + EFI_TCP6_CLOSE_TOKEN Tcp6Token; +} TCP_IO_CLOSE_TOKEN; + +typedef union { + EFI_TCP4_LISTEN_TOKEN Tcp4Token; + EFI_TCP6_LISTEN_TOKEN Tcp6Token; +} TCP_IO_LISTEN_TOKEN; + + +typedef struct { + UINT8 TcpVersion; + EFI_HANDLE Image; + EFI_HANDLE Controller; + EFI_HANDLE Handle; + + TCP_IO_PROTOCOL Tcp; + TCP_IO_PROTOCOL NewTcp; + TCP_IO_CONNECTION_TOKEN ConnToken; + TCP_IO_IO_TOKEN TxToken; + TCP_IO_IO_TOKEN RxToken; + TCP_IO_CLOSE_TOKEN CloseToken; + TCP_IO_LISTEN_TOKEN ListenToken; + + BOOLEAN IsConnDone; + BOOLEAN IsTxDone; + BOOLEAN IsRxDone; + BOOLEAN IsCloseDone; + BOOLEAN IsListenDone; +} TCP_IO; + +/** + Create a TCP socket with the specified configuration data. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + @param[in] TcpVersion The version of Tcp, TCP_VERSION_4 or TCP_VERSION_6. + @param[in] ConfigData The Tcp configuration data. + @param[out] TcpIo The TcpIo. + + @retval EFI_SUCCESS The TCP socket is created and configured. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Others Failed to create the TCP socket or configure it. + +**/ +EFI_STATUS +EFIAPI +TcpIoCreateSocket ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN UINT8 TcpVersion, + IN TCP_IO_CONFIG_DATA *ConfigData, + OUT TCP_IO *TcpIo + ); + +/** + Destroy the socket. + + @param[in] TcpIo The TcpIo which wraps the socket to be destroyed. + +**/ +VOID +EFIAPI +TcpIoDestroySocket ( + IN TCP_IO *TcpIo + ); + +/** + Connect to the other endpoint of the TCP socket. + + @param[in, out] TcpIo The TcpIo wrapping the TCP socket. + @param[in] Timeout The time to wait for connection done. Set to NULL for infinite wait. + + @retval EFI_SUCCESS Connect to the other endpoint of the TCP socket + successfully. + @retval EFI_TIMEOUT Failed to connect to the other endpoint of the + TCP socket in the specified time period. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TcpIoConnect ( + IN OUT TCP_IO *TcpIo, + IN EFI_EVENT Timeout OPTIONAL + ); + +/** + Accept the incomding request from the other endpoint of the TCP socket. + + @param[in, out] TcpIo The TcpIo wrapping the TCP socket. + @param[in] Timeout The time to wait for connection done. Set to NULL for infinite wait. + + + @retval EFI_SUCCESS Connect to the other endpoint of the TCP socket + successfully. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + + @retval EFI_TIMEOUT Failed to connect to the other endpoint of the + TCP socket in the specified time period. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TcpIoAccept ( + IN OUT TCP_IO *TcpIo, + IN EFI_EVENT Timeout OPTIONAL + ); + +/** + Reset the socket. + + @param[in, out] TcpIo The TcpIo wrapping the TCP socket. + +**/ +VOID +EFIAPI +TcpIoReset ( + IN OUT TCP_IO *TcpIo + ); + +/** + Transmit the Packet to the other endpoint of the socket. + + @param[in] TcpIo The TcpIo wrapping the TCP socket. + @param[in] Packet The packet to transmit. + + @retval EFI_SUCCESS The packet is trasmitted. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TcpIoTransmit ( + IN TCP_IO *TcpIo, + IN NET_BUF *Packet + ); + +/** + Receive data from the socket. + + @param[in, out] TcpIo The TcpIo which wraps the socket to be destroyed. + @param[in] Packet The buffer to hold the data copy from the socket rx buffer. + @param[in] AsyncMode Is this receive asyncronous or not. + @param[in] Timeout The time to wait for receiving the amount of data the Packet + can hold. Set to NULL for infinite wait. + + @retval EFI_SUCCESS The required amount of data is received from the socket. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval EFI_OUT_OF_RESOURCES Failed to allocate momery. + @retval EFI_TIMEOUT Failed to receive the required amount of data in the + specified time period. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TcpIoReceive ( + IN OUT TCP_IO *TcpIo, + IN NET_BUF *Packet, + IN BOOLEAN AsyncMode, + IN EFI_EVENT Timeout OPTIONAL + ); + +#endif + diff --git a/NetworkPkg/Include/Library/UdpIoLib.h b/NetworkPkg/Include/Library/UdpIoLib.h new file mode 100644 index 000000000..c3cddfafb --- /dev/null +++ b/NetworkPkg/Include/Library/UdpIoLib.h @@ -0,0 +1,363 @@ +/** @file + This library is used to share code between UEFI network stack modules. + It provides the helper routines to access UDP service. It is used by both DHCP and MTFTP. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _UDP_IO_H_ +#define _UDP_IO_H_ + +#include +#include + +#include + +typedef struct _UDP_IO UDP_IO; + +/// +/// Signatures used by UdpIo Library. +/// + +#define UDP_IO_RX_SIGNATURE SIGNATURE_32 ('U', 'D', 'P', 'R') +#define UDP_IO_TX_SIGNATURE SIGNATURE_32 ('U', 'D', 'P', 'T') +#define UDP_IO_SIGNATURE SIGNATURE_32 ('U', 'D', 'P', 'I') + +#define UDP_IO_UDP4_VERSION 4 +#define UDP_IO_UDP6_VERSION 6 + +/// +/// The UDP address pair. +/// +typedef struct { + EFI_IP_ADDRESS LocalAddr; + UINT16 LocalPort; + EFI_IP_ADDRESS RemoteAddr; + UINT16 RemotePort; +} UDP_END_POINT; + +/** + Prototype called when receiving or sending packets to or from a UDP point. + + This prototype is used by both receive and sending when calling + UdpIoRecvDatagram() or UdpIoSendDatagram(). When receiving, Netbuf is allocated by the + UDP access point and released by the user. When sending, the user allocates the the NetBuf, + which is then provided to the callback as a reference. + + @param[in] Packet The packet received or sent. + @param[in] EndPoint The UDP address pair corresponds to the UDP IO. + @param[in] IoStatus The packet receiving or sending status. + @param[in] Context The user-defined data when calling UdpIoRecvDatagram() or + UdpIoSendDatagram(). +**/ +typedef +VOID +(EFIAPI *UDP_IO_CALLBACK) ( + IN NET_BUF *Packet, + IN UDP_END_POINT *EndPoint, + IN EFI_STATUS IoStatus, + IN VOID *Context + ); + +/// +/// This structure is used internally by the UdpIo Library. +/// +/// Each receive request is wrapped in an UDP_RX_TOKEN. Upon completion, +/// the CallBack will be called. Only one receive request is sent to UDP at a +/// time. HeadLen gives the length of the application's header. UDP_IO will +/// make the application's header continuous before delivering up. +/// +typedef union { + EFI_UDP4_COMPLETION_TOKEN Udp4; + EFI_UDP6_COMPLETION_TOKEN Udp6; +} UDP_COMPLETION_TOKEN; + +typedef struct { + UINT32 Signature; + UDP_IO *UdpIo; + + UDP_IO_CALLBACK CallBack; + VOID *Context; + UINT32 HeadLen; + + UDP_COMPLETION_TOKEN Token; +} UDP_RX_TOKEN; + + + +/// +/// This structure is used internally by UdpIo Library. +/// +/// Each transmit request is wrapped in an UDP_TX_TOKEN. Upon completion, +/// the CallBack will be called. There can be several transmit requests. All transmit +/// requests are linked in a list. +/// + +typedef union { + EFI_UDP4_SESSION_DATA Udp4; + EFI_UDP6_SESSION_DATA Udp6; +} UDP_SESSION_DATA; + +typedef union { + EFI_UDP4_TRANSMIT_DATA Udp4; + EFI_UDP6_TRANSMIT_DATA Udp6; +} UDP_TRANSMIT_DATA; + +typedef struct { + UINT32 Signature; + LIST_ENTRY Link; + UDP_IO *UdpIo; + UDP_IO_CALLBACK CallBack; + NET_BUF *Packet; + VOID *Context; + EFI_IPv4_ADDRESS Gateway; + UDP_SESSION_DATA Session; + UDP_COMPLETION_TOKEN Token; + UDP_TRANSMIT_DATA Data; +} UDP_TX_TOKEN; + +/// +/// Type defined as UDP_IO. +/// +/// This data structure wraps the UDP instance and configuration. +/// UdpIo Library uses this structure for all Udp4 or Udp6 operations. +/// +struct _UDP_IO { + UINT32 Signature; + LIST_ENTRY Link; + INTN RefCnt; + UINT8 UdpVersion; + + // + // Handle used to create/destroy UDP child + // + EFI_HANDLE Controller; + EFI_HANDLE Image; + EFI_HANDLE UdpHandle; + + EFI_SIMPLE_NETWORK_MODE SnpMode; + + LIST_ENTRY SentDatagram; ///< A list of UDP_TX_TOKEN. + UDP_RX_TOKEN *RecvRequest; + + union { + EFI_UDP4_PROTOCOL *Udp4; + EFI_UDP6_PROTOCOL *Udp6; + } Protocol; + + union { + EFI_UDP4_CONFIG_DATA Udp4; + EFI_UDP6_CONFIG_DATA Udp6; + } Config; +}; + +/** + The prototype called when UdpIo Library configures a UDP instance. + + The prototype is set and called when creating a UDP_IO in UdpIoCreatePort(). + + @param[in] UdpIo The UDP_IO to configure. + @param[in] Context The user-defined data when calling UdpIoCreatePort(). + + @retval EFI_SUCCESS The configuration succeeded. + @retval Others The UDP_IO fails to configure indicating + UdpIoCreatePort() should fail. +**/ +typedef +EFI_STATUS +(EFIAPI *UDP_IO_CONFIG) ( + IN UDP_IO *UdpIo, + IN VOID *Context + ); + +/** + The select function to decide whether to cancel the UDP_TX_TOKEN. + + @param[in] Token The UDP_TX_TOKEN to decide whether to cancel. + @param[in] Context User-defined data in UdpIoCancelDgrams(). + + @retval TRUE Cancel the UDP_TX_TOKEN. + @retval FALSE Do not cancel this UDP_TX_TOKEN. + +**/ +typedef +BOOLEAN +(EFIAPI *UDP_IO_TO_CANCEL) ( + IN UDP_TX_TOKEN *Token, + IN VOID *Context + ); + +/** + Cancel all the sent datagram that pass the selection criteria of ToCancel. + + If ToCancel is NULL, all the datagrams are cancelled. + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + @param[in] UdpIo The UDP_IO to cancel packet. + @param[in] IoStatus The IoStatus to return to the packet owners. + @param[in] ToCancel The select funtion to test whether to cancel this + packet or not. + @param[in] Context The opaque parameter to the ToCancel. + +**/ +VOID +EFIAPI +UdpIoCancelDgrams ( + IN UDP_IO *UdpIo, + IN EFI_STATUS IoStatus, + IN UDP_IO_TO_CANCEL ToCancel, OPTIONAL + IN VOID *Context OPTIONAL + ); + +/** + Creates a UDP_IO to access the UDP service. It creates and configures + a UDP child. + + If Configure is NULL, then ASSERT(). + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + It locates the UDP service binding prototype on the Controller parameter + uses the UDP service binding prototype to create a UDP child (also known as + a UDP instance) configures the UDP child by calling Configure function prototype. + Any failures in creating or configuring the UDP child return NULL for failure. + + @param[in] Controller The controller that has the UDP service binding. + protocol installed. + @param[in] ImageHandle The image handle for the driver. + @param[in] Configure The function to configure the created UDP child. + @param[in] UdpVersion The UDP protocol version, UDP4 or UDP6. + @param[in] Context The opaque parameter for the Configure funtion. + + @return The newly-created UDP_IO, or NULL if failed. + +**/ +UDP_IO * +EFIAPI +UdpIoCreateIo ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE ImageHandle, + IN UDP_IO_CONFIG Configure, + IN UINT8 UdpVersion, + IN VOID *Context + ); + +/** + Free the UDP_IO and all its related resources. + + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + The function cancels all sent datagrams and receive requests. + + @param[in] UdpIo The UDP_IO to free. + + @retval EFI_SUCCESS The UDP_IO is freed. + @retval Others Failed to free UDP_IO. + +**/ +EFI_STATUS +EFIAPI +UdpIoFreeIo ( + IN UDP_IO *UdpIo + ); + +/** + Cleans up the UDP_IO without freeing it. Call this function + if you intend to later re-use the UDP_IO. + + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + This function releases all transmitted datagrams and receive requests and configures NULL for the UDP instance. + + @param[in] UdpIo The UDP_IO to clean up. + +**/ +VOID +EFIAPI +UdpIoCleanIo ( + IN UDP_IO *UdpIo + ); + +/** + Send a packet through the UDP_IO. + + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + The packet will be wrapped in UDP_TX_TOKEN. Function Callback will be called + when the packet is sent. The optional parameter EndPoint overrides the default + address pair if specified. + + @param[in] UdpIo The UDP_IO to send the packet through. + @param[in] Packet The packet to send. + @param[in] EndPoint The local and remote access point. Override the + default address pair set during configuration. + @param[in] Gateway The gateway to use. + @param[in] CallBack The function being called when packet is + transmitted or failed. + @param[in] Context The opaque parameter passed to CallBack. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the packet. + @retval EFI_SUCCESS The packet is successfully delivered to UDP for + transmission. + +**/ +EFI_STATUS +EFIAPI +UdpIoSendDatagram ( + IN UDP_IO *UdpIo, + IN NET_BUF *Packet, + IN UDP_END_POINT *EndPoint OPTIONAL, + IN EFI_IP_ADDRESS *Gateway OPTIONAL, + IN UDP_IO_CALLBACK CallBack, + IN VOID *Context + ); + +/** + Cancel a single sent datagram. + + @param[in] UdpIo The UDP_IO from which to cancel the packet + @param[in] Packet The packet to cancel + +**/ +VOID +EFIAPI +UdpIoCancelSentDatagram ( + IN UDP_IO *UdpIo, + IN NET_BUF *Packet + ); + +/** + Issue a receive request to the UDP_IO. + + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + This function is called when upper-layer needs packet from UDP for processing. + Only one receive request is acceptable at a time. Therefore, one common usage model is + to invoke this function inside its Callback function when the former packet + is processed. + + @param[in] UdpIo The UDP_IO to receive the packet from. + @param[in] CallBack The call back function to execute when the packet + is received. + @param[in] Context The opaque context passed to Callback. + @param[in] HeadLen The length of the upper-layer's protocol header. + + @retval EFI_ALREADY_STARTED There is already a pending receive request. Only + one receive request is supported at a time. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. + @retval EFI_SUCCESS The receive request was issued successfully. + @retval EFI_UNSUPPORTED The UDP version in UDP_IO is not supported. + +**/ +EFI_STATUS +EFIAPI +UdpIoRecvDatagram ( + IN UDP_IO *UdpIo, + IN UDP_IO_CALLBACK CallBack, + IN VOID *Context, + IN UINT32 HeadLen + ); + +#endif + diff --git a/NetworkPkg/Include/Protocol/Dpc.h b/NetworkPkg/Include/Protocol/Dpc.h new file mode 100644 index 000000000..1e1d0d316 --- /dev/null +++ b/NetworkPkg/Include/Protocol/Dpc.h @@ -0,0 +1,98 @@ +/** @file + + EFI Deferred Procedure Call Protocol. + +Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#ifndef __DPC_H__ +#define __DPC_H__ + +// +// DPC Protocol GUID value +// +#define EFI_DPC_PROTOCOL_GUID \ + { \ + 0x480f8ae9, 0xc46, 0x4aa9, { 0xbc, 0x89, 0xdb, 0x9f, 0xba, 0x61, 0x98, 0x6 } \ + } + +// +// Forward reference for pure ANSI compatability +// +typedef struct _EFI_DPC_PROTOCOL EFI_DPC_PROTOCOL; + + +/** + Invoke a Deferred Procedure Call. + + @param DpcContext The pointer to the Deferred Procedure Call's context, + which is implementation dependent. + +**/ +typedef +VOID +(EFIAPI *EFI_DPC_PROCEDURE)( + IN VOID *DpcContext + ); + +/** + Add a Deferred Procedure Call to the end of the DPC queue. + + @param This The protocol instance pointer. + @param DpcTpl The EFI_TPL that the DPC should invoke. + @param DpcProcedure The pointer to the DPC's function. + @param DpcContext The pointer to the DPC's context. Passed to DpcProcedure + when DpcProcedure is invoked. + + @retval EFI_SUCCESS The DPC was queued. + @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL. + @retval EFI_INVALID_PARAMETER DpcProcedure is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to + add the DPC to the queue. + +**/ +typedef +EFI_STATUS +(EFIAPI *EFI_DPC_QUEUE_DPC)( + IN EFI_DPC_PROTOCOL *This, + IN EFI_TPL DpcTpl, + IN EFI_DPC_PROCEDURE DpcProcedure, + IN VOID *DpcContext OPTIONAL + ); + +/** + Dispatch the queue of DPCs. + + DPCs with DpcTpl value greater than the current TPL value are queued, and then DPCs + with DpcTpl value lower than the current TPL value are queued. All DPCs in the first + group (higher DpcTpl values) are invoked before DPCs in the second group (lower DpcTpl values). + + @param This Protocol instance pointer. + + @retval EFI_SUCCESS One or more DPCs were invoked. + @retval EFI_NOT_FOUND No DPCs were invoked. + +**/ +typedef +EFI_STATUS +(EFIAPI *EFI_DPC_DISPATCH_DPC)( + IN EFI_DPC_PROTOCOL *This + ); + +/// +/// DPC Protocol structure. +/// +struct _EFI_DPC_PROTOCOL { + EFI_DPC_QUEUE_DPC QueueDpc; + EFI_DPC_DISPATCH_DPC DispatchDpc; +}; + +/// +/// DPC Protocol GUID variable. +/// +extern EFI_GUID gEfiDpcProtocolGuid; + +#endif diff --git a/NetworkPkg/Ip4Dxe/ComponentName.c b/NetworkPkg/Ip4Dxe/ComponentName.c new file mode 100644 index 000000000..3461ab2a8 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/ComponentName.c @@ -0,0 +1,428 @@ +/** @file + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Ip4ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Ip4ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gIp4ComponentName = { + Ip4ComponentNameGetDriverName, + Ip4ComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gIp4ComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) Ip4ComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) Ip4ComponentNameGetControllerName, + "en" +}; + + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mIp4DriverNameTable[] = { + { + "eng;en", + L"IP4 Network Service Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gIp4ControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Ip4ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mIp4DriverNameTable, + DriverName, + (BOOLEAN)(This == &gIp4ComponentName) + ); + +} + +/** + Update the component name for the IP4 child handle. + + @param Ip4[in] A pointer to the EFI_IP4_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateName ( + IN EFI_IP4_PROTOCOL *Ip4 + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[80]; + EFI_IP4_MODE_DATA Ip4ModeData; + + if (Ip4 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer as: + // IPv4 (SrcIP=127.0.0.1, DestIP=127.0.0.1) + // + Status = Ip4->GetModeData (Ip4, &Ip4ModeData, NULL, NULL); + if (EFI_ERROR (Status)) { + return Status; + } + + if (!Ip4ModeData.IsStarted || !Ip4ModeData.IsConfigured) { + UnicodeSPrint (HandleName, sizeof (HandleName), L"IPv4 (Not started)"); + } else { + UnicodeSPrint (HandleName, sizeof (HandleName), + L"IPv4 (SrcIP=%d.%d.%d.%d)", + Ip4ModeData.ConfigData.StationAddress.Addr[0], + Ip4ModeData.ConfigData.StationAddress.Addr[1], + Ip4ModeData.ConfigData.StationAddress.Addr[2], + Ip4ModeData.ConfigData.StationAddress.Addr[3] + ); + } + + if (gIp4ControllerNameTable != NULL) { + FreeUnicodeStringTable (gIp4ControllerNameTable); + gIp4ControllerNameTable = NULL; + } + Status = AddUnicodeString2 ( + "eng", + gIp4ComponentName.SupportedLanguages, + &gIp4ControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gIp4ComponentName2.SupportedLanguages, + &gIp4ControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Ip4ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_IP4_PROTOCOL *Ip4; + + // + // Only provide names for child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiManagedNetworkProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiIp4ProtocolGuid, + (VOID **)&Ip4, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateName (Ip4); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gIp4ControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gIp4ComponentName) + ); +} + diff --git a/NetworkPkg/Ip4Dxe/Ip4Common.c b/NetworkPkg/Ip4Dxe/Ip4Common.c new file mode 100644 index 000000000..c756a2dbf --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Common.c @@ -0,0 +1,306 @@ +/** @file + +Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + + +/** + Return the cast type (Unicast/Boradcast) specific to an + interface. All the addresses are host byte ordered. + + @param[in] IpAddr The IP address to classify in host byte order + @param[in] IpIf The interface that IpAddr received from + + @return The cast type of this IP address specific to the interface. + @retval IP4_LOCAL_HOST The IpAddr equals to the interface's address + @retval IP4_SUBNET_BROADCAST The IpAddr is a directed subnet boradcast to the + interface + @retval IP4_NET_BROADCAST The IpAddr is a network broadcast to the interface + @retval 0 Otherwise. + +**/ +INTN +Ip4GetNetCast ( + IN IP4_ADDR IpAddr, + IN IP4_INTERFACE *IpIf + ) +{ + if (IpAddr == IpIf->Ip) { + return IP4_LOCAL_HOST; + + } else if (IpAddr == IpIf->SubnetBrdcast) { + return IP4_SUBNET_BROADCAST; + + } else if (IpAddr == IpIf->NetBrdcast) { + return IP4_NET_BROADCAST; + + } + + return 0; +} + + +/** + Find the cast type of the packet related to the local host. + This isn't the same as link layer cast type. For example, DHCP + server may send local broadcast to the local unicast MAC. + + @param[in] IpSb The IP4 service binding instance that received the + packet + @param[in] Dst The destination address in the packet (host byte + order) + @param[in] Src The source address in the packet (host byte order) + + @return The cast type for the Dst, it will return on the first non-promiscuous + cast type to a configured interface. If the packet doesn't match any of + the interface, multicast address and local broadcast address are checked. + +**/ +INTN +Ip4GetHostCast ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR Dst, + IN IP4_ADDR Src + ) +{ + LIST_ENTRY *Entry; + IP4_INTERFACE *IpIf; + INTN Type; + INTN Class; + + Type = 0; + + if (IpSb->MnpConfigData.EnablePromiscuousReceive) { + Type = IP4_PROMISCUOUS; + } + + // + // Go through the interface list of the IP service, most likely. + // + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link); + + // + // Skip the unconfigured interface and invalid source address: + // source address can't be broadcast. + // + if (!IpIf->Configured || IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) { + continue; + } + + if ((Class = Ip4GetNetCast (Dst, IpIf)) > Type) { + return Class; + } + } + + // + // If it is local broadcast address. The source address must + // be a unicast address on one of the direct connected network. + // If it is a multicast address, accept it only if we are in + // the group. + // + if (Dst == IP4_ALLONE_ADDRESS) { + IpIf = Ip4FindNet (IpSb, Src); + + if (IpIf != NULL && !IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) { + return IP4_LOCAL_BROADCAST; + } + + } else if (IP4_IS_MULTICAST (Dst) && Ip4FindGroup (&IpSb->IgmpCtrl, Dst) != NULL) { + return IP4_MULTICAST; + } + + return Type; +} + + +/** + Find an interface whose configured IP address is Ip. + + @param[in] IpSb The IP4 service binding instance + @param[in] Ip The Ip address (host byte order) to find + + @return The IP4_INTERFACE point if found, otherwise NULL + +**/ +IP4_INTERFACE * +Ip4FindInterface ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR Ip + ) +{ + LIST_ENTRY *Entry; + IP4_INTERFACE *IpIf; + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link); + + if (IpIf->Configured && (IpIf->Ip == Ip)) { + return IpIf; + } + } + + return NULL; +} + + +/** + Find an interface that Ip is on that connected network. + + @param[in] IpSb The IP4 service binding instance + @param[in] Ip The Ip address (host byte order) to find + + @return The IP4_INTERFACE point if found, otherwise NULL + +**/ +IP4_INTERFACE * +Ip4FindNet ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR Ip + ) +{ + LIST_ENTRY *Entry; + IP4_INTERFACE *IpIf; + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link); + + if (IpIf->Configured && IP4_NET_EQUAL (Ip, IpIf->Ip, IpIf->SubnetMask)) { + return IpIf; + } + } + + return NULL; +} + + +/** + Find an interface of the service with the same Ip/Netmask pair. + + @param[in] IpSb Ip4 service binding instance + @param[in] Ip The Ip adress to find (host byte order) + @param[in] Netmask The network to find (host byte order) + + @return The IP4_INTERFACE point if found, otherwise NULL + +**/ +IP4_INTERFACE * +Ip4FindStationAddress ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR Ip, + IN IP4_ADDR Netmask + ) +{ + LIST_ENTRY *Entry; + IP4_INTERFACE *IpIf; + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link); + + if (IpIf->Configured && (IpIf->Ip == Ip) && (IpIf->SubnetMask == Netmask)) { + return IpIf; + } + } + + return NULL; +} + + +/** + Get the MAC address for a multicast IP address. Call + Mnp's McastIpToMac to find the MAC address in stead of + hard code the NIC to be Ethernet. + + @param[in] Mnp The Mnp instance to get the MAC address. + @param[in] Multicast The multicast IP address to translate. + @param[out] Mac The buffer to hold the translated address. + + @retval EFI_SUCCESS if the multicast IP is successfully translated to a + multicast MAC address. + @retval other Otherwise some error. + +**/ +EFI_STATUS +Ip4GetMulticastMac ( + IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp, + IN IP4_ADDR Multicast, + OUT EFI_MAC_ADDRESS *Mac + ) +{ + EFI_IP_ADDRESS EfiIp; + + EFI_IP4 (EfiIp.v4) = HTONL (Multicast); + return Mnp->McastIpToMac (Mnp, FALSE, &EfiIp, Mac); +} + + +/** + Convert the multibyte field in IP header's byter order. + In spite of its name, it can also be used to convert from + host to network byte order. + + @param[in] Head The IP head to convert + + @return Point to the converted IP head + +**/ +IP4_HEAD * +Ip4NtohHead ( + IN IP4_HEAD *Head + ) +{ + Head->TotalLen = NTOHS (Head->TotalLen); + Head->Id = NTOHS (Head->Id); + Head->Fragment = NTOHS (Head->Fragment); + Head->Src = NTOHL (Head->Src); + Head->Dst = NTOHL (Head->Dst); + + return Head; +} + + +/** + Validate that Ip/Netmask pair is OK to be used as station + address. Only continuous netmasks are supported. and check + that StationAddress is a unicast address on the newtwork. + + @param[in] Ip The IP address to validate. + @param[in] Netmask The netmaks of the IP. + + @retval TRUE The Ip/Netmask pair is valid. + @retval FALSE The Ip/Netmask pair is invalid. + +**/ +BOOLEAN +Ip4StationAddressValid ( + IN IP4_ADDR Ip, + IN IP4_ADDR Netmask + ) +{ + // + // Only support the station address with 0.0.0.0/0 to enable DHCP client. + // + if (Netmask == IP4_ALLZERO_ADDRESS) { + return (BOOLEAN) (Ip == IP4_ALLZERO_ADDRESS); + } + + // + // Only support the continuous net masks + // + if (NetGetMaskLength (Netmask) == (IP4_MASK_MAX + 1)) { + return FALSE; + } + + // + // Station address can't be class D or class E address + // + if (NetGetIpClass (Ip) > IP4_ADDR_CLASSC) { + return FALSE; + } + + return NetIp4IsUnicast (Ip, Netmask); +} diff --git a/NetworkPkg/Ip4Dxe/Ip4Common.h b/NetworkPkg/Ip4Dxe/Ip4Common.h new file mode 100644 index 000000000..8fbfd5487 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Common.h @@ -0,0 +1,217 @@ +/** @file + Common definition for IP4. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP4_COMMON_H__ +#define __EFI_IP4_COMMON_H__ + +typedef struct _IP4_INTERFACE IP4_INTERFACE; +typedef struct _IP4_PROTOCOL IP4_PROTOCOL; +typedef struct _IP4_SERVICE IP4_SERVICE; + +#define IP4_ETHER_PROTO 0x0800 + +// +// The packet is received as link level broadcast/multicast/promiscuous. +// +#define IP4_LINK_BROADCAST 0x00000001 +#define IP4_LINK_MULTICAST 0x00000002 +#define IP4_LINK_PROMISC 0x00000004 + +// +// IP4 address cast type classfication. Keep it true that any +// type bigger than or equal to LOCAL_BROADCAST is broadcast. +// +#define IP4_PROMISCUOUS 1 +#define IP4_LOCAL_HOST 2 +#define IP4_MULTICAST 3 +#define IP4_LOCAL_BROADCAST 4 // Destination is 255.255.255.255 +#define IP4_SUBNET_BROADCAST 5 +#define IP4_NET_BROADCAST 6 + +// +// IP4 header flags +// +#define IP4_HEAD_DF_MASK 0x4000 +#define IP4_HEAD_MF_MASK 0x2000 +#define IP4_HEAD_OFFSET_MASK 0x1fff + +#define IP4_ALLZERO_ADDRESS 0x00000000u +#define IP4_ALLONE_ADDRESS 0xFFFFFFFFu +#define IP4_ALLSYSTEM_ADDRESS 0xE0000001u +#define IP4_ALLROUTER_ADDRESS 0xE0000002u + +/// +/// Compose the fragment field to be used in the IP4 header. +/// +#define IP4_HEAD_FRAGMENT_FIELD(Df, Mf, Offset) \ + ((UINT16)(((Df) ? IP4_HEAD_DF_MASK : 0) | ((Mf) ? IP4_HEAD_MF_MASK : 0) | (((Offset) >> 3) & IP4_HEAD_OFFSET_MASK))) + +#define IP4_LAST_FRAGMENT(FragmentField) \ + (((FragmentField) & IP4_HEAD_MF_MASK) == 0) + +#define IP4_FIRST_FRAGMENT(FragmentField) \ + ((BOOLEAN)(((FragmentField) & IP4_HEAD_OFFSET_MASK) == 0)) + +#define IP4_DO_NOT_FRAGMENT(FragmentField) \ + ((BOOLEAN)(((FragmentField) & IP4_HEAD_DF_MASK) == IP4_HEAD_DF_MASK)) + +#define IP4_IS_BROADCAST(CastType) ((CastType) >= IP4_LOCAL_BROADCAST) + +/// +/// Conver the Microsecond to second. IP transmit/receive time is +/// in the unit of microsecond. IP ticks once per second. +/// +#define IP4_US_TO_SEC(Us) (((Us) + 999999) / 1000000) + +/** + Return the cast type (Unicast/Boradcast) specific to an + interface. All the addresses are host byte ordered. + + @param[in] IpAddr The IP address to classify in host byte order + @param[in] IpIf The interface that IpAddr received from + + @return The cast type of this IP address specific to the interface. + @retval IP4_LOCAL_HOST The IpAddr equals to the interface's address + @retval IP4_SUBNET_BROADCAST The IpAddr is a directed subnet boradcast to the + interface + @retval IP4_NET_BROADCAST The IpAddr is a network broadcast to the interface + @retval 0 Otherwise. + +**/ +INTN +Ip4GetNetCast ( + IN IP4_ADDR IpAddr, + IN IP4_INTERFACE *IpIf + ); + +/** + Find the cast type of the packet related to the local host. + This isn't the same as link layer cast type. For example, DHCP + server may send local broadcast to the local unicast MAC. + + @param[in] IpSb The IP4 service binding instance that received the + packet + @param[in] Dst The destination address in the packet (host byte + order) + @param[in] Src The source address in the packet (host byte order) + + @return The cast type for the Dst, it will return on the first non-promiscuous + cast type to a configured interface. If the packet doesn't match any of + the interface, multicast address and local broadcast address are checked. + +**/ +INTN +Ip4GetHostCast ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR Dst, + IN IP4_ADDR Src + ); + +/** + Find an interface whose configured IP address is Ip. + + @param[in] IpSb The IP4 service binding instance + @param[in] Ip The Ip address (host byte order) to find + + @return The IP4_INTERFACE point if found, otherwise NULL + +**/ +IP4_INTERFACE * +Ip4FindInterface ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR Ip + ); + +/** + Find an interface that Ip is on that connected network. + + @param[in] IpSb The IP4 service binding instance + @param[in] Ip The Ip address (host byte order) to find + + @return The IP4_INTERFACE point if found, otherwise NULL + +**/ +IP4_INTERFACE * +Ip4FindNet ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR Ip + ); + +/** + Find an interface of the service with the same Ip/Netmask pair. + + @param[in] IpSb Ip4 service binding instance + @param[in] Ip The Ip adress to find (host byte order) + @param[in] Netmask The network to find (host byte order) + + @return The IP4_INTERFACE point if found, otherwise NULL + +**/ +IP4_INTERFACE * +Ip4FindStationAddress ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR Ip, + IN IP4_ADDR Netmask + ); + +/** + Get the MAC address for a multicast IP address. Call + Mnp's McastIpToMac to find the MAC address in stead of + hard code the NIC to be Ethernet. + + @param[in] Mnp The Mnp instance to get the MAC address. + @param[in] Multicast The multicast IP address to translate. + @param[out] Mac The buffer to hold the translated address. + + @retval EFI_SUCCESS if the multicast IP is successfully translated to a + multicast MAC address. + @retval other Otherwise some error. + +**/ +EFI_STATUS +Ip4GetMulticastMac ( + IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp, + IN IP4_ADDR Multicast, + OUT EFI_MAC_ADDRESS *Mac + ); + +/** + Convert the multibyte field in IP header's byter order. + In spite of its name, it can also be used to convert from + host to network byte order. + + @param[in] Head The IP head to convert + + @return Point to the converted IP head + +**/ +IP4_HEAD * +Ip4NtohHead ( + IN IP4_HEAD *Head + ); + + +/** + Validate that Ip/Netmask pair is OK to be used as station + address. Only continuous netmasks are supported. and check + that StationAddress is a unicast address on the newtwork. + + @param[in] Ip The IP address to validate. + @param[in] Netmask The netmaks of the IP. + + @retval TRUE The Ip/Netmask pair is valid. + @retval FALSE The Ip/Netmask pair is invalid. + +**/ +BOOLEAN +Ip4StationAddressValid ( + IN IP4_ADDR Ip, + IN IP4_ADDR Netmask + ); + +#endif diff --git a/NetworkPkg/Ip4Dxe/Ip4Config2.vfr b/NetworkPkg/Ip4Dxe/Ip4Config2.vfr new file mode 100644 index 000000000..d85a99180 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Config2.vfr @@ -0,0 +1,94 @@ +/** @file + Vfr file for IP4Dxe. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include "Ip4NvData.h" + +#define EFI_NETWORK_DEVICE_CLASS 0x04 + +formset + guid = IP4_CONFIG2_NVDATA_GUID, + title = STRING_TOKEN(STR_IP4_CONFIG2_FORM_TITLE), + help = STRING_TOKEN(STR_IP4_CONFIG2_FORM_HELP), + class = EFI_NETWORK_DEVICE_CLASS, + subclass = 0x03, + + varstore IP4_CONFIG2_IFR_NVDATA, + name = IP4_CONFIG2_IFR_NVDATA, + guid = IP4_CONFIG2_NVDATA_GUID; + + form formid = FORMID_MAIN_FORM, + title = STRING_TOKEN(STR_IP4_DEVICE_FORM_TITLE); + + checkbox varid = IP4_CONFIG2_IFR_NVDATA.Configure, + prompt = STRING_TOKEN(STR_IP4_CONFIGURE), + help = STRING_TOKEN(STR_IP4_CONFIGURE_HELP), + flags = INTERACTIVE, + key = KEY_ENABLE, + endcheckbox; + + suppressif ideqval IP4_CONFIG2_IFR_NVDATA.Configure == 0x00; + + checkbox varid = IP4_CONFIG2_IFR_NVDATA.DhcpEnable, + prompt = STRING_TOKEN(STR_IP4_ENABLE_DHCP), + help = STRING_TOKEN(STR_IP4_ENABLE_DHCP), + flags = INTERACTIVE, + key = KEY_DHCP_ENABLE, + endcheckbox; + endif; + + suppressif ideqval IP4_CONFIG2_IFR_NVDATA.DhcpEnable == 0x01 OR ideqval IP4_CONFIG2_IFR_NVDATA.Configure == 0x00; + + string varid = IP4_CONFIG2_IFR_NVDATA.StationAddress, + prompt = STRING_TOKEN(STR_IP4_LOCAL_IP_ADDRESS), + help = STRING_TOKEN(STR_IP4_IP_ADDRESS_HELP), + flags = INTERACTIVE, + key = KEY_LOCAL_IP, + minsize = IP_MIN_SIZE, + maxsize = IP_MAX_SIZE, + endstring; + + string varid = IP4_CONFIG2_IFR_NVDATA.SubnetMask, + prompt = STRING_TOKEN(STR_IP4_LOCAL_MASK), + help = STRING_TOKEN(STR_IP4_MASK_HELP), + flags = INTERACTIVE, + key = KEY_SUBNET_MASK, + minsize = IP_MIN_SIZE, + maxsize = IP_MAX_SIZE, + endstring; + + string varid = IP4_CONFIG2_IFR_NVDATA.GatewayAddress, + prompt = STRING_TOKEN(STR_IP4_LOCAL_GATEWAY), + help = STRING_TOKEN(STR_IP4_GATEWAY_HELP), + flags = INTERACTIVE, + key = KEY_GATE_WAY, + minsize = IP_MIN_SIZE, + maxsize = IP_MAX_SIZE, + endstring; + + string varid = IP4_CONFIG2_IFR_NVDATA.DnsAddress, + prompt = STRING_TOKEN(STR_IP4_LOCAL_DNS), + help = STRING_TOKEN(STR_IP4_DNS_HELP), + flags = INTERACTIVE, + key = KEY_DNS, + minsize = IP_MIN_SIZE, + maxsize = ADDRESS_STR_MAX_SIZE, + endstring; + + endif; + + subtitle text = STRING_TOKEN(STR_NULL); + + text + help = STRING_TOKEN(STR_SAVE_CHANGES), + text = STRING_TOKEN(STR_SAVE_CHANGES), + flags = INTERACTIVE, + key = KEY_SAVE_CHANGES; + + endform; + +endformset; + diff --git a/NetworkPkg/Ip4Dxe/Ip4Config2Impl.c b/NetworkPkg/Ip4Dxe/Ip4Config2Impl.c new file mode 100644 index 000000000..9dca48ddd --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Config2Impl.c @@ -0,0 +1,2168 @@ +/** @file + The implementation of EFI IPv4 Configuration II Protocol. + + Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+ (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + +LIST_ENTRY mIp4Config2InstanceList = {&mIp4Config2InstanceList, &mIp4Config2InstanceList}; + +/** + The event process routine when the DHCPv4 service binding protocol is installed + in the system. + + @param[in] Event Not used. + @param[in] Context Pointer to the IP4 config2 instance data. + +**/ +VOID +EFIAPI +Ip4Config2OnDhcp4SbInstalled ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Destroy the Dhcp4 child in IP4_CONFIG2_INSTANCE and release the resources. + + @param[in, out] Instance The buffer of IP4 config2 instance to be freed. + + @retval EFI_SUCCESS The child was successfully destroyed. + @retval Others Failed to destroy the child. + +**/ +EFI_STATUS +Ip4Config2DestroyDhcp4 ( + IN OUT IP4_CONFIG2_INSTANCE *Instance + ) +{ + IP4_SERVICE *IpSb; + EFI_STATUS Status; + EFI_DHCP4_PROTOCOL *Dhcp4; + + Dhcp4 = Instance->Dhcp4; + ASSERT (Dhcp4 != NULL); + + Dhcp4->Stop (Dhcp4); + Dhcp4->Configure (Dhcp4, NULL); + Instance->Dhcp4 = NULL; + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + + // + // Close DHCPv4 protocol and destroy the child. + // + Status = gBS->CloseProtocol ( + Instance->Dhcp4Handle, + &gEfiDhcp4ProtocolGuid, + IpSb->Image, + IpSb->Controller + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = NetLibDestroyServiceChild ( + IpSb->Controller, + IpSb->Image, + &gEfiDhcp4ServiceBindingProtocolGuid, + Instance->Dhcp4Handle + ); + + Instance->Dhcp4Handle = NULL; + + return Status; +} + +/** + Update the current policy to NewPolicy. During the transition + period, the default router list + and address list in all interfaces will be released. + + @param[in] IpSb The IP4 service binding instance. + @param[in] NewPolicy The new policy to be updated to. + +**/ +VOID +Ip4Config2OnPolicyChanged ( + IN IP4_SERVICE *IpSb, + IN EFI_IP4_CONFIG2_POLICY NewPolicy + ) +{ + IP4_INTERFACE *IpIf; + IP4_ROUTE_TABLE *RouteTable; + + // + // Currently there are only two policies: static and dhcp. Regardless of + // what transition is going on, i.e., static -> dhcp and dhcp -> + // static, we have to free default router table and all addresses. + // + + if (IpSb->DefaultInterface != NULL) { + if (IpSb->DefaultRouteTable != NULL) { + Ip4FreeRouteTable (IpSb->DefaultRouteTable); + IpSb->DefaultRouteTable = NULL; + } + + Ip4CancelReceive (IpSb->DefaultInterface); + + Ip4FreeInterface (IpSb->DefaultInterface, NULL); + IpSb->DefaultInterface = NULL; + } + + Ip4CleanAssembleTable (&IpSb->Assemble); + + // + // Create new default interface and route table. + // + IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image); + if (IpIf == NULL) { + return ; + } + + RouteTable = Ip4CreateRouteTable (); + if (RouteTable == NULL) { + Ip4FreeInterface (IpIf, NULL); + return ; + } + + IpSb->DefaultInterface = IpIf; + InsertHeadList (&IpSb->Interfaces, &IpIf->Link); + IpSb->DefaultRouteTable = RouteTable; + Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb); + + if (IpSb->State == IP4_SERVICE_CONFIGED || IpSb->State == IP4_SERVICE_STARTED) { + IpSb->State = IP4_SERVICE_UNSTARTED; + } + + // + // Start the dhcp configuration. + // + if (NewPolicy == Ip4Config2PolicyDhcp) { + Ip4StartAutoConfig (&IpSb->Ip4Config2Instance); + } + +} + +/** + Signal the registered event. It is the callback routine for NetMapIterate. + + @param[in] Map Points to the list of registered event. + @param[in] Item The registered event. + @param[in] Arg Not used. + + @retval EFI_SUCCESS The event was signaled successfully. +**/ +EFI_STATUS +EFIAPI +Ip4Config2SignalEvent ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg + ) +{ + gBS->SignalEvent ((EFI_EVENT) Item->Key); + + return EFI_SUCCESS; +} + +/** + Read the configuration data from variable storage according to the VarName and + gEfiIp4Config2ProtocolGuid. It checks the integrity of variable data. If the + data is corrupted, it clears the variable data to ZERO. Othewise, it outputs the + configuration data to IP4_CONFIG2_INSTANCE. + + @param[in] VarName The pointer to the variable name + @param[in, out] Instance The pointer to the IP4 config2 instance data. + + @retval EFI_NOT_FOUND The variable can not be found or already corrupted. + @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation. + @retval EFI_SUCCESS The configuration data was retrieved successfully. + +**/ +EFI_STATUS +Ip4Config2ReadConfigData ( + IN CHAR16 *VarName, + IN OUT IP4_CONFIG2_INSTANCE *Instance + ) +{ + EFI_STATUS Status; + UINTN VarSize; + IP4_CONFIG2_VARIABLE *Variable; + IP4_CONFIG2_DATA_ITEM *DataItem; + UINTN Index; + IP4_CONFIG2_DATA_RECORD DataRecord; + CHAR8 *Data; + + // + // Try to read the configuration variable. + // + VarSize = 0; + Status = gRT->GetVariable ( + VarName, + &gEfiIp4Config2ProtocolGuid, + NULL, + &VarSize, + NULL + ); + + if (Status == EFI_BUFFER_TOO_SMALL) { + // + // Allocate buffer and read the config variable. + // + Variable = AllocatePool (VarSize); + if (Variable == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = gRT->GetVariable ( + VarName, + &gEfiIp4Config2ProtocolGuid, + NULL, + &VarSize, + Variable + ); + if (EFI_ERROR (Status) || (UINT16) (~NetblockChecksum ((UINT8 *) Variable, (UINT32) VarSize)) != 0) { + // + // GetVariable still error or the variable is corrupted. + // Fall back to the default value. + // + FreePool (Variable); + + // + // Remove the problematic variable and return EFI_NOT_FOUND, a new + // variable will be set again. + // + gRT->SetVariable ( + VarName, + &gEfiIp4Config2ProtocolGuid, + IP4_CONFIG2_VARIABLE_ATTRIBUTE, + 0, + NULL + ); + + return EFI_NOT_FOUND; + } + + + for (Index = 0; Index < Variable->DataRecordCount; Index++) { + + CopyMem (&DataRecord, &Variable->DataRecord[Index], sizeof (DataRecord)); + + DataItem = &Instance->DataItem[DataRecord.DataType]; + if (DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED) && + (DataItem->DataSize != DataRecord.DataSize) + ) { + // + // Perhaps a corrupted data record... + // + continue; + } + + if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED)) { + // + // This data item has variable length data. + // + DataItem->Data.Ptr = AllocatePool (DataRecord.DataSize); + if (DataItem->Data.Ptr == NULL) { + // + // no memory resource + // + continue; + } + } + + Data = (CHAR8 *) Variable + DataRecord.Offset; + CopyMem (DataItem->Data.Ptr, Data, DataRecord.DataSize); + + DataItem->DataSize = DataRecord.DataSize; + DataItem->Status = EFI_SUCCESS; + } + + FreePool (Variable); + return EFI_SUCCESS; + } + + return Status; +} + +/** + Write the configuration data from IP4_CONFIG2_INSTANCE to variable storage. + + @param[in] VarName The pointer to the variable name. + @param[in] Instance The pointer to the IP4 config2 instance data. + + @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation. + @retval EFI_SUCCESS The configuration data is written successfully. + +**/ +EFI_STATUS +Ip4Config2WriteConfigData ( + IN CHAR16 *VarName, + IN IP4_CONFIG2_INSTANCE *Instance + ) +{ + UINTN Index; + UINTN VarSize; + IP4_CONFIG2_DATA_ITEM *DataItem; + IP4_CONFIG2_VARIABLE *Variable; + IP4_CONFIG2_DATA_RECORD *DataRecord; + CHAR8 *Heap; + EFI_STATUS Status; + + VarSize = sizeof (IP4_CONFIG2_VARIABLE) - sizeof (IP4_CONFIG2_DATA_RECORD); + + for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) { + + DataItem = &Instance->DataItem[Index]; + if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_VOLATILE) && !EFI_ERROR (DataItem->Status)) { + + VarSize += sizeof (IP4_CONFIG2_DATA_RECORD) + DataItem->DataSize; + } + } + + Variable = AllocatePool (VarSize); + if (Variable == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Heap = (CHAR8 *) Variable + VarSize; + Variable->DataRecordCount = 0; + + for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) { + + DataItem = &Instance->DataItem[Index]; + if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_VOLATILE) && !EFI_ERROR (DataItem->Status)) { + + Heap -= DataItem->DataSize; + CopyMem (Heap, DataItem->Data.Ptr, DataItem->DataSize); + + DataRecord = &Variable->DataRecord[Variable->DataRecordCount]; + DataRecord->DataType = (EFI_IP4_CONFIG2_DATA_TYPE) Index; + DataRecord->DataSize = (UINT32) DataItem->DataSize; + DataRecord->Offset = (UINT16) (Heap - (CHAR8 *) Variable); + + Variable->DataRecordCount++; + } + } + + Variable->Checksum = 0; + Variable->Checksum = (UINT16) ~NetblockChecksum ((UINT8 *) Variable, (UINT32) VarSize); + + Status = gRT->SetVariable ( + VarName, + &gEfiIp4Config2ProtocolGuid, + IP4_CONFIG2_VARIABLE_ATTRIBUTE, + VarSize, + Variable + ); + + FreePool (Variable); + + return Status; +} + + +/** + Build a EFI_IP4_ROUTE_TABLE to be returned to the caller of GetModeData. + The EFI_IP4_ROUTE_TABLE is clumsy to use in the internal operation of the + IP4 driver. + + @param[in] IpSb The IP4 service binding instance. + @param[out] Table The built IP4 route table. + + @retval EFI_SUCCESS The route table is successfully build + @retval EFI_NOT_FOUND Failed to allocate the memory for the rotue table. + +**/ +EFI_STATUS +Ip4Config2BuildDefaultRouteTable ( + IN IP4_SERVICE *IpSb, + OUT EFI_IP4_ROUTE_TABLE *Table + ) +{ + LIST_ENTRY *Entry; + IP4_ROUTE_ENTRY *RtEntry; + UINT32 Count; + INT32 Index; + + if (IpSb->DefaultRouteTable == NULL) { + return EFI_NOT_FOUND; + } + + Count = IpSb->DefaultRouteTable->TotalNum; + + if (Count == 0) { + return EFI_NOT_FOUND; + } + + // + // Copy the route entry to EFI route table. Keep the order of + // route entry copied from most specific to default route. That + // is, interlevel the route entry from the instance's route area + // and those from the default route table's route area. + // + Count = 0; + + for (Index = IP4_MASK_MAX; Index >= 0; Index--) { + + NET_LIST_FOR_EACH (Entry, &(IpSb->DefaultRouteTable->RouteArea[Index])) { + RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link); + + EFI_IP4 (Table[Count].SubnetAddress) = HTONL (RtEntry->Dest & RtEntry->Netmask); + EFI_IP4 (Table[Count].SubnetMask) = HTONL (RtEntry->Netmask); + EFI_IP4 (Table[Count].GatewayAddress) = HTONL (RtEntry->NextHop); + + Count++; + } + + } + + return EFI_SUCCESS; +} + +/** + The event process routine when the DHCPv4 service binding protocol is installed + in the system. + + @param[in] Event Not used. + @param[in] Context The pointer to the IP4 config2 instance data. + +**/ +VOID +EFIAPI +Ip4Config2OnDhcp4SbInstalled ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + IP4_CONFIG2_INSTANCE *Instance; + + Instance = (IP4_CONFIG2_INSTANCE *) Context; + + if ((Instance->Dhcp4Handle != NULL) || (Instance->Policy != Ip4Config2PolicyDhcp)) { + // + // The DHCP4 child is already created or the policy is no longer DHCP. + // + return ; + } + + Ip4StartAutoConfig (Instance); +} + +/** + Set the station address and subnetmask for the default interface. + + @param[in] IpSb The pointer to the IP4 service binding instance. + @param[in] StationAddress Ip address to be set. + @param[in] SubnetMask Subnet to be set. + + @retval EFI_SUCCESS Set default address successful. + @retval Others Some errors occur in setting. + +**/ +EFI_STATUS +Ip4Config2SetDefaultAddr ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR StationAddress, + IN IP4_ADDR SubnetMask + ) +{ + EFI_STATUS Status; + IP4_INTERFACE *IpIf; + IP4_PROTOCOL *Ip4Instance; + EFI_ARP_PROTOCOL *Arp; + LIST_ENTRY *Entry; + IP4_ADDR Subnet; + IP4_ROUTE_TABLE *RouteTable; + + IpIf = IpSb->DefaultInterface; + ASSERT (IpIf != NULL); + + if ((IpIf->Ip == StationAddress) && (IpIf->SubnetMask == SubnetMask)) { + IpSb->State = IP4_SERVICE_CONFIGED; + return EFI_SUCCESS; + } + + if (IpSb->Reconfig) { + // + // The default address is changed, free the previous interface first. + // + if (IpSb->DefaultRouteTable != NULL) { + Ip4FreeRouteTable (IpSb->DefaultRouteTable); + IpSb->DefaultRouteTable = NULL; + } + + Ip4CancelReceive (IpSb->DefaultInterface); + Ip4FreeInterface (IpSb->DefaultInterface, NULL); + IpSb->DefaultInterface = NULL; + // + // Create new default interface and route table. + // + IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image); + if (IpIf == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + RouteTable = Ip4CreateRouteTable (); + if (RouteTable == NULL) { + Ip4FreeInterface (IpIf, NULL); + return EFI_OUT_OF_RESOURCES; + } + + IpSb->DefaultInterface = IpIf; + InsertHeadList (&IpSb->Interfaces, &IpIf->Link); + IpSb->DefaultRouteTable = RouteTable; + Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb); + } + + if (IpSb->State == IP4_SERVICE_CONFIGED) { + IpSb->State = IP4_SERVICE_UNSTARTED; + } + + Status = Ip4SetAddress (IpIf, StationAddress, SubnetMask); + if (EFI_ERROR (Status)) { + return Status; + } + + if (IpIf->Arp != NULL) { + // + // A non-NULL IpIf->Arp here means a new ARP child is created when setting default address, + // but some IP children may have referenced the default interface before it is configured, + // these IP instances also consume this ARP protocol so they need to open it BY_CHILD_CONTROLLER. + // + Arp = NULL; + NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) { + Ip4Instance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, AddrLink, IP4_PROTOCOL_SIGNATURE); + Status = gBS->OpenProtocol ( + IpIf->ArpHandle, + &gEfiArpProtocolGuid, + (VOID **) &Arp, + gIp4DriverBinding.DriverBindingHandle, + Ip4Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + } + + // + // Add a route for the connected network. + // + Subnet = StationAddress & SubnetMask; + + Ip4AddRoute ( + IpSb->DefaultRouteTable, + Subnet, + SubnetMask, + IP4_ALLZERO_ADDRESS + ); + + IpSb->State = IP4_SERVICE_CONFIGED; + IpSb->Reconfig = FALSE; + + return EFI_SUCCESS; +} + +/** + Set the station address, subnetmask and gateway address for the default interface. + + @param[in] Instance The pointer to the IP4 config2 instance data. + @param[in] StationAddress Ip address to be set. + @param[in] SubnetMask Subnet to be set. + @param[in] GatewayAddress Gateway to be set. + + @retval EFI_SUCCESS Set default If successful. + @retval Others Errors occur as indicated. + +**/ +EFI_STATUS +Ip4Config2SetDefaultIf ( + IN IP4_CONFIG2_INSTANCE *Instance, + IN IP4_ADDR StationAddress, + IN IP4_ADDR SubnetMask, + IN IP4_ADDR GatewayAddress + ) +{ + EFI_STATUS Status; + IP4_SERVICE *IpSb; + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + + // + // Check whether the StationAddress/SubnetMask pair is valid. + // + if (!Ip4StationAddressValid (StationAddress, SubnetMask)) { + return EFI_INVALID_PARAMETER; + } + + Status = Ip4Config2SetDefaultAddr (IpSb, StationAddress, SubnetMask); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Create a route if there is a default router. + // + if (GatewayAddress != IP4_ALLZERO_ADDRESS) { + Ip4AddRoute ( + IpSb->DefaultRouteTable, + IP4_ALLZERO_ADDRESS, + IP4_ALLZERO_ADDRESS, + GatewayAddress + ); + } + + return EFI_SUCCESS; +} + + +/** + Release all the DHCP related resources. + + @param Instance The IP4 config2 instance. + + @return None + +**/ +VOID +Ip4Config2CleanDhcp4 ( + IN IP4_CONFIG2_INSTANCE *Instance + ) +{ + IP4_SERVICE *IpSb; + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + + if (Instance->Dhcp4 != NULL) { + Instance->Dhcp4->Stop (Instance->Dhcp4); + + gBS->CloseProtocol ( + Instance->Dhcp4Handle, + &gEfiDhcp4ProtocolGuid, + IpSb->Image, + IpSb->Controller + ); + + Instance->Dhcp4 = NULL; + } + + if (Instance->Dhcp4Handle != NULL) { + NetLibDestroyServiceChild ( + IpSb->Controller, + IpSb->Image, + &gEfiDhcp4ServiceBindingProtocolGuid, + Instance->Dhcp4Handle + ); + + Instance->Dhcp4Handle = NULL; + } + + if (Instance->Dhcp4Event != NULL) { + gBS->CloseEvent (Instance->Dhcp4Event); + Instance->Dhcp4Event = NULL; + } +} + +/** + This worker function sets the DNS server list for the EFI IPv4 network + stack running on the communication device that this EFI_IP4_CONFIG2_PROTOCOL + manages. The DNS server addresses must be unicast IPv4 addresses. + + @param[in] Instance The pointer to the IP4 config2 instance data. + @param[in] DataSize The size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set, points to an array of + EFI_IPv4_ADDRESS instances. + + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type. + @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation. + @retval EFI_ABORTED The DNS server addresses to be set equal the current + configuration. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv4 + network stack was set. + +**/ +EFI_STATUS +Ip4Config2SetDnsServerWorker ( + IN IP4_CONFIG2_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ) +{ + UINTN OldIndex; + UINTN NewIndex; + EFI_IPv4_ADDRESS *OldDns; + EFI_IPv4_ADDRESS *NewDns; + UINTN OldDnsCount; + UINTN NewDnsCount; + IP4_CONFIG2_DATA_ITEM *Item; + BOOLEAN OneAdded; + VOID *Tmp; + IP4_ADDR DnsAddress; + + if ((DataSize % sizeof (EFI_IPv4_ADDRESS) != 0) || (DataSize == 0)) { + return EFI_BAD_BUFFER_SIZE; + } + + Item = &Instance->DataItem[Ip4Config2DataTypeDnsServer]; + NewDns = (EFI_IPv4_ADDRESS *) Data; + OldDns = Item->Data.DnsServers; + NewDnsCount = DataSize / sizeof (EFI_IPv4_ADDRESS); + OldDnsCount = Item->DataSize / sizeof (EFI_IPv4_ADDRESS); + OneAdded = FALSE; + + if (NewDnsCount != OldDnsCount) { + Tmp = AllocatePool (DataSize); + if (Tmp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } else { + Tmp = NULL; + } + + for (NewIndex = 0; NewIndex < NewDnsCount; NewIndex++) { + CopyMem (&DnsAddress, NewDns + NewIndex, sizeof (IP4_ADDR)); + if (IP4_IS_UNSPECIFIED (NTOHL (DnsAddress)) || IP4_IS_LOCAL_BROADCAST (NTOHL (DnsAddress))) { + // + // The dns server address must be unicast. + // + if (Tmp != NULL) { + FreePool (Tmp); + } + return EFI_INVALID_PARAMETER; + } + + if (OneAdded) { + // + // If any address in the new setting is not in the old settings, skip the + // comparision below. + // + continue; + } + + for (OldIndex = 0; OldIndex < OldDnsCount; OldIndex++) { + if (EFI_IP4_EQUAL (NewDns + NewIndex, OldDns + OldIndex)) { + // + // If found break out. + // + break; + } + } + + if (OldIndex == OldDnsCount) { + OneAdded = TRUE; + } + } + + if (!OneAdded && (DataSize == Item->DataSize)) { + // + // No new item is added and the size is the same. + // + Item->Status = EFI_SUCCESS; + return EFI_ABORTED; + } else { + if (Tmp != NULL) { + if (Item->Data.Ptr != NULL) { + FreePool (Item->Data.Ptr); + } + Item->Data.Ptr = Tmp; + } + + CopyMem (Item->Data.Ptr, Data, DataSize); + Item->DataSize = DataSize; + Item->Status = EFI_SUCCESS; + return EFI_SUCCESS; + } +} + + + +/** + Callback function when DHCP process finished. It will save the + retrieved IP configure parameter from DHCP to the NVRam. + + @param Event The callback event + @param Context Opaque context to the callback + + @return None + +**/ +VOID +EFIAPI +Ip4Config2OnDhcp4Complete ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + IP4_CONFIG2_INSTANCE *Instance; + EFI_DHCP4_MODE_DATA Dhcp4Mode; + EFI_STATUS Status; + IP4_ADDR StationAddress; + IP4_ADDR SubnetMask; + IP4_ADDR GatewayAddress; + UINT32 Index; + UINT32 OptionCount; + EFI_DHCP4_PACKET_OPTION **OptionList; + + Instance = (IP4_CONFIG2_INSTANCE *) Context; + ASSERT (Instance->Dhcp4 != NULL); + + // + // Get the DHCP retrieved parameters + // + Status = Instance->Dhcp4->GetModeData (Instance->Dhcp4, &Dhcp4Mode); + + if (EFI_ERROR (Status)) { + goto Exit; + } + + if (Dhcp4Mode.State == Dhcp4Bound) { + StationAddress = EFI_NTOHL (Dhcp4Mode.ClientAddress); + SubnetMask = EFI_NTOHL (Dhcp4Mode.SubnetMask); + GatewayAddress = EFI_NTOHL (Dhcp4Mode.RouterAddress); + + Status = Ip4Config2SetDefaultIf (Instance, StationAddress, SubnetMask, GatewayAddress); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Parse the ACK to get required DNS server information. + // + OptionCount = 0; + OptionList = NULL; + + Status = Instance->Dhcp4->Parse (Instance->Dhcp4, Dhcp4Mode.ReplyPacket, &OptionCount, OptionList); + if (Status != EFI_BUFFER_TOO_SMALL) { + goto Exit; + } + + OptionList = AllocateZeroPool (OptionCount * sizeof (EFI_DHCP4_PACKET_OPTION *)); + if (OptionList == NULL) { + goto Exit; + } + + Status = Instance->Dhcp4->Parse (Instance->Dhcp4, Dhcp4Mode.ReplyPacket, &OptionCount, OptionList); + if (EFI_ERROR (Status)) { + FreePool (OptionList); + goto Exit; + } + + for (Index = 0; Index < OptionCount; Index++) { + // + // Look for DNS Server opcode (6). + // + if (OptionList[Index]->OpCode == DHCP4_TAG_DNS_SERVER) { + if (((OptionList[Index]->Length & 0x3) != 0) || (OptionList[Index]->Length == 0)) { + break; + } + + Ip4Config2SetDnsServerWorker (Instance, OptionList[Index]->Length, &OptionList[Index]->Data[0]); + break; + } + } + + FreePool (OptionList); + + Instance->DhcpSuccess = TRUE; + } + +Exit: + Ip4Config2CleanDhcp4 (Instance); + DispatchDpc (); +} + + +/** + Start the DHCP configuration for this IP service instance. + It will locates the EFI_IP4_CONFIG2_PROTOCOL, then start the + DHCP configuration. + + @param[in] Instance The IP4 config2 instance to configure + + @retval EFI_SUCCESS The auto configuration is successfully started + @retval Others Failed to start auto configuration. + +**/ +EFI_STATUS +Ip4StartAutoConfig ( + IN IP4_CONFIG2_INSTANCE *Instance + ) +{ + IP4_SERVICE *IpSb; + EFI_DHCP4_PROTOCOL *Dhcp4; + EFI_DHCP4_MODE_DATA Dhcp4Mode; + EFI_DHCP4_PACKET_OPTION *OptionList[1]; + IP4_CONFIG2_DHCP4_OPTION ParaList; + EFI_STATUS Status; + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + + if (IpSb->State > IP4_SERVICE_UNSTARTED) { + return EFI_SUCCESS; + } + + // + // A host must not invoke DHCP configuration if it is already + // participating in the DHCP configuraiton process. + // + if (Instance->Dhcp4Handle != NULL) { + return EFI_SUCCESS; + } + + Status = NetLibCreateServiceChild ( + IpSb->Controller, + IpSb->Image, + &gEfiDhcp4ServiceBindingProtocolGuid, + &Instance->Dhcp4Handle + ); + + if (Status == EFI_UNSUPPORTED) { + // + // No DHCPv4 Service Binding protocol, register a notify. + // + if (Instance->Dhcp4SbNotifyEvent == NULL) { + Instance->Dhcp4SbNotifyEvent = EfiCreateProtocolNotifyEvent ( + &gEfiDhcp4ServiceBindingProtocolGuid, + TPL_CALLBACK, + Ip4Config2OnDhcp4SbInstalled, + (VOID *) Instance, + &Instance->Registration + ); + } + } + + if (EFI_ERROR (Status)) { + return Status; + } + + if (Instance->Dhcp4SbNotifyEvent != NULL) { + gBS->CloseEvent (Instance->Dhcp4SbNotifyEvent); + } + + Status = gBS->OpenProtocol ( + Instance->Dhcp4Handle, + &gEfiDhcp4ProtocolGuid, + (VOID **) &Instance->Dhcp4, + IpSb->Image, + IpSb->Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + NetLibDestroyServiceChild ( + IpSb->Controller, + IpSb->Image, + &gEfiDhcp4ServiceBindingProtocolGuid, + Instance->Dhcp4Handle + ); + + Instance->Dhcp4Handle = NULL; + + return Status; + } + + // + // Check the current DHCP status, if the DHCP process has + // already finished, return now. + // + Dhcp4 = Instance->Dhcp4; + Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4Mode); + if (Dhcp4Mode.State == Dhcp4Bound) { + Ip4Config2OnDhcp4Complete (NULL, Instance); + + return EFI_SUCCESS; + } + + // + // Try to start the DHCP process. Use most of the current + // DHCP configuration to avoid problems if some DHCP client + // yields the control of this DHCP service to us. + // + ParaList.Head.OpCode = DHCP4_TAG_PARA_LIST; + ParaList.Head.Length = 3; + ParaList.Head.Data[0] = DHCP4_TAG_NETMASK; + ParaList.Route = DHCP4_TAG_ROUTER; + ParaList.Dns = DHCP4_TAG_DNS_SERVER; + OptionList[0] = &ParaList.Head; + Dhcp4Mode.ConfigData.OptionCount = 1; + Dhcp4Mode.ConfigData.OptionList = OptionList; + + Status = Dhcp4->Configure (Dhcp4, &Dhcp4Mode.ConfigData); + if (EFI_ERROR (Status)) { + gBS->CloseProtocol ( + Instance->Dhcp4Handle, + &gEfiDhcp4ProtocolGuid, + IpSb->Image, + IpSb->Controller + ); + + NetLibDestroyServiceChild ( + IpSb->Controller, + IpSb->Image, + &gEfiDhcp4ServiceBindingProtocolGuid, + Instance->Dhcp4Handle + ); + + Instance->Dhcp4 = NULL; + + Instance->Dhcp4Handle = NULL; + + return Status; + } + + // + // Start the DHCP process + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + Ip4Config2OnDhcp4Complete, + Instance, + &Instance->Dhcp4Event + ); + if (EFI_ERROR (Status)) { + Ip4Config2DestroyDhcp4 (Instance); + return Status; + } + + Status = Dhcp4->Start (Dhcp4, Instance->Dhcp4Event); + if (EFI_ERROR (Status)) { + Ip4Config2DestroyDhcp4 (Instance); + gBS->CloseEvent (Instance->Dhcp4Event); + Instance->Dhcp4Event = NULL; + + return Status; + } + + IpSb->State = IP4_SERVICE_STARTED; + DispatchDpc (); + + return EFI_SUCCESS; +} + + + +/** + The work function is to get the interface information of the communication + device this IP4_CONFIG2_INSTANCE manages. + + @param[in] Instance Pointer to the IP4 config2 instance data. + @param[in, out] DataSize On input, in bytes, the size of Data. On output, in + bytes, the size of buffer required to store the specified + configuration data. + @param[in] Data The data buffer in which the configuration data is returned. + Ignored if DataSize is ZERO. + + @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified + configuration data, and the required size is + returned in DataSize. + @retval EFI_SUCCESS The specified configuration data was obtained. + +**/ +EFI_STATUS +Ip4Config2GetIfInfo ( + IN IP4_CONFIG2_INSTANCE *Instance, + IN OUT UINTN *DataSize, + IN VOID *Data OPTIONAL + ) +{ + IP4_SERVICE *IpSb; + UINTN Length; + IP4_CONFIG2_DATA_ITEM *Item; + EFI_IP4_CONFIG2_INTERFACE_INFO *IfInfo; + IP4_ADDR Address; + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + Length = sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO); + + if (IpSb->DefaultRouteTable != NULL) { + Length += IpSb->DefaultRouteTable->TotalNum * sizeof (EFI_IP4_ROUTE_TABLE); + } + + if (*DataSize < Length) { + *DataSize = Length; + return EFI_BUFFER_TOO_SMALL; + } + + // + // Copy the fixed size part of the interface info. + // + Item = &Instance->DataItem[Ip4Config2DataTypeInterfaceInfo]; + IfInfo = (EFI_IP4_CONFIG2_INTERFACE_INFO *) Data; + CopyMem (IfInfo, Item->Data.Ptr, sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO)); + + // + // Update the address info. + // + if (IpSb->DefaultInterface != NULL) { + Address = HTONL (IpSb->DefaultInterface->Ip); + CopyMem (&IfInfo->StationAddress, &Address, sizeof (EFI_IPv4_ADDRESS)); + Address = HTONL (IpSb->DefaultInterface->SubnetMask); + CopyMem (&IfInfo->SubnetMask, &Address, sizeof (EFI_IPv4_ADDRESS)); + } + + if (IpSb->DefaultRouteTable != NULL) { + IfInfo->RouteTableSize = IpSb->DefaultRouteTable->TotalNum; + IfInfo->RouteTable = (EFI_IP4_ROUTE_TABLE *) ((UINT8 *) Data + sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO)); + + Ip4Config2BuildDefaultRouteTable (IpSb, IfInfo->RouteTable); + } + + return EFI_SUCCESS; +} + +/** + The work function is to set the general configuration policy for the EFI IPv4 network + stack that is running on the communication device managed by this IP4_CONFIG2_INSTANCE. + The policy will affect other configuration settings. + + @param[in] Instance Pointer to the IP4 config2 instance data. + @param[in] DataSize Size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set. + + @retval EFI_INVALID_PARAMETER The to be set policy is invalid. + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type. + @retval EFI_ABORTED The new policy equals the current policy. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set. + +**/ +EFI_STATUS +Ip4Config2SetPolicy ( + IN IP4_CONFIG2_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_IP4_CONFIG2_POLICY NewPolicy; + IP4_CONFIG2_DATA_ITEM *DataItem; + IP4_SERVICE *IpSb; + + if (DataSize != sizeof (EFI_IP4_CONFIG2_POLICY)) { + return EFI_BAD_BUFFER_SIZE; + } + + NewPolicy = *((EFI_IP4_CONFIG2_POLICY *) Data); + + if (NewPolicy >= Ip4Config2PolicyMax) { + return EFI_INVALID_PARAMETER; + } + + if (NewPolicy == Instance->Policy) { + if (NewPolicy != Ip4Config2PolicyDhcp || Instance->DhcpSuccess) { + return EFI_ABORTED; + } + } else { + // + // The policy is changed. Clean the ManualAddress, Gateway and DnsServers, + // shrink the variable data size, and fire up all the related events. + // + DataItem = &Instance->DataItem[Ip4Config2DataTypeManualAddress]; + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = NULL; + DataItem->DataSize = 0; + DataItem->Status = EFI_NOT_FOUND; + NetMapIterate (&DataItem->EventMap, Ip4Config2SignalEvent, NULL); + + DataItem = &Instance->DataItem[Ip4Config2DataTypeGateway]; + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = NULL; + DataItem->DataSize = 0; + DataItem->Status = EFI_NOT_FOUND; + NetMapIterate (&DataItem->EventMap, Ip4Config2SignalEvent, NULL); + + DataItem = &Instance->DataItem[Ip4Config2DataTypeDnsServer]; + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = NULL; + DataItem->DataSize = 0; + DataItem->Status = EFI_NOT_FOUND; + NetMapIterate (&DataItem->EventMap, Ip4Config2SignalEvent, NULL); + + if (NewPolicy == Ip4Config2PolicyDhcp) { + SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_VOLATILE); + } else { + // + // The policy is changed from dhcp to static. Stop the DHCPv4 process + // and destroy the DHCPv4 child. + // + if (Instance->Dhcp4Handle != NULL) { + Ip4Config2DestroyDhcp4 (Instance); + } + + // + // Close the event. + // + if (Instance->Dhcp4Event != NULL) { + gBS->CloseEvent (Instance->Dhcp4Event); + Instance->Dhcp4Event = NULL; + } + } + } + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + Ip4Config2OnPolicyChanged (IpSb, NewPolicy); + + Instance->Policy = NewPolicy; + + return EFI_SUCCESS; +} + +/** + The work function is to set the station addresses manually for the EFI IPv4 + network stack. It is only configurable when the policy is Ip4Config2PolicyStatic. + + @param[in] Instance Pointer to the IP4 config2 instance data. + @param[in] DataSize Size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set. + + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type. + @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set + under the current policy. + @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid. + @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation. + @retval EFI_NOT_READY An asynchrous process is invoked to set the specified + configuration data, and the process is not finished. + @retval EFI_ABORTED The manual addresses to be set equal current + configuration. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set. + +**/ +EFI_STATUS +Ip4Config2SetManualAddress ( + IN IP4_CONFIG2_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_IP4_CONFIG2_MANUAL_ADDRESS NewAddress; + IP4_CONFIG2_DATA_ITEM *DataItem; + EFI_STATUS Status; + IP4_ADDR StationAddress; + IP4_ADDR SubnetMask; + VOID *Ptr; + IP4_SERVICE *IpSb; + IP4_INTERFACE *IpIf; + IP4_ROUTE_TABLE *RouteTable; + + DataItem = NULL; + Status = EFI_SUCCESS; + Ptr = NULL; + IpIf = NULL; + RouteTable = NULL; + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + + ASSERT (Instance->DataItem[Ip4Config2DataTypeManualAddress].Status != EFI_NOT_READY); + + if ((DataSize != 0) && ((DataSize % sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS)) != 0)) { + return EFI_BAD_BUFFER_SIZE; + } + + if (Instance->Policy != Ip4Config2PolicyStatic) { + return EFI_WRITE_PROTECTED; + } + + DataItem = &Instance->DataItem[Ip4Config2DataTypeManualAddress]; + + if (Data != NULL && DataSize != 0) { + NewAddress = *((EFI_IP4_CONFIG2_MANUAL_ADDRESS *) Data); + + StationAddress = EFI_NTOHL (NewAddress.Address); + SubnetMask = EFI_NTOHL (NewAddress.SubnetMask); + + // + // Check whether the StationAddress/SubnetMask pair is valid. + // + if (!Ip4StationAddressValid (StationAddress, SubnetMask)) { + return EFI_INVALID_PARAMETER; + } + + // + // Store the new data, and init the DataItem status to EFI_NOT_READY because + // we may have an asynchronous configuration process. + // + Ptr = AllocateCopyPool (DataSize, Data); + if (Ptr == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + + DataItem->Data.Ptr = Ptr; + DataItem->DataSize = DataSize; + DataItem->Status = EFI_NOT_READY; + + IpSb->Reconfig = TRUE; + Status = Ip4Config2SetDefaultAddr (IpSb, StationAddress, SubnetMask); + + DataItem->Status = Status; + + if (EFI_ERROR (DataItem->Status) && DataItem->Status != EFI_NOT_READY) { + if (Ptr != NULL) { + FreePool (Ptr); + } + DataItem->Data.Ptr = NULL; + } + } else { + // + // DataSize is 0 and Data is NULL, clean up the manual address. + // + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = NULL; + DataItem->DataSize = 0; + DataItem->Status = EFI_NOT_FOUND; + + // + // Free the default router table and Interface, clean up the assemble table. + // + if (IpSb->DefaultInterface != NULL) { + if (IpSb->DefaultRouteTable != NULL) { + Ip4FreeRouteTable (IpSb->DefaultRouteTable); + IpSb->DefaultRouteTable = NULL; + } + + Ip4CancelReceive (IpSb->DefaultInterface); + + Ip4FreeInterface (IpSb->DefaultInterface, NULL); + IpSb->DefaultInterface = NULL; + } + + Ip4CleanAssembleTable (&IpSb->Assemble); + + // + // Create new default interface and route table. + // + IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image); + if (IpIf == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + RouteTable = Ip4CreateRouteTable (); + if (RouteTable == NULL) { + Ip4FreeInterface (IpIf, NULL); + return EFI_OUT_OF_RESOURCES; + } + + IpSb->DefaultInterface = IpIf; + InsertHeadList (&IpSb->Interfaces, &IpIf->Link); + IpSb->DefaultRouteTable = RouteTable; + Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb); + + // + // Reset the State to unstarted. + // + if (IpSb->State == IP4_SERVICE_CONFIGED || IpSb->State == IP4_SERVICE_STARTED) { + IpSb->State = IP4_SERVICE_UNSTARTED; + } + } + + return Status; +} + +/** + The work function is to set the gateway addresses manually for the EFI IPv4 + network stack that is running on the communication device that this EFI IPv4 + Configuration Protocol manages. It is not configurable when the policy is + Ip4Config2PolicyDhcp. The gateway addresses must be unicast IPv4 addresses. + + @param[in] Instance The pointer to the IP4 config2 instance data. + @param[in] DataSize The size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set. This points to an array of + EFI_IPv6_ADDRESS instances. + + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type. + @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set + under the current policy. + @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to complete the operation. + @retval EFI_ABORTED The manual gateway addresses to be set equal the + current configuration. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set. + +**/ +EFI_STATUS +Ip4Config2SetGateway ( + IN IP4_CONFIG2_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ) +{ + IP4_SERVICE *IpSb; + IP4_CONFIG2_DATA_ITEM *DataItem; + IP4_ADDR Gateway; + + UINTN Index1; + UINTN Index2; + EFI_IPv4_ADDRESS *OldGateway; + EFI_IPv4_ADDRESS *NewGateway; + UINTN OldGatewayCount; + UINTN NewGatewayCount; + BOOLEAN OneRemoved; + BOOLEAN OneAdded; + VOID *Tmp; + + OldGateway = NULL; + NewGateway = NULL; + OneRemoved = FALSE; + OneAdded = FALSE; + Tmp = NULL; + + if ((DataSize != 0) && (DataSize % sizeof (EFI_IPv4_ADDRESS) != 0)) { + return EFI_BAD_BUFFER_SIZE; + } + + if (Instance->Policy != Ip4Config2PolicyStatic) { + return EFI_WRITE_PROTECTED; + } + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + + DataItem = &Instance->DataItem[Ip4Config2DataTypeGateway]; + OldGateway = DataItem->Data.Gateway; + OldGatewayCount = DataItem->DataSize / sizeof (EFI_IPv4_ADDRESS); + + for (Index1 = 0; Index1 < OldGatewayCount; Index1++) { + // + // Remove the old route entry. + // + CopyMem (&Gateway, OldGateway + Index1, sizeof (IP4_ADDR)); + Ip4DelRoute ( + IpSb->DefaultRouteTable, + IP4_ALLZERO_ADDRESS, + IP4_ALLZERO_ADDRESS, + NTOHL (Gateway) + ); + OneRemoved = TRUE; + } + + if (Data != NULL && DataSize != 0) { + NewGateway = (EFI_IPv4_ADDRESS *) Data; + NewGatewayCount = DataSize / sizeof (EFI_IPv4_ADDRESS); + for (Index1 = 0; Index1 < NewGatewayCount; Index1++) { + CopyMem (&Gateway, NewGateway + Index1, sizeof (IP4_ADDR)); + + if ((IpSb->DefaultInterface->SubnetMask != 0) && + !NetIp4IsUnicast (NTOHL (Gateway), IpSb->DefaultInterface->SubnetMask)) { + return EFI_INVALID_PARAMETER; + } + + for (Index2 = Index1 + 1; Index2 < NewGatewayCount; Index2++) { + if (EFI_IP4_EQUAL (NewGateway + Index1, NewGateway + Index2)) { + return EFI_INVALID_PARAMETER; + } + } + } + + if (NewGatewayCount != OldGatewayCount) { + Tmp = AllocatePool (DataSize); + if (Tmp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } else { + Tmp = NULL; + } + + for (Index1 = 0; Index1 < NewGatewayCount; Index1++) { + // + // Add the new route entry. + // + CopyMem (&Gateway, NewGateway + Index1, sizeof (IP4_ADDR)); + Ip4AddRoute ( + IpSb->DefaultRouteTable, + IP4_ALLZERO_ADDRESS, + IP4_ALLZERO_ADDRESS, + NTOHL (Gateway) + ); + + OneAdded = TRUE; + } + + if (!OneRemoved && !OneAdded) { + DataItem->Status = EFI_SUCCESS; + return EFI_ABORTED; + } else { + if (Tmp != NULL) { + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = Tmp; + } + + CopyMem (DataItem->Data.Ptr, Data, DataSize); + DataItem->DataSize = DataSize; + DataItem->Status = EFI_SUCCESS; + } + } else { + // + // DataSize is 0 and Data is NULL, clean up the Gateway address. + // + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = NULL; + DataItem->DataSize = 0; + DataItem->Status = EFI_NOT_FOUND; + } + + return EFI_SUCCESS; +} + +/** + The work function is to set the DNS server list for the EFI IPv4 network + stack running on the communication device that this EFI_IP4_CONFIG2_PROTOCOL + manages. It is not configurable when the policy is Ip4Config2PolicyDhcp. + The DNS server addresses must be unicast IPv4 addresses. + + @param[in] Instance The pointer to the IP4 config2 instance data. + @param[in] DataSize The size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set, points to an array of + EFI_IPv4_ADDRESS instances. + + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type. + @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set + under the current policy. + @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation. + @retval EFI_ABORTED The DNS server addresses to be set equal the current + configuration. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv4 + network stack was set. + +**/ +EFI_STATUS +Ip4Config2SetDnsServer ( + IN IP4_CONFIG2_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_STATUS Status; + IP4_CONFIG2_DATA_ITEM *Item; + + Status = EFI_SUCCESS; + Item = NULL; + + if (Instance->Policy != Ip4Config2PolicyStatic) { + return EFI_WRITE_PROTECTED; + } + + Item = &Instance->DataItem[Ip4Config2DataTypeDnsServer]; + + if (DATA_ATTRIB_SET (Item->Attribute, DATA_ATTRIB_VOLATILE)) { + REMOVE_DATA_ATTRIB (Item->Attribute, DATA_ATTRIB_VOLATILE); + } + + if (Data != NULL && DataSize != 0) { + Status = Ip4Config2SetDnsServerWorker (Instance, DataSize, Data); + } else { + // + // DataSize is 0 and Data is NULL, clean up the DnsServer address. + // + if (Item->Data.Ptr != NULL) { + FreePool (Item->Data.Ptr); + } + Item->Data.Ptr = NULL; + Item->DataSize = 0; + Item->Status = EFI_NOT_FOUND; + } + + return Status; +} + +/** + Generate the operational state of the interface this IP4 config2 instance manages + and output in EFI_IP4_CONFIG2_INTERFACE_INFO. + + @param[in] IpSb The pointer to the IP4 service binding instance. + @param[out] IfInfo The pointer to the IP4 config2 interface information structure. + +**/ +VOID +Ip4Config2InitIfInfo ( + IN IP4_SERVICE *IpSb, + OUT EFI_IP4_CONFIG2_INTERFACE_INFO *IfInfo + ) +{ + UnicodeSPrint ( + IfInfo->Name, + EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE, + L"eth%d", + IpSb->Ip4Config2Instance.IfIndex + ); + + IfInfo->IfType = IpSb->SnpMode.IfType; + IfInfo->HwAddressSize = IpSb->SnpMode.HwAddressSize; + CopyMem (&IfInfo->HwAddress, &IpSb->SnpMode.CurrentAddress, IfInfo->HwAddressSize); +} + + + +/** + Set the configuration for the EFI IPv4 network stack running on the communication + device this EFI_IP4_CONFIG2_PROTOCOL instance manages. + + This function is used to set the configuration data of type DataType for the EFI + IPv4 network stack that is running on the communication device that this EFI IPv4 + Configuration Protocol instance manages. + + DataSize is used to calculate the count of structure instances in the Data for + a DataType in which multiple structure instances are allowed. + + This function is always non-blocking. When setting some type of configuration data, + an asynchronous process is invoked to check the correctness of the data, such as + performing Duplicate Address Detection on the manually set local IPv4 addresses. + EFI_NOT_READY is returned immediately to indicate that such an asynchronous process + is invoked, and the process is not finished yet. The caller wanting to get the result + of the asynchronous process is required to call RegisterDataNotify() to register an + event on the specified configuration data. Once the event is signaled, the caller + can call GetData() to obtain the configuration data and know the result. + For other types of configuration data that do not require an asynchronous configuration + process, the result of the operation is immediately returned. + + @param[in] This The pointer to the EFI_IP4_CONFIG2_PROTOCOL instance. + @param[in] DataType The type of data to set. + @param[in] DataSize Size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set. The type of the data buffer is + associated with the DataType. + + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set successfully. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + - This is NULL. + - One or more fields in Data and DataSize do not match the + requirement of the data type indicated by DataType. + @retval EFI_WRITE_PROTECTED The specified configuration data is read-only or the specified + configuration data cannot be set under the current policy. + @retval EFI_ACCESS_DENIED Another set operation on the specified configuration + data is already in process. + @retval EFI_NOT_READY An asynchronous process was invoked to set the specified + configuration data, and the process is not finished yet. + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type + indicated by DataType. + @retval EFI_UNSUPPORTED This DataType is not supported. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected system error or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Config2SetData ( + IN EFI_IP4_CONFIG2_PROTOCOL *This, + IN EFI_IP4_CONFIG2_DATA_TYPE DataType, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + IP4_CONFIG2_INSTANCE *Instance; + IP4_SERVICE *IpSb; + + if ((This == NULL) || (Data == NULL && DataSize != 0) || (Data != NULL && DataSize == 0)) { + return EFI_INVALID_PARAMETER; + } + + if (DataType >= Ip4Config2DataTypeMaximum) { + return EFI_UNSUPPORTED; + } + + Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This); + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE); + + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Status = Instance->DataItem[DataType].Status; + if (Status != EFI_NOT_READY) { + + if (Instance->DataItem[DataType].SetData == NULL) { + // + // This type of data is readonly. + // + Status = EFI_WRITE_PROTECTED; + } else { + + Status = Instance->DataItem[DataType].SetData (Instance, DataSize, Data); + if (!EFI_ERROR (Status)) { + // + // Fire up the events registered with this type of data. + // + NetMapIterate (&Instance->DataItem[DataType].EventMap, Ip4Config2SignalEvent, NULL); + Ip4Config2WriteConfigData (IpSb->MacString, Instance); + } else if (Status == EFI_ABORTED) { + // + // The SetData is aborted because the data to set is the same with + // the one maintained. + // + Status = EFI_SUCCESS; + NetMapIterate (&Instance->DataItem[DataType].EventMap, Ip4Config2SignalEvent, NULL); + } + } + } else { + // + // Another asynchornous process is on the way. + // + Status = EFI_ACCESS_DENIED; + } + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Get the configuration data for the EFI IPv4 network stack running on the communication + device that this EFI_IP4_CONFIG2_PROTOCOL instance manages. + + This function returns the configuration data of type DataType for the EFI IPv4 network + stack running on the communication device that this EFI IPv4 Configuration Protocol instance + manages. + + The caller is responsible for allocating the buffer used to return the specified + configuration data. The required size will be returned to the caller if the size of + the buffer is too small. + + EFI_NOT_READY is returned if the specified configuration data is not ready due to an + asynchronous configuration process already in progress. The caller can call RegisterDataNotify() + to register an event on the specified configuration data. Once the asynchronous configuration + process is finished, the event will be signaled, and a subsequent GetData() call will return + the specified configuration data. + + @param[in] This Pointer to the EFI_IP4_CONFIG2_PROTOCOL instance. + @param[in] DataType The type of data to get. + @param[in, out] DataSize On input, in bytes, the size of Data. On output, in bytes, the + size of buffer required to store the specified configuration data. + @param[in] Data The data buffer in which the configuration data is returned. The + type of the data buffer is associated with the DataType. + This is an optional parameter that may be NULL. + + @retval EFI_SUCCESS The specified configuration data was obtained successfully. + @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE: + - This is NULL. + - DataSize is NULL. + - Data is NULL if *DataSize is not zero. + @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified configuration data, + and the required size is returned in DataSize. + @retval EFI_NOT_READY The specified configuration data is not ready due to an + asynchronous configuration process already in progress. + @retval EFI_NOT_FOUND The specified configuration data is not found. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Config2GetData ( + IN EFI_IP4_CONFIG2_PROTOCOL *This, + IN EFI_IP4_CONFIG2_DATA_TYPE DataType, + IN OUT UINTN *DataSize, + IN VOID *Data OPTIONAL + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + IP4_CONFIG2_INSTANCE *Instance; + IP4_CONFIG2_DATA_ITEM *DataItem; + + if ((This == NULL) || (DataSize == NULL) || ((*DataSize != 0) && (Data == NULL))) { + return EFI_INVALID_PARAMETER; + } + + if (DataType >= Ip4Config2DataTypeMaximum) { + return EFI_NOT_FOUND; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This); + DataItem = &Instance->DataItem[DataType]; + + Status = Instance->DataItem[DataType].Status; + if (!EFI_ERROR (Status)) { + + if (DataItem->GetData != NULL) { + + Status = DataItem->GetData (Instance, DataSize, Data); + } else if (*DataSize < Instance->DataItem[DataType].DataSize) { + // + // Update the buffer length. + // + *DataSize = Instance->DataItem[DataType].DataSize; + Status = EFI_BUFFER_TOO_SMALL; + } else { + + *DataSize = Instance->DataItem[DataType].DataSize; + CopyMem (Data, Instance->DataItem[DataType].Data.Ptr, *DataSize); + } + } + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Register an event that is signaled whenever a configuration process on the specified + configuration data is done. + + This function registers an event that is to be signaled whenever a configuration + process on the specified configuration data is performed. An event can be registered + for a different DataType simultaneously. The caller is responsible for determining + which type of configuration data causes the signaling of the event in such an event. + + @param[in] This Pointer to the EFI_IP4_CONFIG2_PROTOCOL instance. + @param[in] DataType The type of data to unregister the event for. + @param[in] Event The event to register. + + @retval EFI_SUCCESS The notification event for the specified configuration data is + registered. + @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL. + @retval EFI_UNSUPPORTED The configuration data type specified by DataType is not + supported. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_ACCESS_DENIED The Event is already registered for the DataType. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Config2RegisterDataNotify ( + IN EFI_IP4_CONFIG2_PROTOCOL *This, + IN EFI_IP4_CONFIG2_DATA_TYPE DataType, + IN EFI_EVENT Event + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + IP4_CONFIG2_INSTANCE *Instance; + NET_MAP *EventMap; + NET_MAP_ITEM *Item; + + if ((This == NULL) || (Event == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (DataType >= Ip4Config2DataTypeMaximum) { + return EFI_UNSUPPORTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This); + EventMap = &Instance->DataItem[DataType].EventMap; + + // + // Check whether this event is already registered for this DataType. + // + Item = NetMapFindKey (EventMap, Event); + if (Item == NULL) { + + Status = NetMapInsertTail (EventMap, Event, NULL); + + if (EFI_ERROR (Status)) { + + Status = EFI_OUT_OF_RESOURCES; + } + + } else { + + Status = EFI_ACCESS_DENIED; + } + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Remove a previously registered event for the specified configuration data. + + @param This The pointer to the EFI_IP4_CONFIG2_PROTOCOL instance. + @param DataType The type of data to remove from the previously + registered event. + @param Event The event to be unregistered. + + @retval EFI_SUCCESS The event registered for the specified + configuration data was removed. + @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL. + @retval EFI_NOT_FOUND The Event has not been registered for the + specified DataType. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Config2UnregisterDataNotify ( + IN EFI_IP4_CONFIG2_PROTOCOL *This, + IN EFI_IP4_CONFIG2_DATA_TYPE DataType, + IN EFI_EVENT Event + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + IP4_CONFIG2_INSTANCE *Instance; + NET_MAP_ITEM *Item; + + if ((This == NULL) || (Event == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (DataType >= Ip4Config2DataTypeMaximum) { + return EFI_NOT_FOUND; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This); + + Item = NetMapFindKey (&Instance->DataItem[DataType].EventMap, Event); + if (Item != NULL) { + + NetMapRemoveItem (&Instance->DataItem[DataType].EventMap, Item, NULL); + Status = EFI_SUCCESS; + } else { + + Status = EFI_NOT_FOUND; + } + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Initialize an IP4_CONFIG2_INSTANCE. + + @param[out] Instance The buffer of IP4_CONFIG2_INSTANCE to be initialized. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation. + @retval EFI_SUCCESS The IP4_CONFIG2_INSTANCE initialized successfully. + +**/ +EFI_STATUS +Ip4Config2InitInstance ( + OUT IP4_CONFIG2_INSTANCE *Instance + ) +{ + IP4_SERVICE *IpSb; + IP4_CONFIG2_INSTANCE *TmpInstance; + LIST_ENTRY *Entry; + EFI_STATUS Status; + UINTN Index; + UINT16 IfIndex; + IP4_CONFIG2_DATA_ITEM *DataItem; + + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + + Instance->Signature = IP4_CONFIG2_INSTANCE_SIGNATURE; + + + // + // Determine the index of this interface. + // + IfIndex = 0; + NET_LIST_FOR_EACH (Entry, &mIp4Config2InstanceList) { + TmpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_CONFIG2_INSTANCE, Link, IP4_CONFIG2_INSTANCE_SIGNATURE); + + if (TmpInstance->IfIndex > IfIndex) { + // + // There is a sequence hole because some interface is down. + // + break; + } + + IfIndex++; + } + + Instance->IfIndex = IfIndex; + NetListInsertBefore (Entry, &Instance->Link); + + for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) { + // + // Initialize the event map for each data item. + // + NetMapInit (&Instance->DataItem[Index].EventMap); + } + + + // + // Initialize each data type: associate storage and set data size for the + // fixed size data types, hook the SetData function, set the data attribute. + // + DataItem = &Instance->DataItem[Ip4Config2DataTypeInterfaceInfo]; + DataItem->GetData = Ip4Config2GetIfInfo; + DataItem->Data.Ptr = &Instance->InterfaceInfo; + DataItem->DataSize = sizeof (Instance->InterfaceInfo); + SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED | DATA_ATTRIB_VOLATILE); + Ip4Config2InitIfInfo (IpSb, &Instance->InterfaceInfo); + + DataItem = &Instance->DataItem[Ip4Config2DataTypePolicy]; + DataItem->SetData = Ip4Config2SetPolicy; + DataItem->Data.Ptr = &Instance->Policy; + DataItem->DataSize = sizeof (Instance->Policy); + Instance->Policy = Ip4Config2PolicyStatic; + SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED); + + DataItem = &Instance->DataItem[Ip4Config2DataTypeManualAddress]; + DataItem->SetData = Ip4Config2SetManualAddress; + DataItem->Status = EFI_NOT_FOUND; + + DataItem = &Instance->DataItem[Ip4Config2DataTypeGateway]; + DataItem->SetData = Ip4Config2SetGateway; + DataItem->Status = EFI_NOT_FOUND; + + DataItem = &Instance->DataItem[Ip4Config2DataTypeDnsServer]; + DataItem->SetData = Ip4Config2SetDnsServer; + DataItem->Status = EFI_NOT_FOUND; + + Instance->Configured = TRUE; + + // + // Try to read the config data from NV variable. + // If not found, write initialized config data into NV variable + // as a default config data. + // + Status = Ip4Config2ReadConfigData (IpSb->MacString, Instance); + if (Status == EFI_NOT_FOUND) { + Status = Ip4Config2WriteConfigData (IpSb->MacString, Instance); + } + + if (EFI_ERROR (Status)) { + return Status; + } + + Instance->Ip4Config2.SetData = EfiIp4Config2SetData; + Instance->Ip4Config2.GetData = EfiIp4Config2GetData; + Instance->Ip4Config2.RegisterDataNotify = EfiIp4Config2RegisterDataNotify; + Instance->Ip4Config2.UnregisterDataNotify = EfiIp4Config2UnregisterDataNotify; + + // + // Publish the IP4 configuration form + // + return Ip4Config2FormInit (Instance); +} + + +/** + Release an IP4_CONFIG2_INSTANCE. + + @param[in, out] Instance The buffer of IP4_CONFIG2_INSTANCE to be freed. + +**/ +VOID +Ip4Config2CleanInstance ( + IN OUT IP4_CONFIG2_INSTANCE *Instance + ) +{ + UINTN Index; + IP4_CONFIG2_DATA_ITEM *DataItem; + + if (Instance->DeclineAddress != NULL) { + FreePool (Instance->DeclineAddress); + } + + if (!Instance->Configured) { + return ; + } + + if (Instance->Dhcp4Handle != NULL) { + + Ip4Config2DestroyDhcp4 (Instance); + } + + // + // Close the event. + // + if (Instance->Dhcp4Event != NULL) { + gBS->CloseEvent (Instance->Dhcp4Event); + Instance->Dhcp4Event = NULL; + } + + for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) { + + DataItem = &Instance->DataItem[Index]; + + if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED)) { + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = NULL; + DataItem->DataSize = 0; + } + + NetMapClean (&Instance->DataItem[Index].EventMap); + } + + Ip4Config2FormUnload (Instance); + + RemoveEntryList (&Instance->Link); +} + +/** + The event handle for IP4 auto reconfiguration. The original default + interface and route table will be removed as the default. + + @param[in] Context The IP4 service binding instance. + +**/ +VOID +EFIAPI +Ip4AutoReconfigCallBackDpc ( + IN VOID *Context + ) +{ + IP4_SERVICE *IpSb; + + IpSb = (IP4_SERVICE *) Context; + NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE); + + if (IpSb->State > IP4_SERVICE_UNSTARTED) { + IpSb->State = IP4_SERVICE_UNSTARTED; + } + + IpSb->Reconfig = TRUE; + + Ip4StartAutoConfig (&IpSb->Ip4Config2Instance); + + return ; +} + + +/** + Request Ip4AutoReconfigCallBackDpc as a DPC at TPL_CALLBACK. + + @param Event The event that is signalled. + @param Context The IP4 service binding instance. + +**/ +VOID +EFIAPI +Ip4AutoReconfigCallBack ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request Ip4AutoReconfigCallBackDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, Ip4AutoReconfigCallBackDpc, Context); +} + diff --git a/NetworkPkg/Ip4Dxe/Ip4Config2Impl.h b/NetworkPkg/Ip4Dxe/Ip4Config2Impl.h new file mode 100644 index 000000000..1716dde39 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Config2Impl.h @@ -0,0 +1,294 @@ +/** @file + Definitions for EFI IPv4 Configuration II Protocol implementation. + + Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+ (C) Copyright 2015 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __IP4_CONFIG2_IMPL_H__ +#define __IP4_CONFIG2_IMPL_H__ + +#define IP4_CONFIG2_INSTANCE_SIGNATURE SIGNATURE_32 ('I', 'P', 'C', '2') +#define IP4_FORM_CALLBACK_INFO_SIGNATURE SIGNATURE_32 ('I', 'F', 'C', 'I') + +#define IP4_CONFIG2_VARIABLE_ATTRIBUTE (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS) + +#define DATA_ATTRIB_SIZE_FIXED 0x1 +#define DATA_ATTRIB_VOLATILE 0x2 + +#define DATA_ATTRIB_SET(Attrib, Bits) (BOOLEAN)((Attrib) & (Bits)) +#define SET_DATA_ATTRIB(Attrib, Bits) ((Attrib) |= (Bits)) +#define REMOVE_DATA_ATTRIB(Attrib, Bits) ((Attrib) &= (~Bits)) + +typedef struct _IP4_CONFIG2_INSTANCE IP4_CONFIG2_INSTANCE; + +#define IP4_CONFIG2_INSTANCE_FROM_PROTOCOL(Proto) \ + CR ((Proto), \ + IP4_CONFIG2_INSTANCE, \ + Ip4Config2, \ + IP4_CONFIG2_INSTANCE_SIGNATURE \ + ) + +#define IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE(Instance) \ + CR ((Instance), \ + IP4_SERVICE, \ + Ip4Config2Instance, \ + IP4_SERVICE_SIGNATURE \ + ) + +#define IP4_CONFIG2_INSTANCE_FROM_FORM_CALLBACK(Callback) \ + CR ((Callback), \ + IP4_CONFIG2_INSTANCE, \ + CallbackInfo, \ + IP4_CONFIG2_INSTANCE_SIGNATURE \ + ) + +#define IP4_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS(ConfigAccess) \ + CR ((ConfigAccess), \ + IP4_FORM_CALLBACK_INFO, \ + HiiConfigAccessProtocol, \ + IP4_FORM_CALLBACK_INFO_SIGNATURE \ + ) + +/** + The prototype of work function for EfiIp4Config2SetData(). + + @param[in] Instance The pointer to the IP4 config2 instance data. + @param[in] DataSize In bytes, the size of the buffer pointed to by Data. + @param[in] Data The data buffer to set. + + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type, + 8 bytes. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv4 + network stack was set successfully. + +**/ +typedef +EFI_STATUS +(*IP4_CONFIG2_SET_DATA) ( + IN IP4_CONFIG2_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ); + +/** + The prototype of work function for EfiIp4Config2GetData(). + + @param[in] Instance The pointer to the IP4 config2 instance data. + @param[in, out] DataSize On input, in bytes, the size of Data. On output, in + bytes, the size of buffer required to store the specified + configuration data. + @param[in] Data The data buffer in which the configuration data is returned. + Ignored if DataSize is ZERO. + + @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified + configuration data, and the required size is + returned in DataSize. + @retval EFI_SUCCESS The specified configuration data was obtained successfully. + +**/ +typedef +EFI_STATUS +(*IP4_CONFIG2_GET_DATA) ( + IN IP4_CONFIG2_INSTANCE *Instance, + IN OUT UINTN *DataSize, + IN VOID *Data OPTIONAL + ); + +typedef union { + VOID *Ptr; + EFI_IP4_CONFIG2_INTERFACE_INFO *IfInfo; + EFI_IP4_CONFIG2_POLICY *Policy; + EFI_IP4_CONFIG2_MANUAL_ADDRESS *ManualAddress; + EFI_IPv4_ADDRESS *Gateway; + EFI_IPv4_ADDRESS *DnsServers; +} IP4_CONFIG2_DATA; + +typedef struct { + IP4_CONFIG2_SET_DATA SetData; + IP4_CONFIG2_GET_DATA GetData; + EFI_STATUS Status; + UINT8 Attribute; + NET_MAP EventMap; + IP4_CONFIG2_DATA Data; + UINTN DataSize; +} IP4_CONFIG2_DATA_ITEM; + +typedef struct { + UINT16 Offset; + UINT32 DataSize; + EFI_IP4_CONFIG2_DATA_TYPE DataType; +} IP4_CONFIG2_DATA_RECORD; + +#pragma pack(1) + +// +// heap data that contains the data for each data record. +// +// EFI_IP4_CONFIG2_POLICY Policy; +// UINT32 ManualaddressCount; +// UINT32 GatewayCount; +// UINT32 DnsServersCount; +// EFI_IP4_CONFIG2_MANUAL_ADDRESS ManualAddress[]; +// EFI_IPv4_ADDRESS Gateway[]; +// EFI_IPv4_ADDRESS DnsServers[]; +// +typedef struct { + UINT16 Checksum; + UINT16 DataRecordCount; + IP4_CONFIG2_DATA_RECORD DataRecord[1]; +} IP4_CONFIG2_VARIABLE; + +#pragma pack() + +typedef struct { + EFI_IP4_CONFIG2_POLICY Policy; ///< manual or automatic + EFI_IP4_CONFIG2_MANUAL_ADDRESS *ManualAddress; ///< IP addresses + UINT32 ManualAddressCount; ///< IP addresses count + EFI_IPv4_ADDRESS *GatewayAddress; ///< Gateway address + UINT32 GatewayAddressCount; ///< Gateway address count + EFI_IPv4_ADDRESS *DnsAddress; ///< DNS server address + UINT32 DnsAddressCount; ///< DNS server address count +} IP4_CONFIG2_NVDATA; + +typedef struct _IP4_FORM_CALLBACK_INFO { + UINT32 Signature; + EFI_HANDLE ChildHandle; + EFI_HII_CONFIG_ACCESS_PROTOCOL HiiConfigAccessProtocol; + EFI_DEVICE_PATH_PROTOCOL *HiiVendorDevicePath; + EFI_HII_HANDLE RegisteredHandle; +} IP4_FORM_CALLBACK_INFO; + +struct _IP4_CONFIG2_INSTANCE { + UINT32 Signature; + BOOLEAN Configured; + LIST_ENTRY Link; + UINT16 IfIndex; + + EFI_IP4_CONFIG2_PROTOCOL Ip4Config2; + + EFI_IP4_CONFIG2_INTERFACE_INFO InterfaceInfo; + EFI_IP4_CONFIG2_POLICY Policy; + IP4_CONFIG2_DATA_ITEM DataItem[Ip4Config2DataTypeMaximum]; + + EFI_EVENT Dhcp4SbNotifyEvent; + VOID *Registration; + EFI_HANDLE Dhcp4Handle; + EFI_DHCP4_PROTOCOL *Dhcp4; + BOOLEAN DhcpSuccess; + BOOLEAN OtherInfoOnly; + EFI_EVENT Dhcp4Event; + UINT32 FailedIaAddressCount; + EFI_IPv4_ADDRESS *DeclineAddress; + UINT32 DeclineAddressCount; + + IP4_FORM_CALLBACK_INFO CallbackInfo; + + IP4_CONFIG2_NVDATA Ip4NvData; +}; + +// +// Configure the DHCP to request the routers and netmask +// from server. The DHCP4_TAG_NETMASK is included in Head. +// +#pragma pack(1) +typedef struct { + EFI_DHCP4_PACKET_OPTION Head; + UINT8 Route; + UINT8 Dns; +} IP4_CONFIG2_DHCP4_OPTION; +#pragma pack() + +/** + Read the configuration data from variable storage according to the VarName and + gEfiIp4Config2ProtocolGuid. It checks the integrity of variable data. If the + data is corrupted, it clears the variable data to ZERO. Othewise, it outputs the + configuration data to IP4_CONFIG2_INSTANCE. + + @param[in] VarName The pointer to the variable name + @param[in, out] Instance The pointer to the IP4 config2 instance data. + + @retval EFI_NOT_FOUND The variable can not be found or already corrupted. + @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation. + @retval EFI_SUCCESS The configuration data was retrieved successfully. + +**/ +EFI_STATUS +Ip4Config2ReadConfigData ( + IN CHAR16 *VarName, + IN OUT IP4_CONFIG2_INSTANCE *Instance + ); + +/** + Start the DHCP configuration for this IP service instance. + It will locates the EFI_IP4_CONFIG2_PROTOCOL, then start the + DHCP configuration. + + @param[in] Instance The IP4 config2 instance to configure. + + @retval EFI_SUCCESS The auto configuration is successfully started. + @retval Others Failed to start auto configuration. + +**/ +EFI_STATUS +Ip4StartAutoConfig ( + IN IP4_CONFIG2_INSTANCE *Instance + ); + +/** + Initialize an IP4_CONFIG2_INSTANCE. + + @param[out] Instance The buffer of IP4_CONFIG2_INSTANCE to be initialized. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation. + @retval EFI_SUCCESS The IP4_CONFIG2_INSTANCE initialized successfully. + +**/ +EFI_STATUS +Ip4Config2InitInstance ( + OUT IP4_CONFIG2_INSTANCE *Instance + ); + +/** + Release an IP4_CONFIG2_INSTANCE. + + @param[in, out] Instance The buffer of IP4_CONFIG2_INSTANCE to be freed. + +**/ +VOID +Ip4Config2CleanInstance ( + IN OUT IP4_CONFIG2_INSTANCE *Instance + ); + +/** + Request Ip4AutoReconfigCallBackDpc as a DPC at TPL_CALLBACK. + + @param Event The event that is signalled. + @param Context The IP4 service binding instance. + +**/ +VOID +EFIAPI +Ip4AutoReconfigCallBack ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Destroy the Dhcp4 child in IP4_CONFIG2_INSTANCE and release the resources. + + @param[in, out] Instance The buffer of IP4 config2 instance to be freed. + + @retval EFI_SUCCESS The child was successfully destroyed. + @retval Others Failed to destroy the child. + +**/ +EFI_STATUS +Ip4Config2DestroyDhcp4 ( + IN OUT IP4_CONFIG2_INSTANCE *Instance + ); + +#endif diff --git a/NetworkPkg/Ip4Dxe/Ip4Config2Nv.c b/NetworkPkg/Ip4Dxe/Ip4Config2Nv.c new file mode 100644 index 000000000..a4d2996a6 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Config2Nv.c @@ -0,0 +1,1444 @@ +/** @file + Helper functions for configuring or getting the parameters relating to Ip4. + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + +CHAR16 mIp4Config2StorageName[] = L"IP4_CONFIG2_IFR_NVDATA"; + +/** + Calculate the prefix length of the IPv4 subnet mask. + + @param[in] SubnetMask The IPv4 subnet mask. + + @return The prefix length of the subnet mask. + @retval 0 Other errors as indicated. + +**/ +UINT8 +GetSubnetMaskPrefixLength ( + IN EFI_IPv4_ADDRESS *SubnetMask + ) +{ + UINT8 Len; + UINT32 ReverseMask; + + // + // The SubnetMask is in network byte order. + // + ReverseMask = SwapBytes32 (*(UINT32 *)&SubnetMask[0]); + + // + // Reverse it. + // + ReverseMask = ~ReverseMask; + + if ((ReverseMask & (ReverseMask + 1)) != 0) { + return 0; + } + + Len = 0; + + while (ReverseMask != 0) { + ReverseMask = ReverseMask >> 1; + Len++; + } + + return (UINT8) (32 - Len); +} + +/** + Convert the decimal dotted IPv4 address into the binary IPv4 address. + + @param[in] Str The UNICODE string. + @param[out] Ip The storage to return the IPv4 address. + + @retval EFI_SUCCESS The binary IP address is returned in Ip. + @retval EFI_INVALID_PARAMETER The IP string is malformatted. + +**/ +EFI_STATUS +Ip4Config2StrToIp ( + IN CHAR16 *Str, + OUT EFI_IPv4_ADDRESS *Ip + ) +{ + UINTN Index; + UINTN Number; + + Index = 0; + + while (*Str != L'\0') { + + if (Index > 3) { + return EFI_INVALID_PARAMETER; + } + + Number = 0; + while ((*Str >= L'0') && (*Str <= L'9')) { + Number = Number * 10 + (*Str - L'0'); + Str++; + } + + if (Number > 0xFF) { + return EFI_INVALID_PARAMETER; + } + + Ip->Addr[Index] = (UINT8) Number; + + if ((*Str != L'\0') && (*Str != L'.')) { + // + // The current character should be either the NULL terminator or + // the dot delimiter. + // + return EFI_INVALID_PARAMETER; + } + + if (*Str == L'.') { + // + // Skip the delimiter. + // + Str++; + } + + Index++; + } + + if (Index != 4) { + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + +/** + Convert the decimal dotted IPv4 addresses separated by space into the binary IPv4 address list. + + @param[in] Str The UNICODE string contains IPv4 addresses. + @param[out] PtrIpList The storage to return the IPv4 address list. + @param[out] IpCount The size of the IPv4 address list. + + @retval EFI_SUCCESS The binary IP address list is returned in PtrIpList. + @retval EFI_OUT_OF_RESOURCES Error occurs in allocating memory. + @retval EFI_INVALID_PARAMETER The IP string is malformatted. + +**/ +EFI_STATUS +Ip4Config2StrToIpList ( + IN CHAR16 *Str, + OUT EFI_IPv4_ADDRESS **PtrIpList, + OUT UINTN *IpCount + ) +{ + UINTN BeginIndex; + UINTN EndIndex; + UINTN Index; + UINTN IpIndex; + CHAR16 *StrTemp; + BOOLEAN SpaceTag; + + BeginIndex = 0; + EndIndex = BeginIndex; + Index = 0; + IpIndex = 0; + StrTemp = NULL; + SpaceTag = TRUE; + + *PtrIpList = NULL; + *IpCount = 0; + + if (Str == NULL) { + return EFI_SUCCESS; + } + + // + // Get the number of Ip. + // + while (*(Str + Index) != L'\0') { + if (*(Str + Index) == L' ') { + SpaceTag = TRUE; + } else { + if (SpaceTag) { + (*IpCount)++; + SpaceTag = FALSE; + } + } + + Index++; + } + + if (*IpCount == 0) { + return EFI_SUCCESS; + } + + // + // Allocate buffer for IpList. + // + *PtrIpList = AllocateZeroPool(*IpCount * sizeof(EFI_IPv4_ADDRESS)); + if (*PtrIpList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Get IpList from Str. + // + Index = 0; + while (*(Str + Index) != L'\0') { + if (*(Str + Index) == L' ') { + if(!SpaceTag) { + StrTemp = AllocateZeroPool((EndIndex - BeginIndex + 1) * sizeof(CHAR16)); + if (StrTemp == NULL) { + FreePool(*PtrIpList); + *PtrIpList = NULL; + *IpCount = 0; + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (StrTemp, Str + BeginIndex, (EndIndex - BeginIndex) * sizeof(CHAR16)); + *(StrTemp + (EndIndex - BeginIndex)) = L'\0'; + + if (Ip4Config2StrToIp (StrTemp, &((*PtrIpList)[IpIndex])) != EFI_SUCCESS) { + FreePool(StrTemp); + FreePool(*PtrIpList); + *PtrIpList = NULL; + *IpCount = 0; + return EFI_INVALID_PARAMETER; + } + + BeginIndex = EndIndex; + IpIndex++; + + FreePool(StrTemp); + } + + BeginIndex++; + EndIndex++; + SpaceTag = TRUE; + } else { + EndIndex++; + SpaceTag = FALSE; + } + + Index++; + + if (*(Str + Index) == L'\0') { + if (!SpaceTag) { + StrTemp = AllocateZeroPool((EndIndex - BeginIndex + 1) * sizeof(CHAR16)); + if (StrTemp == NULL) { + FreePool(*PtrIpList); + *PtrIpList = NULL; + *IpCount = 0; + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (StrTemp, Str + BeginIndex, (EndIndex - BeginIndex) * sizeof(CHAR16)); + *(StrTemp + (EndIndex - BeginIndex)) = L'\0'; + + if (Ip4Config2StrToIp (StrTemp, &((*PtrIpList)[IpIndex])) != EFI_SUCCESS) { + FreePool(StrTemp); + FreePool(*PtrIpList); + *PtrIpList = NULL; + *IpCount = 0; + return EFI_INVALID_PARAMETER; + } + + FreePool(StrTemp); + } + } + } + + return EFI_SUCCESS; +} + +/** + Convert the IPv4 address into a dotted string. + + @param[in] Ip The IPv4 address. + @param[out] Str The dotted IP string. + +**/ +VOID +Ip4Config2IpToStr ( + IN EFI_IPv4_ADDRESS *Ip, + OUT CHAR16 *Str + ) +{ + UnicodeSPrint ( + Str, + 2 * IP4_STR_MAX_SIZE, + L"%d.%d.%d.%d", + Ip->Addr[0], + Ip->Addr[1], + Ip->Addr[2], + Ip->Addr[3] + ); +} + + +/** + Convert the IPv4 address list into string consists of several decimal + dotted IPv4 addresses separated by space. + + @param[in] Ip The IPv4 address list. + @param[in] IpCount The size of IPv4 address list. + @param[out] Str The string contains several decimal dotted + IPv4 addresses separated by space. + + @retval EFI_SUCCESS Operation is success. + @retval EFI_OUT_OF_RESOURCES Error occurs in allocating memory. + +**/ +EFI_STATUS +Ip4Config2IpListToStr ( + IN EFI_IPv4_ADDRESS *Ip, + IN UINTN IpCount, + OUT CHAR16 *Str + ) +{ + UINTN Index; + UINTN TemIndex; + UINTN StrIndex; + CHAR16 *TempStr; + EFI_IPv4_ADDRESS *TempIp; + + Index = 0; + TemIndex = 0; + StrIndex = 0; + TempStr = NULL; + TempIp = NULL; + + for (Index = 0; Index < IpCount; Index ++) { + TempIp = Ip + Index; + if (TempStr == NULL) { + TempStr = AllocateZeroPool(2 * IP4_STR_MAX_SIZE); + if (TempStr == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + + UnicodeSPrint ( + TempStr, + 2 * IP4_STR_MAX_SIZE, + L"%d.%d.%d.%d", + TempIp->Addr[0], + TempIp->Addr[1], + TempIp->Addr[2], + TempIp->Addr[3] + ); + + for (TemIndex = 0; TemIndex < IP4_STR_MAX_SIZE; TemIndex ++) { + if (*(TempStr + TemIndex) == L'\0') { + if (Index == IpCount - 1) { + Str[StrIndex++] = L'\0'; + } else { + Str[StrIndex++] = L' '; + } + break; + } else { + Str[StrIndex++] = *(TempStr + TemIndex); + } + } + } + + if (TempStr != NULL) { + FreePool(TempStr); + } + + return EFI_SUCCESS; +} + +/** + The notify function of create event when performing a manual configuration. + + @param[in] Event The pointer of Event. + @param[in] Context The pointer of Context. + +**/ +VOID +EFIAPI +Ip4Config2ManualAddressNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + *((BOOLEAN *) Context) = TRUE; +} + +/** + Convert the network configuration data into the IFR data. + + @param[in] Instance The IP4 config2 instance. + @param[in, out] IfrNvData The IFR nv data. + + @retval EFI_SUCCESS The configure parameter to IFR data was + set successfully. + @retval EFI_INVALID_PARAMETER Source instance or target IFR data is not available. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +Ip4Config2ConvertConfigNvDataToIfrNvData ( + IN IP4_CONFIG2_INSTANCE *Instance, + IN OUT IP4_CONFIG2_IFR_NVDATA *IfrNvData + ) +{ + IP4_SERVICE *IpSb; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + EFI_IP4_CONFIG2_INTERFACE_INFO *Ip4Info; + EFI_IP4_CONFIG2_POLICY Policy; + UINTN DataSize; + UINTN GatewaySize; + EFI_IPv4_ADDRESS GatewayAddress; + EFI_STATUS Status; + UINTN DnsSize; + UINTN DnsCount; + EFI_IPv4_ADDRESS *DnsAddress; + + Status = EFI_SUCCESS; + Ip4Config2 = &Instance->Ip4Config2; + Ip4Info = NULL; + DnsAddress = NULL; + GatewaySize = sizeof (EFI_IPv4_ADDRESS); + + if ((IfrNvData == NULL) || (Instance == NULL)) { + return EFI_INVALID_PARAMETER; + } + + NET_CHECK_SIGNATURE (Instance, IP4_CONFIG2_INSTANCE_SIGNATURE); + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + + if (IpSb->DefaultInterface->Configured) { + IfrNvData->Configure = 1; + } else { + IfrNvData->Configure = 0; + goto Exit; + } + + // + // Get the Policy info. + // + DataSize = sizeof (EFI_IP4_CONFIG2_POLICY); + Status = Ip4Config2->GetData ( + Ip4Config2, + Ip4Config2DataTypePolicy, + &DataSize, + &Policy + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + if (Policy == Ip4Config2PolicyStatic) { + IfrNvData->DhcpEnable = FALSE; + } else if (Policy == Ip4Config2PolicyDhcp) { + IfrNvData->DhcpEnable = TRUE; + goto Exit; + } + + // + // Get the interface info. + // + DataSize = 0; + Status = Ip4Config2->GetData ( + Ip4Config2, + Ip4Config2DataTypeInterfaceInfo, + &DataSize, + NULL + ); + if (Status != EFI_BUFFER_TOO_SMALL) { + return Status; + } + + Ip4Info = AllocateZeroPool (DataSize); + if (Ip4Info == NULL) { + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = Ip4Config2->GetData ( + Ip4Config2, + Ip4Config2DataTypeInterfaceInfo, + &DataSize, + Ip4Info + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Get the Gateway info. + // + Status = Ip4Config2->GetData ( + Ip4Config2, + Ip4Config2DataTypeGateway, + &GatewaySize, + &GatewayAddress + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Get the Dns info. + // + DnsSize = 0; + Status = Ip4Config2->GetData ( + Ip4Config2, + Ip4Config2DataTypeDnsServer, + &DnsSize, + NULL + ); + if ((Status != EFI_BUFFER_TOO_SMALL) && (Status != EFI_NOT_FOUND)) { + goto Exit; + } + + DnsCount = (UINT32) (DnsSize / sizeof (EFI_IPv4_ADDRESS)); + + if (DnsSize > 0) { + DnsAddress = AllocateZeroPool(DnsSize); + if (DnsAddress == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + Status = Ip4Config2->GetData ( + Ip4Config2, + Ip4Config2DataTypeDnsServer, + &DnsSize, + DnsAddress + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + + Ip4Config2IpToStr (&Ip4Info->StationAddress, IfrNvData->StationAddress); + Ip4Config2IpToStr (&Ip4Info->SubnetMask, IfrNvData->SubnetMask); + Ip4Config2IpToStr (&GatewayAddress, IfrNvData->GatewayAddress); + Status = Ip4Config2IpListToStr (DnsAddress, DnsCount, IfrNvData->DnsAddress); + +Exit: + + if (DnsAddress != NULL) { + FreePool(DnsAddress); + } + + if (Ip4Info != NULL) { + FreePool(Ip4Info); + } + + return Status; +} + +/** + Convert the IFR data into the network configuration data and set the IP + configure parameters for the NIC. + + @param[in] IfrFormNvData The IFR NV data. + @param[in, out] Instance The IP4 config2 instance. + + @retval EFI_SUCCESS The configure parameter for this NIC was + set successfully. + @retval EFI_INVALID_PARAMETER The address information for setting is invalid. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +Ip4Config2ConvertIfrNvDataToConfigNvData ( + IN IP4_CONFIG2_IFR_NVDATA *IfrFormNvData, + IN OUT IP4_CONFIG2_INSTANCE *Instance + ) +{ + EFI_STATUS Status; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Cfg2; + IP4_CONFIG2_NVDATA *Ip4NvData; + + EFI_IP_ADDRESS StationAddress; + EFI_IP_ADDRESS SubnetMask; + EFI_IP_ADDRESS Gateway; + IP4_ADDR Ip; + EFI_IPv4_ADDRESS *DnsAddress; + UINTN DnsCount; + UINTN Index; + + EFI_EVENT TimeoutEvent; + EFI_EVENT SetAddressEvent; + BOOLEAN IsAddressOk; + UINTN DataSize; + EFI_INPUT_KEY Key; + + Status = EFI_SUCCESS; + Ip4Cfg2 = &Instance->Ip4Config2; + Ip4NvData = &Instance->Ip4NvData; + + DnsCount = 0; + DnsAddress = NULL; + + TimeoutEvent = NULL; + SetAddressEvent = NULL; + + + + if (Instance == NULL || IfrFormNvData == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (IfrFormNvData->Configure != TRUE) { + return EFI_SUCCESS; + } + + if (IfrFormNvData->DhcpEnable == TRUE) { + Ip4NvData->Policy = Ip4Config2PolicyDhcp; + + Status = Ip4Cfg2->SetData ( + Ip4Cfg2, + Ip4Config2DataTypePolicy, + sizeof (EFI_IP4_CONFIG2_POLICY), + &Ip4NvData->Policy + ); + if (EFI_ERROR(Status)) { + return Status; + } + } else { + // + // Get Ip4NvData from IfrFormNvData if it is valid. + // + Ip4NvData->Policy = Ip4Config2PolicyStatic; + + Status = Ip4Config2StrToIp (IfrFormNvData->SubnetMask, &SubnetMask.v4); + if (EFI_ERROR (Status) || ((SubnetMask.Addr[0] != 0) && (GetSubnetMaskPrefixLength (&SubnetMask.v4) == 0))) { + CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Subnet Mask!", NULL); + return EFI_INVALID_PARAMETER; + } + + Status = Ip4Config2StrToIp (IfrFormNvData->StationAddress, &StationAddress.v4); + if (EFI_ERROR (Status) || + (SubnetMask.Addr[0] != 0 && !NetIp4IsUnicast (NTOHL (StationAddress.Addr[0]), NTOHL (SubnetMask.Addr[0]))) || + !Ip4StationAddressValid (NTOHL (StationAddress.Addr[0]), NTOHL (SubnetMask.Addr[0]))) { + CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid IP address!", NULL); + return EFI_INVALID_PARAMETER; + } + + Status = Ip4Config2StrToIp (IfrFormNvData->GatewayAddress, &Gateway.v4); + if (EFI_ERROR (Status) || + (Gateway.Addr[0] != 0 && SubnetMask.Addr[0] != 0 && !NetIp4IsUnicast (NTOHL (Gateway.Addr[0]), NTOHL (SubnetMask.Addr[0])))) { + CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Gateway!", NULL); + return EFI_INVALID_PARAMETER; + } + + Status = Ip4Config2StrToIpList (IfrFormNvData->DnsAddress, &DnsAddress, &DnsCount); + if (!EFI_ERROR (Status) && DnsCount > 0) { + for (Index = 0; Index < DnsCount; Index ++) { + CopyMem (&Ip, &DnsAddress[Index], sizeof (IP4_ADDR)); + if (IP4_IS_UNSPECIFIED (NTOHL (Ip)) || IP4_IS_LOCAL_BROADCAST (NTOHL (Ip))) { + CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Dns Server!", NULL); + FreePool(DnsAddress); + return EFI_INVALID_PARAMETER; + } + } + } else { + if (EFI_ERROR (Status)) { + CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Dns Server!", NULL); + } + } + + if (Ip4NvData->ManualAddress != NULL) { + FreePool(Ip4NvData->ManualAddress); + } + Ip4NvData->ManualAddressCount = 1; + Ip4NvData->ManualAddress = AllocateZeroPool(sizeof(EFI_IP4_CONFIG2_MANUAL_ADDRESS)); + if (Ip4NvData->ManualAddress == NULL) { + if (DnsAddress != NULL) { + FreePool(DnsAddress); + } + + return EFI_OUT_OF_RESOURCES; + } + CopyMem(&Ip4NvData->ManualAddress->Address, &StationAddress.v4, sizeof(EFI_IPv4_ADDRESS)); + CopyMem(&Ip4NvData->ManualAddress->SubnetMask, &SubnetMask.v4, sizeof(EFI_IPv4_ADDRESS)); + + if (Ip4NvData->GatewayAddress != NULL) { + FreePool(Ip4NvData->GatewayAddress); + } + Ip4NvData->GatewayAddressCount = 1; + Ip4NvData->GatewayAddress = AllocateZeroPool(sizeof(EFI_IPv4_ADDRESS)); + if (Ip4NvData->GatewayAddress == NULL) { + if (DnsAddress != NULL) { + FreePool(DnsAddress); + } + return EFI_OUT_OF_RESOURCES; + } + CopyMem(Ip4NvData->GatewayAddress, &Gateway.v4, sizeof(EFI_IPv4_ADDRESS)); + + if (Ip4NvData->DnsAddress != NULL) { + FreePool(Ip4NvData->DnsAddress); + } + Ip4NvData->DnsAddressCount = (UINT32) DnsCount; + Ip4NvData->DnsAddress = DnsAddress; + + // + // Setting Ip4NvData. + // + Status = Ip4Cfg2->SetData ( + Ip4Cfg2, + Ip4Config2DataTypePolicy, + sizeof (EFI_IP4_CONFIG2_POLICY), + &Ip4NvData->Policy + ); + if (EFI_ERROR(Status)) { + return Status; + } + + // + // Create events & timers for asynchronous settings. + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &TimeoutEvent + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Ip4Config2ManualAddressNotify, + &IsAddressOk, + &SetAddressEvent + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + IsAddressOk = FALSE; + + Status = Ip4Cfg2->RegisterDataNotify ( + Ip4Cfg2, + Ip4Config2DataTypeManualAddress, + SetAddressEvent + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Set ManualAddress. + // + DataSize = Ip4NvData->ManualAddressCount * sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS); + Status = Ip4Cfg2->SetData ( + Ip4Cfg2, + Ip4Config2DataTypeManualAddress, + DataSize, + (VOID *) Ip4NvData->ManualAddress + ); + + if (Status == EFI_NOT_READY) { + gBS->SetTimer (TimeoutEvent, TimerRelative, 50000000); + while (EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) { + if (IsAddressOk) { + Status = EFI_SUCCESS; + break; + } + } + } + + Ip4Cfg2->UnregisterDataNotify ( + Ip4Cfg2, + Ip4Config2DataTypeManualAddress, + SetAddressEvent + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Set gateway. + // + DataSize = Ip4NvData->GatewayAddressCount * sizeof (EFI_IPv4_ADDRESS); + Status = Ip4Cfg2->SetData ( + Ip4Cfg2, + Ip4Config2DataTypeGateway, + DataSize, + Ip4NvData->GatewayAddress + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Set DNS addresses. + // + if (Ip4NvData->DnsAddressCount > 0 && Ip4NvData->DnsAddress != NULL) { + DataSize = Ip4NvData->DnsAddressCount * sizeof (EFI_IPv4_ADDRESS); + Status = Ip4Cfg2->SetData ( + Ip4Cfg2, + Ip4Config2DataTypeDnsServer, + DataSize, + Ip4NvData->DnsAddress + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + } + } + +Exit: + if (SetAddressEvent != NULL) { + gBS->CloseEvent (SetAddressEvent); + } + + if (TimeoutEvent != NULL) { + gBS->CloseEvent (TimeoutEvent); + } + + return Status; +} + +/** + This function allows the caller to request the current + configuration for one or more named elements. The resulting + string is in format. Any and all alternative + configuration strings shall also be appended to the end of the + current configuration string. If they are, they must appear + after the current configuration. They must contain the same + routing (GUID, NAME, PATH) as the current configuration string. + They must have an additional description indicating the type of + alternative configuration the string represents, + "ALTCFG=". That (when + converted from Hex UNICODE to binary) is a reference to a + string in the associated string pack. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Request A null-terminated Unicode string in + format. Note that this + includes the routing information as well as + the configurable name / value pairs. It is + invalid for this string to be in + format. + @param[out] Progress On return, points to a character in the + Request string. Points to the string's null + terminator if request was successful. Points + to the most recent "&" before the first + failing name / value pair (or the beginning + of the string if the failure is in the first + name / value pair) if the request was not + successful. + @param[out] Results A null-terminated Unicode string in + format which has all values + filled in for the names in the Request string. + String to be allocated by the called function. + + @retval EFI_SUCCESS The Results string is filled with the + values corresponding to all requested + names. + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + @retval EFI_NOT_FOUND Routing data doesn't match any + known driver. Progress set to the + first character in the routing header. + Note: There is no requirement that the + driver validate the routing data. It + must skip the in order to + process the names. + @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set + to most recent & before the + error or the beginning of the + string. + @retval EFI_INVALID_PARAMETER Unknown name. Progress points + to the & before the name in + question.Currently not implemented. +**/ +EFI_STATUS +EFIAPI +Ip4FormExtractConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Request, + OUT EFI_STRING *Progress, + OUT EFI_STRING *Results + ) +{ + EFI_STATUS Status; + IP4_CONFIG2_INSTANCE *Ip4Config2Instance; + IP4_FORM_CALLBACK_INFO *Private; + IP4_CONFIG2_IFR_NVDATA *IfrFormNvData; + EFI_STRING ConfigRequestHdr; + EFI_STRING ConfigRequest; + BOOLEAN AllocatedRequest; + EFI_STRING FormResult; + UINTN Size; + UINTN BufferSize; + + if (Progress == NULL || Results == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = EFI_SUCCESS; + IfrFormNvData = NULL; + ConfigRequest = NULL; + FormResult = NULL; + Size = 0; + AllocatedRequest = FALSE; + ConfigRequest = Request; + Private = IP4_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS(This); + Ip4Config2Instance = IP4_CONFIG2_INSTANCE_FROM_FORM_CALLBACK(Private); + BufferSize = sizeof (IP4_CONFIG2_IFR_NVDATA); + *Progress = Request; + + // + // Check Request data in . + // + if ((Request == NULL) || HiiIsConfigHdrMatch (Request, &gIp4Config2NvDataGuid, mIp4Config2StorageName)) { + IfrFormNvData = AllocateZeroPool (sizeof (IP4_CONFIG2_IFR_NVDATA)); + if (IfrFormNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Ip4Config2ConvertConfigNvDataToIfrNvData (Ip4Config2Instance, IfrFormNvData); + + if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) { + // + // Request has no request element, construct full request string. + // Allocate and fill a buffer large enough to hold the template + // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator + // + ConfigRequestHdr = HiiConstructConfigHdr (&gIp4Config2NvDataGuid, mIp4Config2StorageName, Private->ChildHandle); + Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16); + ConfigRequest = AllocateZeroPool (Size); + if (ConfigRequest == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Failure; + } + AllocatedRequest = TRUE; + + UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize); + FreePool (ConfigRequestHdr); + } + + // + // Convert buffer data to by helper function BlockToConfig() + // + Status = gHiiConfigRouting->BlockToConfig ( + gHiiConfigRouting, + ConfigRequest, + (UINT8 *) IfrFormNvData, + BufferSize, + &FormResult, + Progress + ); + + FreePool (IfrFormNvData); + + // + // Free the allocated config request string. + // + if (AllocatedRequest) { + FreePool (ConfigRequest); + ConfigRequest = NULL; + } + + if (EFI_ERROR (Status)) { + goto Failure; + } + } + + if (Request == NULL || HiiIsConfigHdrMatch (Request, &gIp4Config2NvDataGuid, mIp4Config2StorageName)) { + *Results = FormResult; + } else { + return EFI_NOT_FOUND; + } + +Failure: + // + // Set Progress string to the original request string. + // + if (Request == NULL) { + *Progress = NULL; + } else if (StrStr (Request, L"OFFSET") == NULL) { + *Progress = Request + StrLen (Request); + } + + return Status; +} + +/** + This function applies changes in a driver's configuration. + Input is a Configuration, which has the routing data for this + driver followed by name / value configuration pairs. The driver + must apply those pairs to its configurable storage. If the + driver's configuration is stored in a linear block of data + and the driver's name / value pairs are in + format, it may use the ConfigToBlock helper function (above) to + simplify the job. Currently not implemented. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Configuration A null-terminated Unicode string in + format. + @param[out] Progress A pointer to a string filled in with the + offset of the most recent '&' before the + first failing name / value pair (or the + beginn ing of the string if the failure + is in the first name / value pair) or + the terminating NULL if all was + successful. + + @retval EFI_SUCCESS The results have been distributed or are + awaiting distribution. + @retval EFI_OUT_OF_MEMORY Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + @retval EFI_INVALID_PARAMETERS Passing in a NULL for the + Results parameter would result + in this type of error. + @retval EFI_NOT_FOUND Target for the specified routing data + was not found. +**/ +EFI_STATUS +EFIAPI +Ip4FormRouteConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Configuration, + OUT EFI_STRING *Progress + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + IP4_CONFIG2_IFR_NVDATA *IfrFormNvData; + IP4_CONFIG2_INSTANCE *Ip4Config2Instance; + IP4_FORM_CALLBACK_INFO *Private; + + Status = EFI_SUCCESS; + IfrFormNvData = NULL; + + if (Configuration == NULL || Progress == NULL) { + return EFI_INVALID_PARAMETER; + } + + *Progress = Configuration; + + Private = IP4_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS(This); + Ip4Config2Instance = IP4_CONFIG2_INSTANCE_FROM_FORM_CALLBACK(Private); + + // + // Check Routing data in . + // + if (HiiIsConfigHdrMatch (Configuration, &gIp4Config2NvDataGuid, mIp4Config2StorageName)) { + // + // Convert buffer data to by helper function BlockToConfig() + // + IfrFormNvData = AllocateZeroPool (sizeof (IP4_CONFIG2_IFR_NVDATA)); + if (IfrFormNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + BufferSize = 0; + + Status = gHiiConfigRouting->ConfigToBlock ( + gHiiConfigRouting, + Configuration, + (UINT8 *) IfrFormNvData, + &BufferSize, + Progress + ); + if (Status != EFI_BUFFER_TOO_SMALL) { + return Status; + } + + Status = gHiiConfigRouting->ConfigToBlock ( + gHiiConfigRouting, + Configuration, + (UINT8 *) IfrFormNvData, + &BufferSize, + Progress + ); + if (!EFI_ERROR (Status)) { + Status = Ip4Config2ConvertIfrNvDataToConfigNvData (IfrFormNvData, Ip4Config2Instance); + } + + FreePool (IfrFormNvData); + } else { + return EFI_NOT_FOUND; + } + + return Status; + +} + +/** + This function is called to provide results data to the driver. + This data consists of a unique key that is used to identify + which data is either being passed back or being asked for. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Action Specifies the type of action taken by the browser. + @param[in] QuestionId A unique value which is sent to the original + exporting driver so that it can identify the type + of data to expect. The format of the data tends to + vary based on the opcode that enerated the callback. + @param[in] Type The type of value for the question. + @param[in] Value A pointer to the data being sent to the original + exporting driver. + @param[out] ActionRequest On return, points to the action requested by the + callback function. + + @retval EFI_SUCCESS The callback successfully handled the action. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the + variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be saved. + @retval EFI_UNSUPPORTED The specified Action is not supported by the + callback.Currently not implemented. + @retval EFI_INVALID_PARAMETERS Passing in wrong parameter. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +Ip4FormCallback ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN EFI_BROWSER_ACTION Action, + IN EFI_QUESTION_ID QuestionId, + IN UINT8 Type, + IN EFI_IFR_TYPE_VALUE *Value, + OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest + ) +{ + EFI_STATUS Status; + IP4_CONFIG2_INSTANCE *Instance; + IP4_CONFIG2_IFR_NVDATA *IfrFormNvData; + IP4_FORM_CALLBACK_INFO *Private; + + EFI_IP_ADDRESS StationAddress; + EFI_IP_ADDRESS SubnetMask; + EFI_IP_ADDRESS Gateway; + IP4_ADDR Ip; + EFI_IPv4_ADDRESS *DnsAddress; + UINTN DnsCount; + UINTN Index; + EFI_INPUT_KEY Key; + + IfrFormNvData = NULL; + DnsCount = 0; + DnsAddress = NULL; + + if (Action == EFI_BROWSER_ACTION_CHANGED) { + Private = IP4_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS(This); + Instance = IP4_CONFIG2_INSTANCE_FROM_FORM_CALLBACK(Private); + + IfrFormNvData = AllocateZeroPool (sizeof (IP4_CONFIG2_IFR_NVDATA)); + if (IfrFormNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Retrieve uncommitted data from Browser + // + if (!HiiGetBrowserData (&gIp4Config2NvDataGuid, mIp4Config2StorageName, sizeof (IP4_CONFIG2_IFR_NVDATA), (UINT8 *) IfrFormNvData)) { + FreePool (IfrFormNvData); + return EFI_NOT_FOUND; + } + + Status = EFI_SUCCESS; + + switch (QuestionId) { + case KEY_LOCAL_IP: + Status = Ip4Config2StrToIp (IfrFormNvData->StationAddress, &StationAddress.v4); + if (EFI_ERROR (Status) || IP4_IS_UNSPECIFIED (NTOHL (StationAddress.Addr[0])) || IP4_IS_LOCAL_BROADCAST (NTOHL (StationAddress.Addr[0]))) { + CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid IP address!", NULL); + Status = EFI_INVALID_PARAMETER; + } + break; + + case KEY_SUBNET_MASK: + Status = Ip4Config2StrToIp (IfrFormNvData->SubnetMask, &SubnetMask.v4); + if (EFI_ERROR (Status) || ((SubnetMask.Addr[0] != 0) && (GetSubnetMaskPrefixLength (&SubnetMask.v4) == 0))) { + CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Subnet Mask!", NULL); + Status = EFI_INVALID_PARAMETER; + } + break; + + case KEY_GATE_WAY: + Status = Ip4Config2StrToIp (IfrFormNvData->GatewayAddress, &Gateway.v4); + if (EFI_ERROR (Status) || IP4_IS_LOCAL_BROADCAST(NTOHL(Gateway.Addr[0]))) { + CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Gateway!", NULL); + Status = EFI_INVALID_PARAMETER; + } + break; + + case KEY_DNS: + Status = Ip4Config2StrToIpList (IfrFormNvData->DnsAddress, &DnsAddress, &DnsCount); + if (!EFI_ERROR (Status) && DnsCount > 0) { + for (Index = 0; Index < DnsCount; Index ++) { + CopyMem (&Ip, &DnsAddress[Index], sizeof (IP4_ADDR)); + if (IP4_IS_UNSPECIFIED (NTOHL (Ip)) || IP4_IS_LOCAL_BROADCAST (NTOHL (Ip))) { + CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Dns Server!", NULL); + Status = EFI_INVALID_PARAMETER; + break; + } + } + } else { + if (EFI_ERROR (Status)) { + CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Dns Server!", NULL); + } + } + + if(DnsAddress != NULL) { + FreePool(DnsAddress); + } + break; + + case KEY_SAVE_CHANGES: + Status = Ip4Config2ConvertIfrNvDataToConfigNvData (IfrFormNvData, Instance); + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT; + break; + + default: + break; + } + + FreePool (IfrFormNvData); + + return Status; + } + + // + // All other action return unsupported. + // + return EFI_UNSUPPORTED; +} + +/** + Install HII Config Access protocol for network device and allocate resource. + + @param[in, out] Instance The IP4 config2 Instance. + + @retval EFI_SUCCESS The HII Config Access protocol is installed. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +Ip4Config2FormInit ( + IN OUT IP4_CONFIG2_INSTANCE *Instance + ) +{ + EFI_STATUS Status; + IP4_SERVICE *IpSb; + IP4_FORM_CALLBACK_INFO *CallbackInfo; + EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess; + VENDOR_DEVICE_PATH VendorDeviceNode; + EFI_SERVICE_BINDING_PROTOCOL *MnpSb; + CHAR16 *MacString; + CHAR16 MenuString[128]; + CHAR16 PortString[128]; + CHAR16 *OldMenuString; + EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + ASSERT (IpSb != NULL); + + CallbackInfo = &Instance->CallbackInfo; + + CallbackInfo->Signature = IP4_FORM_CALLBACK_INFO_SIGNATURE; + + Status = gBS->HandleProtocol ( + IpSb->Controller, + &gEfiDevicePathProtocolGuid, + (VOID **) &ParentDevicePath + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Construct device path node for EFI HII Config Access protocol, + // which consists of controller physical device path and one hardware + // vendor guid node. + // + ZeroMem (&VendorDeviceNode, sizeof (VENDOR_DEVICE_PATH)); + VendorDeviceNode.Header.Type = HARDWARE_DEVICE_PATH; + VendorDeviceNode.Header.SubType = HW_VENDOR_DP; + + CopyGuid (&VendorDeviceNode.Guid, &gEfiCallerIdGuid); + + SetDevicePathNodeLength (&VendorDeviceNode.Header, sizeof (VENDOR_DEVICE_PATH)); + CallbackInfo->HiiVendorDevicePath = AppendDevicePathNode ( + ParentDevicePath, + (EFI_DEVICE_PATH_PROTOCOL *) &VendorDeviceNode + ); + if (CallbackInfo->HiiVendorDevicePath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + ConfigAccess = &CallbackInfo->HiiConfigAccessProtocol; + ConfigAccess->ExtractConfig = Ip4FormExtractConfig; + ConfigAccess->RouteConfig = Ip4FormRouteConfig; + ConfigAccess->Callback = Ip4FormCallback; + + // + // Install Device Path Protocol and Config Access protocol on new handle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &CallbackInfo->ChildHandle, + &gEfiDevicePathProtocolGuid, + CallbackInfo->HiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + ConfigAccess, + NULL + ); + + if (!EFI_ERROR (Status)) { + // + // Open the Parent Handle for the child + // + Status = gBS->OpenProtocol ( + IpSb->Controller, + &gEfiManagedNetworkServiceBindingProtocolGuid, + (VOID **) &MnpSb, + IpSb->Image, + CallbackInfo->ChildHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + } + + if (EFI_ERROR (Status)) { + goto Error; + } + + // + // Publish our HII data + // + CallbackInfo->RegisteredHandle = HiiAddPackages ( + &gIp4Config2NvDataGuid, + CallbackInfo->ChildHandle, + Ip4DxeStrings, + Ip4Config2Bin, + NULL + ); + if (CallbackInfo->RegisteredHandle == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + // + // Append MAC string in the menu help string and tile help string + // + Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &MacString); + if (!EFI_ERROR (Status)) { + OldMenuString = HiiGetString ( + CallbackInfo->RegisteredHandle, + STRING_TOKEN (STR_IP4_CONFIG2_FORM_HELP), + NULL + ); + UnicodeSPrint (MenuString, 128, L"%s (MAC:%s)", OldMenuString, MacString); + HiiSetString ( + CallbackInfo->RegisteredHandle, + STRING_TOKEN (STR_IP4_CONFIG2_FORM_HELP), + MenuString, + NULL + ); + + UnicodeSPrint (PortString, 128, L"MAC:%s", MacString); + HiiSetString ( + CallbackInfo->RegisteredHandle, + STRING_TOKEN (STR_IP4_DEVICE_FORM_HELP), + PortString, + NULL + ); + + FreePool (MacString); + FreePool (OldMenuString); + + return EFI_SUCCESS; + } + +Error: + Ip4Config2FormUnload (Instance); + return Status; +} + +/** + Uninstall the HII Config Access protocol for network devices and free up the resources. + + @param[in, out] Instance The IP4 config2 instance to unload a form. + +**/ +VOID +Ip4Config2FormUnload ( + IN OUT IP4_CONFIG2_INSTANCE *Instance + ) +{ + IP4_SERVICE *IpSb; + IP4_FORM_CALLBACK_INFO *CallbackInfo; + IP4_CONFIG2_NVDATA *Ip4NvData; + + IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance); + ASSERT (IpSb != NULL); + + CallbackInfo = &Instance->CallbackInfo; + + if (CallbackInfo->ChildHandle != NULL) { + // + // Close the child handle + // + gBS->CloseProtocol ( + IpSb->Controller, + &gEfiManagedNetworkServiceBindingProtocolGuid, + IpSb->Image, + CallbackInfo->ChildHandle + ); + + // + // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL + // + gBS->UninstallMultipleProtocolInterfaces ( + CallbackInfo->ChildHandle, + &gEfiDevicePathProtocolGuid, + CallbackInfo->HiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &CallbackInfo->HiiConfigAccessProtocol, + NULL + ); + } + + if (CallbackInfo->HiiVendorDevicePath != NULL) { + FreePool (CallbackInfo->HiiVendorDevicePath); + } + + if (CallbackInfo->RegisteredHandle != NULL) { + // + // Remove HII package list + // + HiiRemovePackages (CallbackInfo->RegisteredHandle); + } + + Ip4NvData = &Instance->Ip4NvData; + + if(Ip4NvData->ManualAddress != NULL) { + FreePool(Ip4NvData->ManualAddress); + } + + if(Ip4NvData->GatewayAddress != NULL) { + FreePool(Ip4NvData->GatewayAddress); + } + + if(Ip4NvData->DnsAddress != NULL) { + FreePool(Ip4NvData->DnsAddress); + } + + Ip4NvData->ManualAddressCount = 0; + Ip4NvData->GatewayAddressCount = 0; + Ip4NvData->DnsAddressCount = 0; +} diff --git a/NetworkPkg/Ip4Dxe/Ip4Config2Nv.h b/NetworkPkg/Ip4Dxe/Ip4Config2Nv.h new file mode 100644 index 000000000..f453e19e9 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Config2Nv.h @@ -0,0 +1,45 @@ +/** @file + The header file of IP4Config2Nv.c + +Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _IP4_CONFIG2NV_H_ +#define _IP4_CONFIG2NV_H_ + +#include "Ip4Impl.h" + +extern UINT8 Ip4Config2Bin[]; +extern UINT8 Ip4DxeStrings[]; + +#define NIC_ITEM_CONFIG_SIZE (sizeof (IP4_CONFIG2_INSTANCE) + (sizeof (EFI_IPv4_ADDRESS) * MAX_IP4_CONFIG_DNS)) + +/** + Install HII Config Access protocol for network device and allocate resource. + + @param[in, out] Instance The IP4 config2 Instance. + + @retval EFI_SUCCESS The HII Config Access protocol is installed. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +Ip4Config2FormInit ( + IN OUT IP4_CONFIG2_INSTANCE *Instance + ); + +/** + Uninstall the HII Config Access protocol for network devices and free up the resources. + + @param[in, out] Instance The IP4 config2 instance to unload a form. + +**/ +VOID +Ip4Config2FormUnload ( + IN OUT IP4_CONFIG2_INSTANCE *Instance + ); + +#endif diff --git a/NetworkPkg/Ip4Dxe/Ip4Driver.c b/NetworkPkg/Ip4Dxe/Ip4Driver.c new file mode 100644 index 000000000..ebd4dec1d --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Driver.c @@ -0,0 +1,1069 @@ +/** @file + The driver binding and service binding protocol for IP4 driver. + +Copyright (c) 2005 - 2019, Intel Corporation. All rights reserved.
+(C) Copyright 2015 Hewlett-Packard Development Company, L.P.
+ +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + +EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding = { + Ip4DriverBindingSupported, + Ip4DriverBindingStart, + Ip4DriverBindingStop, + 0xa, + NULL, + NULL +}; + +BOOLEAN mIpSec2Installed = FALSE; + +/** + Callback function for IpSec2 Protocol install. + + @param[in] Event Event whose notification function is being invoked + @param[in] Context Pointer to the notification function's context + +**/ +VOID +EFIAPI +IpSec2InstalledCallback ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + // + // Test if protocol was even found. + // Notification function will be called at least once. + // + Status = gBS->LocateProtocol (&gEfiIpSec2ProtocolGuid, NULL, (VOID **)&mIpSec); + if (Status == EFI_SUCCESS && mIpSec != NULL) { + // + // Close the event so it does not get called again. + // + gBS->CloseEvent (Event); + + mIpSec2Installed = TRUE; + } +} + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + The entry point for IP4 driver which install the driver + binding and component name protocol on its image. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +Ip4DriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + VOID *Registration; + + EfiCreateProtocolNotifyEvent ( + &gEfiIpSec2ProtocolGuid, + TPL_CALLBACK, + IpSec2InstalledCallback, + NULL, + &Registration + ); + + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gIp4DriverBinding, + ImageHandle, + &gIp4ComponentName, + &gIp4ComponentName2 + ); +} + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device + @retval EFI_ALREADY_STARTED This driver is already running on this device + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Ip4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL * This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // Test for the MNP service binding Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Test for the Arp service binding Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiArpServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + return Status; +} + +/** + Clean up a IP4 service binding instance. It will release all + the resource allocated by the instance. The instance may be + partly initialized, or partly destroyed. If a resource is + destroyed, it is marked as that in case the destroy failed and + being called again later. + + @param[in] IpSb The IP4 service binding instance to clean up + + @retval EFI_SUCCESS The resource used by the instance are cleaned up + @retval other Failed to clean up some of the resources. + +**/ +EFI_STATUS +Ip4CleanService ( + IN IP4_SERVICE *IpSb + ); + + +/** + Create a new IP4 driver service binding private instance. + + @param Controller The controller that has MNP service binding + installed + @param ImageHandle The IP4 driver's image handle + @param Service The variable to receive the newly created IP4 + service. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource + @retval EFI_SUCCESS A new IP4 service binding private is created. + @retval other Other error occurs. + +**/ +EFI_STATUS +Ip4CreateService ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE ImageHandle, + OUT IP4_SERVICE **Service + ) +{ + IP4_SERVICE *IpSb; + EFI_STATUS Status; + + ASSERT (Service != NULL); + + *Service = NULL; + + // + // allocate a service private data then initialize all the filed to + // empty resources, so if any thing goes wrong when allocating + // resources, Ip4CleanService can be called to clean it up. + // + IpSb = AllocateZeroPool (sizeof (IP4_SERVICE)); + + if (IpSb == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + IpSb->Signature = IP4_SERVICE_SIGNATURE; + IpSb->ServiceBinding.CreateChild = Ip4ServiceBindingCreateChild; + IpSb->ServiceBinding.DestroyChild = Ip4ServiceBindingDestroyChild; + IpSb->State = IP4_SERVICE_UNSTARTED; + + IpSb->NumChildren = 0; + InitializeListHead (&IpSb->Children); + + InitializeListHead (&IpSb->Interfaces); + IpSb->DefaultInterface = NULL; + IpSb->DefaultRouteTable = NULL; + + Ip4InitAssembleTable (&IpSb->Assemble); + + IpSb->IgmpCtrl.Igmpv1QuerySeen = 0; + InitializeListHead (&IpSb->IgmpCtrl.Groups); + + IpSb->Image = ImageHandle; + IpSb->Controller = Controller; + + IpSb->MnpChildHandle = NULL; + IpSb->Mnp = NULL; + + IpSb->MnpConfigData.ReceivedQueueTimeoutValue = 0; + IpSb->MnpConfigData.TransmitQueueTimeoutValue = 0; + IpSb->MnpConfigData.ProtocolTypeFilter = IP4_ETHER_PROTO; + IpSb->MnpConfigData.EnableUnicastReceive = TRUE; + IpSb->MnpConfigData.EnableMulticastReceive = TRUE; + IpSb->MnpConfigData.EnableBroadcastReceive = TRUE; + IpSb->MnpConfigData.EnablePromiscuousReceive = FALSE; + IpSb->MnpConfigData.FlushQueuesOnReset = TRUE; + IpSb->MnpConfigData.EnableReceiveTimestamps = FALSE; + IpSb->MnpConfigData.DisableBackgroundPolling = FALSE; + + ZeroMem (&IpSb->SnpMode, sizeof (EFI_SIMPLE_NETWORK_MODE)); + + IpSb->Timer = NULL; + IpSb->ReconfigCheckTimer = NULL; + + IpSb->ReconfigEvent = NULL; + + IpSb->Reconfig = FALSE; + + IpSb->MediaPresent = TRUE; + + // + // Create various resources. First create the route table, timer + // event, ReconfigEvent and MNP child. IGMP, interface's initialization depend + // on the MNP child. + // + IpSb->DefaultRouteTable = Ip4CreateRouteTable (); + + if (IpSb->DefaultRouteTable == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + Ip4TimerTicking, + IpSb, + &IpSb->Timer + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + Ip4TimerReconfigChecking, + IpSb, + &IpSb->ReconfigCheckTimer + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Ip4AutoReconfigCallBack, + IpSb, + &IpSb->ReconfigEvent + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = NetLibCreateServiceChild ( + Controller, + ImageHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + &IpSb->MnpChildHandle + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + IpSb->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **) &IpSb->Mnp, + ImageHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = Ip4ServiceConfigMnp (IpSb, TRUE); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &IpSb->SnpMode); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = Ip4InitIgmp (IpSb); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + IpSb->MacString = NULL; + Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &IpSb->MacString); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + IpSb->DefaultInterface = Ip4CreateInterface (IpSb->Mnp, Controller, ImageHandle); + + if (IpSb->DefaultInterface == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link); + + ZeroMem (&IpSb->Ip4Config2Instance, sizeof (IP4_CONFIG2_INSTANCE)); + + Status = Ip4Config2InitInstance (&IpSb->Ip4Config2Instance); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + IpSb->MaxPacketSize = IpSb->SnpMode.MaxPacketSize - sizeof (IP4_HEAD); + if (NetLibGetVlanId (IpSb->Controller) != 0) { + // + // This is a VLAN device, reduce MTU by VLAN tag length + // + IpSb->MaxPacketSize -= NET_VLAN_TAG_LEN; + } + IpSb->OldMaxPacketSize = IpSb->MaxPacketSize; + *Service = IpSb; + + return EFI_SUCCESS; + +ON_ERROR: + Ip4CleanService (IpSb); + FreePool (IpSb); + + return Status; +} + + +/** + Clean up a IP4 service binding instance. It will release all + the resource allocated by the instance. The instance may be + partly initialized, or partly destroyed. If a resource is + destroyed, it is marked as that in case the destroy failed and + being called again later. + + @param[in] IpSb The IP4 service binding instance to clean up + + @retval EFI_SUCCESS The resource used by the instance are cleaned up + @retval other Failed to clean up some of the resources. + +**/ +EFI_STATUS +Ip4CleanService ( + IN IP4_SERVICE *IpSb + ) +{ + EFI_STATUS Status; + + IpSb->State = IP4_SERVICE_DESTROY; + + if (IpSb->Timer != NULL) { + gBS->SetTimer (IpSb->Timer, TimerCancel, 0); + gBS->CloseEvent (IpSb->Timer); + + IpSb->Timer = NULL; + } + + if (IpSb->ReconfigCheckTimer != NULL) { + gBS->SetTimer (IpSb->ReconfigCheckTimer, TimerCancel, 0); + gBS->CloseEvent (IpSb->ReconfigCheckTimer); + + IpSb->ReconfigCheckTimer = NULL; + } + + if (IpSb->DefaultInterface != NULL) { + Status = Ip4FreeInterface (IpSb->DefaultInterface, NULL); + + if (EFI_ERROR (Status)) { + return Status; + } + + IpSb->DefaultInterface = NULL; + } + + if (IpSb->DefaultRouteTable != NULL) { + Ip4FreeRouteTable (IpSb->DefaultRouteTable); + IpSb->DefaultRouteTable = NULL; + } + + Ip4CleanAssembleTable (&IpSb->Assemble); + + if (IpSb->MnpChildHandle != NULL) { + if (IpSb->Mnp != NULL) { + gBS->CloseProtocol ( + IpSb->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + IpSb->Image, + IpSb->Controller + ); + + IpSb->Mnp = NULL; + } + + NetLibDestroyServiceChild ( + IpSb->Controller, + IpSb->Image, + &gEfiManagedNetworkServiceBindingProtocolGuid, + IpSb->MnpChildHandle + ); + + IpSb->MnpChildHandle = NULL; + } + + if (IpSb->ReconfigEvent != NULL) { + gBS->CloseEvent (IpSb->ReconfigEvent); + + IpSb->ReconfigEvent = NULL; + } + + IpSb->Reconfig = FALSE; + + if (IpSb->MacString != NULL) { + FreePool (IpSb->MacString); + } + + Ip4Config2CleanInstance (&IpSb->Ip4Config2Instance); + + return EFI_SUCCESS; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +Ip4DestroyChildEntryInHandleBuffer ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + IP4_PROTOCOL *IpInstance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE); + ServiceBinding = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding; + NumberOfChildren = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren; + ChildHandleBuffer = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer; + + if (!NetIsInHandleBuffer (IpInstance->Handle, NumberOfChildren, ChildHandleBuffer)) { + return EFI_SUCCESS; + } + + return ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle); +} + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Ip4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + IP4_SERVICE *IpSb; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Cfg2; + UINTN Index; + IP4_CONFIG2_DATA_ITEM *DataItem; + + IpSb = NULL; + Ip4Cfg2 = NULL; + DataItem = NULL; + + // + // Test for the Ip4 service binding protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiIp4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + if (Status == EFI_SUCCESS) { + return EFI_ALREADY_STARTED; + } + + Status = Ip4CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb); + + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (IpSb != NULL); + + Ip4Cfg2 = &IpSb->Ip4Config2Instance.Ip4Config2; + + // + // Install the Ip4ServiceBinding Protocol onto ControlerHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiIp4ServiceBindingProtocolGuid, + &IpSb->ServiceBinding, + &gEfiIp4Config2ProtocolGuid, + Ip4Cfg2, + NULL + ); + + if (EFI_ERROR (Status)) { + goto FREE_SERVICE; + } + + // + // Read the config data from NV variable again. + // The default data can be changed by other drivers. + // + Status = Ip4Config2ReadConfigData (IpSb->MacString, &IpSb->Ip4Config2Instance); + if (EFI_ERROR (Status)) { + goto UNINSTALL_PROTOCOL; + } + + // + // Consume the installed EFI_IP4_CONFIG2_PROTOCOL to set the default data items. + // + for (Index = Ip4Config2DataTypePolicy; Index < Ip4Config2DataTypeMaximum; Index++) { + DataItem = &IpSb->Ip4Config2Instance.DataItem[Index]; + if (DataItem->Data.Ptr != NULL) { + Status = Ip4Cfg2->SetData ( + Ip4Cfg2, + Index, + DataItem->DataSize, + DataItem->Data.Ptr + ); + if (EFI_ERROR(Status)) { + goto UNINSTALL_PROTOCOL; + } + + if (Index == Ip4Config2DataTypePolicy && (*(DataItem->Data.Policy) == Ip4Config2PolicyDhcp)) { + break; + } + } + } + + // + // Ready to go: start the receiving and timer. + // Ip4Config2SetPolicy maybe call Ip4ReceiveFrame() to set the default interface's RecvRequest first after + // Ip4Config2 instance is initialized. So, EFI_ALREADY_STARTED is the allowed return status. + // + Status = Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb); + + if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) { + goto UNINSTALL_PROTOCOL; + } + + Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_SECOND); + + if (EFI_ERROR (Status)) { + goto UNINSTALL_PROTOCOL; + } + + Status = gBS->SetTimer (IpSb->ReconfigCheckTimer, TimerPeriodic, 500 * TICKS_PER_MS); + + if (EFI_ERROR (Status)) { + goto UNINSTALL_PROTOCOL; + } + + // + // Initialize the IP4 ID + // + mIp4Id = (UINT16)NET_RANDOM (NetRandomInitSeed ()); + + return Status; + +UNINSTALL_PROTOCOL: + gBS->UninstallMultipleProtocolInterfaces ( + ControllerHandle, + &gEfiIp4ServiceBindingProtocolGuid, + &IpSb->ServiceBinding, + &gEfiIp4Config2ProtocolGuid, + Ip4Cfg2, + NULL + ); + +FREE_SERVICE: + Ip4CleanService (IpSb); + FreePool (IpSb); + return Status; +} + + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number + of children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +Ip4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + IP4_SERVICE *IpSb; + EFI_HANDLE NicHandle; + EFI_STATUS Status; + INTN State; + LIST_ENTRY *List; + IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context; + IP4_INTERFACE *IpIf; + IP4_ROUTE_TABLE *RouteTable; + + BOOLEAN IsDhcp4; + + IsDhcp4 = FALSE; + + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp4ProtocolGuid); + if (NicHandle != NULL) { + IsDhcp4 = TRUE; + } else { + return EFI_SUCCESS; + } + } + } + + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiIp4ServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + NicHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding); + + if (IsDhcp4) { + Status = Ip4Config2DestroyDhcp4 (&IpSb->Ip4Config2Instance); + gBS->CloseEvent (IpSb->Ip4Config2Instance.Dhcp4Event); + IpSb->Ip4Config2Instance.Dhcp4Event = NULL; + } else if (NumberOfChildren != 0) { + List = &IpSb->Children; + Context.ServiceBinding = ServiceBinding; + Context.NumberOfChildren = NumberOfChildren; + Context.ChildHandleBuffer = ChildHandleBuffer; + Status = NetDestroyLinkList ( + List, + Ip4DestroyChildEntryInHandleBuffer, + &Context, + NULL + ); + } else if (IpSb->DefaultInterface->ArpHandle == ControllerHandle) { + + // + // The ARP protocol for the default interface is being uninstalled and all + // its IP child handles should have been destroyed before. So, release the + // default interface and route table, create a new one and mark it as not started. + // + Ip4CancelReceive (IpSb->DefaultInterface); + Ip4FreeInterface (IpSb->DefaultInterface, NULL); + Ip4FreeRouteTable (IpSb->DefaultRouteTable); + + IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image); + if (IpIf == NULL) { + goto ON_ERROR; + } + RouteTable = Ip4CreateRouteTable (); + if (RouteTable == NULL) { + Ip4FreeInterface (IpIf, NULL); + goto ON_ERROR;; + } + + IpSb->DefaultInterface = IpIf; + InsertHeadList (&IpSb->Interfaces, &IpIf->Link); + IpSb->DefaultRouteTable = RouteTable; + Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb); + + IpSb->State = IP4_SERVICE_UNSTARTED; + + } else if (IsListEmpty (&IpSb->Children)) { + State = IpSb->State; + // + // OK, clean other resources then uninstall the service binding protocol. + // + Status = Ip4CleanService (IpSb); + if (EFI_ERROR (Status)) { + IpSb->State = State; + goto ON_ERROR; + } + + gBS->UninstallMultipleProtocolInterfaces ( + NicHandle, + &gEfiIp4ServiceBindingProtocolGuid, + ServiceBinding, + &gEfiIp4Config2ProtocolGuid, + &IpSb->Ip4Config2Instance.Ip4Config2, + NULL + ); + + if (gIp4ControllerNameTable != NULL) { + FreeUnicodeStringTable (gIp4ControllerNameTable); + gIp4ControllerNameTable = NULL; + } + FreePool (IpSb); + } + +ON_ERROR: + return Status; +} + + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +Ip4ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ) +{ + IP4_SERVICE *IpSb; + IP4_PROTOCOL *IpInstance; + EFI_TPL OldTpl; + EFI_STATUS Status; + VOID *Mnp; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + IpSb = IP4_SERVICE_FROM_PROTOCOL (This); + IpInstance = AllocatePool (sizeof (IP4_PROTOCOL)); + + if (IpInstance == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Ip4InitProtocol (IpSb, IpInstance); + + // + // Install Ip4 onto ChildHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiIp4ProtocolGuid, + &IpInstance->Ip4Proto, + NULL + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + IpInstance->Handle = *ChildHandle; + + // + // Open the Managed Network protocol BY_CHILD. + // + Status = gBS->OpenProtocol ( + IpSb->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **) &Mnp, + gIp4DriverBinding.DriverBindingHandle, + IpInstance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + gBS->UninstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiIp4ProtocolGuid, + &IpInstance->Ip4Proto, + NULL + ); + + goto ON_ERROR; + } + + // + // Insert it into the service binding instance. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&IpSb->Children, &IpInstance->Link); + IpSb->NumChildren++; + + gBS->RestoreTPL (OldTpl); + +ON_ERROR: + + if (EFI_ERROR (Status)) { + + Ip4CleanProtocol (IpInstance); + + FreePool (IpInstance); + } + + return Status; +} + + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Ip4ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + EFI_STATUS Status; + IP4_SERVICE *IpSb; + IP4_PROTOCOL *IpInstance; + EFI_IP4_PROTOCOL *Ip4; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Retrieve the private context data structures + // + IpSb = IP4_SERVICE_FROM_PROTOCOL (This); + + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiIp4ProtocolGuid, + (VOID **) &Ip4, + gIp4DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + IpInstance = IP4_INSTANCE_FROM_PROTOCOL (Ip4); + + if (IpInstance->Service != IpSb) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // A child can be destroyed more than once. For example, + // Ip4DriverBindingStop will destroy all of its children. + // when UDP driver is being stopped, it will destroy all + // the IP child it opens. + // + if (IpInstance->InDestroy) { + gBS->RestoreTPL (OldTpl); + return EFI_SUCCESS; + } + + IpInstance->InDestroy = TRUE; + + // + // Close the Managed Network protocol. + // + gBS->CloseProtocol ( + IpSb->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + gIp4DriverBinding.DriverBindingHandle, + ChildHandle + ); + + if (IpInstance->Interface != NULL && IpInstance->Interface->Arp != NULL) { + gBS->CloseProtocol ( + IpInstance->Interface->ArpHandle, + &gEfiArpProtocolGuid, + gIp4DriverBinding.DriverBindingHandle, + ChildHandle + ); + } + + // + // Uninstall the IP4 protocol first. Many thing happens during + // this: + // 1. The consumer of the IP4 protocol will be stopped if it + // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is + // stopped, IP driver's stop function will be called, and uninstall + // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This + // makes it possible to create the network stack bottom up, and + // stop it top down. + // 2. the upper layer will recycle the received packet. The recycle + // event's TPL is higher than this function. The recycle events + // will be called back before preceeding. If any packets not recycled, + // that means there is a resource leak. + // + gBS->RestoreTPL (OldTpl); + Status = gBS->UninstallProtocolInterface ( + ChildHandle, + &gEfiIp4ProtocolGuid, + &IpInstance->Ip4Proto + ); + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + if (EFI_ERROR (Status)) { + IpInstance->InDestroy = FALSE; + goto ON_ERROR; + } + + Status = Ip4CleanProtocol (IpInstance); + if (EFI_ERROR (Status)) { + gBS->InstallMultipleProtocolInterfaces ( + &ChildHandle, + &gEfiIp4ProtocolGuid, + Ip4, + NULL + ); + + goto ON_ERROR; + } + + RemoveEntryList (&IpInstance->Link); + IpSb->NumChildren--; + + gBS->RestoreTPL (OldTpl); + + FreePool (IpInstance); + return EFI_SUCCESS; + +ON_ERROR: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/Ip4Dxe/Ip4Driver.h b/NetworkPkg/Ip4Dxe/Ip4Driver.h new file mode 100644 index 000000000..be3747157 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Driver.h @@ -0,0 +1,184 @@ +/** @file + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP4_DRIVER_H__ +#define __EFI_IP4_DRIVER_H__ + +#include + +extern EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding; +extern EFI_COMPONENT_NAME_PROTOCOL gIp4ComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gIp4ComponentName2; +extern EFI_UNICODE_STRING_TABLE *gIp4ControllerNameTable; + +typedef struct { + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; +} IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT; + +// +// Function prototype for the driver's entry point +// +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + The entry point for IP4 driver which install the driver + binding and component name protocol on its image. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +Ip4DriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ); + +// +// Function prototypes for the Drivr Binding Protocol +// +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device + @retval EFI_ALREADY_STARTED This driver is already running on this device + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Ip4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL * This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Ip4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL * This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number + of children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +Ip4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +// +// Function prototypes for the ServiceBinding Protocol +// +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +Ip4ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Ip4ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); +#endif diff --git a/NetworkPkg/Ip4Dxe/Ip4Dxe.inf b/NetworkPkg/Ip4Dxe/Ip4Dxe.inf new file mode 100644 index 000000000..852932f3c --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Dxe.inf @@ -0,0 +1,110 @@ +## @file +# This module produces EFI IPv4 Protocol and EFI IPv4 Service Binding Protocol. +# +# This module produces EFI IPv4 Protocol upon EFI MNP Protocol and EFI ARP Protocol, +# to provide basic network IPv4 packet I/O services, which includes support for a +# subset of the Internet Control Message Protocol (ICMP) and may include support for +# the Internet Group Management Protocol (IGMP). +# +# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = Ip4Dxe + MODULE_UNI_FILE = Ip4Dxe.uni + FILE_GUID = 9FB1A1F3-3B71-4324-B39A-745CBB015FFF + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = Ip4DriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = gIp4DriverBinding +# COMPONENT_NAME = gIp4ComponentName +# COMPONENT_NAME2 = gIp4ComponentName2 +# + +[Sources] + Ip4Driver.c + Ip4Option.h + Ip4Route.h + Ip4If.c + Ip4Igmp.h + Ip4Output.c + Ip4Icmp.c + Ip4Igmp.c + Ip4Impl.c + Ip4Common.h + Ip4Impl.h + Ip4Driver.h + Ip4Common.c + Ip4If.h + Ip4Option.c + Ip4Output.h + ComponentName.c + Ip4Input.h + Ip4Route.c + Ip4Icmp.h + Ip4Input.c + Ip4Config2Impl.c + Ip4Config2Impl.h + Ip4Config2.vfr + Ip4DxeStrings.uni + Ip4NvData.h + Ip4Config2Nv.h + Ip4Config2Nv.c + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + UefiLib + BaseLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiRuntimeServicesTableLib + DebugLib + NetLib + DpcLib + HiiLib + PrintLib + DevicePathLib + UefiHiiServicesLib + +[Protocols] + ## BY_START + ## UNDEFINED # variable + gEfiIp4ServiceBindingProtocolGuid + gEfiIp4ProtocolGuid ## BY_START + gEfiManagedNetworkServiceBindingProtocolGuid ## TO_START + gEfiManagedNetworkProtocolGuid ## TO_START + gEfiArpServiceBindingProtocolGuid ## TO_START + gEfiIp4Config2ProtocolGuid ## BY_START + gEfiArpProtocolGuid ## TO_START + gEfiDhcp4ServiceBindingProtocolGuid ## TO_START + gEfiDhcp4ProtocolGuid ## TO_START + gEfiIpSec2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiHiiConfigAccessProtocolGuid ## BY_START + gEfiDevicePathProtocolGuid ## TO_START + +[Guids] + ## SOMETIMES_CONSUMES ## GUID # HiiIsConfigHdrMatch EFI_NIC_IP4_CONFIG_VARIABLE + ## SOMETIMES_PRODUCES ## GUID # HiiConstructConfigHdr EFI_NIC_IP4_CONFIG_VARIABLE + ## SOMETIMES_PRODUCES ## GUID # HiiGetBrowserData EFI_NIC_IP4_CONFIG_VARIABLE + ## SOMETIMES_CONSUMES ## HII + gIp4Config2NvDataGuid + +[UserExtensions.TianoCore."ExtraFiles"] + Ip4DxeExtra.uni + diff --git a/NetworkPkg/Ip4Dxe/Ip4Dxe.uni b/NetworkPkg/Ip4Dxe/Ip4Dxe.uni new file mode 100644 index 000000000..380d07da0 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Dxe.uni @@ -0,0 +1,19 @@ +// /** @file +// This module produces EFI IPv4 Protocol and EFI IPv4 Service Binding Protocol. +// +// This module produces EFI IPv4 Protocol upon EFI MNP Protocol and EFI ARP Protocol, +// to provide basic network IPv4 packet I/O services, which includes support for a +// subset of the Internet Control Message Protocol (ICMP) and may include support for +// the Internet Group Management Protocol (IGMP). +// +// Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Produces EFI IPv4 Protocol and EFI IPv4 Service Binding Protocol" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces EFI IPv4 Protocol upon EFI MNP Protocol and EFI ARP Protocol to provide basic network IPv4 packet I/O services, which includes support for a subset of the Internet Control Message Protocol (ICMP), and may include support for the Internet Group Management Protocol (IGMP)." + diff --git a/NetworkPkg/Ip4Dxe/Ip4DxeExtra.uni b/NetworkPkg/Ip4Dxe/Ip4DxeExtra.uni new file mode 100644 index 000000000..dbbf9d5f9 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4DxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// Ip4Dxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"IP v4 DXE Driver" + + diff --git a/NetworkPkg/Ip4Dxe/Ip4DxeStrings.uni b/NetworkPkg/Ip4Dxe/Ip4DxeStrings.uni new file mode 100644 index 000000000..c262298f0 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4DxeStrings.uni @@ -0,0 +1,30 @@ +// /** @file +// String definitions for Ip4Config2 formset + +// Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+// SPDX-License-Identifier: BSD-2-Clause-Patent +// +//**/ + + +/=# + +#langdef en-US "English" + +#string STR_IP4_CONFIG2_FORM_TITLE #language en-US "IPv4 Network Configuration" +#string STR_IP4_CONFIG2_FORM_HELP #language en-US "Configure network parameters." +#string STR_IP4_DEVICE_FORM_TITLE #language en-US "" +#string STR_IP4_DEVICE_FORM_HELP #language en-US "" +#string STR_IP4_CONFIGURE #language en-US "Configured" +#string STR_IP4_CONFIGURE_HELP #language en-US "Indicate whether network address configured successfully or not." +#string STR_IP4_ENABLE_DHCP #language en-US "Enable DHCP" +#string STR_IP4_LOCAL_IP_ADDRESS #language en-US "Local IP Address" +#string STR_IP4_IP_ADDRESS_HELP #language en-US "Enter IP address in dotted-decimal notation. Example: 192.168.10.12\r\n" +#string STR_IP4_LOCAL_MASK #language en-US "Local NetMask" +#string STR_IP4_MASK_HELP #language en-US "Enter NetMask in dotted-decimal notation. Example: 255.255.255.0\r\n" +#string STR_IP4_LOCAL_GATEWAY #language en-US "Local Gateway" +#string STR_IP4_GATEWAY_HELP #language en-US "Enter Gateway in dotted-decimal notation. Example: 192.168.10.1\r\n" +#string STR_IP4_LOCAL_DNS #language en-US "Local DNS Servers" +#string STR_IP4_DNS_HELP #language en-US "Enter DNS Servers in dotted-decimal notation. Example: 192.168.10.8 192.168.10.9\r\n" +#string STR_SAVE_CHANGES #language en-US "Save Changes and Exit" +#string STR_NULL #language en-US "" diff --git a/NetworkPkg/Ip4Dxe/Ip4Icmp.c b/NetworkPkg/Ip4Dxe/Ip4Icmp.c new file mode 100644 index 000000000..052d6b77f --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Icmp.c @@ -0,0 +1,363 @@ +/** @file + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + +IP4_ICMP_CLASS +mIcmpClass[] = { + {ICMP_ECHO_REPLY, ICMP_QUERY_MESSAGE }, + {1, ICMP_INVALID_MESSAGE}, + {2, ICMP_INVALID_MESSAGE}, + {ICMP_DEST_UNREACHABLE, ICMP_ERROR_MESSAGE }, + {ICMP_SOURCE_QUENCH, ICMP_ERROR_MESSAGE }, + {ICMP_REDIRECT, ICMP_ERROR_MESSAGE }, + {6, ICMP_INVALID_MESSAGE}, + {7, ICMP_INVALID_MESSAGE}, + {ICMP_ECHO_REQUEST, ICMP_QUERY_MESSAGE }, + {9, ICMP_INVALID_MESSAGE}, + {10, ICMP_INVALID_MESSAGE}, + {ICMP_TIME_EXCEEDED, ICMP_ERROR_MESSAGE }, + {ICMP_PARAMETER_PROBLEM, ICMP_ERROR_MESSAGE }, + {ICMP_TIMESTAMP , ICMP_QUERY_MESSAGE }, + {14, ICMP_INVALID_MESSAGE}, + {ICMP_INFO_REQUEST , ICMP_QUERY_MESSAGE }, + {ICMP_INFO_REPLY , ICMP_QUERY_MESSAGE }, +}; + +EFI_IP4_ICMP_TYPE +mIp4SupportedIcmp[23] = { + {ICMP_ECHO_REPLY, ICMP_DEFAULT_CODE }, + + {ICMP_DEST_UNREACHABLE, ICMP_NET_UNREACHABLE }, + {ICMP_DEST_UNREACHABLE, ICMP_HOST_UNREACHABLE }, + {ICMP_DEST_UNREACHABLE, ICMP_PROTO_UNREACHABLE }, + {ICMP_DEST_UNREACHABLE, ICMP_PORT_UNREACHABLE }, + {ICMP_DEST_UNREACHABLE, ICMP_FRAGMENT_FAILED }, + {ICMP_DEST_UNREACHABLE, ICMP_SOURCEROUTE_FAILED }, + {ICMP_DEST_UNREACHABLE, ICMP_NET_UNKNOWN }, + {ICMP_DEST_UNREACHABLE, ICMP_HOST_UNKNOWN }, + {ICMP_DEST_UNREACHABLE, ICMP_SOURCE_ISOLATED }, + {ICMP_DEST_UNREACHABLE, ICMP_NET_PROHIBITED }, + {ICMP_DEST_UNREACHABLE, ICMP_HOST_PROHIBITED }, + {ICMP_DEST_UNREACHABLE, ICMP_NET_UNREACHABLE_TOS }, + {ICMP_DEST_UNREACHABLE, ICMP_HOST_UNREACHABLE_TOS}, + + {ICMP_SOURCE_QUENCH, ICMP_DEFAULT_CODE }, + + {ICMP_REDIRECT, ICMP_NET_REDIRECT }, + {ICMP_REDIRECT, ICMP_HOST_REDIRECT }, + {ICMP_REDIRECT, ICMP_NET_TOS_REDIRECT }, + {ICMP_REDIRECT, ICMP_HOST_TOS_REDIRECT }, + + {ICMP_ECHO_REQUEST, ICMP_DEFAULT_CODE }, + + {ICMP_TIME_EXCEEDED, ICMP_TIMEOUT_IN_TRANSIT }, + {ICMP_TIME_EXCEEDED, ICMP_TIMEOUT_REASSEMBLE }, + + {ICMP_PARAMETER_PROBLEM, ICMP_DEFAULT_CODE }, +}; + + + +/** + Process the ICMP redirect. Find the instance then update + its route cache. + + All kinds of redirect is treated as host redirect as + specified by RFC1122 3.3.1.2: + "Since the subnet mask appropriate to the destination + address is generally not known, a Network Redirect + message SHOULD be treated identically to a Host Redirect + message;" + + @param[in] IpSb The IP4 service binding instance that received + the packet. + @param[in] Head The IP head of the received ICMPpacket. + @param[in] Packet The content of the ICMP redirect packet with IP + head removed. + @param[in] Icmp The buffer to store the ICMP error message if + something is wrong. + + @retval EFI_INVALID_PARAMETER The parameter is invalid + @retval EFI_SUCCESS Successfully updated the route caches + +**/ +EFI_STATUS +Ip4ProcessIcmpRedirect ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet, + IN IP4_ICMP_ERROR_HEAD *Icmp + ) +{ + LIST_ENTRY *Entry; + IP4_PROTOCOL *Ip4Instance; + IP4_ROUTE_CACHE_ENTRY *CacheEntry; + IP4_INTERFACE *IpIf; + IP4_ADDR Gateway; + IP4_ADDR Src; + IP4_ADDR Dst; + + // + // Find the interface whose IP address is the source of the + // orgianl IP packet. + // + IpIf = Ip4FindInterface (IpSb, NTOHL (Icmp->IpHead.Src)); + Gateway = NTOHL (Icmp->Fourth); + + // + // discard the packet if the new gateway address it specifies + // is not on the same connected net through which the Redirect + // arrived. (RFC1122 3.2.2.2). + // + if ((IpIf == NULL) || !IP4_NET_EQUAL (Gateway, IpIf->Ip, IpIf->SubnetMask)) { + NetbufFree (Packet); + return EFI_INVALID_PARAMETER; + } + + // + // Update each IP child's route cache on the interface. + // + NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) { + Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink); + + if (Ip4Instance->RouteTable == NULL) { + continue; + } + + Dst = NTOHL (Icmp->IpHead.Dst); + Src = NTOHL (Icmp->IpHead.Src); + CacheEntry = Ip4FindRouteCache (Ip4Instance->RouteTable, Dst, Src); + + // + // Only update the route cache's gateway if the source of the + // Redirect is the current first-hop gateway + // + if ((CacheEntry != NULL) && (NTOHL (Head->Src) == CacheEntry->NextHop)) { + CacheEntry->NextHop = Gateway; + } + } + + NetbufFree (Packet); + return EFI_SUCCESS; +} + + +/** + Process the ICMP error packet. If it is an ICMP redirect packet, + update call Ip4ProcessIcmpRedirect to update the IP instance's + route cache, otherwise, deliver the packet to upper layer. + + @param[in] IpSb The IP4 service that received the packet. + @param[in] Head The IP4 head of the ICMP error packet + @param[in] Packet The content of the ICMP error with IP4 head + removed. + + @retval EFI_SUCCESS The ICMP error is processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid + @retval Others Failed to process the packet. + +**/ +EFI_STATUS +Ip4ProcessIcmpError ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet + ) +{ + IP4_ICMP_ERROR_HEAD Icmp; + + if (Packet->TotalSize < sizeof (Icmp)) { + NetbufFree (Packet); + return EFI_INVALID_PARAMETER; + } + + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + + // + // If it is an ICMP redirect error, update the route cache + // as RFC1122. Otherwise, demultiplex it to IP instances. + // + if (Icmp.Head.Type == ICMP_REDIRECT) { + return Ip4ProcessIcmpRedirect (IpSb, Head, Packet, &Icmp); + } + + IP4_GET_CLIP_INFO (Packet)->Status = EFI_ICMP_ERROR; + return Ip4Demultiplex (IpSb, Head, Packet, NULL, 0); +} + + +/** + Replay an ICMP echo request. + + @param[in] IpSb The IP4 service that receivd the packet + @param[in] Head The IP4 head of the ICMP error packet + @param[in] Packet The content of the ICMP error with IP4 head + removed. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource. + @retval EFI_SUCCESS The ICMP Echo request is successfully answered. + @retval Others Failed to answer the ICMP echo request. + +**/ +EFI_STATUS +Ip4IcmpReplyEcho ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet + ) +{ + IP4_ICMP_QUERY_HEAD *Icmp; + NET_BUF *Data; + EFI_STATUS Status; + IP4_HEAD ReplyHead; + + // + // make a copy the packet, it is really a bad idea to + // send the MNP's buffer back to MNP. + // + Data = NetbufDuplicate (Packet, NULL, IP4_MAX_HEADLEN); + + if (Data == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Change the ICMP type to echo reply, exchange the source + // and destination, then send it. The source is updated to + // use specific destination. See RFC1122. SRR/RR option + // update is omitted. + // + Icmp = (IP4_ICMP_QUERY_HEAD *) NetbufGetByte (Data, 0, NULL); + ASSERT (Icmp != NULL); + Icmp->Head.Type = ICMP_ECHO_REPLY; + Icmp->Head.Checksum = 0; + Icmp->Head.Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Icmp, Data->TotalSize)); + + ReplyHead.Tos = 0; + ReplyHead.Fragment = 0; + ReplyHead.Ttl = 64; + ReplyHead.Protocol = EFI_IP_PROTO_ICMP; + ReplyHead.Src = 0; + + // + // Ip4Output will select a source for us + // + ReplyHead.Dst = Head->Src; + + Status = Ip4Output ( + IpSb, + NULL, + Data, + &ReplyHead, + NULL, + 0, + IP4_ALLZERO_ADDRESS, + Ip4SysPacketSent, + NULL + ); + if (EFI_ERROR (Status)) { + NetbufFree (Data); + } + +ON_EXIT: + NetbufFree (Packet); + return Status; +} + + +/** + Process the ICMP query message. If it is an ICMP echo + request, answer it. Otherwise deliver it to upper layer. + + @param[in] IpSb The IP4 service that receivd the packet + @param[in] Head The IP4 head of the ICMP query packet + @param[in] Packet The content of the ICMP query with IP4 head + removed. + + @retval EFI_INVALID_PARAMETER The packet is invalid + @retval EFI_SUCCESS The ICMP query message is processed + @retval Others Failed to process ICMP query. + +**/ +EFI_STATUS +Ip4ProcessIcmpQuery ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet + ) +{ + IP4_ICMP_QUERY_HEAD Icmp; + + if (Packet->TotalSize < sizeof (Icmp)) { + NetbufFree (Packet); + return EFI_INVALID_PARAMETER; + } + + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + + if (Icmp.Head.Type == ICMP_ECHO_REQUEST) { + return Ip4IcmpReplyEcho (IpSb, Head, Packet); + } + + return Ip4Demultiplex (IpSb, Head, Packet, NULL, 0); +} + + +/** + Handle the ICMP packet. First validate the message format, + then according to the message types, process it as query or + error packet. + + @param[in] IpSb The IP4 service that receivd the packet. + @param[in] Head The IP4 head of the ICMP query packet. + @param[in] Packet The content of the ICMP query with IP4 head + removed. + + @retval EFI_INVALID_PARAMETER The packet is malformated. + @retval EFI_SUCCESS The ICMP message is successfully processed. + @retval Others Failed to handle ICMP packet. + +**/ +EFI_STATUS +Ip4IcmpHandle ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet + ) +{ + IP4_ICMP_HEAD Icmp; + UINT16 Checksum; + + if (Packet->TotalSize < sizeof (Icmp)) { + goto DROP; + } + + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + + if (Icmp.Type > ICMP_TYPE_MAX) { + goto DROP; + } + + Checksum = (UINT16) (~NetbufChecksum (Packet)); + if ((Icmp.Checksum != 0) && (Checksum != 0)) { + goto DROP; + } + + if (mIcmpClass[Icmp.Type].IcmpClass == ICMP_ERROR_MESSAGE) { + return Ip4ProcessIcmpError (IpSb, Head, Packet); + + } else if (mIcmpClass[Icmp.Type].IcmpClass == ICMP_QUERY_MESSAGE) { + return Ip4ProcessIcmpQuery (IpSb, Head, Packet); + + } + +DROP: + NetbufFree (Packet); + return EFI_INVALID_PARAMETER; +} diff --git a/NetworkPkg/Ip4Dxe/Ip4Icmp.h b/NetworkPkg/Ip4Dxe/Ip4Icmp.h new file mode 100644 index 000000000..922886a96 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Icmp.h @@ -0,0 +1,97 @@ +/** @file + Header file for ICMP protocol. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP4_ICMP_H__ +#define __EFI_IP4_ICMP_H__ + + // + // ICMP type definations + // +#define ICMP_ECHO_REPLY 0 +#define ICMP_DEST_UNREACHABLE 3 +#define ICMP_SOURCE_QUENCH 4 +#define ICMP_REDIRECT 5 +#define ICMP_ECHO_REQUEST 8 +#define ICMP_TIME_EXCEEDED 11 +#define ICMP_PARAMETER_PROBLEM 12 +#define ICMP_TIMESTAMP 13 +#define ICMP_INFO_REQUEST 15 +#define ICMP_INFO_REPLY 16 +#define ICMP_TYPE_MAX ICMP_INFO_REPLY + +#define ICMP_DEFAULT_CODE 0 + + // + // ICMP code definations for ICMP_DEST_UNREACHABLE + // +#define ICMP_NET_UNREACHABLE 0 +#define ICMP_HOST_UNREACHABLE 1 +#define ICMP_PROTO_UNREACHABLE 2 // Host may generate +#define ICMP_PORT_UNREACHABLE 3 // Host may generate +#define ICMP_FRAGMENT_FAILED 4 +#define ICMP_SOURCEROUTE_FAILED 5 // Host may generate +#define ICMP_NET_UNKNOWN 6 +#define ICMP_HOST_UNKNOWN 7 +#define ICMP_SOURCE_ISOLATED 8 +#define ICMP_NET_PROHIBITED 9 +#define ICMP_HOST_PROHIBITED 10 +#define ICMP_NET_UNREACHABLE_TOS 11 +#define ICMP_HOST_UNREACHABLE_TOS 12 + + // + // ICMP code definations for ICMP_TIME_EXCEEDED + // +#define ICMP_TIMEOUT_IN_TRANSIT 0 +#define ICMP_TIMEOUT_REASSEMBLE 1 // Host may generate + + // + // ICMP code definations for ICMP_TIME_EXCEEDED + // +#define ICMP_NET_REDIRECT 0 +#define ICMP_HOST_REDIRECT 1 +#define ICMP_NET_TOS_REDIRECT 2 +#define ICMP_HOST_TOS_REDIRECT 3 + + // + // ICMP message classes, each class of ICMP message shares + // a common message format. INVALID_MESSAGE is only a flag. + // +#define ICMP_INVALID_MESSAGE 0 +#define ICMP_ERROR_MESSAGE 1 +#define ICMP_QUERY_MESSAGE 2 + +typedef struct { + UINT8 IcmpType; + UINT8 IcmpClass; +} IP4_ICMP_CLASS; + +extern IP4_ICMP_CLASS mIcmpClass[]; +extern EFI_IP4_ICMP_TYPE mIp4SupportedIcmp[]; + +/** + Handle the ICMP packet. First validate the message format, + then according to the message types, process it as query or + error packet. + + @param[in] IpSb The IP4 service that receivd the packet. + @param[in] Head The IP4 head of the ICMP query packet. + @param[in] Packet The content of the ICMP query with IP4 head + removed. + + @retval EFI_INVALID_PARAMETER The packet is malformated. + @retval EFI_SUCCESS The ICMP message is successfully processed. + @retval Others Failed to handle ICMP packet. + +**/ +EFI_STATUS +Ip4IcmpHandle ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet + ); +#endif diff --git a/NetworkPkg/Ip4Dxe/Ip4If.c b/NetworkPkg/Ip4Dxe/Ip4If.c new file mode 100644 index 000000000..44b8d9fc8 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4If.c @@ -0,0 +1,1345 @@ +/** @file + Implement IP4 pesudo interface. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + +// +// Mac address with all zero, used to determine whethter the ARP +// resolve succeeded. Failed ARP requests zero the MAC address buffer. +// +EFI_MAC_ADDRESS mZeroMacAddress; + +/** + Callback funtion when frame transmission is finished. It will + call the frame owner's callback function to tell it the result. + + @param[in] Context Context which is point to the token. + +**/ +VOID +EFIAPI +Ip4OnFrameSentDpc ( + IN VOID *Context + ); + +/** + Request Ip4OnFrameSentDpc as a DPC at TPL_CALLBACK. + + @param[in] Event The transmit token's event. + @param[in] Context Context which is point to the token. + +**/ +VOID +EFIAPI +Ip4OnFrameSent ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Callback function when ARP request are finished. It will cancelled + all the queued frame if the ARP requests failed. Or transmit them + if the request succeed. + + @param[in] Context The context of the callback, a point to the ARP + queue + +**/ +VOID +EFIAPI +Ip4OnArpResolvedDpc ( + IN VOID *Context + ); + +/** + Request Ip4OnArpResolvedDpc as a DPC at TPL_CALLBACK. + + @param Event The Arp request event. + @param Context The context of the callback, a point to the ARP + queue. + +**/ +VOID +EFIAPI +Ip4OnArpResolved ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Received a frame from MNP, wrap it in net buffer then deliver + it to IP's input function. The ownship of the packet also + transferred to IP. When Ip is finished with this packet, it + will call NetbufFree to release the packet, NetbufFree will + again call the Ip4RecycleFrame to signal MNP's event and free + the token used. + + @param Context Context for the callback. + +**/ +VOID +EFIAPI +Ip4OnFrameReceivedDpc ( + IN VOID *Context + ); + +/** + Request Ip4OnFrameReceivedDpc as a DPC at TPL_CALLBACK. + + @param Event The receive event delivered to MNP for receive. + @param Context Context for the callback. + +**/ +VOID +EFIAPI +Ip4OnFrameReceived ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Remove all the frames on the ARP queue that pass the FrameToCancel, + that is, either FrameToCancel is NULL or it returns true for the frame. + + @param[in] ArpQue ARP frame to remove the frames from. + @param[in] IoStatus The status returned to the cancelled frames' + callback function. + @param[in] FrameToCancel Function to select which frame to cancel. + @param[in] Context Opaque parameter to the FrameToCancel. + +**/ +VOID +Ip4CancelFrameArp ( + IN IP4_ARP_QUE *ArpQue, + IN EFI_STATUS IoStatus, + IN IP4_FRAME_TO_CANCEL FrameToCancel OPTIONAL, + IN VOID *Context + ); + + +/** + Wrap a transmit request into a newly allocated IP4_LINK_TX_TOKEN. + + @param[in] Interface The interface to send out to. + @param[in] IpInstance The IpInstance that transmit the packet. NULL if + the packet is sent by the IP4 driver itself. + @param[in] Packet The packet to transmit + @param[in] CallBack Call back function to execute if transmission + finished. + @param[in] Context Opaque parameter to the call back. + @param[in] IpSb The pointer to the IP4 service binding instance. + + @retval Token The wrapped token if succeed + @retval NULL The wrapped token if NULL + +**/ +IP4_LINK_TX_TOKEN * +Ip4WrapLinkTxToken ( + IN IP4_INTERFACE *Interface, + IN IP4_PROTOCOL *IpInstance OPTIONAL, + IN NET_BUF *Packet, + IN IP4_FRAME_CALLBACK CallBack, + IN VOID *Context, + IN IP4_SERVICE *IpSb + ) +{ + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken; + EFI_MANAGED_NETWORK_TRANSMIT_DATA *MnpTxData; + IP4_LINK_TX_TOKEN *Token; + EFI_STATUS Status; + UINT32 Count; + + Token = AllocatePool (sizeof (IP4_LINK_TX_TOKEN) + \ + (Packet->BlockOpNum - 1) * sizeof (EFI_MANAGED_NETWORK_FRAGMENT_DATA)); + + if (Token == NULL) { + return NULL; + } + + Token->Signature = IP4_FRAME_TX_SIGNATURE; + InitializeListHead (&Token->Link); + + Token->Interface = Interface; + Token->IpInstance = IpInstance; + Token->IpSb = IpSb; + Token->CallBack = CallBack; + Token->Packet = Packet; + Token->Context = Context; + CopyMem (&Token->DstMac, &mZeroMacAddress, sizeof (Token->DstMac)); + CopyMem (&Token->SrcMac, &Interface->Mac, sizeof (Token->SrcMac)); + + MnpToken = &(Token->MnpToken); + MnpToken->Status = EFI_NOT_READY; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Ip4OnFrameSent, + Token, + &MnpToken->Event + ); + + if (EFI_ERROR (Status)) { + FreePool (Token); + return NULL; + } + + MnpTxData = &Token->MnpTxData; + MnpToken->Packet.TxData = MnpTxData; + + MnpTxData->DestinationAddress = &Token->DstMac; + MnpTxData->SourceAddress = &Token->SrcMac; + MnpTxData->ProtocolType = IP4_ETHER_PROTO; + MnpTxData->DataLength = Packet->TotalSize; + MnpTxData->HeaderLength = 0; + + Count = Packet->BlockOpNum; + + NetbufBuildExt (Packet, (NET_FRAGMENT *) MnpTxData->FragmentTable, &Count); + MnpTxData->FragmentCount = (UINT16)Count; + + return Token; +} + + +/** + Free the link layer transmit token. It will close the event + then free the memory used. + + @param[in] Token Token to free + +**/ +VOID +Ip4FreeLinkTxToken ( + IN IP4_LINK_TX_TOKEN *Token + ) +{ + NET_CHECK_SIGNATURE (Token, IP4_FRAME_TX_SIGNATURE); + + gBS->CloseEvent (Token->MnpToken.Event); + FreePool (Token); +} + + +/** + Create an IP_ARP_QUE structure to request ARP service. + + @param[in] Interface The interface to send ARP from. + @param[in] DestIp The destination IP (host byte order) to request MAC + for + + @return Point to newly created IP4_ARP_QUE if succeed, otherwise NULL. + +**/ +IP4_ARP_QUE * +Ip4CreateArpQue ( + IN IP4_INTERFACE *Interface, + IN IP4_ADDR DestIp + ) +{ + IP4_ARP_QUE *ArpQue; + EFI_STATUS Status; + + ArpQue = AllocatePool (sizeof (IP4_ARP_QUE)); + + if (ArpQue == NULL) { + return NULL; + } + + ArpQue->Signature = IP4_FRAME_ARP_SIGNATURE; + InitializeListHead (&ArpQue->Link); + + InitializeListHead (&ArpQue->Frames); + ArpQue->Interface = Interface; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Ip4OnArpResolved, + ArpQue, + &ArpQue->OnResolved + ); + + if (EFI_ERROR (Status)) { + FreePool (ArpQue); + return NULL; + } + + ArpQue->Ip = DestIp; + CopyMem (&ArpQue->Mac, &mZeroMacAddress, sizeof (ArpQue->Mac)); + + return ArpQue; +} + + +/** + Remove all the transmit requests queued on the ARP queue, then free it. + + @param[in] ArpQue Arp queue to free + @param[in] IoStatus The transmit status returned to transmit requests' + callback. + +**/ +VOID +Ip4FreeArpQue ( + IN IP4_ARP_QUE *ArpQue, + IN EFI_STATUS IoStatus + ) +{ + NET_CHECK_SIGNATURE (ArpQue, IP4_FRAME_ARP_SIGNATURE); + + // + // Remove all the frame waiting the ARP response + // + Ip4CancelFrameArp (ArpQue, IoStatus, NULL, NULL); + + gBS->CloseEvent (ArpQue->OnResolved); + FreePool (ArpQue); +} + + +/** + Create a link layer receive token to wrap the receive request + + @param[in] Interface The interface to receive from + @param[in] IpInstance The instance that request the receive (NULL for IP4 + driver itself) + @param[in] CallBack Call back function to execute when finished. + @param[in] Context Opaque parameters to the callback + + @return Point to created IP4_LINK_RX_TOKEN if succeed, otherwise NULL. + +**/ +IP4_LINK_RX_TOKEN * +Ip4CreateLinkRxToken ( + IN IP4_INTERFACE *Interface, + IN IP4_PROTOCOL *IpInstance, + IN IP4_FRAME_CALLBACK CallBack, + IN VOID *Context + ) +{ + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken; + IP4_LINK_RX_TOKEN *Token; + EFI_STATUS Status; + + Token = AllocatePool (sizeof (IP4_LINK_RX_TOKEN)); + if (Token == NULL) { + return NULL; + } + + Token->Signature = IP4_FRAME_RX_SIGNATURE; + Token->Interface = Interface; + Token->IpInstance = IpInstance; + Token->CallBack = CallBack; + Token->Context = Context; + + MnpToken = &Token->MnpToken; + MnpToken->Status = EFI_NOT_READY; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Ip4OnFrameReceived, + Token, + &MnpToken->Event + ); + + if (EFI_ERROR (Status)) { + FreePool (Token); + return NULL; + } + + MnpToken->Packet.RxData = NULL; + return Token; +} + + +/** + Free the link layer request token. It will close the event + then free the memory used. + + @param[in] Token Request token to free. + +**/ +VOID +Ip4FreeFrameRxToken ( + IN IP4_LINK_RX_TOKEN *Token + ) +{ + + NET_CHECK_SIGNATURE (Token, IP4_FRAME_RX_SIGNATURE); + + gBS->CloseEvent (Token->MnpToken.Event); + FreePool (Token); +} + + +/** + Remove all the frames on the ARP queue that pass the FrameToCancel, + that is, either FrameToCancel is NULL or it returns true for the frame. + + @param[in] ArpQue ARP frame to remove the frames from. + @param[in] IoStatus The status returned to the cancelled frames' + callback function. + @param[in] FrameToCancel Function to select which frame to cancel. + @param[in] Context Opaque parameter to the FrameToCancel. + +**/ +VOID +Ip4CancelFrameArp ( + IN IP4_ARP_QUE *ArpQue, + IN EFI_STATUS IoStatus, + IN IP4_FRAME_TO_CANCEL FrameToCancel OPTIONAL, + IN VOID *Context + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP4_LINK_TX_TOKEN *Token; + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &ArpQue->Frames) { + Token = NET_LIST_USER_STRUCT (Entry, IP4_LINK_TX_TOKEN, Link); + + if ((FrameToCancel == NULL) || FrameToCancel (Token, Context)) { + RemoveEntryList (Entry); + + Token->CallBack (Token->IpInstance, Token->Packet, IoStatus, 0, Token->Context); + Ip4FreeLinkTxToken (Token); + } + } +} + + +/** + Remove all the frames on the interface that pass the FrameToCancel, + either queued on ARP queues or that have already been delivered to + MNP and not yet recycled. + + @param[in] Interface Interface to remove the frames from. + @param[in] IoStatus The transmit status returned to the frames' + callback. + @param[in] FrameToCancel Function to select the frame to cancel, NULL to + select all. + @param[in] Context Opaque parameters passed to FrameToCancel. + +**/ +VOID +Ip4CancelFrames ( + IN IP4_INTERFACE *Interface, + IN EFI_STATUS IoStatus, + IN IP4_FRAME_TO_CANCEL FrameToCancel OPTIONAL, + IN VOID *Context + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP4_ARP_QUE *ArpQue; + IP4_LINK_TX_TOKEN *Token; + + // + // Cancel all the pending frames on ARP requests + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Interface->ArpQues) { + ArpQue = NET_LIST_USER_STRUCT (Entry, IP4_ARP_QUE, Link); + + Ip4CancelFrameArp (ArpQue, IoStatus, FrameToCancel, Context); + + if (IsListEmpty (&ArpQue->Frames)) { + Interface->Arp->Cancel (Interface->Arp, &ArpQue->Ip, ArpQue->OnResolved); + } + } + + // + // Cancel all the frames that have been delivered to MNP + // but not yet recycled. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Interface->SentFrames) { + Token = NET_LIST_USER_STRUCT (Entry, IP4_LINK_TX_TOKEN, Link); + + if ((FrameToCancel == NULL) || FrameToCancel (Token, Context)) { + Interface->Mnp->Cancel (Interface->Mnp, &Token->MnpToken); + } + } +} + + +/** + Create an IP4_INTERFACE. Delay the creation of ARP instance until + the interface is configured. + + @param[in] Mnp The shared MNP child of this IP4 service binding + instance. + @param[in] Controller The controller this IP4 service binding instance + is installed. Most like the UNDI handle. + @param[in] ImageHandle This driver's image handle. + + @return Point to the created IP4_INTERFACE, otherwise NULL. + +**/ +IP4_INTERFACE * +Ip4CreateInterface ( + IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp, + IN EFI_HANDLE Controller, + IN EFI_HANDLE ImageHandle + ) +{ + IP4_INTERFACE *Interface; + EFI_SIMPLE_NETWORK_MODE SnpMode; + + Interface = AllocatePool (sizeof (IP4_INTERFACE)); + + if ((Interface == NULL) || (Mnp == NULL)) { + return NULL; + } + + Interface->Signature = IP4_INTERFACE_SIGNATURE; + InitializeListHead (&Interface->Link); + Interface->RefCnt = 1; + + Interface->Ip = IP4_ALLZERO_ADDRESS; + Interface->SubnetMask = IP4_ALLZERO_ADDRESS; + Interface->Configured = FALSE; + + Interface->Controller = Controller; + Interface->Image = ImageHandle; + Interface->Mnp = Mnp; + Interface->Arp = NULL; + Interface->ArpHandle = NULL; + + InitializeListHead (&Interface->ArpQues); + InitializeListHead (&Interface->SentFrames); + + Interface->RecvRequest = NULL; + + // + // Get the interface's Mac address and broadcast mac address from SNP + // + if (EFI_ERROR (Mnp->GetModeData (Mnp, NULL, &SnpMode))) { + FreePool (Interface); + return NULL; + } + + CopyMem (&Interface->Mac, &SnpMode.CurrentAddress, sizeof (Interface->Mac)); + CopyMem (&Interface->BroadcastMac, &SnpMode.BroadcastAddress, sizeof (Interface->BroadcastMac)); + Interface->HwaddrLen = SnpMode.HwAddressSize; + + InitializeListHead (&Interface->IpInstances); + Interface->PromiscRecv = FALSE; + + return Interface; +} + + +/** + Set the interface's address, create and configure + the ARP child if necessary. + + @param Interface The interface to set the address. + @param IpAddr The interface's IP address. + @param SubnetMask The interface's netmask. + + @retval EFI_SUCCESS The interface is configured with Ip/netmask pair, + and a ARP is created for it. + @retval Others Failed to set the interface's address. + +**/ +EFI_STATUS +Ip4SetAddress ( + IN OUT IP4_INTERFACE *Interface, + IN IP4_ADDR IpAddr, + IN IP4_ADDR SubnetMask + ) +{ + EFI_ARP_CONFIG_DATA ArpConfig; + EFI_STATUS Status; + + NET_CHECK_SIGNATURE (Interface, IP4_INTERFACE_SIGNATURE); + + // + // Set the ip/netmask, then compute the subnet broadcast + // and network broadcast for easy access. When computing + // nework broadcast, the subnet mask is most like longer + // than the default netmask (not subneted) as defined in + // RFC793. If that isn't the case, we are aggregating the + // networks, use the subnet's mask instead. + // + Interface->Ip = IpAddr; + Interface->SubnetMask = SubnetMask; + Interface->SubnetBrdcast = (IpAddr | ~SubnetMask); + Interface->NetBrdcast = (IpAddr | ~SubnetMask); + + // + // Do clean up for Arp child + // + if (Interface->ArpHandle != NULL) { + if (Interface->Arp != NULL) { + gBS->CloseProtocol ( + Interface->ArpHandle, + &gEfiArpProtocolGuid, + Interface->Image, + Interface->Controller + ); + + Interface->Arp = NULL; + } + + NetLibDestroyServiceChild ( + Interface->Controller, + Interface->Image, + &gEfiArpServiceBindingProtocolGuid, + &Interface->ArpHandle + ); + + Interface->ArpHandle = NULL; + } + + // + // If the address is NOT all zero, create then configure an ARP child. + // Pay attention: DHCP configures its station address as 0.0.0.0/0 + // + if (IpAddr != IP4_ALLZERO_ADDRESS) { + Status = NetLibCreateServiceChild ( + Interface->Controller, + Interface->Image, + &gEfiArpServiceBindingProtocolGuid, + &Interface->ArpHandle + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + Interface->ArpHandle, + &gEfiArpProtocolGuid, + (VOID **) &Interface->Arp, + Interface->Image, + Interface->Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + IpAddr = HTONL (IpAddr); + ArpConfig.SwAddressType = IP4_ETHER_PROTO; + ArpConfig.SwAddressLength = 4; + ArpConfig.StationAddress = &IpAddr; + ArpConfig.EntryTimeOut = 0; + ArpConfig.RetryCount = 0; + ArpConfig.RetryTimeOut = 0; + + Status = Interface->Arp->Configure (Interface->Arp, &ArpConfig); + + if (EFI_ERROR (Status)) { + gBS->CloseProtocol ( + Interface->ArpHandle, + &gEfiArpProtocolGuid, + Interface->Image, + Interface->Controller + ); + + goto ON_ERROR; + } + } + + Interface->Configured = TRUE; + return EFI_SUCCESS; + +ON_ERROR: + NetLibDestroyServiceChild ( + Interface->Controller, + Interface->Image, + &gEfiArpServiceBindingProtocolGuid, + &Interface->ArpHandle + ); + + return Status; +} + + +/** + Filter function to cancel all the frame related to an IP instance. + + @param[in] Frame The transmit request to test whether to cancel + @param[in] Context The context which is the Ip instance that issued + the transmit. + + @retval TRUE The frame belongs to this instance and is to be + removed + @retval FALSE The frame doesn't belong to this instance. + +**/ +BOOLEAN +Ip4CancelInstanceFrame ( + IN IP4_LINK_TX_TOKEN *Frame, + IN VOID *Context + ) +{ + if (Frame->IpInstance == (IP4_PROTOCOL *) Context) { + return TRUE; + } + + return FALSE; +} + + + +/** + If there is a pending receive request, cancel it. Don't call + the receive request's callback because this function can be only + called if the instance or driver is tearing itself down. It + doesn't make sense to call it back. But it is necessary to call + the transmit token's callback to give it a chance to free the + packet and update the upper layer's transmit request status, say + that from the UDP. + + @param[in] Interface The interface used by the IpInstance + +**/ +VOID +Ip4CancelReceive ( + IN IP4_INTERFACE *Interface + ) +{ + EFI_TPL OldTpl; + IP4_LINK_RX_TOKEN *Token; + + if ((Token = Interface->RecvRequest) != NULL) { + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Interface->RecvRequest = NULL; + Interface->Mnp->Cancel (Interface->Mnp, &Token->MnpToken); + + gBS->RestoreTPL (OldTpl); + } +} + + +/** + Free the interface used by IpInstance. All the IP instance with + the same Ip/Netmask pair share the same interface. It is reference + counted. All the frames haven't been sent will be cancelled. + Because the IpInstance is optional, the caller must remove + IpInstance from the interface's instance list itself. + + @param[in] Interface The interface used by the IpInstance. + @param[in] IpInstance The Ip instance that free the interface. NULL if + the Ip driver is releasing the default interface. + + @retval EFI_SUCCESS The interface use IpInstance is freed. + +**/ +EFI_STATUS +Ip4FreeInterface ( + IN IP4_INTERFACE *Interface, + IN IP4_PROTOCOL *IpInstance OPTIONAL + ) +{ + NET_CHECK_SIGNATURE (Interface, IP4_INTERFACE_SIGNATURE); + ASSERT (Interface->RefCnt > 0); + + // + // Remove all the pending transmit token related to this IP instance. + // + Ip4CancelFrames (Interface, EFI_ABORTED, Ip4CancelInstanceFrame, IpInstance); + + if (--Interface->RefCnt > 0) { + return EFI_SUCCESS; + } + + // + // Destroy the interface if this is the last IP instance that + // has the address. Remove all the system transmitted packets + // from this interface, cancel the receive request if there is + // one, and destroy the ARP requests. + // + Ip4CancelFrames (Interface, EFI_ABORTED, Ip4CancelInstanceFrame, NULL); + Ip4CancelReceive (Interface); + + ASSERT (IsListEmpty (&Interface->IpInstances)); + ASSERT (IsListEmpty (&Interface->ArpQues)); + ASSERT (IsListEmpty (&Interface->SentFrames)); + + if (Interface->Arp != NULL) { + gBS->CloseProtocol ( + Interface->ArpHandle, + &gEfiArpProtocolGuid, + Interface->Image, + Interface->Controller + ); + + NetLibDestroyServiceChild ( + Interface->Controller, + Interface->Image, + &gEfiArpServiceBindingProtocolGuid, + Interface->ArpHandle + ); + } + + RemoveEntryList (&Interface->Link); + FreePool (Interface); + + return EFI_SUCCESS; +} + +/** + This function tries to send all the queued frames in ArpQue to the default gateway if + the ARP resolve for direct destination address is failed when using /32 subnet mask. + + @param[in] ArpQue The ARP queue of a failed request. + + @retval EFI_SUCCESS All the queued frames have been send to the default route. + @retval Others Failed to send the queued frames. + +**/ +EFI_STATUS +Ip4SendFrameToDefaultRoute ( + IN IP4_ARP_QUE *ArpQue + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP4_ROUTE_CACHE_ENTRY *RtCacheEntry; + IP4_LINK_TX_TOKEN *Token; + IP4_ADDR Gateway; + EFI_STATUS Status; + IP4_ROUTE_ENTRY *DefaultRoute; + + // + // ARP resolve failed when using /32 subnet mask. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &ArpQue->Frames) { + RemoveEntryList (Entry); + Token = NET_LIST_USER_STRUCT (Entry, IP4_LINK_TX_TOKEN, Link); + ASSERT (Token->Interface->SubnetMask == IP4_ALLONE_ADDRESS); + // + // Find the default gateway IP address. The default route was saved to the RtCacheEntry->Tag in Ip4Route(). + // + RtCacheEntry = NULL; + if (Token->IpInstance != NULL) { + RtCacheEntry = Ip4FindRouteCache (Token->IpInstance->RouteTable, NTOHL (ArpQue->Ip), Token->Interface->Ip); + } + if (RtCacheEntry == NULL) { + RtCacheEntry = Ip4FindRouteCache (Token->IpSb->DefaultRouteTable, NTOHL (ArpQue->Ip), Token->Interface->Ip); + } + if (RtCacheEntry == NULL) { + Status= EFI_NO_MAPPING; + goto ON_ERROR; + } + DefaultRoute = (IP4_ROUTE_ENTRY*)RtCacheEntry->Tag; + if (DefaultRoute == NULL) { + Status= EFI_NO_MAPPING; + goto ON_ERROR; + } + // + // Try to send the frame to the default route. + // + Gateway = DefaultRoute->NextHop; + if (ArpQue->Ip == Gateway) { + // + // ARP resolve for the default route is failed, return error to caller. + // + Status= EFI_NO_MAPPING; + goto ON_ERROR; + } + RtCacheEntry->NextHop = Gateway; + Status = Ip4SendFrame (Token->Interface,Token->IpInstance,Token->Packet,Gateway,Token->CallBack,Token->Context,Token->IpSb); + if (EFI_ERROR (Status)) { + Status= EFI_NO_MAPPING; + goto ON_ERROR; + } + Ip4FreeRouteCacheEntry (RtCacheEntry); + } + + return EFI_SUCCESS; + +ON_ERROR: + if (RtCacheEntry != NULL) { + Ip4FreeRouteCacheEntry (RtCacheEntry); + } + Token->CallBack (Token->IpInstance, Token->Packet, Status, 0, Token->Context); + Ip4FreeLinkTxToken (Token); + return Status; +} + + +/** + Callback function when ARP request are finished. It will cancel + all the queued frame if the ARP requests failed. Or transmit them + if the request succeed. + + @param[in] Context The context of the callback, a point to the ARP + queue + +**/ +VOID +EFIAPI +Ip4OnArpResolvedDpc ( + IN VOID *Context + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP4_ARP_QUE *ArpQue; + IP4_INTERFACE *Interface; + IP4_LINK_TX_TOKEN *Token; + EFI_STATUS Status; + EFI_STATUS IoStatus; + + ArpQue = (IP4_ARP_QUE *) Context; + NET_CHECK_SIGNATURE (ArpQue, IP4_FRAME_ARP_SIGNATURE); + + RemoveEntryList (&ArpQue->Link); + + // + // ARP resolve failed for some reason. + // + if (NET_MAC_EQUAL (&ArpQue->Mac, &mZeroMacAddress, ArpQue->Interface->HwaddrLen)) { + if (ArpQue->Interface->SubnetMask != IP4_ALLONE_ADDRESS) { + // + // Release all the frame and ARP queue itself. Ip4FreeArpQue will call the frame's + // owner back. + // + IoStatus = EFI_NO_MAPPING; + } else { + // + // ARP resolve failed when using 32bit subnet mask, try to send the packets to the + // default route. + // + IoStatus = Ip4SendFrameToDefaultRoute (ArpQue); + } + goto ON_EXIT; + } + + // + // ARP resolve succeeded, Transmit all the frame. Release the ARP + // queue. It isn't necessary for us to cache the ARP binding because + // we always check the ARP cache first before transmit. + // + IoStatus = EFI_SUCCESS; + Interface = ArpQue->Interface; + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &ArpQue->Frames) { + RemoveEntryList (Entry); + + Token = NET_LIST_USER_STRUCT (Entry, IP4_LINK_TX_TOKEN, Link); + CopyMem (&Token->DstMac, &ArpQue->Mac, sizeof (Token->DstMac)); + + // + // Insert the tx token before transmitting it via MNP as the FrameSentDpc + // may be called before Mnp->Transmit returns which will remove this tx + // token from the SentFrames list. Remove it from the list if the returned + // Status of Mnp->Transmit is not EFI_SUCCESS as in this case the + // FrameSentDpc won't be queued. + // + InsertTailList (&Interface->SentFrames, &Token->Link); + + Status = Interface->Mnp->Transmit (Interface->Mnp, &Token->MnpToken); + if (EFI_ERROR (Status)) { + RemoveEntryList (&Token->Link); + Token->CallBack (Token->IpInstance, Token->Packet, Status, 0, Token->Context); + + Ip4FreeLinkTxToken (Token); + continue; + } + } + +ON_EXIT: + Ip4FreeArpQue (ArpQue, IoStatus); +} + +/** + Request Ip4OnArpResolvedDpc as a DPC at TPL_CALLBACK. + + @param Event The Arp request event. + @param Context The context of the callback, a point to the ARP + queue. + +**/ +VOID +EFIAPI +Ip4OnArpResolved ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request Ip4OnArpResolvedDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, Ip4OnArpResolvedDpc, Context); +} + + + +/** + Callback funtion when frame transmission is finished. It will + call the frame owner's callback function to tell it the result. + + @param[in] Context Context which is point to the token. + +**/ +VOID +EFIAPI +Ip4OnFrameSentDpc ( + IN VOID *Context + ) +{ + IP4_LINK_TX_TOKEN *Token; + + Token = (IP4_LINK_TX_TOKEN *) Context; + NET_CHECK_SIGNATURE (Token, IP4_FRAME_TX_SIGNATURE); + + RemoveEntryList (&Token->Link); + + Token->CallBack ( + Token->IpInstance, + Token->Packet, + Token->MnpToken.Status, + 0, + Token->Context + ); + + Ip4FreeLinkTxToken (Token); +} + +/** + Request Ip4OnFrameSentDpc as a DPC at TPL_CALLBACK. + + @param[in] Event The transmit token's event. + @param[in] Context Context which is point to the token. + +**/ +VOID +EFIAPI +Ip4OnFrameSent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request Ip4OnFrameSentDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, Ip4OnFrameSentDpc, Context); +} + + + +/** + Send a frame from the interface. If the next hop is broadcast or + multicast address, it is transmitted immediately. If the next hop + is a unicast, it will consult ARP to resolve the NextHop's MAC. + If some error happened, the CallBack won't be called. So, the caller + must test the return value, and take action when there is an error. + + @param[in] Interface The interface to send the frame from + @param[in] IpInstance The IP child that request the transmission. NULL + if it is the IP4 driver itself. + @param[in] Packet The packet to transmit. + @param[in] NextHop The immediate destination to transmit the packet + to. + @param[in] CallBack Function to call back when transmit finished. + @param[in] Context Opaque parameter to the call back. + @param[in] IpSb The pointer to the IP4 service binding instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to send the frame + @retval EFI_NO_MAPPING Can't resolve the MAC for the nexthop + @retval EFI_SUCCESS The packet is successfully transmitted. + @retval other Other error occurs. + +**/ +EFI_STATUS +Ip4SendFrame ( + IN IP4_INTERFACE *Interface, + IN IP4_PROTOCOL *IpInstance OPTIONAL, + IN NET_BUF *Packet, + IN IP4_ADDR NextHop, + IN IP4_FRAME_CALLBACK CallBack, + IN VOID *Context, + IN IP4_SERVICE *IpSb + ) +{ + IP4_LINK_TX_TOKEN *Token; + LIST_ENTRY *Entry; + IP4_ARP_QUE *ArpQue; + EFI_ARP_PROTOCOL *Arp; + EFI_STATUS Status; + + ASSERT (Interface->Configured); + + Token = Ip4WrapLinkTxToken (Interface, IpInstance, Packet, CallBack, Context, IpSb); + + if (Token == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Get the destination MAC address for multicast and broadcasts. + // Don't depend on ARP to solve the address since there maybe no + // ARP at all. Ip4Output has set NextHop to 255.255.255.255 for + // all the broadcasts. + // + if (NextHop == IP4_ALLONE_ADDRESS) { + CopyMem (&Token->DstMac, &Interface->BroadcastMac, sizeof (Token->DstMac)); + goto SEND_NOW; + + } else if (IP4_IS_MULTICAST (NextHop)) { + + Status = Ip4GetMulticastMac (Interface->Mnp, NextHop, &Token->DstMac); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + goto SEND_NOW; + } + + // + // Can only send out multicast/broadcast if the IP address is zero + // + if ((Arp = Interface->Arp) == NULL) { + Status = EFI_NO_MAPPING; + goto ON_ERROR; + } + + // + // First check whether this binding is in the ARP cache. + // + NextHop = HTONL (NextHop); + Status = Arp->Request (Arp, &NextHop, NULL, &Token->DstMac); + + if (Status == EFI_SUCCESS) { + goto SEND_NOW; + + } else if (Status != EFI_NOT_READY) { + goto ON_ERROR; + } + + // + // Have to do asynchronous ARP resolution. First check + // whether there is already a pending request. + // + ArpQue = NULL; + + NET_LIST_FOR_EACH (Entry, &Interface->ArpQues) { + ArpQue = NET_LIST_USER_STRUCT (Entry, IP4_ARP_QUE, Link); + + if (ArpQue->Ip == NextHop) { + break; + } + } + + // + // Found a pending ARP request, enqueue the frame then return + // + if (Entry != &Interface->ArpQues) { + InsertTailList (&ArpQue->Frames, &Token->Link); + return EFI_SUCCESS; + } + + // + // First frame to NextHop, issue an asynchronous ARP requests + // + ArpQue = Ip4CreateArpQue (Interface, NextHop); + + if (ArpQue == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + Status = Arp->Request (Arp, &ArpQue->Ip, ArpQue->OnResolved, ArpQue->Mac.Addr); + + if (EFI_ERROR (Status) && (Status != EFI_NOT_READY)) { + Ip4FreeArpQue (ArpQue, EFI_NO_MAPPING); + goto ON_ERROR; + } + + InsertHeadList (&ArpQue->Frames, &Token->Link); + InsertHeadList (&Interface->ArpQues, &ArpQue->Link); + return EFI_SUCCESS; + +SEND_NOW: + // + // Insert the tx token into the SentFrames list before calling Mnp->Transmit. + // Remove it if the returned status is not EFI_SUCCESS. + // + InsertTailList (&Interface->SentFrames, &Token->Link); + Status = Interface->Mnp->Transmit (Interface->Mnp, &Token->MnpToken); + if (EFI_ERROR (Status)) { + RemoveEntryList (&Token->Link); + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + Ip4FreeLinkTxToken (Token); + return Status; +} + + +/** + Call back function when the received packet is freed. + Check Ip4OnFrameReceived for information. + + @param Context Context, which is the IP4_LINK_RX_TOKEN. + +**/ +VOID +EFIAPI +Ip4RecycleFrame ( + IN VOID *Context + ) +{ + IP4_LINK_RX_TOKEN *Frame; + + Frame = (IP4_LINK_RX_TOKEN *) Context; + NET_CHECK_SIGNATURE (Frame, IP4_FRAME_RX_SIGNATURE); + + gBS->SignalEvent (Frame->MnpToken.Packet.RxData->RecycleEvent); + Ip4FreeFrameRxToken (Frame); +} + + +/** + Received a frame from MNP, wrap it in net buffer then deliver + it to IP's input function. The ownship of the packet also + transferred to IP. When Ip is finished with this packet, it + will call NetbufFree to release the packet, NetbufFree will + again call the Ip4RecycleFrame to signal MNP's event and free + the token used. + + @param Context Context for the callback. + +**/ +VOID +EFIAPI +Ip4OnFrameReceivedDpc ( + IN VOID *Context + ) +{ + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken; + EFI_MANAGED_NETWORK_RECEIVE_DATA *MnpRxData; + IP4_LINK_RX_TOKEN *Token; + NET_FRAGMENT Netfrag; + NET_BUF *Packet; + UINT32 Flag; + + Token = (IP4_LINK_RX_TOKEN *) Context; + NET_CHECK_SIGNATURE (Token, IP4_FRAME_RX_SIGNATURE); + + // + // First clear the interface's receive request in case the + // caller wants to call Ip4ReceiveFrame in the callback. + // + Token->Interface->RecvRequest = NULL; + + MnpToken = &Token->MnpToken; + MnpRxData = MnpToken->Packet.RxData; + + if (EFI_ERROR (MnpToken->Status) || (MnpRxData == NULL)) { + Token->CallBack (Token->IpInstance, NULL, MnpToken->Status, 0, Token->Context); + Ip4FreeFrameRxToken (Token); + + return ; + } + + // + // Wrap the frame in a net buffer then deliever it to IP input. + // IP will reassemble the packet, and deliver it to upper layer + // + Netfrag.Len = MnpRxData->DataLength; + Netfrag.Bulk = MnpRxData->PacketData; + + Packet = NetbufFromExt (&Netfrag, 1, 0, IP4_MAX_HEADLEN, Ip4RecycleFrame, Token); + + if (Packet == NULL) { + gBS->SignalEvent (MnpRxData->RecycleEvent); + + Token->CallBack (Token->IpInstance, NULL, EFI_OUT_OF_RESOURCES, 0, Token->Context); + Ip4FreeFrameRxToken (Token); + + return ; + } + + Flag = (MnpRxData->BroadcastFlag ? IP4_LINK_BROADCAST : 0); + Flag |= (MnpRxData->MulticastFlag ? IP4_LINK_MULTICAST : 0); + Flag |= (MnpRxData->PromiscuousFlag ? IP4_LINK_PROMISC : 0); + + Token->CallBack (Token->IpInstance, Packet, EFI_SUCCESS, Flag, Token->Context); +} + +/** + Request Ip4OnFrameReceivedDpc as a DPC at TPL_CALLBACK. + + @param Event The receive event delivered to MNP for receive. + @param Context Context for the callback. + +**/ +VOID +EFIAPI +Ip4OnFrameReceived ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request Ip4OnFrameReceivedDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, Ip4OnFrameReceivedDpc, Context); +} + + +/** + Request to receive the packet from the interface. + + @param[in] Interface The interface to receive the frames from. + @param[in] IpInstance The instance that requests the receive. NULL for + the driver itself. + @param[in] CallBack Function to call when receive finished. + @param[in] Context Opaque parameter to the callback. + + @retval EFI_ALREADY_STARTED There is already a pending receive request. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to receive. + @retval EFI_SUCCESS The recieve request has been started. + @retval other Other error occurs. + +**/ +EFI_STATUS +Ip4ReceiveFrame ( + IN IP4_INTERFACE *Interface, + IN IP4_PROTOCOL *IpInstance OPTIONAL, + IN IP4_FRAME_CALLBACK CallBack, + IN VOID *Context + ) +{ + IP4_LINK_RX_TOKEN *Token; + EFI_STATUS Status; + + NET_CHECK_SIGNATURE (Interface, IP4_INTERFACE_SIGNATURE); + + if (Interface->RecvRequest != NULL) { + return EFI_ALREADY_STARTED; + } + + Token = Ip4CreateLinkRxToken (Interface, IpInstance, CallBack, Context); + + if (Token == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Interface->RecvRequest = Token; + Status = Interface->Mnp->Receive (Interface->Mnp, &Token->MnpToken); + if (EFI_ERROR (Status)) { + Interface->RecvRequest = NULL; + Ip4FreeFrameRxToken (Token); + return Status; + } + return EFI_SUCCESS; +} diff --git a/NetworkPkg/Ip4Dxe/Ip4If.h b/NetworkPkg/Ip4Dxe/Ip4If.h new file mode 100644 index 000000000..d73bb5285 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4If.h @@ -0,0 +1,340 @@ +/** @file + Definition for IP4 pesudo interface structure. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP4_IF_H__ +#define __EFI_IP4_IF_H__ + +#define IP4_FRAME_RX_SIGNATURE SIGNATURE_32 ('I', 'P', 'F', 'R') +#define IP4_FRAME_TX_SIGNATURE SIGNATURE_32 ('I', 'P', 'F', 'T') +#define IP4_FRAME_ARP_SIGNATURE SIGNATURE_32 ('I', 'P', 'F', 'A') +#define IP4_INTERFACE_SIGNATURE SIGNATURE_32 ('I', 'P', 'I', 'F') + +/** + This prototype is used by both receive and transmission. + When receiving Netbuf is allocated by IP4_INTERFACE, and + released by IP4. Flag shows whether the frame is received + as link broadcast/multicast... + + When transmitting, the Netbuf is from IP4, and provided + to the callback as a reference. Flag isn't used. + + @param[in] IpInstance The instance that sent or received the packet. + IpInstance can be NULL which means that it is the IP4 driver + itself sending the packets. IP4 driver may send packets that + don't belong to any instance, such as ICMP errors, ICMP echo + responses, or IGMP packets. IpInstance is used as a tag in + this module. + @param[in] Packet The sent or received packet. + @param[in] IoStatus Status of sending or receiving. + @param[in] LinkFlag Indicate if the frame is received as link broadcast/multicast. + When transmitting, it is not used. + @param[in] Context Additional data for callback. + + @retval None. +**/ +typedef +VOID +(*IP4_FRAME_CALLBACK)( + IN IP4_PROTOCOL *IpInstance OPTIONAL, + IN NET_BUF *Packet, + IN EFI_STATUS IoStatus, + IN UINT32 LinkFlag, + IN VOID *Context + ); + +/// +/// Each receive request is wrapped in an IP4_LINK_RX_TOKEN. +/// Upon completion, the Callback will be called. Only one +/// receive request is send to MNP. IpInstance is always NULL. +/// Reference MNP's spec for information. +/// +typedef struct { + UINT32 Signature; + IP4_INTERFACE *Interface; + + IP4_PROTOCOL *IpInstance; + IP4_FRAME_CALLBACK CallBack; + VOID *Context; + + EFI_MANAGED_NETWORK_COMPLETION_TOKEN MnpToken; +} IP4_LINK_RX_TOKEN; + +/// +/// Each transmit request is wrapped in an IP4_LINK_TX_TOKEN. +/// Upon completion, the Callback will be called. +/// +typedef struct { + UINT32 Signature; + LIST_ENTRY Link; + + IP4_INTERFACE *Interface; + IP4_SERVICE *IpSb; + + IP4_PROTOCOL *IpInstance; + IP4_FRAME_CALLBACK CallBack; + NET_BUF *Packet; + VOID *Context; + + EFI_MAC_ADDRESS DstMac; + EFI_MAC_ADDRESS SrcMac; + + EFI_MANAGED_NETWORK_COMPLETION_TOKEN MnpToken; + EFI_MANAGED_NETWORK_TRANSMIT_DATA MnpTxData; +} IP4_LINK_TX_TOKEN; + +/// +/// Only one ARP request is requested for all the frames in +/// a time. It is started for the first frames to the Ip. Any +/// subsequent transmission frame will be linked to Frames, and +/// be sent all at once the ARP requests succeed. +/// +typedef struct { + UINT32 Signature; + LIST_ENTRY Link; + + LIST_ENTRY Frames; + IP4_INTERFACE *Interface; + + // + // ARP requesting staffs + // + EFI_EVENT OnResolved; + IP4_ADDR Ip; + EFI_MAC_ADDRESS Mac; +} IP4_ARP_QUE; + +/** + Callback to select which frame to cancel. Caller can cancel a + single frame, or all the frame from an IP instance. + + @param Frame The sending frame to check for cancellation. + @param Context Additional data for callback. + + @retval TRUE The sending of the frame should be cancelled. + @retval FALSE Do not cancel the frame sending. +**/ +typedef +BOOLEAN +(*IP4_FRAME_TO_CANCEL)( + IP4_LINK_TX_TOKEN *Frame, + VOID *Context + ); + +// +// Each IP4 instance has its own station address. All the instances +// with the same station address share a single interface structure. +// Each interface has its own ARP child, and shares one MNP child. +// Notice the special cases that DHCP can configure the interface +// with 0.0.0.0/0.0.0.0. +// +struct _IP4_INTERFACE { + UINT32 Signature; + LIST_ENTRY Link; + INTN RefCnt; + + // + // IP address and subnet mask of the interface. It also contains + // the subnet/net broadcast address for quick access. The fields + // are invalid if (Configured == FALSE) + // + IP4_ADDR Ip; + IP4_ADDR SubnetMask; + IP4_ADDR SubnetBrdcast; + IP4_ADDR NetBrdcast; + BOOLEAN Configured; + + // + // Handle used to create/destroy ARP child. All the IP children + // share one MNP which is owned by IP service binding. + // + EFI_HANDLE Controller; + EFI_HANDLE Image; + + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + EFI_ARP_PROTOCOL *Arp; + EFI_HANDLE ArpHandle; + + // + // Queues to keep the frames sent and waiting ARP request. + // + LIST_ENTRY ArpQues; + LIST_ENTRY SentFrames; + IP4_LINK_RX_TOKEN *RecvRequest; + + // + // The interface's MAC and broadcast MAC address. + // + EFI_MAC_ADDRESS Mac; + EFI_MAC_ADDRESS BroadcastMac; + UINT32 HwaddrLen; + + // + // All the IP instances that have the same IP/SubnetMask are linked + // together through IpInstances. If any of the instance enables + // promiscuous receive, PromiscRecv is true. + // + LIST_ENTRY IpInstances; + BOOLEAN PromiscRecv; +}; + +/** + Create an IP4_INTERFACE. Delay the creation of ARP instance until + the interface is configured. + + @param[in] Mnp The shared MNP child of this IP4 service binding + instance. + @param[in] Controller The controller this IP4 service binding instance + is installed. Most like the UNDI handle. + @param[in] ImageHandle This driver's image handle. + + @return Point to the created IP4_INTERFACE, otherwise NULL. + +**/ +IP4_INTERFACE * +Ip4CreateInterface ( + IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp, + IN EFI_HANDLE Controller, + IN EFI_HANDLE ImageHandle + ); + +/** + Set the interface's address, create and configure + the ARP child if necessary. + + @param Interface The interface to set the address. + @param IpAddr The interface's IP address. + @param SubnetMask The interface's netmask. + + @retval EFI_SUCCESS The interface is configured with Ip/netmask pair, + and a ARP is created for it. + @retval Others Failed to set the interface's address. + +**/ +EFI_STATUS +Ip4SetAddress ( + IN OUT IP4_INTERFACE *Interface, + IN IP4_ADDR IpAddr, + IN IP4_ADDR SubnetMask + ); + +/** + Free the interface used by IpInstance. All the IP instance with + the same Ip/Netmask pair share the same interface. It is reference + counted. All the frames haven't been sent will be cancelled. + Because the IpInstance is optional, the caller must remove + IpInstance from the interface's instance list itself. + + @param[in] Interface The interface used by the IpInstance. + @param[in] IpInstance The Ip instance that free the interface. NULL if + the Ip driver is releasing the default interface. + + @retval EFI_SUCCESS The interface use IpInstance is freed. + +**/ +EFI_STATUS +Ip4FreeInterface ( + IN IP4_INTERFACE *Interface, + IN IP4_PROTOCOL *IpInstance OPTIONAL + ); + +/** + Send a frame from the interface. If the next hop is broadcast or + multicast address, it is transmitted immediately. If the next hop + is a unicast, it will consult ARP to resolve the NextHop's MAC. + If some error happened, the CallBack won't be called. So, the caller + must test the return value, and take action when there is an error. + + @param[in] Interface The interface to send the frame from + @param[in] IpInstance The IP child that request the transmission. NULL + if it is the IP4 driver itself. + @param[in] Packet The packet to transmit. + @param[in] NextHop The immediate destination to transmit the packet + to. + @param[in] CallBack Function to call back when transmit finished. + @param[in] Context Opaque parameter to the call back. + @param[in] IpSb The pointer to the IP4 service binding instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to send the frame + @retval EFI_NO_MAPPING Can't resolve the MAC for the nexthop + @retval EFI_SUCCESS The packet is successfully transmitted. + @retval other Other error occurs. + +**/ +EFI_STATUS +Ip4SendFrame ( + IN IP4_INTERFACE *Interface, + IN IP4_PROTOCOL *IpInstance OPTIONAL, + IN NET_BUF *Packet, + IN IP4_ADDR NextHop, + IN IP4_FRAME_CALLBACK CallBack, + IN VOID *Context, + IN IP4_SERVICE *IpSb + ); + +/** + Remove all the frames on the interface that pass the FrameToCancel, + either queued on ARP queues or that have already been delivered to + MNP and not yet recycled. + + @param[in] Interface Interface to remove the frames from. + @param[in] IoStatus The transmit status returned to the frames' + callback. + @param[in] FrameToCancel Function to select the frame to cancel, NULL to + select all. + @param[in] Context Opaque parameters passed to FrameToCancel. + +**/ +VOID +Ip4CancelFrames ( + IN IP4_INTERFACE *Interface, + IN EFI_STATUS IoStatus, + IN IP4_FRAME_TO_CANCEL FrameToCancel OPTIONAL, + IN VOID *Context + ); + +/** + If there is a pending receive request, cancel it. Don't call + the receive request's callback because this function can be only + called if the instance or driver is tearing itself down. It + doesn't make sense to call it back. But it is necessary to call + the transmit token's callback to give it a chance to free the + packet and update the upper layer's transmit request status, say + that from the UDP. + + @param[in] Interface The interface used by the IpInstance + +**/ +VOID +Ip4CancelReceive ( + IN IP4_INTERFACE *Interface + ); + +/** + Request to receive the packet from the interface. + + @param[in] Interface The interface to receive the frames from. + @param[in] IpInstance The instance that requests the receive. NULL for + the driver itself. + @param[in] CallBack Function to call when receive finished. + @param[in] Context Opaque parameter to the callback. + + @retval EFI_ALREADY_STARTED There is already a pending receive request. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to receive. + @retval EFI_SUCCESS The recieve request has been started. + @retval other Other error occurs. + +**/ +EFI_STATUS +Ip4ReceiveFrame ( + IN IP4_INTERFACE *Interface, + IN IP4_PROTOCOL *IpInstance OPTIONAL, + IN IP4_FRAME_CALLBACK CallBack, + IN VOID *Context + ); + +#endif diff --git a/NetworkPkg/Ip4Dxe/Ip4Igmp.c b/NetworkPkg/Ip4Dxe/Ip4Igmp.c new file mode 100644 index 000000000..41d9bce97 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Igmp.c @@ -0,0 +1,615 @@ +/** @file + This file implements the RFC2236: IGMP v2. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + +// +// Route Alert option in IGMP report to direct routers to +// examine the packet more closely. +// +UINT32 mRouteAlertOption = 0x00000494; + + +/** + Init the IGMP control data of the IP4 service instance, configure + MNP to receive ALL SYSTEM multicast. + + @param[in, out] IpSb The IP4 service whose IGMP is to be initialized. + + @retval EFI_SUCCESS IGMP of the IpSb is successfully initialized. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to initialize IGMP. + @retval Others Failed to initialize the IGMP of IpSb. + +**/ +EFI_STATUS +Ip4InitIgmp ( + IN OUT IP4_SERVICE *IpSb + ) +{ + IGMP_SERVICE_DATA *IgmpCtrl; + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + IGMP_GROUP *Group; + EFI_STATUS Status; + + IgmpCtrl = &IpSb->IgmpCtrl; + + // + // Configure MNP to receive ALL_SYSTEM multicast + // + Group = AllocatePool (sizeof (IGMP_GROUP)); + + if (Group == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Mnp = IpSb->Mnp; + + Group->Address = IP4_ALLSYSTEM_ADDRESS; + Group->RefCnt = 1; + Group->DelayTime = 0; + Group->ReportByUs = FALSE; + + Status = Ip4GetMulticastMac (Mnp, IP4_ALLSYSTEM_ADDRESS, &Group->Mac); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = Mnp->Groups (Mnp, TRUE, &Group->Mac); + + if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) { + goto ON_ERROR; + } + + InsertHeadList (&IgmpCtrl->Groups, &Group->Link); + return EFI_SUCCESS; + +ON_ERROR: + FreePool (Group); + return Status; +} + + +/** + Find the IGMP_GROUP structure which contains the status of multicast + group Address in this IGMP control block + + @param[in] IgmpCtrl The IGMP control block to search from. + @param[in] Address The multicast address to search. + + @return NULL if the multicast address isn't in the IGMP control block. Otherwise + the point to the IGMP_GROUP which contains the status of multicast group + for Address. + +**/ +IGMP_GROUP * +Ip4FindGroup ( + IN IGMP_SERVICE_DATA *IgmpCtrl, + IN IP4_ADDR Address + ) +{ + LIST_ENTRY *Entry; + IGMP_GROUP *Group; + + NET_LIST_FOR_EACH (Entry, &IgmpCtrl->Groups) { + Group = NET_LIST_USER_STRUCT (Entry, IGMP_GROUP, Link); + + if (Group->Address == Address) { + return Group; + } + } + + return NULL; +} + + +/** + Count the number of IP4 multicast groups that are mapped to the + same MAC address. Several IP4 multicast address may be mapped to + the same MAC address. + + @param[in] IgmpCtrl The IGMP control block to search in. + @param[in] Mac The MAC address to search. + + @return The number of the IP4 multicast group that mapped to the same + multicast group Mac. + +**/ +INTN +Ip4FindMac ( + IN IGMP_SERVICE_DATA *IgmpCtrl, + IN EFI_MAC_ADDRESS *Mac + ) +{ + LIST_ENTRY *Entry; + IGMP_GROUP *Group; + INTN Count; + + Count = 0; + + NET_LIST_FOR_EACH (Entry, &IgmpCtrl->Groups) { + Group = NET_LIST_USER_STRUCT (Entry, IGMP_GROUP, Link); + + if (NET_MAC_EQUAL (&Group->Mac, Mac, sizeof (EFI_MAC_ADDRESS))) { + Count++; + } + } + + return Count; +} + + +/** + Send an IGMP protocol message to the Dst, such as IGMP v1 membership report. + + @param[in] IpSb The IP4 service instance that requests the + transmission. + @param[in] Dst The destinaton to send to. + @param[in] Type The IGMP message type, such as IGMP v1 membership + report. + @param[in] Group The group address in the IGMP message head. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory to build the message. + @retval EFI_SUCCESS The IGMP message is successfully send. + @retval Others Failed to send the IGMP message. + +**/ +EFI_STATUS +Ip4SendIgmpMessage ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR Dst, + IN UINT8 Type, + IN IP4_ADDR Group + ) +{ + IP4_HEAD Head; + NET_BUF *Packet; + IGMP_HEAD *Igmp; + + // + // Allocate a net buffer to hold the message + // + Packet = NetbufAlloc (IP4_MAX_HEADLEN + sizeof (IGMP_HEAD)); + + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Fill in the IGMP and IP header, then transmit the message + // + NetbufReserve (Packet, IP4_MAX_HEADLEN); + + Igmp = (IGMP_HEAD *) NetbufAllocSpace (Packet, sizeof (IGMP_HEAD), FALSE); + if (Igmp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Igmp->Type = Type; + Igmp->MaxRespTime = 0; + Igmp->Checksum = 0; + Igmp->Group = HTONL (Group); + Igmp->Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Igmp, sizeof (IGMP_HEAD))); + + Head.Tos = 0; + Head.Protocol = IP4_PROTO_IGMP; + Head.Ttl = 1; + Head.Fragment = 0; + Head.Dst = Dst; + Head.Src = IP4_ALLZERO_ADDRESS; + + return Ip4Output ( + IpSb, + NULL, + Packet, + &Head, + (UINT8 *) &mRouteAlertOption, + sizeof (UINT32), + IP4_ALLZERO_ADDRESS, + Ip4SysPacketSent, + NULL + ); +} + + +/** + Send an IGMP membership report. Depends on whether the server is + v1 or v2, it will send either a V1 or V2 membership report. + + @param[in] IpSb The IP4 service instance that requests the + transmission. + @param[in] Group The group address to report. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory to build the message. + @retval EFI_SUCCESS The IGMP report message is successfully send. + @retval Others Failed to send the report. + +**/ +EFI_STATUS +Ip4SendIgmpReport ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR Group + ) +{ + if (IpSb->IgmpCtrl.Igmpv1QuerySeen != 0) { + return Ip4SendIgmpMessage (IpSb, Group, IGMP_V1_MEMBERSHIP_REPORT, Group); + } else { + return Ip4SendIgmpMessage (IpSb, Group, IGMP_V2_MEMBERSHIP_REPORT, Group); + } +} + + +/** + Join the multicast group on behalf of this IP4 child + + @param[in] IpInstance The IP4 child that wants to join the group. + @param[in] Address The group to join. + + @retval EFI_SUCCESS Successfully join the multicast group. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval Others Failed to join the multicast group. + +**/ +EFI_STATUS +Ip4JoinGroup ( + IN IP4_PROTOCOL *IpInstance, + IN IP4_ADDR Address + ) +{ + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + IP4_SERVICE *IpSb; + IGMP_SERVICE_DATA *IgmpCtrl; + IGMP_GROUP *Group; + EFI_STATUS Status; + + IpSb = IpInstance->Service; + IgmpCtrl = &IpSb->IgmpCtrl; + Mnp = IpSb->Mnp; + + // + // If the IP service already is a member in the group, just + // increase the refernce count and return. + // + Group = Ip4FindGroup (IgmpCtrl, Address); + + if (Group != NULL) { + Group->RefCnt++; + return EFI_SUCCESS; + } + + // + // Otherwise, create a new IGMP_GROUP, Get the multicast's MAC address, + // send a report, then direct MNP to receive the multicast. + // + Group = AllocatePool (sizeof (IGMP_GROUP)); + + if (Group == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Group->Address = Address; + Group->RefCnt = 1; + Group->DelayTime = IGMP_UNSOLICIATED_REPORT; + Group->ReportByUs = TRUE; + + Status = Ip4GetMulticastMac (Mnp, Address, &Group->Mac); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = Ip4SendIgmpReport (IpSb, Address); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = Mnp->Groups (Mnp, TRUE, &Group->Mac); + + if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) { + goto ON_ERROR; + } + + InsertHeadList (&IgmpCtrl->Groups, &Group->Link); + return EFI_SUCCESS; + +ON_ERROR: + FreePool (Group); + return Status; +} + + +/** + Leave the IP4 multicast group on behalf of IpInstance. + + @param[in] IpInstance The IP4 child that wants to leave the group + address. + @param[in] Address The group address to leave. + + @retval EFI_NOT_FOUND The IP4 service instance isn't in the group. + @retval EFI_SUCCESS Successfully leave the multicast group. + @retval Others Failed to leave the multicast group. + +**/ +EFI_STATUS +Ip4LeaveGroup ( + IN IP4_PROTOCOL *IpInstance, + IN IP4_ADDR Address + ) +{ + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + IP4_SERVICE *IpSb; + IGMP_SERVICE_DATA *IgmpCtrl; + IGMP_GROUP *Group; + EFI_STATUS Status; + + IpSb = IpInstance->Service; + IgmpCtrl = &IpSb->IgmpCtrl; + Mnp = IpSb->Mnp; + + Group = Ip4FindGroup (IgmpCtrl, Address); + + if (Group == NULL) { + return EFI_NOT_FOUND; + } + + // + // If more than one instance is in the group, decrease + // the RefCnt then return. + // + if (--Group->RefCnt > 0) { + return EFI_SUCCESS; + } + + // + // If multiple IP4 group addresses are mapped to the same + // multicast MAC address, don't configure the MNP to leave + // the MAC. + // + if (Ip4FindMac (IgmpCtrl, &Group->Mac) == 1) { + Status = Mnp->Groups (Mnp, FALSE, &Group->Mac); + + if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) { + return Status; + } + } + + // + // Send a leave report if the membership is reported by us + // and we are talking IGMPv2. + // + if (Group->ReportByUs && IgmpCtrl->Igmpv1QuerySeen == 0) { + Ip4SendIgmpMessage (IpSb, IP4_ALLROUTER_ADDRESS, IGMP_LEAVE_GROUP, Group->Address); + } + + RemoveEntryList (&Group->Link); + FreePool (Group); + + return EFI_SUCCESS; +} + + +/** + Handle the received IGMP message for the IP4 service instance. + + @param[in] IpSb The IP4 service instance that received the message. + @param[in] Head The IP4 header of the received message. + @param[in] Packet The IGMP message, without IP4 header. + + @retval EFI_INVALID_PARAMETER The IGMP message is malformated. + @retval EFI_SUCCESS The IGMP message is successfully processed. + +**/ +EFI_STATUS +Ip4IgmpHandle ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet + ) +{ + IGMP_SERVICE_DATA *IgmpCtrl; + IGMP_HEAD Igmp; + IGMP_GROUP *Group; + IP4_ADDR Address; + LIST_ENTRY *Entry; + + IgmpCtrl = &IpSb->IgmpCtrl; + + // + // Must checksum over the whole packet, later IGMP version + // may employ message longer than 8 bytes. IP's header has + // already been trimmed off. + // + if ((Packet->TotalSize < sizeof (Igmp)) || (NetbufChecksum (Packet) != 0)) { + NetbufFree (Packet); + return EFI_INVALID_PARAMETER; + } + + // + // Copy the packet in case it is fragmented + // + NetbufCopy (Packet, 0, sizeof (IGMP_HEAD), (UINT8 *)&Igmp); + + switch (Igmp.Type) { + case IGMP_MEMBERSHIP_QUERY: + // + // If MaxRespTime is zero, it is most likely that we are + // talking to a V1 router + // + if (Igmp.MaxRespTime == 0) { + IgmpCtrl->Igmpv1QuerySeen = IGMP_V1ROUTER_PRESENT; + Igmp.MaxRespTime = 100; + } + + // + // Igmp is ticking once per second but MaxRespTime is in + // the unit of 100ms. + // + Igmp.MaxRespTime /= 10; + Address = NTOHL (Igmp.Group); + + if (Address == IP4_ALLSYSTEM_ADDRESS) { + break; + } + + NET_LIST_FOR_EACH (Entry, &IgmpCtrl->Groups) { + Group = NET_LIST_USER_STRUCT (Entry, IGMP_GROUP, Link); + + // + // If address is all zero, all the memberships will be reported. + // otherwise only one is reported. + // + if ((Address == IP4_ALLZERO_ADDRESS) || (Address == Group->Address)) { + // + // If the timer is pending, only update it if the time left + // is longer than the MaxRespTime. TODO: randomize the DelayTime. + // + if ((Group->DelayTime == 0) || (Group->DelayTime > Igmp.MaxRespTime)) { + Group->DelayTime = MAX (1, Igmp.MaxRespTime); + } + } + } + + break; + + case IGMP_V1_MEMBERSHIP_REPORT: + case IGMP_V2_MEMBERSHIP_REPORT: + Address = NTOHL (Igmp.Group); + Group = Ip4FindGroup (IgmpCtrl, Address); + + if ((Group != NULL) && (Group->DelayTime > 0)) { + Group->DelayTime = 0; + Group->ReportByUs = FALSE; + } + + break; + } + + NetbufFree (Packet); + return EFI_SUCCESS; +} + + +/** + The periodical timer function for IGMP. It does the following + things: + 1. Decrease the Igmpv1QuerySeen to make it possible to refresh + the IGMP server type. + 2. Decrease the report timer for each IGMP group in "delaying + member" state. + + @param[in] IpSb The IP4 service instance that is ticking. + +**/ +VOID +Ip4IgmpTicking ( + IN IP4_SERVICE *IpSb + ) +{ + IGMP_SERVICE_DATA *IgmpCtrl; + LIST_ENTRY *Entry; + IGMP_GROUP *Group; + + IgmpCtrl = &IpSb->IgmpCtrl; + + if (IgmpCtrl->Igmpv1QuerySeen > 0) { + IgmpCtrl->Igmpv1QuerySeen--; + } + + // + // Decrease the report timer for each IGMP group in "delaying member" + // + NET_LIST_FOR_EACH (Entry, &IgmpCtrl->Groups) { + Group = NET_LIST_USER_STRUCT (Entry, IGMP_GROUP, Link); + ASSERT (Group->DelayTime >= 0); + + if (Group->DelayTime > 0) { + Group->DelayTime--; + + if (Group->DelayTime == 0) { + Ip4SendIgmpReport (IpSb, Group->Address); + Group->ReportByUs = TRUE; + } + } + } +} + + +/** + Add a group address to the array of group addresses. + The caller should make sure that no duplicated address + existed in the array. Although the function doesn't + assume the byte order of the both Source and Addr, the + network byte order is used by the caller. + + @param[in] Source The array of group addresses to add to. + @param[in] Count The number of group addresses in the Source. + @param[in] Addr The IP4 multicast address to add. + + @return NULL if failed to allocate memory for the new groups, + otherwise the new combined group addresses. + +**/ +IP4_ADDR * +Ip4CombineGroups ( + IN IP4_ADDR *Source, + IN UINT32 Count, + IN IP4_ADDR Addr + ) +{ + IP4_ADDR *Groups; + + Groups = AllocatePool (sizeof (IP4_ADDR) * (Count + 1)); + + if (Groups == NULL) { + return NULL; + } + + CopyMem (Groups, Source, Count * sizeof (IP4_ADDR)); + Groups[Count] = Addr; + + return Groups; +} + + +/** + Remove a group address from the array of group addresses. + Although the function doesn't assume the byte order of the + both Groups and Addr, the network byte order is used by + the caller. + + @param Groups The array of group addresses to remove from. + @param Count The number of group addresses in the Groups. + @param Addr The IP4 multicast address to remove. + + @return The nubmer of group addresses in the Groups after remove. + It is Count if the Addr isn't in the Groups. + +**/ +INTN +Ip4RemoveGroupAddr ( + IN OUT IP4_ADDR *Groups, + IN UINT32 Count, + IN IP4_ADDR Addr + ) +{ + UINT32 Index; + + for (Index = 0; Index < Count; Index++) { + if (Groups[Index] == Addr) { + break; + } + } + + while (Index < Count - 1) { + Groups[Index] = Groups[Index + 1]; + Index++; + } + + return Index; +} diff --git a/NetworkPkg/Ip4Dxe/Ip4Igmp.h b/NetworkPkg/Ip4Dxe/Ip4Igmp.h new file mode 100644 index 000000000..0cc944594 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Igmp.h @@ -0,0 +1,201 @@ +/** @file + +Copyright (c) 2005 - 2015, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP4_IGMP_H__ +#define __EFI_IP4_IGMP_H__ + +// +// IGMP message type +// +#define IGMP_MEMBERSHIP_QUERY 0x11 +#define IGMP_V1_MEMBERSHIP_REPORT 0x12 +#define IGMP_V2_MEMBERSHIP_REPORT 0x16 +#define IGMP_LEAVE_GROUP 0x17 + +#define IGMP_V1ROUTER_PRESENT 400 +#define IGMP_UNSOLICIATED_REPORT 10 + +#pragma pack(1) +typedef struct { + UINT8 Type; + UINT8 MaxRespTime; + UINT16 Checksum; + IP4_ADDR Group; +} IGMP_HEAD; +#pragma pack() + +/// +/// The status of multicast group. It isn't necessary to maintain +/// explicit state of host state diagram. A group with non-zero +/// DelayTime is in "delaying member" state. otherwise, it is in +/// "idle member" state. +/// +typedef struct { + LIST_ENTRY Link; + INTN RefCnt; + IP4_ADDR Address; + INTN DelayTime; + BOOLEAN ReportByUs; + EFI_MAC_ADDRESS Mac; +} IGMP_GROUP; + +/// +/// The IGMP status. Each IP4 service instance has a IGMP_SERVICE_DATA +/// attached. The Igmpv1QuerySeen remember whether the server on this +/// connected network is v1 or v2. +/// +typedef struct { + INTN Igmpv1QuerySeen; + LIST_ENTRY Groups; +} IGMP_SERVICE_DATA; + +/** + Init the IGMP control data of the IP4 service instance, configure + MNP to receive ALL SYSTEM multicast. + + @param[in, out] IpSb The IP4 service whose IGMP is to be initialized. + + @retval EFI_SUCCESS IGMP of the IpSb is successfully initialized. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to initialize IGMP. + @retval Others Failed to initialize the IGMP of IpSb. + +**/ +EFI_STATUS +Ip4InitIgmp ( + IN OUT IP4_SERVICE *IpSb + ); + +/** + Join the multicast group on behalf of this IP4 child + + @param[in] IpInstance The IP4 child that wants to join the group. + @param[in] Address The group to join. + + @retval EFI_SUCCESS Successfully join the multicast group. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval Others Failed to join the multicast group. + +**/ +EFI_STATUS +Ip4JoinGroup ( + IN IP4_PROTOCOL *IpInstance, + IN IP4_ADDR Address + ); + +/** + Leave the IP4 multicast group on behalf of IpInstance. + + @param[in] IpInstance The IP4 child that wants to leave the group + address. + @param[in] Address The group address to leave. + + @retval EFI_NOT_FOUND The IP4 service instance isn't in the group. + @retval EFI_SUCCESS Successfully leave the multicast group. + @retval Others Failed to leave the multicast group. + +**/ +EFI_STATUS +Ip4LeaveGroup ( + IN IP4_PROTOCOL *IpInstance, + IN IP4_ADDR Address + ); + +/** + Handle the received IGMP message for the IP4 service instance. + + @param[in] IpSb The IP4 service instance that received the message. + @param[in] Head The IP4 header of the received message. + @param[in] Packet The IGMP message, without IP4 header. + + @retval EFI_INVALID_PARAMETER The IGMP message is malformated. + @retval EFI_SUCCESS The IGMP message is successfully processed. + +**/ +EFI_STATUS +Ip4IgmpHandle ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet + ); + +/** + The periodical timer function for IGMP. It does the following + things: + 1. Decrease the Igmpv1QuerySeen to make it possible to refresh + the IGMP server type. + 2. Decrease the report timer for each IGMP group in "delaying + member" state. + + @param[in] IpSb The IP4 service instance that is ticking. + +**/ +VOID +Ip4IgmpTicking ( + IN IP4_SERVICE *IpSb + ); + +/** + Add a group address to the array of group addresses. + The caller should make sure that no duplicated address + existed in the array. Although the function doesn't + assume the byte order of the both Source and Addr, the + network byte order is used by the caller. + + @param[in] Source The array of group addresses to add to. + @param[in] Count The number of group addresses in the Source. + @param[in] Addr The IP4 multicast address to add. + + @return NULL if failed to allocate memory for the new groups, + otherwise the new combined group addresses. + +**/ +IP4_ADDR * +Ip4CombineGroups ( + IN IP4_ADDR *Source, + IN UINT32 Count, + IN IP4_ADDR Addr + ); + +/** + Remove a group address from the array of group addresses. + Although the function doesn't assume the byte order of the + both Groups and Addr, the network byte order is used by + the caller. + + @param Groups The array of group addresses to remove from. + @param Count The number of group addresses in the Groups. + @param Addr The IP4 multicast address to remove. + + @return The nubmer of group addresses in the Groups after remove. + It is Count if the Addr isn't in the Groups. + +**/ +INTN +Ip4RemoveGroupAddr ( + IN OUT IP4_ADDR *Groups, + IN UINT32 Count, + IN IP4_ADDR Addr + ); + +/** + Find the IGMP_GROUP structure which contains the status of multicast + group Address in this IGMP control block + + @param[in] IgmpCtrl The IGMP control block to search from. + @param[in] Address The multicast address to search. + + @return NULL if the multicast address isn't in the IGMP control block. Otherwise + the point to the IGMP_GROUP which contains the status of multicast group + for Address. + +**/ +IGMP_GROUP * +Ip4FindGroup ( + IN IGMP_SERVICE_DATA *IgmpCtrl, + IN IP4_ADDR Address + ); +#endif diff --git a/NetworkPkg/Ip4Dxe/Ip4Impl.c b/NetworkPkg/Ip4Dxe/Ip4Impl.c new file mode 100644 index 000000000..ec6f03707 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Impl.c @@ -0,0 +1,2330 @@ +/** @file + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + +EFI_IPSEC2_PROTOCOL *mIpSec = NULL; + +/** + Gets the current operational settings for this instance of the EFI IPv4 Protocol driver. + + The GetModeData() function returns the current operational mode data for this + driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This + function is used optionally to retrieve the operational mode data of underlying + networks or drivers. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure. + @param[out] MnpConfigData Pointer to the managed network configuration data structure. + @param[out] SnpModeData Pointer to the simple network mode data structure. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated. + +**/ +EFI_STATUS +EFIAPI +EfiIp4GetModeData ( + IN CONST EFI_IP4_PROTOCOL *This, + OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ); + +/** + Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance. + + The Configure() function is used to set, change, or reset the operational + parameters and filter settings for this EFI IPv4 Protocol instance. Until these + parameters have been set, no network traffic can be sent or received by this + instance. Once the parameters have been reset (by calling this function with + IpConfigData set to NULL), no more traffic can be sent or received until these + parameters have been set again. Each EFI IPv4 Protocol instance can be started + and stopped independently of each other by enabling or disabling their receive + filter settings with the Configure() function. + + When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will + be appended as an alias address into the addresses list in the EFI IPv4 Protocol + driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL + to retrieve the default IPv4 address if it is not available yet. Clients could + frequently call GetModeData() to check the status to ensure that the default IPv4 + address is ready. + + If operational parameters are reset or changed, any pending transmit and receive + requests will be cancelled. Their completion token status will be set to EFI_ABORTED + and their events will be signaled. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] IpConfigData Pointer to the EFI IPv4 Protocol configuration data structure. + + @retval EFI_SUCCESS The driver instance was successfully opened. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + @retval EFI_UNSUPPORTED One or more of the following conditions is TRUE: + A configuration protocol (DHCP, BOOTP, RARP, etc.) could + not be located when clients choose to use the default IPv4 + address. This EFI IPv4 Protocol implementation does not + support this requested filter or timeout setting. + @retval EFI_OUT_OF_RESOURCES The EFI IPv4 Protocol driver instance data could not be allocated. + @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the + IPv4 address or subnet mask can be changed. The interface must + also be stopped when switching to/from raw packet mode. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv4 + Protocol driver instance is not opened. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Configure ( + IN EFI_IP4_PROTOCOL *This, + IN EFI_IP4_CONFIG_DATA *IpConfigData OPTIONAL + ); + +/** + Joins and leaves multicast groups. + + The Groups() function is used to join and leave multicast group sessions. Joining + a group will enable reception of matching multicast packets. Leaving a group will + disable the multicast packet reception. + + If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave. + @param[in] GroupAddress Pointer to the IPv4 multicast address. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following is TRUE: + - This is NULL. + - JoinFlag is TRUE and GroupAddress is NULL. + - GroupAddress is not NULL and *GroupAddress is + not a multicast IPv4 address. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_OUT_OF_RESOURCES System resources could not be allocated. + @retval EFI_UNSUPPORTED This EFI IPv4 Protocol implementation does not support multicast groups. + @retval EFI_ALREADY_STARTED The group address is already in the group table (when + JoinFlag is TRUE). + @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Groups ( + IN EFI_IP4_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL + ); + +/** + Adds and deletes routing table entries. + + The Routes() function adds a route to or deletes a route from the routing table. + + Routes are determined by comparing the SubnetAddress with the destination IPv4 + address arithmetically AND-ed with the SubnetMask. The gateway address must be + on the same subnet as the configured station address. + + The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0. + The default route matches all destination IPv4 addresses that do not match any + other routes. + + A GatewayAddress that is zero is a nonroute. Packets are sent to the destination + IP address if it can be found in the ARP cache or on the local subnet. One automatic + nonroute entry will be inserted into the routing table for outgoing packets that + are addressed to a local subnet (gateway address of 0.0.0.0). + + Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI + IPv4 Protocol instances that use the default IPv4 address will also have copies + of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these + copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its + instances. As a result, client modification to the routing table will be lost. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to + FALSE to add this route to the routing table. SubnetAddress + and SubnetMask are used as the key to each route entry. + @param[in] SubnetAddress The address of the subnet that needs to be routed. + @param[in] SubnetMask The subnet mask of SubnetAddress. + @param[in] GatewayAddress The unicast gateway IPv4 address for this route. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The driver instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - SubnetAddress is NULL. + - SubnetMask is NULL. + - GatewayAddress is NULL. + - *SubnetAddress is not a valid subnet address. + - *SubnetMask is not a valid subnet mask. + - *GatewayAddress is not a valid unicast IPv4 address. + @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table. + @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE). + @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when + DeleteRoute is FALSE). + +**/ +EFI_STATUS +EFIAPI +EfiIp4Routes ( + IN EFI_IP4_PROTOCOL *This, + IN BOOLEAN DeleteRoute, + IN EFI_IPv4_ADDRESS *SubnetAddress, + IN EFI_IPv4_ADDRESS *SubnetMask, + IN EFI_IPv4_ADDRESS *GatewayAddress + ); + +/** + Places outgoing data packets into the transmit queue. + + The Transmit() function places a sending request in the transmit queue of this + EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some + errors occur, the event in the token will be signaled and the status is updated. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] Token Pointer to the transmit token. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more pameters are invalid. + @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.Event + was already in the transmit queue. + @retval EFI_NOT_READY The completion token could not be queued because the transmit + queue is full. + @retval EFI_NOT_FOUND Not route is found to destination address. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data. + @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too + short to transmit. + @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + total data length is + greater than MTU (or greater than the maximum packet size if + Token.Packet.TxData.OverrideData. + DoNotFragment is TRUE.) + +**/ +EFI_STATUS +EFIAPI +EfiIp4Transmit ( + IN EFI_IP4_PROTOCOL *This, + IN EFI_IP4_COMPLETION_TOKEN *Token + ); + +/** + Places a receiving request into the receiving queue. + + The Receive() function places a completion token into the receive packet queue. + This function is always asynchronous. + + The Token.Event field in the completion token must be filled in by the caller + and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol + driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event + is signaled. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] Token Pointer to a token that is associated with the receive data descriptor. + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, RARP, etc.) + is not finished yet. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Token is NULL. + - Token.Event is NULL. + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system + resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The EFI IPv4 Protocol instance has been reset to startup defaults. + EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already + in the receive queue. + @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full. + @retval EFI_ICMP_ERROR An ICMP error packet was received. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Receive ( + IN EFI_IP4_PROTOCOL *This, + IN EFI_IP4_COMPLETION_TOKEN *Token + ); + +/** + Abort an asynchronous transmit or receive request. + + The Cancel() function is used to abort a pending transmit or receive request. + If the token is in the transmit or receive request queues, after calling this + function, Token->Status will be set to EFI_ABORTED and then Token->Event will + be signaled. If the token is not in one of the queues, which usually means the + asynchronous operation has completed, this function will not signal the token + and EFI_NOT_FOUND is returned. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_IP4_PROTOCOL.Transmit() or + EFI_IP4_PROTOCOL.Receive(). If NULL, all pending + tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is + defined in EFI_IP4_PROTOCOL.Transmit(). + + @retval EFI_SUCCESS The asynchronous I/O request was aborted and + Token.->Event was signaled. When Token is NULL, all + pending requests were aborted and their events were signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was + not found in the transmit or receive queue. It has either completed + or was not issued by Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +EfiIp4Cancel ( + IN EFI_IP4_PROTOCOL *This, + IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function polls for incoming data packets and processes outgoing data + packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll() + function to increase the rate that data packets are moved between the communications + device and the transmit and receive queues. + + In some systems the periodic timer event may not poll the underlying communications + device fast enough to transmit and/or receive all data packets without missing + incoming packets or dropping outgoing packets. Drivers and applications that are + experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function + more often. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_NOT_READY No incoming or outgoing data is processed. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue. + Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Poll ( + IN EFI_IP4_PROTOCOL *This + ); + +EFI_IP4_PROTOCOL +mEfiIp4ProtocolTemplete = { + EfiIp4GetModeData, + EfiIp4Configure, + EfiIp4Groups, + EfiIp4Routes, + EfiIp4Transmit, + EfiIp4Receive, + EfiIp4Cancel, + EfiIp4Poll +}; + +/** + Gets the current operational settings for this instance of the EFI IPv4 Protocol driver. + + The GetModeData() function returns the current operational mode data for this + driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This + function is used optionally to retrieve the operational mode data of underlying + networks or drivers. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure. + @param[out] MnpConfigData Pointer to the managed network configuration data structure. + @param[out] SnpModeData Pointer to the simple network mode data structure. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated. + +**/ +EFI_STATUS +EFIAPI +EfiIp4GetModeData ( + IN CONST EFI_IP4_PROTOCOL *This, + OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ) +{ + IP4_PROTOCOL *IpInstance; + IP4_SERVICE *IpSb; + EFI_IP4_CONFIG_DATA *Config; + EFI_STATUS Status; + EFI_TPL OldTpl; + IP4_ADDR Ip; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This); + IpSb = IpInstance->Service; + + if (Ip4ModeData != NULL) { + // + // IsStarted is "whether the EfiIp4Configure has been called". + // IsConfigured is "whether the station address has been configured" + // + Ip4ModeData->IsStarted = (BOOLEAN)(IpInstance->State == IP4_STATE_CONFIGED); + CopyMem (&Ip4ModeData->ConfigData, &IpInstance->ConfigData, sizeof (Ip4ModeData->ConfigData)); + Ip4ModeData->IsConfigured = FALSE; + + Ip4ModeData->GroupCount = IpInstance->GroupCount; + Ip4ModeData->GroupTable = (EFI_IPv4_ADDRESS *) IpInstance->Groups; + + Ip4ModeData->IcmpTypeCount = 23; + Ip4ModeData->IcmpTypeList = mIp4SupportedIcmp; + + Ip4ModeData->RouteTable = NULL; + Ip4ModeData->RouteCount = 0; + + Ip4ModeData->MaxPacketSize = IpSb->MaxPacketSize; + + // + // return the current station address for this IP child. So, + // the user can get the default address through this. Some + // application wants to know it station address even it is + // using the default one, such as a ftp server. + // + if (Ip4ModeData->IsStarted) { + Config = &Ip4ModeData->ConfigData; + + Ip = HTONL (IpInstance->Interface->Ip); + CopyMem (&Config->StationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS)); + + Ip = HTONL (IpInstance->Interface->SubnetMask); + CopyMem (&Config->SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS)); + + Ip4ModeData->IsConfigured = IpInstance->Interface->Configured; + + // + // Build a EFI route table for user from the internal route table. + // + Status = Ip4BuildEfiRouteTable (IpInstance); + + if (EFI_ERROR (Status)) { + gBS->RestoreTPL (OldTpl); + return Status; + } + + Ip4ModeData->RouteTable = IpInstance->EfiRouteTable; + Ip4ModeData->RouteCount = IpInstance->EfiRouteCount; + } + } + + // + // Get fresh mode data from MNP, since underlying media status may change + // + Status = IpSb->Mnp->GetModeData (IpSb->Mnp, MnpConfigData, SnpModeData); + + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Config the MNP parameter used by IP. The IP driver use one MNP + child to transmit/receive frames. By default, it configures MNP + to receive unicast/multicast/broadcast. And it will enable/disable + the promiscous receive according to whether there is IP child + enable that or not. If Force is FALSE, it will iterate through + all the IP children to check whether the promiscuous receive + setting has been changed. If it hasn't been changed, it won't + reconfigure the MNP. If Force is TRUE, the MNP is configured no + matter whether that is changed or not. + + @param[in] IpSb The IP4 service instance that is to be changed. + @param[in] Force Force the configuration or not. + + @retval EFI_SUCCESS The MNP is successfully configured/reconfigured. + @retval Others Configuration failed. + +**/ +EFI_STATUS +Ip4ServiceConfigMnp ( + IN IP4_SERVICE *IpSb, + IN BOOLEAN Force + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *ProtoEntry; + IP4_INTERFACE *IpIf; + IP4_PROTOCOL *IpInstance; + BOOLEAN Reconfig; + BOOLEAN PromiscReceive; + EFI_STATUS Status; + + Reconfig = FALSE; + PromiscReceive = FALSE; + + if (!Force) { + // + // Iterate through the IP children to check whether promiscuous + // receive setting has been changed. Update the interface's receive + // filter also. + // + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + + IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link); + IpIf->PromiscRecv = FALSE; + + NET_LIST_FOR_EACH (ProtoEntry, &IpIf->IpInstances) { + IpInstance = NET_LIST_USER_STRUCT (ProtoEntry, IP4_PROTOCOL, AddrLink); + + if (IpInstance->ConfigData.AcceptPromiscuous) { + IpIf->PromiscRecv = TRUE; + PromiscReceive = TRUE; + } + } + } + + // + // If promiscuous receive isn't changed, it isn't necessary to reconfigure. + // + if (PromiscReceive == IpSb->MnpConfigData.EnablePromiscuousReceive) { + return EFI_SUCCESS; + } + + Reconfig = TRUE; + IpSb->MnpConfigData.EnablePromiscuousReceive = PromiscReceive; + } + + Status = IpSb->Mnp->Configure (IpSb->Mnp, &IpSb->MnpConfigData); + + // + // recover the original configuration if failed to set the configure. + // + if (EFI_ERROR (Status) && Reconfig) { + IpSb->MnpConfigData.EnablePromiscuousReceive = (BOOLEAN) !PromiscReceive; + } + + return Status; +} + + +/** + Intiialize the IP4_PROTOCOL structure to the unconfigured states. + + @param IpSb The IP4 service instance. + @param IpInstance The IP4 child instance. + +**/ +VOID +Ip4InitProtocol ( + IN IP4_SERVICE *IpSb, + IN OUT IP4_PROTOCOL *IpInstance + ) +{ + ASSERT ((IpSb != NULL) && (IpInstance != NULL)); + + ZeroMem (IpInstance, sizeof (IP4_PROTOCOL)); + + IpInstance->Signature = IP4_PROTOCOL_SIGNATURE; + CopyMem (&IpInstance->Ip4Proto, &mEfiIp4ProtocolTemplete, sizeof (IpInstance->Ip4Proto)); + IpInstance->State = IP4_STATE_UNCONFIGED; + IpInstance->InDestroy = FALSE; + IpInstance->Service = IpSb; + + InitializeListHead (&IpInstance->Link); + NetMapInit (&IpInstance->RxTokens); + NetMapInit (&IpInstance->TxTokens); + InitializeListHead (&IpInstance->Received); + InitializeListHead (&IpInstance->Delivered); + InitializeListHead (&IpInstance->AddrLink); + + EfiInitializeLock (&IpInstance->RecycleLock, TPL_NOTIFY); +} + + +/** + Configure the IP4 child. If the child is already configured, + change the configuration parameter. Otherwise configure it + for the first time. The caller should validate the configuration + before deliver them to it. It also don't do configure NULL. + + @param[in, out] IpInstance The IP4 child to configure. + @param[in] Config The configure data. + + @retval EFI_SUCCESS The IP4 child is successfully configured. + @retval EFI_DEVICE_ERROR Failed to free the pending transive or to + configure underlying MNP or other errors. + @retval EFI_NO_MAPPING The IP4 child is configured to use default + address, but the default address hasn't been + configured. The IP4 child doesn't need to be + reconfigured when default address is configured. + @retval EFI_OUT_OF_RESOURCES No more memory space is available. + @retval other Other error occurs. + +**/ +EFI_STATUS +Ip4ConfigProtocol ( + IN OUT IP4_PROTOCOL *IpInstance, + IN EFI_IP4_CONFIG_DATA *Config + ) +{ + IP4_SERVICE *IpSb; + IP4_INTERFACE *IpIf; + EFI_STATUS Status; + IP4_ADDR Ip; + IP4_ADDR Netmask; + EFI_ARP_PROTOCOL *Arp; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + EFI_IP4_CONFIG2_POLICY Policy; + + IpSb = IpInstance->Service; + + Ip4Config2 = NULL; + + // + // User is changing packet filters. It must be stopped + // before the station address can be changed. + // + if (IpInstance->State == IP4_STATE_CONFIGED) { + // + // Cancel all the pending transmit/receive from upper layer + // + Status = Ip4Cancel (IpInstance, NULL); + + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData)); + return EFI_SUCCESS; + } + + // + // Configure a fresh IP4 protocol instance. Create a route table. + // Each IP child has its own route table, which may point to the + // default table if it is using default address. + // + Status = EFI_OUT_OF_RESOURCES; + IpInstance->RouteTable = Ip4CreateRouteTable (); + + if (IpInstance->RouteTable == NULL) { + return Status; + } + + // + // Set up the interface. + // + CopyMem (&Ip, &Config->StationAddress, sizeof (IP4_ADDR)); + CopyMem (&Netmask, &Config->SubnetMask, sizeof (IP4_ADDR)); + + Ip = NTOHL (Ip); + Netmask = NTOHL (Netmask); + + if (!Config->UseDefaultAddress) { + // + // Find whether there is already an interface with the same + // station address. All the instances with the same station + // address shares one interface. + // + IpIf = Ip4FindStationAddress (IpSb, Ip, Netmask); + + if (IpIf != NULL) { + NET_GET_REF (IpIf); + + } else { + IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image); + + if (IpIf == NULL) { + goto ON_ERROR; + } + + Status = Ip4SetAddress (IpIf, Ip, Netmask); + + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + Ip4FreeInterface (IpIf, IpInstance); + goto ON_ERROR; + } + + InsertTailList (&IpSb->Interfaces, &IpIf->Link); + } + + // + // Add a route to this connected network in the instance route table. + // + Ip4AddRoute ( + IpInstance->RouteTable, + Ip & Netmask, + Netmask, + IP4_ALLZERO_ADDRESS + ); + } else { + // + // Use the default address. Check the state. + // + if (IpSb->State == IP4_SERVICE_UNSTARTED) { + // + // Trigger the EFI_IP4_CONFIG2_PROTOCOL to retrieve the + // default IPv4 address if it is not available yet. + // + Policy = IpSb->Ip4Config2Instance.Policy; + if (Policy != Ip4Config2PolicyDhcp) { + Ip4Config2 = &IpSb->Ip4Config2Instance.Ip4Config2; + Policy = Ip4Config2PolicyDhcp; + Status= Ip4Config2->SetData ( + Ip4Config2, + Ip4Config2DataTypePolicy, + sizeof (EFI_IP4_CONFIG2_POLICY), + &Policy + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + } + } + + IpIf = IpSb->DefaultInterface; + NET_GET_REF (IpSb->DefaultInterface); + + // + // If default address is used, so is the default route table. + // Any route set by the instance has the precedence over the + // routes in the default route table. Link the default table + // after the instance's table. Routing will search the local + // table first. + // + NET_GET_REF (IpSb->DefaultRouteTable); + IpInstance->RouteTable->Next = IpSb->DefaultRouteTable; + } + + IpInstance->Interface = IpIf; + if (IpIf->Arp != NULL) { + Arp = NULL; + Status = gBS->OpenProtocol ( + IpIf->ArpHandle, + &gEfiArpProtocolGuid, + (VOID **) &Arp, + gIp4DriverBinding.DriverBindingHandle, + IpInstance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + Ip4FreeInterface (IpIf, IpInstance); + goto ON_ERROR; + } + } + InsertTailList (&IpIf->IpInstances, &IpInstance->AddrLink); + + CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData)); + IpInstance->State = IP4_STATE_CONFIGED; + + // + // Although EFI_NO_MAPPING is an error code, the IP child has been + // successfully configured and doesn't need reconfiguration when + // default address is acquired. + // + if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) { + return EFI_NO_MAPPING; + } + + return EFI_SUCCESS; + +ON_ERROR: + Ip4FreeRouteTable (IpInstance->RouteTable); + IpInstance->RouteTable = NULL; + return Status; +} + + +/** + Clean up the IP4 child, release all the resources used by it. + + @param[in] IpInstance The IP4 child to clean up. + + @retval EFI_SUCCESS The IP4 child is cleaned up. + @retval EFI_DEVICE_ERROR Some resources failed to be released. + +**/ +EFI_STATUS +Ip4CleanProtocol ( + IN IP4_PROTOCOL *IpInstance + ) +{ + if (EFI_ERROR (Ip4Cancel (IpInstance, NULL))) { + return EFI_DEVICE_ERROR; + } + + if (EFI_ERROR (Ip4Groups (IpInstance, FALSE, NULL))) { + return EFI_DEVICE_ERROR; + } + + // + // Some packets haven't been recycled. It is because either the + // user forgets to recycle the packets, or because the callback + // hasn't been called. Just leave it alone. + // + if (!IsListEmpty (&IpInstance->Delivered)) { + ; + } + + if (IpInstance->Interface != NULL) { + RemoveEntryList (&IpInstance->AddrLink); + if (IpInstance->Interface->Arp != NULL) { + gBS->CloseProtocol ( + IpInstance->Interface->ArpHandle, + &gEfiArpProtocolGuid, + gIp4DriverBinding.DriverBindingHandle, + IpInstance->Handle + ); + } + Ip4FreeInterface (IpInstance->Interface, IpInstance); + IpInstance->Interface = NULL; + } + + if (IpInstance->RouteTable != NULL) { + if (IpInstance->RouteTable->Next != NULL) { + Ip4FreeRouteTable (IpInstance->RouteTable->Next); + } + + Ip4FreeRouteTable (IpInstance->RouteTable); + IpInstance->RouteTable = NULL; + } + + if (IpInstance->EfiRouteTable != NULL) { + FreePool (IpInstance->EfiRouteTable); + IpInstance->EfiRouteTable = NULL; + IpInstance->EfiRouteCount = 0; + } + + if (IpInstance->Groups != NULL) { + FreePool (IpInstance->Groups); + IpInstance->Groups = NULL; + IpInstance->GroupCount = 0; + } + + NetMapClean (&IpInstance->TxTokens); + + NetMapClean (&IpInstance->RxTokens); + + return EFI_SUCCESS; +} + + +/** + Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance. + + The Configure() function is used to set, change, or reset the operational + parameters and filter settings for this EFI IPv4 Protocol instance. Until these + parameters have been set, no network traffic can be sent or received by this + instance. Once the parameters have been reset (by calling this function with + IpConfigData set to NULL), no more traffic can be sent or received until these + parameters have been set again. Each EFI IPv4 Protocol instance can be started + and stopped independently of each other by enabling or disabling their receive + filter settings with the Configure() function. + + When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will + be appended as an alias address into the addresses list in the EFI IPv4 Protocol + driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL + to retrieve the default IPv4 address if it is not available yet. Clients could + frequently call GetModeData() to check the status to ensure that the default IPv4 + address is ready. + + If operational parameters are reset or changed, any pending transmit and receive + requests will be cancelled. Their completion token status will be set to EFI_ABORTED + and their events will be signaled. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] IpConfigData Pointer to the EFI IPv4 Protocol configuration data structure. + + @retval EFI_SUCCESS The driver instance was successfully opened. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + @retval EFI_UNSUPPORTED One or more of the following conditions is TRUE: + A configuration protocol (DHCP, BOOTP, RARP, etc.) could + not be located when clients choose to use the default IPv4 + address. This EFI IPv4 Protocol implementation does not + support this requested filter or timeout setting. + @retval EFI_OUT_OF_RESOURCES The EFI IPv4 Protocol driver instance data could not be allocated. + @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the + IPv4 address or subnet mask can be changed. The interface must + also be stopped when switching to/from raw packet mode. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv4 + Protocol driver instance is not opened. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Configure ( + IN EFI_IP4_PROTOCOL *This, + IN EFI_IP4_CONFIG_DATA *IpConfigData OPTIONAL + ) +{ + IP4_PROTOCOL *IpInstance; + EFI_IP4_CONFIG_DATA *Current; + EFI_TPL OldTpl; + EFI_STATUS Status; + BOOLEAN AddrOk; + IP4_ADDR IpAddress; + IP4_ADDR SubnetMask; + + // + // First, validate the parameters + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This); + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Validate the configuration first. + // + if (IpConfigData != NULL) { + + CopyMem (&IpAddress, &IpConfigData->StationAddress, sizeof (IP4_ADDR)); + CopyMem (&SubnetMask, &IpConfigData->SubnetMask, sizeof (IP4_ADDR)); + + IpAddress = NTOHL (IpAddress); + SubnetMask = NTOHL (SubnetMask); + + // + // Check whether the station address is a valid unicast address + // + if (!IpConfigData->UseDefaultAddress) { + AddrOk = Ip4StationAddressValid (IpAddress, SubnetMask); + + if (!AddrOk) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + } + + // + // User can only update packet filters when already configured. + // If it wants to change the station address, it must configure(NULL) + // the instance first. + // + if (IpInstance->State == IP4_STATE_CONFIGED) { + Current = &IpInstance->ConfigData; + + if (Current->UseDefaultAddress != IpConfigData->UseDefaultAddress) { + Status = EFI_ALREADY_STARTED; + goto ON_EXIT; + } + + if (!Current->UseDefaultAddress && + (!EFI_IP4_EQUAL (&Current->StationAddress, &IpConfigData->StationAddress) || + !EFI_IP4_EQUAL (&Current->SubnetMask, &IpConfigData->SubnetMask))) { + Status = EFI_ALREADY_STARTED; + goto ON_EXIT; + } + + if (Current->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) { + Status = EFI_NO_MAPPING; + goto ON_EXIT; + } + } + } + + // + // Configure the instance or clean it up. + // + if (IpConfigData != NULL) { + Status = Ip4ConfigProtocol (IpInstance, IpConfigData); + } else { + Status = Ip4CleanProtocol (IpInstance); + + // + // Consider the following valid sequence: Mnp is unloaded-->Ip Stopped-->Udp Stopped, + // Configure (ThisIp, NULL). If the state is changed to UNCONFIGED, + // the unload fails miserably. + // + if (IpInstance->State == IP4_STATE_CONFIGED) { + IpInstance->State = IP4_STATE_UNCONFIGED; + } + } + + // + // Update the MNP's configure data. Ip4ServiceConfigMnp will check + // whether it is necessary to reconfigure the MNP. + // + Ip4ServiceConfigMnp (IpInstance->Service, FALSE); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; + +} + + +/** + Change the IP4 child's multicast setting. The caller + should make sure that the parameters is valid. + + @param[in] IpInstance The IP4 child to change the setting. + @param[in] JoinFlag TRUE to join the group, otherwise leave it. + @param[in] GroupAddress The target group address. + + @retval EFI_ALREADY_STARTED Want to join the group, but already a member of it. + @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources. + @retval EFI_DEVICE_ERROR Failed to set the group configuraton. + @retval EFI_SUCCESS Successfully updated the group setting. + @retval EFI_NOT_FOUND Try to leave the group which it isn't a member. + +**/ +EFI_STATUS +Ip4Groups ( + IN IP4_PROTOCOL *IpInstance, + IN BOOLEAN JoinFlag, + IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL + ) +{ + IP4_ADDR *Members; + IP4_ADDR Group; + UINT32 Index; + + // + // Add it to the instance's Groups, and join the group by IGMP. + // IpInstance->Groups is in network byte order. IGMP operates in + // host byte order + // + if (JoinFlag) { + // + // When JoinFlag is TRUE, GroupAddress shouldn't be NULL. + // + ASSERT (GroupAddress != NULL); + CopyMem (&Group, GroupAddress, sizeof (IP4_ADDR)); + + for (Index = 0; Index < IpInstance->GroupCount; Index++) { + if (IpInstance->Groups[Index] == Group) { + return EFI_ALREADY_STARTED; + } + } + + Members = Ip4CombineGroups (IpInstance->Groups, IpInstance->GroupCount, Group); + + if (Members == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if (EFI_ERROR (Ip4JoinGroup (IpInstance, NTOHL (Group)))) { + FreePool (Members); + return EFI_DEVICE_ERROR; + } + + if (IpInstance->Groups != NULL) { + FreePool (IpInstance->Groups); + } + + IpInstance->Groups = Members; + IpInstance->GroupCount++; + + return EFI_SUCCESS; + } + + // + // Leave the group. Leave all the groups if GroupAddress is NULL. + // Must iterate from the end to the beginning because the GroupCount + // is decreamented each time an address is removed.. + // + for (Index = IpInstance->GroupCount; Index > 0 ; Index--) { + ASSERT (IpInstance->Groups != NULL); + Group = IpInstance->Groups[Index - 1]; + if ((GroupAddress == NULL) || EFI_IP4_EQUAL (&Group, GroupAddress)) { + if (EFI_ERROR (Ip4LeaveGroup (IpInstance, NTOHL (Group)))) { + return EFI_DEVICE_ERROR; + } + + Ip4RemoveGroupAddr (IpInstance->Groups, IpInstance->GroupCount, Group); + IpInstance->GroupCount--; + + if (IpInstance->GroupCount == 0) { + ASSERT (Index == 1); + + FreePool (IpInstance->Groups); + IpInstance->Groups = NULL; + } + + if (GroupAddress != NULL) { + return EFI_SUCCESS; + } + } + } + + return ((GroupAddress != NULL) ? EFI_NOT_FOUND : EFI_SUCCESS); +} + + +/** + Joins and leaves multicast groups. + + The Groups() function is used to join and leave multicast group sessions. Joining + a group will enable reception of matching multicast packets. Leaving a group will + disable the multicast packet reception. + + If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave. + @param[in] GroupAddress Pointer to the IPv4 multicast address. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following is TRUE: + - This is NULL. + - JoinFlag is TRUE and GroupAddress is NULL. + - GroupAddress is not NULL and *GroupAddress is + not a multicast IPv4 address. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_OUT_OF_RESOURCES System resources could not be allocated. + @retval EFI_UNSUPPORTED This EFI IPv4 Protocol implementation does not support multicast groups. + @retval EFI_ALREADY_STARTED The group address is already in the group table (when + JoinFlag is TRUE). + @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Groups ( + IN EFI_IP4_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL + ) +{ + IP4_PROTOCOL *IpInstance; + EFI_STATUS Status; + EFI_TPL OldTpl; + IP4_ADDR McastIp; + + if ((This == NULL) || (JoinFlag && (GroupAddress == NULL))) { + return EFI_INVALID_PARAMETER; + } + + if (GroupAddress != NULL) { + CopyMem (&McastIp, GroupAddress, sizeof (IP4_ADDR)); + + if (!IP4_IS_MULTICAST (NTOHL (McastIp))) { + return EFI_INVALID_PARAMETER; + } + } + + IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This); + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (IpInstance->State != IP4_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) { + Status = EFI_NO_MAPPING; + goto ON_EXIT; + } + + Status = Ip4Groups (IpInstance, JoinFlag, GroupAddress); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Adds and deletes routing table entries. + + The Routes() function adds a route to or deletes a route from the routing table. + + Routes are determined by comparing the SubnetAddress with the destination IPv4 + address arithmetically AND-ed with the SubnetMask. The gateway address must be + on the same subnet as the configured station address. + + The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0. + The default route matches all destination IPv4 addresses that do not match any + other routes. + + A GatewayAddress that is zero is a nonroute. Packets are sent to the destination + IP address if it can be found in the ARP cache or on the local subnet. One automatic + nonroute entry will be inserted into the routing table for outgoing packets that + are addressed to a local subnet (gateway address of 0.0.0.0). + + Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI + IPv4 Protocol instances that use the default IPv4 address will also have copies + of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these + copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its + instances. As a result, client modification to the routing table will be lost. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to + FALSE to add this route to the routing table. SubnetAddress + and SubnetMask are used as the key to each route entry. + @param[in] SubnetAddress The address of the subnet that needs to be routed. + @param[in] SubnetMask The subnet mask of SubnetAddress. + @param[in] GatewayAddress The unicast gateway IPv4 address for this route. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The driver instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - SubnetAddress is NULL. + - SubnetMask is NULL. + - GatewayAddress is NULL. + - *SubnetAddress is not a valid subnet address. + - *SubnetMask is not a valid subnet mask. + - *GatewayAddress is not a valid unicast IPv4 address. + @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table. + @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE). + @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when + DeleteRoute is FALSE). + +**/ +EFI_STATUS +EFIAPI +EfiIp4Routes ( + IN EFI_IP4_PROTOCOL *This, + IN BOOLEAN DeleteRoute, + IN EFI_IPv4_ADDRESS *SubnetAddress, + IN EFI_IPv4_ADDRESS *SubnetMask, + IN EFI_IPv4_ADDRESS *GatewayAddress + ) +{ + IP4_PROTOCOL *IpInstance; + IP4_INTERFACE *IpIf; + IP4_ADDR Dest; + IP4_ADDR Netmask; + IP4_ADDR Nexthop; + EFI_STATUS Status; + EFI_TPL OldTpl; + + // + // First, validate the parameters + // + if ((This == NULL) || (SubnetAddress == NULL) || + (SubnetMask == NULL) || (GatewayAddress == NULL)) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This); + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (IpInstance->State != IP4_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) { + Status = EFI_NO_MAPPING; + goto ON_EXIT; + } + + CopyMem (&Dest, SubnetAddress, sizeof (IP4_ADDR)); + CopyMem (&Netmask, SubnetMask, sizeof (IP4_ADDR)); + CopyMem (&Nexthop, GatewayAddress, sizeof (IP4_ADDR)); + + Dest = NTOHL (Dest); + Netmask = NTOHL (Netmask); + Nexthop = NTOHL (Nexthop); + + IpIf = IpInstance->Interface; + + if (!IP4_IS_VALID_NETMASK (Netmask)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + // + // the gateway address must be a unicast on the connected network if not zero. + // + if ((Nexthop != IP4_ALLZERO_ADDRESS) && + ((IpIf->SubnetMask != IP4_ALLONE_ADDRESS && !IP4_NET_EQUAL (Nexthop, IpIf->Ip, IpIf->SubnetMask)) || + IP4_IS_BROADCAST (Ip4GetNetCast (Nexthop, IpIf)))) { + + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + if (DeleteRoute) { + Status = Ip4DelRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop); + } else { + Status = Ip4AddRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Check whether the user's token or event has already + been enqueued on IP4's list. + + @param[in] Map The container of either user's transmit or receive + token. + @param[in] Item Current item to check against. + @param[in] Context The Token to check againist. + + @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP. + @retval EFI_SUCCESS The current item isn't the same token/event as the + context. + +**/ +EFI_STATUS +EFIAPI +Ip4TokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + EFI_IP4_COMPLETION_TOKEN *Token; + EFI_IP4_COMPLETION_TOKEN *TokenInItem; + + Token = (EFI_IP4_COMPLETION_TOKEN *) Context; + TokenInItem = (EFI_IP4_COMPLETION_TOKEN *) Item->Key; + + if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) { + return EFI_ACCESS_DENIED; + } + + return EFI_SUCCESS; +} + +/** + Validate the user's token against current station address. + + @param[in] Token User's token to validate. + @param[in] IpIf The IP4 child's interface. + @param[in] RawData Set to TRUE to send unformatted packets. + + @retval EFI_INVALID_PARAMETER Some parameters are invalid. + @retval EFI_BAD_BUFFER_SIZE The user's option/data is too long. + @retval EFI_SUCCESS The token is valid. + +**/ +EFI_STATUS +Ip4TxTokenValid ( + IN EFI_IP4_COMPLETION_TOKEN *Token, + IN IP4_INTERFACE *IpIf, + IN BOOLEAN RawData + ) +{ + EFI_IP4_TRANSMIT_DATA *TxData; + EFI_IP4_OVERRIDE_DATA *Override; + IP4_ADDR Src; + IP4_ADDR Gateway; + UINT32 Offset; + UINT32 Index; + UINT32 HeadLen; + + if ((Token == NULL) || (Token->Event == NULL) || (Token->Packet.TxData == NULL)) { + return EFI_INVALID_PARAMETER; + } + + TxData = Token->Packet.TxData; + + // + // Check the fragment table: no empty fragment, and length isn't bogus. + // + if ((TxData->TotalDataLength == 0) || (TxData->FragmentCount == 0)) { + return EFI_INVALID_PARAMETER; + } + + Offset = TxData->TotalDataLength; + + if (Offset > IP4_MAX_PACKET_SIZE) { + return EFI_BAD_BUFFER_SIZE; + } + + for (Index = 0; Index < TxData->FragmentCount; Index++) { + if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) || + (TxData->FragmentTable[Index].FragmentLength == 0)) { + + return EFI_INVALID_PARAMETER; + } + + Offset -= TxData->FragmentTable[Index].FragmentLength; + } + + if (Offset != 0) { + return EFI_INVALID_PARAMETER; + } + + // + // NOTE that OptionsLength/OptionsBuffer/OverrideData are ignored if RawData + // is TRUE. + // + if (RawData) { + return EFI_SUCCESS; + } + + // + // Check the IP options: no more than 40 bytes and format is OK + // + if (TxData->OptionsLength != 0) { + if ((TxData->OptionsLength > 40) || (TxData->OptionsBuffer == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (!Ip4OptionIsValid (TxData->OptionsBuffer, TxData->OptionsLength, FALSE)) { + return EFI_INVALID_PARAMETER; + } + } + + // + // Check the source and gateway: they must be a valid unicast. + // Gateway must also be on the connected network. + // + if (TxData->OverrideData != NULL) { + Override = TxData->OverrideData; + + CopyMem (&Src, &Override->SourceAddress, sizeof (IP4_ADDR)); + CopyMem (&Gateway, &Override->GatewayAddress, sizeof (IP4_ADDR)); + + Src = NTOHL (Src); + Gateway = NTOHL (Gateway); + + if ((NetGetIpClass (Src) > IP4_ADDR_CLASSC) || + (Src == IP4_ALLONE_ADDRESS) || + IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) { + + return EFI_INVALID_PARAMETER; + } + + // + // If gateway isn't zero, it must be a unicast address, and + // on the connected network. + // + if ((Gateway != IP4_ALLZERO_ADDRESS) && + ((NetGetIpClass (Gateway) > IP4_ADDR_CLASSC) || + !IP4_NET_EQUAL (Gateway, IpIf->Ip, IpIf->SubnetMask) || + IP4_IS_BROADCAST (Ip4GetNetCast (Gateway, IpIf)))) { + + return EFI_INVALID_PARAMETER; + } + } + + // + // Check the packet length: Head length and packet length all has a limit + // + HeadLen = sizeof (IP4_HEAD) + ((TxData->OptionsLength + 3) &~0x03); + + if ((HeadLen > IP4_MAX_HEADLEN) || + (TxData->TotalDataLength + HeadLen > IP4_MAX_PACKET_SIZE)) { + + return EFI_BAD_BUFFER_SIZE; + } + + return EFI_SUCCESS; +} + + +/** + The callback function for the net buffer which wraps the user's + transmit token. Although it seems this function is pretty simple, + there are some subtle things. + When user requests the IP to transmit a packet by passing it a + token, the token is wrapped in an IP4_TXTOKEN_WRAP and the data + is wrapped in an net buffer. the net buffer's Free function is + set to Ip4FreeTxToken. The Token and token wrap are added to the + IP child's TxToken map. Then the buffer is passed to Ip4Output for + transmission. If something error happened before that, the buffer + is freed, which in turn will free the token wrap. The wrap may + have been added to the TxToken map or not, and the user's event + shouldn't be fired because we are still in the EfiIp4Transmit. If + the buffer has been sent by Ip4Output, it should be removed from + the TxToken map and user's event signaled. The token wrap and buffer + are bound together. Check the comments in Ip4Output for information + about IP fragmentation. + + @param[in] Context The token's wrap. + +**/ +VOID +EFIAPI +Ip4FreeTxToken ( + IN VOID *Context + ) +{ + IP4_TXTOKEN_WRAP *Wrap; + NET_MAP_ITEM *Item; + + Wrap = (IP4_TXTOKEN_WRAP *) Context; + + // + // Signal IpSecRecycleEvent to inform IPsec free the memory + // + if (Wrap->IpSecRecycleSignal != NULL) { + gBS->SignalEvent (Wrap->IpSecRecycleSignal); + } + + // + // Find the token in the instance's map. EfiIp4Transmit put the + // token to the map. If that failed, NetMapFindKey will return NULL. + // + Item = NetMapFindKey (&Wrap->IpInstance->TxTokens, Wrap->Token); + + if (Item != NULL) { + NetMapRemoveItem (&Wrap->IpInstance->TxTokens, Item, NULL); + } + + if (Wrap->Sent) { + gBS->SignalEvent (Wrap->Token->Event); + + // + // Dispatch the DPC queued by the NotifyFunction of Token->Event. + // + DispatchDpc (); + } + + FreePool (Wrap); +} + + +/** + The callback function to Ip4Output to update the transmit status. + + @param Ip4Instance The Ip4Instance that request the transmit. + @param Packet The user's transmit request. + @param IoStatus The result of the transmission. + @param Flag Not used during transmission. + @param Context The token's wrap. + +**/ +VOID +Ip4OnPacketSent ( + IP4_PROTOCOL *Ip4Instance, + NET_BUF *Packet, + EFI_STATUS IoStatus, + UINT32 Flag, + VOID *Context + ) +{ + IP4_TXTOKEN_WRAP *Wrap; + + // + // This is the transmission request from upper layer, + // not the IP4 driver itself. + // + ASSERT (Ip4Instance != NULL); + + // + // The first fragment of the packet has been sent. Update + // the token's status. That is, if fragmented, the transmit's + // status is the first fragment's status. The Wrap will be + // release when all the fragments are release. Check the comments + // in Ip4FreeTxToken and Ip4Output for information. + // + Wrap = (IP4_TXTOKEN_WRAP *) Context; + Wrap->Token->Status = IoStatus; + + NetbufFree (Wrap->Packet); +} + + +/** + Places outgoing data packets into the transmit queue. + + The Transmit() function places a sending request in the transmit queue of this + EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some + errors occur, the event in the token will be signaled and the status is updated. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] Token Pointer to the transmit token. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more pameters are invalid. + @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.Event + was already in the transmit queue. + @retval EFI_NOT_READY The completion token could not be queued because the transmit + queue is full. + @retval EFI_NOT_FOUND Not route is found to destination address. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data. + @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too + short to transmit. + @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + total data length is + greater than MTU (or greater than the maximum packet size if + Token.Packet.TxData.OverrideData. + DoNotFragment is TRUE). + +**/ +EFI_STATUS +EFIAPI +EfiIp4Transmit ( + IN EFI_IP4_PROTOCOL *This, + IN EFI_IP4_COMPLETION_TOKEN *Token + ) +{ + IP4_SERVICE *IpSb; + IP4_PROTOCOL *IpInstance; + IP4_INTERFACE *IpIf; + IP4_TXTOKEN_WRAP *Wrap; + EFI_IP4_TRANSMIT_DATA *TxData; + EFI_IP4_CONFIG_DATA *Config; + EFI_IP4_OVERRIDE_DATA *Override; + IP4_HEAD Head; + IP4_ADDR GateWay; + EFI_STATUS Status; + EFI_TPL OldTpl; + BOOLEAN DontFragment; + UINT32 HeadLen; + UINT8 RawHdrLen; + UINT32 OptionsLength; + UINT8 *OptionsBuffer; + VOID *FirstFragment; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This); + + if (IpInstance->State != IP4_STATE_CONFIGED) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + IpSb = IpInstance->Service; + IpIf = IpInstance->Interface; + Config = &IpInstance->ConfigData; + + if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) { + Status = EFI_NO_MAPPING; + goto ON_EXIT; + } + + // + // make sure that token is properly formated + // + Status = Ip4TxTokenValid (Token, IpIf, Config->RawData); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Check whether the token or signal already existed. + // + if (EFI_ERROR (NetMapIterate (&IpInstance->TxTokens, Ip4TokenExist, Token))) { + Status = EFI_ACCESS_DENIED; + goto ON_EXIT; + } + + // + // Build the IP header, need to fill in the Tos, TotalLen, Id, + // fragment, Ttl, protocol, Src, and Dst. + // + TxData = Token->Packet.TxData; + + FirstFragment = NULL; + + if (Config->RawData) { + // + // When RawData is TRUE, first buffer in FragmentTable points to a raw + // IPv4 fragment including IPv4 header and options. + // + FirstFragment = TxData->FragmentTable[0].FragmentBuffer; + CopyMem (&RawHdrLen, FirstFragment, sizeof (UINT8)); + + RawHdrLen = (UINT8) (RawHdrLen & 0x0f); + if (RawHdrLen < 5) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + RawHdrLen = (UINT8) (RawHdrLen << 2); + + CopyMem (&Head, FirstFragment, IP4_MIN_HEADLEN); + + Ip4NtohHead (&Head); + HeadLen = 0; + DontFragment = IP4_DO_NOT_FRAGMENT (Head.Fragment); + + if (!DontFragment) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + GateWay = IP4_ALLZERO_ADDRESS; + + // + // Get IPv4 options from first fragment. + // + if (RawHdrLen == IP4_MIN_HEADLEN) { + OptionsLength = 0; + OptionsBuffer = NULL; + } else { + OptionsLength = RawHdrLen - IP4_MIN_HEADLEN; + OptionsBuffer = (UINT8 *) FirstFragment + IP4_MIN_HEADLEN; + } + + // + // Trim off IPv4 header and options from first fragment. + // + TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment + RawHdrLen; + TxData->FragmentTable[0].FragmentLength = TxData->FragmentTable[0].FragmentLength - RawHdrLen; + } else { + CopyMem (&Head.Dst, &TxData->DestinationAddress, sizeof (IP4_ADDR)); + Head.Dst = NTOHL (Head.Dst); + + if (TxData->OverrideData != NULL) { + Override = TxData->OverrideData; + Head.Protocol = Override->Protocol; + Head.Tos = Override->TypeOfService; + Head.Ttl = Override->TimeToLive; + DontFragment = Override->DoNotFragment; + + CopyMem (&Head.Src, &Override->SourceAddress, sizeof (IP4_ADDR)); + CopyMem (&GateWay, &Override->GatewayAddress, sizeof (IP4_ADDR)); + + Head.Src = NTOHL (Head.Src); + GateWay = NTOHL (GateWay); + } else { + Head.Src = IpIf->Ip; + GateWay = IP4_ALLZERO_ADDRESS; + Head.Protocol = Config->DefaultProtocol; + Head.Tos = Config->TypeOfService; + Head.Ttl = Config->TimeToLive; + DontFragment = Config->DoNotFragment; + } + + Head.Fragment = IP4_HEAD_FRAGMENT_FIELD (DontFragment, FALSE, 0); + HeadLen = (TxData->OptionsLength + 3) & (~0x03); + + OptionsLength = TxData->OptionsLength; + OptionsBuffer = (UINT8 *) (TxData->OptionsBuffer); + } + + // + // If don't fragment and fragment needed, return error + // + if (DontFragment && (TxData->TotalDataLength + HeadLen > IpSb->MaxPacketSize)) { + Status = EFI_BAD_BUFFER_SIZE; + goto ON_EXIT; + } + + // + // OK, it survives all the validation check. Wrap the token in + // a IP4_TXTOKEN_WRAP and the data in a netbuf + // + Status = EFI_OUT_OF_RESOURCES; + Wrap = AllocateZeroPool (sizeof (IP4_TXTOKEN_WRAP)); + if (Wrap == NULL) { + goto ON_EXIT; + } + + Wrap->IpInstance = IpInstance; + Wrap->Token = Token; + Wrap->Sent = FALSE; + Wrap->Life = IP4_US_TO_SEC (Config->TransmitTimeout); + Wrap->Packet = NetbufFromExt ( + (NET_FRAGMENT *) TxData->FragmentTable, + TxData->FragmentCount, + IP4_MAX_HEADLEN, + 0, + Ip4FreeTxToken, + Wrap + ); + + if (Wrap->Packet == NULL) { + FreePool (Wrap); + goto ON_EXIT; + } + + Token->Status = EFI_NOT_READY; + + if (EFI_ERROR (NetMapInsertTail (&IpInstance->TxTokens, Token, Wrap))) { + // + // NetbufFree will call Ip4FreeTxToken, which in turn will + // free the IP4_TXTOKEN_WRAP. Now, the token wrap hasn't been + // enqueued. + // + if (Config->RawData) { + // + // Restore pointer of first fragment in RawData mode. + // + TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment; + } + + NetbufFree (Wrap->Packet); + goto ON_EXIT; + } + + // + // Mark the packet sent before output it. Mark it not sent again if the + // returned status is not EFI_SUCCESS; + // + Wrap->Sent = TRUE; + + Status = Ip4Output ( + IpSb, + IpInstance, + Wrap->Packet, + &Head, + OptionsBuffer, + OptionsLength, + GateWay, + Ip4OnPacketSent, + Wrap + ); + + if (EFI_ERROR (Status)) { + Wrap->Sent = FALSE; + + if (Config->RawData) { + // + // Restore pointer of first fragment in RawData mode. + // + TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment; + } + + NetbufFree (Wrap->Packet); + } + + if (Config->RawData) { + // + // Restore pointer of first fragment in RawData mode. + // + TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment; + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Places a receiving request into the receiving queue. + + The Receive() function places a completion token into the receive packet queue. + This function is always asynchronous. + + The Token.Event field in the completion token must be filled in by the caller + and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol + driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event + is signaled. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] Token Pointer to a token that is associated with the receive data descriptor. + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, RARP, etc.) + is not finished yet. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Token is NULL. + - Token.Event is NULL. + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system + resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The EFI IPv4 Protocol instance has been reset to startup defaults. + EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already + in the receive queue. + @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full. + @retval EFI_ICMP_ERROR An ICMP error packet was received. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Receive ( + IN EFI_IP4_PROTOCOL *This, + IN EFI_IP4_COMPLETION_TOKEN *Token + ) +{ + IP4_PROTOCOL *IpInstance; + EFI_STATUS Status; + EFI_TPL OldTpl; + + // + // First validate the parameters + // + if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (IpInstance->State != IP4_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + // + // Check whether the toke is already on the receive queue. + // + Status = NetMapIterate (&IpInstance->RxTokens, Ip4TokenExist, Token); + + if (EFI_ERROR (Status)) { + Status = EFI_ACCESS_DENIED; + goto ON_EXIT; + } + + // + // Queue the token then check whether there is pending received packet. + // + Status = NetMapInsertTail (&IpInstance->RxTokens, Token, NULL); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = Ip4InstanceDeliverPacket (IpInstance); + + // + // Dispatch the DPC queued by the NotifyFunction of this instane's receive + // event. + // + DispatchDpc (); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Cancel the transmitted but not recycled packet. If a matching + token is found, it will call Ip4CancelPacket to cancel the + packet. Ip4CancelPacket will cancel all the fragments of the + packet. When all the fragments are freed, the IP4_TXTOKEN_WRAP + will be deleted from the Map, and user's event signalled. + Because Ip4CancelPacket and other functions are all called in + line, so, after Ip4CancelPacket returns, the Item has been freed. + + @param[in] Map The IP4 child's transmit queue. + @param[in] Item The current transmitted packet to test. + @param[in] Context The user's token to cancel. + + @retval EFI_SUCCESS Continue to check the next Item. + @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled. + +**/ +EFI_STATUS +EFIAPI +Ip4CancelTxTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + EFI_IP4_COMPLETION_TOKEN *Token; + IP4_TXTOKEN_WRAP *Wrap; + + Token = (EFI_IP4_COMPLETION_TOKEN *) Context; + + // + // Return EFI_SUCCESS to check the next item in the map if + // this one doesn't match. + // + if ((Token != NULL) && (Token != Item->Key)) { + return EFI_SUCCESS; + } + + Wrap = (IP4_TXTOKEN_WRAP *) Item->Value; + ASSERT (Wrap != NULL); + + // + // Don't access the Item, Wrap and Token's members after this point. + // Item and wrap has been freed. And we no longer own the Token. + // + Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED); + + // + // If only one item is to be cancel, return EFI_ABORTED to stop + // iterating the map any more. + // + if (Token != NULL) { + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + + +/** + Cancel the receive request. This is quiet simple, because + it is only enqueued in our local receive map. + + @param[in] Map The IP4 child's receive queue. + @param[in] Item Current receive request to cancel. + @param[in] Context The user's token to cancel. + + @retval EFI_SUCCESS Continue to check the next receive request on the + queue. + @retval EFI_ABORTED The user's token (token != NULL) has been + cancelled. + +**/ +EFI_STATUS +EFIAPI +Ip4CancelRxTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + EFI_IP4_COMPLETION_TOKEN *Token; + EFI_IP4_COMPLETION_TOKEN *This; + + Token = (EFI_IP4_COMPLETION_TOKEN *) Context; + This = Item->Key; + + if ((Token != NULL) && (Token != This)) { + return EFI_SUCCESS; + } + + NetMapRemoveItem (Map, Item, NULL); + + This->Status = EFI_ABORTED; + This->Packet.RxData = NULL; + gBS->SignalEvent (This->Event); + + if (Token != NULL) { + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + + +/** + Cancel the user's receive/transmit request. + + @param[in] IpInstance The IP4 child. + @param[in] Token The token to cancel. If NULL, all token will be + cancelled. + + @retval EFI_SUCCESS The token is cancelled. + @retval EFI_NOT_FOUND The token isn't found on either the + transmit/receive queue. + @retval EFI_DEVICE_ERROR Not all token is cancelled when Token is NULL. + +**/ +EFI_STATUS +Ip4Cancel ( + IN IP4_PROTOCOL *IpInstance, + IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // First check the transmitted packet. Ip4CancelTxTokens returns + // EFI_ABORTED to mean that the token has been cancelled when + // token != NULL. So, return EFI_SUCCESS for this condition. + // + Status = NetMapIterate (&IpInstance->TxTokens, Ip4CancelTxTokens, Token); + + if (EFI_ERROR (Status)) { + if ((Token != NULL) && (Status == EFI_ABORTED)) { + return EFI_SUCCESS; + } + + return Status; + } + + // + // Check the receive queue. Ip4CancelRxTokens also returns EFI_ABORT + // for Token!=NULL and it is cancelled. + // + Status = NetMapIterate (&IpInstance->RxTokens, Ip4CancelRxTokens, Token); + // + // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's + // events. + // + DispatchDpc (); + if (EFI_ERROR (Status)) { + if ((Token != NULL) && (Status == EFI_ABORTED)) { + return EFI_SUCCESS; + } + + return Status; + } + + // + // OK, if the Token is found when Token != NULL, the NetMapIterate + // will return EFI_ABORTED, which has been interrupted as EFI_SUCCESS. + // + if (Token != NULL) { + return EFI_NOT_FOUND; + } + + // + // If Token == NULL, cancel all the tokens. return error if no + // all of them are cancelled. + // + if (!NetMapIsEmpty (&IpInstance->TxTokens) || + !NetMapIsEmpty (&IpInstance->RxTokens)) { + + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + + +/** + Abort an asynchronous transmit or receive request. + + The Cancel() function is used to abort a pending transmit or receive request. + If the token is in the transmit or receive request queues, after calling this + function, Token->Status will be set to EFI_ABORTED and then Token->Event will + be signaled. If the token is not in one of the queues, which usually means the + asynchronous operation has completed, this function will not signal the token + and EFI_NOT_FOUND is returned. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_IP4_PROTOCOL.Transmit() or + EFI_IP4_PROTOCOL.Receive(). If NULL, all pending + tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is + defined in EFI_IP4_PROTOCOL.Transmit(). + + @retval EFI_SUCCESS The asynchronous I/O request was aborted and + Token.->Event was signaled. When Token is NULL, all + pending requests were aborted and their events were signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was + not found in the transmit or receive queue. It has either completed + or was not issued by Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +EfiIp4Cancel ( + IN EFI_IP4_PROTOCOL *This, + IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + IP4_PROTOCOL *IpInstance; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (IpInstance->State != IP4_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) { + Status = EFI_NO_MAPPING; + goto ON_EXIT; + } + + Status = Ip4Cancel (IpInstance, Token); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function polls for incoming data packets and processes outgoing data + packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll() + function to increase the rate that data packets are moved between the communications + device and the transmit and receive queues. + + In some systems the periodic timer event may not poll the underlying communications + device fast enough to transmit and/or receive all data packets without missing + incoming packets or dropping outgoing packets. Drivers and applications that are + experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function + more often. + + @param[in] This Pointer to the EFI_IP4_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_NOT_READY No incoming or outgoing data is processed. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue. + Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +EfiIp4Poll ( + IN EFI_IP4_PROTOCOL *This + ) +{ + IP4_PROTOCOL *IpInstance; + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This); + + if (IpInstance->State == IP4_STATE_UNCONFIGED) { + return EFI_NOT_STARTED; + } + + Mnp = IpInstance->Service->Mnp; + + // + // Don't lock the Poll function to enable the deliver of + // the packet polled up. + // + return Mnp->Poll (Mnp); +} + +/** + Decrease the life of the transmitted packets. If it is + decreased to zero, cancel the packet. This function is + called by Ip4PacketTimerTicking which time out both the + received-but-not-delivered and transmitted-but-not-recycle + packets. + + @param[in] Map The IP4 child's transmit map. + @param[in] Item Current transmitted packet. + @param[in] Context Not used. + + @retval EFI_SUCCESS Always returns EFI_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +Ip4SentPacketTicking ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + IP4_TXTOKEN_WRAP *Wrap; + + Wrap = (IP4_TXTOKEN_WRAP *) Item->Value; + ASSERT (Wrap != NULL); + + if ((Wrap->Life > 0) && (--Wrap->Life == 0)) { + Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED); + } + + return EFI_SUCCESS; +} + +/** + This heart beat timer of IP4 service instance times out all of its IP4 children's + received-but-not-delivered and transmitted-but-not-recycle packets, and provides + time input for its IGMP protocol. + + @param[in] Event The IP4 service instance's heart beat timer. + @param[in] Context The IP4 service instance. + +**/ +VOID +EFIAPI +Ip4TimerTicking ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + IP4_SERVICE *IpSb; + + IpSb = (IP4_SERVICE *) Context; + NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE); + + Ip4PacketTimerTicking (IpSb); + Ip4IgmpTicking (IpSb); +} + +/** + This dedicated timer is used to poll underlying network media status. In case + of cable swap or wireless network switch, a new round auto configuration will + be initiated. The timer will signal the IP4 to run DHCP configuration again. + IP4 driver will free old IP address related resource, such as route table and + Interface, then initiate a DHCP process to acquire new IP, eventually create + route table for new IP address. + + @param[in] Event The IP4 service instance's heart beat timer. + @param[in] Context The IP4 service instance. + +**/ +VOID +EFIAPI +Ip4TimerReconfigChecking ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + IP4_SERVICE *IpSb; + BOOLEAN OldMediaPresent; + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_MODE SnpModeData; + + IpSb = (IP4_SERVICE *) Context; + NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE); + + OldMediaPresent = IpSb->MediaPresent; + + // + // Get fresh mode data from MNP, since underlying media status may change. + // Here, it needs to mention that the MediaPresent can also be checked even if + // EFI_NOT_STARTED returned while this MNP child driver instance isn't configured. + // + Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &SnpModeData); + if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) { + return; + } + + IpSb->MediaPresent = SnpModeData.MediaPresent; + // + // Media transimit Unpresent to Present means new link movement is detected. + // + if (!OldMediaPresent && IpSb->MediaPresent && (IpSb->Ip4Config2Instance.Policy == Ip4Config2PolicyDhcp)) { + // + // Signal the IP4 to run the dhcp configuration again. IP4 driver will free + // old IP address related resource, such as route table and Interface, then + // initiate a DHCP round to acquire new IP, eventually + // create route table for new IP address. + // + if (IpSb->ReconfigEvent != NULL) { + Status = gBS->SignalEvent (IpSb->ReconfigEvent); + DispatchDpc (); + } + } +} diff --git a/NetworkPkg/Ip4Dxe/Ip4Impl.h b/NetworkPkg/Ip4Dxe/Ip4Impl.h new file mode 100644 index 000000000..a322a8598 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Impl.h @@ -0,0 +1,417 @@ +/** @file + Ip4 internal functions and type defintions. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2015 Hewlett-Packard Development Company, L.P.
+ +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP4_IMPL_H__ +#define __EFI_IP4_IMPL_H__ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Ip4Common.h" +#include "Ip4Driver.h" +#include "Ip4If.h" +#include "Ip4Icmp.h" +#include "Ip4Option.h" +#include "Ip4Igmp.h" +#include "Ip4Route.h" +#include "Ip4Input.h" +#include "Ip4Output.h" +#include "Ip4Config2Impl.h" +#include "Ip4Config2Nv.h" +#include "Ip4NvData.h" + +#define IP4_PROTOCOL_SIGNATURE SIGNATURE_32 ('I', 'P', '4', 'P') +#define IP4_SERVICE_SIGNATURE SIGNATURE_32 ('I', 'P', '4', 'S') + +// +// The state of IP4 protocol. It starts from UNCONFIGED. if it is +// successfully configured, it goes to CONFIGED. if configure NULL +// is called, it becomes UNCONFIGED again. +// +#define IP4_STATE_UNCONFIGED 0 +#define IP4_STATE_CONFIGED 1 + +// +// The state of IP4 service. It starts from UNSTARTED. It transits +// to STARTED if autoconfigure is started. If default address is +// configured, it becomes CONFIGED. and if partly destroyed, it goes +// to DESTROY. +// +#define IP4_SERVICE_UNSTARTED 0 +#define IP4_SERVICE_STARTED 1 +#define IP4_SERVICE_CONFIGED 2 +#define IP4_SERVICE_DESTROY 3 + + +/// +/// IP4_TXTOKEN_WRAP wraps the upper layer's transmit token. +/// The user's data is kept in the Packet. When fragment is +/// needed, each fragment of the Packet has a reference to the +/// Packet, no data is actually copied. The Packet will be +/// released when all the fragments of it have been recycled by +/// MNP. Upon then, the IP4_TXTOKEN_WRAP will be released, and +/// user's event signalled. +/// +typedef struct { + IP4_PROTOCOL *IpInstance; + EFI_IP4_COMPLETION_TOKEN *Token; + EFI_EVENT IpSecRecycleSignal; + NET_BUF *Packet; + BOOLEAN Sent; + INTN Life; +} IP4_TXTOKEN_WRAP; + +/// +/// IP4_IPSEC_WRAP wraps the packet received from MNP layer. The packet +/// will be released after it has been processed by the receiver. Upon then, +/// the IP4_IPSEC_WRAP will be released, and the IpSecRecycleSignal will be signaled +/// to notice IPsec to free the resources. +/// +typedef struct { + EFI_EVENT IpSecRecycleSignal; + NET_BUF *Packet; +} IP4_IPSEC_WRAP; + +/// +/// IP4_RXDATA_WRAP wraps the data IP4 child delivers to the +/// upper layers. The received packet is kept in the Packet. +/// The Packet itself may be constructured from some fragments. +/// All the fragments of the Packet is organized by a +/// IP4_ASSEMBLE_ENTRY structure. If the Packet is recycled by +/// the upper layer, the assemble entry and its associated +/// fragments will be freed at last. +/// +typedef struct { + LIST_ENTRY Link; + IP4_PROTOCOL *IpInstance; + NET_BUF *Packet; + EFI_IP4_RECEIVE_DATA RxData; +} IP4_RXDATA_WRAP; + + +struct _IP4_PROTOCOL { + UINT32 Signature; + + EFI_IP4_PROTOCOL Ip4Proto; + EFI_HANDLE Handle; + INTN State; + + BOOLEAN InDestroy; + + IP4_SERVICE *Service; + LIST_ENTRY Link; // Link to all the IP protocol from the service + + // + // User's transmit/receive tokens, and received/deliverd packets + // + NET_MAP RxTokens; + NET_MAP TxTokens; // map between (User's Token, IP4_TXTOKE_WRAP) + LIST_ENTRY Received; // Received but not delivered packet + LIST_ENTRY Delivered; // Delivered and to be recycled packets + EFI_LOCK RecycleLock; + + // + // Instance's address and route tables. There are two route tables. + // RouteTable is used by the IP4 driver to route packet. EfiRouteTable + // is used to communicate the current route info to the upper layer. + // + IP4_INTERFACE *Interface; + LIST_ENTRY AddrLink; // Ip instances with the same IP address. + IP4_ROUTE_TABLE *RouteTable; + + EFI_IP4_ROUTE_TABLE *EfiRouteTable; + UINT32 EfiRouteCount; + + // + // IGMP data for this instance + // + IP4_ADDR *Groups; // stored in network byte order + UINT32 GroupCount; + + EFI_IP4_CONFIG_DATA ConfigData; + +}; + +struct _IP4_SERVICE { + UINT32 Signature; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + INTN State; + + // + // List of all the IP instances and interfaces, and default + // interface and route table and caches. + // + UINTN NumChildren; + LIST_ENTRY Children; + + LIST_ENTRY Interfaces; + + IP4_INTERFACE *DefaultInterface; + IP4_ROUTE_TABLE *DefaultRouteTable; + + // + // Ip reassemble utilities, and IGMP data + // + IP4_ASSEMBLE_TABLE Assemble; + IGMP_SERVICE_DATA IgmpCtrl; + + // + // Low level protocol used by this service instance + // + EFI_HANDLE Image; + EFI_HANDLE Controller; + + EFI_HANDLE MnpChildHandle; + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + + EFI_MANAGED_NETWORK_CONFIG_DATA MnpConfigData; + EFI_SIMPLE_NETWORK_MODE SnpMode; + + EFI_EVENT Timer; + EFI_EVENT ReconfigCheckTimer; + EFI_EVENT ReconfigEvent; + + BOOLEAN Reconfig; + + // + // Underlying media present status. + // + BOOLEAN MediaPresent; + + // + // IPv4 Configuration II Protocol instance + // + IP4_CONFIG2_INSTANCE Ip4Config2Instance; + + CHAR16 *MacString; + + UINT32 MaxPacketSize; + UINT32 OldMaxPacketSize; ///< The MTU before IPsec enable. +}; + +#define IP4_INSTANCE_FROM_PROTOCOL(Ip4) \ + CR ((Ip4), IP4_PROTOCOL, Ip4Proto, IP4_PROTOCOL_SIGNATURE) + +#define IP4_SERVICE_FROM_PROTOCOL(Sb) \ + CR ((Sb), IP4_SERVICE, ServiceBinding, IP4_SERVICE_SIGNATURE) + +#define IP4_SERVICE_FROM_CONFIG2_INSTANCE(This) \ + CR (This, IP4_SERVICE, Ip4Config2Instance, IP4_SERVICE_SIGNATURE) + + +#define IP4_NO_MAPPING(IpInstance) (!(IpInstance)->Interface->Configured) + +extern EFI_IP4_PROTOCOL mEfiIp4ProtocolTemplete; + +/** + Config the MNP parameter used by IP. The IP driver use one MNP + child to transmit/receive frames. By default, it configures MNP + to receive unicast/multicast/broadcast. And it will enable/disable + the promiscous receive according to whether there is IP child + enable that or not. If Force is FALSE, it will iterate through + all the IP children to check whether the promiscuous receive + setting has been changed. If it hasn't been changed, it won't + reconfigure the MNP. If Force is TRUE, the MNP is configured no + matter whether that is changed or not. + + @param[in] IpSb The IP4 service instance that is to be changed. + @param[in] Force Force the configuration or not. + + @retval EFI_SUCCESS The MNP is successfully configured/reconfigured. + @retval Others Configuration failed. + +**/ +EFI_STATUS +Ip4ServiceConfigMnp ( + IN IP4_SERVICE *IpSb, + IN BOOLEAN Force + ); + +/** + Intiialize the IP4_PROTOCOL structure to the unconfigured states. + + @param IpSb The IP4 service instance. + @param IpInstance The IP4 child instance. + +**/ +VOID +Ip4InitProtocol ( + IN IP4_SERVICE *IpSb, + IN OUT IP4_PROTOCOL *IpInstance + ); + +/** + Clean up the IP4 child, release all the resources used by it. + + @param[in] IpInstance The IP4 child to clean up. + + @retval EFI_SUCCESS The IP4 child is cleaned up. + @retval EFI_DEVICE_ERROR Some resources failed to be released. + +**/ +EFI_STATUS +Ip4CleanProtocol ( + IN IP4_PROTOCOL *IpInstance + ); + +/** + Cancel the user's receive/transmit request. + + @param[in] IpInstance The IP4 child. + @param[in] Token The token to cancel. If NULL, all token will be + cancelled. + + @retval EFI_SUCCESS The token is cancelled. + @retval EFI_NOT_FOUND The token isn't found on either the + transmit/receive queue. + @retval EFI_DEVICE_ERROR Not all token is cancelled when Token is NULL. + +**/ +EFI_STATUS +Ip4Cancel ( + IN IP4_PROTOCOL *IpInstance, + IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + Change the IP4 child's multicast setting. The caller + should make sure that the parameters is valid. + + @param[in] IpInstance The IP4 child to change the setting. + @param[in] JoinFlag TRUE to join the group, otherwise leave it + @param[in] GroupAddress The target group address + + @retval EFI_ALREADY_STARTED Want to join the group, but already a member of it + @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources. + @retval EFI_DEVICE_ERROR Failed to set the group configuraton + @retval EFI_SUCCESS Successfully updated the group setting. + @retval EFI_NOT_FOUND Try to leave the group which it isn't a member. + +**/ +EFI_STATUS +Ip4Groups ( + IN IP4_PROTOCOL *IpInstance, + IN BOOLEAN JoinFlag, + IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL + ); + +/** + This heart beat timer of IP4 service instance times out all of its IP4 children's + received-but-not-delivered and transmitted-but-not-recycle packets, and provides + time input for its IGMP protocol. + + @param[in] Event The IP4 service instance's heart beat timer. + @param[in] Context The IP4 service instance. + +**/ +VOID +EFIAPI +Ip4TimerTicking ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + This dedicated timer is used to poll underlying network media status. In case + of cable swap or wireless network switch, a new round auto configuration will + be initiated. The timer will signal the IP4 to run DHCP configuration again. + IP4 driver will free old IP address related resource, such as route table and + Interface, then initiate a DHCP process to acquire new IP, eventually create + route table for new IP address. + + @param[in] Event The IP4 service instance's heart beat timer. + @param[in] Context The IP4 service instance. + +**/ +VOID +EFIAPI +Ip4TimerReconfigChecking ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Decrease the life of the transmitted packets. If it is + decreased to zero, cancel the packet. This function is + called by Ip4PacketTimerTicking which time out both the + received-but-not-delivered and transmitted-but-not-recycle + packets. + + @param[in] Map The IP4 child's transmit map. + @param[in] Item Current transmitted packet. + @param[in] Context Not used. + + @retval EFI_SUCCESS Always returns EFI_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +Ip4SentPacketTicking ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ); + +/** + The callback function for the net buffer which wraps the user's + transmit token. Although it seems this function is pretty simple, + there are some subtle things. + When user requests the IP to transmit a packet by passing it a + token, the token is wrapped in an IP4_TXTOKEN_WRAP and the data + is wrapped in an net buffer. the net buffer's Free function is + set to Ip4FreeTxToken. The Token and token wrap are added to the + IP child's TxToken map. Then the buffer is passed to Ip4Output for + transmission. If something error happened before that, the buffer + is freed, which in turn will free the token wrap. The wrap may + have been added to the TxToken map or not, and the user's event + shouldn't be fired because we are still in the EfiIp4Transmit. If + the buffer has been sent by Ip4Output, it should be removed from + the TxToken map and user's event signaled. The token wrap and buffer + are bound together. Check the comments in Ip4Output for information + about IP fragmentation. + + @param[in] Context The token's wrap. + +**/ +VOID +EFIAPI +Ip4FreeTxToken ( + IN VOID *Context + ); + +extern EFI_IPSEC2_PROTOCOL *mIpSec; +extern BOOLEAN mIpSec2Installed; + +#endif diff --git a/NetworkPkg/Ip4Dxe/Ip4Input.c b/NetworkPkg/Ip4Dxe/Ip4Input.c new file mode 100644 index 000000000..24c584658 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Input.c @@ -0,0 +1,1597 @@ +/** @file + IP4 input process. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2015 Hewlett-Packard Development Company, L.P.
+ +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + + +/** + Create an empty assemble entry for the packet identified by + (Dst, Src, Id, Protocol). The default life for the packet is + 120 seconds. + + @param[in] Dst The destination address + @param[in] Src The source address + @param[in] Id The ID field in IP header + @param[in] Protocol The protocol field in IP header + + @return NULL if failed to allocate memory for the entry, otherwise + the point to just created reassemble entry. + +**/ +IP4_ASSEMBLE_ENTRY * +Ip4CreateAssembleEntry ( + IN IP4_ADDR Dst, + IN IP4_ADDR Src, + IN UINT16 Id, + IN UINT8 Protocol + ) +{ + + IP4_ASSEMBLE_ENTRY *Assemble; + + Assemble = AllocatePool (sizeof (IP4_ASSEMBLE_ENTRY)); + + if (Assemble == NULL) { + return NULL; + } + + InitializeListHead (&Assemble->Link); + InitializeListHead (&Assemble->Fragments); + + Assemble->Dst = Dst; + Assemble->Src = Src; + Assemble->Id = Id; + Assemble->Protocol = Protocol; + Assemble->TotalLen = 0; + Assemble->CurLen = 0; + Assemble->Head = NULL; + Assemble->Info = NULL; + Assemble->Life = IP4_FRAGMENT_LIFE; + + return Assemble; +} + + +/** + Release all the fragments of a packet, then free the assemble entry. + + @param[in] Assemble The assemble entry to free + +**/ +VOID +Ip4FreeAssembleEntry ( + IN IP4_ASSEMBLE_ENTRY *Assemble + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + NET_BUF *Fragment; + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Assemble->Fragments) { + Fragment = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + + RemoveEntryList (Entry); + NetbufFree (Fragment); + } + + FreePool (Assemble); +} + + +/** + Initialize an already allocated assemble table. This is generally + the assemble table embedded in the IP4 service instance. + + @param[in, out] Table The assemble table to initialize. + +**/ +VOID +Ip4InitAssembleTable ( + IN OUT IP4_ASSEMBLE_TABLE *Table + ) +{ + UINT32 Index; + + for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) { + InitializeListHead (&Table->Bucket[Index]); + } +} + + +/** + Clean up the assemble table: remove all the fragments + and assemble entries. + + @param[in] Table The assemble table to clean up + +**/ +VOID +Ip4CleanAssembleTable ( + IN IP4_ASSEMBLE_TABLE *Table + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP4_ASSEMBLE_ENTRY *Assemble; + UINT32 Index; + + for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) { + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Table->Bucket[Index]) { + Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link); + + RemoveEntryList (Entry); + Ip4FreeAssembleEntry (Assemble); + } + } +} + + +/** + Trim the packet to fit in [Start, End), and update the per + packet information. + + @param Packet Packet to trim + @param Start The sequence of the first byte to fit in + @param End One beyond the sequence of last byte to fit in. + +**/ +VOID +Ip4TrimPacket ( + IN OUT NET_BUF *Packet, + IN INTN Start, + IN INTN End + ) +{ + IP4_CLIP_INFO *Info; + INTN Len; + + Info = IP4_GET_CLIP_INFO (Packet); + + ASSERT (Info->Start + Info->Length == Info->End); + ASSERT ((Info->Start < End) && (Start < Info->End)); + + if (Info->Start < Start) { + Len = Start - Info->Start; + + NetbufTrim (Packet, (UINT32) Len, NET_BUF_HEAD); + Info->Start = Start; + Info->Length -= Len; + } + + if (End < Info->End) { + Len = End - Info->End; + + NetbufTrim (Packet, (UINT32) Len, NET_BUF_TAIL); + Info->End = End; + Info->Length -= Len; + } +} + + +/** + Release all the fragments of the packet. This is the callback for + the assembled packet's OnFree. It will free the assemble entry, + which in turn will free all the fragments of the packet. + + @param[in] Arg The assemble entry to free + +**/ +VOID +EFIAPI +Ip4OnFreeFragments ( + IN VOID *Arg + ) +{ + Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY *) Arg); +} + + +/** + Reassemble the IP fragments. If all the fragments of the packet + have been received, it will wrap the packet in a net buffer then + return it to caller. If the packet can't be assembled, NULL is + return. + + @param Table The assemble table used. New assemble entry will be created + if the Packet is from a new chain of fragments. + @param Packet The fragment to assemble. It might be freed if the fragment + can't be re-assembled. + + @return NULL if the packet can't be reassemble. The point to just assembled + packet if all the fragments of the packet have arrived. + +**/ +NET_BUF * +Ip4Reassemble ( + IN OUT IP4_ASSEMBLE_TABLE *Table, + IN OUT NET_BUF *Packet + ) +{ + IP4_HEAD *IpHead; + IP4_CLIP_INFO *This; + IP4_CLIP_INFO *Node; + IP4_ASSEMBLE_ENTRY *Assemble; + LIST_ENTRY *Head; + LIST_ENTRY *Prev; + LIST_ENTRY *Cur; + NET_BUF *Fragment; + NET_BUF *NewPacket; + INTN Index; + + IpHead = Packet->Ip.Ip4; + This = IP4_GET_CLIP_INFO (Packet); + + ASSERT (IpHead != NULL); + + // + // First: find the related assemble entry + // + Assemble = NULL; + Index = IP4_ASSEMBLE_HASH (IpHead->Dst, IpHead->Src, IpHead->Id, IpHead->Protocol); + + NET_LIST_FOR_EACH (Cur, &Table->Bucket[Index]) { + Assemble = NET_LIST_USER_STRUCT (Cur, IP4_ASSEMBLE_ENTRY, Link); + + if ((Assemble->Dst == IpHead->Dst) && (Assemble->Src == IpHead->Src) && + (Assemble->Id == IpHead->Id) && (Assemble->Protocol == IpHead->Protocol)) { + break; + } + } + + // + // Create a new assemble entry if no assemble entry is related to this packet + // + if (Cur == &Table->Bucket[Index]) { + Assemble = Ip4CreateAssembleEntry ( + IpHead->Dst, + IpHead->Src, + IpHead->Id, + IpHead->Protocol + ); + + if (Assemble == NULL) { + goto DROP; + } + + InsertHeadList (&Table->Bucket[Index], &Assemble->Link); + } + // + // Assemble shouldn't be NULL here + // + ASSERT (Assemble != NULL); + + // + // Find the point to insert the packet: before the first + // fragment with THIS.Start < CUR.Start. the previous one + // has PREV.Start <= THIS.Start < CUR.Start. + // + Head = &Assemble->Fragments; + + NET_LIST_FOR_EACH (Cur, Head) { + Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List); + + if (This->Start < IP4_GET_CLIP_INFO (Fragment)->Start) { + break; + } + } + + // + // Check whether the current fragment overlaps with the previous one. + // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to + // check whether THIS.Start < PREV.End for overlap. If two fragments + // overlaps, trim the overlapped part off THIS fragment. + // + if ((Prev = Cur->BackLink) != Head) { + Fragment = NET_LIST_USER_STRUCT (Prev, NET_BUF, List); + Node = IP4_GET_CLIP_INFO (Fragment); + + if (This->Start < Node->End) { + if (This->End <= Node->End) { + NetbufFree (Packet); + return NULL; + } + + Ip4TrimPacket (Packet, Node->End, This->End); + } + } + + // + // Insert the fragment into the packet. The fragment may be removed + // from the list by the following checks. + // + NetListInsertBefore (Cur, &Packet->List); + + // + // Check the packets after the insert point. It holds that: + // THIS.Start <= NODE.Start < NODE.End. The equality holds + // if PREV and NEXT are continuous. THIS fragment may fill + // several holes. Remove the completely overlapped fragments + // + while (Cur != Head) { + Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List); + Node = IP4_GET_CLIP_INFO (Fragment); + + // + // Remove fragments completely overlapped by this fragment + // + if (Node->End <= This->End) { + Cur = Cur->ForwardLink; + + RemoveEntryList (&Fragment->List); + Assemble->CurLen -= Node->Length; + + NetbufFree (Fragment); + continue; + } + + // + // The conditions are: THIS.Start <= NODE.Start, and THIS.End < + // NODE.End. Two fragments overlaps if NODE.Start < THIS.End. + // If two fragments start at the same offset, remove THIS fragment + // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)). + // + if (Node->Start < This->End) { + if (This->Start == Node->Start) { + RemoveEntryList (&Packet->List); + goto DROP; + } + + Ip4TrimPacket (Packet, This->Start, Node->Start); + } + + break; + } + + // + // Update the assemble info: increase the current length. If it is + // the frist fragment, update the packet's IP head and per packet + // info. If it is the last fragment, update the total length. + // + Assemble->CurLen += This->Length; + + if (This->Start == 0) { + // + // Once the first fragment is enqueued, it can't be removed + // from the fragment list. So, Assemble->Head always point + // to valid memory area. + // + ASSERT (Assemble->Head == NULL); + + Assemble->Head = IpHead; + Assemble->Info = IP4_GET_CLIP_INFO (Packet); + } + + // + // Don't update the length more than once. + // + if (IP4_LAST_FRAGMENT (IpHead->Fragment) && (Assemble->TotalLen == 0)) { + Assemble->TotalLen = This->End; + } + + // + // Deliver the whole packet if all the fragments received. + // All fragments received if: + // 1. received the last one, so, the total length is know + // 2. received all the data. If the last fragment on the + // queue ends at the total length, all data is received. + // + if ((Assemble->TotalLen != 0) && (Assemble->CurLen >= Assemble->TotalLen)) { + + RemoveEntryList (&Assemble->Link); + + // + // If the packet is properly formated, the last fragment's End + // equals to the packet's total length. Otherwise, the packet + // is a fake, drop it now. + // + Fragment = NET_LIST_USER_STRUCT (Head->BackLink, NET_BUF, List); + + if (IP4_GET_CLIP_INFO (Fragment)->End != Assemble->TotalLen) { + Ip4FreeAssembleEntry (Assemble); + return NULL; + } + + // + // Wrap the packet in a net buffer then deliver it up + // + NewPacket = NetbufFromBufList ( + &Assemble->Fragments, + 0, + 0, + Ip4OnFreeFragments, + Assemble + ); + + if (NewPacket == NULL) { + Ip4FreeAssembleEntry (Assemble); + return NULL; + } + + NewPacket->Ip.Ip4 = Assemble->Head; + + ASSERT (Assemble->Info != NULL); + + CopyMem ( + IP4_GET_CLIP_INFO (NewPacket), + Assemble->Info, + sizeof (*IP4_GET_CLIP_INFO (NewPacket)) + ); + + return NewPacket; + } + + return NULL; + +DROP: + NetbufFree (Packet); + return NULL; +} + +/** + The callback function for the net buffer which wraps the packet processed by + IPsec. It releases the wrap packet and also signals IPsec to free the resources. + + @param[in] Arg The wrap context + +**/ +VOID +EFIAPI +Ip4IpSecFree ( + IN VOID *Arg + ) +{ + IP4_IPSEC_WRAP *Wrap; + + Wrap = (IP4_IPSEC_WRAP *) Arg; + + if (Wrap->IpSecRecycleSignal != NULL) { + gBS->SignalEvent (Wrap->IpSecRecycleSignal); + } + + NetbufFree (Wrap->Packet); + + FreePool (Wrap); + + return; +} + +/** + The work function to locate IPsec protocol to process the inbound or + outbound IP packets. The process routine handls the packet with following + actions: bypass the packet, discard the packet, or protect the packet. + + @param[in] IpSb The IP4 service instance. + @param[in, out] Head The The caller supplied IP4 header. + @param[in, out] Netbuf The IP4 packet to be processed by IPsec. + @param[in, out] Options The caller supplied options. + @param[in, out] OptionsLen The length of the option. + @param[in] Direction The directionality in an SPD entry, + EfiIPsecInBound or EfiIPsecOutBound. + @param[in] Context The token's wrap. + + @retval EFI_SUCCESS The IPsec protocol is not available or disabled. + @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same. + @retval EFI_SUCCESS The packet was protected. + @retval EFI_ACCESS_DENIED The packet was discarded. + @retval EFI_OUT_OF_RESOURCES There is no suffcient resource to complete the operation. + @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the + number of input data blocks when build a fragment table. + +**/ +EFI_STATUS +Ip4IpSecProcessPacket ( + IN IP4_SERVICE *IpSb, + IN OUT IP4_HEAD **Head, + IN OUT NET_BUF **Netbuf, + IN OUT UINT8 **Options, + IN OUT UINT32 *OptionsLen, + IN EFI_IPSEC_TRAFFIC_DIR Direction, + IN VOID *Context + ) +{ + NET_FRAGMENT *FragmentTable; + NET_FRAGMENT *OriginalFragmentTable; + UINT32 FragmentCount; + UINT32 OriginalFragmentCount; + EFI_EVENT RecycleEvent; + NET_BUF *Packet; + IP4_TXTOKEN_WRAP *TxWrap; + IP4_IPSEC_WRAP *IpSecWrap; + EFI_STATUS Status; + IP4_HEAD ZeroHead; + + Status = EFI_SUCCESS; + + if (!mIpSec2Installed) { + goto ON_EXIT; + } + ASSERT (mIpSec != NULL); + + Packet = *Netbuf; + RecycleEvent = NULL; + IpSecWrap = NULL; + FragmentTable = NULL; + TxWrap = (IP4_TXTOKEN_WRAP *) Context; + FragmentCount = Packet->BlockOpNum; + + ZeroMem (&ZeroHead, sizeof (IP4_HEAD)); + + // + // Check whether the IPsec enable variable is set. + // + if (mIpSec->DisabledFlag) { + // + // If IPsec is disabled, restore the original MTU + // + IpSb->MaxPacketSize = IpSb->OldMaxPacketSize; + goto ON_EXIT; + } else { + // + // If IPsec is enabled, use the MTU which reduce the IPsec header length. + // + IpSb->MaxPacketSize = IpSb->OldMaxPacketSize - IP4_MAX_IPSEC_HEADLEN; + } + + // + // Rebuild fragment table from netbuf to ease IPsec process. + // + FragmentTable = AllocateZeroPool (FragmentCount * sizeof (NET_FRAGMENT)); + + if (FragmentTable == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Status = NetbufBuildExt (Packet, FragmentTable, &FragmentCount); + + // + // Record the original FragmentTable and count. + // + OriginalFragmentTable = FragmentTable; + OriginalFragmentCount = FragmentCount; + + if (EFI_ERROR (Status)) { + FreePool (FragmentTable); + goto ON_EXIT; + } + + // + // Convert host byte order to network byte order + // + Ip4NtohHead (*Head); + + Status = mIpSec->ProcessExt ( + mIpSec, + IpSb->Controller, + IP_VERSION_4, + (VOID *) (*Head), + &(*Head)->Protocol, + (VOID **) Options, + OptionsLen, + (EFI_IPSEC_FRAGMENT_DATA **) (&FragmentTable), + &FragmentCount, + Direction, + &RecycleEvent + ); + // + // Convert back to host byte order + // + Ip4NtohHead (*Head); + + if (EFI_ERROR (Status)) { + FreePool (OriginalFragmentTable); + goto ON_EXIT; + } + + if (OriginalFragmentTable == FragmentTable && OriginalFragmentCount == FragmentCount) { + // + // For ByPass Packet + // + FreePool (FragmentTable); + goto ON_EXIT; + } else { + // + // Free the FragmentTable which allocated before calling the IPsec. + // + FreePool (OriginalFragmentTable); + } + + if (Direction == EfiIPsecOutBound && TxWrap != NULL) { + + TxWrap->IpSecRecycleSignal = RecycleEvent; + TxWrap->Packet = NetbufFromExt ( + FragmentTable, + FragmentCount, + IP4_MAX_HEADLEN, + 0, + Ip4FreeTxToken, + TxWrap + ); + if (TxWrap->Packet == NULL) { + // + // Recover the TxWrap->Packet, if meet a error, and the caller will free + // the TxWrap. + // + TxWrap->Packet = *Netbuf; + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Free orginal Netbuf. + // + NetIpSecNetbufFree (*Netbuf); + *Netbuf = TxWrap->Packet; + + } else { + + IpSecWrap = AllocateZeroPool (sizeof (IP4_IPSEC_WRAP)); + + if (IpSecWrap == NULL) { + Status = EFI_OUT_OF_RESOURCES; + gBS->SignalEvent (RecycleEvent); + goto ON_EXIT; + } + + IpSecWrap->IpSecRecycleSignal = RecycleEvent; + IpSecWrap->Packet = Packet; + Packet = NetbufFromExt ( + FragmentTable, + FragmentCount, + IP4_MAX_HEADLEN, + 0, + Ip4IpSecFree, + IpSecWrap + ); + + if (Packet == NULL) { + Packet = IpSecWrap->Packet; + gBS->SignalEvent (RecycleEvent); + FreePool (IpSecWrap); + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + if (Direction == EfiIPsecInBound && 0 != CompareMem (*Head, &ZeroHead, sizeof (IP4_HEAD))) { + Ip4PrependHead (Packet, *Head, *Options, *OptionsLen); + Ip4NtohHead (Packet->Ip.Ip4); + NetbufTrim (Packet, ((*Head)->HeadLen << 2), TRUE); + + CopyMem ( + IP4_GET_CLIP_INFO (Packet), + IP4_GET_CLIP_INFO (IpSecWrap->Packet), + sizeof (IP4_CLIP_INFO) + ); + } + *Netbuf = Packet; + } + +ON_EXIT: + return Status; +} + +/** + Pre-process the IPv4 packet. First validates the IPv4 packet, and + then reassembles packet if it is necessary. + + @param[in] IpSb Pointer to IP4_SERVICE. + @param[in, out] Packet Pointer to the Packet to be processed. + @param[in] Head Pointer to the IP4_HEAD. + @param[in] Option Pointer to a buffer which contains the IPv4 option. + @param[in] OptionLen The length of Option in bytes. + @param[in] Flag The link layer flag for the packet received, such + as multicast. + + @retval EFI_SEUCCESS The recieved packet is in well form. + @retval EFI_INVAILD_PARAMETER The recieved packet is malformed. + +**/ +EFI_STATUS +Ip4PreProcessPacket ( + IN IP4_SERVICE *IpSb, + IN OUT NET_BUF **Packet, + IN IP4_HEAD *Head, + IN UINT8 *Option, + IN UINT32 OptionLen, + IN UINT32 Flag + ) +{ + IP4_CLIP_INFO *Info; + UINT32 HeadLen; + UINT32 TotalLen; + UINT16 Checksum; + + // + // Check if the IP4 header is correctly formatted. + // + if ((*Packet)->TotalSize < IP4_MIN_HEADLEN) { + return EFI_INVALID_PARAMETER; + } + + HeadLen = (Head->HeadLen << 2); + TotalLen = NTOHS (Head->TotalLen); + + // + // Mnp may deliver frame trailer sequence up, trim it off. + // + if (TotalLen < (*Packet)->TotalSize) { + NetbufTrim (*Packet, (*Packet)->TotalSize - TotalLen, FALSE); + } + + if ((Head->Ver != 4) || (HeadLen < IP4_MIN_HEADLEN) || + (TotalLen < HeadLen) || (TotalLen != (*Packet)->TotalSize)) { + return EFI_INVALID_PARAMETER; + } + + // + // Some OS may send IP packets without checksum. + // + Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Head, HeadLen)); + + if ((Head->Checksum != 0) && (Checksum != 0)) { + return EFI_INVALID_PARAMETER; + } + + // + // Convert the IP header to host byte order, then get the per packet info. + // + (*Packet)->Ip.Ip4 = Ip4NtohHead (Head); + + Info = IP4_GET_CLIP_INFO (*Packet); + Info->LinkFlag = Flag; + Info->CastType = Ip4GetHostCast (IpSb, Head->Dst, Head->Src); + Info->Start = (Head->Fragment & IP4_HEAD_OFFSET_MASK) << 3; + Info->Length = Head->TotalLen - HeadLen; + Info->End = Info->Start + Info->Length; + Info->Status = EFI_SUCCESS; + + // + // The packet is destinated to us if the CastType is non-zero. + // + if ((Info->CastType == 0) || (Info->End > IP4_MAX_PACKET_SIZE)) { + return EFI_INVALID_PARAMETER; + } + + // + // Validate the options. Don't call the Ip4OptionIsValid if + // there is no option to save some CPU process. + // + + if ((OptionLen > 0) && !Ip4OptionIsValid (Option, OptionLen, TRUE)) { + return EFI_INVALID_PARAMETER; + } + + // + // Trim the head off, after this point, the packet is headless, + // and Packet->TotalLen == Info->Length. + // + NetbufTrim (*Packet, HeadLen, TRUE); + + // + // Reassemble the packet if this is a fragment. The packet is a + // fragment if its head has MF (more fragment) set, or it starts + // at non-zero byte. + // + if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) || (Info->Start != 0)) { + // + // Drop the fragment if DF is set but it is fragmented. Gateway + // need to send a type 4 destination unreache ICMP message here. + // + if ((Head->Fragment & IP4_HEAD_DF_MASK) != 0) { + return EFI_INVALID_PARAMETER; + } + + // + // The length of all but the last fragments is in the unit of 8 bytes. + // + if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) && (Info->Length % 8 != 0)) { + return EFI_INVALID_PARAMETER; + } + + *Packet = Ip4Reassemble (&IpSb->Assemble, *Packet); + + // + // Packet assembly isn't complete, start receive more packet. + // + if (*Packet == NULL) { + return EFI_INVALID_PARAMETER; + } + } + + return EFI_SUCCESS; +} + +/** + The IP4 input routine. It is called by the IP4_INTERFACE when a + IP4 fragment is received from MNP. + + @param[in] Ip4Instance The IP4 child that request the receive, most like + it is NULL. + @param[in] Packet The IP4 packet received. + @param[in] IoStatus The return status of receive request. + @param[in] Flag The link layer flag for the packet received, such + as multicast. + @param[in] Context The IP4 service instance that own the MNP. + +**/ +VOID +Ip4AccpetFrame ( + IN IP4_PROTOCOL *Ip4Instance, + IN NET_BUF *Packet, + IN EFI_STATUS IoStatus, + IN UINT32 Flag, + IN VOID *Context + ) +{ + IP4_SERVICE *IpSb; + IP4_HEAD *Head; + EFI_STATUS Status; + IP4_HEAD ZeroHead; + UINT8 *Option; + UINT32 OptionLen; + + IpSb = (IP4_SERVICE *) Context; + Option = NULL; + + if (EFI_ERROR (IoStatus) || (IpSb->State == IP4_SERVICE_DESTROY)) { + goto DROP; + } + + Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL); + ASSERT (Head != NULL); + OptionLen = (Head->HeadLen << 2) - IP4_MIN_HEADLEN; + if (OptionLen > 0) { + Option = (UINT8 *) (Head + 1); + } + + // + // Validate packet format and reassemble packet if it is necessary. + // + Status = Ip4PreProcessPacket ( + IpSb, + &Packet, + Head, + Option, + OptionLen, + Flag + ); + + if (EFI_ERROR (Status)) { + goto RESTART; + } + + // + // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer, + // and no need consider any other ahead ext headers. + // + Status = Ip4IpSecProcessPacket ( + IpSb, + &Head, + &Packet, + &Option, + &OptionLen, + EfiIPsecInBound, + NULL + ); + + if (EFI_ERROR (Status)) { + goto RESTART; + } + + // + // If the packet is protected by tunnel mode, parse the inner Ip Packet. + // + ZeroMem (&ZeroHead, sizeof (IP4_HEAD)); + if (0 == CompareMem (Head, &ZeroHead, sizeof (IP4_HEAD))) { + // Packet may have been changed. Head, HeadLen, TotalLen, and + // info must be reloaded bofore use. The ownership of the packet + // is transfered to the packet process logic. + // + Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL); + ASSERT (Head != NULL); + Status = Ip4PreProcessPacket ( + IpSb, + &Packet, + Head, + Option, + OptionLen, + Flag + ); + if (EFI_ERROR (Status)) { + goto RESTART; + } + } + + ASSERT (Packet != NULL); + Head = Packet->Ip.Ip4; + IP4_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS; + + switch (Head->Protocol) { + case EFI_IP_PROTO_ICMP: + Ip4IcmpHandle (IpSb, Head, Packet); + break; + + case IP4_PROTO_IGMP: + Ip4IgmpHandle (IpSb, Head, Packet); + break; + + default: + Ip4Demultiplex (IpSb, Head, Packet, Option, OptionLen); + } + + Packet = NULL; + + // + // Dispatch the DPCs queued by the NotifyFunction of the rx token's events + // which are signaled with received data. + // + DispatchDpc (); + +RESTART: + Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb); + +DROP: + if (Packet != NULL) { + NetbufFree (Packet); + } + + return ; +} + + +/** + Check whether this IP child accepts the packet. + + @param[in] IpInstance The IP child to check + @param[in] Head The IP header of the packet + @param[in] Packet The data of the packet + + @retval TRUE If the child wants to receive the packet. + @retval FALSE Otherwise. + +**/ +BOOLEAN +Ip4InstanceFrameAcceptable ( + IN IP4_PROTOCOL *IpInstance, + IN IP4_HEAD *Head, + IN NET_BUF *Packet + ) +{ + IP4_ICMP_ERROR_HEAD Icmp; + EFI_IP4_CONFIG_DATA *Config; + IP4_CLIP_INFO *Info; + UINT16 Proto; + UINT32 Index; + + Config = &IpInstance->ConfigData; + + // + // Dirty trick for the Tiano UEFI network stack implmentation. If + // ReceiveTimeout == -1, the receive of the packet for this instance + // is disabled. The UEFI spec don't have such capability. We add + // this to improve the performance because IP will make a copy of + // the received packet for each accepting instance. Some IP instances + // used by UDP/TCP only send packets, they don't wants to receive. + // + if (Config->ReceiveTimeout == (UINT32)(-1)) { + return FALSE; + } + + if (Config->AcceptPromiscuous) { + return TRUE; + } + + // + // Use protocol from the IP header embedded in the ICMP error + // message to filter, instead of ICMP itself. ICMP handle will + // call Ip4Demultiplex to deliver ICMP errors. + // + Proto = Head->Protocol; + + if ((Proto == EFI_IP_PROTO_ICMP) && (!Config->AcceptAnyProtocol) && (Proto != Config->DefaultProtocol)) { + NetbufCopy (Packet, 0, sizeof (Icmp.Head), (UINT8 *) &Icmp.Head); + + if (mIcmpClass[Icmp.Head.Type].IcmpClass == ICMP_ERROR_MESSAGE) { + if (!Config->AcceptIcmpErrors) { + return FALSE; + } + + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + Proto = Icmp.IpHead.Protocol; + } + } + + // + // Match the protocol + // + if (!Config->AcceptAnyProtocol && (Proto != Config->DefaultProtocol)) { + return FALSE; + } + + // + // Check for broadcast, the caller has computed the packet's + // cast type for this child's interface. + // + Info = IP4_GET_CLIP_INFO (Packet); + + if (IP4_IS_BROADCAST (Info->CastType)) { + return Config->AcceptBroadcast; + } + + // + // If it is a multicast packet, check whether we are in the group. + // + if (Info->CastType == IP4_MULTICAST) { + // + // Receive the multicast if the instance wants to receive all packets. + // + if (!IpInstance->ConfigData.UseDefaultAddress && (IpInstance->Interface->Ip == 0)) { + return TRUE; + } + + for (Index = 0; Index < IpInstance->GroupCount; Index++) { + if (IpInstance->Groups[Index] == HTONL (Head->Dst)) { + break; + } + } + + return (BOOLEAN)(Index < IpInstance->GroupCount); + } + + return TRUE; +} + + +/** + Enqueue a shared copy of the packet to the IP4 child if the + packet is acceptable to it. Here the data of the packet is + shared, but the net buffer isn't. + + @param[in] IpInstance The IP4 child to enqueue the packet to + @param[in] Head The IP header of the received packet + @param[in] Packet The data of the received packet + + @retval EFI_NOT_STARTED The IP child hasn't been configured. + @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet + @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource + @retval EFI_SUCCESS A shared copy the packet is enqueued to the child. + +**/ +EFI_STATUS +Ip4InstanceEnquePacket ( + IN IP4_PROTOCOL *IpInstance, + IN IP4_HEAD *Head, + IN NET_BUF *Packet + ) +{ + IP4_CLIP_INFO *Info; + NET_BUF *Clone; + + // + // Check whether the packet is acceptable to this instance. + // + if (IpInstance->State != IP4_STATE_CONFIGED) { + return EFI_NOT_STARTED; + } + + if (!Ip4InstanceFrameAcceptable (IpInstance, Head, Packet)) { + return EFI_INVALID_PARAMETER; + } + + // + // Enque a shared copy of the packet. + // + Clone = NetbufClone (Packet); + + if (Clone == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Set the receive time out for the assembled packet. If it expires, + // packet will be removed from the queue. + // + Info = IP4_GET_CLIP_INFO (Clone); + Info->Life = IP4_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout); + + InsertTailList (&IpInstance->Received, &Clone->List); + return EFI_SUCCESS; +} + + +/** + The signal handle of IP4's recycle event. It is called back + when the upper layer release the packet. + + @param Event The IP4's recycle event. + @param Context The context of the handle, which is a + IP4_RXDATA_WRAP + +**/ +VOID +EFIAPI +Ip4OnRecyclePacket ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + IP4_RXDATA_WRAP *Wrap; + + Wrap = (IP4_RXDATA_WRAP *) Context; + + EfiAcquireLockOrFail (&Wrap->IpInstance->RecycleLock); + RemoveEntryList (&Wrap->Link); + EfiReleaseLock (&Wrap->IpInstance->RecycleLock); + + ASSERT (!NET_BUF_SHARED (Wrap->Packet)); + NetbufFree (Wrap->Packet); + + gBS->CloseEvent (Wrap->RxData.RecycleSignal); + FreePool (Wrap); +} + + +/** + Wrap the received packet to a IP4_RXDATA_WRAP, which will be + delivered to the upper layer. Each IP4 child that accepts the + packet will get a not-shared copy of the packet which is wrapped + in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed + to the upper layer. Upper layer will signal the recycle event in + it when it is done with the packet. + + @param[in] IpInstance The IP4 child to receive the packet. + @param[in] Packet The packet to deliver up. + + @retval Wrap if warp the packet succeed. + @retval NULL failed to wrap the packet . + +**/ +IP4_RXDATA_WRAP * +Ip4WrapRxData ( + IN IP4_PROTOCOL *IpInstance, + IN NET_BUF *Packet + ) +{ + IP4_RXDATA_WRAP *Wrap; + EFI_IP4_RECEIVE_DATA *RxData; + EFI_STATUS Status; + BOOLEAN RawData; + + Wrap = AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet->BlockOpNum)); + + if (Wrap == NULL) { + return NULL; + } + + InitializeListHead (&Wrap->Link); + + Wrap->IpInstance = IpInstance; + Wrap->Packet = Packet; + RxData = &Wrap->RxData; + + ZeroMem (RxData, sizeof (EFI_IP4_RECEIVE_DATA)); + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Ip4OnRecyclePacket, + Wrap, + &RxData->RecycleSignal + ); + + if (EFI_ERROR (Status)) { + FreePool (Wrap); + return NULL; + } + + ASSERT (Packet->Ip.Ip4 != NULL); + + ASSERT (IpInstance != NULL); + RawData = IpInstance->ConfigData.RawData; + + // + // The application expects a network byte order header. + // + if (!RawData) { + RxData->HeaderLength = (Packet->Ip.Ip4->HeadLen << 2); + RxData->Header = (EFI_IP4_HEADER *) Ip4NtohHead (Packet->Ip.Ip4); + RxData->OptionsLength = RxData->HeaderLength - IP4_MIN_HEADLEN; + RxData->Options = NULL; + + if (RxData->OptionsLength != 0) { + RxData->Options = (VOID *) (RxData->Header + 1); + } + } + + RxData->DataLength = Packet->TotalSize; + + // + // Build the fragment table to be delivered up. + // + RxData->FragmentCount = Packet->BlockOpNum; + NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount); + + return Wrap; +} + + +/** + Deliver the received packets to upper layer if there are both received + requests and enqueued packets. If the enqueued packet is shared, it will + duplicate it to a non-shared packet, release the shared packet, then + deliver the non-shared packet up. + + @param[in] IpInstance The IP child to deliver the packet up. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the + packets. + @retval EFI_SUCCESS All the enqueued packets that can be delivered + are delivered up. + +**/ +EFI_STATUS +Ip4InstanceDeliverPacket ( + IN IP4_PROTOCOL *IpInstance + ) +{ + EFI_IP4_COMPLETION_TOKEN *Token; + IP4_RXDATA_WRAP *Wrap; + NET_BUF *Packet; + NET_BUF *Dup; + UINT8 *Head; + UINT32 HeadLen; + + // + // Deliver a packet if there are both a packet and a receive token. + // + while (!IsListEmpty (&IpInstance->Received) && + !NetMapIsEmpty (&IpInstance->RxTokens)) { + + Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List); + + if (!NET_BUF_SHARED (Packet)) { + // + // If this is the only instance that wants the packet, wrap it up. + // + Wrap = Ip4WrapRxData (IpInstance, Packet); + + if (Wrap == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + RemoveEntryList (&Packet->List); + + } else { + // + // Create a duplicated packet if this packet is shared + // + if (IpInstance->ConfigData.RawData) { + HeadLen = 0; + } else { + HeadLen = IP4_MAX_HEADLEN; + } + + Dup = NetbufDuplicate (Packet, NULL, HeadLen); + + if (Dup == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if (!IpInstance->ConfigData.RawData) { + // + // Copy the IP head over. The packet to deliver up is + // headless. Trim the head off after copy. The IP head + // may be not continuous before the data. + // + Head = NetbufAllocSpace (Dup, IP4_MAX_HEADLEN, NET_BUF_HEAD); + ASSERT (Head != NULL); + + Dup->Ip.Ip4 = (IP4_HEAD *) Head; + + CopyMem (Head, Packet->Ip.Ip4, Packet->Ip.Ip4->HeadLen << 2); + NetbufTrim (Dup, IP4_MAX_HEADLEN, TRUE); + } + + Wrap = Ip4WrapRxData (IpInstance, Dup); + + if (Wrap == NULL) { + NetbufFree (Dup); + return EFI_OUT_OF_RESOURCES; + } + + RemoveEntryList (&Packet->List); + NetbufFree (Packet); + + Packet = Dup; + } + + // + // Insert it into the delivered packet, then get a user's + // receive token, pass the wrapped packet up. + // + EfiAcquireLockOrFail (&IpInstance->RecycleLock); + InsertHeadList (&IpInstance->Delivered, &Wrap->Link); + EfiReleaseLock (&IpInstance->RecycleLock); + + Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL); + Token->Status = IP4_GET_CLIP_INFO (Packet)->Status; + Token->Packet.RxData = &Wrap->RxData; + + gBS->SignalEvent (Token->Event); + } + + return EFI_SUCCESS; +} + + +/** + Enqueue a received packet to all the IP children that share + the same interface. + + @param[in] IpSb The IP4 service instance that receive the packet. + @param[in] Head The header of the received packet. + @param[in] Packet The data of the received packet. + @param[in] Option Point to the IP4 packet header options. + @param[in] OptionLen Length of the IP4 packet header options. + @param[in] IpIf The interface to enqueue the packet to. + + @return The number of the IP4 children that accepts the packet + +**/ +INTN +Ip4InterfaceEnquePacket ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet, + IN UINT8 *Option, + IN UINT32 OptionLen, + IN IP4_INTERFACE *IpIf + ) +{ + IP4_PROTOCOL *IpInstance; + IP4_CLIP_INFO *Info; + LIST_ENTRY *Entry; + INTN Enqueued; + INTN LocalType; + INTN SavedType; + + // + // First, check that the packet is acceptable to this interface + // and find the local cast type for the interface. A packet sent + // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless + // promiscuous receiving. + // + LocalType = 0; + Info = IP4_GET_CLIP_INFO (Packet); + + if ((Info->CastType == IP4_MULTICAST) || (Info->CastType == IP4_LOCAL_BROADCAST)) { + // + // If the CastType is multicast, don't need to filter against + // the group address here, Ip4InstanceFrameAcceptable will do + // that later. + // + LocalType = Info->CastType; + + } else { + // + // Check the destination againist local IP. If the station + // address is 0.0.0.0, it means receiving all the IP destined + // to local non-zero IP. Otherwise, it is necessary to compare + // the destination to the interface's IP address. + // + if (IpIf->Ip == IP4_ALLZERO_ADDRESS) { + LocalType = IP4_LOCAL_HOST; + + } else { + LocalType = Ip4GetNetCast (Head->Dst, IpIf); + + if ((LocalType == 0) && IpIf->PromiscRecv) { + LocalType = IP4_PROMISCUOUS; + } + } + } + + if (LocalType == 0) { + return 0; + } + + // + // Iterate through the ip instances on the interface, enqueue + // the packet if filter passed. Save the original cast type, + // and pass the local cast type to the IP children on the + // interface. The global cast type will be restored later. + // + SavedType = Info->CastType; + Info->CastType = LocalType; + + Enqueued = 0; + + NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) { + IpInstance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink); + NET_CHECK_SIGNATURE (IpInstance, IP4_PROTOCOL_SIGNATURE); + + // + // In RawData mode, add IPv4 headers and options back to packet. + // + if ((IpInstance->ConfigData.RawData) && (Option != NULL) && (OptionLen != 0)){ + Ip4PrependHead (Packet, Head, Option, OptionLen); + } + + if (Ip4InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) { + Enqueued++; + } + } + + Info->CastType = SavedType; + return Enqueued; +} + + +/** + Deliver the packet for each IP4 child on the interface. + + @param[in] IpSb The IP4 service instance that received the packet + @param[in] IpIf The IP4 interface to deliver the packet. + + @retval EFI_SUCCESS It always returns EFI_SUCCESS now + +**/ +EFI_STATUS +Ip4InterfaceDeliverPacket ( + IN IP4_SERVICE *IpSb, + IN IP4_INTERFACE *IpIf + ) +{ + IP4_PROTOCOL *Ip4Instance; + LIST_ENTRY *Entry; + + NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) { + Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink); + Ip4InstanceDeliverPacket (Ip4Instance); + } + + return EFI_SUCCESS; +} + + +/** + Demultiple the packet. the packet delivery is processed in two + passes. The first pass will enque a shared copy of the packet + to each IP4 child that accepts the packet. The second pass will + deliver a non-shared copy of the packet to each IP4 child that + has pending receive requests. Data is copied if more than one + child wants to consume the packet because each IP child needs + its own copy of the packet to make changes. + + @param[in] IpSb The IP4 service instance that received the packet. + @param[in] Head The header of the received packet. + @param[in] Packet The data of the received packet. + @param[in] Option Point to the IP4 packet header options. + @param[in] OptionLen Length of the IP4 packet header options. + + @retval EFI_NOT_FOUND No IP child accepts the packet. + @retval EFI_SUCCESS The packet is enqueued or delivered to some IP + children. + +**/ +EFI_STATUS +Ip4Demultiplex ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet, + IN UINT8 *Option, + IN UINT32 OptionLen + ) +{ + LIST_ENTRY *Entry; + IP4_INTERFACE *IpIf; + INTN Enqueued; + + // + // Two pass delivery: first, enque a shared copy of the packet + // to each instance that accept the packet. + // + Enqueued = 0; + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link); + + if (IpIf->Configured) { + Enqueued += Ip4InterfaceEnquePacket ( + IpSb, + Head, + Packet, + Option, + OptionLen, + IpIf + ); + } + } + + // + // Second: deliver a duplicate of the packet to each instance. + // Release the local reference first, so that the last instance + // getting the packet will not copy the data. + // + NetbufFree (Packet); + + if (Enqueued == 0) { + return EFI_NOT_FOUND; + } + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link); + + if (IpIf->Configured) { + Ip4InterfaceDeliverPacket (IpSb, IpIf); + } + } + + return EFI_SUCCESS; +} + + +/** + Timeout the fragment and enqueued packets. + + @param[in] IpSb The IP4 service instance to timeout + +**/ +VOID +Ip4PacketTimerTicking ( + IN IP4_SERVICE *IpSb + ) +{ + LIST_ENTRY *InstanceEntry; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP4_PROTOCOL *IpInstance; + IP4_ASSEMBLE_ENTRY *Assemble; + NET_BUF *Packet; + IP4_CLIP_INFO *Info; + UINT32 Index; + + // + // First, time out the fragments. The packet's life is counting down + // once the first-arrived fragment was received. + // + for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) { + NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->Assemble.Bucket[Index]) { + Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link); + + if ((Assemble->Life > 0) && (--Assemble->Life == 0)) { + RemoveEntryList (Entry); + Ip4FreeAssembleEntry (Assemble); + } + } + } + + NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) { + IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP4_PROTOCOL, Link); + + // + // Second, time out the assembled packets enqueued on each IP child. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) { + Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + Info = IP4_GET_CLIP_INFO (Packet); + + if ((Info->Life > 0) && (--Info->Life == 0)) { + RemoveEntryList (Entry); + NetbufFree (Packet); + } + } + + // + // Third: time out the transmitted packets. + // + NetMapIterate (&IpInstance->TxTokens, Ip4SentPacketTicking, NULL); + } +} diff --git a/NetworkPkg/Ip4Dxe/Ip4Input.h b/NetworkPkg/Ip4Dxe/Ip4Input.h new file mode 100644 index 000000000..f4d45d161 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Input.h @@ -0,0 +1,246 @@ +/** @file + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP4_INPUT_H__ +#define __EFI_IP4_INPUT_H__ + +#define IP4_MIN_HEADLEN 20 +#define IP4_MAX_HEADLEN 60 +/// +/// 8(ESP header) + 16(max IV) + 16(max padding) + 2(ESP tail) + 12(max ICV) = 54 +/// +#define IP4_MAX_IPSEC_HEADLEN 54 + +#define IP4_ASSEMLE_HASH_SIZE 31 +#define IP4_FRAGMENT_LIFE 120 +#define IP4_MAX_PACKET_SIZE 65535 + +/// +/// Per packet information for input process. LinkFlag specifies whether +/// the packet is received as Link layer unicast, multicast or broadcast. +/// The CastType is the IP layer cast type, such as IP multicast or unicast. +/// Start, End and Length are staffs used to assemble the packets. Start +/// is the sequence number of the first byte of data in the packet. Length +/// is the number of bytes of data. End = Start + Length, that is, the +/// sequence number of last byte + 1. Each assembled packet has a count down +/// life. If it isn't consumed before Life reaches zero, the packet is released. +/// +typedef struct { + UINTN LinkFlag; + INTN CastType; + INTN Start; + INTN End; + INTN Length; + UINT32 Life; + EFI_STATUS Status; +} IP4_CLIP_INFO; + +/// +/// Structure used to assemble IP packets. +/// +typedef struct { + LIST_ENTRY Link; + + // + // Identity of one IP4 packet. Each fragment of a packet has + // the same (Dst, Src, Id, Protocol). + // + IP4_ADDR Dst; + IP4_ADDR Src; + UINT16 Id; + UINT8 Protocol; + + INTN TotalLen; + INTN CurLen; + LIST_ENTRY Fragments; // List of all the fragments of this packet + + IP4_HEAD *Head; // IP head of the first fragment + IP4_CLIP_INFO *Info; // Per packet info of the first fragment + INTN Life; // Count down life for the packet. +} IP4_ASSEMBLE_ENTRY; + +/// +/// Each Ip service instance has an assemble table to reassemble +/// the packets before delivery to its children. It is organized +/// as hash table. +/// +typedef struct { + LIST_ENTRY Bucket[IP4_ASSEMLE_HASH_SIZE]; +} IP4_ASSEMBLE_TABLE; + +#define IP4_GET_CLIP_INFO(Packet) ((IP4_CLIP_INFO *) ((Packet)->ProtoData)) + +#define IP4_ASSEMBLE_HASH(Dst, Src, Id, Proto) \ + (((Dst) + (Src) + ((Id) << 16) + (Proto)) % IP4_ASSEMLE_HASH_SIZE) + +#define IP4_RXDATA_WRAP_SIZE(NumFrag) \ + (sizeof (IP4_RXDATA_WRAP) + sizeof (EFI_IP4_FRAGMENT_DATA) * ((NumFrag) - 1)) + +/** + Initialize an already allocated assemble table. This is generally + the assemble table embedded in the IP4 service instance. + + @param[in, out] Table The assemble table to initialize. + +**/ +VOID +Ip4InitAssembleTable ( + IN OUT IP4_ASSEMBLE_TABLE *Table + ); + +/** + Clean up the assemble table: remove all the fragments + and assemble entries. + + @param[in] Table The assemble table to clean up + +**/ +VOID +Ip4CleanAssembleTable ( + IN IP4_ASSEMBLE_TABLE *Table + ); + +/** + The IP4 input routine. It is called by the IP4_INTERFACE when a + IP4 fragment is received from MNP. + + @param[in] Ip4Instance The IP4 child that request the receive, most like + it is NULL. + @param[in] Packet The IP4 packet received. + @param[in] IoStatus The return status of receive request. + @param[in] Flag The link layer flag for the packet received, such + as multicast. + @param[in] Context The IP4 service instance that own the MNP. + +**/ +VOID +Ip4AccpetFrame ( + IN IP4_PROTOCOL *Ip4Instance, + IN NET_BUF *Packet, + IN EFI_STATUS IoStatus, + IN UINT32 Flag, + IN VOID *Context + ); + +/** + Demultiple the packet. the packet delivery is processed in two + passes. The first pass will enque a shared copy of the packet + to each IP4 child that accepts the packet. The second pass will + deliver a non-shared copy of the packet to each IP4 child that + has pending receive requests. Data is copied if more than one + child wants to consume the packet because each IP child needs + its own copy of the packet to make changes. + + @param[in] IpSb The IP4 service instance that received the packet. + @param[in] Head The header of the received packet. + @param[in] Packet The data of the received packet. + @param[in] Option Point to the IP4 packet header options. + @param[in] OptionLen Length of the IP4 packet header options. + + @retval EFI_NOT_FOUND No IP child accepts the packet. + @retval EFI_SUCCESS The packet is enqueued or delivered to some IP + children. + +**/ +EFI_STATUS +Ip4Demultiplex ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet, + IN UINT8 *Option, + IN UINT32 OptionLen + ); + +/** + Enqueue a received packet to all the IP children that share + the same interface. + + @param[in] IpSb The IP4 service instance that receive the packet. + @param[in] Head The header of the received packet. + @param[in] Packet The data of the received packet. + @param[in] Option Point to the IP4 packet header options. + @param[in] OptionLen Length of the IP4 packet header options. + @param[in] IpIf The interface to enqueue the packet to. + + @return The number of the IP4 children that accepts the packet + +**/ +INTN +Ip4InterfaceEnquePacket ( + IN IP4_SERVICE *IpSb, + IN IP4_HEAD *Head, + IN NET_BUF *Packet, + IN UINT8 *Option, + IN UINT32 OptionLen, + IN IP4_INTERFACE *IpIf + ); + +/** + Deliver the received packets to upper layer if there are both received + requests and enqueued packets. If the enqueued packet is shared, it will + duplicate it to a non-shared packet, release the shared packet, then + deliver the non-shared packet up. + + @param[in] IpInstance The IP child to deliver the packet up. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the + packets. + @retval EFI_SUCCESS All the enqueued packets that can be delivered + are delivered up. + +**/ +EFI_STATUS +Ip4InstanceDeliverPacket ( + IN IP4_PROTOCOL *IpInstance + ); + +/** + Timeout the fragment and enqueued packets. + + @param[in] IpSb The IP4 service instance to timeout + +**/ +VOID +Ip4PacketTimerTicking ( + IN IP4_SERVICE *IpSb + ); + +/** + The work function to locate IPsec protocol to process the inbound or + outbound IP packets. The process routine handls the packet with following + actions: bypass the packet, discard the packet, or protect the packet. + + @param[in] IpSb The IP4 service instance. + @param[in, out] Head The The caller supplied IP4 header. + @param[in, out] Netbuf The IP4 packet to be processed by IPsec. + @param[in, out] Options The caller supplied options. + @param[in, out] OptionsLen The length of the option. + @param[in] Direction The directionality in an SPD entry, + EfiIPsecInBound or EfiIPsecOutBound. + @param[in] Context The token's wrap. + + @retval EFI_SUCCESS The IPsec protocol is not available or disabled. + @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same. + @retval EFI_SUCCESS The packet was protected. + @retval EFI_ACCESS_DENIED The packet was discarded. + @retval EFI_OUT_OF_RESOURCES There is no suffcient resource to complete the operation. + @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the + number of input data blocks when build a fragment table. + +**/ +EFI_STATUS +Ip4IpSecProcessPacket ( + IN IP4_SERVICE *IpSb, + IN OUT IP4_HEAD **Head, + IN OUT NET_BUF **Netbuf, + IN OUT UINT8 **Options, + IN OUT UINT32 *OptionsLen, + IN EFI_IPSEC_TRAFFIC_DIR Direction, + IN VOID *Context + ); + +#endif diff --git a/NetworkPkg/Ip4Dxe/Ip4NvData.h b/NetworkPkg/Ip4Dxe/Ip4NvData.h new file mode 100644 index 000000000..d161c1c8c --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4NvData.h @@ -0,0 +1,45 @@ +/** @file + Routines used to operate the Ip4Dxe. + +Copyright (c) 2015, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _IP4_NV_DATA_H_ +#define _IP4_NV_DATA_H_ + +#include + +#define FORMID_MAIN_FORM 1 +#define FORMID_DEVICE_FORM 2 + +#define KEY_ENABLE 0x100 +#define KEY_DHCP_ENABLE 0x101 +#define KEY_LOCAL_IP 0x102 +#define KEY_SUBNET_MASK 0x103 +#define KEY_GATE_WAY 0x104 +#define KEY_DNS 0x105 +#define KEY_SAVE_CHANGES 0x106 + +#define IP_MIN_SIZE 7 +#define IP_MAX_SIZE 15 +#define IP4_STR_MAX_SIZE 16 +#define ADDRESS_STR_MAX_SIZE 255 +#define MAX_IP4_CONFIG_DNS 16 + +/// +/// IP4_CONFIG2_IFR_NVDATA contains the IP4 configure +/// parameters for that NIC. +/// +typedef struct { + UINT8 Configure; ///< NIC configure status + UINT8 DhcpEnable; ///< Static or DHCP + CHAR16 StationAddress[IP4_STR_MAX_SIZE]; ///< IP addresses + CHAR16 SubnetMask[IP4_STR_MAX_SIZE]; ///< Subnet address + CHAR16 GatewayAddress[IP4_STR_MAX_SIZE]; ///< Gateway address + CHAR16 DnsAddress[ADDRESS_STR_MAX_SIZE]; ///< DNS server address +} IP4_CONFIG2_IFR_NVDATA; + +#endif + diff --git a/NetworkPkg/Ip4Dxe/Ip4Option.c b/NetworkPkg/Ip4Dxe/Ip4Option.c new file mode 100644 index 000000000..f1c10014e --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Option.c @@ -0,0 +1,204 @@ +/** @file + IP4 option support functions. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + + +/** + Validate the IP4 option format for both the packets we received + and will transmit. + + @param[in] Option The first byte of the option + @param[in] OptionLen The length of the whole option + @param[in] Rcvd The option is from the packet we received if TRUE, + otherwise the option we wants to transmit. + + @retval TRUE The option is properly formatted + @retval FALSE The option is mal-formated + +**/ +BOOLEAN +Ip4OptionIsValid ( + IN UINT8 *Option, + IN UINT32 OptionLen, + IN BOOLEAN Rcvd + ) +{ + UINT32 Cur; + UINT32 Len; + UINT32 Point; + + Cur = 0; + + while (Cur < OptionLen) { + switch (Option[Cur]) { + case IP4_OPTION_NOP: + Cur++; + break; + + case IP4_OPTION_EOP: + Cur = OptionLen; + break; + + case IP4_OPTION_LSRR: + case IP4_OPTION_SSRR: + case IP4_OPTION_RR: + Len = Option[Cur + 1]; + Point = Option[Cur + 2]; + + // + // SRR/RR options are formatted as |Type|Len|Point|Ip1|Ip2|... + // + if ((OptionLen - Cur < Len) || (Len < 3) || ((Len - 3) % 4 != 0)) { + return FALSE; + } + + if ((Point > Len + 1) || (Point % 4 != 0)) { + return FALSE; + } + + // + // The Point must point pass the last entry if the packet is received + // by us. It must point to 4 if the packet is to be sent by us for + // source route option. + // + if ((Option[Cur] != IP4_OPTION_RR) && + ((Rcvd && (Point != Len + 1)) || (!Rcvd && (Point != 4)))) { + + return FALSE; + } + + Cur += Len; + break; + + default: + Len = Option[Cur + 1]; + + if ((OptionLen - Cur < Len) || (Len < 2)) { + return FALSE; + } + + Cur = Cur + Len; + break; + } + + } + + return TRUE; +} + + +/** + Copy the option from the original option to buffer. It + handles the details such as: + 1. whether copy the single IP4 option to the first/non-first + fragments. + 2. Pad the options copied over to aligned to 4 bytes. + + @param[in] Option The original option to copy from + @param[in] OptionLen The length of the original option + @param[in] FirstFragment Whether it is the first fragment + @param[in, out] Buf The buffer to copy options to. NULL + @param[in, out] BufLen The length of the buffer + + @retval EFI_SUCCESS The options are copied over + @retval EFI_BUFFER_TOO_SMALL Buf is NULL or BufLen provided is too small. + +**/ +EFI_STATUS +Ip4CopyOption ( + IN UINT8 *Option, + IN UINT32 OptionLen, + IN BOOLEAN FirstFragment, + IN OUT UINT8 *Buf, OPTIONAL + IN OUT UINT32 *BufLen + ) +{ + UINT8 OptBuf[40]; + UINT32 Cur; + UINT32 Next; + UINT8 Type; + UINT32 Len; + + ASSERT ((BufLen != NULL) && (OptionLen <= 40)); + + Cur = 0; + Next = 0; + + while (Cur < OptionLen) { + Type = Option[Cur]; + Len = Option[Cur + 1]; + + if (Type == IP4_OPTION_NOP) { + // + // Keep the padding, in case that the sender wants to align + // the option, say, to 4 bytes + // + OptBuf[Next] = IP4_OPTION_NOP; + Next++; + Cur++; + + } else if (Type == IP4_OPTION_EOP) { + // + // Don't append the EOP to avoid including only a EOP option + // + break; + + } else { + // + // don't copy options that is only valid for the first fragment + // + if (FirstFragment || (Type & IP4_OPTION_COPY_MASK) != 0) { + CopyMem (OptBuf + Next, Option + Cur, Len); + Next += Len; + } + + Cur += Len; + } + } + + // + // Don't append an EOP only option. + // + if (Next == 0) { + *BufLen = 0; + return EFI_SUCCESS; + } + + // + // Append an EOP if the end of option doesn't coincide with the + // end of the IP header, that is, isn't aligned to 4 bytes.. + // + if ((Next % 4) != 0) { + OptBuf[Next] = IP4_OPTION_EOP; + Next++; + } + + // + // Head length is in the unit of 4 bytes. Now, Len is the + // acutal option length to appear in the IP header. + // + Len = ((Next + 3) &~0x03); + + // + // If the buffer is too small, set the BufLen then return + // + if ((Buf == NULL) || (*BufLen < Len)) { + *BufLen = Len; + return EFI_BUFFER_TOO_SMALL; + } + + // + // Copy the option to the Buf, zero the buffer first to pad + // the options with NOP to align to 4 bytes. + // + ZeroMem (Buf, Len); + CopyMem (Buf, OptBuf, Next); + *BufLen = Len; + return EFI_SUCCESS; +} diff --git a/NetworkPkg/Ip4Dxe/Ip4Option.h b/NetworkPkg/Ip4Dxe/Ip4Option.h new file mode 100644 index 000000000..57a5a5872 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Option.h @@ -0,0 +1,66 @@ +/** @file + IP4 option support routines. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP4_OPTION_H__ +#define __EFI_IP4_OPTION_H__ + +#define IP4_OPTION_EOP 0 +#define IP4_OPTION_NOP 1 +#define IP4_OPTION_LSRR 131 // Loss source and record routing, 10000011 +#define IP4_OPTION_SSRR 137 // Strict source and record routing, 10001001 +#define IP4_OPTION_RR 7 // Record routing, 00000111 + +#define IP4_OPTION_COPY_MASK 0x80 + +/** + Validate the IP4 option format for both the packets we received + and will transmit. It will compute the ICMP error message fields + if the option is mal-formated. But this information isn't used. + + @param[in] Option The first byte of the option + @param[in] OptionLen The length of the whole option + @param[in] Rcvd The option is from the packet we received if TRUE, + otherwise the option we wants to transmit. + + @retval TRUE The option is properly formatted + @retval FALSE The option is mal-formated + +**/ +BOOLEAN +Ip4OptionIsValid ( + IN UINT8 *Option, + IN UINT32 OptionLen, + IN BOOLEAN Rcvd + ); + +/** + Copy the option from the original option to buffer. It + handles the details such as: + 1. whether copy the single IP4 option to the first/non-first + fragments. + 2. Pad the options copied over to aligned to 4 bytes. + + @param[in] Option The original option to copy from + @param[in] OptionLen The length of the original option + @param[in] FirstFragment Whether it is the first fragment + @param[in, out] Buf The buffer to copy options to. NULL + @param[in, out] BufLen The length of the buffer + + @retval EFI_SUCCESS The options are copied over + @retval EFI_BUFFER_TOO_SMALL Buf is NULL or BufLen provided is too small. + +**/ +EFI_STATUS +Ip4CopyOption ( + IN UINT8 *Option, + IN UINT32 OptionLen, + IN BOOLEAN FirstFragment, + IN OUT UINT8 *Buf, OPTIONAL + IN OUT UINT32 *BufLen + ); +#endif diff --git a/NetworkPkg/Ip4Dxe/Ip4Output.c b/NetworkPkg/Ip4Dxe/Ip4Output.c new file mode 100644 index 000000000..5eb381408 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Output.c @@ -0,0 +1,482 @@ +/** @file + Transmit the IP4 packet. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + +UINT16 mIp4Id; + + +/** + Prepend an IP4 head to the Packet. It will copy the options and + build the IP4 header fields. Used for IP4 fragmentation. + + @param Packet The packet to prepend IP4 header to + @param Head The caller supplied header. The caller should set + the following header fields: Tos, TotalLen, Id, + Fragment, Ttl, Protocol, Src and Dst. All the fields + are in host byte order. This function will fill in + the Ver, HeadLen, and checksum. + @param Option The orginal IP4 option to copy from + @param OptLen The length of the IP4 option + + @retval EFI_BAD_BUFFER_SIZE There is no enought room in the head space of + Packet. + @retval EFI_SUCCESS The IP4 header is successfully added to the packet. + +**/ +EFI_STATUS +Ip4PrependHead ( + IN OUT NET_BUF *Packet, + IN IP4_HEAD *Head, + IN UINT8 *Option, + IN UINT32 OptLen + ) +{ + UINT32 HeadLen; + UINT32 Len; + IP4_HEAD *PacketHead; + BOOLEAN FirstFragment; + + // + // Prepend the options: first get the option length, then copy it over. + // + HeadLen = 0; + FirstFragment = IP4_FIRST_FRAGMENT (Head->Fragment); + + Ip4CopyOption (Option, OptLen, FirstFragment, NULL, &Len); + + HeadLen = IP4_MIN_HEADLEN + Len; + ASSERT (((Len % 4) == 0) && (HeadLen <= IP4_MAX_HEADLEN)); + + PacketHead = (IP4_HEAD *) NetbufAllocSpace (Packet, HeadLen, NET_BUF_HEAD); + + if (PacketHead == NULL) { + return EFI_BAD_BUFFER_SIZE; + } + + Ip4CopyOption (Option, OptLen, FirstFragment, (UINT8 *) (PacketHead + 1), &Len); + + // + // Set the head up, convert the host byte order to network byte order + // + PacketHead->Ver = 4; + PacketHead->HeadLen = (UINT8) (HeadLen >> 2); + PacketHead->Tos = Head->Tos; + PacketHead->TotalLen = HTONS ((UINT16) Packet->TotalSize); + PacketHead->Id = HTONS (Head->Id); + PacketHead->Fragment = HTONS (Head->Fragment); + PacketHead->Checksum = 0; + PacketHead->Ttl = Head->Ttl; + PacketHead->Protocol = Head->Protocol; + PacketHead->Src = HTONL (Head->Src); + PacketHead->Dst = HTONL (Head->Dst); + PacketHead->Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) PacketHead, HeadLen)); + + Packet->Ip.Ip4 = PacketHead; + return EFI_SUCCESS; +} + + +/** + Select an interface to send the packet generated in the IP4 driver + itself, that is, not by the requests of IP4 child's consumer. Such + packets include the ICMP echo replies, and other ICMP error packets. + + @param[in] IpSb The IP4 service that wants to send the packets. + @param[in] Dst The destination of the packet + @param[in] Src The source of the packet + + @return NULL if no proper interface is found, otherwise the interface that + can be used to send the system packet from. + +**/ +IP4_INTERFACE * +Ip4SelectInterface ( + IN IP4_SERVICE *IpSb, + IN IP4_ADDR Dst, + IN IP4_ADDR Src + ) +{ + IP4_INTERFACE *IpIf; + IP4_INTERFACE *Selected; + LIST_ENTRY *Entry; + + // + // Select the interface the Dst is on if one of the connected + // network. Some IP instance may be configured with 0.0.0.0/0, + // don't select that interface now. + // + IpIf = Ip4FindNet (IpSb, Dst); + + if ((IpIf != NULL) && (IpIf->Ip != IP4_ALLZERO_ADDRESS)) { + return IpIf; + } + + // + // If source is one of the interface address, select it. + // + IpIf = Ip4FindInterface (IpSb, Src); + + if ((IpIf != NULL) && (IpIf->Ip != IP4_ALLZERO_ADDRESS)) { + return IpIf; + } + + // + // Select a configured interface as the fall back. Always prefer + // an interface with non-zero address. + // + Selected = NULL; + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link); + + if (IpIf->Configured && ((Selected == NULL) || (Selected->Ip == 0))) { + Selected = IpIf; + } + } + + return Selected; +} + + +/** + The default callback function for system generated packet. + It will free the packet. + + @param Ip4Instance The IP4 child that issued the transmission. It most + like is NULL. + @param Packet The packet that transmitted. + @param IoStatus The result of the transmission, succeeded or failed. + @param LinkFlag Not used when transmission. check IP4_FRAME_CALLBACK + for reference. + @param Context The context provided by us + +**/ +VOID +Ip4SysPacketSent ( + IP4_PROTOCOL *Ip4Instance, + NET_BUF *Packet, + EFI_STATUS IoStatus, + UINT32 LinkFlag, + VOID *Context + ) +{ + NetbufFree (Packet); +} + + +/** + Transmit an IP4 packet. The packet comes either from the IP4 + child's consumer (IpInstance != NULL) or the IP4 driver itself + (IpInstance == NULL). It will route the packet, fragment it, + then transmit all the fragments through some interface. + + @param[in] IpSb The IP4 service instance to transmit the packet + @param[in] IpInstance The IP4 child that issues the transmission. It is + NULL if the packet is from the system. + @param[in] Packet The user data to send, excluding the IP header. + @param[in] Head The caller supplied header. The caller should set + the following header fields: Tos, TotalLen, Id, tl, + Fragment, Protocol, Src and Dst. All the fields are + in host byte order. This function will fill in the + Ver, HeadLen, Fragment, and checksum. The Fragment + only need to include the DF flag. Ip4Output will + compute the MF and offset for you. + @param[in] Option The original option to append to the IP headers + @param[in] OptLen The length of the option + @param[in] GateWay The next hop address to transmit packet to. + 255.255.255.255 means broadcast. + @param[in] Callback The callback function to issue when transmission + completed. + @param[in] Context The opaque context for the callback + + @retval EFI_NO_MAPPING There is no interface to the destination. + @retval EFI_NOT_FOUND There is no route to the destination + @retval EFI_SUCCESS The packet is successfully transmitted. + @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + + total data length is greater than MTU (or greater + than the maximum packet size if Token.Packet.TxData. + OverrideData.DoNotFragment is TRUE.) + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Ip4Output ( + IN IP4_SERVICE *IpSb, + IN IP4_PROTOCOL *IpInstance OPTIONAL, + IN NET_BUF *Packet, + IN IP4_HEAD *Head, + IN UINT8 *Option, + IN UINT32 OptLen, + IN IP4_ADDR GateWay, + IN IP4_FRAME_CALLBACK Callback, + IN VOID *Context + ) +{ + IP4_INTERFACE *IpIf; + IP4_ROUTE_CACHE_ENTRY *CacheEntry; + IP4_ADDR Dest; + EFI_STATUS Status; + NET_BUF *Fragment; + UINT32 Index; + UINT32 HeadLen; + UINT32 PacketLen; + UINT32 Offset; + UINT32 Mtu; + UINT32 Num; + BOOLEAN RawData; + + // + // Select an interface/source for system packet, application + // should select them itself. + // + if (IpInstance == NULL) { + IpIf = Ip4SelectInterface (IpSb, Head->Dst, Head->Src); + } else { + IpIf = IpInstance->Interface; + } + + if (IpIf == NULL) { + return EFI_NO_MAPPING; + } + + if ((Head->Src == IP4_ALLZERO_ADDRESS) && (IpInstance == NULL)) { + Head->Src = IpIf->Ip; + } + + // + // Before IPsec process, prepared the IP head. + // If Ip4Output is transmitting RawData, don't update IPv4 header. + // + HeadLen = sizeof (IP4_HEAD) + ((OptLen + 3) & (~0x03)); + + if ((IpInstance != NULL) && IpInstance->ConfigData.RawData) { + RawData = TRUE; + } else { + Head->HeadLen = (UINT8) (HeadLen >> 2); + Head->Id = mIp4Id++; + Head->Ver = 4; + RawData = FALSE; + } + + // + // Call IPsec process. + // + Status = Ip4IpSecProcessPacket ( + IpSb, + &Head, + &Packet, + &Option, + &OptLen, + EfiIPsecOutBound, + Context + ); + + if (EFI_ERROR(Status)) { + return Status; + } + + Dest = Head->Dst; + if (IP4_IS_BROADCAST (Ip4GetNetCast (Dest, IpIf)) || (Dest == IP4_ALLONE_ADDRESS)) { + // + // Set the gateway to local broadcast if the Dest is + // the broadcast address for the connected network or + // it is local broadcast. + // + GateWay = IP4_ALLONE_ADDRESS; + + } else if (IP4_IS_MULTICAST (Dest)) { + // + // Set the gateway to the destination if it is an multicast + // address. The IP4_INTERFACE won't consult ARP to send local + // broadcast and multicast. + // + GateWay = Head->Dst; + + } else if (GateWay == IP4_ALLZERO_ADDRESS) { + // + // Route the packet unless overrided, that is, GateWay isn't zero. + // + if (IpInstance == NULL) { + CacheEntry = Ip4Route (IpSb->DefaultRouteTable, Head->Dst, Head->Src, IpIf->SubnetMask, TRUE); + } else { + CacheEntry = Ip4Route (IpInstance->RouteTable, Head->Dst, Head->Src, IpIf->SubnetMask, FALSE); + // + // If failed to route the packet by using the instance's route table, + // try to use the default route table. + // + if (CacheEntry == NULL) { + CacheEntry = Ip4Route (IpSb->DefaultRouteTable, Head->Dst, Head->Src, IpIf->SubnetMask, TRUE); + } + } + + if (CacheEntry == NULL) { + return EFI_NOT_FOUND; + } + + GateWay = CacheEntry->NextHop; + Ip4FreeRouteCacheEntry (CacheEntry); + } + + // + // OK, selected the source and route, fragment the packet then send + // them. Tag each fragment other than the first one as spawn from it. + // + Mtu = IpSb->MaxPacketSize + sizeof (IP4_HEAD); + + if (Packet->TotalSize + HeadLen > Mtu) { + // + // Fragmentation is diabled for RawData mode. + // + if (RawData) { + return EFI_BAD_BUFFER_SIZE; + } + + // + // Packet is fragmented from the tail to the head, that is, the + // first frame sent is the last fragment of the packet. The first + // fragment is NOT sent in this loop. First compute how many + // fragments there are. + // + Mtu = (Mtu - HeadLen) & (~0x07); + Num = (Packet->TotalSize + Mtu - 1) / Mtu; + + // + // Initialize the packet length and Offset. Other than the last + // fragment, the packet length equals to MTU. The offset is always + // aligned to MTU. + // + PacketLen = Packet->TotalSize - (Num - 1) * Mtu; + Offset = Mtu * (Num - 1); + + for (Index = 0; Index < Num - 1; Index++, Offset -= Mtu) { + Fragment = NetbufGetFragment (Packet, Offset, PacketLen, IP4_MAX_HEADLEN); + + if (Fragment == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + // + // Update the header's fragment. The caller fills the IP4 header + // fields that are required by Ip4PrependHead except the fragment. + // + Head->Fragment = IP4_HEAD_FRAGMENT_FIELD (FALSE, (Index != 0), Offset); + Ip4PrependHead (Fragment, Head, Option, OptLen); + + // + // Transmit the fragments, pass the Packet address as the context. + // So, we can find all the fragments spawned from the Packet by + // compare the NetBuf and Context to the Packet. + // + Status = Ip4SendFrame ( + IpIf, + IpInstance, + Fragment, + GateWay, + Ip4SysPacketSent, + Packet, + IpSb + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + PacketLen = Mtu; + } + + // + // Trim the already sent data, then adjust the head's fragment field. + // + NetbufTrim (Packet, Packet->TotalSize - Mtu, FALSE); + Head->Fragment = IP4_HEAD_FRAGMENT_FIELD (FALSE, TRUE, 0); + } + + // + // Send the first fragment, it is either the orginal packet, or the + // first fragment of a fragmented packet. It seems that there is a subtle + // bug here: what if the caller free the packet in Callback and IpIf (or + // MNP child used by that interface) still holds the fragments and try + // to access the data? The caller can free the packet if it recycles the + // consumer's (such as UDP) data in the Callback. But this can't happen. + // The detailed sequence is: + // 1. for the packets generated by IP4 driver itself: + // The Callback is Ip4SysPacketSent, which is the same as the + // fragments' callback. Ip4SysPacketSent simply calls NetbufFree + // to release its reference to the packet. So, no problem for + // system packets. + // + // 2. for the upper layer's packets (use UDP as an example): + // UDP requests the IP layer to transmit some data which is + // wrapped in an asynchronous token, the token is wrapped + // in IP4_TXTOKEN_WRAP by IP4. IP4 also wrap the user's data + // in a net buffer, which is Packet we get here. IP4_TXTOKEN_WRAP + // is bound with the Packet. It will only be freed when all + // the references to Packet have been released. Upon then, the + // Packet's OnFree callback will release the IP4_TXTOKEN_WRAP, + // and singal the user's recycle event. So, also no problem for + // upper layer's packets. + // + Ip4PrependHead (Packet, Head, Option, OptLen); + Status = Ip4SendFrame (IpIf, IpInstance, Packet, GateWay, Callback, Context, IpSb); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + Ip4CancelPacket (IpIf, Packet, Status); + return Status; +} + + +/** + The filter function to find a packet and all its fragments. + The packet's fragments have their Context set to the packet. + + @param[in] Frame The frames hold by the low level interface + @param[in] Context Context to the function, which is the packet. + + @retval TRUE This is the packet to cancel or its fragments. + @retval FALSE This is unrelated packet. + +**/ +BOOLEAN +Ip4CancelPacketFragments ( + IN IP4_LINK_TX_TOKEN *Frame, + IN VOID *Context + ) +{ + if ((Frame->Packet == (NET_BUF *) Context) || (Frame->Context == Context)) { + return TRUE; + } + + return FALSE; +} + + +/** + Cancel the Packet and all its fragments. + + @param IpIf The interface from which the Packet is sent + @param Packet The Packet to cancel + @param IoStatus The status returns to the sender. + +**/ +VOID +Ip4CancelPacket ( + IN IP4_INTERFACE *IpIf, + IN NET_BUF *Packet, + IN EFI_STATUS IoStatus + ) +{ + Ip4CancelFrames (IpIf, IoStatus, Ip4CancelPacketFragments, Packet); +} diff --git a/NetworkPkg/Ip4Dxe/Ip4Output.h b/NetworkPkg/Ip4Dxe/Ip4Output.h new file mode 100644 index 000000000..ae54f8b48 --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Output.h @@ -0,0 +1,120 @@ +/** @file + +Copyright (c) 2005 - 2006, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP4_OUTPUT_H__ +#define __EFI_IP4_OUTPUT_H__ + +/** + The default callback function for system generated packet. + It will free the packet. + + @param Ip4Instance The IP4 child that issued the transmission. It most + like is NULL. + @param Packet The packet that transmitted. + @param IoStatus The result of the transmission, succeeded or failed. + @param LinkFlag Not used when transmission. check IP4_FRAME_CALLBACK + for reference. + @param Context The context provided by us + +**/ +VOID +Ip4SysPacketSent ( + IP4_PROTOCOL *Ip4Instance, + NET_BUF *Packet, + EFI_STATUS IoStatus, + UINT32 LinkFlag, + VOID *Context + ); + +/** + Transmit an IP4 packet. The packet comes either from the IP4 + child's consumer (IpInstance != NULL) or the IP4 driver itself + (IpInstance == NULL). It will route the packet, fragment it, + then transmit all the fragments through some interface. + + @param[in] IpSb The IP4 service instance to transmit the packet + @param[in] IpInstance The IP4 child that issues the transmission. It is + NULL if the packet is from the system. + @param[in] Packet The user data to send, excluding the IP header. + @param[in] Head The caller supplied header. The caller should set + the following header fields: Tos, TotalLen, Id, tl, + Fragment, Protocol, Src and Dst. All the fields are + in host byte order. This function will fill in the + Ver, HeadLen, Fragment, and checksum. The Fragment + only need to include the DF flag. Ip4Output will + compute the MF and offset for you. + @param[in] Option The original option to append to the IP headers + @param[in] OptLen The length of the option + @param[in] GateWay The next hop address to transmit packet to. + 255.255.255.255 means broadcast. + @param[in] Callback The callback function to issue when transmission + completed. + @param[in] Context The opaque context for the callback + + @retval EFI_NO_MAPPING There is no interface to the destination. + @retval EFI_NOT_FOUND There is no route to the destination + @retval EFI_SUCCESS The packet is successfully transmitted. + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Ip4Output ( + IN IP4_SERVICE *IpSb, + IN IP4_PROTOCOL *IpInstance OPTIONAL, + IN NET_BUF *Packet, + IN IP4_HEAD *Head, + IN UINT8 *Option, + IN UINT32 OptLen, + IN IP4_ADDR GateWay, + IN IP4_FRAME_CALLBACK Callback, + IN VOID *Context + ); + +/** + Cancel the Packet and all its fragments. + + @param IpIf The interface from which the Packet is sent + @param Packet The Packet to cancel + @param IoStatus The status returns to the sender. + +**/ +VOID +Ip4CancelPacket ( + IN IP4_INTERFACE *IpIf, + IN NET_BUF *Packet, + IN EFI_STATUS IoStatus + ); + +/** + Prepend an IP4 head to the Packet. It will copy the options and + build the IP4 header fields. Used for IP4 fragmentation. + + @param Packet The packet to prepend IP4 header to + @param Head The caller supplied header. The caller should set + the following header fields: Tos, TotalLen, Id, + Fragment, Ttl, Protocol, Src and Dst. All the fields + are in host byte order. This function will fill in + the Ver, HeadLen, and checksum. + @param Option The orginal IP4 option to copy from + @param OptLen The length of the IP4 option + + @retval EFI_BAD_BUFFER_SIZE There is no enought room in the head space of + Packet. + @retval EFI_SUCCESS The IP4 header is successfully added to the packet. + +**/ +EFI_STATUS +Ip4PrependHead ( + IN OUT NET_BUF *Packet, + IN IP4_HEAD *Head, + IN UINT8 *Option, + IN UINT32 OptLen + ); + +extern UINT16 mIp4Id; + +#endif diff --git a/NetworkPkg/Ip4Dxe/Ip4Route.c b/NetworkPkg/Ip4Dxe/Ip4Route.c new file mode 100644 index 000000000..124c0730a --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Route.c @@ -0,0 +1,673 @@ +/** @file + +Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip4Impl.h" + + +/** + Allocate a route entry then initialize it with the Dest/Netmaks + and Gateway. + + @param[in] Dest The destination network + @param[in] Netmask The destination network mask + @param[in] GateWay The nexthop address + + @return NULL if failed to allocate memeory, otherwise the newly created + route entry. + +**/ +IP4_ROUTE_ENTRY * +Ip4CreateRouteEntry ( + IN IP4_ADDR Dest, + IN IP4_ADDR Netmask, + IN IP4_ADDR GateWay + ) +{ + IP4_ROUTE_ENTRY *RtEntry; + + RtEntry = AllocatePool (sizeof (IP4_ROUTE_ENTRY)); + + if (RtEntry == NULL) { + return NULL; + } + + InitializeListHead (&RtEntry->Link); + + RtEntry->RefCnt = 1; + RtEntry->Dest = Dest; + RtEntry->Netmask = Netmask; + RtEntry->NextHop = GateWay; + RtEntry->Flag = 0; + + return RtEntry; +} + + +/** + Free the route table entry. It is reference counted. + + @param RtEntry The route entry to free. + +**/ +VOID +Ip4FreeRouteEntry ( + IN IP4_ROUTE_ENTRY *RtEntry + ) +{ + ASSERT (RtEntry->RefCnt > 0); + + if (--RtEntry->RefCnt == 0) { + FreePool (RtEntry); + } +} + + +/** + Allocate and initialize an IP4 route cache entry. + + @param[in] Dst The destination address + @param[in] Src The source address + @param[in] GateWay The next hop address + @param[in] Tag The tag from the caller. This marks all the cache + entries spawned from one route table entry. + + @return NULL if failed to allocate memory for the cache, other point + to the created route cache entry. + +**/ +IP4_ROUTE_CACHE_ENTRY * +Ip4CreateRouteCacheEntry ( + IN IP4_ADDR Dst, + IN IP4_ADDR Src, + IN IP4_ADDR GateWay, + IN UINTN Tag + ) +{ + IP4_ROUTE_CACHE_ENTRY *RtCacheEntry; + + RtCacheEntry = AllocatePool (sizeof (IP4_ROUTE_CACHE_ENTRY)); + + if (RtCacheEntry == NULL) { + return NULL; + } + + InitializeListHead (&RtCacheEntry->Link); + + RtCacheEntry->RefCnt = 1; + RtCacheEntry->Dest = Dst; + RtCacheEntry->Src = Src; + RtCacheEntry->NextHop = GateWay; + RtCacheEntry->Tag = Tag; + + return RtCacheEntry; +} + + +/** + Free the route cache entry. It is reference counted. + + @param RtCacheEntry The route cache entry to free. + +**/ +VOID +Ip4FreeRouteCacheEntry ( + IN IP4_ROUTE_CACHE_ENTRY *RtCacheEntry + ) +{ + ASSERT (RtCacheEntry->RefCnt > 0); + + if (--RtCacheEntry->RefCnt == 0) { + FreePool (RtCacheEntry); + } +} + + +/** + Initialize an empty route cache table. + + @param[in, out] RtCache The rotue cache table to initialize. + +**/ +VOID +Ip4InitRouteCache ( + IN OUT IP4_ROUTE_CACHE *RtCache + ) +{ + UINT32 Index; + + for (Index = 0; Index < IP4_ROUTE_CACHE_HASH_VALUE; Index++) { + InitializeListHead (&(RtCache->CacheBucket[Index])); + } +} + + +/** + Clean up a route cache, that is free all the route cache + entries enqueued in the cache. + + @param[in] RtCache The route cache table to clean up + +**/ +VOID +Ip4CleanRouteCache ( + IN IP4_ROUTE_CACHE *RtCache + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP4_ROUTE_CACHE_ENTRY *RtCacheEntry; + UINT32 Index; + + for (Index = 0; Index < IP4_ROUTE_CACHE_HASH_VALUE; Index++) { + NET_LIST_FOR_EACH_SAFE (Entry, Next, &(RtCache->CacheBucket[Index])) { + RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link); + + RemoveEntryList (Entry); + Ip4FreeRouteCacheEntry (RtCacheEntry); + } + } +} + + + +/** + Create an empty route table, includes its internal route cache + + @return NULL if failed to allocate memory for the route table, otherwise + the point to newly created route table. + +**/ +IP4_ROUTE_TABLE * +Ip4CreateRouteTable ( + VOID + ) +{ + IP4_ROUTE_TABLE *RtTable; + UINT32 Index; + + RtTable = AllocatePool (sizeof (IP4_ROUTE_TABLE)); + + if (RtTable == NULL) { + return NULL; + } + + RtTable->RefCnt = 1; + RtTable->TotalNum = 0; + + for (Index = 0; Index <= IP4_MASK_MAX; Index++) { + InitializeListHead (&(RtTable->RouteArea[Index])); + } + + RtTable->Next = NULL; + + Ip4InitRouteCache (&RtTable->Cache); + return RtTable; +} + + +/** + Free the route table and its associated route cache. Route + table is reference counted. + + @param[in] RtTable The route table to free. + +**/ +VOID +Ip4FreeRouteTable ( + IN IP4_ROUTE_TABLE *RtTable + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP4_ROUTE_ENTRY *RtEntry; + UINT32 Index; + + ASSERT (RtTable->RefCnt > 0); + + if (--RtTable->RefCnt > 0) { + return ; + } + + // + // Free all the route table entry and its route cache. + // + for (Index = 0; Index <= IP4_MASK_MAX; Index++) { + NET_LIST_FOR_EACH_SAFE (Entry, Next, &(RtTable->RouteArea[Index])) { + RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link); + + RemoveEntryList (Entry); + Ip4FreeRouteEntry (RtEntry); + } + } + + Ip4CleanRouteCache (&RtTable->Cache); + + FreePool (RtTable); +} + + + +/** + Remove all the cache entries bearing the Tag. When a route cache + entry is created, it is tagged with the address of route entry + from which it is spawned. When a route entry is deleted, the cache + entries spawned from it are also deleted. + + @param RtCache Route cache to remove the entries from + @param Tag The Tag of the entries to remove + +**/ +VOID +Ip4PurgeRouteCache ( + IN OUT IP4_ROUTE_CACHE *RtCache, + IN UINTN Tag + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP4_ROUTE_CACHE_ENTRY *RtCacheEntry; + UINT32 Index; + + for (Index = 0; Index < IP4_ROUTE_CACHE_HASH_VALUE; Index++) { + NET_LIST_FOR_EACH_SAFE (Entry, Next, &RtCache->CacheBucket[Index]) { + + RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link); + + if (RtCacheEntry->Tag == Tag) { + RemoveEntryList (Entry); + Ip4FreeRouteCacheEntry (RtCacheEntry); + } + } + } +} + + +/** + Add a route entry to the route table. All the IP4_ADDRs are in + host byte order. + + @param[in, out] RtTable Route table to add route to + @param[in] Dest The destination of the network + @param[in] Netmask The netmask of the destination + @param[in] Gateway The next hop address + + @retval EFI_ACCESS_DENIED The same route already exists + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the entry + @retval EFI_SUCCESS The route is added successfully. + +**/ +EFI_STATUS +Ip4AddRoute ( + IN OUT IP4_ROUTE_TABLE *RtTable, + IN IP4_ADDR Dest, + IN IP4_ADDR Netmask, + IN IP4_ADDR Gateway + ) +{ + LIST_ENTRY *Head; + LIST_ENTRY *Entry; + IP4_ROUTE_ENTRY *RtEntry; + + // + // All the route entries with the same netmask length are + // linke to the same route area + // + Head = &(RtTable->RouteArea[NetGetMaskLength (Netmask)]); + + // + // First check whether the route exists + // + NET_LIST_FOR_EACH (Entry, Head) { + RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link); + + if (IP4_NET_EQUAL (RtEntry->Dest, Dest, Netmask) && (RtEntry->NextHop == Gateway)) { + return EFI_ACCESS_DENIED; + } + } + + // + // Create a route entry and insert it to the route area. + // + RtEntry = Ip4CreateRouteEntry (Dest, Netmask, Gateway); + + if (RtEntry == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if (Gateway == IP4_ALLZERO_ADDRESS) { + RtEntry->Flag = IP4_DIRECT_ROUTE; + } + + InsertHeadList (Head, &RtEntry->Link); + RtTable->TotalNum++; + + return EFI_SUCCESS; +} + + +/** + Remove a route entry and all the route caches spawn from it. + + @param RtTable The route table to remove the route from + @param Dest The destination network + @param Netmask The netmask of the Dest + @param Gateway The next hop address + + @retval EFI_SUCCESS The route entry is successfully removed + @retval EFI_NOT_FOUND There is no route entry in the table with that + properity. + +**/ +EFI_STATUS +Ip4DelRoute ( + IN OUT IP4_ROUTE_TABLE *RtTable, + IN IP4_ADDR Dest, + IN IP4_ADDR Netmask, + IN IP4_ADDR Gateway + ) +{ + LIST_ENTRY *Head; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP4_ROUTE_ENTRY *RtEntry; + + Head = &(RtTable->RouteArea[NetGetMaskLength (Netmask)]); + + NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) { + RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link); + + if (IP4_NET_EQUAL (RtEntry->Dest, Dest, Netmask) && (RtEntry->NextHop == Gateway)) { + Ip4PurgeRouteCache (&RtTable->Cache, (UINTN) RtEntry); + RemoveEntryList (Entry); + Ip4FreeRouteEntry (RtEntry); + + RtTable->TotalNum--; + return EFI_SUCCESS; + } + } + + return EFI_NOT_FOUND; +} + + +/** + Find a route cache with the dst and src. This is used by ICMP + redirect messasge process. All kinds of redirect is treated as + host redirect according to RFC1122. So, only route cache entries + are modified according to the ICMP redirect message. + + @param[in] RtTable The route table to search the cache for + @param[in] Dest The destination address + @param[in] Src The source address + + @return NULL if no route entry to the (Dest, Src). Otherwise the point + to the correct route cache entry. + +**/ +IP4_ROUTE_CACHE_ENTRY * +Ip4FindRouteCache ( + IN IP4_ROUTE_TABLE *RtTable, + IN IP4_ADDR Dest, + IN IP4_ADDR Src + ) +{ + LIST_ENTRY *Entry; + IP4_ROUTE_CACHE_ENTRY *RtCacheEntry; + UINT32 Index; + + Index = IP4_ROUTE_CACHE_HASH (Dest, Src); + + NET_LIST_FOR_EACH (Entry, &RtTable->Cache.CacheBucket[Index]) { + RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link); + + if ((RtCacheEntry->Dest == Dest) && (RtCacheEntry->Src == Src)) { + NET_GET_REF (RtCacheEntry); + return RtCacheEntry; + } + } + + return NULL; +} + + +/** + Search the route table for a most specific match to the Dst. It searches + from the longest route area (mask length == 32) to the shortest route area + (default routes). In each route area, it will first search the instance's + route table, then the default route table. This is required by the following + requirements: + 1. IP search the route table for a most specific match + 2. The local route entries have precedence over the default route entry. + + @param[in] RtTable The route table to search from + @param[in] Dst The destionation address to search + + @return NULL if no route matches the Dst, otherwise the point to the + most specific route to the Dst. + +**/ +IP4_ROUTE_ENTRY * +Ip4FindRouteEntry ( + IN IP4_ROUTE_TABLE *RtTable, + IN IP4_ADDR Dst + ) +{ + LIST_ENTRY *Entry; + IP4_ROUTE_ENTRY *RtEntry; + IP4_ROUTE_TABLE *Table; + INTN Index; + + RtEntry = NULL; + + for (Index = IP4_MASK_MAX; Index >= 0; Index--) { + for (Table = RtTable; Table != NULL; Table = Table->Next) { + NET_LIST_FOR_EACH (Entry, &Table->RouteArea[Index]) { + RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link); + + if (IP4_NET_EQUAL (RtEntry->Dest, Dst, RtEntry->Netmask)) { + NET_GET_REF (RtEntry); + return RtEntry; + } + } + } + } + + + return NULL; +} + + +/** + Search the route table to route the packet. Return/create a route + cache if there is a route to the destination. + + @param[in] RtTable The route table to search from + @param[in] Dest The destination address to search for + @param[in] Src The source address to search for + @param[in] SubnetMask The subnet mask of the Src address, this field is + used to check if the station is using /32 subnet. + @param[in] AlwaysTryDestAddr Always try to use the dest address as next hop even + though we can't find a matching route entry. This + field is only valid when using /32 subnet. + + @return NULL if failed to route packet, otherwise a route cache + entry that can be used to route packet. + +**/ +IP4_ROUTE_CACHE_ENTRY * +Ip4Route ( + IN IP4_ROUTE_TABLE *RtTable, + IN IP4_ADDR Dest, + IN IP4_ADDR Src, + IN IP4_ADDR SubnetMask, + IN BOOLEAN AlwaysTryDestAddr + ) +{ + LIST_ENTRY *Head; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP4_ROUTE_CACHE_ENTRY *RtCacheEntry; + IP4_ROUTE_CACHE_ENTRY *Cache; + IP4_ROUTE_ENTRY *RtEntry; + IP4_ADDR NextHop; + UINT32 Count; + + ASSERT (RtTable != NULL); + + Head = &RtTable->Cache.CacheBucket[IP4_ROUTE_CACHE_HASH (Dest, Src)]; + RtCacheEntry = Ip4FindRouteCache (RtTable, Dest, Src); + + // + // If found, promote the cache entry to the head of the hash bucket. LRU + // + if (RtCacheEntry != NULL) { + RemoveEntryList (&RtCacheEntry->Link); + InsertHeadList (Head, &RtCacheEntry->Link); + return RtCacheEntry; + } + + // + // Search the route table for the most specific route + // + RtEntry = Ip4FindRouteEntry (RtTable, Dest); + + if (RtEntry == NULL) { + if (SubnetMask != IP4_ALLONE_ADDRESS) { + return NULL; + } else if (!AlwaysTryDestAddr) { + return NULL; + } + } + + // + // Found a route to the Dest, if it is a direct route, the packet + // will be sent directly to the destination, such as for connected + // network. Otherwise, it is an indirect route, the packet will be + // sent to the next hop router. + // + // When using /32 subnet mask, the packet will always be sent to the direct + // destination first, if we can't find a matching route cache. + // + if (SubnetMask == IP4_ALLONE_ADDRESS || ((RtEntry->Flag & IP4_DIRECT_ROUTE) != 0)) { + NextHop = Dest; + } else { + NextHop = RtEntry->NextHop; + } + + if (RtEntry != NULL) { + Ip4FreeRouteEntry (RtEntry); + } + + // + // Create a route cache entry, and tag it as spawned from this route entry + // For /32 subnet mask, the default route in RtEntry will be used if failed + // to send the packet to driect destination address. + // + RtCacheEntry = Ip4CreateRouteCacheEntry (Dest, Src, NextHop, (UINTN) RtEntry); + + if (RtCacheEntry == NULL) { + return NULL; + } + + InsertHeadList (Head, &RtCacheEntry->Link); + NET_GET_REF (RtCacheEntry); + + // + // Each bucket of route cache can contain at most 64 entries. + // Remove the entries at the tail of the bucket. These entries + // are likely to be used least. + // + Count = 0; + NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) { + if (++Count < IP4_ROUTE_CACHE_MAX) { + continue; + } + + Cache = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link); + + RemoveEntryList (Entry); + Ip4FreeRouteCacheEntry (Cache); + } + + return RtCacheEntry; +} + + +/** + Build a EFI_IP4_ROUTE_TABLE to be returned to the caller of + GetModeData. The EFI_IP4_ROUTE_TABLE is clumsy to use in the + internal operation of the IP4 driver. + + @param[in] IpInstance The IP4 child that requests the route table. + + @retval EFI_SUCCESS The route table is successfully build + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the rotue table. + +**/ +EFI_STATUS +Ip4BuildEfiRouteTable ( + IN IP4_PROTOCOL *IpInstance + ) +{ + LIST_ENTRY *Entry; + IP4_ROUTE_TABLE *RtTable; + IP4_ROUTE_ENTRY *RtEntry; + EFI_IP4_ROUTE_TABLE *Table; + UINT32 Count; + INT32 Index; + + RtTable = IpInstance->RouteTable; + + if (IpInstance->EfiRouteTable != NULL) { + FreePool (IpInstance->EfiRouteTable); + + IpInstance->EfiRouteTable = NULL; + IpInstance->EfiRouteCount = 0; + } + + Count = RtTable->TotalNum; + + if (RtTable->Next != NULL) { + Count += RtTable->Next->TotalNum; + } + + if (Count == 0) { + return EFI_SUCCESS; + } + + Table = AllocatePool (sizeof (EFI_IP4_ROUTE_TABLE) * Count); + + if (Table == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Copy the route entry to EFI route table. Keep the order of + // route entry copied from most specific to default route. That + // is, interlevel the route entry from the instance's route area + // and those from the default route table's route area. + // + Count = 0; + + for (Index = IP4_MASK_MAX; Index >= 0; Index--) { + for (RtTable = IpInstance->RouteTable; RtTable != NULL; RtTable = RtTable->Next) { + NET_LIST_FOR_EACH (Entry, &(RtTable->RouteArea[Index])) { + RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link); + + EFI_IP4 (Table[Count].SubnetAddress) = HTONL (RtEntry->Dest & RtEntry->Netmask); + EFI_IP4 (Table[Count].SubnetMask) = HTONL (RtEntry->Netmask); + EFI_IP4 (Table[Count].GatewayAddress) = HTONL (RtEntry->NextHop); + + Count++; + } + } + } + + IpInstance->EfiRouteTable = Table; + IpInstance->EfiRouteCount = Count; + return EFI_SUCCESS; +} diff --git a/NetworkPkg/Ip4Dxe/Ip4Route.h b/NetworkPkg/Ip4Dxe/Ip4Route.h new file mode 100644 index 000000000..4b0b5282a --- /dev/null +++ b/NetworkPkg/Ip4Dxe/Ip4Route.h @@ -0,0 +1,225 @@ +/** @file + EFI IP4 route table and route cache table defintions. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP4_ROUTE_H__ +#define __EFI_IP4_ROUTE_H__ + +#include "Ip4Common.h" + +#define IP4_DIRECT_ROUTE 0x00000001 + +#define IP4_ROUTE_CACHE_HASH_VALUE 31 +#define IP4_ROUTE_CACHE_MAX 64 // Max NO. of cache entry per hash bucket + +#define IP4_ROUTE_CACHE_HASH(Dst, Src) (((Dst) ^ (Src)) % IP4_ROUTE_CACHE_HASH_VALUE) + +/// +/// The route entry in the route table. Dest/Netmask is the destion +/// network. The nexthop is the gateway to send the packet to in +/// order to reach the Dest/Netmask. If the Flag has IP4_DIRECT_ROUTE +/// on, the gateway is the destination of the IP packet itself. Route +/// enties of the connected network have the flag on. +/// +typedef struct { + LIST_ENTRY Link; + INTN RefCnt; + IP4_ADDR Dest; + IP4_ADDR Netmask; + IP4_ADDR NextHop; + UINT32 Flag; +} IP4_ROUTE_ENTRY; + +/// +/// The route cache entry. The route cache entry is optional. +/// But it is necessary to support the ICMP redirect message. +/// Check Ip4ProcessIcmpRedirect for information. +/// +/// The cache entry field Tag is used to tag all the route +/// cache entry spawned from a route table entry. This makes +/// it simple to delete all the route cache entries from a +/// to-be-deleted route entry. +/// +typedef struct { + LIST_ENTRY Link; + INTN RefCnt; + IP4_ADDR Dest; + IP4_ADDR Src; + IP4_ADDR NextHop; + UINTN Tag; +} IP4_ROUTE_CACHE_ENTRY; + +/// +/// The route cache table is organized as a hash table. Each +/// IP4 route table has a embedded route cache. For now the +/// route cache and route table are binded togehter. But keep +/// the route cache a seperated structure in case we want to +/// detach them later. +/// +typedef struct { + LIST_ENTRY CacheBucket[IP4_ROUTE_CACHE_HASH_VALUE]; +} IP4_ROUTE_CACHE; + +/// +/// Each IP4 instance has its own route table. Each ServiceBinding +/// instance has a default route table and default address. +/// +/// All the route table entries with the same mask are linked +/// together in one route area. For example, RouteArea[0] contains +/// the default routes. A route table also contains a route cache. +/// +typedef struct _IP4_ROUTE_TABLE IP4_ROUTE_TABLE; + +struct _IP4_ROUTE_TABLE { + INTN RefCnt; + UINT32 TotalNum; + LIST_ENTRY RouteArea[IP4_MASK_NUM]; + IP4_ROUTE_TABLE *Next; + IP4_ROUTE_CACHE Cache; +}; + +/** + Create an empty route table, includes its internal route cache + + @return NULL if failed to allocate memory for the route table, otherwise + the point to newly created route table. + +**/ +IP4_ROUTE_TABLE * +Ip4CreateRouteTable ( + VOID + ); + +/** + Free the route table and its associated route cache. Route + table is reference counted. + + @param[in] RtTable The route table to free. + +**/ +VOID +Ip4FreeRouteTable ( + IN IP4_ROUTE_TABLE *RtTable + ); + +/** + Add a route entry to the route table. All the IP4_ADDRs are in + host byte order. + + @param[in, out] RtTable Route table to add route to + @param[in] Dest The destination of the network + @param[in] Netmask The netmask of the destination + @param[in] Gateway The next hop address + + @retval EFI_ACCESS_DENIED The same route already exists + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the entry + @retval EFI_SUCCESS The route is added successfully. + +**/ +EFI_STATUS +Ip4AddRoute ( + IN OUT IP4_ROUTE_TABLE *RtTable, + IN IP4_ADDR Dest, + IN IP4_ADDR Netmask, + IN IP4_ADDR Gateway + ); + +/** + Remove a route entry and all the route caches spawn from it. + + @param RtTable The route table to remove the route from + @param Dest The destination network + @param Netmask The netmask of the Dest + @param Gateway The next hop address + + @retval EFI_SUCCESS The route entry is successfully removed + @retval EFI_NOT_FOUND There is no route entry in the table with that + properity. + +**/ +EFI_STATUS +Ip4DelRoute ( + IN OUT IP4_ROUTE_TABLE *RtTable, + IN IP4_ADDR Dest, + IN IP4_ADDR Netmask, + IN IP4_ADDR Gateway + ); + +/** + Find a route cache with the dst and src. This is used by ICMP + redirect messasge process. All kinds of redirect is treated as + host redirect according to RFC1122. So, only route cache entries + are modified according to the ICMP redirect message. + + @param[in] RtTable The route table to search the cache for + @param[in] Dest The destination address + @param[in] Src The source address + + @return NULL if no route entry to the (Dest, Src). Otherwise the point + to the correct route cache entry. + +**/ +IP4_ROUTE_CACHE_ENTRY * +Ip4FindRouteCache ( + IN IP4_ROUTE_TABLE *RtTable, + IN IP4_ADDR Dest, + IN IP4_ADDR Src + ); + +/** + Free the route cache entry. It is reference counted. + + @param RtCacheEntry The route cache entry to free. + +**/ +VOID +Ip4FreeRouteCacheEntry ( + IN IP4_ROUTE_CACHE_ENTRY *RtCacheEntry + ); + +/** + Search the route table to route the packet. Return/create a route + cache if there is a route to the destination. + + @param[in] RtTable The route table to search from + @param[in] Dest The destination address to search for + @param[in] Src The source address to search for + @param[in] SubnetMask The subnet mask of the Src address, this field is + used to check if the station is using /32 subnet. + @param[in] AlwaysTryDestAddr Always try to use the dest address as next hop even + though we can't find a matching route entry. This + field is only valid when using /32 subnet. + + @return NULL if failed to route packet, otherwise a route cache + entry that can be used to route packet. + +**/ +IP4_ROUTE_CACHE_ENTRY * +Ip4Route ( + IN IP4_ROUTE_TABLE *RtTable, + IN IP4_ADDR Dest, + IN IP4_ADDR Src, + IN IP4_ADDR SubnetMask, + IN BOOLEAN AlwaysTryDestAddr + ); + +/** + Build a EFI_IP4_ROUTE_TABLE to be returned to the caller of + GetModeData. The EFI_IP4_ROUTE_TABLE is clumsy to use in the + internal operation of the IP4 driver. + + @param[in] IpInstance The IP4 child that requests the route table. + + @retval EFI_SUCCESS The route table is successfully build + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the rotue table. + +**/ +EFI_STATUS +Ip4BuildEfiRouteTable ( + IN IP4_PROTOCOL *IpInstance + ); +#endif diff --git a/NetworkPkg/Ip6Dxe/ComponentName.c b/NetworkPkg/Ip6Dxe/ComponentName.c new file mode 100644 index 000000000..e57fe5e60 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/ComponentName.c @@ -0,0 +1,468 @@ +/** @file + Implementation of EFI_COMPONENT_NAME_PROTOCOL and + EFI_COMPONENT_NAME2_PROTOCOL protocol. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +// +// EFI Component Name Functions +// + +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Ip6ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Ip6ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +// +// EFI Component Name Protocol. +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gIp6ComponentName = { + Ip6ComponentNameGetDriverName, + Ip6ComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol. +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gIp6ComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) Ip6ComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) Ip6ComponentNameGetControllerName, + "en" +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mIp6DriverNameTable[] = { + { + "eng;en", + L"IP6 Network Service Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gIp6ControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Ip6ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mIp6DriverNameTable, + DriverName, + (BOOLEAN) (This == &gIp6ComponentName) + ); + +} + +/** + Update the component name for the IP6 child handle. + + @param Ip6[in] A pointer to the EFI_IP6_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateName ( + IN EFI_IP6_PROTOCOL *Ip6 + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[128]; + EFI_IP6_MODE_DATA Ip6ModeData; + UINTN Offset; + CHAR16 Address[sizeof"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"]; + + if (Ip6 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer. + // + Offset = 0; + Status = Ip6->GetModeData (Ip6, &Ip6ModeData, NULL, NULL); + if (!EFI_ERROR (Status)) { + if (Ip6ModeData.AddressList != NULL) { + FreePool (Ip6ModeData.AddressList); + } + + if (Ip6ModeData.GroupTable != NULL) { + FreePool (Ip6ModeData.GroupTable); + } + + if (Ip6ModeData.RouteTable != NULL) { + FreePool (Ip6ModeData.RouteTable); + } + + if (Ip6ModeData.NeighborCache != NULL) { + FreePool (Ip6ModeData.NeighborCache); + } + + if (Ip6ModeData.PrefixTable != NULL) { + FreePool (Ip6ModeData.PrefixTable); + } + + if (Ip6ModeData.IcmpTypeList != NULL) { + FreePool (Ip6ModeData.IcmpTypeList); + } + } + + if (!EFI_ERROR (Status) && Ip6ModeData.IsStarted) { + Status = NetLibIp6ToStr (&Ip6ModeData.ConfigData.StationAddress, Address, sizeof(Address)); + if (EFI_ERROR (Status)) { + return Status; + } + Offset += UnicodeSPrint ( + HandleName, + sizeof(HandleName), + L"IPv6(StationAddress=%s, ", + Address + ); + Status = NetLibIp6ToStr (&Ip6ModeData.ConfigData.DestinationAddress, Address, sizeof(Address)); + if (EFI_ERROR (Status)) { + return Status; + } + UnicodeSPrint ( + HandleName + Offset, + sizeof(HandleName) - Offset * sizeof (CHAR16), + L"DestinationAddress=%s)", + Address + ); + } else if (!Ip6ModeData.IsStarted) { + UnicodeSPrint (HandleName, sizeof(HandleName), L"IPv6(Not started)"); + } else { + UnicodeSPrint (HandleName, sizeof(HandleName), L"IPv6(%r)", Status); + } + + if (gIp6ControllerNameTable != NULL) { + FreeUnicodeStringTable (gIp6ControllerNameTable); + gIp6ControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gIp6ComponentName.SupportedLanguages, + &gIp6ControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gIp6ComponentName2.SupportedLanguages, + &gIp6ControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is being managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Ip6ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_IP6_PROTOCOL *Ip6; + + // + // Only provide names for child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiManagedNetworkProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiIp6ProtocolGuid, + (VOID **)&Ip6, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateName (Ip6); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gIp6ControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gIp6ComponentName) + ); +} diff --git a/NetworkPkg/Ip6Dxe/Ip6Common.c b/NetworkPkg/Ip6Dxe/Ip6Common.c new file mode 100644 index 000000000..4ae04f258 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Common.c @@ -0,0 +1,667 @@ +/** @file + The implementation of common functions shared by IP6 driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +/** + Build a array of EFI_IP6_ADDRESS_INFO to be returned to the caller. The number + of EFI_IP6_ADDRESS_INFO is also returned. If AddressList is NULL, + only the address count is returned. + + @param[in] IpSb The IP6 service binding instance. + @param[out] AddressCount The number of returned addresses. + @param[out] AddressList The pointer to the array of EFI_IP6_ADDRESS_INFO. + This is an optional parameter. + + + @retval EFI_SUCCESS The address array successfully built. + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the address info. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + +**/ +EFI_STATUS +Ip6BuildEfiAddressList ( + IN IP6_SERVICE *IpSb, + OUT UINT32 *AddressCount, + OUT EFI_IP6_ADDRESS_INFO **AddressList OPTIONAL + ) +{ + UINT32 Count; + LIST_ENTRY *Entry; + EFI_IP6_ADDRESS_INFO *EfiAddrInfo; + IP6_ADDRESS_INFO *AddrInfo; + + if (AddressCount == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (IpSb->LinkLocalOk) { + Count = 1 + IpSb->DefaultInterface->AddressCount; + } else { + Count = 0; + } + + *AddressCount = Count; + + if ((AddressList == NULL) || (Count == 0)) { + return EFI_SUCCESS; + } + + if (*AddressList == NULL) { + *AddressList = AllocatePool (sizeof (EFI_IP6_ADDRESS_INFO) * Count); + if (*AddressList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + + EfiAddrInfo = *AddressList; + + IP6_COPY_ADDRESS (&EfiAddrInfo->Address, &IpSb->LinkLocalAddr); + EfiAddrInfo->PrefixLength = IP6_LINK_LOCAL_PREFIX_LENGTH; + + EfiAddrInfo++; + Count = 1; + + NET_LIST_FOR_EACH (Entry, &IpSb->DefaultInterface->AddressList) { + AddrInfo = NET_LIST_USER_STRUCT_S (Entry, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE); + + IP6_COPY_ADDRESS (&EfiAddrInfo->Address, &AddrInfo->Address); + EfiAddrInfo->PrefixLength = AddrInfo->PrefixLength; + + EfiAddrInfo++; + Count++; + } + + ASSERT (Count == *AddressCount); + + return EFI_SUCCESS; +} + +/** + Generate the multicast addresses identify the group of all IPv6 nodes or IPv6 + routers defined in RFC4291. + + All Nodes Addresses: FF01::1, FF02::1. + All Router Addresses: FF01::2, FF02::2, FF05::2. + + @param[in] Router If TRUE, generate all routers addresses, + else generate all node addresses. + @param[in] Scope interface-local(1), link-local(2), or site-local(5) + @param[out] Ip6Addr The generated multicast address. + + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_SUCCESS The address is generated. + +**/ +EFI_STATUS +Ip6SetToAllNodeMulticast ( + IN BOOLEAN Router, + IN UINT8 Scope, + OUT EFI_IPv6_ADDRESS *Ip6Addr + ) +{ + if (Ip6Addr == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (!Router && Scope == IP6_SITE_LOCAL_SCOPE) { + return EFI_INVALID_PARAMETER; + } + + ZeroMem (Ip6Addr, sizeof (EFI_IPv6_ADDRESS)); + Ip6Addr->Addr[0] = 0xFF; + Ip6Addr->Addr[1] = Scope; + + if (!Router) { + Ip6Addr->Addr[15] = 0x1; + } else { + Ip6Addr->Addr[15] = 0x2; + } + + return EFI_SUCCESS; +} + +/** + This function converts MAC address to 64 bits interface ID according to RFC4291 + and returns the interface ID. Currently only 48-bit MAC address is supported by + this function. + + @param[in, out] IpSb The IP6 service binding instance. + + @retval NULL The operation fails. + @return Pointer to the generated interface ID. + +**/ +UINT8 * +Ip6CreateInterfaceID ( + IN OUT IP6_SERVICE *IpSb + ) +{ + UINT8 InterfaceId[8]; + UINT8 Byte; + EFI_MAC_ADDRESS *MacAddr; + UINT32 AddrLen; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + AddrLen = IpSb->SnpMode.HwAddressSize; + + // + // Currently only IEEE 802 48-bit MACs are supported to create link local address. + // + if (AddrLen != IP6_MAC_LEN || IpSb->InterfaceIdLen != IP6_IF_ID_LEN) { + return NULL; + } + + MacAddr = &IpSb->SnpMode.CurrentAddress; + + // + // Convert MAC address to 64 bits interface ID according to Appendix A of RFC4291: + // 1. Insert 0xFFFE to the middle + // 2. Invert the universal/local bit - bit 6 in network order + // + CopyMem (InterfaceId, MacAddr, 3); + InterfaceId[3] = 0xFF; + InterfaceId[4] = 0xFE; + CopyMem (&InterfaceId[5], &MacAddr->Addr[3], 3); + + Byte = (UINT8) (InterfaceId[0] & IP6_U_BIT); + if (Byte == IP6_U_BIT) { + InterfaceId[0] &= ~IP6_U_BIT; + } else { + InterfaceId[0] |= IP6_U_BIT; + } + + // + // Return the interface ID. + // + return AllocateCopyPool (IpSb->InterfaceIdLen, InterfaceId); +} + +/** + This function creates link-local address from interface identifier. The + interface identifier is normally created from MAC address. It might be manually + configured by administrator if the link-local address created from MAC address + is a duplicate address. + + @param[in, out] IpSb The IP6 service binding instance. + + @retval NULL If the operation fails. + @return The generated Link Local address, in network order. + +**/ +EFI_IPv6_ADDRESS * +Ip6CreateLinkLocalAddr ( + IN OUT IP6_SERVICE *IpSb + ) +{ + EFI_IPv6_ADDRESS *Ip6Addr; + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + UINTN DataSize; + EFI_IP6_CONFIG_INTERFACE_ID InterfaceId; + EFI_STATUS Status; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + if (IpSb->InterfaceId != NULL) { + FreePool (IpSb->InterfaceId); + } + + // + // Get the interface id if it is manully configured. + // + Ip6Config = &IpSb->Ip6ConfigInstance.Ip6Config; + DataSize = sizeof (EFI_IP6_CONFIG_INTERFACE_ID); + ZeroMem (&InterfaceId, DataSize); + + Status = Ip6Config->GetData ( + Ip6Config, + Ip6ConfigDataTypeAltInterfaceId, + &DataSize, + &InterfaceId + ); + if (Status == EFI_NOT_FOUND) { + // + // Since the interface id is not configured, generate the interface id from + // MAC address. + // + IpSb->InterfaceId = Ip6CreateInterfaceID (IpSb); + if (IpSb->InterfaceId == NULL) { + return NULL; + } + + CopyMem (&InterfaceId, IpSb->InterfaceId, IpSb->InterfaceIdLen); + // + // Record the interface id. + // + Status = Ip6Config->SetData ( + Ip6Config, + Ip6ConfigDataTypeAltInterfaceId, + DataSize, + &InterfaceId + ); + if (EFI_ERROR (Status)) { + FreePool (IpSb->InterfaceId); + IpSb->InterfaceId = NULL; + return NULL; + } + } else if (!EFI_ERROR (Status)) { + IpSb->InterfaceId = AllocateCopyPool (DataSize, &InterfaceId); + if (IpSb->InterfaceId == NULL) { + return NULL; + } + } else { + return NULL; + } + + // + // Append FE80::/64 to the left of IPv6 address then return. + // + Ip6Addr = AllocateZeroPool (sizeof (EFI_IPv6_ADDRESS)); + if (Ip6Addr == NULL) { + FreePool (IpSb->InterfaceId); + IpSb->InterfaceId = NULL; + return NULL; + } + + CopyMem (&Ip6Addr->Addr[8], IpSb->InterfaceId, IpSb->InterfaceIdLen); + Ip6Addr->Addr[1] = 0x80; + Ip6Addr->Addr[0] = 0xFE; + + return Ip6Addr; +} + +/** + Compute the solicited-node multicast address for an unicast or anycast address, + by taking the low-order 24 bits of this address, and appending those bits to + the prefix FF02:0:0:0:0:1:FF00::/104. + + @param[in] Ip6Addr The unicast or anycast address, in network order. + @param[out] MulticastAddr The generated solicited-node multicast address, + in network order. + +**/ +VOID +Ip6CreateSNMulticastAddr ( + IN EFI_IPv6_ADDRESS *Ip6Addr, + OUT EFI_IPv6_ADDRESS *MulticastAddr + ) +{ + ASSERT (Ip6Addr != NULL && MulticastAddr != NULL); + + ZeroMem (MulticastAddr, sizeof (EFI_IPv6_ADDRESS)); + + MulticastAddr->Addr[0] = 0xFF; + MulticastAddr->Addr[1] = 0x02; + MulticastAddr->Addr[11] = 0x1; + MulticastAddr->Addr[12] = 0xFF; + + CopyMem (&MulticastAddr->Addr[13], &Ip6Addr->Addr[13], 3); +} + +/** + Insert a node IP6_ADDRESS_INFO to an IP6 interface. + + @param[in, out] IpIf Points to an IP6 interface. + @param[in] AddrInfo Points to IP6_ADDRESS_INFO + +**/ +VOID +Ip6AddAddr ( + IN OUT IP6_INTERFACE *IpIf, + IN IP6_ADDRESS_INFO *AddrInfo + ) +{ + InsertHeadList (&IpIf->AddressList, &AddrInfo->Link); + IpIf->AddressCount++; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +Ip6DestroyChildEntryByAddr ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + IP6_PROTOCOL *Instance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + EFI_IPv6_ADDRESS *Address; + + Instance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE); + ServiceBinding = ((IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT*) Context)->ServiceBinding; + Address = ((IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT*) Context)->Address; + + if ((Instance->State == IP6_STATE_CONFIGED) && EFI_IP6_EQUAL (&Instance->ConfigData.StationAddress, Address)) { + return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle); + } + + return EFI_SUCCESS; +} + +/** + Destroy the IP instance if its StationAddress is removed. It is the help function + for Ip6RemoveAddr(). + + @param[in, out] IpSb Points to an IP6 service binding instance. + @param[in] Address The to be removed address + +**/ +VOID +Ip6DestroyInstanceByAddress ( + IN OUT IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Address + ) +{ + LIST_ENTRY *List; + IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT Context; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + List = &IpSb->Children; + Context.ServiceBinding = &IpSb->ServiceBinding; + Context.Address = Address; + NetDestroyLinkList ( + List, + Ip6DestroyChildEntryByAddr, + &Context, + NULL + ); +} + +/** + Remove the IPv6 address from the address list node points to IP6_ADDRESS_INFO. + + This function removes the matching IPv6 addresses from the address list and + adjusts the address count of the address list. If IpSb is not NULL, this function + calls Ip6LeaveGroup to see whether it should call Mnp->Groups() to remove the + its solicited-node multicast MAC address from the filter list and sends out + a Multicast Listener Done. If Prefix is NULL, all address in the address list + will be removed. If Prefix is not NULL, the address that matching the Prefix + with PrefixLength in the address list will be removed. + + @param[in] IpSb NULL or points to IP6 service binding instance. + @param[in, out] AddressList Address list array. + @param[in, out] AddressCount The count of addresses in address list array. + @param[in] Prefix NULL or an IPv6 address prefix. + @param[in] PrefixLength The length of Prefix. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_FOUND The address matching the Prefix with PrefixLength + cannot be found in the address list. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + +**/ +EFI_STATUS +Ip6RemoveAddr ( + IN IP6_SERVICE *IpSb OPTIONAL, + IN OUT LIST_ENTRY *AddressList, + IN OUT UINT32 *AddressCount, + IN EFI_IPv6_ADDRESS *Prefix OPTIONAL, + IN UINT8 PrefixLength + ) +{ + EFI_STATUS Status; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP6_ADDRESS_INFO *AddrInfo; + EFI_IPv6_ADDRESS SnMCastAddr; + + if (IsListEmpty (AddressList) || *AddressCount < 1 || PrefixLength > IP6_PREFIX_MAX) { + return EFI_INVALID_PARAMETER; + } + + Status = EFI_NOT_FOUND; + + NET_LIST_FOR_EACH_SAFE (Entry, Next, AddressList) { + AddrInfo = NET_LIST_USER_STRUCT_S (Entry, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE); + + if (Prefix == NULL || + (PrefixLength == 128 && EFI_IP6_EQUAL (Prefix, &AddrInfo->Address)) || + (PrefixLength == AddrInfo->PrefixLength && NetIp6IsNetEqual (Prefix, &AddrInfo->Address, PrefixLength)) + ) { + if (IpSb != NULL) { + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + Ip6CreateSNMulticastAddr (&AddrInfo->Address, &SnMCastAddr); + Ip6LeaveGroup (IpSb, &SnMCastAddr); + + // + // Destroy any instance who is using the dying address as the source address. + // + Ip6DestroyInstanceByAddress (IpSb, &AddrInfo->Address); + } + + RemoveEntryList (Entry); + FreePool (AddrInfo); + (*AddressCount)--; + + Status = EFI_SUCCESS; + } + } + + return Status; +} + +/** + Check whether the incoming Ipv6 address is a solicited-node multicast address. + + @param[in] Ip6 Ip6 address, in network order. + + @retval TRUE Yes, solicited-node multicast address + @retval FALSE No + +**/ +BOOLEAN +Ip6IsSNMulticastAddr ( + IN EFI_IPv6_ADDRESS *Ip6 + ) +{ + EFI_IPv6_ADDRESS Sn; + BOOLEAN Flag; + + Ip6CreateSNMulticastAddr (Ip6, &Sn); + Flag = FALSE; + + if (CompareMem (Sn.Addr, Ip6->Addr, 13) == 0) { + Flag = TRUE; + } + + return Flag; +} + +/** + Check whether the incoming IPv6 address is one of the maintained addresses in + the IP6 service binding instance. + + @param[in] IpSb Points to a IP6 service binding instance. + @param[in] Address The IP6 address to be checked. + @param[out] Interface If not NULL, output the IP6 interface which + maintains the Address. + @param[out] AddressInfo If not NULL, output the IP6 address information + of the Address. + + @retval TRUE Yes, it is one of the maintained address. + @retval FALSE No, it is not one of the maintained address. + +**/ +BOOLEAN +Ip6IsOneOfSetAddress ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Address, + OUT IP6_INTERFACE **Interface OPTIONAL, + OUT IP6_ADDRESS_INFO **AddressInfo OPTIONAL + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Entry2; + IP6_INTERFACE *IpIf; + IP6_ADDRESS_INFO *TmpAddressInfo; + + // + // Check link-local address first + // + if (IpSb->LinkLocalOk && EFI_IP6_EQUAL (&IpSb->LinkLocalAddr, Address)) { + if (Interface != NULL) { + *Interface = IpSb->DefaultInterface; + } + + if (AddressInfo != NULL) { + *AddressInfo = NULL; + } + + return TRUE; + } + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE); + + NET_LIST_FOR_EACH (Entry2, &IpIf->AddressList) { + TmpAddressInfo = NET_LIST_USER_STRUCT_S (Entry2, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE); + + if (EFI_IP6_EQUAL (&TmpAddressInfo->Address, Address)) { + if (Interface != NULL) { + *Interface = IpIf; + } + + if (AddressInfo != NULL) { + *AddressInfo = TmpAddressInfo; + } + + return TRUE; + } + } + } + + return FALSE; +} + +/** + Check whether the incoming MAC address is valid. + + @param[in] IpSb Points to a IP6 service binding instance. + @param[in] LinkAddress The MAC address. + + @retval TRUE Yes, it is valid. + @retval FALSE No, it is not valid. + +**/ +BOOLEAN +Ip6IsValidLinkAddress ( + IN IP6_SERVICE *IpSb, + IN EFI_MAC_ADDRESS *LinkAddress + ) +{ + UINT32 Index; + + // + // TODO: might be updated later to be more acceptable. + // + for (Index = IpSb->SnpMode.HwAddressSize; Index < sizeof (EFI_MAC_ADDRESS); Index++) { + if (LinkAddress->Addr[Index] != 0) { + return FALSE; + } + } + + return TRUE; +} + +/** + Copy the PrefixLength bits from Src to Dest. + + @param[out] Dest A pointer to the buffer to copy to. + @param[in] Src A pointer to the buffer to copy from. + @param[in] PrefixLength The number of bits to copy. + +**/ +VOID +Ip6CopyAddressByPrefix ( + OUT EFI_IPv6_ADDRESS *Dest, + IN EFI_IPv6_ADDRESS *Src, + IN UINT8 PrefixLength + ) +{ + UINT8 Byte; + UINT8 Bit; + UINT8 Mask; + + ASSERT (Dest != NULL && Src != NULL); + ASSERT (PrefixLength <= IP6_PREFIX_MAX); + + Byte = (UINT8) (PrefixLength / 8); + Bit = (UINT8) (PrefixLength % 8); + + ZeroMem (Dest, sizeof (EFI_IPv6_ADDRESS)); + + CopyMem (Dest, Src, Byte); + + if (Bit > 0) { + Mask = (UINT8) (0xFF << (8 - Bit)); + ASSERT (Byte < 16); + Dest->Addr[Byte] = (UINT8) (Src->Addr[Byte] & Mask); + } +} + +/** + Get the MAC address for a multicast IP address. Call + Mnp's McastIpToMac to find the MAC address instead of + hard-coding the NIC to be Ethernet. + + @param[in] Mnp The Mnp instance to get the MAC address. + @param[in] Multicast The multicast IP address to translate. + @param[out] Mac The buffer to hold the translated address. + + @retval EFI_SUCCESS The multicast IP successfully + translated to a multicast MAC address. + @retval Other The address is not converted because an error occurred. + +**/ +EFI_STATUS +Ip6GetMulticastMac ( + IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp, + IN EFI_IPv6_ADDRESS *Multicast, + OUT EFI_MAC_ADDRESS *Mac + ) +{ + EFI_IP_ADDRESS EfiIp; + + IP6_COPY_ADDRESS (&EfiIp.v6, Multicast); + + return Mnp->McastIpToMac (Mnp, TRUE, &EfiIp, Mac); +} + +/** + Convert the multibyte field in IP header's byter order. + In spite of its name, it can also be used to convert from + host to network byte order. + + @param[in, out] Head The IP head to convert. + + @return Point to the converted IP head. + +**/ +EFI_IP6_HEADER * +Ip6NtohHead ( + IN OUT EFI_IP6_HEADER *Head + ) +{ + Head->FlowLabelL = NTOHS (Head->FlowLabelL); + Head->PayloadLength = NTOHS (Head->PayloadLength); + + return Head; +} + diff --git a/NetworkPkg/Ip6Dxe/Ip6Common.h b/NetworkPkg/Ip6Dxe/Ip6Common.h new file mode 100644 index 000000000..18723f84e --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Common.h @@ -0,0 +1,312 @@ +/** @file + Common definition and functions for IP6 driver. + + Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP6_COMMON_H__ +#define __EFI_IP6_COMMON_H__ + +#define IP6_LINK_EQUAL(Mac1, Mac2) (CompareMem ((Mac1), (Mac2), sizeof (EFI_MAC_ADDRESS)) == 0) + +// +// Convert the Microsecond to second. IP transmit/receive time is +// in the unit of microsecond. IP ticks once per second. +// +#define IP6_US_TO_SEC(Us) (((Us) + 999999) / 1000000) + +#define IP6_ETHER_PROTO 0x86DD + +#define IP6_MAC_LEN 6 +#define IP6_IF_ID_LEN 8 + +#define IP6_INTERFACE_LOCAL_SCOPE 1 +#define IP6_LINK_LOCAL_SCOPE 2 +#define IP6_SITE_LOCAL_SCOPE 5 + +#define IP6_INFINIT_LIFETIME 0xFFFFFFFF + +#define IP6_HOP_LIMIT 255 +// +// Make it to 64 since all 54 bits are zero. +// +#define IP6_LINK_LOCAL_PREFIX_LENGTH 64 + +#define IP6_TIMER_INTERVAL_IN_MS 100 +#define IP6_ONE_SECOND_IN_MS 1000 + +// +// The packet is received as link level broadcast/multicast/promiscuous. +// +#define IP6_LINK_BROADCAST 0x00000001 +#define IP6_LINK_MULTICAST 0x00000002 +#define IP6_LINK_PROMISC 0x00000004 + +#define IP6_U_BIT 0x02 + +typedef enum { + Ip6Promiscuous = 1, + Ip6Unicast, + Ip6Multicast, + Ip6AnyCast +} IP6_ADDRESS_TYPE; + +typedef struct { + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + EFI_IPv6_ADDRESS *Address; +} IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT; + +typedef struct _IP6_INTERFACE IP6_INTERFACE; +typedef struct _IP6_PROTOCOL IP6_PROTOCOL; +typedef struct _IP6_SERVICE IP6_SERVICE; +typedef struct _IP6_ADDRESS_INFO IP6_ADDRESS_INFO; + +/** + Build a array of EFI_IP6_ADDRESS_INFO to be returned to the caller. The number + of EFI_IP6_ADDRESS_INFO is also returned. If AddressList is NULL, + only the address count is returned. + + @param[in] IpSb The IP6 service binding instance. + @param[out] AddressCount The number of returned addresses. + @param[out] AddressList The pointer to the array of EFI_IP6_ADDRESS_INFO. + This is an optional parameter. + + + @retval EFI_SUCCESS The address array is successfully build + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the address info. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + +**/ +EFI_STATUS +Ip6BuildEfiAddressList ( + IN IP6_SERVICE *IpSb, + OUT UINT32 *AddressCount, + OUT EFI_IP6_ADDRESS_INFO **AddressList OPTIONAL + ); + +/** + Generate the multicast addresses identify the group of all IPv6 nodes or IPv6 + routers defined in RFC4291. + + All Nodes Addresses: FF01::1, FF02::1. + All Router Addresses: FF01::2, FF02::2, FF05::2. + + @param[in] Router If TRUE, generate all routers addresses, + else generate all node addresses. + @param[in] Scope interface-local(1), link-local(2), or site-local(5) + @param[out] Ip6Addr The generated multicast address. + + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_SUCCESS The address is generated. + +**/ +EFI_STATUS +Ip6SetToAllNodeMulticast ( + IN BOOLEAN Router, + IN UINT8 Scope, + OUT EFI_IPv6_ADDRESS *Ip6Addr + ); + +/** + This function converts MAC address to 64 bits interface ID according to RFC4291 + and returns the interface ID. Currently only 48-bit MAC address is supported by + this function. + + @param[in, out] IpSb The IP6 service binding instance. + + @retval NULL The operation fails. + @return Pointer to the generated interface ID. + +**/ +UINT8 * +Ip6CreateInterfaceID ( + IN OUT IP6_SERVICE *IpSb + ); + +/** + This function creates link-local address from interface identifier. The + interface identifier is normally created from MAC address. It might be manually + configured by administrator if the link-local address created from MAC address + is a duplicate address. + + @param[in, out] IpSb The IP6 service binding instance. + + @retval NULL If the operation fails. + @return The generated Link Local address, in network order. + +**/ +EFI_IPv6_ADDRESS * +Ip6CreateLinkLocalAddr ( + IN OUT IP6_SERVICE *IpSb + ); + +/** + Compute the solicited-node multicast address for an unicast or anycast address, + by taking the low-order 24 bits of this address, and appending those bits to + the prefix FF02:0:0:0:0:1:FF00::/104. + + @param Ip6Addr The unicast or anycast address, in network order. + @param MulticastAddr The generated solicited-node multicast address, + in network order. + +**/ +VOID +Ip6CreateSNMulticastAddr ( + IN EFI_IPv6_ADDRESS *Ip6Addr, + OUT EFI_IPv6_ADDRESS *MulticastAddr + ); + +/** + Check whether the incoming Ipv6 address is a solicited-node multicast address. + + @param[in] Ip6 Ip6 address, in network order. + + @retval TRUE Yes, solicited-node multicast address + @retval FALSE No + +**/ +BOOLEAN +Ip6IsSNMulticastAddr ( + IN EFI_IPv6_ADDRESS *Ip6 + ); + +/** + Check whether the incoming IPv6 address is one of the maintained address in + the IP6 service binding instance. + + @param[in] IpSb Points to a IP6 service binding instance + @param[in] Address The IP6 address to be checked. + @param[out] Interface If not NULL, output the IP6 interface which + maintains the Address. + @param[out] AddressInfo If not NULL, output the IP6 address information + of the Address. + + @retval TRUE Yes, it is one of the maintained addresses. + @retval FALSE No, it is not one of the maintained addresses. + +**/ +BOOLEAN +Ip6IsOneOfSetAddress ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Address, + OUT IP6_INTERFACE **Interface OPTIONAL, + OUT IP6_ADDRESS_INFO **AddressInfo OPTIONAL + ); + +/** + Check whether the incoming MAC address is valid. + + @param[in] IpSb Points to a IP6 service binding instance. + @param[in] LinkAddress The MAC address. + + @retval TRUE Yes, it is valid. + @retval FALSE No, it is not valid. + +**/ +BOOLEAN +Ip6IsValidLinkAddress ( + IN IP6_SERVICE *IpSb, + IN EFI_MAC_ADDRESS *LinkAddress + ); + + +/** + Copy the PrefixLength bits from Src to Dest. + + @param[out] Dest A pointer to the buffer to copy to. + @param[in] Src A pointer to the buffer to copy from. + @param[in] PrefixLength The number of bits to copy. + +**/ +VOID +Ip6CopyAddressByPrefix ( + OUT EFI_IPv6_ADDRESS *Dest, + IN EFI_IPv6_ADDRESS *Src, + IN UINT8 PrefixLength + ); + +/** + Insert a node IP6_ADDRESS_INFO to an IP6 interface. + + @param[in, out] IpIf Points to an IP6 interface. + @param[in] AddrInfo Points to an IP6_ADDRESS_INFO. + +**/ +VOID +Ip6AddAddr ( + IN OUT IP6_INTERFACE *IpIf, + IN IP6_ADDRESS_INFO *AddrInfo + ); + +/** + Remove the IPv6 address from the address list node points to IP6_ADDRESS_INFO. + + This function removes the matching IPv6 addresses from the address list and + adjusts the address count of the address list. If IpSb is not NULL, this function + calls Ip6LeaveGroup to see whether it should call Mnp->Groups() to remove the + its solicited-node multicast MAC address from the filter list and sends out + a Multicast Listener Done. If Prefix is NULL, all address in the address list + will be removed. If Prefix is not NULL, the address that matching the Prefix + with PrefixLength in the address list will be removed. + + @param[in] IpSb NULL or points to IP6 service binding instance. + @param[in, out] AddressList address list array + @param[in, out] AddressCount the count of addresses in address list array + @param[in] Prefix NULL or an IPv6 address prefix + @param[in] PrefixLength the length of Prefix + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_FOUND The address matching the Prefix with PrefixLength + cannot be found in address list. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + +**/ +EFI_STATUS +Ip6RemoveAddr ( + IN IP6_SERVICE *IpSb OPTIONAL, + IN OUT LIST_ENTRY *AddressList, + IN OUT UINT32 *AddressCount, + IN EFI_IPv6_ADDRESS *Prefix OPTIONAL, + IN UINT8 PrefixLength + ); + +/** + Get the MAC address for a multicast IP address. Call + Mnp's McastIpToMac to find the MAC address instead of + hard-coding the NIC to be Ethernet. + + @param[in] Mnp The Mnp instance to get the MAC address. + @param[in] Multicast The multicast IP address to translate. + @param[out] Mac The buffer to hold the translated address. + + @retval EFI_SUCCESS The multicast IP is successfully + translated to a multicast MAC address. + @retval Other The address is not converted because an error occurred. + +**/ +EFI_STATUS +Ip6GetMulticastMac ( + IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp, + IN EFI_IPv6_ADDRESS *Multicast, + OUT EFI_MAC_ADDRESS *Mac + ); + +/** + Convert the multibyte field in IP header's byter order. + In spite of its name, it can also be used to convert from + host to network byte order. + + @param[in, out] Head The IP head to convert. + + @return Point to the converted IP head. + +**/ +EFI_IP6_HEADER * +Ip6NtohHead ( + IN OUT EFI_IP6_HEADER *Head + ); + +#endif diff --git a/NetworkPkg/Ip6Dxe/Ip6Config.vfr b/NetworkPkg/Ip6Dxe/Ip6Config.vfr new file mode 100644 index 000000000..0d65299fc --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Config.vfr @@ -0,0 +1,172 @@ +/** @file + VFR file used by the IP6 configuration component. + + Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6NvData.h" + +#define EFI_NETWORK_DEVICE_CLASS 0x04 + +formset + guid = IP6_CONFIG_NVDATA_GUID, + title = STRING_TOKEN(STR_IP6_CONFIG_FORM_TITLE), + help = STRING_TOKEN(STR_IP6_CONFIG_FORM_HELP), + + varstore IP6_CONFIG_IFR_NVDATA, + name = IP6_CONFIG_IFR_NVDATA, + guid = IP6_CONFIG_NVDATA_GUID; + + form formid = FORMID_HEAD_FORM, + title = STRING_TOKEN(STR_IP6_DEVICE_FORM_TITLE); + + goto FORMID_MAIN_FORM, + prompt = STRING_TOKEN (STR_GET_CURRENT_SETTING), + help = STRING_TOKEN (STR_GET_CURRENT_SETTING_HELP), + flags = INTERACTIVE, + key = KEY_GET_CURRENT_SETTING; + + endform; + + form formid = FORMID_MAIN_FORM, + title = STRING_TOKEN(STR_IP6_DEVICE_FORM_TITLE); + + text + help = STRING_TOKEN(STR_IP6_INTERFACE_NAME_HELP), + text = STRING_TOKEN(STR_IP6_INTERFACE_NAME), + text = STRING_TOKEN(STR_IP6_INTERFACE_NAME_CONTENT); + + text + help = STRING_TOKEN(STR_IP6_INTERFACE_TYPE_HELP), + text = STRING_TOKEN(STR_IP6_INTERFACE_TYPE), + text = STRING_TOKEN(STR_IP6_INTERFACE_TYPE_CONTENT); + + text + help = STRING_TOKEN(STR_IP6_MAC_ADDRESS_HELP), + text = STRING_TOKEN(STR_IP6_MAC_ADDRESS), + text = STRING_TOKEN(STR_IP6_MAC_ADDRESS_CONTENT); + + text + help = STRING_TOKEN(STR_IP6_HOST_ADDRESS_HELP), + text = STRING_TOKEN(STR_IP6_HOST_ADDRESS), + text = STRING_TOKEN(STR_NULL); + + label HOST_ADDRESS_LABEL; + label LABEL_END; + + text + help = STRING_TOKEN(STR_IP6_ROUTE_TABLE_HELP), + text = STRING_TOKEN(STR_IP6_ROUTE_TABLE), + text = STRING_TOKEN(STR_NULL); + + label ROUTE_TABLE_LABEL; + label LABEL_END; + + text + help = STRING_TOKEN(STR_IP6_GATEWAY_ADDRESS_HELP), + text = STRING_TOKEN(STR_IP6_GATEWAY_ADDRESS), + text = STRING_TOKEN(STR_NULL); + + label GATEWAY_ADDRESS_LABEL; + label LABEL_END; + + text + help = STRING_TOKEN(STR_IP6_DNS_ADDRESS_HELP), + text = STRING_TOKEN(STR_IP6_DNS_ADDRESS), + text = STRING_TOKEN(STR_NULL); + + label DNS_ADDRESS_LABEL; + label LABEL_END; + + string varid = IP6_CONFIG_IFR_NVDATA.InterfaceId, + prompt = STRING_TOKEN(STR_IP6_INTERFACE_ID), + help = STRING_TOKEN(STR_IP6_INTERFACE_ID_HELP), + flags = INTERACTIVE, + key = KEY_INTERFACE_ID, + minsize = INTERFACE_ID_STR_MIN_SIZE, + maxsize = INTERFACE_ID_STR_MAX_SIZE, + endstring; + + numeric varid = IP6_CONFIG_IFR_NVDATA.DadTransmitCount, + prompt = STRING_TOKEN(STR_IP6_DAD_TRANSMIT_COUNT), + help = STRING_TOKEN(STR_IP6_DAD_TRANSMIT_COUNT_HELP), + flags = 0, + minimum = 0, + maximum = DAD_MAX_TRANSMIT_COUNT, + step = 0, + endnumeric; + + oneof varid = IP6_CONFIG_IFR_NVDATA.Policy, + prompt = STRING_TOKEN(STR_POLICY_TYPE_PROMPT), + help = STRING_TOKEN(STR_POLICY_TYPE_HELP), + option text = STRING_TOKEN(STR_POLICY_TYPE_AUTO), value = IP6_POLICY_AUTO, flags = DEFAULT; + option text = STRING_TOKEN(STR_POLICY_TYPE_MANUAL), value = IP6_POLICY_MANUAL, flags = 0; + endoneof; + + subtitle text = STRING_TOKEN(STR_NULL); + + suppressif ideqval IP6_CONFIG_IFR_NVDATA.Policy == IP6_POLICY_AUTO; + goto FORMID_MANUAL_CONFIG_FORM, + prompt = STRING_TOKEN(STR_IP6_AD_CONFIG_FORM), + help = STRING_TOKEN(STR_IP6_AD_CONFIG_FORM_HELP), + flags = 0; + subtitle text = STRING_TOKEN(STR_NULL); + endif; + + text + help = STRING_TOKEN (STR_SAVE_CHANGES_HELP), + text = STRING_TOKEN (STR_SAVE_CHANGES), + flags = INTERACTIVE, + key = KEY_SAVE_CHANGES; + + endform; + + form formid = FORMID_MANUAL_CONFIG_FORM, + title = STRING_TOKEN(STR_IP6_AD_CONFIG_FORM); + + string varid = IP6_CONFIG_IFR_NVDATA.ManualAddress, + prompt = STRING_TOKEN(STR_IP6_MANUAL_ADDRESS), + help = STRING_TOKEN(STR_IP6_MANUAL_ADDRESS_HELP), + flags = INTERACTIVE, + key = KEY_MANUAL_ADDRESS, + minsize = ADDRESS_STR_MIN_SIZE, + maxsize = ADDRESS_STR_MAX_SIZE, + endstring; + + string varid = IP6_CONFIG_IFR_NVDATA.GatewayAddress, + prompt = STRING_TOKEN(STR_IP6_NEW_GATEWAY_ADDRESS), + help = STRING_TOKEN(STR_IP6_NEW_GATEWAY_ADDR_HELP), + flags = INTERACTIVE, + key = KEY_GATEWAY_ADDRESS, + minsize = ADDRESS_STR_MIN_SIZE, + maxsize = ADDRESS_STR_MAX_SIZE, + endstring; + + string varid = IP6_CONFIG_IFR_NVDATA.DnsAddress, + prompt = STRING_TOKEN(STR_IP6_NEW_DNS_ADDRESS), + help = STRING_TOKEN(STR_IP6_NEW_DNS_ADDRESS_HELP), + flags = INTERACTIVE, + key = KEY_DNS_ADDRESS, + minsize = ADDRESS_STR_MIN_SIZE, + maxsize = ADDRESS_STR_MAX_SIZE, + endstring; + + text + help = STRING_TOKEN (STR_SAVE_AND_EXIT), + text = STRING_TOKEN (STR_SAVE_AND_EXIT), + flags = INTERACTIVE, + key = KEY_SAVE_CONFIG_CHANGES; + + text + help = STRING_TOKEN (STR_NO_SAVE_AND_EXIT), + text = STRING_TOKEN (STR_NO_SAVE_AND_EXIT), + flags = INTERACTIVE, + key = KEY_IGNORE_CONFIG_CHANGES; + + endform; + +endformset; + diff --git a/NetworkPkg/Ip6Dxe/Ip6ConfigImpl.c b/NetworkPkg/Ip6Dxe/Ip6ConfigImpl.c new file mode 100644 index 000000000..6efed37a4 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6ConfigImpl.c @@ -0,0 +1,2520 @@ +/** @file + The implementation of EFI IPv6 Configuration Protocol. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +LIST_ENTRY mIp6ConfigInstanceList = {&mIp6ConfigInstanceList, &mIp6ConfigInstanceList}; + +/** + The event process routine when the DHCPv6 service binding protocol is installed + in the system. + + @param[in] Event Not used. + @param[in] Context Pointer to the IP6 config instance data. + +**/ +VOID +EFIAPI +Ip6ConfigOnDhcp6SbInstalled ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Update the current policy to NewPolicy. During the transition + period, the default router list, on-link prefix list, autonomous prefix list + and address list in all interfaces will be released. + + @param[in] IpSb The IP6 service binding instance. + @param[in] NewPolicy The new policy to be updated to. + +**/ +VOID +Ip6ConfigOnPolicyChanged ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_CONFIG_POLICY NewPolicy + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Entry2; + LIST_ENTRY *Next; + IP6_INTERFACE *IpIf; + IP6_DAD_ENTRY *DadEntry; + IP6_DELAY_JOIN_LIST *DelayNode; + IP6_ADDRESS_INFO *AddrInfo; + IP6_PROTOCOL *Instance; + BOOLEAN Recovery; + + Recovery = FALSE; + + // + // Currently there are only two policies: Manual and Automatic. Regardless of + // what transition is going on, i.e., Manual -> Automatic and Automatic -> + // Manual, we have to free default router list, on-link prefix list, autonomous + // prefix list, address list in all the interfaces and destroy any IPv6 child + // instance whose local IP is neither 0 nor the link-local address. + // + Ip6CleanDefaultRouterList (IpSb); + Ip6CleanPrefixListTable (IpSb, &IpSb->OnlinkPrefix); + Ip6CleanPrefixListTable (IpSb, &IpSb->AutonomousPrefix); + + // + // It's tricky... If the LinkLocal address is O.K., add back the link-local + // prefix to the on-link prefix table. + // + if (IpSb->LinkLocalOk) { + Ip6CreatePrefixListEntry ( + IpSb, + TRUE, + (UINT32) IP6_INFINIT_LIFETIME, + (UINT32) IP6_INFINIT_LIFETIME, + IP6_LINK_LOCAL_PREFIX_LENGTH, + &IpSb->LinkLocalAddr + ); + } + + if (!IsListEmpty (&IpSb->DefaultInterface->AddressList) && IpSb->DefaultInterface->AddressCount > 0) { + // + // If any IPv6 children (Instance) in configured state and use global unicast address, it will be + // destroyed in Ip6RemoveAddr() function later. Then, the upper layer driver's Stop() function will be + // called, which may break the upper layer network stacks. So, the driver should take the responsibility + // for the recovery by using ConnectController() after Ip6RemoveAddr(). + // Here, just check whether need to recover the upper layer network stacks later. + // + NET_LIST_FOR_EACH (Entry, &IpSb->DefaultInterface->AddressList) { + AddrInfo = NET_LIST_USER_STRUCT_S (Entry, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE); + if (!IsListEmpty (&IpSb->Children)) { + NET_LIST_FOR_EACH (Entry2, &IpSb->Children) { + Instance = NET_LIST_USER_STRUCT_S (Entry2, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE); + if ((Instance->State == IP6_STATE_CONFIGED) && EFI_IP6_EQUAL (&Instance->ConfigData.StationAddress, &AddrInfo->Address)) { + Recovery = TRUE; + break; + } + } + } + } + + // + // All IPv6 children that use global unicast address as it's source address + // should be destroyed now. The survivers are those use the link-local address + // or the unspecified address as the source address. + // TODO: Conduct a check here. + Ip6RemoveAddr ( + IpSb, + &IpSb->DefaultInterface->AddressList, + &IpSb->DefaultInterface->AddressCount, + NULL, + 0 + ); + + if (IpSb->Controller != NULL && Recovery) { + // + // ConnectController() to recover the upper layer network stacks. + // + gBS->ConnectController (IpSb->Controller, NULL, NULL, TRUE); + } + } + + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + // + // remove all pending delay node and DAD entries for the global addresses. + // + IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE); + + NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DelayJoinList) { + DelayNode = NET_LIST_USER_STRUCT (Entry2, IP6_DELAY_JOIN_LIST, Link); + if (!NetIp6IsLinkLocalAddr (&DelayNode->AddressInfo->Address)) { + RemoveEntryList (&DelayNode->Link); + FreePool (DelayNode); + } + } + + NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DupAddrDetectList) { + DadEntry = NET_LIST_USER_STRUCT_S (Entry2, IP6_DAD_ENTRY, Link, IP6_DAD_ENTRY_SIGNATURE); + + if (!NetIp6IsLinkLocalAddr (&DadEntry->AddressInfo->Address)) { + // + // Fail this DAD entry if the address is not link-local. + // + Ip6OnDADFinished (FALSE, IpIf, DadEntry); + } + } + } + + if (NewPolicy == Ip6ConfigPolicyAutomatic) { + // + // Set parameters to trigger router solicitation sending in timer handler. + // + IpSb->RouterAdvertiseReceived = FALSE; + IpSb->SolicitTimer = IP6_MAX_RTR_SOLICITATIONS; + // + // delay 1 second + // + IpSb->Ticks = (UINT32) IP6_GET_TICKS (IP6_ONE_SECOND_IN_MS); + } +} + +/** + The work function to trigger the DHCPv6 process to perform a stateful autoconfiguration. + + @param[in] Instance Pointer to the IP6 config instance data. + @param[in] OtherInfoOnly If FALSE, get stateful address and other information + via DHCPv6. Otherwise, only get the other information. + + @retval EFI_SUCCESS The operation finished successfully. + @retval EFI_UNSUPPORTED The DHCP6 driver is not available. + +**/ +EFI_STATUS +Ip6ConfigStartStatefulAutoConfig ( + IN IP6_CONFIG_INSTANCE *Instance, + IN BOOLEAN OtherInfoOnly + ) +{ + EFI_STATUS Status; + IP6_SERVICE *IpSb; + EFI_DHCP6_CONFIG_DATA Dhcp6CfgData; + EFI_DHCP6_PROTOCOL *Dhcp6; + EFI_DHCP6_PACKET_OPTION *OptList[1]; + UINT16 OptBuf[4]; + EFI_DHCP6_PACKET_OPTION *Oro; + EFI_DHCP6_RETRANSMISSION InfoReqReXmit; + + // + // A host must not invoke stateful address configuration if it is already + // participating in the statuful protocol as a result of an earlier advertisement. + // + if (Instance->Dhcp6Handle != NULL) { + return EFI_SUCCESS; + } + + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + + Instance->OtherInfoOnly = OtherInfoOnly; + + Status = NetLibCreateServiceChild ( + IpSb->Controller, + IpSb->Image, + &gEfiDhcp6ServiceBindingProtocolGuid, + &Instance->Dhcp6Handle + ); + + if (Status == EFI_UNSUPPORTED) { + // + // No DHCPv6 Service Binding protocol, register a notify. + // + if (Instance->Dhcp6SbNotifyEvent == NULL) { + Instance->Dhcp6SbNotifyEvent = EfiCreateProtocolNotifyEvent ( + &gEfiDhcp6ServiceBindingProtocolGuid, + TPL_CALLBACK, + Ip6ConfigOnDhcp6SbInstalled, + (VOID *) Instance, + &Instance->Registration + ); + } + } + + if (EFI_ERROR (Status)) { + return Status; + } + + if (Instance->Dhcp6SbNotifyEvent != NULL) { + gBS->CloseEvent (Instance->Dhcp6SbNotifyEvent); + } + + Status = gBS->OpenProtocol ( + Instance->Dhcp6Handle, + &gEfiDhcp6ProtocolGuid, + (VOID **) &Instance->Dhcp6, + IpSb->Image, + IpSb->Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + ASSERT_EFI_ERROR (Status); + + Dhcp6 = Instance->Dhcp6; + Dhcp6->Configure (Dhcp6, NULL); + + // + // Set the exta options to send. Here we only want the option request option + // with DNS SERVERS. + // + Oro = (EFI_DHCP6_PACKET_OPTION *) OptBuf; + Oro->OpCode = HTONS (DHCP6_OPT_ORO); + Oro->OpLen = HTONS (2); + *((UINT16 *) &Oro->Data[0]) = HTONS (DHCP6_OPT_DNS_SERVERS); + OptList[0] = Oro; + + Status = EFI_SUCCESS; + + if (!OtherInfoOnly) { + // + // Get stateful address and other information via DHCPv6. + // + Dhcp6CfgData.Dhcp6Callback = NULL; + Dhcp6CfgData.CallbackContext = NULL; + Dhcp6CfgData.OptionCount = 1; + Dhcp6CfgData.OptionList = &OptList[0]; + Dhcp6CfgData.IaDescriptor.Type = EFI_DHCP6_IA_TYPE_NA; + Dhcp6CfgData.IaDescriptor.IaId = Instance->IaId; + Dhcp6CfgData.IaInfoEvent = Instance->Dhcp6Event; + Dhcp6CfgData.ReconfigureAccept = FALSE; + Dhcp6CfgData.RapidCommit = FALSE; + Dhcp6CfgData.SolicitRetransmission = NULL; + + Status = Dhcp6->Configure (Dhcp6, &Dhcp6CfgData); + + if (!EFI_ERROR (Status)) { + + if (IpSb->LinkLocalOk) { + Status = Dhcp6->Start (Dhcp6); + } else { + IpSb->Dhcp6NeedStart = TRUE; + } + + } + } else { + // + // Only get other information via DHCPv6, this doesn't require a config + // action. + // + InfoReqReXmit.Irt = 4; + InfoReqReXmit.Mrc = 64; + InfoReqReXmit.Mrt = 60; + InfoReqReXmit.Mrd = 0; + + if (IpSb->LinkLocalOk) { + Status = Dhcp6->InfoRequest ( + Dhcp6, + TRUE, + Oro, + 0, + NULL, + &InfoReqReXmit, + Instance->Dhcp6Event, + Ip6ConfigOnDhcp6Reply, + Instance + ); + } else { + IpSb->Dhcp6NeedInfoRequest = TRUE; + } + + } + + return Status; +} + +/** + Signal the registered event. It is the callback routine for NetMapIterate. + + @param[in] Map Points to the list of registered event. + @param[in] Item The registered event. + @param[in] Arg Not used. + +**/ +EFI_STATUS +EFIAPI +Ip6ConfigSignalEvent ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg + ) +{ + gBS->SignalEvent ((EFI_EVENT) Item->Key); + + return EFI_SUCCESS; +} + +/** + Read the configuration data from variable storage according to the VarName and + gEfiIp6ConfigProtocolGuid. It checks the integrity of variable data. If the + data is corrupted, it clears the variable data to ZERO. Othewise, it outputs the + configuration data to IP6_CONFIG_INSTANCE. + + @param[in] VarName The pointer to the variable name + @param[in, out] Instance The pointer to the IP6 config instance data. + + @retval EFI_NOT_FOUND The variable can not be found or already corrupted. + @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation. + @retval EFI_SUCCESS The configuration data was retrieved successfully. + +**/ +EFI_STATUS +Ip6ConfigReadConfigData ( + IN CHAR16 *VarName, + IN OUT IP6_CONFIG_INSTANCE *Instance + ) +{ + EFI_STATUS Status; + UINTN VarSize; + IP6_CONFIG_VARIABLE *Variable; + IP6_CONFIG_DATA_ITEM *DataItem; + UINTN Index; + IP6_CONFIG_DATA_RECORD DataRecord; + CHAR8 *Data; + + // + // Try to read the configuration variable. + // + VarSize = 0; + Status = gRT->GetVariable ( + VarName, + &gEfiIp6ConfigProtocolGuid, + NULL, + &VarSize, + NULL + ); + + if (Status == EFI_BUFFER_TOO_SMALL) { + // + // Allocate buffer and read the config variable. + // + Variable = AllocatePool (VarSize); + if (Variable == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = gRT->GetVariable ( + VarName, + &gEfiIp6ConfigProtocolGuid, + NULL, + &VarSize, + Variable + ); + if (EFI_ERROR (Status) || (UINT16) (~NetblockChecksum ((UINT8 *) Variable, (UINT32) VarSize)) != 0) { + // + // GetVariable still error or the variable is corrupted. + // Fall back to the default value. + // + FreePool (Variable); + + // + // Remove the problematic variable and return EFI_NOT_FOUND, a new + // variable will be set again. + // + gRT->SetVariable ( + VarName, + &gEfiIp6ConfigProtocolGuid, + IP6_CONFIG_VARIABLE_ATTRIBUTE, + 0, + NULL + ); + + return EFI_NOT_FOUND; + } + + // + // Get the IAID we use. + // + Instance->IaId = Variable->IaId; + + for (Index = 0; Index < Variable->DataRecordCount; Index++) { + + CopyMem (&DataRecord, &Variable->DataRecord[Index], sizeof (DataRecord)); + + DataItem = &Instance->DataItem[DataRecord.DataType]; + if (DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED) && + (DataItem->DataSize != DataRecord.DataSize) + ) { + // + // Perhaps a corrupted data record... + // + continue; + } + + if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED)) { + // + // This data item has variable length data. + // + DataItem->Data.Ptr = AllocatePool (DataRecord.DataSize); + if (DataItem->Data.Ptr == NULL) { + // + // no memory resource + // + continue; + } + } + + Data = (CHAR8 *) Variable + DataRecord.Offset; + CopyMem (DataItem->Data.Ptr, Data, DataRecord.DataSize); + + DataItem->DataSize = DataRecord.DataSize; + DataItem->Status = EFI_SUCCESS; + } + + FreePool (Variable); + return EFI_SUCCESS; + } + + return Status; +} + +/** + Write the configuration data from IP6_CONFIG_INSTANCE to variable storage. + + @param[in] VarName The pointer to the variable name. + @param[in] Instance The pointer to the IP6 configuration instance data. + + @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation. + @retval EFI_SUCCESS The configuration data is written successfully. + +**/ +EFI_STATUS +Ip6ConfigWriteConfigData ( + IN CHAR16 *VarName, + IN IP6_CONFIG_INSTANCE *Instance + ) +{ + UINTN Index; + UINTN VarSize; + IP6_CONFIG_DATA_ITEM *DataItem; + IP6_CONFIG_VARIABLE *Variable; + IP6_CONFIG_DATA_RECORD *DataRecord; + CHAR8 *Heap; + EFI_STATUS Status; + + VarSize = sizeof (IP6_CONFIG_VARIABLE) - sizeof (IP6_CONFIG_DATA_RECORD); + + for (Index = 0; Index < Ip6ConfigDataTypeMaximum; Index++) { + + DataItem = &Instance->DataItem[Index]; + if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_VOLATILE) && !EFI_ERROR (DataItem->Status)) { + + VarSize += sizeof (IP6_CONFIG_DATA_RECORD) + DataItem->DataSize; + } + } + + Variable = AllocatePool (VarSize); + if (Variable == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Variable->IaId = Instance->IaId; + Heap = (CHAR8 *) Variable + VarSize; + Variable->DataRecordCount = 0; + + for (Index = 0; Index < Ip6ConfigDataTypeMaximum; Index++) { + + DataItem = &Instance->DataItem[Index]; + if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_VOLATILE) && !EFI_ERROR (DataItem->Status)) { + + Heap -= DataItem->DataSize; + CopyMem (Heap, DataItem->Data.Ptr, DataItem->DataSize); + + DataRecord = &Variable->DataRecord[Variable->DataRecordCount]; + DataRecord->DataType = (EFI_IP6_CONFIG_DATA_TYPE) Index; + DataRecord->DataSize = (UINT32) DataItem->DataSize; + DataRecord->Offset = (UINT16) (Heap - (CHAR8 *) Variable); + + Variable->DataRecordCount++; + } + } + + Variable->Checksum = 0; + Variable->Checksum = (UINT16) ~NetblockChecksum ((UINT8 *) Variable, (UINT32) VarSize); + + Status = gRT->SetVariable ( + VarName, + &gEfiIp6ConfigProtocolGuid, + IP6_CONFIG_VARIABLE_ATTRIBUTE, + VarSize, + Variable + ); + + FreePool (Variable); + + return Status; +} + +/** + The work function for EfiIp6ConfigGetData() to get the interface information + of the communication device this IP6Config instance manages. + + @param[in] Instance Pointer to the IP6 config instance data. + @param[in, out] DataSize On input, in bytes, the size of Data. On output, in + bytes, the size of buffer required to store the specified + configuration data. + @param[in] Data The data buffer in which the configuration data is returned. + Ignored if DataSize is ZERO. + + @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified + configuration data, and the required size is + returned in DataSize. + @retval EFI_SUCCESS The specified configuration data was obtained. + +**/ +EFI_STATUS +Ip6ConfigGetIfInfo ( + IN IP6_CONFIG_INSTANCE *Instance, + IN OUT UINTN *DataSize, + IN VOID *Data OPTIONAL + ) +{ + IP6_SERVICE *IpSb; + UINTN Length; + IP6_CONFIG_DATA_ITEM *Item; + EFI_IP6_CONFIG_INTERFACE_INFO *IfInfo; + UINT32 AddressCount; + UINT32 RouteCount; + + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + Length = sizeof (EFI_IP6_CONFIG_INTERFACE_INFO); + + // + // Calculate the required length, add the buffer size for AddressInfo and + // RouteTable + // + Ip6BuildEfiAddressList (IpSb, &AddressCount, NULL); + Ip6BuildEfiRouteTable (IpSb->RouteTable, &RouteCount, NULL); + + Length += AddressCount * sizeof (EFI_IP6_ADDRESS_INFO) + RouteCount * sizeof (EFI_IP6_ROUTE_TABLE); + + if (*DataSize < Length) { + *DataSize = Length; + return EFI_BUFFER_TOO_SMALL; + } + + // + // Copy the fixed size part of the interface info. + // + Item = &Instance->DataItem[Ip6ConfigDataTypeInterfaceInfo]; + IfInfo = (EFI_IP6_CONFIG_INTERFACE_INFO *) Data; + CopyMem (IfInfo, Item->Data.Ptr, sizeof (EFI_IP6_CONFIG_INTERFACE_INFO)); + + // + // AddressInfo + // + IfInfo->AddressInfo = (EFI_IP6_ADDRESS_INFO *) (IfInfo + 1); + Ip6BuildEfiAddressList (IpSb, &IfInfo->AddressInfoCount, &IfInfo->AddressInfo); + + // + // RouteTable + // + IfInfo->RouteTable = (EFI_IP6_ROUTE_TABLE *) (IfInfo->AddressInfo + IfInfo->AddressInfoCount); + Ip6BuildEfiRouteTable (IpSb->RouteTable, &IfInfo->RouteCount, &IfInfo->RouteTable); + + if (IfInfo->AddressInfoCount == 0) { + IfInfo->AddressInfo = NULL; + } + + if (IfInfo->RouteCount == 0) { + IfInfo->RouteTable = NULL; + } + + return EFI_SUCCESS; +} + +/** + The work function for EfiIp6ConfigSetData() to set the alternative inteface ID + for the communication device managed by this IP6Config instance, if the link local + IPv6 addresses generated from the interface ID based on the default source the + EFI IPv6 Protocol uses is a duplicate address. + + @param[in] Instance Pointer to the IP6 configuration instance data. + @param[in] DataSize Size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set. + + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type, + 8 bytes. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set. + +**/ +EFI_STATUS +Ip6ConfigSetAltIfId ( + IN IP6_CONFIG_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_IP6_CONFIG_INTERFACE_ID *OldIfId; + EFI_IP6_CONFIG_INTERFACE_ID *NewIfId; + IP6_CONFIG_DATA_ITEM *DataItem; + + if (DataSize != sizeof (EFI_IP6_CONFIG_INTERFACE_ID)) { + return EFI_BAD_BUFFER_SIZE; + } + + DataItem = &Instance->DataItem[Ip6ConfigDataTypeAltInterfaceId]; + OldIfId = DataItem->Data.AltIfId; + NewIfId = (EFI_IP6_CONFIG_INTERFACE_ID *) Data; + + CopyMem (OldIfId, NewIfId, DataSize); + DataItem->Status = EFI_SUCCESS; + + return EFI_SUCCESS; +} + +/** + The work function for EfiIp6ConfigSetData() to set the general configuration + policy for the EFI IPv6 network stack that is running on the communication device + managed by this IP6Config instance. The policy will affect other configuration settings. + + @param[in] Instance Pointer to the IP6 config instance data. + @param[in] DataSize Size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set. + + @retval EFI_INVALID_PARAMETER The to be set policy is invalid. + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type. + @retval EFI_ABORTED The new policy equals the current policy. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set. + +**/ +EFI_STATUS +Ip6ConfigSetPolicy ( + IN IP6_CONFIG_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_IP6_CONFIG_POLICY NewPolicy; + IP6_CONFIG_DATA_ITEM *DataItem; + IP6_SERVICE *IpSb; + + if (DataSize != sizeof (EFI_IP6_CONFIG_POLICY)) { + return EFI_BAD_BUFFER_SIZE; + } + + NewPolicy = *((EFI_IP6_CONFIG_POLICY *) Data); + + if (NewPolicy > Ip6ConfigPolicyAutomatic) { + return EFI_INVALID_PARAMETER; + } + + if (NewPolicy == Instance->Policy) { + + return EFI_ABORTED; + } else { + // + // Clean the ManualAddress, Gateway and DnsServers, shrink the variable + // data size, and fire up all the related events. + // + DataItem = &Instance->DataItem[Ip6ConfigDataTypeManualAddress]; + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = NULL; + DataItem->DataSize = 0; + DataItem->Status = EFI_NOT_FOUND; + NetMapIterate (&DataItem->EventMap, Ip6ConfigSignalEvent, NULL); + + DataItem = &Instance->DataItem[Ip6ConfigDataTypeGateway]; + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = NULL; + DataItem->DataSize = 0; + DataItem->Status = EFI_NOT_FOUND; + NetMapIterate (&DataItem->EventMap, Ip6ConfigSignalEvent, NULL); + + DataItem = &Instance->DataItem[Ip6ConfigDataTypeDnsServer]; + DataItem->Data.Ptr = NULL; + DataItem->DataSize = 0; + DataItem->Status = EFI_NOT_FOUND; + NetMapIterate (&DataItem->EventMap, Ip6ConfigSignalEvent, NULL); + + if (NewPolicy == Ip6ConfigPolicyManual) { + // + // The policy is changed from automatic to manual. Stop the DHCPv6 process + // and destroy the DHCPv6 child. + // + if (Instance->Dhcp6Handle != NULL) { + Ip6ConfigDestroyDhcp6 (Instance); + } + } + + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + Ip6ConfigOnPolicyChanged (IpSb, NewPolicy); + + Instance->Policy = NewPolicy; + + return EFI_SUCCESS; + } +} + +/** + The work function for EfiIp6ConfigSetData() to set the number of consecutive + Neighbor Solicitation messages sent while performing Duplicate Address Detection + on a tentative address. A value of ZERO indicates that Duplicate Address Detection + will not be performed on a tentative address. + + @param[in] Instance The Instance Pointer to the IP6 config instance data. + @param[in] DataSize Size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set. + + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type. + @retval EFI_ABORTED The new transmit count equals the current configuration. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set. + +**/ +EFI_STATUS +Ip6ConfigSetDadXmits ( + IN IP6_CONFIG_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS *OldDadXmits; + + if (DataSize != sizeof (EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS)) { + return EFI_BAD_BUFFER_SIZE; + } + + OldDadXmits = Instance->DataItem[Ip6ConfigDataTypeDupAddrDetectTransmits].Data.DadXmits; + + if ((*(UINT32 *) Data) == OldDadXmits->DupAddrDetectTransmits) { + + return EFI_ABORTED; + } else { + + OldDadXmits->DupAddrDetectTransmits = *((UINT32 *) Data); + return EFI_SUCCESS; + } +} + +/** + The callback function for Ip6SetAddr. The prototype is defined + as IP6_DAD_CALLBACK. It is called after Duplicate Address Detection is performed + for the manual address set by Ip6ConfigSetManualAddress. + + @param[in] IsDadPassed If TRUE, Duplicate Address Detection passed. + @param[in] TargetAddress The tentative IPv6 address to be checked. + @param[in] Context Pointer to the IP6 configuration instance data. + +**/ +VOID +Ip6ManualAddrDadCallback ( + IN BOOLEAN IsDadPassed, + IN EFI_IPv6_ADDRESS *TargetAddress, + IN VOID *Context + ) +{ + IP6_CONFIG_INSTANCE *Instance; + UINTN Index; + IP6_CONFIG_DATA_ITEM *Item; + EFI_IP6_CONFIG_MANUAL_ADDRESS *ManualAddr; + EFI_IP6_CONFIG_MANUAL_ADDRESS *PassedAddr; + UINTN DadPassCount; + UINTN DadFailCount; + IP6_SERVICE *IpSb; + + Instance = (IP6_CONFIG_INSTANCE *) Context; + NET_CHECK_SIGNATURE (Instance, IP6_CONFIG_INSTANCE_SIGNATURE); + Item = &Instance->DataItem[Ip6ConfigDataTypeManualAddress]; + ManualAddr = NULL; + + if (Item->DataSize == 0) { + return; + } + + for (Index = 0; Index < Item->DataSize / sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS); Index++) { + // + // Find the original tag used to place into the NET_MAP. + // + ManualAddr = Item->Data.ManualAddress + Index; + if (EFI_IP6_EQUAL (TargetAddress, &ManualAddr->Address)) { + break; + } + } + + ASSERT (Index != Item->DataSize / sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS)); + + if (IsDadPassed) { + NetMapInsertTail (&Instance->DadPassedMap, ManualAddr, NULL); + } else { + NetMapInsertTail (&Instance->DadFailedMap, ManualAddr, NULL); + } + + DadPassCount = NetMapGetCount (&Instance->DadPassedMap); + DadFailCount = NetMapGetCount (&Instance->DadFailedMap); + + if ((DadPassCount + DadFailCount) == (Item->DataSize / sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS))) { + // + // All addresses have finished the configuration process. + // + if (DadFailCount != 0) { + // + // There is at least one duplicate address. + // + FreePool (Item->Data.Ptr); + + Item->DataSize = DadPassCount * sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS); + if (Item->DataSize == 0) { + // + // All failed, bad luck. + // + Item->Data.Ptr = NULL; + Item->Status = EFI_NOT_FOUND; + } else { + // + // Part of addresses are detected to be duplicates, so update the + // data with those passed. + // + PassedAddr = (EFI_IP6_CONFIG_MANUAL_ADDRESS *) AllocatePool (Item->DataSize); + ASSERT (PassedAddr != NULL); + + Item->Data.Ptr = PassedAddr; + Item->Status = EFI_SUCCESS; + + while (!NetMapIsEmpty (&Instance->DadPassedMap)) { + ManualAddr = (EFI_IP6_CONFIG_MANUAL_ADDRESS *) NetMapRemoveHead (&Instance->DadPassedMap, NULL); + CopyMem (PassedAddr, ManualAddr, sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS)); + + PassedAddr++; + } + + ASSERT ((UINTN) PassedAddr - (UINTN) Item->Data.Ptr == Item->DataSize); + } + } else { + // + // All addresses are valid. + // + Item->Status = EFI_SUCCESS; + } + + // + // Remove the tags we put in the NET_MAPs. + // + while (!NetMapIsEmpty (&Instance->DadFailedMap)) { + NetMapRemoveHead (&Instance->DadFailedMap, NULL); + } + + while (!NetMapIsEmpty (&Instance->DadPassedMap)) { + NetMapRemoveHead (&Instance->DadPassedMap, NULL); + } + + // + // Signal the waiting events. + // + NetMapIterate (&Item->EventMap, Ip6ConfigSignalEvent, NULL); + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + Ip6ConfigWriteConfigData (IpSb->MacString, Instance); + } +} + +/** + The work function for EfiIp6ConfigSetData() to set the station addresses manually + for the EFI IPv6 network stack. It is only configurable when the policy is + Ip6ConfigPolicyManual. + + @param[in] Instance Pointer to the IP6 configuration instance data. + @param[in] DataSize Size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set. + + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type. + @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set + under the current policy. + @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid. + @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation. + @retval EFI_NOT_READY An asynchrous process is invoked to set the specified + configuration data, and the process is not finished. + @retval EFI_ABORTED The manual addresses to be set equal current + configuration. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set. + +**/ +EFI_STATUS +Ip6ConfigSetManualAddress ( + IN IP6_CONFIG_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_IP6_CONFIG_MANUAL_ADDRESS *NewAddress; + EFI_IP6_CONFIG_MANUAL_ADDRESS *TmpAddress; + IP6_CONFIG_DATA_ITEM *DataItem; + UINTN NewAddressCount; + UINTN Index1; + UINTN Index2; + IP6_SERVICE *IpSb; + IP6_ADDRESS_INFO *CurrentAddrInfo; + IP6_ADDRESS_INFO *Copy; + LIST_ENTRY CurrentSourceList; + UINT32 CurrentSourceCount; + LIST_ENTRY *Entry; + LIST_ENTRY *Entry2; + IP6_INTERFACE *IpIf; + IP6_PREFIX_LIST_ENTRY *PrefixEntry; + EFI_STATUS Status; + BOOLEAN IsUpdated; + LIST_ENTRY *Next; + IP6_DAD_ENTRY *DadEntry; + IP6_DELAY_JOIN_LIST *DelayNode; + + NewAddress = NULL; + TmpAddress = NULL; + CurrentAddrInfo = NULL; + Copy = NULL; + Entry = NULL; + Entry2 = NULL; + IpIf = NULL; + PrefixEntry = NULL; + Next = NULL; + DadEntry = NULL; + DelayNode = NULL; + Status = EFI_SUCCESS; + + ASSERT (Instance->DataItem[Ip6ConfigDataTypeManualAddress].Status != EFI_NOT_READY); + + if ((DataSize != 0) && ((DataSize % sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS)) != 0)) { + return EFI_BAD_BUFFER_SIZE; + } + + if (Instance->Policy != Ip6ConfigPolicyManual) { + return EFI_WRITE_PROTECTED; + } + + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + + DataItem = &Instance->DataItem[Ip6ConfigDataTypeManualAddress]; + + if (Data != NULL && DataSize != 0) { + NewAddressCount = DataSize / sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS); + NewAddress = (EFI_IP6_CONFIG_MANUAL_ADDRESS *) Data; + + for (Index1 = 0; Index1 < NewAddressCount; Index1++, NewAddress++) { + + if (NetIp6IsLinkLocalAddr (&NewAddress->Address) || + !NetIp6IsValidUnicast (&NewAddress->Address) || + (NewAddress->PrefixLength > 128) + ) { + // + // make sure the IPv6 address is unicast and not link-local address && + // the prefix length is valid. + // + return EFI_INVALID_PARAMETER; + } + + TmpAddress = NewAddress + 1; + for (Index2 = Index1 + 1; Index2 < NewAddressCount; Index2++, TmpAddress++) { + // + // Any two addresses in the array can't be equal. + // + if (EFI_IP6_EQUAL (&TmpAddress->Address, &NewAddress->Address)) { + + return EFI_INVALID_PARAMETER; + } + } + } + + // + // Build the current source address list. + // + InitializeListHead (&CurrentSourceList); + CurrentSourceCount = 0; + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE); + + NET_LIST_FOR_EACH (Entry2, &IpIf->AddressList) { + CurrentAddrInfo = NET_LIST_USER_STRUCT_S (Entry2, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE); + + Copy = AllocateCopyPool (sizeof (IP6_ADDRESS_INFO), CurrentAddrInfo); + if (Copy == NULL) { + break; + } + + InsertTailList (&CurrentSourceList, &Copy->Link); + CurrentSourceCount++; + } + } + + // + // Update the value... a long journey starts + // + NewAddress = AllocateCopyPool (DataSize, Data); + if (NewAddress == NULL) { + Ip6RemoveAddr (NULL, &CurrentSourceList, &CurrentSourceCount, NULL, 0); + + return EFI_OUT_OF_RESOURCES; + } + + // + // Store the new data, and init the DataItem status to EFI_NOT_READY because + // we may have an asynchronous configuration process. + // + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = NewAddress; + DataItem->DataSize = DataSize; + DataItem->Status = EFI_NOT_READY; + + // + // Trigger DAD, it's an asynchronous process. + // + IsUpdated = FALSE; + + for (Index1 = 0; Index1 < NewAddressCount; Index1++, NewAddress++) { + if (Ip6IsOneOfSetAddress (IpSb, &NewAddress->Address, NULL, &CurrentAddrInfo)) { + ASSERT (CurrentAddrInfo != NULL); + // + // Remove this already existing source address from the CurrentSourceList + // built before. + // + Ip6RemoveAddr ( + NULL, + &CurrentSourceList, + &CurrentSourceCount, + &CurrentAddrInfo->Address, + 128 + ); + + // + // If the new address's prefix length is not specified, just use the previous configured + // prefix length for this address. + // + if (NewAddress->PrefixLength == 0) { + NewAddress->PrefixLength = CurrentAddrInfo->PrefixLength; + } + + // + // This manual address is already in use, see whether prefix length is changed. + // + if (NewAddress->PrefixLength != CurrentAddrInfo->PrefixLength) { + // + // Remove the on-link prefix table, the route entry will be removed + // implicitly. + // + PrefixEntry = Ip6FindPrefixListEntry ( + IpSb, + TRUE, + CurrentAddrInfo->PrefixLength, + &CurrentAddrInfo->Address + ); + if (PrefixEntry != NULL) { + Ip6DestroyPrefixListEntry (IpSb, PrefixEntry, TRUE, FALSE); + } + + // + // Save the prefix length. + // + CurrentAddrInfo->PrefixLength = NewAddress->PrefixLength; + IsUpdated = TRUE; + } + + // + // create a new on-link prefix entry. + // + PrefixEntry = Ip6FindPrefixListEntry ( + IpSb, + TRUE, + NewAddress->PrefixLength, + &NewAddress->Address + ); + if (PrefixEntry == NULL) { + Ip6CreatePrefixListEntry ( + IpSb, + TRUE, + (UINT32) IP6_INFINIT_LIFETIME, + (UINT32) IP6_INFINIT_LIFETIME, + NewAddress->PrefixLength, + &NewAddress->Address + ); + } + + CurrentAddrInfo->IsAnycast = NewAddress->IsAnycast; + // + // Artificially mark this address passed DAD be'coz it is already in use. + // + Ip6ManualAddrDadCallback (TRUE, &NewAddress->Address, Instance); + } else { + // + // A new address. + // + IsUpdated = TRUE; + + // + // Set the new address, this will trigger DAD and activate the address if + // DAD succeeds. + // + Ip6SetAddress ( + IpSb->DefaultInterface, + &NewAddress->Address, + NewAddress->IsAnycast, + NewAddress->PrefixLength, + (UINT32) IP6_INFINIT_LIFETIME, + (UINT32) IP6_INFINIT_LIFETIME, + Ip6ManualAddrDadCallback, + Instance + ); + } + } + + // + // Check the CurrentSourceList, it now contains those addresses currently in + // use and will be removed. + // + IpIf = IpSb->DefaultInterface; + + while (!IsListEmpty (&CurrentSourceList)) { + IsUpdated = TRUE; + + CurrentAddrInfo = NET_LIST_HEAD (&CurrentSourceList, IP6_ADDRESS_INFO, Link); + + // + // This local address is going to be removed, the IP instances that are + // currently using it will be destroyed. + // + Ip6RemoveAddr ( + IpSb, + &IpIf->AddressList, + &IpIf->AddressCount, + &CurrentAddrInfo->Address, + 128 + ); + + // + // Remove the on-link prefix table, the route entry will be removed + // implicitly. + // + PrefixEntry = Ip6FindPrefixListEntry ( + IpSb, + TRUE, + CurrentAddrInfo->PrefixLength, + &CurrentAddrInfo->Address + ); + if (PrefixEntry != NULL) { + Ip6DestroyPrefixListEntry (IpSb, PrefixEntry, TRUE, FALSE); + } + + RemoveEntryList (&CurrentAddrInfo->Link); + FreePool (CurrentAddrInfo); + } + + if (IsUpdated) { + if (DataItem->Status == EFI_NOT_READY) { + // + // If DAD is disabled on this interface, the configuration process is + // actually synchronous, and the data item's status will be changed to + // the final status before we reach here, just check it. + // + Status = EFI_NOT_READY; + } else { + Status = EFI_SUCCESS; + } + } else { + // + // No update is taken, reset the status to success and return EFI_ABORTED. + // + DataItem->Status = EFI_SUCCESS; + Status = EFI_ABORTED; + } + } else { + // + // DataSize is 0 and Data is NULL, clean up the manual address. + // + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = NULL; + DataItem->DataSize = 0; + DataItem->Status = EFI_NOT_FOUND; + + Ip6CleanDefaultRouterList (IpSb); + Ip6CleanPrefixListTable (IpSb, &IpSb->OnlinkPrefix); + Ip6CleanPrefixListTable (IpSb, &IpSb->AutonomousPrefix); + Ip6CleanAssembleTable (&IpSb->Assemble); + + if (IpSb->LinkLocalOk) { + Ip6CreatePrefixListEntry ( + IpSb, + TRUE, + (UINT32) IP6_INFINIT_LIFETIME, + (UINT32) IP6_INFINIT_LIFETIME, + IP6_LINK_LOCAL_PREFIX_LENGTH, + &IpSb->LinkLocalAddr + ); + } + + Ip6RemoveAddr ( + IpSb, + &IpSb->DefaultInterface->AddressList, + &IpSb->DefaultInterface->AddressCount, + NULL, + 0 + ); + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + // + // Remove all pending delay node and DAD entries for the global addresses. + // + IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE); + + NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DelayJoinList) { + DelayNode = NET_LIST_USER_STRUCT (Entry2, IP6_DELAY_JOIN_LIST, Link); + if (!NetIp6IsLinkLocalAddr (&DelayNode->AddressInfo->Address)) { + RemoveEntryList (&DelayNode->Link); + FreePool (DelayNode); + } + } + + NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DupAddrDetectList) { + DadEntry = NET_LIST_USER_STRUCT_S (Entry2, IP6_DAD_ENTRY, Link, IP6_DAD_ENTRY_SIGNATURE); + + if (!NetIp6IsLinkLocalAddr (&DadEntry->AddressInfo->Address)) { + // + // Fail this DAD entry if the address is not link-local. + // + Ip6OnDADFinished (FALSE, IpIf, DadEntry); + } + } + } + } + + return Status; +} + +/** + The work function for EfiIp6ConfigSetData() to set the gateway addresses manually + for the EFI IPv6 network stack that is running on the communication device that + this EFI IPv6 Configuration Protocol manages. It is not configurable when the policy is + Ip6ConfigPolicyAutomatic. The gateway addresses must be unicast IPv6 addresses. + + @param[in] Instance The pointer to the IP6 config instance data. + @param[in] DataSize The size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set. This points to an array of + EFI_IPv6_ADDRESS instances. + + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type. + @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set + under the current policy. + @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to complete the operation. + @retval EFI_ABORTED The manual gateway addresses to be set equal the + current configuration. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set. + +**/ +EFI_STATUS +Ip6ConfigSetGateway ( + IN IP6_CONFIG_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ) +{ + UINTN Index1; + UINTN Index2; + EFI_IPv6_ADDRESS *OldGateway; + EFI_IPv6_ADDRESS *NewGateway; + UINTN OldGatewayCount; + UINTN NewGatewayCount; + IP6_CONFIG_DATA_ITEM *Item; + BOOLEAN OneRemoved; + BOOLEAN OneAdded; + IP6_SERVICE *IpSb; + IP6_DEFAULT_ROUTER *DefaultRouter; + VOID *Tmp; + + OldGateway = NULL; + NewGateway = NULL; + Item = NULL; + DefaultRouter = NULL; + Tmp = NULL; + OneRemoved = FALSE; + OneAdded = FALSE; + + if ((DataSize != 0) && (DataSize % sizeof (EFI_IPv6_ADDRESS) != 0)) { + return EFI_BAD_BUFFER_SIZE; + } + + if (Instance->Policy != Ip6ConfigPolicyManual) { + return EFI_WRITE_PROTECTED; + } + + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + Item = &Instance->DataItem[Ip6ConfigDataTypeGateway]; + OldGateway = Item->Data.Gateway; + OldGatewayCount = Item->DataSize / sizeof (EFI_IPv6_ADDRESS); + + for (Index1 = 0; Index1 < OldGatewayCount; Index1++) { + // + // Remove this default router. + // + DefaultRouter = Ip6FindDefaultRouter (IpSb, OldGateway + Index1); + if (DefaultRouter != NULL) { + Ip6DestroyDefaultRouter (IpSb, DefaultRouter); + OneRemoved = TRUE; + } + } + + if (Data != NULL && DataSize != 0) { + NewGateway = (EFI_IPv6_ADDRESS *) Data; + NewGatewayCount = DataSize / sizeof (EFI_IPv6_ADDRESS); + for (Index1 = 0; Index1 < NewGatewayCount; Index1++) { + + if (!NetIp6IsValidUnicast (NewGateway + Index1)) { + + return EFI_INVALID_PARAMETER; + } + + for (Index2 = Index1 + 1; Index2 < NewGatewayCount; Index2++) { + if (EFI_IP6_EQUAL (NewGateway + Index1, NewGateway + Index2)) { + return EFI_INVALID_PARAMETER; + } + } + } + + if (NewGatewayCount != OldGatewayCount) { + Tmp = AllocatePool (DataSize); + if (Tmp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } else { + Tmp = NULL; + } + + for (Index1 = 0; Index1 < NewGatewayCount; Index1++) { + + DefaultRouter = Ip6FindDefaultRouter (IpSb, NewGateway + Index1); + if (DefaultRouter == NULL) { + Ip6CreateDefaultRouter (IpSb, NewGateway + Index1, IP6_INF_ROUTER_LIFETIME); + OneAdded = TRUE; + } + } + + if (!OneRemoved && !OneAdded) { + Item->Status = EFI_SUCCESS; + return EFI_ABORTED; + } else { + + if (Tmp != NULL) { + if (Item->Data.Ptr != NULL) { + FreePool (Item->Data.Ptr); + } + Item->Data.Ptr = Tmp; + } + + CopyMem (Item->Data.Ptr, Data, DataSize); + Item->DataSize = DataSize; + Item->Status = EFI_SUCCESS; + return EFI_SUCCESS; + } + } else { + // + // DataSize is 0 and Data is NULL, clean up the Gateway address. + // + if (Item->Data.Ptr != NULL) { + FreePool (Item->Data.Ptr); + } + Item->Data.Ptr = NULL; + Item->DataSize = 0; + Item->Status = EFI_NOT_FOUND; + } + + return EFI_SUCCESS; +} + +/** + The work function for EfiIp6ConfigSetData() to set the DNS server list for the + EFI IPv6 network stack running on the communication device that this EFI IPv6 + Configuration Protocol manages. It is not configurable when the policy is + Ip6ConfigPolicyAutomatic. The DNS server addresses must be unicast IPv6 addresses. + + @param[in] Instance The pointer to the IP6 config instance data. + @param[in] DataSize The size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set, points to an array of + EFI_IPv6_ADDRESS instances. + + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type. + @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set + under the current policy. + @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation. + @retval EFI_ABORTED The DNS server addresses to be set equal the current + configuration. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set. + +**/ +EFI_STATUS +Ip6ConfigSetDnsServer ( + IN IP6_CONFIG_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ) +{ + UINTN OldIndex; + UINTN NewIndex; + EFI_IPv6_ADDRESS *OldDns; + EFI_IPv6_ADDRESS *NewDns; + UINTN OldDnsCount; + UINTN NewDnsCount; + IP6_CONFIG_DATA_ITEM *Item; + BOOLEAN OneAdded; + VOID *Tmp; + + OldDns = NULL; + NewDns = NULL; + Item = NULL; + Tmp = NULL; + + if ((DataSize != 0) && (DataSize % sizeof (EFI_IPv6_ADDRESS) != 0)) { + return EFI_BAD_BUFFER_SIZE; + } + + if (Instance->Policy != Ip6ConfigPolicyManual) { + return EFI_WRITE_PROTECTED; + } + + Item = &Instance->DataItem[Ip6ConfigDataTypeDnsServer]; + + if (Data != NULL && DataSize != 0) { + NewDns = (EFI_IPv6_ADDRESS *) Data; + OldDns = Item->Data.DnsServers; + NewDnsCount = DataSize / sizeof (EFI_IPv6_ADDRESS); + OldDnsCount = Item->DataSize / sizeof (EFI_IPv6_ADDRESS); + OneAdded = FALSE; + + if (NewDnsCount != OldDnsCount) { + Tmp = AllocatePool (DataSize); + if (Tmp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } else { + Tmp = NULL; + } + + for (NewIndex = 0; NewIndex < NewDnsCount; NewIndex++) { + + if (!NetIp6IsValidUnicast (NewDns + NewIndex)) { + // + // The dns server address must be unicast. + // + if (Tmp != NULL) { + FreePool (Tmp); + } + return EFI_INVALID_PARAMETER; + } + + if (OneAdded) { + // + // If any address in the new setting is not in the old settings, skip the + // comparision below. + // + continue; + } + + for (OldIndex = 0; OldIndex < OldDnsCount; OldIndex++) { + if (EFI_IP6_EQUAL (NewDns + NewIndex, OldDns + OldIndex)) { + // + // If found break out. + // + break; + } + } + + if (OldIndex == OldDnsCount) { + OneAdded = TRUE; + } + } + + if (!OneAdded && (DataSize == Item->DataSize)) { + // + // No new item is added and the size is the same. + // + Item->Status = EFI_SUCCESS; + return EFI_ABORTED; + } else { + if (Tmp != NULL) { + if (Item->Data.Ptr != NULL) { + FreePool (Item->Data.Ptr); + } + Item->Data.Ptr = Tmp; + } + + CopyMem (Item->Data.Ptr, Data, DataSize); + Item->DataSize = DataSize; + Item->Status = EFI_SUCCESS; + } + } else { + // + // DataSize is 0 and Data is NULL, clean up the DnsServer address. + // + if (Item->Data.Ptr != NULL) { + FreePool (Item->Data.Ptr); + } + Item->Data.Ptr = NULL; + Item->DataSize = 0; + Item->Status = EFI_NOT_FOUND; + } + + return EFI_SUCCESS; +} + +/** + Generate the operational state of the interface this IP6 config instance manages + and output in EFI_IP6_CONFIG_INTERFACE_INFO. + + @param[in] IpSb The pointer to the IP6 service binding instance. + @param[out] IfInfo The pointer to the IP6 configuration interface information structure. + +**/ +VOID +Ip6ConfigInitIfInfo ( + IN IP6_SERVICE *IpSb, + OUT EFI_IP6_CONFIG_INTERFACE_INFO *IfInfo + ) +{ + UnicodeSPrint ( + IfInfo->Name, + sizeof (IfInfo->Name), + L"eth%d", + IpSb->Ip6ConfigInstance.IfIndex + ); + + IfInfo->IfType = IpSb->SnpMode.IfType; + IfInfo->HwAddressSize = IpSb->SnpMode.HwAddressSize; + CopyMem (&IfInfo->HwAddress, &IpSb->SnpMode.CurrentAddress, IfInfo->HwAddressSize); +} + +/** + Parse DHCPv6 reply packet to get the DNS server list. + It is the work function for Ip6ConfigOnDhcp6Reply and Ip6ConfigOnDhcp6Event. + + @param[in] Dhcp6 The pointer to the EFI_DHCP6_PROTOCOL instance. + @param[in, out] Instance The pointer to the IP6 configuration instance data. + @param[in] Reply The pointer to the DHCPv6 reply packet. + + @retval EFI_SUCCESS The DNS server address was retrieved from the reply packet. + @retval EFI_NOT_READY The reply packet does not contain the DNS server option, or + the DNS server address is not valid. + +**/ +EFI_STATUS +Ip6ConfigParseDhcpReply ( + IN EFI_DHCP6_PROTOCOL *Dhcp6, + IN OUT IP6_CONFIG_INSTANCE *Instance, + IN EFI_DHCP6_PACKET *Reply + ) +{ + EFI_STATUS Status; + UINT32 OptCount; + EFI_DHCP6_PACKET_OPTION **OptList; + UINT16 OpCode; + UINT16 Length; + UINTN Index; + UINTN Index2; + EFI_IPv6_ADDRESS *DnsServer; + IP6_CONFIG_DATA_ITEM *Item; + + // + // A DHCPv6 reply packet is received as the response to our InfoRequest + // packet. + // + OptCount = 0; + Status = Dhcp6->Parse (Dhcp6, Reply, &OptCount, NULL); + if (Status != EFI_BUFFER_TOO_SMALL) { + return EFI_NOT_READY; + } + + OptList = AllocatePool (OptCount * sizeof (EFI_DHCP6_PACKET_OPTION *)); + if (OptList == NULL) { + return EFI_NOT_READY; + } + + Status = Dhcp6->Parse (Dhcp6, Reply, &OptCount, OptList); + if (EFI_ERROR (Status)) { + Status = EFI_NOT_READY; + goto ON_EXIT; + } + + Status = EFI_SUCCESS; + + for (Index = 0; Index < OptCount; Index++) { + // + // Go through all the options to check the ones we are interested in. + // The OpCode and Length are in network byte-order and may not be naturally + // aligned. + // + CopyMem (&OpCode, &OptList[Index]->OpCode, sizeof (OpCode)); + OpCode = NTOHS (OpCode); + + if (OpCode == DHCP6_OPT_DNS_SERVERS) { + CopyMem (&Length, &OptList[Index]->OpLen, sizeof (Length)); + Length = NTOHS (Length); + + if ((Length == 0) || ((Length % sizeof (EFI_IPv6_ADDRESS)) != 0)) { + // + // The length should be a multiple of 16 bytes. + // + Status = EFI_NOT_READY; + break; + } + + // + // Validate the DnsServers: whether they are unicast addresses. + // + DnsServer = (EFI_IPv6_ADDRESS *) OptList[Index]->Data; + for (Index2 = 0; Index2 < Length / sizeof (EFI_IPv6_ADDRESS); Index2++) { + if (!NetIp6IsValidUnicast (DnsServer)) { + Status = EFI_NOT_READY; + goto ON_EXIT; + } + + DnsServer++; + } + + Item = &Instance->DataItem[Ip6ConfigDataTypeDnsServer]; + + if (Item->DataSize != Length) { + if (Item->Data.Ptr != NULL) { + FreePool (Item->Data.Ptr); + } + + Item->Data.Ptr = AllocatePool (Length); + ASSERT (Item->Data.Ptr != NULL); + } + + CopyMem (Item->Data.Ptr, OptList[Index]->Data, Length); + Item->DataSize = Length; + Item->Status = EFI_SUCCESS; + + // + // Signal the waiting events. + // + NetMapIterate (&Item->EventMap, Ip6ConfigSignalEvent, NULL); + + break; + } + } + +ON_EXIT: + + FreePool (OptList); + return Status; +} + +/** + The callback function for Ip6SetAddr. The prototype is defined + as IP6_DAD_CALLBACK. It is called after Duplicate Address Detection is performed + on the tentative address by DHCPv6 in Ip6ConfigOnDhcp6Event(). + + @param[in] IsDadPassed If TRUE, Duplicate Address Detection passes. + @param[in] TargetAddress The tentative IPv6 address to be checked. + @param[in] Context Pointer to the IP6 configuration instance data. + +**/ +VOID +Ip6ConfigSetStatefulAddrCallback ( + IN BOOLEAN IsDadPassed, + IN EFI_IPv6_ADDRESS *TargetAddress, + IN VOID *Context + ) +{ + IP6_CONFIG_INSTANCE *Instance; + + Instance = (IP6_CONFIG_INSTANCE *) Context; + NET_CHECK_SIGNATURE (Instance, IP6_CONFIG_INSTANCE_SIGNATURE); + + // + // We should record the addresses that fail the DAD, and DECLINE them. + // + if (IsDadPassed) { + // + // Decrease the count, no interests in those passed DAD. + // + if (Instance->FailedIaAddressCount > 0 ) { + Instance->FailedIaAddressCount--; + } + } else { + // + // Record it. + // + IP6_COPY_ADDRESS (Instance->DeclineAddress + Instance->DeclineAddressCount, TargetAddress); + Instance->DeclineAddressCount++; + } + + if (Instance->FailedIaAddressCount == Instance->DeclineAddressCount) { + // + // The checking on all addresses are finished. + // + if (Instance->DeclineAddressCount != 0) { + // + // Decline those duplicates. + // + if (Instance->Dhcp6 != NULL) { + Instance->Dhcp6->Decline ( + Instance->Dhcp6, + Instance->DeclineAddressCount, + Instance->DeclineAddress + ); + } + } + + if (Instance->DeclineAddress != NULL) { + FreePool (Instance->DeclineAddress); + } + Instance->DeclineAddress = NULL; + Instance->DeclineAddressCount = 0; + } +} + +/** + The event handle routine when DHCPv6 process is finished or is updated. + + @param[in] Event Not used. + @param[in] Context The pointer to the IP6 configuration instance data. + +**/ +VOID +EFIAPI +Ip6ConfigOnDhcp6Event ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + IP6_CONFIG_INSTANCE *Instance; + EFI_DHCP6_PROTOCOL *Dhcp6; + EFI_STATUS Status; + EFI_DHCP6_MODE_DATA Dhcp6ModeData; + EFI_DHCP6_IA *Ia; + EFI_DHCP6_IA_ADDRESS *IaAddr; + UINT32 Index; + IP6_SERVICE *IpSb; + IP6_ADDRESS_INFO *AddrInfo; + IP6_INTERFACE *IpIf; + + Instance = (IP6_CONFIG_INSTANCE *) Context; + + if ((Instance->Policy != Ip6ConfigPolicyAutomatic) || Instance->OtherInfoOnly) { + // + // IPv6 is not operating in the automatic policy now or + // the DHCPv6 information request message exchange is aborted. + // + return ; + } + + // + // The stateful address autoconfiguration is done or updated. + // + Dhcp6 = Instance->Dhcp6; + + Status = Dhcp6->GetModeData (Dhcp6, &Dhcp6ModeData, NULL); + if (EFI_ERROR (Status)) { + return ; + } + + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + IpIf = IpSb->DefaultInterface; + Ia = Dhcp6ModeData.Ia; + IaAddr = Ia->IaAddress; + + if (Instance->DeclineAddress != NULL) { + FreePool (Instance->DeclineAddress); + } + + Instance->DeclineAddress = (EFI_IPv6_ADDRESS *) AllocatePool (Ia->IaAddressCount * sizeof (EFI_IPv6_ADDRESS)); + if (Instance->DeclineAddress == NULL) { + goto ON_EXIT; + } + + Instance->FailedIaAddressCount = Ia->IaAddressCount; + Instance->DeclineAddressCount = 0; + + for (Index = 0; Index < Ia->IaAddressCount; Index++, IaAddr++) { + if (Ia->IaAddress[Index].ValidLifetime != 0 && Ia->State == Dhcp6Bound) { + // + // Set this address, either it's a new address or with updated lifetimes. + // An appropriate prefix length will be set. + // + Ip6SetAddress ( + IpIf, + &IaAddr->IpAddress, + FALSE, + 0, + IaAddr->ValidLifetime, + IaAddr->PreferredLifetime, + Ip6ConfigSetStatefulAddrCallback, + Instance + ); + } else { + // + // discard this address, artificially decrease the count as if this address + // passed DAD. + // + if (Ip6IsOneOfSetAddress (IpSb, &IaAddr->IpAddress, NULL, &AddrInfo)) { + ASSERT (AddrInfo != NULL); + Ip6RemoveAddr ( + IpSb, + &IpIf->AddressList, + &IpIf->AddressCount, + &AddrInfo->Address, + AddrInfo->PrefixLength + ); + } + + if (Instance->FailedIaAddressCount > 0) { + Instance->FailedIaAddressCount--; + } + } + } + + // + // Parse the Reply packet to get the options we need. + // + if (Dhcp6ModeData.Ia->ReplyPacket != NULL) { + Ip6ConfigParseDhcpReply (Dhcp6, Instance, Dhcp6ModeData.Ia->ReplyPacket); + } + +ON_EXIT: + + FreePool (Dhcp6ModeData.ClientId); + FreePool (Dhcp6ModeData.Ia); +} + +/** + The event process routine when the DHCPv6 server is answered with a reply packet + for an information request. + + @param[in] This Points to the EFI_DHCP6_PROTOCOL. + @param[in] Context The pointer to the IP6 configuration instance data. + @param[in] Packet The DHCPv6 reply packet. + + @retval EFI_SUCCESS The DNS server address was retrieved from the reply packet. + @retval EFI_NOT_READY The reply packet does not contain the DNS server option, or + the DNS server address is not valid. + +**/ +EFI_STATUS +EFIAPI +Ip6ConfigOnDhcp6Reply ( + IN EFI_DHCP6_PROTOCOL *This, + IN VOID *Context, + IN EFI_DHCP6_PACKET *Packet + ) +{ + return Ip6ConfigParseDhcpReply (This, (IP6_CONFIG_INSTANCE *) Context, Packet); +} + +/** + The event process routine when the DHCPv6 service binding protocol is installed + in the system. + + @param[in] Event Not used. + @param[in] Context The pointer to the IP6 config instance data. + +**/ +VOID +EFIAPI +Ip6ConfigOnDhcp6SbInstalled ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + IP6_CONFIG_INSTANCE *Instance; + + Instance = (IP6_CONFIG_INSTANCE *) Context; + + if ((Instance->Dhcp6Handle != NULL) || (Instance->Policy != Ip6ConfigPolicyAutomatic)) { + // + // The DHCP6 child is already created or the policy is no longer AUTOMATIC. + // + return ; + } + + Ip6ConfigStartStatefulAutoConfig (Instance, Instance->OtherInfoOnly); +} + +/** + Set the configuration for the EFI IPv6 network stack running on the communication + device this EFI IPv6 Configuration Protocol instance manages. + + This function is used to set the configuration data of type DataType for the EFI + IPv6 network stack that is running on the communication device that this EFI IPv6 + Configuration Protocol instance manages. + + DataSize is used to calculate the count of structure instances in the Data for + a DataType in which multiple structure instances are allowed. + + This function is always non-blocking. When setting some type of configuration data, + an asynchronous process is invoked to check the correctness of the data, such as + performing Duplicate Address Detection on the manually set local IPv6 addresses. + EFI_NOT_READY is returned immediately to indicate that such an asynchronous process + is invoked, and the process is not finished yet. The caller wanting to get the result + of the asynchronous process is required to call RegisterDataNotify() to register an + event on the specified configuration data. Once the event is signaled, the caller + can call GetData() to obtain the configuration data and know the result. + For other types of configuration data that do not require an asynchronous configuration + process, the result of the operation is immediately returned. + + @param[in] This The pointer to the EFI_IP6_CONFIG_PROTOCOL instance. + @param[in] DataType The type of data to set. + @param[in] DataSize Size of the buffer pointed to by Data in bytes. + @param[in] Data The data buffer to set. The type of the data buffer is + associated with the DataType. + + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set successfully. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + - This is NULL. + - One or more fields in Data and DataSizedo not match the + requirement of the data type indicated by DataType. + @retval EFI_WRITE_PROTECTED The specified configuration data is read-only or the specified + configuration data cannot be set under the current policy. + @retval EFI_ACCESS_DENIED Another set operation on the specified configuration + data is already in process. + @retval EFI_NOT_READY An asynchronous process was invoked to set the specified + configuration data, and the process is not finished yet. + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type + indicated by DataType. + @retval EFI_UNSUPPORTED This DataType is not supported. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_DEVICE_ERROR An unexpected system error or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiIp6ConfigSetData ( + IN EFI_IP6_CONFIG_PROTOCOL *This, + IN EFI_IP6_CONFIG_DATA_TYPE DataType, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + IP6_CONFIG_INSTANCE *Instance; + IP6_SERVICE *IpSb; + + if ((This == NULL) || (Data == NULL && DataSize != 0) || (Data != NULL && DataSize == 0)) { + return EFI_INVALID_PARAMETER; + } + + if (DataType >= Ip6ConfigDataTypeMaximum) { + return EFI_UNSUPPORTED; + } + + Instance = IP6_CONFIG_INSTANCE_FROM_PROTOCOL (This); + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + if (IpSb->LinkLocalDadFail) { + return EFI_DEVICE_ERROR; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Status = Instance->DataItem[DataType].Status; + if (Status != EFI_NOT_READY) { + + if (Instance->DataItem[DataType].SetData == NULL) { + // + // This type of data is readonly. + // + Status = EFI_WRITE_PROTECTED; + } else { + + Status = Instance->DataItem[DataType].SetData (Instance, DataSize, Data); + if (!EFI_ERROR (Status)) { + // + // Fire up the events registered with this type of data. + // + NetMapIterate (&Instance->DataItem[DataType].EventMap, Ip6ConfigSignalEvent, NULL); + Ip6ConfigWriteConfigData (IpSb->MacString, Instance); + } else if (Status == EFI_ABORTED) { + // + // The SetData is aborted because the data to set is the same with + // the one maintained. + // + Status = EFI_SUCCESS; + NetMapIterate (&Instance->DataItem[DataType].EventMap, Ip6ConfigSignalEvent, NULL); + } + } + } else { + // + // Another asynchornous process is on the way. + // + Status = EFI_ACCESS_DENIED; + } + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Get the configuration data for the EFI IPv6 network stack running on the communication + device that this EFI IPv6 Configuration Protocol instance manages. + + This function returns the configuration data of type DataType for the EFI IPv6 network + stack running on the communication device that this EFI IPv6 Configuration Protocol instance + manages. + + The caller is responsible for allocating the buffer used to return the specified + configuration data. The required size will be returned to the caller if the size of + the buffer is too small. + + EFI_NOT_READY is returned if the specified configuration data is not ready due to an + asynchronous configuration process already in progress. The caller can call RegisterDataNotify() + to register an event on the specified configuration data. Once the asynchronous configuration + process is finished, the event will be signaled, and a subsequent GetData() call will return + the specified configuration data. + + @param[in] This Pointer to the EFI_IP6_CONFIG_PROTOCOL instance. + @param[in] DataType The type of data to get. + @param[in, out] DataSize On input, in bytes, the size of Data. On output, in bytes, the + size of buffer required to store the specified configuration data. + @param[in] Data The data buffer in which the configuration data is returned. The + type of the data buffer is associated with the DataType. + This is an optional parameter that may be NULL. + + @retval EFI_SUCCESS The specified configuration data was obtained successfully. + @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE: + - This is NULL. + - DataSize is NULL. + - Data is NULL if *DataSize is not zero. + @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified configuration data, + and the required size is returned in DataSize. + @retval EFI_NOT_READY The specified configuration data is not ready due to an + asynchronous configuration process already in progress. + @retval EFI_NOT_FOUND The specified configuration data is not found. + +**/ +EFI_STATUS +EFIAPI +EfiIp6ConfigGetData ( + IN EFI_IP6_CONFIG_PROTOCOL *This, + IN EFI_IP6_CONFIG_DATA_TYPE DataType, + IN OUT UINTN *DataSize, + IN VOID *Data OPTIONAL + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + IP6_CONFIG_INSTANCE *Instance; + IP6_CONFIG_DATA_ITEM *DataItem; + + if ((This == NULL) || (DataSize == NULL) || ((*DataSize != 0) && (Data == NULL))) { + return EFI_INVALID_PARAMETER; + } + + if (DataType >= Ip6ConfigDataTypeMaximum) { + return EFI_NOT_FOUND; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = IP6_CONFIG_INSTANCE_FROM_PROTOCOL (This); + DataItem = &Instance->DataItem[DataType]; + + Status = Instance->DataItem[DataType].Status; + if (!EFI_ERROR (Status)) { + + if (DataItem->GetData != NULL) { + + Status = DataItem->GetData (Instance, DataSize, Data); + } else if (*DataSize < Instance->DataItem[DataType].DataSize) { + // + // Update the buffer length. + // + *DataSize = Instance->DataItem[DataType].DataSize; + Status = EFI_BUFFER_TOO_SMALL; + } else { + + *DataSize = Instance->DataItem[DataType].DataSize; + CopyMem (Data, Instance->DataItem[DataType].Data.Ptr, *DataSize); + } + } + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Register an event that is signaled whenever a configuration process on the specified + configuration data is done. + + This function registers an event that is to be signaled whenever a configuration + process on the specified configuration data is performed. An event can be registered + for a different DataType simultaneously. The caller is responsible for determining + which type of configuration data causes the signaling of the event in such an event. + + @param[in] This Pointer to the EFI_IP6_CONFIG_PROTOCOL instance. + @param[in] DataType The type of data to unregister the event for. + @param[in] Event The event to register. + + @retval EFI_SUCCESS The notification event for the specified configuration data is + registered. + @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL. + @retval EFI_UNSUPPORTED The configuration data type specified by DataType is not + supported. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_ACCESS_DENIED The Event is already registered for the DataType. + +**/ +EFI_STATUS +EFIAPI +EfiIp6ConfigRegisterDataNotify ( + IN EFI_IP6_CONFIG_PROTOCOL *This, + IN EFI_IP6_CONFIG_DATA_TYPE DataType, + IN EFI_EVENT Event + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + IP6_CONFIG_INSTANCE *Instance; + NET_MAP *EventMap; + NET_MAP_ITEM *Item; + + if ((This == NULL) || (Event == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (DataType >= Ip6ConfigDataTypeMaximum) { + return EFI_UNSUPPORTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = IP6_CONFIG_INSTANCE_FROM_PROTOCOL (This); + EventMap = &Instance->DataItem[DataType].EventMap; + + // + // Check whether this event is already registered for this DataType. + // + Item = NetMapFindKey (EventMap, Event); + if (Item == NULL) { + + Status = NetMapInsertTail (EventMap, Event, NULL); + + if (EFI_ERROR (Status)) { + + Status = EFI_OUT_OF_RESOURCES; + } + + } else { + + Status = EFI_ACCESS_DENIED; + } + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Remove a previously registered event for the specified configuration data. + + @param This The pointer to the EFI_IP6_CONFIG_PROTOCOL instance. + @param DataType The type of data to remove from the previously + registered event. + @param Event The event to be unregistered. + + @retval EFI_SUCCESS The event registered for the specified + configuration data was removed. + @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL. + @retval EFI_NOT_FOUND The Event has not been registered for the + specified DataType. + +**/ +EFI_STATUS +EFIAPI +EfiIp6ConfigUnregisterDataNotify ( + IN EFI_IP6_CONFIG_PROTOCOL *This, + IN EFI_IP6_CONFIG_DATA_TYPE DataType, + IN EFI_EVENT Event + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + IP6_CONFIG_INSTANCE *Instance; + NET_MAP_ITEM *Item; + + if ((This == NULL) || (Event == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (DataType >= Ip6ConfigDataTypeMaximum) { + return EFI_NOT_FOUND; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = IP6_CONFIG_INSTANCE_FROM_PROTOCOL (This); + + Item = NetMapFindKey (&Instance->DataItem[DataType].EventMap, Event); + if (Item != NULL) { + + NetMapRemoveItem (&Instance->DataItem[DataType].EventMap, Item, NULL); + Status = EFI_SUCCESS; + } else { + + Status = EFI_NOT_FOUND; + } + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Initialize an IP6_CONFIG_INSTANCE. + + @param[out] Instance The buffer of IP6_CONFIG_INSTANCE to be initialized. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation. + @retval EFI_SUCCESS The IP6_CONFIG_INSTANCE initialized successfully. + +**/ +EFI_STATUS +Ip6ConfigInitInstance ( + OUT IP6_CONFIG_INSTANCE *Instance + ) +{ + IP6_SERVICE *IpSb; + IP6_CONFIG_INSTANCE *TmpInstance; + LIST_ENTRY *Entry; + EFI_STATUS Status; + UINTN Index; + UINT16 IfIndex; + IP6_CONFIG_DATA_ITEM *DataItem; + + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + + Instance->Signature = IP6_CONFIG_INSTANCE_SIGNATURE; + + // + // Determine the index of this interface. + // + IfIndex = 0; + NET_LIST_FOR_EACH (Entry, &mIp6ConfigInstanceList) { + TmpInstance = NET_LIST_USER_STRUCT_S (Entry, IP6_CONFIG_INSTANCE, Link, IP6_CONFIG_INSTANCE_SIGNATURE); + + if (TmpInstance->IfIndex > IfIndex) { + // + // There is a sequence hole because some interface is down. + // + break; + } + + IfIndex++; + } + + Instance->IfIndex = IfIndex; + NetListInsertBefore (Entry, &Instance->Link); + + for (Index = 0; Index < Ip6ConfigDataTypeMaximum; Index++) { + // + // Initialize the event map for each data item. + // + NetMapInit (&Instance->DataItem[Index].EventMap); + } + + // + // Initialize the NET_MAPs used for DAD on manually configured source addresses. + // + NetMapInit (&Instance->DadFailedMap); + NetMapInit (&Instance->DadPassedMap); + + // + // Initialize each data type: associate storage and set data size for the + // fixed size data types, hook the SetData function, set the data attribute. + // + DataItem = &Instance->DataItem[Ip6ConfigDataTypeInterfaceInfo]; + DataItem->GetData = Ip6ConfigGetIfInfo; + DataItem->Data.Ptr = &Instance->InterfaceInfo; + DataItem->DataSize = sizeof (Instance->InterfaceInfo); + SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED | DATA_ATTRIB_VOLATILE); + Ip6ConfigInitIfInfo (IpSb, &Instance->InterfaceInfo); + + DataItem = &Instance->DataItem[Ip6ConfigDataTypeAltInterfaceId]; + DataItem->SetData = Ip6ConfigSetAltIfId; + DataItem->Data.Ptr = &Instance->AltIfId; + DataItem->DataSize = sizeof (Instance->AltIfId); + DataItem->Status = EFI_NOT_FOUND; + SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED); + + DataItem = &Instance->DataItem[Ip6ConfigDataTypePolicy]; + DataItem->SetData = Ip6ConfigSetPolicy; + DataItem->Data.Ptr = &Instance->Policy; + DataItem->DataSize = sizeof (Instance->Policy); + Instance->Policy = Ip6ConfigPolicyManual; + SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED); + + DataItem = &Instance->DataItem[Ip6ConfigDataTypeDupAddrDetectTransmits]; + DataItem->SetData = Ip6ConfigSetDadXmits; + DataItem->Data.Ptr = &Instance->DadXmits; + DataItem->DataSize = sizeof (Instance->DadXmits); + Instance->DadXmits.DupAddrDetectTransmits = IP6_CONFIG_DEFAULT_DAD_XMITS; + SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED); + + DataItem = &Instance->DataItem[Ip6ConfigDataTypeManualAddress]; + DataItem->SetData = Ip6ConfigSetManualAddress; + DataItem->Status = EFI_NOT_FOUND; + + DataItem = &Instance->DataItem[Ip6ConfigDataTypeGateway]; + DataItem->SetData = Ip6ConfigSetGateway; + DataItem->Status = EFI_NOT_FOUND; + + DataItem = &Instance->DataItem[Ip6ConfigDataTypeDnsServer]; + DataItem->SetData = Ip6ConfigSetDnsServer; + DataItem->Status = EFI_NOT_FOUND; + + // + // Create the event used for DHCP. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + Ip6ConfigOnDhcp6Event, + Instance, + &Instance->Dhcp6Event + ); + ASSERT_EFI_ERROR (Status); + + Instance->Configured = TRUE; + + // + // Try to read the config data from NV variable. + // + Status = Ip6ConfigReadConfigData (IpSb->MacString, Instance); + if (Status == EFI_NOT_FOUND) { + // + // The NV variable is not set, so generate a random IAID, and write down the + // fresh new configuration as the NV variable now. + // + Instance->IaId = NET_RANDOM (NetRandomInitSeed ()); + + for (Index = 0; Index < IpSb->SnpMode.HwAddressSize; Index++) { + Instance->IaId |= (IpSb->SnpMode.CurrentAddress.Addr[Index] << ((Index << 3) & 31)); + } + + Ip6ConfigWriteConfigData (IpSb->MacString, Instance); + } else if (EFI_ERROR (Status)) { + return Status; + } + + Instance->Ip6Config.SetData = EfiIp6ConfigSetData; + Instance->Ip6Config.GetData = EfiIp6ConfigGetData; + Instance->Ip6Config.RegisterDataNotify = EfiIp6ConfigRegisterDataNotify; + Instance->Ip6Config.UnregisterDataNotify = EfiIp6ConfigUnregisterDataNotify; + + + // + // Publish the IP6 configuration form + // + return Ip6ConfigFormInit (Instance); +} + +/** + Release an IP6_CONFIG_INSTANCE. + + @param[in, out] Instance The buffer of IP6_CONFIG_INSTANCE to be freed. + +**/ +VOID +Ip6ConfigCleanInstance ( + IN OUT IP6_CONFIG_INSTANCE *Instance + ) +{ + UINTN Index; + IP6_CONFIG_DATA_ITEM *DataItem; + + if (Instance->DeclineAddress != NULL) { + FreePool (Instance->DeclineAddress); + } + + if (!Instance->Configured) { + return ; + } + + if (Instance->Dhcp6Handle != NULL) { + + Ip6ConfigDestroyDhcp6 (Instance); + } + + // + // Close the event. + // + if (Instance->Dhcp6Event != NULL) { + gBS->CloseEvent (Instance->Dhcp6Event); + } + + NetMapClean (&Instance->DadPassedMap); + NetMapClean (&Instance->DadFailedMap); + + for (Index = 0; Index < Ip6ConfigDataTypeMaximum; Index++) { + + DataItem = &Instance->DataItem[Index]; + + if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED)) { + if (DataItem->Data.Ptr != NULL) { + FreePool (DataItem->Data.Ptr); + } + DataItem->Data.Ptr = NULL; + DataItem->DataSize = 0; + } + + NetMapClean (&Instance->DataItem[Index].EventMap); + } + + Ip6ConfigFormUnload (Instance); + + RemoveEntryList (&Instance->Link); +} + +/** + Destroy the Dhcp6 child in IP6_CONFIG_INSTANCE and release the resources. + + @param[in, out] Instance The buffer of IP6_CONFIG_INSTANCE to be freed. + + @retval EFI_SUCCESS The child was successfully destroyed. + @retval Others Failed to destroy the child. + +**/ +EFI_STATUS +Ip6ConfigDestroyDhcp6 ( + IN OUT IP6_CONFIG_INSTANCE *Instance + ) +{ + IP6_SERVICE *IpSb; + EFI_STATUS Status; + EFI_DHCP6_PROTOCOL *Dhcp6; + + Dhcp6 = Instance->Dhcp6; + ASSERT (Dhcp6 != NULL); + + Dhcp6->Stop (Dhcp6); + Dhcp6->Configure (Dhcp6, NULL); + Instance->Dhcp6 = NULL; + + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + + // + // Close DHCPv6 protocol and destroy the child. + // + Status = gBS->CloseProtocol ( + Instance->Dhcp6Handle, + &gEfiDhcp6ProtocolGuid, + IpSb->Image, + IpSb->Controller + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = NetLibDestroyServiceChild ( + IpSb->Controller, + IpSb->Image, + &gEfiDhcp6ServiceBindingProtocolGuid, + Instance->Dhcp6Handle + ); + + Instance->Dhcp6Handle = NULL; + + return Status; +} + diff --git a/NetworkPkg/Ip6Dxe/Ip6ConfigImpl.h b/NetworkPkg/Ip6Dxe/Ip6ConfigImpl.h new file mode 100644 index 000000000..bfe3bbe53 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6ConfigImpl.h @@ -0,0 +1,307 @@ +/** @file + Definitions for EFI IPv6 Configuartion Protocol implementation. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __IP6_CONFIG_IMPL_H__ +#define __IP6_CONFIG_IMPL_H__ + +#define IP6_CONFIG_INSTANCE_SIGNATURE SIGNATURE_32 ('I', 'P', '6', 'C') +#define IP6_FORM_CALLBACK_INFO_SIGNATURE SIGNATURE_32 ('I', 'F', 'C', 'I') +#define IP6_CONFIG_VARIABLE_ATTRIBUTE (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS) + +#define IP6_CONFIG_DEFAULT_DAD_XMITS 1 + +#define DATA_ATTRIB_SIZE_FIXED 0x1 +#define DATA_ATTRIB_VOLATILE 0x2 + +#define DATA_ATTRIB_SET(Attrib, Bits) (BOOLEAN)((Attrib) & (Bits)) +#define SET_DATA_ATTRIB(Attrib, Bits) ((Attrib) |= (Bits)) + +typedef struct _IP6_CONFIG_INSTANCE IP6_CONFIG_INSTANCE; + +#define IP6_CONFIG_INSTANCE_FROM_PROTOCOL(Proto) \ + CR ((Proto), \ + IP6_CONFIG_INSTANCE, \ + Ip6Config, \ + IP6_CONFIG_INSTANCE_SIGNATURE \ + ) + + +#define IP6_CONFIG_INSTANCE_FROM_FORM_CALLBACK(Callback) \ + CR ((Callback), \ + IP6_CONFIG_INSTANCE, \ + CallbackInfo, \ + IP6_CONFIG_INSTANCE_SIGNATURE \ + ) + +#define IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE(Instance) \ + CR ((Instance), \ + IP6_SERVICE, \ + Ip6ConfigInstance, \ + IP6_SERVICE_SIGNATURE \ + ) + +#define IP6_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS(ConfigAccess) \ + CR ((ConfigAccess), \ + IP6_FORM_CALLBACK_INFO, \ + HiiConfigAccess, \ + IP6_FORM_CALLBACK_INFO_SIGNATURE \ + ) + +/** + The prototype of work function for EfiIp6ConfigSetData(). + + @param[in] Instance The pointer to the IP6 config instance data. + @param[in] DataSize In bytes, the size of the buffer pointed to by Data. + @param[in] Data The data buffer to set. + + @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type, + 8 bytes. + @retval EFI_SUCCESS The specified configuration data for the EFI IPv6 + network stack was set successfully. + +**/ +typedef +EFI_STATUS +(*IP6_CONFIG_SET_DATA) ( + IN IP6_CONFIG_INSTANCE *Instance, + IN UINTN DataSize, + IN VOID *Data + ); + +/** + The prototype of work function for EfiIp6ConfigGetData(). + + @param[in] Instance The pointer to the IP6 config instance data. + @param[in, out] DataSize On input, in bytes, the size of Data. On output, in + bytes, the size of buffer required to store the specified + configuration data. + @param[in] Data The data buffer in which the configuration data is returned. + Ignored if DataSize is ZERO. + + @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified + configuration data, and the required size is + returned in DataSize. + @retval EFI_SUCCESS The specified configuration data was obtained successfully. + +**/ +typedef +EFI_STATUS +(*IP6_CONFIG_GET_DATA) ( + IN IP6_CONFIG_INSTANCE *Instance, + IN OUT UINTN *DataSize, + IN VOID *Data OPTIONAL + ); + +typedef union { + VOID *Ptr; + EFI_IP6_CONFIG_INTERFACE_INFO *IfInfo; + EFI_IP6_CONFIG_INTERFACE_ID *AltIfId; + EFI_IP6_CONFIG_POLICY *Policy; + EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS *DadXmits; + EFI_IP6_CONFIG_MANUAL_ADDRESS *ManualAddress; + EFI_IPv6_ADDRESS *Gateway; + EFI_IPv6_ADDRESS *DnsServers; +} IP6_CONFIG_DATA; + +typedef struct { + IP6_CONFIG_SET_DATA SetData; + IP6_CONFIG_GET_DATA GetData; + EFI_STATUS Status; + UINT8 Attribute; + NET_MAP EventMap; + IP6_CONFIG_DATA Data; + UINTN DataSize; +} IP6_CONFIG_DATA_ITEM; + +typedef struct { + UINT16 Offset; + UINT32 DataSize; + EFI_IP6_CONFIG_DATA_TYPE DataType; +} IP6_CONFIG_DATA_RECORD; + +#pragma pack(1) + +// +// heap data that contains the data for each data record. +// +// BOOLEAN IsAltIfIdSet; +// EFI_IP6_CONFIG_POLICY Policy; +// EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS DadXmits; +// UINT32 ManualaddressCount; +// UINT32 GatewayCount; +// UINT32 DnsServersCount; +// EFI_IP6_CONFIG_INTERFACE_ID AltIfId; +// EFI_IP6_CONFIG_MANUAL_ADDRESS ManualAddress[]; +// EFI_IPv6_ADDRESS Gateway[]; +// EFI_IPv6_ADDRESS DnsServers[]; +// +typedef struct { + UINT32 IaId; + UINT16 Checksum; + UINT16 DataRecordCount; + IP6_CONFIG_DATA_RECORD DataRecord[1]; +} IP6_CONFIG_VARIABLE; + +#pragma pack() + +typedef struct { + LIST_ENTRY Link; + EFI_IP6_ADDRESS_INFO AddrInfo; +} IP6_ADDRESS_INFO_ENTRY; + +typedef struct { + EFI_IP6_CONFIG_POLICY Policy; ///< manual or automatic + EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS DadTransmitCount; ///< dad transmits count + EFI_IP6_CONFIG_INTERFACE_ID InterfaceId; ///< alternative interface id + LIST_ENTRY ManualAddress; ///< IP addresses + UINT32 ManualAddressCount; ///< IP addresses count + LIST_ENTRY GatewayAddress; ///< Gateway address + UINT32 GatewayAddressCount; ///< Gateway address count + LIST_ENTRY DnsAddress; ///< DNS server address + UINT32 DnsAddressCount; ///< DNS server address count +} IP6_CONFIG_NVDATA; + +typedef struct _IP6_FORM_CALLBACK_INFO { + UINT32 Signature; + EFI_HANDLE ChildHandle; + EFI_HII_CONFIG_ACCESS_PROTOCOL HiiConfigAccess; + EFI_DEVICE_PATH_PROTOCOL *HiiVendorDevicePath; + EFI_HII_HANDLE RegisteredHandle; +} IP6_FORM_CALLBACK_INFO; + +struct _IP6_CONFIG_INSTANCE { + UINT32 Signature; + BOOLEAN Configured; + LIST_ENTRY Link; + UINT16 IfIndex; + + EFI_IP6_CONFIG_INTERFACE_INFO InterfaceInfo; + EFI_IP6_CONFIG_INTERFACE_ID AltIfId; + EFI_IP6_CONFIG_POLICY Policy; + EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS DadXmits; + + IP6_CONFIG_DATA_ITEM DataItem[Ip6ConfigDataTypeMaximum]; + NET_MAP DadFailedMap; + NET_MAP DadPassedMap; + + EFI_IP6_CONFIG_PROTOCOL Ip6Config; + + EFI_EVENT Dhcp6SbNotifyEvent; + VOID *Registration; + EFI_HANDLE Dhcp6Handle; + EFI_DHCP6_PROTOCOL *Dhcp6; + BOOLEAN OtherInfoOnly; + UINT32 IaId; + EFI_EVENT Dhcp6Event; + UINT32 FailedIaAddressCount; + EFI_IPv6_ADDRESS *DeclineAddress; + UINT32 DeclineAddressCount; + + IP6_FORM_CALLBACK_INFO CallbackInfo; + IP6_CONFIG_NVDATA Ip6NvData; +}; + +/** + Read the configuration data from variable storage according to the VarName and + gEfiIp6ConfigProtocolGuid. It checks the integrity of variable data. If the + data is corrupted, it clears the variable data to ZERO. Othewise, it outputs the + configuration data to IP6_CONFIG_INSTANCE. + + @param[in] VarName The pointer to the variable name + @param[in, out] Instance The pointer to the IP6 config instance data. + + @retval EFI_NOT_FOUND The variable can not be found or already corrupted. + @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation. + @retval EFI_SUCCESS The configuration data was retrieved successfully. + +**/ +EFI_STATUS +Ip6ConfigReadConfigData ( + IN CHAR16 *VarName, + IN OUT IP6_CONFIG_INSTANCE *Instance + ); + +/** + The event process routine when the DHCPv6 server is answered with a reply packet + for an information request. + + @param[in] This Points to the EFI_DHCP6_PROTOCOL. + @param[in] Context The pointer to the IP6 configuration instance data. + @param[in] Packet The DHCPv6 reply packet. + + @retval EFI_SUCCESS The DNS server address was retrieved from the reply packet. + @retval EFI_NOT_READY The reply packet does not contain the DNS server option, or + the DNS server address is not valid. + +**/ +EFI_STATUS +EFIAPI +Ip6ConfigOnDhcp6Reply ( + IN EFI_DHCP6_PROTOCOL *This, + IN VOID *Context, + IN EFI_DHCP6_PACKET *Packet + ); + +/** + The work function to trigger the DHCPv6 process to perform a stateful autoconfiguration. + + @param[in] Instance Pointer to the IP6 config instance data. + @param[in] OtherInfoOnly If FALSE, get stateful address and other information + via DHCPv6. Otherwise, only get the other information. + + @retval EFI_SUCCESS The operation finished successfully. + @retval EFI_UNSUPPORTED The DHCP6 driver is not available. + +**/ +EFI_STATUS +Ip6ConfigStartStatefulAutoConfig ( + IN IP6_CONFIG_INSTANCE *Instance, + IN BOOLEAN OtherInfoOnly + ); + +/** + Initialize an IP6_CONFIG_INSTANCE. + + @param[out] Instance The buffer of IP6_CONFIG_INSTANCE to be initialized. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation. + @retval EFI_SUCCESS The IP6_CONFIG_INSTANCE initialized successfully. + +**/ +EFI_STATUS +Ip6ConfigInitInstance ( + OUT IP6_CONFIG_INSTANCE *Instance + ); + +/** + Release an IP6_CONFIG_INSTANCE. + + @param[in, out] Instance The buffer of IP6_CONFIG_INSTANCE to be freed. + +**/ +VOID +Ip6ConfigCleanInstance ( + IN OUT IP6_CONFIG_INSTANCE *Instance + ); + +/** + Destroy the Dhcp6 child in IP6_CONFIG_INSTANCE and release the resources. + + @param[in, out] Instance The buffer of IP6_CONFIG_INSTANCE to be freed. + + @retval EFI_SUCCESS The child was successfully destroyed. + @retval Others Failed to destroy the child. + +**/ +EFI_STATUS +Ip6ConfigDestroyDhcp6 ( + IN OUT IP6_CONFIG_INSTANCE *Instance + ); + +#endif diff --git a/NetworkPkg/Ip6Dxe/Ip6ConfigNv.c b/NetworkPkg/Ip6Dxe/Ip6ConfigNv.c new file mode 100644 index 000000000..aac665d3d --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6ConfigNv.c @@ -0,0 +1,2089 @@ +/** @file + Helper functions for configuring or obtaining the parameters relating to IP6. + + Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +CHAR16 mIp6ConfigStorageName[] = L"IP6_CONFIG_IFR_NVDATA"; + +/** + The notify function of create event when performing a manual configuration. + + @param[in] Event The pointer of Event. + @param[in] Context The pointer of Context. + +**/ +VOID +EFIAPI +Ip6ConfigManualAddressNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + *((BOOLEAN *) Context) = TRUE; +} + +/** + Get the configuration data for the EFI IPv6 network stack running on the + communication. It is a help function to the call EfiIp6ConfigGetData(). + + @param[in] Ip6Config The pointer to the EFI_IP6_CONFIG_PROTOCOL instance. + @param[in] DataType The type of data to get. + @param[out] DataSize The size of buffer required in bytes. + @param[out] Data The data buffer in which the configuration data is returned. The + type of the data buffer associated with the DataType. + It is the caller's responsibility to free the resource. + + @retval EFI_SUCCESS The specified configuration data was obtained successfully. + @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE: + - Ip6Config is NULL or invalid. + - DataSize is NULL. + - Data is NULL. + @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resources. + @retval EFI_NOT_READY The specified configuration data is not ready due to an + asynchronous configuration process already in progress. + @retval EFI_NOT_FOUND The specified configuration data was not found. + +**/ +EFI_STATUS +Ip6ConfigNvGetData ( + IN EFI_IP6_CONFIG_PROTOCOL *Ip6Config, + IN EFI_IP6_CONFIG_DATA_TYPE DataType, + OUT UINTN *DataSize, + OUT VOID **Data + ) +{ + UINTN BufferSize; + VOID *Buffer; + EFI_STATUS Status; + + if ((Ip6Config == NULL) || (Data == NULL) || (DataSize == NULL)) { + return EFI_INVALID_PARAMETER; + } + + BufferSize = 0; + Status = Ip6Config->GetData ( + Ip6Config, + DataType, + &BufferSize, + NULL + ); + if (Status != EFI_BUFFER_TOO_SMALL) { + return Status; + } + + Buffer = AllocateZeroPool (BufferSize); + if (Buffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = Ip6Config->GetData ( + Ip6Config, + DataType, + &BufferSize, + Buffer + ); + if (EFI_ERROR (Status)) { + FreePool (Buffer); + return Status; + } + + *DataSize = BufferSize; + *Data = Buffer; + + return EFI_SUCCESS; +} + +/** + Free all nodes in IP6_ADDRESS_INFO_ENTRY in the list array specified + with ListHead. + + @param[in] ListHead The head of the list array in IP6_ADDRESS_INFO_ENTRY. + +**/ +VOID +Ip6FreeAddressInfoList ( + IN LIST_ENTRY *ListHead + ) +{ + IP6_ADDRESS_INFO_ENTRY *Node; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, ListHead) { + Node = NET_LIST_USER_STRUCT (Entry, IP6_ADDRESS_INFO_ENTRY, Link); + RemoveEntryList (&Node->Link); + FreePool (Node); + } +} + +/** + Convert the IPv6 address into a formatted string. + + @param[in] Ip6 The IPv6 address. + @param[out] Str The formatted IP string. + +**/ +VOID +Ip6ToStr ( + IN EFI_IPv6_ADDRESS *Ip6, + OUT CHAR16 *Str + ) +{ + UINTN Index; + BOOLEAN Short; + UINTN Number; + CHAR16 FormatString[8]; + + Short = FALSE; + + for (Index = 0; Index < 15; Index = Index + 2) { + if (!Short && + Index % 2 == 0 && + Ip6->Addr[Index] == 0 && + Ip6->Addr[Index + 1] == 0 + ) { + // + // Deal with the case of ::. + // + if (Index == 0) { + *Str = L':'; + *(Str + 1) = L':'; + Str = Str + 2; + } else { + *Str = L':'; + Str = Str + 1; + } + + while ((Index < 15) && (Ip6->Addr[Index] == 0) && (Ip6->Addr[Index + 1] == 0)) { + Index = Index + 2; + } + + Short = TRUE; + + if (Index == 16) { + // + // :: is at the end of the address. + // + *Str = L'\0'; + break; + } + } + + ASSERT (Index < 15); + + if (Ip6->Addr[Index] == 0) { + Number = UnicodeSPrint (Str, 2 * IP6_STR_MAX_SIZE, L"%x:", (UINTN) Ip6->Addr[Index + 1]); + } else { + if (Ip6->Addr[Index + 1] < 0x10) { + CopyMem (FormatString, L"%x0%x:", StrSize (L"%x0%x:")); + } else { + CopyMem (FormatString, L"%x%x:", StrSize (L"%x%x:")); + } + + Number = UnicodeSPrint ( + Str, + 2 * IP6_STR_MAX_SIZE, + (CONST CHAR16 *) FormatString, + (UINTN) Ip6->Addr[Index], + (UINTN) Ip6->Addr[Index + 1] + ); + } + + Str = Str + Number; + + if (Index + 2 == 16) { + *Str = L'\0'; + if (*(Str - 1) == L':') { + *(Str - 1) = L'\0'; + } + } + } +} + +/** + Convert EFI_IP6_CONFIG_INTERFACE_ID to string format. + + @param[out] String The buffer to store the converted string. + @param[in] IfId The pointer of EFI_IP6_CONFIG_INTERFACE_ID. + + @retval EFI_SUCCESS The string converted successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + +**/ +EFI_STATUS +Ip6ConvertInterfaceIdToString ( + OUT CHAR16 *String, + IN EFI_IP6_CONFIG_INTERFACE_ID *IfId + ) +{ + UINT8 Index; + UINTN Number; + + if ((String == NULL) || (IfId == NULL)) { + return EFI_INVALID_PARAMETER; + } + + for (Index = 0; Index < 8; Index++) { + Number = UnicodeSPrint ( + String, + 2 * INTERFACE_ID_STR_STORAGE, + L"%x:", + (UINTN) IfId->Id[Index] + ); + String = String + Number; + } + + *(String - 1) = '\0'; + + return EFI_SUCCESS; +} + +/** + Parse InterfaceId in string format and convert it to EFI_IP6_CONFIG_INTERFACE_ID. + + @param[in] String The buffer of the string to be parsed. + @param[out] IfId The pointer of EFI_IP6_CONFIG_INTERFACE_ID. + + @retval EFI_SUCCESS The operation finished successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + +**/ +EFI_STATUS +Ip6ParseInterfaceIdFromString ( + IN CONST CHAR16 *String, + OUT EFI_IP6_CONFIG_INTERFACE_ID *IfId + ) +{ + UINT8 Index; + CHAR16 *IfIdStr; + CHAR16 *TempStr; + UINTN NodeVal; + + if ((String == NULL) || (IfId == NULL)) { + return EFI_INVALID_PARAMETER; + } + + IfIdStr = (CHAR16 *) String; + + ZeroMem (IfId, sizeof (EFI_IP6_CONFIG_INTERFACE_ID)); + + for (Index = 0; Index < 8; Index++) { + TempStr = IfIdStr; + + while ((*IfIdStr != L'\0') && (*IfIdStr != L':')) { + IfIdStr++; + } + + // + // The InterfaceId format is X:X:X:X, the number of X should not exceed 8. + // If the number of X is less than 8, zero is appended to the InterfaceId. + // + if ((*IfIdStr == ':') && (Index == 7)) { + return EFI_INVALID_PARAMETER; + } + + // + // Convert the string to interface id. AsciiStrHexToUintn stops at the + // first character that is not a valid hex character, ':' or '\0' here. + // + NodeVal = StrHexToUintn (TempStr); + if (NodeVal > 0xFF) { + return EFI_INVALID_PARAMETER; + } + + IfId->Id[Index] = (UINT8) NodeVal; + + IfIdStr++; + } + + return EFI_SUCCESS; +} + +/** + Create Hii Extend Label OpCode as the start opcode and end opcode. It is + a help function. + + @param[in] StartLabelNumber The number of start label. + @param[out] StartOpCodeHandle Points to the start opcode handle. + @param[out] StartLabel Points to the created start opcode. + @param[out] EndOpCodeHandle Points to the end opcode handle. + @param[out] EndLabel Points to the created end opcode. + + @retval EFI_OUT_OF_RESOURCES Does not have sufficient resources to finish this + operation. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_SUCCESS The operation completed successfully. + +**/ +EFI_STATUS +Ip6CreateOpCode ( + IN UINT16 StartLabelNumber, + OUT VOID **StartOpCodeHandle, + OUT EFI_IFR_GUID_LABEL **StartLabel, + OUT VOID **EndOpCodeHandle, + OUT EFI_IFR_GUID_LABEL **EndLabel + ) +{ + EFI_STATUS Status; + EFI_IFR_GUID_LABEL *InternalStartLabel; + EFI_IFR_GUID_LABEL *InternalEndLabel; + + if (StartOpCodeHandle == NULL || StartLabel == NULL || EndOpCodeHandle == NULL || EndLabel == NULL) { + return EFI_INVALID_PARAMETER; + } + + *StartOpCodeHandle = NULL; + *EndOpCodeHandle = NULL; + Status = EFI_OUT_OF_RESOURCES; + + // + // Initialize the container for dynamic opcodes. + // + *StartOpCodeHandle = HiiAllocateOpCodeHandle (); + if (*StartOpCodeHandle == NULL) { + return Status; + } + + *EndOpCodeHandle = HiiAllocateOpCodeHandle (); + if (*EndOpCodeHandle == NULL) { + goto Exit; + } + + // + // Create Hii Extend Label OpCode as the start opcode. + // + InternalStartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + *StartOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + if (InternalStartLabel == NULL) { + goto Exit; + } + + InternalStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + InternalStartLabel->Number = StartLabelNumber; + + // + // Create Hii Extend Label OpCode as the end opcode. + // + InternalEndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + *EndOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + if (InternalEndLabel == NULL) { + goto Exit; + } + + InternalEndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + InternalEndLabel->Number = LABEL_END; + + *StartLabel = InternalStartLabel; + *EndLabel = InternalEndLabel; + + return EFI_SUCCESS; + +Exit: + + if (*StartOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (*StartOpCodeHandle); + } + + if (*EndOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (*EndOpCodeHandle); + } + + return Status; +} + +/** + This function converts the different format of address list to string format and + then generates the corresponding text opcode to illustarate the address info in + IP6 configuration page. Currently, the following formats are supported: + EFI_IP6_ADDRESS_INFO AddressType: Ip6ConfigNvHostAddress; + EFI_IPv6_ADDRESS AddressType: Ip6ConfigNvGatewayAddress and Ip6ConfigNvDnsAddress; + EFI_IP6_ROUTE_TABLE AddressType: Ip6ConfigNvRouteTable. + + @param[in, out] String The pointer to the buffer to store the converted + string. + @param[in] HiiHandle A handle that was previously registered in the + HII Database. + @param[in] AddressType The address type. + @param[in] AddressInfo Pointer to the address list. + @param[in] AddressCount The address count of the address list. + + @retval EFI_SUCCESS The operation finished successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_UNSUPPORTED The AddressType is not supported. + + +**/ +EFI_STATUS +Ip6ConvertAddressListToString ( + IN OUT CHAR16 *String, + IN EFI_HII_HANDLE HiiHandle, + IN IP6_CONFIG_NV_ADDRESS_TYPE AddressType, + IN VOID *AddressInfo, + IN UINTN AddressCount + ) +{ + UINTN Index; + UINTN Number; + CHAR16 *TempStr; + EFI_STATUS Status; + VOID *StartOpCodeHandle; + EFI_IFR_GUID_LABEL *StartLabel; + VOID *EndOpCodeHandle; + EFI_IFR_GUID_LABEL *EndLabel; + UINT16 StartLabelNumber; + EFI_STRING_ID TextTwo; + UINT8 *AddressHead; + UINT8 PrefixLength; + EFI_IPv6_ADDRESS *Address; + + if ((String == NULL) || (HiiHandle == NULL) || (AddressInfo == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (AddressType == Ip6ConfigNvHostAddress) { + StartLabelNumber = HOST_ADDRESS_LABEL; + } else if (AddressType == Ip6ConfigNvGatewayAddress) { + StartLabelNumber = GATEWAY_ADDRESS_LABEL; + } else if (AddressType == Ip6ConfigNvDnsAddress) { + StartLabelNumber = DNS_ADDRESS_LABEL; + } else if (AddressType == Ip6ConfigNvRouteTable) { + StartLabelNumber = ROUTE_TABLE_LABEL; + } else { + ASSERT (FALSE); + return EFI_UNSUPPORTED; + } + + Status = Ip6CreateOpCode ( + StartLabelNumber, + &StartOpCodeHandle, + &StartLabel, + &EndOpCodeHandle, + &EndLabel + ); + if (EFI_ERROR (Status)) { + return Status; + } + + AddressHead = (UINT8 *) AddressInfo; + + for (Index = 0; Index < AddressCount; Index++) { + if (AddressType == Ip6ConfigNvHostAddress) { + AddressInfo = AddressHead + sizeof (EFI_IP6_ADDRESS_INFO) * Index; + Address = &((EFI_IP6_ADDRESS_INFO *) AddressInfo)->Address; + } else if (AddressType == Ip6ConfigNvRouteTable) { + AddressInfo = AddressHead + sizeof (EFI_IP6_ROUTE_TABLE) * Index; + Address = &((EFI_IP6_ROUTE_TABLE *) AddressInfo)->Destination; + } else { + AddressInfo = AddressHead + sizeof (EFI_IPv6_ADDRESS) * Index; + Address = AddressInfo; + } + + // + // Convert the IP address info to string. + // + Ip6ToStr (Address, String); + TempStr = String + StrLen (String); + + if ((AddressType == Ip6ConfigNvHostAddress) || (AddressType == Ip6ConfigNvRouteTable)) { + if (AddressType == Ip6ConfigNvHostAddress) { + PrefixLength = ((EFI_IP6_ADDRESS_INFO *) AddressInfo)->PrefixLength; + } else { + PrefixLength = ((EFI_IP6_ROUTE_TABLE *) AddressInfo)->PrefixLength; + } + + // + // Append the prefix length to the string. + // + *TempStr = L'/'; + TempStr++; + Number = UnicodeSPrint (TempStr, 6, L"%d", PrefixLength); + TempStr = TempStr + Number; + } + + if (AddressType == Ip6ConfigNvRouteTable) { + // + // Append " >> " to the string. + // + Number = UnicodeSPrint (TempStr, 8, L" >> "); + TempStr = TempStr + Number; + + // + // Append the gateway address to the string. + // + Ip6ToStr (&((EFI_IP6_ROUTE_TABLE *) AddressInfo)->Gateway, TempStr); + TempStr = TempStr + StrLen (TempStr); + } + + // + // Generate a text opcode and update the UI. + // + TextTwo = HiiSetString (HiiHandle, 0, String, NULL); + if (TextTwo == 0) { + Status = EFI_INVALID_PARAMETER; + goto Exit; + } + + HiiCreateTextOpCode (StartOpCodeHandle, STR_NULL, STR_NULL, TextTwo); + + String = TempStr; + *String = IP6_ADDRESS_DELIMITER; + String++; + } + + *(String - 1) = '\0'; + + Status = HiiUpdateForm ( + HiiHandle, // HII handle + &gIp6ConfigNvDataGuid, // Formset GUID + FORMID_MAIN_FORM, // Form ID + StartOpCodeHandle, // Label for where to insert opcodes + EndOpCodeHandle // Replace data + ); + +Exit: + HiiFreeOpCodeHandle (StartOpCodeHandle); + HiiFreeOpCodeHandle (EndOpCodeHandle); + + return Status; +} + +/** + Parse address list in string format and convert it to a list array of node in + IP6_ADDRESS_INFO_ENTRY. + + @param[in] String The buffer to string to be parsed. + @param[out] ListHead The list head of array. + @param[out] AddressCount The number of list nodes in the array. + + @retval EFI_SUCCESS The operation finished successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to perform the operation due to lack of resource. + +**/ +EFI_STATUS +Ip6ParseAddressListFromString ( + IN CONST CHAR16 *String, + OUT LIST_ENTRY *ListHead, + OUT UINT32 *AddressCount + ) +{ + EFI_STATUS Status; + CHAR16 *LocalString; + CHAR16 *Temp; + CHAR16 *TempStr; + EFI_IP6_ADDRESS_INFO AddressInfo; + IP6_ADDRESS_INFO_ENTRY *Node; + BOOLEAN Last; + UINT32 Count; + + if ((String == NULL) || (ListHead == NULL) || (AddressCount == NULL)) { + return EFI_INVALID_PARAMETER; + } + + ZeroMem (&AddressInfo, sizeof (EFI_IP6_ADDRESS_INFO)); + LocalString = (CHAR16 *) AllocateCopyPool (StrSize (String), String); + if (LocalString == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Clean the original address list. + // + Ip6FreeAddressInfoList (ListHead); + + Temp = LocalString; + Last = FALSE; + Count = 0; + + while (*LocalString != L'\0') { + TempStr = LocalString; + while ((*LocalString != L'\0') && (*LocalString != IP6_ADDRESS_DELIMITER)) { + LocalString++; + } + + if (*LocalString == L'\0') { + Last = TRUE; + } + + *LocalString = L'\0'; + + Status = NetLibStrToIp6andPrefix (TempStr, &AddressInfo.Address, &AddressInfo.PrefixLength); + if (EFI_ERROR (Status)) { + goto Error; + } + + if (AddressInfo.PrefixLength == 0xFF) { + AddressInfo.PrefixLength = 0; + } + + if (!NetIp6IsValidUnicast (&AddressInfo.Address)) { + Status = EFI_INVALID_PARAMETER; + goto Error; + } + + Node = AllocatePool (sizeof (IP6_ADDRESS_INFO_ENTRY)); + if (Node == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + CopyMem (&Node->AddrInfo, &AddressInfo, sizeof (EFI_IP6_ADDRESS_INFO)); + InsertTailList (ListHead, &Node->Link); + Count++; + + if (Last) { + break; + } + + LocalString++; + } + + FreePool (Temp); + *AddressCount = Count; + return EFI_SUCCESS; + +Error: + Ip6FreeAddressInfoList (ListHead); + FreePool (Temp); + return Status; +} + +/** + This function converts the interface info to string and draws it to the IP6 UI. + The interface information includes interface name, interface type, hardware + address and route table information. + + @param[in] IfInfo The pointer of EFI_IP6_CONFIG_INTERFACE_INFO. + @param[in] HiiHandle The handle that was previously registered in the + HII Database. + @param[in, out] IfrNvData Points to IP6_CONFIG_IFR_NVDATA. + + @retval EFI_SUCCESS The operation finished successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_OUT_OF_RESOURCES The operation failed due to lack of resources. + +**/ +EFI_STATUS +Ip6ConvertInterfaceInfoToString ( + IN EFI_IP6_CONFIG_INTERFACE_INFO *IfInfo, + IN EFI_HII_HANDLE HiiHandle, + IN OUT IP6_CONFIG_IFR_NVDATA *IfrNvData + ) +{ + UINT32 Index; + UINTN Number; + CHAR16 *String; + CHAR16 PortString[ADDRESS_STR_MAX_SIZE]; + CHAR16 FormatString[8]; + EFI_STRING_ID StringId; + + if ((IfInfo == NULL) || (HiiHandle == NULL) || (IfrNvData == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Print the interface name. + // + StringId = HiiSetString ( + HiiHandle, + STRING_TOKEN (STR_IP6_INTERFACE_NAME_CONTENT), + IfInfo->Name, + NULL + ); + if (StringId == 0) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Print the interface type. + // + if (IfInfo->IfType == Ip6InterfaceTypeEthernet) { + CopyMem (PortString, IP6_ETHERNET, sizeof (IP6_ETHERNET)); + } else if (IfInfo->IfType == Ip6InterfaceTypeExperimentalEthernet) { + CopyMem (PortString, IP6_EXPERIMENTAL_ETHERNET, sizeof (IP6_EXPERIMENTAL_ETHERNET)); + } else { + // + // Refer to RFC1700, chapter Number Hardware Type. + // + UnicodeSPrint (PortString, 6, L"%d", IfInfo->IfType); + } + + StringId = HiiSetString ( + HiiHandle, + STRING_TOKEN (STR_IP6_INTERFACE_TYPE_CONTENT), + PortString, + NULL + ); + if (StringId == 0) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Convert the hardware address. + // + String = PortString; + ASSERT (IfInfo->HwAddressSize <= 32); + + for (Index = 0; Index < IfInfo->HwAddressSize; Index++) { + + if (IfInfo->HwAddress.Addr[Index] < 0x10) { + CopyMem (FormatString, L"0%x-", sizeof (L"0%x-")); + } else { + CopyMem (FormatString, L"%x-", sizeof (L"%x-")); + } + + Number = UnicodeSPrint ( + String, + 8, + (CONST CHAR16 *) FormatString, + (UINTN) IfInfo->HwAddress.Addr[Index] + ); + String = String + Number; + } + + if (Index != 0) { + ASSERT (String > PortString); + String--; + *String = '\0'; + } + + // + // Print the hardware address. + // + StringId = HiiSetString ( + HiiHandle, + STRING_TOKEN (STR_IP6_MAC_ADDRESS_CONTENT), + PortString, + NULL + ); + if (StringId == 0) { + return EFI_OUT_OF_RESOURCES; + } + + return EFI_SUCCESS; +} + +/** + Build the address info list from list array of node in IP6_ADDRESS_INFO_ENTRY. + + @param[in] Instance Points to IP6 config instance data. + @param[in] AddressType The address type. + @param[out] AddressInfo The pointer to the buffer to store the address list. + @param[out] AddressSize The address size of the address list. + + @retval EFI_SUCCESS The operation finished successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_UNSUPPORTED The AddressType is not supported. + +**/ +EFI_STATUS +Ip6BuildNvAddressInfo ( + IN IP6_CONFIG_INSTANCE *Instance, + IN IP6_CONFIG_NV_ADDRESS_TYPE AddressType, + OUT VOID **AddressInfo, + OUT UINTN *AddressSize + ) +{ + IP6_CONFIG_NVDATA *Ip6NvData; + LIST_ENTRY *Entry; + LIST_ENTRY *ListHead; + IP6_ADDRESS_INFO_ENTRY *Node; + VOID *AddressList; + VOID *TmpStr; + UINTN DataSize; + EFI_IPv6_ADDRESS *Ip6Address; + EFI_IP6_CONFIG_MANUAL_ADDRESS *ManualAddress; + + if ((Instance == NULL) || (AddressInfo == NULL) || (AddressSize == NULL)) { + return EFI_INVALID_PARAMETER; + } + + NET_CHECK_SIGNATURE (Instance, IP6_CONFIG_INSTANCE_SIGNATURE); + + Ip6NvData = &Instance->Ip6NvData; + + if (AddressType == Ip6ConfigNvHostAddress) { + ListHead = &Ip6NvData->ManualAddress; + DataSize = sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS) * Ip6NvData->ManualAddressCount; + } else if (AddressType == Ip6ConfigNvGatewayAddress) { + ListHead = &Ip6NvData->GatewayAddress; + DataSize = sizeof (EFI_IPv6_ADDRESS) * Ip6NvData->GatewayAddressCount; + } else if (AddressType == Ip6ConfigNvDnsAddress) { + ListHead = &Ip6NvData->DnsAddress; + DataSize = sizeof (EFI_IPv6_ADDRESS) * Ip6NvData->DnsAddressCount; + } else { + return EFI_UNSUPPORTED; + } + + AddressList = AllocateZeroPool (DataSize); + if (AddressList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + TmpStr = AddressList; + + NET_LIST_FOR_EACH (Entry, ListHead) { + Node = NET_LIST_USER_STRUCT (Entry, IP6_ADDRESS_INFO_ENTRY, Link); + if (AddressType == Ip6ConfigNvHostAddress) { + ManualAddress = (EFI_IP6_CONFIG_MANUAL_ADDRESS *) AddressList; + IP6_COPY_ADDRESS (&ManualAddress->Address, &Node->AddrInfo.Address); + ManualAddress->PrefixLength = Node->AddrInfo.PrefixLength; + AddressList = (UINT8 *) AddressList + sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS); + } else { + Ip6Address = (EFI_IPv6_ADDRESS *) AddressList; + IP6_COPY_ADDRESS (Ip6Address, &Node->AddrInfo.Address); + AddressList = (UINT8 *) AddressList + sizeof (EFI_IPv6_ADDRESS); + } + } + + *AddressInfo = TmpStr; + *AddressSize = DataSize; + return EFI_SUCCESS; +} + +/** + Convert the IP6 configuration data into the IFR data. + + @param[in, out] IfrNvData The IFR NV data. + @param[in] Instance The IP6 config instance data. + + @retval EFI_SUCCESS The operation finished successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_UNSUPPORTED The policy is not supported in the current implementation. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +Ip6ConvertConfigNvDataToIfrNvData ( + IN OUT IP6_CONFIG_IFR_NVDATA *IfrNvData, + IN IP6_CONFIG_INSTANCE *Instance + ) +{ + IP6_CONFIG_NVDATA *Ip6NvData; + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + UINTN DataSize; + VOID *Data; + EFI_STATUS Status; + EFI_IP6_CONFIG_POLICY Policy; + EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS DadXmits; + EFI_HII_HANDLE HiiHandle; + + if ((IfrNvData == NULL) || (Instance == NULL)) { + return EFI_INVALID_PARAMETER; + } + + NET_CHECK_SIGNATURE (Instance, IP6_CONFIG_INSTANCE_SIGNATURE); + + Ip6Config = &Instance->Ip6Config; + Ip6NvData = &Instance->Ip6NvData; + Data = NULL; + DataSize = 0; + HiiHandle = Instance->CallbackInfo.RegisteredHandle; + + // + // Get the current interface info. + // + Status = Ip6ConfigNvGetData ( + Ip6Config, + Ip6ConfigDataTypeInterfaceInfo, + &DataSize, + (VOID **) &Data + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Convert the interface info to string and print. + // + Status = Ip6ConvertInterfaceInfoToString ( + (EFI_IP6_CONFIG_INTERFACE_INFO *) Data, + HiiHandle, + IfrNvData + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Get the interface id. + // + DataSize = sizeof (EFI_IP6_CONFIG_INTERFACE_ID); + ZeroMem (&Ip6NvData->InterfaceId, DataSize); + Status = Ip6Config->GetData ( + Ip6Config, + Ip6ConfigDataTypeAltInterfaceId, + &DataSize, + &Ip6NvData->InterfaceId + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Ip6ConvertInterfaceIdToString (IfrNvData->InterfaceId, &Ip6NvData->InterfaceId); + + // + // Get current policy. + // + DataSize = sizeof (EFI_IP6_CONFIG_POLICY); + Status = Ip6Config->GetData ( + Ip6Config, + Ip6ConfigDataTypePolicy, + &DataSize, + &Policy + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + + if (Policy == Ip6ConfigPolicyManual) { + IfrNvData->Policy = IP6_POLICY_MANUAL; + } else if (Policy == Ip6ConfigPolicyAutomatic) { + IfrNvData->Policy = IP6_POLICY_AUTO; + } else { + ASSERT (FALSE); + Status = EFI_UNSUPPORTED; + goto Exit; + } + + // + // Get Duplicate Address Detection Transmits count. + // + DataSize = sizeof (EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS); + Status = Ip6Config->GetData ( + Ip6Config, + Ip6ConfigDataTypeDupAddrDetectTransmits, + &DataSize, + &DadXmits + ); + + if (EFI_ERROR (Status)) { + goto Exit; + } + + IfrNvData->DadTransmitCount = DadXmits.DupAddrDetectTransmits; + +Exit: + if (Data != NULL) { + FreePool (Data); + } + + return Status; +} + +/** + Convert IFR data into IP6 configuration data. The policy, alternative interface + ID, and DAD transmit counts, and will be saved. + + @param[in] IfrNvData The IFR NV data. + @param[in, out] Instance The IP6 config instance data. + + @retval EFI_SUCCESS The operation finished successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +Ip6ConvertIfrNvDataToConfigNvDataGeneral ( + IN IP6_CONFIG_IFR_NVDATA *IfrNvData, + IN OUT IP6_CONFIG_INSTANCE *Instance + ) +{ + IP6_CONFIG_NVDATA *Ip6NvData; + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + EFI_STATUS Status; + + if ((IfrNvData == NULL) || (Instance == NULL)) { + return EFI_INVALID_PARAMETER; + } + + NET_CHECK_SIGNATURE (Instance, IP6_CONFIG_INSTANCE_SIGNATURE); + Ip6NvData = &Instance->Ip6NvData; + Ip6Config = &Instance->Ip6Config; + + // + // Update those fields which don't have INTERACTIVE attribute. + // + if (IfrNvData->Policy == IP6_POLICY_AUTO) { + Ip6NvData->Policy = Ip6ConfigPolicyAutomatic; + } else if (IfrNvData->Policy == IP6_POLICY_MANUAL) { + Ip6NvData->Policy = Ip6ConfigPolicyManual; + } + + Ip6NvData->DadTransmitCount.DupAddrDetectTransmits = IfrNvData->DadTransmitCount; + + // + // Set the configured policy. + // + Status = Ip6Config->SetData ( + Ip6Config, + Ip6ConfigDataTypePolicy, + sizeof (EFI_IP6_CONFIG_POLICY), + &Ip6NvData->Policy + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Set the duplicate address detection transmits count. + // + Status = Ip6Config->SetData ( + Ip6Config, + Ip6ConfigDataTypeDupAddrDetectTransmits, + sizeof (EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS), + &Ip6NvData->DadTransmitCount + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Set the alternative interface ID + // + Status = Ip6Config->SetData ( + Ip6Config, + Ip6ConfigDataTypeAltInterfaceId, + sizeof (EFI_IP6_CONFIG_INTERFACE_ID), + &Ip6NvData->InterfaceId + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return EFI_SUCCESS; +} + +/** + Convert IFR data into IP6 configuration data. The policy, configured + manual address, gateway address, and DNS server address will be saved. + + @param[in] IfrNvData The IFR NV data. + @param[in, out] Instance The IP6 config instance data. + + @retval EFI_SUCCESS The operation finished successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +Ip6ConvertIfrNvDataToConfigNvDataAdvanced ( + IN IP6_CONFIG_IFR_NVDATA *IfrNvData, + IN OUT IP6_CONFIG_INSTANCE *Instance + ) +{ + IP6_CONFIG_NVDATA *Ip6NvData; + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + EFI_STATUS Status; + EFI_IP6_CONFIG_MANUAL_ADDRESS *ManualAddress; + EFI_IPv6_ADDRESS *Address; + BOOLEAN IsAddressOk; + EFI_EVENT SetAddressEvent; + EFI_EVENT TimeoutEvent; + UINTN DataSize; + + if ((IfrNvData == NULL) || (Instance == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (IfrNvData->Policy == IP6_POLICY_AUTO) { + return EFI_SUCCESS; + } + + NET_CHECK_SIGNATURE (Instance, IP6_CONFIG_INSTANCE_SIGNATURE); + Ip6NvData = &Instance->Ip6NvData; + Ip6Config = &Instance->Ip6Config; + + // + // Update those fields which don't have INTERACTIVE attribute. + // + Ip6NvData->Policy = Ip6ConfigPolicyManual; + + // + // Set the configured policy. + // + Status = Ip6Config->SetData ( + Ip6Config, + Ip6ConfigDataTypePolicy, + sizeof (EFI_IP6_CONFIG_POLICY), + &Ip6NvData->Policy + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Create events & timers for asynchronous settings. + // + SetAddressEvent = NULL; + TimeoutEvent = NULL; + ManualAddress = NULL; + Address = NULL; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Ip6ConfigManualAddressNotify, + &IsAddressOk, + &SetAddressEvent + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &TimeoutEvent + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Set the manual address list. This is an asynchronous process. + // + if (!IsListEmpty (&Ip6NvData->ManualAddress) && (Ip6NvData->ManualAddressCount != 0)) { + Status = Ip6BuildNvAddressInfo ( + Instance, + Ip6ConfigNvHostAddress, + (VOID **) &ManualAddress, + &DataSize + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + IsAddressOk = FALSE; + + Status = Ip6Config->RegisterDataNotify ( + Ip6Config, + Ip6ConfigDataTypeManualAddress, + SetAddressEvent + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status = Ip6Config->SetData ( + Ip6Config, + Ip6ConfigDataTypeManualAddress, + DataSize, + (VOID *) ManualAddress + ); + if (Status == EFI_NOT_READY) { + gBS->SetTimer (TimeoutEvent, TimerRelative, 50000000); + while (EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) { + if (IsAddressOk) { + Status = EFI_SUCCESS; + } + break; + } + } + + Status = Ip6Config->UnregisterDataNotify ( + Ip6Config, + Ip6ConfigDataTypeManualAddress, + SetAddressEvent + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + + // + // Set gateway address list. + // + if (!IsListEmpty (&Ip6NvData->GatewayAddress) && (Ip6NvData->GatewayAddressCount != 0)) { + Status = Ip6BuildNvAddressInfo ( + Instance, + Ip6ConfigNvGatewayAddress, + (VOID **) &Address, + &DataSize + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status = Ip6Config->SetData ( + Ip6Config, + Ip6ConfigDataTypeGateway, + DataSize, + (VOID *) Address + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + FreePool (Address); + Address = NULL; + } + + // + // Set DNS server address list. + // + if (!IsListEmpty (&Ip6NvData->DnsAddress) && (Ip6NvData->DnsAddressCount != 0)) { + Status = Ip6BuildNvAddressInfo ( + Instance, + Ip6ConfigNvDnsAddress, + (VOID **) &Address, + &DataSize + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status = Ip6Config->SetData ( + Ip6Config, + Ip6ConfigDataTypeDnsServer, + DataSize, + (VOID *) Address + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + + Status = EFI_SUCCESS; + +Exit: + if (SetAddressEvent != NULL) { + gBS->CloseEvent (SetAddressEvent); + } + + if (TimeoutEvent != NULL) { + gBS->CloseEvent (TimeoutEvent); + } + + if (ManualAddress != NULL) { + FreePool (ManualAddress); + } + + if (Address != NULL) { + FreePool (Address); + } + + return Status; +} + + +/** + This function allows the caller to request the current + configuration for one or more named elements. The resulting + string is in format. Any and all alternative + configuration strings shall also be appended to the end of the + current configuration string. If they are, they must appear + after the current configuration. They must contain the same + routing (GUID, NAME, PATH) as the current configuration string. + They must have an additional description indicating the type of + alternative configuration the string represents, + "ALTCFG=". That (when + converted from Hex UNICODE to binary) is a reference to a + string in the associated string pack. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Request A null-terminated Unicode string in + format. Note that this + includes the routing information as well as + the configurable name / value pairs. It is + invalid for this string to be in + format. + @param[out] Progress On return, points to a character in the + Request string. Points to the string's null + terminator if request was successful. Points + to the most recent "&" before the first + failing name / value pair (or the beginning + of the string if the failure is in the first + name / value pair) if the request was not + successful. + @param[out] Results A null-terminated Unicode string in + format which has all values + filled in for the names in the Request string. + String to be allocated by the called function. + + @retval EFI_SUCCESS The Results string is filled with the + values corresponding to all requested + names. + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + @retval EFI_INVALID_PARAMETER For example, passing in a NULL + for the Request parameter + would result in this type of + error. In this case, the + Progress parameter would be + set to NULL. + @retval EFI_NOT_FOUND Routing data doesn't match any + known driver. Progress set to the + first character in the routing header. + Note: There is no requirement that the + driver validate the routing data. It + must skip the in order to + process the names. + @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set + to most recent & before the + error or the beginning of the + string. + @retval EFI_INVALID_PARAMETER Unknown name. Progress points + to the & before the name in + question. Currently not implemented. +**/ +EFI_STATUS +EFIAPI +Ip6FormExtractConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Request, + OUT EFI_STRING *Progress, + OUT EFI_STRING *Results + ) +{ + + EFI_STATUS Status; + IP6_FORM_CALLBACK_INFO *Private; + IP6_CONFIG_INSTANCE *Ip6ConfigInstance; + IP6_CONFIG_IFR_NVDATA *IfrNvData; + EFI_STRING ConfigRequestHdr; + EFI_STRING ConfigRequest; + BOOLEAN AllocatedRequest; + UINTN Size; + UINTN BufferSize; + + if (This == NULL || Progress == NULL || Results == NULL) { + return EFI_INVALID_PARAMETER; + } + + *Progress = Request; + if ((Request != NULL) && + !HiiIsConfigHdrMatch (Request, &gIp6ConfigNvDataGuid, mIp6ConfigStorageName)) { + return EFI_NOT_FOUND; + } + + ConfigRequestHdr = NULL; + ConfigRequest = NULL; + AllocatedRequest = FALSE; + Size = 0; + + Private = IP6_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This); + Ip6ConfigInstance = IP6_CONFIG_INSTANCE_FROM_FORM_CALLBACK (Private); + BufferSize = sizeof (IP6_CONFIG_IFR_NVDATA); + + IfrNvData = (IP6_CONFIG_IFR_NVDATA *) AllocateZeroPool (BufferSize); + if (IfrNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = Ip6ConvertConfigNvDataToIfrNvData (IfrNvData, Ip6ConfigInstance); + if (EFI_ERROR (Status)) { + goto Exit; + } + + ConfigRequest = Request; + if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) { + // + // Request has no request element, construct full request string. + // Allocate and fill a buffer large enough to hold the template + // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator. + // + ConfigRequestHdr = HiiConstructConfigHdr ( + &gIp6ConfigNvDataGuid, + mIp6ConfigStorageName, + Private->ChildHandle + ); + Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16); + ConfigRequest = AllocateZeroPool (Size); + ASSERT (ConfigRequest != NULL); + AllocatedRequest = TRUE; + UnicodeSPrint ( + ConfigRequest, + Size, + L"%s&OFFSET=0&WIDTH=%016LX", + ConfigRequestHdr, + (UINT64) BufferSize + ); + FreePool (ConfigRequestHdr); + } + + // + // Convert buffer data to by helper function BlockToConfig() + // + Status = gHiiConfigRouting->BlockToConfig ( + gHiiConfigRouting, + ConfigRequest, + (UINT8 *) IfrNvData, + BufferSize, + Results, + Progress + ); + +Exit: + FreePool (IfrNvData); + // + // Free the allocated config request string. + // + if (AllocatedRequest) { + FreePool (ConfigRequest); + ConfigRequest = NULL; + } + // + // Set Progress string to the original request string. + // + if (Request == NULL) { + *Progress = NULL; + } else if (StrStr (Request, L"OFFSET") == NULL) { + *Progress = Request + StrLen (Request); + } + + return Status; +} + +/** + This function applies changes in a driver's configuration. + Input is a Configuration, which has the routing data for this + driver followed by name / value configuration pairs. The driver + must apply those pairs to its configurable storage. If the + driver's configuration is stored in a linear block of data + and the driver's name / value pairs are in + format, it may use the ConfigToBlock helper function (above) to + simplify the job. Currently not implemented. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Configuration A null-terminated Unicode string in + format. + @param[out] Progress A pointer to a string filled in with the + offset of the most recent '&' before the + first failing name / value pair (or the + beginn ing of the string if the failure + is in the first name / value pair) or + the terminating NULL if all was + successful. + + @retval EFI_SUCCESS The results have been distributed or are + awaiting distribution. + @retval EFI_OUT_OF_MEMORY Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + @retval EFI_INVALID_PARAMETERS Passing in a NULL for the + Results parameter would result + in this type of error. + @retval EFI_NOT_FOUND Target for the specified routing data + was not found. +**/ +EFI_STATUS +EFIAPI +Ip6FormRouteConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Configuration, + OUT EFI_STRING *Progress + ) +{ + if (This == NULL || Configuration == NULL || Progress == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Check routing data in . + // Note: if only one Storage is used, then this checking could be skipped. + // + if (!HiiIsConfigHdrMatch (Configuration, &gIp6ConfigNvDataGuid, mIp6ConfigStorageName)) { + *Progress = Configuration; + return EFI_NOT_FOUND; + } + + *Progress = Configuration + StrLen (Configuration); + + return EFI_SUCCESS; +} + +/** + Display host addresses, route table, DNS addresses and gateway addresses in + "IPv6 Current Setting" page. + + @param[in] Instance The IP6 config instance data. + + @retval EFI_SUCCESS The operation finished successfully. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +Ip6GetCurrentSetting ( + IN IP6_CONFIG_INSTANCE *Instance + ) +{ + EFI_IP6_CONFIG_PROTOCOL *Ip6Config; + EFI_HII_HANDLE HiiHandle; + EFI_IP6_CONFIG_INTERFACE_INFO *Data; + UINTN DataSize; + EFI_STATUS Status; + CHAR16 PortString[ADDRESS_STR_MAX_SIZE]; + EFI_IP6_CONFIG_INTERFACE_INFO *IfInfo; + + + Ip6Config = &Instance->Ip6Config; + HiiHandle = Instance->CallbackInfo.RegisteredHandle; + Data = NULL; + + // + // Get current interface info. + // + Status = Ip6ConfigNvGetData ( + Ip6Config, + Ip6ConfigDataTypeInterfaceInfo, + &DataSize, + (VOID **) &Data + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Generate dynamic text opcode for host address and draw it. + // + IfInfo = (EFI_IP6_CONFIG_INTERFACE_INFO *) Data; + Status = Ip6ConvertAddressListToString ( + PortString, + HiiHandle, + Ip6ConfigNvHostAddress, + IfInfo->AddressInfo, + IfInfo->AddressInfoCount + ); + if (EFI_ERROR (Status)) { + FreePool (Data); + return Status; + } + + // + // Generate the dynamic text opcode for route table and draw it. + // + Status = Ip6ConvertAddressListToString ( + PortString, + HiiHandle, + Ip6ConfigNvRouteTable, + IfInfo->RouteTable, + IfInfo->RouteCount + ); + if (EFI_ERROR (Status)) { + FreePool (Data); + return Status; + } + + // + // Get DNS server list. + // + FreePool (Data); + DataSize = 0; + Data = NULL; + Status = Ip6ConfigNvGetData ( + Ip6Config, + Ip6ConfigDataTypeDnsServer, + &DataSize, + (VOID **) &Data + ); + if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) { + if (Data != NULL) { + FreePool (Data); + } + return Status; + } + + if (DataSize > 0) { + // + // Generate the dynamic text opcode for DNS server and draw it. + // + Status = Ip6ConvertAddressListToString ( + PortString, + HiiHandle, + Ip6ConfigNvDnsAddress, + Data, + DataSize / sizeof (EFI_IPv6_ADDRESS) + ); + if (EFI_ERROR (Status)) { + FreePool (Data); + return Status; + } + } + + // + // Get gateway adderss list. + // + if (Data != NULL) { + FreePool (Data); + } + + DataSize = 0; + Data = NULL; + Status = Ip6ConfigNvGetData ( + Ip6Config, + Ip6ConfigDataTypeGateway, + &DataSize, + (VOID **) &Data + ); + if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) { + if (Data != NULL) { + FreePool (Data); + } + return Status; + } + + if (DataSize > 0) { + // + // Generate the dynamic text opcode for gateway and draw it. + // + Status = Ip6ConvertAddressListToString ( + PortString, + HiiHandle, + Ip6ConfigNvGatewayAddress, + Data, + DataSize / sizeof (EFI_IPv6_ADDRESS) + ); + if (EFI_ERROR (Status)) { + FreePool (Data); + return Status; + } + } + + if (Data != NULL) { + FreePool (Data); + } + + return EFI_SUCCESS; +} + +/** + This function is called to provide results data to the driver. + This data consists of a unique key that is used to identify + which data is either being passed back or being asked for. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Action Specifies the type of action taken by the browser. + @param[in] QuestionId A unique value which is sent to the original + exporting driver so that it can identify the type + of data to expect. The format of the data tends to + vary based on the opcode that generated the callback. + @param[in] Type The type of value for the question. + @param[in] Value A pointer to the data being sent to the original + exporting driver. + @param[out] ActionRequest On return, points to the action requested by the + callback function. + + @retval EFI_SUCCESS The callback successfully handled the action. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the + variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be saved. + @retval EFI_UNSUPPORTED The specified Action is not supported by the + callback. Currently not implemented. + @retval EFI_INVALID_PARAMETER Passed in the wrong parameter. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +Ip6FormCallback ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN EFI_BROWSER_ACTION Action, + IN EFI_QUESTION_ID QuestionId, + IN UINT8 Type, + IN EFI_IFR_TYPE_VALUE *Value, + OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest + ) +{ + IP6_FORM_CALLBACK_INFO *Private; + UINTN BufferSize; + IP6_CONFIG_IFR_NVDATA *IfrNvData; + EFI_STATUS Status; + EFI_INPUT_KEY Key; + IP6_CONFIG_INSTANCE *Instance; + IP6_CONFIG_NVDATA *Ip6NvData; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private = IP6_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This); + Instance = IP6_CONFIG_INSTANCE_FROM_FORM_CALLBACK (Private); + Ip6NvData = &Instance->Ip6NvData; + + if ((Action == EFI_BROWSER_ACTION_FORM_OPEN) || (Action == EFI_BROWSER_ACTION_FORM_CLOSE)){ + return EFI_SUCCESS; + } + + if (Action != EFI_BROWSER_ACTION_CHANGING && Action != EFI_BROWSER_ACTION_CHANGED) { + return EFI_UNSUPPORTED; + } + + if ((Value == NULL) || (ActionRequest == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Retrieve uncommitted data from Browser + // + + BufferSize = sizeof (IP6_CONFIG_IFR_NVDATA); + IfrNvData = AllocateZeroPool (BufferSize); + if (IfrNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = EFI_SUCCESS; + + HiiGetBrowserData (NULL, NULL, BufferSize, (UINT8 *) IfrNvData); + + if (Action == EFI_BROWSER_ACTION_CHANGING) { + switch (QuestionId) { + case KEY_GET_CURRENT_SETTING: + Status = Ip6GetCurrentSetting (Instance); + break; + + default: + break; + } + } else if (Action == EFI_BROWSER_ACTION_CHANGED) { + switch (QuestionId) { + case KEY_SAVE_CONFIG_CHANGES: + Status = Ip6ConvertIfrNvDataToConfigNvDataAdvanced (IfrNvData, Instance); + if (EFI_ERROR (Status)) { + break; + } + + Status = Ip6GetCurrentSetting (Instance); + + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT; + break; + + case KEY_IGNORE_CONFIG_CHANGES: + Ip6FreeAddressInfoList (&Ip6NvData->ManualAddress); + Ip6FreeAddressInfoList (&Ip6NvData->GatewayAddress); + Ip6FreeAddressInfoList (&Ip6NvData->DnsAddress); + + Ip6NvData->ManualAddressCount = 0; + Ip6NvData->GatewayAddressCount = 0; + Ip6NvData->DnsAddressCount = 0; + + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT; + break; + + case KEY_SAVE_CHANGES: + Status = Ip6ConvertIfrNvDataToConfigNvDataGeneral (IfrNvData, Instance); + if (EFI_ERROR (Status)) { + break; + } + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT; + break; + + case KEY_INTERFACE_ID: + Status = Ip6ParseInterfaceIdFromString (IfrNvData->InterfaceId, &Ip6NvData->InterfaceId); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Interface ID!", + NULL + ); + } + + break; + + case KEY_MANUAL_ADDRESS: + Status = Ip6ParseAddressListFromString ( + IfrNvData->ManualAddress, + &Ip6NvData->ManualAddress, + &Ip6NvData->ManualAddressCount + ); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Host Addresses!", + NULL + ); + } + + break; + + case KEY_GATEWAY_ADDRESS: + Status = Ip6ParseAddressListFromString ( + IfrNvData->GatewayAddress, + &Ip6NvData->GatewayAddress, + &Ip6NvData->GatewayAddressCount + ); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid Gateway Addresses!", + NULL + ); + } + + break; + + case KEY_DNS_ADDRESS: + Status = Ip6ParseAddressListFromString ( + IfrNvData->DnsAddress, + &Ip6NvData->DnsAddress, + &Ip6NvData->DnsAddressCount + ); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"Invalid DNS Addresses!", + NULL + ); + } + + break; + + default: + break; + } + } + + if (!EFI_ERROR (Status)) { + // + // Pass changed uncommitted data back to Form Browser. + // + BufferSize = sizeof (IP6_CONFIG_IFR_NVDATA); + HiiSetBrowserData (NULL, NULL, BufferSize, (UINT8 *) IfrNvData, NULL); + } + + FreePool (IfrNvData); + return Status; +} + +/** + Install HII Config Access protocol for network device and allocate resources. + + @param[in, out] Instance The IP6_CONFIG_INSTANCE to create a form. + + @retval EFI_SUCCESS The HII Config Access protocol is installed. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +Ip6ConfigFormInit ( + IN OUT IP6_CONFIG_INSTANCE *Instance + ) +{ + EFI_STATUS Status; + IP6_SERVICE *IpSb; + IP6_FORM_CALLBACK_INFO *CallbackInfo; + EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess; + VENDOR_DEVICE_PATH VendorDeviceNode; + EFI_SERVICE_BINDING_PROTOCOL *MnpSb; + CHAR16 *MacString; + CHAR16 MenuString[128]; + CHAR16 PortString[128]; + CHAR16 *OldMenuString; + EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; + + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + ASSERT (IpSb != NULL); + + Status = gBS->HandleProtocol ( + IpSb->Controller, + &gEfiDevicePathProtocolGuid, + (VOID **) &ParentDevicePath + ); + if (EFI_ERROR (Status)) { + return Status; + } + + CallbackInfo = &Instance->CallbackInfo; + CallbackInfo->Signature = IP6_FORM_CALLBACK_INFO_SIGNATURE; + + // + // Construct device path node for EFI HII Config Access protocol, + // which consists of controller physical device path and one hardware + // vendor guid node. + // + ZeroMem (&VendorDeviceNode, sizeof (VENDOR_DEVICE_PATH)); + VendorDeviceNode.Header.Type = HARDWARE_DEVICE_PATH; + VendorDeviceNode.Header.SubType = HW_VENDOR_DP; + + CopyGuid (&VendorDeviceNode.Guid, &gEfiCallerIdGuid); + + SetDevicePathNodeLength (&VendorDeviceNode.Header, sizeof (VENDOR_DEVICE_PATH)); + CallbackInfo->HiiVendorDevicePath = AppendDevicePathNode ( + ParentDevicePath, + (EFI_DEVICE_PATH_PROTOCOL *) &VendorDeviceNode + ); + if (CallbackInfo->HiiVendorDevicePath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + ConfigAccess = &CallbackInfo->HiiConfigAccess; + ConfigAccess->ExtractConfig = Ip6FormExtractConfig; + ConfigAccess->RouteConfig = Ip6FormRouteConfig; + ConfigAccess->Callback = Ip6FormCallback; + + // + // Install Device Path Protocol and Config Access protocol on new handle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &CallbackInfo->ChildHandle, + &gEfiDevicePathProtocolGuid, + CallbackInfo->HiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + ConfigAccess, + NULL + ); + if (!EFI_ERROR (Status)) { + // + // Open the Parent Handle for the child + // + Status = gBS->OpenProtocol ( + IpSb->Controller, + &gEfiManagedNetworkServiceBindingProtocolGuid, + (VOID **) &MnpSb, + IpSb->Image, + CallbackInfo->ChildHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + } + + if (EFI_ERROR (Status)) { + goto Error; + } + + // + // Publish our HII data + // + CallbackInfo->RegisteredHandle = HiiAddPackages ( + &gIp6ConfigNvDataGuid, + CallbackInfo->ChildHandle, + Ip6DxeStrings, + Ip6ConfigBin, + NULL + ); + if (CallbackInfo->RegisteredHandle == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + // + // Append MAC string in the menu help string and tile help string + // + Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &MacString); + if (!EFI_ERROR (Status)) { + OldMenuString = HiiGetString ( + CallbackInfo->RegisteredHandle, + STRING_TOKEN (STR_IP6_CONFIG_FORM_HELP), + NULL) + ; + UnicodeSPrint (MenuString, 128, L"%s (MAC:%s)", OldMenuString, MacString); + HiiSetString ( + CallbackInfo->RegisteredHandle, + STRING_TOKEN (STR_IP6_CONFIG_FORM_HELP), + MenuString, + NULL + ); + UnicodeSPrint (PortString, 128, L"MAC:%s", MacString); + HiiSetString ( + CallbackInfo->RegisteredHandle, + STRING_TOKEN (STR_IP6_DEVICE_FORM_HELP), + PortString, + NULL + ); + + FreePool (MacString); + FreePool (OldMenuString); + + InitializeListHead (&Instance->Ip6NvData.ManualAddress); + InitializeListHead (&Instance->Ip6NvData.GatewayAddress); + InitializeListHead (&Instance->Ip6NvData.DnsAddress); + + return EFI_SUCCESS; + } + +Error: + Ip6ConfigFormUnload (Instance); + return Status; +} + +/** + Uninstall the HII Config Access protocol for network devices and free up the resources. + + @param[in, out] Instance The IP6_CONFIG_INSTANCE to unload a form. + +**/ +VOID +Ip6ConfigFormUnload ( + IN OUT IP6_CONFIG_INSTANCE *Instance + ) +{ + IP6_SERVICE *IpSb; + IP6_FORM_CALLBACK_INFO *CallbackInfo; + IP6_CONFIG_NVDATA *Ip6NvData; + + IpSb = IP6_SERVICE_FROM_IP6_CONFIG_INSTANCE (Instance); + ASSERT (IpSb != NULL); + + CallbackInfo = &Instance->CallbackInfo; + + if (CallbackInfo->ChildHandle != NULL) { + + // + // Close the child handle + // + gBS->CloseProtocol ( + IpSb->Controller, + &gEfiManagedNetworkServiceBindingProtocolGuid, + IpSb->Image, + CallbackInfo->ChildHandle + ); + // + // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL + // + gBS->UninstallMultipleProtocolInterfaces ( + CallbackInfo->ChildHandle, + &gEfiDevicePathProtocolGuid, + CallbackInfo->HiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &CallbackInfo->HiiConfigAccess, + NULL + ); + } + + if (CallbackInfo->HiiVendorDevicePath != NULL) { + FreePool (CallbackInfo->HiiVendorDevicePath); + } + + if (CallbackInfo->RegisteredHandle != NULL) { + // + // Remove HII package list + // + HiiRemovePackages (CallbackInfo->RegisteredHandle); + } + + Ip6NvData = &Instance->Ip6NvData; + + Ip6FreeAddressInfoList (&Ip6NvData->ManualAddress); + Ip6FreeAddressInfoList (&Ip6NvData->GatewayAddress); + Ip6FreeAddressInfoList (&Ip6NvData->DnsAddress); + + Ip6NvData->ManualAddressCount = 0; + Ip6NvData->GatewayAddressCount = 0; + Ip6NvData->DnsAddressCount = 0; +} diff --git a/NetworkPkg/Ip6Dxe/Ip6ConfigNv.h b/NetworkPkg/Ip6Dxe/Ip6ConfigNv.h new file mode 100644 index 000000000..d4840270c --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6ConfigNv.h @@ -0,0 +1,62 @@ +/** @file + The header file of Ip6ConfigNv.c. + + Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _IP6_CONFIGNV_H_ +#define _IP6_CONFIGNV_H_ + +#include "Ip6NvData.h" +#include "Ip6ConfigImpl.h" + +extern UINT8 Ip6ConfigBin[]; +extern UINT8 Ip6DxeStrings[]; + +#define IP6_ETHERNET L"Ethernet" +#define IP6_EXPERIMENTAL_ETHERNET L"Experimental Ethernet" +#define IP6_ADDRESS_DELIMITER L' ' +#define IP6_LINK_LOCAL_PREFIX L"FE80::" + +typedef enum { + Ip6InterfaceTypeEthernet = 1, + Ip6InterfaceTypeExperimentalEthernet +} IP6_INTERFACE_TYPE; + +typedef enum { + Ip6ConfigNvHostAddress, + Ip6ConfigNvGatewayAddress, + Ip6ConfigNvDnsAddress, + Ip6ConfigNvRouteTable +} IP6_CONFIG_NV_ADDRESS_TYPE; + +/** + Install HII Config Access protocol for network device and allocate resources. + + @param[in, out] Instance The IP6_CONFIG_INSTANCE to create a form. + + @retval EFI_SUCCESS The HII Config Access protocol is installed. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +Ip6ConfigFormInit ( + IN OUT IP6_CONFIG_INSTANCE *Instance + ); + +/** + Uninstall HII Config Access protocol for network device and free resource. + + @param[in, out] Instance The IP6_CONFIG_INSTANCE to unload a form. + +**/ +VOID +Ip6ConfigFormUnload ( + IN OUT IP6_CONFIG_INSTANCE *Instance + ); + +#endif diff --git a/NetworkPkg/Ip6Dxe/Ip6Driver.c b/NetworkPkg/Ip6Dxe/Ip6Driver.c new file mode 100644 index 000000000..7dc9e45af --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Driver.c @@ -0,0 +1,1054 @@ +/** @file + The driver binding and service binding protocol for IP6 driver. + + Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2015 Hewlett-Packard Development Company, L.P.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +EFI_DRIVER_BINDING_PROTOCOL gIp6DriverBinding = { + Ip6DriverBindingSupported, + Ip6DriverBindingStart, + Ip6DriverBindingStop, + 0xa, + NULL, + NULL +}; + +BOOLEAN mIpSec2Installed = FALSE; + +/** + Callback function for IpSec2 Protocol install. + + @param[in] Event Event whose notification function is being invoked + @param[in] Context Pointer to the notification function's context + +**/ +VOID +EFIAPI +IpSec2InstalledCallback ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + // + // Test if protocol was even found. + // Notification function will be called at least once. + // + Status = gBS->LocateProtocol (&gEfiIpSec2ProtocolGuid, NULL, (VOID **)&mIpSec); + if (Status == EFI_SUCCESS && mIpSec != NULL) { + // + // Close the event so it does not get called again. + // + gBS->CloseEvent (Event); + + mIpSec2Installed = TRUE; + } +} + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + The entry point for IP6 driver which installs the driver + binding and component name protocol on its image. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +Ip6DriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + VOID *Registration; + + EfiCreateProtocolNotifyEvent ( + &gEfiIpSec2ProtocolGuid, + TPL_CALLBACK, + IpSec2InstalledCallback, + NULL, + &Registration + ); + + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gIp6DriverBinding, + ImageHandle, + &gIp6ComponentName, + &gIp6ComponentName2 + ); +} + +/** + Test to see if this driver supports ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Ip6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + // + // Test for the MNP service binding Protocol + // + return gBS->OpenProtocol ( + ControllerHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); +} + +/** + Clean up an IP6 service binding instance. It releases all + the resource allocated by the instance. The instance may be + partly initialized, or partly destroyed. If a resource is + destroyed, it is marked as that in case the destroy failed and + being called again later. + + @param[in] IpSb The IP6 service binding instance to clean up. + + @retval EFI_SUCCESS The resource used by the instance are cleaned up. + @retval Others Failed to clean up some of the resources. + +**/ +EFI_STATUS +Ip6CleanService ( + IN IP6_SERVICE *IpSb + ) +{ + EFI_STATUS Status; + EFI_IPv6_ADDRESS AllNodes; + IP6_NEIGHBOR_ENTRY *NeighborCache; + + IpSb->State = IP6_SERVICE_DESTROY; + + if (IpSb->Timer != NULL) { + gBS->SetTimer (IpSb->Timer, TimerCancel, 0); + gBS->CloseEvent (IpSb->Timer); + + IpSb->Timer = NULL; + } + + if (IpSb->FasterTimer != NULL) { + gBS->SetTimer (IpSb->FasterTimer, TimerCancel, 0); + gBS->CloseEvent (IpSb->FasterTimer); + + IpSb->FasterTimer = NULL; + } + + Ip6ConfigCleanInstance (&IpSb->Ip6ConfigInstance); + + if (!IpSb->LinkLocalDadFail) { + // + // Leave link-scope all-nodes multicast address (FF02::1) + // + Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &AllNodes); + + Status = Ip6LeaveGroup (IpSb, &AllNodes); + if (EFI_ERROR (Status)) { + return Status; + } + } + + if (IpSb->DefaultInterface != NULL) { + Ip6CleanInterface (IpSb->DefaultInterface, NULL); + IpSb->DefaultInterface = NULL; + } + + Ip6CleanDefaultRouterList (IpSb); + + Ip6CleanPrefixListTable (IpSb, &IpSb->OnlinkPrefix); + Ip6CleanPrefixListTable (IpSb, &IpSb->AutonomousPrefix); + + if (IpSb->RouteTable != NULL) { + Ip6CleanRouteTable (IpSb->RouteTable); + IpSb->RouteTable = NULL; + } + + if (IpSb->InterfaceId != NULL) { + FreePool (IpSb->InterfaceId); + } + + IpSb->InterfaceId = NULL; + + Ip6CleanAssembleTable (&IpSb->Assemble); + + if (IpSb->MnpChildHandle != NULL) { + if (IpSb->Mnp != NULL) { + IpSb->Mnp->Cancel (IpSb->Mnp, NULL); + IpSb->Mnp->Configure (IpSb->Mnp, NULL); + gBS->CloseProtocol ( + IpSb->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + IpSb->Image, + IpSb->Controller + ); + + IpSb->Mnp = NULL; + } + + NetLibDestroyServiceChild ( + IpSb->Controller, + IpSb->Image, + &gEfiManagedNetworkServiceBindingProtocolGuid, + IpSb->MnpChildHandle + ); + + IpSb->MnpChildHandle = NULL; + } + + if (IpSb->RecvRequest.MnpToken.Event != NULL) { + gBS->CloseEvent (IpSb->RecvRequest.MnpToken.Event); + } + + // + // Free the Neighbor Discovery resources + // + while (!IsListEmpty (&IpSb->NeighborTable)) { + NeighborCache = NET_LIST_HEAD (&IpSb->NeighborTable, IP6_NEIGHBOR_ENTRY, Link); + Ip6FreeNeighborEntry (IpSb, NeighborCache, FALSE, TRUE, EFI_SUCCESS, NULL, NULL); + } + + return EFI_SUCCESS; +} + +/** + Create a new IP6 driver service binding protocol. + + @param[in] Controller The controller that has MNP service binding + installed. + @param[in] ImageHandle The IP6 driver's image handle. + @param[out] Service The variable to receive the newly created IP6 + service. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources. + @retval EFI_SUCCESS A new IP6 service binding private is created. + +**/ +EFI_STATUS +Ip6CreateService ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE ImageHandle, + OUT IP6_SERVICE **Service + ) +{ + IP6_SERVICE *IpSb; + EFI_STATUS Status; + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken; + EFI_MANAGED_NETWORK_CONFIG_DATA *Config; + + ASSERT (Service != NULL); + + *Service = NULL; + + // + // allocate a service private data then initialize all the filed to + // empty resources, so if any thing goes wrong when allocating + // resources, Ip6CleanService can be called to clean it up. + // + IpSb = AllocateZeroPool (sizeof (IP6_SERVICE)); + + if (IpSb == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + IpSb->Signature = IP6_SERVICE_SIGNATURE; + IpSb->ServiceBinding.CreateChild = Ip6ServiceBindingCreateChild; + IpSb->ServiceBinding.DestroyChild = Ip6ServiceBindingDestroyChild; + IpSb->State = IP6_SERVICE_UNSTARTED; + + IpSb->NumChildren = 0; + InitializeListHead (&IpSb->Children); + + InitializeListHead (&IpSb->Interfaces); + IpSb->DefaultInterface = NULL; + IpSb->RouteTable = NULL; + + IpSb->RecvRequest.Signature = IP6_LINK_RX_SIGNATURE; + IpSb->RecvRequest.CallBack = NULL; + IpSb->RecvRequest.Context = NULL; + MnpToken = &IpSb->RecvRequest.MnpToken; + MnpToken->Event = NULL; + MnpToken->Status = EFI_NOT_READY; + MnpToken->Packet.RxData = NULL; + + Ip6CreateAssembleTable (&IpSb->Assemble); + + IpSb->MldCtrl.Mldv1QuerySeen = 0; + InitializeListHead (&IpSb->MldCtrl.Groups); + + ZeroMem (&IpSb->LinkLocalAddr, sizeof (EFI_IPv6_ADDRESS)); + IpSb->LinkLocalOk = FALSE; + IpSb->LinkLocalDadFail = FALSE; + IpSb->Dhcp6NeedStart = FALSE; + IpSb->Dhcp6NeedInfoRequest = FALSE; + + IpSb->CurHopLimit = IP6_HOP_LIMIT; + IpSb->LinkMTU = IP6_MIN_LINK_MTU; + IpSb->BaseReachableTime = IP6_REACHABLE_TIME; + Ip6UpdateReachableTime (IpSb); + // + // RFC4861 RETRANS_TIMER: 1,000 milliseconds + // + IpSb->RetransTimer = IP6_RETRANS_TIMER; + + IpSb->RoundRobin = 0; + + InitializeListHead (&IpSb->NeighborTable); + InitializeListHead (&IpSb->DefaultRouterList); + InitializeListHead (&IpSb->OnlinkPrefix); + InitializeListHead (&IpSb->AutonomousPrefix); + + IpSb->InterfaceIdLen = IP6_IF_ID_LEN; + IpSb->InterfaceId = NULL; + + IpSb->RouterAdvertiseReceived = FALSE; + IpSb->SolicitTimer = IP6_MAX_RTR_SOLICITATIONS; + IpSb->Ticks = 0; + + IpSb->Image = ImageHandle; + IpSb->Controller = Controller; + + IpSb->MnpChildHandle = NULL; + IpSb->Mnp = NULL; + + Config = &IpSb->MnpConfigData; + Config->ReceivedQueueTimeoutValue = 0; + Config->TransmitQueueTimeoutValue = 0; + Config->ProtocolTypeFilter = IP6_ETHER_PROTO; + Config->EnableUnicastReceive = TRUE; + Config->EnableMulticastReceive = TRUE; + Config->EnableBroadcastReceive = TRUE; + Config->EnablePromiscuousReceive = FALSE; + Config->FlushQueuesOnReset = TRUE; + Config->EnableReceiveTimestamps = FALSE; + Config->DisableBackgroundPolling = FALSE; + + ZeroMem (&IpSb->SnpMode, sizeof (EFI_SIMPLE_NETWORK_MODE)); + + IpSb->Timer = NULL; + IpSb->FasterTimer = NULL; + + ZeroMem (&IpSb->Ip6ConfigInstance, sizeof (IP6_CONFIG_INSTANCE)); + + IpSb->MacString = NULL; + + // + // Create various resources. First create the route table, timer + // event, MNP token event and MNP child. + // + + IpSb->RouteTable = Ip6CreateRouteTable (); + if (IpSb->RouteTable == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + Ip6TimerTicking, + IpSb, + &IpSb->Timer + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + Ip6NdFasterTimerTicking, + IpSb, + &IpSb->FasterTimer + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = NetLibCreateServiceChild ( + Controller, + ImageHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + &IpSb->MnpChildHandle + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + IpSb->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **) (&IpSb->Mnp), + ImageHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = Ip6ServiceConfigMnp (IpSb, TRUE); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &IpSb->SnpMode); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + IpSb->MaxPacketSize = IP6_MIN_LINK_MTU - sizeof (EFI_IP6_HEADER); + if (NetLibGetVlanId (IpSb->Controller) != 0) { + // + // This is a VLAN device, reduce MTU by VLAN tag length + // + IpSb->MaxPacketSize -= NET_VLAN_TAG_LEN; + } + IpSb->OldMaxPacketSize = IpSb->MaxPacketSize; + + // + // Currently only ETHERNET is supported in IPv6 stack, since + // link local address requires an IEEE 802 48-bit MACs for + // EUI-64 format interface identifier mapping. + // + if (IpSb->SnpMode.IfType != NET_IFTYPE_ETHERNET) { + Status = EFI_UNSUPPORTED; + goto ON_ERROR; + } + + Status = Ip6InitMld (IpSb); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &IpSb->MacString); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = Ip6ConfigInitInstance (&IpSb->Ip6ConfigInstance); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + IpSb->DefaultInterface = Ip6CreateInterface (IpSb, TRUE); + if (IpSb->DefaultInterface == NULL) { + Status = EFI_DEVICE_ERROR; + goto ON_ERROR; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Ip6OnFrameReceived, + &IpSb->RecvRequest, + &MnpToken->Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link); + + *Service = IpSb; + return EFI_SUCCESS; + +ON_ERROR: + Ip6CleanService (IpSb); + FreePool (IpSb); + return Status; +} + + +/** + Start this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter used to pick a specific child + device to start. + + @retval EFI_SUCCES This driver is added to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Ip6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + IP6_SERVICE *IpSb; + EFI_STATUS Status; + EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg; + IP6_CONFIG_DATA_ITEM *DataItem; + + IpSb = NULL; + Ip6Cfg = NULL; + DataItem = NULL; + + // + // Test for the Ip6 service binding protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiIp6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + if (Status == EFI_SUCCESS) { + return EFI_ALREADY_STARTED; + } + + Status = Ip6CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb); + + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (IpSb != NULL); + + Ip6Cfg = &IpSb->Ip6ConfigInstance.Ip6Config; + + // + // Install the Ip6ServiceBinding Protocol onto ControlerHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiIp6ServiceBindingProtocolGuid, + &IpSb->ServiceBinding, + &gEfiIp6ConfigProtocolGuid, + Ip6Cfg, + NULL + ); + if (EFI_ERROR (Status)) { + goto FREE_SERVICE; + } + + // + // Read the config data from NV variable again. + // The default data can be changed by other drivers. + // + Status = Ip6ConfigReadConfigData (IpSb->MacString, &IpSb->Ip6ConfigInstance); + if (EFI_ERROR (Status)) { + goto UNINSTALL_PROTOCOL; + } + + // + // If there is any default manual address, set it. + // + DataItem = &IpSb->Ip6ConfigInstance.DataItem[Ip6ConfigDataTypeManualAddress]; + if (DataItem->Data.Ptr != NULL) { + Status = Ip6Cfg->SetData ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + DataItem->DataSize, + DataItem->Data.Ptr + ); + if (Status == EFI_INVALID_PARAMETER || Status == EFI_BAD_BUFFER_SIZE) { + // + // Clean the invalid ManualAddress configuration. + // + Status = Ip6Cfg->SetData ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + 0, + NULL + ); + DEBUG ((EFI_D_WARN, "Ip6DriverBindingStart: Clean the invalid ManualAddress configuration.\n")); + } + } + + // + // If there is any default gateway address, set it. + // + DataItem = &IpSb->Ip6ConfigInstance.DataItem[Ip6ConfigDataTypeGateway]; + if (DataItem->Data.Ptr != NULL) { + Status = Ip6Cfg->SetData ( + Ip6Cfg, + Ip6ConfigDataTypeGateway, + DataItem->DataSize, + DataItem->Data.Ptr + ); + if (Status == EFI_INVALID_PARAMETER || Status == EFI_BAD_BUFFER_SIZE) { + // + // Clean the invalid Gateway configuration. + // + Status = Ip6Cfg->SetData ( + Ip6Cfg, + Ip6ConfigDataTypeGateway, + 0, + NULL + ); + DEBUG ((EFI_D_WARN, "Ip6DriverBindingStart: Clean the invalid Gateway configuration.\n")); + } + } + + // + // ready to go: start the receiving and timer + // + Status = Ip6ReceiveFrame (Ip6AcceptFrame, IpSb); + if (EFI_ERROR (Status)) { + goto UNINSTALL_PROTOCOL; + } + + // + // The timer expires every 100 (IP6_TIMER_INTERVAL_IN_MS) milliseconds. + // + Status = gBS->SetTimer ( + IpSb->FasterTimer, + TimerPeriodic, + TICKS_PER_MS * IP6_TIMER_INTERVAL_IN_MS + ); + if (EFI_ERROR (Status)) { + goto UNINSTALL_PROTOCOL; + } + + // + // The timer expires every 1000 (IP6_ONE_SECOND_IN_MS) milliseconds. + // + Status = gBS->SetTimer ( + IpSb->Timer, + TimerPeriodic, + TICKS_PER_MS * IP6_ONE_SECOND_IN_MS + ); + if (EFI_ERROR (Status)) { + goto UNINSTALL_PROTOCOL; + } + + // + // Initialize the IP6 ID + // + mIp6Id = NET_RANDOM (NetRandomInitSeed ()); + + return EFI_SUCCESS; + +UNINSTALL_PROTOCOL: + gBS->UninstallMultipleProtocolInterfaces ( + ControllerHandle, + &gEfiIp6ServiceBindingProtocolGuid, + &IpSb->ServiceBinding, + &gEfiIp6ConfigProtocolGuid, + Ip6Cfg, + NULL + ); + +FREE_SERVICE: + Ip6CleanService (IpSb); + FreePool (IpSb); + return Status; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +Ip6DestroyChildEntryInHandleBuffer ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + IP6_PROTOCOL *IpInstance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE); + ServiceBinding = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding; + NumberOfChildren = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren; + ChildHandleBuffer = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer; + + if (!NetIsInHandleBuffer (IpInstance->Handle, NumberOfChildren, ChildHandleBuffer)) { + return EFI_SUCCESS; + } + + return ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle); +} + +/** + Stop this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number + of children is zero, stop the entire bus driver. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +Ip6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + IP6_SERVICE *IpSb; + EFI_HANDLE NicHandle; + EFI_STATUS Status; + LIST_ENTRY *List; + INTN State; + BOOLEAN IsDhcp6; + IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context; + + IsDhcp6 = FALSE; + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp6ProtocolGuid); + if (NicHandle != NULL) { + IsDhcp6 = TRUE; + } else { + return EFI_SUCCESS; + } + } + + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiIp6ServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + NicHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + IpSb = IP6_SERVICE_FROM_PROTOCOL (ServiceBinding); + + if (IsDhcp6) { + Status = Ip6ConfigDestroyDhcp6 (&IpSb->Ip6ConfigInstance); + gBS->CloseEvent (IpSb->Ip6ConfigInstance.Dhcp6Event); + IpSb->Ip6ConfigInstance.Dhcp6Event = NULL; + } else if (NumberOfChildren != 0) { + // + // NumberOfChildren is not zero, destroy the IP6 children instances in ChildHandleBuffer. + // + List = &IpSb->Children; + Context.ServiceBinding = ServiceBinding; + Context.NumberOfChildren = NumberOfChildren; + Context.ChildHandleBuffer = ChildHandleBuffer; + Status = NetDestroyLinkList ( + List, + Ip6DestroyChildEntryInHandleBuffer, + &Context, + NULL + ); + } else if (IsListEmpty (&IpSb->Children)) { + State = IpSb->State; + Status = Ip6CleanService (IpSb); + if (EFI_ERROR (Status)) { + IpSb->State = State; + goto Exit; + } + + Status = gBS->UninstallMultipleProtocolInterfaces ( + NicHandle, + &gEfiIp6ServiceBindingProtocolGuid, + ServiceBinding, + &gEfiIp6ConfigProtocolGuid, + &IpSb->Ip6ConfigInstance.Ip6Config, + NULL + ); + ASSERT_EFI_ERROR (Status); + FreePool (IpSb); + Status = EFI_SUCCESS; + } + +Exit: + return Status; +} + + +/** + Creates a child handle with a set of I/O services. + + @param[in] This Protocol instance pointer. + @param[in] ChildHandle Pointer to the handle of the child to create. If + it is NULL, then a new handle is created. If it + is not NULL, then the I/O services are added to + the existing child handle. + + @retval EFI_SUCCES The child handle was created with the I/O services. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +Ip6ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ) +{ + IP6_SERVICE *IpSb; + IP6_PROTOCOL *IpInstance; + EFI_TPL OldTpl; + EFI_STATUS Status; + VOID *Mnp; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + IpSb = IP6_SERVICE_FROM_PROTOCOL (This); + + if (IpSb->LinkLocalDadFail) { + return EFI_DEVICE_ERROR; + } + + IpInstance = AllocatePool (sizeof (IP6_PROTOCOL)); + + if (IpInstance == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Ip6InitProtocol (IpSb, IpInstance); + + // + // Install Ip6 onto ChildHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiIp6ProtocolGuid, + &IpInstance->Ip6Proto, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + IpInstance->Handle = *ChildHandle; + + // + // Open the Managed Network protocol BY_CHILD. + // + Status = gBS->OpenProtocol ( + IpSb->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **) &Mnp, + gIp6DriverBinding.DriverBindingHandle, + IpInstance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + gBS->UninstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiIp6ProtocolGuid, + &IpInstance->Ip6Proto, + NULL + ); + + goto ON_ERROR; + } + + // + // Insert it into the service binding instance. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&IpSb->Children, &IpInstance->Link); + IpSb->NumChildren++; + + gBS->RestoreTPL (OldTpl); + +ON_ERROR: + + if (EFI_ERROR (Status)) { + + Ip6CleanProtocol (IpInstance); + + FreePool (IpInstance); + } + + return Status; +} + +/** + Destroys a child handle with a set of I/O services. + + @param[in] This Protocol instance pointer. + @param[in] ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The I/O services were removed from the child + handle. + @retval EFI_UNSUPPORTED The child handle does not support the I/O services + that are being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The child handle could not be destroyed because + its I/O services are being used. + @retval other The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +Ip6ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + EFI_STATUS Status; + IP6_SERVICE *IpSb; + IP6_PROTOCOL *IpInstance; + EFI_IP6_PROTOCOL *Ip6; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Retrieve the private context data structures + // + IpSb = IP6_SERVICE_FROM_PROTOCOL (This); + + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiIp6ProtocolGuid, + (VOID **) &Ip6, + gIp6DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + IpInstance = IP6_INSTANCE_FROM_PROTOCOL (Ip6); + + if (IpInstance->Service != IpSb) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // A child can be destroyed more than once. For example, + // Ip6DriverBindingStop will destroy all of its children. + // when UDP driver is being stopped, it will destroy all + // the IP child it opens. + // + if (IpInstance->InDestroy) { + gBS->RestoreTPL (OldTpl); + return EFI_SUCCESS; + } + + IpInstance->InDestroy = TRUE; + + // + // Close the Managed Network protocol. + // + gBS->CloseProtocol ( + IpSb->MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + gIp6DriverBinding.DriverBindingHandle, + ChildHandle + ); + + // + // Uninstall the IP6 protocol first. Many thing happens during + // this: + // 1. The consumer of the IP6 protocol will be stopped if it + // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is + // stopped, IP driver's stop function will be called, and uninstall + // EFI_IP6_PROTOCOL will trigger the UDP's stop function. This + // makes it possible to create the network stack bottom up, and + // stop it top down. + // 2. the upper layer will recycle the received packet. The recycle + // event's TPL is higher than this function. The recycle events + // will be called back before preceeding. If any packets not recycled, + // that means there is a resource leak. + // + gBS->RestoreTPL (OldTpl); + Status = gBS->UninstallProtocolInterface ( + ChildHandle, + &gEfiIp6ProtocolGuid, + &IpInstance->Ip6Proto + ); + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = Ip6CleanProtocol (IpInstance); + if (EFI_ERROR (Status)) { + gBS->InstallMultipleProtocolInterfaces ( + &ChildHandle, + &gEfiIp6ProtocolGuid, + Ip6, + NULL + ); + + goto ON_ERROR; + } + + RemoveEntryList (&IpInstance->Link); + ASSERT (IpSb->NumChildren > 0); + IpSb->NumChildren--; + + gBS->RestoreTPL (OldTpl); + + FreePool (IpInstance); + return EFI_SUCCESS; + +ON_ERROR: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/Ip6Dxe/Ip6Driver.h b/NetworkPkg/Ip6Dxe/Ip6Driver.h new file mode 100644 index 000000000..b048dafdc --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Driver.h @@ -0,0 +1,186 @@ +/** @file + The driver binding and service binding protocol for IP6 driver. + + Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP6_DRIVER_H__ +#define __EFI_IP6_DRIVER_H__ + +extern EFI_DRIVER_BINDING_PROTOCOL gIp6DriverBinding; +extern EFI_COMPONENT_NAME_PROTOCOL gIp6ComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gIp6ComponentName2; +extern EFI_UNICODE_STRING_TABLE *gIp6ControllerNameTable; + +typedef struct { + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; +}IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT; + +/** + Clean up an IP6 service binding instance. It releases all + the resource allocated by the instance. The instance may be + partly initialized, or partly destroyed. If a resource is + destroyed, it is marked as that in case the destroy failed and + being called again later. + + @param[in] IpSb The IP6 service binding instance to clean up. + + @retval EFI_SUCCESS The resource used by the instance are cleaned up. + @retval Others Failed to clean up some of the resources. + +**/ +EFI_STATUS +Ip6CleanService ( + IN IP6_SERVICE *IpSb + ); + +// +// Function prototype for the driver's entry point +// + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + The entry point for IP6 driver which installs the driver + binding and component name protocol on its image. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +Ip6DriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ); + +// +// Function prototypes for the Drivr Binding Protocol +// + +/** + Test to see if this driver supports ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Ip6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter used to pick a specific child + device to start. + + @retval EFI_SUCCES This driver is added to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Ip6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number + of children is zero, stop the entire bus driver. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +Ip6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +// +// Function prototypes for the ServiceBinding Protocol +// + +/** + Creates a child handle with a set of I/O services. + + @param[in] This Protocol instance pointer. + @param[in] ChildHandle Pointer to the handle of the child to create. If + it is NULL, then a new handle is created. If it + is not NULL, then the I/O services are added to + the existing child handle. + + @retval EFI_SUCCES The child handle was created with the I/O services. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +Ip6ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a set of I/O services. + + @param[in] This Protocol instance pointer. + @param[in] ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The I/O services were removed from the child + handle. + @retval EFI_UNSUPPORTED The child handle does not support the I/O services + that are being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The child handle could not be destroyed because + its I/O services are being used. + @retval other The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +Ip6ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +#endif diff --git a/NetworkPkg/Ip6Dxe/Ip6Dxe.inf b/NetworkPkg/Ip6Dxe/Ip6Dxe.inf new file mode 100644 index 000000000..965bcdf11 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Dxe.inf @@ -0,0 +1,110 @@ +## @file +# Basic IPv6 packet I/O Service. +# +# This module provides basic network IPv6 packet I/O services which includes support for +# Neighbor Discovery Protocol (ND), Multicast Listener Discovery Protocol (MLD), +# and a subset of the Internet Control Message Protocol (ICMPv6). This driver +# also provides the mechanism to set and get various types of configurations for +# the EFI IPv6 network stack. +# +# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = Ip6Dxe + FILE_GUID = 5BEDB5CC-D830-4eb2-8742-2D4CC9B54F2C + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = Ip6DriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload + MODULE_UNI_FILE = Ip6Dxe.uni + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = gIp6DriverBinding +# COMPONENT_NAME = gIp6ComponentName +# COMPONENT_NAME2 = gIp6ComponentName2 +# + +[Sources] + Ip6Output.h + Ip6Option.h + Ip6Input.h + Ip6Nd.h + Ip6Mld.h + Ip6Impl.c + Ip6Driver.c + ComponentName.c + Ip6Nd.c + Ip6Input.c + Ip6ConfigImpl.c + Ip6ConfigImpl.h + Ip6Impl.h + Ip6Option.c + Ip6If.h + Ip6Icmp.h + Ip6Mld.c + Ip6Common.c + Ip6Route.c + Ip6If.c + Ip6Driver.h + Ip6Output.c + Ip6Icmp.c + Ip6Common.h + Ip6Route.h + Ip6DxeStrings.uni + Ip6NvData.h + Ip6ConfigNv.c + Ip6ConfigNv.h + Ip6Config.vfr + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DevicePathLib + HiiLib + UefiHiiServicesLib + PrintLib + MemoryAllocationLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiRuntimeServicesTableLib + UefiLib + DebugLib + NetLib + DpcLib + +[Protocols] + gEfiManagedNetworkServiceBindingProtocolGuid ## TO_START + gEfiManagedNetworkProtocolGuid ## TO_START + gEfiIp6ServiceBindingProtocolGuid ## BY_START + gEfiIp6ProtocolGuid ## BY_START + gEfiIp6ConfigProtocolGuid ## BY_START + gEfiDhcp6ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiDhcp6ProtocolGuid ## SOMETIMES_CONSUMES + gEfiIpSec2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiHiiConfigAccessProtocolGuid ## SOMETIMES_CONSUMES + +[Guids] + ## SOMETIMES_CONSUMES ## HII + gEfiIfrTianoGuid + ## SOMETIMES_CONSUMES ## UNDEFINED # HiiIsConfigHdrMatch mIp6ConfigStorageName + ## SOMETIMES_PRODUCES ## UNDEFINED # HiiConstructConfigHdr mIp6ConfigStorageName + ## SOMETIMES_PRODUCES ## UNDEFINED # HiiAddPackages Ip6DxeStrings Ip6ConfigBin + ## SOMETIMES_CONSUMES ## UNDEFINED # HiiUpdateForm + ## SOMETIMES_CONSUMES ## HII + gIp6ConfigNvDataGuid +[UserExtensions.TianoCore."ExtraFiles"] + Ip6DxeExtra.uni diff --git a/NetworkPkg/Ip6Dxe/Ip6Dxe.uni b/NetworkPkg/Ip6Dxe/Ip6Dxe.uni new file mode 100644 index 000000000..947389ae5 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Dxe.uni @@ -0,0 +1,20 @@ +// /** @file +// Basic IPv6 packet I/O Service. +// +// This module provides basic network IPv6 packet I/O services which includes support for +// Neighbor Discovery Protocol (ND), Multicast Listener Discovery Protocol (MLD), +// and a subset of the Internet Control Message Protocol (ICMPv6). This driver +// also provides the mechanism to set and get various types of configurations for +// the EFI IPv6 network stack. +// +// Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Basic IPv6 packet I/O Service" + +#string STR_MODULE_DESCRIPTION #language en-US "This module provides basic network IPv6 packet I/O services which includes support for Neighbor Discovery Protocol (ND), Multicast Listener Discovery Protocol (MLD), and a subset of the Internet Control Message Protocol (ICMPv6). This driver also provides the mechanism to set and get various types of configurations for the EFI IPv6 network stack." + diff --git a/NetworkPkg/Ip6Dxe/Ip6DxeExtra.uni b/NetworkPkg/Ip6Dxe/Ip6DxeExtra.uni new file mode 100644 index 000000000..3f16fddb4 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6DxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// Ip6Dxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"Ip6 DXE" + + diff --git a/NetworkPkg/Ip6Dxe/Ip6DxeStrings.uni b/NetworkPkg/Ip6Dxe/Ip6DxeStrings.uni new file mode 100644 index 000000000..6b77176d4 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6DxeStrings.uni @@ -0,0 +1,55 @@ +/** @file + String definitions for IP6 configuration. + + Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#langdef en-US "English" + +#string STR_IP6_CONFIG_FORM_TITLE #language en-US "IPv6 Network Configuration" +#string STR_IP6_CONFIG_FORM_HELP #language en-US "Configure IPv6 network parameters." +#string STR_IP6_DEVICE_FORM_TITLE #language en-US "IPv6 Current Setting" +#string STR_IP6_DEVICE_FORM_HELP #language en-US "Display IPv6 network parameters." +#string STR_IP6_INTERFACE_NAME #language en-US "Interface Name :" +#string STR_IP6_INTERFACE_NAME_HELP #language en-US "The name of the interface." +#string STR_IP6_INTERFACE_NAME_CONTENT #language en-US "" +#string STR_IP6_INTERFACE_TYPE #language en-US "Interface Type :" +#string STR_IP6_INTERFACE_TYPE_HELP #language en-US "The interface type of the network interface, defined in RFC1700." +#string STR_IP6_INTERFACE_TYPE_CONTENT #language en-US "" +#string STR_IP6_MAC_ADDRESS #language en-US "MAC address :" +#string STR_IP6_MAC_ADDRESS_HELP #language en-US "The hardware address of the network interface." +#string STR_IP6_MAC_ADDRESS_CONTENT #language en-US "" +#string STR_IP6_HOST_ADDRESS #language en-US "Host addresses :" +#string STR_IP6_HOST_ADDRESS_HELP #language en-US "The address list which contain the local IPv6 addresses and the corresponding prefix length information." +#string STR_IP6_ROUTE_TABLE #language en-US "Route Table :" +#string STR_IP6_ROUTE_TABLE_HELP #language en-US "The route table of the IPv6 network stack runs on this interface." +#string STR_IP6_GATEWAY_ADDRESS #language en-US "Gateway addresses :" +#string STR_IP6_GATEWAY_ADDRESS_HELP #language en-US "Current gateway IPv6 addresses." +#string STR_IP6_DNS_ADDRESS #language en-US "DNS addresses :" +#string STR_IP6_DNS_ADDRESS_HELP #language en-US "Current DNS server list." +#string STR_IP6_INTERFACE_ID #language en-US "Interface ID" +#string STR_IP6_INTERFACE_ID_HELP #language en-US "The 64 bit alternative interface ID for the device. The string is colon separated. e.g. ff:dd:88:66:cc:1:2:3" +#string STR_IP6_DAD_TRANSMIT_COUNT #language en-US "DAD Transmit Count" +#string STR_IP6_DAD_TRANSMIT_COUNT_HELP #language en-US "The number of consecutive Neighbor Solicitation messages sent while performing Duplicate Address Detection on a tentative address. A value of zero indicates that Duplicate Address Detection is not performed." +#string STR_POLICY_TYPE_PROMPT #language en-US "Policy" +#string STR_POLICY_TYPE_HELP #language en-US "automatic or manual" +#string STR_POLICY_TYPE_AUTO #language en-US "automatic" +#string STR_POLICY_TYPE_MANUAL #language en-US "manual" +#string STR_IP6_AD_CONFIG_FORM #language en-US "Advanced Configuration" +#string STR_IP6_AD_CONFIG_FORM_HELP #language en-US "Configure the interface manually. IP address, gateway address, and DNS server address can be configured." +#string STR_IP6_MANUAL_ADDRESS #language en-US "New IPv6 address" +#string STR_IP6_MANUAL_ADDRESS_HELP #language en-US "Manual IP address can only be configured under manual policy. Separate the IP address with blank space to configure more than one address. e.g. 2002::1/64 2002::2/64" +#string STR_IP6_NEW_GATEWAY_ADDRESS #language en-US "New Gateway addresses" +#string STR_IP6_NEW_GATEWAY_ADDR_HELP #language en-US "Gateway IP address can only be configured under manual policy. e.g. 2002::3. Separate the IP address with blank space to configure more than one address." +#string STR_IP6_NEW_DNS_ADDRESS #language en-US "New DNS addresses" +#string STR_IP6_NEW_DNS_ADDRESS_HELP #language en-US "DNS addresses can only be configured under manual policy. e.g. 2002::4. Separate the IP address with blank space to configure more than one address." +#string STR_SAVE_CHANGES #language en-US "Save Changes and Exit" +#string STR_SAVE_CHANGES_HELP #language en-US "Save Changes for interface ID, DAD transmit count, policy, and data in advanced configuration." +#string STR_SAVE_AND_EXIT #language en-US "Commit Changes and Exit" +#string STR_NO_SAVE_AND_EXIT #language en-US "Discard Changes and Exit" +#string STR_NULL #language en-US "" +#string STR_GET_CURRENT_SETTING #language en-US "Enter Configuration Menu" +#string STR_GET_CURRENT_SETTING_HELP #language en-US "Press ENTER to enter configuration menu for IPv6 configuration." diff --git a/NetworkPkg/Ip6Dxe/Ip6Icmp.c b/NetworkPkg/Ip6Dxe/Ip6Icmp.c new file mode 100644 index 000000000..79fa34e04 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Icmp.c @@ -0,0 +1,679 @@ +/** @file + The ICMPv6 handle routines to process the ICMPv6 control messages. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +EFI_IP6_ICMP_TYPE mIp6SupportedIcmp[23] = { + + { + ICMP_V6_DEST_UNREACHABLE, + ICMP_V6_NO_ROUTE_TO_DEST + }, + { + ICMP_V6_DEST_UNREACHABLE, + ICMP_V6_COMM_PROHIBITED + }, + { + ICMP_V6_DEST_UNREACHABLE, + ICMP_V6_BEYOND_SCOPE + }, + { + ICMP_V6_DEST_UNREACHABLE, + ICMP_V6_ADDR_UNREACHABLE + }, + { + ICMP_V6_DEST_UNREACHABLE, + ICMP_V6_PORT_UNREACHABLE + }, + { + ICMP_V6_DEST_UNREACHABLE, + ICMP_V6_SOURCE_ADDR_FAILED + }, + { + ICMP_V6_DEST_UNREACHABLE, + ICMP_V6_ROUTE_REJECTED + }, + + { + ICMP_V6_PACKET_TOO_BIG, + ICMP_V6_DEFAULT_CODE + }, + + { + ICMP_V6_TIME_EXCEEDED, + ICMP_V6_TIMEOUT_HOP_LIMIT + }, + { + ICMP_V6_TIME_EXCEEDED, + ICMP_V6_TIMEOUT_REASSEMBLE + }, + + { + ICMP_V6_PARAMETER_PROBLEM, + ICMP_V6_ERRONEOUS_HEADER + }, + { + ICMP_V6_PARAMETER_PROBLEM, + ICMP_V6_UNRECOGNIZE_NEXT_HDR + }, + { + ICMP_V6_PARAMETER_PROBLEM, + ICMP_V6_UNRECOGNIZE_OPTION + }, + + { + ICMP_V6_ECHO_REQUEST, + ICMP_V6_DEFAULT_CODE + }, + { + ICMP_V6_ECHO_REPLY, + ICMP_V6_DEFAULT_CODE + }, + + { + ICMP_V6_LISTENER_QUERY, + ICMP_V6_DEFAULT_CODE + }, + { + ICMP_V6_LISTENER_REPORT, + ICMP_V6_DEFAULT_CODE + }, + { + ICMP_V6_LISTENER_REPORT_2, + ICMP_V6_DEFAULT_CODE + }, + { + ICMP_V6_LISTENER_DONE, + ICMP_V6_DEFAULT_CODE + }, + + { + ICMP_V6_ROUTER_SOLICIT, + ICMP_V6_DEFAULT_CODE + }, + { + ICMP_V6_ROUTER_ADVERTISE, + ICMP_V6_DEFAULT_CODE + }, + { + ICMP_V6_NEIGHBOR_SOLICIT, + ICMP_V6_DEFAULT_CODE + }, + { + ICMP_V6_NEIGHBOR_ADVERTISE, + ICMP_V6_DEFAULT_CODE + }, +}; + +/** + Reply an ICMPv6 echo request. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the ICMPv6 informational message. + @param[in] Packet The content of the ICMPv6 message with the IP head + removed. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_SUCCESS Successfully answered the ICMPv6 Echo request. + @retval Others Failed to answer the ICMPv6 Echo request. + +**/ +EFI_STATUS +Ip6IcmpReplyEcho ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_ICMP_INFORMATION_HEAD *Icmp; + NET_BUF *Data; + EFI_STATUS Status; + EFI_IP6_HEADER ReplyHead; + + Status = EFI_OUT_OF_RESOURCES; + // + // make a copy the packet, it is really a bad idea to + // send the MNP's buffer back to MNP. + // + Data = NetbufDuplicate (Packet, NULL, IP6_MAX_HEADLEN); + if (Data == NULL) { + goto Exit; + } + + // + // Change the ICMP type to echo reply, exchange the source + // and destination, then send it. The source is updated to + // use specific destination. See RFC1122. SRR/RR option + // update is omitted. + // + Icmp = (IP6_ICMP_INFORMATION_HEAD *) NetbufGetByte (Data, 0, NULL); + if (Icmp == NULL) { + NetbufFree (Data); + goto Exit; + } + + Icmp->Head.Type = ICMP_V6_ECHO_REPLY; + Icmp->Head.Checksum = 0; + + // + // Generate the IPv6 basic header + // If the Echo Reply is a response to a Echo Request sent to one of the node's unicast address, + // the Source address of the Echo Reply must be the same address. + // + ZeroMem (&ReplyHead, sizeof (EFI_IP6_HEADER)); + + ReplyHead.PayloadLength = HTONS ((UINT16) (Packet->TotalSize)); + ReplyHead.NextHeader = IP6_ICMP; + ReplyHead.HopLimit = IpSb->CurHopLimit; + IP6_COPY_ADDRESS (&ReplyHead.DestinationAddress, &Head->SourceAddress); + + if (Ip6IsOneOfSetAddress (IpSb, &Head->DestinationAddress, NULL, NULL)) { + IP6_COPY_ADDRESS (&ReplyHead.SourceAddress, &Head->DestinationAddress); + } + + // + // If source is unspecified, Ip6Output will select a source for us + // + Status = Ip6Output ( + IpSb, + NULL, + NULL, + Data, + &ReplyHead, + NULL, + 0, + Ip6SysPacketSent, + NULL + ); + +Exit: + NetbufFree (Packet); + return Status; +} + +/** + Process Packet Too Big message sent by a router in response to a packet that + it cannot forward because the packet is larger than the MTU of outgoing link. + Since this driver already uses IPv6 minimum link MTU as the maximum packet size, + if Packet Too Big message is still received, do not reduce the packet size, but + rather include a Fragment header in the subsequent packets. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the ICMPv6 error packet. + @param[in] Packet The content of the ICMPv6 error with the IP head + removed. + + @retval EFI_SUCCESS The ICMPv6 error processed successfully. + @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lack of + resource. + @retval EFI_NOT_FOUND The packet too big message is not sent to us. + +**/ +EFI_STATUS +Ip6ProcessPacketTooBig ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_ICMP_ERROR_HEAD Icmp; + UINT32 Mtu; + IP6_ROUTE_ENTRY *RouteEntry; + EFI_IPv6_ADDRESS *DestAddress; + + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + Mtu = NTOHL (Icmp.Fourth); + DestAddress = &Icmp.IpHead.DestinationAddress; + + if (Mtu < IP6_MIN_LINK_MTU) { + // + // Normally the multicast address is considered to be on-link and not recorded + // in route table. Here it is added into the table since the MTU information + // need be recorded. + // + if (IP6_IS_MULTICAST (DestAddress)) { + RouteEntry = Ip6CreateRouteEntry (DestAddress, 128, NULL); + if (RouteEntry == NULL) { + NetbufFree (Packet); + return EFI_OUT_OF_RESOURCES; + } + + RouteEntry->Flag = IP6_DIRECT_ROUTE | IP6_PACKET_TOO_BIG; + InsertHeadList (&IpSb->RouteTable->RouteArea[128], &RouteEntry->Link); + IpSb->RouteTable->TotalNum++; + } else { + RouteEntry = Ip6FindRouteEntry (IpSb->RouteTable, DestAddress, NULL); + if (RouteEntry == NULL) { + NetbufFree (Packet); + return EFI_NOT_FOUND; + } + + RouteEntry->Flag = RouteEntry->Flag | IP6_PACKET_TOO_BIG; + + Ip6FreeRouteEntry (RouteEntry); + } + } + + NetbufFree (Packet); + return EFI_SUCCESS; +} + +/** + Process the ICMPv6 error packet, and deliver the packet to upper layer. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the ICMPv6 error packet. + @param[in] Packet The content of the ICMPv6 error with the IP head + removed. + + @retval EFI_SUCCESS The ICMPv6 error processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid. + @retval Others Failed to process the packet. + +**/ +EFI_STATUS +Ip6ProcessIcmpError ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_ICMP_ERROR_HEAD Icmp; + + // + // Check the validity of the packet + // + if (Packet->TotalSize < sizeof (Icmp)) { + goto DROP; + } + + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + if (Icmp.Head.Type == ICMP_V6_PACKET_TOO_BIG) { + return Ip6ProcessPacketTooBig (IpSb, Head, Packet); + } + + // + // Notify the upper-layer process that an ICMPv6 eror message is received. + // + IP6_GET_CLIP_INFO (Packet)->Status = EFI_ICMP_ERROR; + return Ip6Demultiplex (IpSb, Head, Packet); + +DROP: + NetbufFree (Packet); + Packet = NULL; + return EFI_INVALID_PARAMETER; +} + +/** + Process the ICMPv6 informational messages. If it is an ICMPv6 echo + request, answer it. If it is a MLD message, trigger MLD routines to + process it. If it is a ND message, trigger ND routines to process it. + Otherwise, deliver it to upper layer. + + @param[in] IpSb The IP service that receivd the packet. + @param[in] Head The IP head of the ICMPv6 informational packet. + @param[in] Packet The content of the ICMPv6 informational packet + with IP head removed. + + @retval EFI_INVALID_PARAMETER The packet is invalid. + @retval EFI_SUCCESS The ICMPv6 informational message processed. + @retval Others Failed to process ICMPv6 informational message. + +**/ +EFI_STATUS +Ip6ProcessIcmpInformation ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_ICMP_INFORMATION_HEAD Icmp; + EFI_STATUS Status; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE); + ASSERT (Head != NULL); + + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + Status = EFI_INVALID_PARAMETER; + + switch (Icmp.Head.Type) { + case ICMP_V6_ECHO_REQUEST: + // + // If ICMPv6 echo, reply it + // + if (Icmp.Head.Code == 0) { + Status = Ip6IcmpReplyEcho (IpSb, Head, Packet); + } + break; + case ICMP_V6_LISTENER_QUERY: + Status = Ip6ProcessMldQuery (IpSb, Head, Packet); + break; + case ICMP_V6_LISTENER_REPORT: + case ICMP_V6_LISTENER_REPORT_2: + Status = Ip6ProcessMldReport (IpSb, Head, Packet); + break; + case ICMP_V6_NEIGHBOR_SOLICIT: + Status = Ip6ProcessNeighborSolicit (IpSb, Head, Packet); + break; + case ICMP_V6_NEIGHBOR_ADVERTISE: + Status = Ip6ProcessNeighborAdvertise (IpSb, Head, Packet); + break; + case ICMP_V6_ROUTER_ADVERTISE: + Status = Ip6ProcessRouterAdvertise (IpSb, Head, Packet); + break; + case ICMP_V6_REDIRECT: + Status = Ip6ProcessRedirect (IpSb, Head, Packet); + break; + case ICMP_V6_ECHO_REPLY: + Status = Ip6Demultiplex (IpSb, Head, Packet); + break; + default: + Status = EFI_INVALID_PARAMETER; + break; + } + + return Status; +} + +/** + Handle the ICMPv6 packet. First validate the message format, + then, according to the message types, process it as an informational packet or + an error packet. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the ICMPv6 packet. + @param[in] Packet The content of the ICMPv6 packet with IP head + removed. + + @retval EFI_INVALID_PARAMETER The packet is malformated. + @retval EFI_SUCCESS The ICMPv6 message successfully processed. + @retval Others Failed to handle the ICMPv6 packet. + +**/ +EFI_STATUS +Ip6IcmpHandle ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_ICMP_HEAD Icmp; + UINT16 PseudoCheckSum; + UINT16 CheckSum; + + // + // Check the validity of the incoming packet. + // + if (Packet->TotalSize < sizeof (Icmp)) { + goto DROP; + } + + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + + // + // Make sure checksum is valid. + // + PseudoCheckSum = NetIp6PseudoHeadChecksum ( + &Head->SourceAddress, + &Head->DestinationAddress, + IP6_ICMP, + Packet->TotalSize + ); + CheckSum = (UINT16) ~NetAddChecksum (PseudoCheckSum, NetbufChecksum (Packet)); + if (CheckSum != 0) { + goto DROP; + } + + // + // According to the packet type, call corresponding process + // + if (Icmp.Type <= ICMP_V6_ERROR_MAX) { + return Ip6ProcessIcmpError (IpSb, Head, Packet); + } else { + return Ip6ProcessIcmpInformation (IpSb, Head, Packet); + } + +DROP: + NetbufFree (Packet); + return EFI_INVALID_PARAMETER; +} + +/** + Retrieve the Prefix address according to the PrefixLength by clear the useless + bits. + + @param[in] PrefixLength The prefix length of the prefix. + @param[in, out] Prefix On input, points to the original prefix address + with dirty bits; on output, points to the updated + address with useless bit clear. + +**/ +VOID +Ip6GetPrefix ( + IN UINT8 PrefixLength, + IN OUT EFI_IPv6_ADDRESS *Prefix + ) +{ + UINT8 Byte; + UINT8 Bit; + UINT8 Mask; + UINT8 Value; + + ASSERT ((Prefix != NULL) && (PrefixLength < IP6_PREFIX_MAX)); + + if (PrefixLength == 0) { + ZeroMem (Prefix, sizeof (EFI_IPv6_ADDRESS)); + return ; + } + + if (PrefixLength >= IP6_PREFIX_MAX) { + return ; + } + + Byte = (UINT8) (PrefixLength / 8); + Bit = (UINT8) (PrefixLength % 8); + Value = Prefix->Addr[Byte]; + + if (Byte > 0) { + ZeroMem (Prefix->Addr + Byte, 16 - Byte); + } + + if (Bit > 0) { + Mask = (UINT8) (0xFF << (8 - Bit)); + Prefix->Addr[Byte] = (UINT8) (Value & Mask); + } + +} + +/** + Check whether the DestinationAddress is an anycast address. + + @param[in] IpSb The IP service that received the packet. + @param[in] DestinationAddress Points to the Destination Address of the packet. + + @retval TRUE The DestinationAddress is anycast address. + @retval FALSE The DestinationAddress is not anycast address. + +**/ +BOOLEAN +Ip6IsAnycast ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *DestinationAddress + ) +{ + IP6_PREFIX_LIST_ENTRY *PrefixEntry; + EFI_IPv6_ADDRESS Prefix; + BOOLEAN Flag; + + ZeroMem (&Prefix, sizeof (EFI_IPv6_ADDRESS)); + + Flag = FALSE; + + // + // If the address is known as on-link or autonomous prefix, record it as + // anycast address. + // + do { + PrefixEntry = Ip6FindPrefixListEntry (IpSb, Flag, 255, DestinationAddress); + if (PrefixEntry != NULL) { + IP6_COPY_ADDRESS (&Prefix, &PrefixEntry->Prefix); + Ip6GetPrefix (PrefixEntry->PrefixLength, &Prefix); + if (EFI_IP6_EQUAL (&Prefix, DestinationAddress)) { + return TRUE; + } + } + + Flag = (BOOLEAN) !Flag; + } while (Flag); + + return FALSE; +} + +/** + Generate ICMPv6 error message and send it out to DestinationAddress. Currently + Destination Unreachable message, Time Exceeded message and Parameter Problem + message are supported. + + @param[in] IpSb The IP service that received the packet. + @param[in] Packet The packet which invoking ICMPv6 error. + @param[in] SourceAddress If not NULL, points to the SourceAddress. + Otherwise, the IP layer will select a source address + according to the DestinationAddress. + @param[in] DestinationAddress Points to the Destination Address of the ICMPv6 + error message. + @param[in] Type The type of the ICMPv6 message. + @param[in] Code The additional level of the ICMPv6 message. + @param[in] Pointer If not NULL, identifies the octet offset within + the invoking packet where the error was detected. + + @retval EFI_INVALID_PARAMETER The packet is malformated. + @retval EFI_OUT_OF_RESOURCES There is no sufficient resource to complete the + operation. + @retval EFI_SUCCESS The ICMPv6 message was successfully sent out. + @retval Others Failed to generate the ICMPv6 packet. + +**/ +EFI_STATUS +Ip6SendIcmpError ( + IN IP6_SERVICE *IpSb, + IN NET_BUF *Packet, + IN EFI_IPv6_ADDRESS *SourceAddress OPTIONAL, + IN EFI_IPv6_ADDRESS *DestinationAddress, + IN UINT8 Type, + IN UINT8 Code, + IN UINT32 *Pointer OPTIONAL + ) +{ + UINT32 PacketLen; + NET_BUF *ErrorMsg; + UINT16 PayloadLen; + EFI_IP6_HEADER Head; + IP6_ICMP_INFORMATION_HEAD *IcmpHead; + UINT8 *ErrorBody; + + if (DestinationAddress == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // An ICMPv6 error message must not be originated as a result of receiving + // a packet whose source address does not uniquely identify a single node -- + // e.g., the IPv6 Unspecified Address, an IPv6 multicast address, or an address + // known by the ICMP message originator to be an IPv6 anycast address. + // + if (NetIp6IsUnspecifiedAddr (DestinationAddress) || + IP6_IS_MULTICAST (DestinationAddress) || + Ip6IsAnycast (IpSb, DestinationAddress) + ) { + return EFI_INVALID_PARAMETER; + } + + switch (Type) { + case ICMP_V6_DEST_UNREACHABLE: + case ICMP_V6_TIME_EXCEEDED: + break; + + case ICMP_V6_PARAMETER_PROBLEM: + if (Pointer == NULL) { + return EFI_INVALID_PARAMETER; + } + + break; + + default: + return EFI_INVALID_PARAMETER; + } + + PacketLen = sizeof (IP6_ICMP_ERROR_HEAD) + Packet->TotalSize; + + if (PacketLen > IpSb->MaxPacketSize) { + PacketLen = IpSb->MaxPacketSize; + } + + ErrorMsg = NetbufAlloc (PacketLen); + if (ErrorMsg == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + PayloadLen = (UINT16) (PacketLen - sizeof (EFI_IP6_HEADER)); + + // + // Create the basic IPv6 header. + // + ZeroMem (&Head, sizeof (EFI_IP6_HEADER)); + + Head.PayloadLength = HTONS (PayloadLen); + Head.NextHeader = IP6_ICMP; + Head.HopLimit = IpSb->CurHopLimit; + + if (SourceAddress != NULL) { + IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress); + } else { + ZeroMem (&Head.SourceAddress, sizeof (EFI_IPv6_ADDRESS)); + } + + IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress); + + NetbufReserve (ErrorMsg, sizeof (EFI_IP6_HEADER)); + + // + // Fill in the ICMP error message head + // + IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (ErrorMsg, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE); + if (IcmpHead == NULL) { + NetbufFree (ErrorMsg); + return EFI_OUT_OF_RESOURCES; + } + + ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD)); + IcmpHead->Head.Type = Type; + IcmpHead->Head.Code = Code; + + if (Pointer != NULL) { + IcmpHead->Fourth = HTONL (*Pointer); + } + + // + // Fill in the ICMP error message body + // + PayloadLen -= sizeof (IP6_ICMP_INFORMATION_HEAD); + ErrorBody = NetbufAllocSpace (ErrorMsg, PayloadLen, FALSE); + if (ErrorBody != NULL) { + ZeroMem (ErrorBody, PayloadLen); + NetbufCopy (Packet, 0, PayloadLen, ErrorBody); + } + + // + // Transmit the packet + // + return Ip6Output (IpSb, NULL, NULL, ErrorMsg, &Head, NULL, 0, Ip6SysPacketSent, NULL); +} + diff --git a/NetworkPkg/Ip6Dxe/Ip6Icmp.h b/NetworkPkg/Ip6Dxe/Ip6Icmp.h new file mode 100644 index 000000000..49a65f296 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Icmp.h @@ -0,0 +1,102 @@ +/** @file + Header file for ICMPv6 protocol. + + Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP6_ICMP_H__ +#define __EFI_IP6_ICMP_H__ + +#define ICMP_V6_DEFAULT_CODE 0 + +#define ICMP_V6_ERROR_MAX 127 + +// +// ICMPv6 message classes, each class of ICMPv6 message shares +// a common message format. INVALID_MESSAGE is only a flag. +// +#define ICMP_V6_INVALID_MESSAGE 0 +#define ICMP_V6_ERROR_MESSAGE 1 +#define ICMP_V6_INFORMATION_MESSAGE 2 + + +extern EFI_IP6_ICMP_TYPE mIp6SupportedIcmp[]; + +/** + Handle the ICMPv6 packet. First validate the message format, + then, according to the message types, process it as an informational packet or + an error packet. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the ICMPv6 packet. + @param[in] Packet The content of the ICMPv6 packet with IP head + removed. + + @retval EFI_INVALID_PARAMETER The packet is malformated. + @retval EFI_SUCCESS The ICMPv6 message successfully processed. + @retval Others Failed to handle the ICMPv6 packet. + +**/ +EFI_STATUS +Ip6IcmpHandle ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ); + +/** + Check whether the DestinationAddress is an anycast address. + + @param[in] IpSb The IP service that received the packet. + @param[in] DestinationAddress Points to the Destination Address of the packet. + + @retval TRUE The DestinationAddress is anycast address. + @retval FALSE The DestinationAddress is not anycast address. + +**/ +BOOLEAN +Ip6IsAnycast ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *DestinationAddress + ); + +/** + Generate ICMPv6 error message and send it out to DestinationAddress. Currently + Destination Unreachable message, Time Exceeded message and Parameter Problem + message are supported. + + @param[in] IpSb The IP service that received the packet. + @param[in] Packet The packet which invoking ICMPv6 error. + @param[in] SourceAddress If not NULL, points to the SourceAddress. + Otherwise, the IP layer will select a source address + according to the DestinationAddress. + @param[in] DestinationAddress Points to the Destination Address of the ICMPv6 + error message. + @param[in] Type The type of the ICMPv6 message. + @param[in] Code The additional level of the ICMPv6 message. + @param[in] Pointer If not NULL, identifies the octet offset within + the invoking packet where the error was detected. + + @retval EFI_INVALID_PARAMETER The packet is malformated. + @retval EFI_OUT_OF_RESOURCES There is no sufficient resource to complete the + operation. + @retval EFI_SUCCESS The ICMPv6 message was successfully sent out. + @retval Others Failed to generate the ICMPv6 packet. + +**/ +EFI_STATUS +Ip6SendIcmpError ( + IN IP6_SERVICE *IpSb, + IN NET_BUF *Packet, + IN EFI_IPv6_ADDRESS *SourceAddress OPTIONAL, + IN EFI_IPv6_ADDRESS *DestinationAddress, + IN UINT8 Type, + IN UINT8 Code, + IN UINT32 *Pointer OPTIONAL + ); + +#endif + diff --git a/NetworkPkg/Ip6Dxe/Ip6If.c b/NetworkPkg/Ip6Dxe/Ip6If.c new file mode 100644 index 000000000..956c05c39 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6If.c @@ -0,0 +1,792 @@ +/** @file + Implement IP6 pesudo interface. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +/** + Request Ip6OnFrameSentDpc as a DPC at TPL_CALLBACK. + + @param[in] Event The transmit token's event. + @param[in] Context The Context which is pointed to the token. + +**/ +VOID +EFIAPI +Ip6OnFrameSent ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Fileter function to cancel all the frame related to an IP instance. + + @param[in] Frame The transmit request to test whether to cancel. + @param[in] Context The context which is the Ip instance that issued + the transmit. + + @retval TRUE The frame belongs to this instance and is to be + removed. + @retval FALSE The frame doesn't belong to this instance. + +**/ +BOOLEAN +Ip6CancelInstanceFrame ( + IN IP6_LINK_TX_TOKEN *Frame, + IN VOID *Context + ) +{ + if (Frame->IpInstance == (IP6_PROTOCOL *) Context) { + return TRUE; + } + + return FALSE; +} + +/** + Set the interface's address. This will trigger the DAD process for the + address to set. To set an already set address, the lifetimes wil be + updated to the new value passed in. + + @param[in] Interface The interface to set the address. + @param[in] Ip6Addr The interface's to be assigned IPv6 address. + @param[in] IsAnycast If TRUE, the unicast IPv6 address is anycast. + Otherwise, it is not anycast. + @param[in] PrefixLength The prefix length of the Ip6Addr. + @param[in] ValidLifetime The valid lifetime for this address. + @param[in] PreferredLifetime The preferred lifetime for this address. + @param[in] DadCallback The caller's callback to trigger when DAD finishes. + This is an optional parameter that may be NULL. + @param[in] Context The context that will be passed to DadCallback. + This is an optional parameter that may be NULL. + + @retval EFI_SUCCESS The interface is scheduled to be configured with + the specified address. + @retval EFI_OUT_OF_RESOURCES Failed to set the interface's address due to + lack of resources. + +**/ +EFI_STATUS +Ip6SetAddress ( + IN IP6_INTERFACE *Interface, + IN EFI_IPv6_ADDRESS *Ip6Addr, + IN BOOLEAN IsAnycast, + IN UINT8 PrefixLength, + IN UINT32 ValidLifetime, + IN UINT32 PreferredLifetime, + IN IP6_DAD_CALLBACK DadCallback OPTIONAL, + IN VOID *Context OPTIONAL + ) +{ + IP6_SERVICE *IpSb; + IP6_ADDRESS_INFO *AddressInfo; + LIST_ENTRY *Entry; + IP6_PREFIX_LIST_ENTRY *PrefixEntry; + UINT64 Delay; + IP6_DELAY_JOIN_LIST *DelayNode; + + NET_CHECK_SIGNATURE (Interface, IP6_INTERFACE_SIGNATURE); + + IpSb = Interface->Service; + + if (Ip6IsOneOfSetAddress (IpSb, Ip6Addr, NULL, &AddressInfo)) { + ASSERT (AddressInfo != NULL); + // + // Update the lifetime. + // + AddressInfo->ValidLifetime = ValidLifetime; + AddressInfo->PreferredLifetime = PreferredLifetime; + + if (DadCallback != NULL) { + DadCallback (TRUE, Ip6Addr, Context); + } + + return EFI_SUCCESS; + } + + AddressInfo = (IP6_ADDRESS_INFO *) AllocatePool (sizeof (IP6_ADDRESS_INFO)); + if (AddressInfo == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + AddressInfo->Signature = IP6_ADDR_INFO_SIGNATURE; + IP6_COPY_ADDRESS (&AddressInfo->Address, Ip6Addr); + AddressInfo->IsAnycast = IsAnycast; + AddressInfo->PrefixLength = PrefixLength; + AddressInfo->ValidLifetime = ValidLifetime; + AddressInfo->PreferredLifetime = PreferredLifetime; + + if (AddressInfo->PrefixLength == 0) { + // + // Find an appropriate prefix from on-link prefixes and update the prefixlength. + // Longest prefix match is used here. + // + NET_LIST_FOR_EACH (Entry, &IpSb->OnlinkPrefix) { + PrefixEntry = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link); + + if (NetIp6IsNetEqual (&PrefixEntry->Prefix, &AddressInfo->Address, PrefixEntry->PrefixLength)) { + AddressInfo->PrefixLength = PrefixEntry->PrefixLength; + break; + } + } + } + + if (AddressInfo->PrefixLength == 0) { + // + // If the prefix length is still zero, try the autonomous prefixes. + // Longest prefix match is used here. + // + NET_LIST_FOR_EACH (Entry, &IpSb->AutonomousPrefix) { + PrefixEntry = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link); + + if (NetIp6IsNetEqual (&PrefixEntry->Prefix, &AddressInfo->Address, PrefixEntry->PrefixLength)) { + AddressInfo->PrefixLength = PrefixEntry->PrefixLength; + break; + } + } + } + + if (AddressInfo->PrefixLength == 0) { + // + // BUGBUG: Stil fail, use 64 as the default prefix length. + // + AddressInfo->PrefixLength = IP6_LINK_LOCAL_PREFIX_LENGTH; + } + + + // + // Node should delay joining the solicited-node mulitcast address by a random delay + // between 0 and MAX_RTR_SOLICITATION_DELAY (1 second). + // Thus queue the address to be processed in Duplicate Address Detection module + // after the delay time (in milliseconds). + // + Delay = (UINT64) NET_RANDOM (NetRandomInitSeed ()); + Delay = MultU64x32 (Delay, IP6_ONE_SECOND_IN_MS); + Delay = RShiftU64 (Delay, 32); + + DelayNode = (IP6_DELAY_JOIN_LIST *) AllocatePool (sizeof (IP6_DELAY_JOIN_LIST)); + if (DelayNode == NULL) { + FreePool (AddressInfo); + return EFI_OUT_OF_RESOURCES; + } + + DelayNode->DelayTime = (UINT32) (DivU64x32 (Delay, IP6_TIMER_INTERVAL_IN_MS)); + DelayNode->Interface = Interface; + DelayNode->AddressInfo = AddressInfo; + DelayNode->DadCallback = DadCallback; + DelayNode->Context = Context; + + InsertTailList (&Interface->DelayJoinList, &DelayNode->Link); + return EFI_SUCCESS; +} + +/** + Create an IP6_INTERFACE. + + @param[in] IpSb The IP6 service binding instance. + @param[in] LinkLocal If TRUE, the instance is created for link-local address. + Otherwise, it is not for a link-local address. + + @return Point to the created IP6_INTERFACE, otherwise NULL. + +**/ +IP6_INTERFACE * +Ip6CreateInterface ( + IN IP6_SERVICE *IpSb, + IN BOOLEAN LinkLocal + ) +{ + EFI_STATUS Status; + IP6_INTERFACE *Interface; + EFI_IPv6_ADDRESS *Ip6Addr; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + Interface = AllocatePool (sizeof (IP6_INTERFACE)); + if (Interface == NULL) { + return NULL; + } + + Interface->Signature = IP6_INTERFACE_SIGNATURE; + Interface->RefCnt = 1; + + InitializeListHead (&Interface->AddressList); + Interface->AddressCount = 0; + Interface->Configured = FALSE; + + Interface->Service = IpSb; + Interface->Controller = IpSb->Controller; + Interface->Image = IpSb->Image; + + InitializeListHead (&Interface->ArpQues); + InitializeListHead (&Interface->SentFrames); + + Interface->DupAddrDetect = IpSb->Ip6ConfigInstance.DadXmits.DupAddrDetectTransmits; + InitializeListHead (&Interface->DupAddrDetectList); + + InitializeListHead (&Interface->DelayJoinList); + + InitializeListHead (&Interface->IpInstances); + Interface->PromiscRecv = FALSE; + + if (!LinkLocal) { + return Interface; + } + + // + // Get the link local addr + // + Ip6Addr = Ip6CreateLinkLocalAddr (IpSb); + if (Ip6Addr == NULL) { + goto ON_ERROR; + } + + // + // Perform DAD - Duplicate Address Detection. + // + Status = Ip6SetAddress ( + Interface, + Ip6Addr, + FALSE, + IP6_LINK_LOCAL_PREFIX_LENGTH, + (UINT32) IP6_INFINIT_LIFETIME, + (UINT32) IP6_INFINIT_LIFETIME, + NULL, + NULL + ); + + FreePool (Ip6Addr); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return Interface; + +ON_ERROR: + + FreePool (Interface); + return NULL; +} + +/** + Free the interface used by IpInstance. All the IP instance with + the same Ip/prefix pair share the same interface. It is reference + counted. All the frames that haven't been sent will be cancelled. + Because the IpInstance is optional, the caller must remove + IpInstance from the interface's instance list. + + @param[in] Interface The interface used by the IpInstance. + @param[in] IpInstance The IP instance that free the interface. NULL if + the IP driver is releasing the default interface. + +**/ +VOID +Ip6CleanInterface ( + IN IP6_INTERFACE *Interface, + IN IP6_PROTOCOL *IpInstance OPTIONAL + ) +{ + IP6_DAD_ENTRY *Duplicate; + IP6_DELAY_JOIN_LIST *Delay; + + NET_CHECK_SIGNATURE (Interface, IP6_INTERFACE_SIGNATURE); + ASSERT (Interface->RefCnt > 0); + + // + // Remove all the pending transmit token related to this IP instance. + // + Ip6CancelFrames (Interface, EFI_ABORTED, Ip6CancelInstanceFrame, IpInstance); + + if (--Interface->RefCnt > 0) { + return; + } + + // + // Destroy the interface if this is the last IP instance. + // Remove all the system transmitted packets + // from this interface, cancel the receive request if exists. + // + Ip6CancelFrames (Interface, EFI_ABORTED, Ip6CancelInstanceFrame, NULL); + + ASSERT (IsListEmpty (&Interface->IpInstances)); + ASSERT (IsListEmpty (&Interface->ArpQues)); + ASSERT (IsListEmpty (&Interface->SentFrames)); + + while (!IsListEmpty (&Interface->DupAddrDetectList)) { + Duplicate = NET_LIST_HEAD (&Interface->DupAddrDetectList, IP6_DAD_ENTRY, Link); + NetListRemoveHead (&Interface->DupAddrDetectList); + FreePool (Duplicate); + } + + while (!IsListEmpty (&Interface->DelayJoinList)) { + Delay = NET_LIST_HEAD (&Interface->DelayJoinList, IP6_DELAY_JOIN_LIST, Link); + NetListRemoveHead (&Interface->DelayJoinList); + FreePool (Delay); + } + + Ip6RemoveAddr (Interface->Service, &Interface->AddressList, &Interface->AddressCount, NULL, 0); + + RemoveEntryList (&Interface->Link); + FreePool (Interface); +} + +/** + Create and wrap a transmit request into a newly allocated IP6_LINK_TX_TOKEN. + + @param[in] Interface The interface to send out from. + @param[in] IpInstance The IpInstance that transmit the packet. NULL if + the packet is sent by the IP6 driver itself. + @param[in] Packet The packet to transmit + @param[in] CallBack Call back function to execute if transmission + finished. + @param[in] Context Opaque parameter to the callback. + + @return The wrapped token if succeed or NULL. + +**/ +IP6_LINK_TX_TOKEN * +Ip6CreateLinkTxToken ( + IN IP6_INTERFACE *Interface, + IN IP6_PROTOCOL *IpInstance OPTIONAL, + IN NET_BUF *Packet, + IN IP6_FRAME_CALLBACK CallBack, + IN VOID *Context + ) +{ + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken; + EFI_MANAGED_NETWORK_TRANSMIT_DATA *MnpTxData; + IP6_LINK_TX_TOKEN *Token; + EFI_STATUS Status; + UINT32 Count; + + Token = AllocatePool (sizeof (IP6_LINK_TX_TOKEN) + (Packet->BlockOpNum - 1) * sizeof (EFI_MANAGED_NETWORK_FRAGMENT_DATA)); + + if (Token == NULL) { + return NULL; + } + + Token->Signature = IP6_LINK_TX_SIGNATURE; + InitializeListHead (&Token->Link); + + Token->IpInstance = IpInstance; + Token->CallBack = CallBack; + Token->Packet = Packet; + Token->Context = Context; + ZeroMem (&Token->DstMac, sizeof (EFI_MAC_ADDRESS)); + IP6_COPY_LINK_ADDRESS (&Token->SrcMac, &Interface->Service->SnpMode.CurrentAddress); + + MnpToken = &(Token->MnpToken); + MnpToken->Status = EFI_NOT_READY; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Ip6OnFrameSent, + Token, + &MnpToken->Event + ); + + if (EFI_ERROR (Status)) { + FreePool (Token); + return NULL; + } + + MnpTxData = &Token->MnpTxData; + MnpToken->Packet.TxData = MnpTxData; + + MnpTxData->DestinationAddress = &Token->DstMac; + MnpTxData->SourceAddress = &Token->SrcMac; + MnpTxData->ProtocolType = IP6_ETHER_PROTO; + MnpTxData->DataLength = Packet->TotalSize; + MnpTxData->HeaderLength = 0; + + Count = Packet->BlockOpNum; + + NetbufBuildExt (Packet, (NET_FRAGMENT *) MnpTxData->FragmentTable, &Count); + MnpTxData->FragmentCount = (UINT16)Count; + + return Token; +} + +/** + Free the link layer transmit token. It will close the event, + then free the memory used. + + @param[in] Token Token to free. + +**/ +VOID +Ip6FreeLinkTxToken ( + IN IP6_LINK_TX_TOKEN *Token + ) +{ + NET_CHECK_SIGNATURE (Token, IP6_LINK_TX_SIGNATURE); + + gBS->CloseEvent (Token->MnpToken.Event); + FreePool (Token); +} + +/** + Callback function when the received packet is freed. + Check Ip6OnFrameReceived for information. + + @param[in] Context Points to EFI_MANAGED_NETWORK_RECEIVE_DATA. + +**/ +VOID +EFIAPI +Ip6RecycleFrame ( + IN VOID *Context + ) +{ + EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData; + + RxData = (EFI_MANAGED_NETWORK_RECEIVE_DATA *) Context; + + gBS->SignalEvent (RxData->RecycleEvent); +} + +/** + Received a frame from MNP. Wrap it in net buffer then deliver + it to IP's input function. The ownship of the packet also + is transferred to IP. When Ip is finished with this packet, it + will call NetbufFree to release the packet, NetbufFree will + again call the Ip6RecycleFrame to signal MNP's event and free + the token used. + + @param[in] Context Context for the callback. + +**/ +VOID +EFIAPI +Ip6OnFrameReceivedDpc ( + IN VOID *Context + ) +{ + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken; + EFI_MANAGED_NETWORK_RECEIVE_DATA *MnpRxData; + IP6_LINK_RX_TOKEN *Token; + NET_FRAGMENT Netfrag; + NET_BUF *Packet; + UINT32 Flag; + IP6_SERVICE *IpSb; + + Token = (IP6_LINK_RX_TOKEN *) Context; + NET_CHECK_SIGNATURE (Token, IP6_LINK_RX_SIGNATURE); + + // + // First clear the interface's receive request in case the + // caller wants to call Ip6ReceiveFrame in the callback. + // + IpSb = (IP6_SERVICE *) Token->Context; + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + + MnpToken = &Token->MnpToken; + MnpRxData = MnpToken->Packet.RxData; + + if (EFI_ERROR (MnpToken->Status) || (MnpRxData == NULL)) { + Token->CallBack (NULL, MnpToken->Status, 0, Token->Context); + return ; + } + + // + // Wrap the frame in a net buffer then deliever it to IP input. + // IP will reassemble the packet, and deliver it to upper layer + // + Netfrag.Len = MnpRxData->DataLength; + Netfrag.Bulk = MnpRxData->PacketData; + + Packet = NetbufFromExt (&Netfrag, 1, IP6_MAX_HEADLEN, 0, Ip6RecycleFrame, Token->MnpToken.Packet.RxData); + + if (Packet == NULL) { + gBS->SignalEvent (MnpRxData->RecycleEvent); + + Token->CallBack (NULL, EFI_OUT_OF_RESOURCES, 0, Token->Context); + + return ; + } + + Flag = (MnpRxData->BroadcastFlag ? IP6_LINK_BROADCAST : 0); + Flag |= (MnpRxData->MulticastFlag ? IP6_LINK_MULTICAST : 0); + Flag |= (MnpRxData->PromiscuousFlag ? IP6_LINK_PROMISC : 0); + + Token->CallBack (Packet, EFI_SUCCESS, Flag, Token->Context); +} + +/** + Request Ip6OnFrameReceivedDpc as a DPC at TPL_CALLBACK. + + @param Event The receive event delivered to MNP for receive. + @param Context Context for the callback. + +**/ +VOID +EFIAPI +Ip6OnFrameReceived ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request Ip6OnFrameReceivedDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, Ip6OnFrameReceivedDpc, Context); +} + +/** + Request to receive the packet from the interface. + + @param[in] CallBack Function to call when receive finished. + @param[in] IpSb Points to IP6 service binding instance. + + @retval EFI_ALREADY_STARTED There is already a pending receive request. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to receive. + @retval EFI_SUCCESS The recieve request has been started. + +**/ +EFI_STATUS +Ip6ReceiveFrame ( + IN IP6_FRAME_CALLBACK CallBack, + IN IP6_SERVICE *IpSb + ) +{ + EFI_STATUS Status; + IP6_LINK_RX_TOKEN *Token; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + Token = &IpSb->RecvRequest; + Token->CallBack = CallBack; + Token->Context = (VOID *) IpSb; + + Status = IpSb->Mnp->Receive (IpSb->Mnp, &Token->MnpToken); + if (EFI_ERROR (Status)) { + return Status; + } + + return EFI_SUCCESS; +} + +/** + Callback funtion when frame transmission is finished. It will + call the frame owner's callback function to tell it the result. + + @param[in] Context Context which points to the token. + +**/ +VOID +EFIAPI +Ip6OnFrameSentDpc ( + IN VOID *Context + ) +{ + IP6_LINK_TX_TOKEN *Token; + + Token = (IP6_LINK_TX_TOKEN *) Context; + NET_CHECK_SIGNATURE (Token, IP6_LINK_TX_SIGNATURE); + + RemoveEntryList (&Token->Link); + + Token->CallBack ( + Token->Packet, + Token->MnpToken.Status, + 0, + Token->Context + ); + + Ip6FreeLinkTxToken (Token); +} + +/** + Request Ip6OnFrameSentDpc as a DPC at TPL_CALLBACK. + + @param[in] Event The transmit token's event. + @param[in] Context Context which points to the token. + +**/ +VOID +EFIAPI +Ip6OnFrameSent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request Ip6OnFrameSentDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, Ip6OnFrameSentDpc, Context); +} + +/** + Send a frame from the interface. If the next hop is a multicast address, + it is transmitted immediately. If the next hop is a unicast, + and the NextHop's MAC is not known, it will perform address resolution. + If an error occurred, the CallBack won't be called. So, the caller + must test the return value, and take action when there is an error. + + @param[in] Interface The interface to send the frame from + @param[in] IpInstance The IP child that request the transmission. + NULL if it is the IP6 driver itself. + @param[in] Packet The packet to transmit. + @param[in] NextHop The immediate destination to transmit the packet to. + @param[in] CallBack Function to call back when transmit finished. + @param[in] Context Opaque parameter to the callback. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to send the frame. + @retval EFI_NO_MAPPING Can't resolve the MAC for the nexthop. + @retval EFI_SUCCESS The packet successfully transmitted. + +**/ +EFI_STATUS +Ip6SendFrame ( + IN IP6_INTERFACE *Interface, + IN IP6_PROTOCOL *IpInstance OPTIONAL, + IN NET_BUF *Packet, + IN EFI_IPv6_ADDRESS *NextHop, + IN IP6_FRAME_CALLBACK CallBack, + IN VOID *Context + ) +{ + IP6_SERVICE *IpSb; + IP6_LINK_TX_TOKEN *Token; + EFI_STATUS Status; + IP6_NEIGHBOR_ENTRY *NeighborCache; + LIST_ENTRY *Entry; + IP6_NEIGHBOR_ENTRY *ArpQue; + + IpSb = Interface->Service; + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + // + // Only when link local address is performing DAD, the interface could be used in unconfigured. + // + if (IpSb->LinkLocalOk) { + ASSERT (Interface->Configured); + } + + Token = Ip6CreateLinkTxToken (Interface, IpInstance, Packet, CallBack, Context); + + if (Token == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if (IP6_IS_MULTICAST (NextHop)) { + Status = Ip6GetMulticastMac (IpSb->Mnp, NextHop, &Token->DstMac); + if (EFI_ERROR (Status)) { + goto Error; + } + + goto SendNow; + } + + // + // If send to itself, directly send out + // + if (EFI_IP6_EQUAL (&Packet->Ip.Ip6->DestinationAddress, &Packet->Ip.Ip6->SourceAddress)) { + IP6_COPY_LINK_ADDRESS (&Token->DstMac, &IpSb->SnpMode.CurrentAddress); + goto SendNow; + } + + // + // If unicast, check the neighbor state. + // + + NeighborCache = Ip6FindNeighborEntry (IpSb, NextHop); + ASSERT (NeighborCache != NULL); + + if (NeighborCache->Interface == NULL) { + NeighborCache->Interface = Interface; + } + + switch (NeighborCache->State) { + case EfiNeighborStale: + NeighborCache->State = EfiNeighborDelay; + NeighborCache->Ticks = (UINT32) IP6_GET_TICKS (IP6_DELAY_FIRST_PROBE_TIME); + // + // Fall through + // + case EfiNeighborReachable: + case EfiNeighborDelay: + case EfiNeighborProbe: + IP6_COPY_LINK_ADDRESS (&Token->DstMac, &NeighborCache->LinkAddress); + goto SendNow; + break; + + default: + break; + } + + // + // Have to do asynchronous ARP resolution. First check whether there is + // already a pending request. + // + NET_LIST_FOR_EACH (Entry, &Interface->ArpQues) { + ArpQue = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, ArpList); + if (ArpQue == NeighborCache) { + InsertTailList (&NeighborCache->Frames, &Token->Link); + NeighborCache->ArpFree = TRUE; + return EFI_SUCCESS; + } + } + + // + // First frame requires ARP. + // + InsertTailList (&NeighborCache->Frames, &Token->Link); + InsertTailList (&Interface->ArpQues, &NeighborCache->ArpList); + + NeighborCache->ArpFree = TRUE; + + return EFI_SUCCESS; + +SendNow: + // + // Insert the tx token into the SentFrames list before calling Mnp->Transmit. + // Remove it if the returned status is not EFI_SUCCESS. + // + InsertTailList (&Interface->SentFrames, &Token->Link); + Status = IpSb->Mnp->Transmit (IpSb->Mnp, &Token->MnpToken); + if (EFI_ERROR (Status)) { + RemoveEntryList (&Token->Link); + goto Error; + } + + return EFI_SUCCESS; + +Error: + Ip6FreeLinkTxToken (Token); + return Status; +} + +/** + The heartbeat timer of IP6 service instance. It times out + all of its IP6 children's received-but-not-delivered and + transmitted-but-not-recycle packets. + + @param[in] Event The IP6 service instance's heartbeat timer. + @param[in] Context The IP6 service instance. + +**/ +VOID +EFIAPI +Ip6TimerTicking ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + IP6_SERVICE *IpSb; + + IpSb = (IP6_SERVICE *) Context; + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + Ip6PacketTimerTicking (IpSb); + Ip6NdTimerTicking (IpSb); + Ip6MldTimerTicking (IpSb); +} diff --git a/NetworkPkg/Ip6Dxe/Ip6If.h b/NetworkPkg/Ip6Dxe/Ip6If.h new file mode 100644 index 000000000..a8af4322e --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6If.h @@ -0,0 +1,261 @@ +/** @file + Definition for IP6 pesudo interface structure. + + Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP6_IF_H__ +#define __EFI_IP6_IF_H__ + +#define IP6_LINK_RX_SIGNATURE SIGNATURE_32 ('I', 'P', '6', 'R') +#define IP6_LINK_TX_SIGNATURE SIGNATURE_32 ('I', 'P', '6', 'T') +#define IP6_INTERFACE_SIGNATURE SIGNATURE_32 ('I', 'P', '6', 'I') +#define IP6_ADDR_INFO_SIGNATURE SIGNATURE_32 ('I', 'P', 'A', 'I') + +// +// This prototype is used by both receive and transmission. +// When receiving Netbuf is allocated by IP6_INTERFACE, and +// released by IP6. Flag shows whether the frame is received +// as unicast/multicast/anycast... +// +// When transmitting, the Netbuf is from IP6, and provided +// to the callback as a reference. Flag isn't used. +// +// IpInstance can be NULL which means that it is the IP6 driver +// itself sending the packets. IP6 driver may send packets that +// don't belong to any instance, such as ICMP errors, ICMP +// informational packets. IpInstance is used as a tag in +// this module. +// +typedef +VOID +(*IP6_FRAME_CALLBACK) ( + NET_BUF *Packet, + EFI_STATUS IoStatus, + UINT32 LinkFlag, + VOID *Context + ); + +// +// Each receive request is wrapped in an IP6_LINK_RX_TOKEN. +// Upon completion, the Callback will be called. Only one +// receive request is send to MNP. IpInstance is always NULL. +// Reference MNP's spec for information. +// +typedef struct { + UINT32 Signature; + IP6_FRAME_CALLBACK CallBack; + VOID *Context; + EFI_MANAGED_NETWORK_COMPLETION_TOKEN MnpToken; +} IP6_LINK_RX_TOKEN; + +// +// Each transmit request is wrapped in an IP6_LINK_TX_TOKEN. +// Upon completion, the Callback will be called. +// +typedef struct { + UINT32 Signature; + LIST_ENTRY Link; + + IP6_PROTOCOL *IpInstance; + IP6_FRAME_CALLBACK CallBack; + NET_BUF *Packet; + VOID *Context; + + EFI_MAC_ADDRESS DstMac; + EFI_MAC_ADDRESS SrcMac; + + EFI_MANAGED_NETWORK_COMPLETION_TOKEN MnpToken; + EFI_MANAGED_NETWORK_TRANSMIT_DATA MnpTxData; +} IP6_LINK_TX_TOKEN; + +struct _IP6_ADDRESS_INFO { + UINT32 Signature; + LIST_ENTRY Link; + EFI_IPv6_ADDRESS Address; + BOOLEAN IsAnycast; + UINT8 PrefixLength; + UINT32 ValidLifetime; + UINT32 PreferredLifetime; +}; + +// +// Callback to select which frame to cancel. Caller can cancel a +// single frame, or all the frame from an IP instance. +// +typedef +BOOLEAN +(*IP6_FRAME_TO_CANCEL) ( + IP6_LINK_TX_TOKEN *Frame, + VOID *Context + ); + +struct _IP6_INTERFACE { + UINT32 Signature; + LIST_ENTRY Link; + INTN RefCnt; + + // + // IP address and prefix length of the interface. The fileds + // are invalid if (Configured == FALSE) + // + LIST_ENTRY AddressList; + UINT32 AddressCount; + BOOLEAN Configured; + + IP6_SERVICE *Service; + + EFI_HANDLE Controller; + EFI_HANDLE Image; + + + // + // Queues to keep the frames sent and waiting ARP request. + // + LIST_ENTRY ArpQues; + LIST_ENTRY SentFrames; + + + // + // The interface's configuration variables + // + UINT32 DupAddrDetect; + LIST_ENTRY DupAddrDetectList; + LIST_ENTRY DelayJoinList; + + // + // All the IP instances that have the same IP/SubnetMask are linked + // together through IpInstances. If any of the instance enables + // promiscuous receive, PromiscRecv is true. + // + LIST_ENTRY IpInstances; + BOOLEAN PromiscRecv; +}; + +/** + Create an IP6_INTERFACE. + + @param[in] IpSb The IP6 service binding instance. + @param[in] LinkLocal If TRUE, the instance is created for link-local address. + Otherwise, it is not for a link-local address. + + @return Point to the created IP6_INTERFACE, otherwise NULL. + +**/ +IP6_INTERFACE * +Ip6CreateInterface ( + IN IP6_SERVICE *IpSb, + IN BOOLEAN LinkLocal + ); + +/** + Free the interface used by IpInstance. All the IP instance with + the same Ip/prefix pair share the same interface. It is reference + counted. All the frames that haven't been sent will be cancelled. + Because the IpInstance is optional, the caller must remove + IpInstance from the interface's instance list. + + @param[in] Interface The interface used by the IpInstance. + @param[in] IpInstance The IP instance that free the interface. NULL if + the IP driver is releasing the default interface. + +**/ +VOID +Ip6CleanInterface ( + IN IP6_INTERFACE *Interface, + IN IP6_PROTOCOL *IpInstance OPTIONAL + ); + +/** + Free the link layer transmit token. It will close the event + then free the memory used. + + @param[in] Token Token to free. + +**/ +VOID +Ip6FreeLinkTxToken ( + IN IP6_LINK_TX_TOKEN *Token + ); + +/** + Request Ip6OnFrameReceivedDpc as a DPC at TPL_CALLBACK + + @param Event The receive event delivered to MNP for receive. + @param Context Context for the callback. + +**/ +VOID +EFIAPI +Ip6OnFrameReceived ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Request to receive the packet from the interface. + + @param[in] CallBack Function to call when the receive finished. + @param[in] IpSb Points to the IP6 service binding instance. + + @retval EFI_ALREADY_STARTED There is already a pending receive request. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to receive. + @retval EFI_SUCCESS The recieve request has been started. + +**/ +EFI_STATUS +Ip6ReceiveFrame ( + IN IP6_FRAME_CALLBACK CallBack, + IN IP6_SERVICE *IpSb + ); + +/** + Send a frame from the interface. If the next hop is multicast address, + it is transmitted immediately. If the next hop is a unicast, + and the NextHop's MAC is not known, it will perform address resolution. + If some error happened, the CallBack won't be called. So, the caller + must test the return value, and take action when there is an error. + + @param[in] Interface The interface to send the frame from + @param[in] IpInstance The IP child that request the transmission. + NULL if it is the IP6 driver itself. + @param[in] Packet The packet to transmit. + @param[in] NextHop The immediate destination to transmit the packet to. + @param[in] CallBack Function to call back when transmit finished. + @param[in] Context Opaque parameter to the call back. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to send the frame. + @retval EFI_NO_MAPPING Can't resolve the MAC for the nexthop. + @retval EFI_SUCCESS The packet successfully transmitted. + +**/ +EFI_STATUS +Ip6SendFrame ( + IN IP6_INTERFACE *Interface, + IN IP6_PROTOCOL *IpInstance OPTIONAL, + IN NET_BUF *Packet, + IN EFI_IPv6_ADDRESS *NextHop, + IN IP6_FRAME_CALLBACK CallBack, + IN VOID *Context + ); + +/** + The heartbeat timer of IP6 service instance. It times out + all of its IP6 children's received-but-not-delivered and + transmitted-but-not-recycle packets. + + @param[in] Event The IP6 service instance's heart beat timer. + @param[in] Context The IP6 service instance. + +**/ +VOID +EFIAPI +Ip6TimerTicking ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +#endif diff --git a/NetworkPkg/Ip6Dxe/Ip6Impl.c b/NetworkPkg/Ip6Dxe/Ip6Impl.c new file mode 100644 index 000000000..80862bfe9 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Impl.c @@ -0,0 +1,1841 @@ +/** @file + Implementation of EFI_IP6_PROTOCOL protocol interfaces. + + (C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +EFI_IPSEC2_PROTOCOL *mIpSec = NULL; + +EFI_IP6_PROTOCOL mEfiIp6ProtocolTemplete = { + EfiIp6GetModeData, + EfiIp6Configure, + EfiIp6Groups, + EfiIp6Routes, + EfiIp6Neighbors, + EfiIp6Transmit, + EfiIp6Receive, + EfiIp6Cancel, + EfiIp6Poll +}; + +/** + Gets the current operational settings for this instance of the EFI IPv6 Protocol driver. + + The GetModeData() function returns the current operational mode data for this driver instance. + The data fields in EFI_IP6_MODE_DATA are read only. This function is used optionally to + retrieve the operational mode data of underlying networks or drivers. + + @param[in] This Pointer to the EFI_IP6_PROTOCOL instance. + @param[out] Ip6ModeData Pointer to the EFI IPv6 Protocol mode data structure. + @param[out] MnpConfigData Pointer to the managed network configuration data structure. + @param[out] SnpModeData Pointer to the simple network mode data structure. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated. + +**/ +EFI_STATUS +EFIAPI +EfiIp6GetModeData ( + IN EFI_IP6_PROTOCOL *This, + OUT EFI_IP6_MODE_DATA *Ip6ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ) +{ + IP6_PROTOCOL *IpInstance; + IP6_SERVICE *IpSb; + IP6_INTERFACE *IpIf; + EFI_IP6_CONFIG_DATA *Config; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This); + IpSb = IpInstance->Service; + IpIf = IpInstance->Interface; + + if (IpSb->LinkLocalDadFail) { + return EFI_INVALID_PARAMETER; + } + + if (Ip6ModeData != NULL) { + // + // IsStarted is "whether the EfiIp6Configure has been called". + // IsConfigured is "whether the station address has been configured" + // + Ip6ModeData->IsStarted = (BOOLEAN) (IpInstance->State == IP6_STATE_CONFIGED); + Ip6ModeData->MaxPacketSize = IpSb->MaxPacketSize; + CopyMem (&Ip6ModeData->ConfigData, &IpInstance->ConfigData, sizeof (EFI_IP6_CONFIG_DATA)); + Ip6ModeData->IsConfigured = FALSE; + + Ip6ModeData->AddressCount = 0; + Ip6ModeData->AddressList = NULL; + + Ip6ModeData->GroupCount = IpInstance->GroupCount; + Ip6ModeData->GroupTable = NULL; + + Ip6ModeData->RouteCount = 0; + Ip6ModeData->RouteTable = NULL; + + Ip6ModeData->NeighborCount = 0; + Ip6ModeData->NeighborCache = NULL; + + Ip6ModeData->PrefixCount = 0; + Ip6ModeData->PrefixTable = NULL; + + Ip6ModeData->IcmpTypeCount = 23; + Ip6ModeData->IcmpTypeList = AllocateCopyPool ( + Ip6ModeData->IcmpTypeCount * sizeof (EFI_IP6_ICMP_TYPE), + mIp6SupportedIcmp + ); + if (Ip6ModeData->IcmpTypeList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + // + // Return the currently configured IPv6 addresses and corresponding prefix lengths. + // + Status = Ip6BuildEfiAddressList ( + IpSb, + &Ip6ModeData->AddressCount, + &Ip6ModeData->AddressList + ); + if (EFI_ERROR (Status)) { + goto Error; + } + + // + // Return the current station address for this IP child. + // If UseAnyStationAddress is set to TRUE, IP6 driver will + // select a source address from its address list. Otherwise use the + // StationAddress in config data. + // + if (Ip6ModeData->IsStarted) { + Config = &Ip6ModeData->ConfigData; + + if (IpIf->Configured || NetIp6IsUnspecifiedAddr (&Config->DestinationAddress)) { + Ip6ModeData->IsConfigured = TRUE; + } else { + Ip6ModeData->IsConfigured = FALSE; + } + + // + // Build a EFI route table for user from the internal route table. + // + Status = Ip6BuildEfiRouteTable ( + IpSb->RouteTable, + &Ip6ModeData->RouteCount, + &Ip6ModeData->RouteTable + ); + + if (EFI_ERROR (Status)) { + goto Error; + } + } + + if (Ip6ModeData->IsConfigured) { + // + // Return the joined multicast group addresses. + // + if (IpInstance->GroupCount != 0) { + Ip6ModeData->GroupTable = AllocateCopyPool ( + IpInstance->GroupCount * sizeof (EFI_IPv6_ADDRESS), + IpInstance->GroupList + ); + if (Ip6ModeData->GroupTable == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + } + // + // Return the neighbor cache entries + // + Status = Ip6BuildEfiNeighborCache ( + IpInstance, + &Ip6ModeData->NeighborCount, + &Ip6ModeData->NeighborCache + ); + if (EFI_ERROR (Status)) { + goto Error; + } + + // + // Return the prefix table entries + // + Status = Ip6BuildPrefixTable ( + IpInstance, + &Ip6ModeData->PrefixCount, + &Ip6ModeData->PrefixTable + ); + if (EFI_ERROR (Status)) { + goto Error; + } + + } + } + + // + // Get fresh mode data from MNP, since underlying media status may change + // + Status = IpSb->Mnp->GetModeData (IpSb->Mnp, MnpConfigData, SnpModeData); + + goto Exit; + +Error: + if (Ip6ModeData != NULL) { + if (Ip6ModeData->AddressList != NULL) { + FreePool (Ip6ModeData->AddressList); + } + + if (Ip6ModeData->GroupTable != NULL) { + FreePool (Ip6ModeData->GroupTable); + } + + if (Ip6ModeData->RouteTable != NULL) { + FreePool (Ip6ModeData->RouteTable); + } + + if (Ip6ModeData->NeighborCache != NULL) { + FreePool (Ip6ModeData->NeighborCache); + } + + if (Ip6ModeData->PrefixTable != NULL) { + FreePool (Ip6ModeData->PrefixTable); + } + + if (Ip6ModeData->IcmpTypeList != NULL) { + FreePool (Ip6ModeData->IcmpTypeList); + } + } + +Exit: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Validate that Ipv6 address is OK to be used as station address or next hop address/ neighbor. + + @param[in] IpSb The IP6 service instance. + @param[in] Ip The IPv6 address to validate. + @param[in] Flag If TRUE, validate if the address is OK to be used + as station address. If FALSE, validate if the + address is OK to be used as the next hop address/ + neighbor. + + @retval TRUE The Ip address is valid and could be used. + @retval FALSE Invalid Ip address. + +**/ +BOOLEAN +Ip6IsValidAddress ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Ip, + IN BOOLEAN Flag + ) +{ + if (!NetIp6IsUnspecifiedAddr (Ip)) { + if (!NetIp6IsValidUnicast(Ip)) { + return FALSE; + } + if (Ip6IsOneOfSetAddress (IpSb, Ip, NULL, NULL)) { + return Flag; + } + } else { + return Flag; + } + + return (BOOLEAN) !Flag; +} + +/** + Validate whether the value of protocol is illegal or not. Protocol is the 'Next Header' field + in the last IPv6 extension header, or basic IPv6 header is there's no extension header. + + @param[in] Protocol Default value of 'Next Header' + + @retval TRUE The protocol is illegal. + @retval FALSE The protocol is legal. + +**/ +BOOLEAN +Ip6IsIllegalProtocol ( + IN UINT8 Protocol + ) +{ + if (Protocol == IP6_HOP_BY_HOP || Protocol == EFI_IP_PROTO_ICMP || Protocol == IP4_PROTO_IGMP) { + return TRUE; + } + + if (Protocol == 41 || Protocol == 43 || Protocol == 44 || Protocol == 59 || Protocol == 60 || Protocol == 124) { + return TRUE; + } + + return FALSE; +} + +/** + Intiialize the IP6_PROTOCOL structure to the unconfigured states. + + @param[in] IpSb The IP6 service instance. + @param[in, out] IpInstance The IP6 child instance. + +**/ +VOID +Ip6InitProtocol ( + IN IP6_SERVICE *IpSb, + IN OUT IP6_PROTOCOL *IpInstance + ) +{ + ASSERT ((IpSb != NULL) && (IpInstance != NULL)); + + ZeroMem (IpInstance, sizeof (IP6_PROTOCOL)); + + IpInstance->Signature = IP6_PROTOCOL_SIGNATURE; + IpInstance->State = IP6_STATE_UNCONFIGED; + IpInstance->Service = IpSb; + IpInstance->GroupList = NULL; + CopyMem (&IpInstance->Ip6Proto, &mEfiIp6ProtocolTemplete, sizeof (EFI_IP6_PROTOCOL)); + + NetMapInit (&IpInstance->RxTokens); + NetMapInit (&IpInstance->TxTokens); + InitializeListHead (&IpInstance->Received); + InitializeListHead (&IpInstance->Delivered); + + EfiInitializeLock (&IpInstance->RecycleLock, TPL_NOTIFY); +} + +/** + Configure the IP6 child. If the child is already configured, + change the configuration parameter. Otherwise, configure it + for the first time. The caller should validate the configuration + before deliver them to it. It also don't do configure NULL. + + @param[in, out] IpInstance The IP6 child to configure. + @param[in] Config The configure data. + + @retval EFI_SUCCESS The IP6 child is successfully configured. + @retval EFI_DEVICE_ERROR Failed to free the pending transive or to + configure underlying MNP, or other errors. + @retval EFI_NO_MAPPING The IP6 child is configured to use the default + address, but the default address hasn't been + configured. The IP6 child doesn't need to be + reconfigured when the default address is configured. + @retval EFI_OUT_OF_RESOURCES No more memory space is available. + @retval other Other error occurs. + +**/ +EFI_STATUS +Ip6ConfigProtocol ( + IN OUT IP6_PROTOCOL *IpInstance, + IN EFI_IP6_CONFIG_DATA *Config + ) +{ + IP6_SERVICE *IpSb; + IP6_INTERFACE *IpIf; + EFI_STATUS Status; + EFI_IP6_CONFIG_DATA *Current; + IP6_ADDRESS_INFO *AddressInfo; + BOOLEAN StationZero; + BOOLEAN DestZero; + EFI_IPv6_ADDRESS Source; + BOOLEAN AddrOk; + + IpSb = IpInstance->Service; + Current = &IpInstance->ConfigData; + + // + // User is changing packet filters. It must be stopped + // before the station address can be changed. + // + if (IpInstance->State == IP6_STATE_CONFIGED) { + // + // Cancel all the pending transmit/receive from upper layer + // + Status = Ip6Cancel (IpInstance, NULL); + + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + CopyMem (Current, Config, sizeof (EFI_IP6_CONFIG_DATA)); + return EFI_SUCCESS; + } + + // + // Set up the interface. + // + StationZero = NetIp6IsUnspecifiedAddr (&Config->StationAddress); + DestZero = NetIp6IsUnspecifiedAddr (&Config->DestinationAddress); + + if (StationZero && DestZero) { + // + // StationAddress is still zero. + // + + NET_GET_REF (IpSb->DefaultInterface); + IpInstance->Interface = IpSb->DefaultInterface; + InsertTailList (&IpSb->DefaultInterface->IpInstances, &IpInstance->AddrLink); + + CopyMem (Current, Config, sizeof (EFI_IP6_CONFIG_DATA)); + IpInstance->State = IP6_STATE_CONFIGED; + + return EFI_SUCCESS; + } + + if (StationZero && !DestZero) { + Status = Ip6SelectSourceAddress (IpSb, &Config->DestinationAddress, &Source); + if (EFI_ERROR (Status)) { + return Status; + } + } else { + IP6_COPY_ADDRESS (&Source, &Config->StationAddress); + } + + AddrOk = Ip6IsOneOfSetAddress (IpSb, &Source, &IpIf, &AddressInfo); + if (AddrOk) { + if (AddressInfo != NULL) { + IpInstance->PrefixLength = AddressInfo->PrefixLength; + } else { + IpInstance->PrefixLength = IP6_LINK_LOCAL_PREFIX_LENGTH; + } + } else { + // + // The specified source address is not one of the addresses IPv6 maintains. + // + return EFI_INVALID_PARAMETER; + } + + + NET_GET_REF (IpIf); + IpInstance->Interface = IpIf; + InsertTailList (&IpIf->IpInstances, &IpInstance->AddrLink); + + CopyMem (Current, Config, sizeof (EFI_IP6_CONFIG_DATA)); + IP6_COPY_ADDRESS (&Current->StationAddress, &Source); + IpInstance->State = IP6_STATE_CONFIGED; + + return EFI_SUCCESS; +} + +/** + Clean up the IP6 child, and release all the resources used by it. + + @param[in, out] IpInstance The IP6 child to clean up. + + @retval EFI_SUCCESS The IP6 child is cleaned up. + @retval EFI_DEVICE_ERROR Some resources failed to be released. + +**/ +EFI_STATUS +Ip6CleanProtocol ( + IN OUT IP6_PROTOCOL *IpInstance + ) +{ + if (EFI_ERROR (Ip6Cancel (IpInstance, NULL))) { + return EFI_DEVICE_ERROR; + } + + if (EFI_ERROR (Ip6Groups (IpInstance, FALSE, NULL))) { + return EFI_DEVICE_ERROR; + } + + // + // Some packets haven't been recycled. It is because either the + // user forgets to recycle the packets, or because the callback + // hasn't been called. Just leave it alone. + // + if (!IsListEmpty (&IpInstance->Delivered)) { + ; + } + + if (IpInstance->Interface != NULL) { + RemoveEntryList (&IpInstance->AddrLink); + Ip6CleanInterface (IpInstance->Interface, IpInstance); + IpInstance->Interface = NULL; + } + + if (IpInstance->GroupList != NULL) { + FreePool (IpInstance->GroupList); + IpInstance->GroupList = NULL; + IpInstance->GroupCount = 0; + } + + NetMapClean (&IpInstance->TxTokens); + + NetMapClean (&IpInstance->RxTokens); + + return EFI_SUCCESS; +} + +/** + Configure the MNP parameter used by IP. The IP driver uses one MNP + child to transmit/receive frames. By default, it configures MNP + to receive unicast/multicast/broadcast. Also, it will enable/disable + the promiscuous receive according to whether there is IP child + enable that or not. If Force is FALSE, it will iterate through + all the IP children to check whether the promiscuous receive + setting has been changed. If it hasn't been changed, it won't + reconfigure the MNP. If Force is TRUE, the MNP is configured + whether that is changed or not. + + @param[in] IpSb The IP6 service instance that is to be changed. + @param[in] Force Force the configuration or not. + + @retval EFI_SUCCESS The MNP successfully configured/reconfigured. + @retval Others Configuration failed. + +**/ +EFI_STATUS +Ip6ServiceConfigMnp ( + IN IP6_SERVICE *IpSb, + IN BOOLEAN Force + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *ProtoEntry; + IP6_INTERFACE *IpIf; + IP6_PROTOCOL *IpInstance; + BOOLEAN Reconfig; + BOOLEAN PromiscReceive; + EFI_STATUS Status; + + Reconfig = FALSE; + PromiscReceive = FALSE; + + if (!Force) { + // + // Iterate through the IP children to check whether promiscuous + // receive setting has been changed. Update the interface's receive + // filter also. + // + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + + IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link); + IpIf->PromiscRecv = FALSE; + + NET_LIST_FOR_EACH (ProtoEntry, &IpIf->IpInstances) { + IpInstance = NET_LIST_USER_STRUCT (ProtoEntry, IP6_PROTOCOL, AddrLink); + + if (IpInstance->ConfigData.AcceptPromiscuous) { + IpIf->PromiscRecv = TRUE; + PromiscReceive = TRUE; + } + } + } + + // + // If promiscuous receive isn't changed, it isn't necessary to reconfigure. + // + if (PromiscReceive == IpSb->MnpConfigData.EnablePromiscuousReceive) { + return EFI_SUCCESS; + } + + Reconfig = TRUE; + IpSb->MnpConfigData.EnablePromiscuousReceive = PromiscReceive; + } + + Status = IpSb->Mnp->Configure (IpSb->Mnp, &IpSb->MnpConfigData); + + // + // recover the original configuration if failed to set the configure. + // + if (EFI_ERROR (Status) && Reconfig) { + IpSb->MnpConfigData.EnablePromiscuousReceive = (BOOLEAN) !PromiscReceive; + } + + return Status; +} + +/** + Assigns an IPv6 address and subnet mask to this EFI IPv6 Protocol driver instance. + + The Configure() function is used to set, change, or reset the operational parameters and filter + settings for this EFI IPv6 Protocol instance. Until these parameters have been set, no network traffic + can be sent or received by this instance. Once the parameters have been reset (by calling this + function with Ip6ConfigData set to NULL), no more traffic can be sent or received until these + parameters have been set again. Each EFI IPv6 Protocol instance can be started and stopped + independently of each other by enabling or disabling their receive filter settings with the + Configure() function. + + If Ip6ConfigData.StationAddress is a valid non-zero IPv6 unicast address, it is required + to be one of the currently configured IPv6 addresses listed in the EFI IPv6 drivers, or else + EFI_INVALID_PARAMETER will be returned. If Ip6ConfigData.StationAddress is + unspecified, the IPv6 driver will bind a source address according to the source address selection + algorithm. Clients could frequently call GetModeData() to check get currently configured IPv6 + address list in the EFI IPv6 driver. If both Ip6ConfigData.StationAddress and + Ip6ConfigData.Destination are unspecified, when transmitting the packet afterwards, the + source address filled in each outgoing IPv6 packet is decided based on the destination of this packet. + + If operational parameters are reset or changed, any pending transmit and receive requests will be + cancelled. Their completion token status will be set to EFI_ABORTED and their events will be + signaled. + + @param[in] This Pointer to the EFI_IP6_PROTOCOL instance. + @param[in] Ip6ConfigData Pointer to the EFI IPv6 Protocol configuration data structure. + If NULL, reset the configuration data. + + @retval EFI_SUCCESS The driver instance was successfully opened. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Ip6ConfigData.StationAddress is neither zero nor + a unicast IPv6 address. + - Ip6ConfigData.StationAddress is neither zero nor + one of the configured IP addresses in the EFI IPv6 driver. + - Ip6ConfigData.DefaultProtocol is illegal. + @retval EFI_OUT_OF_RESOURCES The EFI IPv6 Protocol driver instance data could not be allocated. + @retval EFI_NO_MAPPING The IPv6 driver was responsible for choosing a source address for + this instance, but no source address was available for use. + @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the IPv6 + address or prefix length can be changed. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv6 + Protocol driver instance was not opened. + @retval EFI_UNSUPPORTED Default protocol specified through + Ip6ConfigData.DefaulProtocol isn't supported. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Configure ( + IN EFI_IP6_PROTOCOL *This, + IN EFI_IP6_CONFIG_DATA *Ip6ConfigData OPTIONAL + ) +{ + IP6_PROTOCOL *IpInstance; + EFI_IP6_CONFIG_DATA *Current; + EFI_TPL OldTpl; + EFI_STATUS Status; + IP6_SERVICE *IpSb; + + // + // First, validate the parameters + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This); + IpSb = IpInstance->Service; + + if (IpSb->LinkLocalDadFail && Ip6ConfigData != NULL) { + return EFI_DEVICE_ERROR; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Status = EFI_INVALID_PARAMETER; + + // + // Validate the configuration first. + // + if (Ip6ConfigData != NULL) { + // + // Check whether the station address is valid. + // + if (!Ip6IsValidAddress (IpSb, &Ip6ConfigData->StationAddress, TRUE)) { + goto Exit; + } + // + // Check whether the default protocol is valid. + // + if (Ip6IsIllegalProtocol (Ip6ConfigData->DefaultProtocol)) { + goto Exit; + } + + // + // User can only update packet filters when already configured. + // If it wants to change the station address, it must configure(NULL) + // the instance firstly. + // + if (IpInstance->State == IP6_STATE_CONFIGED) { + Current = &IpInstance->ConfigData; + + if (!EFI_IP6_EQUAL (&Current->StationAddress, &Ip6ConfigData->StationAddress)) { + Status = EFI_ALREADY_STARTED; + goto Exit; + } + + if (NetIp6IsUnspecifiedAddr (&Current->StationAddress) && IP6_NO_MAPPING (IpInstance)) { + Status = EFI_NO_MAPPING; + goto Exit; + } + } + } + + // + // Configure the instance or clean it up. + // + if (Ip6ConfigData != NULL) { + Status = Ip6ConfigProtocol (IpInstance, Ip6ConfigData); + } else { + Status = Ip6CleanProtocol (IpInstance); + + // + // Don't change the state if it is DESTROY, consider the following + // valid sequence: Mnp is unloaded-->Ip Stopped-->Udp Stopped, + // Configure (ThisIp, NULL). If the state is changed to UNCONFIGED, + // the unload fails miserably. + // + if (IpInstance->State == IP6_STATE_CONFIGED) { + IpInstance->State = IP6_STATE_UNCONFIGED; + } + } + + // + // Update the MNP's configure data. Ip6ServiceConfigMnp will check + // whether it is necessary to reconfigure the MNP. + // + Ip6ServiceConfigMnp (IpInstance->Service, FALSE); + +Exit: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Joins and leaves multicast groups. + + The Groups() function is used to join and leave multicast group sessions. Joining a group will + enable reception of matching multicast packets. Leaving a group will disable reception of matching + multicast packets. Source-Specific Multicast isn't required to be supported. + + If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left. + + @param[in] This Pointer to the EFI_IP6_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join the multicast group session, and FALSE to leave. + @param[in] GroupAddress Pointer to the IPv6 multicast address. + This is an optional parameter that may be NULL. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following is TRUE: + - This is NULL. + - JoinFlag is TRUE and GroupAddress is NULL. + - GroupAddress is not NULL and *GroupAddress is + not a multicast IPv6 address. + - GroupAddress is not NULL and *GroupAddress is in the + range of SSM destination address. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_OUT_OF_RESOURCES System resources could not be allocated. + @retval EFI_UNSUPPORTED This EFI IPv6 Protocol implementation does not support multicast groups. + @retval EFI_ALREADY_STARTED The group address is already in the group table (when + JoinFlag is TRUE). + @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Groups ( + IN EFI_IP6_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_IPv6_ADDRESS *GroupAddress OPTIONAL + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + IP6_PROTOCOL *IpInstance; + IP6_SERVICE *IpSb; + + if ((This == NULL) || (JoinFlag && GroupAddress == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (GroupAddress != NULL && !IP6_IS_MULTICAST (GroupAddress)) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This); + IpSb = IpInstance->Service; + + if (IpSb->LinkLocalDadFail) { + return EFI_DEVICE_ERROR; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (IpInstance->State != IP6_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + Status = Ip6Groups (IpInstance, JoinFlag, GroupAddress); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Adds and deletes routing table entries. + + The Routes() function adds a route to, or deletes a route from, the routing table. + + Routes are determined by comparing the leftmost PrefixLength bits of Destination with + the destination IPv6 address arithmetically. The gateway address must be on the same subnet as the + configured station address. + + The default route is added with Destination and PrefixLegth both set to all zeros. The + default route matches all destination IPv6 addresses that do not match any other routes. + + All EFI IPv6 Protocol instances share a routing table. + + @param[in] This Pointer to the EFI_IP6_PROTOCOL instance. + @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to + FALSE to add this route to the routing table. Destination, + PrefixLength and Gateway are used as the key to each + route entry. + @param[in] Destination The address prefix of the subnet that needs to be routed. + This is an optional parameter that may be NULL. + @param[in] PrefixLength The prefix length of Destination. Ignored if Destination + is NULL. + @param[in] GatewayAddress The unicast gateway IPv6 address for this route. + This is an optional parameter that may be NULL. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The driver instance has not been started. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - When DeleteRoute is TRUE, both Destination and + GatewayAddress are NULL. + - When DeleteRoute is FALSE, either Destination or + GatewayAddress is NULL. + - *GatewayAddress is not a valid unicast IPv6 address. + - *GatewayAddress is one of the local configured IPv6 + addresses. + @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table. + @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE). + @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when + DeleteRoute is FALSE). + +**/ +EFI_STATUS +EFIAPI +EfiIp6Routes ( + IN EFI_IP6_PROTOCOL *This, + IN BOOLEAN DeleteRoute, + IN EFI_IPv6_ADDRESS *Destination OPTIONAL, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *GatewayAddress OPTIONAL + ) +{ + IP6_PROTOCOL *IpInstance; + EFI_STATUS Status; + EFI_TPL OldTpl; + IP6_SERVICE *IpSb; + + if ((This == NULL) || (PrefixLength > IP6_PREFIX_MAX)) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This); + IpSb = IpInstance->Service; + + if (IpSb->LinkLocalDadFail) { + return EFI_DEVICE_ERROR; + } + + if (IpInstance->State != IP6_STATE_CONFIGED) { + return EFI_NOT_STARTED; + } + + if (DeleteRoute && (Destination == NULL) && (GatewayAddress == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (!DeleteRoute && (Destination == NULL || GatewayAddress == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (GatewayAddress != NULL) { + if (!Ip6IsValidAddress (IpSb, GatewayAddress, FALSE)) { + return EFI_INVALID_PARAMETER; + } + + if (!NetIp6IsUnspecifiedAddr (GatewayAddress) && + !NetIp6IsNetEqual (GatewayAddress, &IpInstance->ConfigData.StationAddress, PrefixLength) + ) { + return EFI_INVALID_PARAMETER; + } + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Update the route table + // + if (DeleteRoute) { + Status = Ip6DelRoute (IpSb->RouteTable, Destination, PrefixLength, GatewayAddress); + } else { + Status = Ip6AddRoute (IpSb->RouteTable, Destination, PrefixLength, GatewayAddress); + } + + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Add or delete Neighbor cache entries. + + The Neighbors() function is used to add, update, or delete an entry from neighbor cache. + IPv6 neighbor cache entries are typically inserted and updated by the network protocol driver as + network traffic is processed. Most neighbor cache entries will timeout and be deleted if the network + traffic stops. Neighbor cache entries that were inserted by Neighbors() may be static (will not + timeout) or dynamic (will timeout). + + The implementation should follow the neighbor cache timeout mechanism which is defined in + RFC4861. The default neighbor cache timeout value should be tuned for the expected network + environment + + @param[in] This Pointer to the EFI_IP6_PROTOCOL instance. + @param[in] DeleteFlag Set to TRUE to delete the specified cache entry, set to FALSE to + add (or update, if it already exists and Override is TRUE) the + specified cache entry. TargetIp6Address is used as the key + to find the requested cache entry. + @param[in] TargetIp6Address Pointer to the Target IPv6 address. + @param[in] TargetLinkAddress Pointer to the link-layer address of the target. Ignored if NULL. + @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor + cache, it will be deleted after Timeout. A value of zero means that + the entry is permanent. A non-zero value means that the entry is + dynamic. + @param[in] Override If TRUE, the cached link-layer address of the matching entry will + be overridden and updated; if FALSE, EFI_ACCESS_DENIED + will be returned if a corresponding cache entry already existed. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - TargetIpAddress is NULL. + - *TargetLinkAddress is invalid when not NULL. + - *TargetIpAddress is not a valid unicast IPv6 address. + - *TargetIpAddress is one of the local configured IPv6 + addresses. + @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache. + @retval EFI_NOT_FOUND This entry is not in the neighbor cache (when DeleteFlag is + TRUE or when DeleteFlag is FALSE while + TargetLinkAddress is NULL.). + @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache, + and that entry is tagged as un-overridden (when Override + is FALSE). + +**/ +EFI_STATUS +EFIAPI +EfiIp6Neighbors ( + IN EFI_IP6_PROTOCOL *This, + IN BOOLEAN DeleteFlag, + IN EFI_IPv6_ADDRESS *TargetIp6Address, + IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL, + IN UINT32 Timeout, + IN BOOLEAN Override + ) +{ + EFI_TPL OldTpl; + EFI_STATUS Status; + IP6_PROTOCOL *IpInstance; + IP6_SERVICE *IpSb; + + if (This == NULL || TargetIp6Address == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (NetIp6IsUnspecifiedAddr (TargetIp6Address)) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This); + IpSb = IpInstance->Service; + + if (IpSb->LinkLocalDadFail) { + return EFI_DEVICE_ERROR; + } + + if (!Ip6IsValidAddress (IpSb, TargetIp6Address, FALSE)) { + return EFI_INVALID_PARAMETER; + } + + if (TargetLinkAddress != NULL) { + if (!Ip6IsValidLinkAddress (IpSb, TargetLinkAddress)) { + return EFI_INVALID_PARAMETER; + } + } + + if (Ip6IsOneOfSetAddress (IpSb, TargetIp6Address, NULL, NULL)) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + if (IpInstance->State != IP6_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto Exit; + } + + if (DeleteFlag) { + Status = Ip6DelNeighbor (IpInstance->Service, TargetIp6Address, TargetLinkAddress, Timeout, Override); + } else { + Status = Ip6AddNeighbor (IpInstance->Service, TargetIp6Address, TargetLinkAddress, Timeout, Override); + } + +Exit: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Check whether the user's token or event has already + been enqueue on IP6's list. + + @param[in] Map The container of either user's transmit or receive + token. + @param[in] Item Current item to check against. + @param[in] Context The Token to check againist. + + @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP + @retval EFI_SUCCESS The current item isn't the same token/event as the + context. + +**/ +EFI_STATUS +EFIAPI +Ip6TokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + EFI_IP6_COMPLETION_TOKEN *Token; + EFI_IP6_COMPLETION_TOKEN *TokenInItem; + + Token = (EFI_IP6_COMPLETION_TOKEN *) Context; + TokenInItem = (EFI_IP6_COMPLETION_TOKEN *) Item->Key; + + if (Token == TokenInItem || Token->Event == TokenInItem->Event) { + return EFI_ACCESS_DENIED; + } + + return EFI_SUCCESS; +} + +/** + Validate the user's token against the current station address. + + @param[in] Token User's token to validate. + + @retval EFI_INVALID_PARAMETER Some parameters are invalid. + @retval EFI_BAD_BUFFER_SIZE The user's option/data is too long. + @retval EFI_SUCCESS The token is OK. + +**/ +EFI_STATUS +Ip6TxTokenValid ( + IN EFI_IP6_COMPLETION_TOKEN *Token + ) +{ + EFI_IP6_TRANSMIT_DATA *TxData; + UINT32 Index; + UINT32 DataLength; + + if (Token == NULL || Token->Event == NULL) { + return EFI_INVALID_PARAMETER; + } + + TxData = Token->Packet.TxData; + + if (TxData == NULL || (TxData->ExtHdrsLength != 0 && TxData->ExtHdrs == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (TxData->FragmentCount == 0 || TxData->DataLength == 0) { + return EFI_INVALID_PARAMETER; + } + + for (DataLength = 0, Index = 0; Index < TxData->FragmentCount; Index++) { + if (TxData->FragmentTable[Index].FragmentLength == 0 || TxData->FragmentTable[Index].FragmentBuffer == NULL) { + return EFI_INVALID_PARAMETER; + } + + DataLength += TxData->FragmentTable[Index].FragmentLength; + } + + if (TxData->DataLength != DataLength) { + return EFI_INVALID_PARAMETER; + } + + // + // TODO: Token.Packet.TxData.DataLength is too short to transmit. + // return EFI_BUFFER_TOO_SMALL; + // + + // + // If Token.Packet.TxData.DataLength is beyond the maximum that which can be + // described through the Fragment Offset field in Fragment header when performing + // fragmentation. + // + if (TxData->DataLength > 64 * 1024) { + return EFI_BAD_BUFFER_SIZE; + } + + return EFI_SUCCESS; +} + +/** + The callback function for the net buffer which wraps the user's + transmit token. Although this function seems simple, there + are some subtle aspects. + When user requests the IP to transmit a packet by passing it a + token, the token is wrapped in an IP6_TXTOKEN_WRAP and the data + is wrapped in an net buffer. The net buffer's Free function is + set to Ip6FreeTxToken. The Token and token wrap are added to the + IP child's TxToken map. Then the buffer is passed to Ip6Output for + transmission. If an error happened before that, the buffer + is freed, which in turn frees the token wrap. The wrap may + have been added to the TxToken map or not, and the user's event + shouldn't be fired because we are still in the EfiIp6Transmit. If + the buffer has been sent by Ip6Output, it should be removed from + the TxToken map and user's event signaled. The token wrap and buffer + are bound together. Check the comments in Ip6Output for information + about IP fragmentation. + + @param[in] Context The token's wrap. + +**/ +VOID +EFIAPI +Ip6FreeTxToken ( + IN VOID *Context + ) +{ + IP6_TXTOKEN_WRAP *Wrap; + NET_MAP_ITEM *Item; + + Wrap = (IP6_TXTOKEN_WRAP *) Context; + + // + // Signal IpSecRecycleEvent to inform IPsec free the memory + // + if (Wrap->IpSecRecycleSignal != NULL) { + gBS->SignalEvent (Wrap->IpSecRecycleSignal); + } + + // + // Find the token in the instance's map. EfiIp6Transmit put the + // token to the map. If that failed, NetMapFindKey will return NULL. + // + Item = NetMapFindKey (&Wrap->IpInstance->TxTokens, Wrap->Token); + + if (Item != NULL) { + NetMapRemoveItem (&Wrap->IpInstance->TxTokens, Item, NULL); + } + + if (Wrap->Sent) { + gBS->SignalEvent (Wrap->Token->Event); + + // + // Dispatch the DPC queued by the NotifyFunction of Token->Event. + // + DispatchDpc (); + } + + FreePool (Wrap); +} + + +/** + The callback function to Ip6Output to update the transmit status. + + @param[in] Packet The user's transmit packet. + @param[in] IoStatus The result of the transmission. + @param[in] Flag Not used during transmission. + @param[in] Context The token's wrap. + +**/ +VOID +Ip6OnPacketSent ( + IN NET_BUF *Packet, + IN EFI_STATUS IoStatus, + IN UINT32 Flag, + IN VOID *Context + ) +{ + IP6_TXTOKEN_WRAP *Wrap; + + // + // This is the transmission request from upper layer, + // not the IP6 driver itself. + // + Wrap = (IP6_TXTOKEN_WRAP *) Context; + Wrap->Token->Status = IoStatus; + + NetbufFree (Wrap->Packet); +} + +/** + Places outgoing data packets into the transmit queue. + + The Transmit() function places a sending request in the transmit queue of this + EFI IPv6 Protocol instance. Whenever the packet in the token is sent out or some + errors occur, the event in the token will be signaled, and the status is updated. + + @param[in] This Pointer to the EFI_IP6_PROTOCOL instance. + @param[in] Token Pointer to the transmit token. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING The IPv6 driver was responsible for choosing + a source address for this transmission, + but no source address was available for use. + @retval EFI_INVALID_PARAMETER One or more of the following is TRUE: + - This is NULL. + - Token is NULL. + - Token.Event is NULL. + - Token.Packet.TxData is NULL. + - Token.Packet.ExtHdrsLength is not zero and + Token.Packet.ExtHdrs is NULL. + - Token.Packet.FragmentCount is zero. + - One or more of the Token.Packet.TxData. + FragmentTable[].FragmentLength fields is zero. + - One or more of the Token.Packet.TxData. + FragmentTable[].FragmentBuffer fields is NULL. + - Token.Packet.TxData.DataLength is zero or not + equal to the sum of fragment lengths. + - Token.Packet.TxData.DestinationAddress is non + zero when DestinationAddress is configured as + non-zero when doing Configure() for this + EFI IPv6 protocol instance. + - Token.Packet.TxData.DestinationAddress is + unspecified when DestinationAddress is unspecified + when doing Configure() for this EFI IPv6 protocol + instance. + @retval EFI_ACCESS_DENIED The transmit completion token with the same Token. + Event was already in the transmit queue. + @retval EFI_NOT_READY The completion token could not be queued because + the transmit queue is full. + @retval EFI_NOT_FOUND Not route is found to destination address. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data. + @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too + short to transmit. + @retval EFI_BAD_BUFFER_SIZE If Token.Packet.TxData.DataLength is beyond the + maximum that which can be described through the + Fragment Offset field in Fragment header when + performing fragmentation. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Transmit ( + IN EFI_IP6_PROTOCOL *This, + IN EFI_IP6_COMPLETION_TOKEN *Token + ) +{ + IP6_SERVICE *IpSb; + IP6_PROTOCOL *IpInstance; + EFI_IP6_CONFIG_DATA *Config; + EFI_STATUS Status; + EFI_TPL OldTpl; + EFI_IP6_HEADER Head; + EFI_IP6_TRANSMIT_DATA *TxData; + EFI_IP6_OVERRIDE_DATA *Override; + IP6_TXTOKEN_WRAP *Wrap; + UINT8 *ExtHdrs; + + // + // Check input parameters. + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + ExtHdrs = NULL; + + Status = Ip6TxTokenValid (Token); + if (EFI_ERROR (Status)) { + return Status; + } + + IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This); + IpSb = IpInstance->Service; + + if (IpSb->LinkLocalDadFail) { + return EFI_DEVICE_ERROR; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (IpInstance->State != IP6_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto Exit; + } + + Config = &IpInstance->ConfigData; + + // + // Check whether the token or signal already existed. + // + if (EFI_ERROR (NetMapIterate (&IpInstance->TxTokens, Ip6TokenExist, Token))) { + Status = EFI_ACCESS_DENIED; + goto Exit; + } + + // + // Build the IP header, fill in the information from ConfigData or OverrideData + // + ZeroMem (&Head, sizeof(EFI_IP6_HEADER)); + TxData = Token->Packet.TxData; + IP6_COPY_ADDRESS (&Head.SourceAddress, &Config->StationAddress); + IP6_COPY_ADDRESS (&Head.DestinationAddress, &Config->DestinationAddress); + + Status = EFI_INVALID_PARAMETER; + + if (NetIp6IsUnspecifiedAddr (&TxData->DestinationAddress)) { + if (NetIp6IsUnspecifiedAddr (&Config->DestinationAddress)) { + goto Exit; + } + + ASSERT (!NetIp6IsUnspecifiedAddr (&Config->StationAddress)); + + } else { + // + // StationAddress is unspecified only when ConfigData.Dest is unspecified. + // Use TxData.Dest to override the DestinationAddress. + // + if (!NetIp6IsUnspecifiedAddr (&Config->DestinationAddress)) { + goto Exit; + } + + if (NetIp6IsUnspecifiedAddr (&Config->StationAddress)) { + Status = Ip6SelectSourceAddress ( + IpSb, + &TxData->DestinationAddress, + &Head.SourceAddress + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + + IP6_COPY_ADDRESS (&Head.DestinationAddress, &TxData->DestinationAddress); + } + + // + // Fill in Head infos. + // + Head.NextHeader = Config->DefaultProtocol; + if (TxData->ExtHdrsLength != 0) { + Head.NextHeader = TxData->NextHeader; + } + + if (TxData->OverrideData != NULL) { + Override = TxData->OverrideData; + Head.NextHeader = Override->Protocol; + Head.HopLimit = Override->HopLimit; + Head.FlowLabelL = HTONS ((UINT16) Override->FlowLabel); + Head.FlowLabelH = (UINT8) ((Override->FlowLabel >> 16) & 0x0F); + } else { + Head.HopLimit = Config->HopLimit; + Head.FlowLabelL = HTONS ((UINT16) Config->FlowLabel); + Head.FlowLabelH = (UINT8) ((Config->FlowLabel >> 16) & 0x0F); + } + + Head.PayloadLength = HTONS ((UINT16) (TxData->ExtHdrsLength + TxData->DataLength)); + + // + // OK, it survives all the validation check. Wrap the token in + // a IP6_TXTOKEN_WRAP and the data in a netbuf + // + Status = EFI_OUT_OF_RESOURCES; + Wrap = AllocateZeroPool (sizeof (IP6_TXTOKEN_WRAP)); + if (Wrap == NULL) { + goto Exit; + } + + Wrap->IpInstance = IpInstance; + Wrap->Token = Token; + Wrap->Sent = FALSE; + Wrap->Life = IP6_US_TO_SEC (Config->TransmitTimeout); + Wrap->Packet = NetbufFromExt ( + (NET_FRAGMENT *) TxData->FragmentTable, + TxData->FragmentCount, + IP6_MAX_HEADLEN, + 0, + Ip6FreeTxToken, + Wrap + ); + + if (Wrap->Packet == NULL) { + FreePool (Wrap); + goto Exit; + } + + Token->Status = EFI_NOT_READY; + + Status = NetMapInsertTail (&IpInstance->TxTokens, Token, Wrap); + if (EFI_ERROR (Status)) { + // + // NetbufFree will call Ip6FreeTxToken, which in turn will + // free the IP6_TXTOKEN_WRAP. Now, the token wrap hasn't been + // enqueued. + // + NetbufFree (Wrap->Packet); + goto Exit; + } + + // + // Allocate a new buffer to store IPv6 extension headers to avoid updating + // the original data in EFI_IP6_COMPLETION_TOKEN. + // + if (TxData->ExtHdrsLength != 0 && TxData->ExtHdrs != NULL) { + ExtHdrs = (UINT8 *) AllocateCopyPool (TxData->ExtHdrsLength, TxData->ExtHdrs); + if (ExtHdrs == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + } + + // + // Mark the packet sent before output it. Mark it not sent again if the + // returned status is not EFI_SUCCESS; + // + Wrap->Sent = TRUE; + + Status = Ip6Output ( + IpSb, + NULL, + IpInstance, + Wrap->Packet, + &Head, + ExtHdrs, + TxData->ExtHdrsLength, + Ip6OnPacketSent, + Wrap + ); + if (EFI_ERROR (Status)) { + Wrap->Sent = FALSE; + NetbufFree (Wrap->Packet); + } + +Exit: + gBS->RestoreTPL (OldTpl); + + if (ExtHdrs != NULL) { + FreePool (ExtHdrs); + } + + return Status; +} + +/** + Places a receiving request into the receiving queue. + + The Receive() function places a completion token into the receive packet queue. + This function is always asynchronous. + + The Token.Event field in the completion token must be filled in by the caller + and cannot be NULL. When the receive operation completes, the EFI IPv6 Protocol + driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event + is signaled. + + Current Udp implementation creates an IP child for each Udp child. + It initates a asynchronous receive immediately no matter whether + there is no mapping or not. Therefore, disable the returning EFI_NO_MAPPING for now. + To enable it, the following check must be performed: + + if (NetIp6IsUnspecifiedAddr (&Config->StationAddress) && IP6_NO_MAPPING (IpInstance)) { + Status = EFI_NO_MAPPING; + goto Exit; + } + + @param[in] This Pointer to the EFI_IP6_PROTOCOL instance. + @param[in] Token Pointer to a token that is associated with the receive data descriptor. + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This EFI IPv6 Protocol instance has not been started. + @retval EFI_NO_MAPPING When IP6 driver responsible for binding source address to this instance, + while no source address is available for use. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Token is NULL. + - Token.Event is NULL. + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system + resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The EFI IPv6 Protocol instance has been reset to startup defaults. + @retval EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already + in the receive queue. + @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Receive ( + IN EFI_IP6_PROTOCOL *This, + IN EFI_IP6_COMPLETION_TOKEN *Token + ) +{ + IP6_PROTOCOL *IpInstance; + EFI_STATUS Status; + EFI_TPL OldTpl; + IP6_SERVICE *IpSb; + + if (This == NULL || Token == NULL || Token->Event == NULL) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This); + IpSb = IpInstance->Service; + + if (IpSb->LinkLocalDadFail) { + return EFI_DEVICE_ERROR; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (IpInstance->State != IP6_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto Exit; + } + + // + // Check whether the toke is already on the receive queue. + // + Status = NetMapIterate (&IpInstance->RxTokens, Ip6TokenExist, Token); + + if (EFI_ERROR (Status)) { + Status = EFI_ACCESS_DENIED; + goto Exit; + } + + // + // Queue the token then check whether there is pending received packet. + // + Status = NetMapInsertTail (&IpInstance->RxTokens, Token, NULL); + + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status = Ip6InstanceDeliverPacket (IpInstance); + + // + // Dispatch the DPC queued by the NotifyFunction of this instane's receive + // event. + // + DispatchDpc (); + +Exit: + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Cancel the transmitted but not recycled packet. If a matching + token is found, it will call Ip6CancelPacket to cancel the + packet. Ip6CancelPacket cancels all the fragments of the + packet. When all the fragments are freed, the IP6_TXTOKEN_WRAP + is deleted from the Map, and user's event is signalled. + Because Ip6CancelPacket and other functions are all called in + line, after Ip6CancelPacket returns, the Item has been freed. + + @param[in] Map The IP6 child's transmit queue. + @param[in] Item The current transmitted packet to test. + @param[in] Context The user's token to cancel. + + @retval EFI_SUCCESS Continue to check the next Item. + @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled. + +**/ +EFI_STATUS +EFIAPI +Ip6CancelTxTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + EFI_IP6_COMPLETION_TOKEN *Token; + IP6_TXTOKEN_WRAP *Wrap; + + Token = (EFI_IP6_COMPLETION_TOKEN *) Context; + + // + // Return EFI_SUCCESS to check the next item in the map if + // this one doesn't match. + // + if ((Token != NULL) && (Token != Item->Key)) { + return EFI_SUCCESS; + } + + Wrap = (IP6_TXTOKEN_WRAP *) Item->Value; + ASSERT (Wrap != NULL); + + // + // Don't access the Item, Wrap and Token's members after this point. + // Item and wrap has been freed. And we no longer own the Token. + // + Ip6CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED); + + // + // If only one item is to be cancel, return EFI_ABORTED to stop + // iterating the map any more. + // + if (Token != NULL) { + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + + +/** + Cancel the receive request. This is simple, because + it is only enqueued in our local receive map. + + @param[in] Map The IP6 child's receive queue. + @param[in] Item Current receive request to cancel. + @param[in] Context The user's token to cancel. + + + @retval EFI_SUCCESS Continue to check the next receive request on the + queue. + @retval EFI_ABORTED The user's token (token != NULL) has been + cancelled. + +**/ +EFI_STATUS +EFIAPI +Ip6CancelRxTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + EFI_IP6_COMPLETION_TOKEN *Token; + EFI_IP6_COMPLETION_TOKEN *This; + + Token = (EFI_IP6_COMPLETION_TOKEN *) Context; + This = Item->Key; + + if ((Token != NULL) && (Token != This)) { + return EFI_SUCCESS; + } + + NetMapRemoveItem (Map, Item, NULL); + + This->Status = EFI_ABORTED; + This->Packet.RxData = NULL; + gBS->SignalEvent (This->Event); + + if (Token != NULL) { + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + +/** + Cancel the user's receive/transmit request. It is the worker function of + EfiIp6Cancel API. + + @param[in] IpInstance The IP6 child. + @param[in] Token The token to cancel. If NULL, all token will be + cancelled. + + @retval EFI_SUCCESS The token is cancelled. + @retval EFI_NOT_FOUND The token isn't found on either the + transmit/receive queue. + @retval EFI_DEVICE_ERROR Not all tokens are cancelled when Token is NULL. + +**/ +EFI_STATUS +Ip6Cancel ( + IN IP6_PROTOCOL *IpInstance, + IN EFI_IP6_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // First check the transmitted packet. Ip6CancelTxTokens returns + // EFI_ABORTED to mean that the token has been cancelled when + // token != NULL. So, return EFI_SUCCESS for this condition. + // + Status = NetMapIterate (&IpInstance->TxTokens, Ip6CancelTxTokens, Token); + if (EFI_ERROR (Status)) { + if ((Token != NULL) && (Status == EFI_ABORTED)) { + return EFI_SUCCESS; + } + + return Status; + } + + // + // Check the receive queue. Ip6CancelRxTokens also returns EFI_ABORT + // for Token!=NULL and it is cancelled. + // + Status = NetMapIterate (&IpInstance->RxTokens, Ip6CancelRxTokens, Token); + // + // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's + // events. + // + DispatchDpc (); + if (EFI_ERROR (Status)) { + if ((Token != NULL) && (Status == EFI_ABORTED)) { + return EFI_SUCCESS; + } + + return Status; + } + + // + // OK, if the Token is found when Token != NULL, the NetMapIterate + // will return EFI_ABORTED, which has been interrupted as EFI_SUCCESS. + // + if (Token != NULL) { + return EFI_NOT_FOUND; + } + + // + // If Token == NULL, cancel all the tokens. return error if not + // all of them are cancelled. + // + if (!NetMapIsEmpty (&IpInstance->TxTokens) || !NetMapIsEmpty (&IpInstance->RxTokens)) { + + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + +/** + Abort an asynchronous transmit or receive request. + + The Cancel() function is used to abort a pending transmit or receive request. + If the token is in the transmit or receive request queues, after calling this + function, Token->Status will be set to EFI_ABORTED, and then Token->Event will + be signaled. If the token is not in one of the queues, which usually means the + asynchronous operation has completed, this function will not signal the token, + and EFI_NOT_FOUND is returned. + + @param[in] This Pointer to the EFI_IP6_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_IP6_PROTOCOL.Transmit() or + EFI_IP6_PROTOCOL.Receive(). If NULL, all pending + tokens are aborted. Type EFI_IP6_COMPLETION_TOKEN is + defined in EFI_IP6_PROTOCOL.Transmit(). + + @retval EFI_SUCCESS The asynchronous I/O request was aborted and + Token->Event was signaled. When Token is NULL, all + pending requests were aborted, and their events were signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was + not found in the transmit or receive queue. It has either completed + or was not issued by Transmit() and Receive(). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Cancel ( + IN EFI_IP6_PROTOCOL *This, + IN EFI_IP6_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + IP6_PROTOCOL *IpInstance; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (IpInstance->State != IP6_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + goto Exit; + } + + Status = Ip6Cancel (IpInstance, Token); + +Exit: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Polls for incoming data packets, and processes outgoing data packets. + + The Poll() function polls for incoming data packets and processes outgoing data + packets. Network drivers and applications can call the EFI_IP6_PROTOCOL.Poll() + function to increase the rate that data packets are moved between the communications + device and the transmit and receive queues. + + In some systems the periodic timer event may not poll the underlying communications + device fast enough to transmit and/or receive all data packets without missing + incoming packets or dropping outgoing packets. Drivers and applications that are + experiencing packet loss should try calling the EFI_IP6_PROTOCOL.Poll() function + more often. + + @param[in] This Pointer to the EFI_IP6_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI IPv6 Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system error or network error occurred. + @retval EFI_NOT_READY No incoming or outgoing data was processed. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue. + Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Poll ( + IN EFI_IP6_PROTOCOL *This + ) +{ + IP6_PROTOCOL *IpInstance; + IP6_SERVICE *IpSb; + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This); + IpSb = IpInstance->Service; + + if (IpSb->LinkLocalDadFail) { + return EFI_DEVICE_ERROR; + } + + if (IpInstance->State == IP6_STATE_UNCONFIGED) { + return EFI_NOT_STARTED; + } + + Mnp = IpInstance->Service->Mnp; + + // + // Don't lock the Poll function to enable the deliver of + // the packet polled up. + // + return Mnp->Poll (Mnp); + +} + diff --git a/NetworkPkg/Ip6Dxe/Ip6Impl.h b/NetworkPkg/Ip6Dxe/Ip6Impl.h new file mode 100644 index 000000000..d46ee600d --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Impl.h @@ -0,0 +1,748 @@ +/** @file + Implementation of EFI_IP6_PROTOCOL protocol interfaces and type definitions. + + Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ (C) Copyright 2015 Hewlett-Packard Development Company, L.P.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP6_IMPL_H__ +#define __EFI_IP6_IMPL_H__ + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "Ip6Common.h" +#include "Ip6Driver.h" +#include "Ip6Icmp.h" +#include "Ip6If.h" +#include "Ip6Input.h" +#include "Ip6Mld.h" +#include "Ip6Nd.h" +#include "Ip6Option.h" +#include "Ip6Output.h" +#include "Ip6Route.h" +#include "Ip6ConfigNv.h" +#include "Ip6ConfigImpl.h" + +#define IP6_PROTOCOL_SIGNATURE SIGNATURE_32 ('I', 'P', '6', 'P') +#define IP6_SERVICE_SIGNATURE SIGNATURE_32 ('I', 'P', '6', 'S') + +// +// The state of IP6 protocol. It starts from UNCONFIGED. if it is +// successfully configured, it goes to CONFIGED. if configure NULL +// is called, it becomes UNCONFIGED again. If (partly) destroyed, it +// becomes DESTROY. +// +#define IP6_STATE_UNCONFIGED 0 +#define IP6_STATE_CONFIGED 1 + +// +// The state of IP6 service. It starts from UNSTARTED. It transits +// to STARTED if autoconfigure is started. If default address is +// configured, it becomes CONFIGED. and if partly destroyed, it goes +// to DESTROY. +// +#define IP6_SERVICE_UNSTARTED 0 +#define IP6_SERVICE_STARTED 1 +#define IP6_SERVICE_CONFIGED 2 +#define IP6_SERVICE_DESTROY 3 + +#define IP6_INSTANCE_FROM_PROTOCOL(Ip6) \ + CR ((Ip6), IP6_PROTOCOL, Ip6Proto, IP6_PROTOCOL_SIGNATURE) + +#define IP6_SERVICE_FROM_PROTOCOL(Sb) \ + CR ((Sb), IP6_SERVICE, ServiceBinding, IP6_SERVICE_SIGNATURE) + +#define IP6_NO_MAPPING(IpInstance) (!(IpInstance)->Interface->Configured) + +extern EFI_IPSEC2_PROTOCOL *mIpSec; +extern BOOLEAN mIpSec2Installed; + +// +// IP6_TXTOKEN_WRAP wraps the upper layer's transmit token. +// The user's data is kept in the Packet. When fragment is +// needed, each fragment of the Packet has a reference to the +// Packet, no data is actually copied. The Packet will be +// released when all the fragments of it have been recycled by +// MNP. Upon then, the IP6_TXTOKEN_WRAP will be released, and +// user's event signalled. +// +typedef struct { + IP6_PROTOCOL *IpInstance; + EFI_IP6_COMPLETION_TOKEN *Token; + EFI_EVENT IpSecRecycleSignal; + NET_BUF *Packet; + BOOLEAN Sent; + INTN Life; +} IP6_TXTOKEN_WRAP; + +typedef struct { + EFI_EVENT IpSecRecycleSignal; + NET_BUF *Packet; +} IP6_IPSEC_WRAP; + +// +// IP6_RXDATA_WRAP wraps the data IP6 child delivers to the +// upper layers. The received packet is kept in the Packet. +// The Packet itself may be constructured from some fragments. +// All the fragments of the Packet is organized by a +// IP6_ASSEMBLE_ENTRY structure. If the Packet is recycled by +// the upper layer, the assemble entry and its associated +// fragments will be freed at last. +// +typedef struct { + LIST_ENTRY Link; + IP6_PROTOCOL *IpInstance; + NET_BUF *Packet; + EFI_IP6_RECEIVE_DATA RxData; +} IP6_RXDATA_WRAP; + +struct _IP6_PROTOCOL { + UINT32 Signature; + + EFI_IP6_PROTOCOL Ip6Proto; + EFI_HANDLE Handle; + INTN State; + + IP6_SERVICE *Service; + LIST_ENTRY Link; // Link to all the IP protocol from the service + + UINT8 PrefixLength; // PrefixLength of the configured station address. + // + // User's transmit/receive tokens, and received/deliverd packets + // + NET_MAP RxTokens; + NET_MAP TxTokens; // map between (User's Token, IP6_TXTOKE_WRAP) + LIST_ENTRY Received; // Received but not delivered packet + LIST_ENTRY Delivered; // Delivered and to be recycled packets + EFI_LOCK RecycleLock; + + IP6_INTERFACE *Interface; + LIST_ENTRY AddrLink; // Ip instances with the same IP address. + + EFI_IPv6_ADDRESS *GroupList; // stored in network order. + UINT32 GroupCount; + + EFI_IP6_CONFIG_DATA ConfigData; + BOOLEAN InDestroy; +}; + +struct _IP6_SERVICE { + UINT32 Signature; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + INTN State; + + // + // List of all the IP instances and interfaces, and default + // interface and route table and caches. + // + UINTN NumChildren; + LIST_ENTRY Children; + + LIST_ENTRY Interfaces; + + IP6_INTERFACE *DefaultInterface; + IP6_ROUTE_TABLE *RouteTable; + + IP6_LINK_RX_TOKEN RecvRequest; + + // + // Ip reassemble utilities and MLD data + // + IP6_ASSEMBLE_TABLE Assemble; + IP6_MLD_SERVICE_DATA MldCtrl; + + EFI_IPv6_ADDRESS LinkLocalAddr; + BOOLEAN LinkLocalOk; + BOOLEAN LinkLocalDadFail; + BOOLEAN Dhcp6NeedStart; + BOOLEAN Dhcp6NeedInfoRequest; + + // + // ND data + // + UINT8 CurHopLimit; + UINT32 LinkMTU; + UINT32 BaseReachableTime; + UINT32 ReachableTime; + UINT32 RetransTimer; + LIST_ENTRY NeighborTable; + + LIST_ENTRY OnlinkPrefix; + LIST_ENTRY AutonomousPrefix; + + LIST_ENTRY DefaultRouterList; + UINT32 RoundRobin; + + UINT8 InterfaceIdLen; + UINT8 *InterfaceId; + + BOOLEAN RouterAdvertiseReceived; + UINT8 SolicitTimer; + UINT32 Ticks; + + // + // Low level protocol used by this service instance + // + EFI_HANDLE Image; + EFI_HANDLE Controller; + + EFI_HANDLE MnpChildHandle; + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + + EFI_MANAGED_NETWORK_CONFIG_DATA MnpConfigData; + EFI_SIMPLE_NETWORK_MODE SnpMode; + + EFI_EVENT Timer; + EFI_EVENT FasterTimer; + + // + // IPv6 Configuration Protocol instance + // + IP6_CONFIG_INSTANCE Ip6ConfigInstance; + + // + // The string representation of the current mac address of the + // NIC this IP6_SERVICE works on. + // + CHAR16 *MacString; + UINT32 MaxPacketSize; + UINT32 OldMaxPacketSize; +}; + +/** + The callback function for the net buffer which wraps the user's + transmit token. Although this function seems simple, + there are some subtle aspects. + When a user requests the IP to transmit a packet by passing it a + token, the token is wrapped in an IP6_TXTOKEN_WRAP and the data + is wrapped in a net buffer. The net buffer's Free function is + set to Ip6FreeTxToken. The Token and token wrap are added to the + IP child's TxToken map. Then the buffer is passed to Ip6Output for + transmission. If an error occurs before that, the buffer + is freed, which in turn frees the token wrap. The wrap may + have been added to the TxToken map or not, and the user's event + shouldn't be signaled because we are still in the EfiIp6Transmit. If + the buffer has been sent by Ip6Output, it should be removed from + the TxToken map and the user's event signaled. The token wrap and buffer + are bound together. Refer to the comments in Ip6Output for information + about IP fragmentation. + + @param[in] Context The token's wrap. + +**/ +VOID +EFIAPI +Ip6FreeTxToken ( + IN VOID *Context + ); + +/** + Config the MNP parameter used by IP. The IP driver use one MNP + child to transmit/receive frames. By default, it configures MNP + to receive unicast/multicast/broadcast. And it will enable/disable + the promiscuous receive according to whether there is IP child + enable that or not. If Force is FALSE, it will iterate through + all the IP children to check whether the promiscuous receive + setting has been changed. If it hasn't been changed, it won't + reconfigure the MNP. If Force is TRUE, the MNP is configured + whether that is changed or not. + + @param[in] IpSb The IP6 service instance that is to be changed. + @param[in] Force Force the configuration or not. + + @retval EFI_SUCCESS The MNP successfully configured/reconfigured. + @retval Others The configuration failed. + +**/ +EFI_STATUS +Ip6ServiceConfigMnp ( + IN IP6_SERVICE *IpSb, + IN BOOLEAN Force + ); + +/** + Cancel the user's receive/transmit request. It is the worker function of + EfiIp6Cancel API. + + @param[in] IpInstance The IP6 child. + @param[in] Token The token to cancel. If NULL, all tokens will be + cancelled. + + @retval EFI_SUCCESS The token was cancelled. + @retval EFI_NOT_FOUND The token isn't found on either the + transmit or receive queue. + @retval EFI_DEVICE_ERROR Not all tokens are cancelled when Token is NULL. + +**/ +EFI_STATUS +Ip6Cancel ( + IN IP6_PROTOCOL *IpInstance, + IN EFI_IP6_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + Initialize the IP6_PROTOCOL structure to the unconfigured states. + + @param[in] IpSb The IP6 service instance. + @param[in, out] IpInstance The IP6 child instance. + +**/ +VOID +Ip6InitProtocol ( + IN IP6_SERVICE *IpSb, + IN OUT IP6_PROTOCOL *IpInstance + ); + +/** + Clean up the IP6 child, release all the resources used by it. + + @param[in, out] IpInstance The IP6 child to clean up. + + @retval EFI_SUCCESS The IP6 child was cleaned up + @retval EFI_DEVICE_ERROR Some resources failed to be released. + +**/ +EFI_STATUS +Ip6CleanProtocol ( + IN OUT IP6_PROTOCOL *IpInstance + ); + +// +// EFI_IP6_PROTOCOL interface prototypes +// + +/** + Gets the current operational settings for this instance of the EFI IPv6 Protocol driver. + + The GetModeData() function returns the current operational mode data for this driver instance. + The data fields in EFI_IP6_MODE_DATA are read only. This function is used optionally to + retrieve the operational mode data of underlying networks or drivers. + + @param[in] This The pointer to the EFI_IP6_PROTOCOL instance. + @param[out] Ip6ModeData The pointer to the EFI IPv6 Protocol mode data structure. + @param[out] MnpConfigData The pointer to the managed network configuration data structure. + @param[out] SnpModeData The pointer to the simple network mode data structure. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated. + +**/ +EFI_STATUS +EFIAPI +EfiIp6GetModeData ( + IN EFI_IP6_PROTOCOL *This, + OUT EFI_IP6_MODE_DATA *Ip6ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ); + +/** + Assigns an IPv6 address and subnet mask to this EFI IPv6 Protocol driver instance. + + The Configure() function is used to set, change, or reset the operational parameters and filter + settings for this EFI IPv6 Protocol instance. Until these parameters have been set, no network traffic + can be sent or received by this instance. Once the parameters have been reset (by calling this + function with Ip6ConfigData set to NULL), no more traffic can be sent or received until these + parameters have been set again. Each EFI IPv6 Protocol instance can be started and stopped + independently of each other by enabling or disabling their receive filter settings with the + Configure() function. + + If Ip6ConfigData.StationAddress is a valid non-zero IPv6 unicast address, it is required + to be one of the currently configured IPv6 addresses list in the EFI IPv6 drivers, or else + EFI_INVALID_PARAMETER will be returned. If Ip6ConfigData.StationAddress is + unspecified, the IPv6 driver will bind a source address according to the source address selection + algorithm. Clients could frequently call GetModeData() to check get a currently configured IPv6. + If both Ip6ConfigData.StationAddress and Ip6ConfigData.Destination are unspecified, when + transmitting the packet afterwards, the source address filled in each outgoing IPv6 packet + is decided based on the destination of this packet. + + If operational parameters are reset or changed, any pending transmit and receive requests will be + cancelled. Their completion token status will be set to EFI_ABORTED, and their events will be + signaled. + + @param[in] This The pointer to the EFI_IP6_PROTOCOL instance. + @param[in] Ip6ConfigData The pointer to the EFI IPv6 Protocol configuration data structure. + If NULL, reset the configuration data. + + @retval EFI_SUCCESS The driver instance was successfully opened. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Ip6ConfigData.StationAddress is neither zero nor + a unicast IPv6 address. + - Ip6ConfigData.StationAddress is neither zero nor + one of the configured IP addresses in the EFI IPv6 driver. + - Ip6ConfigData.DefaultProtocol is illegal. + @retval EFI_OUT_OF_RESOURCES The EFI IPv6 Protocol driver instance data could not be allocated. + @retval EFI_NO_MAPPING The IPv6 driver was responsible for choosing a source address for + this instance, but no source address was available for use. + @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the IPv6 + address or prefix length can be changed. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv6 + Protocol driver instance was not opened. + @retval EFI_UNSUPPORTED Default protocol specified through + Ip6ConfigData.DefaulProtocol isn't supported. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Configure ( + IN EFI_IP6_PROTOCOL *This, + IN EFI_IP6_CONFIG_DATA *Ip6ConfigData OPTIONAL + ); + +/** + Joins and leaves multicast groups. + + The Groups() function is used to join and leave multicast group sessions. Joining a group will + enable reception of matching multicast packets. Leaving a group will disable reception of matching + multicast packets. Source-Specific Multicast isn't required to be supported. + + If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left. + + @param[in] This The pointer to the EFI_IP6_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave. + @param[in] GroupAddress The pointer to the IPv6 multicast address. + This is an optional parameter that may be NULL. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following is TRUE: + - This is NULL. + - JoinFlag is TRUE and GroupAddress is NULL. + - GroupAddress is not NULL and *GroupAddress is + not a multicast IPv6 address. + - GroupAddress is not NULL and *GroupAddress is in the + range of SSM destination address. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_OUT_OF_RESOURCES System resources could not be allocated. + @retval EFI_UNSUPPORTED This EFI IPv6 Protocol implementation does not support multicast groups. + @retval EFI_ALREADY_STARTED The group address is already in the group table (when + JoinFlag is TRUE). + @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Groups ( + IN EFI_IP6_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_IPv6_ADDRESS *GroupAddress OPTIONAL + ); + +/** + Adds and deletes routing table entries. + + The Routes() function adds a route to or deletes a route from the routing table. + + Routes are determined by comparing the leftmost PrefixLength bits of Destination with + the destination IPv6 address arithmetically. The gateway address must be on the same subnet as the + configured station address. + + The default route is added with Destination and PrefixLegth both set to all zeros. The + default route matches all destination IPv6 addresses that do not match any other routes. + + All EFI IPv6 Protocol instances share a routing table. + + @param[in] This The pointer to the EFI_IP6_PROTOCOL instance. + @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to + FALSE to add this route to the routing table. Destination, + PrefixLength and Gateway are used as the key to each + route entry. + @param[in] Destination The address prefix of the subnet that needs to be routed. + This is an optional parameter that may be NULL. + @param[in] PrefixLength The prefix length of Destination. Ignored if Destination + is NULL. + @param[in] GatewayAddress The unicast gateway IPv6 address for this route. + This is an optional parameter that may be NULL. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The driver instance has not been started. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - When DeleteRoute is TRUE, both Destination and + GatewayAddress are NULL. + - When DeleteRoute is FALSE, either Destination or + GatewayAddress is NULL. + - *GatewayAddress is not a valid unicast IPv6 address. + - *GatewayAddress is one of the local configured IPv6 + addresses. + @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table. + @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE). + @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when + DeleteRoute is FALSE). + +**/ +EFI_STATUS +EFIAPI +EfiIp6Routes ( + IN EFI_IP6_PROTOCOL *This, + IN BOOLEAN DeleteRoute, + IN EFI_IPv6_ADDRESS *Destination OPTIONAL, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *GatewayAddress OPTIONAL + ); + +/** + Add or delete Neighbor cache entries. + + The Neighbors() function is used to add, update, or delete an entry from a neighbor cache. + IPv6 neighbor cache entries are typically inserted and updated by the network protocol driver as + network traffic is processed. Most neighbor cache entries will timeout and be deleted if the network + traffic stops. Neighbor cache entries that were inserted by Neighbors() may be static (will not + timeout) or dynamic (will timeout). + + The implementation should follow the neighbor cache timeout mechanism defined in + RFC4861. The default neighbor cache timeout value should be tuned for the expected network + environment. + + @param[in] This The pointer to the EFI_IP6_PROTOCOL instance. + @param[in] DeleteFlag Set to TRUE to delete the specified cache entry. Set to FALSE to + add (or update, if it already exists and Override is TRUE) the + specified cache entry. TargetIp6Address is used as the key + to find the requested cache entry. + @param[in] TargetIp6Address The pointer to the Target IPv6 address. + @param[in] TargetLinkAddress The pointer to link-layer address of the target. Ignored if NULL. + @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor + cache, it will be deleted after Timeout. A value of zero means that + the entry is permanent. A non-zero value means that the entry is + dynamic. + @param[in] Override If TRUE, the cached link-layer address of the matching entry will + be overridden and updated; if FALSE, EFI_ACCESS_DENIED + will be returned if a corresponding cache entry already exists. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - TargetIpAddress is NULL. + - *TargetLinkAddress is invalid when not NULL. + - *TargetIpAddress is not a valid unicast IPv6 address. + - *TargetIpAddress is one of the local configured IPv6 + addresses. + @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache. + @retval EFI_NOT_FOUND This entry is not in the neighbor cache (when DeleteFlag is + TRUE or when DeleteFlag is FALSE while + TargetLinkAddress is NULL.). + @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache, + and that entry is tagged as un-overridden (when Override + is FALSE). + +**/ +EFI_STATUS +EFIAPI +EfiIp6Neighbors ( + IN EFI_IP6_PROTOCOL *This, + IN BOOLEAN DeleteFlag, + IN EFI_IPv6_ADDRESS *TargetIp6Address, + IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL, + IN UINT32 Timeout, + IN BOOLEAN Override + ); + +/** + Places outgoing data packets into the transmit queue. + + The Transmit() function places a sending request in the transmit queue of this + EFI IPv6 Protocol instance. Whenever the packet in the token is sent out or some + errors occur, the event in the token will be signaled and the status is updated. + + @param[in] This The pointer to the EFI_IP6_PROTOCOL instance. + @param[in] Token The pointer to the transmit token. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING The IPv6 driver was responsible for choosing + a source address for this transmission, + but no source address was available for use. + @retval EFI_INVALID_PARAMETER One or more of the following is TRUE: + - This is NULL. + - Token is NULL. + - Token.Event is NULL. + - Token.Packet.TxData is NULL. + - Token.Packet.ExtHdrsLength is not zero and + Token.Packet.ExtHdrs is NULL. + - Token.Packet.FragmentCount is zero. + - One or more of the Token.Packet.TxData. + FragmentTable[].FragmentLength fields is zero. + - One or more of the Token.Packet.TxData. + FragmentTable[].FragmentBuffer fields is NULL. + - Token.Packet.TxData.DataLength is zero or not + equal to the sum of fragment lengths. + - Token.Packet.TxData.DestinationAddress is non- + zero when DestinationAddress is configured as + non-zero when doing Configure() for this + EFI IPv6 protocol instance. + - Token.Packet.TxData.DestinationAddress is + unspecified when DestinationAddress is unspecified + when doing Configure() for this EFI IPv6 protocol + instance. + @retval EFI_ACCESS_DENIED The transmit completion token with the same Token. + The event was already in the transmit queue. + @retval EFI_NOT_READY The completion token could not be queued because + the transmit queue is full. + @retval EFI_NOT_FOUND Not route is found to the destination address. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data. + @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too + short to transmit. + @retval EFI_BAD_BUFFER_SIZE If Token.Packet.TxData.DataLength is beyond the + maximum that which can be described through the + Fragment Offset field in Fragment header when + performing fragmentation. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Transmit ( + IN EFI_IP6_PROTOCOL *This, + IN EFI_IP6_COMPLETION_TOKEN *Token + ); + +/** + Places a receiving request into the receiving queue. + + The Receive() function places a completion token into the receive packet queue. + This function is always asynchronous. + + The Token.Event field in the completion token must be filled in by the caller + and cannot be NULL. When the receive operation completes, the EFI IPv6 Protocol + driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event + is signaled. + + Current Udp implementation creates an IP child for each Udp child. + It initates a asynchronous receive immediately whether or not + there is no mapping. Therefore, disable the returning EFI_NO_MAPPING for now. + To enable it, the following check must be performed: + + if (NetIp6IsUnspecifiedAddr (&Config->StationAddress) && IP6_NO_MAPPING (IpInstance)) { + Status = EFI_NO_MAPPING; + goto Exit; + } + + @param[in] This The pointer to the EFI_IP6_PROTOCOL instance. + @param[in] Token The pointer to a token that is associated with the + receive data descriptor. + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This EFI IPv6 Protocol instance has not been started. + @retval EFI_NO_MAPPING When IP6 driver responsible for binding source address to this instance, + while no source address is available for use. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Token is NULL. + - Token.Event is NULL. + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system + resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The EFI IPv6 Protocol instance has been reset to startup defaults. + @retval EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already + in the receive queue. + @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Receive ( + IN EFI_IP6_PROTOCOL *This, + IN EFI_IP6_COMPLETION_TOKEN *Token + ); + +/** + Abort an asynchronous transmit or receive request. + + The Cancel() function is used to abort a pending transmit or receive request. + If the token is in the transmit or receive request queues, after calling this + function, Token->Status will be set to EFI_ABORTED, and then Token->Event will + be signaled. If the token is not in one of the queues, which usually means the + asynchronous operation has completed, this function will not signal the token, + and EFI_NOT_FOUND is returned. + + @param[in] This The pointer to the EFI_IP6_PROTOCOL instance. + @param[in] Token The pointer to a token that has been issued by + EFI_IP6_PROTOCOL.Transmit() or + EFI_IP6_PROTOCOL.Receive(). If NULL, all pending + tokens are aborted. Type EFI_IP6_COMPLETION_TOKEN is + defined in EFI_IP6_PROTOCOL.Transmit(). + + @retval EFI_SUCCESS The asynchronous I/O request was aborted and + Token->Event was signaled. When Token is NULL, all + pending requests were aborted, and their events were signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was + not found in the transmit or receive queue. It has either completed + or was not issued by Transmit() and Receive(). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Cancel ( + IN EFI_IP6_PROTOCOL *This, + IN EFI_IP6_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function polls for incoming data packets and processes outgoing data + packets. Network drivers and applications can call the EFI_IP6_PROTOCOL.Poll() + function to increase the rate that data packets are moved between the communications + device and the transmit and receive queues. + + In some systems the periodic timer event may not poll the underlying communications + device fast enough to transmit and/or receive all data packets without missing + incoming packets or dropping outgoing packets. Drivers and applications that are + experiencing packet loss should try calling the EFI_IP6_PROTOCOL.Poll() function + more often. + + @param[in] This The pointer to the EFI_IP6_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI IPv6 Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_NOT_READY No incoming or outgoing data was processed. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue. + Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +EfiIp6Poll ( + IN EFI_IP6_PROTOCOL *This + ); + +#endif diff --git a/NetworkPkg/Ip6Dxe/Ip6Input.c b/NetworkPkg/Ip6Dxe/Ip6Input.c new file mode 100644 index 000000000..57a52326e --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Input.c @@ -0,0 +1,1815 @@ +/** @file + IP6 internal functions to process the incoming packets. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ (C) Copyright 2015 Hewlett-Packard Development Company, L.P.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +/** + Create an empty assemble entry for the packet identified by + (Dst, Src, Id). The default life for the packet is 60 seconds. + + @param[in] Dst The destination address. + @param[in] Src The source address. + @param[in] Id The ID field in the IP header. + + @return NULL if failed to allocate memory for the entry. Otherwise, + the pointer to the just created reassemble entry. + +**/ +IP6_ASSEMBLE_ENTRY * +Ip6CreateAssembleEntry ( + IN EFI_IPv6_ADDRESS *Dst, + IN EFI_IPv6_ADDRESS *Src, + IN UINT32 Id + ) +{ + IP6_ASSEMBLE_ENTRY *Assemble; + + Assemble = AllocatePool (sizeof (IP6_ASSEMBLE_ENTRY)); + if (Assemble == NULL) { + return NULL; + } + + IP6_COPY_ADDRESS (&Assemble->Dst, Dst); + IP6_COPY_ADDRESS (&Assemble->Src, Src); + InitializeListHead (&Assemble->Fragments); + + Assemble->Id = Id; + Assemble->Life = IP6_FRAGMENT_LIFE + 1; + + Assemble->TotalLen = 0; + Assemble->CurLen = 0; + Assemble->Head = NULL; + Assemble->Info = NULL; + Assemble->Packet = NULL; + + return Assemble; +} + +/** + Release all the fragments of a packet, then free the assemble entry. + + @param[in] Assemble The assemble entry to free. + +**/ +VOID +Ip6FreeAssembleEntry ( + IN IP6_ASSEMBLE_ENTRY *Assemble + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + NET_BUF *Fragment; + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Assemble->Fragments) { + Fragment = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + + RemoveEntryList (Entry); + NetbufFree (Fragment); + } + + if (Assemble->Packet != NULL) { + NetbufFree (Assemble->Packet); + } + + FreePool (Assemble); +} + +/** + Release all the fragments of the packet. This is the callback for + the assembled packet's OnFree. It will free the assemble entry, + which in turn frees all the fragments of the packet. + + @param[in] Arg The assemble entry to free. + +**/ +VOID +EFIAPI +Ip6OnFreeFragments ( + IN VOID *Arg + ) +{ + Ip6FreeAssembleEntry ((IP6_ASSEMBLE_ENTRY *) Arg); +} + +/** + Trim the packet to fit in [Start, End), and update per the + packet information. + + @param[in, out] Packet Packet to trim. + @param[in] Start The sequence of the first byte to fit in. + @param[in] End One beyond the sequence of last byte to fit in. + +**/ +VOID +Ip6TrimPacket ( + IN OUT NET_BUF *Packet, + IN INTN Start, + IN INTN End + ) +{ + IP6_CLIP_INFO *Info; + INTN Len; + + Info = IP6_GET_CLIP_INFO (Packet); + + ASSERT (Info->Start + Info->Length == Info->End); + ASSERT ((Info->Start < End) && (Start < Info->End)); + + if (Info->Start < Start) { + Len = Start - Info->Start; + + NetbufTrim (Packet, (UINT32) Len, NET_BUF_HEAD); + Info->Start = (UINT32) Start; + Info->Length -= (UINT32) Len; + } + + if (End < Info->End) { + Len = End - Info->End; + + NetbufTrim (Packet, (UINT32) Len, NET_BUF_TAIL); + Info->End = (UINT32) End; + Info->Length -= (UINT32) Len; + } +} + +/** + Reassemble the IP fragments. If all the fragments of the packet + have been received, it will wrap the packet in a net buffer then + return it to caller. If the packet can't be assembled, NULL is + returned. + + @param[in, out] Table The assemble table used. A new assemble entry will be created + if the Packet is from a new chain of fragments. + @param[in] Packet The fragment to assemble. It might be freed if the fragment + can't be re-assembled. + + @return NULL if the packet can't be reassembled. The pointer to the just assembled + packet if all the fragments of the packet have arrived. + +**/ +NET_BUF * +Ip6Reassemble ( + IN OUT IP6_ASSEMBLE_TABLE *Table, + IN NET_BUF *Packet + ) +{ + EFI_IP6_HEADER *Head; + IP6_CLIP_INFO *This; + IP6_CLIP_INFO *Node; + IP6_ASSEMBLE_ENTRY *Assemble; + IP6_ASSEMBLE_ENTRY *Entry; + LIST_ENTRY *ListHead; + LIST_ENTRY *Prev; + LIST_ENTRY *Cur; + NET_BUF *Fragment; + NET_BUF *TmpPacket; + NET_BUF *NewPacket; + NET_BUF *Duplicate; + UINT8 *DupHead; + INTN Index; + UINT16 UnFragmentLen; + UINT8 *NextHeader; + + Head = Packet->Ip.Ip6; + This = IP6_GET_CLIP_INFO (Packet); + + ASSERT (Head != NULL); + + // + // Find the corresponding assemble entry by (Dst, Src, Id) + // + Assemble = NULL; + Index = IP6_ASSEMBLE_HASH (&Head->DestinationAddress, &Head->SourceAddress, This->Id); + + NET_LIST_FOR_EACH (Cur, &Table->Bucket[Index]) { + Entry = NET_LIST_USER_STRUCT (Cur, IP6_ASSEMBLE_ENTRY, Link); + + if (Entry->Id == This->Id && + EFI_IP6_EQUAL (&Entry->Src, &Head->SourceAddress) && + EFI_IP6_EQUAL (&Entry->Dst, &Head->DestinationAddress) + ) { + Assemble = Entry; + break; + } + } + + // + // Create a new entry if can not find an existing one, insert it to assemble table + // + if (Assemble == NULL) { + Assemble = Ip6CreateAssembleEntry ( + &Head->DestinationAddress, + &Head->SourceAddress, + This->Id + ); + + if (Assemble == NULL) { + goto Error; + } + + InsertHeadList (&Table->Bucket[Index], &Assemble->Link); + } + + // + // Find the point to insert the packet: before the first + // fragment with THIS.Start < CUR.Start. the previous one + // has PREV.Start <= THIS.Start < CUR.Start. + // + ListHead = &Assemble->Fragments; + + NET_LIST_FOR_EACH (Cur, ListHead) { + Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List); + + if (This->Start < IP6_GET_CLIP_INFO (Fragment)->Start) { + break; + } + } + + // + // Check whether the current fragment overlaps with the previous one. + // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to + // check whether THIS.Start < PREV.End for overlap. If two fragments + // overlaps, trim the overlapped part off THIS fragment. + // + if ((Prev = Cur->BackLink) != ListHead) { + Fragment = NET_LIST_USER_STRUCT (Prev, NET_BUF, List); + Node = IP6_GET_CLIP_INFO (Fragment); + + if (This->Start < Node->End) { + if (This->End <= Node->End) { + goto Error; + } + + // + // Trim the previous fragment from tail. + // + Ip6TrimPacket (Fragment, Node->Start, This->Start); + } + } + + // + // Insert the fragment into the packet. The fragment may be removed + // from the list by the following checks. + // + NetListInsertBefore (Cur, &Packet->List); + + // + // Check the packets after the insert point. It holds that: + // THIS.Start <= NODE.Start < NODE.End. The equality holds + // if PREV and NEXT are continuous. THIS fragment may fill + // several holes. Remove the completely overlapped fragments + // + while (Cur != ListHead) { + Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List); + Node = IP6_GET_CLIP_INFO (Fragment); + + // + // Remove fragments completely overlapped by this fragment + // + if (Node->End <= This->End) { + Cur = Cur->ForwardLink; + + RemoveEntryList (&Fragment->List); + Assemble->CurLen -= Node->Length; + + NetbufFree (Fragment); + continue; + } + + // + // The conditions are: THIS.Start <= NODE.Start, and THIS.End < + // NODE.End. Two fragments overlaps if NODE.Start < THIS.End. + // If two fragments start at the same offset, remove THIS fragment + // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)). + // + if (Node->Start < This->End) { + if (This->Start == Node->Start) { + RemoveEntryList (&Packet->List); + goto Error; + } + + Ip6TrimPacket (Packet, This->Start, Node->Start); + } + + break; + } + + // + // Update the assemble info: increase the current length. If it is + // the frist fragment, update the packet's IP head and per packet + // info. If it is the last fragment, update the total length. + // + Assemble->CurLen += This->Length; + + if (This->Start == 0) { + // + // Once the first fragment is enqueued, it can't be removed + // from the fragment list. So, Assemble->Head always point + // to valid memory area. + // + if ((Assemble->Head != NULL) || (Assemble->Packet != NULL)) { + goto Error; + } + + // + // Backup the first fragment in case the reasembly of that packet fail. + // + Duplicate = NetbufDuplicate (Packet, NULL, sizeof (EFI_IP6_HEADER)); + if (Duplicate == NULL) { + goto Error; + } + + // + // Revert IP head to network order. + // + DupHead = NetbufGetByte (Duplicate, 0, NULL); + ASSERT (DupHead != NULL); + Duplicate->Ip.Ip6 = Ip6NtohHead ((EFI_IP6_HEADER *) DupHead); + Assemble->Packet = Duplicate; + + // + // Adjust the unfragmentable part in first fragment + // + UnFragmentLen = (UINT16) (This->HeadLen - sizeof (EFI_IP6_HEADER)); + if (UnFragmentLen == 0) { + // + // There is not any unfragmentable extension header. + // + ASSERT (Head->NextHeader == IP6_FRAGMENT); + Head->NextHeader = This->NextHeader; + } else { + NextHeader = NetbufGetByte ( + Packet, + This->FormerNextHeader + sizeof (EFI_IP6_HEADER), + 0 + ); + if (NextHeader == NULL) { + goto Error; + } + + *NextHeader = This->NextHeader; + } + + Assemble->Head = Head; + Assemble->Info = IP6_GET_CLIP_INFO (Packet); + } + + // + // Don't update the length more than once. + // + if ((This->LastFrag != 0) && (Assemble->TotalLen == 0)) { + Assemble->TotalLen = This->End; + } + + // + // Deliver the whole packet if all the fragments received. + // All fragments received if: + // 1. received the last one, so, the totoal length is know + // 2. received all the data. If the last fragment on the + // queue ends at the total length, all data is received. + // + if ((Assemble->TotalLen != 0) && (Assemble->CurLen >= Assemble->TotalLen)) { + + RemoveEntryList (&Assemble->Link); + + // + // If the packet is properly formated, the last fragment's End + // equals to the packet's total length. Otherwise, the packet + // is a fake, drop it now. + // + Fragment = NET_LIST_USER_STRUCT (ListHead->BackLink, NET_BUF, List); + if (IP6_GET_CLIP_INFO (Fragment)->End != (INTN) Assemble->TotalLen) { + Ip6FreeAssembleEntry (Assemble); + goto Error; + } + + Fragment = NET_LIST_HEAD (ListHead, NET_BUF, List); + This = Assemble->Info; + + // + // This TmpPacket is used to hold the unfragmentable part, i.e., + // the IPv6 header and the unfragmentable extension headers. Be noted that + // the Fragment Header is exluded. + // + TmpPacket = NetbufGetFragment (Fragment, 0, This->HeadLen, 0); + ASSERT (TmpPacket != NULL); + + NET_LIST_FOR_EACH (Cur, ListHead) { + // + // Trim off the unfragment part plus the fragment header from all fragments. + // + Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List); + NetbufTrim (Fragment, This->HeadLen + sizeof (IP6_FRAGMENT_HEADER), TRUE); + } + + InsertHeadList (ListHead, &TmpPacket->List); + + // + // Wrap the packet in a net buffer then deliver it up + // + NewPacket = NetbufFromBufList ( + &Assemble->Fragments, + 0, + 0, + Ip6OnFreeFragments, + Assemble + ); + + if (NewPacket == NULL) { + Ip6FreeAssembleEntry (Assemble); + goto Error; + } + + NewPacket->Ip.Ip6 = Assemble->Head; + + CopyMem (IP6_GET_CLIP_INFO (NewPacket), Assemble->Info, sizeof (IP6_CLIP_INFO)); + + return NewPacket; + } + + return NULL; + +Error: + NetbufFree (Packet); + return NULL; +} + + +/** + The callback function for the net buffer that wraps the packet processed by + IPsec. It releases the wrap packet and also signals IPsec to free the resources. + + @param[in] Arg The wrap context. + +**/ +VOID +EFIAPI +Ip6IpSecFree ( + IN VOID *Arg + ) +{ + IP6_IPSEC_WRAP *Wrap; + + Wrap = (IP6_IPSEC_WRAP *) Arg; + + if (Wrap->IpSecRecycleSignal != NULL) { + gBS->SignalEvent (Wrap->IpSecRecycleSignal); + } + + NetbufFree (Wrap->Packet); + + FreePool (Wrap); + + return; +} + +/** + The work function to locate the IPsec protocol to process the inbound or + outbound IP packets. The process routine handles the packet with the following + actions: bypass the packet, discard the packet, or protect the packet. + + @param[in] IpSb The IP6 service instance. + @param[in, out] Head The caller-supplied IP6 header. + @param[in, out] LastHead The next header field of last IP header. + @param[in, out] Netbuf The IP6 packet to be processed by IPsec. + @param[in, out] ExtHdrs The caller-supplied options. + @param[in, out] ExtHdrsLen The length of the option. + @param[in] Direction The directionality in an SPD entry, + EfiIPsecInBound, or EfiIPsecOutBound. + @param[in] Context The token's wrap. + + @retval EFI_SUCCESS The IPsec protocol is not available or disabled. + @retval EFI_SUCCESS The packet was bypassed, and all buffers remain the same. + @retval EFI_SUCCESS The packet was protected. + @retval EFI_ACCESS_DENIED The packet was discarded. + @retval EFI_OUT_OF_RESOURCES There are not suffcient resources to complete the operation. + @retval EFI_BUFFER_TOO_SMALL The number of non-empty blocks is bigger than the + number of input data blocks when building a fragment table. + +**/ +EFI_STATUS +Ip6IpSecProcessPacket ( + IN IP6_SERVICE *IpSb, + IN OUT EFI_IP6_HEADER **Head, + IN OUT UINT8 *LastHead, + IN OUT NET_BUF **Netbuf, + IN OUT UINT8 **ExtHdrs, + IN OUT UINT32 *ExtHdrsLen, + IN EFI_IPSEC_TRAFFIC_DIR Direction, + IN VOID *Context + ) +{ + NET_FRAGMENT *FragmentTable; + NET_FRAGMENT *OriginalFragmentTable; + UINT32 FragmentCount; + UINT32 OriginalFragmentCount; + EFI_EVENT RecycleEvent; + NET_BUF *Packet; + IP6_TXTOKEN_WRAP *TxWrap; + IP6_IPSEC_WRAP *IpSecWrap; + EFI_STATUS Status; + EFI_IP6_HEADER *PacketHead; + UINT8 *Buf; + EFI_IP6_HEADER ZeroHead; + + Status = EFI_SUCCESS; + + if (!mIpSec2Installed) { + goto ON_EXIT; + } + ASSERT (mIpSec != NULL); + + Packet = *Netbuf; + RecycleEvent = NULL; + IpSecWrap = NULL; + FragmentTable = NULL; + PacketHead = NULL; + Buf = NULL; + TxWrap = (IP6_TXTOKEN_WRAP *) Context; + FragmentCount = Packet->BlockOpNum; + ZeroMem (&ZeroHead, sizeof (EFI_IP6_HEADER)); + + // + // Check whether the ipsec enable variable is set. + // + if (mIpSec->DisabledFlag) { + // + // If IPsec is disabled, restore the original MTU + // + IpSb->MaxPacketSize = IpSb->OldMaxPacketSize; + goto ON_EXIT; + } else { + // + // If IPsec is enabled, use the MTU which reduce the IPsec header length. + // + IpSb->MaxPacketSize = IpSb->OldMaxPacketSize - IP6_MAX_IPSEC_HEADLEN; + } + + + // + // Bypass all multicast inbound or outbound traffic. + // + if (IP6_IS_MULTICAST (&(*Head)->DestinationAddress) || IP6_IS_MULTICAST (&(*Head)->SourceAddress)) { + goto ON_EXIT; + } + + // + // Rebuild fragment table from netbuf to ease ipsec process. + // + FragmentTable = AllocateZeroPool (FragmentCount * sizeof (NET_FRAGMENT)); + + if (FragmentTable == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Status = NetbufBuildExt (Packet, FragmentTable, &FragmentCount); + OriginalFragmentTable = FragmentTable; + OriginalFragmentCount = FragmentCount; + + if (EFI_ERROR(Status)) { + FreePool (FragmentTable); + goto ON_EXIT; + } + + // + // Convert host byte order to network byte order + // + Ip6NtohHead (*Head); + + Status = mIpSec->ProcessExt ( + mIpSec, + IpSb->Controller, + IP_VERSION_6, + (VOID *) (*Head), + LastHead, + (VOID **) ExtHdrs, + ExtHdrsLen, + (EFI_IPSEC_FRAGMENT_DATA **) (&FragmentTable), + &FragmentCount, + Direction, + &RecycleEvent + ); + // + // Convert back to host byte order + // + Ip6NtohHead (*Head); + + if (EFI_ERROR (Status)) { + FreePool (OriginalFragmentTable); + goto ON_EXIT; + } + + if (OriginalFragmentCount == FragmentCount && OriginalFragmentTable == FragmentTable) { + // + // For ByPass Packet + // + FreePool (FragmentTable); + goto ON_EXIT; + } else { + // + // Free the FragmentTable which allocated before calling the IPsec. + // + FreePool (OriginalFragmentTable); + } + + if (Direction == EfiIPsecOutBound && TxWrap != NULL) { + TxWrap->IpSecRecycleSignal = RecycleEvent; + TxWrap->Packet = NetbufFromExt ( + FragmentTable, + FragmentCount, + IP6_MAX_HEADLEN, + 0, + Ip6FreeTxToken, + TxWrap + ); + if (TxWrap->Packet == NULL) { + TxWrap->Packet = *Netbuf; + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + CopyMem ( + IP6_GET_CLIP_INFO (TxWrap->Packet), + IP6_GET_CLIP_INFO (Packet), + sizeof (IP6_CLIP_INFO) + ); + + NetIpSecNetbufFree(Packet); + *Netbuf = TxWrap->Packet; + + } else { + + IpSecWrap = AllocateZeroPool (sizeof (IP6_IPSEC_WRAP)); + + if (IpSecWrap == NULL) { + Status = EFI_OUT_OF_RESOURCES; + gBS->SignalEvent (RecycleEvent); + goto ON_EXIT; + } + + IpSecWrap->IpSecRecycleSignal = RecycleEvent; + IpSecWrap->Packet = Packet; + Packet = NetbufFromExt ( + FragmentTable, + FragmentCount, + IP6_MAX_HEADLEN, + 0, + Ip6IpSecFree, + IpSecWrap + ); + + if (Packet == NULL) { + Packet = IpSecWrap->Packet; + gBS->SignalEvent (RecycleEvent); + FreePool (IpSecWrap); + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + if (Direction == EfiIPsecInBound && 0 != CompareMem (&ZeroHead, *Head, sizeof (EFI_IP6_HEADER))) { + + PacketHead = (EFI_IP6_HEADER *) NetbufAllocSpace ( + Packet, + sizeof (EFI_IP6_HEADER) + *ExtHdrsLen, + NET_BUF_HEAD + ); + if (PacketHead == NULL) { + *Netbuf = Packet; + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + CopyMem (PacketHead, *Head, sizeof (EFI_IP6_HEADER)); + *Head = PacketHead; + Packet->Ip.Ip6 = PacketHead; + + if (*ExtHdrs != NULL) { + Buf = (UINT8 *) (PacketHead + 1); + CopyMem (Buf, *ExtHdrs, *ExtHdrsLen); + } + + NetbufTrim (Packet, sizeof (EFI_IP6_HEADER) + *ExtHdrsLen, TRUE); + CopyMem ( + IP6_GET_CLIP_INFO (Packet), + IP6_GET_CLIP_INFO (IpSecWrap->Packet), + sizeof (IP6_CLIP_INFO) + ); + } + *Netbuf = Packet; + } + +ON_EXIT: + return Status; +} + +/** + Pre-process the IPv6 packet. First validates the IPv6 packet, and + then reassembles packet if it is necessary. + + @param[in] IpSb The IP6 service instance. + @param[in, out] Packet The received IP6 packet to be processed. + @param[in] Flag The link layer flag for the packet received, such + as multicast. + @param[out] Payload The pointer to the payload of the recieved packet. + it starts from the first byte of the extension header. + @param[out] LastHead The pointer of NextHeader of the last extension + header processed by IP6. + @param[out] ExtHdrsLen The length of the whole option. + @param[out] UnFragmentLen The length of unfragmented length of extension headers. + @param[out] Fragmented Indicate whether the packet is fragmented. + @param[out] Head The pointer to the EFI_IP6_Header. + + @retval EFI_SUCCESS The received packet is well format. + @retval EFI_INVALID_PARAMETER The received packet is malformed. + +**/ +EFI_STATUS +Ip6PreProcessPacket ( + IN IP6_SERVICE *IpSb, + IN OUT NET_BUF **Packet, + IN UINT32 Flag, + OUT UINT8 **Payload, + OUT UINT8 **LastHead, + OUT UINT32 *ExtHdrsLen, + OUT UINT32 *UnFragmentLen, + OUT BOOLEAN *Fragmented, + OUT EFI_IP6_HEADER **Head + ) +{ + UINT16 PayloadLen; + UINT16 TotalLen; + UINT32 FormerHeadOffset; + UINT32 HeadLen; + IP6_FRAGMENT_HEADER *FragmentHead; + UINT16 FragmentOffset; + IP6_CLIP_INFO *Info; + EFI_IPv6_ADDRESS Loopback; + + HeadLen = 0; + PayloadLen = 0; + // + // Check whether the input packet is a valid packet + // + if ((*Packet)->TotalSize < IP6_MIN_HEADLEN) { + return EFI_INVALID_PARAMETER; + } + + // + // Get header information of the packet. + // + *Head = (EFI_IP6_HEADER *) NetbufGetByte (*Packet, 0, NULL); + if (*Head == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Multicast addresses must not be used as source addresses in IPv6 packets. + // + if (((*Head)->Version != 6) || (IP6_IS_MULTICAST (&(*Head)->SourceAddress))) { + return EFI_INVALID_PARAMETER; + } + + // + // A packet with a destination address of loopback ::1/128 or unspecified must be dropped. + // + ZeroMem (&Loopback, sizeof (EFI_IPv6_ADDRESS)); + Loopback.Addr[15] = 0x1; + if ((CompareMem (&Loopback, &(*Head)->DestinationAddress, sizeof (EFI_IPv6_ADDRESS)) == 0) || + (NetIp6IsUnspecifiedAddr (&(*Head)->DestinationAddress))) { + return EFI_INVALID_PARAMETER; + } + + // + // Convert the IP header to host byte order. + // + (*Packet)->Ip.Ip6 = Ip6NtohHead (*Head); + + // + // Get the per packet info. + // + Info = IP6_GET_CLIP_INFO (*Packet); + Info->LinkFlag = Flag; + Info->CastType = 0; + + if (IpSb->MnpConfigData.EnablePromiscuousReceive) { + Info->CastType = Ip6Promiscuous; + } + + if (Ip6IsOneOfSetAddress (IpSb, &(*Head)->DestinationAddress, NULL, NULL)) { + Info->CastType = Ip6Unicast; + } else if (IP6_IS_MULTICAST (&(*Head)->DestinationAddress)) { + if (Ip6FindMldEntry (IpSb, &(*Head)->DestinationAddress) != NULL) { + Info->CastType = Ip6Multicast; + } + } + + // + // Drop the packet that is not delivered to us. + // + if (Info->CastType == 0) { + return EFI_INVALID_PARAMETER; + } + + + PayloadLen = (*Head)->PayloadLength; + + Info->Start = 0; + Info->Length = PayloadLen; + Info->End = Info->Start + Info->Length; + Info->HeadLen = (UINT16) sizeof (EFI_IP6_HEADER); + Info->Status = EFI_SUCCESS; + Info->LastFrag = FALSE; + + TotalLen = (UINT16) (PayloadLen + sizeof (EFI_IP6_HEADER)); + + // + // Mnp may deliver frame trailer sequence up, trim it off. + // + if (TotalLen < (*Packet)->TotalSize) { + NetbufTrim (*Packet, (*Packet)->TotalSize - TotalLen, FALSE); + } + + if (TotalLen != (*Packet)->TotalSize) { + return EFI_INVALID_PARAMETER; + } + + // + // Check the extension headers, if exist validate them + // + if (PayloadLen != 0) { + *Payload = AllocatePool ((UINTN) PayloadLen); + if (*Payload == NULL) { + return EFI_INVALID_PARAMETER; + } + + NetbufCopy (*Packet, sizeof (EFI_IP6_HEADER), PayloadLen, *Payload); + } + + if (!Ip6IsExtsValid ( + IpSb, + *Packet, + &(*Head)->NextHeader, + *Payload, + (UINT32) PayloadLen, + TRUE, + &FormerHeadOffset, + LastHead, + ExtHdrsLen, + UnFragmentLen, + Fragmented + )) { + return EFI_INVALID_PARAMETER; + } + + HeadLen = sizeof (EFI_IP6_HEADER) + *UnFragmentLen; + + if (*Fragmented) { + // + // Get the fragment offset from the Fragment header + // + FragmentHead = (IP6_FRAGMENT_HEADER *) NetbufGetByte (*Packet, HeadLen, NULL); + if (FragmentHead == NULL) { + return EFI_INVALID_PARAMETER; + } + + FragmentOffset = NTOHS (FragmentHead->FragmentOffset); + + if ((FragmentOffset & 0x1) == 0) { + Info->LastFrag = TRUE; + } + + FragmentOffset &= (~0x1); + + // + // This is the first fragment of the packet + // + if (FragmentOffset == 0) { + Info->NextHeader = FragmentHead->NextHeader; + } + + Info->HeadLen = (UINT16) HeadLen; + HeadLen += sizeof (IP6_FRAGMENT_HEADER); + Info->Start = FragmentOffset; + Info->Length = TotalLen - (UINT16) HeadLen; + Info->End = Info->Start + Info->Length; + Info->Id = FragmentHead->Identification; + Info->FormerNextHeader = FormerHeadOffset; + + // + // Fragments should in the unit of 8 octets long except the last one. + // + if ((Info->LastFrag == 0) && (Info->Length % 8 != 0)) { + return EFI_INVALID_PARAMETER; + } + + // + // Reassemble the packet. + // + *Packet = Ip6Reassemble (&IpSb->Assemble, *Packet); + if (*Packet == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Re-check the assembled packet to get the right values. + // + *Head = (*Packet)->Ip.Ip6; + PayloadLen = (*Head)->PayloadLength; + if (PayloadLen != 0) { + if (*Payload != NULL) { + FreePool (*Payload); + } + + *Payload = AllocatePool ((UINTN) PayloadLen); + if (*Payload == NULL) { + return EFI_INVALID_PARAMETER; + } + + NetbufCopy (*Packet, sizeof (EFI_IP6_HEADER), PayloadLen, *Payload); + } + + if (!Ip6IsExtsValid ( + IpSb, + *Packet, + &(*Head)->NextHeader, + *Payload, + (UINT32) PayloadLen, + TRUE, + NULL, + LastHead, + ExtHdrsLen, + UnFragmentLen, + Fragmented + )) { + return EFI_INVALID_PARAMETER; + } + } + + // + // Trim the head off, after this point, the packet is headless. + // and Packet->TotalLen == Info->Length. + // + NetbufTrim (*Packet, sizeof (EFI_IP6_HEADER) + *ExtHdrsLen, TRUE); + + return EFI_SUCCESS; +} + +/** + The IP6 input routine. It is called by the IP6_INTERFACE when an + IP6 fragment is received from MNP. + + @param[in] Packet The IP6 packet received. + @param[in] IoStatus The return status of receive request. + @param[in] Flag The link layer flag for the packet received, such + as multicast. + @param[in] Context The IP6 service instance that owns the MNP. + +**/ +VOID +Ip6AcceptFrame ( + IN NET_BUF *Packet, + IN EFI_STATUS IoStatus, + IN UINT32 Flag, + IN VOID *Context + ) +{ + IP6_SERVICE *IpSb; + EFI_IP6_HEADER *Head; + UINT8 *Payload; + UINT8 *LastHead; + UINT32 UnFragmentLen; + UINT32 ExtHdrsLen; + BOOLEAN Fragmented; + EFI_STATUS Status; + EFI_IP6_HEADER ZeroHead; + + IpSb = (IP6_SERVICE *) Context; + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + Payload = NULL; + LastHead = NULL; + + // + // Check input parameters + // + if (EFI_ERROR (IoStatus) || (IpSb->State == IP6_SERVICE_DESTROY)) { + goto Drop; + } + + // + // Pre-Process the Ipv6 Packet and then reassemble if it is necessary. + // + Status = Ip6PreProcessPacket ( + IpSb, + &Packet, + Flag, + &Payload, + &LastHead, + &ExtHdrsLen, + &UnFragmentLen, + &Fragmented, + &Head + ); + if (EFI_ERROR (Status)) { + goto Restart; + } + // + // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer, + // and no need consider any other ahead ext headers. + // + Status = Ip6IpSecProcessPacket ( + IpSb, + &Head, + LastHead, // need get the lasthead value for input + &Packet, + &Payload, + &ExtHdrsLen, + EfiIPsecInBound, + NULL + ); + + if (EFI_ERROR (Status)) { + goto Restart; + } + + // + // If the packet is protected by IPsec Tunnel Mode, Check the Inner Ip Packet. + // + ZeroMem (&ZeroHead, sizeof (EFI_IP6_HEADER)); + if (0 == CompareMem (Head, &ZeroHead, sizeof (EFI_IP6_HEADER))) { + Status = Ip6PreProcessPacket ( + IpSb, + &Packet, + Flag, + &Payload, + &LastHead, + &ExtHdrsLen, + &UnFragmentLen, + &Fragmented, + &Head + ); + if (EFI_ERROR (Status)) { + goto Restart; + } + } + + // + // Check the Packet again. + // + if (Packet == NULL) { + goto Restart; + } + + // + // Packet may have been changed. The ownership of the packet + // is transfered to the packet process logic. + // + Head = Packet->Ip.Ip6; + IP6_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS; + + switch (*LastHead) { + case IP6_ICMP: + Ip6IcmpHandle (IpSb, Head, Packet); + break; + default: + Ip6Demultiplex (IpSb, Head, Packet); + } + + Packet = NULL; + + // + // Dispatch the DPCs queued by the NotifyFunction of the rx token's events + // which are signaled with received data. + // + DispatchDpc (); + +Restart: + if (Payload != NULL) { + FreePool (Payload); + } + + Ip6ReceiveFrame (Ip6AcceptFrame, IpSb); + +Drop: + if (Packet != NULL) { + NetbufFree (Packet); + } + + return ; +} + +/** + Initialize an already allocated assemble table. This is generally + the assemble table embedded in the IP6 service instance. + + @param[in, out] Table The assemble table to initialize. + +**/ +VOID +Ip6CreateAssembleTable ( + IN OUT IP6_ASSEMBLE_TABLE *Table + ) +{ + UINT32 Index; + + for (Index = 0; Index < IP6_ASSEMLE_HASH_SIZE; Index++) { + InitializeListHead (&Table->Bucket[Index]); + } +} + +/** + Clean up the assemble table by removing all of the fragments + and assemble entries. + + @param[in, out] Table The assemble table to clean up. + +**/ +VOID +Ip6CleanAssembleTable ( + IN OUT IP6_ASSEMBLE_TABLE *Table + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP6_ASSEMBLE_ENTRY *Assemble; + UINT32 Index; + + for (Index = 0; Index < IP6_ASSEMLE_HASH_SIZE; Index++) { + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Table->Bucket[Index]) { + Assemble = NET_LIST_USER_STRUCT (Entry, IP6_ASSEMBLE_ENTRY, Link); + + RemoveEntryList (Entry); + Ip6FreeAssembleEntry (Assemble); + } + } +} + + +/** + The signal handle of IP6's recycle event. It is called back + when the upper layer releases the packet. + + @param[in] Event The IP6's recycle event. + @param[in] Context The context of the handle, which is a IP6_RXDATA_WRAP. + +**/ +VOID +EFIAPI +Ip6OnRecyclePacket ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + IP6_RXDATA_WRAP *Wrap; + + Wrap = (IP6_RXDATA_WRAP *) Context; + + EfiAcquireLockOrFail (&Wrap->IpInstance->RecycleLock); + RemoveEntryList (&Wrap->Link); + EfiReleaseLock (&Wrap->IpInstance->RecycleLock); + + ASSERT (!NET_BUF_SHARED (Wrap->Packet)); + NetbufFree (Wrap->Packet); + + gBS->CloseEvent (Wrap->RxData.RecycleSignal); + FreePool (Wrap); +} + +/** + Wrap the received packet to a IP6_RXDATA_WRAP, which will be + delivered to the upper layer. Each IP6 child that accepts the + packet will get a not-shared copy of the packet which is wrapped + in the IP6_RXDATA_WRAP. The IP6_RXDATA_WRAP->RxData is passed + to the upper layer. The upper layer will signal the recycle event in + it when it is done with the packet. + + @param[in] IpInstance The IP6 child to receive the packet. + @param[in] Packet The packet to deliver up. + + @return NULL if it failed to wrap the packet; otherwise, the wrapper. + +**/ +IP6_RXDATA_WRAP * +Ip6WrapRxData ( + IN IP6_PROTOCOL *IpInstance, + IN NET_BUF *Packet + ) +{ + IP6_RXDATA_WRAP *Wrap; + EFI_IP6_RECEIVE_DATA *RxData; + EFI_STATUS Status; + + Wrap = AllocatePool (IP6_RXDATA_WRAP_SIZE (Packet->BlockOpNum)); + + if (Wrap == NULL) { + return NULL; + } + + InitializeListHead (&Wrap->Link); + + Wrap->IpInstance = IpInstance; + Wrap->Packet = Packet; + RxData = &Wrap->RxData; + + ZeroMem (&RxData->TimeStamp, sizeof (EFI_TIME)); + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Ip6OnRecyclePacket, + Wrap, + &RxData->RecycleSignal + ); + + if (EFI_ERROR (Status)) { + FreePool (Wrap); + return NULL; + } + + ASSERT (Packet->Ip.Ip6 != NULL); + + // + // The application expects a network byte order header. + // + RxData->HeaderLength = sizeof (EFI_IP6_HEADER); + RxData->Header = (EFI_IP6_HEADER *) Ip6NtohHead (Packet->Ip.Ip6); + RxData->DataLength = Packet->TotalSize; + + // + // Build the fragment table to be delivered up. + // + RxData->FragmentCount = Packet->BlockOpNum; + NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount); + + return Wrap; +} + +/** + Check whether this IP child accepts the packet. + + @param[in] IpInstance The IP child to check. + @param[in] Head The IP header of the packet. + @param[in] Packet The data of the packet. + + @retval TRUE The child wants to receive the packet. + @retval FALSE The child does not want to receive the packet. + +**/ +BOOLEAN +Ip6InstanceFrameAcceptable ( + IN IP6_PROTOCOL *IpInstance, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_ICMP_ERROR_HEAD Icmp; + EFI_IP6_CONFIG_DATA *Config; + IP6_CLIP_INFO *Info; + UINT8 *Proto; + UINT32 Index; + UINT8 *ExtHdrs; + UINT16 ErrMsgPayloadLen; + UINT8 *ErrMsgPayload; + + Config = &IpInstance->ConfigData; + Proto = NULL; + + // + // Dirty trick for the Tiano UEFI network stack implmentation. If + // ReceiveTimeout == -1, the receive of the packet for this instance + // is disabled. The UEFI spec don't have such captibility. We add + // this to improve the performance because IP will make a copy of + // the received packet for each accepting instance. Some IP instances + // used by UDP/TCP only send packets, they don't wants to receive. + // + if (Config->ReceiveTimeout == (UINT32)(-1)) { + return FALSE; + } + + if (Config->AcceptPromiscuous) { + return TRUE; + } + + // + // Check whether the protocol is acceptable. + // + ExtHdrs = NetbufGetByte (Packet, 0, NULL); + + if (!Ip6IsExtsValid ( + IpInstance->Service, + Packet, + &Head->NextHeader, + ExtHdrs, + (UINT32) Head->PayloadLength, + TRUE, + NULL, + &Proto, + NULL, + NULL, + NULL + )) { + return FALSE; + } + + // + // The upper layer driver may want to receive the ICMPv6 error packet + // invoked by its packet, like UDP. + // + if ((*Proto == IP6_ICMP) && (!Config->AcceptAnyProtocol) && (*Proto != Config->DefaultProtocol)) { + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + + if (Icmp.Head.Type <= ICMP_V6_ERROR_MAX) { + if (!Config->AcceptIcmpErrors) { + return FALSE; + } + + // + // Get the protocol of the invoking packet of ICMPv6 error packet. + // + ErrMsgPayloadLen = NTOHS (Icmp.IpHead.PayloadLength); + ErrMsgPayload = NetbufGetByte (Packet, sizeof (Icmp), NULL); + + if (!Ip6IsExtsValid ( + NULL, + NULL, + &Icmp.IpHead.NextHeader, + ErrMsgPayload, + ErrMsgPayloadLen, + TRUE, + NULL, + &Proto, + NULL, + NULL, + NULL + )) { + return FALSE; + } + } + } + + // + // Match the protocol + // + if (!Config->AcceptAnyProtocol && (*Proto != Config->DefaultProtocol)) { + return FALSE; + } + + // + // Check for broadcast, the caller has computed the packet's + // cast type for this child's interface. + // + Info = IP6_GET_CLIP_INFO (Packet); + + // + // If it is a multicast packet, check whether we are in the group. + // + if (Info->CastType == Ip6Multicast) { + // + // Receive the multicast if the instance wants to receive all packets. + // + if (NetIp6IsUnspecifiedAddr (&IpInstance->ConfigData.StationAddress)) { + return TRUE; + } + + for (Index = 0; Index < IpInstance->GroupCount; Index++) { + if (EFI_IP6_EQUAL (IpInstance->GroupList + Index, &Head->DestinationAddress)) { + break; + } + } + + return (BOOLEAN)(Index < IpInstance->GroupCount); + } + + return TRUE; +} + +/** + Enqueue a shared copy of the packet to the IP6 child if the + packet is acceptable to it. Here the data of the packet is + shared, but the net buffer isn't. + + @param IpInstance The IP6 child to enqueue the packet to. + @param Head The IP header of the received packet. + @param Packet The data of the received packet. + + @retval EFI_NOT_STARTED The IP child hasn't been configured. + @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet. + @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources + @retval EFI_SUCCESS A shared copy the packet is enqueued to the child. + +**/ +EFI_STATUS +Ip6InstanceEnquePacket ( + IN IP6_PROTOCOL *IpInstance, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_CLIP_INFO *Info; + NET_BUF *Clone; + + // + // Check whether the packet is acceptable to this instance. + // + if (IpInstance->State != IP6_STATE_CONFIGED) { + return EFI_NOT_STARTED; + } + + if (!Ip6InstanceFrameAcceptable (IpInstance, Head, Packet)) { + return EFI_INVALID_PARAMETER; + } + + // + // Enque a shared copy of the packet. + // + Clone = NetbufClone (Packet); + + if (Clone == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Set the receive time out for the assembled packet. If it expires, + // packet will be removed from the queue. + // + Info = IP6_GET_CLIP_INFO (Clone); + Info->Life = IP6_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout); + + InsertTailList (&IpInstance->Received, &Clone->List); + return EFI_SUCCESS; +} + +/** + Deliver the received packets to the upper layer if there are both received + requests and enqueued packets. If the enqueued packet is shared, it will + duplicate it to a non-shared packet, release the shared packet, then + deliver the non-shared packet up. + + @param[in] IpInstance The IP child to deliver the packet up. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the + packets. + @retval EFI_SUCCESS All the enqueued packets that can be delivered + are delivered up. + +**/ +EFI_STATUS +Ip6InstanceDeliverPacket ( + IN IP6_PROTOCOL *IpInstance + ) +{ + EFI_IP6_COMPLETION_TOKEN *Token; + IP6_RXDATA_WRAP *Wrap; + NET_BUF *Packet; + NET_BUF *Dup; + UINT8 *Head; + + // + // Deliver a packet if there are both a packet and a receive token. + // + while (!IsListEmpty (&IpInstance->Received) && !NetMapIsEmpty (&IpInstance->RxTokens)) { + + Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List); + + if (!NET_BUF_SHARED (Packet)) { + // + // If this is the only instance that wants the packet, wrap it up. + // + Wrap = Ip6WrapRxData (IpInstance, Packet); + + if (Wrap == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + RemoveEntryList (&Packet->List); + + } else { + // + // Create a duplicated packet if this packet is shared + // + Dup = NetbufDuplicate (Packet, NULL, sizeof (EFI_IP6_HEADER)); + + if (Dup == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Copy the IP head over. The packet to deliver up is + // headless. Trim the head off after copy. The IP head + // may be not continuous before the data. + // + Head = NetbufAllocSpace (Dup, sizeof (EFI_IP6_HEADER), NET_BUF_HEAD); + ASSERT (Head != NULL); + Dup->Ip.Ip6 = (EFI_IP6_HEADER *) Head; + + CopyMem (Head, Packet->Ip.Ip6, sizeof (EFI_IP6_HEADER)); + NetbufTrim (Dup, sizeof (EFI_IP6_HEADER), TRUE); + + Wrap = Ip6WrapRxData (IpInstance, Dup); + + if (Wrap == NULL) { + NetbufFree (Dup); + return EFI_OUT_OF_RESOURCES; + } + + RemoveEntryList (&Packet->List); + NetbufFree (Packet); + + Packet = Dup; + } + + // + // Insert it into the delivered packet, then get a user's + // receive token, pass the wrapped packet up. + // + EfiAcquireLockOrFail (&IpInstance->RecycleLock); + InsertHeadList (&IpInstance->Delivered, &Wrap->Link); + EfiReleaseLock (&IpInstance->RecycleLock); + + Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL); + Token->Status = IP6_GET_CLIP_INFO (Packet)->Status; + Token->Packet.RxData = &Wrap->RxData; + + gBS->SignalEvent (Token->Event); + } + + return EFI_SUCCESS; +} + +/** + Enqueue a received packet to all the IP children that share + the same interface. + + @param[in] IpSb The IP6 service instance that receive the packet. + @param[in] Head The header of the received packet. + @param[in] Packet The data of the received packet. + @param[in] IpIf The interface to enqueue the packet to. + + @return The number of the IP6 children that accepts the packet. + +**/ +INTN +Ip6InterfaceEnquePacket ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet, + IN IP6_INTERFACE *IpIf + ) +{ + IP6_PROTOCOL *IpInstance; + IP6_CLIP_INFO *Info; + LIST_ENTRY *Entry; + INTN Enqueued; + INTN LocalType; + INTN SavedType; + + // + // First, check that the packet is acceptable to this interface + // and find the local cast type for the interface. + // + LocalType = 0; + Info = IP6_GET_CLIP_INFO (Packet); + + if (IpIf->PromiscRecv) { + LocalType = Ip6Promiscuous; + } else { + LocalType = Info->CastType; + } + + // + // Iterate through the ip instances on the interface, enqueue + // the packet if filter passed. Save the original cast type, + // and pass the local cast type to the IP children on the + // interface. The global cast type will be restored later. + // + SavedType = Info->CastType; + Info->CastType = (UINT32) LocalType; + + Enqueued = 0; + + NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) { + IpInstance = NET_LIST_USER_STRUCT (Entry, IP6_PROTOCOL, AddrLink); + NET_CHECK_SIGNATURE (IpInstance, IP6_PROTOCOL_SIGNATURE); + + if (Ip6InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) { + Enqueued++; + } + } + + Info->CastType = (UINT32) SavedType; + return Enqueued; +} + +/** + Deliver the packet for each IP6 child on the interface. + + @param[in] IpSb The IP6 service instance that received the packet. + @param[in] IpIf The IP6 interface to deliver the packet. + +**/ +VOID +Ip6InterfaceDeliverPacket ( + IN IP6_SERVICE *IpSb, + IN IP6_INTERFACE *IpIf + ) +{ + IP6_PROTOCOL *IpInstance; + LIST_ENTRY *Entry; + + NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) { + IpInstance = NET_LIST_USER_STRUCT (Entry, IP6_PROTOCOL, AddrLink); + Ip6InstanceDeliverPacket (IpInstance); + } +} + +/** + De-multiplex the packet. the packet delivery is processed in two + passes. The first pass will enqueue a shared copy of the packet + to each IP6 child that accepts the packet. The second pass will + deliver a non-shared copy of the packet to each IP6 child that + has pending receive requests. Data is copied if more than one + child wants to consume the packet, because each IP child needs + its own copy of the packet to make changes. + + @param[in] IpSb The IP6 service instance that received the packet. + @param[in] Head The header of the received packet. + @param[in] Packet The data of the received packet. + + @retval EFI_NOT_FOUND No IP child accepts the packet. + @retval EFI_SUCCESS The packet is enqueued or delivered to some IP + children. + +**/ +EFI_STATUS +Ip6Demultiplex ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + + LIST_ENTRY *Entry; + IP6_INTERFACE *IpIf; + INTN Enqueued; + + // + // Two pass delivery: first, enque a shared copy of the packet + // to each instance that accept the packet. + // + Enqueued = 0; + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link); + + if (IpIf->Configured) { + Enqueued += Ip6InterfaceEnquePacket (IpSb, Head, Packet, IpIf); + } + } + + // + // Second: deliver a duplicate of the packet to each instance. + // Release the local reference first, so that the last instance + // getting the packet will not copy the data. + // + NetbufFree (Packet); + Packet = NULL; + + if (Enqueued == 0) { + return EFI_NOT_FOUND; + } + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link); + + if (IpIf->Configured) { + Ip6InterfaceDeliverPacket (IpSb, IpIf); + } + } + + return EFI_SUCCESS; +} + +/** + Decrease the life of the transmitted packets. If it is + decreased to zero, cancel the packet. This function is + called by Ip6packetTimerTicking that provides timeout for both the + received-but-not-delivered and transmitted-but-not-recycle + packets. + + @param[in] Map The IP6 child's transmit map. + @param[in] Item Current transmitted packet. + @param[in] Context Not used. + + @retval EFI_SUCCESS Always returns EFI_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +Ip6SentPacketTicking ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + IP6_TXTOKEN_WRAP *Wrap; + + Wrap = (IP6_TXTOKEN_WRAP *) Item->Value; + ASSERT (Wrap != NULL); + + if ((Wrap->Life > 0) && (--Wrap->Life == 0)) { + Ip6CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED); + } + + return EFI_SUCCESS; +} + +/** + Timeout the fragments, and the enqueued, and transmitted packets. + + @param[in] IpSb The IP6 service instance to timeout. + +**/ +VOID +Ip6PacketTimerTicking ( + IN IP6_SERVICE *IpSb + ) +{ + LIST_ENTRY *InstanceEntry; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP6_PROTOCOL *IpInstance; + IP6_ASSEMBLE_ENTRY *Assemble; + NET_BUF *Packet; + IP6_CLIP_INFO *Info; + UINT32 Index; + + // + // First, time out the fragments. The packet's life is counting down + // once the first-arriving fragment of that packet was received. + // + for (Index = 0; Index < IP6_ASSEMLE_HASH_SIZE; Index++) { + NET_LIST_FOR_EACH_SAFE (Entry, Next, &(IpSb->Assemble.Bucket[Index])) { + Assemble = NET_LIST_USER_STRUCT (Entry, IP6_ASSEMBLE_ENTRY, Link); + + if ((Assemble->Life > 0) && (--Assemble->Life == 0)) { + // + // If the first fragment (the one with a Fragment Offset of zero) + // has been received, an ICMP Time Exceeded - Fragment Reassembly + // Time Exceeded message should be sent to the source of that fragment. + // + if ((Assemble->Packet != NULL) && + !IP6_IS_MULTICAST (&Assemble->Head->DestinationAddress)) { + Ip6SendIcmpError ( + IpSb, + Assemble->Packet, + NULL, + &Assemble->Head->SourceAddress, + ICMP_V6_TIME_EXCEEDED, + ICMP_V6_TIMEOUT_REASSEMBLE, + NULL + ); + } + + // + // If reassembly of a packet is not completed within 60 seconds of + // the reception of the first-arriving fragment of that packet, the + // reassembly must be abandoned and all the fragments that have been + // received for that packet must be discarded. + // + RemoveEntryList (Entry); + Ip6FreeAssembleEntry (Assemble); + } + } + } + + NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) { + IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP6_PROTOCOL, Link); + + // + // Second, time out the assembled packets enqueued on each IP child. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) { + Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + Info = IP6_GET_CLIP_INFO (Packet); + + if ((Info->Life > 0) && (--Info->Life == 0)) { + RemoveEntryList (Entry); + NetbufFree (Packet); + } + } + + // + // Third: time out the transmitted packets. + // + NetMapIterate (&IpInstance->TxTokens, Ip6SentPacketTicking, NULL); + } +} + diff --git a/NetworkPkg/Ip6Dxe/Ip6Input.h b/NetworkPkg/Ip6Dxe/Ip6Input.h new file mode 100644 index 000000000..66ddf35ea --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Input.h @@ -0,0 +1,229 @@ +/** @file + IP6 internal functions and definitions to process the incoming packets. + + Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP6_INPUT_H__ +#define __EFI_IP6_INPUT_H__ + +#define IP6_MIN_HEADLEN 40 +#define IP6_MAX_HEADLEN 120 +/// +/// 8(ESP header) + 16(max IV) + 16(max padding) + 2(ESP tail) + 12(max ICV) = 54 +/// +#define IP6_MAX_IPSEC_HEADLEN 54 + + +#define IP6_ASSEMLE_HASH_SIZE 127 +/// +/// Lift time in seconds. +/// +#define IP6_FRAGMENT_LIFE 60 +#define IP6_MAX_PACKET_SIZE 65535 + + +#define IP6_GET_CLIP_INFO(Packet) ((IP6_CLIP_INFO *) ((Packet)->ProtoData)) + +#define IP6_ASSEMBLE_HASH(Dst, Src, Id) \ + ((*((UINT32 *) (Dst)) + *((UINT32 *) (Src)) + (Id)) % IP6_ASSEMLE_HASH_SIZE) + +#define IP6_RXDATA_WRAP_SIZE(NumFrag) \ + (sizeof (IP6_RXDATA_WRAP) + sizeof (EFI_IP6_FRAGMENT_DATA) * ((NumFrag) - 1)) + +// +// Per packet information for input process. LinkFlag specifies whether +// the packet is received as Link layer unicast, multicast or broadcast. +// The CastType is the IP layer cast type, such as IP multicast or unicast. +// Start, End and Length are staffs used to assemble the packets. Start +// is the sequence number of the first byte of data in the packet. Length +// is the number of bytes of data. End = Start + Length, that is, the +// sequence number of last byte + 1. Each assembled packet has a count down +// life. If it isn't consumed before Life reaches zero, the packet is released. +// +typedef struct { + UINT32 LinkFlag; + INT32 CastType; + INT32 Start; + INT32 End; + INT32 Length; + UINT32 Life; + EFI_STATUS Status; + UINT32 Id; + UINT16 HeadLen; + UINT8 NextHeader; + UINT8 LastFrag; + UINT32 FormerNextHeader; +} IP6_CLIP_INFO; + +// +// Structure used to assemble IP packets. +// +typedef struct { + LIST_ENTRY Link; + LIST_ENTRY Fragments; // List of all the fragments of this packet + + // + // Identity of one IP6 packet. Each fragment of a packet has + // the same (Dst, Src, Id). + // + EFI_IPv6_ADDRESS Dst; + EFI_IPv6_ADDRESS Src; + UINT32 Id; + + UINT32 TotalLen; + UINT32 CurLen; + UINT32 Life; // Count down life for the packet. + + EFI_IP6_HEADER *Head; // IP head of the first fragment + IP6_CLIP_INFO *Info; // Per packet information of the first fragment + NET_BUF *Packet; // The first fragment of the packet +} IP6_ASSEMBLE_ENTRY; + +// +// Each Ip service instance has an assemble table to reassemble +// the packets before delivery to its children. It is organized +// as hash table. +// +typedef struct { + LIST_ENTRY Bucket[IP6_ASSEMLE_HASH_SIZE]; +} IP6_ASSEMBLE_TABLE; + +/** + The IP6 input routine. It is called by the IP6_INTERFACE when an + IP6 fragment is received from MNP. + + @param[in] Packet The IP6 packet received. + @param[in] IoStatus The return status of receive request. + @param[in] Flag The link layer flag for the packet received, such + as multicast. + @param[in] Context The IP6 service instance that own the MNP. + +**/ +VOID +Ip6AcceptFrame ( + IN NET_BUF *Packet, + IN EFI_STATUS IoStatus, + IN UINT32 Flag, + IN VOID *Context + ); + +/** + Deliver the received packets to upper layer if there are both received + requests and enqueued packets. If the enqueued packet is shared, it will + duplicate it to a non-shared packet, release the shared packet, then + deliver the non-shared packet up. + + @param[in] IpInstance The IP child to deliver the packet up. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the + packets. + @retval EFI_SUCCESS All the enqueued packets that can be delivered + are delivered up. + +**/ +EFI_STATUS +Ip6InstanceDeliverPacket ( + IN IP6_PROTOCOL *IpInstance + ); + +/** + The work function to locate the IPsec protocol to process the inbound or + outbound IP packets. The process routine handles the packet with the following + actions: bypass the packet, discard the packet, or protect the packet. + + @param[in] IpSb The IP6 service instance. + @param[in, out] Head The caller-supplied IP6 header. + @param[in, out] LastHead The next header field of last IP header. + @param[in, out] Netbuf The IP6 packet to be processed by IPsec. + @param[in, out] ExtHdrs The caller-supplied options. + @param[in, out] ExtHdrsLen The length of the option. + @param[in] Direction The directionality in an SPD entry, + EfiIPsecInBound, or EfiIPsecOutBound. + @param[in] Context The token's wrap. + + @retval EFI_SUCCESS The IPsec protocol is not available or disabled. + @retval EFI_SUCCESS The packet was bypassed, and all buffers remain the same. + @retval EFI_SUCCESS The packet was protected. + @retval EFI_ACCESS_DENIED The packet was discarded. + @retval EFI_OUT_OF_RESOURCES There are not suffcient resources to complete the operation. + @retval EFI_BUFFER_TOO_SMALL The number of non-empty blocks is bigger than the + number of input data blocks when building a fragment table. + +**/ +EFI_STATUS +Ip6IpSecProcessPacket ( + IN IP6_SERVICE *IpSb, + IN OUT EFI_IP6_HEADER **Head, + IN OUT UINT8 *LastHead, + IN OUT NET_BUF **Netbuf, + IN OUT UINT8 **ExtHdrs, + IN OUT UINT32 *ExtHdrsLen, + IN EFI_IPSEC_TRAFFIC_DIR Direction, + IN VOID *Context + ); + +/** + Initialize an already allocated assemble table. This is generally + the assemble table embedded in the IP6 service instance. + + @param[in, out] Table The assemble table to initialize. + +**/ +VOID +Ip6CreateAssembleTable ( + IN OUT IP6_ASSEMBLE_TABLE *Table + ); + +/** + Clean up the assemble table: remove all the fragments + and assemble entries. + + @param[in, out] Table The assemble table to clean up. + +**/ +VOID +Ip6CleanAssembleTable ( + IN OUT IP6_ASSEMBLE_TABLE *Table + ); + +/** + Demultiple the packet. the packet delivery is processed in two + passes. The first pass will enque a shared copy of the packet + to each IP6 child that accepts the packet. The second pass will + deliver a non-shared copy of the packet to each IP6 child that + has pending receive requests. Data is copied if more than one + child wants to consume the packet bacause each IP child need + its own copy of the packet to make changes. + + @param[in] IpSb The IP6 service instance that received the packet. + @param[in] Head The header of the received packet. + @param[in] Packet The data of the received packet. + + @retval EFI_NOT_FOUND No IP child accepts the packet. + @retval EFI_SUCCESS The packet is enqueued or delivered to some IP + children. + +**/ +EFI_STATUS +Ip6Demultiplex ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ); + +/** + Timeout the fragmented, enqueued, and transmitted packets. + + @param[in] IpSb The IP6 service instance to timeout. + +**/ +VOID +Ip6PacketTimerTicking ( + IN IP6_SERVICE *IpSb + ); + +#endif diff --git a/NetworkPkg/Ip6Dxe/Ip6Mld.c b/NetworkPkg/Ip6Dxe/Ip6Mld.c new file mode 100644 index 000000000..b7a882b25 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Mld.c @@ -0,0 +1,902 @@ +/** @file + Multicast Listener Discovery support routines. + + Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +/** + Create a IP6_MLD_GROUP list entry node and record to IP6 service binding data. + + @param[in, out] IpSb Points to IP6 service binding instance. + @param[in] MulticastAddr The IPv6 multicast address to be recorded. + @param[in] DelayTimer The maximum allowed delay before sending a responding + report, in units of milliseconds. + @return The created IP6_ML_GROUP list entry or NULL. + +**/ +IP6_MLD_GROUP * +Ip6CreateMldEntry ( + IN OUT IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *MulticastAddr, + IN UINT32 DelayTimer + ) +{ + IP6_MLD_GROUP *Entry; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + ASSERT (MulticastAddr != NULL && IP6_IS_MULTICAST (MulticastAddr)); + + Entry = AllocatePool (sizeof (IP6_MLD_GROUP)); + if (Entry != NULL) { + Entry->RefCnt = 1; + Entry->DelayTimer = DelayTimer; + Entry->SendByUs = FALSE; + IP6_COPY_ADDRESS (&Entry->Address, MulticastAddr); + InsertTailList (&IpSb->MldCtrl.Groups, &Entry->Link); + } + + return Entry; +} + +/** + Search a IP6_MLD_GROUP list entry node from a list array. + + @param[in] IpSb Points to IP6 service binding instance. + @param[in] MulticastAddr The IPv6 multicast address to be searched. + + @return The found IP6_ML_GROUP list entry or NULL. + +**/ +IP6_MLD_GROUP * +Ip6FindMldEntry ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *MulticastAddr + ) +{ + LIST_ENTRY *Entry; + IP6_MLD_GROUP *Group; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + ASSERT (MulticastAddr != NULL && IP6_IS_MULTICAST (MulticastAddr)); + + NET_LIST_FOR_EACH (Entry, &IpSb->MldCtrl.Groups) { + Group = NET_LIST_USER_STRUCT (Entry, IP6_MLD_GROUP, Link); + if (EFI_IP6_EQUAL (MulticastAddr, &Group->Address)) { + return Group; + } + } + + return NULL; +} + +/** + Count the number of IP6 multicast groups that are mapped to the + same MAC address. Several IP6 multicast address may be mapped to + the same MAC address. + + @param[in] MldCtrl The MLD control block to search in. + @param[in] Mac The MAC address to search. + + @return The number of the IP6 multicast group that mapped to the same + multicast group Mac. + +**/ +INTN +Ip6FindMac ( + IN IP6_MLD_SERVICE_DATA *MldCtrl, + IN EFI_MAC_ADDRESS *Mac + ) +{ + LIST_ENTRY *Entry; + IP6_MLD_GROUP *Group; + INTN Count; + + Count = 0; + + NET_LIST_FOR_EACH (Entry, &MldCtrl->Groups) { + Group = NET_LIST_USER_STRUCT (Entry, IP6_MLD_GROUP, Link); + + if (NET_MAC_EQUAL (&Group->Mac, Mac, sizeof (EFI_MAC_ADDRESS))) { + Count++; + } + } + + return Count; +} + +/** + Generate MLD report message and send it out to MulticastAddr. + + @param[in] IpSb The IP service to send the packet. + @param[in] Interface The IP interface to send the packet. + If NULL, a system interface will be selected. + @param[in] MulticastAddr The specific IPv6 multicast address to which + the message sender is listening. + + @retval EFI_OUT_OF_RESOURCES There are not sufficient resources to complete the + operation. + @retval EFI_SUCCESS The MLD report message was successfully sent out. + +**/ +EFI_STATUS +Ip6SendMldReport ( + IN IP6_SERVICE *IpSb, + IN IP6_INTERFACE *Interface OPTIONAL, + IN EFI_IPv6_ADDRESS *MulticastAddr + ) +{ + IP6_MLD_HEAD *MldHead; + NET_BUF *Packet; + EFI_IP6_HEADER Head; + UINT16 PayloadLen; + UINTN OptionLen; + UINT8 *Options; + EFI_STATUS Status; + UINT16 HeadChecksum; + UINT16 PseudoChecksum; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + ASSERT (MulticastAddr != NULL && IP6_IS_MULTICAST (MulticastAddr)); + + // + // Generate the packet to be sent + // IPv6 basic header + Hop by Hop option + MLD message + // + + OptionLen = 0; + Status = Ip6FillHopByHop (NULL, &OptionLen, IP6_ICMP); + ASSERT (Status == EFI_BUFFER_TOO_SMALL); + + PayloadLen = (UINT16) (OptionLen + sizeof (IP6_MLD_HEAD)); + Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Create the basic IPv6 header. + // RFC3590: Use link-local address as source address if it is available, + // otherwise use the unspecified address. + // + Head.FlowLabelL = 0; + Head.FlowLabelH = 0; + Head.PayloadLength = HTONS (PayloadLen); + Head.NextHeader = IP6_HOP_BY_HOP; + Head.HopLimit = 1; + IP6_COPY_ADDRESS (&Head.DestinationAddress, MulticastAddr); + + // + // If Link-Local address is not ready, we use unspecified address. + // + IP6_COPY_ADDRESS (&Head.SourceAddress, &IpSb->LinkLocalAddr); + + NetbufReserve (Packet, sizeof (EFI_IP6_HEADER)); + + // + // Fill a IPv6 Router Alert option in a Hop-by-Hop Options Header + // + Options = NetbufAllocSpace (Packet, (UINT32) OptionLen, FALSE); + ASSERT (Options != NULL); + Status = Ip6FillHopByHop (Options, &OptionLen, IP6_ICMP); + if (EFI_ERROR (Status)) { + NetbufFree (Packet); + Packet = NULL; + return Status; + } + + // + // Fill in MLD message - Report + // + MldHead = (IP6_MLD_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_MLD_HEAD), FALSE); + ASSERT (MldHead != NULL); + ZeroMem (MldHead, sizeof (IP6_MLD_HEAD)); + MldHead->Head.Type = ICMP_V6_LISTENER_REPORT; + MldHead->Head.Code = 0; + IP6_COPY_ADDRESS (&MldHead->Group, MulticastAddr); + + HeadChecksum = NetblockChecksum ((UINT8 *) MldHead, sizeof (IP6_MLD_HEAD)); + PseudoChecksum = NetIp6PseudoHeadChecksum ( + &Head.SourceAddress, + &Head.DestinationAddress, + IP6_ICMP, + sizeof (IP6_MLD_HEAD) + ); + + MldHead->Head.Checksum = (UINT16) ~NetAddChecksum (HeadChecksum, PseudoChecksum); + + // + // Transmit the packet + // + return Ip6Output (IpSb, Interface, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL); +} + +/** + Generate MLD Done message and send it out to MulticastAddr. + + @param[in] IpSb The IP service to send the packet. + @param[in] MulticastAddr The specific IPv6 multicast address to which + the message sender is ceasing to listen. + + @retval EFI_OUT_OF_RESOURCES There are not sufficient resources to complete the + operation. + @retval EFI_SUCCESS The MLD report message was successfully sent out. + +**/ +EFI_STATUS +Ip6SendMldDone ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *MulticastAddr + ) +{ + IP6_MLD_HEAD *MldHead; + NET_BUF *Packet; + EFI_IP6_HEADER Head; + UINT16 PayloadLen; + UINTN OptionLen; + UINT8 *Options; + EFI_STATUS Status; + EFI_IPv6_ADDRESS Destination; + UINT16 HeadChecksum; + UINT16 PseudoChecksum; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + ASSERT (MulticastAddr != NULL && IP6_IS_MULTICAST (MulticastAddr)); + + // + // Generate the packet to be sent + // IPv6 basic header + Hop by Hop option + MLD message + // + + OptionLen = 0; + Status = Ip6FillHopByHop (NULL, &OptionLen, IP6_ICMP); + ASSERT (Status == EFI_BUFFER_TOO_SMALL); + + PayloadLen = (UINT16) (OptionLen + sizeof (IP6_MLD_HEAD)); + Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Create the basic IPv6 header. + // + Head.FlowLabelL = 0; + Head.FlowLabelH = 0; + Head.PayloadLength = HTONS (PayloadLen); + Head.NextHeader = IP6_HOP_BY_HOP; + Head.HopLimit = 1; + + // + // If Link-Local address is not ready, we use unspecified address. + // + IP6_COPY_ADDRESS (&Head.SourceAddress, &IpSb->LinkLocalAddr); + + Ip6SetToAllNodeMulticast (TRUE, IP6_LINK_LOCAL_SCOPE, &Destination); + IP6_COPY_ADDRESS (&Head.DestinationAddress, &Destination); + + NetbufReserve (Packet, sizeof (EFI_IP6_HEADER)); + + // + // Fill a IPv6 Router Alert option in a Hop-by-Hop Options Header + // + Options = NetbufAllocSpace (Packet, (UINT32) OptionLen, FALSE); + ASSERT (Options != NULL); + Status = Ip6FillHopByHop (Options, &OptionLen, IP6_ICMP); + if (EFI_ERROR (Status)) { + NetbufFree (Packet); + Packet = NULL; + return Status; + } + + // + // Fill in MLD message - Done + // + MldHead = (IP6_MLD_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_MLD_HEAD), FALSE); + ASSERT (MldHead != NULL); + ZeroMem (MldHead, sizeof (IP6_MLD_HEAD)); + MldHead->Head.Type = ICMP_V6_LISTENER_DONE; + MldHead->Head.Code = 0; + IP6_COPY_ADDRESS (&MldHead->Group, MulticastAddr); + + HeadChecksum = NetblockChecksum ((UINT8 *) MldHead, sizeof (IP6_MLD_HEAD)); + PseudoChecksum = NetIp6PseudoHeadChecksum ( + &Head.SourceAddress, + &Head.DestinationAddress, + IP6_ICMP, + sizeof (IP6_MLD_HEAD) + ); + + MldHead->Head.Checksum = (UINT16) ~NetAddChecksum (HeadChecksum, PseudoChecksum); + + // + // Transmit the packet + // + return Ip6Output (IpSb, NULL, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL); +} + +/** + Init the MLD data of the IP6 service instance. Configure + MNP to receive ALL SYSTEM multicast. + + @param[in] IpSb The IP6 service whose MLD is to be initialized. + + @retval EFI_OUT_OF_RESOURCES There are not sufficient resourcet to complete the + operation. + @retval EFI_SUCCESS The MLD module successfully initialized. + +**/ +EFI_STATUS +Ip6InitMld ( + IN IP6_SERVICE *IpSb + ) +{ + EFI_IPv6_ADDRESS AllNodes; + IP6_MLD_GROUP *Group; + EFI_STATUS Status; + + // + // Join the link-scope all-nodes multicast address (FF02::1). + // This address is started in Idle Listener state and never transitions to + // another state, and never sends a Report or Done for that address. + // + + Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &AllNodes); + + Group = Ip6CreateMldEntry (IpSb, &AllNodes, (UINT32) IP6_INFINIT_LIFETIME); + if (Group == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = Ip6GetMulticastMac (IpSb->Mnp, &AllNodes, &Group->Mac); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + // + // Configure MNP to receive all-nodes multicast + // + Status = IpSb->Mnp->Groups (IpSb->Mnp, TRUE, &Group->Mac); + if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) { + goto ERROR; + } + + return EFI_SUCCESS; + +ERROR: + RemoveEntryList (&Group->Link); + FreePool (Group); + return Status; +} + +/** + Add a group address to the array of group addresses. + The caller should make sure that no duplicated address + existed in the array. + + @param[in, out] IpInstance Points to an IP6_PROTOCOL instance. + @param[in] Group The IP6 multicast address to add. + + @retval EFI_OUT_OF_RESOURCES There are not sufficient resources to complete + the operation. + @retval EFI_SUCESS The address is added to the group address array. + +**/ +EFI_STATUS +Ip6CombineGroups ( + IN OUT IP6_PROTOCOL *IpInstance, + IN EFI_IPv6_ADDRESS *Group + ) +{ + EFI_IPv6_ADDRESS *GroupList; + + NET_CHECK_SIGNATURE (IpInstance, IP6_PROTOCOL_SIGNATURE); + ASSERT (Group != NULL && IP6_IS_MULTICAST (Group)); + + IpInstance->GroupCount++; + + GroupList = AllocatePool (IpInstance->GroupCount * sizeof (EFI_IPv6_ADDRESS)); + if (GroupList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if (IpInstance->GroupCount > 1) { + ASSERT (IpInstance->GroupList != NULL); + + CopyMem ( + GroupList, + IpInstance->GroupList, + (IpInstance->GroupCount - 1) * sizeof (EFI_IPv6_ADDRESS) + ); + + FreePool (IpInstance->GroupList); + } + + IP6_COPY_ADDRESS (GroupList + (IpInstance->GroupCount - 1), Group); + + IpInstance->GroupList = GroupList; + + return EFI_SUCCESS; +} + +/** + Remove a group address from the array of group addresses. + Although the function doesn't assume the byte order of Group, + the network byte order is used by the caller. + + @param[in, out] IpInstance Points to an IP6_PROTOCOL instance. + @param[in] Group The IP6 multicast address to remove. + + @retval EFI_NOT_FOUND Cannot find the to be removed group address. + @retval EFI_SUCCESS The group address was successfully removed. + +**/ +EFI_STATUS +Ip6RemoveGroup ( + IN OUT IP6_PROTOCOL *IpInstance, + IN EFI_IPv6_ADDRESS *Group + ) +{ + UINT32 Index; + UINT32 Count; + + Count = IpInstance->GroupCount; + + for (Index = 0; Index < Count; Index++) { + if (EFI_IP6_EQUAL (IpInstance->GroupList + Index, Group)) { + break; + } + } + + if (Index == Count) { + return EFI_NOT_FOUND; + } + + while (Index < Count - 1) { + IP6_COPY_ADDRESS (IpInstance->GroupList + Index, IpInstance->GroupList + Index + 1); + Index++; + } + + ASSERT (IpInstance->GroupCount > 0); + IpInstance->GroupCount--; + + return EFI_SUCCESS; +} + +/** + Join the multicast group on behalf of this IP6 service binding instance. + + @param[in] IpSb The IP6 service binding instance. + @param[in] Interface Points to an IP6_INTERFACE structure. + @param[in] Address The group address to join. + + @retval EFI_SUCCESS Successfully join the multicast group. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval Others Failed to join the multicast group. + +**/ +EFI_STATUS +Ip6JoinGroup ( + IN IP6_SERVICE *IpSb, + IN IP6_INTERFACE *Interface, + IN EFI_IPv6_ADDRESS *Address + ) +{ + IP6_MLD_GROUP *Group; + EFI_STATUS Status; + + Group = Ip6FindMldEntry (IpSb, Address); + if (Group != NULL) { + Group->RefCnt++; + return EFI_SUCCESS; + } + + // + // Repeat the report once or twcie after short delays [Unsolicited Report Interval] (default:10s) + // Simulate this operation as a Multicast-Address-Specific Query was received for that addresss. + // + Group = Ip6CreateMldEntry (IpSb, Address, IP6_UNSOLICITED_REPORT_INTERVAL); + if (Group == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Group->SendByUs = TRUE; + + Status = Ip6GetMulticastMac (IpSb->Mnp, Address, &Group->Mac); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = IpSb->Mnp->Groups (IpSb->Mnp, TRUE, &Group->Mac); + if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) { + goto ERROR; + } + + // + // Send unsolicited report when a node starts listening to a multicast address + // + Status = Ip6SendMldReport (IpSb, Interface, Address); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + return EFI_SUCCESS; + +ERROR: + RemoveEntryList (&Group->Link); + FreePool (Group); + return Status; +} + +/** + Leave the IP6 multicast group. + + @param[in] IpSb The IP6 service binding instance. + @param[in] Address The group address to leave. + + @retval EFI_NOT_FOUND The IP6 service instance isn't in the group. + @retval EFI_SUCCESS Successfully leave the multicast group.. + @retval Others Failed to leave the multicast group. + +**/ +EFI_STATUS +Ip6LeaveGroup ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Address + ) +{ + IP6_MLD_GROUP *Group; + EFI_STATUS Status; + + Group = Ip6FindMldEntry (IpSb, Address); + if (Group == NULL) { + return EFI_NOT_FOUND; + } + + // + // If more than one instance is in the group, decrease + // the RefCnt then return. + // + if ((Group->RefCnt > 0) && (--Group->RefCnt > 0)) { + return EFI_SUCCESS; + } + + // + // If multiple IP6 group addresses are mapped to the same + // multicast MAC address, don't configure the MNP to leave + // the MAC. + // + if (Ip6FindMac (&IpSb->MldCtrl, &Group->Mac) == 1) { + Status = IpSb->Mnp->Groups (IpSb->Mnp, FALSE, &Group->Mac); + if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) { + return Status; + } + } + + // + // Send a leave report if we are the last node to report + // + if (Group->SendByUs) { + Status = Ip6SendMldDone (IpSb, Address); + if (EFI_ERROR (Status)) { + return Status; + } + } + + RemoveEntryList (&Group->Link); + FreePool (Group); + + return EFI_SUCCESS; +} + +/** + Worker function for EfiIp6Groups(). The caller + should make sure that the parameters are valid. + + @param[in] IpInstance The IP6 child to change the setting. + @param[in] JoinFlag TRUE to join the group, otherwise leave it. + @param[in] GroupAddress The target group address. If NULL, leave all + the group addresses. + + @retval EFI_ALREADY_STARTED Wants to join the group, but is already a member of it + @retval EFI_OUT_OF_RESOURCES Failed to allocate sufficient resources. + @retval EFI_DEVICE_ERROR Failed to set the group configuraton. + @retval EFI_SUCCESS Successfully updated the group setting. + @retval EFI_NOT_FOUND Try to leave the group which it isn't a member. + +**/ +EFI_STATUS +Ip6Groups ( + IN IP6_PROTOCOL *IpInstance, + IN BOOLEAN JoinFlag, + IN EFI_IPv6_ADDRESS *GroupAddress OPTIONAL + ) +{ + EFI_STATUS Status; + IP6_SERVICE *IpSb; + UINT32 Index; + EFI_IPv6_ADDRESS *Group; + + IpSb = IpInstance->Service; + + if (JoinFlag) { + ASSERT (GroupAddress != NULL); + + for (Index = 0; Index < IpInstance->GroupCount; Index++) { + if (EFI_IP6_EQUAL (IpInstance->GroupList + Index, GroupAddress)) { + return EFI_ALREADY_STARTED; + } + } + + Status = Ip6JoinGroup (IpSb, IpInstance->Interface, GroupAddress); + if (!EFI_ERROR (Status)) { + return Ip6CombineGroups (IpInstance, GroupAddress); + } + + return Status; + } + + // + // Leave the group. Leave all the groups if GroupAddress is NULL. + // + for (Index = IpInstance->GroupCount; Index > 0; Index--) { + Group = IpInstance->GroupList + (Index - 1); + + if ((GroupAddress == NULL) || EFI_IP6_EQUAL (Group, GroupAddress)) { + Status = Ip6LeaveGroup (IpInstance->Service, Group); + if (EFI_ERROR (Status)) { + return Status; + } + + Ip6RemoveGroup (IpInstance, Group); + + if (IpInstance->GroupCount == 0) { + ASSERT (Index == 1); + FreePool (IpInstance->GroupList); + IpInstance->GroupList = NULL; + } + + if (GroupAddress != NULL) { + return EFI_SUCCESS; + } + } + } + + return ((GroupAddress != NULL) ? EFI_NOT_FOUND : EFI_SUCCESS); +} + +/** + Set a random value of the delay timer for the multicast address from the range + [0, Maximum Response Delay]. If a timer for any address is already + running, it is reset to the new random value only if the requested + Maximum Response Delay is less than the remaining value of the + running timer. If the Query packet specifies a Maximum Response + Delay of zero, each timer is effectively set to zero, and the action + specified below for timer expiration is performed immediately. + + @param[in] IpSb The IP6 service binding instance. + @param[in] MaxRespDelay The Maximum Response Delay, in milliseconds. + @param[in] MulticastAddr The multicast address. + @param[in, out] Group Points to a IP6_MLD_GROUP list entry node. + + @retval EFI_SUCCESS The delay timer is successfully updated or + timer expiration is performed immediately. + @retval Others Failed to send out MLD report message. + +**/ +EFI_STATUS +Ip6UpdateDelayTimer ( + IN IP6_SERVICE *IpSb, + IN UINT16 MaxRespDelay, + IN EFI_IPv6_ADDRESS *MulticastAddr, + IN OUT IP6_MLD_GROUP *Group + ) +{ + UINT32 Delay; + + // + // If the Query packet specifies a Maximum Response Delay of zero, perform timer + // expiration immediately. + // + if (MaxRespDelay == 0) { + Group->DelayTimer = 0; + return Ip6SendMldReport (IpSb, NULL, MulticastAddr); + } + + Delay = (UINT32) (MaxRespDelay / 1000); + + // + // Sets a delay timer to a random value selected from the range [0, Maximum Response Delay] + // If a timer is already running, resets it if the request Maximum Response Delay + // is less than the remaining value of the running timer. + // + if (Group->DelayTimer == 0 || Delay < Group->DelayTimer) { + Group->DelayTimer = Delay / 4294967295UL * NET_RANDOM (NetRandomInitSeed ()); + } + + return EFI_SUCCESS; +} + +/** + Process the Multicast Listener Query message. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the MLD query packet. + @param[in] Packet The content of the MLD query packet with IP head + removed. + + @retval EFI_SUCCESS The MLD query packet processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid. + @retval Others Failed to process the packet. + +**/ +EFI_STATUS +Ip6ProcessMldQuery ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + EFI_IPv6_ADDRESS AllNodes; + IP6_MLD_GROUP *Group; + IP6_MLD_HEAD MldPacket; + LIST_ENTRY *Entry; + EFI_STATUS Status; + + Status = EFI_INVALID_PARAMETER; + + // + // Check the validity of the packet, generic query or specific query + // + if (!NetIp6IsUnspecifiedAddr (&Head->SourceAddress) && !NetIp6IsLinkLocalAddr (&Head->SourceAddress)) { + goto Exit; + } + + if (Head->HopLimit != 1 || !IP6_IS_MULTICAST (&Head->DestinationAddress)) { + goto Exit; + } + + // + // The Packet points to MLD report raw data without Hop-By-Hop option. + // + NetbufCopy (Packet, 0, sizeof (IP6_MLD_HEAD), (UINT8 *) &MldPacket); + MldPacket.MaxRespDelay = NTOHS (MldPacket.MaxRespDelay); + + Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &AllNodes); + if (!EFI_IP6_EQUAL (&Head->DestinationAddress, &AllNodes)) { + // + // Receives a Multicast-Address-Specific Query, check it firstly + // + if (!EFI_IP6_EQUAL (&Head->DestinationAddress, &MldPacket.Group)) { + goto Exit; + } + // + // The node is not listening but it receives the specific query. Just return. + // + Group = Ip6FindMldEntry (IpSb, &MldPacket.Group); + if (Group == NULL) { + Status = EFI_SUCCESS; + goto Exit; + } + + Status = Ip6UpdateDelayTimer ( + IpSb, + MldPacket.MaxRespDelay, + &MldPacket.Group, + Group + ); + goto Exit; + } + + // + // Receives a General Query, sets a delay timer for each multicast address it is listening + // + NET_LIST_FOR_EACH (Entry, &IpSb->MldCtrl.Groups) { + Group = NET_LIST_USER_STRUCT (Entry, IP6_MLD_GROUP, Link); + Status = Ip6UpdateDelayTimer (IpSb, MldPacket.MaxRespDelay, &Group->Address, Group); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + + Status = EFI_SUCCESS; + +Exit: + NetbufFree (Packet); + return Status; +} + +/** + Process the Multicast Listener Report message. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the MLD report packet. + @param[in] Packet The content of the MLD report packet with IP head + removed. + + @retval EFI_SUCCESS The MLD report packet processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid. + +**/ +EFI_STATUS +Ip6ProcessMldReport ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_MLD_HEAD MldPacket; + IP6_MLD_GROUP *Group; + EFI_STATUS Status; + + Status = EFI_INVALID_PARAMETER; + + // + // Validate the incoming message, if invalid, drop it. + // + if (!NetIp6IsUnspecifiedAddr (&Head->SourceAddress) && !NetIp6IsLinkLocalAddr (&Head->SourceAddress)) { + goto Exit; + } + + if (Head->HopLimit != 1 || !IP6_IS_MULTICAST (&Head->DestinationAddress)) { + goto Exit; + } + + // + // The Packet points to MLD report raw data without Hop-By-Hop option. + // + NetbufCopy (Packet, 0, sizeof (IP6_MLD_HEAD), (UINT8 *) &MldPacket); + if (!EFI_IP6_EQUAL (&Head->DestinationAddress, &MldPacket.Group)) { + goto Exit; + } + + Group = Ip6FindMldEntry (IpSb, &MldPacket.Group); + if (Group == NULL) { + goto Exit; + } + + // + // The report is sent by another node, stop its own timer relates to the multicast address and clear + // + + if (!Group->SendByUs) { + Group->DelayTimer = 0; + } + + Status = EFI_SUCCESS; + +Exit: + NetbufFree (Packet); + return Status; +} + +/** + The heartbeat timer of MLD module. It sends out a solicited MLD report when + DelayTimer expires. + + @param[in] IpSb The IP6 service binding instance. + +**/ +VOID +Ip6MldTimerTicking ( + IN IP6_SERVICE *IpSb + ) +{ + IP6_MLD_GROUP *Group; + LIST_ENTRY *Entry; + + // + // Send solicited report when timer expires + // + NET_LIST_FOR_EACH (Entry, &IpSb->MldCtrl.Groups) { + Group = NET_LIST_USER_STRUCT (Entry, IP6_MLD_GROUP, Link); + if ((Group->DelayTimer > 0) && (--Group->DelayTimer == 0)) { + Ip6SendMldReport (IpSb, NULL, &Group->Address); + } + } +} + diff --git a/NetworkPkg/Ip6Dxe/Ip6Mld.h b/NetworkPkg/Ip6Dxe/Ip6Mld.h new file mode 100644 index 000000000..d907a15c6 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Mld.h @@ -0,0 +1,192 @@ +/** @file + Multicast Listener Discovery support routines. + + Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP6_MLD_H__ +#define __EFI_IP6_MLD_H__ + +#define IP6_UNSOLICITED_REPORT_INTERVAL 10 + +#pragma pack(1) +typedef struct { + IP6_ICMP_HEAD Head; + UINT16 MaxRespDelay; + UINT16 Reserved; + EFI_IPv6_ADDRESS Group; +} IP6_MLD_HEAD; +#pragma pack() + +// +// The status of multicast group. It isn't necessary to maintain +// explicit state of host state diagram. A group with finity +// DelayTime (less than 0xffffffff) is in "delaying listener" state. otherwise, it is in +// "idle listener" state. +// +typedef struct { + LIST_ENTRY Link; + INTN RefCnt; + EFI_IPv6_ADDRESS Address; + UINT32 DelayTimer; + BOOLEAN SendByUs; + EFI_MAC_ADDRESS Mac; +} IP6_MLD_GROUP; + +// +// The MLD status. Each IP6 service instance has a MLD_SERVICE_DATA +// attached. The Mldv1QuerySeen remember whether the server on this +// connected network is v1 or v2. +// +typedef struct { + INTN Mldv1QuerySeen; + LIST_ENTRY Groups; +} IP6_MLD_SERVICE_DATA; + +/** + Search a IP6_MLD_GROUP list entry node from a list array. + + @param[in] IpSb Points to an IP6 service binding instance. + @param[in] MulticastAddr The IPv6 multicast address to be searched. + + @return The found IP6_ML_GROUP list entry or NULL. + +**/ +IP6_MLD_GROUP * +Ip6FindMldEntry ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *MulticastAddr + ); + +/** + Init the MLD data of the IP6 service instance, configure + MNP to receive ALL SYSTEM multicasts. + + @param[in] IpSb The IP6 service whose MLD is to be initialized. + + @retval EFI_OUT_OF_RESOURCES There are not sufficient resources to complete the + operation. + @retval EFI_SUCCESS The MLD module successfully initialized. + +**/ +EFI_STATUS +Ip6InitMld ( + IN IP6_SERVICE *IpSb + ); + +/** + Join the multicast group on behalf of this IP6 service binding instance. + + @param[in] IpSb The IP6 service binding instance. + @param[in] Interface Points to an IP6_INTERFACE structure. + @param[in] Address The group address to join. + + @retval EFI_SUCCESS Successfully joined the multicast group. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval Others Failed to join the multicast group. + +**/ +EFI_STATUS +Ip6JoinGroup ( + IN IP6_SERVICE *IpSb, + IN IP6_INTERFACE *Interface, + IN EFI_IPv6_ADDRESS *Address + ); + +/** + Leave the IP6 multicast group. + + @param[in] IpSb The IP6 service binding instance. + @param[in] Address The group address to leave. + + @retval EFI_NOT_FOUND The IP6 service instance isn't in the group. + @retval EFI_SUCCESS Successfully left the multicast group. + @retval Others Failed to leave the multicast group. + +**/ +EFI_STATUS +Ip6LeaveGroup ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Address + ); + +/** + Worker function for EfiIp6Groups(). The caller + should verify that the parameters are valid. + + @param[in] IpInstance The IP6 child to change the setting. + @param[in] JoinFlag TRUE to join the group, otherwise leave it. + @param[in] GroupAddress The target group address. If NULL, leave all + the group addresses. + + @retval EFI_ALREADY_STARTED Wants to join the group, but is already a member of it. + @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources. + @retval EFI_DEVICE_ERROR Failed to set the group configuraton. + @retval EFI_SUCCESS Successfully updated the group setting. + @retval EFI_NOT_FOUND Tried to leave a group of whom it isn't a member. + +**/ +EFI_STATUS +Ip6Groups ( + IN IP6_PROTOCOL *IpInstance, + IN BOOLEAN JoinFlag, + IN EFI_IPv6_ADDRESS *GroupAddress OPTIONAL + ); + +/** + Process the Multicast Listener Query message. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the MLD query packet. + @param[in] Packet The content of the MLD query packet with IP head + removed. + + @retval EFI_SUCCESS The MLD query packet processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid. + @retval Others Failed to process the packet. + +**/ +EFI_STATUS +Ip6ProcessMldQuery ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ); + +/** + Process the Multicast Listener Report message. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the MLD report packet. + @param[in] Packet The content of the MLD report packet with IP head + removed. + + @retval EFI_SUCCESS The MLD report packet processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid. + +**/ +EFI_STATUS +Ip6ProcessMldReport ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ); + + +/** + The heartbeat timer of the MLD module. It sends out solicited MLD report when + DelayTimer expires. + + @param[in] IpSb The IP6 service binding instance. + +**/ +VOID +Ip6MldTimerTicking ( + IN IP6_SERVICE *IpSb + ); + +#endif + diff --git a/NetworkPkg/Ip6Dxe/Ip6Nd.c b/NetworkPkg/Ip6Dxe/Ip6Nd.c new file mode 100644 index 000000000..67d7022a7 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Nd.c @@ -0,0 +1,3149 @@ +/** @file + Implementation of Neighbor Discovery support routines. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +EFI_MAC_ADDRESS mZeroMacAddress; + +/** + Update the ReachableTime in IP6 service binding instance data, in milliseconds. + + @param[in, out] IpSb Points to the IP6_SERVICE. + +**/ +VOID +Ip6UpdateReachableTime ( + IN OUT IP6_SERVICE *IpSb + ) +{ + UINT32 Random; + + Random = (NetRandomInitSeed () / 4294967295UL) * IP6_RANDOM_FACTOR_SCALE; + Random = Random + IP6_MIN_RANDOM_FACTOR_SCALED; + IpSb->ReachableTime = (IpSb->BaseReachableTime * Random) / IP6_RANDOM_FACTOR_SCALE; +} + +/** + Build a array of EFI_IP6_NEIGHBOR_CACHE to be returned to the caller. The number + of EFI_IP6_NEIGHBOR_CACHE is also returned. + + @param[in] IpInstance The pointer to IP6_PROTOCOL instance. + @param[out] NeighborCount The number of returned neighbor cache entries. + @param[out] NeighborCache The pointer to the array of EFI_IP6_NEIGHBOR_CACHE. + + @retval EFI_SUCCESS The EFI_IP6_NEIGHBOR_CACHE successfully built. + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the route table. + +**/ +EFI_STATUS +Ip6BuildEfiNeighborCache ( + IN IP6_PROTOCOL *IpInstance, + OUT UINT32 *NeighborCount, + OUT EFI_IP6_NEIGHBOR_CACHE **NeighborCache + ) +{ + IP6_NEIGHBOR_ENTRY *Neighbor; + LIST_ENTRY *Entry; + IP6_SERVICE *IpSb; + UINT32 Count; + EFI_IP6_NEIGHBOR_CACHE *EfiNeighborCache; + EFI_IP6_NEIGHBOR_CACHE *NeighborCacheTmp; + + NET_CHECK_SIGNATURE (IpInstance, IP6_PROTOCOL_SIGNATURE); + ASSERT (NeighborCount != NULL && NeighborCache != NULL); + + IpSb = IpInstance->Service; + Count = 0; + + NET_LIST_FOR_EACH (Entry, &IpSb->NeighborTable) { + Count++; + } + + if (Count == 0) { + return EFI_SUCCESS; + } + + NeighborCacheTmp = AllocatePool (Count * sizeof (EFI_IP6_NEIGHBOR_CACHE)); + if (NeighborCacheTmp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + *NeighborCount = Count; + Count = 0; + + NET_LIST_FOR_EACH (Entry, &IpSb->NeighborTable) { + Neighbor = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, Link); + + EfiNeighborCache = NeighborCacheTmp + Count; + + EfiNeighborCache->State = Neighbor->State; + IP6_COPY_ADDRESS (&EfiNeighborCache->Neighbor, &Neighbor->Neighbor); + IP6_COPY_LINK_ADDRESS (&EfiNeighborCache->LinkAddress, &Neighbor->LinkAddress); + + Count++; + } + + ASSERT (*NeighborCount == Count); + *NeighborCache = NeighborCacheTmp; + + return EFI_SUCCESS; +} + +/** + Build a array of EFI_IP6_ADDRESS_INFO to be returned to the caller. The number + of prefix entries is also returned. + + @param[in] IpInstance The pointer to IP6_PROTOCOL instance. + @param[out] PrefixCount The number of returned prefix entries. + @param[out] PrefixTable The pointer to the array of PrefixTable. + + @retval EFI_SUCCESS The prefix table successfully built. + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the prefix table. + +**/ +EFI_STATUS +Ip6BuildPrefixTable ( + IN IP6_PROTOCOL *IpInstance, + OUT UINT32 *PrefixCount, + OUT EFI_IP6_ADDRESS_INFO **PrefixTable + ) +{ + LIST_ENTRY *Entry; + IP6_SERVICE *IpSb; + UINT32 Count; + IP6_PREFIX_LIST_ENTRY *PrefixList; + EFI_IP6_ADDRESS_INFO *EfiPrefix; + EFI_IP6_ADDRESS_INFO *PrefixTableTmp; + + NET_CHECK_SIGNATURE (IpInstance, IP6_PROTOCOL_SIGNATURE); + ASSERT (PrefixCount != NULL && PrefixTable != NULL); + + IpSb = IpInstance->Service; + Count = 0; + + NET_LIST_FOR_EACH (Entry, &IpSb->OnlinkPrefix) { + Count++; + } + + if (Count == 0) { + return EFI_SUCCESS; + } + + PrefixTableTmp = AllocatePool (Count * sizeof (EFI_IP6_ADDRESS_INFO)); + if (PrefixTableTmp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + *PrefixCount = Count; + Count = 0; + + NET_LIST_FOR_EACH (Entry, &IpSb->OnlinkPrefix) { + PrefixList = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link); + EfiPrefix = PrefixTableTmp + Count; + IP6_COPY_ADDRESS (&EfiPrefix->Address, &PrefixList->Prefix); + EfiPrefix->PrefixLength = PrefixList->PrefixLength; + + Count++; + } + + ASSERT (*PrefixCount == Count); + *PrefixTable = PrefixTableTmp; + + return EFI_SUCCESS; +} + +/** + Allocate and initialize a IP6 prefix list entry. + + @param[in] IpSb The pointer to IP6_SERVICE instance. + @param[in] OnLinkOrAuto If TRUE, the entry is created for the on link prefix list. + Otherwise, it is created for the autoconfiguration prefix list. + @param[in] ValidLifetime The length of time in seconds that the prefix + is valid for the purpose of on-link determination. + @param[in] PreferredLifetime The length of time in seconds that addresses + generated from the prefix via stateless address + autoconfiguration remain preferred. + @param[in] PrefixLength The prefix length of the Prefix. + @param[in] Prefix The prefix address. + + @return NULL if it failed to allocate memory for the prefix node. Otherwise, point + to the created or existing prefix list entry. + +**/ +IP6_PREFIX_LIST_ENTRY * +Ip6CreatePrefixListEntry ( + IN IP6_SERVICE *IpSb, + IN BOOLEAN OnLinkOrAuto, + IN UINT32 ValidLifetime, + IN UINT32 PreferredLifetime, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *Prefix + ) +{ + IP6_PREFIX_LIST_ENTRY *PrefixEntry; + IP6_ROUTE_ENTRY *RtEntry; + LIST_ENTRY *ListHead; + LIST_ENTRY *Entry; + IP6_PREFIX_LIST_ENTRY *TmpPrefixEntry; + + if (Prefix == NULL || PreferredLifetime > ValidLifetime || PrefixLength > IP6_PREFIX_MAX) { + return NULL; + } + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + PrefixEntry = Ip6FindPrefixListEntry ( + IpSb, + OnLinkOrAuto, + PrefixLength, + Prefix + ); + if (PrefixEntry != NULL) { + PrefixEntry->RefCnt ++; + return PrefixEntry; + } + + PrefixEntry = AllocatePool (sizeof (IP6_PREFIX_LIST_ENTRY)); + if (PrefixEntry == NULL) { + return NULL; + } + + PrefixEntry->RefCnt = 1; + PrefixEntry->ValidLifetime = ValidLifetime; + PrefixEntry->PreferredLifetime = PreferredLifetime; + PrefixEntry->PrefixLength = PrefixLength; + IP6_COPY_ADDRESS (&PrefixEntry->Prefix, Prefix); + + ListHead = OnLinkOrAuto ? &IpSb->OnlinkPrefix : &IpSb->AutonomousPrefix; + + // + // Create a direct route entry for on-link prefix and insert to route area. + // + if (OnLinkOrAuto) { + RtEntry = Ip6CreateRouteEntry (Prefix, PrefixLength, NULL); + if (RtEntry == NULL) { + FreePool (PrefixEntry); + return NULL; + } + + RtEntry->Flag = IP6_DIRECT_ROUTE; + InsertHeadList (&IpSb->RouteTable->RouteArea[PrefixLength], &RtEntry->Link); + IpSb->RouteTable->TotalNum++; + } + + // + // Insert the prefix entry in the order that a prefix with longer prefix length + // is put ahead in the list. + // + NET_LIST_FOR_EACH (Entry, ListHead) { + TmpPrefixEntry = NET_LIST_USER_STRUCT(Entry, IP6_PREFIX_LIST_ENTRY, Link); + + if (TmpPrefixEntry->PrefixLength < PrefixEntry->PrefixLength) { + break; + } + } + + NetListInsertBefore (Entry, &PrefixEntry->Link); + + return PrefixEntry; +} + +/** + Destroy a IP6 prefix list entry. + + @param[in] IpSb The pointer to IP6_SERVICE instance. + @param[in] PrefixEntry The to be destroyed prefix list entry. + @param[in] OnLinkOrAuto If TRUE, the entry is removed from on link prefix list. + Otherwise remove from autoconfiguration prefix list. + @param[in] ImmediateDelete If TRUE, remove the entry directly. + Otherwise, check the reference count to see whether + it should be removed. + +**/ +VOID +Ip6DestroyPrefixListEntry ( + IN IP6_SERVICE *IpSb, + IN IP6_PREFIX_LIST_ENTRY *PrefixEntry, + IN BOOLEAN OnLinkOrAuto, + IN BOOLEAN ImmediateDelete + ) +{ + LIST_ENTRY *Entry; + IP6_INTERFACE *IpIf; + EFI_STATUS Status; + + if ((!ImmediateDelete) && (PrefixEntry->RefCnt > 0) && ((--PrefixEntry->RefCnt) > 0)) { + return ; + } + + if (OnLinkOrAuto) { + // + // Remove the direct route for onlink prefix from route table. + // + do { + Status = Ip6DelRoute ( + IpSb->RouteTable, + &PrefixEntry->Prefix, + PrefixEntry->PrefixLength, + NULL + ); + } while (Status != EFI_NOT_FOUND); + } else { + // + // Remove the corresponding addresses generated from this autonomous prefix. + // + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE); + + Ip6RemoveAddr (IpSb, &IpIf->AddressList, &IpIf->AddressCount, &PrefixEntry->Prefix, PrefixEntry->PrefixLength); + } + } + + RemoveEntryList (&PrefixEntry->Link); + FreePool (PrefixEntry); +} + +/** + Search the list array to find an IP6 prefix list entry. + + @param[in] IpSb The pointer to IP6_SERVICE instance. + @param[in] OnLinkOrAuto If TRUE, the search the link prefix list, + Otherwise search the autoconfiguration prefix list. + @param[in] PrefixLength The prefix length of the Prefix + @param[in] Prefix The prefix address. + + @return NULL if cannot find the IP6 prefix list entry. Otherwise, return the + pointer to the IP6 prefix list entry. + +**/ +IP6_PREFIX_LIST_ENTRY * +Ip6FindPrefixListEntry ( + IN IP6_SERVICE *IpSb, + IN BOOLEAN OnLinkOrAuto, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *Prefix + ) +{ + IP6_PREFIX_LIST_ENTRY *PrefixList; + LIST_ENTRY *Entry; + LIST_ENTRY *ListHead; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + ASSERT (Prefix != NULL); + + if (OnLinkOrAuto) { + ListHead = &IpSb->OnlinkPrefix; + } else { + ListHead = &IpSb->AutonomousPrefix; + } + + NET_LIST_FOR_EACH (Entry, ListHead) { + PrefixList = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link); + if (PrefixLength != 255) { + // + // Perform exactly prefix match. + // + if (PrefixList->PrefixLength == PrefixLength && + NetIp6IsNetEqual (&PrefixList->Prefix, Prefix, PrefixLength)) { + return PrefixList; + } + } else { + // + // Perform the longest prefix match. The list is already sorted with + // the longest length prefix put at the head of the list. + // + if (NetIp6IsNetEqual (&PrefixList->Prefix, Prefix, PrefixList->PrefixLength)) { + return PrefixList; + } + } + } + + return NULL; +} + +/** + Release the resource in the prefix list table, and destroy the list entry and + corresponding addresses or route entries. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] ListHead The list entry head of the prefix list table. + +**/ +VOID +Ip6CleanPrefixListTable ( + IN IP6_SERVICE *IpSb, + IN LIST_ENTRY *ListHead + ) +{ + IP6_PREFIX_LIST_ENTRY *PrefixList; + BOOLEAN OnLink; + + OnLink = (BOOLEAN) (ListHead == &IpSb->OnlinkPrefix); + + while (!IsListEmpty (ListHead)) { + PrefixList = NET_LIST_HEAD (ListHead, IP6_PREFIX_LIST_ENTRY, Link); + Ip6DestroyPrefixListEntry (IpSb, PrefixList, OnLink, TRUE); + } +} + +/** + Callback function when address resolution is finished. It will cancel + all the queued frames if the address resolution failed, or transmit them + if the request succeeded. + + @param[in] Context The context of the callback, a pointer to IP6_NEIGHBOR_ENTRY. + +**/ +VOID +Ip6OnArpResolved ( + IN VOID *Context + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP6_NEIGHBOR_ENTRY *ArpQue; + IP6_SERVICE *IpSb; + IP6_LINK_TX_TOKEN *Token; + EFI_STATUS Status; + BOOLEAN Sent; + + ArpQue = (IP6_NEIGHBOR_ENTRY *) Context; + if ((ArpQue == NULL) || (ArpQue->Interface == NULL)) { + return ; + } + + IpSb = ArpQue->Interface->Service; + if ((IpSb == NULL) || (IpSb->Signature != IP6_SERVICE_SIGNATURE)) { + return ; + } + + // + // ARP resolve failed for some reason. Release all the frame + // and ARP queue itself. Ip6FreeArpQue will call the frame's + // owner back. + // + if (NET_MAC_EQUAL (&ArpQue->LinkAddress, &mZeroMacAddress, IpSb->SnpMode.HwAddressSize)) { + Ip6FreeNeighborEntry (IpSb, ArpQue, FALSE, TRUE, EFI_NO_MAPPING, NULL, NULL); + return ; + } + + // + // ARP resolve succeeded, Transmit all the frame. + // + Sent = FALSE; + NET_LIST_FOR_EACH_SAFE (Entry, Next, &ArpQue->Frames) { + RemoveEntryList (Entry); + + Token = NET_LIST_USER_STRUCT (Entry, IP6_LINK_TX_TOKEN, Link); + IP6_COPY_LINK_ADDRESS (&Token->DstMac, &ArpQue->LinkAddress); + + // + // Insert the tx token before transmitting it via MNP as the FrameSentDpc + // may be called before Mnp->Transmit returns which will remove this tx + // token from the SentFrames list. Remove it from the list if the returned + // Status of Mnp->Transmit is not EFI_SUCCESS as in this case the + // FrameSentDpc won't be queued. + // + InsertTailList (&ArpQue->Interface->SentFrames, &Token->Link); + + Status = IpSb->Mnp->Transmit (IpSb->Mnp, &Token->MnpToken); + if (EFI_ERROR (Status)) { + RemoveEntryList (&Token->Link); + Token->CallBack (Token->Packet, Status, 0, Token->Context); + + Ip6FreeLinkTxToken (Token); + continue; + } else { + Sent = TRUE; + } + } + + // + // Free the ArpQue only but not the whole neighbor entry. + // + Ip6FreeNeighborEntry (IpSb, ArpQue, FALSE, FALSE, EFI_SUCCESS, NULL, NULL); + + if (Sent && (ArpQue->State == EfiNeighborStale)) { + ArpQue->State = EfiNeighborDelay; + ArpQue->Ticks = (UINT32) IP6_GET_TICKS (IP6_DELAY_FIRST_PROBE_TIME); + } +} + +/** + Allocate and initialize an IP6 neighbor cache entry. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] CallBack The callback function to be called when + address resolution is finished. + @param[in] Ip6Address Points to the IPv6 address of the neighbor. + @param[in] LinkAddress Points to the MAC address of the neighbor. + Ignored if NULL. + + @return NULL if failed to allocate memory for the neighbor cache entry. + Otherwise, point to the created neighbor cache entry. + +**/ +IP6_NEIGHBOR_ENTRY * +Ip6CreateNeighborEntry ( + IN IP6_SERVICE *IpSb, + IN IP6_ARP_CALLBACK CallBack, + IN EFI_IPv6_ADDRESS *Ip6Address, + IN EFI_MAC_ADDRESS *LinkAddress OPTIONAL + ) +{ + IP6_NEIGHBOR_ENTRY *Entry; + IP6_DEFAULT_ROUTER *DefaultRouter; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + ASSERT (Ip6Address!= NULL); + + Entry = AllocateZeroPool (sizeof (IP6_NEIGHBOR_ENTRY)); + if (Entry == NULL) { + return NULL; + } + + Entry->RefCnt = 1; + Entry->IsRouter = FALSE; + Entry->ArpFree = FALSE; + Entry->Dynamic = FALSE; + Entry->State = EfiNeighborInComplete; + Entry->Transmit = IP6_MAX_MULTICAST_SOLICIT + 1; + Entry->CallBack = CallBack; + Entry->Interface = NULL; + + InitializeListHead (&Entry->Frames); + + IP6_COPY_ADDRESS (&Entry->Neighbor, Ip6Address); + + if (LinkAddress != NULL) { + IP6_COPY_LINK_ADDRESS (&Entry->LinkAddress, LinkAddress); + } else { + IP6_COPY_LINK_ADDRESS (&Entry->LinkAddress, &mZeroMacAddress); + } + + InsertHeadList (&IpSb->NeighborTable, &Entry->Link); + + // + // If corresponding default router entry exists, establish the relationship. + // + DefaultRouter = Ip6FindDefaultRouter (IpSb, Ip6Address); + if (DefaultRouter != NULL) { + DefaultRouter->NeighborCache = Entry; + } + + return Entry; +} + +/** + Search a IP6 neighbor cache entry. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] Ip6Address Points to the IPv6 address of the neighbor. + + @return NULL if it failed to find the matching neighbor cache entry. + Otherwise, point to the found neighbor cache entry. + +**/ +IP6_NEIGHBOR_ENTRY * +Ip6FindNeighborEntry ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Ip6Address + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP6_NEIGHBOR_ENTRY *Neighbor; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + ASSERT (Ip6Address != NULL); + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->NeighborTable) { + Neighbor = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, Link); + if (EFI_IP6_EQUAL (Ip6Address, &Neighbor->Neighbor)) { + RemoveEntryList (Entry); + InsertHeadList (&IpSb->NeighborTable, Entry); + + return Neighbor; + } + } + + return NULL; +} + +/** + Free a IP6 neighbor cache entry and remove all the frames on the address + resolution queue that pass the FrameToCancel. That is, either FrameToCancel + is NULL, or it returns true for the frame. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] NeighborCache The to be free neighbor cache entry. + @param[in] SendIcmpError If TRUE, send out ICMP error. + @param[in] FullFree If TRUE, remove the neighbor cache entry. + Otherwise remove the pending frames. + @param[in] IoStatus The status returned to the cancelled frames' + callback function. + @param[in] FrameToCancel Function to select which frame to cancel. + This is an optional parameter that may be NULL. + @param[in] Context Opaque parameter to the FrameToCancel. + Ignored if FrameToCancel is NULL. + + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + @retval EFI_SUCCESS The operation finished successfully. + +**/ +EFI_STATUS +Ip6FreeNeighborEntry ( + IN IP6_SERVICE *IpSb, + IN IP6_NEIGHBOR_ENTRY *NeighborCache, + IN BOOLEAN SendIcmpError, + IN BOOLEAN FullFree, + IN EFI_STATUS IoStatus, + IN IP6_FRAME_TO_CANCEL FrameToCancel OPTIONAL, + IN VOID *Context OPTIONAL + ) +{ + IP6_LINK_TX_TOKEN *TxToken; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP6_DEFAULT_ROUTER *DefaultRouter; + + // + // If FrameToCancel fails, the token will not be released. + // To avoid the memory leak, stop this usage model. + // + if (FullFree && FrameToCancel != NULL) { + return EFI_INVALID_PARAMETER; + } + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &NeighborCache->Frames) { + TxToken = NET_LIST_USER_STRUCT (Entry, IP6_LINK_TX_TOKEN, Link); + + if (SendIcmpError && !IP6_IS_MULTICAST (&TxToken->Packet->Ip.Ip6->DestinationAddress)) { + Ip6SendIcmpError ( + IpSb, + TxToken->Packet, + NULL, + &TxToken->Packet->Ip.Ip6->SourceAddress, + ICMP_V6_DEST_UNREACHABLE, + ICMP_V6_ADDR_UNREACHABLE, + NULL + ); + } + + if ((FrameToCancel == NULL) || FrameToCancel (TxToken, Context)) { + RemoveEntryList (Entry); + TxToken->CallBack (TxToken->Packet, IoStatus, 0, TxToken->Context); + Ip6FreeLinkTxToken (TxToken); + } + } + + if (NeighborCache->ArpFree && IsListEmpty (&NeighborCache->Frames)) { + RemoveEntryList (&NeighborCache->ArpList); + NeighborCache->ArpFree = FALSE; + } + + if (FullFree) { + if (NeighborCache->IsRouter) { + DefaultRouter = Ip6FindDefaultRouter (IpSb, &NeighborCache->Neighbor); + if (DefaultRouter != NULL) { + Ip6DestroyDefaultRouter (IpSb, DefaultRouter); + } + } + + RemoveEntryList (&NeighborCache->Link); + FreePool (NeighborCache); + } + + return EFI_SUCCESS; +} + +/** + Allocate and initialize an IP6 default router entry. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] Ip6Address The IPv6 address of the default router. + @param[in] RouterLifetime The lifetime associated with the default + router, in units of seconds. + + @return NULL if it failed to allocate memory for the default router node. + Otherwise, point to the created default router node. + +**/ +IP6_DEFAULT_ROUTER * +Ip6CreateDefaultRouter ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Ip6Address, + IN UINT16 RouterLifetime + ) +{ + IP6_DEFAULT_ROUTER *Entry; + IP6_ROUTE_ENTRY *RtEntry; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + ASSERT (Ip6Address != NULL); + + Entry = AllocatePool (sizeof (IP6_DEFAULT_ROUTER)); + if (Entry == NULL) { + return NULL; + } + + Entry->RefCnt = 1; + Entry->Lifetime = RouterLifetime; + Entry->NeighborCache = Ip6FindNeighborEntry (IpSb, Ip6Address); + IP6_COPY_ADDRESS (&Entry->Router, Ip6Address); + + // + // Add a default route into route table with both Destination and PrefixLength set to zero. + // + RtEntry = Ip6CreateRouteEntry (NULL, 0, Ip6Address); + if (RtEntry == NULL) { + FreePool (Entry); + return NULL; + } + + InsertHeadList (&IpSb->RouteTable->RouteArea[0], &RtEntry->Link); + IpSb->RouteTable->TotalNum++; + + InsertTailList (&IpSb->DefaultRouterList, &Entry->Link); + + return Entry; +} + +/** + Destroy an IP6 default router entry. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] DefaultRouter The to be destroyed IP6_DEFAULT_ROUTER. + +**/ +VOID +Ip6DestroyDefaultRouter ( + IN IP6_SERVICE *IpSb, + IN IP6_DEFAULT_ROUTER *DefaultRouter + ) +{ + EFI_STATUS Status; + + RemoveEntryList (&DefaultRouter->Link); + + // + // Update the Destination Cache - all entries using the time-out router as next-hop + // should perform next-hop determination again. + // + do { + Status = Ip6DelRoute (IpSb->RouteTable, NULL, 0, &DefaultRouter->Router); + } while (Status != EFI_NOT_FOUND); + + FreePool (DefaultRouter); +} + +/** + Clean an IP6 default router list. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + +**/ +VOID +Ip6CleanDefaultRouterList ( + IN IP6_SERVICE *IpSb + ) +{ + IP6_DEFAULT_ROUTER *DefaultRouter; + + while (!IsListEmpty (&IpSb->DefaultRouterList)) { + DefaultRouter = NET_LIST_HEAD (&IpSb->DefaultRouterList, IP6_DEFAULT_ROUTER, Link); + Ip6DestroyDefaultRouter (IpSb, DefaultRouter); + } +} + +/** + Search a default router node from an IP6 default router list. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] Ip6Address The IPv6 address of the to be searched default router node. + + @return NULL if it failed to find the matching default router node. + Otherwise, point to the found default router node. + +**/ +IP6_DEFAULT_ROUTER * +Ip6FindDefaultRouter ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Ip6Address + ) +{ + LIST_ENTRY *Entry; + IP6_DEFAULT_ROUTER *DefaultRouter; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + ASSERT (Ip6Address != NULL); + + NET_LIST_FOR_EACH (Entry, &IpSb->DefaultRouterList) { + DefaultRouter = NET_LIST_USER_STRUCT (Entry, IP6_DEFAULT_ROUTER, Link); + if (EFI_IP6_EQUAL (Ip6Address, &DefaultRouter->Router)) { + return DefaultRouter; + } + } + + return NULL; +} + +/** + The function to be called after DAD (Duplicate Address Detection) is performed. + + @param[in] IsDadPassed If TRUE, the DAD operation succeed. Otherwise, the DAD operation failed. + @param[in] IpIf Points to the IP6_INTERFACE. + @param[in] DadEntry The DAD entry which already performed DAD. + +**/ +VOID +Ip6OnDADFinished ( + IN BOOLEAN IsDadPassed, + IN IP6_INTERFACE *IpIf, + IN IP6_DAD_ENTRY *DadEntry + ) +{ + IP6_SERVICE *IpSb; + IP6_ADDRESS_INFO *AddrInfo; + EFI_DHCP6_PROTOCOL *Dhcp6; + UINT16 OptBuf[4]; + EFI_DHCP6_PACKET_OPTION *Oro; + EFI_DHCP6_RETRANSMISSION InfoReqReXmit; + EFI_IPv6_ADDRESS AllNodes; + + IpSb = IpIf->Service; + AddrInfo = DadEntry->AddressInfo; + + if (IsDadPassed) { + // + // DAD succeed. + // + if (NetIp6IsLinkLocalAddr (&AddrInfo->Address)) { + ASSERT (!IpSb->LinkLocalOk); + + IP6_COPY_ADDRESS (&IpSb->LinkLocalAddr, &AddrInfo->Address); + IpSb->LinkLocalOk = TRUE; + IpIf->Configured = TRUE; + + // + // Check whether DHCP6 need to be started. + // + Dhcp6 = IpSb->Ip6ConfigInstance.Dhcp6; + + if (IpSb->Dhcp6NeedStart) { + Dhcp6->Start (Dhcp6); + IpSb->Dhcp6NeedStart = FALSE; + } + + if (IpSb->Dhcp6NeedInfoRequest) { + // + // Set the exta options to send. Here we only want the option request option + // with DNS SERVERS. + // + Oro = (EFI_DHCP6_PACKET_OPTION *) OptBuf; + Oro->OpCode = HTONS (DHCP6_OPT_ORO); + Oro->OpLen = HTONS (2); + *((UINT16 *) &Oro->Data[0]) = HTONS (DHCP6_OPT_DNS_SERVERS); + + InfoReqReXmit.Irt = 4; + InfoReqReXmit.Mrc = 64; + InfoReqReXmit.Mrt = 60; + InfoReqReXmit.Mrd = 0; + + Dhcp6->InfoRequest ( + Dhcp6, + TRUE, + Oro, + 0, + NULL, + &InfoReqReXmit, + IpSb->Ip6ConfigInstance.Dhcp6Event, + Ip6ConfigOnDhcp6Reply, + &IpSb->Ip6ConfigInstance + ); + } + + // + // Add an on-link prefix for link-local address. + // + Ip6CreatePrefixListEntry ( + IpSb, + TRUE, + (UINT32) IP6_INFINIT_LIFETIME, + (UINT32) IP6_INFINIT_LIFETIME, + IP6_LINK_LOCAL_PREFIX_LENGTH, + &IpSb->LinkLocalAddr + ); + + } else { + // + // Global scope unicast address. + // + Ip6AddAddr (IpIf, AddrInfo); + + // + // Add an on-link prefix for this address. + // + Ip6CreatePrefixListEntry ( + IpSb, + TRUE, + AddrInfo->ValidLifetime, + AddrInfo->PreferredLifetime, + AddrInfo->PrefixLength, + &AddrInfo->Address + ); + + IpIf->Configured = TRUE; + } + } else { + // + // Leave the group we joined before. + // + Ip6LeaveGroup (IpSb, &DadEntry->Destination); + } + + if (DadEntry->Callback != NULL) { + DadEntry->Callback (IsDadPassed, &AddrInfo->Address, DadEntry->Context); + } + + if (!IsDadPassed && NetIp6IsLinkLocalAddr (&AddrInfo->Address)) { + FreePool (AddrInfo); + RemoveEntryList (&DadEntry->Link); + FreePool (DadEntry); + // + // Leave link-scope all-nodes multicast address (FF02::1) + // + Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &AllNodes); + Ip6LeaveGroup (IpSb, &AllNodes); + // + // Disable IP operation since link-local address is a duplicate address. + // + IpSb->LinkLocalDadFail = TRUE; + IpSb->Mnp->Configure (IpSb->Mnp, NULL); + gBS->SetTimer (IpSb->Timer, TimerCancel, 0); + gBS->SetTimer (IpSb->FasterTimer, TimerCancel, 0); + return ; + } + + if (!IsDadPassed || NetIp6IsLinkLocalAddr (&AddrInfo->Address)) { + // + // Free the AddressInfo we hold if DAD fails or it is a link-local address. + // + FreePool (AddrInfo); + } + + RemoveEntryList (&DadEntry->Link); + FreePool (DadEntry); +} + +/** + Create a DAD (Duplicate Address Detection) entry and queue it to be performed. + + @param[in] IpIf Points to the IP6_INTERFACE. + @param[in] AddressInfo The address information which needs DAD performed. + @param[in] Callback The callback routine that will be called after DAD + is performed. This is an optional parameter that + may be NULL. + @param[in] Context The opaque parameter for a DAD callback routine. + This is an optional parameter that may be NULL. + + @retval EFI_SUCCESS The DAD entry was created and queued. + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory to complete the + operation. + + +**/ +EFI_STATUS +Ip6InitDADProcess ( + IN IP6_INTERFACE *IpIf, + IN IP6_ADDRESS_INFO *AddressInfo, + IN IP6_DAD_CALLBACK Callback OPTIONAL, + IN VOID *Context OPTIONAL + ) +{ + IP6_DAD_ENTRY *Entry; + EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS *DadXmits; + IP6_SERVICE *IpSb; + EFI_STATUS Status; + UINT32 MaxDelayTick; + + NET_CHECK_SIGNATURE (IpIf, IP6_INTERFACE_SIGNATURE); + ASSERT (AddressInfo != NULL); + + // + // Do nothing if we have already started DAD on the address. + // + if (Ip6FindDADEntry (IpIf->Service, &AddressInfo->Address, NULL) != NULL) { + return EFI_SUCCESS; + } + + Status = EFI_SUCCESS; + IpSb = IpIf->Service; + DadXmits = &IpSb->Ip6ConfigInstance.DadXmits; + + // + // Allocate the resources and insert info + // + Entry = AllocatePool (sizeof (IP6_DAD_ENTRY)); + if (Entry == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Map the incoming unicast address to solicited-node multicast address + // + Ip6CreateSNMulticastAddr (&AddressInfo->Address, &Entry->Destination); + + // + // Join in the solicited-node multicast address. + // + Status = Ip6JoinGroup (IpSb, IpIf, &Entry->Destination); + if (EFI_ERROR (Status)) { + FreePool (Entry); + return Status; + } + + Entry->Signature = IP6_DAD_ENTRY_SIGNATURE; + Entry->MaxTransmit = DadXmits->DupAddrDetectTransmits; + Entry->Transmit = 0; + Entry->Receive = 0; + MaxDelayTick = IP6_MAX_RTR_SOLICITATION_DELAY / IP6_TIMER_INTERVAL_IN_MS; + Entry->RetransTick = (MaxDelayTick * ((NET_RANDOM (NetRandomInitSeed ()) % 5) + 1)) / 5; + Entry->AddressInfo = AddressInfo; + Entry->Callback = Callback; + Entry->Context = Context; + InsertTailList (&IpIf->DupAddrDetectList, &Entry->Link); + + if (Entry->MaxTransmit == 0) { + // + // DAD is disabled on this interface, immediately mark this DAD successful. + // + Ip6OnDADFinished (TRUE, IpIf, Entry); + } + + return EFI_SUCCESS; +} + +/** + Search IP6_DAD_ENTRY from the Duplicate Address Detection List. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] Target The address information which needs DAD performed . + @param[out] Interface If not NULL, output the IP6 interface that configures + the tentative address. + + @return NULL if failed to find the matching DAD entry. + Otherwise, point to the found DAD entry. + +**/ +IP6_DAD_ENTRY * +Ip6FindDADEntry ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Target, + OUT IP6_INTERFACE **Interface OPTIONAL + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Entry2; + IP6_INTERFACE *IpIf; + IP6_DAD_ENTRY *DupAddrDetect; + IP6_ADDRESS_INFO *AddrInfo; + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link); + + NET_LIST_FOR_EACH (Entry2, &IpIf->DupAddrDetectList) { + DupAddrDetect = NET_LIST_USER_STRUCT_S (Entry2, IP6_DAD_ENTRY, Link, IP6_DAD_ENTRY_SIGNATURE); + AddrInfo = DupAddrDetect->AddressInfo; + if (EFI_IP6_EQUAL (&AddrInfo->Address, Target)) { + if (Interface != NULL) { + *Interface = IpIf; + } + return DupAddrDetect; + } + } + } + + return NULL; +} + +/** + Generate router solicit message and send it out to Destination Address or + All Router Link Local scope multicast address. + + @param[in] IpSb The IP service to send the packet. + @param[in] Interface If not NULL, points to the IP6 interface to send + the packet. + @param[in] SourceAddress If not NULL, the source address of the message. + @param[in] DestinationAddress If not NULL, the destination address of the message. + @param[in] SourceLinkAddress If not NULL, the MAC address of the source. + A source link-layer address option will be appended + to the message. + + @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the + operation. + @retval EFI_SUCCESS The router solicit message was successfully sent. + +**/ +EFI_STATUS +Ip6SendRouterSolicit ( + IN IP6_SERVICE *IpSb, + IN IP6_INTERFACE *Interface OPTIONAL, + IN EFI_IPv6_ADDRESS *SourceAddress OPTIONAL, + IN EFI_IPv6_ADDRESS *DestinationAddress OPTIONAL, + IN EFI_MAC_ADDRESS *SourceLinkAddress OPTIONAL + ) +{ + NET_BUF *Packet; + EFI_IP6_HEADER Head; + IP6_ICMP_INFORMATION_HEAD *IcmpHead; + IP6_ETHER_ADDR_OPTION *LinkLayerOption; + UINT16 PayloadLen; + IP6_INTERFACE *IpIf; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + IpIf = Interface; + if (IpIf == NULL && IpSb->DefaultInterface != NULL) { + IpIf = IpSb->DefaultInterface; + } + + // + // Generate the packet to be sent + // + + PayloadLen = (UINT16) sizeof (IP6_ICMP_INFORMATION_HEAD); + if (SourceLinkAddress != NULL) { + PayloadLen += sizeof (IP6_ETHER_ADDR_OPTION); + } + + Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Create the basic IPv6 header. + // + Head.FlowLabelL = 0; + Head.FlowLabelH = 0; + Head.PayloadLength = HTONS (PayloadLen); + Head.NextHeader = IP6_ICMP; + Head.HopLimit = IP6_HOP_LIMIT; + + if (SourceAddress != NULL) { + IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress); + } else { + ZeroMem (&Head.SourceAddress, sizeof (EFI_IPv6_ADDRESS)); + } + + + if (DestinationAddress != NULL) { + IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress); + } else { + Ip6SetToAllNodeMulticast (TRUE, IP6_LINK_LOCAL_SCOPE, &Head.DestinationAddress); + } + + NetbufReserve (Packet, sizeof (EFI_IP6_HEADER)); + + // + // Fill in the ICMP header, and Source link-layer address if contained. + // + + IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE); + ASSERT (IcmpHead != NULL); + ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD)); + IcmpHead->Head.Type = ICMP_V6_ROUTER_SOLICIT; + IcmpHead->Head.Code = 0; + + LinkLayerOption = NULL; + if (SourceLinkAddress != NULL) { + LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace ( + Packet, + sizeof (IP6_ETHER_ADDR_OPTION), + FALSE + ); + ASSERT (LinkLayerOption != NULL); + LinkLayerOption->Type = Ip6OptionEtherSource; + LinkLayerOption->Length = (UINT8) sizeof (IP6_ETHER_ADDR_OPTION); + CopyMem (LinkLayerOption->EtherAddr, SourceLinkAddress, 6); + } + + // + // Transmit the packet + // + return Ip6Output (IpSb, IpIf, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL); +} + +/** + Generate a Neighbor Advertisement message and send it out to Destination Address. + + @param[in] IpSb The IP service to send the packet. + @param[in] SourceAddress The source address of the message. + @param[in] DestinationAddress The destination address of the message. + @param[in] TargetIp6Address The target address field in the Neighbor Solicitation + message that prompted this advertisement. + @param[in] TargetLinkAddress The MAC address for the target, i.e. the sender + of the advertisement. + @param[in] IsRouter If TRUE, indicates the sender is a router. + @param[in] Override If TRUE, indicates the advertisement should override + an existing cache entry and update the MAC address. + @param[in] Solicited If TRUE, indicates the advertisement was sent + in response to a Neighbor Solicitation from + the Destination address. + + @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the + operation. + @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent. + +**/ +EFI_STATUS +Ip6SendNeighborAdvertise ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *SourceAddress, + IN EFI_IPv6_ADDRESS *DestinationAddress, + IN EFI_IPv6_ADDRESS *TargetIp6Address, + IN EFI_MAC_ADDRESS *TargetLinkAddress, + IN BOOLEAN IsRouter, + IN BOOLEAN Override, + IN BOOLEAN Solicited + ) +{ + NET_BUF *Packet; + EFI_IP6_HEADER Head; + IP6_ICMP_INFORMATION_HEAD *IcmpHead; + IP6_ETHER_ADDR_OPTION *LinkLayerOption; + EFI_IPv6_ADDRESS *Target; + UINT16 PayloadLen; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + // + // The Neighbor Advertisement message must include a Target link-layer address option + // when responding to multicast solicitation and should include such option when + // responding to unicast solicitation. It also must include such option as unsolicited + // advertisement. + // + ASSERT (DestinationAddress != NULL && TargetIp6Address != NULL && TargetLinkAddress != NULL); + + PayloadLen = (UINT16) (sizeof (IP6_ICMP_INFORMATION_HEAD) + sizeof (EFI_IPv6_ADDRESS) + sizeof (IP6_ETHER_ADDR_OPTION)); + + // + // Generate the packet to be sent + // + + Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Create the basic IPv6 header. + // + Head.FlowLabelL = 0; + Head.FlowLabelH = 0; + Head.PayloadLength = HTONS (PayloadLen); + Head.NextHeader = IP6_ICMP; + Head.HopLimit = IP6_HOP_LIMIT; + + IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress); + IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress); + + NetbufReserve (Packet, sizeof (EFI_IP6_HEADER)); + + // + // Fill in the ICMP header, Target address, and Target link-layer address. + // Set the Router flag, Solicited flag and Override flag. + // + + IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE); + ASSERT (IcmpHead != NULL); + ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD)); + IcmpHead->Head.Type = ICMP_V6_NEIGHBOR_ADVERTISE; + IcmpHead->Head.Code = 0; + + if (IsRouter) { + IcmpHead->Fourth |= IP6_IS_ROUTER_FLAG; + } + + if (Solicited) { + IcmpHead->Fourth |= IP6_SOLICITED_FLAG; + } + + if (Override) { + IcmpHead->Fourth |= IP6_OVERRIDE_FLAG; + } + + Target = (EFI_IPv6_ADDRESS *) NetbufAllocSpace (Packet, sizeof (EFI_IPv6_ADDRESS), FALSE); + ASSERT (Target != NULL); + IP6_COPY_ADDRESS (Target, TargetIp6Address); + + LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace ( + Packet, + sizeof (IP6_ETHER_ADDR_OPTION), + FALSE + ); + ASSERT (LinkLayerOption != NULL); + LinkLayerOption->Type = Ip6OptionEtherTarget; + LinkLayerOption->Length = 1; + CopyMem (LinkLayerOption->EtherAddr, TargetLinkAddress, 6); + + // + // Transmit the packet + // + return Ip6Output (IpSb, NULL, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL); +} + +/** + Generate the Neighbor Solicitation message and send it to the Destination Address. + + @param[in] IpSb The IP service to send the packet + @param[in] SourceAddress The source address of the message. + @param[in] DestinationAddress The destination address of the message. + @param[in] TargetIp6Address The IP address of the target of the solicitation. + It must not be a multicast address. + @param[in] SourceLinkAddress The MAC address for the sender. If not NULL, + a source link-layer address option will be appended + to the message. + + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the + operation. + @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent. + +**/ +EFI_STATUS +Ip6SendNeighborSolicit ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *SourceAddress, + IN EFI_IPv6_ADDRESS *DestinationAddress, + IN EFI_IPv6_ADDRESS *TargetIp6Address, + IN EFI_MAC_ADDRESS *SourceLinkAddress OPTIONAL + ) +{ + NET_BUF *Packet; + EFI_IP6_HEADER Head; + IP6_ICMP_INFORMATION_HEAD *IcmpHead; + IP6_ETHER_ADDR_OPTION *LinkLayerOption; + EFI_IPv6_ADDRESS *Target; + BOOLEAN IsDAD; + UINT16 PayloadLen; + IP6_NEIGHBOR_ENTRY *Neighbor; + + // + // Check input parameters + // + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + if (DestinationAddress == NULL || TargetIp6Address == NULL) { + return EFI_INVALID_PARAMETER; + } + + IsDAD = FALSE; + + if (SourceAddress == NULL || (SourceAddress != NULL && NetIp6IsUnspecifiedAddr (SourceAddress))) { + IsDAD = TRUE; + } + + // + // The Neighbor Solicitation message should include a source link-layer address option + // if the solicitation is not sent by performing DAD - Duplicate Address Detection. + // Otherwise must not include it. + // + PayloadLen = (UINT16) (sizeof (IP6_ICMP_INFORMATION_HEAD) + sizeof (EFI_IPv6_ADDRESS)); + + if (!IsDAD) { + if (SourceLinkAddress == NULL) { + return EFI_INVALID_PARAMETER; + } + + PayloadLen = (UINT16) (PayloadLen + sizeof (IP6_ETHER_ADDR_OPTION)); + } + + // + // Generate the packet to be sent + // + + Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Create the basic IPv6 header + // + Head.FlowLabelL = 0; + Head.FlowLabelH = 0; + Head.PayloadLength = HTONS (PayloadLen); + Head.NextHeader = IP6_ICMP; + Head.HopLimit = IP6_HOP_LIMIT; + + if (SourceAddress != NULL) { + IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress); + } else { + ZeroMem (&Head.SourceAddress, sizeof (EFI_IPv6_ADDRESS)); + } + + IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress); + + NetbufReserve (Packet, sizeof (EFI_IP6_HEADER)); + + // + // Fill in the ICMP header, Target address, and Source link-layer address. + // + IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE); + ASSERT (IcmpHead != NULL); + ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD)); + IcmpHead->Head.Type = ICMP_V6_NEIGHBOR_SOLICIT; + IcmpHead->Head.Code = 0; + + Target = (EFI_IPv6_ADDRESS *) NetbufAllocSpace (Packet, sizeof (EFI_IPv6_ADDRESS), FALSE); + ASSERT (Target != NULL); + IP6_COPY_ADDRESS (Target, TargetIp6Address); + + LinkLayerOption = NULL; + if (!IsDAD) { + + // + // Fill in the source link-layer address option + // + LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace ( + Packet, + sizeof (IP6_ETHER_ADDR_OPTION), + FALSE + ); + ASSERT (LinkLayerOption != NULL); + LinkLayerOption->Type = Ip6OptionEtherSource; + LinkLayerOption->Length = 1; + CopyMem (LinkLayerOption->EtherAddr, SourceLinkAddress, 6); + } + + // + // Create a Neighbor Cache entry in the INCOMPLETE state when performing + // address resolution. + // + if (!IsDAD && Ip6IsSNMulticastAddr (DestinationAddress)) { + Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address); + if (Neighbor == NULL) { + Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, TargetIp6Address, NULL); + ASSERT (Neighbor != NULL); + } + } + + // + // Transmit the packet + // + return Ip6Output (IpSb, IpSb->DefaultInterface, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL); +} + +/** + Process the Neighbor Solicitation message. The message may be sent for Duplicate + Address Detection or Address Resolution. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the message. + @param[in] Packet The content of the message with IP head removed. + + @retval EFI_SUCCESS The packet processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid. + @retval EFI_ICMP_ERROR The packet indicates that DAD is failed. + @retval Others Failed to process the packet. + +**/ +EFI_STATUS +Ip6ProcessNeighborSolicit ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_ICMP_INFORMATION_HEAD Icmp; + EFI_IPv6_ADDRESS Target; + IP6_ETHER_ADDR_OPTION LinkLayerOption; + BOOLEAN IsDAD; + BOOLEAN IsUnicast; + BOOLEAN IsMaintained; + IP6_DAD_ENTRY *DupAddrDetect; + IP6_INTERFACE *IpIf; + IP6_NEIGHBOR_ENTRY *Neighbor; + BOOLEAN Solicited; + BOOLEAN UpdateCache; + EFI_IPv6_ADDRESS Dest; + UINT16 OptionLen; + UINT8 *Option; + BOOLEAN Provided; + EFI_STATUS Status; + VOID *MacAddress; + + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + NetbufCopy (Packet, sizeof (Icmp), sizeof (Target), Target.Addr); + + // + // Perform Message Validation: + // The IP Hop Limit field has a value of 255, i.e., the packet + // could not possibly have been forwarded by a router. + // ICMP Code is 0. + // Target Address is not a multicast address. + // + Status = EFI_INVALID_PARAMETER; + + if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 || !NetIp6IsValidUnicast (&Target)) { + goto Exit; + } + + // + // ICMP length is 24 or more octets. + // + OptionLen = 0; + if (Head->PayloadLength < IP6_ND_LENGTH) { + goto Exit; + } else { + OptionLen = (UINT16) (Head->PayloadLength - IP6_ND_LENGTH); + if (OptionLen != 0) { + Option = NetbufGetByte (Packet, IP6_ND_LENGTH, NULL); + ASSERT (Option != NULL); + + // + // All included options should have a length that is greater than zero. + // + if (!Ip6IsNDOptionValid (Option, OptionLen)) { + goto Exit; + } + } + } + + IsDAD = NetIp6IsUnspecifiedAddr (&Head->SourceAddress); + IsUnicast = (BOOLEAN) !Ip6IsSNMulticastAddr (&Head->DestinationAddress); + IsMaintained = Ip6IsOneOfSetAddress (IpSb, &Target, &IpIf, NULL); + + Provided = FALSE; + if (OptionLen >= sizeof (IP6_ETHER_ADDR_OPTION)) { + NetbufCopy ( + Packet, + IP6_ND_LENGTH, + sizeof (IP6_ETHER_ADDR_OPTION), + (UINT8 *) &LinkLayerOption + ); + // + // The solicitation for neighbor discovery should include a source link-layer + // address option. If the option is not recognized, silently ignore it. + // + if (LinkLayerOption.Type == Ip6OptionEtherSource) { + if (IsDAD) { + // + // If the IP source address is the unspecified address, the source + // link-layer address option must not be included in the message. + // + goto Exit; + } + + Provided = TRUE; + } + } + + // + // If the IP source address is the unspecified address, the IP + // destination address is a solicited-node multicast address. + // + if (IsDAD && IsUnicast) { + goto Exit; + } + + // + // If the target address is tentative, and the source address is a unicast address, + // the solicitation's sender is performing address resolution on the target; + // the solicitation should be silently ignored. + // + if (!IsDAD && !IsMaintained) { + goto Exit; + } + + // + // If received unicast neighbor solicitation but destination is not this node, + // drop the packet. + // + if (IsUnicast && !IsMaintained) { + goto Exit; + } + + // + // In DAD, when target address is a tentative address, + // process the received neighbor solicitation message but not send out response. + // + if (IsDAD && !IsMaintained) { + DupAddrDetect = Ip6FindDADEntry (IpSb, &Target, &IpIf); + if (DupAddrDetect != NULL) { + // + // Check the MAC address of the incoming packet. + // + if (IpSb->RecvRequest.MnpToken.Packet.RxData == NULL) { + goto Exit; + } + + MacAddress = IpSb->RecvRequest.MnpToken.Packet.RxData->SourceAddress; + if (MacAddress != NULL) { + if (CompareMem ( + MacAddress, + &IpSb->SnpMode.CurrentAddress, + IpSb->SnpMode.HwAddressSize + ) != 0) { + // + // The NS is from another node to performing DAD on the same address. + // Fail DAD for the tentative address. + // + Ip6OnDADFinished (FALSE, IpIf, DupAddrDetect); + Status = EFI_ICMP_ERROR; + } else { + // + // The below layer loopback the NS we sent. Record it and wait for more. + // + DupAddrDetect->Receive++; + Status = EFI_SUCCESS; + } + } + } + goto Exit; + } + + // + // If the solicitation does not contain a link-layer address, DO NOT create or + // update the neighbor cache entries. + // + if (Provided) { + Neighbor = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress); + UpdateCache = FALSE; + + if (Neighbor == NULL) { + Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, &Head->SourceAddress, NULL); + if (Neighbor == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + UpdateCache = TRUE; + } else { + if (CompareMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6) != 0) { + UpdateCache = TRUE; + } + } + + if (UpdateCache) { + Neighbor->State = EfiNeighborStale; + Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME; + CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6); + // + // Send queued packets if exist. + // + Neighbor->CallBack ((VOID *) Neighbor); + } + } + + // + // Sends a Neighbor Advertisement as response. + // Set the Router flag to zero since the node is a host. + // If the source address of the solicitation is unspeicifed, and target address + // is one of the maintained address, reply a unsolicited multicast advertisement. + // + if (IsDAD && IsMaintained) { + Solicited = FALSE; + Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &Dest); + } else { + Solicited = TRUE; + IP6_COPY_ADDRESS (&Dest, &Head->SourceAddress); + } + + Status = Ip6SendNeighborAdvertise ( + IpSb, + &Target, + &Dest, + &Target, + &IpSb->SnpMode.CurrentAddress, + FALSE, + TRUE, + Solicited + ); +Exit: + NetbufFree (Packet); + return Status; +} + +/** + Process the Neighbor Advertisement message. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the message. + @param[in] Packet The content of the message with IP head removed. + + @retval EFI_SUCCESS The packet processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid. + @retval EFI_ICMP_ERROR The packet indicates that DAD is failed. + @retval Others Failed to process the packet. + +**/ +EFI_STATUS +Ip6ProcessNeighborAdvertise ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_ICMP_INFORMATION_HEAD Icmp; + EFI_IPv6_ADDRESS Target; + IP6_ETHER_ADDR_OPTION LinkLayerOption; + BOOLEAN Provided; + INTN Compare; + IP6_NEIGHBOR_ENTRY *Neighbor; + IP6_DEFAULT_ROUTER *DefaultRouter; + BOOLEAN Solicited; + BOOLEAN IsRouter; + BOOLEAN Override; + IP6_DAD_ENTRY *DupAddrDetect; + IP6_INTERFACE *IpIf; + UINT16 OptionLen; + UINT8 *Option; + EFI_STATUS Status; + + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + NetbufCopy (Packet, sizeof (Icmp), sizeof (Target), Target.Addr); + + // + // Validate the incoming Neighbor Advertisement + // + Status = EFI_INVALID_PARAMETER; + // + // The IP Hop Limit field has a value of 255, i.e., the packet + // could not possibly have been forwarded by a router. + // ICMP Code is 0. + // Target Address is not a multicast address. + // + if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 || !NetIp6IsValidUnicast (&Target)) { + goto Exit; + } + + // + // ICMP length is 24 or more octets. + // + Provided = FALSE; + OptionLen = 0; + if (Head->PayloadLength < IP6_ND_LENGTH) { + goto Exit; + } else { + OptionLen = (UINT16) (Head->PayloadLength - IP6_ND_LENGTH); + if (OptionLen != 0) { + Option = NetbufGetByte (Packet, IP6_ND_LENGTH, NULL); + ASSERT (Option != NULL); + + // + // All included options should have a length that is greater than zero. + // + if (!Ip6IsNDOptionValid (Option, OptionLen)) { + goto Exit; + } + } + } + + // + // If the IP destination address is a multicast address, Solicited Flag is ZERO. + // + Solicited = FALSE; + if ((Icmp.Fourth & IP6_SOLICITED_FLAG) == IP6_SOLICITED_FLAG) { + Solicited = TRUE; + } + if (IP6_IS_MULTICAST (&Head->DestinationAddress) && Solicited) { + goto Exit; + } + + // + // DAD - Check whether the Target is one of our tentative address. + // + DupAddrDetect = Ip6FindDADEntry (IpSb, &Target, &IpIf); + if (DupAddrDetect != NULL) { + // + // DAD fails, some other node is using this address. + // + NetbufFree (Packet); + Ip6OnDADFinished (FALSE, IpIf, DupAddrDetect); + return EFI_ICMP_ERROR; + } + + // + // Search the Neighbor Cache for the target's entry. If no entry exists, + // the advertisement should be silently discarded. + // + Neighbor = Ip6FindNeighborEntry (IpSb, &Target); + if (Neighbor == NULL) { + goto Exit; + } + + // + // Get IsRouter Flag and Override Flag + // + IsRouter = FALSE; + Override = FALSE; + if ((Icmp.Fourth & IP6_IS_ROUTER_FLAG) == IP6_IS_ROUTER_FLAG) { + IsRouter = TRUE; + } + if ((Icmp.Fourth & IP6_OVERRIDE_FLAG) == IP6_OVERRIDE_FLAG) { + Override = TRUE; + } + + // + // Check whether link layer option is included. + // + if (OptionLen >= sizeof (IP6_ETHER_ADDR_OPTION)) { + NetbufCopy ( + Packet, + IP6_ND_LENGTH, + sizeof (IP6_ETHER_ADDR_OPTION), + (UINT8 *) &LinkLayerOption + ); + + if (LinkLayerOption.Type == Ip6OptionEtherTarget) { + Provided = TRUE; + } + } + + Compare = 0; + if (Provided) { + Compare = CompareMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6); + } + + if (!Neighbor->IsRouter && IsRouter) { + DefaultRouter = Ip6FindDefaultRouter (IpSb, &Target); + if (DefaultRouter != NULL) { + DefaultRouter->NeighborCache = Neighbor; + } + } + + if (Neighbor->State == EfiNeighborInComplete) { + // + // If the target's Neighbor Cache entry is in INCOMPLETE state and no + // Target Link-Layer address option is included while link layer has + // address, the message should be silently discarded. + // + if (!Provided) { + goto Exit; + } + // + // Update the Neighbor Cache + // + CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6); + if (Solicited) { + Neighbor->State = EfiNeighborReachable; + Neighbor->Ticks = IP6_GET_TICKS (IpSb->ReachableTime); + } else { + Neighbor->State = EfiNeighborStale; + Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME; + // + // Send any packets queued for the neighbor awaiting address resolution. + // + Neighbor->CallBack ((VOID *) Neighbor); + } + + Neighbor->IsRouter = IsRouter; + + } else { + if (!Override && Compare != 0) { + // + // When the Override Flag is clear and supplied link-layer address differs from + // that in the cache, if the state of the entry is not REACHABLE, ignore the + // message. Otherwise set it to STALE but do not update the entry in any + // other way. + // + if (Neighbor->State == EfiNeighborReachable) { + Neighbor->State = EfiNeighborStale; + Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME; + } + } else { + if (Compare != 0) { + CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6); + } + // + // Update the entry's state + // + if (Solicited) { + Neighbor->State = EfiNeighborReachable; + Neighbor->Ticks = IP6_GET_TICKS (IpSb->ReachableTime); + } else { + if (Compare != 0) { + Neighbor->State = EfiNeighborStale; + Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME; + } + } + + // + // When IsRouter is changed from TRUE to FALSE, remove the router from the + // Default Router List and remove the Destination Cache entries for all destinations + // using the neighbor as a router. + // + if (Neighbor->IsRouter && !IsRouter) { + DefaultRouter = Ip6FindDefaultRouter (IpSb, &Target); + if (DefaultRouter != NULL) { + Ip6DestroyDefaultRouter (IpSb, DefaultRouter); + } + } + + Neighbor->IsRouter = IsRouter; + } + } + + if (Neighbor->State == EfiNeighborReachable) { + Neighbor->CallBack ((VOID *) Neighbor); + } + + Status = EFI_SUCCESS; + +Exit: + NetbufFree (Packet); + return Status; +} + +/** + Process the Router Advertisement message according to RFC4861. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the message. + @param[in] Packet The content of the message with the IP head removed. + + @retval EFI_SUCCESS The packet processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid. + @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the + operation. + @retval Others Failed to process the packet. + +**/ +EFI_STATUS +Ip6ProcessRouterAdvertise ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_ICMP_INFORMATION_HEAD Icmp; + UINT32 ReachableTime; + UINT32 RetransTimer; + UINT16 RouterLifetime; + UINT16 Offset; + UINT8 Type; + UINT8 Length; + IP6_ETHER_ADDR_OPTION LinkLayerOption; + UINT32 Fourth; + UINT8 CurHopLimit; + BOOLEAN Mflag; + BOOLEAN Oflag; + IP6_DEFAULT_ROUTER *DefaultRouter; + IP6_NEIGHBOR_ENTRY *NeighborCache; + EFI_MAC_ADDRESS LinkLayerAddress; + IP6_MTU_OPTION MTUOption; + IP6_PREFIX_INFO_OPTION PrefixOption; + IP6_PREFIX_LIST_ENTRY *PrefixList; + BOOLEAN OnLink; + BOOLEAN Autonomous; + EFI_IPv6_ADDRESS StatelessAddress; + EFI_STATUS Status; + UINT16 OptionLen; + UINT8 *Option; + INTN Result; + + Status = EFI_INVALID_PARAMETER; + + if (IpSb->Ip6ConfigInstance.Policy != Ip6ConfigPolicyAutomatic) { + // + // Skip the process below as it's not required under the current policy. + // + goto Exit; + } + + NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp); + + // + // Validate the incoming Router Advertisement + // + + // + // The IP source address must be a link-local address + // + if (!NetIp6IsLinkLocalAddr (&Head->SourceAddress)) { + goto Exit; + } + // + // The IP Hop Limit field has a value of 255, i.e. the packet + // could not possibly have been forwarded by a router. + // ICMP Code is 0. + // ICMP length (derived from the IP length) is 16 or more octets. + // + if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 || + Head->PayloadLength < IP6_RA_LENGTH) { + goto Exit; + } + + // + // All included options have a length that is greater than zero. + // + OptionLen = (UINT16) (Head->PayloadLength - IP6_RA_LENGTH); + if (OptionLen != 0) { + Option = NetbufGetByte (Packet, IP6_RA_LENGTH, NULL); + ASSERT (Option != NULL); + + if (!Ip6IsNDOptionValid (Option, OptionLen)) { + goto Exit; + } + } + + // + // Process Fourth field. + // In Router Advertisement, Fourth is composed of CurHopLimit (8bit), M flag, O flag, + // and Router Lifetime (16 bit). + // + + Fourth = NTOHL (Icmp.Fourth); + CopyMem (&RouterLifetime, &Fourth, sizeof (UINT16)); + + // + // If the source address already in the default router list, update it. + // Otherwise create a new entry. + // A Lifetime of zero indicates that the router is not a default router. + // + DefaultRouter = Ip6FindDefaultRouter (IpSb, &Head->SourceAddress); + if (DefaultRouter == NULL) { + if (RouterLifetime != 0) { + DefaultRouter = Ip6CreateDefaultRouter (IpSb, &Head->SourceAddress, RouterLifetime); + if (DefaultRouter == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + } + } else { + if (RouterLifetime != 0) { + DefaultRouter->Lifetime = RouterLifetime; + // + // Check the corresponding neighbor cache entry here. + // + if (DefaultRouter->NeighborCache == NULL) { + DefaultRouter->NeighborCache = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress); + } + } else { + // + // If the address is in the host's default router list and the router lifetime is zero, + // immediately time-out the entry. + // + Ip6DestroyDefaultRouter (IpSb, DefaultRouter); + } + } + + CurHopLimit = *((UINT8 *) &Fourth + 3); + if (CurHopLimit != 0) { + IpSb->CurHopLimit = CurHopLimit; + } + + Mflag = FALSE; + Oflag = FALSE; + if ((*((UINT8 *) &Fourth + 2) & IP6_M_ADDR_CONFIG_FLAG) == IP6_M_ADDR_CONFIG_FLAG) { + Mflag = TRUE; + } else { + if ((*((UINT8 *) &Fourth + 2) & IP6_O_CONFIG_FLAG) == IP6_O_CONFIG_FLAG) { + Oflag = TRUE; + } + } + + if (Mflag || Oflag) { + // + // Use Ip6Config to get available addresses or other configuration from DHCP. + // + Ip6ConfigStartStatefulAutoConfig (&IpSb->Ip6ConfigInstance, Oflag); + } + + // + // Process Reachable Time and Retrans Timer fields. + // + NetbufCopy (Packet, sizeof (Icmp), sizeof (UINT32), (UINT8 *) &ReachableTime); + NetbufCopy (Packet, sizeof (Icmp) + sizeof (UINT32), sizeof (UINT32), (UINT8 *) &RetransTimer); + ReachableTime = NTOHL (ReachableTime); + RetransTimer = NTOHL (RetransTimer); + + if (ReachableTime != 0 && ReachableTime != IpSb->BaseReachableTime) { + // + // If new value is not unspecified and differs from the previous one, record it + // in BaseReachableTime and recompute a ReachableTime. + // + IpSb->BaseReachableTime = ReachableTime; + Ip6UpdateReachableTime (IpSb); + } + + if (RetransTimer != 0) { + IpSb->RetransTimer = RetransTimer; + } + + // + // IsRouter flag must be set to TRUE if corresponding neighbor cache entry exists. + // + NeighborCache = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress); + if (NeighborCache != NULL) { + NeighborCache->IsRouter = TRUE; + } + + // + // If an valid router advertisment is received, stops router solicitation. + // + IpSb->RouterAdvertiseReceived = TRUE; + + // + // The only defined options that may appear are the Source + // Link-Layer Address, Prefix information and MTU options. + // All included options have a length that is greater than zero. + // + Offset = 16; + while (Offset < Head->PayloadLength) { + NetbufCopy (Packet, Offset, sizeof (UINT8), &Type); + switch (Type) { + case Ip6OptionEtherSource: + // + // Update the neighbor cache + // + NetbufCopy (Packet, Offset, sizeof (IP6_ETHER_ADDR_OPTION), (UINT8 *) &LinkLayerOption); + if (LinkLayerOption.Length <= 0) { + goto Exit; + } + + ZeroMem (&LinkLayerAddress, sizeof (EFI_MAC_ADDRESS)); + CopyMem (&LinkLayerAddress, LinkLayerOption.EtherAddr, 6); + + if (NeighborCache == NULL) { + NeighborCache = Ip6CreateNeighborEntry ( + IpSb, + Ip6OnArpResolved, + &Head->SourceAddress, + &LinkLayerAddress + ); + if (NeighborCache == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + NeighborCache->IsRouter = TRUE; + NeighborCache->State = EfiNeighborStale; + NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME; + } else { + Result = CompareMem (&LinkLayerAddress, &NeighborCache->LinkAddress, 6); + + // + // If the link-local address is the same as that already in the cache, + // the cache entry's state remains unchanged. Otherwise update the + // reachability state to STALE. + // + if ((NeighborCache->State == EfiNeighborInComplete) || (Result != 0)) { + CopyMem (&NeighborCache->LinkAddress, &LinkLayerAddress, 6); + + NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME; + + if (NeighborCache->State == EfiNeighborInComplete) { + // + // Send queued packets if exist. + // + NeighborCache->State = EfiNeighborStale; + NeighborCache->CallBack ((VOID *) NeighborCache); + } else { + NeighborCache->State = EfiNeighborStale; + } + } + } + + Offset = (UINT16) (Offset + (UINT16) LinkLayerOption.Length * 8); + break; + case Ip6OptionPrefixInfo: + NetbufCopy (Packet, Offset, sizeof (IP6_PREFIX_INFO_OPTION), (UINT8 *) &PrefixOption); + if (PrefixOption.Length != 4) { + goto Exit; + } + PrefixOption.ValidLifetime = NTOHL (PrefixOption.ValidLifetime); + PrefixOption.PreferredLifetime = NTOHL (PrefixOption.PreferredLifetime); + + // + // Get L and A flag, recorded in the lower 2 bits of Reserved1 + // + OnLink = FALSE; + if ((PrefixOption.Reserved1 & IP6_ON_LINK_FLAG) == IP6_ON_LINK_FLAG) { + OnLink = TRUE; + } + Autonomous = FALSE; + if ((PrefixOption.Reserved1 & IP6_AUTO_CONFIG_FLAG) == IP6_AUTO_CONFIG_FLAG) { + Autonomous = TRUE; + } + + // + // If the prefix is the link-local prefix, silently ignore the prefix option. + // + if (PrefixOption.PrefixLength == IP6_LINK_LOCAL_PREFIX_LENGTH && + NetIp6IsLinkLocalAddr (&PrefixOption.Prefix) + ) { + Offset += sizeof (IP6_PREFIX_INFO_OPTION); + break; + } + // + // Do following if on-link flag is set according to RFC4861. + // + if (OnLink) { + PrefixList = Ip6FindPrefixListEntry ( + IpSb, + TRUE, + PrefixOption.PrefixLength, + &PrefixOption.Prefix + ); + // + // Create a new entry for the prefix, if the ValidLifetime is zero, + // silently ignore the prefix option. + // + if (PrefixList == NULL && PrefixOption.ValidLifetime != 0) { + PrefixList = Ip6CreatePrefixListEntry ( + IpSb, + TRUE, + PrefixOption.ValidLifetime, + PrefixOption.PreferredLifetime, + PrefixOption.PrefixLength, + &PrefixOption.Prefix + ); + if (PrefixList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + } else if (PrefixList != NULL) { + if (PrefixOption.ValidLifetime != 0) { + PrefixList->ValidLifetime = PrefixOption.ValidLifetime; + } else { + // + // If the prefix exists and incoming ValidLifetime is zero, immediately + // remove the prefix. + Ip6DestroyPrefixListEntry (IpSb, PrefixList, OnLink, TRUE); + } + } + } + + // + // Do following if Autonomous flag is set according to RFC4862. + // + if (Autonomous && PrefixOption.PreferredLifetime <= PrefixOption.ValidLifetime) { + PrefixList = Ip6FindPrefixListEntry ( + IpSb, + FALSE, + PrefixOption.PrefixLength, + &PrefixOption.Prefix + ); + // + // Create a new entry for the prefix, and form an address by prefix + interface id + // If the sum of the prefix length and interface identifier length + // does not equal 128 bits, the Prefix Information option MUST be ignored. + // + if (PrefixList == NULL && + PrefixOption.ValidLifetime != 0 && + PrefixOption.PrefixLength + IpSb->InterfaceIdLen * 8 == 128 + ) { + // + // Form the address in network order. + // + CopyMem (&StatelessAddress, &PrefixOption.Prefix, sizeof (UINT64)); + CopyMem (&StatelessAddress.Addr[8], IpSb->InterfaceId, sizeof (UINT64)); + + // + // If the address is not yet in the assigned address list, adds it into. + // + if (!Ip6IsOneOfSetAddress (IpSb, &StatelessAddress, NULL, NULL)) { + // + // And also not in the DAD process, check its uniqeness firstly. + // + if (Ip6FindDADEntry (IpSb, &StatelessAddress, NULL) == NULL) { + Status = Ip6SetAddress ( + IpSb->DefaultInterface, + &StatelessAddress, + FALSE, + PrefixOption.PrefixLength, + PrefixOption.ValidLifetime, + PrefixOption.PreferredLifetime, + NULL, + NULL + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + } + + // + // Adds the prefix option to stateless prefix option list. + // + PrefixList = Ip6CreatePrefixListEntry ( + IpSb, + FALSE, + PrefixOption.ValidLifetime, + PrefixOption.PreferredLifetime, + PrefixOption.PrefixLength, + &PrefixOption.Prefix + ); + if (PrefixList == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + } else if (PrefixList != NULL) { + + // + // Reset the preferred lifetime of the address if the advertised prefix exists. + // Perform specific action to valid lifetime together. + // + PrefixList->PreferredLifetime = PrefixOption.PreferredLifetime; + if ((PrefixOption.ValidLifetime > 7200) || + (PrefixOption.ValidLifetime > PrefixList->ValidLifetime)) { + // + // If the received Valid Lifetime is greater than 2 hours or + // greater than RemainingLifetime, set the valid lifetime of the + // corresponding address to the advertised Valid Lifetime. + // + PrefixList->ValidLifetime = PrefixOption.ValidLifetime; + + } else if (PrefixList->ValidLifetime <= 7200) { + // + // If RemainingLifetime is less than or equls to 2 hours, ignore the + // Prefix Information option with regards to the valid lifetime. + // TODO: If this option has been authenticated, set the valid lifetime. + // + } else { + // + // Otherwise, reset the valid lifetime of the corresponding + // address to 2 hours. + // + PrefixList->ValidLifetime = 7200; + } + } + } + + Offset += sizeof (IP6_PREFIX_INFO_OPTION); + break; + case Ip6OptionMtu: + NetbufCopy (Packet, Offset, sizeof (IP6_MTU_OPTION), (UINT8 *) &MTUOption); + if (MTUOption.Length != 1) { + goto Exit; + } + + // + // Use IPv6 minimum link MTU 1280 bytes as the maximum packet size in order + // to omit implementation of Path MTU Discovery. Thus ignore the MTU option + // in Router Advertisement. + // + + Offset += sizeof (IP6_MTU_OPTION); + break; + default: + // + // Silently ignore unrecognized options + // + NetbufCopy (Packet, Offset + sizeof (UINT8), sizeof (UINT8), &Length); + if (Length <= 0) { + goto Exit; + } + + Offset = (UINT16) (Offset + (UINT16) Length * 8); + break; + } + } + + Status = EFI_SUCCESS; + +Exit: + NetbufFree (Packet); + return Status; +} + +/** + Process the ICMPv6 redirect message. Find the instance, then update + its route cache. + + @param[in] IpSb The IP6 service binding instance that received + the packet. + @param[in] Head The IP head of the received ICMPv6 packet. + @param[in] Packet The content of the ICMPv6 redirect packet with + the IP head removed. + + @retval EFI_INVALID_PARAMETER The parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Insuffcient resources to complete the + operation. + @retval EFI_SUCCESS Successfully updated the route caches. + +**/ +EFI_STATUS +Ip6ProcessRedirect ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ) +{ + IP6_ICMP_INFORMATION_HEAD *Icmp; + EFI_IPv6_ADDRESS *Target; + EFI_IPv6_ADDRESS *IcmpDest; + UINT8 *Option; + UINT16 OptionLen; + IP6_ROUTE_ENTRY *RouteEntry; + IP6_ROUTE_CACHE_ENTRY *RouteCache; + IP6_NEIGHBOR_ENTRY *NeighborCache; + INT32 Length; + UINT8 OptLen; + IP6_ETHER_ADDR_OPTION *LinkLayerOption; + EFI_MAC_ADDRESS Mac; + UINT32 Index; + BOOLEAN IsRouter; + EFI_STATUS Status; + INTN Result; + + Status = EFI_INVALID_PARAMETER; + + Icmp = (IP6_ICMP_INFORMATION_HEAD *) NetbufGetByte (Packet, 0, NULL); + if (Icmp == NULL) { + goto Exit; + } + + // + // Validate the incoming Redirect message + // + + // + // The IP Hop Limit field has a value of 255, i.e. the packet + // could not possibly have been forwarded by a router. + // ICMP Code is 0. + // ICMP length (derived from the IP length) is 40 or more octets. + // + if (Head->HopLimit != IP6_HOP_LIMIT || Icmp->Head.Code != 0 || + Head->PayloadLength < IP6_REDITECT_LENGTH) { + goto Exit; + } + + // + // The IP source address must be a link-local address + // + if (!NetIp6IsLinkLocalAddr (&Head->SourceAddress)) { + goto Exit; + } + + // + // The dest of this ICMP redirect message is not us. + // + if (!Ip6IsOneOfSetAddress (IpSb, &Head->DestinationAddress, NULL, NULL)) { + goto Exit; + } + + // + // All included options have a length that is greater than zero. + // + OptionLen = (UINT16) (Head->PayloadLength - IP6_REDITECT_LENGTH); + if (OptionLen != 0) { + Option = NetbufGetByte (Packet, IP6_REDITECT_LENGTH, NULL); + ASSERT (Option != NULL); + + if (!Ip6IsNDOptionValid (Option, OptionLen)) { + goto Exit; + } + } + + Target = (EFI_IPv6_ADDRESS *) (Icmp + 1); + IcmpDest = Target + 1; + + // + // The ICMP Destination Address field in the redirect message does not contain + // a multicast address. + // + if (IP6_IS_MULTICAST (IcmpDest)) { + goto Exit; + } + + // + // The ICMP Target Address is either a link-local address (when redirected to + // a router) or the same as the ICMP Destination Address (when redirected to + // the on-link destination). + // + IsRouter = (BOOLEAN) !EFI_IP6_EQUAL (Target, IcmpDest); + if (!NetIp6IsLinkLocalAddr (Target) && IsRouter) { + goto Exit; + } + + // + // Check the options. The only interested option here is the target-link layer + // address option. + // + Length = Packet->TotalSize - 40; + Option = (UINT8 *) (IcmpDest + 1); + LinkLayerOption = NULL; + while (Length > 0) { + switch (*Option) { + case Ip6OptionEtherTarget: + + LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) Option; + OptLen = LinkLayerOption->Length; + if (OptLen != 1) { + // + // For ethernet, the length must be 1. + // + goto Exit; + } + break; + + default: + + OptLen = *(Option + 1); + if (OptLen == 0) { + // + // A length of 0 is invalid. + // + goto Exit; + } + break; + } + + Length -= 8 * OptLen; + Option += 8 * OptLen; + } + + if (Length != 0) { + goto Exit; + } + + // + // The IP source address of the Redirect is the same as the current + // first-hop router for the specified ICMP Destination Address. + // + RouteCache = Ip6FindRouteCache (IpSb->RouteTable, IcmpDest, &Head->DestinationAddress); + if (RouteCache != NULL) { + if (!EFI_IP6_EQUAL (&RouteCache->NextHop, &Head->SourceAddress)) { + // + // The source of this Redirect message must match the NextHop of the + // corresponding route cache entry. + // + goto Exit; + } + + // + // Update the NextHop. + // + IP6_COPY_ADDRESS (&RouteCache->NextHop, Target); + + if (!IsRouter) { + RouteEntry = (IP6_ROUTE_ENTRY *) RouteCache->Tag; + RouteEntry->Flag = RouteEntry->Flag | IP6_DIRECT_ROUTE; + } + + } else { + // + // Get the Route Entry. + // + RouteEntry = Ip6FindRouteEntry (IpSb->RouteTable, IcmpDest, NULL); + if (RouteEntry == NULL) { + RouteEntry = Ip6CreateRouteEntry (IcmpDest, 0, NULL); + if (RouteEntry == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + } + + if (!IsRouter) { + RouteEntry->Flag = IP6_DIRECT_ROUTE; + } + + // + // Create a route cache for this. + // + RouteCache = Ip6CreateRouteCacheEntry ( + IcmpDest, + &Head->DestinationAddress, + Target, + (UINTN) RouteEntry + ); + if (RouteCache == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + // + // Insert the newly created route cache entry. + // + Index = IP6_ROUTE_CACHE_HASH (IcmpDest, &Head->DestinationAddress); + InsertHeadList (&IpSb->RouteTable->Cache.CacheBucket[Index], &RouteCache->Link); + } + + // + // Try to locate the neighbor cache for the Target. + // + NeighborCache = Ip6FindNeighborEntry (IpSb, Target); + + if (LinkLayerOption != NULL) { + if (NeighborCache == NULL) { + // + // Create a neighbor cache for the Target. + // + ZeroMem (&Mac, sizeof (EFI_MAC_ADDRESS)); + CopyMem (&Mac, LinkLayerOption->EtherAddr, 6); + NeighborCache = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, Target, &Mac); + if (NeighborCache == NULL) { + // + // Just report a success here. The neighbor cache can be created in + // some other place. + // + Status = EFI_SUCCESS; + goto Exit; + } + + NeighborCache->State = EfiNeighborStale; + NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME; + } else { + Result = CompareMem (LinkLayerOption->EtherAddr, &NeighborCache->LinkAddress, 6); + + // + // If the link-local address is the same as that already in the cache, + // the cache entry's state remains unchanged. Otherwise update the + // reachability state to STALE. + // + if ((NeighborCache->State == EfiNeighborInComplete) || (Result != 0)) { + CopyMem (&NeighborCache->LinkAddress, LinkLayerOption->EtherAddr, 6); + + NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME; + + if (NeighborCache->State == EfiNeighborInComplete) { + // + // Send queued packets if exist. + // + NeighborCache->State = EfiNeighborStale; + NeighborCache->CallBack ((VOID *) NeighborCache); + } else { + NeighborCache->State = EfiNeighborStale; + } + } + } + } + + if (NeighborCache != NULL && IsRouter) { + // + // The Target is a router, set IsRouter to TRUE. + // + NeighborCache->IsRouter = TRUE; + } + + Status = EFI_SUCCESS; + +Exit: + NetbufFree (Packet); + return Status; +} + +/** + Add Neighbor cache entries. It is a work function for EfiIp6Neighbors(). + + @param[in] IpSb The IP6 service binding instance. + @param[in] TargetIp6Address Pointer to Target IPv6 address. + @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL. + @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor + cache. It will be deleted after Timeout. A value of zero means that + the entry is permanent. A non-zero value means that the entry is + dynamic. + @param[in] Override If TRUE, the cached link-layer address of the matching entry will + be overridden and updated; if FALSE, and if a + corresponding cache entry already existed, EFI_ACCESS_DENIED + will be returned. + + @retval EFI_SUCCESS The neighbor cache entry has been added. + @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache + due to insufficient resources. + @retval EFI_NOT_FOUND TargetLinkAddress is NULL. + @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache, + and that entry is tagged as un-overridden (when DeleteFlag + is FALSE). + +**/ +EFI_STATUS +Ip6AddNeighbor ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *TargetIp6Address, + IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL, + IN UINT32 Timeout, + IN BOOLEAN Override + ) +{ + IP6_NEIGHBOR_ENTRY *Neighbor; + + Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address); + if (Neighbor != NULL) { + if (!Override) { + return EFI_ACCESS_DENIED; + } else { + if (TargetLinkAddress != NULL) { + IP6_COPY_LINK_ADDRESS (&Neighbor->LinkAddress, TargetLinkAddress); + } + } + } else { + if (TargetLinkAddress == NULL) { + return EFI_NOT_FOUND; + } + + Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, TargetIp6Address, TargetLinkAddress); + if (Neighbor == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + + Neighbor->State = EfiNeighborReachable; + + if (Timeout != 0) { + Neighbor->Ticks = IP6_GET_TICKS (Timeout / TICKS_PER_MS); + Neighbor->Dynamic = TRUE; + } else { + Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME; + } + + return EFI_SUCCESS; +} + +/** + Delete or update Neighbor cache entries. It is a work function for EfiIp6Neighbors(). + + @param[in] IpSb The IP6 service binding instance. + @param[in] TargetIp6Address Pointer to Target IPv6 address. + @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL. + @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor + cache. It will be deleted after Timeout. A value of zero means that + the entry is permanent. A non-zero value means that the entry is + dynamic. + @param[in] Override If TRUE, the cached link-layer address of the matching entry will + be overridden and updated; if FALSE, and if a + corresponding cache entry already existed, EFI_ACCESS_DENIED + will be returned. + + @retval EFI_SUCCESS The neighbor cache entry has been updated or deleted. + @retval EFI_NOT_FOUND This entry is not in the neighbor cache. + +**/ +EFI_STATUS +Ip6DelNeighbor ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *TargetIp6Address, + IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL, + IN UINT32 Timeout, + IN BOOLEAN Override + ) +{ + IP6_NEIGHBOR_ENTRY *Neighbor; + + Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address); + if (Neighbor == NULL) { + return EFI_NOT_FOUND; + } + + RemoveEntryList (&Neighbor->Link); + FreePool (Neighbor); + + return EFI_SUCCESS; +} + +/** + The heartbeat timer of ND module in IP6_TIMER_INTERVAL_IN_MS milliseconds. + This time routine handles DAD module and neighbor state transition. + It is also responsible for sending out router solicitations. + + @param[in] Event The IP6 service instance's heartbeat timer. + @param[in] Context The IP6 service instance. + +**/ +VOID +EFIAPI +Ip6NdFasterTimerTicking ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + LIST_ENTRY *Entry2; + IP6_INTERFACE *IpIf; + IP6_DELAY_JOIN_LIST *DelayNode; + EFI_IPv6_ADDRESS Source; + IP6_DAD_ENTRY *DupAddrDetect; + EFI_STATUS Status; + IP6_NEIGHBOR_ENTRY *NeighborCache; + EFI_IPv6_ADDRESS Destination; + IP6_SERVICE *IpSb; + BOOLEAN Flag; + + IpSb = (IP6_SERVICE *) Context; + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + ZeroMem (&Source, sizeof (EFI_IPv6_ADDRESS)); + + // + // A host SHOULD transmit up to MAX_RTR_SOLICITATIONS (3) Router + // Solicitation messages, each separated by at least + // RTR_SOLICITATION_INTERVAL (4) seconds. + // + if ((IpSb->Ip6ConfigInstance.Policy == Ip6ConfigPolicyAutomatic) && + !IpSb->RouterAdvertiseReceived && + IpSb->SolicitTimer > 0 + ) { + if ((IpSb->Ticks == 0) || (--IpSb->Ticks == 0)) { + Status = Ip6SendRouterSolicit (IpSb, NULL, NULL, NULL, NULL); + if (!EFI_ERROR (Status)) { + IpSb->SolicitTimer--; + IpSb->Ticks = (UINT32) IP6_GET_TICKS (IP6_RTR_SOLICITATION_INTERVAL); + } + } + } + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link); + + // + // Process the delay list to join the solicited-node multicast address. + // + NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DelayJoinList) { + DelayNode = NET_LIST_USER_STRUCT (Entry2, IP6_DELAY_JOIN_LIST, Link); + if ((DelayNode->DelayTime == 0) || (--DelayNode->DelayTime == 0)) { + // + // The timer expires, init the duplicate address detection. + // + Ip6InitDADProcess ( + DelayNode->Interface, + DelayNode->AddressInfo, + DelayNode->DadCallback, + DelayNode->Context + ); + + // + // Remove the delay node + // + RemoveEntryList (&DelayNode->Link); + FreePool (DelayNode); + } + } + + // + // Process the duplicate address detection list. + // + NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DupAddrDetectList) { + DupAddrDetect = NET_LIST_USER_STRUCT (Entry2, IP6_DAD_ENTRY, Link); + + if ((DupAddrDetect->RetransTick == 0) || (--DupAddrDetect->RetransTick == 0)) { + // + // The timer expires, check the remaining transmit counts. + // + if (DupAddrDetect->Transmit < DupAddrDetect->MaxTransmit) { + // + // Send the Neighbor Solicitation message with + // Source - unspecified address, destination - solicited-node multicast address + // Target - the address to be validated + // + Status = Ip6SendNeighborSolicit ( + IpSb, + NULL, + &DupAddrDetect->Destination, + &DupAddrDetect->AddressInfo->Address, + NULL + ); + if (EFI_ERROR (Status)) { + return; + } + + DupAddrDetect->Transmit++; + DupAddrDetect->RetransTick = IP6_GET_TICKS (IpSb->RetransTimer); + } else { + // + // All required solicitation has been sent out, and the RetransTime after the last + // Neighbor Solicit is elapsed, finish the DAD process. + // + Flag = FALSE; + if ((DupAddrDetect->Receive == 0) || + (DupAddrDetect->Transmit <= DupAddrDetect->Receive)) { + Flag = TRUE; + } + + Ip6OnDADFinished (Flag, IpIf, DupAddrDetect); + } + } + } + } + + // + // Polling the state of Neighbor cache + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->NeighborTable) { + NeighborCache = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, Link); + + switch (NeighborCache->State) { + case EfiNeighborInComplete: + if (NeighborCache->Ticks > 0) { + --NeighborCache->Ticks; + } + + // + // Retransmit Neighbor Solicitation messages approximately every + // RetransTimer milliseconds while awaiting a response. + // + if (NeighborCache->Ticks == 0) { + if (NeighborCache->Transmit > 1) { + // + // Send out multicast neighbor solicitation for address resolution. + // After last neighbor solicitation message has been sent out, wait + // for RetransTimer and then remove entry if no response is received. + // + Ip6CreateSNMulticastAddr (&NeighborCache->Neighbor, &Destination); + Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source); + if (EFI_ERROR (Status)) { + return; + } + + Status = Ip6SendNeighborSolicit ( + IpSb, + &Source, + &Destination, + &NeighborCache->Neighbor, + &IpSb->SnpMode.CurrentAddress + ); + if (EFI_ERROR (Status)) { + return; + } + } + + // + // Update the retransmit times. + // + if (NeighborCache->Transmit > 0) { + --NeighborCache->Transmit; + NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer); + } + } + + if (NeighborCache->Transmit == 0) { + // + // Timeout, send ICMP destination unreachable packet and then remove entry + // + Status = Ip6FreeNeighborEntry ( + IpSb, + NeighborCache, + TRUE, + TRUE, + EFI_ICMP_ERROR, + NULL, + NULL + ); + if (EFI_ERROR (Status)) { + return; + } + } + + break; + + case EfiNeighborReachable: + // + // This entry is inserted by EfiIp6Neighbors() as static entry + // and will not timeout. + // + if (!NeighborCache->Dynamic && (NeighborCache->Ticks == IP6_INFINIT_LIFETIME)) { + break; + } + + if ((NeighborCache->Ticks == 0) || (--NeighborCache->Ticks == 0)) { + if (NeighborCache->Dynamic) { + // + // This entry is inserted by EfiIp6Neighbors() as dynamic entry + // and will be deleted after timeout. + // + Status = Ip6FreeNeighborEntry ( + IpSb, + NeighborCache, + FALSE, + TRUE, + EFI_TIMEOUT, + NULL, + NULL + ); + if (EFI_ERROR (Status)) { + return; + } + } else { + NeighborCache->State = EfiNeighborStale; + NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME; + } + } + + break; + + case EfiNeighborDelay: + if ((NeighborCache->Ticks == 0) || (--NeighborCache->Ticks == 0)) { + + NeighborCache->State = EfiNeighborProbe; + NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer); + NeighborCache->Transmit = IP6_MAX_UNICAST_SOLICIT + 1; + // + // Send out unicast neighbor solicitation for Neighbor Unreachability Detection + // + Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source); + if (EFI_ERROR (Status)) { + return; + } + + Status = Ip6SendNeighborSolicit ( + IpSb, + &Source, + &NeighborCache->Neighbor, + &NeighborCache->Neighbor, + &IpSb->SnpMode.CurrentAddress + ); + if (EFI_ERROR (Status)) { + return; + } + + NeighborCache->Transmit--; + } + + break; + + case EfiNeighborProbe: + if (NeighborCache->Ticks > 0) { + --NeighborCache->Ticks; + } + + // + // Retransmit Neighbor Solicitation messages approximately every + // RetransTimer milliseconds while awaiting a response. + // + if (NeighborCache->Ticks == 0) { + if (NeighborCache->Transmit > 1) { + // + // Send out unicast neighbor solicitation for Neighbor Unreachability + // Detection. After last neighbor solicitation message has been sent out, + // wait for RetransTimer and then remove entry if no response is received. + // + Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source); + if (EFI_ERROR (Status)) { + return; + } + + Status = Ip6SendNeighborSolicit ( + IpSb, + &Source, + &NeighborCache->Neighbor, + &NeighborCache->Neighbor, + &IpSb->SnpMode.CurrentAddress + ); + if (EFI_ERROR (Status)) { + return; + } + } + + // + // Update the retransmit times. + // + if (NeighborCache->Transmit > 0) { + --NeighborCache->Transmit; + NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer); + } + } + + if (NeighborCache->Transmit == 0) { + // + // Delete the neighbor entry. + // + Status = Ip6FreeNeighborEntry ( + IpSb, + NeighborCache, + FALSE, + TRUE, + EFI_TIMEOUT, + NULL, + NULL + ); + if (EFI_ERROR (Status)) { + return; + } + } + + break; + + default: + break; + } + } +} + +/** + The heartbeat timer of ND module in 1 second. This time routine handles following + things: 1) maitain default router list; 2) maintain prefix options; + 3) maintain route caches. + + @param[in] IpSb The IP6 service binding instance. + +**/ +VOID +Ip6NdTimerTicking ( + IN IP6_SERVICE *IpSb + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP6_DEFAULT_ROUTER *DefaultRouter; + IP6_PREFIX_LIST_ENTRY *PrefixOption; + UINT8 Index; + IP6_ROUTE_CACHE_ENTRY *RouteCache; + + // + // Decrease the lifetime of default router, if expires remove it from default router list. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->DefaultRouterList) { + DefaultRouter = NET_LIST_USER_STRUCT (Entry, IP6_DEFAULT_ROUTER, Link); + if (DefaultRouter->Lifetime != IP6_INF_ROUTER_LIFETIME) { + if ((DefaultRouter->Lifetime == 0) || (--DefaultRouter->Lifetime == 0)) { + Ip6DestroyDefaultRouter (IpSb, DefaultRouter); + } + } + } + + // + // Decrease Valid lifetime and Preferred lifetime of Prefix options and corresponding addresses. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->AutonomousPrefix) { + PrefixOption = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link); + if (PrefixOption->ValidLifetime != (UINT32) IP6_INFINIT_LIFETIME) { + if ((PrefixOption->ValidLifetime > 0) && (--PrefixOption->ValidLifetime > 0)) { + if ((PrefixOption->PreferredLifetime != (UINT32) IP6_INFINIT_LIFETIME) && + (PrefixOption->PreferredLifetime > 0) + ) { + --PrefixOption->PreferredLifetime; + } + } else { + Ip6DestroyPrefixListEntry (IpSb, PrefixOption, FALSE, TRUE); + } + } + } + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->OnlinkPrefix) { + PrefixOption = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link); + if (PrefixOption->ValidLifetime != (UINT32) IP6_INFINIT_LIFETIME) { + if ((PrefixOption->ValidLifetime == 0) || (--PrefixOption->ValidLifetime == 0)) { + Ip6DestroyPrefixListEntry (IpSb, PrefixOption, TRUE, TRUE); + } + } + } + + // + // Each bucket of route cache can contain at most IP6_ROUTE_CACHE_MAX entries. + // Remove the entries at the tail of the bucket. These entries + // are likely to be used least. + // Reclaim frequency is set to 1 second. + // + for (Index = 0; Index < IP6_ROUTE_CACHE_HASH_SIZE; Index++) { + while (IpSb->RouteTable->Cache.CacheNum[Index] > IP6_ROUTE_CACHE_MAX) { + Entry = NetListRemoveTail (&IpSb->RouteTable->Cache.CacheBucket[Index]); + if (Entry == NULL) { + break; + } + + RouteCache = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_CACHE_ENTRY, Link); + Ip6FreeRouteCacheEntry (RouteCache); + ASSERT (IpSb->RouteTable->Cache.CacheNum[Index] > 0); + IpSb->RouteTable->Cache.CacheNum[Index]--; + } + } +} + diff --git a/NetworkPkg/Ip6Dxe/Ip6Nd.h b/NetworkPkg/Ip6Dxe/Ip6Nd.h new file mode 100644 index 000000000..891a32d7d --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Nd.h @@ -0,0 +1,743 @@ +/** @file + Definition of Neighbor Discovery support routines. + + Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP6_ND_H__ +#define __EFI_IP6_ND_H__ + +#define IP6_GET_TICKS(Ms) (((Ms) + IP6_TIMER_INTERVAL_IN_MS - 1) / IP6_TIMER_INTERVAL_IN_MS) + +enum { + IP6_INF_ROUTER_LIFETIME = 0xFFFF, + + IP6_MAX_RTR_SOLICITATION_DELAY = 1000, ///< 1000 milliseconds + IP6_MAX_RTR_SOLICITATIONS = 3, + IP6_RTR_SOLICITATION_INTERVAL = 4000, + + IP6_MIN_RANDOM_FACTOR_SCALED = 1, + IP6_MAX_RANDOM_FACTOR_SCALED = 3, + IP6_RANDOM_FACTOR_SCALE = 2, + + IP6_MAX_MULTICAST_SOLICIT = 3, + IP6_MAX_UNICAST_SOLICIT = 3, + IP6_MAX_ANYCAST_DELAY_TIME = 1, + IP6_MAX_NEIGHBOR_ADV = 3, + IP6_REACHABLE_TIME = 30000, + IP6_RETRANS_TIMER = 1000, + IP6_DELAY_FIRST_PROBE_TIME = 5000, + + IP6_MIN_LINK_MTU = 1280, + IP6_MAX_LINK_MTU = 1500, + + IP6_IS_ROUTER_FLAG = 0x80, + IP6_SOLICITED_FLAG = 0x40, + IP6_OVERRIDE_FLAG = 0x20, + + IP6_M_ADDR_CONFIG_FLAG = 0x80, + IP6_O_CONFIG_FLAG = 0x40, + + IP6_ON_LINK_FLAG = 0x80, + IP6_AUTO_CONFIG_FLAG = 0x40, + + IP6_ND_LENGTH = 24, + IP6_RA_LENGTH = 16, + IP6_REDITECT_LENGTH = 40, + IP6_DAD_ENTRY_SIGNATURE = SIGNATURE_32 ('I', 'P', 'D', 'E') +}; + +typedef +VOID +(*IP6_ARP_CALLBACK) ( + VOID *Context + ); + +typedef struct _IP6_ETHE_ADDR_OPTION { + UINT8 Type; + UINT8 Length; + UINT8 EtherAddr[6]; +} IP6_ETHER_ADDR_OPTION; + +typedef struct _IP6_MTU_OPTION { + UINT8 Type; + UINT8 Length; + UINT16 Reserved; + UINT32 Mtu; +} IP6_MTU_OPTION; + +typedef struct _IP6_PREFIX_INFO_OPTION { + UINT8 Type; + UINT8 Length; + UINT8 PrefixLength; + UINT8 Reserved1; + UINT32 ValidLifetime; + UINT32 PreferredLifetime; + UINT32 Reserved2; + EFI_IPv6_ADDRESS Prefix; +} IP6_PREFIX_INFO_OPTION; + +typedef +VOID +(*IP6_DAD_CALLBACK) ( + IN BOOLEAN IsDadPassed, + IN EFI_IPv6_ADDRESS *TargetAddress, + IN VOID *Context + ); + +typedef struct _IP6_DAD_ENTRY { + UINT32 Signature; + LIST_ENTRY Link; + UINT32 MaxTransmit; + UINT32 Transmit; + UINT32 Receive; + UINT32 RetransTick; + IP6_ADDRESS_INFO *AddressInfo; + EFI_IPv6_ADDRESS Destination; + IP6_DAD_CALLBACK Callback; + VOID *Context; +} IP6_DAD_ENTRY; + +typedef struct _IP6_DELAY_JOIN_LIST { + LIST_ENTRY Link; + UINT32 DelayTime; ///< in tick per 50 milliseconds + IP6_INTERFACE *Interface; + IP6_ADDRESS_INFO *AddressInfo; + IP6_DAD_CALLBACK DadCallback; + VOID *Context; +} IP6_DELAY_JOIN_LIST; + +typedef struct _IP6_NEIGHBOR_ENTRY { + LIST_ENTRY Link; + LIST_ENTRY ArpList; + INTN RefCnt; + BOOLEAN IsRouter; + BOOLEAN ArpFree; + BOOLEAN Dynamic; + EFI_IPv6_ADDRESS Neighbor; + EFI_MAC_ADDRESS LinkAddress; + EFI_IP6_NEIGHBOR_STATE State; + UINT32 Transmit; + UINT32 Ticks; + + LIST_ENTRY Frames; + IP6_INTERFACE *Interface; + IP6_ARP_CALLBACK CallBack; +} IP6_NEIGHBOR_ENTRY; + +typedef struct _IP6_DEFAULT_ROUTER { + LIST_ENTRY Link; + INTN RefCnt; + UINT16 Lifetime; + EFI_IPv6_ADDRESS Router; + IP6_NEIGHBOR_ENTRY *NeighborCache; +} IP6_DEFAULT_ROUTER; + +typedef struct _IP6_PREFIX_LIST_ENTRY { + LIST_ENTRY Link; + INTN RefCnt; + UINT32 ValidLifetime; + UINT32 PreferredLifetime; + UINT8 PrefixLength; + EFI_IPv6_ADDRESS Prefix; +} IP6_PREFIX_LIST_ENTRY; + +/** + Build a array of EFI_IP6_NEIGHBOR_CACHE to be returned to the caller. The number + of EFI_IP6_NEIGHBOR_CACHE is also returned. + + @param[in] IpInstance The pointer to IP6_PROTOCOL instance. + @param[out] NeighborCount The number of returned neighbor cache entries. + @param[out] NeighborCache The pointer to the array of EFI_IP6_NEIGHBOR_CACHE. + + @retval EFI_SUCCESS The EFI_IP6_NEIGHBOR_CACHE successfully built. + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the route table. + +**/ +EFI_STATUS +Ip6BuildEfiNeighborCache ( + IN IP6_PROTOCOL *IpInstance, + OUT UINT32 *NeighborCount, + OUT EFI_IP6_NEIGHBOR_CACHE **NeighborCache + ); + +/** + Build a array of EFI_IP6_ADDRESS_INFO to be returned to the caller. The number + of prefix entries is also returned. + + @param[in] IpInstance The pointer to IP6_PROTOCOL instance. + @param[out] PrefixCount The number of returned prefix entries. + @param[out] PrefixTable The pointer to the array of PrefixTable. + + @retval EFI_SUCCESS The prefix table successfully built. + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the prefix table. + +**/ +EFI_STATUS +Ip6BuildPrefixTable ( + IN IP6_PROTOCOL *IpInstance, + OUT UINT32 *PrefixCount, + OUT EFI_IP6_ADDRESS_INFO **PrefixTable + ); + +/** + Allocate and initialize an IP6 default router entry. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] Ip6Address The IPv6 address of the default router. + @param[in] RouterLifetime The lifetime associated with the default + router, in units of seconds. + + @return NULL if it failed to allocate memory for the default router node. + Otherwise, point to the created default router node. + +**/ +IP6_DEFAULT_ROUTER * +Ip6CreateDefaultRouter ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Ip6Address, + IN UINT16 RouterLifetime + ); + +/** + Destroy an IP6 default router entry. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] DefaultRouter The to be destroyed IP6_DEFAULT_ROUTER. + +**/ +VOID +Ip6DestroyDefaultRouter ( + IN IP6_SERVICE *IpSb, + IN IP6_DEFAULT_ROUTER *DefaultRouter + ); + +/** + Clean an IP6 default router list. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + +**/ +VOID +Ip6CleanDefaultRouterList ( + IN IP6_SERVICE *IpSb + ); + +/** + Search a default router node from an IP6 default router list. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] Ip6Address The IPv6 address of the to be searched default router node. + + @return NULL if it failed to find the matching default router node. + Otherwise, point to the found default router node. + +**/ +IP6_DEFAULT_ROUTER * +Ip6FindDefaultRouter ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Ip6Address + ); + +/** + The function to be called after DAD (Duplicate Address Detection) is performed. + + @param[in] IsDadPassed If TRUE, the DAD operation succeed. Otherwise, the DAD operation failed. + @param[in] IpIf Points to the IP6_INTERFACE. + @param[in] DadEntry The DAD entry which already performed DAD. + +**/ +VOID +Ip6OnDADFinished ( + IN BOOLEAN IsDadPassed, + IN IP6_INTERFACE *IpIf, + IN IP6_DAD_ENTRY *DadEntry + ); + +/** + Create a DAD (Duplicate Address Detection) entry and queue it to be performed. + + @param[in] IpIf Points to the IP6_INTERFACE. + @param[in] AddressInfo The address information which needs DAD performed. + @param[in] Callback The callback routine that will be called after DAD + is performed. This is an optional parameter that + may be NULL. + @param[in] Context The opaque parameter for a DAD callback routine. + This is an optional parameter that may be NULL. + + @retval EFI_SUCCESS The DAD entry was created and queued. + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory to complete the + operation. + + +**/ +EFI_STATUS +Ip6InitDADProcess ( + IN IP6_INTERFACE *IpIf, + IN IP6_ADDRESS_INFO *AddressInfo, + IN IP6_DAD_CALLBACK Callback OPTIONAL, + IN VOID *Context OPTIONAL + ); + +/** + Search IP6_DAD_ENTRY from the Duplicate Address Detection List. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] Target The address information which needs DAD performed . + @param[out] Interface If not NULL, output the IP6 interface that configures + the tentative address. + + @return NULL if failed to find the matching DAD entry. + Otherwise, point to the found DAD entry. + +**/ +IP6_DAD_ENTRY * +Ip6FindDADEntry ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Target, + OUT IP6_INTERFACE **Interface OPTIONAL + ); + +/** + Allocate and initialize a IP6 prefix list entry. + + @param[in] IpSb The pointer to IP6_SERVICE instance. + @param[in] OnLinkOrAuto If TRUE, the entry is created for the on link prefix list. + Otherwise, it is created for the autoconfiguration prefix list. + @param[in] ValidLifetime The length of time in seconds that the prefix + is valid for the purpose of on-link determination. + @param[in] PreferredLifetime The length of time in seconds that addresses + generated from the prefix via stateless address + autoconfiguration remain preferred. + @param[in] PrefixLength The prefix length of the Prefix. + @param[in] Prefix The prefix address. + + @return NULL if it failed to allocate memory for the prefix node. Otherwise, point + to the created or existing prefix list entry. + +**/ +IP6_PREFIX_LIST_ENTRY * +Ip6CreatePrefixListEntry ( + IN IP6_SERVICE *IpSb, + IN BOOLEAN OnLinkOrAuto, + IN UINT32 ValidLifetime, + IN UINT32 PreferredLifetime, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *Prefix + ); + +/** + Destroy a IP6 prefix list entry. + + @param[in] IpSb The pointer to IP6_SERVICE instance. + @param[in] PrefixEntry The to be destroyed prefix list entry. + @param[in] OnLinkOrAuto If TRUE, the entry is removed from on link prefix list. + Otherwise remove from autoconfiguration prefix list. + @param[in] ImmediateDelete If TRUE, remove the entry directly. + Otherwise, check the reference count to see whether + it should be removed. + +**/ +VOID +Ip6DestroyPrefixListEntry ( + IN IP6_SERVICE *IpSb, + IN IP6_PREFIX_LIST_ENTRY *PrefixEntry, + IN BOOLEAN OnLinkOrAuto, + IN BOOLEAN ImmediateDelete + ); + +/** + Search the list array to find an IP6 prefix list entry. + + @param[in] IpSb The pointer to IP6_SERVICE instance. + @param[in] OnLinkOrAuto If TRUE, the search the link prefix list, + Otherwise search the autoconfiguration prefix list. + @param[in] PrefixLength The prefix length of the Prefix + @param[in] Prefix The prefix address. + + @return NULL if cannot find the IP6 prefix list entry. Otherwise, return the + pointer to the IP6 prefix list entry. + +**/ +IP6_PREFIX_LIST_ENTRY * +Ip6FindPrefixListEntry ( + IN IP6_SERVICE *IpSb, + IN BOOLEAN OnLinkOrAuto, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *Prefix + ); + +/** + Release the resource in prefix list table, and destroy the list entry and + corresponding addresses or route entries. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] ListHead The list entry head of the prefix list table. + +**/ +VOID +Ip6CleanPrefixListTable ( + IN IP6_SERVICE *IpSb, + IN LIST_ENTRY *ListHead + ); + +/** + Allocate and initialize an IP6 neighbor cache entry. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] CallBack The callback function to be called when + address resolution is finished. + @param[in] Ip6Address Points to the IPv6 address of the neighbor. + @param[in] LinkAddress Points to the MAC address of the neighbor. + Ignored if NULL. + + @return NULL if failed to allocate memory for the neighbor cache entry. + Otherwise, point to the created neighbor cache entry. + +**/ +IP6_NEIGHBOR_ENTRY * +Ip6CreateNeighborEntry ( + IN IP6_SERVICE *IpSb, + IN IP6_ARP_CALLBACK CallBack, + IN EFI_IPv6_ADDRESS *Ip6Address, + IN EFI_MAC_ADDRESS *LinkAddress OPTIONAL + ); + +/** + Search a IP6 neighbor cache entry. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] Ip6Address Points to the IPv6 address of the neighbor. + + @return NULL if it failed to find the matching neighbor cache entry. + Otherwise, point to the found neighbor cache entry. + +**/ +IP6_NEIGHBOR_ENTRY * +Ip6FindNeighborEntry ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Ip6Address + ); + +/** + Free a IP6 neighbor cache entry and remove all the frames on the address + resolution queue that pass the FrameToCancel. That is, either FrameToCancel + is NULL, or it returns true for the frame. + + @param[in] IpSb The pointer to the IP6_SERVICE instance. + @param[in] NeighborCache The to be free neighbor cache entry. + @param[in] SendIcmpError If TRUE, send out ICMP error. + @param[in] FullFree If TRUE, remove the neighbor cache entry. + Otherwise remove the pending frames. + @param[in] IoStatus The status returned to the cancelled frames' + callback function. + @param[in] FrameToCancel Function to select which frame to cancel. + This is an optional parameter that may be NULL. + @param[in] Context Opaque parameter to the FrameToCancel. + Ignored if FrameToCancel is NULL. + + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + @retval EFI_SUCCESS The operation finished successfully. + +**/ +EFI_STATUS +Ip6FreeNeighborEntry ( + IN IP6_SERVICE *IpSb, + IN IP6_NEIGHBOR_ENTRY *NeighborCache, + IN BOOLEAN SendIcmpError, + IN BOOLEAN FullFree, + IN EFI_STATUS IoStatus, + IN IP6_FRAME_TO_CANCEL FrameToCancel OPTIONAL, + IN VOID *Context OPTIONAL + ); + +/** + Add Neighbor cache entries. It is a work function for EfiIp6Neighbors(). + + @param[in] IpSb The IP6 service binding instance. + @param[in] TargetIp6Address Pointer to Target IPv6 address. + @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL. + @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor + cache. It will be deleted after Timeout. A value of zero means that + the entry is permanent. A non-zero value means that the entry is + dynamic. + @param[in] Override If TRUE, the cached link-layer address of the matching entry will + be overridden and updated; if FALSE, and if a + corresponding cache entry already existed, EFI_ACCESS_DENIED + will be returned. + + @retval EFI_SUCCESS The neighbor cache entry has been added. + @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache + due to insufficient resources. + @retval EFI_NOT_FOUND TargetLinkAddress is NULL. + @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache, + and that entry is tagged as un-overridden (when DeleteFlag + is FALSE). + +**/ +EFI_STATUS +Ip6AddNeighbor ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *TargetIp6Address, + IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL, + IN UINT32 Timeout, + IN BOOLEAN Override + ); + +/** + Delete or update Neighbor cache entries. It is a work function for EfiIp6Neighbors(). + + @param[in] IpSb The IP6 service binding instance. + @param[in] TargetIp6Address Pointer to Target IPv6 address. + @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL. + @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor + cache. It will be deleted after Timeout. A value of zero means that + the entry is permanent. A non-zero value means that the entry is + dynamic. + @param[in] Override If TRUE, the cached link-layer address of the matching entry will + be overridden and updated; if FALSE, and if a + corresponding cache entry already existed, EFI_ACCESS_DENIED + will be returned. + + @retval EFI_SUCCESS The neighbor cache entry has been updated or deleted. + @retval EFI_NOT_FOUND This entry is not in the neighbor cache. + +**/ +EFI_STATUS +Ip6DelNeighbor ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *TargetIp6Address, + IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL, + IN UINT32 Timeout, + IN BOOLEAN Override + ); + +/** + Process the Neighbor Solicitation message. The message may be sent for Duplicate + Address Detection or Address Resolution. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the message. + @param[in] Packet The content of the message with IP head removed. + + @retval EFI_SUCCESS The packet processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid. + @retval EFI_ICMP_ERROR The packet indicates that DAD is failed. + @retval Others Failed to process the packet. + +**/ +EFI_STATUS +Ip6ProcessNeighborSolicit ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ); + +/** + Process the Neighbor Advertisement message. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the message. + @param[in] Packet The content of the message with IP head removed. + + @retval EFI_SUCCESS The packet processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid. + @retval EFI_ICMP_ERROR The packet indicates that DAD is failed. + @retval Others Failed to process the packet. + +**/ +EFI_STATUS +Ip6ProcessNeighborAdvertise ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ); + +/** + Process the Router Advertisement message according to RFC4861. + + @param[in] IpSb The IP service that received the packet. + @param[in] Head The IP head of the message. + @param[in] Packet The content of the message with the IP head removed. + + @retval EFI_SUCCESS The packet processed successfully. + @retval EFI_INVALID_PARAMETER The packet is invalid. + @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the operation. + @retval Others Failed to process the packet. + +**/ +EFI_STATUS +Ip6ProcessRouterAdvertise ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ); + +/** + Process the ICMPv6 redirect message. Find the instance, then update + its route cache. + + @param[in] IpSb The IP6 service binding instance that received + the packet. + @param[in] Head The IP head of the received ICMPv6 packet. + @param[in] Packet The content of the ICMPv6 redirect packet with + the IP head removed. + + @retval EFI_INVALID_PARAMETER The parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Insuffcient resources to complete the + operation. + @retval EFI_SUCCESS Successfully updated the route caches. + +**/ +EFI_STATUS +Ip6ProcessRedirect ( + IN IP6_SERVICE *IpSb, + IN EFI_IP6_HEADER *Head, + IN NET_BUF *Packet + ); + +/** + Generate router solicit message and send it out to Destination Address or + All Router Link Local scope multicast address. + + @param[in] IpSb The IP service to send the packet. + @param[in] Interface If not NULL, points to the IP6 interface to send + the packet. + @param[in] SourceAddress If not NULL, the source address of the message. + @param[in] DestinationAddress If not NULL, the destination address of the message. + @param[in] SourceLinkAddress If not NULL, the MAC address of the source. + A source link-layer address option will be appended + to the message. + + @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the operation. + @retval EFI_SUCCESS The router solicit message was successfully sent. + +**/ +EFI_STATUS +Ip6SendRouterSolicit ( + IN IP6_SERVICE *IpSb, + IN IP6_INTERFACE *Interface OPTIONAL, + IN EFI_IPv6_ADDRESS *SourceAddress OPTIONAL, + IN EFI_IPv6_ADDRESS *DestinationAddress OPTIONAL, + IN EFI_MAC_ADDRESS *SourceLinkAddress OPTIONAL + ); + +/** + Generate the Neighbor Solicitation message and send it to the Destination Address. + + @param[in] IpSb The IP service to send the packet + @param[in] SourceAddress The source address of the message. + @param[in] DestinationAddress The destination address of the message. + @param[in] TargetIp6Address The IP address of the target of the solicitation. + It must not be a multicast address. + @param[in] SourceLinkAddress The MAC address for the sender. If not NULL, + a source link-layer address option will be appended + to the message. + + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the + operation. + @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent. + +**/ +EFI_STATUS +Ip6SendNeighborSolicit ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *SourceAddress, + IN EFI_IPv6_ADDRESS *DestinationAddress, + IN EFI_IPv6_ADDRESS *TargetIp6Address, + IN EFI_MAC_ADDRESS *SourceLinkAddress OPTIONAL + ); + +/** + Set the interface's address. This will trigger the DAD process for the + address to set. To set an already set address, the lifetimes wil be + updated to the new value passed in. + + @param[in] Interface The interface to set the address. + @param[in] Ip6Addr The interface's to be assigned IPv6 address. + @param[in] IsAnycast If TRUE, the unicast IPv6 address is anycast. + Otherwise, it is not anycast. + @param[in] PrefixLength The prefix length of the Ip6Addr. + @param[in] ValidLifetime The valid lifetime for this address. + @param[in] PreferredLifetime The preferred lifetime for this address. + @param[in] DadCallback The caller's callback to trigger when DAD finishes. + This is an optional parameter that may be NULL. + @param[in] Context The context that will be passed to DadCallback. + This is an optional parameter that may be NULL. + + @retval EFI_SUCCESS The interface is scheduled to be configured with + the specified address. + @retval EFI_OUT_OF_RESOURCES Failed to set the interface's address due to + lack of resources. + +**/ +EFI_STATUS +Ip6SetAddress ( + IN IP6_INTERFACE *Interface, + IN EFI_IPv6_ADDRESS *Ip6Addr, + IN BOOLEAN IsAnycast, + IN UINT8 PrefixLength, + IN UINT32 ValidLifetime, + IN UINT32 PreferredLifetime, + IN IP6_DAD_CALLBACK DadCallback OPTIONAL, + IN VOID *Context OPTIONAL + ); + +/** + The heartbeat timer of ND module in IP6_TIMER_INTERVAL_IN_MS milliseconds. + This time routine handles DAD module and neighbor state transition. + It is also responsible for sending out router solicitations. + + @param[in] Event The IP6 service instance's heartbeat timer. + @param[in] Context The IP6 service instance. + +**/ +VOID +EFIAPI +Ip6NdFasterTimerTicking ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + The heartbeat timer of ND module in 1 second. This time routine handles following + things: 1) maitain default router list; 2) maintain prefix options; + 3) maintain route caches. + + @param[in] IpSb The IP6 service binding instance. + +**/ +VOID +Ip6NdTimerTicking ( + IN IP6_SERVICE *IpSb + ); + +/** + Callback function when address resolution is finished. It will cancel + all the queued frames if the address resolution failed, or transmit them + if the request succeeded. + + @param[in] Context The context of the callback, a pointer to IP6_NEIGHBOR_ENTRY. + +**/ +VOID +Ip6OnArpResolved ( + IN VOID *Context + ); + +/** + Update the ReachableTime in IP6 service binding instance data, in milliseconds. + + @param[in, out] IpSb Points to the IP6_SERVICE. + +**/ +VOID +Ip6UpdateReachableTime ( + IN OUT IP6_SERVICE *IpSb + ); + +#endif diff --git a/NetworkPkg/Ip6Dxe/Ip6NvData.h b/NetworkPkg/Ip6Dxe/Ip6NvData.h new file mode 100644 index 000000000..eb36d7ed4 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6NvData.h @@ -0,0 +1,63 @@ +/** @file + NVData structure used by the IP6 configuration component. + + Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _IP6_NV_DATA_H_ +#define _IP6_NV_DATA_H_ + +#include + +#define FORMID_MAIN_FORM 1 +#define FORMID_MANUAL_CONFIG_FORM 2 +#define FORMID_HEAD_FORM 3 + +#define IP6_POLICY_AUTO 0 +#define IP6_POLICY_MANUAL 1 +#define DAD_MAX_TRANSMIT_COUNT 10 + +#define KEY_INTERFACE_ID 0x101 +#define KEY_MANUAL_ADDRESS 0x102 +#define KEY_GATEWAY_ADDRESS 0x103 +#define KEY_DNS_ADDRESS 0x104 +#define KEY_SAVE_CHANGES 0x105 +#define KEY_SAVE_CONFIG_CHANGES 0x106 +#define KEY_IGNORE_CONFIG_CHANGES 0x107 +#define KEY_GET_CURRENT_SETTING 0x108 + +#define HOST_ADDRESS_LABEL 0x9000 +#define ROUTE_TABLE_LABEL 0xa000 +#define GATEWAY_ADDRESS_LABEL 0xb000 +#define DNS_ADDRESS_LABEL 0xc000 +#define LABEL_END 0xffff + +#define INTERFACE_ID_STR_MIN_SIZE 1 +#define INTERFACE_ID_STR_MAX_SIZE 23 +#define INTERFACE_ID_STR_STORAGE 25 +#define IP6_STR_MAX_SIZE 40 +#define ADDRESS_STR_MIN_SIZE 2 +#define ADDRESS_STR_MAX_SIZE 255 + +/// +/// IP6_CONFIG_IFR_NVDATA contains the IP6 configure +/// parameters for that NIC. +/// +#pragma pack(1) +typedef struct { + UINT8 IfType; ///< interface type + UINT8 Padding[3]; + UINT32 Policy; ///< manual or automatic + UINT32 DadTransmitCount; ///< dad transmits count + CHAR16 InterfaceId[INTERFACE_ID_STR_STORAGE]; ///< alternative interface id + CHAR16 ManualAddress[ADDRESS_STR_MAX_SIZE]; ///< IP addresses + CHAR16 GatewayAddress[ADDRESS_STR_MAX_SIZE]; ///< Gateway address + CHAR16 DnsAddress[ADDRESS_STR_MAX_SIZE]; ///< DNS server address +} IP6_CONFIG_IFR_NVDATA; +#pragma pack() + +#endif + diff --git a/NetworkPkg/Ip6Dxe/Ip6Option.c b/NetworkPkg/Ip6Dxe/Ip6Option.c new file mode 100644 index 000000000..88111e3c2 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Option.c @@ -0,0 +1,752 @@ +/** @file + IP6 option support functions and routines. + + Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +/** + Validate the IP6 option format for both the packets we received + and that we will transmit. It will compute the ICMPv6 error message fields + if the option is malformatted. + + @param[in] IpSb The IP6 service data. + @param[in] Packet The to be validated packet. + @param[in] Option The first byte of the option. + @param[in] OptionLen The length of the whole option. + @param[in] Pointer Identifies the octet offset within + the invoking packet where the error was detected. + + + @retval TRUE The option is properly formatted. + @retval FALSE The option is malformatted. + +**/ +BOOLEAN +Ip6IsOptionValid ( + IN IP6_SERVICE *IpSb, + IN NET_BUF *Packet, + IN UINT8 *Option, + IN UINT8 OptionLen, + IN UINT32 Pointer + ) +{ + UINT8 Offset; + UINT8 OptionType; + + Offset = 0; + + while (Offset < OptionLen) { + OptionType = *(Option + Offset); + + switch (OptionType) { + case Ip6OptionPad1: + // + // It is a Pad1 option + // + Offset++; + break; + case Ip6OptionPadN: + // + // It is a PadN option + // + Offset = (UINT8) (Offset + *(Option + Offset + 1) + 2); + break; + case Ip6OptionRouterAlert: + // + // It is a Router Alert Option + // + Offset += 4; + break; + default: + // + // The highest-order two bits specify the action must be taken if + // the processing IPv6 node does not recognize the option type. + // + switch (OptionType & Ip6OptionMask) { + case Ip6OptionSkip: + Offset = (UINT8) (Offset + *(Option + Offset + 1)); + break; + case Ip6OptionDiscard: + return FALSE; + case Ip6OptionParameterProblem: + Pointer = Pointer + Offset + sizeof (EFI_IP6_HEADER); + Ip6SendIcmpError ( + IpSb, + Packet, + NULL, + &Packet->Ip.Ip6->SourceAddress, + ICMP_V6_PARAMETER_PROBLEM, + 2, + &Pointer + ); + return FALSE; + case Ip6OptionMask: + if (!IP6_IS_MULTICAST (&Packet->Ip.Ip6->DestinationAddress)) { + Pointer = Pointer + Offset + sizeof (EFI_IP6_HEADER); + Ip6SendIcmpError ( + IpSb, + Packet, + NULL, + &Packet->Ip.Ip6->SourceAddress, + ICMP_V6_PARAMETER_PROBLEM, + 2, + &Pointer + ); + } + + return FALSE; + break; + } + + break; + } + + } + + return TRUE; +} + +/** + Validate the IP6 option format for both the packets we received + and that we will transmit. It supports the defined options in Neighbor + Discovery messages. + + @param[in] Option The first byte of the option. + @param[in] OptionLen The length of the whole option. + + @retval TRUE The option is properly formatted. + @retval FALSE The option is malformatted. + +**/ +BOOLEAN +Ip6IsNDOptionValid ( + IN UINT8 *Option, + IN UINT16 OptionLen + ) +{ + UINT16 Offset; + UINT8 OptionType; + UINT16 Length; + + Offset = 0; + + while (Offset < OptionLen) { + OptionType = *(Option + Offset); + Length = (UINT16) (*(Option + Offset + 1) * 8); + + switch (OptionType) { + case Ip6OptionPrefixInfo: + if (Length != 32) { + return FALSE; + } + + break; + + case Ip6OptionMtu: + if (Length != 8) { + return FALSE; + } + + break; + + default: + // + // Check the length of Ip6OptionEtherSource, Ip6OptionEtherTarget, and + // Ip6OptionRedirected here. For unrecognized options, silently ignore + // and continue processsing the message. + // + if (Length == 0) { + return FALSE; + } + + break; + } + + Offset = (UINT16) (Offset + Length); + } + + return TRUE; +} + + +/** + Validate whether the NextHeader is a known valid protocol or one of the user configured + protocols from the upper layer. + + @param[in] IpSb The IP6 service instance. + @param[in] NextHeader The next header field. + + @retval TRUE The NextHeader is a known valid protocol or user configured. + @retval FALSE The NextHeader is not a known valid protocol. + +**/ +BOOLEAN +Ip6IsValidProtocol ( + IN IP6_SERVICE *IpSb, + IN UINT8 NextHeader + ) +{ + LIST_ENTRY *Entry; + IP6_PROTOCOL *IpInstance; + + if (NextHeader == EFI_IP_PROTO_TCP || + NextHeader == EFI_IP_PROTO_UDP || + NextHeader == IP6_ICMP || + NextHeader == IP6_ESP + ) { + return TRUE; + } + + if (IpSb == NULL) { + return FALSE; + } + + if (IpSb->Signature != IP6_SERVICE_SIGNATURE) { + return FALSE; + } + + NET_LIST_FOR_EACH (Entry, &IpSb->Children) { + IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE); + if (IpInstance->State == IP6_STATE_CONFIGED) { + if (IpInstance->ConfigData.DefaultProtocol == NextHeader) { + return TRUE; + } + } + } + + return FALSE; +} + +/** + Validate the IP6 extension header format for both the packets we received + and that we will transmit. It will compute the ICMPv6 error message fields + if the option is mal-formatted. + + @param[in] IpSb The IP6 service instance. This is an optional parameter. + @param[in] Packet The data of the packet. Ignored if NULL. + @param[in] NextHeader The next header field in IPv6 basic header. + @param[in] ExtHdrs The first byte of the option. + @param[in] ExtHdrsLen The length of the whole option. + @param[in] Rcvd The option is from the packet we received if TRUE, + otherwise, the option we want to transmit. + @param[out] FormerHeader The offset of NextHeader which points to Fragment + Header when we received, of the ExtHdrs. + Ignored if we transmit. + @param[out] LastHeader The pointer of NextHeader of the last extension + header processed by IP6. + @param[out] RealExtsLen The length of extension headers processed by IP6 layer. + This is an optional parameter that may be NULL. + @param[out] UnFragmentLen The length of unfragmented length of extension headers. + This is an optional parameter that may be NULL. + @param[out] Fragmented Indicate whether the packet is fragmented. + This is an optional parameter that may be NULL. + + @retval TRUE The option is properly formatted. + @retval FALSE The option is malformatted. + +**/ +BOOLEAN +Ip6IsExtsValid ( + IN IP6_SERVICE *IpSb OPTIONAL, + IN NET_BUF *Packet OPTIONAL, + IN UINT8 *NextHeader, + IN UINT8 *ExtHdrs, + IN UINT32 ExtHdrsLen, + IN BOOLEAN Rcvd, + OUT UINT32 *FormerHeader OPTIONAL, + OUT UINT8 **LastHeader, + OUT UINT32 *RealExtsLen OPTIONAL, + OUT UINT32 *UnFragmentLen OPTIONAL, + OUT BOOLEAN *Fragmented OPTIONAL + ) +{ + UINT32 Pointer; + UINT32 Offset; + UINT8 *Option; + UINT8 OptionLen; + BOOLEAN Flag; + UINT8 CountD; + UINT8 CountA; + IP6_FRAGMENT_HEADER *FragmentHead; + UINT16 FragmentOffset; + IP6_ROUTING_HEADER *RoutingHead; + + if (RealExtsLen != NULL) { + *RealExtsLen = 0; + } + + if (UnFragmentLen != NULL) { + *UnFragmentLen = 0; + } + + if (Fragmented != NULL) { + *Fragmented = FALSE; + } + + *LastHeader = NextHeader; + + if (ExtHdrs == NULL && ExtHdrsLen == 0) { + return TRUE; + } + + if ((ExtHdrs == NULL && ExtHdrsLen != 0) || (ExtHdrs != NULL && ExtHdrsLen == 0)) { + return FALSE; + } + + Pointer = 0; + Offset = 0; + Flag = FALSE; + CountD = 0; + CountA = 0; + + while (Offset <= ExtHdrsLen) { + + switch (*NextHeader) { + case IP6_HOP_BY_HOP: + if (Offset != 0) { + if (!Rcvd) { + return FALSE; + } + // + // Hop-by-Hop Options header is restricted to appear immediately after an IPv6 header only. + // If not, generate a ICMP parameter problem message with code value of 1. + // + if (Pointer == 0) { + Pointer = sizeof (EFI_IP6_HEADER); + } else { + Pointer = Offset + sizeof (EFI_IP6_HEADER); + } + + if ((IpSb != NULL) && (Packet != NULL) && + !IP6_IS_MULTICAST (&Packet->Ip.Ip6->DestinationAddress)) { + Ip6SendIcmpError ( + IpSb, + Packet, + NULL, + &Packet->Ip.Ip6->SourceAddress, + ICMP_V6_PARAMETER_PROBLEM, + 1, + &Pointer + ); + } + return FALSE; + } + + Flag = TRUE; + + // + // Fall through + // + case IP6_DESTINATION: + if (*NextHeader == IP6_DESTINATION) { + CountD++; + } + + if (CountD > 2) { + return FALSE; + } + + NextHeader = ExtHdrs + Offset; + Pointer = Offset; + + Offset++; + Option = ExtHdrs + Offset; + OptionLen = (UINT8) ((*Option + 1) * 8 - 2); + Option++; + Offset++; + + if (IpSb != NULL && Packet != NULL && !Ip6IsOptionValid (IpSb, Packet, Option, OptionLen, Offset)) { + return FALSE; + } + + Offset = Offset + OptionLen; + + if (Flag) { + if (UnFragmentLen != NULL) { + *UnFragmentLen = Offset; + } + + Flag = FALSE; + } + + break; + + case IP6_ROUTING: + NextHeader = ExtHdrs + Offset; + RoutingHead = (IP6_ROUTING_HEADER *) NextHeader; + + // + // Type 0 routing header is defined in RFC2460 and deprecated in RFC5095. + // Thus all routing types are processed as unrecognized. + // + if (RoutingHead->SegmentsLeft == 0) { + // + // Ignore the routing header and proceed to process the next header. + // + Offset = Offset + (RoutingHead->HeaderLen + 1) * 8; + + if (UnFragmentLen != NULL) { + *UnFragmentLen = Offset; + } + + } else { + // + // Discard the packet and send an ICMP Parameter Problem, Code 0, message + // to the packet's source address, pointing to the unrecognized routing + // type. + // + Pointer = Offset + 2 + sizeof (EFI_IP6_HEADER); + if ((IpSb != NULL) && (Packet != NULL) && + !IP6_IS_MULTICAST (&Packet->Ip.Ip6->DestinationAddress)) { + Ip6SendIcmpError ( + IpSb, + Packet, + NULL, + &Packet->Ip.Ip6->SourceAddress, + ICMP_V6_PARAMETER_PROBLEM, + 0, + &Pointer + ); + } + + return FALSE; + } + + break; + + case IP6_FRAGMENT: + + // + // RFC2402, AH header should after fragment header. + // + if (CountA > 1) { + return FALSE; + } + + // + // RFC2460, ICMP Parameter Problem message with code 0 should be sent + // if the length of a fragment is not a multiple of 8 octets and the M + // flag of that fragment is 1, pointing to the Payload length field of the + // fragment packet. + // + if (IpSb != NULL && Packet != NULL && (ExtHdrsLen % 8) != 0) { + // + // Check whether it is the last fragment. + // + FragmentHead = (IP6_FRAGMENT_HEADER *) (ExtHdrs + Offset); + if (FragmentHead == NULL) { + return FALSE; + } + + FragmentOffset = NTOHS (FragmentHead->FragmentOffset); + + if (((FragmentOffset & 0x1) == 0x1) && + !IP6_IS_MULTICAST (&Packet->Ip.Ip6->DestinationAddress)) { + Pointer = sizeof (UINT32); + Ip6SendIcmpError ( + IpSb, + Packet, + NULL, + &Packet->Ip.Ip6->SourceAddress, + ICMP_V6_PARAMETER_PROBLEM, + 0, + &Pointer + ); + return FALSE; + } + } + + if (Fragmented != NULL) { + *Fragmented = TRUE; + } + + if (Rcvd && FormerHeader != NULL) { + *FormerHeader = (UINT32) (NextHeader - ExtHdrs); + } + + NextHeader = ExtHdrs + Offset; + Offset = Offset + 8; + break; + + case IP6_AH: + if (++CountA > 1) { + return FALSE; + } + + Option = ExtHdrs + Offset; + NextHeader = Option; + Option++; + // + // RFC2402, Payload length is specified in 32-bit words, minus "2". + // + OptionLen = (UINT8) ((*Option + 2) * 4); + Offset = Offset + OptionLen; + break; + + case IP6_NO_NEXT_HEADER: + *LastHeader = NextHeader; + return FALSE; + break; + + default: + if (Ip6IsValidProtocol (IpSb, *NextHeader)) { + + *LastHeader = NextHeader; + + if (RealExtsLen != NULL) { + *RealExtsLen = Offset; + } + + return TRUE; + } + + // + // The Next Header value is unrecognized by the node, discard the packet and + // send an ICMP parameter problem message with code value of 1. + // + if (Offset == 0) { + // + // The Next Header directly follows IPv6 basic header. + // + Pointer = 6; + } else { + if (Pointer == 0) { + Pointer = sizeof (EFI_IP6_HEADER); + } else { + Pointer = Offset + sizeof (EFI_IP6_HEADER); + } + } + + if ((IpSb != NULL) && (Packet != NULL) && + !IP6_IS_MULTICAST (&Packet->Ip.Ip6->DestinationAddress)) { + Ip6SendIcmpError ( + IpSb, + Packet, + NULL, + &Packet->Ip.Ip6->SourceAddress, + ICMP_V6_PARAMETER_PROBLEM, + 1, + &Pointer + ); + } + return FALSE; + } + } + + *LastHeader = NextHeader; + + if (RealExtsLen != NULL) { + *RealExtsLen = Offset; + } + + return TRUE; +} + +/** + Generate an IPv6 router alert option in network order and output it through Buffer. + + @param[out] Buffer Points to a buffer to record the generated option. + @param[in, out] BufferLen The length of Buffer, in bytes. + @param[in] NextHeader The 8-bit selector indicates the type of header + immediately following the Hop-by-Hop Options header. + + @retval EFI_BUFFER_TOO_SMALL The Buffer is too small to contain the generated + option. BufferLen is updated for the required size. + + @retval EFI_SUCCESS The option is generated and filled in to Buffer. + +**/ +EFI_STATUS +Ip6FillHopByHop ( + OUT UINT8 *Buffer, + IN OUT UINTN *BufferLen, + IN UINT8 NextHeader + ) +{ + UINT8 BufferArray[8]; + + if (*BufferLen < 8) { + *BufferLen = 8; + return EFI_BUFFER_TOO_SMALL; + } + + // + // Form the Hop-By-Hop option in network order. + // NextHeader (1 octet) + HdrExtLen (1 octet) + RouterAlertOption(4 octets) + PadN + // The Hdr Ext Len is the length in 8-octet units, and does not including the first 8 octets. + // + ZeroMem (BufferArray, sizeof (BufferArray)); + BufferArray[0] = NextHeader; + BufferArray[2] = 0x5; + BufferArray[3] = 0x2; + BufferArray[6] = 1; + + CopyMem (Buffer, BufferArray, sizeof (BufferArray)); + return EFI_SUCCESS; +} + +/** + Insert a Fragment Header to the Extension headers and output it in UpdatedExtHdrs. + + @param[in] IpSb The IP6 service instance to transmit the packet. + @param[in] NextHeader The extension header type of first extension header. + @param[in] LastHeader The extension header type of last extension header. + @param[in] ExtHdrs The length of the original extension header. + @param[in] ExtHdrsLen The length of the extension headers. + @param[in] FragmentOffset The fragment offset of the data following the header. + @param[out] UpdatedExtHdrs The updated ExtHdrs with Fragment header inserted. + It's caller's responsibility to free this buffer. + + @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lake of + resource. + @retval EFI_UNSUPPORTED The extension header specified in ExtHdrs is not + supported currently. + @retval EFI_SUCCESS The operation performed successfully. + +**/ +EFI_STATUS +Ip6FillFragmentHeader ( + IN IP6_SERVICE *IpSb, + IN UINT8 NextHeader, + IN UINT8 LastHeader, + IN UINT8 *ExtHdrs, + IN UINT32 ExtHdrsLen, + IN UINT16 FragmentOffset, + OUT UINT8 **UpdatedExtHdrs + ) +{ + UINT32 Length; + UINT8 *Buffer; + UINT32 FormerHeader; + UINT32 Offset; + UINT32 Part1Len; + UINT32 HeaderLen; + UINT8 Current; + IP6_FRAGMENT_HEADER FragmentHead; + + if (UpdatedExtHdrs == NULL) { + return EFI_INVALID_PARAMETER; + } + + Length = ExtHdrsLen + sizeof (IP6_FRAGMENT_HEADER); + Buffer = AllocatePool (Length); + if (Buffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Offset = 0; + Part1Len = 0; + FormerHeader = 0; + Current = NextHeader; + + while ((ExtHdrs != NULL) && (Offset <= ExtHdrsLen)) { + switch (NextHeader) { + case IP6_ROUTING: + case IP6_HOP_BY_HOP: + case IP6_DESTINATION: + Current = NextHeader; + NextHeader = *(ExtHdrs + Offset); + + if ((Current == IP6_DESTINATION) && (NextHeader != IP6_ROUTING)) { + // + // Destination Options header should occur at most twice, once before + // a Routing header and once before the upper-layer header. Here we + // find the one before the upper-layer header. Insert the Fragment + // Header before it. + // + CopyMem (Buffer, ExtHdrs, Part1Len); + *(Buffer + FormerHeader) = IP6_FRAGMENT; + // + // Exit the loop. + // + Offset = ExtHdrsLen + 1; + break; + } + + + FormerHeader = Offset; + HeaderLen = (*(ExtHdrs + Offset + 1) + 1) * 8; + Part1Len = Part1Len + HeaderLen; + Offset = Offset + HeaderLen; + break; + + case IP6_FRAGMENT: + Current = NextHeader; + + if (Part1Len != 0) { + CopyMem (Buffer, ExtHdrs, Part1Len); + } + + *(Buffer + FormerHeader) = IP6_FRAGMENT; + + // + // Exit the loop. + // + Offset = ExtHdrsLen + 1; + break; + + case IP6_AH: + Current = NextHeader; + NextHeader = *(ExtHdrs + Offset); + // + // RFC2402, Payload length is specified in 32-bit words, minus "2". + // + HeaderLen = (*(ExtHdrs + Offset + 1) + 2) * 4; + Part1Len = Part1Len + HeaderLen; + Offset = Offset + HeaderLen; + break; + + default: + if (Ip6IsValidProtocol (IpSb, NextHeader)) { + Current = NextHeader; + CopyMem (Buffer, ExtHdrs, Part1Len); + *(Buffer + FormerHeader) = IP6_FRAGMENT; + // + // Exit the loop. + // + Offset = ExtHdrsLen + 1; + break; + } + + FreePool (Buffer); + return EFI_UNSUPPORTED; + } + } + + // + // Append the Fragment header. If the fragment offset indicates the fragment + // is the first fragment. + // + if ((FragmentOffset & IP6_FRAGMENT_OFFSET_MASK) == 0) { + FragmentHead.NextHeader = Current; + } else { + FragmentHead.NextHeader = LastHeader; + } + + FragmentHead.Reserved = 0; + FragmentHead.FragmentOffset = HTONS (FragmentOffset); + FragmentHead.Identification = mIp6Id; + + CopyMem (Buffer + Part1Len, &FragmentHead, sizeof (IP6_FRAGMENT_HEADER)); + + if ((ExtHdrs != NULL) && (Part1Len < ExtHdrsLen)) { + // + // Append the part2 (fragmentable part) of Extension headers + // + CopyMem ( + Buffer + Part1Len + sizeof (IP6_FRAGMENT_HEADER), + ExtHdrs + Part1Len, + ExtHdrsLen - Part1Len + ); + } + + *UpdatedExtHdrs = Buffer; + + return EFI_SUCCESS; +} + diff --git a/NetworkPkg/Ip6Dxe/Ip6Option.h b/NetworkPkg/Ip6Dxe/Ip6Option.h new file mode 100644 index 000000000..c81b3fda2 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Option.h @@ -0,0 +1,185 @@ +/** @file + Definition of IP6 option process routines. + + Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP6_OPTION_H__ +#define __EFI_IP6_OPTION_H__ + +#define IP6_FRAGMENT_OFFSET_MASK (~0x3) + +typedef struct _IP6_FRAGMENT_HEADER { + UINT8 NextHeader; + UINT8 Reserved; + UINT16 FragmentOffset; + UINT32 Identification; +} IP6_FRAGMENT_HEADER; + +typedef struct _IP6_ROUTING_HEADER { + UINT8 NextHeader; + UINT8 HeaderLen; + UINT8 RoutingType; + UINT8 SegmentsLeft; +} IP6_ROUTING_HEADER; + +typedef enum { + Ip6OptionPad1 = 0, + Ip6OptionPadN = 1, + Ip6OptionRouterAlert = 5, + Ip6OptionSkip = 0, + Ip6OptionDiscard = 0x40, + Ip6OptionParameterProblem = 0x80, + Ip6OptionMask = 0xc0, + + Ip6OptionEtherSource = 1, + Ip6OptionEtherTarget = 2, + Ip6OptionPrefixInfo = 3, + Ip6OptionRedirected = 4, + Ip6OptionMtu = 5 +} IP6_OPTION_TYPE; + +/** + Validate the IP6 extension header format for both the packets we received + and that we will transmit. It will compute the ICMPv6 error message fields + if the option is mal-formatted. + + @param[in] IpSb The IP6 service instance. This is an optional parameter. + @param[in] Packet The data of the packet. Ignored if NULL. + @param[in] NextHeader The next header field in IPv6 basic header. + @param[in] ExtHdrs The first byte of the option. + @param[in] ExtHdrsLen The length of the whole option. + @param[in] Rcvd The option is from the packet we received if TRUE, + otherwise, the option we want to transmit. + @param[out] FormerHeader The offset of NextHeader which points to Fragment + Header when we received, of the ExtHdrs. + Ignored if we transmit. + @param[out] LastHeader The pointer of NextHeader of the last extension + header processed by IP6. + @param[out] RealExtsLen The length of extension headers processed by IP6 layer. + This is an optional parameter that may be NULL. + @param[out] UnFragmentLen The length of unfragmented length of extension headers. + This is an optional parameter that may be NULL. + @param[out] Fragmented Indicate whether the packet is fragmented. + This is an optional parameter that may be NULL. + + @retval TRUE The option is properly formatted. + @retval FALSE The option is malformatted. + +**/ +BOOLEAN +Ip6IsExtsValid ( + IN IP6_SERVICE *IpSb OPTIONAL, + IN NET_BUF *Packet OPTIONAL, + IN UINT8 *NextHeader, + IN UINT8 *ExtHdrs, + IN UINT32 ExtHdrsLen, + IN BOOLEAN Rcvd, + OUT UINT32 *FormerHeader OPTIONAL, + OUT UINT8 **LastHeader, + OUT UINT32 *RealExtsLen OPTIONAL, + OUT UINT32 *UnFragmentLen OPTIONAL, + OUT BOOLEAN *Fragmented OPTIONAL + ); + +/** + Generate an IPv6 router alert option in network order and output it through Buffer. + + @param[out] Buffer Points to a buffer to record the generated option. + @param[in, out] BufferLen The length of Buffer, in bytes. + @param[in] NextHeader The 8-bit selector indicates the type of header + immediately following the Hop-by-Hop Options header. + + @retval EFI_BUFFER_TOO_SMALL The Buffer is too small to contain the generated + option. BufferLen is updated for the required size. + + @retval EFI_SUCCESS The option is generated and filled in to Buffer. + +**/ +EFI_STATUS +Ip6FillHopByHop ( + OUT UINT8 *Buffer, + IN OUT UINTN *BufferLen, + IN UINT8 NextHeader + ); + +/** + Insert a Fragment Header to the Extension headers and output it in UpdatedExtHdrs. + + @param[in] IpSb The IP6 service instance to transmit the packet. + @param[in] NextHeader The extension header type of first extension header. + @param[in] LastHeader The extension header type of last extension header. + @param[in] ExtHdrs The length of the original extension header. + @param[in] ExtHdrsLen The length of the extension headers. + @param[in] FragmentOffset The fragment offset of the data following the header. + @param[out] UpdatedExtHdrs The updated ExtHdrs with Fragment header inserted. + It's caller's responsibility to free this buffer. + + @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lake of + resource. + @retval EFI_UNSUPPORTED The extension header specified in ExtHdrs is not + supported currently. + @retval EFI_SUCCESS The operation performed successfully. + +**/ +EFI_STATUS +Ip6FillFragmentHeader ( + IN IP6_SERVICE *IpSb, + IN UINT8 NextHeader, + IN UINT8 LastHeader, + IN UINT8 *ExtHdrs, + IN UINT32 ExtHdrsLen, + IN UINT16 FragmentOffset, + OUT UINT8 **UpdatedExtHdrs + ); + +/** + Copy the extension headers from the original to buffer. A Fragment header is + appended to the end. + + @param[in] NextHeader The 8-bit selector indicates the type of + the fragment header's next header. + @param[in] ExtHdrs The length of the original extension header. + @param[in] LastHeader The pointer of next header of last extension header. + @param[in] FragmentOffset The fragment offset of the data following the header. + @param[in] UnFragmentHdrLen The length of unfragmented length of extension headers. + @param[in, out] Buf The buffer to copy options to. + @param[in, out] BufLen The length of the buffer. + + @retval EFI_SUCCESS The options are copied over. + @retval EFI_BUFFER_TOO_SMALL The buffer caller provided is too small. + +**/ +EFI_STATUS +Ip6CopyExts ( + IN UINT8 NextHeader, + IN UINT8 *ExtHdrs, + IN UINT8 *LastHeader, + IN UINT16 FragmentOffset, + IN UINT32 UnFragmentHdrLen, + IN OUT UINT8 *Buf, + IN OUT UINT32 *BufLen + ); + +/** + Validate the IP6 option format for both the packets we received + and that we will transmit. It supports the defined options in Neighbor + Discovery messages. + + @param[in] Option The first byte of the option. + @param[in] OptionLen The length of the whole option. + + @retval TRUE The option is properly formatted. + @retval FALSE The option is malformatted. + +**/ +BOOLEAN +Ip6IsNDOptionValid ( + IN UINT8 *Option, + IN UINT16 OptionLen + ); + +#endif diff --git a/NetworkPkg/Ip6Dxe/Ip6Output.c b/NetworkPkg/Ip6Dxe/Ip6Output.c new file mode 100644 index 000000000..6ab4459ba --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Output.c @@ -0,0 +1,1085 @@ +/** @file + The internal functions and routines to transmit the IP6 packet. + + Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +UINT32 mIp6Id; + +/** + Output all the available source addresses to a list entry head SourceList. The + number of source addresses are also returned. + + @param[in] IpSb Points to an IP6 service binding instance. + @param[out] SourceList The list entry head of all source addresses. + It is the caller's responsibility to free the + resources. + @param[out] SourceCount The number of source addresses. + + @retval EFI_SUCCESS The source addresses were copied to a list entry head + SourceList. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation. + +**/ +EFI_STATUS +Ip6CandidateSource ( + IN IP6_SERVICE *IpSb, + OUT LIST_ENTRY *SourceList, + OUT UINT32 *SourceCount + ) +{ + IP6_INTERFACE *IpIf; + LIST_ENTRY *Entry; + LIST_ENTRY *Entry2; + IP6_ADDRESS_INFO *AddrInfo; + IP6_ADDRESS_INFO *Copy; + + *SourceCount = 0; + + if (IpSb->LinkLocalOk) { + Copy = AllocatePool (sizeof (IP6_ADDRESS_INFO)); + if (Copy == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Copy->Signature = IP6_ADDR_INFO_SIGNATURE; + IP6_COPY_ADDRESS (&Copy->Address, &IpSb->LinkLocalAddr); + Copy->IsAnycast = FALSE; + Copy->PrefixLength = IP6_LINK_LOCAL_PREFIX_LENGTH; + Copy->ValidLifetime = (UINT32) IP6_INFINIT_LIFETIME; + Copy->PreferredLifetime = (UINT32) IP6_INFINIT_LIFETIME; + + InsertTailList (SourceList, &Copy->Link); + (*SourceCount)++; + } + + NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) { + IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link); + + NET_LIST_FOR_EACH (Entry2, &IpIf->AddressList) { + AddrInfo = NET_LIST_USER_STRUCT_S (Entry2, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE); + + if (AddrInfo->IsAnycast) { + // + // Never use an anycast address. + // + continue; + } + + Copy = AllocateCopyPool (sizeof (IP6_ADDRESS_INFO), AddrInfo); + if (Copy == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + InsertTailList (SourceList, &Copy->Link); + (*SourceCount)++; + } + } + + return EFI_SUCCESS; +} + +/** + Calculate how many bits are the same between two IPv6 addresses. + + @param[in] AddressA Points to an IPv6 address. + @param[in] AddressB Points to another IPv6 address. + + @return The common bits of the AddressA and AddressB. + +**/ +UINT8 +Ip6CommonPrefixLen ( + IN EFI_IPv6_ADDRESS *AddressA, + IN EFI_IPv6_ADDRESS *AddressB + ) +{ + UINT8 Count; + UINT8 Index; + UINT8 ByteA; + UINT8 ByteB; + UINT8 NumBits; + + Count = 0; + Index = 0; + + while (Index < 16) { + ByteA = AddressA->Addr[Index]; + ByteB = AddressB->Addr[Index]; + + if (ByteA == ByteB) { + Count += 8; + Index++; + continue; + } + + // + // Check how many bits are common between the two bytes. + // + NumBits = 8; + ByteA = (UINT8) (ByteA ^ ByteB); + + while (ByteA != 0) { + NumBits--; + ByteA = (UINT8) (ByteA >> 1); + } + + return (UINT8) (Count + NumBits); + } + + return Count; +} + +/** + Output all the available source addresses to a list entry head SourceList. The + number of source addresses are also returned. + + @param[in] IpSb Points to a IP6 service binding instance. + @param[in] Destination The IPv6 destination address. + @param[out] Source The selected IPv6 source address according to + the Destination. + + @retval EFI_SUCCESS The source addresses were copied to a list entry + head SourceList. + @retval EFI_NO_MAPPING The IPv6 stack is not auto configured. + +**/ +EFI_STATUS +Ip6SelectSourceAddress ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Destination, + OUT EFI_IPv6_ADDRESS *Source + ) +{ + EFI_STATUS Status; + LIST_ENTRY SourceList; + UINT32 SourceCount; + UINT8 ScopeD; + LIST_ENTRY *Entry; + IP6_ADDRESS_INFO *AddrInfo; + IP6_PREFIX_LIST_ENTRY *Prefix; + UINT8 LastCommonLength; + UINT8 CurrentCommonLength; + EFI_IPv6_ADDRESS *TmpAddress; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + Status = EFI_SUCCESS; + InitializeListHead (&SourceList); + + if (!IpSb->LinkLocalOk) { + return EFI_NO_MAPPING; + } + + // + // Rule 1: Prefer same address. + // + if (Ip6IsOneOfSetAddress (IpSb, Destination, NULL, NULL)) { + IP6_COPY_ADDRESS (Source, Destination); + goto Exit; + } + + // + // Rule 2: Prefer appropriate scope. + // + if (IP6_IS_MULTICAST (Destination)) { + ScopeD = (UINT8) (Destination->Addr[1] >> 4); + } else if (NetIp6IsLinkLocalAddr (Destination)) { + ScopeD = 0x2; + } else { + ScopeD = 0xE; + } + + if (ScopeD <= 0x2) { + // + // Return the link-local address if it exists + // One IP6_SERVICE only has one link-local address. + // + IP6_COPY_ADDRESS (Source, &IpSb->LinkLocalAddr); + goto Exit; + } + + // + // All candidate source addresses are global unicast address. + // + Ip6CandidateSource (IpSb, &SourceList, &SourceCount); + + if (SourceCount == 0) { + Status = EFI_NO_MAPPING; + goto Exit; + } + + IP6_COPY_ADDRESS (Source, &IpSb->LinkLocalAddr); + + if (SourceCount == 1) { + goto Exit; + } + + // + // Rule 3: Avoid deprecated addresses. + // TODO: check the "deprecated" state of the stateful configured address + // + NET_LIST_FOR_EACH (Entry, &IpSb->AutonomousPrefix) { + Prefix = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link); + if (Prefix->PreferredLifetime == 0) { + Ip6RemoveAddr (NULL, &SourceList, &SourceCount, &Prefix->Prefix, Prefix->PrefixLength); + + if (SourceCount == 1) { + goto Exit; + } + } + } + + // + // TODO: Rule 4: Prefer home addresses. + // TODO: Rule 5: Prefer outgoing interface. + // TODO: Rule 6: Prefer matching label. + // TODO: Rule 7: Prefer public addresses. + // + + // + // Rule 8: Use longest matching prefix. + // + LastCommonLength = Ip6CommonPrefixLen (Source, Destination); + TmpAddress = NULL; + + for (Entry = SourceList.ForwardLink; Entry != &SourceList; Entry = Entry->ForwardLink) { + AddrInfo = NET_LIST_USER_STRUCT_S (Entry, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE); + + CurrentCommonLength = Ip6CommonPrefixLen (&AddrInfo->Address, Destination); + if (CurrentCommonLength > LastCommonLength) { + LastCommonLength = CurrentCommonLength; + TmpAddress = &AddrInfo->Address; + } + } + + if (TmpAddress != NULL) { + IP6_COPY_ADDRESS (Source, TmpAddress); + } + +Exit: + + Ip6RemoveAddr (NULL, &SourceList, &SourceCount, NULL, 0); + + return Status; +} + +/** + Select an interface to send the packet generated in the IP6 driver + itself: that is, not by the requests of the IP6 child's consumer. Such + packets include the ICMPv6 echo replies and other ICMPv6 error packets. + + @param[in] IpSb The IP4 service that wants to send the packets. + @param[in] Destination The destination of the packet. + @param[in, out] Source The source of the packet. + + @return NULL if no proper interface is found, otherwise, the interface that + can be used to send the system packet from. + +**/ +IP6_INTERFACE * +Ip6SelectInterface ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Destination, + IN OUT EFI_IPv6_ADDRESS *Source + ) +{ + EFI_STATUS Status; + EFI_IPv6_ADDRESS SelectedSource; + IP6_INTERFACE *IpIf; + BOOLEAN Exist; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + ASSERT (Destination != NULL && Source != NULL); + + if (NetIp6IsUnspecifiedAddr (Destination)) { + return NULL; + } + + if (!NetIp6IsUnspecifiedAddr (Source)) { + Exist = Ip6IsOneOfSetAddress (IpSb, Source, &IpIf, NULL); + ASSERT (Exist); + + return IpIf; + } + + // + // If source is unspecified, select a source according to the destination. + // + Status = Ip6SelectSourceAddress (IpSb, Destination, &SelectedSource); + if (EFI_ERROR (Status)) { + return IpSb->DefaultInterface; + } + + Ip6IsOneOfSetAddress (IpSb, &SelectedSource, &IpIf, NULL); + IP6_COPY_ADDRESS (Source, &SelectedSource); + + return IpIf; +} + +/** + The default callback function for the system generated packet. + It will free the packet. + + @param[in] Packet The packet that transmitted. + @param[in] IoStatus The result of the transmission, succeeded or failed. + @param[in] LinkFlag Not used when transmitted. Check IP6_FRAME_CALLBACK + for reference. + @param[in] Context The context provided by us. + +**/ +VOID +Ip6SysPacketSent ( + NET_BUF *Packet, + EFI_STATUS IoStatus, + UINT32 LinkFlag, + VOID *Context + ) +{ + NetbufFree (Packet); + Packet = NULL; +} + +/** + Prefix an IP6 basic head and unfragmentable extension headers and a fragment header + to the Packet. Used for IP6 fragmentation. + + @param[in] IpSb The IP6 service instance to transmit the packet. + @param[in] Packet The packet to prefix the IP6 header to. + @param[in] Head The caller supplied header. + @param[in] FragmentOffset The fragment offset of the data following the header. + @param[in] ExtHdrs The length of the original extension header. + @param[in] ExtHdrsLen The length of the extension headers. + @param[in] LastHeader The pointer of next header of last extension header. + @param[in] HeadLen The length of the unfragmented part of the IP6 header. + + @retval EFI_BAD_BUFFER_SIZE There is no enough room in the head space of + Packet. + @retval EFI_SUCCESS The operation performed successfully. + +**/ +EFI_STATUS +Ip6PrependHead ( + IN IP6_SERVICE *IpSb, + IN NET_BUF *Packet, + IN EFI_IP6_HEADER *Head, + IN UINT16 FragmentOffset, + IN UINT8 *ExtHdrs, + IN UINT32 ExtHdrsLen, + IN UINT8 LastHeader, + IN UINT32 HeadLen + ) +{ + UINT32 Len; + UINT32 UnFragExtHdrsLen; + EFI_IP6_HEADER *PacketHead; + UINT8 *UpdatedExtHdrs; + EFI_STATUS Status; + UINT8 NextHeader; + + UpdatedExtHdrs = NULL; + + // + // HeadLen is the length of the fixed part of the sequences of fragments, i.e. + // the unfragment part. + // + PacketHead = (EFI_IP6_HEADER *) NetbufAllocSpace (Packet, HeadLen, NET_BUF_HEAD); + if (PacketHead == NULL) { + return EFI_BAD_BUFFER_SIZE; + } + + // + // Set the head up, convert the host byte order to network byte order + // + CopyMem (PacketHead, Head, sizeof (EFI_IP6_HEADER)); + PacketHead->PayloadLength = HTONS ((UINT16) (Packet->TotalSize - sizeof (EFI_IP6_HEADER))); + Packet->Ip.Ip6 = PacketHead; + + Len = HeadLen - sizeof (EFI_IP6_HEADER); + UnFragExtHdrsLen = Len - sizeof (IP6_FRAGMENT_HEADER); + + if (UnFragExtHdrsLen == 0) { + PacketHead->NextHeader = IP6_FRAGMENT; + } + + // + // Append the extension headers: firstly copy the unfragmentable headers, then append + // fragmentation header. + // + if ((FragmentOffset & IP6_FRAGMENT_OFFSET_MASK) == 0) { + NextHeader = Head->NextHeader; + } else { + NextHeader = PacketHead->NextHeader; + } + + Status = Ip6FillFragmentHeader ( + IpSb, + NextHeader, + LastHeader, + ExtHdrs, + ExtHdrsLen, + FragmentOffset, + &UpdatedExtHdrs + ); + if (EFI_ERROR (Status)) { + return Status; + } + + CopyMem ( + (UINT8 *) (PacketHead + 1), + UpdatedExtHdrs, + UnFragExtHdrsLen + sizeof (IP6_FRAGMENT_HEADER) + ); + + FreePool (UpdatedExtHdrs); + return EFI_SUCCESS; +} + +/** + Transmit an IP6 packet. The packet comes either from the IP6 + child's consumer (IpInstance != NULL) or the IP6 driver itself + (IpInstance == NULL). It will route the packet, fragment it, + then transmit all the fragments through an interface. + + @param[in] IpSb The IP6 service instance to transmit the packet. + @param[in] Interface The IP6 interface to transmit the packet. Ignored + if NULL. + @param[in] IpInstance The IP6 child that issues the transmission. It is + NULL if the packet is from the system. + @param[in] Packet The user data to send, excluding the IP header. + @param[in] Head The caller supplied header. The caller should set + the following header fields: NextHeader, HopLimit, + Src, Dest, FlowLabel, PayloadLength. This function + will fill in the Ver, TrafficClass. + @param[in] ExtHdrs The extension headers to append to the IPv6 basic + header. + @param[in] ExtHdrsLen The length of the extension headers. + @param[in] Callback The callback function to issue when transmission + completed. + @param[in] Context The opaque context for the callback. + + @retval EFI_INVALID_PARAMETER Any input parameter or the packet is invalid. + @retval EFI_NO_MAPPING There is no interface to the destination. + @retval EFI_NOT_FOUND There is no route to the destination. + @retval EFI_SUCCESS The packet successfully transmitted. + @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lack of + resources. + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Ip6Output ( + IN IP6_SERVICE *IpSb, + IN IP6_INTERFACE *Interface OPTIONAL, + IN IP6_PROTOCOL *IpInstance OPTIONAL, + IN NET_BUF *Packet, + IN EFI_IP6_HEADER *Head, + IN UINT8 *ExtHdrs, + IN UINT32 ExtHdrsLen, + IN IP6_FRAME_CALLBACK Callback, + IN VOID *Context + ) +{ + IP6_INTERFACE *IpIf; + EFI_IPv6_ADDRESS NextHop; + IP6_NEIGHBOR_ENTRY *NeighborCache; + IP6_ROUTE_CACHE_ENTRY *RouteCache; + EFI_STATUS Status; + UINT32 Mtu; + UINT32 HeadLen; + UINT16 FragmentOffset; + UINT8 *LastHeader; + UINT32 UnFragmentLen; + UINT32 UnFragmentHdrsLen; + UINT32 FragmentHdrsLen; + UINT16 *Checksum; + UINT16 PacketChecksum; + UINT16 PseudoChecksum; + UINT32 Index; + UINT32 PacketLen; + UINT32 RealExtLen; + UINT32 Offset; + NET_BUF *TmpPacket; + NET_BUF *Fragment; + UINT32 Num; + UINT8 *Buf; + EFI_IP6_HEADER *PacketHead; + IP6_ICMP_HEAD *IcmpHead; + IP6_TXTOKEN_WRAP *Wrap; + IP6_ROUTE_ENTRY *RouteEntry; + UINT8 *UpdatedExtHdrs; + UINT8 NextHeader; + UINT8 LastHeaderBackup; + BOOLEAN FragmentHeadInserted; + UINT8 *ExtHdrsBackup; + UINT8 NextHeaderBackup; + EFI_IPv6_ADDRESS Source; + EFI_IPv6_ADDRESS Destination; + + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + // + // RFC2460: Each extension header is an integer multiple of 8 octets long, + // in order to retain 8-octet alignment for subsequent headers. + // + if ((ExtHdrsLen & 0x7) != 0) { + return EFI_INVALID_PARAMETER; + } + + LastHeader = NULL; + + Ip6IsExtsValid ( + NULL, + NULL, + &Head->NextHeader, + ExtHdrs, + ExtHdrsLen, + FALSE, + NULL, + &LastHeader, + NULL, + NULL, + NULL + ); + + // + // Select an interface/source for system packet, application + // should select them itself. + // + IpIf = Interface; + if (IpIf == NULL) { + // + // IpInstance->Interface is NULL when IpInstance is configured with both stationaddress + // and destinationaddress is unspecified. + // + if (IpInstance == NULL || IpInstance->Interface == NULL) { + IpIf = Ip6SelectInterface (IpSb, &Head->DestinationAddress, &Head->SourceAddress); + if (IpInstance != NULL) { + IpInstance->Interface = IpIf; + } + } else { + IpIf = IpInstance->Interface; + } + } + + if (IpIf == NULL) { + return EFI_NO_MAPPING; + } + + // + // Update the common field in Head here. + // + Head->Version = 6; + Head->TrafficClassL = 0; + Head->TrafficClassH = 0; + + Checksum = NULL; + NextHeader = *LastHeader; + + switch (NextHeader) { + case EFI_IP_PROTO_UDP: + Packet->Udp = (EFI_UDP_HEADER *) NetbufGetByte (Packet, 0, NULL); + ASSERT (Packet->Udp != NULL); + if (Packet->Udp->Checksum == 0) { + Checksum = &Packet->Udp->Checksum; + } + break; + + case EFI_IP_PROTO_TCP: + Packet->Tcp = (TCP_HEAD *) NetbufGetByte (Packet, 0, NULL); + ASSERT (Packet->Tcp != NULL); + if (Packet->Tcp->Checksum == 0) { + Checksum = &Packet->Tcp->Checksum; + } + break; + + case IP6_ICMP: + // + // Don't send ICMP packet to an IPv6 anycast address. + // + if (Ip6IsAnycast (IpSb, &Head->DestinationAddress)) { + return EFI_INVALID_PARAMETER; + } + + IcmpHead = (IP6_ICMP_HEAD *) NetbufGetByte (Packet, 0, NULL); + ASSERT (IcmpHead != NULL); + if (IcmpHead->Checksum == 0) { + Checksum = &IcmpHead->Checksum; + } + break; + + default: + break; + } + + if (Checksum != NULL) { + // + // Calculate the checksum for upper layer protocol if it is not calculated due to lack of + // IPv6 source address. + // + PacketChecksum = NetbufChecksum (Packet); + PseudoChecksum = NetIp6PseudoHeadChecksum ( + &Head->SourceAddress, + &Head->DestinationAddress, + NextHeader, + Packet->TotalSize + ); + *Checksum = (UINT16) ~NetAddChecksum (PacketChecksum, PseudoChecksum); + } + + Status = Ip6IpSecProcessPacket ( + IpSb, + &Head, + LastHeader, // no need get the lasthead value for output + &Packet, + &ExtHdrs, + &ExtHdrsLen, + EfiIPsecOutBound, + Context + ); + + if (EFI_ERROR(Status)) { + return Status; + } + + LastHeader = NULL; + // + // Check incoming parameters. + // + if (!Ip6IsExtsValid ( + IpSb, + Packet, + &Head->NextHeader, + ExtHdrs, + ExtHdrsLen, + FALSE, + NULL, + &LastHeader, + &RealExtLen, + &UnFragmentHdrsLen, + NULL + )) { + return EFI_INVALID_PARAMETER; + } + + if ((RealExtLen & 0x7) != 0) { + return EFI_INVALID_PARAMETER; + } + + LastHeaderBackup = *LastHeader; + + // + // Perform next hop determination: + // For multicast packets, the next-hop is always the destination address and + // is considered to be on-link. + // + if (IP6_IS_MULTICAST (&Head->DestinationAddress)) { + IP6_COPY_ADDRESS (&NextHop, &Head->DestinationAddress); + } else { + // + // For unicast packets, use a combination of the Destination Cache, the Prefix List + // and the Default Router List to determine the IP address of the appropriate next hop. + // + + NeighborCache = Ip6FindNeighborEntry (IpSb, &Head->DestinationAddress); + if (NeighborCache != NULL) { + // + // Hit Neighbor Cache. + // + IP6_COPY_ADDRESS (&NextHop, &Head->DestinationAddress); + } else { + // + // Not in Neighbor Cache, check Router cache + // + RouteCache = Ip6Route (IpSb, &Head->DestinationAddress, &Head->SourceAddress); + if (RouteCache == NULL) { + return EFI_NOT_FOUND; + } + + IP6_COPY_ADDRESS (&NextHop, &RouteCache->NextHop); + Ip6FreeRouteCacheEntry (RouteCache); + } + } + + // + // Examines the Neighbor Cache for link-layer information about that neighbor. + // DO NOT create neighbor cache if neighbor is itself - when reporting ICMP error. + // + if (!IP6_IS_MULTICAST (&NextHop) && !EFI_IP6_EQUAL (&Head->DestinationAddress, &Head->SourceAddress)) { + NeighborCache = Ip6FindNeighborEntry (IpSb, &NextHop); + if (NeighborCache == NULL) { + NeighborCache = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, &NextHop, NULL); + + if (NeighborCache == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Send out multicast neighbor solicitation for address resolution immediately. + // + Ip6CreateSNMulticastAddr (&NeighborCache->Neighbor, &Destination); + Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = Ip6SendNeighborSolicit ( + IpSb, + &Source, + &Destination, + &NeighborCache->Neighbor, + &IpSb->SnpMode.CurrentAddress + ); + if (EFI_ERROR (Status)) { + return Status; + } + + --NeighborCache->Transmit; + NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer) + 1; + } + + NeighborCache->Interface = IpIf; + } + + UpdatedExtHdrs = NULL; + ExtHdrsBackup = NULL; + NextHeaderBackup = 0; + FragmentHeadInserted = FALSE; + + // + // Check whether we received Packet Too Big message for the packet sent to the + // Destination. If yes include a Fragment Header in the subsequent packets. + // + RouteEntry = Ip6FindRouteEntry ( + IpSb->RouteTable, + &Head->DestinationAddress, + NULL + ); + if (RouteEntry != NULL) { + if ((RouteEntry->Flag & IP6_PACKET_TOO_BIG) == IP6_PACKET_TOO_BIG) { + + // + // FragmentHead is inserted after Hop-by-Hop Options header, Destination + // Options header (first occur), Routing header, and before Fragment header, + // Authentication header, Encapsulating Security Payload header, and + // Destination Options header (last occur), and upper-layer header. + // + Status = Ip6FillFragmentHeader ( + IpSb, + Head->NextHeader, + LastHeaderBackup, + ExtHdrs, + ExtHdrsLen, + 0, + &UpdatedExtHdrs + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if ((ExtHdrs == NULL) && (ExtHdrsLen == 0)) { + NextHeaderBackup = Head->NextHeader; + Head->NextHeader = IP6_FRAGMENT; + } + + ExtHdrsBackup = ExtHdrs; + ExtHdrs = UpdatedExtHdrs; + ExtHdrsLen = ExtHdrsLen + sizeof (IP6_FRAGMENT_HEADER); + RealExtLen = RealExtLen + sizeof (IP6_FRAGMENT_HEADER); + + mIp6Id++; + + FragmentHeadInserted = TRUE; + } + + Ip6FreeRouteEntry (RouteEntry); + } + + // + // OK, selected the source and route, fragment the packet then send + // them. Tag each fragment other than the first one as spawn from it. + // Each extension header is an integer multiple of 8 octets long, in + // order to retain 8-octet alignment for subsequent headers. + // + Mtu = IpSb->MaxPacketSize + sizeof (EFI_IP6_HEADER); + HeadLen = sizeof (EFI_IP6_HEADER) + RealExtLen; + + if (Packet->TotalSize + HeadLen > Mtu) { + // + // Remove the inserted Fragment Header since we need fragment the packet. + // + if (FragmentHeadInserted) { + ExtHdrs = ExtHdrsBackup; + ExtHdrsLen = ExtHdrsLen - sizeof (IP6_FRAGMENT_HEADER); + + if ((ExtHdrs == NULL) && (ExtHdrsLen == 0)) { + Head->NextHeader = NextHeaderBackup; + } + } + + FragmentHdrsLen = ExtHdrsLen - UnFragmentHdrsLen; + + // + // The packet is beyond the maximum which can be described through the + // fragment offset field in Fragment header. + // + if ((((Packet->TotalSize + FragmentHdrsLen) >> 3) & (~0x1fff)) != 0) { + Status = EFI_BAD_BUFFER_SIZE; + goto Error; + } + + if (FragmentHdrsLen != 0) { + // + // Append the fragmentable extension hdrs before the upper layer payload + // to form a new NET_BUF. This NET_BUF contains all the buffer which will + // be fragmented below. + // + TmpPacket = NetbufGetFragment (Packet, 0, Packet->TotalSize, FragmentHdrsLen); + ASSERT (TmpPacket != NULL); + + // + // Allocate the space to contain the fragmentable hdrs and copy the data. + // + Buf = NetbufAllocSpace (TmpPacket, FragmentHdrsLen, TRUE); + ASSERT (Buf != NULL); + CopyMem (Buf, ExtHdrs + UnFragmentHdrsLen, FragmentHdrsLen); + + // + // Free the old Packet. + // + NetbufFree (Packet); + Packet = TmpPacket; + } + + // + // The unfragment part which appears in every fragmented IPv6 packet includes + // the IPv6 header, the unfragmentable extension hdrs and the fragment header. + // + UnFragmentLen = sizeof (EFI_IP6_HEADER) + UnFragmentHdrsLen + sizeof (IP6_FRAGMENT_HEADER); + + // + // Mtu now is the length of the fragment part in a full-length fragment. + // + Mtu = (Mtu - UnFragmentLen) & (~0x07); + Num = (Packet->TotalSize + Mtu - 1) / Mtu; + + for (Index = 0, Offset = 0, PacketLen = Mtu; Index < Num; Index++) { + // + // Get fragment from the Packet, append UnFragnmentLen spare buffer + // before the fragmented data, the corresponding data is filled in later. + // + Fragment = NetbufGetFragment (Packet, Offset, PacketLen, UnFragmentLen); + if (Fragment == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + FragmentOffset = (UINT16) ((UINT16) Offset | 0x1); + if (Index == Num - 1){ + // + // The last fragment, clear the M flag. + // + FragmentOffset &= (~0x1); + } + + Status = Ip6PrependHead ( + IpSb, + Fragment, + Head, + FragmentOffset, + ExtHdrs, + ExtHdrsLen, + LastHeaderBackup, + UnFragmentLen + ); + ASSERT (Status == EFI_SUCCESS); + + Status = Ip6SendFrame ( + IpIf, + IpInstance, + Fragment, + &NextHop, + Ip6SysPacketSent, + Packet + ); + if (EFI_ERROR (Status)) { + goto Error; + } + + // + // The last fragment of upper layer packet, update the IP6 token status. + // + if ((Index == Num -1) && (Context != NULL)) { + Wrap = (IP6_TXTOKEN_WRAP *) Context; + Wrap->Token->Status = Status; + } + + Offset += PacketLen; + PacketLen = Packet->TotalSize - Offset; + if (PacketLen > Mtu) { + PacketLen = Mtu; + } + } + + NetbufFree (Packet); + mIp6Id++; + + if (UpdatedExtHdrs != NULL) { + FreePool (UpdatedExtHdrs); + } + + return EFI_SUCCESS; + } + + // + // Need not fragment the packet, send it in one frame. + // + PacketHead = (EFI_IP6_HEADER *) NetbufAllocSpace (Packet, HeadLen, NET_BUF_HEAD); + if (PacketHead == NULL) { + Status = EFI_BAD_BUFFER_SIZE; + goto Error; + } + + CopyMem (PacketHead, Head, sizeof (EFI_IP6_HEADER)); + Packet->Ip.Ip6 = PacketHead; + + if (ExtHdrs != NULL) { + Buf = (UINT8 *) (PacketHead + 1); + CopyMem (Buf, ExtHdrs, ExtHdrsLen); + } + + if (UpdatedExtHdrs != NULL) { + // + // A Fragment Header is inserted to the packet, update the payload length. + // + PacketHead->PayloadLength = (UINT16) (NTOHS (PacketHead->PayloadLength) + + sizeof (IP6_FRAGMENT_HEADER)); + PacketHead->PayloadLength = HTONS (PacketHead->PayloadLength); + FreePool (UpdatedExtHdrs); + } + + return Ip6SendFrame ( + IpIf, + IpInstance, + Packet, + &NextHop, + Callback, + Context + ); + +Error: + if (UpdatedExtHdrs != NULL) { + FreePool (UpdatedExtHdrs); + } + Ip6CancelPacket (IpIf, Packet, Status); + return Status; +} + +/** + The filter function to find a packet and all its fragments. + The packet's fragments have their Context set to the packet. + + @param[in] Frame The frames hold by the low level interface. + @param[in] Context Context to the function, which is the packet. + + @retval TRUE This is the packet to cancel or its fragments. + @retval FALSE This is an unrelated packet. + +**/ +BOOLEAN +Ip6CancelPacketFragments ( + IN IP6_LINK_TX_TOKEN *Frame, + IN VOID *Context + ) +{ + if ((Frame->Packet == (NET_BUF *) Context) || (Frame->Context == Context)) { + return TRUE; + } + + return FALSE; +} + +/** + Remove all the frames on the interface that pass the FrameToCancel, + either queued on ARP queues or that have already been delivered to + MNP and not yet recycled. + + @param[in] Interface Interface to remove the frames from. + @param[in] IoStatus The transmit status returned to the frames' callback. + @param[in] FrameToCancel Function to select the frame to cancel; NULL to select all. + @param[in] Context Opaque parameters passed to FrameToCancel. Ignored if + FrameToCancel is NULL. + +**/ +VOID +Ip6CancelFrames ( + IN IP6_INTERFACE *Interface, + IN EFI_STATUS IoStatus, + IN IP6_FRAME_TO_CANCEL FrameToCancel OPTIONAL, + IN VOID *Context OPTIONAL + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP6_LINK_TX_TOKEN *Token; + IP6_SERVICE *IpSb; + IP6_NEIGHBOR_ENTRY *ArpQue; + EFI_STATUS Status; + + IpSb = Interface->Service; + NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE); + + // + // Cancel all the pending frames on ARP requests + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Interface->ArpQues) { + ArpQue = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, ArpList); + + Status = Ip6FreeNeighborEntry ( + IpSb, + ArpQue, + FALSE, + FALSE, + IoStatus, + FrameToCancel, + Context + ); + ASSERT_EFI_ERROR (Status); + } + + // + // Cancel all the frames that have been delivered to MNP + // but not yet recycled. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Interface->SentFrames) { + Token = NET_LIST_USER_STRUCT (Entry, IP6_LINK_TX_TOKEN, Link); + + if ((FrameToCancel == NULL) || FrameToCancel (Token, Context)) { + IpSb->Mnp->Cancel (IpSb->Mnp, &Token->MnpToken); + } + } +} + +/** + Cancel the Packet and all its fragments. + + @param[in] IpIf The interface from which the Packet is sent. + @param[in] Packet The Packet to cancel. + @param[in] IoStatus The status returns to the sender. + +**/ +VOID +Ip6CancelPacket ( + IN IP6_INTERFACE *IpIf, + IN NET_BUF *Packet, + IN EFI_STATUS IoStatus + ) +{ + Ip6CancelFrames (IpIf, IoStatus, Ip6CancelPacketFragments, Packet); +} + diff --git a/NetworkPkg/Ip6Dxe/Ip6Output.h b/NetworkPkg/Ip6Dxe/Ip6Output.h new file mode 100644 index 000000000..a2aa579d4 --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Output.h @@ -0,0 +1,135 @@ +/** @file + The internal functions and routines to transmit the IP6 packet. + + Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP6_OUTPUT_H__ +#define __EFI_IP6_OUTPUT_H__ + +extern UINT32 mIp6Id; + +/** + Output all the available source addresses to the list entry head SourceList. The + number of source addresses are also returned. + + @param[in] IpSb Points to a IP6 service binding instance. + @param[in] Destination The IPv6 destination address. + @param[out] Source The selected IPv6 source address according to + the Destination. + + @retval EFI_SUCCESS The source addresses were copied to the list entry + head SourceList. + @retval EFI_NO_MAPPING The IPv6 stack is not auto configured. + +**/ +EFI_STATUS +Ip6SelectSourceAddress ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Destination, + OUT EFI_IPv6_ADDRESS *Source + ); + +/** + The default callback function for system generated packet. + It will free the packet. + + @param[in] Packet The packet that transmitted. + @param[in] IoStatus The result of the transmission: succeeded or failed. + @param[in] LinkFlag Not used when transmission. Check IP6_FRAME_CALLBACK + for reference. + @param[in] Context The context provided by us. + +**/ +VOID +Ip6SysPacketSent ( + NET_BUF *Packet, + EFI_STATUS IoStatus, + UINT32 LinkFlag, + VOID *Context + ); + +/** + Transmit an IP6 packet. The packet comes either from the IP6 + child's consumer (IpInstance != NULL) or the IP6 driver itself + (IpInstance == NULL). It will route the packet, fragment it, + then transmit all the fragments through an interface. + + @param[in] IpSb The IP6 service instance to transmit the packet. + @param[in] Interface The IP6 interface to transmit the packet. Ignored + if NULL. + @param[in] IpInstance The IP6 child that issues the transmission. It is + NULL if the packet is from the system. + @param[in] Packet The user data to send, excluding the IP header. + @param[in] Head The caller supplied header. The caller should set + the following header fields: NextHeader, HopLimit, + Src, Dest, FlowLabel, PayloadLength. This function + will fill in the Ver, TrafficClass. + @param[in] ExtHdrs The extension headers to append to the IPv6 basic + header. + @param[in] ExtHdrsLen The length of the extension headers. + @param[in] Callback The callback function to issue when transmission + completed. + @param[in] Context The opaque context for the callback. + + @retval EFI_INVALID_PARAMETER Any input parameter or the packet is invalid. + @retval EFI_NO_MAPPING There is no interface to the destination. + @retval EFI_NOT_FOUND There is no route to the destination. + @retval EFI_SUCCESS The packet successfully transmitted. + @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lack of + resources. + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Ip6Output ( + IN IP6_SERVICE *IpSb, + IN IP6_INTERFACE *Interface OPTIONAL, + IN IP6_PROTOCOL *IpInstance OPTIONAL, + IN NET_BUF *Packet, + IN EFI_IP6_HEADER *Head, + IN UINT8 *ExtHdrs, + IN UINT32 ExtHdrsLen, + IN IP6_FRAME_CALLBACK Callback, + IN VOID *Context + ); + +/** + Remove all the frames on the interface that pass the FrameToCancel, + either queued on ARP queues, or that have already been delivered to + MNP and not yet recycled. + + @param[in] Interface Interface to remove the frames from. + @param[in] IoStatus The transmit status returned to the frames' callback. + @param[in] FrameToCancel Function to select the frame to cancel; NULL to select all. + @param[in] Context Opaque parameters passed to FrameToCancel. Ignored if + FrameToCancel is NULL. + +**/ +VOID +Ip6CancelFrames ( + IN IP6_INTERFACE *Interface, + IN EFI_STATUS IoStatus, + IN IP6_FRAME_TO_CANCEL FrameToCancel OPTIONAL, + IN VOID *Context OPTIONAL + ); + +/** + Cancel the Packet and all its fragments. + + @param[in] IpIf The interface from which the Packet is sent. + @param[in] Packet The Packet to cancel. + @param[in] IoStatus The status returns to the sender. + +**/ +VOID +Ip6CancelPacket ( + IN IP6_INTERFACE *IpIf, + IN NET_BUF *Packet, + IN EFI_STATUS IoStatus + ); + +#endif diff --git a/NetworkPkg/Ip6Dxe/Ip6Route.c b/NetworkPkg/Ip6Dxe/Ip6Route.c new file mode 100644 index 000000000..c8d79cd6d --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Route.c @@ -0,0 +1,629 @@ +/** @file + The functions and routines to handle the route caches and route table. + + Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Ip6Impl.h" + +/** + This is the worker function for IP6_ROUTE_CACHE_HASH(). It calculates the value + as the index of the route cache bucket according to the prefix of two IPv6 addresses. + + @param[in] Ip1 The IPv6 address. + @param[in] Ip2 The IPv6 address. + + @return The hash value of the prefix of two IPv6 addresses. + +**/ +UINT32 +Ip6RouteCacheHash ( + IN EFI_IPv6_ADDRESS *Ip1, + IN EFI_IPv6_ADDRESS *Ip2 + ) +{ + UINT32 Prefix1; + UINT32 Prefix2; + + Prefix1 = *((UINT32 *) ((UINTN *) (Ip1))); + Prefix2 = *((UINT32 *) ((UINTN *) (Ip2))); + + return ((UINT32) (Prefix1 ^ Prefix2) % IP6_ROUTE_CACHE_HASH_SIZE); +} + +/** + Allocate a route entry then initialize it with the Destination/PrefixLength + and Gateway. + + @param[in] Destination The IPv6 destination address. This is an optional + parameter that may be NULL. + @param[in] PrefixLength The destination network's prefix length. + @param[in] GatewayAddress The next hop address. This is an optional parameter + that may be NULL. + + @return NULL if failed to allocate memeory; otherwise, the newly created route entry. + +**/ +IP6_ROUTE_ENTRY * +Ip6CreateRouteEntry ( + IN EFI_IPv6_ADDRESS *Destination OPTIONAL, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *GatewayAddress OPTIONAL + ) +{ + IP6_ROUTE_ENTRY *RtEntry; + + RtEntry = AllocateZeroPool (sizeof (IP6_ROUTE_ENTRY)); + + if (RtEntry == NULL) { + return NULL; + } + + RtEntry->RefCnt = 1; + RtEntry->Flag = 0; + RtEntry->PrefixLength = PrefixLength; + + if (Destination != NULL) { + IP6_COPY_ADDRESS (&RtEntry->Destination, Destination); + } + + if (GatewayAddress != NULL) { + IP6_COPY_ADDRESS (&RtEntry->NextHop, GatewayAddress); + } + + return RtEntry; +} + +/** + Free the route table entry. It is reference counted. + + @param[in, out] RtEntry The route entry to free. + +**/ +VOID +Ip6FreeRouteEntry ( + IN OUT IP6_ROUTE_ENTRY *RtEntry + ) +{ + ASSERT ((RtEntry != NULL) && (RtEntry->RefCnt > 0)); + + if (--RtEntry->RefCnt == 0) { + FreePool (RtEntry); + } +} + +/** + Search the route table for a most specific match to the Dst. It searches + from the longest route area (prefix length == 128) to the shortest route area + (default routes). In each route area, it will first search the instance's + route table, then the default route table. This is required per the following + requirements: + 1. IP search the route table for a most specific match. + 2. The local route entries have precedence over the default route entry. + + @param[in] RtTable The route table to search from. + @param[in] Destination The destionation address to search. If NULL, search + the route table by NextHop. + @param[in] NextHop The next hop address. If NULL, search the route table + by Destination. + + @return NULL if no route matches the Dst. Otherwise, the point to the + @return most specific route to the Dst. + +**/ +IP6_ROUTE_ENTRY * +Ip6FindRouteEntry ( + IN IP6_ROUTE_TABLE *RtTable, + IN EFI_IPv6_ADDRESS *Destination OPTIONAL, + IN EFI_IPv6_ADDRESS *NextHop OPTIONAL + ) +{ + LIST_ENTRY *Entry; + IP6_ROUTE_ENTRY *RtEntry; + INTN Index; + + ASSERT (Destination != NULL || NextHop != NULL); + + RtEntry = NULL; + + for (Index = IP6_PREFIX_MAX; Index >= 0; Index--) { + NET_LIST_FOR_EACH (Entry, &RtTable->RouteArea[Index]) { + RtEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_ENTRY, Link); + + if (Destination != NULL) { + if (NetIp6IsNetEqual (Destination, &RtEntry->Destination, RtEntry->PrefixLength)) { + NET_GET_REF (RtEntry); + return RtEntry; + } + } else if (NextHop != NULL) { + if (NetIp6IsNetEqual (NextHop, &RtEntry->NextHop, RtEntry->PrefixLength)) { + NET_GET_REF (RtEntry); + return RtEntry; + } + } + + } + } + + return NULL; +} + +/** + Allocate and initialize a IP6 route cache entry. + + @param[in] Dst The destination address. + @param[in] Src The source address. + @param[in] GateWay The next hop address. + @param[in] Tag The tag from the caller. This marks all the cache entries + spawned from one route table entry. + + @return NULL if failed to allocate memory for the cache. Otherwise, point + to the created route cache entry. + +**/ +IP6_ROUTE_CACHE_ENTRY * +Ip6CreateRouteCacheEntry ( + IN EFI_IPv6_ADDRESS *Dst, + IN EFI_IPv6_ADDRESS *Src, + IN EFI_IPv6_ADDRESS *GateWay, + IN UINTN Tag + ) +{ + IP6_ROUTE_CACHE_ENTRY *RtCacheEntry; + + RtCacheEntry = AllocatePool (sizeof (IP6_ROUTE_CACHE_ENTRY)); + + if (RtCacheEntry == NULL) { + return NULL; + } + + RtCacheEntry->RefCnt = 1; + RtCacheEntry->Tag = Tag; + + IP6_COPY_ADDRESS (&RtCacheEntry->Destination, Dst); + IP6_COPY_ADDRESS (&RtCacheEntry->Source, Src); + IP6_COPY_ADDRESS (&RtCacheEntry->NextHop, GateWay); + + return RtCacheEntry; +} + +/** + Free the route cache entry. It is reference counted. + + @param[in, out] RtCacheEntry The route cache entry to free. + +**/ +VOID +Ip6FreeRouteCacheEntry ( + IN OUT IP6_ROUTE_CACHE_ENTRY *RtCacheEntry + ) +{ + ASSERT (RtCacheEntry->RefCnt > 0); + + if (--RtCacheEntry->RefCnt == 0) { + FreePool (RtCacheEntry); + } +} + +/** + Find a route cache with the destination and source address. This is + used by the ICMPv6 redirect messasge process. + + @param[in] RtTable The route table to search the cache for. + @param[in] Dest The destination address. + @param[in] Src The source address. + + @return NULL if no route entry to the (Dest, Src). Otherwise, the pointer + to the correct route cache entry. + +**/ +IP6_ROUTE_CACHE_ENTRY * +Ip6FindRouteCache ( + IN IP6_ROUTE_TABLE *RtTable, + IN EFI_IPv6_ADDRESS *Dest, + IN EFI_IPv6_ADDRESS *Src + ) +{ + LIST_ENTRY *Entry; + IP6_ROUTE_CACHE_ENTRY *RtCacheEntry; + UINT32 Index; + + Index = IP6_ROUTE_CACHE_HASH (Dest, Src); + + NET_LIST_FOR_EACH (Entry, &RtTable->Cache.CacheBucket[Index]) { + RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_CACHE_ENTRY, Link); + + if (EFI_IP6_EQUAL (Dest, &RtCacheEntry->Destination)&& EFI_IP6_EQUAL (Src, &RtCacheEntry->Source)) { + NET_GET_REF (RtCacheEntry); + return RtCacheEntry; + } + } + + return NULL; +} + +/** + Build an array of EFI_IP6_ROUTE_TABLE to be returned to the caller. The number + of EFI_IP6_ROUTE_TABLE is also returned. + + @param[in] RouteTable The pointer of IP6_ROUTE_TABLE internal used. + @param[out] EfiRouteCount The number of returned route entries. + @param[out] EfiRouteTable The pointer to the array of EFI_IP6_ROUTE_TABLE. + If NULL, only the route entry count is returned. + + @retval EFI_SUCCESS The EFI_IP6_ROUTE_TABLE successfully built. + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the route table. + +**/ +EFI_STATUS +Ip6BuildEfiRouteTable ( + IN IP6_ROUTE_TABLE *RouteTable, + OUT UINT32 *EfiRouteCount, + OUT EFI_IP6_ROUTE_TABLE **EfiRouteTable OPTIONAL + ) +{ + LIST_ENTRY *Entry; + IP6_ROUTE_ENTRY *RtEntry; + EFI_IP6_ROUTE_TABLE *EfiTable; + UINT32 Count; + INT32 Index; + + ASSERT (EfiRouteCount != NULL); + + Count = RouteTable->TotalNum; + *EfiRouteCount = Count; + + if ((EfiRouteTable == NULL) || (Count == 0)) { + return EFI_SUCCESS; + } + + if (*EfiRouteTable == NULL) { + *EfiRouteTable = AllocatePool (sizeof (EFI_IP6_ROUTE_TABLE) * Count); + if (*EfiRouteTable == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + + EfiTable = *EfiRouteTable; + + // + // Copy the route entry to EFI route table. + // + Count = 0; + + for (Index = IP6_PREFIX_MAX; Index >= 0; Index--) { + + NET_LIST_FOR_EACH (Entry, &(RouteTable->RouteArea[Index])) { + RtEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_ENTRY, Link); + + Ip6CopyAddressByPrefix ( + &EfiTable[Count].Destination, + &RtEntry->Destination, + RtEntry->PrefixLength + ); + + IP6_COPY_ADDRESS (&EfiTable[Count].Gateway, &RtEntry->NextHop); + EfiTable[Count].PrefixLength = RtEntry->PrefixLength; + + Count++; + } + } + + ASSERT (Count == RouteTable->TotalNum); + + return EFI_SUCCESS; +} + +/** + Create an empty route table. This includes its internal route cache. + + @return NULL if failed to allocate memory for the route table. Otherwise, + the point to newly created route table. + +**/ +IP6_ROUTE_TABLE * +Ip6CreateRouteTable ( + VOID + ) +{ + IP6_ROUTE_TABLE *RtTable; + UINT32 Index; + + RtTable = AllocatePool (sizeof (IP6_ROUTE_TABLE)); + if (RtTable == NULL) { + return NULL; + } + + RtTable->RefCnt = 1; + RtTable->TotalNum = 0; + + for (Index = 0; Index <= IP6_PREFIX_MAX; Index++) { + InitializeListHead (&RtTable->RouteArea[Index]); + } + + for (Index = 0; Index < IP6_ROUTE_CACHE_HASH_SIZE; Index++) { + InitializeListHead (&RtTable->Cache.CacheBucket[Index]); + RtTable->Cache.CacheNum[Index] = 0; + } + + return RtTable; +} + +/** + Free the route table and its associated route cache. Route + table is reference counted. + + @param[in, out] RtTable The route table to free. + +**/ +VOID +Ip6CleanRouteTable ( + IN OUT IP6_ROUTE_TABLE *RtTable + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP6_ROUTE_ENTRY *RtEntry; + IP6_ROUTE_CACHE_ENTRY *RtCacheEntry; + UINT32 Index; + + ASSERT (RtTable->RefCnt > 0); + + if (--RtTable->RefCnt > 0) { + return ; + } + + // + // Free all the route table entry and its route cache. + // + for (Index = 0; Index <= IP6_PREFIX_MAX; Index++) { + NET_LIST_FOR_EACH_SAFE (Entry, Next, &RtTable->RouteArea[Index]) { + RtEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_ENTRY, Link); + RemoveEntryList (Entry); + Ip6FreeRouteEntry (RtEntry); + } + } + + for (Index = 0; Index < IP6_ROUTE_CACHE_HASH_SIZE; Index++) { + NET_LIST_FOR_EACH_SAFE (Entry, Next, &RtTable->Cache.CacheBucket[Index]) { + RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_CACHE_ENTRY, Link); + RemoveEntryList (Entry); + Ip6FreeRouteCacheEntry (RtCacheEntry); + } + } + + FreePool (RtTable); +} + +/** + Remove all the cache entries bearing the Tag. When a route cache + entry is created, it is tagged with the address of route entry + from which it is spawned. When a route entry is deleted, the cache + entries spawned from it are also deleted. + + @param[in] RtCache Route cache to remove the entries from. + @param[in] Tag The Tag of the entries to remove. + +**/ +VOID +Ip6PurgeRouteCache ( + IN IP6_ROUTE_CACHE *RtCache, + IN UINTN Tag + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP6_ROUTE_CACHE_ENTRY *RtCacheEntry; + UINT32 Index; + + for (Index = 0; Index < IP6_ROUTE_CACHE_HASH_SIZE; Index++) { + NET_LIST_FOR_EACH_SAFE (Entry, Next, &RtCache->CacheBucket[Index]) { + + RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_CACHE_ENTRY, Link); + + if (RtCacheEntry->Tag == Tag) { + RemoveEntryList (Entry); + Ip6FreeRouteCacheEntry (RtCacheEntry); + } + } + } +} + +/** + Add a route entry to the route table. It is the help function for EfiIp6Routes. + + @param[in, out] RtTable Route table to add route to. + @param[in] Destination The destination of the network. + @param[in] PrefixLength The PrefixLength of the destination. + @param[in] GatewayAddress The next hop address. + + @retval EFI_ACCESS_DENIED The same route already exists. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the entry. + @retval EFI_SUCCESS The route was added successfully. + +**/ +EFI_STATUS +Ip6AddRoute ( + IN OUT IP6_ROUTE_TABLE *RtTable, + IN EFI_IPv6_ADDRESS *Destination, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *GatewayAddress + ) +{ + LIST_ENTRY *ListHead; + LIST_ENTRY *Entry; + IP6_ROUTE_ENTRY *Route; + + ListHead = &RtTable->RouteArea[PrefixLength]; + + // + // First check whether the route exists + // + NET_LIST_FOR_EACH (Entry, ListHead) { + Route = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_ENTRY, Link); + + if (NetIp6IsNetEqual (Destination, &Route->Destination, PrefixLength) && + EFI_IP6_EQUAL (GatewayAddress, &Route->NextHop)) { + return EFI_ACCESS_DENIED; + } + } + + // + // Create a route entry and insert it to the route area. + // + Route = Ip6CreateRouteEntry (Destination, PrefixLength, GatewayAddress); + + if (Route == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if (NetIp6IsUnspecifiedAddr (GatewayAddress)) { + Route->Flag = IP6_DIRECT_ROUTE; + } + + InsertHeadList (ListHead, &Route->Link); + RtTable->TotalNum++; + + return EFI_SUCCESS; +} + +/** + Remove a route entry and all the route caches spawn from it. + It is the help function for EfiIp6Routes. + + @param[in, out] RtTable The route table to remove the route from. + @param[in] Destination The destination network. + @param[in] PrefixLength The PrefixLength of the Destination. + @param[in] GatewayAddress The next hop address. + + @retval EFI_SUCCESS The route entry was successfully removed. + @retval EFI_NOT_FOUND There is no route entry in the table with that + property. + +**/ +EFI_STATUS +Ip6DelRoute ( + IN OUT IP6_ROUTE_TABLE *RtTable, + IN EFI_IPv6_ADDRESS *Destination, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *GatewayAddress + ) +{ + LIST_ENTRY *ListHead; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + IP6_ROUTE_ENTRY *Route; + UINT32 TotalNum; + + ListHead = &RtTable->RouteArea[PrefixLength]; + TotalNum = RtTable->TotalNum; + + NET_LIST_FOR_EACH_SAFE (Entry, Next, ListHead) { + Route = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_ENTRY, Link); + + if (Destination != NULL && !NetIp6IsNetEqual (Destination, &Route->Destination, PrefixLength)) { + continue; + } + if (GatewayAddress != NULL && !EFI_IP6_EQUAL (GatewayAddress, &Route->NextHop)) { + continue; + } + + Ip6PurgeRouteCache (&RtTable->Cache, (UINTN) Route); + RemoveEntryList (Entry); + Ip6FreeRouteEntry (Route); + + ASSERT (RtTable->TotalNum > 0); + RtTable->TotalNum--; + } + + return TotalNum == RtTable->TotalNum ? EFI_NOT_FOUND : EFI_SUCCESS; +} + +/** + Search the route table to route the packet. Return/create a route + cache if there is a route to the destination. + + @param[in] IpSb The IP6 service data. + @param[in] Dest The destination address to search for. + @param[in] Src The source address to search for. + + @return NULL if it failed to route the packet. Otherwise, a route cache + entry that can be used to route packets. + +**/ +IP6_ROUTE_CACHE_ENTRY * +Ip6Route ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Dest, + IN EFI_IPv6_ADDRESS *Src + ) +{ + IP6_ROUTE_TABLE *RtTable; + LIST_ENTRY *ListHead; + IP6_ROUTE_CACHE_ENTRY *RtCacheEntry; + IP6_ROUTE_ENTRY *RtEntry; + EFI_IPv6_ADDRESS NextHop; + UINT32 Index; + + RtTable = IpSb->RouteTable; + + ASSERT (RtTable != NULL); + + // + // Search the destination cache in IP6_ROUTE_TABLE. + // + Index = IP6_ROUTE_CACHE_HASH (Dest, Src); + ListHead = &RtTable->Cache.CacheBucket[Index]; + + RtCacheEntry = Ip6FindRouteCache (RtTable, Dest, Src); + + // + // If found, promote the cache entry to the head of the hash bucket. + // + if (RtCacheEntry != NULL) { + RemoveEntryList (&RtCacheEntry->Link); + InsertHeadList (ListHead, &RtCacheEntry->Link); + return RtCacheEntry; + } + + // + // Search the route table for the most specific route + // + RtEntry = Ip6FindRouteEntry (RtTable, Dest, NULL); + if (RtEntry == NULL) { + return NULL; + } + + // + // Found a route to the Dest, if it is a direct route, the packet + // will be send directly to the destination, such as for connected + // network. Otherwise, it is an indirect route, the packet will be + // send the next hop router. + // + if ((RtEntry->Flag & IP6_DIRECT_ROUTE) == IP6_DIRECT_ROUTE) { + IP6_COPY_ADDRESS (&NextHop, Dest); + } else { + IP6_COPY_ADDRESS (&NextHop, &RtEntry->NextHop); + } + + Ip6FreeRouteEntry (RtEntry); + + // + // Create a route cache entry, and tag it as spawned from this route entry + // + RtCacheEntry = Ip6CreateRouteCacheEntry (Dest, Src, &NextHop, (UINTN) RtEntry); + + if (RtCacheEntry == NULL) { + return NULL; + } + + InsertHeadList (ListHead, &RtCacheEntry->Link); + NET_GET_REF (RtCacheEntry); + RtTable->Cache.CacheNum[Index]++; + + return RtCacheEntry; +} + diff --git a/NetworkPkg/Ip6Dxe/Ip6Route.h b/NetworkPkg/Ip6Dxe/Ip6Route.h new file mode 100644 index 000000000..9ddc1ab7b --- /dev/null +++ b/NetworkPkg/Ip6Dxe/Ip6Route.h @@ -0,0 +1,293 @@ +/** @file + EFI IP6 route table and route cache table defintions. + + Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_IP6_ROUTE_H__ +#define __EFI_IP6_ROUTE_H__ + +#define IP6_DIRECT_ROUTE 0x00000001 +#define IP6_PACKET_TOO_BIG 0x00000010 + +#define IP6_ROUTE_CACHE_HASH_SIZE 31 +/// +/// Max NO. of cache entry per hash bucket +/// +#define IP6_ROUTE_CACHE_MAX 32 + +#define IP6_ROUTE_CACHE_HASH(Ip1, Ip2) Ip6RouteCacheHash ((Ip1), (Ip2)) + +typedef struct { + LIST_ENTRY Link; + INTN RefCnt; + UINT32 Flag; + UINT8 PrefixLength; + EFI_IPv6_ADDRESS Destination; + EFI_IPv6_ADDRESS NextHop; +} IP6_ROUTE_ENTRY; + +typedef struct { + LIST_ENTRY Link; + INTN RefCnt; + UINTN Tag; + EFI_IPv6_ADDRESS Destination; + EFI_IPv6_ADDRESS Source; + EFI_IPv6_ADDRESS NextHop; +} IP6_ROUTE_CACHE_ENTRY; + +typedef struct { + LIST_ENTRY CacheBucket[IP6_ROUTE_CACHE_HASH_SIZE]; + UINT8 CacheNum[IP6_ROUTE_CACHE_HASH_SIZE]; +} IP6_ROUTE_CACHE; + +// +// Each IP6 instance has its own route table. Each ServiceBinding +// instance has a default route table and default address. +// +// All the route table entries with the same prefix length are linked +// together in one route area. For example, RouteArea[0] contains +// the default routes. A route table also contains a route cache. +// + +typedef struct _IP6_ROUTE_TABLE { + INTN RefCnt; + UINT32 TotalNum; + LIST_ENTRY RouteArea[IP6_PREFIX_NUM]; + IP6_ROUTE_CACHE Cache; +} IP6_ROUTE_TABLE; + +/** + This is the worker function for IP6_ROUTE_CACHE_HASH(). It calculates the value + as the index of the route cache bucket according to the prefix of two IPv6 addresses. + + @param[in] Ip1 The IPv6 address. + @param[in] Ip2 The IPv6 address. + + @return The hash value of the prefix of two IPv6 addresses. + +**/ +UINT32 +Ip6RouteCacheHash ( + IN EFI_IPv6_ADDRESS *Ip1, + IN EFI_IPv6_ADDRESS *Ip2 + ); + +/** + Allocate and initialize an IP6 route cache entry. + + @param[in] Dst The destination address. + @param[in] Src The source address. + @param[in] GateWay The next hop address. + @param[in] Tag The tag from the caller. This marks all the cache entries + spawned from one route table entry. + + @return NULL if it failed to allocate memory for the cache. Otherwise, point + to the created route cache entry. + +**/ +IP6_ROUTE_CACHE_ENTRY * +Ip6CreateRouteCacheEntry ( + IN EFI_IPv6_ADDRESS *Dst, + IN EFI_IPv6_ADDRESS *Src, + IN EFI_IPv6_ADDRESS *GateWay, + IN UINTN Tag + ); + +/** + Free the route cache entry. It is reference counted. + + @param[in, out] RtCacheEntry The route cache entry to free. + +**/ +VOID +Ip6FreeRouteCacheEntry ( + IN OUT IP6_ROUTE_CACHE_ENTRY *RtCacheEntry + ); + +/** + Find a route cache with the destination and source address. This is + used by the ICMPv6 redirect messasge process. + + @param[in] RtTable The route table to search the cache for. + @param[in] Dest The destination address. + @param[in] Src The source address. + + @return NULL if no route entry to the (Dest, Src). Otherwise, point + to the correct route cache entry. + +**/ +IP6_ROUTE_CACHE_ENTRY * +Ip6FindRouteCache ( + IN IP6_ROUTE_TABLE *RtTable, + IN EFI_IPv6_ADDRESS *Dest, + IN EFI_IPv6_ADDRESS *Src + ); + +/** + Build a array of EFI_IP6_ROUTE_TABLE to be returned to the caller. The number + of EFI_IP6_ROUTE_TABLE is also returned. + + @param[in] RouteTable The pointer of IP6_ROUTE_TABLE internal used. + @param[out] EfiRouteCount The number of returned route entries. + @param[out] EfiRouteTable The pointer to the array of EFI_IP6_ROUTE_TABLE. + If NULL, only the route entry count is returned. + + @retval EFI_SUCCESS The EFI_IP6_ROUTE_TABLE successfully built. + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the route table. + +**/ +EFI_STATUS +Ip6BuildEfiRouteTable ( + IN IP6_ROUTE_TABLE *RouteTable, + OUT UINT32 *EfiRouteCount, + OUT EFI_IP6_ROUTE_TABLE **EfiRouteTable OPTIONAL + ); + +/** + Create an empty route table, includes its internal route cache. + + @return NULL if failed to allocate memory for the route table. Otherwise, + the point to newly created route table. + +**/ +IP6_ROUTE_TABLE * +Ip6CreateRouteTable ( + VOID + ); + +/** + Free the route table and its associated route cache. Route + table is reference counted. + + @param[in, out] RtTable The route table to free. + +**/ +VOID +Ip6CleanRouteTable ( + IN OUT IP6_ROUTE_TABLE *RtTable + ); + +/** + Allocate a route entry then initialize it with the Destination/PrefixLength + and Gateway. + + @param[in] Destination The IPv6 destination address. This is an optional + parameter that may be NULL. + @param[in] PrefixLength The destination network's prefix length. + @param[in] GatewayAddress The next hop address. This is optional parameter + that may be NULL. + + @return NULL if it failed to allocate memeory. Otherwise, the newly created route entry. + +**/ +IP6_ROUTE_ENTRY * +Ip6CreateRouteEntry ( + IN EFI_IPv6_ADDRESS *Destination OPTIONAL, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *GatewayAddress OPTIONAL + ); + +/** + Search the route table for a most specific match to the Dst. It searches + from the longest route area (prefix length == 128) to the shortest route area + (default routes). In each route area, it will first search the instance's + route table, then the default route table. This is required per the following + requirements: + 1. IP search the route table for a most specific match. + 2. The local route entries have precedence over the default route entry. + + @param[in] RtTable The route table to search from. + @param[in] Destination The destionation address to search. If NULL, search + the route table by NextHop. + @param[in] NextHop The next hop address. If NULL, search the route table + by Destination. + + @return NULL if no route matches the Dst. Otherwise the point to the + most specific route to the Dst. + +**/ +IP6_ROUTE_ENTRY * +Ip6FindRouteEntry ( + IN IP6_ROUTE_TABLE *RtTable, + IN EFI_IPv6_ADDRESS *Destination OPTIONAL, + IN EFI_IPv6_ADDRESS *NextHop OPTIONAL + ); + +/** + Free the route table entry. It is reference counted. + + @param[in, out] RtEntry The route entry to free. + +**/ +VOID +Ip6FreeRouteEntry ( + IN OUT IP6_ROUTE_ENTRY *RtEntry + ); + +/** + Add a route entry to the route table. It is the help function for EfiIp6Routes. + + @param[in, out] RtTable Route table to add route to. + @param[in] Destination The destination of the network. + @param[in] PrefixLength The PrefixLength of the destination. + @param[in] GatewayAddress The next hop address. + + @retval EFI_ACCESS_DENIED The same route already exists. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the entry. + @retval EFI_SUCCESS The route was added successfully. + +**/ +EFI_STATUS +Ip6AddRoute ( + IN OUT IP6_ROUTE_TABLE *RtTable, + IN EFI_IPv6_ADDRESS *Destination, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *GatewayAddress + ); + +/** + Remove a route entry and all the route caches spawn from it. + It is the help function for EfiIp6Routes. + + @param[in, out] RtTable The route table to remove the route from. + @param[in] Destination The destination network. + @param[in] PrefixLength The PrefixLength of the Destination. + @param[in] GatewayAddress The next hop address. + + @retval EFI_SUCCESS Successfully removed the route entry. + @retval EFI_NOT_FOUND There is no route entry in the table with that + properity. + +**/ +EFI_STATUS +Ip6DelRoute ( + IN OUT IP6_ROUTE_TABLE *RtTable, + IN EFI_IPv6_ADDRESS *Destination, + IN UINT8 PrefixLength, + IN EFI_IPv6_ADDRESS *GatewayAddress + ); + +/** + Search the route table to route the packet. Return/create a route + cache if there is a route to the destination. + + @param[in] IpSb The IP6 service data. + @param[in] Dest The destination address to search for. + @param[in] Src The source address to search for. + + @return NULL if failed to route packet. Otherwise, a route cache + entry that can be used to route packet. + +**/ +IP6_ROUTE_CACHE_ENTRY * +Ip6Route ( + IN IP6_SERVICE *IpSb, + IN EFI_IPv6_ADDRESS *Dest, + IN EFI_IPv6_ADDRESS *Src + ); + +#endif diff --git a/NetworkPkg/Library/DxeDpcLib/DpcLib.c b/NetworkPkg/Library/DxeDpcLib/DpcLib.c new file mode 100644 index 000000000..29d810776 --- /dev/null +++ b/NetworkPkg/Library/DxeDpcLib/DpcLib.c @@ -0,0 +1,94 @@ +/** @file + Help functions to access UDP service. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include +#include +#include +#include + +// +// Pointer to the DPC Protocol +// +EFI_DPC_PROTOCOL *mDpc; + +/** + This constructor function caches the EFI_DPC_PROTOCOL pointer. + + @param[in] ImageHandle The firmware allocated handle for the EFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The constructor always return EFI_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +DpcLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + // + // Locate the EFI_DPC_PROTOCOL in the handle database + // + Status = gBS->LocateProtocol (&gEfiDpcProtocolGuid, NULL, (VOID **)&mDpc); + ASSERT_EFI_ERROR (Status); + + return Status; +} + +/** + Add a Deferred Procedure Call to the end of the DPC queue. + + @param[in] DpcTpl The EFI_TPL that the DPC should be invoked. + @param[in] DpcProcedure Pointer to the DPC's function. + @param[in] DpcContext Pointer to the DPC's context. Passed to DpcProcedure + when DpcProcedure is invoked. + + @retval EFI_SUCCESS The DPC was queued. + @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL. + @retval EFI_INVALID_PARAMETER DpcProcedure is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to + add the DPC to the queue. + +**/ +EFI_STATUS +EFIAPI +QueueDpc ( + IN EFI_TPL DpcTpl, + IN EFI_DPC_PROCEDURE DpcProcedure, + IN VOID *DpcContext OPTIONAL + ) +{ + // + // Call the EFI_DPC_PROTOCOL to queue the DPC + // + return mDpc->QueueDpc (mDpc, DpcTpl, DpcProcedure, DpcContext); +} + +/** + Dispatch the queue of DPCs. ALL DPCs that have been queued with a DpcTpl + value greater than or equal to the current TPL are invoked in the order that + they were queued. DPCs with higher DpcTpl values are invoked before DPCs with + lower DpcTpl values. + + @retval EFI_SUCCESS One or more DPCs were invoked. + @retval EFI_NOT_FOUND No DPCs were invoked. + +**/ +EFI_STATUS +EFIAPI +DispatchDpc ( + VOID + ) +{ + // + // Call the EFI_DPC_PROTOCOL to dispatch previously queued DPCs + // + return mDpc->DispatchDpc (mDpc); +} diff --git a/NetworkPkg/Library/DxeDpcLib/DxeDpcLib.inf b/NetworkPkg/Library/DxeDpcLib/DxeDpcLib.inf new file mode 100644 index 000000000..423037090 --- /dev/null +++ b/NetworkPkg/Library/DxeDpcLib/DxeDpcLib.inf @@ -0,0 +1,40 @@ +## @file +# This library instance provides DPC service by consuming EFI DPC Protocol. +# +# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = DxeDpcLib + MODULE_UNI_FILE = DxeDpcLib.uni + FILE_GUID = 38897D86-FF36-4472-AE64-1DB9AE715C81 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = DpcLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER + CONSTRUCTOR = DpcLibConstructor + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources] + DpcLib.c + +[Packages] + MdePkg/MdePkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + DebugLib + UefiBootServicesTableLib + +[Protocols] + gEfiDpcProtocolGuid ## CONSUMES + +[Depex.common.DXE_DRIVER, Depex.common.DXE_RUNTIME_DRIVER, Depex.common.DXE_SAL_DRIVER, Depex.common.DXE_SMM_DRIVER] + gEfiDpcProtocolGuid diff --git a/NetworkPkg/Library/DxeDpcLib/DxeDpcLib.uni b/NetworkPkg/Library/DxeDpcLib/DxeDpcLib.uni new file mode 100644 index 000000000..3105bbcfe --- /dev/null +++ b/NetworkPkg/Library/DxeDpcLib/DxeDpcLib.uni @@ -0,0 +1,16 @@ +// /** @file +// This library instance provides DPC service by consuming EFI DPC Protocol. +// +// This library instance provides the DPC service by consuming EFI DPC Protocol. +// +// Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Provides the DPC service by consuming EFI DPC Protocol" + +#string STR_MODULE_DESCRIPTION #language en-US "This library instance provides the DPC service by consuming EFI DPC Protocol." + diff --git a/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.c b/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.c new file mode 100644 index 000000000..8b74554cd --- /dev/null +++ b/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.c @@ -0,0 +1,2084 @@ +/** @file + This library is used to share code between UEFI network stack modules. + It provides the helper routines to parse the HTTP message byte stream. + +Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "DxeHttpLib.h" + + + +/** + Decode a percent-encoded URI component to the ASCII character. + + Decode the input component in Buffer according to RFC 3986. The caller is responsible to make + sure ResultBuffer points to a buffer with size equal or greater than ((AsciiStrSize (Buffer)) + in bytes. + + @param[in] Buffer The pointer to a percent-encoded URI component. + @param[in] BufferLength Length of Buffer in bytes. + @param[out] ResultBuffer Point to the buffer to store the decode result. + @param[out] ResultLength Length of decoded string in ResultBuffer in bytes. + + @retval EFI_SUCCESS Successfully decoded the URI. + @retval EFI_INVALID_PARAMETER Buffer is not a valid percent-encoded string. + +**/ +EFI_STATUS +EFIAPI +UriPercentDecode ( + IN CHAR8 *Buffer, + IN UINT32 BufferLength, + OUT CHAR8 *ResultBuffer, + OUT UINT32 *ResultLength + ) +{ + UINTN Index; + UINTN Offset; + CHAR8 HexStr[3]; + + if (Buffer == NULL || BufferLength == 0 || ResultBuffer == NULL) { + return EFI_INVALID_PARAMETER; + } + + Index = 0; + Offset = 0; + HexStr[2] = '\0'; + while (Index < BufferLength) { + if (Buffer[Index] == '%') { + if (Index + 1 >= BufferLength || Index + 2 >= BufferLength || + !NET_IS_HEX_CHAR (Buffer[Index+1]) || !NET_IS_HEX_CHAR (Buffer[Index+2])) { + return EFI_INVALID_PARAMETER; + } + HexStr[0] = Buffer[Index+1]; + HexStr[1] = Buffer[Index+2]; + ResultBuffer[Offset] = (CHAR8) AsciiStrHexToUintn (HexStr); + Index += 3; + } else { + ResultBuffer[Offset] = Buffer[Index]; + Index++; + } + Offset++; + } + + *ResultLength = (UINT32) Offset; + + return EFI_SUCCESS; +} + +/** + This function return the updated state according to the input state and next character of + the authority. + + @param[in] Char Next character. + @param[in] State Current value of the parser state machine. + @param[in] IsRightBracket TRUE if there is an sign ']' in the authority component and + indicates the next part is ':' before Port. + + @return Updated state value. +**/ +HTTP_URL_PARSE_STATE +NetHttpParseAuthorityChar ( + IN CHAR8 Char, + IN HTTP_URL_PARSE_STATE State, + IN BOOLEAN *IsRightBracket + ) +{ + + // + // RFC 3986: + // The authority component is preceded by a double slash ("//") and is + // terminated by the next slash ("/"), question mark ("?"), or number + // sign ("#") character, or by the end of the URI. + // + if (Char == ' ' || Char == '\r' || Char == '\n') { + return UrlParserStateMax; + } + + // + // authority = [ userinfo "@" ] host [ ":" port ] + // + switch (State) { + case UrlParserUserInfo: + if (Char == '@') { + return UrlParserHostStart; + } + break; + + case UrlParserHost: + case UrlParserHostStart: + if (Char == '[') { + return UrlParserHostIpv6; + } + + if (Char == ':') { + return UrlParserPortStart; + } + + return UrlParserHost; + + case UrlParserHostIpv6: + if (Char == ']') { + *IsRightBracket = TRUE; + } + + if (Char == ':' && *IsRightBracket) { + return UrlParserPortStart; + } + return UrlParserHostIpv6; + + case UrlParserPort: + case UrlParserPortStart: + return UrlParserPort; + + default: + break; + } + + return State; +} + +/** + This function parse the authority component of the input URL and update the parser. + + @param[in] Url The pointer to a HTTP URL string. + @param[in] FoundAt TRUE if there is an at sign ('@') in the authority component. + @param[in, out] UrlParser Pointer to the buffer of the parse result. + + @retval EFI_SUCCESS Successfully parse the authority. + @retval EFI_INVALID_PARAMETER The Url is invalid to parse the authority component. + +**/ +EFI_STATUS +NetHttpParseAuthority ( + IN CHAR8 *Url, + IN BOOLEAN FoundAt, + IN OUT HTTP_URL_PARSER *UrlParser + ) +{ + CHAR8 *Char; + CHAR8 *Authority; + UINT32 Length; + HTTP_URL_PARSE_STATE State; + UINT32 Field; + UINT32 OldField; + BOOLEAN IsrightBracket; + + ASSERT ((UrlParser->FieldBitMap & BIT (HTTP_URI_FIELD_AUTHORITY)) != 0); + + // + // authority = [ userinfo "@" ] host [ ":" port ] + // + if (FoundAt) { + State = UrlParserUserInfo; + } else { + State = UrlParserHost; + } + + IsrightBracket = FALSE; + Field = HTTP_URI_FIELD_MAX; + OldField = Field; + Authority = Url + UrlParser->FieldData[HTTP_URI_FIELD_AUTHORITY].Offset; + Length = UrlParser->FieldData[HTTP_URI_FIELD_AUTHORITY].Length; + for (Char = Authority; Char < Authority + Length; Char++) { + State = NetHttpParseAuthorityChar (*Char, State, &IsrightBracket); + switch (State) { + case UrlParserStateMax: + return EFI_INVALID_PARAMETER; + + case UrlParserHostStart: + case UrlParserPortStart: + continue; + + case UrlParserUserInfo: + Field = HTTP_URI_FIELD_USERINFO; + break; + + case UrlParserHost: + Field = HTTP_URI_FIELD_HOST; + break; + + case UrlParserHostIpv6: + Field = HTTP_URI_FIELD_HOST; + break; + + case UrlParserPort: + Field = HTTP_URI_FIELD_PORT; + break; + + default: + ASSERT (FALSE); + } + + // + // Field not changed, count the length. + // + ASSERT (Field < HTTP_URI_FIELD_MAX); + if (Field == OldField) { + UrlParser->FieldData[Field].Length++; + continue; + } + + // + // New field start + // + UrlParser->FieldBitMap |= BIT (Field); + UrlParser->FieldData[Field].Offset = (UINT32) (Char - Url); + UrlParser->FieldData[Field].Length = 1; + OldField = Field; + } + + return EFI_SUCCESS; +} + +/** + This function return the updated state according to the input state and next character of a URL. + + @param[in] Char Next character. + @param[in] State Current value of the parser state machine. + + @return Updated state value. + +**/ +HTTP_URL_PARSE_STATE +NetHttpParseUrlChar ( + IN CHAR8 Char, + IN HTTP_URL_PARSE_STATE State + ) +{ + if (Char == ' ' || Char == '\r' || Char == '\n') { + return UrlParserStateMax; + } + + // + // http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]] + // + // Request-URI = "*" | absolute-URI | path-absolute | authority + // + // absolute-URI = scheme ":" hier-part [ "?" query ] + // path-absolute = "/" [ segment-nz *( "/" segment ) ] + // authority = [ userinfo "@" ] host [ ":" port ] + // + switch (State) { + case UrlParserUrlStart: + if (Char == '*' || Char == '/') { + return UrlParserPath; + } + return UrlParserScheme; + + case UrlParserScheme: + if (Char == ':') { + return UrlParserSchemeColon; + } + break; + + case UrlParserSchemeColon: + if (Char == '/') { + return UrlParserSchemeColonSlash; + } + break; + + case UrlParserSchemeColonSlash: + if (Char == '/') { + return UrlParserSchemeColonSlashSlash; + } + break; + + case UrlParserAtInAuthority: + if (Char == '@') { + return UrlParserStateMax; + } + + case UrlParserAuthority: + case UrlParserSchemeColonSlashSlash: + if (Char == '@') { + return UrlParserAtInAuthority; + } + if (Char == '/') { + return UrlParserPath; + } + if (Char == '?') { + return UrlParserQueryStart; + } + if (Char == '#') { + return UrlParserFragmentStart; + } + return UrlParserAuthority; + + case UrlParserPath: + if (Char == '?') { + return UrlParserQueryStart; + } + if (Char == '#') { + return UrlParserFragmentStart; + } + break; + + case UrlParserQuery: + case UrlParserQueryStart: + if (Char == '#') { + return UrlParserFragmentStart; + } + return UrlParserQuery; + + case UrlParserFragmentStart: + return UrlParserFragment; + + default: + break; + } + + return State; +} +/** + Create a URL parser for the input URL string. + + This function will parse and dereference the input HTTP URL into it components. The original + content of the URL won't be modified and the result will be returned in UrlParser, which can + be used in other functions like NetHttpUrlGetHostName(). + + @param[in] Url The pointer to a HTTP URL string. + @param[in] Length Length of Url in bytes. + @param[in] IsConnectMethod Whether the Url is used in HTTP CONNECT method or not. + @param[out] UrlParser Pointer to the returned buffer to store the parse result. + + @retval EFI_SUCCESS Successfully dereferenced the HTTP URL. + @retval EFI_INVALID_PARAMETER UrlParser is NULL or Url is not a valid HTTP URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpParseUrl ( + IN CHAR8 *Url, + IN UINT32 Length, + IN BOOLEAN IsConnectMethod, + OUT VOID **UrlParser + ) +{ + HTTP_URL_PARSE_STATE State; + CHAR8 *Char; + UINT32 Field; + UINT32 OldField; + BOOLEAN FoundAt; + EFI_STATUS Status; + HTTP_URL_PARSER *Parser; + + Parser = NULL; + + if (Url == NULL || Length == 0 || UrlParser == NULL) { + return EFI_INVALID_PARAMETER; + } + + Parser = AllocateZeroPool (sizeof (HTTP_URL_PARSER)); + if (Parser == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + if (IsConnectMethod) { + // + // According to RFC 2616, the authority form is only used by the CONNECT method. + // + State = UrlParserAuthority; + } else { + State = UrlParserUrlStart; + } + + Field = HTTP_URI_FIELD_MAX; + OldField = Field; + FoundAt = FALSE; + for (Char = Url; Char < Url + Length; Char++) { + // + // Update state machine according to next char. + // + State = NetHttpParseUrlChar (*Char, State); + + switch (State) { + case UrlParserStateMax: + FreePool (Parser); + return EFI_INVALID_PARAMETER; + + case UrlParserSchemeColon: + case UrlParserSchemeColonSlash: + case UrlParserSchemeColonSlashSlash: + case UrlParserQueryStart: + case UrlParserFragmentStart: + // + // Skip all the delimiting char: "://" "?" "@" + // + continue; + + case UrlParserScheme: + Field = HTTP_URI_FIELD_SCHEME; + break; + + case UrlParserAtInAuthority: + FoundAt = TRUE; + case UrlParserAuthority: + Field = HTTP_URI_FIELD_AUTHORITY; + break; + + case UrlParserPath: + Field = HTTP_URI_FIELD_PATH; + break; + + case UrlParserQuery: + Field = HTTP_URI_FIELD_QUERY; + break; + + case UrlParserFragment: + Field = HTTP_URI_FIELD_FRAGMENT; + break; + + default: + ASSERT (FALSE); + } + + // + // Field not changed, count the length. + // + ASSERT (Field < HTTP_URI_FIELD_MAX); + if (Field == OldField) { + Parser->FieldData[Field].Length++; + continue; + } + + // + // New field start + // + Parser->FieldBitMap |= BIT (Field); + Parser->FieldData[Field].Offset = (UINT32) (Char - Url); + Parser->FieldData[Field].Length = 1; + OldField = Field; + } + + // + // If has authority component, continue to parse the username, host and port. + // + if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_AUTHORITY)) != 0) { + Status = NetHttpParseAuthority (Url, FoundAt, Parser); + if (EFI_ERROR (Status)) { + FreePool (Parser); + return Status; + } + } + + *UrlParser = Parser; + return EFI_SUCCESS; +} + +/** + Get the Hostname from a HTTP URL. + + This function will return the HostName according to the Url and previous parse result ,and + it is the caller's responsibility to free the buffer returned in *HostName. + + @param[in] Url The pointer to a HTTP URL string. + @param[in] UrlParser URL Parse result returned by NetHttpParseUrl(). + @param[out] HostName Pointer to a buffer to store the HostName. + + @retval EFI_SUCCESS Successfully get the required component. + @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid. + @retval EFI_NOT_FOUND No hostName component in the URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpUrlGetHostName ( + IN CHAR8 *Url, + IN VOID *UrlParser, + OUT CHAR8 **HostName + ) +{ + CHAR8 *Name; + EFI_STATUS Status; + UINT32 ResultLength; + HTTP_URL_PARSER *Parser; + + if (Url == NULL || UrlParser == NULL || HostName == NULL) { + return EFI_INVALID_PARAMETER; + } + + Parser = (HTTP_URL_PARSER *) UrlParser; + + if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_HOST)) == 0) { + return EFI_NOT_FOUND; + } + + Name = AllocatePool (Parser->FieldData[HTTP_URI_FIELD_HOST].Length + 1); + if (Name == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = UriPercentDecode ( + Url + Parser->FieldData[HTTP_URI_FIELD_HOST].Offset, + Parser->FieldData[HTTP_URI_FIELD_HOST].Length, + Name, + &ResultLength + ); + if (EFI_ERROR (Status)) { + FreePool (Name); + return Status; + } + + Name[ResultLength] = '\0'; + *HostName = Name; + return EFI_SUCCESS; +} + + +/** + Get the IPv4 address from a HTTP URL. + + This function will return the IPv4 address according to the Url and previous parse result. + + @param[in] Url The pointer to a HTTP URL string. + @param[in] UrlParser URL Parse result returned by NetHttpParseUrl(). + @param[out] Ip4Address Pointer to a buffer to store the IP address. + + @retval EFI_SUCCESS Successfully get the required component. + @retval EFI_INVALID_PARAMETER Uri is NULL or Ip4Address is NULL or UrlParser is invalid. + @retval EFI_NOT_FOUND No IPv4 address component in the URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpUrlGetIp4 ( + IN CHAR8 *Url, + IN VOID *UrlParser, + OUT EFI_IPv4_ADDRESS *Ip4Address + ) +{ + CHAR8 *Ip4String; + EFI_STATUS Status; + UINT32 ResultLength; + HTTP_URL_PARSER *Parser; + + if (Url == NULL || UrlParser == NULL || Ip4Address == NULL) { + return EFI_INVALID_PARAMETER; + } + + Parser = (HTTP_URL_PARSER *) UrlParser; + + if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_HOST)) == 0) { + return EFI_NOT_FOUND; + } + + Ip4String = AllocatePool (Parser->FieldData[HTTP_URI_FIELD_HOST].Length + 1); + if (Ip4String == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = UriPercentDecode ( + Url + Parser->FieldData[HTTP_URI_FIELD_HOST].Offset, + Parser->FieldData[HTTP_URI_FIELD_HOST].Length, + Ip4String, + &ResultLength + ); + if (EFI_ERROR (Status)) { + FreePool (Ip4String); + return Status; + } + + Ip4String[ResultLength] = '\0'; + Status = NetLibAsciiStrToIp4 (Ip4String, Ip4Address); + FreePool (Ip4String); + + return Status; +} + +/** + Get the IPv6 address from a HTTP URL. + + This function will return the IPv6 address according to the Url and previous parse result. + + @param[in] Url The pointer to a HTTP URL string. + @param[in] UrlParser URL Parse result returned by NetHttpParseUrl(). + @param[out] Ip6Address Pointer to a buffer to store the IP address. + + @retval EFI_SUCCESS Successfully get the required component. + @retval EFI_INVALID_PARAMETER Uri is NULL or Ip6Address is NULL or UrlParser is invalid. + @retval EFI_NOT_FOUND No IPv6 address component in the URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpUrlGetIp6 ( + IN CHAR8 *Url, + IN VOID *UrlParser, + OUT EFI_IPv6_ADDRESS *Ip6Address + ) +{ + CHAR8 *Ip6String; + CHAR8 *Ptr; + UINT32 Length; + EFI_STATUS Status; + UINT32 ResultLength; + HTTP_URL_PARSER *Parser; + + if (Url == NULL || UrlParser == NULL || Ip6Address == NULL) { + return EFI_INVALID_PARAMETER; + } + + Parser = (HTTP_URL_PARSER *) UrlParser; + + if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_HOST)) == 0) { + return EFI_NOT_FOUND; + } + + // + // IP-literal = "[" ( IPv6address / IPvFuture ) "]" + // + Length = Parser->FieldData[HTTP_URI_FIELD_HOST].Length; + if (Length < 2) { + return EFI_INVALID_PARAMETER; + } + + Ptr = Url + Parser->FieldData[HTTP_URI_FIELD_HOST].Offset; + if ((Ptr[0] != '[') || (Ptr[Length - 1] != ']')) { + return EFI_INVALID_PARAMETER; + } + + Ip6String = AllocatePool (Length); + if (Ip6String == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = UriPercentDecode ( + Ptr + 1, + Length - 2, + Ip6String, + &ResultLength + ); + if (EFI_ERROR (Status)) { + FreePool (Ip6String); + return Status; + } + + Ip6String[ResultLength] = '\0'; + Status = NetLibAsciiStrToIp6 (Ip6String, Ip6Address); + FreePool (Ip6String); + + return Status; +} + +/** + Get the port number from a HTTP URL. + + This function will return the port number according to the Url and previous parse result. + + @param[in] Url The pointer to a HTTP URL string. + @param[in] UrlParser URL Parse result returned by NetHttpParseUrl(). + @param[out] Port Pointer to a buffer to store the port number. + + @retval EFI_SUCCESS Successfully get the required component. + @retval EFI_INVALID_PARAMETER Uri is NULL or Port is NULL or UrlParser is invalid. + @retval EFI_NOT_FOUND No port number in the URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpUrlGetPort ( + IN CHAR8 *Url, + IN VOID *UrlParser, + OUT UINT16 *Port + ) +{ + CHAR8 *PortString; + EFI_STATUS Status; + UINTN Index; + UINTN Data; + UINT32 ResultLength; + HTTP_URL_PARSER *Parser; + + if (Url == NULL || UrlParser == NULL || Port == NULL) { + return EFI_INVALID_PARAMETER; + } + + *Port = 0; + Index = 0; + + Parser = (HTTP_URL_PARSER *) UrlParser; + + if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_PORT)) == 0) { + return EFI_NOT_FOUND; + } + + PortString = AllocatePool (Parser->FieldData[HTTP_URI_FIELD_PORT].Length + 1); + if (PortString == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = UriPercentDecode ( + Url + Parser->FieldData[HTTP_URI_FIELD_PORT].Offset, + Parser->FieldData[HTTP_URI_FIELD_PORT].Length, + PortString, + &ResultLength + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + PortString[ResultLength] = '\0'; + + while (Index < ResultLength) { + if (!NET_IS_DIGIT (PortString[Index])) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + Index ++; + } + + Status = AsciiStrDecimalToUintnS (Url + Parser->FieldData[HTTP_URI_FIELD_PORT].Offset, (CHAR8 **) NULL, &Data); + + if (Data > HTTP_URI_PORT_MAX_NUM) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + *Port = (UINT16) Data; + +ON_EXIT: + FreePool (PortString); + return Status; +} + +/** + Get the Path from a HTTP URL. + + This function will return the Path according to the Url and previous parse result,and + it is the caller's responsibility to free the buffer returned in *Path. + + @param[in] Url The pointer to a HTTP URL string. + @param[in] UrlParser URL Parse result returned by NetHttpParseUrl(). + @param[out] Path Pointer to a buffer to store the Path. + + @retval EFI_SUCCESS Successfully get the required component. + @retval EFI_INVALID_PARAMETER Uri is NULL or HostName is NULL or UrlParser is invalid. + @retval EFI_NOT_FOUND No hostName component in the URL. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EFIAPI +HttpUrlGetPath ( + IN CHAR8 *Url, + IN VOID *UrlParser, + OUT CHAR8 **Path + ) +{ + CHAR8 *PathStr; + EFI_STATUS Status; + UINT32 ResultLength; + HTTP_URL_PARSER *Parser; + + if (Url == NULL || UrlParser == NULL || Path == NULL) { + return EFI_INVALID_PARAMETER; + } + + Parser = (HTTP_URL_PARSER *) UrlParser; + + if ((Parser->FieldBitMap & BIT (HTTP_URI_FIELD_PATH)) == 0) { + return EFI_NOT_FOUND; + } + + PathStr = AllocatePool (Parser->FieldData[HTTP_URI_FIELD_PATH].Length + 1); + if (PathStr == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = UriPercentDecode ( + Url + Parser->FieldData[HTTP_URI_FIELD_PATH].Offset, + Parser->FieldData[HTTP_URI_FIELD_PATH].Length, + PathStr, + &ResultLength + ); + if (EFI_ERROR (Status)) { + FreePool (PathStr); + return Status; + } + + PathStr[ResultLength] = '\0'; + *Path = PathStr; + return EFI_SUCCESS; +} + +/** + Release the resource of the URL parser. + + @param[in] UrlParser Pointer to the parser. + +**/ +VOID +EFIAPI +HttpUrlFreeParser ( + IN VOID *UrlParser + ) +{ + FreePool (UrlParser); +} + +/** + Find a specified header field according to the field name. + + @param[in] HeaderCount Number of HTTP header structures in Headers list. + @param[in] Headers Array containing list of HTTP headers. + @param[in] FieldName Null terminated string which describes a field name. + + @return Pointer to the found header or NULL. + +**/ +EFI_HTTP_HEADER * +EFIAPI +HttpFindHeader ( + IN UINTN HeaderCount, + IN EFI_HTTP_HEADER *Headers, + IN CHAR8 *FieldName + ) +{ + UINTN Index; + + if (HeaderCount == 0 || Headers == NULL || FieldName == NULL) { + return NULL; + } + + for (Index = 0; Index < HeaderCount; Index++){ + // + // Field names are case-insensitive (RFC 2616). + // + if (AsciiStriCmp (Headers[Index].FieldName, FieldName) == 0) { + return &Headers[Index]; + } + } + return NULL; +} + +typedef enum { + BodyParserBodyStart, + BodyParserBodyIdentity, + BodyParserChunkSizeStart, + BodyParserChunkSize, + BodyParserChunkSizeEndCR, + BodyParserChunkExtStart, + BodyParserChunkDataStart, + BodyParserChunkDataEnd, + BodyParserChunkDataEndCR, + BodyParserTrailer, + BodyParserLastCRLF, + BodyParserLastCRLFEnd, + BodyParserComplete, + BodyParserStateMax +} HTTP_BODY_PARSE_STATE; + +typedef struct { + BOOLEAN IgnoreBody; // "MUST NOT" include a message-body + BOOLEAN IsChunked; // "chunked" transfer-coding. + BOOLEAN ContentLengthIsValid; + UINTN ContentLength; // Entity length (not the message-body length), invalid until ContentLengthIsValid is TRUE + + HTTP_BODY_PARSER_CALLBACK Callback; + VOID *Context; + UINTN ParsedBodyLength; + HTTP_BODY_PARSE_STATE State; + UINTN CurrentChunkSize; + UINTN CurrentChunkParsedSize; +} HTTP_BODY_PARSER; + +/** + Convert an hexadecimal char to a value of type UINTN. + + @param[in] Char Ascii character. + + @return Value translated from Char. + +**/ +UINTN +HttpIoHexCharToUintn ( + IN CHAR8 Char + ) +{ + if (Char >= '0' && Char <= '9') { + return Char - '0'; + } + + return (10 + AsciiCharToUpper (Char) - 'A'); +} + +/** + Get the value of the content length if there is a "Content-Length" header. + + @param[in] HeaderCount Number of HTTP header structures in Headers. + @param[in] Headers Array containing list of HTTP headers. + @param[out] ContentLength Pointer to save the value of the content length. + + @retval EFI_SUCCESS Successfully get the content length. + @retval EFI_NOT_FOUND No "Content-Length" header in the Headers. + +**/ +EFI_STATUS +HttpIoParseContentLengthHeader ( + IN UINTN HeaderCount, + IN EFI_HTTP_HEADER *Headers, + OUT UINTN *ContentLength + ) +{ + EFI_HTTP_HEADER *Header; + + Header = HttpFindHeader (HeaderCount, Headers, HTTP_HEADER_CONTENT_LENGTH); + if (Header == NULL) { + return EFI_NOT_FOUND; + } + + return AsciiStrDecimalToUintnS (Header->FieldValue, (CHAR8 **) NULL, ContentLength); +} + +/** + + Check whether the HTTP message is using the "chunked" transfer-coding. + + @param[in] HeaderCount Number of HTTP header structures in Headers. + @param[in] Headers Array containing list of HTTP headers. + + @return The message is "chunked" transfer-coding (TRUE) or not (FALSE). + +**/ +BOOLEAN +HttpIoIsChunked ( + IN UINTN HeaderCount, + IN EFI_HTTP_HEADER *Headers + ) +{ + EFI_HTTP_HEADER *Header; + + + Header = HttpFindHeader (HeaderCount, Headers, HTTP_HEADER_TRANSFER_ENCODING); + if (Header == NULL) { + return FALSE; + } + + if (AsciiStriCmp (Header->FieldValue, "identity") != 0) { + return TRUE; + } + + return FALSE; +} + +/** + Check whether the HTTP message should have a message-body. + + @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message. + @param[in] StatusCode Response status code returned by the remote host. + + @return The message should have a message-body (FALSE) or not (TRUE). + +**/ +BOOLEAN +HttpIoNoMessageBody ( + IN EFI_HTTP_METHOD Method, + IN EFI_HTTP_STATUS_CODE StatusCode + ) +{ + // + // RFC 2616: + // All responses to the HEAD request method + // MUST NOT include a message-body, even though the presence of entity- + // header fields might lead one to believe they do. All 1xx + // (informational), 204 (no content), and 304 (not modified) responses + // MUST NOT include a message-body. All other responses do include a + // message-body, although it MAY be of zero length. + // + if (Method == HttpMethodHead) { + return TRUE; + } + + if ((StatusCode == HTTP_STATUS_100_CONTINUE) || + (StatusCode == HTTP_STATUS_101_SWITCHING_PROTOCOLS) || + (StatusCode == HTTP_STATUS_204_NO_CONTENT) || + (StatusCode == HTTP_STATUS_304_NOT_MODIFIED)) + { + return TRUE; + } + + return FALSE; +} + +/** + Initialize a HTTP message-body parser. + + This function will create and initialize a HTTP message parser according to caller provided HTTP message + header information. It is the caller's responsibility to free the buffer returned in *UrlParser by HttpFreeMsgParser(). + + @param[in] Method The HTTP method (e.g. GET, POST) for this HTTP message. + @param[in] StatusCode Response status code returned by the remote host. + @param[in] HeaderCount Number of HTTP header structures in Headers. + @param[in] Headers Array containing list of HTTP headers. + @param[in] Callback Callback function that is invoked when parsing the HTTP message-body, + set to NULL to ignore all events. + @param[in] Context Pointer to the context that will be passed to Callback. + @param[out] MsgParser Pointer to the returned buffer to store the message parser. + + @retval EFI_SUCCESS Successfully initialized the parser. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + @retval EFI_INVALID_PARAMETER MsgParser is NULL or HeaderCount is not NULL but Headers is NULL. + @retval Others Failed to initialize the parser. + +**/ +EFI_STATUS +EFIAPI +HttpInitMsgParser ( + IN EFI_HTTP_METHOD Method, + IN EFI_HTTP_STATUS_CODE StatusCode, + IN UINTN HeaderCount, + IN EFI_HTTP_HEADER *Headers, + IN HTTP_BODY_PARSER_CALLBACK Callback, + IN VOID *Context, + OUT VOID **MsgParser + ) +{ + EFI_STATUS Status; + HTTP_BODY_PARSER *Parser; + + if (HeaderCount != 0 && Headers == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (MsgParser == NULL) { + return EFI_INVALID_PARAMETER; + } + + Parser = AllocateZeroPool (sizeof (HTTP_BODY_PARSER)); + if (Parser == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Parser->State = BodyParserBodyStart; + + // + // Determine the message length according to RFC 2616. + // 1. Check whether the message "MUST NOT" have a message-body. + // + Parser->IgnoreBody = HttpIoNoMessageBody (Method, StatusCode); + // + // 2. Check whether the message using "chunked" transfer-coding. + // + Parser->IsChunked = HttpIoIsChunked (HeaderCount, Headers); + // + // 3. Check whether the message has a Content-Length header field. + // + Status = HttpIoParseContentLengthHeader (HeaderCount, Headers, &Parser->ContentLength); + if (!EFI_ERROR (Status)) { + Parser->ContentLengthIsValid = TRUE; + } + // + // 4. Range header is not supported now, so we won't meet media type "multipart/byteranges". + // 5. By server closing the connection + // + + // + // Set state to skip body parser if the message shouldn't have a message body. + // + if (Parser->IgnoreBody) { + Parser->State = BodyParserComplete; + } else { + Parser->Callback = Callback; + Parser->Context = Context; + } + + *MsgParser = Parser; + return EFI_SUCCESS; +} + +/** + Parse message body. + + Parse BodyLength of message-body. This function can be called repeatedly to parse the message-body partially. + + @param[in, out] MsgParser Pointer to the message parser. + @param[in] BodyLength Length in bytes of the Body. + @param[in] Body Pointer to the buffer of the message-body to be parsed. + + @retval EFI_SUCCESS Successfully parse the message-body. + @retval EFI_INVALID_PARAMETER MsgParser is NULL or Body is NULL or BodyLength is 0. + @retval EFI_ABORTED Operation aborted. + @retval Other Error happened while parsing message body. + +**/ +EFI_STATUS +EFIAPI +HttpParseMessageBody ( + IN OUT VOID *MsgParser, + IN UINTN BodyLength, + IN CHAR8 *Body + ) +{ + CHAR8 *Char; + UINTN RemainderLengthInThis; + UINTN LengthForCallback; + EFI_STATUS Status; + HTTP_BODY_PARSER *Parser; + + if (BodyLength == 0 || Body == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (MsgParser == NULL) { + return EFI_INVALID_PARAMETER; + } + + Parser = (HTTP_BODY_PARSER *) MsgParser; + + if (Parser->IgnoreBody) { + Parser->State = BodyParserComplete; + if (Parser->Callback != NULL) { + Status = Parser->Callback ( + BodyParseEventOnComplete, + Body, + 0, + Parser->Context + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + return EFI_SUCCESS; + } + + if (Parser->State == BodyParserBodyStart) { + Parser->ParsedBodyLength = 0; + if (Parser->IsChunked) { + Parser->State = BodyParserChunkSizeStart; + } else { + Parser->State = BodyParserBodyIdentity; + } + } + + // + // The message body might be truncated in anywhere, so we need to parse is byte-by-byte. + // + for (Char = Body; Char < Body + BodyLength; ) { + + switch (Parser->State) { + case BodyParserStateMax: + return EFI_ABORTED; + + case BodyParserBodyIdentity: + // + // Identity transfer-coding, just notify user to save the body data. + // + if (Parser->Callback != NULL) { + Status = Parser->Callback ( + BodyParseEventOnData, + Char, + MIN (BodyLength, Parser->ContentLength - Parser->ParsedBodyLength), + Parser->Context + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + Char += MIN (BodyLength, Parser->ContentLength - Parser->ParsedBodyLength); + Parser->ParsedBodyLength += MIN (BodyLength, Parser->ContentLength - Parser->ParsedBodyLength); + if (Parser->ParsedBodyLength == Parser->ContentLength) { + Parser->State = BodyParserComplete; + if (Parser->Callback != NULL) { + Status = Parser->Callback ( + BodyParseEventOnComplete, + Char, + 0, + Parser->Context + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + } + break; + + case BodyParserChunkSizeStart: + // + // First byte of chunk-size, the chunk-size might be truncated. + // + Parser->CurrentChunkSize = 0; + Parser->State = BodyParserChunkSize; + case BodyParserChunkSize: + if (!NET_IS_HEX_CHAR (*Char)) { + if (*Char == ';') { + Parser->State = BodyParserChunkExtStart; + Char++; + } else if (*Char == '\r') { + Parser->State = BodyParserChunkSizeEndCR; + Char++; + } else { + Parser->State = BodyParserStateMax; + } + break; + } + + if (Parser->CurrentChunkSize > (((~((UINTN) 0)) - 16) / 16)) { + return EFI_INVALID_PARAMETER; + } + Parser->CurrentChunkSize = Parser->CurrentChunkSize * 16 + HttpIoHexCharToUintn (*Char); + Char++; + break; + + case BodyParserChunkExtStart: + // + // Ignore all the chunk extensions. + // + if (*Char == '\r') { + Parser->State = BodyParserChunkSizeEndCR; + } + Char++; + break; + + case BodyParserChunkSizeEndCR: + if (*Char != '\n') { + Parser->State = BodyParserStateMax; + break; + } + Char++; + if (Parser->CurrentChunkSize == 0) { + // + // The last chunk has been parsed and now assumed the state + // of HttpBodyParse is ParserLastCRLF. So it need to decide + // whether the rest message is trailer or last CRLF in the next round. + // + Parser->ContentLengthIsValid = TRUE; + Parser->State = BodyParserLastCRLF; + break; + } + Parser->State = BodyParserChunkDataStart; + Parser->CurrentChunkParsedSize = 0; + break; + + case BodyParserLastCRLF: + // + // Judge the byte is belong to the Last CRLF or trailer, and then + // configure the state of HttpBodyParse to corresponding state. + // + if (*Char == '\r') { + Char++; + Parser->State = BodyParserLastCRLFEnd; + break; + } else { + Parser->State = BodyParserTrailer; + break; + } + + case BodyParserLastCRLFEnd: + if (*Char == '\n') { + Parser->State = BodyParserComplete; + Char++; + if (Parser->Callback != NULL) { + Status = Parser->Callback ( + BodyParseEventOnComplete, + Char, + 0, + Parser->Context + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + break; + } else { + Parser->State = BodyParserStateMax; + break; + } + + case BodyParserTrailer: + if (*Char == '\r') { + Parser->State = BodyParserChunkSizeEndCR; + } + Char++; + break; + + case BodyParserChunkDataStart: + // + // First byte of chunk-data, the chunk data also might be truncated. + // + RemainderLengthInThis = BodyLength - (Char - Body); + LengthForCallback = MIN (Parser->CurrentChunkSize - Parser->CurrentChunkParsedSize, RemainderLengthInThis); + if (Parser->Callback != NULL) { + Status = Parser->Callback ( + BodyParseEventOnData, + Char, + LengthForCallback, + Parser->Context + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + Char += LengthForCallback; + Parser->ContentLength += LengthForCallback; + Parser->CurrentChunkParsedSize += LengthForCallback; + if (Parser->CurrentChunkParsedSize == Parser->CurrentChunkSize) { + Parser->State = BodyParserChunkDataEnd; + } + break; + + case BodyParserChunkDataEnd: + if (*Char == '\r') { + Parser->State = BodyParserChunkDataEndCR; + } else { + Parser->State = BodyParserStateMax; + } + Char++; + break; + + case BodyParserChunkDataEndCR: + if (*Char != '\n') { + Parser->State = BodyParserStateMax; + break; + } + Char++; + Parser->State = BodyParserChunkSizeStart; + break; + + default: + break; + } + + } + + if (Parser->State == BodyParserStateMax) { + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + +/** + Check whether the message-body is complete or not. + + @param[in] MsgParser Pointer to the message parser. + + @retval TRUE Message-body is complete. + @retval FALSE Message-body is not complete. + +**/ +BOOLEAN +EFIAPI +HttpIsMessageComplete ( + IN VOID *MsgParser + ) +{ + HTTP_BODY_PARSER *Parser; + + if (MsgParser == NULL) { + return FALSE; + } + + Parser = (HTTP_BODY_PARSER *) MsgParser; + + if (Parser->State == BodyParserComplete) { + return TRUE; + } + return FALSE; +} + +/** + Get the content length of the entity. + + Note that in trunk transfer, the entity length is not valid until the whole message body is received. + + @param[in] MsgParser Pointer to the message parser. + @param[out] ContentLength Pointer to store the length of the entity. + + @retval EFI_SUCCESS Successfully to get the entity length. + @retval EFI_NOT_READY Entity length is not valid yet. + @retval EFI_INVALID_PARAMETER MsgParser is NULL or ContentLength is NULL. + +**/ +EFI_STATUS +EFIAPI +HttpGetEntityLength ( + IN VOID *MsgParser, + OUT UINTN *ContentLength + ) +{ + HTTP_BODY_PARSER *Parser; + + if (MsgParser == NULL || ContentLength == NULL) { + return EFI_INVALID_PARAMETER; + } + + Parser = (HTTP_BODY_PARSER *) MsgParser; + + if (!Parser->ContentLengthIsValid) { + return EFI_NOT_READY; + } + + *ContentLength = Parser->ContentLength; + return EFI_SUCCESS; +} + +/** + Release the resource of the message parser. + + @param[in] MsgParser Pointer to the message parser. + +**/ +VOID +EFIAPI +HttpFreeMsgParser ( + IN VOID *MsgParser + ) +{ + FreePool (MsgParser); +} + + +/** + Get the next string, which is distinguished by specified separator. + + @param[in] String Pointer to the string. + @param[in] Separator Specified separator used to distinguish where is the beginning + of next string. + + @return Pointer to the next string. + @return NULL if not find or String is NULL. + +**/ +CHAR8 * +AsciiStrGetNextToken ( + IN CONST CHAR8 *String, + IN CHAR8 Separator + ) +{ + CONST CHAR8 *Token; + + Token = String; + while (TRUE) { + if (*Token == 0) { + return NULL; + } + if (*Token == Separator) { + return (CHAR8 *)(Token + 1); + } + Token++; + } +} + +/** + Set FieldName and FieldValue into specified HttpHeader. + + @param[in,out] HttpHeader Specified HttpHeader. + @param[in] FieldName FieldName of this HttpHeader, a NULL terminated ASCII string. + @param[in] FieldValue FieldValue of this HttpHeader, a NULL terminated ASCII string. + + + @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully. + @retval EFI_INVALID_PARAMETER The parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + +**/ +EFI_STATUS +EFIAPI +HttpSetFieldNameAndValue ( + IN OUT EFI_HTTP_HEADER *HttpHeader, + IN CONST CHAR8 *FieldName, + IN CONST CHAR8 *FieldValue + ) +{ + UINTN FieldNameSize; + UINTN FieldValueSize; + + if (HttpHeader == NULL || FieldName == NULL || FieldValue == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (HttpHeader->FieldName != NULL) { + FreePool (HttpHeader->FieldName); + } + if (HttpHeader->FieldValue != NULL) { + FreePool (HttpHeader->FieldValue); + } + + FieldNameSize = AsciiStrSize (FieldName); + HttpHeader->FieldName = AllocateZeroPool (FieldNameSize); + if (HttpHeader->FieldName == NULL) { + return EFI_OUT_OF_RESOURCES; + } + CopyMem (HttpHeader->FieldName, FieldName, FieldNameSize); + HttpHeader->FieldName[FieldNameSize - 1] = 0; + + FieldValueSize = AsciiStrSize (FieldValue); + HttpHeader->FieldValue = AllocateZeroPool (FieldValueSize); + if (HttpHeader->FieldValue == NULL) { + FreePool (HttpHeader->FieldName); + return EFI_OUT_OF_RESOURCES; + } + CopyMem (HttpHeader->FieldValue, FieldValue, FieldValueSize); + HttpHeader->FieldValue[FieldValueSize - 1] = 0; + + return EFI_SUCCESS; +} + +/** + Get one key/value header pair from the raw string. + + @param[in] String Pointer to the raw string. + @param[out] FieldName Points directly to field name within 'HttpHeader'. + @param[out] FieldValue Points directly to field value within 'HttpHeader'. + + @return Pointer to the next raw string. + @return NULL if no key/value header pair from this raw string. + +**/ +CHAR8 * +EFIAPI +HttpGetFieldNameAndValue ( + IN CHAR8 *String, + OUT CHAR8 **FieldName, + OUT CHAR8 **FieldValue + ) +{ + CHAR8 *FieldNameStr; + CHAR8 *FieldValueStr; + CHAR8 *StrPtr; + CHAR8 *EndofHeader; + + if (String == NULL || FieldName == NULL || FieldValue == NULL) { + return NULL; + } + + *FieldName = NULL; + *FieldValue = NULL; + FieldNameStr = NULL; + FieldValueStr = NULL; + StrPtr = NULL; + EndofHeader = NULL; + + + // + // Check whether the raw HTTP header string is valid or not. + // + EndofHeader = AsciiStrStr (String, "\r\n\r\n"); + if (EndofHeader == NULL) { + return NULL; + } + + // + // Each header field consists of a name followed by a colon (":") and the field value. + // The field value MAY be preceded by any amount of LWS, though a single SP is preferred. + // + // message-header = field-name ":" [ field-value ] + // field-name = token + // field-value = *( field-content | LWS ) + // + // Note: "*(element)" allows any number element, including zero; "1*(element)" requires at least one element. + // [element] means element is optional. + // LWS = [CRLF] 1*(SP|HT), it can be ' ' or '\t' or '\r\n ' or '\r\n\t'. + // CRLF = '\r\n'. + // SP = ' '. + // HT = '\t' (Tab). + // + FieldNameStr = String; + FieldValueStr = AsciiStrGetNextToken (FieldNameStr, ':'); + if (FieldValueStr == NULL) { + return NULL; + } + + // + // Replace ':' with 0, then FieldName has been retrived from String. + // + *(FieldValueStr - 1) = 0; + + // + // Handle FieldValueStr, skip all the preceded LWS. + // + while (TRUE) { + if (*FieldValueStr == ' ' || *FieldValueStr == '\t') { + // + // Boundary condition check. + // + if ((UINTN) EndofHeader - (UINTN) FieldValueStr < 1) { + // + // Wrong String format! + // + return NULL; + } + + FieldValueStr ++; + } else if (*FieldValueStr == '\r') { + // + // Boundary condition check. + // + if ((UINTN) EndofHeader - (UINTN) FieldValueStr < 3) { + // + // No more preceded LWS, so break here. + // + break; + } + + if (*(FieldValueStr + 1) == '\n' ) { + if (*(FieldValueStr + 2) == ' ' || *(FieldValueStr + 2) == '\t') { + FieldValueStr = FieldValueStr + 3; + } else { + // + // No more preceded LWS, so break here. + // + break; + } + } else { + // + // Wrong String format! + // + return NULL; + } + } else { + // + // No more preceded LWS, so break here. + // + break; + } + } + + StrPtr = FieldValueStr; + do { + // + // Handle the LWS within the field value. + // + StrPtr = AsciiStrGetNextToken (StrPtr, '\r'); + if (StrPtr == NULL || *StrPtr != '\n') { + // + // Wrong String format! + // + return NULL; + } + + StrPtr++; + } while (*StrPtr == ' ' || *StrPtr == '\t'); + + // + // Replace '\r' with 0 + // + *(StrPtr - 2) = 0; + + // + // Get FieldName and FieldValue. + // + *FieldName = FieldNameStr; + *FieldValue = FieldValueStr; + + return StrPtr; +} + +/** + Free existing HeaderFields. + + @param[in] HeaderFields Pointer to array of key/value header pairs waitting for free. + @param[in] FieldCount The number of header pairs in HeaderFields. + +**/ +VOID +EFIAPI +HttpFreeHeaderFields ( + IN EFI_HTTP_HEADER *HeaderFields, + IN UINTN FieldCount + ) +{ + UINTN Index; + + if (HeaderFields != NULL) { + for (Index = 0; Index < FieldCount; Index++) { + if (HeaderFields[Index].FieldName != NULL) { + FreePool (HeaderFields[Index].FieldName); + } + if (HeaderFields[Index].FieldValue != NULL) { + FreePool (HeaderFields[Index].FieldValue); + } + } + + FreePool (HeaderFields); + } +} + +/** + Generate HTTP request message. + + This function will allocate memory for the whole HTTP message and generate a + well formatted HTTP Request message in it, include the Request-Line, header + fields and also the message body. It is the caller's responsibility to free + the buffer returned in *RequestMsg. + + @param[in] Message Pointer to the EFI_HTTP_MESSAGE structure which + contains the required information to generate + the HTTP request message. + @param[in] Url The URL of a remote host. + @param[out] RequestMsg Pointer to the created HTTP request message. + NULL if any error occured. + @param[out] RequestMsgSize Size of the RequestMsg (in bytes). + + @retval EFI_SUCCESS If HTTP request string was created successfully. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_INVALID_PARAMETER The input arguments are invalid. + +**/ +EFI_STATUS +EFIAPI +HttpGenRequestMessage ( + IN CONST EFI_HTTP_MESSAGE *Message, + IN CONST CHAR8 *Url, + OUT CHAR8 **RequestMsg, + OUT UINTN *RequestMsgSize + ) +{ + EFI_STATUS Status; + UINTN StrLength; + CHAR8 *RequestPtr; + UINTN HttpHdrSize; + UINTN MsgSize; + BOOLEAN Success; + VOID *HttpHdr; + EFI_HTTP_HEADER **AppendList; + UINTN Index; + EFI_HTTP_UTILITIES_PROTOCOL *HttpUtilitiesProtocol; + + Status = EFI_SUCCESS; + HttpHdrSize = 0; + MsgSize = 0; + Success = FALSE; + HttpHdr = NULL; + AppendList = NULL; + HttpUtilitiesProtocol = NULL; + + // + // 1. If we have a Request, we cannot have a NULL Url + // 2. If we have a Request, HeaderCount can not be non-zero + // 3. If we do not have a Request, HeaderCount should be zero + // 4. If we do not have Request and Headers, we need at least a message-body + // + if ((Message == NULL || RequestMsg == NULL || RequestMsgSize == NULL) || + (Message->Data.Request != NULL && Url == NULL) || + (Message->Data.Request != NULL && Message->HeaderCount == 0) || + (Message->Data.Request == NULL && Message->HeaderCount != 0) || + (Message->Data.Request == NULL && Message->HeaderCount == 0 && Message->BodyLength == 0)) { + return EFI_INVALID_PARAMETER; + } + + if (Message->HeaderCount != 0) { + // + // Locate the HTTP_UTILITIES protocol. + // + Status = gBS->LocateProtocol ( + &gEfiHttpUtilitiesProtocolGuid, + NULL, + (VOID **) &HttpUtilitiesProtocol + ); + + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR,"Failed to locate Http Utilities protocol. Status = %r.\n", Status)); + return Status; + } + + // + // Build AppendList to send into HttpUtilitiesBuild + // + AppendList = AllocateZeroPool (sizeof (EFI_HTTP_HEADER *) * (Message->HeaderCount)); + if (AppendList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + for(Index = 0; Index < Message->HeaderCount; Index++){ + AppendList[Index] = &Message->Headers[Index]; + } + + // + // Build raw HTTP Headers + // + Status = HttpUtilitiesProtocol->Build ( + HttpUtilitiesProtocol, + 0, + NULL, + 0, + NULL, + Message->HeaderCount, + AppendList, + &HttpHdrSize, + &HttpHdr + ); + + FreePool (AppendList); + + if (EFI_ERROR (Status) || HttpHdr == NULL){ + return Status; + } + } + + // + // If we have headers to be sent, account for it. + // + if (Message->HeaderCount != 0) { + MsgSize = HttpHdrSize; + } + + // + // If we have a request line, account for the fields. + // + if (Message->Data.Request != NULL) { + MsgSize += HTTP_METHOD_MAXIMUM_LEN + AsciiStrLen (HTTP_VERSION_CRLF_STR) + AsciiStrLen (Url); + } + + + // + // If we have a message body to be sent, account for it. + // + MsgSize += Message->BodyLength; + + // + // memory for the string that needs to be sent to TCP + // + *RequestMsg = NULL; + *RequestMsg = AllocateZeroPool (MsgSize); + if (*RequestMsg == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + RequestPtr = *RequestMsg; + // + // Construct header request + // + if (Message->Data.Request != NULL) { + switch (Message->Data.Request->Method) { + case HttpMethodGet: + StrLength = sizeof (HTTP_METHOD_GET) - 1; + CopyMem (RequestPtr, HTTP_METHOD_GET, StrLength); + RequestPtr += StrLength; + break; + case HttpMethodPut: + StrLength = sizeof (HTTP_METHOD_PUT) - 1; + CopyMem (RequestPtr, HTTP_METHOD_PUT, StrLength); + RequestPtr += StrLength; + break; + case HttpMethodPatch: + StrLength = sizeof (HTTP_METHOD_PATCH) - 1; + CopyMem (RequestPtr, HTTP_METHOD_PATCH, StrLength); + RequestPtr += StrLength; + break; + case HttpMethodPost: + StrLength = sizeof (HTTP_METHOD_POST) - 1; + CopyMem (RequestPtr, HTTP_METHOD_POST, StrLength); + RequestPtr += StrLength; + break; + case HttpMethodHead: + StrLength = sizeof (HTTP_METHOD_HEAD) - 1; + CopyMem (RequestPtr, HTTP_METHOD_HEAD, StrLength); + RequestPtr += StrLength; + break; + case HttpMethodDelete: + StrLength = sizeof (HTTP_METHOD_DELETE) - 1; + CopyMem (RequestPtr, HTTP_METHOD_DELETE, StrLength); + RequestPtr += StrLength; + break; + default: + ASSERT (FALSE); + Status = EFI_INVALID_PARAMETER; + goto Exit; + } + + StrLength = AsciiStrLen(EMPTY_SPACE); + CopyMem (RequestPtr, EMPTY_SPACE, StrLength); + RequestPtr += StrLength; + + StrLength = AsciiStrLen (Url); + CopyMem (RequestPtr, Url, StrLength); + RequestPtr += StrLength; + + StrLength = sizeof (HTTP_VERSION_CRLF_STR) - 1; + CopyMem (RequestPtr, HTTP_VERSION_CRLF_STR, StrLength); + RequestPtr += StrLength; + + if (HttpHdr != NULL) { + // + // Construct header + // + CopyMem (RequestPtr, HttpHdr, HttpHdrSize); + RequestPtr += HttpHdrSize; + } + } + + // + // Construct body + // + if (Message->Body != NULL) { + CopyMem (RequestPtr, Message->Body, Message->BodyLength); + RequestPtr += Message->BodyLength; + } + + // + // Done + // + (*RequestMsgSize) = (UINTN)(RequestPtr) - (UINTN)(*RequestMsg); + Success = TRUE; + +Exit: + + if (!Success) { + if (*RequestMsg != NULL) { + FreePool (*RequestMsg); + } + *RequestMsg = NULL; + return Status; + } + + if (HttpHdr != NULL) { + FreePool (HttpHdr); + } + + return EFI_SUCCESS; +} + +/** + Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined + in UEFI 2.5 specification. + + @param[in] StatusCode The status code value in HTTP message. + + @return Value defined in EFI_HTTP_STATUS_CODE . + +**/ +EFI_HTTP_STATUS_CODE +EFIAPI +HttpMappingToStatusCode ( + IN UINTN StatusCode + ) +{ + switch (StatusCode) { + case 100: + return HTTP_STATUS_100_CONTINUE; + case 101: + return HTTP_STATUS_101_SWITCHING_PROTOCOLS; + case 200: + return HTTP_STATUS_200_OK; + case 201: + return HTTP_STATUS_201_CREATED; + case 202: + return HTTP_STATUS_202_ACCEPTED; + case 203: + return HTTP_STATUS_203_NON_AUTHORITATIVE_INFORMATION; + case 204: + return HTTP_STATUS_204_NO_CONTENT; + case 205: + return HTTP_STATUS_205_RESET_CONTENT; + case 206: + return HTTP_STATUS_206_PARTIAL_CONTENT; + case 300: + return HTTP_STATUS_300_MULTIPLE_CHOICES; + case 301: + return HTTP_STATUS_301_MOVED_PERMANENTLY; + case 302: + return HTTP_STATUS_302_FOUND; + case 303: + return HTTP_STATUS_303_SEE_OTHER; + case 304: + return HTTP_STATUS_304_NOT_MODIFIED; + case 305: + return HTTP_STATUS_305_USE_PROXY; + case 307: + return HTTP_STATUS_307_TEMPORARY_REDIRECT; + case 308: + return HTTP_STATUS_308_PERMANENT_REDIRECT; + case 400: + return HTTP_STATUS_400_BAD_REQUEST; + case 401: + return HTTP_STATUS_401_UNAUTHORIZED; + case 402: + return HTTP_STATUS_402_PAYMENT_REQUIRED; + case 403: + return HTTP_STATUS_403_FORBIDDEN; + case 404: + return HTTP_STATUS_404_NOT_FOUND; + case 405: + return HTTP_STATUS_405_METHOD_NOT_ALLOWED; + case 406: + return HTTP_STATUS_406_NOT_ACCEPTABLE; + case 407: + return HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED; + case 408: + return HTTP_STATUS_408_REQUEST_TIME_OUT; + case 409: + return HTTP_STATUS_409_CONFLICT; + case 410: + return HTTP_STATUS_410_GONE; + case 411: + return HTTP_STATUS_411_LENGTH_REQUIRED; + case 412: + return HTTP_STATUS_412_PRECONDITION_FAILED; + case 413: + return HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE; + case 414: + return HTTP_STATUS_414_REQUEST_URI_TOO_LARGE; + case 415: + return HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE; + case 416: + return HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED; + case 417: + return HTTP_STATUS_417_EXPECTATION_FAILED; + case 500: + return HTTP_STATUS_500_INTERNAL_SERVER_ERROR; + case 501: + return HTTP_STATUS_501_NOT_IMPLEMENTED; + case 502: + return HTTP_STATUS_502_BAD_GATEWAY; + case 503: + return HTTP_STATUS_503_SERVICE_UNAVAILABLE; + case 504: + return HTTP_STATUS_504_GATEWAY_TIME_OUT; + case 505: + return HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED; + + default: + return HTTP_STATUS_UNSUPPORTED_STATUS; + } +} + +/** + Check whether header field called FieldName is in DeleteList. + + @param[in] DeleteList Pointer to array of key/value header pairs. + @param[in] DeleteCount The number of header pairs. + @param[in] FieldName Pointer to header field's name. + + @return TRUE if FieldName is not in DeleteList, that means this header field is valid. + @return FALSE if FieldName is in DeleteList, that means this header field is invalid. + +**/ +BOOLEAN +EFIAPI +HttpIsValidHttpHeader ( + IN CHAR8 *DeleteList[], + IN UINTN DeleteCount, + IN CHAR8 *FieldName + ) +{ + UINTN Index; + + if (FieldName == NULL) { + return FALSE; + } + + for (Index = 0; Index < DeleteCount; Index++) { + if (DeleteList[Index] == NULL) { + continue; + } + + if (AsciiStrCmp (FieldName, DeleteList[Index]) == 0) { + return FALSE; + } + } + + return TRUE; +} + diff --git a/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.h b/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.h new file mode 100644 index 000000000..d064a9843 --- /dev/null +++ b/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.h @@ -0,0 +1,85 @@ +/** @file +Header file for HttpLib. + + Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _DXE_HTTP_LIB_H_ +#define _DXE_HTTP_LIB_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BIT(x) (1 << x) + +#define HTTP_VERSION_CRLF_STR " HTTP/1.1\r\n" +#define EMPTY_SPACE " " + +#define NET_IS_HEX_CHAR(Ch) \ + ((('0' <= (Ch)) && ((Ch) <= '9')) || \ + (('A' <= (Ch)) && ((Ch) <= 'F')) || \ + (('a' <= (Ch)) && ((Ch) <= 'f'))) + +// +// Field index of the HTTP URL parse result. +// +#define HTTP_URI_FIELD_SCHEME 0 +#define HTTP_URI_FIELD_AUTHORITY 1 +#define HTTP_URI_FIELD_PATH 2 +#define HTTP_URI_FIELD_QUERY 3 +#define HTTP_URI_FIELD_FRAGMENT 4 +#define HTTP_URI_FIELD_USERINFO 5 +#define HTTP_URI_FIELD_HOST 6 +#define HTTP_URI_FIELD_PORT 7 +#define HTTP_URI_FIELD_MAX 8 + +#define HTTP_URI_PORT_MAX_NUM 65535 + +// +// Structure to store the parse result of a HTTP URL. +// +typedef struct { + UINT32 Offset; + UINT32 Length; +} HTTP_URL_FILED_DATA; + +typedef struct { + UINT16 FieldBitMap; + HTTP_URL_FILED_DATA FieldData[HTTP_URI_FIELD_MAX]; +} HTTP_URL_PARSER; + +typedef enum { + UrlParserUrlStart, + UrlParserScheme, + UrlParserSchemeColon, // ":" + UrlParserSchemeColonSlash, // ":/" + UrlParserSchemeColonSlashSlash, // "://" + UrlParserAuthority, + UrlParserAtInAuthority, + UrlParserPath, + UrlParserQueryStart, // "?" + UrlParserQuery, + UrlParserFragmentStart, // "#" + UrlParserFragment, + UrlParserUserInfo, + UrlParserHostStart, // "@" + UrlParserHost, + UrlParserHostIpv6, // "["(Ipv6 address) "]" + UrlParserPortStart, // ":" + UrlParserPort, + UrlParserStateMax +} HTTP_URL_PARSE_STATE; + +#endif + diff --git a/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.inf b/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.inf new file mode 100644 index 000000000..984a24ffd --- /dev/null +++ b/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.inf @@ -0,0 +1,43 @@ +## @file +# It provides the helper routines to parse the HTTP message byte stream. +# +# Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+# (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = DxeHttpLib + MODULE_UNI_FILE = DxeHttpLib.uni + FILE_GUID = ABBAB4CD-EA88-45b9-8234-C8A7450531FC + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = HttpLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources] + DxeHttpLib.c + DxeHttpLib.h + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + UefiBootServicesTableLib + MemoryAllocationLib + NetLib + +[Protocols] + gEfiHttpUtilitiesProtocolGuid ## SOMETIMES_CONSUMES diff --git a/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.uni b/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.uni new file mode 100644 index 000000000..a2954c023 --- /dev/null +++ b/NetworkPkg/Library/DxeHttpLib/DxeHttpLib.uni @@ -0,0 +1,16 @@ +// /** @file +// Provides the helper routines for HTTP. +// +// This library instance provides the helper routines to parse the HTTP message byte stream. +// +// Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Provides the helper routines for HTTP" + +#string STR_MODULE_DESCRIPTION #language en-US "This library instance provides the helper routines to parse the HTTP message byte stream." + diff --git a/NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.c b/NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.c new file mode 100644 index 000000000..d45f0070b --- /dev/null +++ b/NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.c @@ -0,0 +1,2291 @@ +/** @file + IpIo Library. + +(C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + + +GLOBAL_REMOVE_IF_UNREFERENCED LIST_ENTRY mActiveIpIoList = { + &mActiveIpIoList, + &mActiveIpIoList +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_IP4_CONFIG_DATA mIp4IoDefaultIpConfigData = { + EFI_IP_PROTO_UDP, + FALSE, + TRUE, + FALSE, + FALSE, + FALSE, + {{0, 0, 0, 0}}, + {{0, 0, 0, 0}}, + 0, + 255, + FALSE, + FALSE, + 0, + 0 +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_IP6_CONFIG_DATA mIp6IoDefaultIpConfigData = { + EFI_IP_PROTO_UDP, + FALSE, + TRUE, + FALSE, + {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + 0, + 255, + 0, + 0, + 0 +}; + +GLOBAL_REMOVE_IF_UNREFERENCED ICMP_ERROR_INFO mIcmpErrMap[10] = { + {FALSE, TRUE }, // ICMP_ERR_UNREACH_NET + {FALSE, TRUE }, // ICMP_ERR_UNREACH_HOST + {TRUE, TRUE }, // ICMP_ERR_UNREACH_PROTOCOL + {TRUE, TRUE }, // ICMP_ERR_UNREACH_PORT + {TRUE, TRUE }, // ICMP_ERR_MSGSIZE + {FALSE, TRUE }, // ICMP_ERR_UNREACH_SRCFAIL + {FALSE, TRUE }, // ICMP_ERR_TIMXCEED_INTRANS + {FALSE, TRUE }, // ICMP_ERR_TIMEXCEED_REASS + {FALSE, FALSE}, // ICMP_ERR_QUENCH + {FALSE, TRUE } // ICMP_ERR_PARAMPROB +}; + +GLOBAL_REMOVE_IF_UNREFERENCED ICMP_ERROR_INFO mIcmp6ErrMap[10] = { + {FALSE, TRUE}, // ICMP6_ERR_UNREACH_NET + {FALSE, TRUE}, // ICMP6_ERR_UNREACH_HOST + {TRUE, TRUE}, // ICMP6_ERR_UNREACH_PROTOCOL + {TRUE, TRUE}, // ICMP6_ERR_UNREACH_PORT + {TRUE, TRUE}, // ICMP6_ERR_PACKAGE_TOOBIG + {FALSE, TRUE}, // ICMP6_ERR_TIMXCEED_HOPLIMIT + {FALSE, TRUE}, // ICMP6_ERR_TIMXCEED_REASS + {FALSE, TRUE}, // ICMP6_ERR_PARAMPROB_HEADER + {FALSE, TRUE}, // ICMP6_ERR_PARAMPROB_NEXHEADER + {FALSE, TRUE} // ICMP6_ERR_PARAMPROB_IPV6OPTION +}; + + +/** + Notify function for IP transmit token. + + @param[in] Context The context passed in by the event notifier. + +**/ +VOID +EFIAPI +IpIoTransmitHandlerDpc ( + IN VOID *Context + ); + + +/** + Notify function for IP transmit token. + + @param[in] Event The event signaled. + @param[in] Context The context passed in by the event notifier. + +**/ +VOID +EFIAPI +IpIoTransmitHandler ( + IN EFI_EVENT Event, + IN VOID *Context + ); + + +/** + This function create an IP child ,open the IP protocol, and return the opened + IP protocol as Interface. + + @param[in] ControllerHandle The controller handle. + @param[in] ImageHandle The image handle. + @param[in] ChildHandle Pointer to the buffer to save the IP child handle. + @param[in] IpVersion The version of the IP protocol to use, either + IPv4 or IPv6. + @param[out] Interface Pointer used to get the IP protocol interface. + + @retval EFI_SUCCESS The IP child is created and the IP protocol + interface is retrieved. + @retval EFI_UNSUPPORTED Upsupported IpVersion. + @retval Others The required operation failed. + +**/ +EFI_STATUS +IpIoCreateIpChildOpenProtocol ( + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE *ChildHandle, + IN UINT8 IpVersion, + OUT VOID **Interface + ) +{ + EFI_STATUS Status; + EFI_GUID *ServiceBindingGuid; + EFI_GUID *IpProtocolGuid; + + if (IpVersion == IP_VERSION_4) { + ServiceBindingGuid = &gEfiIp4ServiceBindingProtocolGuid; + IpProtocolGuid = &gEfiIp4ProtocolGuid; + } else if (IpVersion == IP_VERSION_6){ + ServiceBindingGuid = &gEfiIp6ServiceBindingProtocolGuid; + IpProtocolGuid = &gEfiIp6ProtocolGuid; + } else { + return EFI_UNSUPPORTED; + } + + // + // Create an IP child. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + ImageHandle, + ServiceBindingGuid, + ChildHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Open the IP protocol installed on the *ChildHandle. + // + Status = gBS->OpenProtocol ( + *ChildHandle, + IpProtocolGuid, + Interface, + ImageHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + // + // On failure, destroy the IP child. + // + NetLibDestroyServiceChild ( + ControllerHandle, + ImageHandle, + ServiceBindingGuid, + *ChildHandle + ); + } + + return Status; +} + + +/** + This function close the previously openned IP protocol and destroy the IP child. + + @param[in] ControllerHandle The controller handle. + @param[in] ImageHandle The image handle. + @param[in] ChildHandle The child handle of the IP child. + @param[in] IpVersion The version of the IP protocol to use, either + IPv4 or IPv6. + + @retval EFI_SUCCESS The IP protocol is closed and the relevant IP child + is destroyed. + @retval EFI_UNSUPPORTED Upsupported IpVersion. + @retval Others The required operation failed. + +**/ +EFI_STATUS +IpIoCloseProtocolDestroyIpChild ( + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ChildHandle, + IN UINT8 IpVersion + ) +{ + EFI_STATUS Status; + EFI_GUID *ServiceBindingGuid; + EFI_GUID *IpProtocolGuid; + + if (IpVersion == IP_VERSION_4) { + ServiceBindingGuid = &gEfiIp4ServiceBindingProtocolGuid; + IpProtocolGuid = &gEfiIp4ProtocolGuid; + } else if (IpVersion == IP_VERSION_6) { + ServiceBindingGuid = &gEfiIp6ServiceBindingProtocolGuid; + IpProtocolGuid = &gEfiIp6ProtocolGuid; + } else { + return EFI_UNSUPPORTED; + } + + // + // Close the previously openned IP protocol. + // + Status = gBS->CloseProtocol ( + ChildHandle, + IpProtocolGuid, + ImageHandle, + ControllerHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Destroy the IP child. + // + return NetLibDestroyServiceChild ( + ControllerHandle, + ImageHandle, + ServiceBindingGuid, + ChildHandle + ); +} + +/** + This function handles ICMPv4 packets. It is the worker function of + IpIoIcmpHandler. + + @param[in] IpIo Pointer to the IP_IO instance. + @param[in, out] Pkt Pointer to the ICMPv4 packet. + @param[in] Session Pointer to the net session of this ICMPv4 packet. + + @retval EFI_SUCCESS The ICMPv4 packet is handled successfully. + @retval EFI_ABORTED This type of ICMPv4 packet is not supported. + +**/ +EFI_STATUS +IpIoIcmpv4Handler ( + IN IP_IO *IpIo, + IN OUT NET_BUF *Pkt, + IN EFI_NET_SESSION_DATA *Session + ) +{ + IP4_ICMP_ERROR_HEAD *IcmpHdr; + EFI_IP4_HEADER *IpHdr; + UINT8 IcmpErr; + UINT8 *PayLoadHdr; + UINT8 Type; + UINT8 Code; + UINT32 TrimBytes; + + ASSERT (IpIo != NULL); + ASSERT (Pkt != NULL); + ASSERT (Session != NULL); + ASSERT (IpIo->IpVersion == IP_VERSION_4); + + // + // Check the ICMP packet length. + // + if (Pkt->TotalSize < sizeof (IP4_ICMP_ERROR_HEAD)) { + return EFI_ABORTED; + } + + IcmpHdr = NET_PROTO_HDR (Pkt, IP4_ICMP_ERROR_HEAD); + IpHdr = (EFI_IP4_HEADER *) (&IcmpHdr->IpHead); + + if (Pkt->TotalSize < ICMP_ERRLEN (IpHdr)) { + + return EFI_ABORTED; + } + + Type = IcmpHdr->Head.Type; + Code = IcmpHdr->Head.Code; + + // + // Analyze the ICMP Error in this ICMP pkt + // + switch (Type) { + case ICMP_TYPE_UNREACH: + switch (Code) { + case ICMP_CODE_UNREACH_NET: + case ICMP_CODE_UNREACH_HOST: + case ICMP_CODE_UNREACH_PROTOCOL: + case ICMP_CODE_UNREACH_PORT: + case ICMP_CODE_UNREACH_SRCFAIL: + IcmpErr = (UINT8) (ICMP_ERR_UNREACH_NET + Code); + + break; + + case ICMP_CODE_UNREACH_NEEDFRAG: + IcmpErr = ICMP_ERR_MSGSIZE; + + break; + + case ICMP_CODE_UNREACH_NET_UNKNOWN: + case ICMP_CODE_UNREACH_NET_PROHIB: + case ICMP_CODE_UNREACH_TOSNET: + IcmpErr = ICMP_ERR_UNREACH_NET; + + break; + + case ICMP_CODE_UNREACH_HOST_UNKNOWN: + case ICMP_CODE_UNREACH_ISOLATED: + case ICMP_CODE_UNREACH_HOST_PROHIB: + case ICMP_CODE_UNREACH_TOSHOST: + IcmpErr = ICMP_ERR_UNREACH_HOST; + + break; + + default: + return EFI_ABORTED; + } + + break; + + case ICMP_TYPE_TIMXCEED: + if (Code > 1) { + return EFI_ABORTED; + } + + IcmpErr = (UINT8) (Code + ICMP_ERR_TIMXCEED_INTRANS); + + break; + + case ICMP_TYPE_PARAMPROB: + if (Code > 1) { + return EFI_ABORTED; + } + + IcmpErr = ICMP_ERR_PARAMPROB; + + break; + + case ICMP_TYPE_SOURCEQUENCH: + if (Code != 0) { + return EFI_ABORTED; + } + + IcmpErr = ICMP_ERR_QUENCH; + + break; + + default: + return EFI_ABORTED; + } + + // + // Notify user the ICMP pkt only containing payload except + // IP and ICMP header + // + PayLoadHdr = (UINT8 *) ((UINT8 *) IpHdr + EFI_IP4_HEADER_LEN (IpHdr)); + TrimBytes = (UINT32) (PayLoadHdr - (UINT8 *) IcmpHdr); + + NetbufTrim (Pkt, TrimBytes, TRUE); + + // + // If the input packet has invalid format, and TrimBytes is larger than + // the packet size, the NetbufTrim might trim the packet to zero. + // + if (Pkt->TotalSize != 0) { + IpIo->PktRcvdNotify (EFI_ICMP_ERROR, IcmpErr, Session, Pkt, IpIo->RcvdContext); + } + + return EFI_SUCCESS; +} + +/** + This function handles ICMPv6 packets. It is the worker function of + IpIoIcmpHandler. + + @param[in] IpIo Pointer to the IP_IO instance. + @param[in, out] Pkt Pointer to the ICMPv6 packet. + @param[in] Session Pointer to the net session of this ICMPv6 packet. + + @retval EFI_SUCCESS The ICMPv6 packet is handled successfully. + @retval EFI_ABORTED This type of ICMPv6 packet is not supported. + +**/ +EFI_STATUS +IpIoIcmpv6Handler ( + IN IP_IO *IpIo, + IN OUT NET_BUF *Pkt, + IN EFI_NET_SESSION_DATA *Session + ) +{ + IP6_ICMP_ERROR_HEAD *IcmpHdr; + EFI_IP6_HEADER *IpHdr; + UINT8 IcmpErr; + UINT8 *PayLoadHdr; + UINT8 Type; + UINT8 Code; + UINT8 NextHeader; + UINT32 TrimBytes; + BOOLEAN Flag; + + ASSERT (IpIo != NULL); + ASSERT (Pkt != NULL); + ASSERT (Session != NULL); + ASSERT (IpIo->IpVersion == IP_VERSION_6); + + // + // Check the ICMPv6 packet length. + // + if (Pkt->TotalSize < sizeof (IP6_ICMP_ERROR_HEAD)) { + + return EFI_ABORTED; + } + + IcmpHdr = NET_PROTO_HDR (Pkt, IP6_ICMP_ERROR_HEAD); + Type = IcmpHdr->Head.Type; + Code = IcmpHdr->Head.Code; + + // + // Analyze the ICMPv6 Error in this ICMPv6 packet + // + switch (Type) { + case ICMP_V6_DEST_UNREACHABLE: + switch (Code) { + case ICMP_V6_NO_ROUTE_TO_DEST: + case ICMP_V6_BEYOND_SCOPE: + case ICMP_V6_ROUTE_REJECTED: + IcmpErr = ICMP6_ERR_UNREACH_NET; + + break; + + case ICMP_V6_COMM_PROHIBITED: + case ICMP_V6_ADDR_UNREACHABLE: + case ICMP_V6_SOURCE_ADDR_FAILED: + IcmpErr = ICMP6_ERR_UNREACH_HOST; + + break; + + case ICMP_V6_PORT_UNREACHABLE: + IcmpErr = ICMP6_ERR_UNREACH_PORT; + + break; + + default: + return EFI_ABORTED; + } + + break; + + case ICMP_V6_PACKET_TOO_BIG: + if (Code >= 1) { + return EFI_ABORTED; + } + + IcmpErr = ICMP6_ERR_PACKAGE_TOOBIG; + + break; + + case ICMP_V6_TIME_EXCEEDED: + if (Code > 1) { + return EFI_ABORTED; + } + + IcmpErr = (UINT8) (ICMP6_ERR_TIMXCEED_HOPLIMIT + Code); + + break; + + case ICMP_V6_PARAMETER_PROBLEM: + if (Code > 3) { + return EFI_ABORTED; + } + + IcmpErr = (UINT8) (ICMP6_ERR_PARAMPROB_HEADER + Code); + + break; + + default: + + return EFI_ABORTED; + } + + // + // Notify user the ICMPv6 packet only containing payload except + // IPv6 basic header, extension header and ICMP header + // + + IpHdr = (EFI_IP6_HEADER *) (&IcmpHdr->IpHead); + NextHeader = IpHdr->NextHeader; + PayLoadHdr = (UINT8 *) ((UINT8 *) IcmpHdr + sizeof (IP6_ICMP_ERROR_HEAD)); + Flag = TRUE; + + do { + switch (NextHeader) { + case EFI_IP_PROTO_UDP: + case EFI_IP_PROTO_TCP: + case EFI_IP_PROTO_ICMP: + case IP6_NO_NEXT_HEADER: + Flag = FALSE; + + break; + + case IP6_HOP_BY_HOP: + case IP6_DESTINATION: + // + // The Hdr Ext Len is 8-bit unsigned integer in 8-octet units, not including + // the first 8 octets. + // + NextHeader = *(PayLoadHdr); + PayLoadHdr = (UINT8 *) (PayLoadHdr + (*(PayLoadHdr + 1) + 1) * 8); + + break; + + case IP6_FRAGMENT: + // + // The Fragment Header Length is 8 octets. + // + NextHeader = *(PayLoadHdr); + PayLoadHdr = (UINT8 *) (PayLoadHdr + 8); + + break; + + default: + + return EFI_ABORTED; + } + } while (Flag); + + TrimBytes = (UINT32) (PayLoadHdr - (UINT8 *) IcmpHdr); + + NetbufTrim (Pkt, TrimBytes, TRUE); + + // + // If the input packet has invalid format, and TrimBytes is larger than + // the packet size, the NetbufTrim might trim the packet to zero. + // + if (Pkt->TotalSize != 0) { + IpIo->PktRcvdNotify (EFI_ICMP_ERROR, IcmpErr, Session, Pkt, IpIo->RcvdContext); + } + + return EFI_SUCCESS; +} + +/** + This function handles ICMP packets. + + @param[in] IpIo Pointer to the IP_IO instance. + @param[in, out] Pkt Pointer to the ICMP packet. + @param[in] Session Pointer to the net session of this ICMP packet. + + @retval EFI_SUCCESS The ICMP packet is handled successfully. + @retval EFI_ABORTED This type of ICMP packet is not supported. + @retval EFI_UNSUPPORTED The IP protocol version in IP_IO is not supported. + +**/ +EFI_STATUS +IpIoIcmpHandler ( + IN IP_IO *IpIo, + IN OUT NET_BUF *Pkt, + IN EFI_NET_SESSION_DATA *Session + ) +{ + + if (IpIo->IpVersion == IP_VERSION_4) { + + return IpIoIcmpv4Handler (IpIo, Pkt, Session); + + } else if (IpIo->IpVersion == IP_VERSION_6) { + + return IpIoIcmpv6Handler (IpIo, Pkt, Session); + + } else { + + return EFI_UNSUPPORTED; + } +} + + +/** + Free function for receive token of IP_IO. It is used to + signal the recycle event to notify IP to recycle the + data buffer. + + @param[in] Event The event to be signaled. + +**/ +VOID +EFIAPI +IpIoExtFree ( + IN VOID *Event + ) +{ + gBS->SignalEvent ((EFI_EVENT) Event); +} + + +/** + Create a send entry to wrap a packet before sending + out it through IP. + + @param[in, out] IpIo Pointer to the IP_IO instance. + @param[in, out] Pkt Pointer to the packet. + @param[in] Sender Pointer to the IP sender. + @param[in] Context Pointer to the context. + @param[in] NotifyData Pointer to the notify data. + @param[in] Dest Pointer to the destination IP address. + @param[in] Override Pointer to the overriden IP_IO data. + + @return Pointer to the data structure created to wrap the packet. If any error occurs, + then return NULL. + +**/ +IP_IO_SEND_ENTRY * +IpIoCreateSndEntry ( + IN OUT IP_IO *IpIo, + IN OUT NET_BUF *Pkt, + IN IP_IO_IP_PROTOCOL Sender, + IN VOID *Context OPTIONAL, + IN VOID *NotifyData OPTIONAL, + IN EFI_IP_ADDRESS *Dest OPTIONAL, + IN IP_IO_OVERRIDE *Override + ) +{ + IP_IO_SEND_ENTRY *SndEntry; + EFI_EVENT Event; + EFI_STATUS Status; + NET_FRAGMENT *ExtFragment; + UINT32 FragmentCount; + IP_IO_OVERRIDE *OverrideData; + IP_IO_IP_TX_DATA *TxData; + EFI_IP4_TRANSMIT_DATA *Ip4TxData; + EFI_IP6_TRANSMIT_DATA *Ip6TxData; + + if ((IpIo->IpVersion != IP_VERSION_4) && (IpIo->IpVersion != IP_VERSION_6)) { + return NULL; + } + + Event = NULL; + TxData = NULL; + OverrideData = NULL; + + // + // Allocate resource for SndEntry + // + SndEntry = AllocatePool (sizeof (IP_IO_SEND_ENTRY)); + if (NULL == SndEntry) { + return NULL; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + IpIoTransmitHandler, + SndEntry, + &Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + FragmentCount = Pkt->BlockOpNum; + + // + // Allocate resource for TxData + // + TxData = (IP_IO_IP_TX_DATA *) AllocatePool ( + sizeof (IP_IO_IP_TX_DATA) + sizeof (NET_FRAGMENT) * (FragmentCount - 1) + ); + + if (NULL == TxData) { + goto ON_ERROR; + } + + // + // Build a fragment table to contain the fragments in the packet. + // + if (IpIo->IpVersion == IP_VERSION_4) { + ExtFragment = (NET_FRAGMENT *) TxData->Ip4TxData.FragmentTable; + } else { + ExtFragment = (NET_FRAGMENT *) TxData->Ip6TxData.FragmentTable; + } + + NetbufBuildExt (Pkt, ExtFragment, &FragmentCount); + + + // + // Allocate resource for OverrideData if needed + // + if (NULL != Override) { + + OverrideData = AllocateCopyPool (sizeof (IP_IO_OVERRIDE), Override); + if (NULL == OverrideData) { + goto ON_ERROR; + } + } + + // + // Set other fields of TxData except the fragment table + // + if (IpIo->IpVersion == IP_VERSION_4) { + + Ip4TxData = &TxData->Ip4TxData; + + IP4_COPY_ADDRESS (&Ip4TxData->DestinationAddress, Dest); + + Ip4TxData->OverrideData = &OverrideData->Ip4OverrideData; + Ip4TxData->OptionsLength = 0; + Ip4TxData->OptionsBuffer = NULL; + Ip4TxData->TotalDataLength = Pkt->TotalSize; + Ip4TxData->FragmentCount = FragmentCount; + + // + // Set the fields of SndToken + // + SndEntry->SndToken.Ip4Token.Event = Event; + SndEntry->SndToken.Ip4Token.Packet.TxData = Ip4TxData; + } else { + + Ip6TxData = &TxData->Ip6TxData; + + if (Dest != NULL) { + CopyMem (&Ip6TxData->DestinationAddress, Dest, sizeof (EFI_IPv6_ADDRESS)); + } else { + ZeroMem (&Ip6TxData->DestinationAddress, sizeof (EFI_IPv6_ADDRESS)); + } + + Ip6TxData->OverrideData = &OverrideData->Ip6OverrideData; + Ip6TxData->DataLength = Pkt->TotalSize; + Ip6TxData->FragmentCount = FragmentCount; + Ip6TxData->ExtHdrsLength = 0; + Ip6TxData->ExtHdrs = NULL; + + // + // Set the fields of SndToken + // + SndEntry->SndToken.Ip6Token.Event = Event; + SndEntry->SndToken.Ip6Token.Packet.TxData = Ip6TxData; + } + + // + // Set the fields of SndEntry + // + SndEntry->IpIo = IpIo; + SndEntry->Ip = Sender; + SndEntry->Context = Context; + SndEntry->NotifyData = NotifyData; + + SndEntry->Pkt = Pkt; + NET_GET_REF (Pkt); + + InsertTailList (&IpIo->PendingSndList, &SndEntry->Entry); + + return SndEntry; + +ON_ERROR: + + if (OverrideData != NULL) { + FreePool (OverrideData); + } + + if (TxData != NULL) { + FreePool (TxData); + } + + if (SndEntry != NULL) { + FreePool (SndEntry); + } + + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + return NULL; +} + + +/** + Destroy the SndEntry. + + This function pairs with IpIoCreateSndEntry(). + + @param[in] SndEntry Pointer to the send entry to be destroyed. + +**/ +VOID +IpIoDestroySndEntry ( + IN IP_IO_SEND_ENTRY *SndEntry + ) +{ + EFI_EVENT Event; + IP_IO_IP_TX_DATA *TxData; + IP_IO_OVERRIDE *Override; + + if (SndEntry->IpIo->IpVersion == IP_VERSION_4) { + Event = SndEntry->SndToken.Ip4Token.Event; + TxData = (IP_IO_IP_TX_DATA *) SndEntry->SndToken.Ip4Token.Packet.TxData; + Override = (IP_IO_OVERRIDE *) TxData->Ip4TxData.OverrideData; + } else if (SndEntry->IpIo->IpVersion == IP_VERSION_6) { + Event = SndEntry->SndToken.Ip6Token.Event; + TxData = (IP_IO_IP_TX_DATA *) SndEntry->SndToken.Ip6Token.Packet.TxData; + Override = (IP_IO_OVERRIDE *) TxData->Ip6TxData.OverrideData; + } else { + return ; + } + + gBS->CloseEvent (Event); + + FreePool (TxData); + + if (NULL != Override) { + FreePool (Override); + } + + NetbufFree (SndEntry->Pkt); + + RemoveEntryList (&SndEntry->Entry); + + FreePool (SndEntry); +} + + +/** + Notify function for IP transmit token. + + @param[in] Context The context passed in by the event notifier. + +**/ +VOID +EFIAPI +IpIoTransmitHandlerDpc ( + IN VOID *Context + ) +{ + IP_IO *IpIo; + IP_IO_SEND_ENTRY *SndEntry; + EFI_STATUS Status; + + SndEntry = (IP_IO_SEND_ENTRY *) Context; + + IpIo = SndEntry->IpIo; + + if (IpIo->IpVersion == IP_VERSION_4) { + Status = SndEntry->SndToken.Ip4Token.Status; + } else if (IpIo->IpVersion == IP_VERSION_6){ + Status = SndEntry->SndToken.Ip6Token.Status; + } else { + return ; + } + + if ((IpIo->PktSentNotify != NULL) && (SndEntry->NotifyData != NULL)) { + IpIo->PktSentNotify ( + Status, + SndEntry->Context, + SndEntry->Ip, + SndEntry->NotifyData + ); + } + + IpIoDestroySndEntry (SndEntry); +} + + +/** + Notify function for IP transmit token. + + @param[in] Event The event signaled. + @param[in] Context The context passed in by the event notifier. + +**/ +VOID +EFIAPI +IpIoTransmitHandler ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request IpIoTransmitHandlerDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, IpIoTransmitHandlerDpc, Context); +} + + +/** + The dummy handler for the dummy IP receive token. + + @param[in] Context The context passed in by the event notifier. + +**/ +VOID +EFIAPI +IpIoDummyHandlerDpc ( + IN VOID *Context + ) +{ + IP_IO_IP_INFO *IpInfo; + EFI_STATUS Status; + EFI_EVENT RecycleEvent; + + IpInfo = (IP_IO_IP_INFO *) Context; + + if ((IpInfo->IpVersion != IP_VERSION_4) && (IpInfo->IpVersion != IP_VERSION_6)) { + return ; + } + + RecycleEvent = NULL; + + if (IpInfo->IpVersion == IP_VERSION_4) { + Status = IpInfo->DummyRcvToken.Ip4Token.Status; + + if (IpInfo->DummyRcvToken.Ip4Token.Packet.RxData != NULL) { + RecycleEvent = IpInfo->DummyRcvToken.Ip4Token.Packet.RxData->RecycleSignal; + } + } else { + Status = IpInfo->DummyRcvToken.Ip6Token.Status; + + if (IpInfo->DummyRcvToken.Ip6Token.Packet.RxData != NULL) { + RecycleEvent = IpInfo->DummyRcvToken.Ip6Token.Packet.RxData->RecycleSignal; + } + } + + + + if (EFI_ABORTED == Status) { + // + // The reception is actively aborted by the consumer, directly return. + // + return; + } else if (EFI_SUCCESS == Status) { + // + // Recycle the RxData. + // + ASSERT (RecycleEvent != NULL); + + gBS->SignalEvent (RecycleEvent); + } + + // + // Continue the receive. + // + if (IpInfo->IpVersion == IP_VERSION_4) { + IpInfo->Ip.Ip4->Receive ( + IpInfo->Ip.Ip4, + &IpInfo->DummyRcvToken.Ip4Token + ); + } else { + IpInfo->Ip.Ip6->Receive ( + IpInfo->Ip.Ip6, + &IpInfo->DummyRcvToken.Ip6Token + ); + } +} + + +/** + This function add IpIoDummyHandlerDpc to the end of the DPC queue. + + @param[in] Event The event signaled. + @param[in] Context The context passed in by the event notifier. + +**/ +VOID +EFIAPI +IpIoDummyHandler ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request IpIoDummyHandlerDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, IpIoDummyHandlerDpc, Context); +} + + +/** + Notify function for the IP receive token, used to process + the received IP packets. + + @param[in] Context The context passed in by the event notifier. + +**/ +VOID +EFIAPI +IpIoListenHandlerDpc ( + IN VOID *Context + ) +{ + IP_IO *IpIo; + EFI_STATUS Status; + IP_IO_IP_RX_DATA *RxData; + EFI_NET_SESSION_DATA Session; + NET_BUF *Pkt; + + IpIo = (IP_IO *) Context; + + if (IpIo->IpVersion == IP_VERSION_4) { + Status = IpIo->RcvToken.Ip4Token.Status; + RxData = (IP_IO_IP_RX_DATA *) IpIo->RcvToken.Ip4Token.Packet.RxData; + } else if (IpIo->IpVersion == IP_VERSION_6) { + Status = IpIo->RcvToken.Ip6Token.Status; + RxData = (IP_IO_IP_RX_DATA *) IpIo->RcvToken.Ip6Token.Packet.RxData; + } else { + return; + } + + if (EFI_ABORTED == Status) { + // + // The reception is actively aborted by the consumer, directly return. + // + return; + } + + if ((EFI_SUCCESS != Status) && (EFI_ICMP_ERROR != Status)) { + // + // Only process the normal packets and the icmp error packets. + // + if (RxData != NULL) { + goto CleanUp; + } else { + goto Resume; + } + } + + // + // if RxData is NULL with Status == EFI_SUCCESS or EFI_ICMP_ERROR, this should be a code issue in the low layer (IP). + // + ASSERT (RxData != NULL); + if (RxData == NULL) { + goto Resume; + } + + if (NULL == IpIo->PktRcvdNotify) { + goto CleanUp; + } + + if (IpIo->IpVersion == IP_VERSION_4) { + ASSERT (RxData->Ip4RxData.Header != NULL); + if (IP4_IS_LOCAL_BROADCAST (EFI_IP4 (RxData->Ip4RxData.Header->SourceAddress))) { + // + // The source address is a broadcast address, discard it. + // + goto CleanUp; + } + if ((EFI_IP4 (RxData->Ip4RxData.Header->SourceAddress) != 0) && + (IpIo->SubnetMask != 0) && + IP4_NET_EQUAL (IpIo->StationIp, EFI_NTOHL (((EFI_IP4_RECEIVE_DATA *) RxData)->Header->SourceAddress), IpIo->SubnetMask) && + !NetIp4IsUnicast (EFI_NTOHL (((EFI_IP4_RECEIVE_DATA *) RxData)->Header->SourceAddress), IpIo->SubnetMask)) { + // + // The source address doesn't match StationIp and it's not a unicast IP address, discard it. + // + goto CleanUp; + } + + if (RxData->Ip4RxData.DataLength == 0) { + // + // Discard zero length data payload packet. + // + goto CleanUp; + } + + // + // The fragment should always be valid for non-zero length packet. + // + ASSERT (RxData->Ip4RxData.FragmentCount != 0); + + // + // Create a netbuffer representing IPv4 packet + // + Pkt = NetbufFromExt ( + (NET_FRAGMENT *) RxData->Ip4RxData.FragmentTable, + RxData->Ip4RxData.FragmentCount, + 0, + 0, + IpIoExtFree, + RxData->Ip4RxData.RecycleSignal + ); + if (NULL == Pkt) { + goto CleanUp; + } + + // + // Create a net session + // + Session.Source.Addr[0] = EFI_IP4 (RxData->Ip4RxData.Header->SourceAddress); + Session.Dest.Addr[0] = EFI_IP4 (RxData->Ip4RxData.Header->DestinationAddress); + Session.IpHdr.Ip4Hdr = RxData->Ip4RxData.Header; + Session.IpHdrLen = RxData->Ip4RxData.HeaderLength; + Session.IpVersion = IP_VERSION_4; + } else { + ASSERT (RxData->Ip6RxData.Header != NULL); + if (!NetIp6IsValidUnicast(&RxData->Ip6RxData.Header->SourceAddress)) { + goto CleanUp; + } + + if (RxData->Ip6RxData.DataLength == 0) { + // + // Discard zero length data payload packet. + // + goto CleanUp; + } + + // + // The fragment should always be valid for non-zero length packet. + // + ASSERT (RxData->Ip6RxData.FragmentCount != 0); + + // + // Create a netbuffer representing IPv6 packet + // + Pkt = NetbufFromExt ( + (NET_FRAGMENT *) RxData->Ip6RxData.FragmentTable, + RxData->Ip6RxData.FragmentCount, + 0, + 0, + IpIoExtFree, + RxData->Ip6RxData.RecycleSignal + ); + if (NULL == Pkt) { + goto CleanUp; + } + + // + // Create a net session + // + CopyMem ( + &Session.Source, + &RxData->Ip6RxData.Header->SourceAddress, + sizeof(EFI_IPv6_ADDRESS) + ); + CopyMem ( + &Session.Dest, + &RxData->Ip6RxData.Header->DestinationAddress, + sizeof(EFI_IPv6_ADDRESS) + ); + Session.IpHdr.Ip6Hdr = RxData->Ip6RxData.Header; + Session.IpHdrLen = RxData->Ip6RxData.HeaderLength; + Session.IpVersion = IP_VERSION_6; + } + + if (EFI_SUCCESS == Status) { + + IpIo->PktRcvdNotify (EFI_SUCCESS, 0, &Session, Pkt, IpIo->RcvdContext); + } else { + // + // Status is EFI_ICMP_ERROR + // + Status = IpIoIcmpHandler (IpIo, Pkt, &Session); + if (EFI_ERROR (Status)) { + NetbufFree (Pkt); + } + } + + goto Resume; + +CleanUp: + + if (IpIo->IpVersion == IP_VERSION_4){ + gBS->SignalEvent (RxData->Ip4RxData.RecycleSignal); + } else { + gBS->SignalEvent (RxData->Ip6RxData.RecycleSignal); + } + +Resume: + + if (IpIo->IpVersion == IP_VERSION_4){ + IpIo->Ip.Ip4->Receive (IpIo->Ip.Ip4, &(IpIo->RcvToken.Ip4Token)); + } else { + IpIo->Ip.Ip6->Receive (IpIo->Ip.Ip6, &(IpIo->RcvToken.Ip6Token)); + } +} + +/** + This function add IpIoListenHandlerDpc to the end of the DPC queue. + + @param[in] Event The event signaled. + @param[in] Context The context passed in by the event notifier. + +**/ +VOID +EFIAPI +IpIoListenHandler ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request IpIoListenHandlerDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, IpIoListenHandlerDpc, Context); +} + + +/** + Create a new IP_IO instance. + + If IpVersion is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + This function uses IP4/IP6 service binding protocol in Controller to create + an IP4/IP6 child (aka IP4/IP6 instance). + + @param[in] Image The image handle of the driver or application that + consumes IP_IO. + @param[in] Controller The controller handle that has IP4 or IP6 service + binding protocol installed. + @param[in] IpVersion The version of the IP protocol to use, either + IPv4 or IPv6. + + @return Pointer to a newly created IP_IO instance, or NULL if failed. + +**/ +IP_IO * +EFIAPI +IpIoCreate ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN UINT8 IpVersion + ) +{ + EFI_STATUS Status; + IP_IO *IpIo; + EFI_EVENT Event; + + ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6)); + + IpIo = AllocateZeroPool (sizeof (IP_IO)); + if (NULL == IpIo) { + return NULL; + } + + InitializeListHead (&(IpIo->PendingSndList)); + InitializeListHead (&(IpIo->IpList)); + IpIo->Controller = Controller; + IpIo->Image = Image; + IpIo->IpVersion = IpVersion; + Event = NULL; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + IpIoListenHandler, + IpIo, + &Event + ); + if (EFI_ERROR (Status)) { + goto ReleaseIpIo; + } + + if (IpVersion == IP_VERSION_4) { + IpIo->RcvToken.Ip4Token.Event = Event; + } else { + IpIo->RcvToken.Ip6Token.Event = Event; + } + + // + // Create an IP child and open IP protocol + // + Status = IpIoCreateIpChildOpenProtocol ( + Controller, + Image, + &IpIo->ChildHandle, + IpVersion, + (VOID **) & (IpIo->Ip) + ); + if (EFI_ERROR (Status)) { + goto ReleaseIpIo; + } + + return IpIo; + +ReleaseIpIo: + + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + gBS->FreePool (IpIo); + + return NULL; +} + + +/** + Open an IP_IO instance for use. + + If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + This function is called after IpIoCreate(). It is used for configuring the IP + instance and register the callbacks and their context data for sending and + receiving IP packets. + + @param[in, out] IpIo Pointer to an IP_IO instance that needs + to open. + @param[in] OpenData The configuration data and callbacks for + the IP_IO instance. + + @retval EFI_SUCCESS The IP_IO instance opened with OpenData + successfully. + @retval EFI_ACCESS_DENIED The IP_IO instance is configured, avoid to + reopen it. + @retval EFI_UNSUPPORTED IPv4 RawData mode is no supported. + @retval EFI_INVALID_PARAMETER Invalid input parameter. + @retval Others Error condition occurred. + +**/ +EFI_STATUS +EFIAPI +IpIoOpen ( + IN OUT IP_IO *IpIo, + IN IP_IO_OPEN_DATA *OpenData + ) +{ + EFI_STATUS Status; + UINT8 IpVersion; + + if (IpIo == NULL || OpenData == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (IpIo->IsConfigured) { + return EFI_ACCESS_DENIED; + } + + IpVersion = IpIo->IpVersion; + + ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6)); + + // + // configure ip + // + if (IpVersion == IP_VERSION_4){ + // + // RawData mode is no supported. + // + ASSERT (!OpenData->IpConfigData.Ip4CfgData.RawData); + if (OpenData->IpConfigData.Ip4CfgData.RawData) { + return EFI_UNSUPPORTED; + } + + if (!OpenData->IpConfigData.Ip4CfgData.UseDefaultAddress) { + IpIo->StationIp = EFI_NTOHL (OpenData->IpConfigData.Ip4CfgData.StationAddress); + IpIo->SubnetMask = EFI_NTOHL (OpenData->IpConfigData.Ip4CfgData.SubnetMask); + } + + Status = IpIo->Ip.Ip4->Configure ( + IpIo->Ip.Ip4, + &OpenData->IpConfigData.Ip4CfgData + ); + } else { + + Status = IpIo->Ip.Ip6->Configure ( + IpIo->Ip.Ip6, + &OpenData->IpConfigData.Ip6CfgData + ); + } + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // @bug To delete the default route entry in this Ip, if it is: + // @bug (0.0.0.0, 0.0.0.0, 0.0.0.0). Delete this statement if Ip modified + // @bug its code + // + if (IpVersion == IP_VERSION_4){ + Status = IpIo->Ip.Ip4->Routes ( + IpIo->Ip.Ip4, + TRUE, + &mZeroIp4Addr, + &mZeroIp4Addr, + &mZeroIp4Addr + ); + + if (EFI_ERROR (Status) && (EFI_NOT_FOUND != Status)) { + return Status; + } + } + + IpIo->PktRcvdNotify = OpenData->PktRcvdNotify; + IpIo->PktSentNotify = OpenData->PktSentNotify; + + IpIo->RcvdContext = OpenData->RcvdContext; + IpIo->SndContext = OpenData->SndContext; + + if (IpVersion == IP_VERSION_4){ + IpIo->Protocol = OpenData->IpConfigData.Ip4CfgData.DefaultProtocol; + + // + // start to listen incoming packet + // + Status = IpIo->Ip.Ip4->Receive ( + IpIo->Ip.Ip4, + &(IpIo->RcvToken.Ip4Token) + ); + if (EFI_ERROR (Status)) { + IpIo->Ip.Ip4->Configure (IpIo->Ip.Ip4, NULL); + return Status; + } + + } else { + + IpIo->Protocol = OpenData->IpConfigData.Ip6CfgData.DefaultProtocol; + Status = IpIo->Ip.Ip6->Receive ( + IpIo->Ip.Ip6, + &(IpIo->RcvToken.Ip6Token) + ); + if (EFI_ERROR (Status)) { + IpIo->Ip.Ip6->Configure (IpIo->Ip.Ip6, NULL); + return Status; + } + } + + IpIo->IsConfigured = TRUE; + InsertTailList (&mActiveIpIoList, &IpIo->Entry); + + return Status; +} + + +/** + Stop an IP_IO instance. + + If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + This function is paired with IpIoOpen(). The IP_IO will be unconfigured and all + the pending send/receive tokens will be canceled. + + @param[in, out] IpIo Pointer to the IP_IO instance that needs to stop. + + @retval EFI_SUCCESS The IP_IO instance stopped successfully. + @retval EFI_INVALID_PARAMETER Invalid input parameter. + @retval Others Error condition occurred. + +**/ +EFI_STATUS +EFIAPI +IpIoStop ( + IN OUT IP_IO *IpIo + ) +{ + EFI_STATUS Status; + IP_IO_IP_INFO *IpInfo; + UINT8 IpVersion; + + if (IpIo == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (!IpIo->IsConfigured) { + return EFI_SUCCESS; + } + + IpVersion = IpIo->IpVersion; + + ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6)); + + // + // Remove the IpIo from the active IpIo list. + // + RemoveEntryList (&IpIo->Entry); + + // + // Configure NULL Ip + // + if (IpVersion == IP_VERSION_4) { + Status = IpIo->Ip.Ip4->Configure (IpIo->Ip.Ip4, NULL); + } else { + Status = IpIo->Ip.Ip6->Configure (IpIo->Ip.Ip6, NULL); + } + if (EFI_ERROR (Status)) { + return Status; + } + + IpIo->IsConfigured = FALSE; + + // + // Detroy the Ip List used by IpIo + // + + while (!IsListEmpty (&(IpIo->IpList))) { + IpInfo = NET_LIST_HEAD (&(IpIo->IpList), IP_IO_IP_INFO, Entry); + + IpIoRemoveIp (IpIo, IpInfo); + } + + // + // All pending send tokens should be flushed by resetting the IP instances. + // + ASSERT (IsListEmpty (&IpIo->PendingSndList)); + + // + // Close the receive event. + // + if (IpVersion == IP_VERSION_4){ + gBS->CloseEvent (IpIo->RcvToken.Ip4Token.Event); + } else { + gBS->CloseEvent (IpIo->RcvToken.Ip6Token.Event); + } + + return EFI_SUCCESS; +} + + +/** + Destroy an IP_IO instance. + + This function is paired with IpIoCreate(). The IP_IO will be closed first. + Resource will be freed afterwards. See IpIoCloseProtocolDestroyIpChild(). + + @param[in, out] IpIo Pointer to the IP_IO instance that needs to be + destroyed. + + @retval EFI_SUCCESS The IP_IO instance destroyed successfully. + @retval Others Error condition occurred. + +**/ +EFI_STATUS +EFIAPI +IpIoDestroy ( + IN OUT IP_IO *IpIo + ) +{ + EFI_STATUS Status; + + // + // Stop the IpIo. + // + Status = IpIoStop (IpIo); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Close the IP protocol and destroy the child. + // + Status = IpIoCloseProtocolDestroyIpChild ( + IpIo->Controller, + IpIo->Image, + IpIo->ChildHandle, + IpIo->IpVersion + ); + if (EFI_ERROR (Status)) { + return Status; + } + + gBS->FreePool (IpIo); + + return EFI_SUCCESS; +} + + +/** + Send out an IP packet. + + This function is called after IpIoOpen(). The data to be sent is wrapped in + Pkt. The IP instance wrapped in IpIo is used for sending by default but can be + overriden by Sender. Other sending configs, like source address and gateway + address etc., are specified in OverrideData. + + @param[in, out] IpIo Pointer to an IP_IO instance used for sending IP + packet. + @param[in, out] Pkt Pointer to the IP packet to be sent. + @param[in] Sender The IP protocol instance used for sending. + @param[in] Context Optional context data. + @param[in] NotifyData Optional notify data. + @param[in] Dest The destination IP address to send this packet to. + This parameter is optional when using IPv6. + @param[in] OverrideData The data to override some configuration of the IP + instance used for sending. + + @retval EFI_SUCCESS The operation is completed successfully. + @retval EFI_INVALID_PARAMETER The input parameter is not correct. + @retval EFI_NOT_STARTED The IpIo is not configured. + @retval EFI_OUT_OF_RESOURCES Failed due to resource limit. + @retval Others Error condition occurred. + +**/ +EFI_STATUS +EFIAPI +IpIoSend ( + IN OUT IP_IO *IpIo, + IN OUT NET_BUF *Pkt, + IN IP_IO_IP_INFO *Sender OPTIONAL, + IN VOID *Context OPTIONAL, + IN VOID *NotifyData OPTIONAL, + IN EFI_IP_ADDRESS *Dest OPTIONAL, + IN IP_IO_OVERRIDE *OverrideData OPTIONAL + ) +{ + EFI_STATUS Status; + IP_IO_IP_PROTOCOL Ip; + IP_IO_SEND_ENTRY *SndEntry; + + if ((IpIo == NULL) || (Pkt == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if ((IpIo->IpVersion == IP_VERSION_4) && (Dest == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (!IpIo->IsConfigured) { + return EFI_NOT_STARTED; + } + + Ip = (NULL == Sender) ? IpIo->Ip : Sender->Ip; + + // + // create a new SndEntry + // + SndEntry = IpIoCreateSndEntry (IpIo, Pkt, Ip, Context, NotifyData, Dest, OverrideData); + if (NULL == SndEntry) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Send this Packet + // + if (IpIo->IpVersion == IP_VERSION_4){ + Status = Ip.Ip4->Transmit ( + Ip.Ip4, + &SndEntry->SndToken.Ip4Token + ); + } else { + Status = Ip.Ip6->Transmit ( + Ip.Ip6, + &SndEntry->SndToken.Ip6Token + ); + } + + if (EFI_ERROR (Status)) { + IpIoDestroySndEntry (SndEntry); + } + + return Status; +} + + +/** + Cancel the IP transmit token which wraps this Packet. + + If IpIo is NULL, then ASSERT(). + If Packet is NULL, then ASSERT(). + + @param[in] IpIo Pointer to the IP_IO instance. + @param[in] Packet Pointer to the packet of NET_BUF to cancel. + +**/ +VOID +EFIAPI +IpIoCancelTxToken ( + IN IP_IO *IpIo, + IN VOID *Packet + ) +{ + LIST_ENTRY *Node; + IP_IO_SEND_ENTRY *SndEntry; + IP_IO_IP_PROTOCOL Ip; + + ASSERT ((IpIo != NULL) && (Packet != NULL)); + + NET_LIST_FOR_EACH (Node, &IpIo->PendingSndList) { + + SndEntry = NET_LIST_USER_STRUCT (Node, IP_IO_SEND_ENTRY, Entry); + + if (SndEntry->Pkt == Packet) { + + Ip = SndEntry->Ip; + + if (IpIo->IpVersion == IP_VERSION_4) { + Ip.Ip4->Cancel ( + Ip.Ip4, + &SndEntry->SndToken.Ip4Token + ); + } else { + Ip.Ip6->Cancel ( + Ip.Ip6, + &SndEntry->SndToken.Ip6Token + ); + } + + break; + } + } + +} + + +/** + Add a new IP instance for sending data. + + If IpIo is NULL, then ASSERT(). + If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + The function is used to add the IP_IO to the IP_IO sending list. The caller + can later use IpIoFindSender() to get the IP_IO and call IpIoSend() to send + data. + + @param[in, out] IpIo Pointer to a IP_IO instance to add a new IP + instance for sending purpose. + + @return Pointer to the created IP_IO_IP_INFO structure, NULL if failed. + +**/ +IP_IO_IP_INFO * +EFIAPI +IpIoAddIp ( + IN OUT IP_IO *IpIo + ) +{ + EFI_STATUS Status; + IP_IO_IP_INFO *IpInfo; + EFI_EVENT Event; + + ASSERT (IpIo != NULL); + ASSERT ((IpIo->IpVersion == IP_VERSION_4) || (IpIo->IpVersion == IP_VERSION_6)); + + IpInfo = AllocatePool (sizeof (IP_IO_IP_INFO)); + if (IpInfo == NULL) { + return NULL; + } + + // + // Init this IpInfo, set the Addr and SubnetMask to 0 before we configure the IP + // instance. + // + InitializeListHead (&IpInfo->Entry); + IpInfo->ChildHandle = NULL; + ZeroMem (&IpInfo->Addr, sizeof (IpInfo->Addr)); + ZeroMem (&IpInfo->PreMask, sizeof (IpInfo->PreMask)); + + IpInfo->RefCnt = 1; + IpInfo->IpVersion = IpIo->IpVersion; + + // + // Create the IP instance and open the IP protocol. + // + Status = IpIoCreateIpChildOpenProtocol ( + IpIo->Controller, + IpIo->Image, + &IpInfo->ChildHandle, + IpInfo->IpVersion, + (VOID **) &IpInfo->Ip + ); + if (EFI_ERROR (Status)) { + goto ReleaseIpInfo; + } + + // + // Create the event for the DummyRcvToken. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + IpIoDummyHandler, + IpInfo, + &Event + ); + if (EFI_ERROR (Status)) { + goto ReleaseIpChild; + } + + if (IpInfo->IpVersion == IP_VERSION_4) { + IpInfo->DummyRcvToken.Ip4Token.Event = Event; + } else { + IpInfo->DummyRcvToken.Ip6Token.Event = Event; + } + + // + // Link this IpInfo into the IpIo. + // + InsertTailList (&IpIo->IpList, &IpInfo->Entry); + + return IpInfo; + +ReleaseIpChild: + + IpIoCloseProtocolDestroyIpChild ( + IpIo->Controller, + IpIo->Image, + IpInfo->ChildHandle, + IpInfo->IpVersion + ); + +ReleaseIpInfo: + + gBS->FreePool (IpInfo); + + return NULL; +} + + +/** + Configure the IP instance of this IpInfo and start the receiving if IpConfigData + is not NULL. + + If IpInfo is NULL, then ASSERT(). + If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + @param[in, out] IpInfo Pointer to the IP_IO_IP_INFO instance. + @param[in, out] IpConfigData The IP configure data used to configure the IP + instance, if NULL the IP instance is reset. If + UseDefaultAddress is set to TRUE, and the configure + operation succeeds, the default address information + is written back in this IpConfigData. + + @retval EFI_SUCCESS The IP instance of this IpInfo is configured successfully + or no need to reconfigure it. + @retval Others Configuration fails. + +**/ +EFI_STATUS +EFIAPI +IpIoConfigIp ( + IN OUT IP_IO_IP_INFO *IpInfo, + IN OUT VOID *IpConfigData OPTIONAL + ) +{ + EFI_STATUS Status; + IP_IO_IP_PROTOCOL Ip; + UINT8 IpVersion; + EFI_IP4_MODE_DATA Ip4ModeData; + EFI_IP6_MODE_DATA Ip6ModeData; + + ASSERT (IpInfo != NULL); + + if (IpInfo->RefCnt > 1) { + // + // This IP instance is shared, don't reconfigure it until it has only one + // consumer. Currently, only the tcp children cloned from their passive parent + // will share the same IP. So this cases only happens while IpConfigData is NULL, + // let the last consumer clean the IP instance. + // + return EFI_SUCCESS; + } + + IpVersion = IpInfo->IpVersion; + ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6)); + + Ip = IpInfo->Ip; + + if (IpInfo->IpVersion == IP_VERSION_4) { + Status = Ip.Ip4->Configure (Ip.Ip4, IpConfigData); + } else { + Status = Ip.Ip6->Configure (Ip.Ip6, IpConfigData); + } + + if (EFI_ERROR (Status)) { + return Status; + } + + if (IpConfigData != NULL) { + if (IpInfo->IpVersion == IP_VERSION_4) { + + if (((EFI_IP4_CONFIG_DATA *) IpConfigData)->UseDefaultAddress) { + Status = Ip.Ip4->GetModeData ( + Ip.Ip4, + &Ip4ModeData, + NULL, + NULL + ); + if (EFI_ERROR (Status)) { + Ip.Ip4->Configure (Ip.Ip4, NULL); + return Status; + } + + IP4_COPY_ADDRESS (&((EFI_IP4_CONFIG_DATA*) IpConfigData)->StationAddress, &Ip4ModeData.ConfigData.StationAddress); + IP4_COPY_ADDRESS (&((EFI_IP4_CONFIG_DATA*) IpConfigData)->SubnetMask, &Ip4ModeData.ConfigData.SubnetMask); + } + + CopyMem ( + &IpInfo->Addr.Addr, + &((EFI_IP4_CONFIG_DATA *) IpConfigData)->StationAddress, + sizeof (IP4_ADDR) + ); + CopyMem ( + &IpInfo->PreMask.SubnetMask, + &((EFI_IP4_CONFIG_DATA *) IpConfigData)->SubnetMask, + sizeof (IP4_ADDR) + ); + + Status = Ip.Ip4->Receive ( + Ip.Ip4, + &IpInfo->DummyRcvToken.Ip4Token + ); + if (EFI_ERROR (Status)) { + Ip.Ip4->Configure (Ip.Ip4, NULL); + } + } else { + Status = Ip.Ip6->GetModeData ( + Ip.Ip6, + &Ip6ModeData, + NULL, + NULL + ); + if (EFI_ERROR (Status)) { + Ip.Ip6->Configure (Ip.Ip6, NULL); + return Status; + } + + if (Ip6ModeData.IsConfigured) { + CopyMem ( + &((EFI_IP6_CONFIG_DATA *) IpConfigData)->StationAddress, + &Ip6ModeData.ConfigData.StationAddress, + sizeof (EFI_IPv6_ADDRESS) + ); + + if (Ip6ModeData.AddressList != NULL) { + FreePool (Ip6ModeData.AddressList); + } + + if (Ip6ModeData.GroupTable != NULL) { + FreePool (Ip6ModeData.GroupTable); + } + + if (Ip6ModeData.RouteTable != NULL) { + FreePool (Ip6ModeData.RouteTable); + } + + if (Ip6ModeData.NeighborCache != NULL) { + FreePool (Ip6ModeData.NeighborCache); + } + + if (Ip6ModeData.PrefixTable != NULL) { + FreePool (Ip6ModeData.PrefixTable); + } + + if (Ip6ModeData.IcmpTypeList != NULL) { + FreePool (Ip6ModeData.IcmpTypeList); + } + + } else { + Status = EFI_NO_MAPPING; + return Status; + } + + CopyMem ( + &IpInfo->Addr, + &Ip6ModeData.ConfigData.StationAddress, + sizeof (EFI_IPv6_ADDRESS) + ); + + Status = Ip.Ip6->Receive ( + Ip.Ip6, + &IpInfo->DummyRcvToken.Ip6Token + ); + if (EFI_ERROR (Status)) { + Ip.Ip6->Configure (Ip.Ip6, NULL); + } + } + } else { + // + // The IP instance is reset, set the stored Addr and SubnetMask to zero. + // + ZeroMem (&IpInfo->Addr, sizeof (IpInfo->Addr)); + ZeroMem (&IpInfo->PreMask, sizeof (IpInfo->PreMask)); + } + + return Status; +} + + +/** + Destroy an IP instance maintained in IpIo->IpList for + sending purpose. + + If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT(). + + This function pairs with IpIoAddIp(). The IpInfo is previously created by + IpIoAddIp(). The IP_IO_IP_INFO::RefCnt is decremented and the IP instance + will be dstroyed if the RefCnt is zero. + + @param[in] IpIo Pointer to the IP_IO instance. + @param[in] IpInfo Pointer to the IpInfo to be removed. + +**/ +VOID +EFIAPI +IpIoRemoveIp ( + IN IP_IO *IpIo, + IN IP_IO_IP_INFO *IpInfo + ) +{ + + UINT8 IpVersion; + + if (IpIo == NULL || IpInfo == NULL) { + return; + } + + ASSERT (IpInfo->RefCnt > 0); + + NET_PUT_REF (IpInfo); + + if (IpInfo->RefCnt > 0) { + + return; + } + + IpVersion = IpIo->IpVersion; + + ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6)); + + RemoveEntryList (&IpInfo->Entry); + + if (IpVersion == IP_VERSION_4){ + IpInfo->Ip.Ip4->Configure ( + IpInfo->Ip.Ip4, + NULL + ); + IpIoCloseProtocolDestroyIpChild ( + IpIo->Controller, + IpIo->Image, + IpInfo->ChildHandle, + IP_VERSION_4 + ); + + gBS->CloseEvent (IpInfo->DummyRcvToken.Ip4Token.Event); + + } else { + + IpInfo->Ip.Ip6->Configure ( + IpInfo->Ip.Ip6, + NULL + ); + + IpIoCloseProtocolDestroyIpChild ( + IpIo->Controller, + IpIo->Image, + IpInfo->ChildHandle, + IP_VERSION_6 + ); + + gBS->CloseEvent (IpInfo->DummyRcvToken.Ip6Token.Event); + } + + FreePool (IpInfo); +} + + +/** + Find the first IP protocol maintained in IpIo whose local + address is the same as Src. + + This function is called when the caller needs the IpIo to send data to the + specified Src. The IpIo was added previously by IpIoAddIp(). + + @param[in, out] IpIo Pointer to the pointer of the IP_IO instance. + @param[in] IpVersion The version of the IP protocol to use, either + IPv4 or IPv6. + @param[in] Src The local IP address. + + @return Pointer to the IP protocol can be used for sending purpose and its local + address is the same with Src. NULL if failed. + +**/ +IP_IO_IP_INFO * +EFIAPI +IpIoFindSender ( + IN OUT IP_IO **IpIo, + IN UINT8 IpVersion, + IN EFI_IP_ADDRESS *Src + ) +{ + LIST_ENTRY *IpIoEntry; + IP_IO *IpIoPtr; + LIST_ENTRY *IpInfoEntry; + IP_IO_IP_INFO *IpInfo; + + if (IpIo == NULL || Src == NULL) { + return NULL; + } + + if ((IpVersion != IP_VERSION_4) && (IpVersion != IP_VERSION_6)) { + return NULL; + } + + NET_LIST_FOR_EACH (IpIoEntry, &mActiveIpIoList) { + IpIoPtr = NET_LIST_USER_STRUCT (IpIoEntry, IP_IO, Entry); + + if (((*IpIo != NULL) && (*IpIo != IpIoPtr)) || (IpIoPtr->IpVersion != IpVersion)) { + continue; + } + + NET_LIST_FOR_EACH (IpInfoEntry, &IpIoPtr->IpList) { + IpInfo = NET_LIST_USER_STRUCT (IpInfoEntry, IP_IO_IP_INFO, Entry); + if (IpInfo->IpVersion == IP_VERSION_4){ + + if (EFI_IP4_EQUAL (&IpInfo->Addr.v4, &Src->v4)) { + *IpIo = IpIoPtr; + return IpInfo; + } + + } else { + + if (EFI_IP6_EQUAL (&IpInfo->Addr.v6, &Src->v6)) { + *IpIo = IpIoPtr; + return IpInfo; + } + } + } + } + + // + // No match. + // + return NULL; +} + + +/** + Get the ICMP error map information. + + The ErrorStatus will be returned. The IsHard and Notify are optional. If they + are not NULL, this routine will fill them. + + @param[in] IcmpError IcmpError Type. + @param[in] IpVersion The version of the IP protocol to use, + either IPv4 or IPv6. + @param[out] IsHard If TRUE, indicates that it is a hard error. + @param[out] Notify If TRUE, SockError needs to be notified. + + @retval EFI_UNSUPPORTED Unrecognizable ICMP error code. + @return ICMP Error Status, such as EFI_NETWORK_UNREACHABLE. + +**/ +EFI_STATUS +EFIAPI +IpIoGetIcmpErrStatus ( + IN UINT8 IcmpError, + IN UINT8 IpVersion, + OUT BOOLEAN *IsHard OPTIONAL, + OUT BOOLEAN *Notify OPTIONAL + ) +{ + if (IpVersion == IP_VERSION_4 ) { + ASSERT (IcmpError <= ICMP_ERR_PARAMPROB); + + if (IsHard != NULL) { + *IsHard = mIcmpErrMap[IcmpError].IsHard; + } + + if (Notify != NULL) { + *Notify = mIcmpErrMap[IcmpError].Notify; + } + + switch (IcmpError) { + case ICMP_ERR_UNREACH_NET: + return EFI_NETWORK_UNREACHABLE; + + case ICMP_ERR_TIMXCEED_INTRANS: + case ICMP_ERR_TIMXCEED_REASS: + case ICMP_ERR_UNREACH_HOST: + return EFI_HOST_UNREACHABLE; + + case ICMP_ERR_UNREACH_PROTOCOL: + return EFI_PROTOCOL_UNREACHABLE; + + case ICMP_ERR_UNREACH_PORT: + return EFI_PORT_UNREACHABLE; + + case ICMP_ERR_MSGSIZE: + case ICMP_ERR_UNREACH_SRCFAIL: + case ICMP_ERR_QUENCH: + case ICMP_ERR_PARAMPROB: + return EFI_ICMP_ERROR; + + default: + ASSERT (FALSE); + return EFI_UNSUPPORTED; + } + + } else if (IpVersion == IP_VERSION_6) { + + ASSERT (IcmpError <= ICMP6_ERR_PARAMPROB_IPV6OPTION); + + if (IsHard != NULL) { + *IsHard = mIcmp6ErrMap[IcmpError].IsHard; + } + + if (Notify != NULL) { + *Notify = mIcmp6ErrMap[IcmpError].Notify; + } + + switch (IcmpError) { + case ICMP6_ERR_UNREACH_NET: + return EFI_NETWORK_UNREACHABLE; + + case ICMP6_ERR_UNREACH_HOST: + case ICMP6_ERR_TIMXCEED_HOPLIMIT: + case ICMP6_ERR_TIMXCEED_REASS: + return EFI_HOST_UNREACHABLE; + + case ICMP6_ERR_UNREACH_PROTOCOL: + return EFI_PROTOCOL_UNREACHABLE; + + case ICMP6_ERR_UNREACH_PORT: + return EFI_PORT_UNREACHABLE; + + case ICMP6_ERR_PACKAGE_TOOBIG: + case ICMP6_ERR_PARAMPROB_HEADER: + case ICMP6_ERR_PARAMPROB_NEXHEADER: + case ICMP6_ERR_PARAMPROB_IPV6OPTION: + return EFI_ICMP_ERROR; + + default: + ASSERT (FALSE); + return EFI_UNSUPPORTED; + } + + } else { + // + // Should never be here + // + ASSERT (FALSE); + return EFI_UNSUPPORTED; + } +} + + +/** + Refresh the remote peer's Neighbor Cache entries. + + This function is called when the caller needs the IpIo to refresh the existing + IPv6 neighbor cache entries since the neighbor is considered reachable by the + node has recently received a confirmation that packets sent recently to the + neighbor were received by its IP layer. + + @param[in] IpIo Pointer to an IP_IO instance + @param[in] Neighbor The IP address of the neighbor + @param[in] Timeout Time in 100-ns units that this entry will + remain in the neighbor cache. A value of + zero means that the entry is permanent. + A value of non-zero means that the entry is + dynamic and will be deleted after Timeout. + + @retval EFI_SUCCESS The operation is completed successfully. + @retval EFI_NOT_STARTED The IpIo is not configured. + @retval EFI_INVALID_PARAMETER Neighbor Address is invalid. + @retval EFI_NOT_FOUND The neighbor cache entry is not in the + neighbor table. + @retval EFI_UNSUPPORTED IP version is IPv4, which doesn't support neighbor cache refresh. + @retval EFI_OUT_OF_RESOURCES Failed due to resource limit. + +**/ +EFI_STATUS +EFIAPI +IpIoRefreshNeighbor ( + IN IP_IO *IpIo, + IN EFI_IP_ADDRESS *Neighbor, + IN UINT32 Timeout + ) +{ + EFI_IP6_PROTOCOL *Ip; + + if (!IpIo->IsConfigured) { + return EFI_NOT_STARTED; + } + + if (IpIo->IpVersion != IP_VERSION_6) { + return EFI_UNSUPPORTED; + } + + Ip = IpIo->Ip.Ip6; + + return Ip->Neighbors (Ip, FALSE, &Neighbor->v6, NULL, Timeout, TRUE); +} + diff --git a/NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.inf b/NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.inf new file mode 100644 index 000000000..bfd631a31 --- /dev/null +++ b/NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.inf @@ -0,0 +1,47 @@ +## @file +# This library instance provides IP services upon EFI IPv4/IPv6 Protocols. +# +# Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = DxeIpIoLib + MODULE_UNI_FILE = DxeIpIoLib.uni + FILE_GUID = A302F877-8625-425c-B1EC-7487B62C4FDA + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = IpIoLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources] + DxeIpIoLib.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + BaseLib + DebugLib + UefiBootServicesTableLib + MemoryAllocationLib + BaseMemoryLib + DpcLib + +[Protocols] + gEfiIp4ProtocolGuid ## SOMETIMES_CONSUMES + gEfiIp4ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiIp6ProtocolGuid ## SOMETIMES_CONSUMES + gEfiIp6ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + diff --git a/NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.uni b/NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.uni new file mode 100644 index 000000000..4af043cfe --- /dev/null +++ b/NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.uni @@ -0,0 +1,16 @@ +// /** @file +// This library instance provides IP services upon EFI IPv4/IPv6 Protocols. +// +// This library instance provides IP services upon EFI IPv4/IPv6 Protocols. +// +// Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "EFI IPv4/IPv6 IP Services Library" + +#string STR_MODULE_DESCRIPTION #language en-US "This library instance provides IP services upon EFI IPv4/IPv6 Protocols." + diff --git a/NetworkPkg/Library/DxeNetLib/DxeNetLib.c b/NetworkPkg/Library/DxeNetLib/DxeNetLib.c new file mode 100644 index 000000000..8e2f72066 --- /dev/null +++ b/NetworkPkg/Library/DxeNetLib/DxeNetLib.c @@ -0,0 +1,3394 @@ +/** @file + Network library. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2015 Hewlett Packard Enterprise Development LP
+SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#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. +// +GLOBAL_REMOVE_IF_UNREFERENCED IP4_ADDR gIp4AllMasks[IP4_MASK_NUM] = { + 0x00000000, + 0x80000000, + 0xC0000000, + 0xE0000000, + 0xF0000000, + 0xF8000000, + 0xFC000000, + 0xFE000000, + + 0xFF000000, + 0xFF800000, + 0xFFC00000, + 0xFFE00000, + 0xFFF00000, + 0xFFF80000, + 0xFFFC0000, + 0xFFFE0000, + + 0xFFFF0000, + 0xFFFF8000, + 0xFFFFC000, + 0xFFFFE000, + 0xFFFFF000, + 0xFFFFF800, + 0xFFFFFC00, + 0xFFFFFE00, + + 0xFFFFFF00, + 0xFFFFFF80, + 0xFFFFFFC0, + 0xFFFFFFE0, + 0xFFFFFFF0, + 0xFFFFFFF8, + 0xFFFFFFFC, + 0xFFFFFFFE, + 0xFFFFFFFF, +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_IPv4_ADDRESS mZeroIp4Addr = {{0, 0, 0, 0}}; + +// +// Any error level digitally larger than mNetDebugLevelMax +// will be silently discarded. +// +GLOBAL_REMOVE_IF_UNREFERENCED UINTN mNetDebugLevelMax = NETDEBUG_LEVEL_ERROR; +GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogPacketSeq = 0xDEADBEEF; + +// +// You can change mSyslogDstMac mSyslogDstIp and mSyslogSrcIp +// here to direct the syslog packets to the syslog deamon. The +// default is broadcast to both the ethernet and IP. +// +GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mSyslogDstMac[NET_ETHER_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; +GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogDstIp = 0xffffffff; +GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogSrcIp = 0; + +GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 *mMonthName[] = { + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec" +}; + +// +// VLAN device path node template +// +GLOBAL_REMOVE_IF_UNREFERENCED VLAN_DEVICE_PATH mNetVlanDevicePathTemplate = { + { + MESSAGING_DEVICE_PATH, + MSG_VLAN_DP, + { + (UINT8) (sizeof (VLAN_DEVICE_PATH)), + (UINT8) ((sizeof (VLAN_DEVICE_PATH)) >> 8) + } + }, + 0 +}; + +/** + Locate the handles that support SNP, then open one of them + to send the syslog packets. The caller isn't required to close + the SNP after use because the SNP is opened by HandleProtocol. + + @return The point to SNP if one is properly openned. Otherwise NULL + +**/ +EFI_SIMPLE_NETWORK_PROTOCOL * +SyslogLocateSnp ( + VOID + ) +{ + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_STATUS Status; + EFI_HANDLE *Handles; + UINTN HandleCount; + UINTN Index; + + // + // Locate the handles which has SNP installed. + // + Handles = NULL; + Status = gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiSimpleNetworkProtocolGuid, + NULL, + &HandleCount, + &Handles + ); + + if (EFI_ERROR (Status) || (HandleCount == 0)) { + return NULL; + } + + // + // Try to open one of the ethernet SNP protocol to send packet + // + Snp = NULL; + + for (Index = 0; Index < HandleCount; Index++) { + Status = gBS->HandleProtocol ( + Handles[Index], + &gEfiSimpleNetworkProtocolGuid, + (VOID **) &Snp + ); + + if ((Status == EFI_SUCCESS) && (Snp != NULL) && + (Snp->Mode->IfType == NET_IFTYPE_ETHERNET) && + (Snp->Mode->MaxPacketSize >= NET_SYSLOG_PACKET_LEN)) { + + break; + } + + Snp = NULL; + } + + FreePool (Handles); + return Snp; +} + +/** + Transmit a syslog packet synchronously through SNP. The Packet + already has the ethernet header prepended. This function should + fill in the source MAC because it will try to locate a SNP each + time it is called to avoid the problem if SNP is unloaded. + This code snip is copied from MNP. + If Packet is NULL, then ASSERT(). + + @param[in] Packet The Syslog packet + @param[in] Length The length of the packet + + @retval EFI_DEVICE_ERROR Failed to locate a usable SNP protocol + @retval EFI_TIMEOUT Timeout happened to send the packet. + @retval EFI_SUCCESS Packet is sent. + +**/ +EFI_STATUS +SyslogSendPacket ( + IN CHAR8 *Packet, + IN UINT32 Length + ) +{ + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + ETHER_HEAD *Ether; + EFI_STATUS Status; + EFI_EVENT TimeoutEvent; + UINT8 *TxBuf; + + ASSERT (Packet != NULL); + + Snp = SyslogLocateSnp (); + + if (Snp == NULL) { + return EFI_DEVICE_ERROR; + } + + Ether = (ETHER_HEAD *) Packet; + CopyMem (Ether->SrcMac, Snp->Mode->CurrentAddress.Addr, NET_ETHER_ADDR_LEN); + + // + // Start the timeout event. + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_NOTIFY, + NULL, + NULL, + &TimeoutEvent + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->SetTimer (TimeoutEvent, TimerRelative, NET_SYSLOG_TX_TIMEOUT); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + for (;;) { + // + // Transmit the packet through SNP. + // + Status = Snp->Transmit (Snp, 0, Length, Packet, NULL, NULL, NULL); + + if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_READY)) { + Status = EFI_DEVICE_ERROR; + break; + } + + // + // If Status is EFI_SUCCESS, the packet is put in the transmit queue. + // if Status is EFI_NOT_READY, the transmit engine of the network + // interface is busy. Both need to sync SNP. + // + TxBuf = NULL; + + do { + // + // Get the recycled transmit buffer status. + // + Snp->GetStatus (Snp, NULL, (VOID **) &TxBuf); + + if (!EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) { + Status = EFI_TIMEOUT; + break; + } + + } while (TxBuf == NULL); + + if ((Status == EFI_SUCCESS) || (Status == EFI_TIMEOUT)) { + break; + } + + // + // Status is EFI_NOT_READY. Restart the timer event and + // call Snp->Transmit again. + // + gBS->SetTimer (TimeoutEvent, TimerRelative, NET_SYSLOG_TX_TIMEOUT); + } + + gBS->SetTimer (TimeoutEvent, TimerCancel, 0); + +ON_EXIT: + gBS->CloseEvent (TimeoutEvent); + return Status; +} + +/** + Build a syslog packet, including the Ethernet/Ip/Udp headers + and user's message. + + @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 + @param[in] Message The log message + @param[in] BufLen The lenght of the Buf + @param[out] Buf The buffer to put the packet data + + @return The length of the syslog packet built, 0 represents no packet is built. + +**/ +UINT32 +SyslogBuildPacket ( + IN UINT32 Level, + IN UINT8 *Module, + IN UINT8 *File, + IN UINT32 Line, + IN UINT8 *Message, + IN UINT32 BufLen, + OUT CHAR8 *Buf + ) +{ + EFI_STATUS Status; + ETHER_HEAD *Ether; + IP4_HEAD *Ip4; + EFI_UDP_HEADER *Udp4; + EFI_TIME Time; + UINT32 Pri; + UINT32 Len; + + // + // Fill in the Ethernet header. Leave alone the source MAC. + // SyslogSendPacket will fill in the address for us. + // + Ether = (ETHER_HEAD *) Buf; + CopyMem (Ether->DstMac, mSyslogDstMac, NET_ETHER_ADDR_LEN); + ZeroMem (Ether->SrcMac, NET_ETHER_ADDR_LEN); + + Ether->EtherType = HTONS (0x0800); // IPv4 protocol + + Buf += sizeof (ETHER_HEAD); + BufLen -= sizeof (ETHER_HEAD); + + // + // Fill in the IP header + // + Ip4 = (IP4_HEAD *) Buf; + Ip4->HeadLen = 5; + Ip4->Ver = 4; + Ip4->Tos = 0; + Ip4->TotalLen = 0; + Ip4->Id = (UINT16) mSyslogPacketSeq; + Ip4->Fragment = 0; + Ip4->Ttl = 16; + Ip4->Protocol = 0x11; + Ip4->Checksum = 0; + Ip4->Src = mSyslogSrcIp; + Ip4->Dst = mSyslogDstIp; + + Buf += sizeof (IP4_HEAD); + BufLen -= sizeof (IP4_HEAD); + + // + // Fill in the UDP header, Udp checksum is optional. Leave it zero. + // + Udp4 = (EFI_UDP_HEADER *) Buf; + Udp4->SrcPort = HTONS (514); + Udp4->DstPort = HTONS (514); + Udp4->Length = 0; + Udp4->Checksum = 0; + + Buf += sizeof (EFI_UDP_HEADER); + BufLen -= sizeof (EFI_UDP_HEADER); + + // + // Build the syslog message body with Timestamp machine module Message + // + Pri = ((NET_SYSLOG_FACILITY & 31) << 3) | (Level & 7); + Status = gRT->GetTime (&Time, NULL); + if (EFI_ERROR (Status)) { + return 0; + } + + // + // Use %a to format the ASCII strings, %s to format UNICODE strings + // + Len = 0; + Len += (UINT32) AsciiSPrint ( + Buf, + BufLen, + "<%d> %a %d %d:%d:%d ", + Pri, + mMonthName [Time.Month-1], + Time.Day, + Time.Hour, + Time.Minute, + Time.Second + ); + + Len += (UINT32) AsciiSPrint ( + Buf + Len, + BufLen - Len, + "Tiano %a: %a (Line: %d File: %a)", + Module, + Message, + Line, + File + ); + Len ++; + + // + // OK, patch the IP length/checksum and UDP length fields. + // + Len += sizeof (EFI_UDP_HEADER); + Udp4->Length = HTONS ((UINT16) Len); + + Len += sizeof (IP4_HEAD); + Ip4->TotalLen = HTONS ((UINT16) Len); + Ip4->Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Ip4, sizeof (IP4_HEAD))); + + return Len + sizeof (ETHER_HEAD); +} + +/** + Allocate a buffer, then format the message to it. This is a + help function for the NET_DEBUG_XXX macros. The PrintArg of + these macros treats the variable length print parameters as a + single parameter, and pass it to the NetDebugASPrint. For + example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name)) + if extracted to: + + NetDebugOutput ( + NETDEBUG_LEVEL_TRACE, + "Tcp", + __FILE__, + __LINE__, + NetDebugASPrint ("State transit to %a\n", Name) + ) + + If Format is NULL, then ASSERT(). + + @param Format The ASCII format string. + @param ... The variable length parameter whose format is determined + by the Format string. + + @return The buffer containing the formatted message, + or NULL if failed to allocate memory. + +**/ +CHAR8 * +EFIAPI +NetDebugASPrint ( + IN CHAR8 *Format, + ... + ) +{ + VA_LIST Marker; + CHAR8 *Buf; + + ASSERT (Format != NULL); + + Buf = (CHAR8 *) AllocatePool (NET_DEBUG_MSG_LEN); + + if (Buf == NULL) { + return NULL; + } + + VA_START (Marker, Format); + AsciiVSPrint (Buf, NET_DEBUG_MSG_LEN, Format, Marker); + VA_END (Marker); + + return Buf; +} + +/** + Builds an UDP4 syslog packet and send it using SNP. + + 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 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. + @param Message The user message to log. + + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet. + @retval EFI_DEVICE_ERROR Device error occurs. + @retval EFI_SUCCESS The log is discard because that it is more verbose + than the mNetDebugLevelMax. Or, it has been sent out. +**/ +EFI_STATUS +EFIAPI +NetDebugOutput ( + IN UINT32 Level, + IN UINT8 *Module, + IN UINT8 *File, + IN UINT32 Line, + IN UINT8 *Message + ) +{ + CHAR8 *Packet; + UINT32 Len; + EFI_STATUS Status; + + // + // Check whether the message should be sent out + // + if (Message == NULL || File == NULL || Module == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (Level > mNetDebugLevelMax) { + Status = EFI_SUCCESS; + goto ON_EXIT; + } + + // + // Allocate a maxium of 1024 bytes, the caller should ensure + // that the message plus the ethernet/ip/udp header is shorter + // than this + // + Packet = (CHAR8 *) AllocatePool (NET_SYSLOG_PACKET_LEN); + + if (Packet == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Build the message: Ethernet header + IP header + Udp Header + user data + // + Len = SyslogBuildPacket ( + Level, + Module, + File, + Line, + Message, + NET_SYSLOG_PACKET_LEN, + Packet + ); + if (Len == 0) { + Status = EFI_DEVICE_ERROR; + } else { + mSyslogPacketSeq++; + Status = SyslogSendPacket (Packet, Len); + } + + FreePool (Packet); + +ON_EXIT: + FreePool (Message); + return Status; +} +/** + Return the length of the mask. + + Return the length of the mask, the correct value is from 0 to 32. + If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM. + NetMask is in the host byte order. + + @param[in] NetMask The netmask to get the length from. + + @return The length of the netmask, IP4_MASK_NUM if the mask is invalid. + +**/ +INTN +EFIAPI +NetGetMaskLength ( + IN IP4_ADDR NetMask + ) +{ + INTN Index; + + for (Index = 0; Index <= IP4_MASK_MAX; Index++) { + if (NetMask == gIp4AllMasks[Index]) { + break; + } + } + + return Index; +} + + + +/** + 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. + If the address belong to class B, return IP4_ADDR_CLASSB. + The address of class C starts with 110. + If the address belong to class C, return IP4_ADDR_CLASSC. + The address of class D starts with 1110. + If the address belong to class D, return IP4_ADDR_CLASSD. + The address of class E starts with 1111. + If the address belong to class E, return IP4_ADDR_CLASSE. + + + @param[in] Addr The address to get the class from. + + @return IP address class, such as IP4_ADDR_CLASSA. + +**/ +INTN +EFIAPI +NetGetIpClass ( + IN IP4_ADDR Addr + ) +{ + UINT8 ByteOne; + + ByteOne = (UINT8) (Addr >> 24); + + if ((ByteOne & 0x80) == 0) { + return IP4_ADDR_CLASSA; + + } else if ((ByteOne & 0xC0) == 0x80) { + return IP4_ADDR_CLASSB; + + } else if ((ByteOne & 0xE0) == 0xC0) { + return IP4_ADDR_CLASSC; + + } else if ((ByteOne & 0xF0) == 0xE0) { + return IP4_ADDR_CLASSD; + + } else { + return IP4_ADDR_CLASSE; + + } +} + + +/** + Check whether the IP is a valid unicast address according to + the netmask. + + 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), or a 32bit NetMask (all 0xFF) is used for special network environment (e.g. + PPP link). + + @param[in] Ip The IP to check against. + @param[in] NetMask The mask of the IP. + + @return TRUE if IP is a valid unicast address on the network, otherwise FALSE. + +**/ +BOOLEAN +EFIAPI +NetIp4IsUnicast ( + IN IP4_ADDR Ip, + IN IP4_ADDR NetMask + ) +{ + INTN MaskLength; + + ASSERT (NetMask != 0); + + if (Ip == 0 || IP4_IS_LOCAL_BROADCAST (Ip)) { + return FALSE; + } + + MaskLength = NetGetMaskLength (NetMask); + ASSERT ((MaskLength >= 0) && (MaskLength <= IP4_MASK_NUM)); + if (MaskLength < 31) { + if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) { + return FALSE; + } + } + + return TRUE; +} + +/** + Check whether the incoming IPv6 address is a valid unicast address. + + ASSERT if Ip6 is NULL. + + If the address is a multicast address has binary 0xFF at the start, it is not + a valid unicast address. If the address is unspecified ::, it is not a valid + unicast address to be assigned to any node. If the address is loopback address + ::1, it is also not a valid unicast address to be assigned to any physical + interface. + + @param[in] Ip6 The IPv6 address to check against. + + @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE. + +**/ +BOOLEAN +EFIAPI +NetIp6IsValidUnicast ( + IN EFI_IPv6_ADDRESS *Ip6 + ) +{ + UINT8 Byte; + UINT8 Index; + + ASSERT (Ip6 != NULL); + + if (Ip6->Addr[0] == 0xFF) { + return FALSE; + } + + for (Index = 0; Index < 15; Index++) { + if (Ip6->Addr[Index] != 0) { + return TRUE; + } + } + + Byte = Ip6->Addr[Index]; + + if (Byte == 0x0 || Byte == 0x1) { + return FALSE; + } + + return TRUE; +} + +/** + Check whether the incoming Ipv6 address is the unspecified address or not. + + ASSERT if Ip6 is NULL. + + @param[in] Ip6 - Ip6 address, in network order. + + @retval TRUE - Yes, unspecified + @retval FALSE - No + +**/ +BOOLEAN +EFIAPI +NetIp6IsUnspecifiedAddr ( + IN EFI_IPv6_ADDRESS *Ip6 + ) +{ + UINT8 Index; + + ASSERT (Ip6 != NULL); + + for (Index = 0; Index < 16; Index++) { + if (Ip6->Addr[Index] != 0) { + return FALSE; + } + } + + return TRUE; +} + +/** + Check whether the incoming Ipv6 address is a link-local address. + + ASSERT if Ip6 is NULL. + + @param[in] Ip6 - Ip6 address, in network order. + + @retval TRUE - Yes, link-local address + @retval FALSE - No + +**/ +BOOLEAN +EFIAPI +NetIp6IsLinkLocalAddr ( + IN EFI_IPv6_ADDRESS *Ip6 + ) +{ + UINT8 Index; + + ASSERT (Ip6 != NULL); + + if (Ip6->Addr[0] != 0xFE) { + return FALSE; + } + + if (Ip6->Addr[1] != 0x80) { + return FALSE; + } + + for (Index = 2; Index < 8; Index++) { + if (Ip6->Addr[Index] != 0) { + return FALSE; + } + } + + return TRUE; +} + +/** + Check whether the Ipv6 address1 and address2 are on the connected network. + + ASSERT if Ip1 or Ip2 is NULL. + ASSERT if PrefixLength exceeds or equals to IP6_PREFIX_MAX. + + @param[in] Ip1 - Ip6 address1, in network order. + @param[in] Ip2 - Ip6 address2, in network order. + @param[in] PrefixLength - The prefix length of the checking net. + + @retval TRUE - Yes, connected. + @retval FALSE - No. + +**/ +BOOLEAN +EFIAPI +NetIp6IsNetEqual ( + EFI_IPv6_ADDRESS *Ip1, + EFI_IPv6_ADDRESS *Ip2, + UINT8 PrefixLength + ) +{ + UINT8 Byte; + UINT8 Bit; + UINT8 Mask; + + ASSERT ((Ip1 != NULL) && (Ip2 != NULL) && (PrefixLength < IP6_PREFIX_MAX)); + + if (PrefixLength == 0) { + return TRUE; + } + + Byte = (UINT8) (PrefixLength / 8); + Bit = (UINT8) (PrefixLength % 8); + + if (CompareMem (Ip1, Ip2, Byte) != 0) { + return FALSE; + } + + if (Bit > 0) { + Mask = (UINT8) (0xFF << (8 - Bit)); + + ASSERT (Byte < 16); + if (Byte >= 16) { + return FALSE; + } + if ((Ip1->Addr[Byte] & Mask) != (Ip2->Addr[Byte] & Mask)) { + return FALSE; + } + } + + return TRUE; +} + + +/** + Switches the endianess of an IPv6 address + + ASSERT if Ip6 is NULL. + + This function swaps the bytes in a 128-bit IPv6 address to switch the value + from little endian to big endian or vice versa. The byte swapped value is + returned. + + @param Ip6 Points to an IPv6 address + + @return The byte swapped IPv6 address. + +**/ +EFI_IPv6_ADDRESS * +EFIAPI +Ip6Swap128 ( + EFI_IPv6_ADDRESS *Ip6 + ) +{ + UINT64 High; + UINT64 Low; + + ASSERT (Ip6 != NULL); + + CopyMem (&High, Ip6, sizeof (UINT64)); + CopyMem (&Low, &Ip6->Addr[8], sizeof (UINT64)); + + High = SwapBytes64 (High); + Low = SwapBytes64 (Low); + + CopyMem (Ip6, &Low, sizeof (UINT64)); + CopyMem (&Ip6->Addr[8], &High, sizeof (UINT64)); + + return Ip6; +} + +/** + Initialize a random seed using current time and monotonic count. + + 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. + +**/ +UINT32 +EFIAPI +NetRandomInitSeed ( + VOID + ) +{ + 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.Nanosecond; + Seed ^= Time.Year << 7; + + gBS->GetNextMonotonicCount (&MonotonicCount); + Seed += (UINT32) MonotonicCount; + + return Seed; +} + + +/** + Extract a UINT32 from a byte stream. + + ASSERT if Buf is NULL. + + Copy a UINT32 from a byte stream, then converts it from Network + byte order to host byte order. Use this function to avoid alignment error. + + @param[in] Buf The buffer to extract the UINT32. + + @return The UINT32 extracted. + +**/ +UINT32 +EFIAPI +NetGetUint32 ( + IN UINT8 *Buf + ) +{ + UINT32 Value; + + ASSERT (Buf != NULL); + + CopyMem (&Value, Buf, sizeof (UINT32)); + return NTOHL (Value); +} + + +/** + Put a UINT32 to the byte stream in network byte order. + + ASSERT if Buf is NULL. + + Converts a UINT32 from host byte order to network byte order. Then copy it to the + byte stream. + + @param[in, out] Buf The buffer to put the UINT32. + @param[in] Data The data to be converted and put into the byte stream. + +**/ +VOID +EFIAPI +NetPutUint32 ( + IN OUT UINT8 *Buf, + IN UINT32 Data + ) +{ + ASSERT (Buf != NULL); + + Data = HTONL (Data); + CopyMem (Buf, &Data, sizeof (UINT32)); +} + + +/** + Remove the first node entry on the list, and return the removed node entry. + + Removes the first node Entry from a doubly linked list. It is up to the caller of + this function to release the memory used by the first node if that is required. On + exit, the removed node is returned. + + If Head is NULL, then ASSERT(). + If Head was not initialized, then ASSERT(). + If PcdMaximumLinkedListLength is not zero, and the number of nodes in the + linked list including the head node is greater than or equal to PcdMaximumLinkedListLength, + then ASSERT(). + + @param[in, out] Head The list header. + + @return The first node entry that is removed from the list, NULL if the list is empty. + +**/ +LIST_ENTRY * +EFIAPI +NetListRemoveHead ( + IN OUT LIST_ENTRY *Head + ) +{ + LIST_ENTRY *First; + + ASSERT (Head != NULL); + + if (IsListEmpty (Head)) { + return NULL; + } + + First = Head->ForwardLink; + Head->ForwardLink = First->ForwardLink; + First->ForwardLink->BackLink = Head; + + DEBUG_CODE ( + First->ForwardLink = (LIST_ENTRY *) NULL; + First->BackLink = (LIST_ENTRY *) NULL; + ); + + return First; +} + + +/** + Remove the last node entry on the list and and return the removed node entry. + + Removes the last node entry from a doubly linked list. It is up to the caller of + this function to release the memory used by the first node if that is required. On + exit, the removed node is returned. + + If Head is NULL, then ASSERT(). + If Head was not initialized, then ASSERT(). + If PcdMaximumLinkedListLength is not zero, and the number of nodes in the + linked list including the head node is greater than or equal to PcdMaximumLinkedListLength, + then ASSERT(). + + @param[in, out] Head The list head. + + @return The last node entry that is removed from the list, NULL if the list is empty. + +**/ +LIST_ENTRY * +EFIAPI +NetListRemoveTail ( + IN OUT LIST_ENTRY *Head + ) +{ + LIST_ENTRY *Last; + + ASSERT (Head != NULL); + + if (IsListEmpty (Head)) { + return NULL; + } + + Last = Head->BackLink; + Head->BackLink = Last->BackLink; + Last->BackLink->ForwardLink = Head; + + DEBUG_CODE ( + Last->ForwardLink = (LIST_ENTRY *) NULL; + Last->BackLink = (LIST_ENTRY *) NULL; + ); + + return Last; +} + + +/** + Insert a new node entry after a designated node entry of a doubly linked list. + + ASSERT if PrevEntry or NewEntry is NULL. + + Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry + of the doubly linked list. + + @param[in, out] PrevEntry The previous entry to insert after. + @param[in, out] NewEntry The new entry to insert. + +**/ +VOID +EFIAPI +NetListInsertAfter ( + IN OUT LIST_ENTRY *PrevEntry, + IN OUT LIST_ENTRY *NewEntry + ) +{ + ASSERT (PrevEntry != NULL && NewEntry != NULL); + + NewEntry->BackLink = PrevEntry; + NewEntry->ForwardLink = PrevEntry->ForwardLink; + PrevEntry->ForwardLink->BackLink = NewEntry; + PrevEntry->ForwardLink = NewEntry; +} + + +/** + Insert a new node entry before a designated node entry of a doubly linked list. + + ASSERT if PostEntry or NewEntry is NULL. + + Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry + of the doubly linked list. + + @param[in, out] PostEntry The entry to insert before. + @param[in, out] NewEntry The new entry to insert. + +**/ +VOID +EFIAPI +NetListInsertBefore ( + IN OUT LIST_ENTRY *PostEntry, + IN OUT LIST_ENTRY *NewEntry + ) +{ + ASSERT (PostEntry != NULL && NewEntry != NULL); + + NewEntry->ForwardLink = PostEntry; + NewEntry->BackLink = PostEntry->BackLink; + PostEntry->BackLink->ForwardLink = NewEntry; + 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. + + Initialize the forward and backward links of two head nodes donated by Map->Used + and Map->Recycled of two doubly linked lists. + Initializes the count of the pairs in the netmap to zero. + + If Map is NULL, then ASSERT(). + If the address of Map->Used is NULL, then ASSERT(). + If the address of Map->Recycled is NULl, then ASSERT(). + + @param[in, out] Map The netmap to initialize. + +**/ +VOID +EFIAPI +NetMapInit ( + IN OUT NET_MAP *Map + ) +{ + ASSERT (Map != NULL); + + InitializeListHead (&Map->Used); + InitializeListHead (&Map->Recycled); + Map->Count = 0; +} + + +/** + To clean up the netmap, that is, release allocated memories. + + Removes all nodes of the Used doubly linked list and free memory of all related netmap items. + Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items. + The number of the pairs in the netmap is set to be zero. + + If Map is NULL, then ASSERT(). + + @param[in, out] Map The netmap to clean up. + +**/ +VOID +EFIAPI +NetMapClean ( + IN OUT NET_MAP *Map + ) +{ + NET_MAP_ITEM *Item; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + + ASSERT (Map != NULL); + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Used) { + Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link); + + RemoveEntryList (&Item->Link); + Map->Count--; + + gBS->FreePool (Item); + } + + ASSERT ((Map->Count == 0) && IsListEmpty (&Map->Used)); + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Recycled) { + Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link); + + RemoveEntryList (&Item->Link); + gBS->FreePool (Item); + } + + ASSERT (IsListEmpty (&Map->Recycled)); +} + + +/** + Test whether the netmap is empty and return true if it is. + + If the number of the pairs in the netmap is zero, return TRUE. + + If Map is NULL, then ASSERT(). + + @param[in] Map The net map to test. + + @return TRUE if the netmap is empty, otherwise FALSE. + +**/ +BOOLEAN +EFIAPI +NetMapIsEmpty ( + IN NET_MAP *Map + ) +{ + ASSERT (Map != NULL); + return (BOOLEAN) (Map->Count == 0); +} + + +/** + Return the number of the pairs in the netmap. + + If Map is NULL, then ASSERT(). + + @param[in] Map The netmap to get the entry number. + + @return The entry number in the netmap. + +**/ +UINTN +EFIAPI +NetMapGetCount ( + IN NET_MAP *Map + ) +{ + ASSERT (Map != NULL); + return Map->Count; +} + + +/** + Return one allocated item. + + If the Recycled doubly linked list of the netmap is empty, it will try to allocate + a batch of items if there are enough resources and add corresponding nodes to the begining + of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove + the fist node entry of the Recycled doubly linked list and return the corresponding item. + + If Map is NULL, then ASSERT(). + + @param[in, out] Map The netmap to allocate item for. + + @return The allocated item. If NULL, the + allocation failed due to resource limit. + +**/ +NET_MAP_ITEM * +NetMapAllocItem ( + IN OUT NET_MAP *Map + ) +{ + NET_MAP_ITEM *Item; + LIST_ENTRY *Head; + UINTN Index; + + ASSERT (Map != NULL); + + Head = &Map->Recycled; + + if (IsListEmpty (Head)) { + for (Index = 0; Index < NET_MAP_INCREAMENT; Index++) { + Item = AllocatePool (sizeof (NET_MAP_ITEM)); + + if (Item == NULL) { + if (Index == 0) { + return NULL; + } + + break; + } + + InsertHeadList (Head, &Item->Link); + } + } + + Item = NET_LIST_HEAD (Head, NET_MAP_ITEM, Link); + NetListRemoveHead (Head); + + return Item; +} + + +/** + Allocate an item to save the pair to the head of the netmap. + + Allocate an item to save the pair and add corresponding node entry + to the beginning of the Used doubly linked list. The number of the + pairs in the netmap increase by 1. + + If Map is NULL, then ASSERT(). + If Key is NULL, then ASSERT(). + + @param[in, out] Map The netmap to insert into. + @param[in] Key The user's key. + @param[in] Value The user's value for the key. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item. + @retval EFI_SUCCESS The item is inserted to the head. + +**/ +EFI_STATUS +EFIAPI +NetMapInsertHead ( + IN OUT NET_MAP *Map, + IN VOID *Key, + IN VOID *Value OPTIONAL + ) +{ + NET_MAP_ITEM *Item; + + ASSERT (Map != NULL && Key != NULL); + + Item = NetMapAllocItem (Map); + + if (Item == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Item->Key = Key; + Item->Value = Value; + InsertHeadList (&Map->Used, &Item->Link); + + Map->Count++; + return EFI_SUCCESS; +} + + +/** + Allocate an item to save the pair to the tail of the netmap. + + Allocate an item to save the pair and add corresponding node entry + to the tail of the Used doubly linked list. The number of the + pairs in the netmap increase by 1. + + If Map is NULL, then ASSERT(). + If Key is NULL, then ASSERT(). + + @param[in, out] Map The netmap to insert into. + @param[in] Key The user's key. + @param[in] Value The user's value for the key. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item. + @retval EFI_SUCCESS The item is inserted to the tail. + +**/ +EFI_STATUS +EFIAPI +NetMapInsertTail ( + IN OUT NET_MAP *Map, + IN VOID *Key, + IN VOID *Value OPTIONAL + ) +{ + NET_MAP_ITEM *Item; + + ASSERT (Map != NULL && Key != NULL); + + Item = NetMapAllocItem (Map); + + if (Item == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Item->Key = Key; + Item->Value = Value; + InsertTailList (&Map->Used, &Item->Link); + + Map->Count++; + + return EFI_SUCCESS; +} + + +/** + Check whether the item is in the Map and return TRUE if it is. + + If Map is NULL, then ASSERT(). + If Item is NULL, then ASSERT(). + + @param[in] Map The netmap to search within. + @param[in] Item The item to search. + + @return TRUE if the item is in the netmap, otherwise FALSE. + +**/ +BOOLEAN +NetItemInMap ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item + ) +{ + LIST_ENTRY *ListEntry; + + ASSERT (Map != NULL && Item != NULL); + + NET_LIST_FOR_EACH (ListEntry, &Map->Used) { + if (ListEntry == &Item->Link) { + return TRUE; + } + } + + return FALSE; +} + + +/** + Find the key in the netmap and returns the point to the item contains the Key. + + Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every + item with the key to search. It returns the point to the item contains the Key if found. + + If Map is NULL, then ASSERT(). + If Key is NULL, then ASSERT(). + + @param[in] Map The netmap to search within. + @param[in] Key The key to search. + + @return The point to the item contains the Key, or NULL if Key isn't in the map. + +**/ +NET_MAP_ITEM * +EFIAPI +NetMapFindKey ( + IN NET_MAP *Map, + IN VOID *Key + ) +{ + LIST_ENTRY *Entry; + NET_MAP_ITEM *Item; + + ASSERT (Map != NULL && Key != NULL); + + NET_LIST_FOR_EACH (Entry, &Map->Used) { + Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link); + + if (Item->Key == Key) { + return Item; + } + } + + return NULL; +} + + +/** + Remove the node entry of the item from the netmap and return the key of the removed item. + + Remove the node entry of the item from the Used doubly linked list of the netmap. + The number of the pairs in the netmap decrease by 1. Then add the node + entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL, + Value will point to the value of the item. It returns the key of the removed item. + + If Map is NULL, then ASSERT(). + If Item is NULL, then ASSERT(). + if item in not in the netmap, then ASSERT(). + + @param[in, out] Map The netmap to remove the item from. + @param[in, out] Item The item to remove. + @param[out] Value The variable to receive the value if not NULL. + + @return The key of the removed item. + +**/ +VOID * +EFIAPI +NetMapRemoveItem ( + IN OUT NET_MAP *Map, + IN OUT NET_MAP_ITEM *Item, + OUT VOID **Value OPTIONAL + ) +{ + ASSERT ((Map != NULL) && (Item != NULL)); + ASSERT (NetItemInMap (Map, Item)); + + RemoveEntryList (&Item->Link); + Map->Count--; + InsertHeadList (&Map->Recycled, &Item->Link); + + if (Value != NULL) { + *Value = Item->Value; + } + + return Item->Key; +} + + +/** + Remove the first node entry on the netmap and return the key of the removed item. + + Remove the first node entry from the Used doubly linked list of the netmap. + The number of the pairs in the netmap decrease by 1. Then add the node + entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL, + parameter Value will point to the value of the item. It returns the key of the removed item. + + If Map is NULL, then ASSERT(). + If the Used doubly linked list is empty, then ASSERT(). + + @param[in, out] Map The netmap to remove the head from. + @param[out] Value The variable to receive the value if not NULL. + + @return The key of the item removed. + +**/ +VOID * +EFIAPI +NetMapRemoveHead ( + IN OUT NET_MAP *Map, + OUT VOID **Value OPTIONAL + ) +{ + NET_MAP_ITEM *Item; + + // + // Often, it indicates a programming error to remove + // the first entry in an empty list + // + ASSERT (Map && !IsListEmpty (&Map->Used)); + + Item = NET_LIST_HEAD (&Map->Used, NET_MAP_ITEM, Link); + RemoveEntryList (&Item->Link); + Map->Count--; + InsertHeadList (&Map->Recycled, &Item->Link); + + if (Value != NULL) { + *Value = Item->Value; + } + + return Item->Key; +} + + +/** + Remove the last node entry on the netmap and return the key of the removed item. + + Remove the last node entry from the Used doubly linked list of the netmap. + The number of the pairs in the netmap decrease by 1. Then add the node + entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL, + parameter Value will point to the value of the item. It returns the key of the removed item. + + If Map is NULL, then ASSERT(). + If the Used doubly linked list is empty, then ASSERT(). + + @param[in, out] Map The netmap to remove the tail from. + @param[out] Value The variable to receive the value if not NULL. + + @return The key of the item removed. + +**/ +VOID * +EFIAPI +NetMapRemoveTail ( + IN OUT NET_MAP *Map, + OUT VOID **Value OPTIONAL + ) +{ + NET_MAP_ITEM *Item; + + // + // Often, it indicates a programming error to remove + // the last entry in an empty list + // + ASSERT (Map && !IsListEmpty (&Map->Used)); + + Item = NET_LIST_TAIL (&Map->Used, NET_MAP_ITEM, Link); + RemoveEntryList (&Item->Link); + Map->Count--; + InsertHeadList (&Map->Recycled, &Item->Link); + + if (Value != NULL) { + *Value = Item->Value; + } + + return Item->Key; +} + + +/** + Iterate through the netmap and call CallBack for each item. + + 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. + + If Map is NULL, then ASSERT(). + If CallBack is NULL, then ASSERT(). + + @param[in] Map The Map to iterate through. + @param[in] CallBack The callback function to call for each item. + @param[in] Arg The opaque parameter to the callback. + + @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item + return EFI_SUCCESS. + @retval Others It returns the CallBack's last return value. + +**/ +EFI_STATUS +EFIAPI +NetMapIterate ( + IN NET_MAP *Map, + IN NET_MAP_CALLBACK CallBack, + IN VOID *Arg OPTIONAL + ) +{ + + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + LIST_ENTRY *Head; + NET_MAP_ITEM *Item; + EFI_STATUS Result; + + ASSERT ((Map != NULL) && (CallBack != NULL)); + + Head = &Map->Used; + + if (IsListEmpty (Head)) { + return EFI_SUCCESS; + } + + NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) { + Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link); + Result = CallBack (Map, Item, Arg); + + if (EFI_ERROR (Result)) { + return Result; + } + } + + return EFI_SUCCESS; +} + + +/** + This is the default unload handle for all the network drivers. + + Disconnect the driver specified by ImageHandle from all the devices in the handle database. + Uninstall all the protocols installed in the driver entry point. + + @param[in] ImageHandle The drivers' driver image. + + @retval EFI_SUCCESS The image is unloaded. + @retval Others Failed to unload the image. + +**/ +EFI_STATUS +EFIAPI +NetLibDefaultUnload ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_STATUS Status; + EFI_HANDLE *DeviceHandleBuffer; + UINTN DeviceHandleCount; + UINTN Index; + UINTN Index2; + EFI_DRIVER_BINDING_PROTOCOL *DriverBinding; + EFI_COMPONENT_NAME_PROTOCOL *ComponentName; + EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2; + + // + // Get the list of all the handles in the handle database. + // If there is an error getting the list, then the unload + // operation fails. + // + Status = gBS->LocateHandleBuffer ( + AllHandles, + NULL, + NULL, + &DeviceHandleCount, + &DeviceHandleBuffer + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + for (Index = 0; Index < DeviceHandleCount; Index++) { + Status = gBS->HandleProtocol ( + DeviceHandleBuffer[Index], + &gEfiDriverBindingProtocolGuid, + (VOID **) &DriverBinding + ); + if (EFI_ERROR (Status)) { + continue; + } + + 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 ( + DriverBinding->DriverBindingHandle, + &gEfiDriverBindingProtocolGuid, + DriverBinding + ); + + Status = gBS->HandleProtocol ( + DeviceHandleBuffer[Index], + &gEfiComponentNameProtocolGuid, + (VOID **) &ComponentName + ); + if (!EFI_ERROR (Status)) { + gBS->UninstallProtocolInterface ( + DriverBinding->DriverBindingHandle, + &gEfiComponentNameProtocolGuid, + ComponentName + ); + } + + Status = gBS->HandleProtocol ( + DeviceHandleBuffer[Index], + &gEfiComponentName2ProtocolGuid, + (VOID **) &ComponentName2 + ); + if (!EFI_ERROR (Status)) { + gBS->UninstallProtocolInterface ( + DriverBinding->DriverBindingHandle, + &gEfiComponentName2ProtocolGuid, + ComponentName2 + ); + } + } + + // + // Free the buffer containing the list of handles from the handle database + // + if (DeviceHandleBuffer != NULL) { + gBS->FreePool (DeviceHandleBuffer); + } + + return EFI_SUCCESS; +} + + + +/** + Create a child of the service that is identified by ServiceBindingGuid. + + Get the ServiceBinding Protocol first, then use it to create a child. + + If ServiceBindingGuid is NULL, then ASSERT(). + If ChildHandle is NULL, then ASSERT(). + + @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, out] ChildHandle The handle to receive the create child. + + @retval EFI_SUCCESS The child is successfully created. + @retval Others Failed to create the child. + +**/ +EFI_STATUS +EFIAPI +NetLibCreateServiceChild ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE Image, + IN EFI_GUID *ServiceBindingGuid, + IN OUT EFI_HANDLE *ChildHandle + ) +{ + EFI_STATUS Status; + EFI_SERVICE_BINDING_PROTOCOL *Service; + + + ASSERT ((ServiceBindingGuid != NULL) && (ChildHandle != NULL)); + + // + // Get the ServiceBinding Protocol + // + Status = gBS->OpenProtocol ( + Controller, + ServiceBindingGuid, + (VOID **) &Service, + Image, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Create a child + // + Status = Service->CreateChild (Service, ChildHandle); + return Status; +} + + +/** + Destroy a child of the service that is identified by ServiceBindingGuid. + + Get the ServiceBinding Protocol first, then use it to destroy a child. + + If ServiceBindingGuid is NULL, then ASSERT(). + + @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 destroy. + + @retval EFI_SUCCESS The child is successfully destroyed. + @retval Others Failed to destroy the child. + +**/ +EFI_STATUS +EFIAPI +NetLibDestroyServiceChild ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE Image, + IN EFI_GUID *ServiceBindingGuid, + IN EFI_HANDLE ChildHandle + ) +{ + EFI_STATUS Status; + EFI_SERVICE_BINDING_PROTOCOL *Service; + + ASSERT (ServiceBindingGuid != NULL); + + // + // Get the ServiceBinding Protocol + // + Status = gBS->OpenProtocol ( + Controller, + ServiceBindingGuid, + (VOID **) &Service, + Image, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // destroy the child + // + Status = Service->DestroyChild (Service, ChildHandle); + return Status; +} + +/** + Get handle with Simple Network Protocol installed on it. + + There should be MNP Service Binding Protocol installed on the input ServiceHandle. + If Simple Network Protocol is already installed on the ServiceHandle, the + ServiceHandle will be returned. If SNP is not installed on the ServiceHandle, + try to find its parent handle with SNP installed. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + @param[out] Snp The pointer to store the address of the SNP instance. + This is an optional parameter that may be NULL. + + @return The SNP handle, or NULL if not found. + +**/ +EFI_HANDLE +EFIAPI +NetLibGetSnpHandle ( + IN EFI_HANDLE ServiceHandle, + OUT EFI_SIMPLE_NETWORK_PROTOCOL **Snp OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *SnpInstance; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_HANDLE SnpHandle; + + // + // Try to open SNP from ServiceHandle + // + SnpInstance = NULL; + Status = gBS->HandleProtocol (ServiceHandle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &SnpInstance); + if (!EFI_ERROR (Status)) { + if (Snp != NULL) { + *Snp = SnpInstance; + } + return ServiceHandle; + } + + // + // Failed to open SNP, try to get SNP handle by LocateDevicePath() + // + DevicePath = DevicePathFromHandle (ServiceHandle); + if (DevicePath == NULL) { + return NULL; + } + + SnpHandle = NULL; + Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &DevicePath, &SnpHandle); + if (EFI_ERROR (Status)) { + // + // Failed to find SNP handle + // + return NULL; + } + + Status = gBS->HandleProtocol (SnpHandle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &SnpInstance); + if (!EFI_ERROR (Status)) { + if (Snp != NULL) { + *Snp = SnpInstance; + } + return SnpHandle; + } + + return NULL; +} + +/** + Retrieve VLAN ID of a VLAN device handle. + + Search VLAN device path node in Device Path of specified ServiceHandle and + return its VLAN ID. If no VLAN device path node found, then this ServiceHandle + is not a VLAN device handle, and 0 will be returned. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + + @return VLAN ID of the device handle, or 0 if not a VLAN device. + +**/ +UINT16 +EFIAPI +NetLibGetVlanId ( + IN EFI_HANDLE ServiceHandle + ) +{ + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_DEVICE_PATH_PROTOCOL *Node; + + DevicePath = DevicePathFromHandle (ServiceHandle); + if (DevicePath == NULL) { + return 0; + } + + Node = DevicePath; + while (!IsDevicePathEnd (Node)) { + if (Node->Type == MESSAGING_DEVICE_PATH && Node->SubType == MSG_VLAN_DP) { + return ((VLAN_DEVICE_PATH *) Node)->VlanId; + } + Node = NextDevicePathNode (Node); + } + + return 0; +} + +/** + Find VLAN device handle with specified VLAN ID. + + The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle. + This function will append VLAN device path node to the parent device path, + and then use LocateDevicePath() to find the correct VLAN device handle. + + @param[in] ControllerHandle The handle where network service binding protocols are + installed on. + @param[in] VlanId The configured VLAN ID for the VLAN device. + + @return The VLAN device handle, or NULL if not found. + +**/ +EFI_HANDLE +EFIAPI +NetLibGetVlanHandle ( + IN EFI_HANDLE ControllerHandle, + IN UINT16 VlanId + ) +{ + EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; + EFI_DEVICE_PATH_PROTOCOL *VlanDevicePath; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + VLAN_DEVICE_PATH VlanNode; + EFI_HANDLE Handle; + + ParentDevicePath = DevicePathFromHandle (ControllerHandle); + if (ParentDevicePath == NULL) { + return NULL; + } + + // + // Construct VLAN device path + // + CopyMem (&VlanNode, &mNetVlanDevicePathTemplate, sizeof (VLAN_DEVICE_PATH)); + VlanNode.VlanId = VlanId; + VlanDevicePath = AppendDevicePathNode ( + ParentDevicePath, + (EFI_DEVICE_PATH_PROTOCOL *) &VlanNode + ); + if (VlanDevicePath == NULL) { + return NULL; + } + + // + // Find VLAN device handle + // + Handle = NULL; + DevicePath = VlanDevicePath; + gBS->LocateDevicePath ( + &gEfiDevicePathProtocolGuid, + &DevicePath, + &Handle + ); + if (!IsDevicePathEnd (DevicePath)) { + // + // Device path is not exactly match + // + Handle = NULL; + } + + FreePool (VlanDevicePath); + return Handle; +} + +/** + Get MAC address associated with the network service handle. + + If MacAddress is NULL, then ASSERT(). + If AddressSize is NULL, then ASSERT(). + + There should be MNP Service Binding Protocol installed on the input ServiceHandle. + If SNP is installed on the ServiceHandle or its parent handle, MAC address will + be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + @param[out] MacAddress The pointer to store the returned MAC address. + @param[out] AddressSize The length of returned MAC address. + + @retval EFI_SUCCESS MAC address is returned successfully. + @retval Others Failed to get SNP mode data. + +**/ +EFI_STATUS +EFIAPI +NetLibGetMacAddress ( + IN EFI_HANDLE ServiceHandle, + OUT EFI_MAC_ADDRESS *MacAddress, + OUT UINTN *AddressSize + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + EFI_SIMPLE_NETWORK_MODE SnpModeData; + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + EFI_SERVICE_BINDING_PROTOCOL *MnpSb; + EFI_HANDLE *SnpHandle; + EFI_HANDLE MnpChildHandle; + + ASSERT (MacAddress != NULL); + ASSERT (AddressSize != NULL); + + // + // Try to get SNP handle + // + Snp = NULL; + SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp); + if (SnpHandle != NULL) { + // + // SNP found, use it directly + // + SnpMode = Snp->Mode; + } else { + // + // Failed to get SNP handle, try to get MAC address from MNP + // + MnpChildHandle = NULL; + Status = gBS->HandleProtocol ( + ServiceHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + (VOID **) &MnpSb + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Create a MNP child + // + Status = MnpSb->CreateChild (MnpSb, &MnpChildHandle); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Open MNP protocol + // + Status = gBS->HandleProtocol ( + MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **) &Mnp + ); + if (EFI_ERROR (Status)) { + MnpSb->DestroyChild (MnpSb, MnpChildHandle); + return Status; + } + + // + // Try to get SNP mode from MNP + // + Status = Mnp->GetModeData (Mnp, NULL, &SnpModeData); + if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) { + MnpSb->DestroyChild (MnpSb, MnpChildHandle); + return Status; + } + SnpMode = &SnpModeData; + + // + // Destroy the MNP child + // + MnpSb->DestroyChild (MnpSb, MnpChildHandle); + } + + *AddressSize = SnpMode->HwAddressSize; + CopyMem (MacAddress->Addr, SnpMode->CurrentAddress.Addr, SnpMode->HwAddressSize); + + return EFI_SUCCESS; +} + +/** + Convert MAC address of the NIC associated with specified Service Binding Handle + to a unicode string. Callers are responsible for freeing the string storage. + + If MacString is NULL, then ASSERT(). + + Locate simple network protocol associated with the Service Binding Handle and + get the mac address from SNP. Then convert the mac address into a unicode + string. It takes 2 unicode characters to represent a 1 byte binary buffer. + Plus one unicode character for the null-terminator. + + @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. 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. + + @retval EFI_SUCCESS Convert the mac address a unicode string successfully. + @retval EFI_OUT_OF_RESOURCES There are not enough memory resource. + @retval Others Failed to open the simple network protocol. + +**/ +EFI_STATUS +EFIAPI +NetLibGetMacString ( + IN EFI_HANDLE ServiceHandle, + IN EFI_HANDLE ImageHandle, OPTIONAL + OUT CHAR16 **MacString + ) +{ + EFI_STATUS Status; + EFI_MAC_ADDRESS MacAddress; + UINT8 *HwAddress; + UINTN HwAddressSize; + UINT16 VlanId; + CHAR16 *String; + UINTN Index; + UINTN BufferSize; + + ASSERT (MacString != NULL); + + // + // Get MAC address of the network device + // + Status = NetLibGetMacAddress (ServiceHandle, &MacAddress, &HwAddressSize); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // It takes 2 unicode characters to represent a 1 byte binary buffer. + // If VLAN is configured, it will need extra 5 characters like "\0005". + // Plus one unicode character for the null-terminator. + // + BufferSize = (2 * HwAddressSize + 5 + 1) * sizeof (CHAR16); + String = AllocateZeroPool (BufferSize); + if (String == NULL) { + return EFI_OUT_OF_RESOURCES; + } + *MacString = String; + + // + // Convert the MAC address into a unicode string. + // + HwAddress = &MacAddress.Addr[0]; + for (Index = 0; Index < HwAddressSize; Index++) { + UnicodeValueToStringS ( + String, + BufferSize - ((UINTN)String - (UINTN)*MacString), + PREFIX_ZERO | RADIX_HEX, + *(HwAddress++), + 2 + ); + String += StrnLenS (String, (BufferSize - ((UINTN)String - (UINTN)*MacString)) / sizeof (CHAR16)); + } + + // + // Append VLAN ID if any + // + VlanId = NetLibGetVlanId (ServiceHandle); + if (VlanId != 0) { + *String++ = L'\\'; + UnicodeValueToStringS ( + String, + BufferSize - ((UINTN)String - (UINTN)*MacString), + PREFIX_ZERO | RADIX_HEX, + VlanId, + 4 + ); + String += StrnLenS (String, (BufferSize - ((UINTN)String - (UINTN)*MacString)) / sizeof (CHAR16)); + } + + // + // Null terminate the Unicode string + // + *String = L'\0'; + + return EFI_SUCCESS; +} + +/** + Detect media status for specified network device. + + If MediaPresent is NULL, then ASSERT(). + + The underlying UNDI driver may or may not support reporting media status from + GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine + will try to invoke Snp->GetStatus() to get the media status: if media already + present, it return directly; if media not present, it will stop SNP and then + restart SNP to get the latest media status, this give chance to get the correct + media status for old UNDI driver which doesn't support reporting media status + from GET_STATUS command. + Note: there will be two limitations for current algorithm: + 1) for UNDI with this capability, in case of cable is not attached, there will + be an redundant Stop/Start() process; + 2) for UNDI without this capability, in case that network cable is attached when + Snp->Initialize() is invoked while network cable is unattached later, + NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer + apps to wait for timeout time. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + @param[out] MediaPresent The pointer to store the media status. + + @retval EFI_SUCCESS Media detection success. + @retval EFI_INVALID_PARAMETER ServiceHandle is not valid network device handle. + @retval EFI_UNSUPPORTED Network device does not support media detection. + @retval EFI_DEVICE_ERROR SNP is in unknown state. + +**/ +EFI_STATUS +EFIAPI +NetLibDetectMedia ( + IN EFI_HANDLE ServiceHandle, + OUT BOOLEAN *MediaPresent + ) +{ + EFI_STATUS Status; + EFI_HANDLE SnpHandle; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + UINT32 InterruptStatus; + UINT32 OldState; + EFI_MAC_ADDRESS *MCastFilter; + UINT32 MCastFilterCount; + UINT32 EnableFilterBits; + UINT32 DisableFilterBits; + BOOLEAN ResetMCastFilters; + + ASSERT (MediaPresent != NULL); + + // + // Get SNP handle + // + Snp = NULL; + SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp); + if (SnpHandle == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Check whether SNP support media detection + // + if (!Snp->Mode->MediaPresentSupported) { + return EFI_UNSUPPORTED; + } + + // + // Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data + // + Status = Snp->GetStatus (Snp, &InterruptStatus, NULL); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Snp->Mode->MediaPresent) { + // + // Media is present, return directly + // + *MediaPresent = TRUE; + return EFI_SUCCESS; + } + + // + // Till now, GetStatus() report no media; while, in case UNDI not support + // reporting media status from GetStatus(), this media status may be incorrect. + // So, we will stop SNP and then restart it to get the correct media status. + // + OldState = Snp->Mode->State; + if (OldState >= EfiSimpleNetworkMaxState) { + return EFI_DEVICE_ERROR; + } + + MCastFilter = NULL; + + if (OldState == EfiSimpleNetworkInitialized) { + // + // SNP is already in use, need Shutdown/Stop and then Start/Initialize + // + + // + // Backup current SNP receive filter settings + // + EnableFilterBits = Snp->Mode->ReceiveFilterSetting; + DisableFilterBits = Snp->Mode->ReceiveFilterMask ^ EnableFilterBits; + + ResetMCastFilters = TRUE; + MCastFilterCount = Snp->Mode->MCastFilterCount; + if (MCastFilterCount != 0) { + MCastFilter = AllocateCopyPool ( + MCastFilterCount * sizeof (EFI_MAC_ADDRESS), + Snp->Mode->MCastFilter + ); + ASSERT (MCastFilter != NULL); + if (MCastFilter == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + ResetMCastFilters = FALSE; + } + + // + // Shutdown/Stop the simple network + // + Status = Snp->Shutdown (Snp); + if (!EFI_ERROR (Status)) { + Status = Snp->Stop (Snp); + } + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Start/Initialize the simple network + // + Status = Snp->Start (Snp); + if (!EFI_ERROR (Status)) { + Status = Snp->Initialize (Snp, 0, 0); + } + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Here we get the correct media status + // + *MediaPresent = Snp->Mode->MediaPresent; + + // + // Restore SNP receive filter settings + // + Status = Snp->ReceiveFilters ( + Snp, + EnableFilterBits, + DisableFilterBits, + ResetMCastFilters, + MCastFilterCount, + MCastFilter + ); + + if (MCastFilter != NULL) { + FreePool (MCastFilter); + } + + return Status; + } + + // + // SNP is not in use, it's in state of EfiSimpleNetworkStopped or EfiSimpleNetworkStarted + // + if (OldState == EfiSimpleNetworkStopped) { + // + // SNP not start yet, start it + // + Status = Snp->Start (Snp); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + + // + // Initialize the simple network + // + Status = Snp->Initialize (Snp, 0, 0); + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + + // + // Here we get the correct media status + // + *MediaPresent = Snp->Mode->MediaPresent; + + // + // Shut down the simple network + // + Snp->Shutdown (Snp); + +Exit: + if (OldState == EfiSimpleNetworkStopped) { + // + // Original SNP sate is Stopped, restore to original state + // + Snp->Stop (Snp); + } + + if (MCastFilter != NULL) { + FreePool (MCastFilter); + } + + return Status; +} + +/** + + Detect media state for a network device. This routine will wait for a period of time at + a specified checking interval when a certain network is under connecting until connection + process finishs or timeout. If Aip protocol is supported by low layer drivers, three kinds + of media states can be detected: EFI_SUCCESS, EFI_NOT_READY and EFI_NO_MEDIA, represents + connected state, connecting state and no media state respectively. When function detects + the current state is EFI_NOT_READY, it will loop to wait for next time's check until state + turns to be EFI_SUCCESS or EFI_NO_MEDIA. If Aip protocol is not supported, function will + call NetLibDetectMedia() and return state directly. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + @param[in] Timeout The maximum number of 100ns units to wait when network + is connecting. Zero value means detect once and return + immediately. + @param[out] MediaState The pointer to the detected media state. + + @retval EFI_SUCCESS Media detection success. + @retval EFI_INVALID_PARAMETER ServiceHandle is not a valid network device handle or + MediaState pointer is NULL. + @retval EFI_DEVICE_ERROR A device error occurred. + @retval EFI_TIMEOUT Network is connecting but timeout. + +**/ +EFI_STATUS +EFIAPI +NetLibDetectMediaWaitTimeout ( + IN EFI_HANDLE ServiceHandle, + IN UINT64 Timeout, + OUT EFI_STATUS *MediaState + ) +{ + EFI_STATUS Status; + EFI_HANDLE SnpHandle; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_ADAPTER_INFORMATION_PROTOCOL *Aip; + EFI_ADAPTER_INFO_MEDIA_STATE *MediaInfo; + BOOLEAN MediaPresent; + UINTN DataSize; + EFI_STATUS TimerStatus; + EFI_EVENT Timer; + UINT64 TimeRemained; + + if (MediaState == NULL) { + return EFI_INVALID_PARAMETER; + } + *MediaState = EFI_SUCCESS; + MediaInfo = NULL; + + // + // Get SNP handle + // + Snp = NULL; + SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp); + if (SnpHandle == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = gBS->HandleProtocol ( + SnpHandle, + &gEfiAdapterInformationProtocolGuid, + (VOID *) &Aip + ); + if (EFI_ERROR (Status)) { + + MediaPresent = TRUE; + Status = NetLibDetectMedia (ServiceHandle, &MediaPresent); + if (!EFI_ERROR (Status)) { + if (MediaPresent) { + *MediaState = EFI_SUCCESS; + } else { + *MediaState = EFI_NO_MEDIA; + } + } + + // + // NetLibDetectMedia doesn't support EFI_NOT_READY status, return now! + // + return Status; + } + + Status = Aip->GetInformation ( + Aip, + &gEfiAdapterInfoMediaStateGuid, + (VOID **) &MediaInfo, + &DataSize + ); + if (!EFI_ERROR (Status)) { + + *MediaState = MediaInfo->MediaState; + FreePool (MediaInfo); + if (*MediaState != EFI_NOT_READY || Timeout < MEDIA_STATE_DETECT_TIME_INTERVAL) { + + return EFI_SUCCESS; + } + } else { + + if (MediaInfo != NULL) { + FreePool (MediaInfo); + } + + if (Status == EFI_UNSUPPORTED) { + + // + // If gEfiAdapterInfoMediaStateGuid is not supported, call NetLibDetectMedia to get media state! + // + MediaPresent = TRUE; + Status = NetLibDetectMedia (ServiceHandle, &MediaPresent); + if (!EFI_ERROR (Status)) { + if (MediaPresent) { + *MediaState = EFI_SUCCESS; + } else { + *MediaState = EFI_NO_MEDIA; + } + } + return Status; + } + + return Status; + } + + // + // Loop to check media state + // + + Timer = NULL; + TimeRemained = Timeout; + Status = gBS->CreateEvent (EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Timer); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + do { + Status = gBS->SetTimer ( + Timer, + TimerRelative, + MEDIA_STATE_DETECT_TIME_INTERVAL + ); + if (EFI_ERROR (Status)) { + gBS->CloseEvent(Timer); + return EFI_DEVICE_ERROR; + } + + do { + TimerStatus = gBS->CheckEvent (Timer); + if (!EFI_ERROR (TimerStatus)) { + + TimeRemained -= MEDIA_STATE_DETECT_TIME_INTERVAL; + Status = Aip->GetInformation ( + Aip, + &gEfiAdapterInfoMediaStateGuid, + (VOID **) &MediaInfo, + &DataSize + ); + if (!EFI_ERROR (Status)) { + + *MediaState = MediaInfo->MediaState; + FreePool (MediaInfo); + } else { + + if (MediaInfo != NULL) { + FreePool (MediaInfo); + } + gBS->CloseEvent(Timer); + return Status; + } + } + } while (TimerStatus == EFI_NOT_READY); + } while (*MediaState == EFI_NOT_READY && TimeRemained >= MEDIA_STATE_DETECT_TIME_INTERVAL); + + gBS->CloseEvent(Timer); + if (*MediaState == EFI_NOT_READY && TimeRemained < MEDIA_STATE_DETECT_TIME_INTERVAL) { + return EFI_TIMEOUT; + } else { + return EFI_SUCCESS; + } +} + +/** + Check the default address used by the IPv4 driver is static or dynamic (acquired + from DHCP). + + 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 EFI_IP4_CONFIG2_PROTOCOL + relative with the default address to judge. + + @retval TRUE If the default address is static. + @retval FALSE If the default address is acquired from DHCP. + +**/ +BOOLEAN +NetLibDefaultAddressIsStatic ( + IN EFI_HANDLE Controller + ) +{ + EFI_STATUS Status; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + UINTN DataSize; + EFI_IP4_CONFIG2_POLICY Policy; + BOOLEAN IsStatic; + + Ip4Config2 = NULL; + + DataSize = sizeof (EFI_IP4_CONFIG2_POLICY); + + IsStatic = TRUE; + + // + // Get Ip4Config2 policy. + // + Status = gBS->HandleProtocol (Controller, &gEfiIp4Config2ProtocolGuid, (VOID **) &Ip4Config2); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypePolicy, &DataSize, &Policy); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + IsStatic = (BOOLEAN) (Policy == Ip4Config2PolicyStatic); + +ON_EXIT: + + return IsStatic; +} + +/** + Create an IPv4 device path node. + + If Node is NULL, then ASSERT(). + + The header type of IPv4 device path node is MESSAGING_DEVICE_PATH. + The header subtype of IPv4 device path node is MSG_IPv4_DP. + 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. + @param[in] Controller The controller handle. + @param[in] LocalIp The local IPv4 address. + @param[in] LocalPort The local port. + @param[in] RemoteIp The remote IPv4 address. + @param[in] RemotePort The remote port. + @param[in] Protocol The protocol type in the IP header. + @param[in] UseDefaultAddress Whether this instance is using default address or not. + +**/ +VOID +EFIAPI +NetLibCreateIPv4DPathNode ( + IN OUT IPv4_DEVICE_PATH *Node, + IN EFI_HANDLE Controller, + IN IP4_ADDR LocalIp, + IN UINT16 LocalPort, + IN IP4_ADDR RemoteIp, + IN UINT16 RemotePort, + IN UINT16 Protocol, + IN BOOLEAN UseDefaultAddress + ) +{ + ASSERT (Node != NULL); + + Node->Header.Type = MESSAGING_DEVICE_PATH; + Node->Header.SubType = MSG_IPv4_DP; + SetDevicePathNodeLength (&Node->Header, sizeof (IPv4_DEVICE_PATH)); + + CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS)); + + Node->LocalPort = LocalPort; + Node->RemotePort = RemotePort; + + Node->Protocol = Protocol; + + if (!UseDefaultAddress) { + Node->StaticIpAddress = TRUE; + } 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; +} + +/** + Create an IPv6 device path node. + + If Node is NULL, then ASSERT(). + If LocalIp is NULL, then ASSERT(). + If RemoteIp is NULL, then ASSERT(). + + The header type of IPv6 device path node is MESSAGING_DEVICE_PATH. + The header subtype of IPv6 device path node is MSG_IPv6_DP. + Get other info from parameters to make up the whole IPv6 device path node. + + @param[in, out] Node Pointer to the IPv6 device path node. + @param[in] Controller The controller handle. + @param[in] LocalIp The local IPv6 address. + @param[in] LocalPort The local port. + @param[in] RemoteIp The remote IPv6 address. + @param[in] RemotePort The remote port. + @param[in] Protocol The protocol type in the IP header. + +**/ +VOID +EFIAPI +NetLibCreateIPv6DPathNode ( + IN OUT IPv6_DEVICE_PATH *Node, + IN EFI_HANDLE Controller, + IN EFI_IPv6_ADDRESS *LocalIp, + IN UINT16 LocalPort, + IN EFI_IPv6_ADDRESS *RemoteIp, + IN UINT16 RemotePort, + IN UINT16 Protocol + ) +{ + ASSERT (Node != NULL && LocalIp != NULL && RemoteIp != NULL); + + Node->Header.Type = MESSAGING_DEVICE_PATH; + Node->Header.SubType = MSG_IPv6_DP; + SetDevicePathNodeLength (&Node->Header, sizeof (IPv6_DEVICE_PATH)); + + CopyMem (&Node->LocalIpAddress, LocalIp, sizeof (EFI_IPv6_ADDRESS)); + CopyMem (&Node->RemoteIpAddress, RemoteIp, sizeof (EFI_IPv6_ADDRESS)); + + Node->LocalPort = LocalPort; + Node->RemotePort = RemotePort; + + Node->Protocol = Protocol; + + // + // 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)); +} + +/** + Find the UNDI/SNP handle from controller and protocol GUID. + + If ProtocolGuid is NULL, then ASSERT(). + + For example, IP will open a MNP child to transmit/receive + packets, when MNP is stopped, IP should also be stopped. IP + needs to find its own private data which is related the IP's + service binding instance that is install on UNDI/SNP handle. + Now, the controller is either a MNP or ARP child handle. But + IP opens these handle BY_DRIVER, use that info, we can get the + UNDI/SNP handle. + + @param[in] Controller Then protocol handle to check. + @param[in] ProtocolGuid The protocol that is related with the handle. + + @return The UNDI/SNP handle or NULL for errors. + +**/ +EFI_HANDLE +EFIAPI +NetLibGetNicHandle ( + IN EFI_HANDLE Controller, + IN EFI_GUID *ProtocolGuid + ) +{ + EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer; + EFI_HANDLE Handle; + EFI_STATUS Status; + UINTN OpenCount; + UINTN Index; + + ASSERT (ProtocolGuid != NULL); + + Status = gBS->OpenProtocolInformation ( + Controller, + ProtocolGuid, + &OpenBuffer, + &OpenCount + ); + + if (EFI_ERROR (Status)) { + return NULL; + } + + Handle = NULL; + + for (Index = 0; Index < OpenCount; Index++) { + if ((OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) { + Handle = OpenBuffer[Index].ControllerHandle; + break; + } + } + + gBS->FreePool (OpenBuffer); + return Handle; +} + +/** + Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS. + + @param[in] String The pointer to the Ascii string. + @param[out] Ip4Address The pointer to the converted IPv4 address. + + @retval EFI_SUCCESS Convert to IPv4 address successfully. + @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL. + +**/ +EFI_STATUS +EFIAPI +NetLibAsciiStrToIp4 ( + IN CONST CHAR8 *String, + OUT EFI_IPv4_ADDRESS *Ip4Address + ) +{ + RETURN_STATUS Status; + CHAR8 *EndPointer; + + Status = AsciiStrToIpv4Address (String, &EndPointer, Ip4Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer != '\0')) { + return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; + } +} + + +/** + Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of 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. + +**/ +EFI_STATUS +EFIAPI +NetLibAsciiStrToIp6 ( + IN CONST CHAR8 *String, + OUT EFI_IPv6_ADDRESS *Ip6Address + ) +{ + RETURN_STATUS Status; + CHAR8 *EndPointer; + + Status = AsciiStrToIpv6Address (String, &EndPointer, Ip6Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer != '\0')) { + return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; + } +} + + +/** + Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS. + + @param[in] String The pointer to the Ascii string. + @param[out] Ip4Address The pointer to the converted IPv4 address. + + @retval EFI_SUCCESS Convert to IPv4 address successfully. + @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL. + +**/ +EFI_STATUS +EFIAPI +NetLibStrToIp4 ( + IN CONST CHAR16 *String, + OUT EFI_IPv4_ADDRESS *Ip4Address + ) +{ + RETURN_STATUS Status; + CHAR16 *EndPointer; + + Status = StrToIpv4Address (String, &EndPointer, Ip4Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) { + return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; + } +} + + +/** + Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of + 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. + +**/ +EFI_STATUS +EFIAPI +NetLibStrToIp6 ( + IN CONST CHAR16 *String, + OUT EFI_IPv6_ADDRESS *Ip6Address + ) +{ + RETURN_STATUS Status; + CHAR16 *EndPointer; + + Status = StrToIpv6Address (String, &EndPointer, Ip6Address, NULL); + if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) { + return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; + } +} + +/** + Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length. + 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. + @param[out] Ip6Address The pointer to the converted IPv6 address. + @param[out] PrefixLength The pointer to the converted prefix length. + + @retval EFI_SUCCESS Convert to IPv6 address successfully. + @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL. + +**/ +EFI_STATUS +EFIAPI +NetLibStrToIp6andPrefix ( + IN CONST CHAR16 *String, + OUT EFI_IPv6_ADDRESS *Ip6Address, + OUT UINT8 *PrefixLength + ) +{ + RETURN_STATUS Status; + CHAR16 *EndPointer; + + Status = StrToIpv6Address (String, &EndPointer, Ip6Address, PrefixLength); + if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) { + return EFI_INVALID_PARAMETER; + } else { + return EFI_SUCCESS; + } +} + +/** + + 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; + } + + // + // 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)); + } + + // + // Find the longest zeros and mark it. + // + 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 (CurrentZerosStart != DEFAULT_ZERO_START && CurrentZerosLength > 2) { + if (LongestZerosStart == DEFAULT_ZERO_START || LongestZerosLength < CurrentZerosLength) { + LongestZerosStart = CurrentZerosStart; + LongestZerosLength = CurrentZerosLength; + } + } + + 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'; + + if ((UINTN)Ptr - (UINTN)Buffer > StringSize) { + return EFI_BUFFER_TOO_SMALL; + } + + StrCpyS (String, StringSize / sizeof (CHAR16), Buffer); + + return EFI_SUCCESS; +} + +/** + This function obtains the system guid from the smbios table. + + If SystemGuid is NULL, then ASSERT(). + + @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; + + ASSERT (SystemGuid != NULL); + + 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); + } + + 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; + } + + // + // 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++) { + } + + 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. + + If DomainName is NULL, then ASSERT(). + + 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; + + ASSERT (DomainName != NULL); + + 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; +} diff --git a/NetworkPkg/Library/DxeNetLib/DxeNetLib.inf b/NetworkPkg/Library/DxeNetLib/DxeNetLib.inf new file mode 100644 index 000000000..40e855695 --- /dev/null +++ b/NetworkPkg/Library/DxeNetLib/DxeNetLib.inf @@ -0,0 +1,62 @@ +## @file +# This library instance provides the basic network services. +# +# Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+# (C) Copyright 2015 Hewlett Packard Enterprise Development LP
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = DxeNetLib + MODULE_UNI_FILE = DxeNetLib.uni + FILE_GUID = db6dcef3-9f4e-4340-9351-fc35aa8a5888 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = NetLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources] + DxeNetLib.c + NetBuffer.c + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + BaseLib + DebugLib + BaseMemoryLib + UefiBootServicesTableLib + UefiRuntimeServicesTableLib + UefiLib + MemoryAllocationLib + DevicePathLib + PrintLib + + +[Guids] + gEfiSmbiosTableGuid ## SOMETIMES_CONSUMES ## SystemTable + gEfiSmbios3TableGuid ## SOMETIMES_CONSUMES ## SystemTable + gEfiAdapterInfoMediaStateGuid ## SOMETIMES_CONSUMES + + +[Protocols] + gEfiSimpleNetworkProtocolGuid ## SOMETIMES_CONSUMES + gEfiManagedNetworkProtocolGuid ## SOMETIMES_CONSUMES + gEfiManagedNetworkServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiIp4Config2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiComponentNameProtocolGuid ## SOMETIMES_CONSUMES + gEfiComponentName2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiAdapterInformationProtocolGuid ## SOMETIMES_CONSUMES diff --git a/NetworkPkg/Library/DxeNetLib/DxeNetLib.uni b/NetworkPkg/Library/DxeNetLib/DxeNetLib.uni new file mode 100644 index 000000000..0c9f1fddd --- /dev/null +++ b/NetworkPkg/Library/DxeNetLib/DxeNetLib.uni @@ -0,0 +1,16 @@ +// /** @file +// This library instance provides the basic network services. +// +// This library instance provides the basic network services. +// +// Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Provides the basic network services" + +#string STR_MODULE_DESCRIPTION #language en-US "This library instance provides the basic network services." + diff --git a/NetworkPkg/Library/DxeNetLib/NetBuffer.c b/NetworkPkg/Library/DxeNetLib/NetBuffer.c new file mode 100644 index 000000000..2408e9a10 --- /dev/null +++ b/NetworkPkg/Library/DxeNetLib/NetBuffer.c @@ -0,0 +1,1890 @@ +/** @file + Network library functions providing net buffer operation support. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +#include +#include +#include +#include +#include +#include + + +/** + Allocate and build up the sketch for a NET_BUF. + + The net buffer allocated has the BlockOpNum's NET_BLOCK_OP, and its associated + NET_VECTOR has the BlockNum's NET_BLOCK. But all the NET_BLOCK_OP and + NET_BLOCK remain un-initialized. + + @param[in] BlockNum The number of NET_BLOCK in the vector of net buffer + @param[in] BlockOpNum The number of NET_BLOCK_OP in the net buffer + + @return Pointer to the allocated NET_BUF, or NULL if the + allocation failed due to resource limit. + +**/ +NET_BUF * +NetbufAllocStruct ( + IN UINT32 BlockNum, + IN UINT32 BlockOpNum + ) +{ + NET_BUF *Nbuf; + NET_VECTOR *Vector; + + ASSERT (BlockOpNum >= 1); + + // + // Allocate three memory blocks. + // + Nbuf = AllocateZeroPool (NET_BUF_SIZE (BlockOpNum)); + + if (Nbuf == NULL) { + return NULL; + } + + Nbuf->Signature = NET_BUF_SIGNATURE; + Nbuf->RefCnt = 1; + Nbuf->BlockOpNum = BlockOpNum; + InitializeListHead (&Nbuf->List); + + if (BlockNum != 0) { + Vector = AllocateZeroPool (NET_VECTOR_SIZE (BlockNum)); + + if (Vector == NULL) { + goto FreeNbuf; + } + + Vector->Signature = NET_VECTOR_SIGNATURE; + Vector->RefCnt = 1; + Vector->BlockNum = BlockNum; + Nbuf->Vector = Vector; + } + + return Nbuf; + +FreeNbuf: + + FreePool (Nbuf); + return NULL; +} + + +/** + Allocate a single block NET_BUF. Upon allocation, all the + free space is in the tail room. + + @param[in] Len The length of the block. + + @return Pointer to the allocated NET_BUF, or NULL if the + allocation failed due to resource limit. + +**/ +NET_BUF * +EFIAPI +NetbufAlloc ( + IN UINT32 Len + ) +{ + NET_BUF *Nbuf; + NET_VECTOR *Vector; + UINT8 *Bulk; + + ASSERT (Len > 0); + + Nbuf = NetbufAllocStruct (1, 1); + + if (Nbuf == NULL) { + return NULL; + } + + Bulk = AllocatePool (Len); + + if (Bulk == NULL) { + goto FreeNBuf; + } + + Vector = Nbuf->Vector; + Vector->Len = Len; + + Vector->Block[0].Bulk = Bulk; + Vector->Block[0].Len = Len; + + Nbuf->BlockOp[0].BlockHead = Bulk; + Nbuf->BlockOp[0].BlockTail = Bulk + Len; + + Nbuf->BlockOp[0].Head = Bulk; + Nbuf->BlockOp[0].Tail = Bulk; + Nbuf->BlockOp[0].Size = 0; + + return Nbuf; + +FreeNBuf: + FreePool (Nbuf); + return NULL; +} + +/** + Free the net vector. + + Decrease the reference count of the net vector by one. The real resource free + operation isn't performed until the reference count of the net vector is + decreased to 0. + + @param[in] Vector Pointer to the NET_VECTOR to be freed. + +**/ +VOID +NetbufFreeVector ( + IN NET_VECTOR *Vector + ) +{ + UINT32 Index; + + ASSERT (Vector != NULL); + NET_CHECK_SIGNATURE (Vector, NET_VECTOR_SIGNATURE); + ASSERT (Vector->RefCnt > 0); + + Vector->RefCnt--; + + if (Vector->RefCnt > 0) { + return; + } + + if (Vector->Free != NULL) { + // + // Call external free function to free the vector if it + // isn't NULL. If NET_VECTOR_OWN_FIRST is set, release the + // first block since it is allocated by us + // + if ((Vector->Flag & NET_VECTOR_OWN_FIRST) != 0) { + gBS->FreePool (Vector->Block[0].Bulk); + } + + Vector->Free (Vector->Arg); + + } else { + // + // Free each memory block associated with the Vector + // + for (Index = 0; Index < Vector->BlockNum; Index++) { + gBS->FreePool (Vector->Block[Index].Bulk); + } + } + + FreePool (Vector); +} + + +/** + Free the net buffer and its associated NET_VECTOR. + + Decrease the reference count of the net buffer by one. Free the associated net + vector and itself if the reference count of the net buffer is decreased to 0. + The net vector free operation just decrease the reference count of the net + vector by one and do the real resource free operation when the reference count + of the net vector is 0. + + @param[in] Nbuf Pointer to the NET_BUF to be freed. + +**/ +VOID +EFIAPI +NetbufFree ( + IN NET_BUF *Nbuf + ) +{ + ASSERT (Nbuf != NULL); + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + ASSERT (Nbuf->RefCnt > 0); + + Nbuf->RefCnt--; + + if (Nbuf->RefCnt == 0) { + // + // Update Vector only when NBuf is to be released. That is, + // all the sharing of Nbuf increse Vector's RefCnt by one + // + NetbufFreeVector (Nbuf->Vector); + FreePool (Nbuf); + } +} + + +/** + Create a copy of the net buffer that shares the associated net vector. + + The reference count of the newly created net buffer is set to 1. The reference + count of the associated net vector is increased by one. + + @param[in] Nbuf Pointer to the net buffer to be cloned. + + @return Pointer to the cloned net buffer, or NULL if the + allocation failed due to resource limit. + +**/ +NET_BUF * +EFIAPI +NetbufClone ( + IN NET_BUF *Nbuf + ) +{ + NET_BUF *Clone; + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + + Clone = AllocatePool (NET_BUF_SIZE (Nbuf->BlockOpNum)); + + if (Clone == NULL) { + return NULL; + } + + Clone->Signature = NET_BUF_SIGNATURE; + Clone->RefCnt = 1; + InitializeListHead (&Clone->List); + + Clone->Ip = Nbuf->Ip; + Clone->Tcp = Nbuf->Tcp; + + CopyMem (Clone->ProtoData, Nbuf->ProtoData, NET_PROTO_DATA); + + NET_GET_REF (Nbuf->Vector); + + Clone->Vector = Nbuf->Vector; + Clone->BlockOpNum = Nbuf->BlockOpNum; + Clone->TotalSize = Nbuf->TotalSize; + CopyMem (Clone->BlockOp, Nbuf->BlockOp, sizeof (NET_BLOCK_OP) * Nbuf->BlockOpNum); + + return Clone; +} + + +/** + Create a duplicated copy of the net buffer with data copied and HeadSpace + bytes of head space reserved. + + The duplicated net buffer will allocate its own memory to hold the data of the + source net buffer. + + @param[in] Nbuf Pointer to the net buffer to be duplicated from. + @param[in, out] Duplicate Pointer to the net buffer to duplicate to, if + NULL a new net buffer is allocated. + @param[in] HeadSpace Length of the head space to reserve. + + @return Pointer to the duplicated net buffer, or NULL if + the allocation failed due to resource limit. + +**/ +NET_BUF * +EFIAPI +NetbufDuplicate ( + IN NET_BUF *Nbuf, + IN OUT NET_BUF *Duplicate OPTIONAL, + IN UINT32 HeadSpace + ) +{ + UINT8 *Dst; + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + + if (Duplicate == NULL) { + Duplicate = NetbufAlloc (Nbuf->TotalSize + HeadSpace); + } + + if (Duplicate == NULL) { + return NULL; + } + + // + // Don't set the IP and TCP head point, since it is most + // like that they are pointing to the memory of Nbuf. + // + CopyMem (Duplicate->ProtoData, Nbuf->ProtoData, NET_PROTO_DATA); + NetbufReserve (Duplicate, HeadSpace); + + Dst = NetbufAllocSpace (Duplicate, Nbuf->TotalSize, NET_BUF_TAIL); + NetbufCopy (Nbuf, 0, Nbuf->TotalSize, Dst); + + return Duplicate; +} + + +/** + Free a list of net buffers. + + @param[in, out] Head Pointer to the head of linked net buffers. + +**/ +VOID +EFIAPI +NetbufFreeList ( + IN OUT LIST_ENTRY *Head + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + NET_BUF *Nbuf; + + Entry = Head->ForwardLink; + + NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) { + Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + + RemoveEntryList (Entry); + NetbufFree (Nbuf); + } + + ASSERT (IsListEmpty (Head)); +} + + +/** + Get the index of NET_BLOCK_OP that contains the byte at Offset in the net + buffer. + + This can be used to, for example, retrieve the IP header in the packet. It + also can be used to get the fragment that contains the byte which is used + mainly by the library implementation itself. + + @param[in] Nbuf Pointer to the net buffer. + @param[in] Offset The offset of the byte. + @param[out] Index Index of the NET_BLOCK_OP that contains the byte at + Offset. + + @return Pointer to the Offset'th byte of data in the net buffer, or NULL + if there is no such data in the net buffer. + +**/ +UINT8 * +EFIAPI +NetbufGetByte ( + IN NET_BUF *Nbuf, + IN UINT32 Offset, + OUT UINT32 *Index OPTIONAL + ) +{ + NET_BLOCK_OP *BlockOp; + UINT32 Loop; + UINT32 Len; + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + + if (Offset >= Nbuf->TotalSize) { + return NULL; + } + + BlockOp = Nbuf->BlockOp; + Len = 0; + + for (Loop = 0; Loop < Nbuf->BlockOpNum; Loop++) { + + if (Len + BlockOp[Loop].Size <= Offset) { + Len += BlockOp[Loop].Size; + continue; + } + + if (Index != NULL) { + *Index = Loop; + } + + return BlockOp[Loop].Head + (Offset - Len); + } + + return NULL; +} + + + +/** + Set the NET_BLOCK and corresponding NET_BLOCK_OP in the net buffer and + corresponding net vector according to the bulk pointer and bulk length. + + All the pointers in the Index'th NET_BLOCK and NET_BLOCK_OP are set to the + bulk's head and tail respectively. So, this function alone can't be used by + NetbufAlloc. + + @param[in, out] Nbuf Pointer to the net buffer. + @param[in] Bulk Pointer to the data. + @param[in] Len Length of the bulk data. + @param[in] Index The data block index in the net buffer the bulk + data should belong to. + +**/ +VOID +NetbufSetBlock ( + IN OUT NET_BUF *Nbuf, + IN UINT8 *Bulk, + IN UINT32 Len, + IN UINT32 Index + ) +{ + NET_BLOCK_OP *BlockOp; + NET_BLOCK *Block; + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + NET_CHECK_SIGNATURE (Nbuf->Vector, NET_VECTOR_SIGNATURE); + ASSERT (Index < Nbuf->BlockOpNum); + + Block = &(Nbuf->Vector->Block[Index]); + BlockOp = &(Nbuf->BlockOp[Index]); + Block->Len = Len; + Block->Bulk = Bulk; + BlockOp->BlockHead = Bulk; + BlockOp->BlockTail = Bulk + Len; + BlockOp->Head = Bulk; + BlockOp->Tail = Bulk + Len; + BlockOp->Size = Len; +} + + + +/** + Set the NET_BLOCK_OP in the net buffer. The corresponding NET_BLOCK + structure is left untouched. + + Some times, there is no 1:1 relationship between NET_BLOCK and NET_BLOCK_OP. + For example, that in NetbufGetFragment. + + @param[in, out] Nbuf Pointer to the net buffer. + @param[in] Bulk Pointer to the data. + @param[in] Len Length of the bulk data. + @param[in] Index The data block index in the net buffer the bulk + data should belong to. + +**/ +VOID +NetbufSetBlockOp ( + IN OUT NET_BUF *Nbuf, + IN UINT8 *Bulk, + IN UINT32 Len, + IN UINT32 Index + ) +{ + NET_BLOCK_OP *BlockOp; + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + ASSERT (Index < Nbuf->BlockOpNum); + + BlockOp = &(Nbuf->BlockOp[Index]); + BlockOp->BlockHead = Bulk; + BlockOp->BlockTail = Bulk + Len; + BlockOp->Head = Bulk; + BlockOp->Tail = Bulk + Len; + BlockOp->Size = Len; +} + + +/** + Helper function for NetbufGetFragment. NetbufGetFragment may allocate the + first block to reserve HeadSpace bytes header space. So it needs to create a + new net vector for the first block and can avoid copy for the remaining data + by sharing the old net vector. + + @param[in] Arg Point to the old NET_VECTOR. + +**/ +VOID +EFIAPI +NetbufGetFragmentFree ( + IN VOID *Arg + ) +{ + NET_VECTOR *Vector; + + Vector = (NET_VECTOR *)Arg; + NetbufFreeVector (Vector); +} + + +/** + Create a NET_BUF structure which contains Len byte data of Nbuf starting from + Offset. + + A new NET_BUF structure will be created but the associated data in NET_VECTOR + is shared. This function exists to do IP packet fragmentation. + + @param[in] Nbuf Pointer to the net buffer to be extracted. + @param[in] Offset Starting point of the data to be included in the new + net buffer. + @param[in] Len Bytes of data to be included in the new net buffer. + @param[in] HeadSpace Bytes of head space to reserve for protocol header. + + @return Pointer to the cloned net buffer, or NULL if the + allocation failed due to resource limit. + +**/ +NET_BUF * +EFIAPI +NetbufGetFragment ( + IN NET_BUF *Nbuf, + IN UINT32 Offset, + IN UINT32 Len, + IN UINT32 HeadSpace + ) +{ + NET_BUF *Child; + NET_VECTOR *Vector; + NET_BLOCK_OP *BlockOp; + UINT32 CurBlockOp; + UINT32 BlockOpNum; + UINT8 *FirstBulk; + UINT32 Index; + UINT32 First; + UINT32 Last; + UINT32 FirstSkip; + UINT32 FirstLen; + UINT32 LastLen; + UINT32 Cur; + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + + if ((Len == 0) || (Offset + Len > Nbuf->TotalSize)) { + return NULL; + } + + // + // First find the first and last BlockOp that contains + // the valid data, and compute the offset of the first + // BlockOp and length of the last BlockOp + // + BlockOp = Nbuf->BlockOp; + Cur = 0; + + for (Index = 0; Index < Nbuf->BlockOpNum; Index++) { + if (Offset < Cur + BlockOp[Index].Size) { + break; + } + + Cur += BlockOp[Index].Size; + } + + // + // First is the index of the first BlockOp, FirstSkip is + // the offset of the first byte in the first BlockOp. + // + First = Index; + FirstSkip = Offset - Cur; + FirstLen = BlockOp[Index].Size - FirstSkip; + + Last = 0; + LastLen = 0; + + if (Len > FirstLen) { + Cur += BlockOp[Index].Size; + Index++; + + for (; Index < Nbuf->BlockOpNum; Index++) { + if (Offset + Len <= Cur + BlockOp[Index].Size) { + Last = Index; + LastLen = Offset + Len - Cur; + break; + } + + Cur += BlockOp[Index].Size; + } + + } else { + Last = First; + LastLen = Len; + FirstLen = Len; + } + + ASSERT (Last >= First); + BlockOpNum = Last - First + 1; + CurBlockOp = 0; + + if (HeadSpace != 0) { + // + // Allocate an extra block to accomdate the head space. + // + BlockOpNum++; + + Child = NetbufAllocStruct (1, BlockOpNum); + + if (Child == NULL) { + return NULL; + } + + FirstBulk = AllocatePool (HeadSpace); + + if (FirstBulk == NULL) { + goto FreeChild; + } + + Vector = Child->Vector; + Vector->Free = NetbufGetFragmentFree; + Vector->Arg = Nbuf->Vector; + Vector->Flag = NET_VECTOR_OWN_FIRST; + Vector->Len = HeadSpace; + + // + // Reserve the head space in the first block + // + NetbufSetBlock (Child, FirstBulk, HeadSpace, 0); + Child->BlockOp[0].Head += HeadSpace; + Child->BlockOp[0].Size = 0; + CurBlockOp++; + + } else { + Child = NetbufAllocStruct (0, BlockOpNum); + + if (Child == NULL) { + return NULL; + } + + Child->Vector = Nbuf->Vector; + } + + NET_GET_REF (Nbuf->Vector); + Child->TotalSize = Len; + + // + // Set all the BlockOp up, the first and last one are special + // and need special process. + // + NetbufSetBlockOp ( + Child, + Nbuf->BlockOp[First].Head + FirstSkip, + FirstLen, + CurBlockOp++ + ); + + for (Index = First + 1; Index < Last; Index++) { + NetbufSetBlockOp ( + Child, + BlockOp[Index].Head, + BlockOp[Index].Size, + CurBlockOp++ + ); + } + + if (First != Last) { + NetbufSetBlockOp ( + Child, + BlockOp[Last].Head, + LastLen, + CurBlockOp + ); + } + + CopyMem (Child->ProtoData, Nbuf->ProtoData, NET_PROTO_DATA); + return Child; + +FreeChild: + + FreePool (Child); + return NULL; +} + + + +/** + Build a NET_BUF from external blocks. + + A new NET_BUF structure will be created from external blocks. Additional block + of memory will be allocated to hold reserved HeadSpace bytes of header room + and existing HeadLen bytes of header but the external blocks are shared by the + net buffer to avoid data copying. + + @param[in] ExtFragment Pointer to the data block. + @param[in] ExtNum The number of the data blocks. + @param[in] HeadSpace The head space to be reserved. + @param[in] HeadLen The length of the protocol header, This function + will pull that number of data into a linear block. + @param[in] ExtFree Pointer to the caller provided free function. + @param[in] Arg The argument passed to ExtFree when ExtFree is + called. + + @return Pointer to the net buffer built from the data blocks, + or NULL if the allocation failed due to resource + limit. + +**/ +NET_BUF * +EFIAPI +NetbufFromExt ( + IN NET_FRAGMENT *ExtFragment, + IN UINT32 ExtNum, + IN UINT32 HeadSpace, + IN UINT32 HeadLen, + IN NET_VECTOR_EXT_FREE ExtFree, + IN VOID *Arg OPTIONAL + ) +{ + NET_BUF *Nbuf; + NET_VECTOR *Vector; + NET_FRAGMENT SavedFragment; + UINT32 SavedIndex; + UINT32 TotalLen; + UINT32 BlockNum; + UINT8 *FirstBlock; + UINT32 FirstBlockLen; + UINT8 *Header; + UINT32 CurBlock; + UINT32 Index; + UINT32 Len; + UINT32 Copied; + + ASSERT ((ExtFragment != NULL) && (ExtNum > 0) && (ExtFree != NULL)); + + SavedFragment.Bulk = NULL; + SavedFragment.Len = 0; + + FirstBlockLen = 0; + FirstBlock = NULL; + BlockNum = ExtNum; + Index = 0; + TotalLen = 0; + SavedIndex = 0; + Len = 0; + Copied = 0; + + // + // No need to consolidate the header if the first block is + // longer than the header length or there is only one block. + // + if ((ExtFragment[0].Len >= HeadLen) || (ExtNum == 1)) { + HeadLen = 0; + } + + // + // Allocate an extra block if we need to: + // 1. Allocate some header space + // 2. aggreate the packet header + // + if ((HeadSpace != 0) || (HeadLen != 0)) { + FirstBlockLen = HeadLen + HeadSpace; + FirstBlock = AllocatePool (FirstBlockLen); + + if (FirstBlock == NULL) { + return NULL; + } + + BlockNum++; + } + + // + // Copy the header to the first block, reduce the NET_BLOCK + // to allocate by one for each block that is completely covered + // by the first bulk. + // + if (HeadLen != 0) { + Len = HeadLen; + Header = FirstBlock + HeadSpace; + + for (Index = 0; Index < ExtNum; Index++) { + if (Len >= ExtFragment[Index].Len) { + CopyMem (Header, ExtFragment[Index].Bulk, ExtFragment[Index].Len); + + Copied += ExtFragment[Index].Len; + Len -= ExtFragment[Index].Len; + Header += ExtFragment[Index].Len; + TotalLen += ExtFragment[Index].Len; + BlockNum--; + + if (Len == 0) { + // + // Increament the index number to point to the next + // non-empty fragment. + // + Index++; + break; + } + + } else { + CopyMem (Header, ExtFragment[Index].Bulk, Len); + + Copied += Len; + TotalLen += Len; + + // + // Adjust the block structure to exclude the data copied, + // So, the left-over block can be processed as other blocks. + // But it must be recovered later. (SavedIndex > 0) always + // holds since we don't aggreate the header if the first block + // is bigger enough that the header is continuous + // + SavedIndex = Index; + SavedFragment = ExtFragment[Index]; + ExtFragment[Index].Bulk += Len; + ExtFragment[Index].Len -= Len; + break; + } + } + } + + Nbuf = NetbufAllocStruct (BlockNum, BlockNum); + + if (Nbuf == NULL) { + goto FreeFirstBlock; + } + + Vector = Nbuf->Vector; + Vector->Free = ExtFree; + Vector->Arg = Arg; + Vector->Flag = ((FirstBlockLen != 0) ? NET_VECTOR_OWN_FIRST : 0); + + // + // Set the first block up which may contain + // some head space and aggregated header + // + CurBlock = 0; + + if (FirstBlockLen != 0) { + NetbufSetBlock (Nbuf, FirstBlock, HeadSpace + Copied, 0); + Nbuf->BlockOp[0].Head += HeadSpace; + Nbuf->BlockOp[0].Size = Copied; + + CurBlock++; + } + + for (; Index < ExtNum; Index++) { + NetbufSetBlock (Nbuf, ExtFragment[Index].Bulk, ExtFragment[Index].Len, CurBlock); + TotalLen += ExtFragment[Index].Len; + CurBlock++; + } + + Vector->Len = TotalLen + HeadSpace; + Nbuf->TotalSize = TotalLen; + + if (SavedIndex != 0) { + ExtFragment[SavedIndex] = SavedFragment; + } + + return Nbuf; + +FreeFirstBlock: + if (FirstBlock != NULL) { + FreePool (FirstBlock); + } + return NULL; +} + + +/** + Build a fragment table to contain the fragments in the net buffer. This is the + opposite operation of the NetbufFromExt. + + @param[in] Nbuf Point to the net buffer. + @param[in, out] ExtFragment Pointer to the data block. + @param[in, out] ExtNum The number of the data blocks. + + @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than + ExtNum. + @retval EFI_SUCCESS Fragment table is built successfully. + +**/ +EFI_STATUS +EFIAPI +NetbufBuildExt ( + IN NET_BUF *Nbuf, + IN OUT NET_FRAGMENT *ExtFragment, + IN OUT UINT32 *ExtNum + ) +{ + UINT32 Index; + UINT32 Current; + + Current = 0; + + for (Index = 0; (Index < Nbuf->BlockOpNum); Index++) { + if (Nbuf->BlockOp[Index].Size == 0) { + continue; + } + + if (Current < *ExtNum) { + ExtFragment[Current].Bulk = Nbuf->BlockOp[Index].Head; + ExtFragment[Current].Len = Nbuf->BlockOp[Index].Size; + Current++; + } else { + return EFI_BUFFER_TOO_SMALL; + } + } + + *ExtNum = Current; + return EFI_SUCCESS; +} + + +/** + Build a net buffer from a list of net buffers. + + All the fragments will be collected from the list of NEW_BUF and then a new + net buffer will be created through NetbufFromExt. + + @param[in] BufList A List of the net buffer. + @param[in] HeadSpace The head space to be reserved. + @param[in] HeaderLen The length of the protocol header, This function + will pull that number of data into a linear block. + @param[in] ExtFree Pointer to the caller provided free function. + @param[in] Arg The argument passed to ExtFree when ExtFree is called. + + @return Pointer to the net buffer built from the list of net + buffers. + +**/ +NET_BUF * +EFIAPI +NetbufFromBufList ( + IN LIST_ENTRY *BufList, + IN UINT32 HeadSpace, + IN UINT32 HeaderLen, + IN NET_VECTOR_EXT_FREE ExtFree, + IN VOID *Arg OPTIONAL + ) +{ + NET_FRAGMENT *Fragment; + UINT32 FragmentNum; + LIST_ENTRY *Entry; + NET_BUF *Nbuf; + UINT32 Index; + UINT32 Current; + + // + //Compute how many blocks are there + // + FragmentNum = 0; + + NET_LIST_FOR_EACH (Entry, BufList) { + Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + FragmentNum += Nbuf->BlockOpNum; + } + + // + //Allocate and copy block points + // + Fragment = AllocatePool (sizeof (NET_FRAGMENT) * FragmentNum); + + if (Fragment == NULL) { + return NULL; + } + + Current = 0; + + NET_LIST_FOR_EACH (Entry, BufList) { + Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + + for (Index = 0; Index < Nbuf->BlockOpNum; Index++) { + if (Nbuf->BlockOp[Index].Size != 0) { + Fragment[Current].Bulk = Nbuf->BlockOp[Index].Head; + Fragment[Current].Len = Nbuf->BlockOp[Index].Size; + Current++; + } + } + } + + Nbuf = NetbufFromExt (Fragment, Current, HeadSpace, HeaderLen, ExtFree, Arg); + FreePool (Fragment); + + return Nbuf; +} + + +/** + Reserve some space in the header room of the net buffer. + + Upon allocation, all the space are in the tail room of the buffer. Call this + function to move some space to the header room. This function is quite limited + in that it can only reserve space from the first block of an empty NET_BUF not + built from the external. But it should be enough for the network stack. + + @param[in, out] Nbuf Pointer to the net buffer. + @param[in] Len The length of buffer to be reserved from the header. + +**/ +VOID +EFIAPI +NetbufReserve ( + IN OUT NET_BUF *Nbuf, + IN UINT32 Len + ) +{ + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + NET_CHECK_SIGNATURE (Nbuf->Vector, NET_VECTOR_SIGNATURE); + + ASSERT ((Nbuf->BlockOpNum == 1) && (Nbuf->TotalSize == 0)); + ASSERT ((Nbuf->Vector->Free == NULL) && (Nbuf->Vector->Len >= Len)); + + Nbuf->BlockOp[0].Head += Len; + Nbuf->BlockOp[0].Tail += Len; + + ASSERT (Nbuf->BlockOp[0].Tail <= Nbuf->BlockOp[0].BlockTail); +} + + +/** + Allocate Len bytes of space from the header or tail of the buffer. + + @param[in, out] Nbuf Pointer to the net buffer. + @param[in] Len The length of the buffer to be allocated. + @param[in] FromHead The flag to indicate whether reserve the data + from head (TRUE) or tail (FALSE). + + @return Pointer to the first byte of the allocated buffer, + or NULL if there is no sufficient space. + +**/ +UINT8* +EFIAPI +NetbufAllocSpace ( + IN OUT NET_BUF *Nbuf, + IN UINT32 Len, + IN BOOLEAN FromHead + ) +{ + NET_BLOCK_OP *BlockOp; + UINT32 Index; + UINT8 *SavedTail; + + Index = 0; + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + NET_CHECK_SIGNATURE (Nbuf->Vector, NET_VECTOR_SIGNATURE); + + ASSERT (Len > 0); + + if (FromHead) { + // + // Allocate some space from head. If the buffer is empty, + // allocate from the first block. If it isn't, allocate + // from the first non-empty block, or the block before that. + // + if (Nbuf->TotalSize == 0) { + Index = 0; + } else { + NetbufGetByte (Nbuf, 0, &Index); + + if ((NET_HEADSPACE(&(Nbuf->BlockOp[Index])) < Len) && (Index > 0)) { + Index--; + } + } + + BlockOp = &(Nbuf->BlockOp[Index]); + + if (NET_HEADSPACE (BlockOp) < Len) { + return NULL; + } + + BlockOp->Head -= Len; + BlockOp->Size += Len; + Nbuf->TotalSize += Len; + + return BlockOp->Head; + + } else { + // + // Allocate some space from the tail. If the buffer is empty, + // allocate from the first block. If it isn't, allocate + // from the last non-empty block, or the block after that. + // + if (Nbuf->TotalSize == 0) { + Index = 0; + } else { + NetbufGetByte (Nbuf, Nbuf->TotalSize - 1, &Index); + + if ((NET_TAILSPACE(&(Nbuf->BlockOp[Index])) < Len) && + (Index < Nbuf->BlockOpNum - 1)) { + + Index++; + } + } + + BlockOp = &(Nbuf->BlockOp[Index]); + + if (NET_TAILSPACE (BlockOp) < Len) { + return NULL; + } + + SavedTail = BlockOp->Tail; + + BlockOp->Tail += Len; + BlockOp->Size += Len; + Nbuf->TotalSize += Len; + + return SavedTail; + } +} + + +/** + Trim a single NET_BLOCK by Len bytes from the header or tail. + + @param[in, out] BlockOp Pointer to the NET_BLOCK. + @param[in] Len The length of the data to be trimmed. + @param[in] FromHead The flag to indicate whether trim data from head + (TRUE) or tail (FALSE). + +**/ +VOID +NetblockTrim ( + IN OUT NET_BLOCK_OP *BlockOp, + IN UINT32 Len, + IN BOOLEAN FromHead + ) +{ + ASSERT ((BlockOp != NULL) && (BlockOp->Size >= Len)); + + BlockOp->Size -= Len; + + if (FromHead) { + BlockOp->Head += Len; + } else { + BlockOp->Tail -= Len; + } +} + + +/** + Trim Len bytes from the header or tail of the net buffer. + + @param[in, out] Nbuf Pointer to the net buffer. + @param[in] Len The length of the data to be trimmed. + @param[in] FromHead The flag to indicate whether trim data from head + (TRUE) or tail (FALSE). + + @return Length of the actually trimmed data, which is possible to be less + than Len because the TotalSize of Nbuf is less than Len. + +**/ +UINT32 +EFIAPI +NetbufTrim ( + IN OUT NET_BUF *Nbuf, + IN UINT32 Len, + IN BOOLEAN FromHead + ) +{ + NET_BLOCK_OP *BlockOp; + UINT32 Index; + UINT32 Trimmed; + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + + if (Len == 0 || Nbuf->TotalSize == 0) { + return 0; + } + + if (Len > Nbuf->TotalSize) { + Len = Nbuf->TotalSize; + } + + // + // If FromTail is true, iterate backward. That + // is, init Index to NBuf->BlockNum - 1, and + // decrease it by 1 during each loop. Otherwise, + // iterate forward. That is, init Index to 0, and + // increase it by 1 during each loop. + // + Trimmed = 0; + Nbuf->TotalSize -= Len; + + Index = (FromHead ? 0 : Nbuf->BlockOpNum - 1); + BlockOp = Nbuf->BlockOp; + + for (;;) { + if (BlockOp[Index].Size == 0) { + Index += (FromHead ? 1 : -1); + continue; + } + + if (Len > BlockOp[Index].Size) { + Len -= BlockOp[Index].Size; + Trimmed += BlockOp[Index].Size; + NetblockTrim (&BlockOp[Index], BlockOp[Index].Size, FromHead); + } else { + Trimmed += Len; + NetblockTrim (&BlockOp[Index], Len, FromHead); + break; + } + + Index += (FromHead ? 1 : -1); + } + + return Trimmed; +} + + +/** + Copy Len bytes of data from the specific offset of the net buffer to the + destination memory. + + The Len bytes of data may cross the several fragments of the net buffer. + + @param[in] Nbuf Pointer to the net buffer. + @param[in] Offset The sequence number of the first byte to copy. + @param[in] Len Length of the data to copy. + @param[in] Dest The destination of the data to copy to. + + @return The length of the actual copied data, or 0 if the offset + specified exceeds the total size of net buffer. + +**/ +UINT32 +EFIAPI +NetbufCopy ( + IN NET_BUF *Nbuf, + IN UINT32 Offset, + IN UINT32 Len, + IN UINT8 *Dest + ) +{ + NET_BLOCK_OP *BlockOp; + UINT32 Skip; + UINT32 Left; + UINT32 Copied; + UINT32 Index; + UINT32 Cur; + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + ASSERT (Dest); + + if ((Len == 0) || (Nbuf->TotalSize <= Offset)) { + return 0; + } + + if (Nbuf->TotalSize - Offset < Len) { + Len = Nbuf->TotalSize - Offset; + } + + BlockOp = Nbuf->BlockOp; + + // + // Skip to the offset. Don't make "Offset-By-One" error here. + // Cur + BLOCK.SIZE is the first sequence number of next block. + // So, (Offset < Cur + BLOCK.SIZE) means that the first byte + // is in the current block. if (Offset == Cur + BLOCK.SIZE), the + // first byte is the next block's first byte. + // + Cur = 0; + + for (Index = 0; Index < Nbuf->BlockOpNum; Index++) { + if (BlockOp[Index].Size == 0) { + continue; + } + + if (Offset < Cur + BlockOp[Index].Size) { + break; + } + + Cur += BlockOp[Index].Size; + } + + // + // Cur is the sequence number of the first byte in the block + // Offset - Cur is the number of bytes before first byte to + // to copy in the current block. + // + Skip = Offset - Cur; + Left = BlockOp[Index].Size - Skip; + + if (Len <= Left) { + CopyMem (Dest, BlockOp[Index].Head + Skip, Len); + return Len; + } + + CopyMem (Dest, BlockOp[Index].Head + Skip, Left); + + Dest += Left; + Len -= Left; + Copied = Left; + + Index++; + + for (; Index < Nbuf->BlockOpNum; Index++) { + if (Len > BlockOp[Index].Size) { + Len -= BlockOp[Index].Size; + Copied += BlockOp[Index].Size; + + CopyMem (Dest, BlockOp[Index].Head, BlockOp[Index].Size); + Dest += BlockOp[Index].Size; + } else { + Copied += Len; + CopyMem (Dest, BlockOp[Index].Head, Len); + break; + } + } + + return Copied; +} + + +/** + Initiate the net buffer queue. + + @param[in, out] NbufQue Pointer to the net buffer queue to be initialized. + +**/ +VOID +EFIAPI +NetbufQueInit ( + IN OUT NET_BUF_QUEUE *NbufQue + ) +{ + NbufQue->Signature = NET_QUE_SIGNATURE; + NbufQue->RefCnt = 1; + InitializeListHead (&NbufQue->List); + + InitializeListHead (&NbufQue->BufList); + NbufQue->BufSize = 0; + NbufQue->BufNum = 0; +} + + +/** + Allocate and initialize a net buffer queue. + + @return Pointer to the allocated net buffer queue, or NULL if the + allocation failed due to resource limit. + +**/ +NET_BUF_QUEUE * +EFIAPI +NetbufQueAlloc ( + VOID + ) +{ + NET_BUF_QUEUE *NbufQue; + + NbufQue = AllocatePool (sizeof (NET_BUF_QUEUE)); + if (NbufQue == NULL) { + return NULL; + } + + NetbufQueInit (NbufQue); + + return NbufQue; +} + + +/** + Free a net buffer queue. + + Decrease the reference count of the net buffer queue by one. The real resource + free operation isn't performed until the reference count of the net buffer + queue is decreased to 0. + + @param[in] NbufQue Pointer to the net buffer queue to be freed. + +**/ +VOID +EFIAPI +NetbufQueFree ( + IN NET_BUF_QUEUE *NbufQue + ) +{ + ASSERT (NbufQue != NULL); + NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE); + + NbufQue->RefCnt--; + + if (NbufQue->RefCnt == 0) { + NetbufQueFlush (NbufQue); + FreePool (NbufQue); + } +} + + +/** + Append a net buffer to the net buffer queue. + + @param[in, out] NbufQue Pointer to the net buffer queue. + @param[in, out] Nbuf Pointer to the net buffer to be appended. + +**/ +VOID +EFIAPI +NetbufQueAppend ( + IN OUT NET_BUF_QUEUE *NbufQue, + IN OUT NET_BUF *Nbuf + ) +{ + NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE); + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + + InsertTailList (&NbufQue->BufList, &Nbuf->List); + + NbufQue->BufSize += Nbuf->TotalSize; + NbufQue->BufNum++; +} + + +/** + Remove a net buffer from the head in the specific queue and return it. + + @param[in, out] NbufQue Pointer to the net buffer queue. + + @return Pointer to the net buffer removed from the specific queue, + or NULL if there is no net buffer in the specific queue. + +**/ +NET_BUF * +EFIAPI +NetbufQueRemove ( + IN OUT NET_BUF_QUEUE *NbufQue + ) +{ + NET_BUF *First; + + NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE); + + if (NbufQue->BufNum == 0) { + return NULL; + } + + First = NET_LIST_USER_STRUCT (NbufQue->BufList.ForwardLink, NET_BUF, List); + + NetListRemoveHead (&NbufQue->BufList); + + NbufQue->BufSize -= First->TotalSize; + NbufQue->BufNum--; + return First; +} + + +/** + Copy Len bytes of data from the net buffer queue at the specific offset to the + destination memory. + + The copying operation is the same as NetbufCopy but applies to the net buffer + queue instead of the net buffer. + + @param[in] NbufQue Pointer to the net buffer queue. + @param[in] Offset The sequence number of the first byte to copy. + @param[in] Len Length of the data to copy. + @param[out] Dest The destination of the data to copy to. + + @return The length of the actual copied data, or 0 if the offset + specified exceeds the total size of net buffer queue. + +**/ +UINT32 +EFIAPI +NetbufQueCopy ( + IN NET_BUF_QUEUE *NbufQue, + IN UINT32 Offset, + IN UINT32 Len, + OUT UINT8 *Dest + ) +{ + LIST_ENTRY *Entry; + NET_BUF *Nbuf; + UINT32 Skip; + UINT32 Left; + UINT32 Cur; + UINT32 Copied; + + NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE); + ASSERT (Dest != NULL); + + if ((Len == 0) || (NbufQue->BufSize <= Offset)) { + return 0; + } + + if (NbufQue->BufSize - Offset < Len) { + Len = NbufQue->BufSize - Offset; + } + + // + // skip to the Offset + // + Cur = 0; + Nbuf = NULL; + + NET_LIST_FOR_EACH (Entry, &NbufQue->BufList) { + Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + + if (Offset < Cur + Nbuf->TotalSize) { + break; + } + + Cur += Nbuf->TotalSize; + } + + ASSERT (Nbuf != NULL); + + // + // Copy the data in the first buffer. + // + Skip = Offset - Cur; + Left = Nbuf->TotalSize - Skip; + + if (Len < Left) { + return NetbufCopy (Nbuf, Skip, Len, Dest); + } + + NetbufCopy (Nbuf, Skip, Left, Dest); + Dest += Left; + Len -= Left; + Copied = Left; + + // + // Iterate over the others + // + Entry = Entry->ForwardLink; + + while ((Len > 0) && (Entry != &NbufQue->BufList)) { + Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + + if (Len > Nbuf->TotalSize) { + Len -= Nbuf->TotalSize; + Copied += Nbuf->TotalSize; + + NetbufCopy (Nbuf, 0, Nbuf->TotalSize, Dest); + Dest += Nbuf->TotalSize; + + } else { + NetbufCopy (Nbuf, 0, Len, Dest); + Copied += Len; + break; + } + + Entry = Entry->ForwardLink; + } + + return Copied; +} + + +/** + Trim Len bytes of data from the buffer queue and free any net buffer + that is completely trimmed. + + The trimming operation is the same as NetbufTrim but applies to the net buffer + queue instead of the net buffer. + + @param[in, out] NbufQue Pointer to the net buffer queue. + @param[in] Len Length of the data to trim. + + @return The actual length of the data trimmed. + +**/ +UINT32 +EFIAPI +NetbufQueTrim ( + IN OUT NET_BUF_QUEUE *NbufQue, + IN UINT32 Len + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + NET_BUF *Nbuf; + UINT32 Trimmed; + + NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE); + + if (Len == 0) { + return 0; + } + + if (Len > NbufQue->BufSize) { + Len = NbufQue->BufSize; + } + + NbufQue->BufSize -= Len; + Trimmed = 0; + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &NbufQue->BufList) { + Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + + if (Len >= Nbuf->TotalSize) { + Trimmed += Nbuf->TotalSize; + Len -= Nbuf->TotalSize; + + RemoveEntryList (Entry); + NetbufFree (Nbuf); + + NbufQue->BufNum--; + + if (Len == 0) { + break; + } + + } else { + Trimmed += NetbufTrim (Nbuf, Len, NET_BUF_HEAD); + break; + } + } + + return Trimmed; +} + + +/** + Flush the net buffer queue. + + @param[in, out] NbufQue Pointer to the queue to be flushed. + +**/ +VOID +EFIAPI +NetbufQueFlush ( + IN OUT NET_BUF_QUEUE *NbufQue + ) +{ + NET_CHECK_SIGNATURE (NbufQue, NET_QUE_SIGNATURE); + + NetbufFreeList (&NbufQue->BufList); + + NbufQue->BufNum = 0; + NbufQue->BufSize = 0; +} + + +/** + Compute the checksum for a bulk of data. + + @param[in] Bulk Pointer to the data. + @param[in] Len Length of the data, in bytes. + + @return The computed checksum. + +**/ +UINT16 +EFIAPI +NetblockChecksum ( + IN UINT8 *Bulk, + IN UINT32 Len + ) +{ + register UINT32 Sum; + + Sum = 0; + + // + // Add left-over byte, if any + // + if (Len % 2 != 0) { + Sum += *(Bulk + Len - 1); + } + + while (Len > 1) { + Sum += *(UINT16 *) Bulk; + Bulk += 2; + Len -= 2; + } + + // + // Fold 32-bit sum to 16 bits + // + while ((Sum >> 16) != 0) { + Sum = (Sum & 0xffff) + (Sum >> 16); + + } + + return (UINT16) Sum; +} + + +/** + Add two checksums. + + @param[in] Checksum1 The first checksum to be added. + @param[in] Checksum2 The second checksum to be added. + + @return The new checksum. + +**/ +UINT16 +EFIAPI +NetAddChecksum ( + IN UINT16 Checksum1, + IN UINT16 Checksum2 + ) +{ + UINT32 Sum; + + Sum = Checksum1 + Checksum2; + + // + // two UINT16 can only add up to a carry of 1. + // + if ((Sum >> 16) != 0) { + Sum = (Sum & 0xffff) + 1; + + } + + return (UINT16) Sum; +} + + +/** + Compute the checksum for a NET_BUF. + + @param[in] Nbuf Pointer to the net buffer. + + @return The computed checksum. + +**/ +UINT16 +EFIAPI +NetbufChecksum ( + IN NET_BUF *Nbuf + ) +{ + NET_BLOCK_OP *BlockOp; + UINT32 Offset; + UINT16 TotalSum; + UINT16 BlockSum; + UINT32 Index; + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + + TotalSum = 0; + Offset = 0; + BlockOp = Nbuf->BlockOp; + + for (Index = 0; Index < Nbuf->BlockOpNum; Index++) { + if (BlockOp[Index].Size == 0) { + continue; + } + + BlockSum = NetblockChecksum (BlockOp[Index].Head, BlockOp[Index].Size); + + if ((Offset & 0x01) != 0) { + // + // The checksum starts with an odd byte, swap + // the checksum before added to total checksum + // + BlockSum = SwapBytes16 (BlockSum); + } + + TotalSum = NetAddChecksum (BlockSum, TotalSum); + Offset += BlockOp[Index].Size; + } + + return TotalSum; +} + + +/** + Compute the checksum for TCP/UDP pseudo header. + + Src and Dst are in network byte order, and Len is in host byte order. + + @param[in] Src The source address of the packet. + @param[in] Dst The destination address of the packet. + @param[in] Proto The protocol type of the packet. + @param[in] Len The length of the packet. + + @return The computed checksum. + +**/ +UINT16 +EFIAPI +NetPseudoHeadChecksum ( + IN IP4_ADDR Src, + IN IP4_ADDR Dst, + IN UINT8 Proto, + IN UINT16 Len + ) +{ + NET_PSEUDO_HDR Hdr; + + // + // Zero the memory to relieve align problems + // + ZeroMem (&Hdr, sizeof (Hdr)); + + Hdr.SrcIp = Src; + Hdr.DstIp = Dst; + Hdr.Protocol = Proto; + Hdr.Len = HTONS (Len); + + return NetblockChecksum ((UINT8 *) &Hdr, sizeof (Hdr)); +} + +/** + Compute the checksum for TCP6/UDP6 pseudo header. + + Src and Dst are in network byte order, and Len is in host byte order. + + @param[in] Src The source address of the packet. + @param[in] Dst The destination address of the packet. + @param[in] NextHeader The protocol type of the packet. + @param[in] Len The length of the packet. + + @return The computed checksum. + +**/ +UINT16 +EFIAPI +NetIp6PseudoHeadChecksum ( + IN EFI_IPv6_ADDRESS *Src, + IN EFI_IPv6_ADDRESS *Dst, + IN UINT8 NextHeader, + IN UINT32 Len + ) +{ + NET_IP6_PSEUDO_HDR Hdr; + + // + // Zero the memory to relieve align problems + // + ZeroMem (&Hdr, sizeof (Hdr)); + + IP6_COPY_ADDRESS (&Hdr.SrcIp, Src); + IP6_COPY_ADDRESS (&Hdr.DstIp, Dst); + + Hdr.NextHeader = NextHeader; + Hdr.Len = HTONL (Len); + + return NetblockChecksum ((UINT8 *) &Hdr, sizeof (Hdr)); +} + +/** + The function frees the net buffer which allocated by the IP protocol. It releases + only the net buffer and doesn't call the external free function. + + This function should be called after finishing the process of mIpSec->ProcessExt() + for outbound traffic. The (EFI_IPSEC2_PROTOCOL)->ProcessExt() allocates a new + buffer for the ESP, so there needs a function to free the old net buffer. + + @param[in] Nbuf The network buffer to be freed. + +**/ +VOID +NetIpSecNetbufFree ( + NET_BUF *Nbuf + ) +{ + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + ASSERT (Nbuf->RefCnt > 0); + + Nbuf->RefCnt--; + + if (Nbuf->RefCnt == 0) { + + // + // Update Vector only when NBuf is to be released. That is, + // all the sharing of Nbuf increse Vector's RefCnt by one + // + NET_CHECK_SIGNATURE (Nbuf->Vector, NET_VECTOR_SIGNATURE); + ASSERT (Nbuf->Vector->RefCnt > 0); + + Nbuf->Vector->RefCnt--; + + if (Nbuf->Vector->RefCnt > 0) { + return; + } + + // + // If NET_VECTOR_OWN_FIRST is set, release the first block since it is + // allocated by us + // + if ((Nbuf->Vector->Flag & NET_VECTOR_OWN_FIRST) != 0) { + FreePool (Nbuf->Vector->Block[0].Bulk); + } + FreePool (Nbuf->Vector); + FreePool (Nbuf); + } +} + diff --git a/NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.c b/NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.c new file mode 100644 index 000000000..341295d0b --- /dev/null +++ b/NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.c @@ -0,0 +1,1011 @@ +/** @file + This library is used to share code between UEFI network stack modules. + It provides the helper routines to access TCP service. + +Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include +#include +#include +#include +#include +#include + +/** + The common notify function associated with various TcpIo events. + + @param[in] Event The event signaled. + @param[in] Context The context. + +**/ +VOID +EFIAPI +TcpIoCommonNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + if ((Event == NULL) || (Context == NULL)) { + return ; + } + + *((BOOLEAN *) Context) = TRUE; +} + +/** + The internal function for delay configuring TCP6 when IP6 driver is still in DAD. + + @param[in] Tcp6 The EFI_TCP6_PROTOCOL protocol instance. + @param[in] Tcp6ConfigData The Tcp6 configuration data. + + @retval EFI_SUCCESS The operational settings successfully + completed. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval Others Failed to finish the operation. + +**/ +EFI_STATUS +TcpIoGetMapping ( + IN EFI_TCP6_PROTOCOL *Tcp6, + IN EFI_TCP6_CONFIG_DATA *Tcp6ConfigData + ) +{ + EFI_STATUS Status; + EFI_EVENT Event; + + if ((Tcp6 == NULL) || (Tcp6ConfigData == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Event = NULL; + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &Event + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = gBS->SetTimer ( + Event, + TimerRelative, + TCP_GET_MAPPING_TIMEOUT + ); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + while (EFI_ERROR (gBS->CheckEvent (Event))) { + + Tcp6->Poll (Tcp6); + + Status = Tcp6->Configure (Tcp6, Tcp6ConfigData); + + if (!EFI_ERROR (Status)) { + break; + } + } + +ON_EXIT: + + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + return Status; +} + +/** + Create a TCP socket with the specified configuration data. + + @param[in] Image The handle of the driver image. + @param[in] Controller The handle of the controller. + @param[in] TcpVersion The version of Tcp, TCP_VERSION_4 or TCP_VERSION_6. + @param[in] ConfigData The Tcp configuration data. + @param[out] TcpIo The TcpIo. + + @retval EFI_SUCCESS The TCP socket is created and configured. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Others Failed to create the TCP socket or configure it. + +**/ +EFI_STATUS +EFIAPI +TcpIoCreateSocket ( + IN EFI_HANDLE Image, + IN EFI_HANDLE Controller, + IN UINT8 TcpVersion, + IN TCP_IO_CONFIG_DATA *ConfigData, + OUT TCP_IO *TcpIo + ) +{ + EFI_STATUS Status; + EFI_EVENT Event; + EFI_GUID *ServiceBindingGuid; + EFI_GUID *ProtocolGuid; + VOID **Interface; + EFI_TCP4_OPTION ControlOption; + EFI_TCP4_CONFIG_DATA Tcp4ConfigData; + EFI_TCP4_ACCESS_POINT *AccessPoint4; + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP6_CONFIG_DATA Tcp6ConfigData; + EFI_TCP6_ACCESS_POINT *AccessPoint6; + EFI_TCP6_PROTOCOL *Tcp6; + EFI_TCP4_RECEIVE_DATA *RxData; + + if ((Image == NULL) || (Controller == NULL) || (ConfigData == NULL) || (TcpIo == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Tcp4 = NULL; + Tcp6 = NULL; + + ZeroMem (TcpIo, sizeof (TCP_IO)); + + if (TcpVersion == TCP_VERSION_4) { + ServiceBindingGuid = &gEfiTcp4ServiceBindingProtocolGuid; + ProtocolGuid = &gEfiTcp4ProtocolGuid; + Interface = (VOID **) (&TcpIo->Tcp.Tcp4); + } else if (TcpVersion == TCP_VERSION_6) { + ServiceBindingGuid = &gEfiTcp6ServiceBindingProtocolGuid; + ProtocolGuid = &gEfiTcp6ProtocolGuid; + Interface = (VOID **) (&TcpIo->Tcp.Tcp6); + } else { + return EFI_UNSUPPORTED; + } + + TcpIo->TcpVersion = TcpVersion; + + // + // Create the TCP child instance and get the TCP protocol. + // + Status = NetLibCreateServiceChild ( + Controller, + Image, + ServiceBindingGuid, + &TcpIo->Handle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + TcpIo->Handle, + ProtocolGuid, + Interface, + Image, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status) || (*Interface == NULL)) { + goto ON_ERROR; + } + + if (TcpVersion == TCP_VERSION_4) { + Tcp4 = TcpIo->Tcp.Tcp4; + } else { + Tcp6 = TcpIo->Tcp.Tcp6; + } + + TcpIo->Image = Image; + TcpIo->Controller = Controller; + + // + // Set the configuration parameters. + // + ControlOption.ReceiveBufferSize = 0x200000; + ControlOption.SendBufferSize = 0x200000; + ControlOption.MaxSynBackLog = 0; + ControlOption.ConnectionTimeout = 0; + ControlOption.DataRetries = 6; + ControlOption.FinTimeout = 0; + ControlOption.TimeWaitTimeout = 0; + ControlOption.KeepAliveProbes = 4; + ControlOption.KeepAliveTime = 0; + ControlOption.KeepAliveInterval = 0; + ControlOption.EnableNagle = FALSE; + ControlOption.EnableTimeStamp = FALSE; + ControlOption.EnableWindowScaling = TRUE; + ControlOption.EnableSelectiveAck = FALSE; + ControlOption.EnablePathMtuDiscovery = FALSE; + + if (TcpVersion == TCP_VERSION_4) { + Tcp4ConfigData.TypeOfService = 8; + Tcp4ConfigData.TimeToLive = 255; + Tcp4ConfigData.ControlOption = &ControlOption; + + AccessPoint4 = &Tcp4ConfigData.AccessPoint; + + ZeroMem (AccessPoint4, sizeof (EFI_TCP4_ACCESS_POINT)); + AccessPoint4->StationPort = ConfigData->Tcp4IoConfigData.StationPort; + AccessPoint4->RemotePort = ConfigData->Tcp4IoConfigData.RemotePort; + AccessPoint4->ActiveFlag = ConfigData->Tcp4IoConfigData.ActiveFlag; + + CopyMem ( + &AccessPoint4->StationAddress, + &ConfigData->Tcp4IoConfigData.LocalIp, + sizeof (EFI_IPv4_ADDRESS) + ); + CopyMem ( + &AccessPoint4->SubnetMask, + &ConfigData->Tcp4IoConfigData.SubnetMask, + sizeof (EFI_IPv4_ADDRESS) + ); + CopyMem ( + &AccessPoint4->RemoteAddress, + &ConfigData->Tcp4IoConfigData.RemoteIp, + sizeof (EFI_IPv4_ADDRESS) + ); + + ASSERT (Tcp4 != NULL); + + // + // Configure the TCP4 protocol. + // + Status = Tcp4->Configure (Tcp4, &Tcp4ConfigData); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + if (!EFI_IP4_EQUAL (&ConfigData->Tcp4IoConfigData.Gateway, &mZeroIp4Addr)) { + // + // The gateway is not zero. Add the default route manually. + // + Status = Tcp4->Routes ( + Tcp4, + FALSE, + &mZeroIp4Addr, + &mZeroIp4Addr, + &ConfigData->Tcp4IoConfigData.Gateway + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + } + } else { + Tcp6ConfigData.TrafficClass = 0; + Tcp6ConfigData.HopLimit = 255; + Tcp6ConfigData.ControlOption = (EFI_TCP6_OPTION *) &ControlOption; + + AccessPoint6 = &Tcp6ConfigData.AccessPoint; + + ZeroMem (AccessPoint6, sizeof (EFI_TCP6_ACCESS_POINT)); + AccessPoint6->StationPort = ConfigData->Tcp6IoConfigData.StationPort; + AccessPoint6->RemotePort = ConfigData->Tcp6IoConfigData.RemotePort; + AccessPoint6->ActiveFlag = ConfigData->Tcp6IoConfigData.ActiveFlag; + + IP6_COPY_ADDRESS (&AccessPoint6->RemoteAddress, &ConfigData->Tcp6IoConfigData.RemoteIp); + + + ASSERT (Tcp6 != NULL); + // + // Configure the TCP6 protocol. + // + Status = Tcp6->Configure (Tcp6, &Tcp6ConfigData); + if (Status == EFI_NO_MAPPING) { + Status = TcpIoGetMapping (Tcp6, &Tcp6ConfigData); + } + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + } + + // + // Create events for variuos asynchronous operations. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + TcpIoCommonNotify, + &TcpIo->IsConnDone, + &Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + TcpIo->ConnToken.Tcp4Token.CompletionToken.Event = Event; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + TcpIoCommonNotify, + &TcpIo->IsListenDone, + &Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + TcpIo->ListenToken.Tcp4Token.CompletionToken.Event = Event; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + TcpIoCommonNotify, + &TcpIo->IsTxDone, + &Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + TcpIo->TxToken.Tcp4Token.CompletionToken.Event = Event; + + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + TcpIoCommonNotify, + &TcpIo->IsRxDone, + &Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + TcpIo->RxToken.Tcp4Token.CompletionToken.Event = Event; + + RxData = (EFI_TCP4_RECEIVE_DATA *) AllocateZeroPool (sizeof (EFI_TCP4_RECEIVE_DATA)); + if (RxData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + TcpIo->RxToken.Tcp4Token.Packet.RxData = RxData; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + TcpIoCommonNotify, + &TcpIo->IsCloseDone, + &Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + TcpIo->CloseToken.Tcp4Token.CompletionToken.Event = Event; + + + return EFI_SUCCESS; + +ON_ERROR: + + TcpIoDestroySocket (TcpIo); + + return Status; +} + +/** + Destroy the socket. + + @param[in] TcpIo The TcpIo which wraps the socket to be destroyed. + +**/ +VOID +EFIAPI +TcpIoDestroySocket ( + IN TCP_IO *TcpIo + ) +{ + EFI_EVENT Event; + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP6_PROTOCOL *Tcp6; + UINT8 TcpVersion; + EFI_GUID *ServiceBindingGuid; + EFI_GUID *ProtocolGuid; + EFI_HANDLE ChildHandle; + + if (TcpIo == NULL) { + return ; + } + + TcpVersion = TcpIo->TcpVersion; + + if ((TcpVersion != TCP_VERSION_4) && (TcpVersion != TCP_VERSION_6)) { + return ; + } + + Event = TcpIo->ConnToken.Tcp4Token.CompletionToken.Event; + + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + Event = TcpIo->ListenToken.Tcp4Token.CompletionToken.Event; + + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + Event = TcpIo->TxToken.Tcp4Token.CompletionToken.Event; + + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + Event = TcpIo->RxToken.Tcp4Token.CompletionToken.Event; + + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + Event = TcpIo->CloseToken.Tcp4Token.CompletionToken.Event; + + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + if (TcpIo->RxToken.Tcp4Token.Packet.RxData != NULL) { + FreePool (TcpIo->RxToken.Tcp4Token.Packet.RxData); + } + + Tcp4 = NULL; + Tcp6 = NULL; + + + if (TcpVersion == TCP_VERSION_4) { + ServiceBindingGuid = &gEfiTcp4ServiceBindingProtocolGuid; + ProtocolGuid = &gEfiTcp4ProtocolGuid; + Tcp4 = TcpIo->Tcp.Tcp4; + if (Tcp4 != NULL) { + Tcp4->Configure (Tcp4, NULL); + } + } else { + ServiceBindingGuid = &gEfiTcp6ServiceBindingProtocolGuid; + ProtocolGuid = &gEfiTcp6ProtocolGuid; + Tcp6 = TcpIo->Tcp.Tcp6; + if (Tcp6 != NULL) { + Tcp6->Configure (Tcp6, NULL); + } + } + + if ((Tcp4 != NULL) || (Tcp6 != NULL)) { + + gBS->CloseProtocol ( + TcpIo->Handle, + ProtocolGuid, + TcpIo->Image, + TcpIo->Controller + ); + } + + ChildHandle = NULL; + + if (TcpIo->IsListenDone) { + if (TcpVersion == TCP_VERSION_4) { + Tcp4 = TcpIo->NewTcp.Tcp4; + if (Tcp4 != NULL) { + Tcp4->Configure (Tcp4, NULL); + ChildHandle = TcpIo->ListenToken.Tcp4Token.NewChildHandle; + } + } else { + Tcp6 = TcpIo->NewTcp.Tcp6; + if (Tcp6 != NULL) { + Tcp6->Configure (Tcp6, NULL); + ChildHandle = TcpIo->ListenToken.Tcp6Token.NewChildHandle; + } + } + + if (ChildHandle != NULL) { + + gBS->CloseProtocol ( + ChildHandle, + ProtocolGuid, + TcpIo->Image, + TcpIo->Controller + ); + } + } + + NetLibDestroyServiceChild ( + TcpIo->Controller, + TcpIo->Image, + ServiceBindingGuid, + TcpIo->Handle + ); +} + +/** + Connect to the other endpoint of the TCP socket. + + @param[in, out] TcpIo The TcpIo wrapping the TCP socket. + @param[in] Timeout The time to wait for connection done. Set to NULL for infinite wait. + + @retval EFI_SUCCESS Connect to the other endpoint of the TCP socket + successfully. + @retval EFI_TIMEOUT Failed to connect to the other endpoint of the + TCP socket in the specified time period. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TcpIoConnect ( + IN OUT TCP_IO *TcpIo, + IN EFI_EVENT Timeout OPTIONAL + ) +{ + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP6_PROTOCOL *Tcp6; + EFI_STATUS Status; + + if ((TcpIo == NULL) || (TcpIo->Tcp.Tcp4 == NULL)) { + return EFI_INVALID_PARAMETER; + } + + TcpIo->IsConnDone = FALSE; + + Tcp4 = NULL; + Tcp6 = NULL; + + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Tcp4 = TcpIo->Tcp.Tcp4; + Status = Tcp4->Connect (Tcp4, &TcpIo->ConnToken.Tcp4Token); + } else if (TcpIo->TcpVersion == TCP_VERSION_6) { + Tcp6 = TcpIo->Tcp.Tcp6; + Status = Tcp6->Connect (Tcp6, &TcpIo->ConnToken.Tcp6Token); + } else { + return EFI_UNSUPPORTED; + } + + if (EFI_ERROR (Status)) { + return Status; + } + + while (!TcpIo->IsConnDone && ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) { + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Tcp4->Poll (Tcp4); + } else { + Tcp6->Poll (Tcp6); + } + } + + if (!TcpIo->IsConnDone) { + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Tcp4->Cancel (Tcp4, &TcpIo->ConnToken.Tcp4Token.CompletionToken); + } else { + Tcp6->Cancel (Tcp6, &TcpIo->ConnToken.Tcp6Token.CompletionToken); + } + Status = EFI_TIMEOUT; + } else { + Status = TcpIo->ConnToken.Tcp4Token.CompletionToken.Status; + } + + return Status; +} + +/** + Accept the incomding request from the other endpoint of the TCP socket. + + @param[in, out] TcpIo The TcpIo wrapping the TCP socket. + @param[in] Timeout The time to wait for connection done. Set to NULL for infinite wait. + + + @retval EFI_SUCCESS Connect to the other endpoint of the TCP socket + successfully. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + + @retval EFI_TIMEOUT Failed to connect to the other endpoint of the + TCP socket in the specified time period. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TcpIoAccept ( + IN OUT TCP_IO *TcpIo, + IN EFI_EVENT Timeout OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_GUID *ProtocolGuid; + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP6_PROTOCOL *Tcp6; + + if ((TcpIo == NULL) || (TcpIo->Tcp.Tcp4 == NULL)) { + return EFI_INVALID_PARAMETER; + } + + TcpIo->IsListenDone = FALSE; + + Tcp4 = NULL; + Tcp6 = NULL; + + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Tcp4 = TcpIo->Tcp.Tcp4; + Status = Tcp4->Accept (Tcp4, &TcpIo->ListenToken.Tcp4Token); + } else if (TcpIo->TcpVersion == TCP_VERSION_6) { + Tcp6 = TcpIo->Tcp.Tcp6; + Status = Tcp6->Accept (Tcp6, &TcpIo->ListenToken.Tcp6Token); + } else { + return EFI_UNSUPPORTED; + } + + if (EFI_ERROR (Status)) { + return Status; + } + + while (!TcpIo->IsListenDone && ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) { + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Tcp4->Poll (Tcp4); + } else { + Tcp6->Poll (Tcp6); + } + } + + if (!TcpIo->IsListenDone) { + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Tcp4->Cancel (Tcp4, &TcpIo->ListenToken.Tcp4Token.CompletionToken); + } else { + Tcp6->Cancel (Tcp6, &TcpIo->ListenToken.Tcp6Token.CompletionToken); + } + Status = EFI_TIMEOUT; + } else { + Status = TcpIo->ListenToken.Tcp4Token.CompletionToken.Status; + } + + // + // The new TCP instance handle created for the established connection is + // in ListenToken. + // + if (!EFI_ERROR (Status)) { + if (TcpIo->TcpVersion == TCP_VERSION_4) { + ProtocolGuid = &gEfiTcp4ProtocolGuid; + } else { + ProtocolGuid = &gEfiTcp6ProtocolGuid; + } + + Status = gBS->OpenProtocol ( + TcpIo->ListenToken.Tcp4Token.NewChildHandle, + ProtocolGuid, + (VOID **) (&TcpIo->NewTcp.Tcp4), + TcpIo->Image, + TcpIo->Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + } + + return Status; +} + +/** + Reset the socket. + + @param[in, out] TcpIo The TcpIo wrapping the TCP socket. + +**/ +VOID +EFIAPI +TcpIoReset ( + IN OUT TCP_IO *TcpIo + ) +{ + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP6_PROTOCOL *Tcp6; + EFI_STATUS Status; + + if ((TcpIo == NULL) || (TcpIo->Tcp.Tcp4 == NULL)) { + return ; + } + + TcpIo->IsCloseDone = FALSE; + Tcp4 = NULL; + Tcp6 = NULL; + + if (TcpIo->TcpVersion == TCP_VERSION_4) { + TcpIo->CloseToken.Tcp4Token.AbortOnClose = TRUE; + Tcp4 = TcpIo->Tcp.Tcp4; + Status = Tcp4->Close (Tcp4, &TcpIo->CloseToken.Tcp4Token); + } else if (TcpIo->TcpVersion == TCP_VERSION_6) { + TcpIo->CloseToken.Tcp6Token.AbortOnClose = TRUE; + Tcp6 = TcpIo->Tcp.Tcp6; + Status = Tcp6->Close (Tcp6, &TcpIo->CloseToken.Tcp6Token); + } else { + return ; + } + + if (EFI_ERROR (Status)) { + return ; + } + + while (!TcpIo->IsCloseDone) { + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Tcp4->Poll (Tcp4); + } else { + Tcp6->Poll (Tcp6); + } + } +} + + +/** + Transmit the Packet to the other endpoint of the socket. + + @param[in] TcpIo The TcpIo wrapping the TCP socket. + @param[in] Packet The packet to transmit. + + @retval EFI_SUCCESS The packet is trasmitted. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TcpIoTransmit ( + IN TCP_IO *TcpIo, + IN NET_BUF *Packet + ) +{ + EFI_STATUS Status; + VOID *Data; + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP6_PROTOCOL *Tcp6; + UINTN Size; + + if ((TcpIo == NULL) || (TcpIo->Tcp.Tcp4 == NULL)|| (Packet == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (TcpIo->TcpVersion == TCP_VERSION_4) { + + Size = sizeof (EFI_TCP4_TRANSMIT_DATA) + + (Packet->BlockOpNum - 1) * sizeof (EFI_TCP4_FRAGMENT_DATA); + } else if (TcpIo->TcpVersion == TCP_VERSION_6) { + Size = sizeof (EFI_TCP6_TRANSMIT_DATA) + + (Packet->BlockOpNum - 1) * sizeof (EFI_TCP6_FRAGMENT_DATA); + } else { + return EFI_UNSUPPORTED; + } + + Data = AllocatePool (Size); + if (Data == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + ((EFI_TCP4_TRANSMIT_DATA *) Data)->Push = TRUE; + ((EFI_TCP4_TRANSMIT_DATA *) Data)->Urgent = FALSE; + ((EFI_TCP4_TRANSMIT_DATA *) Data)->DataLength = Packet->TotalSize; + + // + // Build the fragment table. + // + ((EFI_TCP4_TRANSMIT_DATA *) Data)->FragmentCount = Packet->BlockOpNum; + + NetbufBuildExt ( + Packet, + (NET_FRAGMENT *) &((EFI_TCP4_TRANSMIT_DATA *) Data)->FragmentTable[0], + &((EFI_TCP4_TRANSMIT_DATA *) Data)->FragmentCount + ); + + Tcp4 = NULL; + Tcp6 = NULL; + Status = EFI_DEVICE_ERROR; + + // + // Trasnmit the packet. + // + if (TcpIo->TcpVersion == TCP_VERSION_4) { + TcpIo->TxToken.Tcp4Token.Packet.TxData = (EFI_TCP4_TRANSMIT_DATA *) Data; + Tcp4 = TcpIo->Tcp.Tcp4; + if (TcpIo->IsListenDone) { + Tcp4 = TcpIo->NewTcp.Tcp4; + } + + if (Tcp4 == NULL) { + goto ON_EXIT; + } + + Status = Tcp4->Transmit (Tcp4, &TcpIo->TxToken.Tcp4Token); + } else { + TcpIo->TxToken.Tcp6Token.Packet.TxData = (EFI_TCP6_TRANSMIT_DATA *) Data; + Tcp6 = TcpIo->Tcp.Tcp6; + if (TcpIo->IsListenDone) { + Tcp6 = TcpIo->NewTcp.Tcp6; + } + + if (Tcp6 == NULL) { + goto ON_EXIT; + } + + Status = Tcp6->Transmit (Tcp6, &TcpIo->TxToken.Tcp6Token); + } + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + while (!TcpIo->IsTxDone) { + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Tcp4->Poll (Tcp4); + } else { + Tcp6->Poll (Tcp6); + } + } + + TcpIo->IsTxDone = FALSE; + Status = TcpIo->TxToken.Tcp4Token.CompletionToken.Status; + +ON_EXIT: + + FreePool (Data); + + return Status; +} + +/** + Receive data from the socket. + + @param[in, out] TcpIo The TcpIo which wraps the socket to be destroyed. + @param[in] Packet The buffer to hold the data copy from the socket rx buffer. + @param[in] AsyncMode Is this receive asyncronous or not. + @param[in] Timeout The time to wait for receiving the amount of data the Packet + can hold. Set to NULL for infinite wait. + + @retval EFI_SUCCESS The required amount of data is received from the socket. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval EFI_OUT_OF_RESOURCES Failed to allocate momery. + @retval EFI_TIMEOUT Failed to receive the required amount of data in the + specified time period. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +TcpIoReceive ( + IN OUT TCP_IO *TcpIo, + IN NET_BUF *Packet, + IN BOOLEAN AsyncMode, + IN EFI_EVENT Timeout OPTIONAL + ) +{ + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP6_PROTOCOL *Tcp6; + EFI_TCP4_RECEIVE_DATA *RxData; + EFI_STATUS Status; + NET_FRAGMENT *Fragment; + UINT32 FragmentCount; + UINT32 CurrentFragment; + + if ((TcpIo == NULL) || (TcpIo->Tcp.Tcp4 == NULL)|| (Packet == NULL)) { + return EFI_INVALID_PARAMETER; + } + + RxData = TcpIo->RxToken.Tcp4Token.Packet.RxData; + if (RxData == NULL) { + return EFI_INVALID_PARAMETER; + } + + Tcp4 = NULL; + Tcp6 = NULL; + + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Tcp4 = TcpIo->Tcp.Tcp4; + + if (TcpIo->IsListenDone) { + Tcp4 = TcpIo->NewTcp.Tcp4; + } + + if (Tcp4 == NULL) { + return EFI_DEVICE_ERROR; + } + + } else if (TcpIo->TcpVersion == TCP_VERSION_6) { + Tcp6 = TcpIo->Tcp.Tcp6; + + if (TcpIo->IsListenDone) { + Tcp6 = TcpIo->NewTcp.Tcp6; + } + + if (Tcp6 == NULL) { + return EFI_DEVICE_ERROR; + } + + } else { + return EFI_UNSUPPORTED; + } + + FragmentCount = Packet->BlockOpNum; + Fragment = AllocatePool (FragmentCount * sizeof (NET_FRAGMENT)); + if (Fragment == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + // + // Build the fragment table. + // + NetbufBuildExt (Packet, Fragment, &FragmentCount); + + RxData->FragmentCount = 1; + CurrentFragment = 0; + Status = EFI_SUCCESS; + + while (CurrentFragment < FragmentCount) { + RxData->DataLength = Fragment[CurrentFragment].Len; + RxData->FragmentTable[0].FragmentLength = Fragment[CurrentFragment].Len; + RxData->FragmentTable[0].FragmentBuffer = Fragment[CurrentFragment].Bulk; + + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Status = Tcp4->Receive (Tcp4, &TcpIo->RxToken.Tcp4Token); + } else { + Status = Tcp6->Receive (Tcp6, &TcpIo->RxToken.Tcp6Token); + } + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + while (!TcpIo->IsRxDone && ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) { + // + // Poll until some data is received or an error occurs. + // + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Tcp4->Poll (Tcp4); + } else { + Tcp6->Poll (Tcp6); + } + } + + if (!TcpIo->IsRxDone) { + // + // Timeout occurs, cancel the receive request. + // + if (TcpIo->TcpVersion == TCP_VERSION_4) { + Tcp4->Cancel (Tcp4, &TcpIo->RxToken.Tcp4Token.CompletionToken); + } else { + Tcp6->Cancel (Tcp6, &TcpIo->RxToken.Tcp6Token.CompletionToken); + } + + Status = EFI_TIMEOUT; + goto ON_EXIT; + } else { + TcpIo->IsRxDone = FALSE; + } + + Status = TcpIo->RxToken.Tcp4Token.CompletionToken.Status; + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Fragment[CurrentFragment].Len -= RxData->FragmentTable[0].FragmentLength; + if (Fragment[CurrentFragment].Len == 0) { + CurrentFragment++; + } else { + Fragment[CurrentFragment].Bulk += RxData->FragmentTable[0].FragmentLength; + } + } + +ON_EXIT: + + if (Fragment != NULL) { + FreePool (Fragment); + } + + return Status; +} diff --git a/NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.inf b/NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.inf new file mode 100644 index 000000000..4dcf34429 --- /dev/null +++ b/NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.inf @@ -0,0 +1,45 @@ +## @file +# This library instance provides TCP services by EFI TCPv4/TCPv6 Protocols. +# +# Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = DxeTcpIoLib + MODULE_UNI_FILE = DxeTcpIoLib.uni + FILE_GUID = D4608509-1AB0-4cc7-827A-AB8E1E7BD3E6 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = TcpIoLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources] + DxeTcpIoLib.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + BaseLib + DebugLib + UefiBootServicesTableLib + MemoryAllocationLib + BaseMemoryLib + +[Protocols] + gEfiTcp4ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiTcp4ProtocolGuid ## SOMETIMES_CONSUMES + gEfiTcp6ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiTcp6ProtocolGuid ## SOMETIMES_CONSUMES diff --git a/NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.uni b/NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.uni new file mode 100644 index 000000000..79ad484a6 --- /dev/null +++ b/NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.uni @@ -0,0 +1,16 @@ +// /** @file +// This library instance provides TCP services by EFI TCPv4/TCPv6 Protocols. +// +// This library instance provides TCP services by EFI TCPv4/TCPv6 Protocols. +// +// Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Provides TCP services by EFI TCPv4/TCPv6 Protocols" + +#string STR_MODULE_DESCRIPTION #language en-US "This library instance provides TCP services by EFI TCPv4/TCPv6 Protocols." + diff --git a/NetworkPkg/Library/DxeUdpIoLib/DxeUdpIoLib.c b/NetworkPkg/Library/DxeUdpIoLib/DxeUdpIoLib.c new file mode 100644 index 000000000..155cb3104 --- /dev/null +++ b/NetworkPkg/Library/DxeUdpIoLib/DxeUdpIoLib.c @@ -0,0 +1,1119 @@ +/** @file + Help functions to access UDP service, it is used by both the DHCP and MTFTP. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + + +/** + Free a UDP_TX_TOKEN. The TX event is closed. + + @param[in] TxToken The UDP_TX_TOKEN to release. + +**/ +VOID +UdpIoFreeTxToken ( + IN UDP_TX_TOKEN *TxToken + ) +{ + + if (TxToken->UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + gBS->CloseEvent (TxToken->Token.Udp4.Event); + } else if (TxToken->UdpIo->UdpVersion == UDP_IO_UDP6_VERSION) { + gBS->CloseEvent (TxToken->Token.Udp6.Event); + } else { + ASSERT (FALSE); + } + + FreePool (TxToken); +} + +/** + Free a UDP_RX_TOKEN. The RX event is closed. + + @param[in] RxToken The UDP_RX_TOKEN to release. + +**/ +VOID +UdpIoFreeRxToken ( + IN UDP_RX_TOKEN *RxToken + ) +{ + if (RxToken->UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + gBS->CloseEvent (RxToken->Token.Udp4.Event); + } else if (RxToken->UdpIo->UdpVersion == UDP_IO_UDP6_VERSION) { + gBS->CloseEvent (RxToken->Token.Udp6.Event); + } else { + ASSERT (FALSE); + } + + FreePool (RxToken); +} + +/** + The callback function when the packet is sent by UDP. + + It will remove the packet from the local list then call + the packet owner's callback function set by UdpIoSendDatagram. + + @param[in] Context The UDP TX Token. + +**/ +VOID +EFIAPI +UdpIoOnDgramSentDpc ( + IN VOID *Context + ) +{ + UDP_TX_TOKEN *TxToken; + + TxToken = (UDP_TX_TOKEN *) Context; + ASSERT (TxToken->Signature == UDP_IO_TX_SIGNATURE); + ASSERT ((TxToken->UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) || + (TxToken->UdpIo->UdpVersion == UDP_IO_UDP6_VERSION)); + + RemoveEntryList (&TxToken->Link); + + if (TxToken->UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + TxToken->CallBack (TxToken->Packet, NULL, TxToken->Token.Udp4.Status, TxToken->Context); + } else { + TxToken->CallBack (TxToken->Packet, NULL, TxToken->Token.Udp6.Status, TxToken->Context); + } + + UdpIoFreeTxToken (TxToken); +} + +/** + Request UdpIoOnDgramSentDpc as a DPC at TPL_CALLBACK. + + @param[in] Event The event signaled. + @param[in] Context The UDP TX Token. + +**/ +VOID +EFIAPI +UdpIoOnDgramSent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request UdpIoOnDgramSentDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, UdpIoOnDgramSentDpc, Context); +} + +/** + Recycle the received UDP data. + + @param[in] Context The UDP_RX_TOKEN. + +**/ +VOID +EFIAPI +UdpIoRecycleDgram ( + IN VOID *Context + ) +{ + UDP_RX_TOKEN *RxToken; + + RxToken = (UDP_RX_TOKEN *) Context; + + if (RxToken->UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + gBS->SignalEvent (RxToken->Token.Udp4.Packet.RxData->RecycleSignal); + } else if (RxToken->UdpIo->UdpVersion == UDP_IO_UDP6_VERSION) { + gBS->SignalEvent (RxToken->Token.Udp6.Packet.RxData->RecycleSignal); + } else { + ASSERT (FALSE); + } + + UdpIoFreeRxToken (RxToken); +} + +/** + The event handle for UDP receive request. + + It will build a NET_BUF from the recieved UDP data, then deliver it + to the receiver. + + @param[in] Context The UDP RX token. + +**/ +VOID +EFIAPI +UdpIoOnDgramRcvdDpc ( + IN VOID *Context + ) +{ + EFI_STATUS Status; + VOID *Token; + VOID *RxData; + VOID *Session; + UDP_RX_TOKEN *RxToken; + UDP_END_POINT EndPoint; + NET_BUF *Netbuf; + + RxToken = (UDP_RX_TOKEN *) Context; + + ZeroMem (&EndPoint, sizeof(UDP_END_POINT)); + + ASSERT ((RxToken->Signature == UDP_IO_RX_SIGNATURE) && + (RxToken == RxToken->UdpIo->RecvRequest)); + + ASSERT ((RxToken->UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) || + (RxToken->UdpIo->UdpVersion == UDP_IO_UDP6_VERSION)); + + // + // Clear the receive request first in case that the caller + // wants to restart the receive in the callback. + // + RxToken->UdpIo->RecvRequest = NULL; + + if (RxToken->UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + Token = &RxToken->Token.Udp4; + RxData = ((EFI_UDP4_COMPLETION_TOKEN *) Token)->Packet.RxData; + Status = ((EFI_UDP4_COMPLETION_TOKEN *) Token)->Status; + } else { + Token = &RxToken->Token.Udp6; + RxData = ((EFI_UDP6_COMPLETION_TOKEN *) Token)->Packet.RxData; + Status = ((EFI_UDP6_COMPLETION_TOKEN *) Token)->Status; + } + + if (EFI_ERROR (Status) || RxData == NULL) { + if (Status != EFI_ABORTED) { + // + // Invoke the CallBack only if the reception is not actively aborted. + // + RxToken->CallBack (NULL, NULL, Status, RxToken->Context); + } + + UdpIoFreeRxToken (RxToken); + return; + } + + // + // Build a NET_BUF from the UDP receive data, then deliver it up. + // + if (RxToken->UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + if (((EFI_UDP4_RECEIVE_DATA *) RxData)->DataLength == 0) { + // + // Discard zero length data payload packet. + // + goto Resume; + } + + Netbuf = NetbufFromExt ( + (NET_FRAGMENT *)((EFI_UDP4_RECEIVE_DATA *) RxData)->FragmentTable, + ((EFI_UDP4_RECEIVE_DATA *) RxData)->FragmentCount, + 0, + (UINT32) RxToken->HeadLen, + UdpIoRecycleDgram, + RxToken + ); + + if (Netbuf == NULL) { + gBS->SignalEvent (((EFI_UDP4_RECEIVE_DATA *) RxData)->RecycleSignal); + RxToken->CallBack (NULL, NULL, EFI_OUT_OF_RESOURCES, RxToken->Context); + + UdpIoFreeRxToken (RxToken); + return; + } + + Session = &((EFI_UDP4_RECEIVE_DATA *) RxData)->UdpSession; + EndPoint.LocalPort = ((EFI_UDP4_SESSION_DATA *) Session)->DestinationPort; + EndPoint.RemotePort = ((EFI_UDP4_SESSION_DATA *) Session)->SourcePort; + + CopyMem ( + &EndPoint.LocalAddr, + &((EFI_UDP4_SESSION_DATA *) Session)->DestinationAddress, + sizeof (EFI_IPv4_ADDRESS) + ); + + CopyMem ( + &EndPoint.RemoteAddr, + &((EFI_UDP4_SESSION_DATA *) Session)->SourceAddress, + sizeof (EFI_IPv4_ADDRESS) + ); + + EndPoint.LocalAddr.Addr[0] = NTOHL (EndPoint.LocalAddr.Addr[0]); + EndPoint.RemoteAddr.Addr[0] = NTOHL (EndPoint.RemoteAddr.Addr[0]); + } else { + if (((EFI_UDP6_RECEIVE_DATA *) RxData)->DataLength == 0) { + // + // Discard zero length data payload packet. + // + goto Resume; + } + + Netbuf = NetbufFromExt ( + (NET_FRAGMENT *)((EFI_UDP6_RECEIVE_DATA *) RxData)->FragmentTable, + ((EFI_UDP6_RECEIVE_DATA *) RxData)->FragmentCount, + 0, + (UINT32) RxToken->HeadLen, + UdpIoRecycleDgram, + RxToken + ); + + if (Netbuf == NULL) { + gBS->SignalEvent (((EFI_UDP6_RECEIVE_DATA *) RxData)->RecycleSignal); + RxToken->CallBack (NULL, NULL, EFI_OUT_OF_RESOURCES, RxToken->Context); + + UdpIoFreeRxToken (RxToken); + return; + } + + Session = &((EFI_UDP6_RECEIVE_DATA *) RxData)->UdpSession; + EndPoint.LocalPort = ((EFI_UDP6_SESSION_DATA *) Session)->DestinationPort; + EndPoint.RemotePort = ((EFI_UDP6_SESSION_DATA *) Session)->SourcePort; + + CopyMem ( + &EndPoint.LocalAddr, + &((EFI_UDP6_SESSION_DATA *) Session)->DestinationAddress, + sizeof (EFI_IPv6_ADDRESS) + ); + + CopyMem ( + &EndPoint.RemoteAddr, + &((EFI_UDP6_SESSION_DATA *) Session)->SourceAddress, + sizeof (EFI_IPv6_ADDRESS) + ); + + Ip6Swap128 (&EndPoint.LocalAddr.v6); + Ip6Swap128 (&EndPoint.RemoteAddr.v6); + } + + RxToken->CallBack (Netbuf, &EndPoint, EFI_SUCCESS, RxToken->Context); + return; + +Resume: + if (RxToken->UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + gBS->SignalEvent (((EFI_UDP4_RECEIVE_DATA *) RxData)->RecycleSignal); + RxToken->UdpIo->Protocol.Udp4->Receive (RxToken->UdpIo->Protocol.Udp4, &RxToken->Token.Udp4); + } else { + gBS->SignalEvent (((EFI_UDP6_RECEIVE_DATA *) RxData)->RecycleSignal); + RxToken->UdpIo->Protocol.Udp6->Receive (RxToken->UdpIo->Protocol.Udp6, &RxToken->Token.Udp6); + } +} + +/** + Request UdpIoOnDgramRcvdDpc() as a DPC at TPL_CALLBACK. + + @param[in] Event The UDP receive request event. + @param[in] Context The UDP RX token. + +**/ +VOID +EFIAPI +UdpIoOnDgramRcvd ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Request UdpIoOnDgramRcvdDpc as a DPC at TPL_CALLBACK + // + QueueDpc (TPL_CALLBACK, UdpIoOnDgramRcvdDpc, Context); +} + +/** + Create a UDP_RX_TOKEN to wrap the request. + + @param[in] UdpIo The UdpIo to receive packets from. + @param[in] CallBack The function to call when receive finished. + @param[in] Context The opaque parameter to the CallBack. + @param[in] HeadLen The head length to reserver for the packet. + + @return The Wrapped request or NULL if failed to allocate resources or some errors happened. + +**/ +UDP_RX_TOKEN * +UdpIoCreateRxToken ( + IN UDP_IO *UdpIo, + IN UDP_IO_CALLBACK CallBack, + IN VOID *Context, + IN UINT32 HeadLen + ) +{ + UDP_RX_TOKEN *Token; + EFI_STATUS Status; + + ASSERT ((UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) || + (UdpIo->UdpVersion == UDP_IO_UDP6_VERSION)); + + Token = AllocatePool (sizeof (UDP_RX_TOKEN)); + + if (Token == NULL) { + return NULL; + } + + Token->Signature = UDP_IO_RX_SIGNATURE; + Token->UdpIo = UdpIo; + Token->CallBack = CallBack; + Token->Context = Context; + Token->HeadLen = HeadLen; + + if (UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + + Token->Token.Udp4.Status = EFI_NOT_READY; + Token->Token.Udp4.Packet.RxData = NULL; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + UdpIoOnDgramRcvd, + Token, + &Token->Token.Udp4.Event + ); + } else { + + Token->Token.Udp6.Status = EFI_NOT_READY; + Token->Token.Udp6.Packet.RxData = NULL; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + UdpIoOnDgramRcvd, + Token, + &Token->Token.Udp6.Event + ); + } + + + if (EFI_ERROR (Status)) { + FreePool (Token); + return NULL; + } + + return Token; +} + +/** + Wrap a transmit request into a new created UDP_TX_TOKEN. + + If Packet is NULL, then ASSERT(). + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + @param[in] UdpIo The UdpIo to send packet to. + @param[in] Packet The user's packet. + @param[in] EndPoint The local and remote access point. + @param[in] Gateway The overrided next hop. + @param[in] CallBack The function to call when transmission completed. + @param[in] Context The opaque parameter to the call back. + + @return The wrapped transmission request or NULL if failed to allocate resources + or for some errors. + +**/ +UDP_TX_TOKEN * +UdpIoCreateTxToken ( + IN UDP_IO *UdpIo, + IN NET_BUF *Packet, + IN UDP_END_POINT *EndPoint OPTIONAL, + IN EFI_IP_ADDRESS *Gateway OPTIONAL, + IN UDP_IO_CALLBACK CallBack, + IN VOID *Context + ) +{ + UDP_TX_TOKEN *TxToken; + VOID *Token; + VOID *Data; + EFI_STATUS Status; + UINT32 Count; + UINTN Size; + IP4_ADDR Ip; + + ASSERT (Packet != NULL); + ASSERT ((UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) || + (UdpIo->UdpVersion == UDP_IO_UDP6_VERSION)); + + if (UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + Size = sizeof (UDP_TX_TOKEN) + sizeof (EFI_UDP4_FRAGMENT_DATA) * (Packet->BlockOpNum - 1); + } else { + Size = sizeof (UDP_TX_TOKEN) + sizeof (EFI_UDP6_FRAGMENT_DATA) * (Packet->BlockOpNum - 1); + } + + TxToken = AllocatePool (Size); + + if (TxToken == NULL) { + return NULL; + } + + TxToken->Signature = UDP_IO_TX_SIGNATURE; + InitializeListHead (&TxToken->Link); + + TxToken->UdpIo = UdpIo; + TxToken->CallBack = CallBack; + TxToken->Packet = Packet; + TxToken->Context = Context; + + Token = &(TxToken->Token); + Count = Packet->BlockOpNum; + + if (UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + + ((EFI_UDP4_COMPLETION_TOKEN *) Token)->Status = EFI_NOT_READY; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + UdpIoOnDgramSent, + TxToken, + &((EFI_UDP4_COMPLETION_TOKEN *) Token)->Event + ); + + if (EFI_ERROR (Status)) { + FreePool (TxToken); + return NULL; + } + + Data = &(TxToken->Data.Udp4); + ((EFI_UDP4_COMPLETION_TOKEN *) Token)->Packet.TxData = Data; + + ((EFI_UDP4_TRANSMIT_DATA *) Data)->UdpSessionData = NULL; + ((EFI_UDP4_TRANSMIT_DATA *) Data)->GatewayAddress = NULL; + ((EFI_UDP4_TRANSMIT_DATA *) Data)->DataLength = Packet->TotalSize; + + NetbufBuildExt ( + Packet, + (NET_FRAGMENT *)((EFI_UDP4_TRANSMIT_DATA *) Data)->FragmentTable, + &Count + ); + + ((EFI_UDP4_TRANSMIT_DATA *) Data)->FragmentCount = Count; + + if (EndPoint != NULL) { + Ip = HTONL (EndPoint->LocalAddr.Addr[0]); + CopyMem ( + &TxToken->Session.Udp4.SourceAddress, + &Ip, + sizeof (EFI_IPv4_ADDRESS) + ); + + Ip = HTONL (EndPoint->RemoteAddr.Addr[0]); + CopyMem ( + &TxToken->Session.Udp4.DestinationAddress, + &Ip, + sizeof (EFI_IPv4_ADDRESS) + ); + + TxToken->Session.Udp4.SourcePort = EndPoint->LocalPort; + TxToken->Session.Udp4.DestinationPort = EndPoint->RemotePort; + ((EFI_UDP4_TRANSMIT_DATA *) Data)->UdpSessionData = &(TxToken->Session.Udp4); + } + + if (Gateway != NULL && (Gateway->Addr[0] != 0)) { + Ip = HTONL (Gateway->Addr[0]); + CopyMem (&TxToken->Gateway, &Ip, sizeof (EFI_IPv4_ADDRESS)); + ((EFI_UDP4_TRANSMIT_DATA *) Data)->GatewayAddress = &TxToken->Gateway; + } + + } else { + + ((EFI_UDP6_COMPLETION_TOKEN *) Token)->Status = EFI_NOT_READY; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + UdpIoOnDgramSent, + TxToken, + &((EFI_UDP6_COMPLETION_TOKEN *) Token)->Event + ); + + if (EFI_ERROR (Status)) { + FreePool (TxToken); + return NULL; + } + + Data = &(TxToken->Data.Udp6); + ((EFI_UDP6_COMPLETION_TOKEN *) Token)->Packet.TxData = Data; + ((EFI_UDP6_TRANSMIT_DATA *) Data)->UdpSessionData = NULL; + ((EFI_UDP6_TRANSMIT_DATA *) Data)->DataLength = Packet->TotalSize; + + NetbufBuildExt ( + Packet, + (NET_FRAGMENT *)((EFI_UDP6_TRANSMIT_DATA *) Data)->FragmentTable, + &Count + ); + + ((EFI_UDP6_TRANSMIT_DATA *) Data)->FragmentCount = Count; + + if (EndPoint != NULL) { + CopyMem ( + &TxToken->Session.Udp6.SourceAddress, + &EndPoint->LocalAddr.v6, + sizeof(EFI_IPv6_ADDRESS) + ); + + CopyMem ( + &TxToken->Session.Udp6.DestinationAddress, + &EndPoint->RemoteAddr.v6, + sizeof(EFI_IPv6_ADDRESS) + ); + + TxToken->Session.Udp6.SourcePort = EndPoint->LocalPort; + TxToken->Session.Udp6.DestinationPort = EndPoint->RemotePort; + ((EFI_UDP6_TRANSMIT_DATA *) Data)->UdpSessionData = &(TxToken->Session.Udp6); + } + } + + return TxToken; +} + +/** + Creates a UDP_IO to access the UDP service. It creates and configures + a UDP child. + + If Configure is NULL, then ASSERT(). + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + It locates the UDP service binding prototype on the Controller parameter + uses the UDP service binding prototype to create a UDP child (also known as + a UDP instance) configures the UDP child by calling Configure function prototype. + Any failures in creating or configuring the UDP child return NULL for failure. + + @param[in] Controller The controller that has the UDP service binding. + protocol installed. + @param[in] ImageHandle The image handle for the driver. + @param[in] Configure The function to configure the created UDP child. + @param[in] UdpVersion The UDP protocol version, UDP4 or UDP6. + @param[in] Context The opaque parameter for the Configure funtion. + + @return Newly-created UDP_IO or NULL if failed. + +**/ +UDP_IO * +EFIAPI +UdpIoCreateIo ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE ImageHandle, + IN UDP_IO_CONFIG Configure, + IN UINT8 UdpVersion, + IN VOID *Context + ) +{ + UDP_IO *UdpIo; + EFI_STATUS Status; + + ASSERT (Configure != NULL); + ASSERT ((UdpVersion == UDP_IO_UDP4_VERSION) || (UdpVersion == UDP_IO_UDP6_VERSION)); + + UdpIo = AllocatePool (sizeof (UDP_IO)); + + if (UdpIo == NULL) { + return NULL; + } + + UdpIo->UdpVersion = UdpVersion; + UdpIo->Signature = UDP_IO_SIGNATURE; + InitializeListHead (&UdpIo->Link); + UdpIo->RefCnt = 1; + + UdpIo->Controller = Controller; + UdpIo->Image = ImageHandle; + + InitializeListHead (&UdpIo->SentDatagram); + UdpIo->RecvRequest = NULL; + UdpIo->UdpHandle = NULL; + + if (UdpVersion == UDP_IO_UDP4_VERSION) { + // + // Create a UDP child then open and configure it + // + Status = NetLibCreateServiceChild ( + Controller, + ImageHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + &UdpIo->UdpHandle + ); + + if (EFI_ERROR (Status)) { + goto FREE_MEM; + } + + Status = gBS->OpenProtocol ( + UdpIo->UdpHandle, + &gEfiUdp4ProtocolGuid, + (VOID **) &UdpIo->Protocol.Udp4, + ImageHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + goto FREE_CHILD; + } + + if (EFI_ERROR (Configure (UdpIo, Context))) { + goto CLOSE_PROTOCOL; + } + + Status = UdpIo->Protocol.Udp4->GetModeData ( + UdpIo->Protocol.Udp4, + NULL, + NULL, + NULL, + &UdpIo->SnpMode + ); + + if (EFI_ERROR (Status)) { + goto CLOSE_PROTOCOL; + } + + } else { + + Status = NetLibCreateServiceChild ( + Controller, + ImageHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + &UdpIo->UdpHandle + ); + + if (EFI_ERROR (Status)) { + goto FREE_MEM; + } + + Status = gBS->OpenProtocol ( + UdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + (VOID **) &UdpIo->Protocol.Udp6, + ImageHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + goto FREE_CHILD; + } + + if (EFI_ERROR (Configure (UdpIo, Context))) { + goto CLOSE_PROTOCOL; + } + + Status = UdpIo->Protocol.Udp6->GetModeData ( + UdpIo->Protocol.Udp6, + NULL, + NULL, + NULL, + &UdpIo->SnpMode + ); + + if (EFI_ERROR (Status)) { + goto CLOSE_PROTOCOL; + } + } + + return UdpIo; + +CLOSE_PROTOCOL: + if (UdpVersion == UDP_IO_UDP4_VERSION) { + gBS->CloseProtocol (UdpIo->UdpHandle, &gEfiUdp4ProtocolGuid, ImageHandle, Controller); + } else { + gBS->CloseProtocol (UdpIo->UdpHandle, &gEfiUdp6ProtocolGuid, ImageHandle, Controller); + } + +FREE_CHILD: + if (UdpVersion == UDP_IO_UDP4_VERSION) { + NetLibDestroyServiceChild ( + Controller, + ImageHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + UdpIo->UdpHandle + ); + } else { + NetLibDestroyServiceChild ( + Controller, + ImageHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + UdpIo->UdpHandle + ); + } + +FREE_MEM: + FreePool (UdpIo); + return NULL; +} + +/** + Cancel all the sent datagram that pass the selection criteria of ToCancel. + + If ToCancel is NULL, all the datagrams are cancelled. + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + @param[in] UdpIo The UDP_IO to cancel packet. + @param[in] IoStatus The IoStatus to return to the packet owners. + @param[in] ToCancel The select funtion to test whether to cancel this + packet or not. + @param[in] Context The opaque parameter to the ToCancel. + +**/ +VOID +EFIAPI +UdpIoCancelDgrams ( + IN UDP_IO *UdpIo, + IN EFI_STATUS IoStatus, + IN UDP_IO_TO_CANCEL ToCancel, OPTIONAL + IN VOID *Context OPTIONAL + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + UDP_TX_TOKEN *TxToken; + + ASSERT ((UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) || + (UdpIo->UdpVersion == UDP_IO_UDP6_VERSION)); + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &UdpIo->SentDatagram) { + TxToken = NET_LIST_USER_STRUCT (Entry, UDP_TX_TOKEN, Link); + + if ((ToCancel == NULL) || (ToCancel (TxToken, Context))) { + + if (UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + UdpIo->Protocol.Udp4->Cancel (UdpIo->Protocol.Udp4, &TxToken->Token.Udp4); + } else { + UdpIo->Protocol.Udp6->Cancel (UdpIo->Protocol.Udp6, &TxToken->Token.Udp6); + } + } + } +} + +/** + Free the UDP_IO and all its related resources. + + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + The function will cancel all sent datagram and receive request. + + @param[in] UdpIo The UDP_IO to free. + + @retval EFI_SUCCESS The UDP_IO is freed. + @retval Others Failed to free UDP_IO. + +**/ +EFI_STATUS +EFIAPI +UdpIoFreeIo ( + IN UDP_IO *UdpIo + ) +{ + EFI_STATUS Status; + UDP_RX_TOKEN *RxToken; + + ASSERT ((UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) || + (UdpIo->UdpVersion == UDP_IO_UDP6_VERSION)); + + // + // Cancel all the sent datagram and receive requests. The + // callbacks of transmit requests are executed to allow the + // caller to release the resource. The callback of receive + // request are NOT executed. This is because it is most + // likely that the current user of the UDP IO port is closing + // itself. + // + UdpIoCancelDgrams (UdpIo, EFI_ABORTED, NULL, NULL); + + if (UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + + if ((RxToken = UdpIo->RecvRequest) != NULL) { + Status = UdpIo->Protocol.Udp4->Cancel (UdpIo->Protocol.Udp4, &RxToken->Token.Udp4); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Close then destroy the Udp4 child + // + Status = gBS->CloseProtocol ( + UdpIo->UdpHandle, + &gEfiUdp4ProtocolGuid, + UdpIo->Image, + UdpIo->Controller + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = NetLibDestroyServiceChild ( + UdpIo->Controller, + UdpIo->Image, + &gEfiUdp4ServiceBindingProtocolGuid, + UdpIo->UdpHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + } else { + + if ((RxToken = UdpIo->RecvRequest) != NULL) { + Status = UdpIo->Protocol.Udp6->Cancel (UdpIo->Protocol.Udp6, &RxToken->Token.Udp6); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Close then destroy the Udp6 child + // + Status = gBS->CloseProtocol ( + UdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + UdpIo->Image, + UdpIo->Controller + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = NetLibDestroyServiceChild ( + UdpIo->Controller, + UdpIo->Image, + &gEfiUdp6ServiceBindingProtocolGuid, + UdpIo->UdpHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + if (!IsListEmpty(&UdpIo->Link)) { + RemoveEntryList (&UdpIo->Link); + } + + FreePool (UdpIo); + return EFI_SUCCESS; +} + + +/** + Clean up the UDP_IO without freeing it. The function is called when + user wants to re-use the UDP_IO later. + + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + It will release all the transmitted datagrams and receive request. It will + also configure NULL for the UDP instance. + + @param[in] UdpIo The UDP_IO to clean up. + +**/ +VOID +EFIAPI +UdpIoCleanIo ( + IN UDP_IO *UdpIo + ) +{ + UDP_RX_TOKEN *RxToken; + + ASSERT ((UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) || + (UdpIo->UdpVersion == UDP_IO_UDP6_VERSION)); + + // + // Cancel all the sent datagram and receive requests. + // + UdpIoCancelDgrams (UdpIo, EFI_ABORTED, NULL, NULL); + + if (UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + if ((RxToken = UdpIo->RecvRequest) != NULL) { + UdpIo->Protocol.Udp4->Cancel (UdpIo->Protocol.Udp4, &RxToken->Token.Udp4); + } + + UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, NULL); + + } else { + if ((RxToken = UdpIo->RecvRequest) != NULL) { + UdpIo->Protocol.Udp6->Cancel (UdpIo->Protocol.Udp6, &RxToken->Token.Udp6); + } + + UdpIo->Protocol.Udp6->Configure (UdpIo->Protocol.Udp6, NULL); + } +} + +/** + Send a packet through the UDP_IO. + + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + The packet will be wrapped in UDP_TX_TOKEN. Function Callback will be called + when the packet is sent. The optional parameter EndPoint overrides the default + address pair if specified. + + @param[in] UdpIo The UDP_IO to send the packet through. + @param[in] Packet The packet to send. + @param[in] EndPoint The local and remote access point. Override the + default address pair set during configuration. + @param[in] Gateway The gateway to use. + @param[in] CallBack The function being called when packet is + transmitted or failed. + @param[in] Context The opaque parameter passed to CallBack. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the packet. + @retval EFI_SUCCESS The packet is successfully delivered to UDP for + transmission. + +**/ +EFI_STATUS +EFIAPI +UdpIoSendDatagram ( + IN UDP_IO *UdpIo, + IN NET_BUF *Packet, + IN UDP_END_POINT *EndPoint OPTIONAL, + IN EFI_IP_ADDRESS *Gateway OPTIONAL, + IN UDP_IO_CALLBACK CallBack, + IN VOID *Context + ) +{ + UDP_TX_TOKEN *TxToken; + EFI_STATUS Status; + + ASSERT ((UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) || + (UdpIo->UdpVersion == UDP_IO_UDP6_VERSION)); + + TxToken = UdpIoCreateTxToken (UdpIo, Packet, EndPoint, Gateway, CallBack, Context); + + if (TxToken == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Insert the tx token into SendDatagram list before transmitting it. Remove + // it from the list if the returned status is not EFI_SUCCESS. + // + InsertHeadList (&UdpIo->SentDatagram, &TxToken->Link); + + if (UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + Status = UdpIo->Protocol.Udp4->Transmit (UdpIo->Protocol.Udp4, &TxToken->Token.Udp4); + } else { + Status = UdpIo->Protocol.Udp6->Transmit (UdpIo->Protocol.Udp6, &TxToken->Token.Udp6); + } + + if (EFI_ERROR (Status)) { + RemoveEntryList (&TxToken->Link); + UdpIoFreeTxToken (TxToken); + return Status; + } + + return EFI_SUCCESS; +} + + +/** + The select function to cancel a single sent datagram. + + @param[in] Token The UDP_TX_TOKEN to test against + @param[in] Context The NET_BUF of the sent datagram + + @retval TRUE The packet is to be cancelled. + @retval FALSE The packet is not to be cancelled. +**/ +BOOLEAN +EFIAPI +UdpIoCancelSingleDgram ( + IN UDP_TX_TOKEN *Token, + IN VOID *Context + ) +{ + NET_BUF *Packet; + + Packet = (NET_BUF *) Context; + + if (Token->Packet == Packet) { + return TRUE; + } + + return FALSE; +} + +/** + Cancel a single sent datagram. + + @param[in] UdpIo The UDP_IO to cancel the packet from + @param[in] Packet The packet to cancel + +**/ +VOID +EFIAPI +UdpIoCancelSentDatagram ( + IN UDP_IO *UdpIo, + IN NET_BUF *Packet + ) +{ + UdpIoCancelDgrams (UdpIo, EFI_ABORTED, UdpIoCancelSingleDgram, Packet); +} + +/** + Issue a receive request to the UDP_IO. + + If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT(). + + This function is called when upper-layer needs packet from UDP for processing. + Only one receive request is acceptable at a time so a common usage model is + to invoke this function inside its Callback function when the former packet + is processed. + + @param[in] UdpIo The UDP_IO to receive the packet from. + @param[in] CallBack The call back function to execute when the packet + is received. + @param[in] Context The opaque context passed to Callback. + @param[in] HeadLen The length of the upper-layer's protocol header. + + @retval EFI_ALREADY_STARTED There is already a pending receive request. Only + one receive request is supported at a time. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. + @retval EFI_SUCCESS The receive request is issued successfully. + @retval EFI_UNSUPPORTED The UDP version in UDP_IO is not supported. + +**/ +EFI_STATUS +EFIAPI +UdpIoRecvDatagram ( + IN UDP_IO *UdpIo, + IN UDP_IO_CALLBACK CallBack, + IN VOID *Context, + IN UINT32 HeadLen + ) +{ + UDP_RX_TOKEN *RxToken; + EFI_STATUS Status; + + ASSERT ((UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) || + (UdpIo->UdpVersion == UDP_IO_UDP6_VERSION)); + + if (UdpIo->RecvRequest != NULL) { + return EFI_ALREADY_STARTED; + } + + RxToken = UdpIoCreateRxToken (UdpIo, CallBack, Context, HeadLen); + + if (RxToken == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + UdpIo->RecvRequest = RxToken; + if (UdpIo->UdpVersion == UDP_IO_UDP4_VERSION) { + Status = UdpIo->Protocol.Udp4->Receive (UdpIo->Protocol.Udp4, &RxToken->Token.Udp4); + } else { + Status = UdpIo->Protocol.Udp6->Receive (UdpIo->Protocol.Udp6, &RxToken->Token.Udp6); + } + + if (EFI_ERROR (Status)) { + UdpIo->RecvRequest = NULL; + UdpIoFreeRxToken (RxToken); + } + + return Status; +} diff --git a/NetworkPkg/Library/DxeUdpIoLib/DxeUdpIoLib.inf b/NetworkPkg/Library/DxeUdpIoLib/DxeUdpIoLib.inf new file mode 100644 index 000000000..7614faa95 --- /dev/null +++ b/NetworkPkg/Library/DxeUdpIoLib/DxeUdpIoLib.inf @@ -0,0 +1,47 @@ +## @file +# This library instance provides UDP services by consuming EFI UDPv4/UDPv6 Protocols. +# +# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = DxeUpdIoLib + MODULE_UNI_FILE = DxeUpdIoLib.uni + FILE_GUID = 7E615AA1-41EE-49d4-B7E9-1D7A60AA5C8D + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = UdpIoLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources] + DxeUdpIoLib.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + BaseLib + DebugLib + UefiBootServicesTableLib + MemoryAllocationLib + BaseMemoryLib + DpcLib + +[Protocols] + gEfiUdp4ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiUdp4ProtocolGuid ## SOMETIMES_CONSUMES + gEfiUdp6ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiUdp6ProtocolGuid ## SOMETIMES_CONSUMES + diff --git a/NetworkPkg/Library/DxeUdpIoLib/DxeUpdIoLib.uni b/NetworkPkg/Library/DxeUdpIoLib/DxeUpdIoLib.uni new file mode 100644 index 000000000..bc9490599 --- /dev/null +++ b/NetworkPkg/Library/DxeUdpIoLib/DxeUpdIoLib.uni @@ -0,0 +1,16 @@ +// /** @file +// This library instance provides UDP services by consuming EFI UDPv4/UDPv6 Protocols. +// +// This library instance provides UDP services by consuming EFI UDPv4/UDPv6 Protocols. +// +// Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Provides UDP services by consuming EFI UDPv4/UDPv6 Protocols" + +#string STR_MODULE_DESCRIPTION #language en-US "This library instance provides UDP services by consuming EFI UDPv4/UDPv6 Protocols." + diff --git a/NetworkPkg/MnpDxe/ComponentName.c b/NetworkPkg/MnpDxe/ComponentName.c new file mode 100644 index 000000000..fe85c3bc2 --- /dev/null +++ b/NetworkPkg/MnpDxe/ComponentName.c @@ -0,0 +1,341 @@ +/** @file + UEFI Component Name(2) protocol implementation for MnpDxe driver. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpImpl.h" + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gMnpComponentName = { + MnpComponentNameGetDriverName, + MnpComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gMnpComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) MnpComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) MnpComponentNameGetControllerName, + "en" +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mMnpDriverNameTable[] = { + { + "eng;en", + L"MNP Network Service Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gMnpControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MnpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mMnpDriverNameTable, + DriverName, + (BOOLEAN) (This == &gMnpComponentName) + ); +} + +/** + Update the component name for the MNP child handle. + + @param Mnp[in] A pointer to the EFI_MANAGED_NETWORK_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateName ( + IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + CHAR16 HandleName[80]; + EFI_MANAGED_NETWORK_CONFIG_DATA MnpConfigData; + EFI_SIMPLE_NETWORK_MODE SnpModeData; + UINTN OffSet; + UINTN Index; + + if (Mnp == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (Mnp); + // + // Format the child name into the string buffer as: + // MNP (MAC=FF-FF-FF-FF-FF-FF, ProtocolType=0x0800, VlanId=0) + // + Status = Mnp->GetModeData (Mnp, &MnpConfigData, &SnpModeData); + if (!EFI_ERROR (Status)) { + OffSet = 0; + // + // Print the MAC address. + // + OffSet += UnicodeSPrint ( + HandleName, + sizeof (HandleName), + L"MNP (MAC=" + ); + for (Index = 0; Index < SnpModeData.HwAddressSize; Index++) { + OffSet += UnicodeSPrint ( + HandleName + OffSet, + sizeof (HandleName) - OffSet * sizeof (CHAR16), + L"%02X-", + SnpModeData.CurrentAddress.Addr[Index] + ); + } + ASSERT (OffSet > 0); + // + // Remove the last '-' + // + OffSet--; + // + // Print the ProtocolType and VLAN ID for this instance. + // + OffSet += UnicodeSPrint ( + HandleName + OffSet, + sizeof (HandleName) - OffSet * sizeof (CHAR16), + L", ProtocolType=0x%X, VlanId=%d)", + MnpConfigData.ProtocolTypeFilter, + Instance->MnpServiceData->VlanId + ); + } else if (Status == EFI_NOT_STARTED) { + UnicodeSPrint ( + HandleName, + sizeof (HandleName), + L"MNP (Not started)" + ); + } else { + return Status; + } + + if (gMnpControllerNameTable != NULL) { + FreeUnicodeStringTable (gMnpControllerNameTable); + gMnpControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gMnpComponentName.SupportedLanguages, + &gMnpControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gMnpComponentName2.SupportedLanguages, + &gMnpControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + Currently not implemented. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name + specified by This, ControllerHandle, ChildHandle, + and Language was returned in ControllerName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MnpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + + // + // Only provide names for MNP child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver is currently managing ControllerHandle + // + Status = EfiTestManagedDevice ( + ControllerHandle, + gMnpDriverBinding.DriverBindingHandle, + &gEfiSimpleNetworkProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **)&Mnp, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateName (Mnp); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gMnpControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gMnpComponentName) + ); +} diff --git a/NetworkPkg/MnpDxe/ComponentName.h b/NetworkPkg/MnpDxe/ComponentName.h new file mode 100644 index 000000000..201c6da11 --- /dev/null +++ b/NetworkPkg/MnpDxe/ComponentName.h @@ -0,0 +1,144 @@ +/** @file + The header file of UEFI Component Name(2) protocol. + +Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _COMPONENT_NAME_H_ +#define _COMPONENT_NAME_H_ + +#include +#include + +extern EFI_COMPONENT_NAME2_PROTOCOL gMnpComponentName2; +extern EFI_COMPONENT_NAME_PROTOCOL gMnpComponentName; +extern EFI_UNICODE_STRING_TABLE *gMnpControllerNameTable; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MnpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + Currently not implemented. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name + specified by This, ControllerHandle, ChildHandle, + and Language was returned in ControllerName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +MnpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +#endif diff --git a/NetworkPkg/MnpDxe/MnpConfig.c b/NetworkPkg/MnpDxe/MnpConfig.c new file mode 100644 index 000000000..5906ad546 --- /dev/null +++ b/NetworkPkg/MnpDxe/MnpConfig.c @@ -0,0 +1,1939 @@ +/** @file + Implementation of Managed Network Protocol private services. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpImpl.h" +#include "MnpVlan.h" + +EFI_SERVICE_BINDING_PROTOCOL mMnpServiceBindingProtocol = { + MnpServiceBindingCreateChild, + MnpServiceBindingDestroyChild +}; + +EFI_MANAGED_NETWORK_PROTOCOL mMnpProtocolTemplate = { + MnpGetModeData, + MnpConfigure, + MnpMcastIpToMac, + MnpGroups, + MnpTransmit, + MnpReceive, + MnpCancel, + MnpPoll +}; + +EFI_MANAGED_NETWORK_CONFIG_DATA mMnpDefaultConfigData = { + 10000000, + 10000000, + 0, + FALSE, + FALSE, + FALSE, + FALSE, + FALSE, + FALSE, + FALSE +}; + +/** + Add Count of net buffers to MnpDeviceData->FreeNbufQue. The length of the net + buffer is specified by MnpDeviceData->BufferLength. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + @param[in] Count Number of NET_BUFFERs to add. + + @retval EFI_SUCCESS The specified amount of NET_BUFs are allocated + and added to MnpDeviceData->FreeNbufQue. + @retval EFI_OUT_OF_RESOURCES Failed to allocate a NET_BUF structure. + +**/ +EFI_STATUS +MnpAddFreeNbuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN UINTN Count + ) +{ + EFI_STATUS Status; + UINTN Index; + NET_BUF *Nbuf; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + ASSERT ((Count > 0) && (MnpDeviceData->BufferLength > 0)); + + Status = EFI_SUCCESS; + for (Index = 0; Index < Count; Index++) { + Nbuf = NetbufAlloc (MnpDeviceData->BufferLength + MnpDeviceData->PaddingSize); + if (Nbuf == NULL) { + DEBUG ((EFI_D_ERROR, "MnpAddFreeNbuf: NetBufAlloc failed.\n")); + + Status = EFI_OUT_OF_RESOURCES; + break; + } + + if (MnpDeviceData->PaddingSize > 0) { + // + // Pad padding bytes before the media header + // + NetbufAllocSpace (Nbuf, MnpDeviceData->PaddingSize, NET_BUF_TAIL); + NetbufTrim (Nbuf, MnpDeviceData->PaddingSize, NET_BUF_HEAD); + } + + NetbufQueAppend (&MnpDeviceData->FreeNbufQue, Nbuf); + } + + MnpDeviceData->NbufCnt += Index; + return Status; +} + + +/** + Allocate a free NET_BUF from MnpDeviceData->FreeNbufQue. If there is none + in the queue, first try to allocate some and add them into the queue, then + fetch the NET_BUF from the updated FreeNbufQue. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + + @return Pointer to the allocated free NET_BUF structure, if NULL the + operation is failed. + +**/ +NET_BUF * +MnpAllocNbuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ) +{ + EFI_STATUS Status; + NET_BUF_QUEUE *FreeNbufQue; + NET_BUF *Nbuf; + EFI_TPL OldTpl; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + FreeNbufQue = &MnpDeviceData->FreeNbufQue; + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + + // + // Check whether there are available buffers, or else try to add some. + // + if (FreeNbufQue->BufNum == 0) { + if ((MnpDeviceData->NbufCnt + MNP_NET_BUFFER_INCREASEMENT) > MNP_MAX_NET_BUFFER_NUM) { + DEBUG ( + (EFI_D_ERROR, + "MnpAllocNbuf: The maximum NET_BUF size is reached for MNP driver instance %p.\n", + MnpDeviceData) + ); + + Nbuf = NULL; + goto ON_EXIT; + } + + Status = MnpAddFreeNbuf (MnpDeviceData, MNP_NET_BUFFER_INCREASEMENT); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpAllocNbuf: Failed to add NET_BUFs into the FreeNbufQue, %r.\n", + Status) + ); + + // + // Don't return NULL, perhaps MnpAddFreeNbuf does add some NET_BUFs but + // the amount is less than MNP_NET_BUFFER_INCREASEMENT. + // + } + } + + Nbuf = NetbufQueRemove (FreeNbufQue); + + // + // Increase the RefCnt. + // + if (Nbuf != NULL) { + NET_GET_REF (Nbuf); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Nbuf; +} + + +/** + Try to reclaim the Nbuf into the buffer pool. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in, out] Nbuf Pointer to the NET_BUF to free. + +**/ +VOID +MnpFreeNbuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN OUT NET_BUF *Nbuf + ) +{ + EFI_TPL OldTpl; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + ASSERT (Nbuf->RefCnt > 1); + + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + + NET_PUT_REF (Nbuf); + + if (Nbuf->RefCnt == 1) { + // + // Trim all buffer contained in the Nbuf, then append it to the NbufQue. + // + NetbufTrim (Nbuf, Nbuf->TotalSize, NET_BUF_TAIL); + + if (NetbufAllocSpace (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_HEAD) != NULL) { + // + // There is space reserved for vlan tag in the head, reclaim it + // + NetbufTrim (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_TAIL); + } + + NetbufQueAppend (&MnpDeviceData->FreeNbufQue, Nbuf); + } + + gBS->RestoreTPL (OldTpl); +} + +/** + Add Count of TX buffers to MnpDeviceData->AllTxBufList and MnpDeviceData->FreeTxBufList. + The length of the buffer is specified by MnpDeviceData->BufferLength. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + @param[in] Count Number of TX buffers to add. + + @retval EFI_SUCCESS The specified amount of TX buffers are allocated. + @retval EFI_OUT_OF_RESOURCES Failed to allocate a TX buffer. + +**/ +EFI_STATUS +MnpAddFreeTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN UINTN Count + ) +{ + EFI_STATUS Status; + UINT32 Index; + MNP_TX_BUF_WRAP *TxBufWrap; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + ASSERT ((Count > 0) && (MnpDeviceData->BufferLength > 0)); + + Status = EFI_SUCCESS; + for (Index = 0; Index < Count; Index++) { + TxBufWrap = (MNP_TX_BUF_WRAP*) AllocatePool (OFFSET_OF (MNP_TX_BUF_WRAP, TxBuf) + MnpDeviceData->BufferLength ); + if (TxBufWrap == NULL) { + DEBUG ((EFI_D_ERROR, "MnpAddFreeTxBuf: TxBuf Alloc failed.\n")); + + Status = EFI_OUT_OF_RESOURCES; + break; + } + DEBUG ((EFI_D_INFO, "MnpAddFreeTxBuf: Add TxBufWrap %p, TxBuf %p\n", TxBufWrap, TxBufWrap->TxBuf)); + TxBufWrap->Signature = MNP_TX_BUF_WRAP_SIGNATURE; + TxBufWrap->InUse = FALSE; + InsertTailList (&MnpDeviceData->FreeTxBufList, &TxBufWrap->WrapEntry); + InsertTailList (&MnpDeviceData->AllTxBufList, &TxBufWrap->AllEntry); + } + + MnpDeviceData->TxBufCount += Index; + return Status; +} + +/** + Allocate a free TX buffer from MnpDeviceData->FreeTxBufList. If there is none + in the queue, first try to recycle some from SNP, then try to allocate some and add + them into the queue, then fetch the NET_BUF from the updated FreeTxBufList. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + + @return Pointer to the allocated free NET_BUF structure, if NULL the + operation is failed. + +**/ +UINT8 * +MnpAllocTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ) +{ + EFI_TPL OldTpl; + UINT8 *TxBuf; + EFI_STATUS Status; + LIST_ENTRY *Entry; + MNP_TX_BUF_WRAP *TxBufWrap; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (IsListEmpty (&MnpDeviceData->FreeTxBufList)) { + // + // First try to recycle some TX buffer from SNP + // + Status = MnpRecycleTxBuf (MnpDeviceData); + if (EFI_ERROR (Status)) { + TxBuf = NULL; + goto ON_EXIT; + } + + // + // If still no free TX buffer, allocate more. + // + if (IsListEmpty (&MnpDeviceData->FreeTxBufList)) { + if ((MnpDeviceData->TxBufCount + MNP_TX_BUFFER_INCREASEMENT) > MNP_MAX_TX_BUFFER_NUM) { + DEBUG ( + (EFI_D_ERROR, + "MnpAllocTxBuf: The maximum TxBuf size is reached for MNP driver instance %p.\n", + MnpDeviceData) + ); + + TxBuf = NULL; + goto ON_EXIT; + } + + Status = MnpAddFreeTxBuf (MnpDeviceData, MNP_TX_BUFFER_INCREASEMENT); + if (IsListEmpty (&MnpDeviceData->FreeTxBufList)) { + DEBUG ( + (EFI_D_ERROR, + "MnpAllocNbuf: Failed to add TxBuf into the FreeTxBufList, %r.\n", + Status) + ); + + TxBuf = NULL; + goto ON_EXIT; + } + } + } + + ASSERT (!IsListEmpty (&MnpDeviceData->FreeTxBufList)); + Entry = MnpDeviceData->FreeTxBufList.ForwardLink; + RemoveEntryList (MnpDeviceData->FreeTxBufList.ForwardLink); + TxBufWrap = NET_LIST_USER_STRUCT_S (Entry, MNP_TX_BUF_WRAP, WrapEntry, MNP_TX_BUF_WRAP_SIGNATURE); + TxBufWrap->InUse = TRUE; + TxBuf = TxBufWrap->TxBuf; + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return TxBuf; +} + +/** + Try to reclaim the TX buffer into the buffer pool. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in, out] TxBuf Pointer to the TX buffer to free. + +**/ +VOID +MnpFreeTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN OUT UINT8 *TxBuf + ) +{ + MNP_TX_BUF_WRAP *TxBufWrap; + EFI_TPL OldTpl; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + if (TxBuf == NULL) { + return; + } + + TxBufWrap = NET_LIST_USER_STRUCT (TxBuf, MNP_TX_BUF_WRAP, TxBuf); + if (TxBufWrap->Signature != MNP_TX_BUF_WRAP_SIGNATURE) { + DEBUG ( + (EFI_D_ERROR, + "MnpFreeTxBuf: Signature check failed in MnpFreeTxBuf.\n") + ); + return; + } + + if (!TxBufWrap->InUse) { + DEBUG ( + (EFI_D_WARN, + "MnpFreeTxBuf: Duplicated recycle report from SNP.\n") + ); + return; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + InsertTailList (&MnpDeviceData->FreeTxBufList, &TxBufWrap->WrapEntry); + TxBufWrap->InUse = FALSE; + gBS->RestoreTPL (OldTpl); +} + +/** + Try to recycle all the transmitted buffer address from SNP. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS Successed to recyclethe transmitted buffer address. + @retval Others Failed to recyclethe transmitted buffer address. + +**/ +EFI_STATUS +MnpRecycleTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ) +{ + UINT8 *TxBuf; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_STATUS Status; + + Snp = MnpDeviceData->Snp; + ASSERT (Snp != NULL); + + do { + TxBuf = NULL; + Status = Snp->GetStatus (Snp, NULL, (VOID **) &TxBuf); + if (EFI_ERROR (Status)) { + return Status; + } + + if (TxBuf != NULL) { + MnpFreeTxBuf (MnpDeviceData, TxBuf); + } + } while (TxBuf != NULL); + + return EFI_SUCCESS; +} + +/** + Initialize the mnp device context data. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in] ImageHandle The driver image handle. + @param[in] ControllerHandle Handle of device to bind driver to. + + @retval EFI_SUCCESS The mnp service context is initialized. + @retval EFI_UNSUPPORTED ControllerHandle does not support Simple Network Protocol. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpInitializeDeviceData ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + + MnpDeviceData->Signature = MNP_DEVICE_DATA_SIGNATURE; + MnpDeviceData->ImageHandle = ImageHandle; + MnpDeviceData->ControllerHandle = ControllerHandle; + + // + // Copy the MNP Protocol interfaces from the template. + // + CopyMem (&MnpDeviceData->VlanConfig, &mVlanConfigProtocolTemplate, sizeof (EFI_VLAN_CONFIG_PROTOCOL)); + + // + // Open the Simple Network protocol. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiSimpleNetworkProtocolGuid, + (VOID **) &Snp, + ImageHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + // + // Get MTU from Snp. + // + SnpMode = Snp->Mode; + MnpDeviceData->Snp = Snp; + + // + // Initialize the lists. + // + InitializeListHead (&MnpDeviceData->ServiceList); + InitializeListHead (&MnpDeviceData->GroupAddressList); + + // + // Get the buffer length used to allocate NET_BUF to hold data received + // from SNP. Do this before fill the FreeNetBufQue. + // + // + MnpDeviceData->BufferLength = SnpMode->MediaHeaderSize + NET_VLAN_TAG_LEN + SnpMode->MaxPacketSize + NET_ETHER_FCS_SIZE; + + // + // Make sure the protocol headers immediately following the media header + // 4-byte aligned, and also preserve additional space for VLAN tag + // + MnpDeviceData->PaddingSize = ((4 - SnpMode->MediaHeaderSize) & 0x3) + NET_VLAN_TAG_LEN; + + // + // Initialize MAC string which will be used as VLAN configuration variable name + // + Status = NetLibGetMacString (ControllerHandle, ImageHandle, &MnpDeviceData->MacString); + if (EFI_ERROR (Status)) { + goto ERROR; + } + + // + // Initialize the FreeNetBufQue and pre-allocate some NET_BUFs. + // + NetbufQueInit (&MnpDeviceData->FreeNbufQue); + Status = MnpAddFreeNbuf (MnpDeviceData, MNP_INIT_NET_BUFFER_NUM); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: MnpAddFreeNbuf failed, %r.\n", Status)); + + goto ERROR; + } + + // + // Get one NET_BUF from the FreeNbufQue for rx cache. + // + MnpDeviceData->RxNbufCache = MnpAllocNbuf (MnpDeviceData); + NetbufAllocSpace ( + MnpDeviceData->RxNbufCache, + MnpDeviceData->BufferLength, + NET_BUF_TAIL + ); + + // + // Allocate buffer pool for tx. + // + InitializeListHead (&MnpDeviceData->FreeTxBufList); + InitializeListHead (&MnpDeviceData->AllTxBufList); + MnpDeviceData->TxBufCount = 0; + + // + // Create the system poll timer. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + MnpSystemPoll, + MnpDeviceData, + &MnpDeviceData->PollTimer + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: CreateEvent for poll timer failed.\n")); + + goto ERROR; + } + + // + // Create the timer for packet timeout check. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + MnpCheckPacketTimeout, + MnpDeviceData, + &MnpDeviceData->TimeoutCheckTimer + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: CreateEvent for packet timeout check failed.\n")); + + goto ERROR; + } + + // + // Create the timer for media detection. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + MnpCheckMediaStatus, + MnpDeviceData, + &MnpDeviceData->MediaDetectTimer + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: CreateEvent for media detection failed.\n")); + + goto ERROR; + } + +ERROR: + if (EFI_ERROR (Status)) { + // + // Free the dynamic allocated resources if necessary. + // + if (MnpDeviceData->MacString != NULL) { + FreePool (MnpDeviceData->MacString); + } + + if (MnpDeviceData->TimeoutCheckTimer != NULL) { + gBS->CloseEvent (MnpDeviceData->TimeoutCheckTimer); + } + + if (MnpDeviceData->MediaDetectTimer != NULL) { + gBS->CloseEvent (MnpDeviceData->MediaDetectTimer); + } + + if (MnpDeviceData->PollTimer != NULL) { + gBS->CloseEvent (MnpDeviceData->PollTimer); + } + + if (MnpDeviceData->RxNbufCache != NULL) { + MnpFreeNbuf (MnpDeviceData, MnpDeviceData->RxNbufCache); + } + + if (MnpDeviceData->FreeNbufQue.BufNum != 0) { + NetbufQueFlush (&MnpDeviceData->FreeNbufQue); + } + + // + // Close the Simple Network Protocol. + // + gBS->CloseProtocol ( + ControllerHandle, + &gEfiSimpleNetworkProtocolGuid, + ImageHandle, + ControllerHandle + ); + } + + return Status; +} + + +/** + Destroy the MNP device context data. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in] ImageHandle The driver image handle. + +**/ +VOID +MnpDestroyDeviceData ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN EFI_HANDLE ImageHandle + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + MNP_TX_BUF_WRAP *TxBufWrap; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + // + // Free Vlan Config variable name string + // + if (MnpDeviceData->MacString != NULL) { + FreePool (MnpDeviceData->MacString); + } + + // + // The GroupAddressList must be empty. + // + ASSERT (IsListEmpty (&MnpDeviceData->GroupAddressList)); + + // + // Close the event. + // + gBS->CloseEvent (MnpDeviceData->TimeoutCheckTimer); + gBS->CloseEvent (MnpDeviceData->MediaDetectTimer); + gBS->CloseEvent (MnpDeviceData->PollTimer); + + // + // Free the Tx buffer pool. + // + NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, &MnpDeviceData->AllTxBufList) { + TxBufWrap = NET_LIST_USER_STRUCT (Entry, MNP_TX_BUF_WRAP, AllEntry); + RemoveEntryList (Entry); + FreePool (TxBufWrap); + MnpDeviceData->TxBufCount--; + } + ASSERT (IsListEmpty (&MnpDeviceData->AllTxBufList)); + ASSERT (MnpDeviceData->TxBufCount == 0); + + // + // Free the RxNbufCache. + // + MnpFreeNbuf (MnpDeviceData, MnpDeviceData->RxNbufCache); + + // + // Flush the FreeNbufQue. + // + MnpDeviceData->NbufCnt -= MnpDeviceData->FreeNbufQue.BufNum; + NetbufQueFlush (&MnpDeviceData->FreeNbufQue); + + // + // Close the Simple Network Protocol. + // + gBS->CloseProtocol ( + MnpDeviceData->ControllerHandle, + &gEfiSimpleNetworkProtocolGuid, + ImageHandle, + MnpDeviceData->ControllerHandle + ); +} + + +/** + Create mnp service context data. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + @param[in] VlanId The VLAN ID. + @param[in] Priority The VLAN priority. If VlanId is 0, + Priority is ignored. + + @return A pointer to MNP_SERVICE_DATA or NULL if failed to create MNP service context. + +**/ +MNP_SERVICE_DATA * +MnpCreateServiceData ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINT16 VlanId, + IN UINT8 Priority OPTIONAL + ) +{ + EFI_HANDLE MnpServiceHandle; + MNP_SERVICE_DATA *MnpServiceData; + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + + // + // Initialize the Mnp Service Data. + // + MnpServiceData = AllocateZeroPool (sizeof (MNP_SERVICE_DATA)); + if (MnpServiceData == NULL) { + DEBUG ((EFI_D_ERROR, "MnpCreateServiceData: Faild to allocate memory for the new Mnp Service Data.\n")); + + return NULL; + } + + // + // Add to MNP service list + // + InsertTailList (&MnpDeviceData->ServiceList, &MnpServiceData->Link); + + MnpServiceData->Signature = MNP_SERVICE_DATA_SIGNATURE; + MnpServiceData->MnpDeviceData = MnpDeviceData; + + // + // Copy the ServiceBinding structure. + // + CopyMem (&MnpServiceData->ServiceBinding, &mMnpServiceBindingProtocol, sizeof (EFI_SERVICE_BINDING_PROTOCOL)); + + // + // Initialize the lists. + // + InitializeListHead (&MnpServiceData->ChildrenList); + + SnpMode = MnpDeviceData->Snp->Mode; + if (VlanId != 0) { + // + // Create VLAN child handle + // + MnpServiceHandle = MnpCreateVlanChild ( + MnpDeviceData->ImageHandle, + MnpDeviceData->ControllerHandle, + VlanId, + &MnpServiceData->DevicePath + ); + if (MnpServiceHandle == NULL) { + DEBUG ((EFI_D_ERROR, "MnpCreateServiceData: Faild to create child handle.\n")); + + return NULL; + } + + // + // Open VLAN Config Protocol by child + // + Status = gBS->OpenProtocol ( + MnpDeviceData->ControllerHandle, + &gEfiVlanConfigProtocolGuid, + (VOID **) &VlanConfig, + MnpDeviceData->ImageHandle, + MnpServiceHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Reduce MTU for VLAN device + // + MnpServiceData->Mtu = SnpMode->MaxPacketSize - NET_VLAN_TAG_LEN; + } else { + // + // VlanId set to 0 means rx/tx untagged frame + // + MnpServiceHandle = MnpDeviceData->ControllerHandle; + MnpServiceData->Mtu = SnpMode->MaxPacketSize; + } + + MnpServiceData->ServiceHandle = MnpServiceHandle; + MnpServiceData->VlanId = VlanId; + MnpServiceData->Priority = Priority; + + // + // Install the MNP Service Binding Protocol + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &MnpServiceHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + &MnpServiceData->ServiceBinding, + NULL + ); + +Exit: + if (EFI_ERROR (Status)) { + MnpDestroyServiceData (MnpServiceData); + MnpServiceData = NULL; + } + + return MnpServiceData; +} + +/** + Destroy the MNP service context data. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + + @retval EFI_SUCCESS The mnp service context is destroyed. + @retval Others Errors as indicated. + +**/ +EFI_STATUS +MnpDestroyServiceData ( + IN OUT MNP_SERVICE_DATA *MnpServiceData + ) +{ + EFI_STATUS Status; + + // + // Uninstall the MNP Service Binding Protocol + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + MnpServiceData->ServiceHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + &MnpServiceData->ServiceBinding, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (MnpServiceData->VlanId != 0) { + // + // Close VlanConfig Protocol opened by VLAN child handle + // + Status = gBS->CloseProtocol ( + MnpServiceData->MnpDeviceData->ControllerHandle, + &gEfiVlanConfigProtocolGuid, + MnpServiceData->MnpDeviceData->ImageHandle, + MnpServiceData->ServiceHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Uninstall Device Path Protocol to destroy the VLAN child handle + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + MnpServiceData->ServiceHandle, + &gEfiDevicePathProtocolGuid, + MnpServiceData->DevicePath, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (MnpServiceData->DevicePath != NULL) { + FreePool (MnpServiceData->DevicePath); + } + } + + // + // Remove from MnpDeviceData service list + // + RemoveEntryList (&MnpServiceData->Link); + + FreePool (MnpServiceData); + + return Status; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +MnpDestoryChildEntry ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + MNP_INSTANCE_DATA *Instance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + + ServiceBinding = (EFI_SERVICE_BINDING_PROTOCOL *) Context; + Instance = CR (Entry, MNP_INSTANCE_DATA, InstEntry, MNP_INSTANCE_DATA_SIGNATURE); + return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle); +} + +/** + Destroy all child of the MNP service data. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + + @retval EFI_SUCCESS All child are destroyed. + @retval Others Failed to destroy all child. + +**/ +EFI_STATUS +MnpDestroyServiceChild ( + IN OUT MNP_SERVICE_DATA *MnpServiceData + ) +{ + LIST_ENTRY *List; + EFI_STATUS Status; + UINTN ListLength; + + List = &MnpServiceData->ChildrenList; + + Status = NetDestroyLinkList ( + List, + MnpDestoryChildEntry, + &MnpServiceData->ServiceBinding, + &ListLength + ); + if (EFI_ERROR (Status) || ListLength != 0) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + +/** + Find the MNP Service Data for given VLAN ID. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + @param[in] VlanId The VLAN ID. + + @return A pointer to MNP_SERVICE_DATA or NULL if not found. + +**/ +MNP_SERVICE_DATA * +MnpFindServiceData ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINT16 VlanId + ) +{ + LIST_ENTRY *Entry; + MNP_SERVICE_DATA *MnpServiceData; + + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->ServiceList) { + // + // Check VLAN ID of each Mnp Service Data + // + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + if (MnpServiceData->VlanId == VlanId) { + return MnpServiceData; + } + } + + return NULL; +} + +/** + Initialize the mnp instance context data. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in, out] Instance Pointer to the mnp instance context data + to initialize. + +**/ +VOID +MnpInitializeInstanceData ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN OUT MNP_INSTANCE_DATA *Instance + ) +{ + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + ASSERT (Instance != NULL); + + // + // Set the signature. + // + Instance->Signature = MNP_INSTANCE_DATA_SIGNATURE; + + // + // Copy the MNP Protocol interfaces from the template. + // + CopyMem (&Instance->ManagedNetwork, &mMnpProtocolTemplate, sizeof (Instance->ManagedNetwork)); + + // + // Copy the default config data. + // + CopyMem (&Instance->ConfigData, &mMnpDefaultConfigData, sizeof (Instance->ConfigData)); + + // + // Initialize the lists. + // + InitializeListHead (&Instance->GroupCtrlBlkList); + InitializeListHead (&Instance->RcvdPacketQueue); + InitializeListHead (&Instance->RxDeliveredPacketQueue); + + // + // Initialize the RxToken Map. + // + NetMapInit (&Instance->RxTokenMap); + + // + // Save the MnpServiceData info. + // + Instance->MnpServiceData = MnpServiceData; +} + + +/** + Check whether the token specified by Arg matches the token in Item. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg, it's a pointer to the token to + check. + + @retval EFI_SUCCESS The token specified by Arg is different from the + token in Item. + @retval EFI_ACCESS_DENIED The token specified by Arg is the same as that in + Item. + +**/ +EFI_STATUS +EFIAPI +MnpTokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg + ) +{ + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token; + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TokenInItem; + + Token = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Arg; + TokenInItem = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Item->Key; + + if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) { + // + // The token is the same either the two tokens equals or the Events in + // the two tokens are the same. + // + return EFI_ACCESS_DENIED; + } + + return EFI_SUCCESS; +} + +/** + Cancel the token specified by Arg if it matches the token in Item. + + @param[in, out] Map Pointer to the NET_MAP. + @param[in, out] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg, it's a pointer to the + token to cancel. + + @retval EFI_SUCCESS The Arg is NULL, and the token in Item is cancelled, + or the Arg isn't NULL, and the token in Item is + different from the Arg. + @retval EFI_ABORTED The Arg isn't NULL, the token in Item mathces the + Arg, and the token is cancelled. + +**/ +EFI_STATUS +EFIAPI +MnpCancelTokens ( + IN OUT NET_MAP *Map, + IN OUT NET_MAP_ITEM *Item, + IN VOID *Arg + ) +{ + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TokenToCancel; + + if ((Arg != NULL) && (Item->Key != Arg)) { + // + // The token in Item is not the token specified by Arg. + // + return EFI_SUCCESS; + } + + TokenToCancel = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Item->Key; + + // + // Remove the item from the map. + // + NetMapRemoveItem (Map, Item, NULL); + + // + // Cancel this token with status set to EFI_ABORTED. + // + TokenToCancel->Status = EFI_ABORTED; + gBS->SignalEvent (TokenToCancel->Event); + + if (Arg != NULL) { + // + // Only abort the token specified by Arg if Arg isn't NULL. + // + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + + +/** + Start and initialize the simple network. + + @param[in] Snp Pointer to the simple network protocol. + + @retval EFI_SUCCESS The simple network protocol is started. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpStartSnp ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp + ) +{ + EFI_STATUS Status; + + ASSERT (Snp != NULL); + + // + // Start the simple network. + // + Status = Snp->Start (Snp); + + if (!EFI_ERROR (Status)) { + // + // Initialize the simple network. + // + Status = Snp->Initialize (Snp, 0, 0); + } + + return Status; +} + + +/** + Stop the simple network. + + @param[in] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + + @retval EFI_SUCCESS The simple network is stopped. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpStopSnp ( + IN MNP_DEVICE_DATA *MnpDeviceData + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + + Snp = MnpDeviceData->Snp; + ASSERT (Snp != NULL); + + // + // Recycle all the transmit buffer from SNP. + // + Status = MnpRecycleTxBuf (MnpDeviceData); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Shut down the simple network. + // + Status = Snp->Shutdown (Snp); + if (!EFI_ERROR (Status)) { + // + // Stop the simple network. + // + Status = Snp->Stop (Snp); + } + + return Status; +} + + +/** + Start the managed network, this function is called when one instance is configured + or reconfigured. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + @param[in] IsConfigUpdate The instance is reconfigured or it's the first + time the instanced is configured. + @param[in] EnableSystemPoll Enable the system polling or not. + + @retval EFI_SUCCESS The managed network is started and some + configuration is updated. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpStart ( + IN OUT MNP_SERVICE_DATA *MnpServiceData, + IN BOOLEAN IsConfigUpdate, + IN BOOLEAN EnableSystemPoll + ) +{ + EFI_STATUS Status; + EFI_TIMER_DELAY TimerOpType; + MNP_DEVICE_DATA *MnpDeviceData; + + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + + Status = EFI_SUCCESS; + MnpDeviceData = MnpServiceData->MnpDeviceData; + + if (!IsConfigUpdate) { + // + // If it's not a configuration update, increase the configured children number. + // + MnpDeviceData->ConfiguredChildrenNumber++; + + if (MnpDeviceData->ConfiguredChildrenNumber == 1) { + // + // It's the first configured child, start the simple network. + // + Status = MnpStartSnp (MnpDeviceData->Snp); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpStart: MnpStartSnp failed, %r.\n", Status)); + + goto ErrorExit; + } + + // + // Start the timeout timer. + // + Status = gBS->SetTimer ( + MnpDeviceData->TimeoutCheckTimer, + TimerPeriodic, + MNP_TIMEOUT_CHECK_INTERVAL + ); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpStart, gBS->SetTimer for TimeoutCheckTimer %r.\n", + Status) + ); + + goto ErrorExit; + } + + // + // Start the media detection timer. + // + Status = gBS->SetTimer ( + MnpDeviceData->MediaDetectTimer, + TimerPeriodic, + MNP_MEDIA_DETECT_INTERVAL + ); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpStart, gBS->SetTimer for MediaDetectTimer %r.\n", + Status) + ); + + goto ErrorExit; + } + } + } + + if (MnpDeviceData->EnableSystemPoll ^ EnableSystemPoll) { + // + // The EnableSystemPoll differs with the current state, disable or enable + // the system poll. + // + TimerOpType = EnableSystemPoll ? TimerPeriodic : TimerCancel; + + Status = gBS->SetTimer (MnpDeviceData->PollTimer, TimerOpType, MNP_SYS_POLL_INTERVAL); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpStart: gBS->SetTimer for PollTimer failed, %r.\n", Status)); + + goto ErrorExit; + } + + MnpDeviceData->EnableSystemPoll = EnableSystemPoll; + } + + // + // Change the receive filters if need. + // + Status = MnpConfigReceiveFilters (MnpDeviceData); + +ErrorExit: + return Status; +} + + +/** + Stop the managed network. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + + @retval EFI_SUCCESS The managed network is stopped. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpStop ( + IN OUT MNP_SERVICE_DATA *MnpServiceData + ) +{ + EFI_STATUS Status; + MNP_DEVICE_DATA *MnpDeviceData; + + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + MnpDeviceData = MnpServiceData->MnpDeviceData; + ASSERT (MnpDeviceData->ConfiguredChildrenNumber > 0); + + // + // Configure the receive filters. + // + MnpConfigReceiveFilters (MnpDeviceData); + + // + // Decrease the children number. + // + MnpDeviceData->ConfiguredChildrenNumber--; + + if (MnpDeviceData->ConfiguredChildrenNumber > 0) { + // + // If there are other configured chilren, return and keep the timers and + // simple network unchanged. + // + return EFI_SUCCESS; + } + + // + // No configured children now. + // + if (MnpDeviceData->EnableSystemPoll) { + // + // The system poll in on, cancel the poll timer. + // + Status = gBS->SetTimer (MnpDeviceData->PollTimer, TimerCancel, 0); + MnpDeviceData->EnableSystemPoll = FALSE; + } + + // + // Cancel the timeout timer. + // + Status = gBS->SetTimer (MnpDeviceData->TimeoutCheckTimer, TimerCancel, 0); + + // + // Cancel the media detect timer. + // + Status = gBS->SetTimer (MnpDeviceData->MediaDetectTimer, TimerCancel, 0); + + // + // Stop the simple network. + // + Status = MnpStopSnp (MnpDeviceData); + return Status; +} + + +/** + Flush the instance's received data. + + @param[in, out] Instance Pointer to the mnp instance context data. + +**/ +VOID +MnpFlushRcvdDataQueue ( + IN OUT MNP_INSTANCE_DATA *Instance + ) +{ + EFI_TPL OldTpl; + MNP_RXDATA_WRAP *RxDataWrap; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + + while (!IsListEmpty (&Instance->RcvdPacketQueue)) { + // + // Remove all the Wraps. + // + RxDataWrap = NET_LIST_HEAD (&Instance->RcvdPacketQueue, MNP_RXDATA_WRAP, WrapEntry); + + // + // Recycle the RxDataWrap. + // + MnpRecycleRxData (NULL, (VOID *) RxDataWrap); + Instance->RcvdPacketQueueSize--; + } + + ASSERT (Instance->RcvdPacketQueueSize == 0); + + gBS->RestoreTPL (OldTpl); +} + + +/** + Configure the Instance using ConfigData. + + @param[in, out] Instance Pointer to the mnp instance context data. + @param[in] ConfigData Pointer to the configuration data used to configure + the isntance. + + @retval EFI_SUCCESS The Instance is configured. + @retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the + implementation doesn't support it. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpConfigureInstance ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData OPTIONAL + ) +{ + EFI_STATUS Status; + MNP_SERVICE_DATA *MnpServiceData; + MNP_DEVICE_DATA *MnpDeviceData; + EFI_MANAGED_NETWORK_CONFIG_DATA *OldConfigData; + EFI_MANAGED_NETWORK_CONFIG_DATA *NewConfigData; + BOOLEAN IsConfigUpdate; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + if ((ConfigData != NULL) && ConfigData->EnableReceiveTimestamps) { + // + // Don't support timestamp. + // + return EFI_UNSUPPORTED; + } + + Status = EFI_SUCCESS; + + MnpServiceData = Instance->MnpServiceData; + MnpDeviceData = MnpServiceData->MnpDeviceData; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + IsConfigUpdate = (BOOLEAN) ((Instance->Configured) && (ConfigData != NULL)); + + OldConfigData = &Instance->ConfigData; + NewConfigData = ConfigData; + if (NewConfigData == NULL) { + // + // Restore back the default config data if a reset of this instance + // is required. + // + NewConfigData = &mMnpDefaultConfigData; + } + + // + // Reset the instance's receive filter. + // + Instance->ReceiveFilter = 0; + + // + // Clear the receive counters according to the old ConfigData. + // + if (OldConfigData->EnableUnicastReceive) { + MnpDeviceData->UnicastCount--; + } + + if (OldConfigData->EnableMulticastReceive) { + MnpDeviceData->MulticastCount--; + } + + if (OldConfigData->EnableBroadcastReceive) { + MnpDeviceData->BroadcastCount--; + } + + if (OldConfigData->EnablePromiscuousReceive) { + MnpDeviceData->PromiscuousCount--; + } + + // + // Set the receive filter counters and the receive filter of the + // instance according to the new ConfigData. + // + if (NewConfigData->EnableUnicastReceive) { + MnpDeviceData->UnicastCount++; + Instance->ReceiveFilter |= MNP_RECEIVE_UNICAST; + } + + if (NewConfigData->EnableMulticastReceive) { + MnpDeviceData->MulticastCount++; + } + + if (NewConfigData->EnableBroadcastReceive) { + MnpDeviceData->BroadcastCount++; + Instance->ReceiveFilter |= MNP_RECEIVE_BROADCAST; + } + + if (NewConfigData->EnablePromiscuousReceive) { + MnpDeviceData->PromiscuousCount++; + } + + if (OldConfigData->FlushQueuesOnReset) { + MnpFlushRcvdDataQueue (Instance); + } + + if (ConfigData == NULL) { + Instance->ManagedNetwork.Cancel (&Instance->ManagedNetwork, NULL); + } + + if (!NewConfigData->EnableMulticastReceive) { + MnpGroupOp (Instance, FALSE, NULL, NULL); + } + + // + // Save the new configuration data. + // + CopyMem (OldConfigData, NewConfigData, sizeof (*OldConfigData)); + + Instance->Configured = (BOOLEAN) (ConfigData != NULL); + if (Instance->Configured) { + // + // The instance is configured, start the Mnp. + // + Status = MnpStart ( + MnpServiceData, + IsConfigUpdate, + (BOOLEAN) !NewConfigData->DisableBackgroundPolling + ); + } else { + // + // The instance is changed to the unconfigured state, stop the Mnp. + // + Status = MnpStop (MnpServiceData); + } + + return Status; +} + +/** + Configure the Snp receive filters according to the instances' receive filter + settings. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS The receive filters is configured. + @retval EFI_OUT_OF_RESOURCES The receive filters can't be configured due + to lack of memory resource. + +**/ +EFI_STATUS +MnpConfigReceiveFilters ( + IN MNP_DEVICE_DATA *MnpDeviceData + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_MAC_ADDRESS *MCastFilter; + UINT32 MCastFilterCnt; + UINT32 EnableFilterBits; + UINT32 DisableFilterBits; + BOOLEAN ResetMCastFilters; + LIST_ENTRY *Entry; + UINT32 Index; + MNP_GROUP_ADDRESS *GroupAddress; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + Snp = MnpDeviceData->Snp; + + // + // Initialize the enable filter and disable filter. + // + EnableFilterBits = 0; + DisableFilterBits = Snp->Mode->ReceiveFilterMask; + + if (MnpDeviceData->UnicastCount != 0) { + // + // Enable unicast if any instance wants to receive unicast. + // + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST; + } + + if (MnpDeviceData->BroadcastCount != 0) { + // + // Enable broadcast if any instance wants to receive broadcast. + // + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST; + } + + MCastFilter = NULL; + MCastFilterCnt = 0; + ResetMCastFilters = TRUE; + + if ((MnpDeviceData->MulticastCount != 0) && (MnpDeviceData->GroupAddressCount != 0)) { + // + // There are instances configured to receive multicast and already some group + // addresses are joined. + // + + ResetMCastFilters = FALSE; + + if (MnpDeviceData->GroupAddressCount <= Snp->Mode->MaxMCastFilterCount) { + // + // The joind group address is less than simple network's maximum count. + // Just configure the snp to do the multicast filtering. + // + + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST; + + // + // Allocate pool for the mulicast addresses. + // + MCastFilterCnt = MnpDeviceData->GroupAddressCount; + MCastFilter = AllocatePool (sizeof (EFI_MAC_ADDRESS) * MCastFilterCnt); + if (MCastFilter == NULL) { + DEBUG ((EFI_D_ERROR, "MnpConfigReceiveFilters: Failed to allocate memory resource for MCastFilter.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + // + // Fill the multicast HW address buffer. + // + Index = 0; + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->GroupAddressList) { + + GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry); + CopyMem (MCastFilter + Index, &GroupAddress->Address, sizeof (*(MCastFilter + Index))); + Index++; + + ASSERT (Index <= MCastFilterCnt); + } + } else { + // + // The maximum multicast is reached, set the filter to be promiscuous + // multicast. + // + + if ((Snp->Mode->ReceiveFilterMask & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) != 0) { + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST; + } else { + // + // Either MULTICAST or PROMISCUOUS_MULTICAST is not supported by Snp, + // set the NIC to be promiscuous although this will tremendously degrade + // the performance. + // + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS; + } + } + } + + if (MnpDeviceData->PromiscuousCount != 0) { + // + // Enable promiscuous if any instance wants to receive promiscuous. + // + EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS; + } + + // + // Set the disable filter. + // + DisableFilterBits ^= EnableFilterBits; + + // + // Configure the receive filters of SNP. + // + Status = Snp->ReceiveFilters ( + Snp, + EnableFilterBits, + DisableFilterBits, + ResetMCastFilters, + MCastFilterCnt, + MCastFilter + ); + DEBUG_CODE ( + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpConfigReceiveFilters: Snp->ReceiveFilters failed, %r.\n", + Status) + ); + } + ); + + if (MCastFilter != NULL) { + // + // Free the buffer used to hold the group addresses. + // + FreePool (MCastFilter); + } + + return Status; +} + + +/** + Add a group address control block which controls the MacAddress for + this instance. + + @param[in, out] Instance Pointer to the mnp instance context data. + @param[in, out] CtrlBlk Pointer to the group address control block. + @param[in, out] GroupAddress Pointer to the group adress. + @param[in] MacAddress Pointer to the mac address. + @param[in] HwAddressSize The hardware address size. + + @retval EFI_SUCCESS The group address control block is added. + @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources. + +**/ +EFI_STATUS +MnpGroupOpAddCtrlBlk ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN OUT MNP_GROUP_CONTROL_BLOCK *CtrlBlk, + IN OUT MNP_GROUP_ADDRESS *GroupAddress OPTIONAL, + IN EFI_MAC_ADDRESS *MacAddress, + IN UINT32 HwAddressSize + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + MnpDeviceData = Instance->MnpServiceData->MnpDeviceData; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + if (GroupAddress == NULL) { + ASSERT (MacAddress != NULL); + + // + // Allocate a new GroupAddress to be added into MNP's GroupAddressList. + // + GroupAddress = AllocatePool (sizeof (MNP_GROUP_ADDRESS)); + if (GroupAddress == NULL) { + + DEBUG ((EFI_D_ERROR, "MnpGroupOpFormCtrlBlk: Failed to allocate memory resource.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (&GroupAddress->Address, MacAddress, sizeof (GroupAddress->Address)); + GroupAddress->RefCnt = 0; + InsertTailList ( + &MnpDeviceData->GroupAddressList, + &GroupAddress->AddrEntry + ); + MnpDeviceData->GroupAddressCount++; + } + + // + // Increase the RefCnt. + // + GroupAddress->RefCnt++; + + // + // Add the CtrlBlk into the instance's GroupCtrlBlkList. + // + CtrlBlk->GroupAddress = GroupAddress; + InsertTailList (&Instance->GroupCtrlBlkList, &CtrlBlk->CtrlBlkEntry); + + return EFI_SUCCESS; +} + + +/** + Delete a group control block from the instance. If the controlled group address's + reference count reaches zero, the group address is removed too. + + @param[in] Instance Pointer to the instance context data. + @param[in] CtrlBlk Pointer to the group control block to delete. + + @return The group address controlled by the control block is no longer used or not. + +**/ +BOOLEAN +MnpGroupOpDelCtrlBlk ( + IN MNP_INSTANCE_DATA *Instance, + IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + MNP_GROUP_ADDRESS *GroupAddress; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + MnpDeviceData = Instance->MnpServiceData->MnpDeviceData; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + // + // Remove and free the CtrlBlk. + // + GroupAddress = CtrlBlk->GroupAddress; + RemoveEntryList (&CtrlBlk->CtrlBlkEntry); + FreePool (CtrlBlk); + + ASSERT (GroupAddress->RefCnt > 0); + + // + // Count down the RefCnt. + // + GroupAddress->RefCnt--; + + if (GroupAddress->RefCnt == 0) { + // + // Free this GroupAddress entry if no instance uses it. + // + MnpDeviceData->GroupAddressCount--; + RemoveEntryList (&GroupAddress->AddrEntry); + FreePool (GroupAddress); + + return TRUE; + } + + return FALSE; +} + + +/** + Do the group operations for this instance. + + @param[in, out] Instance Pointer to the instance context data. + @param[in] JoinFlag Set to TRUE to join a group. Set to TRUE to + leave a group/groups. + @param[in] MacAddress Pointer to the group address to join or leave. + @param[in] CtrlBlk Pointer to the group control block if JoinFlag + is FALSE. + + @retval EFI_SUCCESS The group operation finished. + @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpGroupOp ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN BOOLEAN JoinFlag, + IN EFI_MAC_ADDRESS *MacAddress OPTIONAL, + IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk OPTIONAL + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + MNP_GROUP_ADDRESS *GroupAddress; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + MNP_GROUP_CONTROL_BLOCK *NewCtrlBlk; + EFI_STATUS Status; + BOOLEAN AddressExist; + BOOLEAN NeedUpdate; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + MnpDeviceData = Instance->MnpServiceData->MnpDeviceData; + SnpMode = MnpDeviceData->Snp->Mode; + + if (JoinFlag) { + // + // A new gropu address is to be added. + // + GroupAddress = NULL; + AddressExist = FALSE; + + // + // Allocate memory for the control block. + // + NewCtrlBlk = AllocatePool (sizeof (MNP_GROUP_CONTROL_BLOCK)); + if (NewCtrlBlk == NULL) { + DEBUG ((EFI_D_ERROR, "MnpGroupOp: Failed to allocate memory resource.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->GroupAddressList) { + // + // Check whether the MacAddress is already joined by other instances. + // + GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry); + if (CompareMem (MacAddress, &GroupAddress->Address, SnpMode->HwAddressSize) == 0) { + AddressExist = TRUE; + break; + } + } + + if (!AddressExist) { + GroupAddress = NULL; + } + + // + // Add the GroupAddress for this instance. + // + Status = MnpGroupOpAddCtrlBlk ( + Instance, + NewCtrlBlk, + GroupAddress, + MacAddress, + SnpMode->HwAddressSize + ); + if (EFI_ERROR (Status)) { + return Status; + } + + NeedUpdate = TRUE; + } else { + if (MacAddress != NULL) { + ASSERT (CtrlBlk != NULL); + + // + // Leave the specific multicast mac address. + // + NeedUpdate = MnpGroupOpDelCtrlBlk (Instance, CtrlBlk); + } else { + // + // Leave all multicast mac addresses. + // + NeedUpdate = FALSE; + + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->GroupCtrlBlkList) { + + NewCtrlBlk = NET_LIST_USER_STRUCT ( + Entry, + MNP_GROUP_CONTROL_BLOCK, + CtrlBlkEntry + ); + // + // Update is required if the group address left is no longer used + // by other instances. + // + NeedUpdate = MnpGroupOpDelCtrlBlk (Instance, NewCtrlBlk); + } + } + } + + Status = EFI_SUCCESS; + + if (NeedUpdate) { + // + // Reconfigure the receive filters if necessary. + // + Status = MnpConfigReceiveFilters (MnpDeviceData); + } + + return Status; +} diff --git a/NetworkPkg/MnpDxe/MnpDriver.c b/NetworkPkg/MnpDxe/MnpDriver.c new file mode 100644 index 000000000..e99e7c5a6 --- /dev/null +++ b/NetworkPkg/MnpDxe/MnpDriver.c @@ -0,0 +1,683 @@ +/** @file + Implementation of driver entry point and driver binding protocol. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpDriver.h" +#include "MnpImpl.h" +#include "MnpVlan.h" + +EFI_DRIVER_BINDING_PROTOCOL gMnpDriverBinding = { + MnpDriverBindingSupported, + MnpDriverBindingStart, + MnpDriverBindingStop, + 0xa, + NULL, + NULL +}; + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +MnpDestroyServiceDataEntry ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + MNP_SERVICE_DATA *MnpServiceData; + + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + return MnpDestroyServiceData (MnpServiceData); +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +MnpDestroyServiceChildEntry ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + MNP_SERVICE_DATA *MnpServiceData; + + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + return MnpDestroyServiceChild (MnpServiceData); +} + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + + // + // Test to open the Simple Network protocol BY_DRIVER. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiSimpleNetworkProtocolGuid, + (VOID **) &Snp, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Close the openned SNP protocol. + // + gBS->CloseProtocol ( + ControllerHandle, + &gEfiSimpleNetworkProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + + return EFI_SUCCESS; +} + + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make drivers as small + as possible, there are a few calling restrictions for this service. + ConnectController() must follow these calling restrictions. If any other + agent wishes to call Start() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for Mnp Service Data. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + MNP_SERVICE_DATA *MnpServiceData; + MNP_DEVICE_DATA *MnpDeviceData; + LIST_ENTRY *Entry; + VLAN_TCI *VlanVariable; + UINTN NumberOfVlan; + UINTN Index; + + VlanVariable = NULL; + + // + // Initialize the Mnp Device Data + // + MnpDeviceData = AllocateZeroPool (sizeof (MNP_DEVICE_DATA)); + if (MnpDeviceData == NULL) { + DEBUG ((EFI_D_ERROR, "MnpDriverBindingStart(): Failed to allocate the Mnp Device Data.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + Status = MnpInitializeDeviceData (MnpDeviceData, This->DriverBindingHandle, ControllerHandle); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpDriverBindingStart: MnpInitializeDeviceData failed, %r.\n", Status)); + + FreePool (MnpDeviceData); + return Status; + } + + // + // Check whether NIC driver has already produced VlanConfig protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiVlanConfigProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + // + // NIC hardware already implement VLAN, + // no need to provide software VLAN implementation in MNP driver + // + MnpDeviceData->NumberOfVlan = 0; + ZeroMem (&MnpDeviceData->VlanConfig, sizeof (EFI_VLAN_CONFIG_PROTOCOL)); + MnpServiceData = MnpCreateServiceData (MnpDeviceData, 0, 0); + Status = (MnpServiceData != NULL) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES; + goto Exit; + } + + // + // Install VLAN Config Protocol + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiVlanConfigProtocolGuid, + &MnpDeviceData->VlanConfig, + NULL + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Get current VLAN configuration from EFI Variable + // + NumberOfVlan = 0; + Status = MnpGetVlanVariable (MnpDeviceData, &NumberOfVlan, &VlanVariable); + if (EFI_ERROR (Status)) { + // + // No VLAN is set, create a default MNP service data for untagged frame + // + MnpDeviceData->NumberOfVlan = 0; + MnpServiceData = MnpCreateServiceData (MnpDeviceData, 0, 0); + Status = (MnpServiceData != NULL) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES; + goto Exit; + } + + // + // Create MNP service data for each VLAN + // + MnpDeviceData->NumberOfVlan = NumberOfVlan; + for (Index = 0; Index < NumberOfVlan; Index++) { + MnpServiceData = MnpCreateServiceData ( + MnpDeviceData, + VlanVariable[Index].Bits.Vid, + (UINT8) VlanVariable[Index].Bits.Priority + ); + + if (MnpServiceData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + + goto Exit; + } + } + +Exit: + if (VlanVariable != NULL) { + FreePool (VlanVariable); + } + + if (EFI_ERROR (Status)) { + // + // Destroy all MNP service data + // + while (!IsListEmpty (&MnpDeviceData->ServiceList)) { + Entry = GetFirstNode (&MnpDeviceData->ServiceList); + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + MnpDestroyServiceData (MnpServiceData); + } + + // + // Uninstall the VLAN Config Protocol if any + // + if (MnpDeviceData->VlanConfig.Set != NULL) { + gBS->UninstallMultipleProtocolInterfaces ( + MnpDeviceData->ControllerHandle, + &gEfiVlanConfigProtocolGuid, + &MnpDeviceData->VlanConfig, + NULL + ); + } + + // + // Destroy Mnp Device Data + // + MnpDestroyDeviceData (MnpDeviceData, This->DriverBindingHandle); + FreePool (MnpDeviceData); + } + + return Status; +} + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to make drivers as + small as possible, there are a few calling restrictions for this service. + DisconnectController() must follow these calling restrictions. If any other + agent wishes to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If + number of children is zero stop the entire + bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + MNP_DEVICE_DATA *MnpDeviceData; + MNP_SERVICE_DATA *MnpServiceData; + LIST_ENTRY *List; + UINTN ListLength; + + // + // Try to retrieve MNP service binding protocol from the ControllerHandle + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + // + // Retrieve VLAN Config Protocol from the ControllerHandle + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiVlanConfigProtocolGuid, + (VOID **) &VlanConfig, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpDriverBindingStop: try to stop unknown Controller.\n")); + return EFI_DEVICE_ERROR; + } + + MnpDeviceData = MNP_DEVICE_DATA_FROM_THIS (VlanConfig); + } else { + MnpServiceData = MNP_SERVICE_DATA_FROM_THIS (ServiceBinding); + MnpDeviceData = MnpServiceData->MnpDeviceData; + } + + if (NumberOfChildren == 0) { + // + // Destroy all MNP service data + // + List = &MnpDeviceData->ServiceList; + Status = NetDestroyLinkList ( + List, + MnpDestroyServiceDataEntry, + NULL, + &ListLength + ); + if (EFI_ERROR (Status) || ListLength !=0) { + return EFI_DEVICE_ERROR; + } + + // + // Uninstall the VLAN Config Protocol if any + // + if (MnpDeviceData->VlanConfig.Set != NULL) { + gBS->UninstallMultipleProtocolInterfaces ( + MnpDeviceData->ControllerHandle, + &gEfiVlanConfigProtocolGuid, + &MnpDeviceData->VlanConfig, + NULL + ); + } + + // + // Destroy Mnp Device Data + // + MnpDestroyDeviceData (MnpDeviceData, This->DriverBindingHandle); + FreePool (MnpDeviceData); + + if (gMnpControllerNameTable != NULL) { + FreeUnicodeStringTable (gMnpControllerNameTable); + gMnpControllerNameTable = NULL; + } + return EFI_SUCCESS; + } + + // + // Stop all MNP child + // + List = &MnpDeviceData->ServiceList; + Status = NetDestroyLinkList ( + List, + MnpDestroyServiceChildEntry, + NULL, + &ListLength + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + + +/** + Creates a child handle with a set of I/O services. + + @param[in] This Protocol instance pointer. + @param[in, out] ChildHandle Pointer to the handle of the child to create. If + it is NULL, then a new handle is created. If + it is not NULL, then the I/O services are added + to the existing child handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to + create the child. + @retval Others The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +MnpServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ) +{ + EFI_STATUS Status; + MNP_SERVICE_DATA *MnpServiceData; + MNP_INSTANCE_DATA *Instance; + VOID *MnpSb; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + MnpServiceData = MNP_SERVICE_DATA_FROM_THIS (This); + + // + // Allocate buffer for the new instance. + // + Instance = AllocateZeroPool (sizeof (MNP_INSTANCE_DATA)); + if (Instance == NULL) { + DEBUG ((EFI_D_ERROR, "MnpServiceBindingCreateChild: Faild to allocate memory for the new instance.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + // + // Init the instance data. + // + MnpInitializeInstanceData (MnpServiceData, Instance); + + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiManagedNetworkProtocolGuid, + &Instance->ManagedNetwork, + NULL + ); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpServiceBindingCreateChild: Failed to install the MNP protocol, %r.\n", + Status) + ); + + goto ErrorExit; + } + + // + // Save the instance's childhandle. + // + Instance->Handle = *ChildHandle; + + Status = gBS->OpenProtocol ( + MnpServiceData->ServiceHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + (VOID **) &MnpSb, + gMnpDriverBinding.DriverBindingHandle, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ErrorExit; + } + + // + // Add the child instance into ChildrenList. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&MnpServiceData->ChildrenList, &Instance->InstEntry); + MnpServiceData->ChildrenNumber++; + + gBS->RestoreTPL (OldTpl); + +ErrorExit: + + if (EFI_ERROR (Status)) { + + if (Instance->Handle != NULL) { + + gBS->UninstallMultipleProtocolInterfaces ( + Instance->Handle, + &gEfiManagedNetworkProtocolGuid, + &Instance->ManagedNetwork, + NULL + ); + } + + FreePool (Instance); + } + + return Status; +} + + +/** + Destroys a child handle with a set of I/O services. + + The DestroyChild() function does the opposite of CreateChild(). It removes a + protocol that was installed by CreateChild() from ChildHandle. If the removed + protocol is the last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL + instance. + @param[in] ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that + is being removed. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the + ChildHandle because its services are being + used. + @retval Others The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +MnpServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + EFI_STATUS Status; + MNP_SERVICE_DATA *MnpServiceData; + EFI_MANAGED_NETWORK_PROTOCOL *ManagedNetwork; + MNP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + MnpServiceData = MNP_SERVICE_DATA_FROM_THIS (This); + + // + // Try to retrieve ManagedNetwork Protocol from ChildHandle. + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **) &ManagedNetwork, + gMnpDriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (ManagedNetwork); + + // + // MnpServiceBindingDestroyChild may be called twice: first called by + // MnpServiceBindingStop, second called by uninstalling the MNP protocol + // in this ChildHandle. Use destroyed to make sure the resource clean code + // will only excecute once. + // + if (Instance->Destroyed) { + return EFI_SUCCESS; + } + + Instance->Destroyed = TRUE; + + // + // Close the Simple Network protocol. + // + gBS->CloseProtocol ( + MnpServiceData->ServiceHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + MnpServiceData->MnpDeviceData->ImageHandle, + ChildHandle + ); + + // + // Uninstall the ManagedNetwork protocol. + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiManagedNetworkProtocolGuid, + &Instance->ManagedNetwork, + NULL + ); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "MnpServiceBindingDestroyChild: Failed to uninstall the ManagedNetwork protocol, %r.\n", + Status) + ); + + Instance->Destroyed = FALSE; + return Status; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Reset the configuration. + // + ManagedNetwork->Configure (ManagedNetwork, NULL); + + // + // Try to flush the RcvdPacketQueue. + // + MnpFlushRcvdDataQueue (Instance); + + // + // Clean the RxTokenMap. + // + NetMapClean (&Instance->RxTokenMap); + + // + // Remove this instance from the ChildrenList. + // + RemoveEntryList (&Instance->InstEntry); + MnpServiceData->ChildrenNumber--; + + gBS->RestoreTPL (OldTpl); + + FreePool (Instance); + + return Status; +} + +/** + The entry point for Mnp driver which installs the driver binding and component + name protocol on its ImageHandle. + + @param[in] ImageHandle The image handle of the driver. + @param[in] SystemTable The system table. + + @retval EFI_SUCCES The driver binding and component name protocols are + successfully installed. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +EFIAPI +MnpDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gMnpDriverBinding, + ImageHandle, + &gMnpComponentName, + &gMnpComponentName2 + ); +} diff --git a/NetworkPkg/MnpDxe/MnpDriver.h b/NetworkPkg/MnpDxe/MnpDriver.h new file mode 100644 index 000000000..150d21e9e --- /dev/null +++ b/NetworkPkg/MnpDxe/MnpDriver.h @@ -0,0 +1,268 @@ +/** @file + Declaration of strctures and functions for MnpDxe driver. + +Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _MNP_DRIVER_H_ +#define _MNP_DRIVER_H_ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ComponentName.h" + +#define MNP_DEVICE_DATA_SIGNATURE SIGNATURE_32 ('M', 'n', 'p', 'D') + +// +// Global Variables +// +extern EFI_DRIVER_BINDING_PROTOCOL gMnpDriverBinding; + +typedef struct { + UINT32 Signature; + + EFI_HANDLE ControllerHandle; + EFI_HANDLE ImageHandle; + + EFI_VLAN_CONFIG_PROTOCOL VlanConfig; + UINTN NumberOfVlan; + CHAR16 *MacString; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + + // + // List of MNP_SERVICE_DATA + // + LIST_ENTRY ServiceList; + // + // Number of configured MNP Service Binding child + // + UINTN ConfiguredChildrenNumber; + + LIST_ENTRY GroupAddressList; + UINT32 GroupAddressCount; + + LIST_ENTRY FreeTxBufList; + LIST_ENTRY AllTxBufList; + UINT32 TxBufCount; + + NET_BUF_QUEUE FreeNbufQue; + INTN NbufCnt; + + EFI_EVENT PollTimer; + BOOLEAN EnableSystemPoll; + + EFI_EVENT TimeoutCheckTimer; + EFI_EVENT MediaDetectTimer; + + UINT32 UnicastCount; + UINT32 BroadcastCount; + UINT32 MulticastCount; + UINT32 PromiscuousCount; + + // + // The size of the data buffer in the MNP_PACKET_BUFFER used to + // store a packet. + // + UINT32 BufferLength; + UINT32 PaddingSize; + NET_BUF *RxNbufCache; +} MNP_DEVICE_DATA; + +#define MNP_DEVICE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + MNP_DEVICE_DATA, \ + VlanConfig, \ + MNP_DEVICE_DATA_SIGNATURE \ + ) + +#define MNP_SERVICE_DATA_SIGNATURE SIGNATURE_32 ('M', 'n', 'p', 'S') + +typedef struct { + UINT32 Signature; + + LIST_ENTRY Link; + + MNP_DEVICE_DATA *MnpDeviceData; + EFI_HANDLE ServiceHandle; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + + LIST_ENTRY ChildrenList; + UINTN ChildrenNumber; + + UINT32 Mtu; + + UINT16 VlanId; + UINT8 Priority; +} MNP_SERVICE_DATA; + + +#define MNP_SERVICE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + MNP_SERVICE_DATA, \ + ServiceBinding, \ + MNP_SERVICE_DATA_SIGNATURE \ + ) + +#define MNP_SERVICE_DATA_FROM_LINK(a) \ + CR ( \ + (a), \ + MNP_SERVICE_DATA, \ + Link, \ + MNP_SERVICE_DATA_SIGNATURE \ + ) + + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make drivers as small + as possible, there are a few calling restrictions for this service. + ConnectController() must follow these calling restrictions. If any other + agent wishes to call Start() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for Mnp Service Data. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to make drivers as + small as possible, there are a few calling restrictions for this service. + DisconnectController() must follow these calling restrictions. If any other + agent wishes to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If + number of children is zero stop the entire + bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +MnpDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + Creates a child handle with a set of I/O services. + + @param[in] This Protocol instance pointer. + @param[in, out] ChildHandle Pointer to the handle of the child to create. If + it is NULL, then a new handle is created. If + it is not NULL, then the I/O services are added + to the existing child handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to + create the child. + @retval Others The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +MnpServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a set of I/O services. + + The DestroyChild() function does the opposite of CreateChild(). It removes a + protocol that was installed by CreateChild() from ChildHandle. If the removed + protocol is the last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL + instance. + @param[in] ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that + is being removed. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the + ChildHandle because its services are being + used. + @retval Others The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +MnpServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +#endif diff --git a/NetworkPkg/MnpDxe/MnpDxe.inf b/NetworkPkg/MnpDxe/MnpDxe.inf new file mode 100644 index 000000000..e39923241 --- /dev/null +++ b/NetworkPkg/MnpDxe/MnpDxe.inf @@ -0,0 +1,69 @@ +## @file +# This module produces EFI MNP Protocol, EFI MNP Servie Binding Protocol and EFI VLAN Protocol. +# +# This module produces EFI Managed Network Protocol upon EFI Simple Network Protocol, +# to provide raw asynchronous network I/O services. It also produces EFI VLAN Protocol +# to provide manageability interface for VLAN configuration. +# +# Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = MnpDxe + MODULE_UNI_FILE = MnpDxe.uni + FILE_GUID = 025BBFC7-E6A9-4b8b-82AD-6815A1AEAF4A + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = MnpDriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = gMnpDriverBinding +# COMPONENT_NAME = gMnpComponentName +# COMPONENT_NAME2 = gMnpComponentName2 +# + +[Sources] + MnpMain.c + MnpIo.c + ComponentName.h + MnpDriver.h + ComponentName.c + MnpDriver.c + MnpConfig.c + MnpImpl.h + MnpVlan.h + MnpVlan.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + MemoryAllocationLib + UefiLib + UefiBootServicesTableLib + UefiDriverEntryPoint + DebugLib + NetLib + DpcLib + +[Protocols] + gEfiManagedNetworkServiceBindingProtocolGuid ## BY_START + gEfiSimpleNetworkProtocolGuid ## TO_START + gEfiManagedNetworkProtocolGuid ## BY_START + ## BY_START + ## UNDEFINED # variable + gEfiVlanConfigProtocolGuid + +[UserExtensions.TianoCore."ExtraFiles"] + MnpDxeExtra.uni diff --git a/NetworkPkg/MnpDxe/MnpDxe.uni b/NetworkPkg/MnpDxe/MnpDxe.uni new file mode 100644 index 000000000..04be3cd7f --- /dev/null +++ b/NetworkPkg/MnpDxe/MnpDxe.uni @@ -0,0 +1,18 @@ +// /** @file +// This module produces EFI MNP Protocol, EFI MNP Servie Binding Protocol and EFI VLAN Protocol. +// +// This module produces EFI Managed Network Protocol upon EFI Simple Network Protocol, +// to provide raw asynchronous network I/O services. It also produces EFI VLAN Protocol +// to provide manageability interface for VLAN configuration. +// +// Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Produces EFI MNP Protocol, EFI MNP Servie Binding Protocol and EFI VLAN Protocol" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces EFI Managed Network Protocol upon EFI Simple Network Protocol to provide raw asynchronous network I/O services. It also produces EFI VLAN Protocol to provide manageability interface for VLAN configuration." + diff --git a/NetworkPkg/MnpDxe/MnpDxeExtra.uni b/NetworkPkg/MnpDxe/MnpDxeExtra.uni new file mode 100644 index 000000000..504ad2d8a --- /dev/null +++ b/NetworkPkg/MnpDxe/MnpDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// MnpDxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"MNP DXE Driver" + + diff --git a/NetworkPkg/MnpDxe/MnpImpl.h b/NetworkPkg/MnpDxe/MnpImpl.h new file mode 100644 index 000000000..7d5424034 --- /dev/null +++ b/NetworkPkg/MnpDxe/MnpImpl.h @@ -0,0 +1,898 @@ +/** @file + Declaration of structures and functions of MnpDxe driver. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _MNP_IMPL_H_ +#define _MNP_IMPL_H_ + +#include "MnpDriver.h" + +#define NET_ETHER_FCS_SIZE 4 + +#define MNP_SYS_POLL_INTERVAL (10 * TICKS_PER_MS) // 10 milliseconds +#define MNP_TIMEOUT_CHECK_INTERVAL (50 * TICKS_PER_MS) // 50 milliseconds +#define MNP_MEDIA_DETECT_INTERVAL (500 * TICKS_PER_MS) // 500 milliseconds +#define MNP_TX_TIMEOUT_TIME (500 * TICKS_PER_MS) // 500 milliseconds +#define MNP_INIT_NET_BUFFER_NUM 512 +#define MNP_NET_BUFFER_INCREASEMENT 64 +#define MNP_MAX_NET_BUFFER_NUM 65536 +#define MNP_TX_BUFFER_INCREASEMENT 32 // Same as the recycling Q length for xmit_done in UNDI command. +#define MNP_MAX_TX_BUFFER_NUM 65536 + +#define MNP_MAX_RCVD_PACKET_QUE_SIZE 256 + +#define MNP_RECEIVE_UNICAST 0x01 +#define MNP_RECEIVE_BROADCAST 0x02 + +#define UNICAST_PACKET MNP_RECEIVE_UNICAST +#define BROADCAST_PACKET MNP_RECEIVE_BROADCAST + +#define MNP_INSTANCE_DATA_SIGNATURE SIGNATURE_32 ('M', 'n', 'p', 'I') + +#define MNP_INSTANCE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + MNP_INSTANCE_DATA, \ + ManagedNetwork, \ + MNP_INSTANCE_DATA_SIGNATURE \ + ) + +typedef struct { + UINT32 Signature; + + MNP_SERVICE_DATA *MnpServiceData; + + EFI_HANDLE Handle; + + LIST_ENTRY InstEntry; + + EFI_MANAGED_NETWORK_PROTOCOL ManagedNetwork; + + BOOLEAN Configured; + BOOLEAN Destroyed; + + LIST_ENTRY GroupCtrlBlkList; + + NET_MAP RxTokenMap; + + LIST_ENTRY RxDeliveredPacketQueue; + LIST_ENTRY RcvdPacketQueue; + UINTN RcvdPacketQueueSize; + + EFI_MANAGED_NETWORK_CONFIG_DATA ConfigData; + + UINT8 ReceiveFilter; +} MNP_INSTANCE_DATA; + +typedef struct { + LIST_ENTRY AddrEntry; + EFI_MAC_ADDRESS Address; + INTN RefCnt; +} MNP_GROUP_ADDRESS; + +typedef struct { + LIST_ENTRY CtrlBlkEntry; + MNP_GROUP_ADDRESS *GroupAddress; +} MNP_GROUP_CONTROL_BLOCK; + +typedef struct { + LIST_ENTRY WrapEntry; + MNP_INSTANCE_DATA *Instance; + EFI_MANAGED_NETWORK_RECEIVE_DATA RxData; + NET_BUF *Nbuf; + UINT64 TimeoutTick; +} MNP_RXDATA_WRAP; + +#define MNP_TX_BUF_WRAP_SIGNATURE SIGNATURE_32 ('M', 'T', 'B', 'W') + +typedef struct { + UINT32 Signature; + LIST_ENTRY WrapEntry; // Link to FreeTxBufList + LIST_ENTRY AllEntry; // Link to AllTxBufList + BOOLEAN InUse; + UINT8 TxBuf[1]; +} MNP_TX_BUF_WRAP; + +/** + Initialize the mnp device context data. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in] ImageHandle The driver image handle. + @param[in] ControllerHandle Handle of device to bind driver to. + + @retval EFI_SUCCESS The mnp service context is initialized. + @retval EFI_UNSUPPORTED ControllerHandle does not support Simple Network Protocol. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpInitializeDeviceData ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle + ); + +/** + Destroy the MNP device context data. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in] ImageHandle The driver image handle. + +**/ +VOID +MnpDestroyDeviceData ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN EFI_HANDLE ImageHandle + ); + +/** + Create mnp service context data. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + @param[in] VlanId The VLAN ID. + @param[in] Priority The VLAN priority. If VlanId is 0, + Priority is ignored. + + @return A pointer to MNP_SERVICE_DATA or NULL if failed to create MNP service context. + +**/ +MNP_SERVICE_DATA * +MnpCreateServiceData ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINT16 VlanId, + IN UINT8 Priority OPTIONAL + ); + +/** + Initialize the mnp service context data. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + @param[in] ImageHandle The driver image handle. + @param[in] ControllerHandle Handle of device to bind driver to. + + @retval EFI_SUCCESS The mnp service context is initialized. + @retval EFI_UNSUPPORTED ControllerHandle does not support Simple Network Protocol. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpInitializeServiceData ( + IN OUT MNP_SERVICE_DATA *MnpServiceData, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle + ); + +/** + Destroy the MNP service context data. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + + @retval EFI_SUCCESS The mnp service context is destroyed. + @retval Others Errors as indicated. + +**/ +EFI_STATUS +MnpDestroyServiceData ( + IN OUT MNP_SERVICE_DATA *MnpServiceData + ); + +/** + Destroy all child of the MNP service data. + + @param[in, out] MnpServiceData Pointer to the mnp service context data. + + @retval EFI_SUCCESS All child are destroyed. + @retval Others Failed to destroy all child. + +**/ +EFI_STATUS +MnpDestroyServiceChild ( + IN OUT MNP_SERVICE_DATA *MnpServiceData + ); + +/** + Find the MNP Service Data for given VLAN ID. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + @param[in] VlanId The VLAN ID. + + @return A pointer to MNP_SERVICE_DATA or NULL if not found. + +**/ +MNP_SERVICE_DATA * +MnpFindServiceData ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINT16 VlanId + ); + +/** + Initialize the mnp instance context data. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in, out] Instance Pointer to the mnp instance context data + to initialize. + +**/ +VOID +MnpInitializeInstanceData ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN OUT MNP_INSTANCE_DATA *Instance + ); + +/** + Check whether the token specified by Arg matches the token in Item. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg, it's a pointer to the token to + check. + + @retval EFI_SUCCESS The token specified by Arg is different from the + token in Item. + @retval EFI_ACCESS_DENIED The token specified by Arg is the same as that in + Item. + +**/ +EFI_STATUS +EFIAPI +MnpTokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg + ); + +/** + Cancel the token specified by Arg if it matches the token in Item. + + @param[in, out] Map Pointer to the NET_MAP. + @param[in, out] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg, it's a pointer to the + token to cancel. + + @retval EFI_SUCCESS The Arg is NULL, and the token in Item is cancelled, + or the Arg isn't NULL, and the token in Item is + different from the Arg. + @retval EFI_ABORTED The Arg isn't NULL, the token in Item mathces the + Arg, and the token is cancelled. + +**/ +EFI_STATUS +EFIAPI +MnpCancelTokens ( + IN OUT NET_MAP *Map, + IN OUT NET_MAP_ITEM *Item, + IN VOID *Arg + ); + +/** + Flush the instance's received data. + + @param[in, out] Instance Pointer to the mnp instance context data. + +**/ +VOID +MnpFlushRcvdDataQueue ( + IN OUT MNP_INSTANCE_DATA *Instance + ); + +/** + Configure the Instance using ConfigData. + + @param[in, out] Instance Pointer to the mnp instance context data. + @param[in] ConfigData Pointer to the configuration data used to configure + the isntance. + + @retval EFI_SUCCESS The Instance is configured. + @retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the + implementation doesn't support it. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpConfigureInstance ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData OPTIONAL + ); + +/** + Do the group operations for this instance. + + @param[in, out] Instance Pointer to the instance context data. + @param[in] JoinFlag Set to TRUE to join a group. Set to TRUE to + leave a group/groups. + @param[in] MacAddress Pointer to the group address to join or leave. + @param[in] CtrlBlk Pointer to the group control block if JoinFlag + is FALSE. + + @retval EFI_SUCCESS The group operation finished. + @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +MnpGroupOp ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN BOOLEAN JoinFlag, + IN EFI_MAC_ADDRESS *MacAddress OPTIONAL, + IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk OPTIONAL + ); + +/** + Validates the Mnp transmit token. + + @param[in] Instance Pointer to the Mnp instance context data. + @param[in] Token Pointer to the transmit token to check. + + @return The Token is valid or not. + +**/ +BOOLEAN +MnpIsValidTxToken ( + IN MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ); + +/** + Build the packet to transmit from the TxData passed in. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] TxData Pointer to the transmit data containing the information + to build the packet. + @param[out] PktBuf Pointer to record the address of the packet. + @param[out] PktLen Pointer to a UINT32 variable used to record the packet's + length. + + @retval EFI_SUCCESS TxPackage is built. + @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource. + +**/ +EFI_STATUS +MnpBuildTxPacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData, + OUT UINT8 **PktBuf, + OUT UINT32 *PktLen + ); + +/** + Synchronously send out the packet. + + This functon places the packet buffer to SNP driver's tansmit queue. The packet + can be considered successfully sent out once SNP acccetp the packet, while the + packet buffer recycle is deferred for better performance. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] Packet Pointer to the pakcet buffer. + @param[in] Length The length of the packet. + @param[in, out] Token Pointer to the token the packet generated from. + + @retval EFI_SUCCESS The packet is sent out. + @retval EFI_TIMEOUT Time out occurs, the packet isn't sent. + @retval EFI_DEVICE_ERROR An unexpected network error occurs. + +**/ +EFI_STATUS +MnpSyncSendPacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN UINT8 *Packet, + IN UINT32 Length, + IN OUT EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ); + +/** + Try to deliver the received packet to the instance. + + @param[in, out] Instance Pointer to the mnp instance context data. + + @retval EFI_SUCCESS The received packet is delivered, or there is no + packet to deliver, or there is no available receive + token. + @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource. + +**/ +EFI_STATUS +MnpInstanceDeliverPacket ( + IN OUT MNP_INSTANCE_DATA *Instance + ); + +/** + Recycle the RxData and other resources used to hold and deliver the received + packet. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registerd to the Event. + +**/ +VOID +EFIAPI +MnpRecycleRxData ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Try to receive a packet and deliver it. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS add return value to function comment + @retval EFI_NOT_STARTED The simple network protocol is not started. + @retval EFI_NOT_READY No packet received. + @retval EFI_DEVICE_ERROR An unexpected error occurs. + +**/ +EFI_STATUS +MnpReceivePacket ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ); + +/** + Allocate a free NET_BUF from MnpDeviceData->FreeNbufQue. If there is none + in the queue, first try to allocate some and add them into the queue, then + fetch the NET_BUF from the updated FreeNbufQue. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + + @return Pointer to the allocated free NET_BUF structure, if NULL the + operation is failed. + +**/ +NET_BUF * +MnpAllocNbuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ); + +/** + Try to reclaim the Nbuf into the buffer pool. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in, out] Nbuf Pointer to the NET_BUF to free. + +**/ +VOID +MnpFreeNbuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN OUT NET_BUF *Nbuf + ); + +/** + Allocate a free TX buffer from MnpDeviceData->FreeTxBufList. If there is none + in the queue, first try to recycle some from SNP, then try to allocate some and add + them into the queue, then fetch the NET_BUF from the updated FreeTxBufList. + + @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA. + + @return Pointer to the allocated free NET_BUF structure, if NULL the + operation is failed. + +**/ +UINT8 * +MnpAllocTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ); + +/** + Try to recycle all the transmitted buffer address from SNP. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS Successed to recyclethe transmitted buffer address. + @retval Others Failed to recyclethe transmitted buffer address. + +**/ +EFI_STATUS +MnpRecycleTxBuf ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ); + +/** + Remove the received packets if timeout occurs. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpCheckPacketTimeout ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Poll to update MediaPresent field in SNP ModeData by Snp.GetStatus(). + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpCheckMediaStatus ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Poll to receive the packets from Snp. This function is either called by upperlayer + protocols/applications or the system poll timer notify mechanism. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpSystemPoll ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Returns the operational parameters for the current MNP child driver. May also + support returning the underlying SNP driver mode data. + + The GetModeData() function is used to read the current mode data (operational + parameters) from the MNP or the underlying SNP. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[out] MnpConfigData Pointer to storage for MNP operational parameters. Type + EFI_MANAGED_NETWORK_CONFIG_DATA is defined in "Related + Definitions" below. + @param[out] SnpModeData Pointer to storage for SNP operational parameters. This + feature may be unsupported. Type EFI_SIMPLE_NETWORK_MODE + is defined in the EFI_SIMPLE_NETWORK_PROTOCOL. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this + MNP implementation. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. The default values are returned in + MnpConfigData if it is not NULL. + @retval Others The mode data could not be read. + +**/ +EFI_STATUS +EFIAPI +MnpGetModeData ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ); + +/** + Sets or clears the operational parameters for the MNP child driver. + + The Configure() function is used to set, change, or reset the operational + parameters for the MNP child driver instance. Until the operational parameters + have been set, no network traffic can be sent or received by this MNP child + driver instance. Once the operational parameters have been reset, no more + traffic can be sent or received until the operational parameters have been set + again. + Each MNP child driver instance can be started and stopped independently of + each other by setting or resetting their receive filter settings with the + Configure() function. + After any successful call to Configure(), the MNP child driver instance is + started. The internal periodic timer (if supported) is enabled. Data can be + transmitted and may be received if the receive filters have also been enabled. + Note: If multiple MNP child driver instances will receive the same packet + because of overlapping receive filter settings, then the first MNP child + driver instance will receive the original packet and additional instances will + receive copies of the original packet. + Note: Warning: Receive filter settings that overlap will consume extra + processor and/or DMA resources and degrade system and network performance. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] MnpConfigData Pointer to configuration data that will be assigned + to the MNP child driver instance. If NULL, the MNP + child driver instance is reset to startup defaults + and all pending transmit and receive requests are + flushed. Type EFI_MANAGED_NETWORK_CONFIG_DATA is + defined in EFI_MANAGED_NETWORK_PROTOCOL.GetModeData(). + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * MnpConfigData.ProtocolTypeFilter is not + valid. + The operational data for the MNP child driver + instance is unchanged. + @retval EFI_OUT_OF_RESOURCES Required system resources (usually memory) + could not be allocated. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_UNSUPPORTED The requested feature is unsupported in + this [MNP] implementation. The operational data + for the MNP child driver instance is unchanged. + @retval EFI_DEVICE_ERROR An unexpected network or system error + occurred. The MNP child driver instance has + been reset to startup defaults. + @retval Others The MNP child driver instance has been reset to + startup defaults. + +**/ +EFI_STATUS +EFIAPI +MnpConfigure ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL + ); + +/** + Translates an IP multicast address to a hardware (MAC) multicast address. This + function may be unsupported in some MNP implementations. + + The McastIpToMac() function translates an IP multicast address to a hardware + (MAC) multicast address. This function may be implemented by calling the + underlying EFI_SIMPLE_NETWORK. MCastIpToMac() function, which may also be + unsupported in some MNP implementations. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Ipv6Flag Set to TRUE to if IpAddress is an IPv6 multicast address. + Set to FALSE if IpAddress is an IPv4 multicast address. + @param[in] IpAddress Pointer to the multicast IP address (in network byte + order) to convert. + @param[out] MacAddress Pointer to the resulting multicast MAC address. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One of the following conditions is TRUE: + * This is NULL. + * IpAddress is NULL. + * IpAddress is not a valid multicast IP + address. + * MacAddress is NULL. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this + MNP implementation. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval Others The address could not be converted. +**/ +EFI_STATUS +EFIAPI +MnpMcastIpToMac ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN BOOLEAN Ipv6Flag, + IN EFI_IP_ADDRESS *IpAddress, + OUT EFI_MAC_ADDRESS *MacAddress + ); + +/** + Enables and disables receive filters for multicast address. This function may + be unsupported in some MNP implementations. + + The Groups() function only adds and removes multicast MAC addresses from the + filter list. The MNP driver does not transmit or process Internet Group + Management Protocol (IGMP) packets. If JoinFlag is FALSE and MacAddress is + NULL, then all joined groups are left. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join this multicast group. + Set to FALSE to leave this multicast group. + @param[in] MacAddress Pointer to the multicast MAC group (address) to join or + leave. + + @retval EFI_SUCCESS The requested operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + * This is NULL. + * JoinFlag is TRUE and MacAddress is NULL. + * MacAddress is not a valid multicast MAC + address. + * The MNP multicast group settings are + unchanged. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_ALREADY_STARTED The supplied multicast group is already joined. + @retval EFI_NOT_FOUND The supplied multicast group is not joined. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this MNP + implementation. + @retval Others The requested operation could not be completed. + The MNP multicast group settings are unchanged. + +**/ +EFI_STATUS +EFIAPI +MnpGroups ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_MAC_ADDRESS *MacAddress OPTIONAL + ); + +/** + Places asynchronous outgoing data packets into the transmit queue. + + The Transmit() function places a completion token into the transmit packet + queue. This function is always asynchronous. + The caller must fill in the Token.Event and Token.TxData fields in the + completion token, and these fields cannot be NULL. When the transmit operation + completes, the MNP updates the Token.Status field and the Token.Event is + signaled. + Note: There may be a performance penalty if the packet needs to be + defragmented before it can be transmitted by the network device. Systems in + which performance is critical should review the requirements and features of + the underlying communications device and drivers. + + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token associated with the transmit data + descriptor. Type EFI_MANAGED_NETWORK_COMPLETION_TOKEN + is defined in "Related Definitions" below. + + @retval EFI_SUCCESS The transmit completion token was cached. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * Token is NULL. + * Token.Event is NULL. + * Token.TxData is NULL. + * Token.TxData.DestinationAddress is not + NULL and Token.TxData.HeaderLength is zero. + * Token.TxData.FragmentCount is zero. + * (Token.TxData.HeaderLength + + Token.TxData.DataLength) is not equal to the + sum of the + Token.TxData.FragmentTable[].FragmentLength + fields. + * One or more of the + Token.TxData.FragmentTable[].FragmentLength + fields is zero. + * One or more of the + Token.TxData.FragmentTable[].FragmentBufferfields + is NULL. + * Token.TxData.DataLength is greater than MTU. + @retval EFI_ACCESS_DENIED The transmit completion token is already in the + transmit queue. + @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a + lack of system resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_NOT_READY The transmit request could not be queued because + the transmit queue is full. + +**/ +EFI_STATUS +EFIAPI +MnpTransmit ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ); + +/** + Aborts an asynchronous transmit or receive request. + + The Cancel() function is used to abort a pending transmit or receive request. + If the token is in the transmit or receive request queues, after calling this + function, Token.Status will be set to EFI_ABORTED and then Token.Event will be + signaled. If the token is not in one of the queues, which usually means that + the asynchronous operation has completed, this function will not signal the + token and EFI_NOT_FOUND is returned. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_MANAGED_NETWORK_PROTOCOL.Transmit() or + EFI_MANAGED_NETWORK_PROTOCOL.Receive(). If NULL, all + pending tokens are aborted. + + @retval EFI_SUCCESS The asynchronous I/O request was aborted and + Token.Event was signaled. When Token is NULL, + all pending requests were aborted and their + events were signaled. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O + request was not found in the transmit or + receive queue. It has either completed or was + not issued by Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +MnpCancel ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + Places an asynchronous receiving request into the receiving queue. + + The Receive() function places a completion token into the receive packet + queue. This function is always asynchronous. + The caller must fill in the Token.Event field in the completion token, and + this field cannot be NULL. When the receive operation completes, the MNP + updates the Token.Status and Token.RxData fields and the Token.Event is + signaled. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token associated with the receive + data descriptor. Type + EFI_MANAGED_NETWORK_COMPLETION_TOKEN is defined in + EFI_MANAGED_NETWORK_PROTOCOL.Transmit(). + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * Token is NULL. + * Token.Event is NULL + @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a + lack of system resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_ACCESS_DENIED The receive completion token was already in the + receive queue. + @retval EFI_NOT_READY The receive request could not be queued because + the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +MnpReceive ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ); + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications to + increase the rate that data packets are moved between the communications + device and the transmit and receive queues. + Normally, a periodic timer event internally calls the Poll() function. But, in + some systems, the periodic timer event may not call Poll() fast enough to + transmit and/or receive all data packets without missing packets. Drivers and + applications that are experiencing packet loss should try calling the Poll() + function more often. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The + MNP child driver instance has been reset to startup + defaults. + @retval EFI_NOT_READY No incoming or outgoing data was processed. Consider + increasing the polling rate. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive + queue. Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +MnpPoll ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This + ); + +/** + Configure the Snp receive filters according to the instances' receive filter + settings. + + @param[in] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS The receive filters is configured. + @retval EFI_OUT_OF_RESOURCES The receive filters can't be configured due + to lack of memory resource. + +**/ +EFI_STATUS +MnpConfigReceiveFilters ( + IN MNP_DEVICE_DATA *MnpDeviceData + ); + +#endif diff --git a/NetworkPkg/MnpDxe/MnpIo.c b/NetworkPkg/MnpDxe/MnpIo.c new file mode 100644 index 000000000..56405d62b --- /dev/null +++ b/NetworkPkg/MnpDxe/MnpIo.c @@ -0,0 +1,1133 @@ +/** @file + Implementation of Managed Network Protocol I/O functions. + +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpImpl.h" +#include "MnpVlan.h" + +/** + Validates the Mnp transmit token. + + @param[in] Instance Pointer to the Mnp instance context data. + @param[in] Token Pointer to the transmit token to check. + + @return The Token is valid or not. + +**/ +BOOLEAN +MnpIsValidTxToken ( + IN MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ) +{ + MNP_SERVICE_DATA *MnpServiceData; + EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData; + UINT32 Index; + UINT32 TotalLength; + EFI_MANAGED_NETWORK_FRAGMENT_DATA *FragmentTable; + + MnpServiceData = Instance->MnpServiceData; + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + + TxData = Token->Packet.TxData; + + if ((Token->Event == NULL) || (TxData == NULL) || (TxData->FragmentCount == 0)) { + // + // The token is invalid if the Event is NULL, or the TxData is NULL, or + // the fragment count is zero. + // + DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: Invalid Token.\n")); + return FALSE; + } + + if ((TxData->DestinationAddress != NULL) && (TxData->HeaderLength != 0)) { + // + // The token is invalid if the HeaderLength isn't zero while the DestinationAddress + // is NULL (The destination address is already put into the packet). + // + DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: DestinationAddress isn't NULL, HeaderLength must be 0.\n")); + return FALSE; + } + + TotalLength = 0; + FragmentTable = TxData->FragmentTable; + for (Index = 0; Index < TxData->FragmentCount; Index++) { + + if ((FragmentTable[Index].FragmentLength == 0) || (FragmentTable[Index].FragmentBuffer == NULL)) { + // + // The token is invalid if any FragmentLength is zero or any FragmentBuffer is NULL. + // + DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: Invalid FragmentLength or FragmentBuffer.\n")); + return FALSE; + } + + TotalLength += FragmentTable[Index].FragmentLength; + } + + if ((TxData->DestinationAddress == NULL) && (FragmentTable[0].FragmentLength < TxData->HeaderLength)) { + // + // Media header is split between fragments. + // + return FALSE; + } + + if (TotalLength != (TxData->DataLength + TxData->HeaderLength)) { + // + // The length calculated from the fragment information doesn't equal to the + // sum of the DataLength and the HeaderLength. + // + DEBUG ((EFI_D_WARN, "MnpIsValidTxData: Invalid Datalength compared with the sum of fragment length.\n")); + return FALSE; + } + + if (TxData->DataLength > MnpServiceData->Mtu) { + // + // The total length is larger than the MTU. + // + DEBUG ((EFI_D_WARN, "MnpIsValidTxData: TxData->DataLength exceeds Mtu.\n")); + return FALSE; + } + + return TRUE; +} + +/** + Build the packet to transmit from the TxData passed in. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] TxData Pointer to the transmit data containing the information + to build the packet. + @param[out] PktBuf Pointer to record the address of the packet. + @param[out] PktLen Pointer to a UINT32 variable used to record the packet's + length. + + @retval EFI_SUCCESS TxPackage is built. + @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource. + +**/ +EFI_STATUS +MnpBuildTxPacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData, + OUT UINT8 **PktBuf, + OUT UINT32 *PktLen + ) +{ + EFI_SIMPLE_NETWORK_MODE *SnpMode; + UINT8 *DstPos; + UINT16 Index; + MNP_DEVICE_DATA *MnpDeviceData; + UINT8 *TxBuf; + + MnpDeviceData = MnpServiceData->MnpDeviceData; + + TxBuf = MnpAllocTxBuf (MnpDeviceData); + if (TxBuf == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Reserve space for vlan tag if needed. + // + if (MnpServiceData->VlanId != 0) { + *PktBuf = TxBuf + NET_VLAN_TAG_LEN; + } else { + *PktBuf = TxBuf; + } + + if ((TxData->DestinationAddress == NULL) && (TxData->FragmentCount == 1)) { + CopyMem ( + *PktBuf, + TxData->FragmentTable[0].FragmentBuffer, + TxData->FragmentTable[0].FragmentLength + ); + + *PktLen = TxData->FragmentTable[0].FragmentLength; + } else { + // + // Either media header isn't in FragmentTable or there is more than + // one fragment, copy the data into the packet buffer. Reserve the + // media header space if necessary. + // + SnpMode = MnpDeviceData->Snp->Mode; + DstPos = *PktBuf; + *PktLen = 0; + if (TxData->DestinationAddress != NULL) { + // + // If dest address is not NULL, move DstPos to reserve space for the + // media header. Add the media header length to buflen. + // + DstPos += SnpMode->MediaHeaderSize; + *PktLen += SnpMode->MediaHeaderSize; + } + + for (Index = 0; Index < TxData->FragmentCount; Index++) { + // + // Copy the data. + // + CopyMem ( + DstPos, + TxData->FragmentTable[Index].FragmentBuffer, + TxData->FragmentTable[Index].FragmentLength + ); + DstPos += TxData->FragmentTable[Index].FragmentLength; + } + + // + // Set the buffer length. + // + *PktLen += TxData->DataLength + TxData->HeaderLength; + } + + return EFI_SUCCESS; +} + + +/** + Synchronously send out the packet. + + This functon places the packet buffer to SNP driver's tansmit queue. The packet + can be considered successfully sent out once SNP acccetp the packet, while the + packet buffer recycle is deferred for better performance. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] Packet Pointer to the pakcet buffer. + @param[in] Length The length of the packet. + @param[in, out] Token Pointer to the token the packet generated from. + + @retval EFI_SUCCESS The packet is sent out. + @retval EFI_TIMEOUT Time out occurs, the packet isn't sent. + @retval EFI_DEVICE_ERROR An unexpected network error occurs. + +**/ +EFI_STATUS +MnpSyncSendPacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN UINT8 *Packet, + IN UINT32 Length, + IN OUT EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData; + UINT32 HeaderSize; + MNP_DEVICE_DATA *MnpDeviceData; + UINT16 ProtocolType; + + MnpDeviceData = MnpServiceData->MnpDeviceData; + Snp = MnpDeviceData->Snp; + TxData = Token->Packet.TxData; + Token->Status = EFI_SUCCESS; + HeaderSize = Snp->Mode->MediaHeaderSize - TxData->HeaderLength; + + // + // Check media status before transmit packet. + // Note: media status will be updated by periodic timer MediaDetectTimer. + // + if (Snp->Mode->MediaPresentSupported && !Snp->Mode->MediaPresent) { + // + // Media not present, skip packet transmit and report EFI_NO_MEDIA + // + DEBUG ((EFI_D_WARN, "MnpSyncSendPacket: No network cable detected.\n")); + Token->Status = EFI_NO_MEDIA; + goto SIGNAL_TOKEN; + } + + + if (MnpServiceData->VlanId != 0) { + // + // Insert VLAN tag + // + MnpInsertVlanTag (MnpServiceData, TxData, &ProtocolType, &Packet, &Length); + } else { + ProtocolType = TxData->ProtocolType; + } + + // + // Transmit the packet through SNP. + // + Status = Snp->Transmit ( + Snp, + HeaderSize, + Length, + Packet, + TxData->SourceAddress, + TxData->DestinationAddress, + &ProtocolType + ); + if (Status == EFI_NOT_READY) { + Status = MnpRecycleTxBuf (MnpDeviceData); + if (EFI_ERROR (Status)) { + Token->Status = EFI_DEVICE_ERROR; + goto SIGNAL_TOKEN; + } + + Status = Snp->Transmit ( + Snp, + HeaderSize, + Length, + Packet, + TxData->SourceAddress, + TxData->DestinationAddress, + &ProtocolType + ); + } + + if (EFI_ERROR (Status)) { + Token->Status = EFI_DEVICE_ERROR; + } + +SIGNAL_TOKEN: + + gBS->SignalEvent (Token->Event); + + // + // Dispatch the DPC queued by the NotifyFunction of Token->Event. + // + DispatchDpc (); + + return EFI_SUCCESS; +} + + +/** + Try to deliver the received packet to the instance. + + @param[in, out] Instance Pointer to the mnp instance context data. + + @retval EFI_SUCCESS The received packet is delivered, or there is no + packet to deliver, or there is no available receive + token. + @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource. + +**/ +EFI_STATUS +MnpInstanceDeliverPacket ( + IN OUT MNP_INSTANCE_DATA *Instance + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + MNP_RXDATA_WRAP *RxDataWrap; + NET_BUF *DupNbuf; + EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + EFI_MANAGED_NETWORK_COMPLETION_TOKEN *RxToken; + + MnpDeviceData = Instance->MnpServiceData->MnpDeviceData; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + if (NetMapIsEmpty (&Instance->RxTokenMap) || IsListEmpty (&Instance->RcvdPacketQueue)) { + // + // No pending received data or no available receive token, return. + // + return EFI_SUCCESS; + } + + ASSERT (Instance->RcvdPacketQueueSize != 0); + + RxDataWrap = NET_LIST_HEAD (&Instance->RcvdPacketQueue, MNP_RXDATA_WRAP, WrapEntry); + if (RxDataWrap->Nbuf->RefCnt > 2) { + // + // There are other instances share this Nbuf, duplicate to get a + // copy to allow the instance to do R/W operations. + // + DupNbuf = MnpAllocNbuf (MnpDeviceData); + if (DupNbuf == NULL) { + DEBUG ((EFI_D_WARN, "MnpDeliverPacket: Failed to allocate a free Nbuf.\n")); + + return EFI_OUT_OF_RESOURCES; + } + + // + // Duplicate the net buffer. + // + NetbufDuplicate (RxDataWrap->Nbuf, DupNbuf, 0); + MnpFreeNbuf (MnpDeviceData, RxDataWrap->Nbuf); + RxDataWrap->Nbuf = DupNbuf; + } + + // + // All resources are OK, remove the packet from the queue. + // + NetListRemoveHead (&Instance->RcvdPacketQueue); + Instance->RcvdPacketQueueSize--; + + RxData = &RxDataWrap->RxData; + SnpMode = MnpDeviceData->Snp->Mode; + + // + // Set all the buffer pointers. + // + RxData->MediaHeader = NetbufGetByte (RxDataWrap->Nbuf, 0, NULL); + RxData->DestinationAddress = RxData->MediaHeader; + RxData->SourceAddress = (UINT8 *) RxData->MediaHeader + SnpMode->HwAddressSize; + RxData->PacketData = (UINT8 *) RxData->MediaHeader + SnpMode->MediaHeaderSize; + + // + // Insert this RxDataWrap into the delivered queue. + // + InsertTailList (&Instance->RxDeliveredPacketQueue, &RxDataWrap->WrapEntry); + + // + // Get the receive token from the RxTokenMap. + // + RxToken = NetMapRemoveHead (&Instance->RxTokenMap, NULL); + + // + // Signal this token's event. + // + RxToken->Packet.RxData = &RxDataWrap->RxData; + RxToken->Status = EFI_SUCCESS; + gBS->SignalEvent (RxToken->Event); + + return EFI_SUCCESS; +} + + +/** + Deliver the received packet for the instances belonging to the MnpServiceData. + + @param[in] MnpServiceData Pointer to the mnp service context data. + +**/ +VOID +MnpDeliverPacket ( + IN MNP_SERVICE_DATA *MnpServiceData + ) +{ + LIST_ENTRY *Entry; + MNP_INSTANCE_DATA *Instance; + + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + + NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) { + Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry); + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + // + // Try to deliver packet for this instance. + // + MnpInstanceDeliverPacket (Instance); + } +} + + +/** + Recycle the RxData and other resources used to hold and deliver the received + packet. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registerd to the Event. + +**/ +VOID +EFIAPI +MnpRecycleRxData ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MNP_RXDATA_WRAP *RxDataWrap; + MNP_DEVICE_DATA *MnpDeviceData; + + ASSERT (Context != NULL); + + RxDataWrap = (MNP_RXDATA_WRAP *) Context; + NET_CHECK_SIGNATURE (RxDataWrap->Instance, MNP_INSTANCE_DATA_SIGNATURE); + + ASSERT (RxDataWrap->Nbuf != NULL); + + MnpDeviceData = RxDataWrap->Instance->MnpServiceData->MnpDeviceData; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + // + // Free this Nbuf. + // + MnpFreeNbuf (MnpDeviceData, RxDataWrap->Nbuf); + RxDataWrap->Nbuf = NULL; + + // + // Close the recycle event. + // + gBS->CloseEvent (RxDataWrap->RxData.RecycleEvent); + + // + // Remove this Wrap entry from the list. + // + RemoveEntryList (&RxDataWrap->WrapEntry); + + FreePool (RxDataWrap); +} + + +/** + Queue the received packet into instance's receive queue. + + @param[in, out] Instance Pointer to the mnp instance context data. + @param[in, out] RxDataWrap Pointer to the Wrap structure containing the + received data and other information. +**/ +VOID +MnpQueueRcvdPacket ( + IN OUT MNP_INSTANCE_DATA *Instance, + IN OUT MNP_RXDATA_WRAP *RxDataWrap + ) +{ + MNP_RXDATA_WRAP *OldRxDataWrap; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + // + // Check the queue size. If it exceeds the limit, drop one packet + // from the head. + // + if (Instance->RcvdPacketQueueSize == MNP_MAX_RCVD_PACKET_QUE_SIZE) { + + DEBUG ((EFI_D_WARN, "MnpQueueRcvdPacket: Drop one packet bcz queue size limit reached.\n")); + + // + // Get the oldest packet. + // + OldRxDataWrap = NET_LIST_HEAD ( + &Instance->RcvdPacketQueue, + MNP_RXDATA_WRAP, + WrapEntry + ); + + // + // Recycle this OldRxDataWrap, this entry will be removed by the callee. + // + MnpRecycleRxData (NULL, (VOID *) OldRxDataWrap); + Instance->RcvdPacketQueueSize--; + } + + // + // Update the timeout tick using the configured parameter. + // + RxDataWrap->TimeoutTick = Instance->ConfigData.ReceivedQueueTimeoutValue; + + // + // Insert this Wrap into the instance queue. + // + InsertTailList (&Instance->RcvdPacketQueue, &RxDataWrap->WrapEntry); + Instance->RcvdPacketQueueSize++; +} + + +/** + Match the received packet with the instance receive filters. + + @param[in] Instance Pointer to the mnp instance context data. + @param[in] RxData Pointer to the EFI_MANAGED_NETWORK_RECEIVE_DATA. + @param[in] GroupAddress Pointer to the GroupAddress, the GroupAddress is + non-NULL and it contains the destination multicast + mac address of the received packet if the packet + destinated to a multicast mac address. + @param[in] PktAttr The received packets attribute. + + @return The received packet matches the instance's receive filters or not. + +**/ +BOOLEAN +MnpMatchPacket ( + IN MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData, + IN MNP_GROUP_ADDRESS *GroupAddress OPTIONAL, + IN UINT8 PktAttr + ) +{ + EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData; + LIST_ENTRY *Entry; + MNP_GROUP_CONTROL_BLOCK *GroupCtrlBlk; + + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + ConfigData = &Instance->ConfigData; + + // + // Check the protocol type. + // + if ((ConfigData->ProtocolTypeFilter != 0) && (ConfigData->ProtocolTypeFilter != RxData->ProtocolType)) { + return FALSE; + } + + if (ConfigData->EnablePromiscuousReceive) { + // + // Always match if this instance is configured to be promiscuous. + // + return TRUE; + } + + // + // The protocol type is matched, check receive filter, include unicast and broadcast. + // + if ((Instance->ReceiveFilter & PktAttr) != 0) { + return TRUE; + } + + // + // Check multicast addresses. + // + if (ConfigData->EnableMulticastReceive && RxData->MulticastFlag) { + + ASSERT (GroupAddress != NULL); + + NET_LIST_FOR_EACH (Entry, &Instance->GroupCtrlBlkList) { + + GroupCtrlBlk = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_CONTROL_BLOCK, CtrlBlkEntry); + if (GroupCtrlBlk->GroupAddress == GroupAddress) { + // + // The instance is configured to receiveing packets destinated to this + // multicast address. + // + return TRUE; + } + } + } + + // + // No match. + // + return FALSE; +} + + +/** + Analyse the received packets. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] Nbuf Pointer to the net buffer holding the received + packet. + @param[in, out] RxData Pointer to the buffer used to save the analysed + result in EFI_MANAGED_NETWORK_RECEIVE_DATA. + @param[out] GroupAddress Pointer to pointer to a MNP_GROUP_ADDRESS used to + pass out the address of the multicast address the + received packet destinated to. + @param[out] PktAttr Pointer to the buffer used to save the analysed + packet attribute. + +**/ +VOID +MnpAnalysePacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN NET_BUF *Nbuf, + IN OUT EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData, + OUT MNP_GROUP_ADDRESS **GroupAddress, + OUT UINT8 *PktAttr + ) +{ + EFI_SIMPLE_NETWORK_MODE *SnpMode; + MNP_DEVICE_DATA *MnpDeviceData; + UINT8 *BufPtr; + LIST_ENTRY *Entry; + + MnpDeviceData = MnpServiceData->MnpDeviceData; + SnpMode = MnpDeviceData->Snp->Mode; + + // + // Get the packet buffer. + // + BufPtr = NetbufGetByte (Nbuf, 0, NULL); + ASSERT (BufPtr != NULL); + + // + // Set the initial values. + // + RxData->BroadcastFlag = FALSE; + RxData->MulticastFlag = FALSE; + RxData->PromiscuousFlag = FALSE; + *PktAttr = UNICAST_PACKET; + + if (!NET_MAC_EQUAL (&SnpMode->CurrentAddress, BufPtr, SnpMode->HwAddressSize)) { + // + // This packet isn't destinated to our current mac address, it't not unicast. + // + *PktAttr = 0; + + if (NET_MAC_EQUAL (&SnpMode->BroadcastAddress, BufPtr, SnpMode->HwAddressSize)) { + // + // It's broadcast. + // + RxData->BroadcastFlag = TRUE; + *PktAttr = BROADCAST_PACKET; + } else if ((*BufPtr & 0x01) == 0x1) { + // + // It's multicast, try to match the multicast filters. + // + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->GroupAddressList) { + + *GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry); + if (NET_MAC_EQUAL (BufPtr, &((*GroupAddress)->Address), SnpMode->HwAddressSize)) { + RxData->MulticastFlag = TRUE; + break; + } + } + + if (!RxData->MulticastFlag) { + // + // No match, set GroupAddress to NULL. This multicast packet must + // be the result of PROMISUCOUS or PROMISUCOUS_MULTICAST flag is on. + // + *GroupAddress = NULL; + RxData->PromiscuousFlag = TRUE; + + if (MnpDeviceData->PromiscuousCount == 0) { + // + // Skip the below code, there is no receiver of this packet. + // + return ; + } + } + } else { + RxData->PromiscuousFlag = TRUE; + } + } + + ZeroMem (&RxData->Timestamp, sizeof (EFI_TIME)); + + // + // Fill the common parts of RxData. + // + RxData->PacketLength = Nbuf->TotalSize; + RxData->HeaderLength = SnpMode->MediaHeaderSize; + RxData->AddressLength = SnpMode->HwAddressSize; + RxData->DataLength = RxData->PacketLength - RxData->HeaderLength; + RxData->ProtocolType = NTOHS (*(UINT16 *) (BufPtr + 2 * SnpMode->HwAddressSize)); +} + + +/** + Wrap the RxData. + + @param[in] Instance Pointer to the mnp instance context data. + @param[in] RxData Pointer to the receive data to wrap. + + @return Pointer to a MNP_RXDATA_WRAP which wraps the RxData. + +**/ +MNP_RXDATA_WRAP * +MnpWrapRxData ( + IN MNP_INSTANCE_DATA *Instance, + IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData + ) +{ + EFI_STATUS Status; + MNP_RXDATA_WRAP *RxDataWrap; + + // + // Allocate memory. + // + RxDataWrap = AllocatePool (sizeof (MNP_RXDATA_WRAP)); + if (RxDataWrap == NULL) { + DEBUG ((EFI_D_ERROR, "MnpDispatchPacket: Failed to allocate a MNP_RXDATA_WRAP.\n")); + return NULL; + } + + RxDataWrap->Instance = Instance; + + // + // Fill the RxData in RxDataWrap, + // + CopyMem (&RxDataWrap->RxData, RxData, sizeof (RxDataWrap->RxData)); + + // + // Create the recycle event. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + MnpRecycleRxData, + RxDataWrap, + &RxDataWrap->RxData.RecycleEvent + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "MnpDispatchPacket: gBS->CreateEvent failed, %r.\n", Status)); + + FreePool (RxDataWrap); + return NULL; + } + + return RxDataWrap; +} + + +/** + Enqueue the received the packets to the instances belonging to the + MnpServiceData. + + @param[in] MnpServiceData Pointer to the mnp service context data. + @param[in] Nbuf Pointer to the net buffer representing the received + packet. + +**/ +VOID +MnpEnqueuePacket ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN NET_BUF *Nbuf + ) +{ + LIST_ENTRY *Entry; + MNP_INSTANCE_DATA *Instance; + EFI_MANAGED_NETWORK_RECEIVE_DATA RxData; + UINT8 PktAttr; + MNP_GROUP_ADDRESS *GroupAddress; + MNP_RXDATA_WRAP *RxDataWrap; + + + GroupAddress = NULL; + // + // First, analyse the packet header. + // + MnpAnalysePacket (MnpServiceData, Nbuf, &RxData, &GroupAddress, &PktAttr); + + if (RxData.PromiscuousFlag && (MnpServiceData->MnpDeviceData->PromiscuousCount == 0)) { + // + // No receivers, no more action need. + // + return ; + } + + // + // Iterate the children to find match. + // + NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) { + + Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry); + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + if (!Instance->Configured) { + continue; + } + + // + // Check the packet against the instance receive filters. + // + if (MnpMatchPacket (Instance, &RxData, GroupAddress, PktAttr)) { + // + // Wrap the RxData. + // + RxDataWrap = MnpWrapRxData (Instance, &RxData); + if (RxDataWrap == NULL) { + continue; + } + + // + // Associate RxDataWrap with Nbuf and increase the RefCnt. + // + RxDataWrap->Nbuf = Nbuf; + NET_GET_REF (RxDataWrap->Nbuf); + + // + // Queue the packet into the instance queue. + // + MnpQueueRcvdPacket (Instance, RxDataWrap); + } + } +} + + +/** + Try to receive a packet and deliver it. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + + @retval EFI_SUCCESS add return value to function comment + @retval EFI_NOT_STARTED The simple network protocol is not started. + @retval EFI_NOT_READY No packet received. + @retval EFI_DEVICE_ERROR An unexpected error occurs. + +**/ +EFI_STATUS +MnpReceivePacket ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + NET_BUF *Nbuf; + UINT8 *BufPtr; + UINTN BufLen; + UINTN HeaderSize; + UINT32 Trimmed; + MNP_SERVICE_DATA *MnpServiceData; + UINT16 VlanId; + BOOLEAN IsVlanPacket; + + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + Snp = MnpDeviceData->Snp; + if (Snp->Mode->State != EfiSimpleNetworkInitialized) { + // + // The simple network protocol is not started. + // + return EFI_NOT_STARTED; + } + + if (MnpDeviceData->RxNbufCache == NULL) { + // + // Try to get a new buffer as there may be buffers recycled. + // + MnpDeviceData->RxNbufCache = MnpAllocNbuf (MnpDeviceData); + + if (MnpDeviceData->RxNbufCache == NULL) { + // + // No available buffer in the buffer pool. + // + return EFI_DEVICE_ERROR; + } + + NetbufAllocSpace ( + MnpDeviceData->RxNbufCache, + MnpDeviceData->BufferLength, + NET_BUF_TAIL + ); + } + + Nbuf = MnpDeviceData->RxNbufCache; + BufLen = Nbuf->TotalSize; + BufPtr = NetbufGetByte (Nbuf, 0, NULL); + ASSERT (BufPtr != NULL); + + // + // Receive packet through Snp. + // + Status = Snp->Receive (Snp, &HeaderSize, &BufLen, BufPtr, NULL, NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG_CODE ( + if (Status != EFI_NOT_READY) { + DEBUG ((EFI_D_WARN, "MnpReceivePacket: Snp->Receive() = %r.\n", Status)); + } + ); + + return Status; + } + + // + // Sanity check. + // + if ((HeaderSize != Snp->Mode->MediaHeaderSize) || (BufLen < HeaderSize)) { + DEBUG ( + (EFI_D_WARN, + "MnpReceivePacket: Size error, HL:TL = %d:%d.\n", + HeaderSize, + BufLen) + ); + return EFI_DEVICE_ERROR; + } + + Trimmed = 0; + if (Nbuf->TotalSize != BufLen) { + // + // Trim the packet from tail. + // + Trimmed = NetbufTrim (Nbuf, Nbuf->TotalSize - (UINT32) BufLen, NET_BUF_TAIL); + ASSERT (Nbuf->TotalSize == BufLen); + } + + VlanId = 0; + if (MnpDeviceData->NumberOfVlan != 0) { + // + // VLAN is configured, remove the VLAN tag if any + // + IsVlanPacket = MnpRemoveVlanTag (MnpDeviceData, Nbuf, &VlanId); + } else { + IsVlanPacket = FALSE; + } + + MnpServiceData = MnpFindServiceData (MnpDeviceData, VlanId); + if (MnpServiceData == NULL) { + // + // VLAN is not set for this tagged frame, ignore this packet + // + if (Trimmed > 0) { + NetbufAllocSpace (Nbuf, Trimmed, NET_BUF_TAIL); + } + + if (IsVlanPacket) { + NetbufAllocSpace (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_HEAD); + } + + goto EXIT; + } + + // + // Enqueue the packet to the matched instances. + // + MnpEnqueuePacket (MnpServiceData, Nbuf); + + if (Nbuf->RefCnt > 2) { + // + // RefCnt > 2 indicates there is at least one receiver of this packet. + // Free the current RxNbufCache and allocate a new one. + // + MnpFreeNbuf (MnpDeviceData, Nbuf); + + Nbuf = MnpAllocNbuf (MnpDeviceData); + MnpDeviceData->RxNbufCache = Nbuf; + if (Nbuf == NULL) { + DEBUG ((EFI_D_ERROR, "MnpReceivePacket: Alloc packet for receiving cache failed.\n")); + return EFI_DEVICE_ERROR; + } + + NetbufAllocSpace (Nbuf, MnpDeviceData->BufferLength, NET_BUF_TAIL); + } else { + // + // No receiver for this packet. + // + if (Trimmed > 0) { + NetbufAllocSpace (Nbuf, Trimmed, NET_BUF_TAIL); + } + if (IsVlanPacket) { + NetbufAllocSpace (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_HEAD); + } + + goto EXIT; + } + // + // Deliver the queued packets. + // + MnpDeliverPacket (MnpServiceData); + +EXIT: + + ASSERT (Nbuf->TotalSize == MnpDeviceData->BufferLength); + + return Status; +} + + +/** + Remove the received packets if timeout occurs. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpCheckPacketTimeout ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + MNP_SERVICE_DATA *MnpServiceData; + LIST_ENTRY *Entry; + LIST_ENTRY *ServiceEntry; + LIST_ENTRY *RxEntry; + LIST_ENTRY *NextEntry; + MNP_INSTANCE_DATA *Instance; + MNP_RXDATA_WRAP *RxDataWrap; + EFI_TPL OldTpl; + + MnpDeviceData = (MNP_DEVICE_DATA *) Context; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + NET_LIST_FOR_EACH (ServiceEntry, &MnpDeviceData->ServiceList) { + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (ServiceEntry); + + NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) { + + Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry); + NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE); + + if (!Instance->Configured || (Instance->ConfigData.ReceivedQueueTimeoutValue == 0)) { + // + // This instance is not configured or there is no receive time out, + // just skip to the next instance. + // + continue; + } + + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + + NET_LIST_FOR_EACH_SAFE (RxEntry, NextEntry, &Instance->RcvdPacketQueue) { + + RxDataWrap = NET_LIST_USER_STRUCT (RxEntry, MNP_RXDATA_WRAP, WrapEntry); + + // + // TimeoutTick unit is microsecond, MNP_TIMEOUT_CHECK_INTERVAL unit is 100ns. + // + if (RxDataWrap->TimeoutTick >= (MNP_TIMEOUT_CHECK_INTERVAL / 10)) { + RxDataWrap->TimeoutTick -= (MNP_TIMEOUT_CHECK_INTERVAL / 10); + } else { + // + // Drop the timeout packet. + // + DEBUG ((EFI_D_WARN, "MnpCheckPacketTimeout: Received packet timeout.\n")); + MnpRecycleRxData (NULL, RxDataWrap); + Instance->RcvdPacketQueueSize--; + } + } + + gBS->RestoreTPL (OldTpl); + } + } +} + +/** + Poll to update MediaPresent field in SNP ModeData by Snp->GetStatus(). + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpCheckMediaStatus ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + UINT32 InterruptStatus; + + MnpDeviceData = (MNP_DEVICE_DATA *) Context; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + Snp = MnpDeviceData->Snp; + if (Snp->Mode->MediaPresentSupported) { + // + // Upon successful return of GetStatus(), the MediaPresent field of + // EFI_SIMPLE_NETWORK_MODE will be updated to reflect any change of media status + // + Snp->GetStatus (Snp, &InterruptStatus, NULL); + } +} + +/** + Poll to receive the packets from Snp. This function is either called by upperlayer + protocols/applications or the system poll timer notify mechanism. + + @param[in] Event The event this notify function registered to. + @param[in] Context Pointer to the context data registered to the event. + +**/ +VOID +EFIAPI +MnpSystemPoll ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + + MnpDeviceData = (MNP_DEVICE_DATA *) Context; + NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE); + + // + // Try to receive packets from Snp. + // + MnpReceivePacket (MnpDeviceData); + + // + // Dispatch the DPC queued by the NotifyFunction of rx token's events. + // + DispatchDpc (); +} diff --git a/NetworkPkg/MnpDxe/MnpMain.c b/NetworkPkg/MnpDxe/MnpMain.c new file mode 100644 index 000000000..d96178a1d --- /dev/null +++ b/NetworkPkg/MnpDxe/MnpMain.c @@ -0,0 +1,789 @@ +/** @file + Implementation of Managed Network Protocol public services. + +Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpImpl.h" + +/** + Returns the operational parameters for the current MNP child driver. May also + support returning the underlying SNP driver mode data. + + The GetModeData() function is used to read the current mode data (operational + parameters) from the MNP or the underlying SNP. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[out] MnpConfigData Pointer to storage for MNP operational parameters. Type + EFI_MANAGED_NETWORK_CONFIG_DATA is defined in "Related + Definitions" below. + @param[out] SnpModeData Pointer to storage for SNP operational parameters. This + feature may be unsupported. Type EFI_SIMPLE_NETWORK_MODE + is defined in the EFI_SIMPLE_NETWORK_PROTOCOL. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this + MNP implementation. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. The default values are returned in + MnpConfigData if it is not NULL. + @retval Others The mode data could not be read. + +**/ +EFI_STATUS +EFIAPI +MnpGetModeData ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ) +{ + MNP_INSTANCE_DATA *Instance; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_TPL OldTpl; + EFI_STATUS Status; + UINT32 InterruptStatus; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (MnpConfigData != NULL) { + // + // Copy the instance configuration data. + // + CopyMem (MnpConfigData, &Instance->ConfigData, sizeof (*MnpConfigData)); + } + + if (SnpModeData != NULL) { + // + // Copy the underlayer Snp mode data. + // + Snp = Instance->MnpServiceData->MnpDeviceData->Snp; + + // + // Upon successful return of GetStatus(), the Snp->Mode->MediaPresent + // will be updated to reflect any change of media status + // + Snp->GetStatus (Snp, &InterruptStatus, NULL); + CopyMem (SnpModeData, Snp->Mode, sizeof (*SnpModeData)); + } + + if (!Instance->Configured) { + Status = EFI_NOT_STARTED; + } else { + Status = EFI_SUCCESS; + } + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Sets or clears the operational parameters for the MNP child driver. + + The Configure() function is used to set, change, or reset the operational + parameters for the MNP child driver instance. Until the operational parameters + have been set, no network traffic can be sent or received by this MNP child + driver instance. Once the operational parameters have been reset, no more + traffic can be sent or received until the operational parameters have been set + again. + Each MNP child driver instance can be started and stopped independently of + each other by setting or resetting their receive filter settings with the + Configure() function. + After any successful call to Configure(), the MNP child driver instance is + started. The internal periodic timer (if supported) is enabled. Data can be + transmitted and may be received if the receive filters have also been enabled. + Note: If multiple MNP child driver instances will receive the same packet + because of overlapping receive filter settings, then the first MNP child + driver instance will receive the original packet and additional instances will + receive copies of the original packet. + Note: Warning: Receive filter settings that overlap will consume extra + processor and/or DMA resources and degrade system and network performance. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] MnpConfigData Pointer to configuration data that will be assigned + to the MNP child driver instance. If NULL, the MNP + child driver instance is reset to startup defaults + and all pending transmit and receive requests are + flushed. Type EFI_MANAGED_NETWORK_CONFIG_DATA is + defined in EFI_MANAGED_NETWORK_PROTOCOL.GetModeData(). + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * MnpConfigData.ProtocolTypeFilter is not + valid. + The operational data for the MNP child driver + instance is unchanged. + @retval EFI_OUT_OF_RESOURCES Required system resources (usually memory) + could not be allocated. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_UNSUPPORTED The requested feature is unsupported in + this [MNP] implementation. The operational data + for the MNP child driver instance is unchanged. + @retval EFI_DEVICE_ERROR An unexpected network or system error + occurred. The MNP child driver instance has + been reset to startup defaults. + @retval Others The MNP child driver instance has been reset to + startup defaults. + +**/ +EFI_STATUS +EFIAPI +MnpConfigure ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL + ) +{ + MNP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + EFI_STATUS Status; + + if ((This == NULL) || + ((MnpConfigData != NULL) && + (MnpConfigData->ProtocolTypeFilter > 0) && + (MnpConfigData->ProtocolTypeFilter <= 1500)) + ) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if ((MnpConfigData == NULL) && (!Instance->Configured)) { + // + // If the instance is not configured and a reset is requested, just return. + // + Status = EFI_SUCCESS; + goto ON_EXIT; + } + + // + // Configure the instance. + // + Status = MnpConfigureInstance (Instance, MnpConfigData); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Translates an IP multicast address to a hardware (MAC) multicast address. This + function may be unsupported in some MNP implementations. + + The McastIpToMac() function translates an IP multicast address to a hardware + (MAC) multicast address. This function may be implemented by calling the + underlying EFI_SIMPLE_NETWORK. MCastIpToMac() function, which may also be + unsupported in some MNP implementations. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Ipv6Flag Set to TRUE to if IpAddress is an IPv6 multicast address. + Set to FALSE if IpAddress is an IPv4 multicast address. + @param[in] IpAddress Pointer to the multicast IP address (in network byte + order) to convert. + @param[out] MacAddress Pointer to the resulting multicast MAC address. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One of the following conditions is TRUE: + * This is NULL. + * IpAddress is NULL. + * IpAddress is not a valid multicast IP + address. + * MacAddress is NULL. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this + MNP implementation. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval Others The address could not be converted. +**/ +EFI_STATUS +EFIAPI +MnpMcastIpToMac ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN BOOLEAN Ipv6Flag, + IN EFI_IP_ADDRESS *IpAddress, + OUT EFI_MAC_ADDRESS *MacAddress + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_TPL OldTpl; + EFI_IPv6_ADDRESS *Ip6Address; + + if ((This == NULL) || (IpAddress == NULL) || (MacAddress == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Ip6Address = &IpAddress->v6; + + if ((Ipv6Flag && !IP6_IS_MULTICAST (Ip6Address)) || + (!Ipv6Flag && !IP4_IS_MULTICAST (EFI_NTOHL (*IpAddress))) + ) { + // + // The IP address passed in is not a multicast address. + // + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + Snp = Instance->MnpServiceData->MnpDeviceData->Snp; + ASSERT (Snp != NULL); + + ZeroMem (MacAddress, sizeof (EFI_MAC_ADDRESS)); + + if (Snp->Mode->IfType == NET_IFTYPE_ETHERNET) { + if (!Ipv6Flag) { + // + // Translate the IPv4 address into a multicast MAC address if the NIC is an + // ethernet NIC according to RFC1112.. + // + MacAddress->Addr[0] = 0x01; + MacAddress->Addr[1] = 0x00; + MacAddress->Addr[2] = 0x5E; + MacAddress->Addr[3] = (UINT8) (IpAddress->v4.Addr[1] & 0x7F); + MacAddress->Addr[4] = IpAddress->v4.Addr[2]; + MacAddress->Addr[5] = IpAddress->v4.Addr[3]; + } else { + // + // Translate the IPv6 address into a multicast MAC address if the NIC is an + // ethernet NIC according to RFC2464. + // + + MacAddress->Addr[0] = 0x33; + MacAddress->Addr[1] = 0x33; + MacAddress->Addr[2] = Ip6Address->Addr[12]; + MacAddress->Addr[3] = Ip6Address->Addr[13]; + MacAddress->Addr[4] = Ip6Address->Addr[14]; + MacAddress->Addr[5] = Ip6Address->Addr[15]; + } + + Status = EFI_SUCCESS; + } else { + // + // Invoke Snp to translate the multicast IP address. + // + Status = Snp->MCastIpToMac ( + Snp, + Ipv6Flag, + IpAddress, + MacAddress + ); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Enables and disables receive filters for multicast address. This function may + be unsupported in some MNP implementations. + + The Groups() function only adds and removes multicast MAC addresses from the + filter list. The MNP driver does not transmit or process Internet Group + Management Protocol (IGMP) packets. If JoinFlag is FALSE and MacAddress is + NULL, then all joined groups are left. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join this multicast group. + Set to FALSE to leave this multicast group. + @param[in] MacAddress Pointer to the multicast MAC group (address) to join or + leave. + + @retval EFI_SUCCESS The requested operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + * This is NULL. + * JoinFlag is TRUE and MacAddress is NULL. + * MacAddress is not a valid multicast MAC + address. + * The MNP multicast group settings are + unchanged. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_ALREADY_STARTED The supplied multicast group is already joined. + @retval EFI_NOT_FOUND The supplied multicast group is not joined. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_UNSUPPORTED The requested feature is unsupported in this MNP + implementation. + @retval Others The requested operation could not be completed. + The MNP multicast group settings are unchanged. + +**/ +EFI_STATUS +EFIAPI +MnpGroups ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_MAC_ADDRESS *MacAddress OPTIONAL + ) +{ + MNP_INSTANCE_DATA *Instance; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + MNP_GROUP_CONTROL_BLOCK *GroupCtrlBlk; + MNP_GROUP_ADDRESS *GroupAddress; + LIST_ENTRY *ListEntry; + BOOLEAN AddressExist; + EFI_TPL OldTpl; + EFI_STATUS Status; + + if (This == NULL || (JoinFlag && (MacAddress == NULL))) { + // + // This is NULL, or it's a join operation but MacAddress is NULL. + // + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + SnpMode = Instance->MnpServiceData->MnpDeviceData->Snp->Mode; + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + if ((!Instance->ConfigData.EnableMulticastReceive) || + ((MacAddress != NULL) && !NET_MAC_IS_MULTICAST (MacAddress, &SnpMode->BroadcastAddress, SnpMode->HwAddressSize))) { + // + // The instance isn't configured to do mulitcast receive. OR + // the passed in MacAddress is not a mutlticast mac address. + // + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + Status = EFI_SUCCESS; + AddressExist = FALSE; + GroupCtrlBlk = NULL; + + if (MacAddress != NULL) { + // + // Search the instance's GroupCtrlBlkList to find the specific address. + // + NET_LIST_FOR_EACH (ListEntry, &Instance->GroupCtrlBlkList) { + + GroupCtrlBlk = NET_LIST_USER_STRUCT ( + ListEntry, + MNP_GROUP_CONTROL_BLOCK, + CtrlBlkEntry + ); + GroupAddress = GroupCtrlBlk->GroupAddress; + if (0 == CompareMem ( + MacAddress, + &GroupAddress->Address, + SnpMode->HwAddressSize + )) { + // + // There is already the same multicast mac address configured. + // + AddressExist = TRUE; + break; + } + } + + if (JoinFlag && AddressExist) { + // + // The multicast mac address to join already exists. + // + Status = EFI_ALREADY_STARTED; + } + + if (!JoinFlag && !AddressExist) { + // + // The multicast mac address to leave doesn't exist in this instance. + // + Status = EFI_NOT_FOUND; + } + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } else if (IsListEmpty (&Instance->GroupCtrlBlkList)) { + // + // The MacAddress is NULL and there is no configured multicast mac address, + // just return. + // + goto ON_EXIT; + } + + // + // OK, it is time to take action. + // + Status = MnpGroupOp (Instance, JoinFlag, MacAddress, GroupCtrlBlk); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Places asynchronous outgoing data packets into the transmit queue. + + The Transmit() function places a completion token into the transmit packet + queue. This function is always asynchronous. + The caller must fill in the Token.Event and Token.TxData fields in the + completion token, and these fields cannot be NULL. When the transmit operation + completes, the MNP updates the Token.Status field and the Token.Event is + signaled. + Note: There may be a performance penalty if the packet needs to be + defragmented before it can be transmitted by the network device. Systems in + which performance is critical should review the requirements and features of + the underlying communications device and drivers. + + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token associated with the transmit data + descriptor. Type EFI_MANAGED_NETWORK_COMPLETION_TOKEN + is defined in "Related Definitions" below. + + @retval EFI_SUCCESS The transmit completion token was cached. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * Token is NULL. + * Token.Event is NULL. + * Token.TxData is NULL. + * Token.TxData.DestinationAddress is not + NULL and Token.TxData.HeaderLength is zero. + * Token.TxData.FragmentCount is zero. + * (Token.TxData.HeaderLength + + Token.TxData.DataLength) is not equal to the + sum of the + Token.TxData.FragmentTable[].FragmentLength + fields. + * One or more of the + Token.TxData.FragmentTable[].FragmentLength + fields is zero. + * One or more of the + Token.TxData.FragmentTable[].FragmentBufferfields + is NULL. + * Token.TxData.DataLength is greater than MTU. + @retval EFI_ACCESS_DENIED The transmit completion token is already in the + transmit queue. + @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a + lack of system resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_NOT_READY The transmit request could not be queued because + the transmit queue is full. + +**/ +EFI_STATUS +EFIAPI +MnpTransmit ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + MNP_SERVICE_DATA *MnpServiceData; + UINT8 *PktBuf; + UINT32 PktLen; + EFI_TPL OldTpl; + + if ((This == NULL) || (Token == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + if (!MnpIsValidTxToken (Instance, Token)) { + // + // The Token is invalid. + // + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + MnpServiceData = Instance->MnpServiceData; + NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE); + + // + // Build the tx packet + // + Status = MnpBuildTxPacket (MnpServiceData, Token->Packet.TxData, &PktBuf, &PktLen); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // OK, send the packet synchronously. + // + Status = MnpSyncSendPacket (MnpServiceData, PktBuf, PktLen, Token); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Places an asynchronous receiving request into the receiving queue. + + The Receive() function places a completion token into the receive packet + queue. This function is always asynchronous. + The caller must fill in the Token.Event field in the completion token, and + this field cannot be NULL. When the receive operation completes, the MNP + updates the Token.Status and Token.RxData fields and the Token.Event is + signaled. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token associated with the receive + data descriptor. Type + EFI_MANAGED_NETWORK_COMPLETION_TOKEN is defined in + EFI_MANAGED_NETWORK_PROTOCOL.Transmit(). + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is + TRUE: + * This is NULL. + * Token is NULL. + * Token.Event is NULL + @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a + lack of system resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The MNP child driver instance has been reset to + startup defaults. + @retval EFI_ACCESS_DENIED The receive completion token was already in the + receive queue. + @retval EFI_NOT_READY The receive request could not be queued because + the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +MnpReceive ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + // + // Check whether this token(event) is already in the rx token queue. + // + Status = NetMapIterate (&Instance->RxTokenMap, MnpTokenExist, (VOID *) Token); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Insert the Token into the RxTokenMap. + // + Status = NetMapInsertTail (&Instance->RxTokenMap, (VOID *) Token, NULL); + if (!EFI_ERROR (Status)) { + // + // Try to deliver any buffered packets. + // + Status = MnpInstanceDeliverPacket (Instance); + + // + // Dispatch the DPC queued by the NotifyFunction of Token->Event. + // + DispatchDpc (); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Aborts an asynchronous transmit or receive request. + + The Cancel() function is used to abort a pending transmit or receive request. + If the token is in the transmit or receive request queues, after calling this + function, Token.Status will be set to EFI_ABORTED and then Token.Event will be + signaled. If the token is not in one of the queues, which usually means that + the asynchronous operation has completed, this function will not signal the + token and EFI_NOT_FOUND is returned. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_MANAGED_NETWORK_PROTOCOL.Transmit() or + EFI_MANAGED_NETWORK_PROTOCOL.Receive(). If NULL, all + pending tokens are aborted. + + @retval EFI_SUCCESS The asynchronous I/O request was aborted and + Token.Event was signaled. When Token is NULL, + all pending requests were aborted and their + events were signaled. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O + request was not found in the transmit or + receive queue. It has either completed or was + not issued by Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +MnpCancel ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This, + IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + // + // Iterate the RxTokenMap to cancel the specified Token. + // + Status = NetMapIterate (&Instance->RxTokenMap, MnpCancelTokens, (VOID *) Token); + if (Token != NULL) { + Status = (Status == EFI_ABORTED) ? EFI_SUCCESS : EFI_NOT_FOUND; + } + + // + // Dispatch the DPC queued by the NotifyFunction of the cancled token's events. + // + DispatchDpc (); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications to + increase the rate that data packets are moved between the communications + device and the transmit and receive queues. + Normally, a periodic timer event internally calls the Poll() function. But, in + some systems, the periodic timer event may not call Poll() fast enough to + transmit and/or receive all data packets without missing packets. Drivers and + applications that are experiencing packet loss should try calling the Poll() + function more often. + + @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This MNP child driver instance has not been + configured. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The + MNP child driver instance has been reset to startup + defaults. + @retval EFI_NOT_READY No incoming or outgoing data was processed. Consider + increasing the polling rate. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive + queue. Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +MnpPoll ( + IN EFI_MANAGED_NETWORK_PROTOCOL *This + ) +{ + EFI_STATUS Status; + MNP_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MNP_INSTANCE_DATA_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (!Instance->Configured) { + Status = EFI_NOT_STARTED; + goto ON_EXIT; + } + + // + // Try to receive packets. + // + Status = MnpReceivePacket (Instance->MnpServiceData->MnpDeviceData); + + // + // Dispatch the DPC queued by the NotifyFunction of rx token's events. + // + DispatchDpc (); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/MnpDxe/MnpVlan.c b/NetworkPkg/MnpDxe/MnpVlan.c new file mode 100644 index 000000000..54fb18950 --- /dev/null +++ b/NetworkPkg/MnpDxe/MnpVlan.c @@ -0,0 +1,732 @@ +/** @file + VLAN Config Protocol implementation and VLAN packet process routine. + +Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "MnpImpl.h" +#include "MnpVlan.h" + +VLAN_DEVICE_PATH mVlanDevicePathTemplate = { + { + MESSAGING_DEVICE_PATH, + MSG_VLAN_DP, + { + (UINT8) (sizeof (VLAN_DEVICE_PATH)), + (UINT8) ((sizeof (VLAN_DEVICE_PATH)) >> 8) + } + }, + 0 +}; + +EFI_VLAN_CONFIG_PROTOCOL mVlanConfigProtocolTemplate = { + VlanConfigSet, + VlanConfigFind, + VlanConfigRemove +}; + + +/** + Create a child handle for the VLAN ID. + + @param[in] ImageHandle The driver image handle. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] VlanId The VLAN ID. + @param[out] Devicepath Pointer to returned device path for child handle. + + @return The handle of VLAN child or NULL if failed to create VLAN child. + +**/ +EFI_HANDLE +MnpCreateVlanChild ( + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle, + IN UINT16 VlanId, + OUT EFI_DEVICE_PATH_PROTOCOL **Devicepath OPTIONAL + ) +{ + EFI_HANDLE ChildHandle; + VLAN_DEVICE_PATH VlanNode; + EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; + EFI_DEVICE_PATH_PROTOCOL *VlanDevicePath; + EFI_STATUS Status; + + // + // Try to get parent device path + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDevicePathProtocolGuid, + (VOID **) &ParentDevicePath, + ImageHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return NULL; + } + + // + // Construct device path for child handle: MAC + VLAN + // + CopyMem (&VlanNode, &mVlanDevicePathTemplate, sizeof (VLAN_DEVICE_PATH)); + VlanNode.VlanId = VlanId; + VlanDevicePath = AppendDevicePathNode ( + ParentDevicePath, + (EFI_DEVICE_PATH_PROTOCOL *) &VlanNode + ); + if (VlanDevicePath == NULL) { + return NULL; + } + + // + // Create child VLAN handle by installing DevicePath protocol + // + ChildHandle = NULL; + Status = gBS->InstallMultipleProtocolInterfaces ( + &ChildHandle, + &gEfiDevicePathProtocolGuid, + VlanDevicePath, + NULL + ); + if (EFI_ERROR (Status)) { + FreePool (VlanDevicePath); + return NULL; + } + + if (Devicepath != NULL) { + *Devicepath = VlanDevicePath; + } + + return ChildHandle; +} + +/** + Remove VLAN tag from a packet. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in, out] Nbuf Pointer to the NET_BUF to remove VLAN tag. + @param[out] VlanId Pointer to the returned VLAN ID. + + @retval TRUE VLAN tag is removed from this packet. + @retval FALSE There is no VLAN tag in this packet. + +**/ +BOOLEAN +MnpRemoveVlanTag ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN OUT NET_BUF *Nbuf, + OUT UINT16 *VlanId + ) +{ + UINT8 *Packet; + UINTN ProtocolOffset; + UINT16 ProtocolType; + VLAN_TCI VlanTag; + + ProtocolOffset = MnpDeviceData->Snp->Mode->HwAddressSize * 2; + + // + // Get the packet buffer. + // + Packet = NetbufGetByte (Nbuf, 0, NULL); + ASSERT (Packet != NULL); + + // + // Check whether this is VLAN tagged frame by Ether Type + // + *VlanId = 0; + ProtocolType = NTOHS (*(UINT16 *) (Packet + ProtocolOffset)); + if (ProtocolType != ETHER_TYPE_VLAN) { + // + // Not a VLAN tagged frame + // + return FALSE; + } + + VlanTag.Uint16 = NTOHS (*(UINT16 *) (Packet + ProtocolOffset + sizeof (ProtocolType))); + *VlanId = VlanTag.Bits.Vid; + + // + // Move hardware address (DA + SA) 4 bytes right to override VLAN tag + // + CopyMem (Packet + NET_VLAN_TAG_LEN, Packet, ProtocolOffset); + + // + // Remove VLAN tag from the Nbuf + // + NetbufTrim (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_HEAD); + + return TRUE; +} + + +/** + Build the vlan packet to transmit from the TxData passed in. + + @param MnpServiceData Pointer to the mnp service context data. + @param TxData Pointer to the transmit data containing the + information to build the packet. + @param ProtocolType Pointer to the Ethernet protocol type. + @param Packet Pointer to record the address of the packet. + @param Length Pointer to a UINT32 variable used to record the + packet's length. + +**/ +VOID +MnpInsertVlanTag ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData, + OUT UINT16 *ProtocolType, + IN OUT UINT8 **Packet, + IN OUT UINT32 *Length + ) +{ + VLAN_TCI *VlanTci; + UINT16 *Tpid; + UINT16 *EtherType; + MNP_DEVICE_DATA *MnpDeviceData; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + + MnpDeviceData = MnpServiceData->MnpDeviceData; + SnpMode = MnpDeviceData->Snp->Mode; + + *ProtocolType = ETHER_TYPE_VLAN; + *Length = *Length + NET_VLAN_TAG_LEN; + *Packet = *Packet - NET_VLAN_TAG_LEN; + + Tpid = (UINT16 *) (*Packet + SnpMode->MediaHeaderSize - sizeof (*ProtocolType)); + VlanTci = (VLAN_TCI *) (UINTN) (Tpid + 1); + if (TxData->HeaderLength != 0) { + // + // Media header is in packet, move DA+SA 4 bytes left + // + CopyMem ( + *Packet, + *Packet + NET_VLAN_TAG_LEN, + SnpMode->MediaHeaderSize - sizeof (*ProtocolType) + ); + *Tpid = HTONS (ETHER_TYPE_VLAN); + } else { + // + // Media header not in packet, VLAN TCI and original protocol type becomes payload + // + EtherType = (UINT16 *) (UINTN) (VlanTci + 1); + *EtherType = HTONS (TxData->ProtocolType); + } + + VlanTci->Bits.Vid = MnpServiceData->VlanId; + VlanTci->Bits.Cfi = VLAN_TCI_CFI_CANONICAL_MAC; + VlanTci->Bits.Priority = MnpServiceData->Priority; + VlanTci->Uint16 = HTONS (VlanTci->Uint16); +} + +/** + Check VLAN configuration variable and delete the duplicative content if has identical Vlan ID. + + @param[in] MnpDeviceData Pointer to the MNP device context data. + @param[in] Buffer Pointer to the buffer contains the array of VLAN_TCI. + @param[in] NumberOfVlan Pointer to number of VLAN. + @param[out] NewNumberOfVlan Pointer to number of unique VLAN. + + @retval EFI_SUCCESS The VLAN variable is successfully checked. + @retval EFI_OUT_OF_RESOURCES There is not enough resource to set the configuration. + +**/ +EFI_STATUS +MnpCheckVlanVariable ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN VLAN_TCI *Buffer, + IN UINTN NumberOfVlan, + OUT UINTN *NewNumberOfVlan + ) +{ + UINTN Index; + UINTN Index2; + UINTN Count; + BOOLEAN FoundDuplicateItem; + EFI_STATUS Status; + + Count = 0; + FoundDuplicateItem = FALSE; + Status = EFI_SUCCESS; + + for (Index = 0; Index < NumberOfVlan; Index++) { + for (Index2 = Index + 1; Index2 < NumberOfVlan; Index2++) { + if (Buffer[Index].Bits.Vid == Buffer[Index2].Bits.Vid) { + FoundDuplicateItem = TRUE; + Count++; + break; + } + } + if (FoundDuplicateItem) { + for (Index2 = Index +1; Index2 < NumberOfVlan; Index++, Index2++) { + CopyMem (Buffer + Index, Buffer + Index2, sizeof (VLAN_TCI)); + } + } + FoundDuplicateItem = FALSE; + } + + *NewNumberOfVlan = NumberOfVlan - Count; + if (Count != 0) { + Status = MnpSetVlanVariable (MnpDeviceData, *NewNumberOfVlan, Buffer); + } + + return Status; +} + +/** + Get VLAN configuration variable. + + @param[in] MnpDeviceData Pointer to the MNP device context data. + @param[out] NumberOfVlan Pointer to number of VLAN to be returned. + @param[out] VlanVariable Pointer to the buffer to return requested + array of VLAN_TCI. + + @retval EFI_SUCCESS The array of VLAN_TCI was returned in VlanVariable + and number of VLAN was returned in NumberOfVlan. + @retval EFI_NOT_FOUND VLAN configuration variable not found. + @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the configuration. + +**/ +EFI_STATUS +MnpGetVlanVariable ( + IN MNP_DEVICE_DATA *MnpDeviceData, + OUT UINTN *NumberOfVlan, + OUT VLAN_TCI **VlanVariable + ) +{ + UINTN BufferSize; + EFI_STATUS Status; + VLAN_TCI *Buffer; + UINTN NewNumberOfVlan; + + // + // Get VLAN configuration from EFI Variable + // + Buffer = NULL; + BufferSize = 0; + Status = gRT->GetVariable ( + MnpDeviceData->MacString, + &gEfiVlanConfigProtocolGuid, + NULL, + &BufferSize, + NULL + ); + if (Status != EFI_BUFFER_TOO_SMALL) { + return EFI_NOT_FOUND; + } + + // + // Allocate buffer to read the variable + // + Buffer = AllocateZeroPool (BufferSize); + if (Buffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = gRT->GetVariable ( + MnpDeviceData->MacString, + &gEfiVlanConfigProtocolGuid, + NULL, + &BufferSize, + Buffer + ); + if (EFI_ERROR (Status)) { + FreePool (Buffer); + return Status; + } + + Status = MnpCheckVlanVariable (MnpDeviceData, Buffer, BufferSize / sizeof (VLAN_TCI), &NewNumberOfVlan); + if (!EFI_ERROR (Status)) { + *NumberOfVlan = NewNumberOfVlan; + *VlanVariable = Buffer; + } + + return Status; +} + +/** + Set VLAN configuration variable. + + @param[in] MnpDeviceData Pointer to the MNP device context data. + @param[in] NumberOfVlan Number of VLAN in array VlanVariable. + @param[in] VlanVariable Pointer to array of VLAN_TCI. + + @retval EFI_SUCCESS The VLAN variable is successfully set. + @retval EFI_OUT_OF_RESOURCES There is not enough resource to set the configuration. + +**/ +EFI_STATUS +MnpSetVlanVariable ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINTN NumberOfVlan, + IN VLAN_TCI *VlanVariable + ) +{ + return gRT->SetVariable ( + MnpDeviceData->MacString, + &gEfiVlanConfigProtocolGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS, + NumberOfVlan * sizeof (VLAN_TCI), + VlanVariable + ); +} + + +/** + Create a VLAN device or modify the configuration parameter of an + already-configured VLAN. + + The Set() function is used to create a new VLAN device or change the VLAN + configuration parameters. If the VlanId hasn't been configured in the + physical Ethernet device, a new VLAN device will be created. If a VLAN with + this VlanId is already configured, then related configuration will be updated + as the input parameters. + + If VlanId is zero, the VLAN device will send and receive untagged frames. + Otherwise, the VLAN device will send and receive VLAN-tagged frames containing the VlanId. + If VlanId is out of scope of (0-4094), EFI_INVALID_PARAMETER is returned. + If Priority is out of the scope of (0-7), then EFI_INVALID_PARAMETER is returned. + If there is not enough system memory to perform the registration, then + EFI_OUT_OF_RESOURCES is returned. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId A unique identifier (1-4094) of the VLAN which is being created + or modified, or zero (0). + @param[in] Priority 3 bit priority in VLAN header. Priority 0 is default value. If + VlanId is zero (0), Priority is ignored. + + @retval EFI_SUCCESS The VLAN is successfully configured. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - VlanId is an invalid VLAN Identifier. + - Priority is invalid. + @retval EFI_OUT_OF_RESOURCES There is not enough system memory to perform the registration. + +**/ +EFI_STATUS +EFIAPI +VlanConfigSet ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 VlanId, + IN UINT8 Priority + ) +{ + EFI_STATUS Status; + MNP_DEVICE_DATA *MnpDeviceData; + MNP_SERVICE_DATA *MnpServiceData; + VLAN_TCI *OldVariable; + VLAN_TCI *NewVariable; + UINTN NumberOfVlan; + UINTN Index; + BOOLEAN IsAdd; + LIST_ENTRY *Entry; + + if ((This == NULL) || (VlanId > 4094) || (Priority > 7)) { + return EFI_INVALID_PARAMETER; + } + + IsAdd = FALSE; + MnpDeviceData = MNP_DEVICE_DATA_FROM_THIS (This); + if (MnpDeviceData->NumberOfVlan == 0) { + // + // No existing VLAN, this is the first VLAN to add + // + IsAdd = TRUE; + Entry = GetFirstNode (&MnpDeviceData->ServiceList); + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + + if (VlanId != 0) { + // + // VlanId is not 0, need destroy the default MNP service data + // + Status = MnpDestroyServiceChild (MnpServiceData); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = MnpDestroyServiceData (MnpServiceData); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Create a new MNP service data for this VLAN + // + MnpServiceData = MnpCreateServiceData (MnpDeviceData, VlanId, Priority); + if (MnpServiceData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + } else { + // + // Try to find VlanId in existing VLAN list + // + MnpServiceData = MnpFindServiceData (MnpDeviceData, VlanId); + if (MnpServiceData == NULL) { + // + // VlanId not found, create a new MNP service data + // + IsAdd = TRUE; + MnpServiceData = MnpCreateServiceData (MnpDeviceData, VlanId, Priority); + if (MnpServiceData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + } + + MnpServiceData->VlanId = VlanId; + MnpServiceData->Priority = Priority; + if (IsAdd) { + MnpDeviceData->NumberOfVlan++; + } + + // + // Update VLAN configuration variable + // + OldVariable = NULL; + NewVariable = NULL; + NumberOfVlan = 0; + MnpGetVlanVariable (MnpDeviceData, &NumberOfVlan, &OldVariable); + + if (IsAdd) { + // + // VLAN not exist - add + // + NewVariable = AllocateZeroPool ((NumberOfVlan + 1) * sizeof (VLAN_TCI)); + if (NewVariable == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + if (OldVariable != NULL) { + CopyMem (NewVariable, OldVariable, NumberOfVlan * sizeof (VLAN_TCI)); + } + + Index = NumberOfVlan++; + } else { + // + // VLAN already exist - update + // + for (Index = 0; Index < NumberOfVlan; Index++) { + if (OldVariable[Index].Bits.Vid == VlanId) { + break; + } + } + ASSERT (Index < NumberOfVlan); + + NewVariable = OldVariable; + OldVariable = NULL; + } + + NewVariable[Index].Bits.Vid = VlanId; + NewVariable[Index].Bits.Priority = Priority; + + Status = MnpSetVlanVariable (MnpDeviceData, NumberOfVlan, NewVariable); + FreePool (NewVariable); + +Exit: + if (OldVariable != NULL) { + FreePool (OldVariable); + } + + return Status; +} + + +/** + Find configuration information for specified VLAN or all configured VLANs. + + The Find() function is used to find the configuration information for matching + VLAN and allocate a buffer into which those entries are copied. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId Pointer to VLAN identifier. Set to NULL to find all + configured VLANs. + @param[out] NumberOfVlan The number of VLANs which is found by the specified criteria. + @param[out] Entries The buffer which receive the VLAN configuration. + + @retval EFI_SUCCESS The VLAN is successfully found. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - Specified VlanId is invalid. + @retval EFI_NOT_FOUND No matching VLAN is found. + +**/ +EFI_STATUS +EFIAPI +VlanConfigFind ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 *VlanId OPTIONAL, + OUT UINT16 *NumberOfVlan, + OUT EFI_VLAN_FIND_DATA **Entries + ) +{ + MNP_DEVICE_DATA *MnpDeviceData; + MNP_SERVICE_DATA *MnpServiceData; + LIST_ENTRY *Entry; + EFI_VLAN_FIND_DATA *VlanData; + + if ((This == NULL) || (VlanId != NULL && *VlanId > 4094) || (NumberOfVlan == NULL) || (Entries == NULL)) { + return EFI_INVALID_PARAMETER; + } + + *NumberOfVlan = 0; + *Entries = NULL; + + MnpDeviceData = MNP_DEVICE_DATA_FROM_THIS (This); + if (MnpDeviceData->NumberOfVlan == 0) { + return EFI_NOT_FOUND; + } + + if (VlanId == NULL) { + // + // Return all current VLAN configuration + // + *NumberOfVlan = (UINT16) MnpDeviceData->NumberOfVlan; + VlanData = AllocateZeroPool (*NumberOfVlan * sizeof (EFI_VLAN_FIND_DATA)); + if (VlanData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + *Entries = VlanData; + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->ServiceList) { + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + + VlanData->VlanId = MnpServiceData->VlanId; + VlanData->Priority = MnpServiceData->Priority; + VlanData++; + } + + return EFI_SUCCESS; + } + + // + // VlanId is specified, try to find it in current VLAN list + // + MnpServiceData = MnpFindServiceData (MnpDeviceData, *VlanId); + if (MnpServiceData == NULL) { + return EFI_NOT_FOUND; + } + + VlanData = AllocateZeroPool (sizeof (EFI_VLAN_FIND_DATA)); + if (VlanData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + VlanData->VlanId = MnpServiceData->VlanId; + VlanData->Priority = MnpServiceData->Priority; + + *NumberOfVlan = 1; + *Entries = VlanData; + + return EFI_SUCCESS; +} + + +/** + Remove the configured VLAN device. + + The Remove() function is used to remove the specified VLAN device. + If the VlanId is out of the scope of (0-4094), EFI_INVALID_PARAMETER is returned. + If specified VLAN hasn't been previously configured, EFI_NOT_FOUND is returned. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId Identifier (0-4094) of the VLAN to be removed. + + @retval EFI_SUCCESS The VLAN is successfully removed. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - VlanId is an invalid parameter. + @retval EFI_NOT_FOUND The to-be-removed VLAN does not exist. + +**/ +EFI_STATUS +EFIAPI +VlanConfigRemove ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 VlanId + ) +{ + EFI_STATUS Status; + MNP_DEVICE_DATA *MnpDeviceData; + MNP_SERVICE_DATA *MnpServiceData; + LIST_ENTRY *Entry; + VLAN_TCI *VlanVariable; + VLAN_TCI *VlanData; + + if ((This == NULL) || (VlanId > 4094)) { + return EFI_INVALID_PARAMETER; + } + + MnpDeviceData = MNP_DEVICE_DATA_FROM_THIS (This); + if (MnpDeviceData->NumberOfVlan == 0) { + return EFI_NOT_FOUND; + } + + // + // Try to find the VlanId + // + MnpServiceData = MnpFindServiceData (MnpDeviceData, VlanId); + if (MnpServiceData == NULL) { + return EFI_NOT_FOUND; + } + + MnpDeviceData->NumberOfVlan--; + + if ((VlanId != 0) || (MnpDeviceData->NumberOfVlan != 0)) { + // + // If VlanId is not 0 or VlanId is 0 and it is not the last VLAN to remove, + // destroy its MNP service data + // + Status = MnpDestroyServiceChild (MnpServiceData); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = MnpDestroyServiceData (MnpServiceData); + if (EFI_ERROR (Status)) { + return Status; + } + } + + if ((VlanId != 0) && (MnpDeviceData->NumberOfVlan == 0)) { + // + // This is the last VLAN to be removed, restore the default MNP service data + // + MnpServiceData = MnpCreateServiceData (MnpDeviceData, 0, 0); + if (MnpServiceData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + + // + // Update VLAN configuration variable + // + VlanVariable = NULL; + if (MnpDeviceData->NumberOfVlan != 0) { + VlanVariable = AllocatePool (MnpDeviceData->NumberOfVlan * sizeof (VLAN_TCI)); + if (VlanVariable == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + VlanData = VlanVariable; + NET_LIST_FOR_EACH (Entry, &MnpDeviceData->ServiceList) { + MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry); + + VlanData->Bits.Vid = MnpServiceData->VlanId; + VlanData->Bits.Priority = MnpServiceData->Priority; + VlanData++; + } + } + + Status = MnpSetVlanVariable (MnpDeviceData, MnpDeviceData->NumberOfVlan, VlanVariable); + + if (VlanVariable != NULL) { + FreePool (VlanVariable); + } + + return Status; +} diff --git a/NetworkPkg/MnpDxe/MnpVlan.h b/NetworkPkg/MnpDxe/MnpVlan.h new file mode 100644 index 000000000..4e0ddef2d --- /dev/null +++ b/NetworkPkg/MnpDxe/MnpVlan.h @@ -0,0 +1,205 @@ +/** @file + Header file to be included by MnpVlan.c. + +Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __MNP_VLAN_H__ +#define __MNP_VLAN_H__ + +#include "MnpDriver.h" + +extern EFI_VLAN_CONFIG_PROTOCOL mVlanConfigProtocolTemplate; + + +/** + Create a child handle for the VLAN ID. + + @param[in] ImageHandle The driver image handle. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] VlanId The VLAN ID. + @param[out] Devicepath Pointer to returned device path for child handle. + + @return The handle of VLAN child or NULL if failed to create VLAN child. + +**/ +EFI_HANDLE +MnpCreateVlanChild ( + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle, + IN UINT16 VlanId, + OUT EFI_DEVICE_PATH_PROTOCOL **Devicepath OPTIONAL + ); + +/** + Remove VLAN tag of a packet. + + @param[in, out] MnpDeviceData Pointer to the mnp device context data. + @param[in, out] Nbuf Pointer to the NET_BUF to remove VLAN tag. + @param[out] VlanId Pointer to the returned VLAN ID. + + @retval TRUE VLAN tag is removed from this packet. + @retval FALSE There is no VLAN tag in this packet. + +**/ +BOOLEAN +MnpRemoveVlanTag ( + IN OUT MNP_DEVICE_DATA *MnpDeviceData, + IN OUT NET_BUF *Nbuf, + OUT UINT16 *VlanId + ); + +/** + Build the vlan packet to transmit from the TxData passed in. + + @param MnpServiceData Pointer to the mnp service context data. + @param TxData Pointer to the transmit data containing the + information to build the packet. + @param ProtocolType Pointer to the Ethernet protocol type. + @param Packet Pointer to record the address of the packet. + @param Length Pointer to a UINT32 variable used to record the + packet's length. + +**/ +VOID +MnpInsertVlanTag ( + IN MNP_SERVICE_DATA *MnpServiceData, + IN EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData, + OUT UINT16 *ProtocolType, + IN OUT UINT8 **Packet, + IN OUT UINT32 *Length + ); + +/** + Get VLAN configuration variable. + + @param[in] MnpDeviceData Pointer to the MNP device context data. + @param[out] NumberOfVlan Pointer to number of VLAN to be returned. + @param[out] VlanVariable Pointer to the buffer to return requested + array of VLAN_TCI. + + @retval EFI_SUCCESS The array of VLAN_TCI was returned in VlanVariable + and number of VLAN was returned in NumberOfVlan. + @retval EFI_NOT_FOUND VLAN configuration variable not found. + @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the configuration. + +**/ +EFI_STATUS +MnpGetVlanVariable ( + IN MNP_DEVICE_DATA *MnpDeviceData, + OUT UINTN *NumberOfVlan, + OUT VLAN_TCI **VlanVariable + ); + +/** + Set VLAN configuration variable. + + @param[in] MnpDeviceData Pointer to the MNP device context data. + @param[in] NumberOfVlan Number of VLAN in array VlanVariable. + @param[in] VlanVariable Pointer to array of VLAN_TCI. + + @retval EFI_SUCCESS The VLAN variable is successfully set. + @retval EFI_OUT_OF_RESOURCES There is not enough resource to set the configuration. + +**/ +EFI_STATUS +MnpSetVlanVariable ( + IN MNP_DEVICE_DATA *MnpDeviceData, + IN UINTN NumberOfVlan, + IN VLAN_TCI *VlanVariable + ); + +/** + Create a VLAN device or modify the configuration parameter of an + already-configured VLAN. + + The Set() function is used to create a new VLAN device or change the VLAN + configuration parameters. If the VlanId hasn't been configured in the + physical Ethernet device, a new VLAN device will be created. If a VLAN with + this VlanId is already configured, then related configuration will be updated + as the input parameters. + + If VlanId is zero, the VLAN device will send and receive untagged frames. + Otherwise, the VLAN device will send and receive VLAN-tagged frames containing the VlanId. + If VlanId is out of scope of (0-4094), EFI_INVALID_PARAMETER is returned. + If Priority is out of the scope of (0-7), then EFI_INVALID_PARAMETER is returned. + If there is not enough system memory to perform the registration, then + EFI_OUT_OF_RESOURCES is returned. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId A unique identifier (1-4094) of the VLAN which is being created + or modified, or zero (0). + @param[in] Priority 3 bit priority in VLAN header. Priority 0 is default value. If + VlanId is zero (0), Priority is ignored. + + @retval EFI_SUCCESS The VLAN is successfully configured. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - VlanId is an invalid VLAN Identifier. + - Priority is invalid. + @retval EFI_OUT_OF_RESOURCES There is not enough system memory to perform the registration. + +**/ +EFI_STATUS +EFIAPI +VlanConfigSet ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 VlanId, + IN UINT8 Priority + ); + +/** + Find configuration information for specified VLAN or all configured VLANs. + + The Find() function is used to find the configuration information for matching + VLAN and allocate a buffer into which those entries are copied. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId Pointer to VLAN identifier. Set to NULL to find all + configured VLANs. + @param[out] NumberOfVlan The number of VLANs which is found by the specified criteria. + @param[out] Entries The buffer which receive the VLAN configuration. + + @retval EFI_SUCCESS The VLAN is successfully found. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - Specified VlanId is invalid. + @retval EFI_NOT_FOUND No matching VLAN is found. + +**/ +EFI_STATUS +EFIAPI +VlanConfigFind ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 *VlanId OPTIONAL, + OUT UINT16 *NumberOfVlan, + OUT EFI_VLAN_FIND_DATA **Entries + ); + +/** + Remove the configured VLAN device. + + The Remove() function is used to remove the specified VLAN device. + If the VlanId is out of the scope of (0-4094), EFI_INVALID_PARAMETER is returned. + If specified VLAN hasn't been previously configured, EFI_NOT_FOUND is returned. + + @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL. + @param[in] VlanId Identifier (0-4094) of the VLAN to be removed. + + @retval EFI_SUCCESS The VLAN is successfully removed. + @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE: + - This is NULL. + - VlanId is an invalid parameter. + @retval EFI_NOT_FOUND The to-be-removed VLAN does not exist. + +**/ +EFI_STATUS +EFIAPI +VlanConfigRemove ( + IN EFI_VLAN_CONFIG_PROTOCOL *This, + IN UINT16 VlanId + ); + +#endif diff --git a/NetworkPkg/Mtftp4Dxe/ComponentName.c b/NetworkPkg/Mtftp4Dxe/ComponentName.c new file mode 100644 index 000000000..8b84caf11 --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/ComponentName.c @@ -0,0 +1,425 @@ +/** @file + UEFI Component Name(2) protocol implementation for Mtftp4Dxe driver. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp4Impl.h" + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Mtftp4ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Mtftp4ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + + +/// +/// EFI Component Name Protocol +/// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gMtftp4ComponentName = { + Mtftp4ComponentNameGetDriverName, + Mtftp4ComponentNameGetControllerName, + "eng" +}; + +/// +/// EFI Component Name 2 Protocol +/// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gMtftp4ComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) Mtftp4ComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) Mtftp4ComponentNameGetControllerName, + "en" +}; + + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mMtftp4DriverNameTable[] = { + { + "eng;en", + L"MTFTP4 Network Service" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gMtftp4ControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Mtftp4ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mMtftp4DriverNameTable, + DriverName, + (BOOLEAN)(This == &gMtftp4ComponentName) + ); +} + +/** + Update the component name for the Mtftp4 child handle. + + @param Mtftp4[in] A pointer to the EFI_MTFTP4_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateName ( + IN EFI_MTFTP4_PROTOCOL *Mtftp4 + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[80]; + EFI_MTFTP4_MODE_DATA ModeData; + + if (Mtftp4 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer as: + // MTFTPv4 (ServerIp=192.168.1.10, ServerPort=69) + // + Status = Mtftp4->GetModeData (Mtftp4, &ModeData); + if (EFI_ERROR (Status)) { + return Status; + } + + UnicodeSPrint (HandleName, sizeof (HandleName), + L"MTFTPv4 (ServerIp=%d.%d.%d.%d, ServerPort=%d)", + ModeData.ConfigData.ServerIp.Addr[0], + ModeData.ConfigData.ServerIp.Addr[1], + ModeData.ConfigData.ServerIp.Addr[2], + ModeData.ConfigData.ServerIp.Addr[3], + ModeData.ConfigData.InitialServerPort + ); + + if (gMtftp4ControllerNameTable != NULL) { + FreeUnicodeStringTable (gMtftp4ControllerNameTable); + gMtftp4ControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gMtftp4ComponentName.SupportedLanguages, + &gMtftp4ControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gMtftp4ComponentName2.SupportedLanguages, + &gMtftp4ControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Mtftp4ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_MTFTP4_PROTOCOL *Mtftp4; + + // + // Only provide names for child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiUdp4ProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiMtftp4ProtocolGuid, + (VOID **)&Mtftp4, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateName (Mtftp4); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gMtftp4ControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gMtftp4ComponentName) + ); +} diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Driver.c b/NetworkPkg/Mtftp4Dxe/Mtftp4Driver.c new file mode 100644 index 000000000..ae9e65544 --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Driver.c @@ -0,0 +1,739 @@ +/** @file + Implementation of Mtftp drivers. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp4Impl.h" + +EFI_DRIVER_BINDING_PROTOCOL gMtftp4DriverBinding = { + Mtftp4DriverBindingSupported, + Mtftp4DriverBindingStart, + Mtftp4DriverBindingStop, + 0xa, + NULL, + NULL +}; + +EFI_SERVICE_BINDING_PROTOCOL gMtftp4ServiceBindingTemplete = { + Mtftp4ServiceBindingCreateChild, + Mtftp4ServiceBindingDestroyChild +}; + + +/** + The driver entry point which installs multiple protocols to the ImageHandle. + + @param ImageHandle The MTFTP's image handle. + @param SystemTable The system table. + + @retval EFI_SUCCESS The handles are successfully installed on the image. + @retval others some EFI_ERROR occured. + +**/ +EFI_STATUS +EFIAPI +Mtftp4DriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gMtftp4DriverBinding, + ImageHandle, + &gMtftp4ComponentName, + &gMtftp4ComponentName2 + ); +} + + +/** + Test whether MTFTP driver support this controller. + + @param This The MTFTP driver binding instance + @param Controller The controller to test + @param RemainingDevicePath The remaining device path + + @retval EFI_SUCCESS The controller has UDP service binding protocol + installed, MTFTP can support it. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by + the driver specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a + different driver or an application that requires + exclusive access. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver + specified by This. + +**/ +EFI_STATUS +EFIAPI +Mtftp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS Status; + + Status = gBS->OpenProtocol ( + Controller, + &gEfiUdp4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + return Status; +} + + +/** + Config a NULL UDP that is used to keep the connection between UDP and MTFTP. + + Just leave the Udp child unconfigured. When UDP is unloaded, + MTFTP will be informed with DriverBinding Stop. + + @param UdpIo The UDP_IO to configure + @param Context The opaque parameter to the callback + + @retval EFI_SUCCESS It always return EFI_SUCCESS directly. + +**/ +EFI_STATUS +EFIAPI +Mtftp4ConfigNullUdp ( + IN UDP_IO *UdpIo, + IN VOID *Context + ) +{ + return EFI_SUCCESS; +} + + +/** + Create then initialize a MTFTP service binding instance. + + @param Controller The controller to install the MTFTP service + binding on + @param Image The driver binding image of the MTFTP driver + @param Service The variable to receive the created service + binding instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to create the instance + @retval EFI_DEVICE_ERROR Failed to create a NULL UDP port to keep + connection with UDP. + @retval EFI_SUCCESS The service instance is created for the + controller. + +**/ +EFI_STATUS +Mtftp4CreateService ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE Image, + OUT MTFTP4_SERVICE **Service + ) +{ + MTFTP4_SERVICE *MtftpSb; + EFI_STATUS Status; + + *Service = NULL; + MtftpSb = AllocatePool (sizeof (MTFTP4_SERVICE)); + + if (MtftpSb == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + MtftpSb->Signature = MTFTP4_SERVICE_SIGNATURE; + MtftpSb->ServiceBinding = gMtftp4ServiceBindingTemplete; + MtftpSb->ChildrenNum = 0; + InitializeListHead (&MtftpSb->Children); + + MtftpSb->Timer = NULL; + MtftpSb->TimerNotifyLevel = NULL; + MtftpSb->TimerToGetMap = NULL; + MtftpSb->Controller = Controller; + MtftpSb->Image = Image; + MtftpSb->ConnectUdp = NULL; + + // + // Create the timer and a udp to be notified when UDP is uninstalled + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + Mtftp4OnTimerTick, + MtftpSb, + &MtftpSb->Timer + ); + if (EFI_ERROR (Status)) { + FreePool (MtftpSb); + return Status; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_NOTIFY, + Mtftp4OnTimerTickNotifyLevel, + MtftpSb, + &MtftpSb->TimerNotifyLevel + ); + if (EFI_ERROR (Status)) { + gBS->CloseEvent (MtftpSb->Timer); + FreePool (MtftpSb); + return Status; + } + + // + // Create the timer used to time out the procedure which is used to + // get the default IP address. + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &MtftpSb->TimerToGetMap + ); + if (EFI_ERROR (Status)) { + gBS->CloseEvent (MtftpSb->TimerNotifyLevel); + gBS->CloseEvent (MtftpSb->Timer); + FreePool (MtftpSb); + return Status; + } + + MtftpSb->ConnectUdp = UdpIoCreateIo ( + Controller, + Image, + Mtftp4ConfigNullUdp, + UDP_IO_UDP4_VERSION, + NULL + ); + + if (MtftpSb->ConnectUdp == NULL) { + gBS->CloseEvent (MtftpSb->TimerToGetMap); + gBS->CloseEvent (MtftpSb->TimerNotifyLevel); + gBS->CloseEvent (MtftpSb->Timer); + FreePool (MtftpSb); + return EFI_DEVICE_ERROR; + } + + *Service = MtftpSb; + return EFI_SUCCESS; +} + + +/** + Release all the resource used the MTFTP service binding instance. + + @param MtftpSb The MTFTP service binding instance. + +**/ +VOID +Mtftp4CleanService ( + IN MTFTP4_SERVICE *MtftpSb + ) +{ + UdpIoFreeIo (MtftpSb->ConnectUdp); + gBS->CloseEvent (MtftpSb->TimerToGetMap); + gBS->CloseEvent (MtftpSb->TimerNotifyLevel); + gBS->CloseEvent (MtftpSb->Timer); +} + + +/** + Start the MTFTP driver on this controller. + + MTFTP driver will install a MTFTP SERVICE BINDING protocol on the supported + controller, which can be used to create/destroy MTFTP children. + + @param This The MTFTP driver binding protocol. + @param Controller The controller to manage. + @param RemainingDevicePath Remaining device path. + + @retval EFI_ALREADY_STARTED The MTFTP service binding protocol has been + started on the controller. + @retval EFI_SUCCESS The MTFTP service binding is installed on the + controller. + +**/ +EFI_STATUS +EFIAPI +Mtftp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + MTFTP4_SERVICE *MtftpSb; + EFI_STATUS Status; + + // + // Directly return if driver is already running. + // + Status = gBS->OpenProtocol ( + Controller, + &gEfiMtftp4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + if (Status == EFI_SUCCESS) { + return EFI_ALREADY_STARTED; + } + + Status = Mtftp4CreateService (Controller, This->DriverBindingHandle, &MtftpSb); + + if (EFI_ERROR (Status)) { + return Status; + } + ASSERT (MtftpSb != NULL); + + Status = gBS->SetTimer (MtftpSb->Timer, TimerPeriodic, TICKS_PER_SECOND); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->SetTimer (MtftpSb->TimerNotifyLevel, TimerPeriodic, TICKS_PER_SECOND); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Install the Mtftp4ServiceBinding Protocol onto Controller + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &Controller, + &gEfiMtftp4ServiceBindingProtocolGuid, + &MtftpSb->ServiceBinding, + NULL + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + Mtftp4CleanService (MtftpSb); + FreePool (MtftpSb); + + return Status; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +Mtftp4DestroyChildEntryInHandleBuffer ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + MTFTP4_PROTOCOL *Instance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = NET_LIST_USER_STRUCT_S (Entry, MTFTP4_PROTOCOL, Link, MTFTP4_PROTOCOL_SIGNATURE); + ServiceBinding = ((MTFTP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding; + NumberOfChildren = ((MTFTP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren; + ChildHandleBuffer = ((MTFTP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer; + + if (!NetIsInHandleBuffer (Instance->Handle, NumberOfChildren, ChildHandleBuffer)) { + return EFI_SUCCESS; + } + + return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle); +} + +/** + Stop the MTFTP driver on controller. The controller is a UDP + child handle. + + @param This The MTFTP driver binding protocol + @param Controller The controller to stop + @param NumberOfChildren The number of children + @param ChildHandleBuffer The array of the child handle. + + @retval EFI_SUCCESS The driver is stopped on the controller. + @retval EFI_DEVICE_ERROR Failed to stop the driver on the controller. + +**/ +EFI_STATUS +EFIAPI +Mtftp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + MTFTP4_SERVICE *MtftpSb; + EFI_HANDLE NicHandle; + EFI_STATUS Status; + LIST_ENTRY *List; + MTFTP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context; + + // + // MTFTP driver opens UDP child, So, Controller is a UDP + // child handle. Locate the Nic handle first. Then get the + // MTFTP private data back. + // + NicHandle = NetLibGetNicHandle (Controller, &gEfiUdp4ProtocolGuid); + + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiMtftp4ServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + NicHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + MtftpSb = MTFTP4_SERVICE_FROM_THIS (ServiceBinding); + + if (!IsListEmpty (&MtftpSb->Children)) { + // + // Destroy the Mtftp4 child instance in ChildHandleBuffer. + // + List = &MtftpSb->Children; + Context.ServiceBinding = ServiceBinding; + Context.NumberOfChildren = NumberOfChildren; + Context.ChildHandleBuffer = ChildHandleBuffer; + Status = NetDestroyLinkList ( + List, + Mtftp4DestroyChildEntryInHandleBuffer, + &Context, + NULL + ); + } + + if (NumberOfChildren == 0 && IsListEmpty (&MtftpSb->Children)) { + gBS->UninstallProtocolInterface ( + NicHandle, + &gEfiMtftp4ServiceBindingProtocolGuid, + ServiceBinding + ); + + Mtftp4CleanService (MtftpSb); + if (gMtftp4ControllerNameTable != NULL) { + FreeUnicodeStringTable (gMtftp4ControllerNameTable); + gMtftp4ControllerNameTable = NULL; + } + FreePool (MtftpSb); + + Status = EFI_SUCCESS; + } + + return Status; +} + + +/** + Initialize a MTFTP protocol instance which is the child of MtftpSb. + + @param MtftpSb The MTFTP service binding protocol. + @param Instance The MTFTP instance to initialize. + +**/ +VOID +Mtftp4InitProtocol ( + IN MTFTP4_SERVICE *MtftpSb, + OUT MTFTP4_PROTOCOL *Instance + ) +{ + ZeroMem (Instance, sizeof (MTFTP4_PROTOCOL)); + + Instance->Signature = MTFTP4_PROTOCOL_SIGNATURE; + InitializeListHead (&Instance->Link); + CopyMem (&Instance->Mtftp4, &gMtftp4ProtocolTemplate, sizeof (Instance->Mtftp4)); + Instance->State = MTFTP4_STATE_UNCONFIGED; + Instance->Service = MtftpSb; + + InitializeListHead (&Instance->Blocks); +} + + +/** + Create a MTFTP child for the service binding instance, then + install the MTFTP protocol to the ChildHandle. + + @param This The MTFTP service binding instance. + @param ChildHandle The Child handle to install the MTFTP protocol. + + @retval EFI_INVALID_PARAMETER The parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the new child. + @retval EFI_SUCCESS The child is successfully create. + +**/ +EFI_STATUS +EFIAPI +Mtftp4ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ) +{ + MTFTP4_SERVICE *MtftpSb; + MTFTP4_PROTOCOL *Instance; + EFI_STATUS Status; + EFI_TPL OldTpl; + VOID *Udp4; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = AllocatePool (sizeof (*Instance)); + + if (Instance == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + MtftpSb = MTFTP4_SERVICE_FROM_THIS (This); + + Mtftp4InitProtocol (MtftpSb, Instance); + + Instance->UnicastPort = UdpIoCreateIo ( + MtftpSb->Controller, + MtftpSb->Image, + Mtftp4ConfigNullUdp, + UDP_IO_UDP4_VERSION, + Instance + ); + + if (Instance->UnicastPort == NULL) { + FreePool (Instance); + return EFI_OUT_OF_RESOURCES; + } + + // + // Install the MTFTP protocol onto ChildHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiMtftp4ProtocolGuid, + &Instance->Mtftp4, + NULL + ); + + if (EFI_ERROR (Status)) { + UdpIoFreeIo (Instance->UnicastPort); + FreePool (Instance); + return Status; + } + + Instance->Handle = *ChildHandle; + + // + // Open the Udp4 protocol BY_CHILD. + // + Status = gBS->OpenProtocol ( + MtftpSb->ConnectUdp->UdpHandle, + &gEfiUdp4ProtocolGuid, + (VOID **) &Udp4, + gMtftp4DriverBinding.DriverBindingHandle, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Open the Udp4 protocol by child. + // + Status = gBS->OpenProtocol ( + Instance->UnicastPort->UdpHandle, + &gEfiUdp4ProtocolGuid, + (VOID **) &Udp4, + gMtftp4DriverBinding.DriverBindingHandle, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + // + // Close the Udp4 protocol. + // + gBS->CloseProtocol ( + MtftpSb->ConnectUdp->UdpHandle, + &gEfiUdp4ProtocolGuid, + gMtftp4DriverBinding.DriverBindingHandle, + ChildHandle + ); + goto ON_ERROR; + } + + // + // Add it to the parent's child list. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&MtftpSb->Children, &Instance->Link); + MtftpSb->ChildrenNum++; + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; + +ON_ERROR: + if (Instance->Handle != NULL) { + gBS->UninstallMultipleProtocolInterfaces ( + Instance->Handle, + &gEfiMtftp4ProtocolGuid, + &Instance->Mtftp4, + NULL + ); + } + + UdpIoFreeIo (Instance->UnicastPort); + FreePool (Instance); + + return Status; +} + + +/** + Destroy one of the service binding's child. + + @param This The service binding instance + @param ChildHandle The child handle to destroy + + @retval EFI_INVALID_PARAMETER The parameter is invaid. + @retval EFI_UNSUPPORTED The child may have already been destroyed. + @retval EFI_SUCCESS The child is destroyed and removed from the + parent's child list. + +**/ +EFI_STATUS +EFIAPI +Mtftp4ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + MTFTP4_SERVICE *MtftpSb; + MTFTP4_PROTOCOL *Instance; + EFI_MTFTP4_PROTOCOL *Mtftp4; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Retrieve the private context data structures + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiMtftp4ProtocolGuid, + (VOID **) &Mtftp4, + gMtftp4DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Instance = MTFTP4_PROTOCOL_FROM_THIS (Mtftp4); + MtftpSb = MTFTP4_SERVICE_FROM_THIS (This); + + if (Instance->Service != MtftpSb) { + return EFI_INVALID_PARAMETER; + } + + if (Instance->InDestroy) { + return EFI_SUCCESS; + } + + Instance->InDestroy = TRUE; + + // + // Close the Udp4 protocol. + // + gBS->CloseProtocol ( + MtftpSb->ConnectUdp->UdpHandle, + &gEfiUdp4ProtocolGuid, + gMtftp4DriverBinding.DriverBindingHandle, + ChildHandle + ); + + gBS->CloseProtocol ( + Instance->UnicastPort->UdpHandle, + &gEfiUdp4ProtocolGuid, + gMtftp4DriverBinding.DriverBindingHandle, + ChildHandle + ); + + if (Instance->McastUdpPort != NULL) { + gBS->CloseProtocol ( + Instance->McastUdpPort->UdpHandle, + &gEfiUdp4ProtocolGuid, + gMtftp4DriverBinding.DriverBindingHandle, + ChildHandle + ); + } + + // + // Uninstall the MTFTP4 protocol first to enable a top down destruction. + // + Status = gBS->UninstallProtocolInterface ( + ChildHandle, + &gEfiMtftp4ProtocolGuid, + Mtftp4 + ); + + if (EFI_ERROR (Status)) { + Instance->InDestroy = FALSE; + return Status; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Mtftp4CleanOperation (Instance, EFI_DEVICE_ERROR); + UdpIoFreeIo (Instance->UnicastPort); + + RemoveEntryList (&Instance->Link); + MtftpSb->ChildrenNum--; + + gBS->RestoreTPL (OldTpl); + + FreePool (Instance); + return EFI_SUCCESS; +} diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Driver.h b/NetworkPkg/Mtftp4Dxe/Mtftp4Driver.h new file mode 100644 index 000000000..c90e69018 --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Driver.h @@ -0,0 +1,131 @@ +/** @file + Mtftp drivers function header. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_MTFTP4_DRIVER_H__ +#define __EFI_MTFTP4_DRIVER_H__ + +#include + +#include + +#include +#include +#include + +extern EFI_COMPONENT_NAME_PROTOCOL gMtftp4ComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gMtftp4ComponentName2; +extern EFI_DRIVER_BINDING_PROTOCOL gMtftp4DriverBinding; +extern EFI_UNICODE_STRING_TABLE *gMtftp4ControllerNameTable; + +/** + Test whether MTFTP driver support this controller. + + @param This The MTFTP driver binding instance + @param Controller The controller to test + @param RemainingDevicePath The remaining device path + + @retval EFI_SUCCESS The controller has UDP service binding protocol + installed, MTFTP can support it. + @retval Others MTFTP can't support the controller. + +**/ +EFI_STATUS +EFIAPI +Mtftp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Start the MTFTP driver on this controller. + + MTFTP driver will install a MTFTP SERVICE BINDING protocol on the supported + controller, which can be used to create/destroy MTFTP children. + + @param This The MTFTP driver binding protocol. + @param Controller The controller to manage. + @param RemainingDevicePath Remaining device path. + + @retval EFI_ALREADY_STARTED The MTFTP service binding protocol has been + started on the controller. + @retval EFI_SUCCESS The MTFTP service binding is installed on the + controller. + +**/ +EFI_STATUS +EFIAPI +Mtftp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Stop the MTFTP driver on controller. The controller is a UDP + child handle. + + @param This The MTFTP driver binding protocol + @param Controller The controller to stop + @param NumberOfChildren The number of children + @param ChildHandleBuffer The array of the child handle. + + @retval EFI_SUCCESS The driver is stopped on the controller. + @retval EFI_DEVICE_ERROR Failed to stop the driver on the controller. + +**/ +EFI_STATUS +EFIAPI +Mtftp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +/** + Create a MTFTP child for the service binding instance, then + install the MTFTP protocol to the ChildHandle. + + @param This The MTFTP service binding instance. + @param ChildHandle The Child handle to install the MTFTP protocol. + + @retval EFI_INVALID_PARAMETER The parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the new child. + @retval EFI_SUCCESS The child is successfully create. + +**/ +EFI_STATUS +EFIAPI +Mtftp4ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ); + +/** + Destroy one of the service binding's child. + + @param This The service binding instance + @param ChildHandle The child handle to destroy + + @retval EFI_INVALID_PARAMETER The parameter is invaid. + @retval EFI_UNSUPPORTED The child may have already been destroyed. + @retval EFI_SUCCESS The child is destroyed and removed from the + parent's child list. + +**/ +EFI_STATUS +EFIAPI +Mtftp4ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + + + +#endif diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.inf b/NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.inf new file mode 100644 index 000000000..25cbdc185 --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.inf @@ -0,0 +1,70 @@ +## @file +# This module produces EFI MTFTPv4 Protocol and EFI MTFTPv4 Service Binding Protocol. +# +# This module produces EFI MTFTPv4 Protocol upon EFI UDPv4 Protocol, to provide +# basic services for client-side unicast and/or multicase TFTP operations. +# +# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = Mtftp4Dxe + MODULE_UNI_FILE = Mtftp4Dxe.uni + FILE_GUID = DC3641B8-2FA8-4ed3-BC1F-F9962A03454B + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = Mtftp4DriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = gMtftp4DriverBinding +# COMPONENT_NAME = gMtftp4ComponentName +# COMPONENT_NAME2 = gMtftp4ComponentName2 +# + +[Sources] + Mtftp4Option.c + Mtftp4Rrq.c + Mtftp4Impl.h + ComponentName.c + Mtftp4Support.c + Mtftp4Impl.c + Mtftp4Option.h + Mtftp4Support.h + Mtftp4Driver.h + Mtftp4Driver.c + Mtftp4Wrq.c + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + UefiLib + UefiBootServicesTableLib + UefiDriverEntryPoint + DebugLib + NetLib + UdpIoLib + MemoryAllocationLib + BaseMemoryLib + + +[Protocols] + gEfiMtftp4ServiceBindingProtocolGuid ## BY_START + gEfiUdp4ServiceBindingProtocolGuid ## TO_START + gEfiMtftp4ProtocolGuid ## BY_START + gEfiUdp4ProtocolGuid ## TO_START + +[UserExtensions.TianoCore."ExtraFiles"] + Mtftp4DxeExtra.uni diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.uni b/NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.uni new file mode 100644 index 000000000..1c2c80bcd --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.uni @@ -0,0 +1,17 @@ +// /** @file +// This module produces EFI MTFTPv4 Protocol and EFI MTFTPv4 Service Binding Protocol. +// +// This module produces EFI MTFTPv4 Protocol upon EFI UDPv4 Protocol, to provide +// basic services for client-side unicast and/or multicase TFTP operations. +// +// Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Produces EFI MTFTPv4 Protocol and EFI MTFTPv4 Service Binding Protocol" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces EFI MTFTPv4 Protocol upon EFI UDPv4 Protocol, to provide basic services for client-side unicast or multicase TFTP operations or both." + diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4DxeExtra.uni b/NetworkPkg/Mtftp4Dxe/Mtftp4DxeExtra.uni new file mode 100644 index 000000000..cd685c452 --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4DxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// Mtftp4Dxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"MTFTP v4 DXE Driver" + + diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Impl.c b/NetworkPkg/Mtftp4Dxe/Mtftp4Impl.c new file mode 100644 index 000000000..70bd693e4 --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Impl.c @@ -0,0 +1,1113 @@ +/** @file + Interface routine for Mtftp4. + +(C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Mtftp4Impl.h" + + +/** + Clean up the MTFTP session to get ready for new operation. + + @param Instance The MTFTP session to clean up + @param Result The result to return to the caller who initiated + the operation. +**/ +VOID +Mtftp4CleanOperation ( + IN OUT MTFTP4_PROTOCOL *Instance, + IN EFI_STATUS Result + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + MTFTP4_BLOCK_RANGE *Block; + EFI_MTFTP4_TOKEN *Token; + + // + // Free various resources. + // + Token = Instance->Token; + + if (Token != NULL) { + Token->Status = Result; + + if (Token->Event != NULL) { + gBS->SignalEvent (Token->Event); + } + + Instance->Token = NULL; + } + + ASSERT (Instance->UnicastPort != NULL); + UdpIoCleanIo (Instance->UnicastPort); + + if (Instance->LastPacket != NULL) { + NetbufFree (Instance->LastPacket); + Instance->LastPacket = NULL; + } + + if (Instance->McastUdpPort != NULL) { + gBS->CloseProtocol ( + Instance->McastUdpPort->UdpHandle, + &gEfiUdp4ProtocolGuid, + gMtftp4DriverBinding.DriverBindingHandle, + Instance->Handle + ); + UdpIoFreeIo (Instance->McastUdpPort); + Instance->McastUdpPort = NULL; + } + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Instance->Blocks) { + Block = NET_LIST_USER_STRUCT (Entry, MTFTP4_BLOCK_RANGE, Link); + RemoveEntryList (Entry); + FreePool (Block); + } + + ZeroMem (&Instance->RequestOption, sizeof (MTFTP4_OPTION)); + + Instance->Operation = 0; + + Instance->BlkSize = MTFTP4_DEFAULT_BLKSIZE; + Instance->WindowSize = 1; + Instance->TotalBlock = 0; + Instance->AckedBlock = 0; + Instance->LastBlock = 0; + Instance->ServerIp = 0; + Instance->ListeningPort = 0; + Instance->ConnectedPort = 0; + Instance->Gateway = 0; + Instance->PacketToLive = 0; + Instance->MaxRetry = 0; + Instance->CurRetry = 0; + Instance->Timeout = 0; + Instance->McastIp = 0; + Instance->McastPort = 0; + Instance->Master = TRUE; +} + + +/** + Check packet for GetInfo. + + GetInfo is implemented with EfiMtftp4ReadFile. It use Mtftp4GetInfoCheckPacket + to inspect the first packet from server, then abort the session. + + @param This The MTFTP4 protocol instance + @param Token The user's token + @param PacketLen The length of the packet + @param Packet The received packet. + + @retval EFI_ABORTED Abort the ReadFile operation and return. + +**/ +EFI_STATUS +EFIAPI +Mtftp4GetInfoCheckPacket ( + IN EFI_MTFTP4_PROTOCOL *This, + IN EFI_MTFTP4_TOKEN *Token, + IN UINT16 PacketLen, + IN EFI_MTFTP4_PACKET *Packet + ) +{ + MTFTP4_GETINFO_STATE *State; + EFI_STATUS Status; + UINT16 OpCode; + EFI_MTFTP4_ERROR_HEADER *ErrorHeader; + + State = (MTFTP4_GETINFO_STATE *) Token->Context; + OpCode = NTOHS (Packet->OpCode); + + // + // Set the GetInfo's return status according to the OpCode. + // + switch (OpCode) { + case EFI_MTFTP4_OPCODE_ERROR: + ErrorHeader = (EFI_MTFTP4_ERROR_HEADER *) Packet; + if (ErrorHeader->ErrorCode == EFI_MTFTP4_ERRORCODE_FILE_NOT_FOUND) { + DEBUG ((EFI_D_ERROR, "TFTP error code 1 (File Not Found)\n")); + } else { + DEBUG ((EFI_D_ERROR, "TFTP error code %d\n", ErrorHeader->ErrorCode)); + } + State->Status = EFI_TFTP_ERROR; + break; + + case EFI_MTFTP4_OPCODE_OACK: + State->Status = EFI_SUCCESS; + break; + + default: + State->Status = EFI_PROTOCOL_ERROR; + } + + // + // Allocate buffer then copy the packet over. Use gBS->AllocatePool + // in case AllocatePool will implements something tricky. + // + Status = gBS->AllocatePool (EfiBootServicesData, PacketLen, (VOID **) State->Packet); + + if (EFI_ERROR (Status)) { + State->Status = EFI_OUT_OF_RESOURCES; + return EFI_ABORTED; + } + + *(State->PacketLen) = PacketLen; + CopyMem (*(State->Packet), Packet, PacketLen); + + return EFI_ABORTED; +} + + +/** + Check whether the override data is valid. + + It will first validate whether the server is a valid unicast. If a gateway + is provided in the Override, it also check that it is a unicast on the + connected network. + + @param Instance The MTFTP instance + @param Override The override data to validate. + + @retval TRUE The override data is valid + @retval FALSE The override data is invalid + +**/ +BOOLEAN +Mtftp4OverrideValid ( + IN MTFTP4_PROTOCOL *Instance, + IN EFI_MTFTP4_OVERRIDE_DATA *Override + ) +{ + EFI_MTFTP4_CONFIG_DATA *Config; + IP4_ADDR Ip; + IP4_ADDR Netmask; + IP4_ADDR Gateway; + + CopyMem (&Ip, &Override->ServerIp, sizeof (IP4_ADDR)); + if (IP4_IS_UNSPECIFIED (NTOHL (Ip)) || IP4_IS_LOCAL_BROADCAST (NTOHL (Ip))) { + return FALSE; + } + + Config = &Instance->Config; + + CopyMem (&Gateway, &Override->GatewayIp, sizeof (IP4_ADDR)); + Gateway = NTOHL (Gateway); + + if (!Config->UseDefaultSetting && (Gateway != 0)) { + CopyMem (&Netmask, &Config->SubnetMask, sizeof (IP4_ADDR)); + CopyMem (&Ip, &Config->StationIp, sizeof (IP4_ADDR)); + + Netmask = NTOHL (Netmask); + Ip = NTOHL (Ip); + + if ((Netmask != 0 && !NetIp4IsUnicast (Gateway, Netmask)) || !IP4_NET_EQUAL (Gateway, Ip, Netmask)) { + return FALSE; + } + } + + return TRUE; +} + + +/** + Poll the UDP to get the IP4 default address, which may be retrieved + by DHCP. + + The default time out value is 5 seconds. If IP has retrieved the default address, + the UDP is reconfigured. + + @param Instance The Mtftp instance + @param UdpIo The UDP_IO to poll + @param UdpCfgData The UDP configure data to reconfigure the UDP_IO + + @retval TRUE The default address is retrieved and UDP is reconfigured. + @retval FALSE Some error occured. + +**/ +BOOLEAN +Mtftp4GetMapping ( + IN MTFTP4_PROTOCOL *Instance, + IN UDP_IO *UdpIo, + IN EFI_UDP4_CONFIG_DATA *UdpCfgData + ) +{ + MTFTP4_SERVICE *Service; + EFI_IP4_MODE_DATA Ip4Mode; + EFI_UDP4_PROTOCOL *Udp; + EFI_STATUS Status; + + ASSERT (Instance->Config.UseDefaultSetting); + + Service = Instance->Service; + Udp = UdpIo->Protocol.Udp4; + + Status = gBS->SetTimer ( + Service->TimerToGetMap, + TimerRelative, + MTFTP4_TIME_TO_GETMAP * TICKS_PER_SECOND + ); + if (EFI_ERROR (Status)) { + return FALSE; + } + + while (EFI_ERROR (gBS->CheckEvent (Service->TimerToGetMap))) { + Udp->Poll (Udp); + + if (!EFI_ERROR (Udp->GetModeData (Udp, NULL, &Ip4Mode, NULL, NULL)) && + Ip4Mode.IsConfigured) { + + Udp->Configure (Udp, NULL); + return (BOOLEAN) (Udp->Configure (Udp, UdpCfgData) == EFI_SUCCESS); + } + } + + return FALSE; +} + + +/** + Configure the UDP port for unicast receiving. + + @param UdpIo The UDP_IO instance + @param Instance The MTFTP session + + @retval EFI_SUCCESS The UDP port is successfully configured for the + session to unicast receive. + +**/ +EFI_STATUS +Mtftp4ConfigUnicastPort ( + IN UDP_IO *UdpIo, + IN MTFTP4_PROTOCOL *Instance + ) +{ + EFI_MTFTP4_CONFIG_DATA *Config; + EFI_UDP4_CONFIG_DATA UdpConfig; + EFI_STATUS Status; + IP4_ADDR Ip; + + Config = &Instance->Config; + + UdpConfig.AcceptBroadcast = FALSE; + UdpConfig.AcceptPromiscuous = FALSE; + UdpConfig.AcceptAnyPort = FALSE; + UdpConfig.AllowDuplicatePort = FALSE; + UdpConfig.TypeOfService = 0; + UdpConfig.TimeToLive = 64; + UdpConfig.DoNotFragment = FALSE; + UdpConfig.ReceiveTimeout = 0; + UdpConfig.TransmitTimeout = 0; + UdpConfig.UseDefaultAddress = Config->UseDefaultSetting; + IP4_COPY_ADDRESS (&UdpConfig.StationAddress, &Config->StationIp); + IP4_COPY_ADDRESS (&UdpConfig.SubnetMask, &Config->SubnetMask); + UdpConfig.StationPort = Config->LocalPort; + UdpConfig.RemotePort = 0; + + Ip = HTONL (Instance->ServerIp); + IP4_COPY_ADDRESS (&UdpConfig.RemoteAddress, &Ip); + + Status = UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfig); + + if ((Status == EFI_NO_MAPPING) && Mtftp4GetMapping (Instance, UdpIo, &UdpConfig)) { + return EFI_SUCCESS; + } + + if (!Config->UseDefaultSetting && !EFI_IP4_EQUAL (&mZeroIp4Addr, &Config->GatewayIp)) { + // + // The station IP address is manually configured and the Gateway IP is not 0. + // Add the default route for this UDP instance. + // + Status = UdpIo->Protocol.Udp4->Routes ( + UdpIo->Protocol.Udp4, + FALSE, + &mZeroIp4Addr, + &mZeroIp4Addr, + &Config->GatewayIp + ); + if (EFI_ERROR (Status)) { + UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, NULL); + } + } + return Status; +} + + +/** + Start the MTFTP session to do the operation, such as read file, + write file, and read directory. + + @param This The MTFTP session + @param Token The token than encapsues the user's request. + @param Operation The operation to do + + @retval EFI_INVALID_PARAMETER Some of the parameters are invalid. + @retval EFI_NOT_STARTED The MTFTP session hasn't been configured. + @retval EFI_ALREADY_STARTED There is pending operation for the session. + @retval EFI_SUCCESS The operation is successfully started. + +**/ +EFI_STATUS +Mtftp4Start ( + IN EFI_MTFTP4_PROTOCOL *This, + IN EFI_MTFTP4_TOKEN *Token, + IN UINT16 Operation + ) +{ + MTFTP4_PROTOCOL *Instance; + EFI_MTFTP4_OVERRIDE_DATA *Override; + EFI_MTFTP4_CONFIG_DATA *Config; + EFI_TPL OldTpl; + EFI_STATUS Status; + EFI_STATUS TokenStatus; + + // + // Validate the parameters + // + if ((This == NULL) || (Token == NULL) || (Token->Filename == NULL) || + ((Token->OptionCount != 0) && (Token->OptionList == NULL))) { + return EFI_INVALID_PARAMETER; + } + + // + // User must provide at least one method to collect the data for download. + // + if (((Operation == EFI_MTFTP4_OPCODE_RRQ) || (Operation == EFI_MTFTP4_OPCODE_DIR)) && + ((Token->Buffer == NULL) && (Token->CheckPacket == NULL))) { + return EFI_INVALID_PARAMETER; + } + + // + // User must provide at least one method to provide the data for upload. + // + if ((Operation == EFI_MTFTP4_OPCODE_WRQ) && + ((Token->Buffer == NULL) && (Token->PacketNeeded == NULL))) { + return EFI_INVALID_PARAMETER; + } + + Instance = MTFTP4_PROTOCOL_FROM_THIS (This); + + Status = EFI_SUCCESS; + TokenStatus = EFI_SUCCESS; + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (Instance->State != MTFTP4_STATE_CONFIGED) { + Status = EFI_NOT_STARTED; + } + + if (Instance->Operation != 0) { + Status = EFI_ACCESS_DENIED; + } + + if ((Token->OverrideData != NULL) && !Mtftp4OverrideValid (Instance, Token->OverrideData)) { + Status = EFI_INVALID_PARAMETER; + } + + if (EFI_ERROR (Status)) { + gBS->RestoreTPL (OldTpl); + return Status; + } + + // + // Set the Operation now to prevent the application start other + // operations. + // + Instance->Operation = Operation; + Override = Token->OverrideData; + + if (Token->OptionCount != 0) { + Status = Mtftp4ParseOption ( + Token->OptionList, + Token->OptionCount, + TRUE, + Instance->Operation, + &Instance->RequestOption + ); + + if (EFI_ERROR (Status)) { + TokenStatus = EFI_DEVICE_ERROR; + goto ON_ERROR; + } + } + + // + // Set the operation parameters from the configuration or override data. + // + Config = &Instance->Config; + Instance->Token = Token; + Instance->BlkSize = MTFTP4_DEFAULT_BLKSIZE; + Instance->WindowSize = MTFTP4_DEFAULT_WINDOWSIZE; + + CopyMem (&Instance->ServerIp, &Config->ServerIp, sizeof (IP4_ADDR)); + Instance->ServerIp = NTOHL (Instance->ServerIp); + + Instance->ListeningPort = Config->InitialServerPort; + Instance->ConnectedPort = 0; + + CopyMem (&Instance->Gateway, &Config->GatewayIp, sizeof (IP4_ADDR)); + Instance->Gateway = NTOHL (Instance->Gateway); + + Instance->MaxRetry = Config->TryCount; + Instance->Timeout = Config->TimeoutValue; + Instance->Master = TRUE; + + if (Override != NULL) { + CopyMem (&Instance->ServerIp, &Override->ServerIp, sizeof (IP4_ADDR)); + CopyMem (&Instance->Gateway, &Override->GatewayIp, sizeof (IP4_ADDR)); + + Instance->ServerIp = NTOHL (Instance->ServerIp); + Instance->Gateway = NTOHL (Instance->Gateway); + + Instance->ListeningPort = Override->ServerPort; + Instance->MaxRetry = Override->TryCount; + Instance->Timeout = Override->TimeoutValue; + } + + if (Instance->ListeningPort == 0) { + Instance->ListeningPort = MTFTP4_DEFAULT_SERVER_PORT; + } + + if (Instance->MaxRetry == 0) { + Instance->MaxRetry = MTFTP4_DEFAULT_RETRY; + } + + if (Instance->Timeout == 0) { + Instance->Timeout = MTFTP4_DEFAULT_TIMEOUT; + } + + // + // Config the unicast UDP child to send initial request + // + Status = Mtftp4ConfigUnicastPort (Instance->UnicastPort, Instance); + if (EFI_ERROR (Status)) { + TokenStatus = EFI_DEVICE_ERROR; + goto ON_ERROR; + } + + // + // Set initial status. + // + Token->Status = EFI_NOT_READY; + + // + // Build and send an initial requests + // + if (Operation == EFI_MTFTP4_OPCODE_WRQ) { + Status = Mtftp4WrqStart (Instance, Operation); + } else { + Status = Mtftp4RrqStart (Instance, Operation); + } + + if (EFI_ERROR (Status)) { + TokenStatus = EFI_DEVICE_ERROR; + goto ON_ERROR; + } + + gBS->RestoreTPL(OldTpl); + + if (Token->Event != NULL) { + return EFI_SUCCESS; + } + + // + // Return immediately for asynchronous operation or poll the + // instance for synchronous operation. + // + while (Token->Status == EFI_NOT_READY) { + This->Poll (This); + } + + return Token->Status; + +ON_ERROR: + Mtftp4CleanOperation (Instance, TokenStatus); + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Reads the current operational settings. + + The GetModeData()function reads the current operational settings of this + EFI MTFTPv4 Protocol driver instance. + + @param This Pointer to the EFI_MTFTP4_PROTOCOL instance. + @param ModeData Pointer to storage for the EFI MTFTPv4 Protocol + driver mode data. + + @retval EFI_SUCCESS The configuration data was successfully returned. + @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated. + @retval EFI_INVALID_PARAMETER This is NULL or ModeData is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp4GetModeData ( + IN EFI_MTFTP4_PROTOCOL *This, + OUT EFI_MTFTP4_MODE_DATA *ModeData + ) +{ + MTFTP4_PROTOCOL *Instance; + EFI_TPL OldTpl; + + if ((This == NULL) || (ModeData == NULL)) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = MTFTP4_PROTOCOL_FROM_THIS (This); + CopyMem(&ModeData->ConfigData, &Instance->Config, sizeof (Instance->Config)); + ModeData->SupportedOptionCount = MTFTP4_SUPPORTED_OPTIONS; + ModeData->SupportedOptoins = (UINT8 **) mMtftp4SupportedOptions; + ModeData->UnsupportedOptionCount = 0; + ModeData->UnsupportedOptoins = NULL; + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; +} + + + +/** + Initializes, changes, or resets the default operational setting for this + EFI MTFTPv4 Protocol driver instance. + + The Configure() function is used to set and change the configuration data for + this EFI MTFTPv4 Protocol driver instance. The configuration data can be reset + to startup defaults by calling Configure() with MtftpConfigData set to NULL. + Whenever the instance is reset, any pending operation is aborted. By changing + the EFI MTFTPv4 Protocol driver instance configuration data, the client can + connect to different MTFTPv4 servers. The configuration parameters in + MtftpConfigData are used as the default parameters in later MTFTPv4 operations + and can be overridden in later operations. + + @param This Pointer to the EFI_MTFTP4_PROTOCOL instance + @param ConfigData MtftpConfigDataPointer to the configuration data + structure + + @retval EFI_SUCCESS The EFI MTFTPv4 Protocol driver was configured + successfully. + @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE: + 1.This is NULL. + 2.MtftpConfigData.UseDefaultSetting is FALSE and + MtftpConfigData.StationIp is not a valid IPv4 + unicast address. + 3.MtftpCofigData.UseDefaultSetting is FALSE and + MtftpConfigData.SubnetMask is invalid. + 4.MtftpCofigData.ServerIp is not a valid IPv4 + unicast address. + 5.MtftpConfigData.UseDefaultSetting is FALSE and + MtftpConfigData.GatewayIp is not a valid IPv4 + unicast address or is not in the same subnet + with station address. + @retval EFI_ACCESS_DENIED The EFI configuration could not be changed at this + time because there is one MTFTP background operation + in progress. + @retval EFI_NO_MAPPING When using a default address, configuration + (DHCP, BOOTP, RARP, etc.) has not finished yet. + @retval EFI_UNSUPPORTED A configuration protocol (DHCP, BOOTP, RARP, etc.) + could not be located when clients choose to use + the default address settings. + @retval EFI_OUT_OF_RESOURCES The EFI MTFTPv4 Protocol driver instance data could + not be allocated. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The EFI MTFTPv4 Protocol driver instance is not + configured. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp4Configure ( + IN EFI_MTFTP4_PROTOCOL *This, + IN EFI_MTFTP4_CONFIG_DATA *ConfigData + ) +{ + MTFTP4_PROTOCOL *Instance; + EFI_TPL OldTpl; + IP4_ADDR Ip; + IP4_ADDR Netmask; + IP4_ADDR Gateway; + IP4_ADDR ServerIp; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MTFTP4_PROTOCOL_FROM_THIS (This); + + if (ConfigData == NULL) { + // + // Reset the operation if ConfigData is NULL + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Mtftp4CleanOperation (Instance, EFI_ABORTED); + ZeroMem (&Instance->Config, sizeof (EFI_MTFTP4_CONFIG_DATA)); + Instance->State = MTFTP4_STATE_UNCONFIGED; + + gBS->RestoreTPL (OldTpl); + + } else { + // + // Configure the parameters for new operation. + // + CopyMem (&Ip, &ConfigData->StationIp, sizeof (IP4_ADDR)); + CopyMem (&Netmask, &ConfigData->SubnetMask, sizeof (IP4_ADDR)); + CopyMem (&Gateway, &ConfigData->GatewayIp, sizeof (IP4_ADDR)); + CopyMem (&ServerIp, &ConfigData->ServerIp, sizeof (IP4_ADDR)); + + Ip = NTOHL (Ip); + Netmask = NTOHL (Netmask); + Gateway = NTOHL (Gateway); + ServerIp = NTOHL (ServerIp); + + if (ServerIp == 0 || IP4_IS_LOCAL_BROADCAST (ServerIp)) { + return EFI_INVALID_PARAMETER; + } + + if (!ConfigData->UseDefaultSetting && + ((!IP4_IS_VALID_NETMASK (Netmask) || (Netmask != 0 && !NetIp4IsUnicast (Ip, Netmask))))) { + + return EFI_INVALID_PARAMETER; + } + + if ((Gateway != 0) && + ((Netmask != 0xFFFFFFFF && !IP4_NET_EQUAL (Gateway, Ip, Netmask)) || (Netmask != 0 && !NetIp4IsUnicast (Gateway, Netmask)))) { + + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if ((Instance->State == MTFTP4_STATE_CONFIGED) && (Instance->Operation != 0)) { + gBS->RestoreTPL (OldTpl); + return EFI_ACCESS_DENIED; + } + + CopyMem(&Instance->Config, ConfigData, sizeof (*ConfigData));; + Instance->State = MTFTP4_STATE_CONFIGED; + + gBS->RestoreTPL (OldTpl); + } + + return EFI_SUCCESS; +} + + + +/** + Parses the options in an MTFTPv4 OACK packet. + + The ParseOptions() function parses the option fields in an MTFTPv4 OACK packet + and returns the number of options that were found and optionally a list of + pointers to the options in the packet. + If one or more of the option fields are not valid, then EFI_PROTOCOL_ERROR is + returned and *OptionCount and *OptionList stop at the last valid option. + The OptionList is allocated by this function, and caller should free it when used. + + @param This Pointer to the EFI_MTFTP4_PROTOCOL instance. + @param PacketLen Length of the OACK packet to be parsed. + @param Packet Pointer to the OACK packet to be parsed. + @param OptionCount Pointer to the number of options in following OptionList. + @param OptionList Pointer to EFI_MTFTP4_OPTION storage. Call the + EFI Boot Service FreePool() to release theOptionList + if the options in this OptionList are not needed + any more + + @retval EFI_SUCCESS The OACK packet was valid and the OptionCount and + OptionList parameters have been updated. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + 1.PacketLen is 0. + 2.Packet is NULL or Packet is not a valid MTFTPv4 packet. + 3.OptionCount is NULL. + @retval EFI_NOT_FOUND No options were found in the OACK packet. + @retval EFI_OUT_OF_RESOURCES Storage for the OptionList array cannot be allocated. + @retval EFI_PROTOCOL_ERROR One or more of the option fields is invalid. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp4ParseOptions ( + IN EFI_MTFTP4_PROTOCOL *This, + IN UINT32 PacketLen, + IN EFI_MTFTP4_PACKET *Packet, + OUT UINT32 *OptionCount, + OUT EFI_MTFTP4_OPTION **OptionList OPTIONAL + ) +{ + EFI_STATUS Status; + + if ((This == NULL) || (PacketLen < MTFTP4_OPCODE_LEN) || + (Packet == NULL) || (OptionCount == NULL)) { + + return EFI_INVALID_PARAMETER; + } + + Status = Mtftp4ExtractOptions (Packet, PacketLen, OptionCount, OptionList); + + if (EFI_ERROR (Status)) { + return Status; + } + + if (*OptionCount == 0) { + return EFI_NOT_FOUND; + } + + return EFI_SUCCESS; +} + + +/** + Downloads a file from an MTFTPv4 server. + + The ReadFile() function is used to initialize and start an MTFTPv4 download + process and optionally wait for completion. When the download operation completes, + whether successfully or not, the Token.Status field is updated by the EFI MTFTPv4 + Protocol driver and then Token.Event is signaled (if it is not NULL). + Data can be downloaded from the MTFTPv4 server into either of the following locations: + 1.A fixed buffer that is pointed to by Token.Buffer + 2.A download service function that is pointed to by Token.CheckPacket + If both Token.Buffer and Token.CheckPacket are used, then Token.CheckPacket + will be called first. If the call is successful, the packet will be stored in + Token.Buffer. + + @param This Pointer to the EFI_MTFTP4_PROTOCOL instance + @param Token Pointer to the token structure to provide the + parameters that are used in this operation. + + @retval EFI_SUCCESS The data file has been transferred successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_BUFFER_TOO_SMALL BufferSize is not large enough to hold the downloaded + data in downloading process. + @retval EFI_ABORTED Current operation is aborted by user. + @retval EFI_ICMP_ERROR An ICMP ERROR packet was received. + @retval EFI_TIMEOUT No responses were received from the MTFTPv4 server. + @retval EFI_TFTP_ERROR An MTFTPv4 ERROR packet was received. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + @retval EFI_NO_MEDIA There was a media error. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp4ReadFile ( + IN EFI_MTFTP4_PROTOCOL *This, + IN EFI_MTFTP4_TOKEN *Token + ) +{ + return Mtftp4Start (This, Token, EFI_MTFTP4_OPCODE_RRQ); +} + + +/** + Sends a data file to an MTFTPv4 server. May be unsupported in some EFI implementations + + The WriteFile() function is used to initialize an uploading operation with the + given option list and optionally wait for completion. If one or more of the + options is not supported by the server, the unsupported options are ignored and + a standard TFTP process starts instead. When the upload process completes, + whether successfully or not, Token.Event is signaled, and the EFI MTFTPv4 Protocol + driver updates Token.Status. + The caller can supply the data to be uploaded in the following two modes: + 1.Through the user-provided buffer + 2.Through a callback function + With the user-provided buffer, the Token.BufferSize field indicates the length + of the buffer, and the driver will upload the data in the buffer. With an + EFI_MTFTP4_PACKET_NEEDED callback function, the driver will call this callback + function to get more data from the user to upload. See the definition of + EFI_MTFTP4_PACKET_NEEDED for more information. These two modes cannot be used at + the same time. The callback function will be ignored if the user provides the buffer. + + @param This Pointer to the EFI_MTFTP4_PROTOCOL instance. + @param Token Pointer to the token structure to provide the + parameters that are used in this function + + @retval EFI_SUCCESS The upload session has started. + @retval EFI_UNSUPPORTED The operation is not supported by this implementation. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + 1. This is NULL. + 2. Token is NULL. + 3. Token.Filename is NULL. + 4. Token.OptionCount is not zero and + Token.OptionList is NULL. + 5. One or more options in Token.OptionList have wrong + format. + 6. Token.Buffer and Token.PacketNeeded are both + NULL. + 7. One or more IPv4 addresses in Token.OverrideData + are not valid unicast IPv4 addresses if + Token.OverrideData is not NULL. + @retval EFI_UNSUPPORTED One or more options in the Token.OptionList are in the + unsupported list of structure EFI_MTFTP4_MODE_DATA. + @retval EFI_NOT_STARTED The EFI MTFTPv4 Protocol driver has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, + BOOTP, RARP, etc.) is not finished yet. + @retval EFI_ALREADY_STARTED This Token is already being used in another MTFTPv4 + session. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_ACCESS_DENIED The previous operation has not completed yet. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp4WriteFile ( + IN EFI_MTFTP4_PROTOCOL *This, + IN EFI_MTFTP4_TOKEN *Token + ) +{ + return Mtftp4Start (This, Token, EFI_MTFTP4_OPCODE_WRQ); +} + + +/** + Downloads a data file "directory" from an MTFTPv4 server. + May be unsupported in some EFI implementations + + The ReadDirectory() function is used to return a list of files on the MTFTPv4 + server that are logically (or operationally) related to Token.Filename. The + directory request packet that is sent to the server is built with the option + list that was provided by caller, if present. + The file information that the server returns is put into either of the following + locations: + 1.A fixed buffer that is pointed to by Token.Buffer + 2.A download service function that is pointed to by Token.CheckPacket + If both Token.Buffer and Token.CheckPacket are used, then Token.CheckPacket will + be called first. If the call is successful, the packet will be stored in Token.Buffer. + The returned directory listing in the Token.Buffer or EFI_MTFTP4_PACKET consists + of a list of two or three variable-length ASCII strings, each terminated by a + null character, for each file in the directory. If the multicast option is involved, + the first field of each directory entry is the static multicast IP address and + UDP port number that is associated with the file name. The format of the field + is ip:ip:ip:ip:port. If the multicast option is not involved, this field and its + terminating null character are not present. + The next field of each directory entry is the file name and the last field is + the file information string. The information string contains the file size and + the create/modify timestamp. The format of the information string is filesize + yyyy-mm-dd hh:mm:ss:ffff. The timestamp is Coordinated Universal Time + (UTC; also known as Greenwich Mean Time [GMT]). + The only difference between ReadFile and ReadDirectory is the opcode used. + + @param This Pointer to the EFI_MTFTP4_PROTOCOL instance + @param Token Pointer to the token structure to provide the + parameters that are used in this function + + @retval EFI_SUCCESS The MTFTPv4 related file "directory" has been downloaded. + @retval EFI_UNSUPPORTED The operation is not supported by this implementation. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + 1. This is NULL. + 2. Token is NULL. + 3. Token.Filename is NULL. + 4. Token.OptionCount is not zero and + Token.OptionList is NULL. + 5. One or more options in Token.OptionList have wrong + format. + 6. Token.Buffer and Token.PacketNeeded are both + NULL. + 7. One or more IPv4 addresses in Token.OverrideData + are not valid unicast IPv4 addresses if + Token.OverrideData is not NULL. + @retval EFI_UNSUPPORTED One or more options in the Token.OptionList are in the + unsupported list of structure EFI_MTFTP4_MODE_DATA. + @retval EFI_NOT_STARTED The EFI MTFTPv4 Protocol driver has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, + BOOTP, RARP, etc.) is not finished yet. + @retval EFI_ALREADY_STARTED This Token is already being used in another MTFTPv4 + session. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_ACCESS_DENIED The previous operation has not completed yet. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp4ReadDirectory ( + IN EFI_MTFTP4_PROTOCOL *This, + IN EFI_MTFTP4_TOKEN *Token + ) +{ + return Mtftp4Start (This, Token, EFI_MTFTP4_OPCODE_DIR); +} + + +/** + Gets information about a file from an MTFTPv4 server. + + The GetInfo() function assembles an MTFTPv4 request packet with options; + sends it to the MTFTPv4 server; and may return an MTFTPv4 OACK, MTFTPv4 ERROR, + or ICMP ERROR packet. Retries occur only if no response packets are received + from the MTFTPv4 server before the timeout expires. + It is implemented with EfiMtftp4ReadFile: build a token, then pass it to + EfiMtftp4ReadFile. In its check packet callback abort the opertions. + + @param This Pointer to the EFI_MTFTP4_PROTOCOL instance + @param OverrideData Data that is used to override the existing + parameters. If NULL, the default parameters that + were set in the EFI_MTFTP4_PROTOCOL.Configure() + function are used + @param Filename Pointer to null-terminated ASCII file name string + @param ModeStr Pointer to null-terminated ASCII mode string. If NULL, "octet" + will be used + @param OptionCount Number of option/value string pairs in OptionList + @param OptionList Pointer to array of option/value string pairs. + Ignored if OptionCount is zero + @param PacketLength The number of bytes in the returned packet + @param Packet PacketThe pointer to the received packet. This + buffer must be freed by the caller. + + @retval EFI_SUCCESS An MTFTPv4 OACK packet was received and is in + the Buffer. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + 1.This is NULL. + 2.Filename is NULL. + 3.OptionCount is not zero and OptionList is NULL. + 4.One or more options in OptionList have wrong format. + 5.PacketLength is NULL. + 6.One or more IPv4 addresses in OverrideData are + not valid unicast IPv4 addresses if OverrideData + is not NULL. + @retval EFI_UNSUPPORTED One or more options in the OptionList are in the + unsupported list of structure EFI_MTFTP4_MODE_DATA + @retval EFI_NOT_STARTED The EFI MTFTPv4 Protocol driver has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, + BOOTP, RARP, etc.) has not finished yet. + @retval EFI_ACCESS_DENIED The previous operation has not completed yet. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_TFTP_ERROR An MTFTPv4 ERROR packet was received and is in + the Buffer. + @retval EFI_ICMP_ERROR An ICMP ERROR packet was received and the Packet + is set to NULL. + @retval EFI_PROTOCOL_ERROR An unexpected MTFTPv4 packet was received and is + in the Buffer. + @retval EFI_TIMEOUT No responses were received from the MTFTPv4 server. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + @retval EFI_NO_MEDIA There was a media error. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp4GetInfo ( + IN EFI_MTFTP4_PROTOCOL *This, + IN EFI_MTFTP4_OVERRIDE_DATA *OverrideData OPTIONAL, + IN UINT8 *Filename, + IN UINT8 *ModeStr OPTIONAL, + IN UINT8 OptionCount, + IN EFI_MTFTP4_OPTION *OptionList OPTIONAL, + OUT UINT32 *PacketLength, + OUT EFI_MTFTP4_PACKET **Packet OPTIONAL + ) +{ + EFI_MTFTP4_TOKEN Token; + MTFTP4_GETINFO_STATE State; + EFI_STATUS Status; + + if ((This == NULL) || (Filename == NULL) || (PacketLength == NULL) || + ((OptionCount != 0) && (OptionList == NULL))) { + return EFI_INVALID_PARAMETER; + } + + if (Packet != NULL) { + *Packet = NULL; + } + + *PacketLength = 0; + State.Packet = Packet; + State.PacketLen = PacketLength; + State.Status = EFI_SUCCESS; + + // + // Fill in the Token to issue an synchronous ReadFile operation + // + Token.Status = EFI_SUCCESS; + Token.Event = NULL; + Token.OverrideData = OverrideData; + Token.Filename = Filename; + Token.ModeStr = ModeStr; + Token.OptionCount = OptionCount; + Token.OptionList = OptionList; + Token.BufferSize = 0; + Token.Buffer = NULL; + Token.Context = &State; + Token.CheckPacket = Mtftp4GetInfoCheckPacket; + Token.TimeoutCallback = NULL; + Token.PacketNeeded = NULL; + + Status = EfiMtftp4ReadFile (This, &Token); + + if (EFI_ABORTED == Status) { + return State.Status; + } + + return Status; +} + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications to increase + the rate that data packets are moved between the communications device and the + transmit and receive queues. + In some systems, the periodic timer event in the managed network driver may not + poll the underlying communications device fast enough to transmit and/or receive + all data packets without missing incoming packets or dropping outgoing packets. + Drivers and applications that are experiencing packet loss should try calling + the Poll() function more often. + + @param This Pointer to the EFI_MTFTP4_PROTOCOL instance + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI MTFTPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, + BOOTP, RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive + queue. Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp4Poll ( + IN EFI_MTFTP4_PROTOCOL *This + ) +{ + MTFTP4_PROTOCOL *Instance; + EFI_UDP4_PROTOCOL *Udp; + EFI_STATUS Status; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MTFTP4_PROTOCOL_FROM_THIS (This); + + if (Instance->State == MTFTP4_STATE_UNCONFIGED) { + return EFI_NOT_STARTED; + } else if (Instance->State == MTFTP4_STATE_DESTROY) { + return EFI_DEVICE_ERROR; + } + + Udp = Instance->UnicastPort->Protocol.Udp4; + Status = Udp->Poll (Udp); + Mtftp4OnTimerTick (NULL, Instance->Service); + return Status; +} + +EFI_MTFTP4_PROTOCOL gMtftp4ProtocolTemplate = { + EfiMtftp4GetModeData, + EfiMtftp4Configure, + EfiMtftp4GetInfo, + EfiMtftp4ParseOptions, + EfiMtftp4ReadFile, + EfiMtftp4WriteFile, + EfiMtftp4ReadDirectory, + EfiMtftp4Poll +}; diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Impl.h b/NetworkPkg/Mtftp4Dxe/Mtftp4Impl.h new file mode 100644 index 000000000..33393d424 --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Impl.h @@ -0,0 +1,226 @@ +/** @file + + Mtftp4 Implementation. + + Mtftp4 Implementation, it supports the following RFCs: + RFC1350 - THE TFTP PROTOCOL (REVISION 2) + RFC2090 - TFTP Multicast Option + RFC2347 - TFTP Option Extension + RFC2348 - TFTP Blocksize Option + RFC2349 - TFTP Timeout Interval and Transfer Size Options + RFC7440 - TFTP Windowsize Option + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#ifndef __EFI_MTFTP4_IMPL_H__ +#define __EFI_MTFTP4_IMPL_H__ + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +extern EFI_MTFTP4_PROTOCOL gMtftp4ProtocolTemplate; + +typedef struct _MTFTP4_SERVICE MTFTP4_SERVICE; +typedef struct _MTFTP4_PROTOCOL MTFTP4_PROTOCOL; + +#include "Mtftp4Driver.h" +#include "Mtftp4Option.h" +#include "Mtftp4Support.h" + + +/// +/// Some constant value of Mtftp service. +/// +#define MTFTP4_SERVICE_SIGNATURE SIGNATURE_32 ('T', 'F', 'T', 'P') +#define MTFTP4_PROTOCOL_SIGNATURE SIGNATURE_32 ('t', 'f', 't', 'p') + +#define MTFTP4_DEFAULT_SERVER_PORT 69 +#define MTFTP4_DEFAULT_TIMEOUT 3 +#define MTFTP4_DEFAULT_RETRY 5 +#define MTFTP4_DEFAULT_BLKSIZE 512 +#define MTFTP4_DEFAULT_WINDOWSIZE 1 +#define MTFTP4_TIME_TO_GETMAP 5 + +#define MTFTP4_STATE_UNCONFIGED 0 +#define MTFTP4_STATE_CONFIGED 1 +#define MTFTP4_STATE_DESTROY 2 + +/// +/// Mtftp service block +/// +struct _MTFTP4_SERVICE { + UINT32 Signature; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + + UINT16 ChildrenNum; + LIST_ENTRY Children; + + EFI_EVENT Timer; ///< Ticking timer for all the MTFTP clients to handle the packet timeout case. + EFI_EVENT TimerNotifyLevel; ///< Ticking timer for all the MTFTP clients to calculate the packet live time. + EFI_EVENT TimerToGetMap; + + EFI_HANDLE Controller; + EFI_HANDLE Image; + + // + // This UDP child is used to keep the connection between the UDP + // and MTFTP, so MTFTP will be notified when UDP is uninstalled. + // + UDP_IO *ConnectUdp; +}; + + +typedef struct { + EFI_MTFTP4_PACKET **Packet; + UINT32 *PacketLen; + EFI_STATUS Status; +} MTFTP4_GETINFO_STATE; + +struct _MTFTP4_PROTOCOL { + UINT32 Signature; + LIST_ENTRY Link; + EFI_MTFTP4_PROTOCOL Mtftp4; + + INTN State; + BOOLEAN InDestroy; + + MTFTP4_SERVICE *Service; + EFI_HANDLE Handle; + + EFI_MTFTP4_CONFIG_DATA Config; + + // + // Operation parameters: token and requested options. + // + EFI_MTFTP4_TOKEN *Token; + MTFTP4_OPTION RequestOption; + UINT16 Operation; + + // + // Blocks is a list of MTFTP4_BLOCK_RANGE which contains + // holes in the file + // + UINT16 BlkSize; + UINT16 LastBlock; + LIST_ENTRY Blocks; + + UINT16 WindowSize; + + // + // Record the total received and saved block number. + // + UINT64 TotalBlock; + + // + // Record the acked block number. + // + UINT64 AckedBlock; + + // + // The server's communication end point: IP and two ports. one for + // initial request, one for its selected port. + // + IP4_ADDR ServerIp; + UINT16 ListeningPort; + UINT16 ConnectedPort; + IP4_ADDR Gateway; + UDP_IO *UnicastPort; + + // + // Timeout and retransmit status + // + NET_BUF *LastPacket; + UINT32 PacketToLive; + BOOLEAN HasTimeout; + UINT32 CurRetry; + UINT32 MaxRetry; + UINT32 Timeout; + + // + // Parameter used by RRQ's multicast download. + // + IP4_ADDR McastIp; + UINT16 McastPort; + BOOLEAN Master; + UDP_IO *McastUdpPort; +}; + +typedef struct { + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; +} MTFTP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT; + +/** + Clean up the MTFTP session to get ready for new operation. + + @param Instance The MTFTP session to clean up + @param Result The result to return to the caller who initiated + the operation. +**/ +VOID +Mtftp4CleanOperation ( + IN OUT MTFTP4_PROTOCOL *Instance, + IN EFI_STATUS Result + ); + +/** + Start the MTFTP session for upload. + + It will first init some states, then send the WRQ request packet, + and start receiving the packet. + + @param Instance The MTFTP session + @param Operation Redundant parameter, which is always + EFI_MTFTP4_OPCODE_WRQ here. + + @retval EFI_SUCCESS The upload process has been started. + @retval Others Failed to start the upload. + +**/ +EFI_STATUS +Mtftp4WrqStart ( + IN MTFTP4_PROTOCOL *Instance, + IN UINT16 Operation + ); + +/** + Start the MTFTP session to download. + + It will first initialize some of the internal states then build and send a RRQ + reqeuest packet, at last, it will start receive for the downloading. + + @param Instance The Mtftp session + @param Operation The MTFTP opcode, it may be a EFI_MTFTP4_OPCODE_RRQ + or EFI_MTFTP4_OPCODE_DIR. + + @retval EFI_SUCCESS The mtftp download session is started. + @retval Others Failed to start downloading. + +**/ +EFI_STATUS +Mtftp4RrqStart ( + IN MTFTP4_PROTOCOL *Instance, + IN UINT16 Operation + ); + +#define MTFTP4_SERVICE_FROM_THIS(a) \ + CR (a, MTFTP4_SERVICE, ServiceBinding, MTFTP4_SERVICE_SIGNATURE) + +#define MTFTP4_PROTOCOL_FROM_THIS(a) \ + CR (a, MTFTP4_PROTOCOL, Mtftp4, MTFTP4_PROTOCOL_SIGNATURE) + +#endif diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Option.c b/NetworkPkg/Mtftp4Dxe/Mtftp4Option.c new file mode 100644 index 000000000..d97f157f1 --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Option.c @@ -0,0 +1,549 @@ +/** @file + Routines to process MTFTP4 options. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp4Impl.h" + +CHAR8 *mMtftp4SupportedOptions[MTFTP4_SUPPORTED_OPTIONS] = { + "blksize", + "windowsize", + "timeout", + "tsize", + "multicast" +}; + + +/** + Check whether two ascii strings are equel, ignore the case. + + @param Str1 The first ascii string + @param Str2 The second ascii string + + @retval TRUE Two strings are equal when case is ignored. + @retval FALSE Two string are not equal. + +**/ +BOOLEAN +NetStringEqualNoCase ( + IN UINT8 *Str1, + IN UINT8 *Str2 + ) +{ + UINT8 Ch1; + UINT8 Ch2; + + ASSERT ((Str1 != NULL) && (Str2 != NULL)); + + for (; (*Str1 != '\0') && (*Str2 != '\0'); Str1++, Str2++) { + Ch1 = *Str1; + Ch2 = *Str2; + + // + // Convert them to lower case then compare two + // + if (('A' <= Ch1) && (Ch1 <= 'Z')) { + Ch1 += 'a' - 'A'; + } + + if (('A' <= Ch2) && (Ch2 <= 'Z')) { + Ch2 += 'a' - 'A'; + } + + if (Ch1 != Ch2) { + return FALSE; + } + } + + return (BOOLEAN) (*Str1 == *Str2); +} + + +/** + Convert a string to a UINT32 number. + + @param Str The string to convert from + + @return The number get from the string + +**/ +UINT32 +NetStringToU32 ( + IN UINT8 *Str + ) +{ + UINT32 Num; + + ASSERT (Str != NULL); + + Num = 0; + + for (; NET_IS_DIGIT (*Str); Str++) { + Num = Num * 10 + (*Str - '0'); + } + + return Num; +} + + +/** + Convert a string of the format "192.168.0.1" to an IP address. + + @param Str The string representation of IP + @param Ip The varible to get IP. + + @retval EFI_INVALID_PARAMETER The IP string is invalid. + @retval EFI_SUCCESS The IP is parsed into the Ip + +**/ +EFI_STATUS +NetStringToIp ( + IN UINT8 *Str, + OUT IP4_ADDR *Ip + ) +{ + UINT32 Byte; + UINT32 Addr; + UINTN Index; + + *Ip = 0; + Addr = 0; + + for (Index = 0; Index < 4; Index++) { + if (!NET_IS_DIGIT (*Str)) { + return EFI_INVALID_PARAMETER; + } + + Byte = NetStringToU32 (Str); + + if (Byte > 255) { + return EFI_INVALID_PARAMETER; + } + + Addr = (Addr << 8) | Byte; + + // + // Skip all the digitals and check whether the sepeator is the dot + // + while (NET_IS_DIGIT (*Str)) { + Str++; + } + + if ((Index < 3) && (*Str != '.')) { + return EFI_INVALID_PARAMETER; + } + + Str++; + } + + *Ip = Addr; + + return EFI_SUCCESS; +} + + +/** + Go through the packet to fill the Options array with the start + addresses of each MTFTP option name/value pair. + + @param Packet The packet to check + @param PacketLen The packet's length + @param Count The size of the Options on input. The actual + options on output + @param Options The option array to fill in + + @retval EFI_INVALID_PARAMETER The packet is mal-formated + @retval EFI_BUFFER_TOO_SMALL The Options array is too small + @retval EFI_SUCCESS The packet has been parsed into the Options array. + +**/ +EFI_STATUS +Mtftp4FillOptions ( + IN EFI_MTFTP4_PACKET *Packet, + IN UINT32 PacketLen, + IN OUT UINT32 *Count, + OUT EFI_MTFTP4_OPTION *Options OPTIONAL + ) +{ + UINT8 *Cur; + UINT8 *Last; + UINT8 Num; + UINT8 *Name; + UINT8 *Value; + + Num = 0; + Cur = (UINT8 *) Packet + MTFTP4_OPCODE_LEN; + Last = (UINT8 *) Packet + PacketLen - 1; + + // + // process option name and value pairs. The last byte is always zero + // + while (Cur < Last) { + Name = Cur; + + while (*Cur != 0) { + Cur++; + } + + if (Cur == Last) { + return EFI_INVALID_PARAMETER; + } + + Value = ++Cur; + + while (*Cur != 0) { + Cur++; + } + + Num++; + + if ((Options != NULL) && (Num <= *Count)) { + Options[Num - 1].OptionStr = Name; + Options[Num - 1].ValueStr = Value; + } + + Cur++; + } + + if ((*Count < Num) || (Options == NULL)) { + *Count = Num; + return EFI_BUFFER_TOO_SMALL; + } + + *Count = Num; + return EFI_SUCCESS; +} + + +/** + Allocate and fill in a array of Mtftp options from the Packet. + + It first calls Mtftp4FillOption to get the option number, then allocate + the array, at last, call Mtftp4FillOption again to save the options. + + @param Packet The packet to parse + @param PacketLen The length of the packet + @param OptionCount The number of options in the packet + @param OptionList The point to get the option array. + + @retval EFI_INVALID_PARAMETER The parametera are invalid or packet isn't a + well-formated OACK packet. + @retval EFI_SUCCESS The option array is build + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the array + +**/ +EFI_STATUS +Mtftp4ExtractOptions ( + IN EFI_MTFTP4_PACKET *Packet, + IN UINT32 PacketLen, + OUT UINT32 *OptionCount, + OUT EFI_MTFTP4_OPTION **OptionList OPTIONAL + ) +{ + EFI_STATUS Status; + + *OptionCount = 0; + + if (OptionList != NULL) { + *OptionList = NULL; + } + + if (NTOHS (Packet->OpCode) != EFI_MTFTP4_OPCODE_OACK) { + return EFI_INVALID_PARAMETER; + } + + if (PacketLen == MTFTP4_OPCODE_LEN) { + return EFI_SUCCESS; + } + + // + // The last byte must be zero to terminate the options + // + if (*((UINT8 *) Packet + PacketLen - 1) != 0) { + return EFI_INVALID_PARAMETER; + } + + // + // Get the number of options + // + Status = Mtftp4FillOptions (Packet, PacketLen, OptionCount, NULL); + + if ((Status == EFI_SUCCESS) || (Status != EFI_BUFFER_TOO_SMALL)) { + return Status; + } + + // + // Allocate memory for the options, then call Mtftp4FillOptions to + // fill it if caller want that. + // + if (OptionList == NULL) { + return EFI_SUCCESS; + } + + *OptionList = AllocatePool (*OptionCount * sizeof (EFI_MTFTP4_OPTION)); + + if (*OptionList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Mtftp4FillOptions (Packet, PacketLen, OptionCount, *OptionList); + return EFI_SUCCESS; +} + + +/** + Parse the MTFTP multicast option. + + @param Value The Mtftp multicast value string + @param Option The option to save the info into. + + @retval EFI_INVALID_PARAMETER The multicast value string is invalid. + @retval EFI_SUCCESS The multicast value is parsed into the Option + +**/ +EFI_STATUS +Mtftp4ExtractMcast ( + IN UINT8 *Value, + IN OUT MTFTP4_OPTION *Option + ) +{ + EFI_STATUS Status; + UINT32 Num; + + // + // The multicast option is formated like "204.0.0.1,1857,1" + // The server can also omit the ip and port, use ",,1" + // + if (*Value == ',') { + Option->McastIp = 0; + } else { + Status = NetStringToIp (Value, &Option->McastIp); + + if (EFI_ERROR (Status)) { + return Status; + } + + while ((*Value != 0) && (*Value != ',')) { + Value++; + } + } + + if (*Value != ',') { + return EFI_INVALID_PARAMETER; + } + + Value++; + + // + // Convert the port setting. the server can send us a port number or + // empty string. such as the port in ",,1" + // + if (*Value == ',') { + Option->McastPort = 0; + } else { + Num = NetStringToU32 (Value); + + if (Num > 65535) { + return EFI_INVALID_PARAMETER; + } + + Option->McastPort = (UINT16) Num; + + while (NET_IS_DIGIT (*Value)) { + Value++; + } + } + + if (*Value != ',') { + return EFI_INVALID_PARAMETER; + } + + Value++; + + // + // Check the master/slave setting, 1 for master, 0 for slave. + // + Num = NetStringToU32 (Value); + + if ((Num != 0) && (Num != 1)) { + return EFI_INVALID_PARAMETER; + } + + Option->Master = (BOOLEAN) (Num == 1); + + while (NET_IS_DIGIT (*Value)) { + Value++; + } + + if (*Value != '\0') { + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + + +/** + Parse the option in Options array to MTFTP4_OPTION which program + can access directly. + + @param Options The option array, which contains addresses of each + option's name/value string. + @param Count The number of options in the Options + @param Request Whether this is a request or OACK. The format of + multicast is different according to this setting. + @param Operation The current performed operation. + @param MtftpOption The MTFTP4_OPTION for easy access. + + @retval EFI_INVALID_PARAMETER The option is mal-formated + @retval EFI_UNSUPPORTED Some option isn't supported + @retval EFI_SUCCESS The option are OK and has been parsed. + +**/ +EFI_STATUS +Mtftp4ParseOption ( + IN EFI_MTFTP4_OPTION *Options, + IN UINT32 Count, + IN BOOLEAN Request, + IN UINT16 Operation, + OUT MTFTP4_OPTION *MtftpOption + ) +{ + EFI_STATUS Status; + UINT32 Index; + UINT32 Value; + EFI_MTFTP4_OPTION *This; + + MtftpOption->Exist = 0; + + for (Index = 0; Index < Count; Index++) { + This = Options + Index; + + if ((This->OptionStr == NULL) || (This->ValueStr == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (NetStringEqualNoCase (This->OptionStr, (UINT8 *) "blksize")) { + // + // block size option, valid value is between [8, 65464] + // + Value = NetStringToU32 (This->ValueStr); + + if ((Value < 8) || (Value > 65464)) { + return EFI_INVALID_PARAMETER; + } + + MtftpOption->BlkSize = (UINT16) Value; + MtftpOption->Exist |= MTFTP4_BLKSIZE_EXIST; + + } else if (NetStringEqualNoCase (This->OptionStr, (UINT8 *) "timeout")) { + // + // timeout option, valid value is between [1, 255] + // + Value = NetStringToU32 (This->ValueStr); + + if ((Value < 1) || (Value > 255)) { + return EFI_INVALID_PARAMETER; + } + + MtftpOption->Timeout = (UINT8) Value; + + } else if (NetStringEqualNoCase (This->OptionStr, (UINT8 *) "tsize")) { + // + // tsize option, the biggest transfer supported is 4GB with block size option + // + MtftpOption->Tsize = NetStringToU32 (This->ValueStr); + MtftpOption->Exist |= MTFTP4_TSIZE_EXIST; + + } else if (NetStringEqualNoCase (This->OptionStr, (UINT8 *) "multicast")) { + // + // Multicast option, if it is a request, the value must be a zero + // length string, otherwise, it is formated like "204.0.0.1,1857,1\0" + // + if (Request) { + if (*(This->ValueStr) != '\0') { + return EFI_INVALID_PARAMETER; + } + + } else { + Status = Mtftp4ExtractMcast (This->ValueStr, MtftpOption); + + if (EFI_ERROR (Status)) { + return Status; + } + } + + MtftpOption->Exist |= MTFTP4_MCAST_EXIST; + + } else if (NetStringEqualNoCase (This->OptionStr, (UINT8 *) "windowsize")) { + if (Operation == EFI_MTFTP4_OPCODE_WRQ) { + // + // Currently, windowsize is not supported in the write operation. + // + return EFI_UNSUPPORTED; + } + + Value = NetStringToU32 (This->ValueStr); + + if (Value < 1) { + return EFI_INVALID_PARAMETER; + } + + MtftpOption->WindowSize = (UINT16) Value; + MtftpOption->Exist |= MTFTP4_WINDOWSIZE_EXIST; + } else if (Request) { + // + // Ignore the unsupported option if it is a reply, and return + // EFI_UNSUPPORTED if it's a request according to the UEFI spec. + // + return EFI_UNSUPPORTED; + } + } + + return EFI_SUCCESS; +} + + +/** + Parse the options in the OACK packet to MTFTP4_OPTION which program + can access directly. + + @param Packet The OACK packet to parse + @param PacketLen The length of the packet + @param Operation The current performed operation. + @param MtftpOption The MTFTP_OPTION for easy access. + + @retval EFI_INVALID_PARAMETER The packet option is mal-formated + @retval EFI_UNSUPPORTED Some option isn't supported + @retval EFI_SUCCESS The option are OK and has been parsed. + +**/ +EFI_STATUS +Mtftp4ParseOptionOack ( + IN EFI_MTFTP4_PACKET *Packet, + IN UINT32 PacketLen, + IN UINT16 Operation, + OUT MTFTP4_OPTION *MtftpOption + ) +{ + EFI_MTFTP4_OPTION *OptionList; + EFI_STATUS Status; + UINT32 Count; + + MtftpOption->Exist = 0; + + Status = Mtftp4ExtractOptions (Packet, PacketLen, &Count, &OptionList); + + if (EFI_ERROR (Status) || (Count == 0)) { + return Status; + } + ASSERT (OptionList != NULL); + + Status = Mtftp4ParseOption (OptionList, Count, FALSE, Operation, MtftpOption); + + FreePool (OptionList); + return Status; +} diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Option.h b/NetworkPkg/Mtftp4Dxe/Mtftp4Option.h new file mode 100644 index 000000000..fcc4cbe8f --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Option.h @@ -0,0 +1,111 @@ +/** @file + Routines to process MTFTP4 options. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#ifndef __EFI_MTFTP4_OPTION_H__ +#define __EFI_MTFTP4_OPTION_H__ + +#define MTFTP4_SUPPORTED_OPTIONS 5 +#define MTFTP4_OPCODE_LEN 2 +#define MTFTP4_ERRCODE_LEN 2 +#define MTFTP4_BLKNO_LEN 2 +#define MTFTP4_DATA_HEAD_LEN 4 + +#define MTFTP4_BLKSIZE_EXIST 0x01 +#define MTFTP4_TIMEOUT_EXIST 0x02 +#define MTFTP4_TSIZE_EXIST 0x04 +#define MTFTP4_MCAST_EXIST 0x08 +#define MTFTP4_WINDOWSIZE_EXIST 0x10 + +typedef struct { + UINT16 BlkSize; + UINT16 WindowSize; + UINT8 Timeout; + UINT32 Tsize; + IP4_ADDR McastIp; + UINT16 McastPort; + BOOLEAN Master; + UINT32 Exist; +} MTFTP4_OPTION; + +/** + Allocate and fill in a array of Mtftp options from the Packet. + + It first calls Mtftp4FillOption to get the option number, then allocate + the array, at last, call Mtftp4FillOption again to save the options. + + @param Packet The packet to parse + @param PacketLen The length of the packet + @param OptionCount The number of options in the packet + @param OptionList The point to get the option array. + + @retval EFI_INVALID_PARAMETER The parametera are invalid or packet isn't a + well-formated OACK packet. + @retval EFI_SUCCESS The option array is build + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the array + +**/ +EFI_STATUS +Mtftp4ExtractOptions ( + IN EFI_MTFTP4_PACKET *Packet, + IN UINT32 PacketLen, + OUT UINT32 *OptionCount, + OUT EFI_MTFTP4_OPTION **OptionList OPTIONAL + ); + +/** + Parse the option in Options array to MTFTP4_OPTION which program + can access directly. + + @param Options The option array, which contains addresses of each + option's name/value string. + @param Count The number of options in the Options + @param Request Whether this is a request or OACK. The format of + multicast is different according to this setting. + @param Operation The current performed operation. + @param MtftpOption The MTFTP4_OPTION for easy access. + + @retval EFI_INVALID_PARAMETER The option is mal-formated + @retval EFI_UNSUPPORTED Some option isn't supported + @retval EFI_SUCCESS The option are OK and has been parsed. + +**/ +EFI_STATUS +Mtftp4ParseOption ( + IN EFI_MTFTP4_OPTION *Options, + IN UINT32 Count, + IN BOOLEAN Request, + IN UINT16 Operation, + OUT MTFTP4_OPTION *MtftpOption + ); + +/** + Parse the options in the OACK packet to MTFTP4_OPTION which program + can access directly. + + @param Packet The OACK packet to parse + @param PacketLen The length of the packet + @param Operation The current performed operation. + @param MtftpOption The MTFTP_OPTION for easy access. + + @retval EFI_INVALID_PARAMETER The packet option is mal-formated + @retval EFI_UNSUPPORTED Some option isn't supported + @retval EFI_SUCCESS The option are OK and has been parsed. + +**/ +EFI_STATUS +Mtftp4ParseOptionOack ( + IN EFI_MTFTP4_PACKET *Packet, + IN UINT32 PacketLen, + IN UINT16 Operation, + OUT MTFTP4_OPTION *MtftpOption + ); + +extern CHAR8 *mMtftp4SupportedOptions[MTFTP4_SUPPORTED_OPTIONS]; + +#endif diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Rrq.c b/NetworkPkg/Mtftp4Dxe/Mtftp4Rrq.c new file mode 100644 index 000000000..24c965afb --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Rrq.c @@ -0,0 +1,818 @@ +/** @file + Routines to process Rrq (download). + +(C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Mtftp4Impl.h" + + +/** + The packet process callback for MTFTP download. + + @param UdpPacket The packet received + @param EndPoint The local/remote access point of the packet + @param IoStatus The status of the receiving + @param Context Opaque parameter, which is the MTFTP session + +**/ +VOID +EFIAPI +Mtftp4RrqInput ( + IN NET_BUF *UdpPacket, + IN UDP_END_POINT *EndPoint, + IN EFI_STATUS IoStatus, + IN VOID *Context + ); + + +/** + Start the MTFTP session to download. + + It will first initialize some of the internal states then build and send a RRQ + reqeuest packet, at last, it will start receive for the downloading. + + @param Instance The Mtftp session + @param Operation The MTFTP opcode, it may be a EFI_MTFTP4_OPCODE_RRQ + or EFI_MTFTP4_OPCODE_DIR. + + @retval EFI_SUCCESS The mtftp download session is started. + @retval Others Failed to start downloading. + +**/ +EFI_STATUS +Mtftp4RrqStart ( + IN MTFTP4_PROTOCOL *Instance, + IN UINT16 Operation + ) +{ + EFI_STATUS Status; + + // + // The valid block number range are [1, 0xffff]. For example: + // the client sends an RRQ request to the server, the server + // transfers the DATA1 block. If option negoitation is ongoing, + // the server will send back an OACK, then client will send ACK0. + // + Status = Mtftp4InitBlockRange (&Instance->Blocks, 1, 0xffff); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status = Mtftp4SendRequest (Instance); + + if (EFI_ERROR (Status)) { + return Status; + } + + return UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4RrqInput, Instance, 0); +} + + +/** + Build and send a ACK packet for the download session. + + @param Instance The Mtftp session + @param BlkNo The BlkNo to ack. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet + @retval EFI_SUCCESS The ACK has been sent + @retval Others Failed to send the ACK. + +**/ +EFI_STATUS +Mtftp4RrqSendAck ( + IN MTFTP4_PROTOCOL *Instance, + IN UINT16 BlkNo + ) +{ + EFI_MTFTP4_PACKET *Ack; + NET_BUF *Packet; + EFI_STATUS Status; + + Status = EFI_SUCCESS; + + Packet = NetbufAlloc (sizeof (EFI_MTFTP4_ACK_HEADER)); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Ack = (EFI_MTFTP4_PACKET *) NetbufAllocSpace ( + Packet, + sizeof (EFI_MTFTP4_ACK_HEADER), + FALSE + ); + ASSERT (Ack != NULL); + + Ack->Ack.OpCode = HTONS (EFI_MTFTP4_OPCODE_ACK); + Ack->Ack.Block[0] = HTONS (BlkNo); + + Status = Mtftp4SendPacket (Instance, Packet); + if (!EFI_ERROR (Status)) { + Instance->AckedBlock = Instance->TotalBlock; + } + + return Status; +} + + +/** + Deliver the received data block to the user, which can be saved + in the user provide buffer or through the CheckPacket callback. + + @param Instance The Mtftp session + @param Packet The received data packet + @param Len The packet length + + @retval EFI_SUCCESS The data is saved successfully + @retval EFI_ABORTED The user tells to abort by return an error through + CheckPacket + @retval EFI_BUFFER_TOO_SMALL The user's buffer is too small and buffer length is + updated to the actual buffer size needed. + +**/ +EFI_STATUS +Mtftp4RrqSaveBlock ( + IN OUT MTFTP4_PROTOCOL *Instance, + IN EFI_MTFTP4_PACKET *Packet, + IN UINT32 Len + ) +{ + EFI_MTFTP4_TOKEN *Token; + EFI_STATUS Status; + UINT16 Block; + UINT64 Start; + UINT32 DataLen; + UINT64 BlockCounter; + BOOLEAN Completed; + + Completed = FALSE; + Token = Instance->Token; + Block = NTOHS (Packet->Data.Block); + DataLen = Len - MTFTP4_DATA_HEAD_LEN; + + // + // This is the last block, save the block no + // + if (DataLen < Instance->BlkSize) { + Completed = TRUE; + Instance->LastBlock = Block; + Mtftp4SetLastBlockNum (&Instance->Blocks, Block); + } + + // + // Remove this block number from the file hole. If Mtftp4RemoveBlockNum + // returns EFI_NOT_FOUND, the block has been saved, don't save it again. + // Note that : For bigger files, allowing the block counter to roll over + // to accept transfers of unlimited size. So BlockCounter is memorised as + // continuous block counter. + // + Status = Mtftp4RemoveBlockNum (&Instance->Blocks, Block, Completed, &BlockCounter); + + if (Status == EFI_NOT_FOUND) { + return EFI_SUCCESS; + } else if (EFI_ERROR (Status)) { + return Status; + } + + if (Token->CheckPacket != NULL) { + Status = Token->CheckPacket (&Instance->Mtftp4, Token, (UINT16) Len, Packet); + + if (EFI_ERROR (Status)) { + Mtftp4SendError ( + Instance, + EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION, + (UINT8 *) "User aborted download" + ); + + return EFI_ABORTED; + } + } + + if (Token->Buffer != NULL) { + Start = MultU64x32 (BlockCounter - 1, Instance->BlkSize); + + if (Start + DataLen <= Token->BufferSize) { + CopyMem ((UINT8 *) Token->Buffer + Start, Packet->Data.Data, DataLen); + + // + // Update the file size when received the last block + // + if ((Instance->LastBlock == Block) && Completed) { + Token->BufferSize = Start + DataLen; + } + + } else if (Instance->LastBlock != 0) { + // + // Don't save the data if the buffer is too small, return + // EFI_BUFFER_TOO_SMALL if received the last packet. This + // will give a accurate file length. + // + Token->BufferSize = Start + DataLen; + + Mtftp4SendError ( + Instance, + EFI_MTFTP4_ERRORCODE_DISK_FULL, + (UINT8 *) "User provided memory block is too small" + ); + + return EFI_BUFFER_TOO_SMALL; + } + } + + return EFI_SUCCESS; +} + + +/** + Function to process the received data packets. + + It will save the block then send back an ACK if it is active. + + @param Instance The downloading MTFTP session + @param Packet The packet received + @param Len The length of the packet + @param Multicast Whether this packet is multicast or unicast + @param Completed Return whether the download has completed + + @retval EFI_SUCCESS The data packet is successfully processed + @retval EFI_ABORTED The download is aborted by the user + @retval EFI_BUFFER_TOO_SMALL The user provided buffer is too small + +**/ +EFI_STATUS +Mtftp4RrqHandleData ( + IN MTFTP4_PROTOCOL *Instance, + IN EFI_MTFTP4_PACKET *Packet, + IN UINT32 Len, + IN BOOLEAN Multicast, + OUT BOOLEAN *Completed + ) +{ + EFI_STATUS Status; + UINT16 BlockNum; + INTN Expected; + + *Completed = FALSE; + Status = EFI_SUCCESS; + BlockNum = NTOHS (Packet->Data.Block); + Expected = Mtftp4GetNextBlockNum (&Instance->Blocks); + + ASSERT (Expected >= 0); + + // + // If we are active (Master) and received an unexpected packet, transmit + // the ACK for the block we received, then restart receiving the + // expected one. If we are passive (Slave), save the block. + // + if (Instance->Master && (Expected != BlockNum)) { + // + // If Expected is 0, (UINT16) (Expected - 1) is also the expected Ack number (65535). + // + return Mtftp4RrqSendAck (Instance, (UINT16) (Expected - 1)); + } + + Status = Mtftp4RrqSaveBlock (Instance, Packet, Len); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Record the total received and saved block number. + // + Instance->TotalBlock ++; + + // + // Reset the passive client's timer whenever it received a + // valid data packet. + // + if (!Instance->Master) { + Mtftp4SetTimeout (Instance); + } + + // + // Check whether we have received all the blocks. Send the ACK if we + // are active (unicast client or master client for multicast download). + // If we have received all the blocks, send an ACK even if we are passive + // to tell the server that we are done. + // + Expected = Mtftp4GetNextBlockNum (&Instance->Blocks); + + if (Instance->Master || (Expected < 0)) { + if (Expected < 0) { + // + // If we are passive client, then the just received Block maybe + // isn't the last block. We need to send an ACK to the last block + // to inform the server that we are done. If we are active client, + // the Block == Instance->LastBlock. + // + BlockNum = Instance->LastBlock; + *Completed = TRUE; + + } else { + BlockNum = (UINT16) (Expected - 1); + } + + if (Instance->WindowSize == (Instance->TotalBlock - Instance->AckedBlock) || Expected < 0) { + Status = Mtftp4RrqSendAck (Instance, BlockNum); + } + + } + + return Status; +} + + +/** + Validate whether the options received in the server's OACK packet is valid. + + The options are valid only if: + 1. The server doesn't include options not requested by us + 2. The server can only use smaller blksize than that is requested + 3. The server can only use the same timeout as requested + 4. The server doesn't change its multicast channel. + + @param This The downloading Mtftp session + @param Reply The options in the OACK packet + @param Request The requested options + + @retval TRUE The options in the OACK is OK. + @retval FALSE The options in the OACK is invalid. + +**/ +BOOLEAN +Mtftp4RrqOackValid ( + IN MTFTP4_PROTOCOL *This, + IN MTFTP4_OPTION *Reply, + IN MTFTP4_OPTION *Request + ) +{ + + // + // It is invalid for server to return options we don't request + // + if ((Reply->Exist &~Request->Exist) != 0) { + return FALSE; + } + + // + // Server can only specify a smaller block size and window size to be used and + // return the timeout matches that requested. + // + if ((((Reply->Exist & MTFTP4_BLKSIZE_EXIST) != 0)&& (Reply->BlkSize > Request->BlkSize)) || + (((Reply->Exist & MTFTP4_WINDOWSIZE_EXIST) != 0)&& (Reply->WindowSize > Request->WindowSize)) || + (((Reply->Exist & MTFTP4_TIMEOUT_EXIST) != 0) && (Reply->Timeout != Request->Timeout)) + ) { + return FALSE; + } + + // + // The server can send ",,master" to client to change its master + // setting. But if it use the specific multicast channel, it can't + // change the setting. + // + if (((Reply->Exist & MTFTP4_MCAST_EXIST) != 0) && (This->McastIp != 0)) { + if ((Reply->McastIp != 0) && (Reply->McastIp != This->McastIp)) { + return FALSE; + } + + if ((Reply->McastPort != 0) && (Reply->McastPort != This->McastPort)) { + return FALSE; + } + } + + return TRUE; +} + + +/** + Configure a UDP IO port to receive the multicast. + + @param McastIo The UDP IO to configure + @param Context The opaque parameter to the function which is the + MTFTP session. + + @retval EFI_SUCCESS The UDP child is successfully configured. + @retval Others Failed to configure the UDP child. + +**/ +EFI_STATUS +EFIAPI +Mtftp4RrqConfigMcastPort ( + IN UDP_IO *McastIo, + IN VOID *Context + ) +{ + MTFTP4_PROTOCOL *Instance; + EFI_MTFTP4_CONFIG_DATA *Config; + EFI_UDP4_CONFIG_DATA UdpConfig; + EFI_IPv4_ADDRESS Group; + EFI_STATUS Status; + IP4_ADDR Ip; + + Instance = (MTFTP4_PROTOCOL *) Context; + Config = &Instance->Config; + + UdpConfig.AcceptBroadcast = FALSE; + UdpConfig.AcceptPromiscuous = FALSE; + UdpConfig.AcceptAnyPort = FALSE; + UdpConfig.AllowDuplicatePort = FALSE; + UdpConfig.TypeOfService = 0; + UdpConfig.TimeToLive = 64; + UdpConfig.DoNotFragment = FALSE; + UdpConfig.ReceiveTimeout = 0; + UdpConfig.TransmitTimeout = 0; + UdpConfig.UseDefaultAddress = Config->UseDefaultSetting; + IP4_COPY_ADDRESS (&UdpConfig.StationAddress, &Config->StationIp); + IP4_COPY_ADDRESS (&UdpConfig.SubnetMask, &Config->SubnetMask); + UdpConfig.StationPort = Instance->McastPort; + UdpConfig.RemotePort = 0; + + Ip = HTONL (Instance->ServerIp); + IP4_COPY_ADDRESS (&UdpConfig.RemoteAddress, &Ip); + + Status = McastIo->Protocol.Udp4->Configure (McastIo->Protocol.Udp4, &UdpConfig); + + if (EFI_ERROR (Status)) { + return Status; + } + + if (!Config->UseDefaultSetting && + !EFI_IP4_EQUAL (&mZeroIp4Addr, &Config->GatewayIp)) { + // + // The station IP address is manually configured and the Gateway IP is not 0. + // Add the default route for this UDP instance. + // + Status = McastIo->Protocol.Udp4->Routes ( + McastIo->Protocol.Udp4, + FALSE, + &mZeroIp4Addr, + &mZeroIp4Addr, + &Config->GatewayIp + ); + + if (EFI_ERROR (Status)) { + McastIo->Protocol.Udp4->Configure (McastIo->Protocol.Udp4, NULL); + return Status; + } + } + + // + // join the multicast group + // + Ip = HTONL (Instance->McastIp); + IP4_COPY_ADDRESS (&Group, &Ip); + + return McastIo->Protocol.Udp4->Groups (McastIo->Protocol.Udp4, TRUE, &Group); +} + + +/** + Function to process the OACK. + + It will first validate the OACK packet, then update the various negotiated parameters. + + @param Instance The download MTFTP session + @param Packet The packet received + @param Len The packet length + @param Multicast Whether this packet is received as a multicast + @param Completed Returns whether the download has completed. NOT + used by this function. + + @retval EFI_DEVICE_ERROR Failed to create/start a multicast UDP child + @retval EFI_TFTP_ERROR Some error happened during the process + @retval EFI_SUCCESS The OACK is successfully processed. + +**/ +EFI_STATUS +Mtftp4RrqHandleOack ( + IN OUT MTFTP4_PROTOCOL *Instance, + IN EFI_MTFTP4_PACKET *Packet, + IN UINT32 Len, + IN BOOLEAN Multicast, + OUT BOOLEAN *Completed + ) +{ + MTFTP4_OPTION Reply; + EFI_STATUS Status; + INTN Expected; + EFI_UDP4_PROTOCOL *Udp4; + + *Completed = FALSE; + + // + // If already started the master download, don't change the + // setting. Master download always succeeds. + // + Expected = Mtftp4GetNextBlockNum (&Instance->Blocks); + ASSERT (Expected != -1); + + if (Instance->Master && (Expected != 1)) { + return EFI_SUCCESS; + } + + // + // Parse and validate the options from server + // + ZeroMem (&Reply, sizeof (MTFTP4_OPTION)); + + Status = Mtftp4ParseOptionOack (Packet, Len, Instance->Operation, &Reply); + + if (EFI_ERROR (Status) || + !Mtftp4RrqOackValid (Instance, &Reply, &Instance->RequestOption)) { + // + // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES. + // + if (Status != EFI_OUT_OF_RESOURCES) { + Mtftp4SendError ( + Instance, + EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION, + (UINT8 *) "Mal-formated OACK packet" + ); + } + + return EFI_TFTP_ERROR; + } + + if ((Reply.Exist & MTFTP4_MCAST_EXIST) != 0) { + + // + // Save the multicast info. Always update the Master, only update the + // multicast IP address, block size, window size, timeoute at the first time. If IP + // address is updated, create a UDP child to receive the multicast. + // + Instance->Master = Reply.Master; + + if (Instance->McastIp == 0) { + if ((Reply.McastIp == 0) || (Reply.McastPort == 0)) { + Mtftp4SendError ( + Instance, + EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION, + (UINT8 *) "Illegal multicast setting" + ); + + return EFI_TFTP_ERROR; + } + + // + // Create a UDP child then start receive the multicast from it. + // + Instance->McastIp = Reply.McastIp; + Instance->McastPort = Reply.McastPort; + if (Instance->McastUdpPort == NULL) { + Instance->McastUdpPort = UdpIoCreateIo ( + Instance->Service->Controller, + Instance->Service->Image, + Mtftp4RrqConfigMcastPort, + UDP_IO_UDP4_VERSION, + Instance + ); + if (Instance->McastUdpPort != NULL) { + Status = gBS->OpenProtocol ( + Instance->McastUdpPort->UdpHandle, + &gEfiUdp4ProtocolGuid, + (VOID **) &Udp4, + Instance->Service->Image, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + UdpIoFreeIo (Instance->McastUdpPort); + Instance->McastUdpPort = NULL; + return EFI_DEVICE_ERROR; + } + } + } + + + if (Instance->McastUdpPort == NULL) { + return EFI_DEVICE_ERROR; + } + + Status = UdpIoRecvDatagram (Instance->McastUdpPort, Mtftp4RrqInput, Instance, 0); + + if (EFI_ERROR (Status)) { + Mtftp4SendError ( + Instance, + EFI_MTFTP4_ERRORCODE_ACCESS_VIOLATION, + (UINT8 *) "Failed to create socket to receive multicast packet" + ); + + return Status; + } + + // + // Update the parameters used. + // + if (Reply.BlkSize != 0) { + Instance->BlkSize = Reply.BlkSize; + } + + if (Reply.WindowSize != 0) { + Instance->WindowSize = Reply.WindowSize; + } + + if (Reply.Timeout != 0) { + Instance->Timeout = Reply.Timeout; + } + } + + } else { + Instance->Master = TRUE; + + if (Reply.BlkSize != 0) { + Instance->BlkSize = Reply.BlkSize; + } + + if (Reply.WindowSize != 0) { + Instance->WindowSize = Reply.WindowSize; + } + + if (Reply.Timeout != 0) { + Instance->Timeout = Reply.Timeout; + } + } + + // + // Send an ACK to (Expected - 1) which is 0 for unicast download, + // or tell the server we want to receive the Expected block. + // + return Mtftp4RrqSendAck (Instance, (UINT16) (Expected - 1)); +} + + +/** + The packet process callback for MTFTP download. + + @param UdpPacket The packet received + @param EndPoint The local/remote access point of the packet + @param IoStatus The status of the receiving + @param Context Opaque parameter, which is the MTFTP session + +**/ +VOID +EFIAPI +Mtftp4RrqInput ( + IN NET_BUF *UdpPacket, + IN UDP_END_POINT *EndPoint, + IN EFI_STATUS IoStatus, + IN VOID *Context + ) +{ + MTFTP4_PROTOCOL *Instance; + EFI_MTFTP4_PACKET *Packet; + BOOLEAN Completed; + BOOLEAN Multicast; + EFI_STATUS Status; + UINT16 Opcode; + UINT32 Len; + + Instance = (MTFTP4_PROTOCOL *) Context; + NET_CHECK_SIGNATURE (Instance, MTFTP4_PROTOCOL_SIGNATURE); + + Status = EFI_SUCCESS; + Packet = NULL; + Completed = FALSE; + Multicast = FALSE; + + if (EFI_ERROR (IoStatus)) { + Status = IoStatus; + goto ON_EXIT; + } + + ASSERT (UdpPacket != NULL); + + // + // Find the port this packet is from to restart receive correctly. + // + Multicast = (BOOLEAN) (EndPoint->LocalAddr.Addr[0] == Instance->McastIp); + + if (UdpPacket->TotalSize < MTFTP4_OPCODE_LEN) { + goto ON_EXIT; + } + + // + // Client send initial request to server's listening port. Server + // will select a UDP port to communicate with the client. The server + // is required to use the same port as RemotePort to multicast the + // data. + // + if (EndPoint->RemotePort != Instance->ConnectedPort) { + if (Instance->ConnectedPort != 0) { + goto ON_EXIT; + } else { + Instance->ConnectedPort = EndPoint->RemotePort; + } + } + + // + // Copy the MTFTP packet to a continuous buffer if it isn't already so. + // + Len = UdpPacket->TotalSize; + + if (UdpPacket->BlockOpNum > 1) { + Packet = AllocatePool (Len); + + if (Packet == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet); + + } else { + Packet = (EFI_MTFTP4_PACKET *) NetbufGetByte (UdpPacket, 0, NULL); + ASSERT (Packet != NULL); + } + + Opcode = NTOHS (Packet->OpCode); + + // + // Call the user's CheckPacket if provided. Abort the transmission + // if CheckPacket returns an EFI_ERROR code. + // + if ((Instance->Token->CheckPacket != NULL) && + ((Opcode == EFI_MTFTP4_OPCODE_OACK) || (Opcode == EFI_MTFTP4_OPCODE_ERROR))) { + + Status = Instance->Token->CheckPacket ( + &Instance->Mtftp4, + Instance->Token, + (UINT16) Len, + Packet + ); + + if (EFI_ERROR (Status)) { + // + // Send an error message to the server to inform it + // + if (Opcode != EFI_MTFTP4_OPCODE_ERROR) { + Mtftp4SendError ( + Instance, + EFI_MTFTP4_ERRORCODE_REQUEST_DENIED, + (UINT8 *) "User aborted the transfer" + ); + } + + Status = EFI_ABORTED; + goto ON_EXIT; + } + } + + switch (Opcode) { + case EFI_MTFTP4_OPCODE_DATA: + if ((Len > (UINT32) (MTFTP4_DATA_HEAD_LEN + Instance->BlkSize)) || + (Len < (UINT32) MTFTP4_DATA_HEAD_LEN)) { + goto ON_EXIT; + } + + Status = Mtftp4RrqHandleData (Instance, Packet, Len, Multicast, &Completed); + break; + + case EFI_MTFTP4_OPCODE_OACK: + if (Multicast || (Len <= MTFTP4_OPCODE_LEN)) { + goto ON_EXIT; + } + + Status = Mtftp4RrqHandleOack (Instance, Packet, Len, Multicast, &Completed); + break; + + case EFI_MTFTP4_OPCODE_ERROR: + Status = EFI_TFTP_ERROR; + break; + + default: + break; + } + +ON_EXIT: + + // + // Free the resources, then if !EFI_ERROR (Status), restart the + // receive, otherwise end the session. + // + if ((Packet != NULL) && (UdpPacket->BlockOpNum > 1)) { + FreePool (Packet); + } + + if (UdpPacket != NULL) { + NetbufFree (UdpPacket); + } + + if (!EFI_ERROR (Status) && !Completed) { + if (Multicast) { + Status = UdpIoRecvDatagram (Instance->McastUdpPort, Mtftp4RrqInput, Instance, 0); + } else { + Status = UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4RrqInput, Instance, 0); + } + } + + if (EFI_ERROR (Status) || Completed) { + Mtftp4CleanOperation (Instance, Status); + } +} diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Support.c b/NetworkPkg/Mtftp4Dxe/Mtftp4Support.c new file mode 100644 index 000000000..ad2ff7bf3 --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Support.c @@ -0,0 +1,663 @@ +/** @file + Support routines for Mtftp. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp4Impl.h" + + +/** + Allocate a MTFTP4 block range, then init it to the range of [Start, End] + + @param Start The start block number + @param End The last block number in the range + + @return Pointer to the created block range, NULL if failed to allocate memory. + +**/ +MTFTP4_BLOCK_RANGE * +Mtftp4AllocateRange ( + IN UINT16 Start, + IN UINT16 End + ) +{ + MTFTP4_BLOCK_RANGE *Range; + + Range = AllocateZeroPool (sizeof (MTFTP4_BLOCK_RANGE)); + + if (Range == NULL) { + return NULL; + } + + InitializeListHead (&Range->Link); + Range->Start = Start; + Range->End = End; + Range->Bound = End; + + return Range; +} + + +/** + Initialize the block range for either RRQ or WRQ. + + RRQ and WRQ have different requirements for Start and End. + For example, during start up, WRQ initializes its whole valid block range + to [0, 0xffff]. This is bacause the server will send us a ACK0 to inform us + to start the upload. When the client received ACK0, it will remove 0 from the + range, get the next block number, which is 1, then upload the BLOCK1. For RRQ + without option negotiation, the server will directly send us the BLOCK1 in + response to the client's RRQ. When received BLOCK1, the client will remove + it from the block range and send an ACK. It also works if there is option + negotiation. + + @param Head The block range head to initialize + @param Start The Start block number. + @param End The last block number. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range + @retval EFI_SUCCESS The initial block range is created. + +**/ +EFI_STATUS +Mtftp4InitBlockRange ( + IN LIST_ENTRY *Head, + IN UINT16 Start, + IN UINT16 End + ) +{ + MTFTP4_BLOCK_RANGE *Range; + + Range = Mtftp4AllocateRange (Start, End); + + if (Range == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + InsertTailList (Head, &Range->Link); + return EFI_SUCCESS; +} + + +/** + Get the first valid block number on the range list. + + @param Head The block range head + + @return The first valid block number, -1 if the block range is empty. + +**/ +INTN +Mtftp4GetNextBlockNum ( + IN LIST_ENTRY *Head + ) +{ + MTFTP4_BLOCK_RANGE *Range; + + if (IsListEmpty (Head)) { + return -1; + } + + Range = NET_LIST_HEAD (Head, MTFTP4_BLOCK_RANGE, Link); + return Range->Start; +} + + +/** + Set the last block number of the block range list. + + It will remove all the blocks after the Last. MTFTP initialize the block range + to the maximum possible range, such as [0, 0xffff] for WRQ. When it gets the + last block number, it will call this function to set the last block number. + + @param Head The block range list + @param Last The last block number + +**/ +VOID +Mtftp4SetLastBlockNum ( + IN LIST_ENTRY *Head, + IN UINT16 Last + ) +{ + MTFTP4_BLOCK_RANGE *Range; + + // + // Iterate from the tail to head to remove the block number + // after the last. + // + while (!IsListEmpty (Head)) { + Range = NET_LIST_TAIL (Head, MTFTP4_BLOCK_RANGE, Link); + + if (Range->Start > Last) { + RemoveEntryList (&Range->Link); + FreePool (Range); + continue; + } + + if (Range->End > Last) { + Range->End = Last; + } + + return ; + } +} + + +/** + Remove the block number from the block range list. + + @param Head The block range list to remove from + @param Num The block number to remove + @param Completed Whether Num is the last block number. + @param BlockCounter The continuous block counter instead of the value after roll-over. + + @retval EFI_NOT_FOUND The block number isn't in the block range list + @retval EFI_SUCCESS The block number has been removed from the list + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource + +**/ +EFI_STATUS +Mtftp4RemoveBlockNum ( + IN LIST_ENTRY *Head, + IN UINT16 Num, + IN BOOLEAN Completed, + OUT UINT64 *BlockCounter + ) +{ + MTFTP4_BLOCK_RANGE *Range; + MTFTP4_BLOCK_RANGE *NewRange; + LIST_ENTRY *Entry; + + NET_LIST_FOR_EACH (Entry, Head) { + + // + // Each block represents a hole [Start, End] in the file, + // skip to the first range with End >= Num + // + Range = NET_LIST_USER_STRUCT (Entry, MTFTP4_BLOCK_RANGE, Link); + + if (Range->End < Num) { + continue; + } + + // + // There are three different cases for Start + // 1. (Start > Num) && (End >= Num): + // because all the holes before this one has the condition of + // End < Num, so this block number has been removed. + // + // 2. (Start == Num) && (End >= Num): + // Need to increase the Start by one, and if End == Num, this + // hole has been removed completely, remove it. + // + // 3. (Start < Num) && (End >= Num): + // if End == Num, only need to decrease the End by one because + // we have (Start < Num) && (Num == End), so (Start <= End - 1). + // if (End > Num), the hold is splited into two holes, with + // [Start, Num - 1] and [Num + 1, End]. + // + if (Range->Start > Num) { + return EFI_NOT_FOUND; + + } else if (Range->Start == Num) { + Range->Start++; + + // + // Note that: RFC 1350 does not mention block counter roll-over, + // but several TFTP hosts implement the roll-over be able to accept + // transfers of unlimited size. There is no consensus, however, whether + // the counter should wrap around to zero or to one. Many implementations + // wrap to zero, because this is the simplest to implement. Here we choose + // this solution. + // + *BlockCounter = Num; + + if (Range->Round > 0) { + *BlockCounter += Range->Bound + MultU64x32 ((UINTN) (Range->Round -1), (UINT32) (Range->Bound + 1)) + 1; + } + + if (Range->Start > Range->Bound) { + Range->Start = 0; + Range->Round ++; + } + + if ((Range->Start > Range->End) || Completed) { + RemoveEntryList (&Range->Link); + FreePool (Range); + } + + return EFI_SUCCESS; + + } else { + if (Range->End == Num) { + Range->End--; + } else { + NewRange = Mtftp4AllocateRange ((UINT16) (Num + 1), (UINT16) Range->End); + + if (NewRange == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Range->End = Num - 1; + NetListInsertAfter (&Range->Link, &NewRange->Link); + } + + return EFI_SUCCESS; + } + } + + return EFI_NOT_FOUND; +} + + +/** + Build then transmit the request packet for the MTFTP session. + + @param Instance The Mtftp session + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request + @retval EFI_SUCCESS The request is built and sent + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp4SendRequest ( + IN MTFTP4_PROTOCOL *Instance + ) +{ + EFI_MTFTP4_PACKET *Packet; + EFI_MTFTP4_OPTION *Options; + EFI_MTFTP4_TOKEN *Token; + RETURN_STATUS Status; + NET_BUF *Nbuf; + UINT8 *Mode; + UINT8 *Cur; + UINTN Index; + UINT32 BufferLength; + UINTN FileNameLength; + UINTN ModeLength; + UINTN OptionStrLength; + UINTN ValueStrLength; + + Token = Instance->Token; + Options = Token->OptionList; + Mode = Instance->Token->ModeStr; + + if (Mode == NULL) { + Mode = (UINT8 *) "octet"; + } + + // + // Compute the packet length + // + FileNameLength = AsciiStrLen ((CHAR8 *) Token->Filename); + ModeLength = AsciiStrLen ((CHAR8 *) Mode); + BufferLength = (UINT32) FileNameLength + (UINT32) ModeLength + 4; + + for (Index = 0; Index < Token->OptionCount; Index++) { + OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr); + ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr); + BufferLength += (UINT32) OptionStrLength + (UINT32) ValueStrLength + 2; + } + // + // Allocate a packet then copy the data over + // + if ((Nbuf = NetbufAlloc (BufferLength)) == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Packet = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (Nbuf, BufferLength, FALSE); + ASSERT (Packet != NULL); + + Packet->OpCode = HTONS (Instance->Operation); + BufferLength -= sizeof (Packet->OpCode); + + Cur = Packet->Rrq.Filename; + Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Token->Filename); + ASSERT_EFI_ERROR (Status); + BufferLength -= (UINT32) (FileNameLength + 1); + Cur += FileNameLength + 1; + Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Mode); + ASSERT_EFI_ERROR (Status); + BufferLength -= (UINT32) (ModeLength + 1); + Cur += ModeLength + 1; + + for (Index = 0; Index < Token->OptionCount; ++Index) { + OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr); + ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr); + + Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].OptionStr); + ASSERT_EFI_ERROR (Status); + BufferLength -= (UINT32) (OptionStrLength + 1); + Cur += OptionStrLength + 1; + + Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].ValueStr); + ASSERT_EFI_ERROR (Status); + BufferLength -= (UINT32) (ValueStrLength + 1); + Cur += ValueStrLength + 1; + + } + + return Mtftp4SendPacket (Instance, Nbuf); +} + + +/** + Build then send an error message. + + @param Instance The MTFTP session + @param ErrCode The error code + @param ErrInfo The error message + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet + @retval EFI_SUCCESS The error packet is transmitted. + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp4SendError ( + IN MTFTP4_PROTOCOL *Instance, + IN UINT16 ErrCode, + IN UINT8 *ErrInfo + ) +{ + NET_BUF *Packet; + EFI_MTFTP4_PACKET *TftpError; + UINT32 Len; + + Len = (UINT32) (AsciiStrLen ((CHAR8 *) ErrInfo) + sizeof (EFI_MTFTP4_ERROR_HEADER)); + Packet = NetbufAlloc (Len); + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + TftpError = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (Packet, Len, FALSE); + ASSERT (TftpError != NULL); + + TftpError->OpCode = HTONS (EFI_MTFTP4_OPCODE_ERROR); + TftpError->Error.ErrorCode = HTONS (ErrCode); + + AsciiStrCpyS ((CHAR8 *) TftpError->Error.ErrorMessage, Len, (CHAR8 *) ErrInfo); + + return Mtftp4SendPacket (Instance, Packet); +} + + +/** + The callback function called when the packet is transmitted. + + It simply frees the packet. + + @param Packet The transmitted (or failed to) packet + @param EndPoint The local and remote UDP access point + @param IoStatus The result of the transmission + @param Context Opaque parameter to the callback + +**/ +VOID +EFIAPI +Mtftp4OnPacketSent ( + IN NET_BUF *Packet, + IN UDP_END_POINT *EndPoint, + IN EFI_STATUS IoStatus, + IN VOID *Context + ) +{ + NetbufFree (Packet); +} + + +/** + Set the timeout for the instance. User a longer time for passive instances. + + @param Instance The Mtftp session to set time out + +**/ +VOID +Mtftp4SetTimeout ( + IN OUT MTFTP4_PROTOCOL *Instance + ) +{ + if (Instance->Master) { + Instance->PacketToLive = Instance->Timeout; + } else { + Instance->PacketToLive = Instance->Timeout * 2; + } +} + + +/** + Send the packet for the instance. + + It will first save a reference to the packet for later retransmission. + Then determine the destination port, listen port for requests, and connected + port for others. At last, send the packet out. + + @param Instance The Mtftp instance + @param Packet The packet to send + + @retval EFI_SUCCESS The packet is sent out + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp4SendPacket ( + IN OUT MTFTP4_PROTOCOL *Instance, + IN OUT NET_BUF *Packet + ) +{ + UDP_END_POINT UdpPoint; + EFI_STATUS Status; + UINT16 OpCode; + UINT8 *Buffer; + + // + // Save the packet for retransmission + // + if (Instance->LastPacket != NULL) { + NetbufFree (Instance->LastPacket); + } + + Instance->LastPacket = Packet; + + Instance->CurRetry = 0; + Mtftp4SetTimeout (Instance); + + ZeroMem (&UdpPoint, sizeof (UdpPoint)); + UdpPoint.RemoteAddr.Addr[0] = Instance->ServerIp; + + // + // Send the requests to the listening port, other packets + // to the connected port + // + Buffer = NetbufGetByte (Packet, 0, NULL); + ASSERT (Buffer != NULL); + OpCode = NTOHS (*(UINT16 *)Buffer); + + if ((OpCode == EFI_MTFTP4_OPCODE_RRQ) || + (OpCode == EFI_MTFTP4_OPCODE_DIR) || + (OpCode == EFI_MTFTP4_OPCODE_WRQ)) { + UdpPoint.RemotePort = Instance->ListeningPort; + } else { + UdpPoint.RemotePort = Instance->ConnectedPort; + } + + NET_GET_REF (Packet); + + Status = UdpIoSendDatagram ( + Instance->UnicastPort, + Packet, + &UdpPoint, + NULL, + Mtftp4OnPacketSent, + Instance + ); + + if (EFI_ERROR (Status)) { + NET_PUT_REF (Packet); + } + + return Status; +} + + +/** + Retransmit the last packet for the instance. + + @param Instance The Mtftp instance + + @retval EFI_SUCCESS The last packet is retransmitted. + @retval Others Failed to retransmit. + +**/ +EFI_STATUS +Mtftp4Retransmit ( + IN MTFTP4_PROTOCOL *Instance + ) +{ + UDP_END_POINT UdpPoint; + EFI_STATUS Status; + UINT16 OpCode; + UINT8 *Buffer; + + ASSERT (Instance->LastPacket != NULL); + + ZeroMem (&UdpPoint, sizeof (UdpPoint)); + UdpPoint.RemoteAddr.Addr[0] = Instance->ServerIp; + + // + // Set the requests to the listening port, other packets to the connected port + // + Buffer = NetbufGetByte (Instance->LastPacket, 0, NULL); + ASSERT (Buffer != NULL); + OpCode = NTOHS (*(UINT16 *) Buffer); + + if ((OpCode == EFI_MTFTP4_OPCODE_RRQ) || (OpCode == EFI_MTFTP4_OPCODE_DIR) || + (OpCode == EFI_MTFTP4_OPCODE_WRQ)) { + UdpPoint.RemotePort = Instance->ListeningPort; + } else { + UdpPoint.RemotePort = Instance->ConnectedPort; + } + + NET_GET_REF (Instance->LastPacket); + + Status = UdpIoSendDatagram ( + Instance->UnicastPort, + Instance->LastPacket, + &UdpPoint, + NULL, + Mtftp4OnPacketSent, + Instance + ); + + if (EFI_ERROR (Status)) { + NET_PUT_REF (Instance->LastPacket); + } + + return Status; +} + + +/** + The timer ticking function in TPL_NOTIFY level for the Mtftp service instance. + + @param Event The ticking event + @param Context The Mtftp service instance + +**/ +VOID +EFIAPI +Mtftp4OnTimerTickNotifyLevel ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MTFTP4_SERVICE *MtftpSb; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + MTFTP4_PROTOCOL *Instance; + + MtftpSb = (MTFTP4_SERVICE *) Context; + + // + // Iterate through all the children of the Mtftp service instance. Time + // out the current packet transmit. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &MtftpSb->Children) { + Instance = NET_LIST_USER_STRUCT (Entry, MTFTP4_PROTOCOL, Link); + if ((Instance->PacketToLive == 0) || (--Instance->PacketToLive > 0)) { + Instance->HasTimeout = FALSE; + } else { + Instance->HasTimeout = TRUE; + } + } +} + + +/** + The timer ticking function for the Mtftp service instance. + + @param Event The ticking event + @param Context The Mtftp service instance + +**/ +VOID +EFIAPI +Mtftp4OnTimerTick ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MTFTP4_SERVICE *MtftpSb; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + MTFTP4_PROTOCOL *Instance; + EFI_MTFTP4_TOKEN *Token; + + MtftpSb = (MTFTP4_SERVICE *) Context; + + // + // Iterate through all the children of the Mtftp service instance. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &MtftpSb->Children) { + Instance = NET_LIST_USER_STRUCT (Entry, MTFTP4_PROTOCOL, Link); + if (!Instance->HasTimeout) { + continue; + } + + Instance->HasTimeout = FALSE; + + // + // Call the user's time out handler + // + Token = Instance->Token; + + if (Token != NULL && Token->TimeoutCallback != NULL && + EFI_ERROR (Token->TimeoutCallback (&Instance->Mtftp4, Token))) { + Mtftp4SendError ( + Instance, + EFI_MTFTP4_ERRORCODE_REQUEST_DENIED, + (UINT8 *) "User aborted the transfer in time out" + ); + + Mtftp4CleanOperation (Instance, EFI_ABORTED); + continue; + } + + // + // Retransmit the packet if haven't reach the maxmium retry count, + // otherwise exit the transfer. + // + if (++Instance->CurRetry < Instance->MaxRetry) { + Mtftp4Retransmit (Instance); + Mtftp4SetTimeout (Instance); + } else { + Mtftp4CleanOperation (Instance, EFI_TIMEOUT); + continue; + } + } +} diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Support.h b/NetworkPkg/Mtftp4Dxe/Mtftp4Support.h new file mode 100644 index 000000000..cfa7582fa --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Support.h @@ -0,0 +1,198 @@ +/** @file + Support routines for MTFTP. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_MTFTP4_SUPPORT_H__ +#define __EFI_MTFTP4_SUPPORT_H__ + +// +// The structure representing a range of block numbers, [Start, End]. +// It is used to remember the holes in the MTFTP block space. If all +// the holes are filled in, then the download or upload has completed. +// +typedef struct { + LIST_ENTRY Link; + INTN Start; + INTN End; + INTN Round; + INTN Bound; +} MTFTP4_BLOCK_RANGE; + + +/** + Initialize the block range for either RRQ or WRQ. + + RRQ and WRQ have different requirements for Start and End. + For example, during start up, WRQ initializes its whole valid block range + to [0, 0xffff]. This is bacause the server will send us a ACK0 to inform us + to start the upload. When the client received ACK0, it will remove 0 from the + range, get the next block number, which is 1, then upload the BLOCK1. For RRQ + without option negotiation, the server will directly send us the BLOCK1 in + response to the client's RRQ. When received BLOCK1, the client will remove + it from the block range and send an ACK. It also works if there is option + negotiation. + + @param Head The block range head to initialize + @param Start The Start block number. + @param End The last block number. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range + @retval EFI_SUCCESS The initial block range is created. + +**/ +EFI_STATUS +Mtftp4InitBlockRange ( + IN LIST_ENTRY *Head, + IN UINT16 Start, + IN UINT16 End + ); + +/** + Get the first valid block number on the range list. + + @param Head The block range head + + @return The first valid block number, -1 if the block range is empty. + +**/ +INTN +Mtftp4GetNextBlockNum ( + IN LIST_ENTRY *Head + ); + +/** + Set the last block number of the block range list. + + It will remove all the blocks after the Last. MTFTP initialize the block range + to the maximum possible range, such as [0, 0xffff] for WRQ. When it gets the + last block number, it will call this function to set the last block number. + + @param Head The block range list + @param Last The last block number + +**/ +VOID +Mtftp4SetLastBlockNum ( + IN LIST_ENTRY *Head, + IN UINT16 Last + ); + +/** + Remove the block number from the block range list. + + @param Head The block range list to remove from + @param Num The block number to remove + @param Completed Whether Num is the last block number. + @param BlockCounter The continuous block counter instead of the value after roll-over. + + @retval EFI_NOT_FOUND The block number isn't in the block range list + @retval EFI_SUCCESS The block number has been removed from the list + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource + +**/ +EFI_STATUS +Mtftp4RemoveBlockNum ( + IN LIST_ENTRY *Head, + IN UINT16 Num, + IN BOOLEAN Completed, + OUT UINT64 *BlockCounter + ); + +/** + Set the timeout for the instance. User a longer time for passive instances. + + @param Instance The Mtftp session to set time out + +**/ +VOID +Mtftp4SetTimeout ( + IN OUT MTFTP4_PROTOCOL *Instance + ); + +/** + Send the packet for the instance. + + It will first save a reference to the packet for later retransmission. + Then determine the destination port, listen port for requests, and connected + port for others. At last, send the packet out. + + @param Instance The Mtftp instance + @param Packet The packet to send + + @retval EFI_SUCCESS The packet is sent out + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp4SendPacket ( + IN OUT MTFTP4_PROTOCOL *Instance, + IN OUT NET_BUF *Packet + ); + +/** + Build then transmit the request packet for the MTFTP session. + + @param Instance The Mtftp session + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request + @retval EFI_SUCCESS The request is built and sent + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp4SendRequest ( + IN MTFTP4_PROTOCOL *Instance + ); + +/** + Build then send an error message. + + @param Instance The MTFTP session + @param ErrCode The error code + @param ErrInfo The error message + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet + @retval EFI_SUCCESS The error packet is transmitted. + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp4SendError ( + IN MTFTP4_PROTOCOL *Instance, + IN UINT16 ErrCode, + IN UINT8 *ErrInfo + ); + + +/** + The timer ticking function in TPL_NOTIFY level for the Mtftp service instance. + + @param Event The ticking event + @param Context The Mtftp service instance + +**/ +VOID +EFIAPI +Mtftp4OnTimerTickNotifyLevel ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + The timer ticking function for the Mtftp service instance. + + @param Event The ticking event + @param Context The Mtftp service instance + +**/ +VOID +EFIAPI +Mtftp4OnTimerTick ( + IN EFI_EVENT Event, + IN VOID *Context + ); +#endif diff --git a/NetworkPkg/Mtftp4Dxe/Mtftp4Wrq.c b/NetworkPkg/Mtftp4Dxe/Mtftp4Wrq.c new file mode 100644 index 000000000..14156b801 --- /dev/null +++ b/NetworkPkg/Mtftp4Dxe/Mtftp4Wrq.c @@ -0,0 +1,529 @@ +/** @file + Routines to process Wrq (upload). + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp4Impl.h" + + + +/** + Build then send a MTFTP data packet for the MTFTP upload session. + + @param Instance The MTFTP upload session. + @param BlockNum The block number to send. + + @retval EFI_OUT_OF_RESOURCES Failed to build the packet. + @retval EFI_ABORTED The consumer of this child directs to abort the + transmission by return an error through PacketNeeded. + @retval EFI_SUCCESS The data is sent. + +**/ +EFI_STATUS +Mtftp4WrqSendBlock ( + IN OUT MTFTP4_PROTOCOL *Instance, + IN UINT16 BlockNum + ) +{ + EFI_MTFTP4_PACKET *Packet; + EFI_MTFTP4_TOKEN *Token; + NET_BUF *UdpPacket; + EFI_STATUS Status; + UINT16 DataLen; + UINT8 *DataBuf; + UINT64 Start; + + // + // Allocate a buffer to hold the user data + // + UdpPacket = NetbufAlloc (Instance->BlkSize + MTFTP4_DATA_HEAD_LEN); + + if (UdpPacket == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Packet = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (UdpPacket, MTFTP4_DATA_HEAD_LEN, FALSE); + ASSERT (Packet != NULL); + + Packet->Data.OpCode = HTONS (EFI_MTFTP4_OPCODE_DATA); + Packet->Data.Block = HTONS (BlockNum); + + // + // Read the block from either the buffer or PacketNeeded callback + // + Token = Instance->Token; + DataLen = Instance->BlkSize; + + if (Token->Buffer != NULL) { + Start = MultU64x32 (BlockNum - 1, Instance->BlkSize); + + if (Token->BufferSize < Start + Instance->BlkSize) { + DataLen = (UINT16) (Token->BufferSize - Start); + Instance->LastBlock = BlockNum; + Mtftp4SetLastBlockNum (&Instance->Blocks, BlockNum); + } + + if (DataLen > 0) { + NetbufAllocSpace (UdpPacket, DataLen, FALSE); + CopyMem (Packet->Data.Data, (UINT8 *) Token->Buffer + Start, DataLen); + } + + } else { + // + // Get data from PacketNeeded + // + DataBuf = NULL; + Status = Token->PacketNeeded ( + &Instance->Mtftp4, + Token, + &DataLen, + (VOID **) &DataBuf + ); + + if (EFI_ERROR (Status) || (DataLen > Instance->BlkSize)) { + if (DataBuf != NULL) { + FreePool (DataBuf); + } + + if (UdpPacket != NULL) { + NetbufFree (UdpPacket); + } + + Mtftp4SendError ( + Instance, + EFI_MTFTP4_ERRORCODE_REQUEST_DENIED, + (UINT8 *) "User aborted the transfer" + ); + + return EFI_ABORTED; + } + + if (DataLen < Instance->BlkSize) { + Instance->LastBlock = BlockNum; + Mtftp4SetLastBlockNum (&Instance->Blocks, BlockNum); + } + + if (DataLen > 0) { + NetbufAllocSpace (UdpPacket, DataLen, FALSE); + CopyMem (Packet->Data.Data, DataBuf, DataLen); + FreePool (DataBuf); + } + } + + return Mtftp4SendPacket (Instance, UdpPacket); +} + + +/** + Function to handle received ACK packet. + + If the ACK number matches the expected block number, and there are more + data pending, send the next block. Otherwise tell the caller that we are done. + + @param Instance The MTFTP upload session + @param Packet The MTFTP packet received + @param Len The packet length + @param Completed Return whether the upload has finished. + + @retval EFI_SUCCESS The ACK is successfully processed. + @retval EFI_TFTP_ERROR The block number loops back. + @retval Others Failed to transmit the next data packet. + +**/ +EFI_STATUS +Mtftp4WrqHandleAck ( + IN MTFTP4_PROTOCOL *Instance, + IN EFI_MTFTP4_PACKET *Packet, + IN UINT32 Len, + OUT BOOLEAN *Completed + ) +{ + UINT16 AckNum; + INTN Expected; + UINT64 BlockCounter; + + *Completed = FALSE; + AckNum = NTOHS (Packet->Ack.Block[0]); + Expected = Mtftp4GetNextBlockNum (&Instance->Blocks); + + ASSERT (Expected >= 0); + + // + // Get an unwanted ACK, return EFI_SUCCESS to let Mtftp4WrqInput + // restart receive. + // + if (Expected != AckNum) { + return EFI_SUCCESS; + } + + // + // Remove the acked block number, if this is the last block number, + // tell the Mtftp4WrqInput to finish the transfer. This is the last + // block number if the block range are empty. + // + Mtftp4RemoveBlockNum (&Instance->Blocks, AckNum, *Completed, &BlockCounter); + + Expected = Mtftp4GetNextBlockNum (&Instance->Blocks); + + if (Expected < 0) { + + // + // The block range is empty. It may either because the the last + // block has been ACKed, or the sequence number just looped back, + // that is, there is more than 0xffff blocks. + // + if (Instance->LastBlock == AckNum) { + ASSERT (Instance->LastBlock >= 1); + *Completed = TRUE; + return EFI_SUCCESS; + + } else { + Mtftp4SendError ( + Instance, + EFI_MTFTP4_ERRORCODE_REQUEST_DENIED, + (UINT8 *) "Block number rolls back, not supported, try blksize option" + ); + + return EFI_TFTP_ERROR; + } + } + + return Mtftp4WrqSendBlock (Instance, (UINT16) Expected); +} + + +/** + Check whether the received OACK is valid. + + The OACK is valid only if: + 1. It only include options requested by us + 2. It can only include a smaller block size + 3. It can't change the proposed time out value. + 4. Other requirements of the individal MTFTP options as required. + + @param Reply The options included in the OACK + @param Request The options we requested + + @retval TRUE The options included in OACK is valid. + @retval FALSE The options included in OACK is invalid. + +**/ +BOOLEAN +Mtftp4WrqOackValid ( + IN MTFTP4_OPTION *Reply, + IN MTFTP4_OPTION *Request + ) +{ + // + // It is invalid for server to return options we don't request + // + if ((Reply->Exist & ~Request->Exist) != 0) { + return FALSE; + } + + // + // Server can only specify a smaller block size to be used and + // return the timeout matches that requested. + // + if ((((Reply->Exist & MTFTP4_BLKSIZE_EXIST) != 0) && (Reply->BlkSize > Request->BlkSize)) || + (((Reply->Exist & MTFTP4_TIMEOUT_EXIST) != 0) && (Reply->Timeout != Request->Timeout))) { + return FALSE; + } + + return TRUE; +} + + +/** + Function to handle the MTFTP OACK packet. + + It parses the packet's options, and update the internal states of the session. + + @param Instance The MTFTP session + @param Packet The received OACK packet + @param Len The length of the packet + @param Completed Whether the transmisson has completed. NOT used by + this function. + + @retval EFI_SUCCESS The OACK process is OK + @retval EFI_TFTP_ERROR Some error occured, and the session reset. + +**/ +EFI_STATUS +Mtftp4WrqHandleOack ( + IN OUT MTFTP4_PROTOCOL *Instance, + IN EFI_MTFTP4_PACKET *Packet, + IN UINT32 Len, + OUT BOOLEAN *Completed + ) +{ + MTFTP4_OPTION Reply; + EFI_MTFTP4_PACKET Bogus; + EFI_STATUS Status; + INTN Expected; + + *Completed = FALSE; + + // + // Ignore the OACK if already started the upload + // + Expected = Mtftp4GetNextBlockNum (&Instance->Blocks); + + if (Expected != 0) { + return EFI_SUCCESS; + } + + // + // Parse and validate the options from server + // + ZeroMem (&Reply, sizeof (MTFTP4_OPTION)); + Status = Mtftp4ParseOptionOack (Packet, Len, Instance->Operation, &Reply); + + if (EFI_ERROR (Status) || !Mtftp4WrqOackValid (&Reply, &Instance->RequestOption)) { + // + // Don't send a MTFTP error packet when out of resource, it can + // only make it worse. + // + if (Status != EFI_OUT_OF_RESOURCES) { + Mtftp4SendError ( + Instance, + EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION, + (UINT8 *) "Mal-formated OACK packet" + ); + } + + return EFI_TFTP_ERROR; + } + + if (Reply.BlkSize != 0) { + Instance->BlkSize = Reply.BlkSize; + } + + if (Reply.Timeout != 0) { + Instance->Timeout = Reply.Timeout; + } + + // + // Build a bogus ACK0 packet then pass it to the Mtftp4WrqHandleAck, + // which will start the transmission of the first data block. + // + Bogus.Ack.OpCode = HTONS (EFI_MTFTP4_OPCODE_ACK); + Bogus.Ack.Block[0] = 0; + + Status = Mtftp4WrqHandleAck ( + Instance, + &Bogus, + sizeof (EFI_MTFTP4_ACK_HEADER), + Completed + ); + + return Status; +} + + +/** + The input process routine for MTFTP upload. + + @param UdpPacket The received MTFTP packet. + @param EndPoint The local/remote access point + @param IoStatus The result of the packet receiving + @param Context Opaque parameter for the callback, which is the + MTFTP session. +**/ +VOID +EFIAPI +Mtftp4WrqInput ( + IN NET_BUF *UdpPacket, + IN UDP_END_POINT *EndPoint, + IN EFI_STATUS IoStatus, + IN VOID *Context + ) +{ + MTFTP4_PROTOCOL *Instance; + EFI_MTFTP4_PACKET *Packet; + BOOLEAN Completed; + EFI_STATUS Status; + UINT32 Len; + UINT16 Opcode; + + Instance = (MTFTP4_PROTOCOL *) Context; + NET_CHECK_SIGNATURE (Instance, MTFTP4_PROTOCOL_SIGNATURE); + + Completed = FALSE; + Packet = NULL; + Status = EFI_SUCCESS; + + if (EFI_ERROR (IoStatus)) { + Status = IoStatus; + goto ON_EXIT; + } + + ASSERT (UdpPacket != NULL); + + if (UdpPacket->TotalSize < MTFTP4_OPCODE_LEN) { + goto ON_EXIT; + } + + // + // Client send initial request to server's listening port. Server + // will select a UDP port to communicate with the client. + // + if (EndPoint->RemotePort != Instance->ConnectedPort) { + if (Instance->ConnectedPort != 0) { + goto ON_EXIT; + } else { + Instance->ConnectedPort = EndPoint->RemotePort; + } + } + + // + // Copy the MTFTP packet to a continuous buffer if it isn't already so. + // + Len = UdpPacket->TotalSize; + + if (UdpPacket->BlockOpNum > 1) { + Packet = AllocatePool (Len); + + if (Packet == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet); + + } else { + Packet = (EFI_MTFTP4_PACKET *) NetbufGetByte (UdpPacket, 0, NULL); + ASSERT (Packet != NULL); + } + + Opcode = NTOHS (Packet->OpCode); + + // + // Call the user's CheckPacket if provided. Abort the transmission + // if CheckPacket returns an EFI_ERROR code. + // + if ((Instance->Token->CheckPacket != NULL) && + ((Opcode == EFI_MTFTP4_OPCODE_OACK) || (Opcode == EFI_MTFTP4_OPCODE_ERROR))) { + + Status = Instance->Token->CheckPacket ( + &Instance->Mtftp4, + Instance->Token, + (UINT16) Len, + Packet + ); + + if (EFI_ERROR (Status)) { + // + // Send an error message to the server to inform it + // + if (Opcode != EFI_MTFTP4_OPCODE_ERROR) { + Mtftp4SendError ( + Instance, + EFI_MTFTP4_ERRORCODE_REQUEST_DENIED, + (UINT8 *) "User aborted the transfer" + ); + } + + Status = EFI_ABORTED; + goto ON_EXIT; + } + } + + switch (Opcode) { + case EFI_MTFTP4_OPCODE_ACK: + if (Len != MTFTP4_OPCODE_LEN + MTFTP4_BLKNO_LEN) { + goto ON_EXIT; + } + + Status = Mtftp4WrqHandleAck (Instance, Packet, Len, &Completed); + break; + + case EFI_MTFTP4_OPCODE_OACK: + if (Len <= MTFTP4_OPCODE_LEN) { + goto ON_EXIT; + } + + Status = Mtftp4WrqHandleOack (Instance, Packet, Len, &Completed); + break; + + case EFI_MTFTP4_OPCODE_ERROR: + Status = EFI_TFTP_ERROR; + break; + + default: + break; + } + +ON_EXIT: + // + // Free the resources, then if !EFI_ERROR (Status) and not completed, + // restart the receive, otherwise end the session. + // + if ((Packet != NULL) && (UdpPacket->BlockOpNum > 1)) { + FreePool (Packet); + } + + if (UdpPacket != NULL) { + NetbufFree (UdpPacket); + } + + if (!EFI_ERROR (Status) && !Completed) { + Status = UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4WrqInput, Instance, 0); + } + + // + // Status may have been updated by UdpIoRecvDatagram + // + if (EFI_ERROR (Status) || Completed) { + Mtftp4CleanOperation (Instance, Status); + } +} + + + +/** + Start the MTFTP session for upload. + + It will first init some states, then send the WRQ request packet, + and start receiving the packet. + + @param Instance The MTFTP session + @param Operation Redundant parameter, which is always + EFI_MTFTP4_OPCODE_WRQ here. + + @retval EFI_SUCCESS The upload process has been started. + @retval Others Failed to start the upload. + +**/ +EFI_STATUS +Mtftp4WrqStart ( + IN MTFTP4_PROTOCOL *Instance, + IN UINT16 Operation + ) +{ + EFI_STATUS Status; + + // + // The valid block number range are [0, 0xffff]. For example: + // the client sends an WRQ request to the server, the server + // ACK with an ACK0 to let client start transfer the first + // packet. + // + Status = Mtftp4InitBlockRange (&Instance->Blocks, 0, 0xffff); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status = Mtftp4SendRequest (Instance); + + if (EFI_ERROR (Status)) { + return Status; + } + + return UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4WrqInput, Instance, 0); +} + diff --git a/NetworkPkg/Mtftp6Dxe/ComponentName.c b/NetworkPkg/Mtftp6Dxe/ComponentName.c new file mode 100644 index 000000000..296be48ec --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/ComponentName.c @@ -0,0 +1,424 @@ +/** @file + UEFI Component Name(2) protocol implementation for Mtftp6 driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp6Impl.h" + + +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Mtftp6ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is being managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for bus drivers + attempting to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that attempts to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Mtftp6ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gMtftp6ComponentName = { + Mtftp6ComponentNameGetDriverName, + Mtftp6ComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gMtftp6ComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) Mtftp6ComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) Mtftp6ComponentNameGetControllerName, + "en" +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mMtftp6DriverNameTable[] = { + { + "eng;en", + L"MTFTP6 Network Service Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gMtftp6ControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Mtftp6ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mMtftp6DriverNameTable, + DriverName, + (BOOLEAN)(This == &gMtftp6ComponentName) + ); +} + +/** + Update the component name for the Mtftp6 child handle. + + @param Mtftp6[in] A pointer to the EFI_MTFTP6_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateName ( + IN EFI_MTFTP6_PROTOCOL *Mtftp6 + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[128]; + EFI_MTFTP6_MODE_DATA Mtftp6ModeData; + CHAR16 Address[sizeof"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"]; + + if (Mtftp6 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer. + // + Status = Mtftp6->GetModeData (Mtftp6, &Mtftp6ModeData); + if (!EFI_ERROR (Status)) { + Status = NetLibIp6ToStr (&Mtftp6ModeData.ConfigData.ServerIp, Address, sizeof(Address)); + if (EFI_ERROR (Status)) { + return Status; + } + UnicodeSPrint (HandleName, sizeof (HandleName), + L"MTFTPv6(ServerIp=%s, InitialServerPort=%d)", + Address, + Mtftp6ModeData.ConfigData.InitialServerPort + ); + } else { + UnicodeSPrint (HandleName, 0x100, L"MTFTPv6(%r)", Status); + } + + if (gMtftp6ControllerNameTable != NULL) { + FreeUnicodeStringTable (gMtftp6ControllerNameTable); + gMtftp6ControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gMtftp6ComponentName.SupportedLanguages, + &gMtftp6ControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gMtftp6ComponentName2.SupportedLanguages, + &gMtftp6ControllerNameTable, + HandleName, + FALSE + ); +} + + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is being managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + attempting to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that attempts to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Mtftp6ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_MTFTP6_PROTOCOL *Mtftp6; + + // + // Only provide names for child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiUdp6ProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiMtftp6ProtocolGuid, + (VOID **)&Mtftp6, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateName (Mtftp6); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gMtftp6ControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gMtftp6ComponentName) + ); +} + diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Driver.c b/NetworkPkg/Mtftp6Dxe/Mtftp6Driver.c new file mode 100644 index 000000000..18cdcddbe --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Driver.c @@ -0,0 +1,749 @@ +/** @file + Driver Binding functions and Service Binding functions + implementation for Mtftp6 Driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp6Impl.h" + + +EFI_DRIVER_BINDING_PROTOCOL gMtftp6DriverBinding = { + Mtftp6DriverBindingSupported, + Mtftp6DriverBindingStart, + Mtftp6DriverBindingStop, + 0xa, + NULL, + NULL +}; + +EFI_SERVICE_BINDING_PROTOCOL gMtftp6ServiceBindingTemplate = { + Mtftp6ServiceBindingCreateChild, + Mtftp6ServiceBindingDestroyChild +}; + + +/** + Destroy the MTFTP6 service. The MTFTP6 service may be partly initialized, + or partly destroyed. If a resource is destroyed, it is marked as such in + case the destroy failed and is called again later. + + @param[in] Service The MTFTP6 service to be destroyed. + +**/ +VOID +Mtftp6DestroyService ( + IN MTFTP6_SERVICE *Service + ) +{ + // + // Make sure all children instances have been already destroyed. + // + ASSERT (Service->ChildrenNum == 0); + + if (Service->DummyUdpIo != NULL) { + UdpIoFreeIo (Service->DummyUdpIo); + } + + if (Service->Timer != NULL) { + gBS->CloseEvent (Service->Timer); + } + + FreePool (Service); +} + + +/** + Create then initialize a MTFTP6 service binding instance. + + @param[in] Controller The controller to install the MTFTP6 service + binding on. + @param[in] Image The driver binding image of the MTFTP6 driver. + @param[out] Service The variable to receive the created service + binding instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to create the instance + @retval EFI_DEVICE_ERROR Failed to create a NULL UDP port to keep connection with UDP. + @retval EFI_SUCCESS The service instance is created for the controller. + +**/ +EFI_STATUS +Mtftp6CreateService ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE Image, + OUT MTFTP6_SERVICE **Service + ) +{ + MTFTP6_SERVICE *Mtftp6Srv; + EFI_STATUS Status; + + ASSERT (Service != NULL); + + *Service = NULL; + Mtftp6Srv = AllocateZeroPool (sizeof (MTFTP6_SERVICE)); + + if (Mtftp6Srv == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Mtftp6Srv->Signature = MTFTP6_SERVICE_SIGNATURE; + Mtftp6Srv->Controller = Controller; + Mtftp6Srv->Image = Image; + Mtftp6Srv->ChildrenNum = 0; + + CopyMem ( + &Mtftp6Srv->ServiceBinding, + &gMtftp6ServiceBindingTemplate, + sizeof (EFI_SERVICE_BINDING_PROTOCOL) + ); + + InitializeListHead (&Mtftp6Srv->Children); + + // + // Create a internal timer for all instances. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + Mtftp6OnTimerTick, + Mtftp6Srv, + &Mtftp6Srv->Timer + ); + + if (EFI_ERROR (Status)) { + FreePool (Mtftp6Srv); + return Status; + } + + // + // Create a dummy Udp6Io to build parent-child relationship between Udp6 driver + // and Mtftp6 driver. + // + Mtftp6Srv->DummyUdpIo = UdpIoCreateIo ( + Controller, + Image, + Mtftp6ConfigDummyUdpIo, + UDP_IO_UDP6_VERSION, + NULL + ); + + if (Mtftp6Srv->DummyUdpIo == NULL) { + gBS->CloseEvent (Mtftp6Srv->Timer); + FreePool (Mtftp6Srv); + return EFI_DEVICE_ERROR; + } + + *Service = Mtftp6Srv; + return EFI_SUCCESS; +} + + +/** + Destroy the MTFTP6 instance and recycle the resources. + + @param[in] Instance The pointer to the MTFTP6 instance. + +**/ +VOID +Mtftp6DestroyInstance ( + IN MTFTP6_INSTANCE *Instance + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + MTFTP6_BLOCK_RANGE *Block; + + if (Instance->Config != NULL) { + FreePool (Instance->Config); + } + + if (Instance->Token != NULL && Instance->Token->Event != NULL) { + gBS->SignalEvent (Instance->Token->Event); + } + + if (Instance->LastPacket != NULL) { + NetbufFree (Instance->LastPacket); + } + + if (Instance->UdpIo!= NULL) { + UdpIoFreeIo (Instance->UdpIo); + } + + if (Instance->McastUdpIo != NULL) { + UdpIoFreeIo (Instance->McastUdpIo); + } + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Instance->BlkList) { + Block = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link); + RemoveEntryList (Entry); + FreePool (Block); + } + + FreePool (Instance); +} + + +/** + Create the MTFTP6 instance and initialize it. + + @param[in] Service The pointer to the MTFTP6 service. + @param[out] Instance The pointer to the MTFTP6 instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_SUCCESS The MTFTP6 instance is created. + +**/ +EFI_STATUS +Mtftp6CreateInstance ( + IN MTFTP6_SERVICE *Service, + OUT MTFTP6_INSTANCE **Instance + ) +{ + MTFTP6_INSTANCE *Mtftp6Ins; + + *Instance = NULL; + Mtftp6Ins = AllocateZeroPool (sizeof (MTFTP6_INSTANCE)); + + if (Mtftp6Ins == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Mtftp6Ins->Signature = MTFTP6_INSTANCE_SIGNATURE; + Mtftp6Ins->InDestroy = FALSE; + Mtftp6Ins->Service = Service; + + CopyMem ( + &Mtftp6Ins->Mtftp6, + &gMtftp6ProtocolTemplate, + sizeof (EFI_MTFTP6_PROTOCOL) + ); + + InitializeListHead (&Mtftp6Ins->Link); + InitializeListHead (&Mtftp6Ins->BlkList); + + *Instance = Mtftp6Ins; + + return EFI_SUCCESS; +} + + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +Mtftp6DestroyChildEntryInHandleBuffer ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + MTFTP6_INSTANCE *Instance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = NET_LIST_USER_STRUCT_S (Entry, MTFTP6_INSTANCE, Link, MTFTP6_INSTANCE_SIGNATURE); + ServiceBinding = ((MTFTP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding; + NumberOfChildren = ((MTFTP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren; + ChildHandleBuffer = ((MTFTP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer; + + if (!NetIsInHandleBuffer (Instance->Handle, NumberOfChildren, ChildHandleBuffer)) { + return EFI_SUCCESS; + } + + return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle); +} + + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers, including + both device drivers and bus drivers. + + Entry point of the MTFTP6 driver to install various protocols. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[in] SystemTable The pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +Mtftp6DriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gMtftp6DriverBinding, + ImageHandle, + &gMtftp6ComponentName, + &gMtftp6ComponentName2 + ); +} + + +/** + Test to see if this driver supports Controller. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported(), it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to test + @param[in] RemainingDevicePath Optional parameter use to pick a specific child. + device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Mtftp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + return gBS->OpenProtocol ( + Controller, + &gEfiUdp6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); +} + + +/** + Start this driver on Controller. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to Controller. + @retval EFI_ALREADY_STARTED This driver is already running on Controller. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Mtftp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + MTFTP6_SERVICE *Service; + EFI_STATUS Status; + + // + // Directly return if driver is already running on this Nic handle. + // + Status = gBS->OpenProtocol ( + Controller, + &gEfiMtftp6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + // + // Create Mtftp6 service for this Nic handle + // + Status = Mtftp6CreateService ( + Controller, + This->DriverBindingHandle, + &Service + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (Service != NULL); + + // + // Start the internal timer to track the packet retransmission. + // + Status = gBS->SetTimer ( + Service->Timer, + TimerPeriodic, + TICKS_PER_SECOND + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Install the Mtftp6 service on the Nic handle. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &Controller, + &gEfiMtftp6ServiceBindingProtocolGuid, + &Service->ServiceBinding, + NULL + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + + Mtftp6DestroyService (Service); + return Status; +} + + +/** + Stop this driver on Controller. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop(), it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero, stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed Controller. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others This driver was not removed from this device. + +**/ +EFI_STATUS +EFIAPI +Mtftp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + MTFTP6_SERVICE *Service; + EFI_HANDLE NicHandle; + EFI_STATUS Status; + LIST_ENTRY *List; + MTFTP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context; + + // + // Locate the Nic handle to retrieve the Mtftp6 private data. + // + NicHandle = NetLibGetNicHandle (Controller, &gEfiUdp6ProtocolGuid); + + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiMtftp6ServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + NicHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + Service = MTFTP6_SERVICE_FROM_THIS (ServiceBinding); + + if (!IsListEmpty (&Service->Children)) { + // + // Destroy the Mtftp6 child instance in ChildHandleBuffer. + // + List = &Service->Children; + Context.ServiceBinding = ServiceBinding; + Context.NumberOfChildren = NumberOfChildren; + Context.ChildHandleBuffer = ChildHandleBuffer; + Status = NetDestroyLinkList ( + List, + Mtftp6DestroyChildEntryInHandleBuffer, + &Context, + NULL + ); + } + + if (NumberOfChildren == 0 && IsListEmpty (&Service->Children)) { + // + // Destroy the Mtftp6 service if there is no Mtftp6 child instance left. + // + gBS->UninstallProtocolInterface ( + NicHandle, + &gEfiMtftp6ServiceBindingProtocolGuid, + ServiceBinding + ); + + Mtftp6DestroyService (Service); + Status = EFI_SUCCESS; + } + + return Status; +} + + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing + UEFI handle, then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval Others The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +Mtftp6ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ) +{ + MTFTP6_SERVICE *Service; + MTFTP6_INSTANCE *Instance; + EFI_STATUS Status; + EFI_TPL OldTpl; + VOID *Udp6; + + if (This == NULL || ChildHandle == NULL) { + return EFI_INVALID_PARAMETER; + } + + Service = MTFTP6_SERVICE_FROM_THIS (This); + + Status = Mtftp6CreateInstance (Service, &Instance); + + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (Instance != NULL); + + // + // Install the Mtftp6 protocol on the new child handle. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiMtftp6ProtocolGuid, + &Instance->Mtftp6, + NULL + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Instance->Handle = *ChildHandle; + + // + // Open the Udp6 protocol by child. + // + Status = gBS->OpenProtocol ( + Service->DummyUdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + (VOID **) &Udp6, + gMtftp6DriverBinding.DriverBindingHandle, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + + if (EFI_ERROR (Status)) { + gBS->UninstallMultipleProtocolInterfaces ( + Instance->Handle, + &gEfiMtftp6ProtocolGuid, + &Instance->Mtftp6, + NULL + ); + + goto ON_ERROR; + } + + // + // Add the new Mtftp6 instance into the children list of Mtftp6 service. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&Service->Children, &Instance->Link); + Service->ChildrenNum++; + + gBS->RestoreTPL (OldTpl); + return EFI_SUCCESS; + +ON_ERROR: + + Mtftp6DestroyInstance (Instance); + return Status; +} + + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval Others The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Mtftp6ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + MTFTP6_SERVICE *Service; + MTFTP6_INSTANCE *Instance; + EFI_MTFTP6_PROTOCOL *Mtftp6; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if (This == NULL || ChildHandle == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Locate the Nic handle to retrieve the Mtftp6 private data. + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiMtftp6ProtocolGuid, + (VOID **) &Mtftp6, + gMtftp6DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Instance = MTFTP6_INSTANCE_FROM_THIS (Mtftp6); + Service = MTFTP6_SERVICE_FROM_THIS (This); + + if (Instance->Service != Service) { + return EFI_INVALID_PARAMETER; + } + + // + // Check whether the instance already in Destroy state. + // + if (Instance->InDestroy) { + return EFI_SUCCESS; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance->InDestroy = TRUE; + + gBS->CloseProtocol ( + Service->DummyUdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + gMtftp6DriverBinding.DriverBindingHandle, + ChildHandle + ); + + if (Instance->UdpIo != NULL) { + gBS->CloseProtocol ( + Instance->UdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + gMtftp6DriverBinding.DriverBindingHandle, + Instance->Handle + ); + } + + if (Instance->McastUdpIo != NULL) { + gBS->CloseProtocol ( + Instance->McastUdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + gMtftp6DriverBinding.DriverBindingHandle, + Instance->Handle + ); + } + + // + // Uninstall the MTFTP6 protocol first to enable a top down destruction. + // + gBS->RestoreTPL (OldTpl); + Status = gBS->UninstallProtocolInterface ( + ChildHandle, + &gEfiMtftp6ProtocolGuid, + Mtftp6 + ); + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + if (EFI_ERROR (Status)) { + Instance->InDestroy = FALSE; + gBS->RestoreTPL (OldTpl); + return Status; + } + + // + // Remove the Mtftp6 instance from the children list of Mtftp6 service. + // + RemoveEntryList (&Instance->Link); + Service->ChildrenNum --; + + gBS->RestoreTPL (OldTpl); + + Mtftp6DestroyInstance (Instance); + + return EFI_SUCCESS; +} diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Driver.h b/NetworkPkg/Mtftp6Dxe/Mtftp6Driver.h new file mode 100644 index 000000000..30e6c8a3e --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Driver.h @@ -0,0 +1,146 @@ +/** @file + Driver Binding functions and Service Binding functions + declaration for Mtftp6 Driver. + + Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_MTFTP6_DRIVER_H__ +#define __EFI_MTFTP6_DRIVER_H__ + +#include + +extern EFI_COMPONENT_NAME_PROTOCOL gMtftp6ComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gMtftp6ComponentName2; +extern EFI_UNICODE_STRING_TABLE *gMtftp6ControllerNameTable; + +/** + Test to see if this driver supports Controller. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported(), it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Mtftp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Start this driver on Controller. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to Controller. + @retval EFI_ALREADY_STARTED This driver is already running on Controller. + @retval Others This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Mtftp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Stop this driver on Controller. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop(), it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] Controller Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero, stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed Controller. + @retval EFI_DEVICE_ERROR An unexpected error. + @retval Others This driver was not removed from this device. + +**/ +EFI_STATUS +EFIAPI +Mtftp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing + UEFI handle, then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval Others The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +Mtftp6ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval Others The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Mtftp6ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +#endif diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.inf b/NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.inf new file mode 100644 index 000000000..7a1af34f9 --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.inf @@ -0,0 +1,71 @@ +## @file +# Client-side Mtftp6 service. +# +# This module produces EFI MTFTPv6 Protocol which provides basic services for +# client-side unicast and/or multicast TFTP. +# +# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = Mtftp6Dxe + FILE_GUID = 99F03B99-98D8-49dd-A8D3-3219D0FFE41E + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = Mtftp6DriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload + MODULE_UNI_FILE = Mtftp6Dxe.uni + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = gMtftp6DriverBinding +# COMPONENT_NAME = gMtftp6ComponentName +# COMPONENT_NAME2 = gMtftp6ComponentName2 +# + +[Sources] + Mtftp6Driver.c + Mtftp6Driver.h + Mtftp6Impl.c + Mtftp6Impl.h + Mtftp6Option.c + Mtftp6Option.h + Mtftp6Support.h + Mtftp6Support.c + Mtftp6Rrq.c + Mtftp6Wrq.c + ComponentName.c + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + UefiLib + BaseLib + UefiBootServicesTableLib + UefiRuntimeServicesTableLib + UefiDriverEntryPoint + DebugLib + NetLib + UdpIoLib + + +[Protocols] + gEfiUdp6ServiceBindingProtocolGuid ## TO_START + gEfiUdp6ProtocolGuid ## TO_START + gEfiMtftp6ServiceBindingProtocolGuid ## BY_START + gEfiMtftp6ProtocolGuid ## BY_START + +[UserExtensions.TianoCore."ExtraFiles"] + Mtftp6DxeExtra.uni diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.uni b/NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.uni new file mode 100644 index 000000000..627442cfd --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.uni @@ -0,0 +1,17 @@ +// /** @file +// Client-side Mtftp6 service. +// +// This module produces EFI MTFTPv6 Protocol which provides basic services for +// client-side unicast and/or multicast TFTP. +// +// Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Client-side Mtftp6 service" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces EFI MTFTPv6 Protocol which provides basic services for client-side unicast and/or multicast TFTP." + diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6DxeExtra.uni b/NetworkPkg/Mtftp6Dxe/Mtftp6DxeExtra.uni new file mode 100644 index 000000000..cedd2be91 --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6DxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// Mtftp6Dxe Localized Strings and Content +// +// Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"MTFTP6 DXE" + + diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Impl.c b/NetworkPkg/Mtftp6Dxe/Mtftp6Impl.c new file mode 100644 index 000000000..92c9d5cb3 --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Impl.c @@ -0,0 +1,641 @@ +/** @file + This EFI_MTFTP6_PROTOCOL interface implementation. + + It supports the following RFCs: + RFC1350 - THE TFTP PROTOCOL (REVISION 2) + RFC2090 - TFTP Multicast Option + RFC2347 - TFTP Option Extension + RFC2348 - TFTP Blocksize Option + RFC2349 - TFTP Timeout Interval and Transfer Size Options + + Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp6Impl.h" + +EFI_MTFTP6_PROTOCOL gMtftp6ProtocolTemplate = { + EfiMtftp6GetModeData, + EfiMtftp6Configure, + EfiMtftp6GetInfo, + EfiMtftp6ParseOptions, + EfiMtftp6ReadFile, + EfiMtftp6WriteFile, + EfiMtftp6ReadDirectory, + EfiMtftp6Poll + }; + +/** + Returns the current operating mode data for the MTFTP6 instance. + + The GetModeData() function returns the current operating mode and + cached data packet for the MTFTP6 instance. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[out] ModeData The buffer in which the EFI MTFTPv6 Protocol driver mode + data is returned. + + @retval EFI_SUCCESS The configuration data was returned successfully. + @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated. + @retval EFI_INVALID_PARAMETER This is NULL or ModeData is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6GetModeData ( + IN EFI_MTFTP6_PROTOCOL *This, + OUT EFI_MTFTP6_MODE_DATA *ModeData + ) +{ + MTFTP6_INSTANCE *Instance; + EFI_TPL OldTpl; + + if (This == NULL || ModeData == NULL) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Instance = MTFTP6_INSTANCE_FROM_THIS (This); + + // + // Copy back the configure data if the instance already configured. + // + if (Instance->Config != NULL) { + CopyMem ( + &ModeData->ConfigData, + Instance->Config, + sizeof (EFI_MTFTP6_CONFIG_DATA) + ); + } else { + ZeroMem ( + &ModeData->ConfigData, + sizeof (EFI_MTFTP6_CONFIG_DATA) + ); + } + + // + // Set the current support options in mode data. + // + ModeData->SupportedOptionCount = MTFTP6_SUPPORTED_OPTIONS_NUM; + ModeData->SupportedOptions = (UINT8 **) mMtftp6SupportedOptions; + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; +} + + +/** + Initializes, changes, or resets the default operational setting for + this EFI MTFTPv6 Protocol driver instance. + + The Configure() function is used to set and change the configuration + data for this EFI MTFTPv6 Protocol driver instance. The configuration + data can be reset to startup defaults by calling Configure() with + MtftpConfigData set to NULL. Whenever the instance is reset, any + pending operation is aborted. By changing the EFI MTFTPv6 Protocol + driver instance configuration data, the client can connect to + different MTFTPv6 servers. The configuration parameters in + MtftpConfigData are used as the default parameters in later MTFTPv6 + operations and can be overridden in later operations. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] MtftpConfigData Pointer to the configuration data structure. + + @retval EFI_SUCCESS The EFI MTFTPv6 Protocol instance was configured successfully. + @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE: + - This is NULL. + - MtftpConfigData.StationIp is neither zero nor one + of the configured IP addresses in the underlying IPv6 driver. + - MtftpCofigData.ServerIp is not a valid IPv6 unicast address. + Note: It does not match the UEFI 2.3 Specification. + @retval EFI_ACCESS_DENIED - The configuration could not be changed at this time because there + is some MTFTP background operation in progress. + - MtftpCofigData.LocalPort is already in use. + Note: It does not match the UEFI 2.3 Specification. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for use. + @retval EFI_OUT_OF_RESOURCES The EFI MTFTPv6 Protocol driver instance data could not be + allocated. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI + MTFTPv6 Protocol driver instance is not configured. + Note: It is not defined in the UEFI 2.3 Specification. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6Configure ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_CONFIG_DATA *MtftpConfigData OPTIONAL + ) +{ + MTFTP6_SERVICE *Service; + MTFTP6_INSTANCE *Instance; + EFI_UDP6_PROTOCOL *Udp6; + EFI_UDP6_CONFIG_DATA Udp6Cfg; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (MtftpConfigData != NULL && !NetIp6IsValidUnicast (&MtftpConfigData->ServerIp)) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Instance = MTFTP6_INSTANCE_FROM_THIS (This); + Service = Instance->Service; + Status = EFI_SUCCESS; + + if (MtftpConfigData == NULL) { + // + // Configure the instance as NULL to abort the current session. + // + Mtftp6OperationClean (Instance, EFI_ABORTED); + FreePool (Instance->Config); + Instance->Config = NULL; + } else { + // + // It's not allowed to configure one instance twice without configure null. + // + if (Instance->Config != NULL) { + Status = EFI_ACCESS_DENIED; + goto ON_EXIT; + } + // + // Allocate the configure buffer of the instance and store the user's data. + // + Instance->Config = AllocateZeroPool (sizeof (EFI_MTFTP6_CONFIG_DATA)); + + if (Instance->Config == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + CopyMem (Instance->Config, MtftpConfigData, sizeof (EFI_MTFTP6_CONFIG_DATA)); + + // + // Don't configure the udpio here because each operation might override + // the configuration, so delay udpio configuration in each operation. + // + if (Instance->UdpIo == NULL) { + Instance->UdpIo = UdpIoCreateIo ( + Service->Controller, + Service->Image, + Mtftp6ConfigDummyUdpIo, + UDP_IO_UDP6_VERSION, + NULL + ); + if (Instance->UdpIo != NULL) { + Status = gBS->OpenProtocol ( + Instance->UdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + (VOID **) &Udp6, + Service->Image, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + } + + if (Instance->UdpIo == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Continue to configure the downside Udp6 instance by user's data. + // + ZeroMem (&Udp6Cfg, sizeof (EFI_UDP6_CONFIG_DATA)); + + Udp6Cfg.AcceptPromiscuous = FALSE; + Udp6Cfg.AcceptAnyPort = FALSE; + Udp6Cfg.AllowDuplicatePort = FALSE; + Udp6Cfg.TrafficClass = 0; + Udp6Cfg.HopLimit = 128; + Udp6Cfg.ReceiveTimeout = 0; + Udp6Cfg.TransmitTimeout = 0; + Udp6Cfg.StationPort = Instance->Config->LocalPort; + Udp6Cfg.RemotePort = Instance->Config->InitialServerPort; + + CopyMem ( + &Udp6Cfg.StationAddress, + &Instance->Config->StationIp, + sizeof(EFI_IPv6_ADDRESS) + ); + + CopyMem ( + &Udp6Cfg.RemoteAddress, + &Instance->Config->ServerIp, + sizeof (EFI_IPv6_ADDRESS) + ); + + Udp6 = Instance->UdpIo->Protocol.Udp6; + Status = Udp6->Configure (Udp6, &Udp6Cfg); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + +ON_EXIT: + if (EFI_ERROR (Status)) { + if (Instance->Config != NULL) { + FreePool (Instance->Config); + Instance->Config = NULL; + } + if (Instance->UdpIo != NULL) { + UdpIoFreeIo (Instance->UdpIo); + Instance->UdpIo = NULL; + } + } + gBS->RestoreTPL (OldTpl); + return Status; +} + + +/** + Get the information of the download from the server. + + The GetInfo() function assembles an MTFTPv6 request packet + with options, sends it to the MTFTPv6 server, and may return + an MTFTPv6 OACK, MTFTPv6 ERROR, or ICMP ERROR packet. Retries + occur only if no response packets are received from the MTFTPv6 + server before the timeout expires. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] OverrideData Data that is used to override the existing parameters. If NULL, the + default parameters that were set in the EFI_MTFTP6_PROTOCOL.Configure() + function are used. + @param[in] Filename Pointer to null-terminated ASCII file name string. + @param[in] ModeStr Pointer to null-terminated ASCII mode string. If NULL, octet will be used. + @param[in] OptionCount Number of option/value string pairs in OptionList. + @param[in] OptionList Pointer to array of option/value string pairs. Ignored if + OptionCount is zero. + @param[out] PacketLength The number of bytes in the returned packet. + @param[out] Packet The pointer to the received packet. This buffer must be freed by + the caller. + + @retval EFI_SUCCESS An MTFTPv6 OACK packet was received and is in the Packet. + Note: It does not match the UEFI 2.3 Specification. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Filename is NULL. + - OptionCount is not zero and OptionList is NULL. + - One or more options in OptionList have wrong format. + - PacketLength is NULL. + - OverrideData.ServerIp is not valid unicast IPv6 addresses. + @retval EFI_UNSUPPORTED One or more options in the OptionList are unsupported by + this implementation. + @retval EFI_NOT_STARTED The EFI MTFTPv6 Protocol driver has not been started. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for use. + @retval EFI_ACCESS_DENIED The previous operation has not completed yet. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_TFTP_ERROR An MTFTPv6 ERROR packet was received and is in the Packet. + @retval EFI_NETWORK_UNREACHABLE An ICMP network unreachable error packet was received and the Packet is set to NULL. + Note: It is not defined in UEFI 2.3 Specification. + @retval EFI_HOST_UNREACHABLE An ICMP host unreachable error packet was received and the Packet is set to NULL. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_PROTOCOL_UNREACHABLE An ICMP protocol unreachable error packet was received and the Packet is set to NULL. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_PORT_UNREACHABLE An ICMP port unreachable error packet was received and the Packet is set to NULL. + @retval EFI_ICMP_ERROR Some other ICMP ERROR packet was received and the Packet is set to NULL. + Note: It does not match the UEFI 2.3 Specification. + @retval EFI_PROTOCOL_ERROR An unexpected MTFTPv6 packet was received and is in the Packet. + @retval EFI_TIMEOUT No responses were received from the MTFTPv6 server. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + @retval EFI_NO_MEDIA There was a media error. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6GetInfo ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_OVERRIDE_DATA *OverrideData OPTIONAL, + IN UINT8 *Filename, + IN UINT8 *ModeStr OPTIONAL, + IN UINT8 OptionCount, + IN EFI_MTFTP6_OPTION *OptionList OPTIONAL, + OUT UINT32 *PacketLength, + OUT EFI_MTFTP6_PACKET **Packet OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_MTFTP6_TOKEN Token; + MTFTP6_GETINFO_CONTEXT Context; + + if (This == NULL || + Filename == NULL || + PacketLength == NULL || + (OptionCount != 0 && OptionList == NULL) || + (OverrideData != NULL && !NetIp6IsValidUnicast (&OverrideData->ServerIp)) + ) { + return EFI_INVALID_PARAMETER; + } + + if (Packet != NULL) { + *Packet = NULL; + } + + *PacketLength = 0; + + Context.Packet = Packet; + Context.PacketLen = PacketLength; + Context.Status = EFI_SUCCESS; + + // + // Fill fields of the Token for GetInfo operation. + // + Token.Status = EFI_SUCCESS; + Token.Event = NULL; + Token.OverrideData = OverrideData; + Token.Filename = Filename; + Token.ModeStr = ModeStr; + Token.OptionCount = OptionCount; + Token.OptionList = OptionList; + Token.BufferSize = 0; + Token.Buffer = NULL; + Token.Context = &Context; + Token.CheckPacket = Mtftp6CheckPacket; + Token.TimeoutCallback = NULL; + Token.PacketNeeded = NULL; + + // + // Start the GetInfo operation by issue the Token. + // + Status = Mtftp6OperationStart (This, &Token, EFI_MTFTP6_OPCODE_RRQ); + + if (Status == EFI_ABORTED) { + // + // Return the status if failed to issue. + // + return Context.Status; + } + + return Status; +} + + +/** + Parse the options in an MTFTPv6 OACK packet. + + The ParseOptions() function parses the option fields in an MTFTPv6 OACK + packet and returns the number of options that were found, and optionally, + a list of pointers to the options in the packet. If one or more of the + option fields are not valid, then EFI_PROTOCOL_ERROR is returned and + *OptionCount and *OptionList stop at the last valid option. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] PacketLen Length of the OACK packet to be parsed. + @param[in] Packet Pointer to the OACK packet to be parsed. + @param[out] OptionCount Pointer to the number of options in the following OptionList. + @param[out] OptionList Pointer to EFI_MTFTP6_OPTION storage. Each pointer in the + OptionList points to the corresponding MTFTP option buffer + in the Packet. Call the EFI Boot Service FreePool() to + release the OptionList if the options in this OptionList + are not needed anymore. + + @retval EFI_SUCCESS The OACK packet was valid and the OptionCount and + OptionList parameters have been updated. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - PacketLen is 0. + - Packet is NULL or Packet is not a valid MTFTPv6 packet. + - OptionCount is NULL. + @retval EFI_NOT_FOUND No options were found in the OACK packet. + @retval EFI_OUT_OF_RESOURCES Storage for the OptionList array can not be allocated. + @retval EFI_PROTOCOL_ERROR One or more of the option fields is invalid. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6ParseOptions ( + IN EFI_MTFTP6_PROTOCOL *This, + IN UINT32 PacketLen, + IN EFI_MTFTP6_PACKET *Packet, + OUT UINT32 *OptionCount, + OUT EFI_MTFTP6_OPTION **OptionList OPTIONAL + ) +{ + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + return Mtftp6ParseStart (Packet, PacketLen, OptionCount, OptionList); +} + + +/** + Download a file from an MTFTPv6 server. + + The ReadFile() function is used to initialize and start an MTFTPv6 download + process, and optionally, wait for completion. When the download operation + completes, whether successfully or not, the Token.Status field is updated + by the EFI MTFTPv6 Protocol driver, and then Token.Event is signaled if it + is not NULL. + Data can be downloaded from the MTFTPv6 server into either of the following + locations: + - A fixed buffer that is pointed to by Token.Buffer + - A download service function that is pointed to by Token.CheckPacket. + If both Token.Buffer and Token.CheckPacket are used, then Token.CheckPacket + will be called first. If the call is successful, the packet will be stored + in Token.Buffer. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] Token Pointer to the token structure to provide the parameters that are + used in this operation. + + @retval EFI_SUCCESS The data file has been transferred successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_BUFFER_TOO_SMALL BufferSize is not zero but not large enough to hold the + downloaded data in downloading process. + Note: It does not match the UEFI 2.3 Specification. + @retval EFI_ABORTED Current operation is aborted by user. + @retval EFI_NETWORK_UNREACHABLE An ICMP network unreachable error packet was received. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_HOST_UNREACHABLE An ICMP host unreachable error packet was received. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_PROTOCOL_UNREACHABLE An ICMP protocol unreachable error packet was received. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_PORT_UNREACHABLE An ICMP port unreachable error packet was received. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_ICMP_ERROR An ICMP ERROR packet was received. + @retval EFI_TIMEOUT No responses were received from the MTFTPv6 server. + @retval EFI_TFTP_ERROR An MTFTPv6 ERROR packet was received. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + @retval EFI_NO_MEDIA There was a media error. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6ReadFile ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_TOKEN *Token + ) +{ + return Mtftp6OperationStart (This, Token, EFI_MTFTP6_OPCODE_RRQ); +} + + +/** + Send a file to an MTFTPv6 server. + + The WriteFile() function is used to initialize an uploading operation + with the given option list and optionally wait for completion. If one + or more of the options is not supported by the server, the unsupported + options are ignored and a standard TFTP process starts instead. When + the upload process completes, whether successfully or not, Token.Event + is signaled, and the EFI MTFTPv6 Protocol driver updates Token.Status. + The caller can supply the data to be uploaded in the following two modes: + - Through the user-provided buffer + - Through a callback function + With the user-provided buffer, the Token.BufferSize field indicates + the length of the buffer, and the driver will upload the data in the + buffer. With an EFI_MTFTP6_PACKET_NEEDED callback function, the driver + will call this callback function to get more data from the user to upload. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] Token Pointer to the token structure to provide the parameters that are + used in this operation. + + @retval EFI_SUCCESS The upload session has started. + @retval EFI_UNSUPPORTED The operation is not supported by this implementation. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Token is NULL. + - Token.Filename is NULL. + - Token.OptionCount is not zero and Token.OptionList is NULL. + - One or more options in Token.OptionList have wrong format. + - Token.Buffer and Token.PacketNeeded are both NULL. + - Token.OverrideData.ServerIp is not a valid unicast IPv6 address. + @retval EFI_UNSUPPORTED One or more options in the Token.OptionList are not + supported by this implementation. + @retval EFI_NOT_STARTED The EFI MTFTPv6 Protocol driver has not been started. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for use. + @retval EFI_ALREADY_STARTED This Token is already being used in another MTFTPv6 session. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_ACCESS_DENIED The previous operation has not completed yet. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6WriteFile ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_TOKEN *Token + ) +{ + return Mtftp6OperationStart (This, Token, EFI_MTFTP6_OPCODE_WRQ); +} + + +/** + Download a data file directory from an MTFTPv6 server. + + The ReadDirectory() function is used to return a list of files on the + MTFTPv6 server that are logically (or operationally) related to + Token.Filename. The directory request packet that is sent to the server + is built with the option list that was provided by the caller, if present. + The file information that the server returns is put into either of + the following locations: + - A fixed buffer that is pointed to by Token.Buffer. + - A download service function that is pointed to by Token.CheckPacket. + If both Token.Buffer and Token.CheckPacket are used, then Token.CheckPacket + will be called first. If the call is successful, the packet will be stored + in Token.Buffer. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] Token Pointer to the token structure to provide the parameters that are + used in this operation. + + @retval EFI_SUCCESS The MTFTPv6 related file "directory" has been downloaded. + @retval EFI_UNSUPPORTED The EFI MTFTPv6 Protocol driver does not support this function. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Token is NULL. + - Token.Filename is NULL. + - Token.OptionCount is not zero and Token.OptionList is NULL. + - One or more options in Token.OptionList have wrong format. + - Token.Buffer and Token.CheckPacket are both NULL. + - Token.OverrideData.ServerIp is not valid unicast IPv6 addresses. + @retval EFI_UNSUPPORTED One or more options in the Token.OptionList are not + supported by this implementation. + @retval EFI_NOT_STARTED The EFI MTFTPv6 Protocol driver has not been started. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for use. + @retval EFI_ALREADY_STARTED This Token is already being used in another MTFTPv6 session. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_ACCESS_DENIED The previous operation has not completed yet. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6ReadDirectory ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_TOKEN *Token + ) +{ + return Mtftp6OperationStart (This, Token, EFI_MTFTP6_OPCODE_DIR); +} + + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications + to increase the rate that data packets are moved between the + communications device and the transmit and receive queues. In some + systems, the periodic timer event in the managed network driver may + not poll the underlying communications device fast enough to transmit + and/or receive all data packets without missing incoming packets or + dropping outgoing packets. Drivers and applications that are + experiencing packet loss should try calling the Poll() function + more often. + + @param[in] This The MTFTP6 protocol instance. + + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI MTFTPv6 Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue. + Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6Poll ( + IN EFI_MTFTP6_PROTOCOL *This + ) +{ + MTFTP6_INSTANCE *Instance; + EFI_UDP6_PROTOCOL *Udp6; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MTFTP6_INSTANCE_FROM_THIS (This); + + // + // Check the instance whether configured or in destroy. + // + if (Instance->Config == NULL) { + return EFI_NOT_STARTED; + } + + Udp6 = Instance->UdpIo->Protocol.Udp6; + + return Udp6->Poll (Udp6); +} diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Impl.h b/NetworkPkg/Mtftp6Dxe/Mtftp6Impl.h new file mode 100644 index 000000000..2c557b192 --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Impl.h @@ -0,0 +1,484 @@ +/** @file + Mtftp6 internal data structure and definition declaration. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_MTFTP6_IMPL_H__ +#define __EFI_MTFTP6_IMPL_H__ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +typedef struct _MTFTP6_SERVICE MTFTP6_SERVICE; +typedef struct _MTFTP6_INSTANCE MTFTP6_INSTANCE; + +#include "Mtftp6Driver.h" +#include "Mtftp6Option.h" +#include "Mtftp6Support.h" + +#define MTFTP6_SERVICE_SIGNATURE SIGNATURE_32 ('M', 'F', '6', 'S') +#define MTFTP6_INSTANCE_SIGNATURE SIGNATURE_32 ('M', 'F', '6', 'I') + +#define MTFTP6_DEFAULT_SERVER_CMD_PORT 69 +#define MTFTP6_DEFAULT_TIMEOUT 3 +#define MTFTP6_GET_MAPPING_TIMEOUT 3 +#define MTFTP6_DEFAULT_MAX_RETRY 5 +#define MTFTP6_DEFAULT_BLK_SIZE 512 +#define MTFTP6_DEFAULT_WINDOWSIZE 1 +#define MTFTP6_TICK_PER_SECOND 10000000U + +#define MTFTP6_SERVICE_FROM_THIS(a) CR (a, MTFTP6_SERVICE, ServiceBinding, MTFTP6_SERVICE_SIGNATURE) +#define MTFTP6_INSTANCE_FROM_THIS(a) CR (a, MTFTP6_INSTANCE, Mtftp6, MTFTP6_INSTANCE_SIGNATURE) + +extern EFI_MTFTP6_PROTOCOL gMtftp6ProtocolTemplate; + +typedef struct _MTFTP6_GETINFO_CONTEXT{ + EFI_MTFTP6_PACKET **Packet; + UINT32 *PacketLen; + EFI_STATUS Status; +} MTFTP6_GETINFO_CONTEXT; + +// +// Control block for MTFTP6 instance, it's per configuration data. +// +struct _MTFTP6_INSTANCE { + UINT32 Signature; + EFI_HANDLE Handle; + LIST_ENTRY Link; + EFI_MTFTP6_PROTOCOL Mtftp6; + MTFTP6_SERVICE *Service; + EFI_MTFTP6_CONFIG_DATA *Config; + + EFI_MTFTP6_TOKEN *Token; + MTFTP6_EXT_OPTION_INFO ExtInfo; + + UINT16 BlkSize; + UINT16 LastBlk; + LIST_ENTRY BlkList; + + UINT16 Operation; + + UINT16 WindowSize; + + // + // Record the total received and saved block number. + // + UINT64 TotalBlock; + + // + // Record the acked block number. + // + UINT64 AckedBlock; + + EFI_IPv6_ADDRESS ServerIp; + UINT16 ServerCmdPort; + UINT16 ServerDataPort; + UDP_IO *UdpIo; + + EFI_IPv6_ADDRESS McastIp; + UINT16 McastPort; + UDP_IO *McastUdpIo; + + NET_BUF *LastPacket; + UINT32 CurRetry; + UINT32 MaxRetry; + UINT32 PacketToLive; + UINT32 Timeout; + + EFI_TPL OldTpl; + BOOLEAN IsTransmitted; + BOOLEAN IsMaster; + BOOLEAN InDestroy; +}; + +// +// Control block for MTFTP6 service, it's per Nic handle. +// +struct _MTFTP6_SERVICE { + UINT32 Signature; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + EFI_HANDLE Controller; + EFI_HANDLE Image; + + UINT16 ChildrenNum; + LIST_ENTRY Children; + // + // It is used to be as internal calculagraph for all instances. + // + EFI_EVENT Timer; + // + // It is used to maintain the parent-child relationship between + // mtftp driver and udp driver. + // + UDP_IO *DummyUdpIo; +}; + +typedef struct { + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; +} MTFTP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT; + +/** + Returns the current operating mode data for the MTFTP6 instance. + + The GetModeData() function returns the current operating mode and + cached data packet for the MTFTP6 instance. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[out] ModeData The buffer in which the EFI MTFTPv6 Protocol driver mode + data is returned. + + @retval EFI_SUCCESS The configuration data was returned successfully. + @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated. + @retval EFI_INVALID_PARAMETER This is NULL, or ModeData is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6GetModeData ( + IN EFI_MTFTP6_PROTOCOL *This, + OUT EFI_MTFTP6_MODE_DATA *ModeData + ); + +/** + Initializes, changes, or resets the default operational setting for + this EFI MTFTPv6 Protocol driver instance. + + The Configure() function is used to set and change the configuration + data for this EFI MTFTPv6 Protocol driver instance. The configuration + data can be reset to startup defaults by calling Configure() with + MtftpConfigData set to NULL. Whenever the instance is reset, any + pending operation is aborted. By changing the EFI MTFTPv6 Protocol + driver instance configuration data, the client can connect to + different MTFTPv6 servers. The configuration parameters in + MtftpConfigData are used as the default parameters in later MTFTPv6 + operations and can be overridden in later operations. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] MtftpConfigData Pointer to the configuration data structure. + + @retval EFI_SUCCESS The EFI MTFTPv6 Protocol instance was configured successfully. + @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE: + - This is NULL. + - MtftpConfigData.StationIp is neither zero nor one + of the configured IP addresses in the underlying IPv6 driver. + - MtftpCofigData.ServerIp is not a valid IPv6 unicast address. + Note: It does not match the UEFI 2.3 Specification. + @retval EFI_ACCESS_DENIED - The configuration could not be changed at this time because there + is some MTFTP background operation in progress. + - MtftpCofigData.LocalPort is already in use. + Note: It does not match the UEFI 2.3 Specification. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for use. + @retval EFI_OUT_OF_RESOURCES The EFI MTFTPv6 Protocol driver instance data could not be + allocated. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI + MTFTPv6 Protocol driver instance is not configured. + Note: It is not defined in the UEFI 2.3 Specification. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6Configure ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_CONFIG_DATA *MtftpConfigData OPTIONAL + ); + +/** + Get the information of the download from the server. + + The GetInfo() function assembles an MTFTPv6 request packet + with options, sends it to the MTFTPv6 server, and may return + an MTFTPv6 OACK, MTFTPv6 ERROR, or ICMP ERROR packet. Retries + occur only if no response packets are received from the MTFTPv6 + server before the timeout expires. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] OverrideData Data that is used to override the existing parameters. If NULL, the + default parameters that were set in the EFI_MTFTP6_PROTOCOL.Configure() + function are used. + @param[in] Filename Pointer to null-terminated ASCII file name string. + @param[in] ModeStr Pointer to null-terminated ASCII mode string. If NULL, octet will be used + @param[in] OptionCount Number of option/value string pairs in OptionList. + @param[in] OptionList Pointer to array of option/value string pairs. Ignored if + OptionCount is zero. + @param[out] PacketLength The number of bytes in the returned packet. + @param[out] Packet The pointer to the received packet. This buffer must be freed by + the caller. + + @retval EFI_SUCCESS An MTFTPv6 OACK packet was received and is in the Packet. + Note: It does not match the UEFI 2.3 Specification. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Filename is NULL. + - OptionCount is not zero and OptionList is NULL. + - One or more options in OptionList have wrong format. + - PacketLength is NULL. + - OverrideData.ServerIp is not a valid unicast IPv6 address. + @retval EFI_UNSUPPORTED One or more options in the OptionList are unsupported by + this implementation. + @retval EFI_NOT_STARTED The EFI MTFTPv6 Protocol driver has not been started. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for use. + @retval EFI_ACCESS_DENIED The previous operation has not completed yet. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_TFTP_ERROR An MTFTPv6 ERROR packet was received and is in the Packet. + @retval EFI_NETWORK_UNREACHABLE An ICMP network unreachable error packet was received, and the Packet is set to NULL. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_HOST_UNREACHABLE An ICMP host unreachable error packet was received, and the Packet is set to NULL. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_PROTOCOL_UNREACHABLE An ICMP protocol unreachable error packet was received, and the Packet is set to NULL. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_PORT_UNREACHABLE An ICMP port unreachable error packet was received, and the Packet is set to NULL. + @retval EFI_ICMP_ERROR Some other ICMP ERROR packet was received, and the Packet is set to NULL. + Note: It does not match the UEFI 2.3 Specification. + @retval EFI_PROTOCOL_ERROR An unexpected MTFTPv6 packet was received and is in the Packet. + @retval EFI_TIMEOUT No responses were received from the MTFTPv6 server. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6GetInfo ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_OVERRIDE_DATA *OverrideData OPTIONAL, + IN UINT8 *Filename, + IN UINT8 *ModeStr OPTIONAL, + IN UINT8 OptionCount, + IN EFI_MTFTP6_OPTION *OptionList OPTIONAL, + OUT UINT32 *PacketLength, + OUT EFI_MTFTP6_PACKET **Packet OPTIONAL + ); + +/** + Parse the options in an MTFTPv6 OACK packet. + + The ParseOptions() function parses the option fields in an MTFTPv6 OACK + packet and returns the number of options that were found, and optionally, + a list of pointers to the options in the packet. If one or more of the + option fields are not valid, then EFI_PROTOCOL_ERROR is returned and + *OptionCount and *OptionList stop at the last valid option. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] PacketLen Length of the OACK packet to be parsed. + @param[in] Packet Pointer to the OACK packet to be parsed. + @param[out] OptionCount Pointer to the number of options in the following OptionList. + @param[out] OptionList Pointer to EFI_MTFTP6_OPTION storage. Each pointer in the + OptionList points to the corresponding MTFTP option buffer + in the Packet. Call the EFI Boot Service FreePool() to + release the OptionList if the options in this OptionList + are not needed any more. + + @retval EFI_SUCCESS The OACK packet was valid and the OptionCount, and + OptionList parameters have been updated. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - PacketLen is 0. + - Packet is NULL or Packet is not a valid MTFTPv6 packet. + - OptionCount is NULL. + @retval EFI_NOT_FOUND No options were found in the OACK packet. + @retval EFI_OUT_OF_RESOURCES Storage for the OptionList array can not be allocated. + @retval EFI_PROTOCOL_ERROR One or more of the option fields is invalid. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6ParseOptions ( + IN EFI_MTFTP6_PROTOCOL *This, + IN UINT32 PacketLen, + IN EFI_MTFTP6_PACKET *Packet, + OUT UINT32 *OptionCount, + OUT EFI_MTFTP6_OPTION **OptionList OPTIONAL + ); + +/** + Download a file from an MTFTPv6 server. + + The ReadFile() function is used to initialize and start an MTFTPv6 download + process and optionally wait for completion. When the download operation + completes, whether successfully or not, the Token.Status field is updated + by the EFI MTFTPv6 Protocol driver, and then Token.Event is signaled if it + is not NULL. + Data can be downloaded from the MTFTPv6 server into either of the following + locations: + - A fixed buffer that is pointed to by Token.Buffer. + - A download service function that is pointed to by Token.CheckPacket. + If both Token.Buffer and Token.CheckPacket are used, then Token.CheckPacket + will be called first. If the call is successful, the packet will be stored + in Token.Buffer. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] Token Pointer to the token structure to provide the parameters that are + used in this operation. + + @retval EFI_SUCCESS The data file has been transferred successfully. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_BUFFER_TOO_SMALL BufferSize is not zero but not large enough to hold the + downloaded data in downloading process. + Note: It does not match the UEFI 2.3 Specification. + @retval EFI_ABORTED Current operation is aborted by user. + @retval EFI_NETWORK_UNREACHABLE An ICMP network unreachable error packet was received. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_HOST_UNREACHABLE An ICMP host unreachable error packet was received. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_PROTOCOL_UNREACHABLE An ICMP protocol unreachable error packet was received. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_PORT_UNREACHABLE An ICMP port unreachable error packet was received. + Note: It is not defined in the UEFI 2.3 Specification. + @retval EFI_ICMP_ERROR An ICMP ERROR packet was received. + @retval EFI_TIMEOUT No responses were received from the MTFTPv6 server. + @retval EFI_TFTP_ERROR An MTFTPv6 ERROR packet was received. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6ReadFile ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_TOKEN *Token + ); + +/** + Send a file to an MTFTPv6 server. + + The WriteFile() function is used to initialize an uploading operation + with the given option list, and optionally, wait for completion. If one + or more of the options is not supported by the server, the unsupported + options are ignored and a standard TFTP process starts instead. When + the upload process completes, whether successfully or not, Token.Event + is signaled, and the EFI MTFTPv6 Protocol driver updates Token.Status. + The caller can supply the data to be uploaded in the following two modes: + - Through the user-provided buffer. + - Through a callback function. + With the user-provided buffer, the Token.BufferSize field indicates + the length of the buffer, and the driver will upload the data in the + buffer. With an EFI_MTFTP6_PACKET_NEEDED callback function, the driver + will call this callback function to get more data from the user to upload. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] Token Pointer to the token structure to provide the parameters that are + used in this operation. + + @retval EFI_SUCCESS The upload session has started. + @retval EFI_UNSUPPORTED The operation is not supported by this implementation. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Token is NULL. + - Token.Filename is NULL. + - Token.OptionCount is not zero and Token.OptionList is NULL. + - One or more options in Token.OptionList have wrong format. + - Token.Buffer and Token.PacketNeeded are both NULL. + - Token.OverrideData.ServerIp is not valid unicast IPv6 addresses. + @retval EFI_UNSUPPORTED One or more options in the Token.OptionList are not + supported by this implementation. + @retval EFI_NOT_STARTED The EFI MTFTPv6 Protocol driver has not been started. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for use. + @retval EFI_ALREADY_STARTED This Token is already being used in another MTFTPv6 session. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_ACCESS_DENIED The previous operation has not completed yet. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6WriteFile ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_TOKEN *Token + ); + +/** + Download a data file directory from an MTFTPv6 server. + + The ReadDirectory() function is used to return a list of files on the + MTFTPv6 server that are logically (or operationally) related to + Token.Filename. The directory request packet that is sent to the server + is built with the option list that was provided by caller, if present. + The file information that the server returns is put into either of + the following locations: + - A fixed buffer that is pointed to by Token.Buffer. + - A download service function that is pointed to by Token.CheckPacket. + If both Token.Buffer and Token.CheckPacket are used, then Token.CheckPacket + will be called first. If the call is successful, the packet will be stored + in Token.Buffer. + + @param[in] This Pointer to the EFI_MTFTP6_PROTOCOL instance. + @param[in] Token Pointer to the token structure to provide the parameters that are + used in this operation. + + @retval EFI_SUCCESS The MTFTPv6 related file "directory" has been downloaded. + @retval EFI_UNSUPPORTED The EFI MTFTPv6 Protocol driver does not support this function. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Token is NULL. + - Token.Filename is NULL. + - Token.OptionCount is not zero and Token.OptionList is NULL. + - One or more options in Token.OptionList have wrong format. + - Token.Buffer and Token.CheckPacket are both NULL. + - Token.OverrideData.ServerIp is not valid unicast IPv6 addresses. + @retval EFI_UNSUPPORTED One or more options in the Token.OptionList are not + supported by this implementation. + @retval EFI_NOT_STARTED The EFI MTFTPv6 Protocol driver has not been started. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for use. + @retval EFI_ALREADY_STARTED This Token is already being used in another MTFTPv6 session. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + @retval EFI_ACCESS_DENIED The previous operation has not completed yet. + @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6ReadDirectory ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_TOKEN *Token + ); + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications + to increase the rate that data packets are moved between the + communications device and the transmit and receive queues.In some + systems, the periodic timer event in the managed network driver may + not poll the underlying communications device fast enough to transmit + and/or receive all data packets without missing incoming packets or + dropping outgoing packets. Drivers and applications that are + experiencing packet loss should try calling the Poll() function + more often. + + @param[in] This The MTFTP6 protocol instance. + + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_NOT_STARTED This EFI MTFTPv6 Protocol instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue. + Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +EfiMtftp6Poll ( + IN EFI_MTFTP6_PROTOCOL *This + ); + +#endif diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Option.c b/NetworkPkg/Mtftp6Dxe/Mtftp6Option.c new file mode 100644 index 000000000..2a83af987 --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Option.c @@ -0,0 +1,430 @@ +/** @file + Mtftp6 option parse functions implementation. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp6Impl.h" + +CHAR8 *mMtftp6SupportedOptions[MTFTP6_SUPPORTED_OPTIONS_NUM] = { + "blksize", + "windowsize", + "timeout", + "tsize", + "multicast" +}; + + +/** + Parse the NULL terminated ASCII string of multicast option. + + @param[in] Str The pointer to the Ascii string of multicast option. + @param[in] ExtInfo The pointer to the option information to be filled. + + @retval EFI_SUCCESS Parse the multicast option successfully. + @retval EFI_INVALID_PARAMETER The string is malformatted. + @retval EFI_OUT_OF_RESOURCES Failed to perform the operation due to lack of + resources. + +**/ +EFI_STATUS +Mtftp6ParseMcastOption ( + IN UINT8 *Str, + IN MTFTP6_EXT_OPTION_INFO *ExtInfo + ) +{ + EFI_STATUS Status; + UINT32 Num; + CHAR8 *Ip6Str; + CHAR8 *TempStr; + + // + // The multicast option is formated like "addr,port,mc" + // The server can also omit the ip and port, use ",,1" + // + if (*Str == ',') { + + ZeroMem (&ExtInfo->McastIp, sizeof (EFI_IPv6_ADDRESS)); + } else { + + Ip6Str = (CHAR8 *) AllocateCopyPool (AsciiStrSize ((CHAR8 *) Str), Str); + if (Ip6Str == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // The IPv6 address locates before comma in the input Str. + // + TempStr = Ip6Str; + while ((*TempStr != '\0') && (*TempStr != ',')) { + TempStr++; + } + + *TempStr = '\0'; + + Status = NetLibAsciiStrToIp6 (Ip6Str, &ExtInfo->McastIp); + FreePool (Ip6Str); + + if (EFI_ERROR (Status)) { + return Status; + } + + while ((*Str != '\0') && (*Str != ',')) { + Str++; + } + } + + if (*Str != ',') { + return EFI_INVALID_PARAMETER; + } + + Str++; + + // + // Convert the port setting. the server can send us a port number or + // empty string. such as the port in ",,1" + // + if (*Str == ',') { + + ExtInfo->McastPort = 0; + } else { + + Num = (UINT32) AsciiStrDecimalToUintn ((CHAR8 *) Str); + + if (Num > 65535) { + return EFI_INVALID_PARAMETER; + } + + ExtInfo->McastPort = (UINT16) Num; + + while (NET_IS_DIGIT (*Str)) { + Str++; + } + } + + if (*Str != ',') { + return EFI_INVALID_PARAMETER; + } + + Str++; + + // + // Check the master/slave setting, 1 for master, 0 for slave. + // + Num = (UINT32) AsciiStrDecimalToUintn ((CHAR8 *) Str); + + if (Num != 0 && Num != 1) { + return EFI_INVALID_PARAMETER; + } + + ExtInfo->IsMaster = (BOOLEAN) (Num == 1); + + while (NET_IS_DIGIT (*Str)) { + Str++; + } + + if (*Str != '\0') { + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + + +/** + Parse the MTFTP6 extesion options. + + @param[in] Options The pointer to the extension options list. + @param[in] Count The num of the extension options. + @param[in] IsRequest If FALSE, the extension options is included + by a request packet. + @param[in] Operation The current performed operation. + @param[in] ExtInfo The pointer to the option information to be filled. + + @retval EFI_SUCCESS Parse the multicast option successfully. + @retval EFI_INVALID_PARAMETER There is one option is malformatted at least. + @retval EFI_UNSUPPORTED There is one option is not supported at least. + +**/ +EFI_STATUS +Mtftp6ParseExtensionOption ( + IN EFI_MTFTP6_OPTION *Options, + IN UINT32 Count, + IN BOOLEAN IsRequest, + IN UINT16 Operation, + IN MTFTP6_EXT_OPTION_INFO *ExtInfo + ) +{ + EFI_STATUS Status; + EFI_MTFTP6_OPTION *Opt; + UINT32 Index; + UINT32 Value; + + ExtInfo->BitMap = 0; + + for (Index = 0; Index < Count; Index++) { + + Opt = Options + Index; + + if (Opt->OptionStr == NULL || Opt->ValueStr == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (AsciiStriCmp ((CHAR8 *) Opt->OptionStr, "blksize") == 0) { + // + // block size option, valid value is between [8, 65464] + // + Value = (UINT32) AsciiStrDecimalToUintn ((CHAR8 *) Opt->ValueStr); + + if ((Value < 8) || (Value > 65464)) { + return EFI_INVALID_PARAMETER; + } + + ExtInfo->BlkSize = (UINT16) Value; + ExtInfo->BitMap |= MTFTP6_OPT_BLKSIZE_BIT; + + } else if (AsciiStriCmp ((CHAR8 *) Opt->OptionStr, "timeout") == 0) { + // + // timeout option, valid value is between [1, 255] + // + Value = (UINT32) AsciiStrDecimalToUintn ((CHAR8 *) Opt->ValueStr); + + if (Value < 1 || Value > 255) { + return EFI_INVALID_PARAMETER; + } + + ExtInfo->Timeout = (UINT8) Value; + ExtInfo->BitMap |= MTFTP6_OPT_TIMEOUT_BIT; + + } else if (AsciiStriCmp ((CHAR8 *) Opt->OptionStr, "tsize") == 0) { + // + // tsize option, the biggest transfer supported is 4GB with block size option + // + ExtInfo->Tsize = (UINT32) AsciiStrDecimalToUintn ((CHAR8 *) Opt->ValueStr); + ExtInfo->BitMap |= MTFTP6_OPT_TSIZE_BIT; + + } else if (AsciiStriCmp ((CHAR8 *) Opt->OptionStr, "multicast") == 0) { + // + // Multicast option, if it is a request, the value must be a zero string, + // otherwise, it must be like "addr,port,mc" string, mc indicates master. + // + if (!IsRequest) { + + Status = Mtftp6ParseMcastOption (Opt->ValueStr, ExtInfo); + + if (EFI_ERROR (Status)) { + return Status; + } + } else if (*(Opt->ValueStr) != '\0') { + + return EFI_INVALID_PARAMETER; + } + + ExtInfo->BitMap |= MTFTP6_OPT_MCAST_BIT; + + } else if (AsciiStriCmp ((CHAR8 *) Opt->OptionStr, "windowsize") == 0) { + if (Operation == EFI_MTFTP6_OPCODE_WRQ) { + // + // Currently, windowsize is not supported in the write operation. + // + return EFI_UNSUPPORTED; + } + + Value = (UINT32) AsciiStrDecimalToUintn ((CHAR8 *) Opt->ValueStr); + + if ((Value < 1)) { + return EFI_INVALID_PARAMETER; + } + + ExtInfo->WindowSize = (UINT16) Value; + ExtInfo->BitMap |= MTFTP6_OPT_WINDOWSIZE_BIT; + + } else if (IsRequest) { + // + // If it's a request, unsupported; else if it's a reply, ignore. + // + return EFI_UNSUPPORTED; + } + } + + return EFI_SUCCESS; +} + + +/** + Go through the packet to fill the options array with the start + addresses of each MTFTP option name/value pair. + + @param[in] Packet The packet to be checked. + @param[in] PacketLen The length of the packet. + @param[in, out] Count The num of the Options on input. + The actual one on output. + @param[in] Options The option array to be filled. + It is optional. + + @retval EFI_SUCCESS The packet has been parsed successfully. + @retval EFI_INVALID_PARAMETER The packet is malformatted. + @retval EFI_BUFFER_TOO_SMALL The Options array is too small. + @retval EFI_PROTOCOL_ERROR An unexpected MTFTPv6 packet was received. + +**/ +EFI_STATUS +Mtftp6ParsePacketOption ( + IN EFI_MTFTP6_PACKET *Packet, + IN UINT32 PacketLen, + IN OUT UINT32 *Count, + IN EFI_MTFTP6_OPTION *Options OPTIONAL + ) +{ + UINT8 *Cur; + UINT8 *Last; + UINT8 Num; + UINT8 *Name; + UINT8 *Value; + + Num = 0; + Cur = (UINT8 *) Packet + MTFTP6_OPCODE_LEN; + Last = (UINT8 *) Packet + PacketLen - 1; + + // + // process option name and value pairs. + // The last byte is always zero. + // + while (Cur < Last) { + Name = Cur; + + while (*Cur != 0) { + Cur++; + } + + if (Cur == Last) { + return EFI_PROTOCOL_ERROR; + } + + Value = ++Cur; + + while (*Cur != 0) { + Cur++; + } + + Num++; + + if (Options != NULL && Num <= *Count) { + Options[Num - 1].OptionStr = Name; + Options[Num - 1].ValueStr = Value; + } + + Cur++; + } + + // + // Return buffer too small if the buffer passed-in isn't enough. + // + if (*Count < Num || Options == NULL) { + *Count = Num; + return EFI_BUFFER_TOO_SMALL; + } + + *Count = Num; + return EFI_SUCCESS; +} + + +/** + Go through the packet, generate option list array and fill it + by the result of parse options. + + @param[in] Packet The packet to be checked. + @param[in] PacketLen The length of the packet. + @param[in, out] OptionCount The num of the Options on input. + The actual one on output. + @param[out] OptionList The option list array to be generated + and filled. It is optional. + + @retval EFI_SUCCESS The packet has been parsed successfully. + @retval EFI_INVALID_PARAMETER The packet is malformatted. + @retval EFI_PROTOCOL_ERROR There is one option is malformatted at least. + @retval EFI_NOT_FOUND The packet has no options. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the array. + @retval EFI_BUFFER_TOO_SMALL The size of option list array is too small. + +**/ +EFI_STATUS +Mtftp6ParseStart ( + IN EFI_MTFTP6_PACKET *Packet, + IN UINT32 PacketLen, + IN OUT UINT32 *OptionCount, + OUT EFI_MTFTP6_OPTION **OptionList OPTIONAL + ) +{ + EFI_STATUS Status; + + if (PacketLen == 0 || Packet == NULL || OptionCount == NULL) { + return EFI_INVALID_PARAMETER; + } + + *OptionCount = 0; + + if (OptionList != NULL) { + *OptionList = NULL; + } + + if (NTOHS (Packet->OpCode) != EFI_MTFTP6_OPCODE_OACK) { + return EFI_INVALID_PARAMETER; + } + + // + // The last byte must be zero to terminate the options. + // + if (*((UINT8 *) Packet + PacketLen - 1) != 0) { + return EFI_PROTOCOL_ERROR; + } + + // + // Parse packet with NULL buffer for the first time to get the number + // of options in the packet. + // + Status = Mtftp6ParsePacketOption (Packet, PacketLen, OptionCount, NULL); + + if (Status != EFI_BUFFER_TOO_SMALL) { + return Status; + } + + // + // Return not found if there is no option parsed. + // + if (*OptionCount == 0) { + return EFI_NOT_FOUND; + } + + // + // Only need parse out the number of options. + // + if (OptionList == NULL) { + return EFI_SUCCESS; + } + + // + // Allocate the buffer according to the option number parsed before. + // + *OptionList = AllocateZeroPool (*OptionCount * sizeof (EFI_MTFTP6_OPTION)); + + if (*OptionList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Parse packet with allocated buffer for the second time to fill the pointer array + // of the options in the packet. + // + Status = Mtftp6ParsePacketOption (Packet, PacketLen, OptionCount, *OptionList); + + if (EFI_ERROR (Status)) { + return Status; + } + + return EFI_SUCCESS; +} diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Option.h b/NetworkPkg/Mtftp6Dxe/Mtftp6Option.h new file mode 100644 index 000000000..6dab4b1e7 --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Option.h @@ -0,0 +1,146 @@ +/** @file + Mtftp6 option parse functions declaration. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_MTFTP6_OPTION_H__ +#define __EFI_MTFTP6_OPTION_H__ + +#include + +#include + +#include +#include +#include +#include +#include + +#define MTFTP6_SUPPORTED_OPTIONS_NUM 5 +#define MTFTP6_OPCODE_LEN 2 +#define MTFTP6_ERRCODE_LEN 2 +#define MTFTP6_BLKNO_LEN 2 +#define MTFTP6_DATA_HEAD_LEN 4 + +// +// The bit map definition for Mtftp6 extension options. +// +#define MTFTP6_OPT_BLKSIZE_BIT 0x01 +#define MTFTP6_OPT_TIMEOUT_BIT 0x02 +#define MTFTP6_OPT_TSIZE_BIT 0x04 +#define MTFTP6_OPT_MCAST_BIT 0x08 +#define MTFTP6_OPT_WINDOWSIZE_BIT 0X10 + +extern CHAR8 *mMtftp6SupportedOptions[MTFTP6_SUPPORTED_OPTIONS_NUM]; + +typedef struct { + UINT16 BlkSize; + UINT16 WindowSize; + UINT8 Timeout; + UINT32 Tsize; + EFI_IPv6_ADDRESS McastIp; + UINT16 McastPort; + BOOLEAN IsMaster; + UINT32 BitMap; +} MTFTP6_EXT_OPTION_INFO; + +/** + Parse the Ascii string of multi-cast option. + + @param[in] Str The pointer to the Ascii string of multi-cast option. + @param[in] ExtInfo The pointer to the option information to be filled. + + @retval EFI_SUCCESS Parse the multicast option successfully. + @retval EFI_INVALID_PARAMETER The string is malformatted. + +**/ +EFI_STATUS +Mtftp6ParseMcastOption ( + IN UINT8 *Str, + IN MTFTP6_EXT_OPTION_INFO *ExtInfo + ); + + +/** + Parse the MTFTP6 extesion options. + + @param[in] Options The pointer to the extension options list. + @param[in] Count The num of the extension options. + @param[in] IsRequest If FALSE, the extension options is included + by a request packet. + @param[in] Operation The current performed operation. + @param[in] ExtInfo The pointer to the option information to be filled. + + @retval EFI_SUCCESS Parse the multicast option successfully. + @retval EFI_INVALID_PARAMETER There is one option is malformatted at least. + @retval EFI_UNSUPPORTED There is one option is not supported at least. + +**/ +EFI_STATUS +Mtftp6ParseExtensionOption ( + IN EFI_MTFTP6_OPTION *Options, + IN UINT32 Count, + IN BOOLEAN IsRequest, + IN UINT16 Operation, + IN MTFTP6_EXT_OPTION_INFO *ExtInfo + ); + + +/** + Go through the packet to fill the options array with the start + addresses of each MTFTP option name/value pair. + + @param[in] Packet The packet to be checked. + @param[in] PacketLen The length of the packet. + @param[in, out] Count The num of the Options on input. + The actual one on output. + @param[in] Options The option array to be filled + it's optional. + + @retval EFI_SUCCESS The packet has been parsed successfully. + @retval EFI_INVALID_PARAMETER The packet is malformatted + @retval EFI_BUFFER_TOO_SMALL The Options array is too small + @retval EFI_PROTOCOL_ERROR An unexpected MTFTPv6 packet was received. + +**/ +EFI_STATUS +Mtftp6ParsePacketOption ( + IN EFI_MTFTP6_PACKET *Packet, + IN UINT32 PacketLen, + IN OUT UINT32 *Count, + IN EFI_MTFTP6_OPTION *Options OPTIONAL + ); + + +/** + Go through the packet, generate option list array and fill it + by the result of parse options. + + @param[in] Packet The packet to be checked. + @param[in] PacketLen The length of the packet. + @param[in, out] OptionCount The num of the Options on input. + The actual one on output. + @param[out] OptionList The option list array to be generated + and filled. It is optional. + + @retval EFI_SUCCESS The packet has been parsed successfully. + @retval EFI_INVALID_PARAMETER The packet is malformatted. + @retval EFI_PROTOCOL_ERROR An option is malformatted. + @retval EFI_NOT_FOUND The packet has no options. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the array. + @retval EFI_BUFFER_TOO_SMALL The size of option list array is too small. + +**/ +EFI_STATUS +Mtftp6ParseStart ( + IN EFI_MTFTP6_PACKET *Packet, + IN UINT32 PacketLen, + IN OUT UINT32 *OptionCount, + OUT EFI_MTFTP6_OPTION **OptionList OPTIONAL + ); + +#endif diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Rrq.c b/NetworkPkg/Mtftp6Dxe/Mtftp6Rrq.c new file mode 100644 index 000000000..897358e5f --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Rrq.c @@ -0,0 +1,942 @@ +/** @file + Mtftp6 Rrq process functions implementation. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp6Impl.h" + + +/** + Build and send a ACK packet for download. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] BlockNum The block number to be acked. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet. + @retval EFI_SUCCESS The ACK has been sent. + @retval Others Failed to send the ACK. + +**/ +EFI_STATUS +Mtftp6RrqSendAck ( + IN MTFTP6_INSTANCE *Instance, + IN UINT16 BlockNum + ) +{ + EFI_MTFTP6_PACKET *Ack; + NET_BUF *Packet; + EFI_STATUS Status; + + Status = EFI_SUCCESS; + + // + // Allocate net buffer to create ack packet. + // + Packet = NetbufAlloc (sizeof (EFI_MTFTP6_ACK_HEADER)); + + if (Packet == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Ack = (EFI_MTFTP6_PACKET *) NetbufAllocSpace ( + Packet, + sizeof (EFI_MTFTP6_ACK_HEADER), + FALSE + ); + ASSERT (Ack != NULL); + + Ack->Ack.OpCode = HTONS (EFI_MTFTP6_OPCODE_ACK); + Ack->Ack.Block[0] = HTONS (BlockNum); + + // + // Reset current retry count of the instance. + // + Instance->CurRetry = 0; + Instance->LastPacket = Packet; + + Status = Mtftp6TransmitPacket (Instance, Packet); + if (!EFI_ERROR (Status)) { + Instance->AckedBlock = Instance->TotalBlock; + } + + return Status; +} + + +/** + Deliver the received data block to the user, which can be saved + in the user provide buffer or through the CheckPacket callback. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Packet The pointer to the received packet. + @param[in] Len The packet length. + @param[out] UdpPacket The net buf of the received packet. + + @retval EFI_SUCCESS The data was saved successfully. + @retval EFI_ABORTED The user tells to abort by return an error through + CheckPacket. + @retval EFI_BUFFER_TOO_SMALL The user's buffer is too small, and buffer length is + updated to the actual buffer size needed. + +**/ +EFI_STATUS +Mtftp6RrqSaveBlock ( + IN MTFTP6_INSTANCE *Instance, + IN EFI_MTFTP6_PACKET *Packet, + IN UINT32 Len, + OUT NET_BUF **UdpPacket + ) +{ + EFI_MTFTP6_TOKEN *Token; + EFI_STATUS Status; + UINT16 Block; + UINT64 Start; + UINT32 DataLen; + UINT64 BlockCounter; + BOOLEAN Completed; + + Completed = FALSE; + Token = Instance->Token; + Block = NTOHS (Packet->Data.Block); + DataLen = Len - MTFTP6_DATA_HEAD_LEN; + + // + // This is the last block, save the block num + // + if (DataLen < Instance->BlkSize) { + Completed = TRUE; + Instance->LastBlk = Block; + Mtftp6SetLastBlockNum (&Instance->BlkList, Block); + } + + // + // Remove this block number from the file hole. If Mtftp6RemoveBlockNum + // returns EFI_NOT_FOUND, the block has been saved, don't save it again. + // Note that : For bigger files, allowing the block counter to roll over + // to accept transfers of unlimited size. So BlockCounter is memorised as + // continuous block counter. + // + Status = Mtftp6RemoveBlockNum (&Instance->BlkList, Block, Completed, &BlockCounter); + + if (Status == EFI_NOT_FOUND) { + return EFI_SUCCESS; + } else if (EFI_ERROR (Status)) { + return Status; + } + + if (Token->CheckPacket != NULL) { + // + // Callback to the check packet routine with the received packet. + // + Status = Token->CheckPacket (&Instance->Mtftp6, Token, (UINT16) Len, Packet); + + if (EFI_ERROR (Status)) { + // + // Free the received packet before send new packet in ReceiveNotify, + // since the Udp6Io might need to be reconfigured. + // + NetbufFree (*UdpPacket); + *UdpPacket = NULL; + // + // Send the Mtftp6 error message if user aborted the current session. + // + Mtftp6SendError ( + Instance, + EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION, + (UINT8 *) "User aborted download" + ); + + return EFI_ABORTED; + } + } + + if (Token->Buffer != NULL) { + + Start = MultU64x32 (BlockCounter - 1, Instance->BlkSize); + if (Start + DataLen <= Token->BufferSize) { + CopyMem ((UINT8 *) Token->Buffer + Start, Packet->Data.Data, DataLen); + // + // Update the file size when received the last block + // + if ((Instance->LastBlk == Block) && Completed) { + Token->BufferSize = Start + DataLen; + } + } else if (Instance->LastBlk != 0) { + // + // Don't save the data if the buffer is too small, return + // EFI_BUFFER_TOO_SMALL if received the last packet. This + // will give a accurate file length. + // + Token->BufferSize = Start + DataLen; + + // + // Free the received packet before send new packet in ReceiveNotify, + // since the udpio might need to be reconfigured. + // + NetbufFree (*UdpPacket); + *UdpPacket = NULL; + // + // Send the Mtftp6 error message if no enough buffer. + // + Mtftp6SendError ( + Instance, + EFI_MTFTP6_ERRORCODE_DISK_FULL, + (UINT8 *) "User provided memory block is too small" + ); + + return EFI_BUFFER_TOO_SMALL; + } + } + + return EFI_SUCCESS; +} + + +/** + Process the received data packets. It will save the block + then send back an ACK if it is active. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Packet The pointer to the received packet. + @param[in] Len The length of the packet. + @param[out] UdpPacket The net buf of received packet. + @param[out] IsCompleted If TRUE, the download has been completed. + Otherwise, the download has not been completed. + + @retval EFI_SUCCESS The data packet was successfully processed. + @retval EFI_ABORTED The download was aborted by the user. + @retval EFI_BUFFER_TOO_SMALL The user-provided buffer is too small. + +**/ +EFI_STATUS +Mtftp6RrqHandleData ( + IN MTFTP6_INSTANCE *Instance, + IN EFI_MTFTP6_PACKET *Packet, + IN UINT32 Len, + OUT NET_BUF **UdpPacket, + OUT BOOLEAN *IsCompleted + ) +{ + EFI_STATUS Status; + UINT16 BlockNum; + INTN Expected; + + *IsCompleted = FALSE; + Status = EFI_SUCCESS; + BlockNum = NTOHS (Packet->Data.Block); + Expected = Mtftp6GetNextBlockNum (&Instance->BlkList); + + ASSERT (Expected >= 0); + + // + // If we are active (Master) and received an unexpected packet, transmit + // the ACK for the block we received, then restart receiving the + // expected one. If we are passive (Slave), save the block. + // + if (Instance->IsMaster && (Expected != BlockNum)) { + // + // Free the received packet before send new packet in ReceiveNotify, + // since the udpio might need to be reconfigured. + // + NetbufFree (*UdpPacket); + *UdpPacket = NULL; + + // + // If Expected is 0, (UINT16) (Expected - 1) is also the expected Ack number (65535). + // + return Mtftp6RrqSendAck (Instance, (UINT16) (Expected - 1)); + } + + Status = Mtftp6RrqSaveBlock (Instance, Packet, Len, UdpPacket); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Record the total received and saved block number. + // + Instance->TotalBlock ++; + + // + // Reset the passive client's timer whenever it received a valid data packet. + // + if (!Instance->IsMaster) { + Instance->PacketToLive = Instance->Timeout * 2; + } + + // + // Check whether we have received all the blocks. Send the ACK if we + // are active (unicast client or master client for multicast download). + // If we have received all the blocks, send an ACK even if we are passive + // to tell the server that we are done. + // + Expected = Mtftp6GetNextBlockNum (&Instance->BlkList); + + if (Instance->IsMaster || Expected < 0) { + if (Expected < 0) { + // + // If we are passive client, then the just received Block maybe + // isn't the last block. We need to send an ACK to the last block + // to inform the server that we are done. If we are active client, + // the Block == Instance->LastBlock. + // + BlockNum = Instance->LastBlk; + *IsCompleted = TRUE; + + } else { + BlockNum = (UINT16) (Expected - 1); + } + // + // Free the received packet before send new packet in ReceiveNotify, + // since the udpio might need to be reconfigured. + // + NetbufFree (*UdpPacket); + *UdpPacket = NULL; + + if (Instance->WindowSize == (Instance->TotalBlock - Instance->AckedBlock) || Expected < 0) { + Status = Mtftp6RrqSendAck (Instance, BlockNum); + } + } + + return Status; +} + + +/** + Validate whether the options received in the server's OACK packet is valid. + The options are valid only if: + 1. The server doesn't include options not requested by us. + 2. The server can only use smaller blksize than that is requested. + 3. The server can only use the same timeout as requested. + 4. The server doesn't change its multicast channel. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] ReplyInfo The pointer to options information in reply packet. + @param[in] RequestInfo The pointer to requested options info. + + @retval TRUE If the option in the OACK is valid. + @retval FALSE If the option is invalid. + +**/ +BOOLEAN +Mtftp6RrqOackValid ( + IN MTFTP6_INSTANCE *Instance, + IN MTFTP6_EXT_OPTION_INFO *ReplyInfo, + IN MTFTP6_EXT_OPTION_INFO *RequestInfo + ) +{ + // + // It is invalid for server to return options we don't request + // + if ((ReplyInfo->BitMap & ~RequestInfo->BitMap) != 0) { + return FALSE; + } + + // + // Server can only specify a smaller block size and windowsize to be used and + // return the timeout matches that requested. + // + if ((((ReplyInfo->BitMap & MTFTP6_OPT_BLKSIZE_BIT) != 0) && (ReplyInfo->BlkSize > RequestInfo->BlkSize)) || + (((ReplyInfo->BitMap & MTFTP6_OPT_WINDOWSIZE_BIT) != 0) && (ReplyInfo->BlkSize > RequestInfo->BlkSize)) || + (((ReplyInfo->BitMap & MTFTP6_OPT_TIMEOUT_BIT) != 0) && (ReplyInfo->Timeout != RequestInfo->Timeout)) + ) { + return FALSE; + } + + // + // The server can send ",,master" to client to change its master + // setting. But if it use the specific multicast channel, it can't + // change the setting. + // + if (((ReplyInfo->BitMap & MTFTP6_OPT_MCAST_BIT) != 0) && !NetIp6IsUnspecifiedAddr (&Instance->McastIp)) { + + if (!NetIp6IsUnspecifiedAddr (&ReplyInfo->McastIp) && CompareMem ( + &ReplyInfo->McastIp, + &Instance->McastIp, + sizeof (EFI_IPv6_ADDRESS) + ) != 0) { + return FALSE; + } + + if ((ReplyInfo->McastPort != 0) && (ReplyInfo->McastPort != Instance->McastPort)) { + return FALSE; + } + } + + return TRUE; +} + + +/** + Configure Udp6Io to receive a packet from a multicast address. + + @param[in] McastIo The pointer to the mcast Udp6Io. + @param[in] Context The pointer to the context. + + @retval EFI_SUCCESS The mcast Udp6Io was successfully configured. + @retval Others Failed to configure the Udp6Io. + +**/ +EFI_STATUS +EFIAPI +Mtftp6RrqConfigMcastUdpIo ( + IN UDP_IO *McastIo, + IN VOID *Context + ) +{ + EFI_STATUS Status; + EFI_UDP6_PROTOCOL *Udp6; + EFI_UDP6_CONFIG_DATA *Udp6Cfg; + EFI_IPv6_ADDRESS Group; + MTFTP6_INSTANCE *Instance; + + Udp6 = McastIo->Protocol.Udp6; + Udp6Cfg = &(McastIo->Config.Udp6); + Instance = (MTFTP6_INSTANCE *) Context; + + // + // Set the configure data for the mcast Udp6Io. + // + ZeroMem (Udp6Cfg, sizeof (EFI_UDP6_CONFIG_DATA)); + + Udp6Cfg->AcceptPromiscuous = FALSE; + Udp6Cfg->AcceptAnyPort = FALSE; + Udp6Cfg->AllowDuplicatePort = FALSE; + Udp6Cfg->TrafficClass = 0; + Udp6Cfg->HopLimit = 128; + Udp6Cfg->ReceiveTimeout = 0; + Udp6Cfg->TransmitTimeout = 0; + Udp6Cfg->StationPort = Instance->McastPort; + Udp6Cfg->RemotePort = 0; + + CopyMem ( + &Udp6Cfg->RemoteAddress, + &Instance->ServerIp, + sizeof (EFI_IPv6_ADDRESS) + ); + + // + // Configure the mcast Udp6Io. + // + Status = Udp6->Configure (Udp6, Udp6Cfg); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Join the multicast group + // + CopyMem (&Group, &Instance->McastIp, sizeof (EFI_IPv6_ADDRESS)); + + return Udp6->Groups (Udp6, TRUE, &Group); +} + + +/** + Process the OACK packet for Rrq. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Packet The pointer to the received packet. + @param[in] Len The length of the packet. + @param[out] UdpPacket The net buf of received packet. + @param[out] IsCompleted If TRUE, the download has been completed. + Otherwise, the download has not been completed. + + @retval EFI_DEVICE_ERROR Failed to create/start a multicast Udp6 child. + @retval EFI_TFTP_ERROR An error happened during the process. + @retval EFI_SUCCESS The OACK packet successfully processed. + +**/ +EFI_STATUS +Mtftp6RrqHandleOack ( + IN MTFTP6_INSTANCE *Instance, + IN EFI_MTFTP6_PACKET *Packet, + IN UINT32 Len, + OUT NET_BUF **UdpPacket, + OUT BOOLEAN *IsCompleted + ) +{ + EFI_MTFTP6_OPTION *Options; + UINT32 Count; + MTFTP6_EXT_OPTION_INFO ExtInfo; + EFI_STATUS Status; + INTN Expected; + EFI_UDP6_PROTOCOL *Udp6; + + *IsCompleted = FALSE; + Options = NULL; + + // + // If already started the master download, don't change the + // setting. Master download always succeeds. + // + Expected = Mtftp6GetNextBlockNum (&Instance->BlkList); + ASSERT (Expected != -1); + + if (Instance->IsMaster && Expected != 1) { + return EFI_SUCCESS; + } + + ZeroMem (&ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO)); + + // + // Parse the options in the packet. + // + Status = Mtftp6ParseStart (Packet, Len, &Count, &Options); + + if (EFI_ERROR (Status)) { + return Status; + } + ASSERT (Options != NULL); + + // + // Parse the extensive options in the packet. + // + Status = Mtftp6ParseExtensionOption (Options, Count, FALSE, Instance->Operation, &ExtInfo); + + if (EFI_ERROR (Status) || !Mtftp6RrqOackValid (Instance, &ExtInfo, &Instance->ExtInfo)) { + // + // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES. + // + if (Status != EFI_OUT_OF_RESOURCES) { + // + // Free the received packet before send new packet in ReceiveNotify, + // since the udpio might need to be reconfigured. + // + NetbufFree (*UdpPacket); + *UdpPacket = NULL; + // + // Send the Mtftp6 error message if invalid packet. + // + Mtftp6SendError ( + Instance, + EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION, + (UINT8 *) "Mal-formated OACK packet" + ); + } + + return EFI_TFTP_ERROR; + } + + if ((ExtInfo.BitMap & MTFTP6_OPT_MCAST_BIT) != 0) { + + // + // Save the multicast info. Always update the Master, only update the + // multicast IP address, block size, window size, timeoute at the first time. If IP + // address is updated, create a UDP child to receive the multicast. + // + Instance->IsMaster = ExtInfo.IsMaster; + + if (NetIp6IsUnspecifiedAddr (&Instance->McastIp)) { + if (NetIp6IsUnspecifiedAddr (&ExtInfo.McastIp) || ExtInfo.McastPort == 0) { + // + // Free the received packet before send new packet in ReceiveNotify, + // since the udpio might need to be reconfigured. + // + NetbufFree (*UdpPacket); + *UdpPacket = NULL; + // + // Send the Mtftp6 error message if invalid multi-cast setting. + // + Mtftp6SendError ( + Instance, + EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION, + (UINT8 *) "Illegal multicast setting" + ); + + return EFI_TFTP_ERROR; + } + + // + // Create a UDP child then start receive the multicast from it. + // + CopyMem ( + &Instance->McastIp, + &ExtInfo.McastIp, + sizeof (EFI_IP_ADDRESS) + ); + + Instance->McastPort = ExtInfo.McastPort; + if (Instance->McastUdpIo == NULL) { + Instance->McastUdpIo = UdpIoCreateIo ( + Instance->Service->Controller, + Instance->Service->Image, + Mtftp6RrqConfigMcastUdpIo, + UDP_IO_UDP6_VERSION, + Instance + ); + if (Instance->McastUdpIo != NULL) { + Status = gBS->OpenProtocol ( + Instance->McastUdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + (VOID **) &Udp6, + Instance->Service->Image, + Instance->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + UdpIoFreeIo (Instance->McastUdpIo); + Instance->McastUdpIo = NULL; + return EFI_DEVICE_ERROR; + } + } + } + + if (Instance->McastUdpIo == NULL) { + return EFI_DEVICE_ERROR; + } + + Status = UdpIoRecvDatagram ( + Instance->McastUdpIo, + Mtftp6RrqInput, + Instance, + 0 + ); + + if (EFI_ERROR (Status)) { + // + // Free the received packet before send new packet in ReceiveNotify, + // since the udpio might need to be reconfigured. + // + NetbufFree (*UdpPacket); + *UdpPacket = NULL; + // + // Send the Mtftp6 error message if failed to create Udp6Io to receive. + // + Mtftp6SendError ( + Instance, + EFI_MTFTP6_ERRORCODE_ACCESS_VIOLATION, + (UINT8 *) "Failed to create socket to receive multicast packet" + ); + + return Status; + } + + // + // Update the parameters used. + // + if (ExtInfo.BlkSize != 0) { + Instance->BlkSize = ExtInfo.BlkSize; + } + + if (ExtInfo.WindowSize != 0) { + Instance->WindowSize = ExtInfo.WindowSize; + } + + if (ExtInfo.Timeout != 0) { + Instance->Timeout = ExtInfo.Timeout; + } + } + + } else { + + Instance->IsMaster = TRUE; + + if (ExtInfo.BlkSize != 0) { + Instance->BlkSize = ExtInfo.BlkSize; + } + + if (ExtInfo.WindowSize != 0) { + Instance->WindowSize = ExtInfo.WindowSize; + } + + if (ExtInfo.Timeout != 0) { + Instance->Timeout = ExtInfo.Timeout; + } + } + + // + // Free the received packet before send new packet in ReceiveNotify, + // since the udpio might need to be reconfigured. + // + NetbufFree (*UdpPacket); + *UdpPacket = NULL; + // + // Send an ACK to (Expected - 1) which is 0 for unicast download, + // or tell the server we want to receive the Expected block. + // + return Mtftp6RrqSendAck (Instance, (UINT16) (Expected - 1)); +} + + +/** + The packet process callback for Mtftp6 download. + + @param[in] UdpPacket The pointer to the packet received. + @param[in] UdpEpt The pointer to the Udp6 access point. + @param[in] IoStatus The status from Udp6 instance. + @param[in] Context The pointer to the context. + +**/ +VOID +EFIAPI +Mtftp6RrqInput ( + IN NET_BUF *UdpPacket, + IN UDP_END_POINT *UdpEpt, + IN EFI_STATUS IoStatus, + IN VOID *Context + ) +{ + MTFTP6_INSTANCE *Instance; + EFI_MTFTP6_PACKET *Packet; + BOOLEAN IsCompleted; + BOOLEAN IsMcast; + EFI_STATUS Status; + UINT16 Opcode; + UINT32 TotalNum; + UINT32 Len; + + Instance = (MTFTP6_INSTANCE *) Context; + + NET_CHECK_SIGNATURE (Instance, MTFTP6_INSTANCE_SIGNATURE); + + Status = EFI_SUCCESS; + Packet = NULL; + IsCompleted = FALSE; + IsMcast = FALSE; + TotalNum = 0; + + // + // Return error status if Udp6 instance failed to receive. + // + if (EFI_ERROR (IoStatus)) { + Status = IoStatus; + goto ON_EXIT; + } + + ASSERT (UdpPacket != NULL); + + if (UdpPacket->TotalSize < MTFTP6_OPCODE_LEN) { + goto ON_EXIT; + } + + // + // Find the port this packet is from to restart receive correctly. + // + if (CompareMem ( + Ip6Swap128 (&UdpEpt->LocalAddr.v6), + &Instance->McastIp, + sizeof (EFI_IPv6_ADDRESS) + ) == 0) { + IsMcast = TRUE; + } else { + IsMcast = FALSE; + } + + // + // Client send initial request to server's listening port. Server + // will select a UDP port to communicate with the client. The server + // is required to use the same port as RemotePort to multicast the + // data. + // + if (UdpEpt->RemotePort != Instance->ServerDataPort) { + if (Instance->ServerDataPort != 0) { + goto ON_EXIT; + } else { + // + // For the subsequent exchange of requests, reconfigure the udpio as + // (serverip, serverport, localip, localport). + // Ususally, the client set serverport as 0 to receive and reset it + // once the first packet arrives to send ack. + // + Instance->ServerDataPort = UdpEpt->RemotePort; + } + } + + // + // Copy the MTFTP packet to a continuous buffer if it isn't already so. + // + Len = UdpPacket->TotalSize; + TotalNum = UdpPacket->BlockOpNum; + + if (TotalNum > 1) { + Packet = AllocateZeroPool (Len); + + if (Packet == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet); + + } else { + Packet = (EFI_MTFTP6_PACKET *) NetbufGetByte (UdpPacket, 0, NULL); + ASSERT (Packet != NULL); + } + + Opcode = NTOHS (Packet->OpCode); + + // + // Callback to the user's CheckPacket if provided. Abort the transmission + // if CheckPacket returns an EFI_ERROR code. + // + if ((Instance->Token->CheckPacket != NULL) && + (Opcode == EFI_MTFTP6_OPCODE_OACK || Opcode == EFI_MTFTP6_OPCODE_ERROR) + ) { + + Status = Instance->Token->CheckPacket ( + &Instance->Mtftp6, + Instance->Token, + (UINT16) Len, + Packet + ); + + if (EFI_ERROR (Status)) { + // + // Send an error message to the server to inform it + // + if (Opcode != EFI_MTFTP6_OPCODE_ERROR) { + // + // Free the received packet before send new packet in ReceiveNotify, + // since the udpio might need to be reconfigured. + // + NetbufFree (UdpPacket); + UdpPacket = NULL; + // + // Send the Mtftp6 error message if user aborted the current session. + // + Mtftp6SendError ( + Instance, + EFI_MTFTP6_ERRORCODE_REQUEST_DENIED, + (UINT8 *) "User aborted the transfer" + ); + } + + Status = EFI_ABORTED; + goto ON_EXIT; + } + } + + // + // Switch the process routines by the operation code. + // + switch (Opcode) { + case EFI_MTFTP6_OPCODE_DATA: + if ((Len > (UINT32) (MTFTP6_DATA_HEAD_LEN + Instance->BlkSize)) || (Len < (UINT32) MTFTP6_DATA_HEAD_LEN)) { + goto ON_EXIT; + } + // + // Handle the data packet of Rrq. + // + Status = Mtftp6RrqHandleData ( + Instance, + Packet, + Len, + &UdpPacket, + &IsCompleted + ); + break; + + case EFI_MTFTP6_OPCODE_OACK: + if (IsMcast || Len <= MTFTP6_OPCODE_LEN) { + goto ON_EXIT; + } + // + // Handle the Oack packet of Rrq. + // + Status = Mtftp6RrqHandleOack ( + Instance, + Packet, + Len, + &UdpPacket, + &IsCompleted + ); + break; + + default: + // + // Drop and return eror if received error message. + // + Status = EFI_TFTP_ERROR; + break; + } + +ON_EXIT: + // + // Free the resources, then if !EFI_ERROR (Status), restart the + // receive, otherwise end the session. + // + if (Packet != NULL && TotalNum > 1) { + FreePool (Packet); + } + if (UdpPacket != NULL) { + NetbufFree (UdpPacket); + } + if (!EFI_ERROR (Status) && !IsCompleted) { + if (IsMcast) { + Status = UdpIoRecvDatagram ( + Instance->McastUdpIo, + Mtftp6RrqInput, + Instance, + 0 + ); + } else { + Status = UdpIoRecvDatagram ( + Instance->UdpIo, + Mtftp6RrqInput, + Instance, + 0 + ); + } + } + // + // Clean up the current session if failed to continue. + // + if (EFI_ERROR (Status) || IsCompleted) { + Mtftp6OperationClean (Instance, Status); + } +} + + +/** + Start the Mtftp6 instance to download. It first initializes some + of the internal states, then builds and sends an RRQ reqeuest packet. + Finally, it starts receive for the downloading. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Operation The operation code of current packet. + + @retval EFI_SUCCESS The Mtftp6 is started to download. + @retval Others Failed to start to download. + +**/ +EFI_STATUS +Mtftp6RrqStart ( + IN MTFTP6_INSTANCE *Instance, + IN UINT16 Operation + ) +{ + EFI_STATUS Status; + + // + // The valid block number range are [1, 0xffff]. For example: + // the client sends an RRQ request to the server, the server + // transfers the DATA1 block. If option negoitation is ongoing, + // the server will send back an OACK, then client will send ACK0. + // + Status = Mtftp6InitBlockRange (&Instance->BlkList, 1, 0xffff); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status = Mtftp6SendRequest (Instance, Operation); + + if (EFI_ERROR (Status)) { + return Status; + } + + return UdpIoRecvDatagram ( + Instance->UdpIo, + Mtftp6RrqInput, + Instance, + 0 + ); +} + diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c b/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c new file mode 100644 index 000000000..5b10da52c --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Support.c @@ -0,0 +1,1240 @@ +/** @file + Mtftp6 support functions implementation. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp6Impl.h" + + +/** + Allocate a MTFTP block range, then init it to the range of [Start, End]. + + @param[in] Start The start block number. + @param[in] End The last block number in the range. + + @return Range The range of the allocated block buffer. + +**/ +MTFTP6_BLOCK_RANGE * +Mtftp6AllocateRange ( + IN UINT16 Start, + IN UINT16 End + ) +{ + MTFTP6_BLOCK_RANGE *Range; + + Range = AllocateZeroPool (sizeof (MTFTP6_BLOCK_RANGE)); + + if (Range == NULL) { + return NULL; + } + + InitializeListHead (&Range->Link); + Range->Start = Start; + Range->End = End; + Range->Bound = End; + + return Range; +} + + +/** + Initialize the block range for either RRQ or WRQ. RRQ and WRQ have + different requirements for Start and End. For example, during startup, + WRQ initializes its whole valid block range to [0, 0xffff]. This + is bacause the server will send an ACK0 to inform the user to start the + upload. When the client receives an ACK0, it will remove 0 from the range, + get the next block number, which is 1, then upload the BLOCK1. For RRQ + without option negotiation, the server will directly send the BLOCK1 + in response to the client's RRQ. When received BLOCK1, the client will + remove it from the block range and send an ACK. It also works if there + is option negotiation. + + @param[in] Head The block range head to initialize. + @param[in] Start The Start block number. + @param[in] End The last block number. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range. + @retval EFI_SUCCESS The initial block range is created. + +**/ +EFI_STATUS +Mtftp6InitBlockRange ( + IN LIST_ENTRY *Head, + IN UINT16 Start, + IN UINT16 End + ) +{ + MTFTP6_BLOCK_RANGE *Range; + + Range = Mtftp6AllocateRange (Start, End); + + if (Range == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + InsertTailList (Head, &Range->Link); + return EFI_SUCCESS; +} + + +/** + Get the first valid block number on the range list. + + @param[in] Head The block range head. + + @retval ==-1 If the block range is empty. + @retval >-1 The first valid block number. + +**/ +INTN +Mtftp6GetNextBlockNum ( + IN LIST_ENTRY *Head + ) +{ + MTFTP6_BLOCK_RANGE *Range; + + if (IsListEmpty (Head)) { + return -1; + } + + Range = NET_LIST_HEAD (Head, MTFTP6_BLOCK_RANGE, Link); + return Range->Start; +} + + +/** + Set the last block number of the block range list. It + removes all the blocks after the Last. MTFTP initialize the + block range to the maximum possible range, such as [0, 0xffff] + for WRQ. When it gets the last block number, it calls + this function to set the last block number. + + @param[in] Head The block range list. + @param[in] Last The last block number. + +**/ +VOID +Mtftp6SetLastBlockNum ( + IN LIST_ENTRY *Head, + IN UINT16 Last + ) +{ + MTFTP6_BLOCK_RANGE *Range; + + // + // Iterate from the tail to head to remove the block number + // after the last. + // + while (!IsListEmpty (Head)) { + Range = NET_LIST_TAIL (Head, MTFTP6_BLOCK_RANGE, Link); + + if (Range->Start > Last) { + RemoveEntryList (&Range->Link); + FreePool (Range); + continue; + } + + if (Range->End > Last) { + Range->End = Last; + } + return ; + } +} + + +/** + Remove the block number from the block range list. + + @param[in] Head The block range list to remove from. + @param[in] Num The block number to remove. + @param[in] Completed Whether Num is the last block number. + @param[out] BlockCounter The continuous block counter instead of the value after roll-over. + + @retval EFI_NOT_FOUND The block number isn't in the block range list. + @retval EFI_SUCCESS The block number has been removed from the list. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + +**/ +EFI_STATUS +Mtftp6RemoveBlockNum ( + IN LIST_ENTRY *Head, + IN UINT16 Num, + IN BOOLEAN Completed, + OUT UINT64 *BlockCounter + ) +{ + MTFTP6_BLOCK_RANGE *Range; + MTFTP6_BLOCK_RANGE *NewRange; + LIST_ENTRY *Entry; + + NET_LIST_FOR_EACH (Entry, Head) { + + // + // Each block represents a hole [Start, End] in the file, + // skip to the first range with End >= Num + // + Range = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link); + + if (Range->End < Num) { + continue; + } + + // + // There are three different cases for Start + // 1. (Start > Num) && (End >= Num): + // because all the holes before this one has the condition of + // End < Num, so this block number has been removed. + // + // 2. (Start == Num) && (End >= Num): + // Need to increase the Start by one, and if End == Num, this + // hole has been removed completely, remove it. + // + // 3. (Start < Num) && (End >= Num): + // if End == Num, only need to decrease the End by one because + // we have (Start < Num) && (Num == End), so (Start <= End - 1). + // if (End > Num), the hold is splited into two holes, with + // [Start, Num - 1] and [Num + 1, End]. + // + if (Range->Start > Num) { + return EFI_NOT_FOUND; + + } else if (Range->Start == Num) { + Range->Start++; + + // + // Note that: RFC 1350 does not mention block counter roll-over, + // but several TFTP hosts implement the roll-over be able to accept + // transfers of unlimited size. There is no consensus, however, whether + // the counter should wrap around to zero or to one. Many implementations + // wrap to zero, because this is the simplest to implement. Here we choose + // this solution. + // + *BlockCounter = Num; + + if (Range->Round > 0) { + *BlockCounter += Range->Bound + MultU64x32 (Range->Round - 1, (UINT32)(Range->Bound + 1)) + 1; + } + + if (Range->Start > Range->Bound) { + Range->Start = 0; + Range->Round ++; + } + + if ((Range->Start > Range->End) || Completed) { + RemoveEntryList (&Range->Link); + FreePool (Range); + } + + return EFI_SUCCESS; + + } else { + if (Range->End == Num) { + Range->End--; + } else { + NewRange = Mtftp6AllocateRange ((UINT16) (Num + 1), (UINT16) Range->End); + + if (NewRange == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Range->End = Num - 1; + NetListInsertAfter (&Range->Link, &NewRange->Link); + } + + return EFI_SUCCESS; + } + } + + return EFI_NOT_FOUND; +} + + +/** + Configure the opened Udp6 instance until the corresponding Ip6 instance + has been configured. + + @param[in] UdpIo The pointer to the Udp6 Io. + @param[in] UdpCfgData The pointer to the Udp6 configure data. + + @retval EFI_SUCCESS Configure the Udp6 instance successfully. + @retval EFI_NO_MAPPING The corresponding Ip6 instance has not + been configured yet. + +**/ +EFI_STATUS +Mtftp6GetMapping ( + IN UDP_IO *UdpIo, + IN EFI_UDP6_CONFIG_DATA *UdpCfgData + ) +{ + EFI_IP6_MODE_DATA Ip6Mode; + EFI_UDP6_PROTOCOL *Udp6; + EFI_STATUS Status; + EFI_EVENT Event; + + Event = NULL; + Udp6 = UdpIo->Protocol.Udp6; + + // + // Create a timer to check whether the Ip6 instance configured or not. + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &Event + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = gBS->SetTimer ( + Event, + TimerRelative, + MTFTP6_GET_MAPPING_TIMEOUT * MTFTP6_TICK_PER_SECOND + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Check the Ip6 mode data till timeout. + // + while (EFI_ERROR (gBS->CheckEvent (Event))) { + + Udp6->Poll (Udp6); + + Status = Udp6->GetModeData (Udp6, NULL, &Ip6Mode, NULL, NULL); + + if (!EFI_ERROR (Status)) { + if (Ip6Mode.AddressList != NULL) { + FreePool (Ip6Mode.AddressList); + } + + if (Ip6Mode.GroupTable != NULL) { + FreePool (Ip6Mode.GroupTable); + } + + if (Ip6Mode.RouteTable != NULL) { + FreePool (Ip6Mode.RouteTable); + } + + if (Ip6Mode.NeighborCache != NULL) { + FreePool (Ip6Mode.NeighborCache); + } + + if (Ip6Mode.PrefixTable != NULL) { + FreePool (Ip6Mode.PrefixTable); + } + + if (Ip6Mode.IcmpTypeList != NULL) { + FreePool (Ip6Mode.IcmpTypeList); + } + + if (Ip6Mode.IsConfigured) { + // + // Continue to configure the Udp6 instance. + // + Status = Udp6->Configure (Udp6, UdpCfgData); + } else { + Status = EFI_NO_MAPPING; + } + } + } + +ON_EXIT: + + if (Event != NULL) { + gBS->CloseEvent (Event); + } + + return Status; +} + + +/** + The dummy configure routine for create a new Udp6 Io. + + @param[in] UdpIo The pointer to the Udp6 Io. + @param[in] Context The pointer to the context. + + @retval EFI_SUCCESS This value is always returned. + +**/ +EFI_STATUS +EFIAPI +Mtftp6ConfigDummyUdpIo ( + IN UDP_IO *UdpIo, + IN VOID *Context + ) +{ + return EFI_SUCCESS; +} + + +/** + The configure routine for Mtftp6 instance to transmit/receive. + + @param[in] UdpIo The pointer to the Udp6 Io. + @param[in] ServerIp The pointer to the server address. + @param[in] ServerPort The pointer to the server port. + @param[in] LocalIp The pointer to the local address. + @param[in] LocalPort The pointer to the local port. + + @retval EFI_SUCCESS Configured the Udp6 Io for Mtftp6 successfully. + @retval EFI_NO_MAPPING The corresponding Ip6 instance has not been + configured yet. + +**/ +EFI_STATUS +Mtftp6ConfigUdpIo ( + IN UDP_IO *UdpIo, + IN EFI_IPv6_ADDRESS *ServerIp, + IN UINT16 ServerPort, + IN EFI_IPv6_ADDRESS *LocalIp, + IN UINT16 LocalPort + ) +{ + EFI_STATUS Status; + EFI_UDP6_PROTOCOL *Udp6; + EFI_UDP6_CONFIG_DATA *Udp6Cfg; + + Udp6 = UdpIo->Protocol.Udp6; + Udp6Cfg = &(UdpIo->Config.Udp6); + + ZeroMem (Udp6Cfg, sizeof (EFI_UDP6_CONFIG_DATA)); + + // + // Set the Udp6 Io configure data. + // + Udp6Cfg->AcceptPromiscuous = FALSE; + Udp6Cfg->AcceptAnyPort = FALSE; + Udp6Cfg->AllowDuplicatePort = FALSE; + Udp6Cfg->TrafficClass = 0; + Udp6Cfg->HopLimit = 128; + Udp6Cfg->ReceiveTimeout = 0; + Udp6Cfg->TransmitTimeout = 0; + Udp6Cfg->StationPort = LocalPort; + Udp6Cfg->RemotePort = ServerPort; + + CopyMem ( + &Udp6Cfg->StationAddress, + LocalIp, + sizeof (EFI_IPv6_ADDRESS) + ); + + CopyMem ( + &Udp6Cfg->RemoteAddress, + ServerIp, + sizeof (EFI_IPv6_ADDRESS) + ); + + // + // Configure the Udp6 instance with current configure data. + // + Status = Udp6->Configure (Udp6, Udp6Cfg); + + if (Status == EFI_NO_MAPPING) { + + return Mtftp6GetMapping (UdpIo, Udp6Cfg); + } + + return Status; +} + + +/** + Build and transmit the request packet for the Mtftp6 instance. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Operation The operation code of this packet. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request. + @retval EFI_SUCCESS The request is built and sent. + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp6SendRequest ( + IN MTFTP6_INSTANCE *Instance, + IN UINT16 Operation + ) +{ + EFI_MTFTP6_PACKET *Packet; + EFI_MTFTP6_OPTION *Options; + EFI_MTFTP6_TOKEN *Token; + RETURN_STATUS Status; + NET_BUF *Nbuf; + UINT8 *Mode; + UINT8 *Cur; + UINTN Index; + UINT32 BufferLength; + UINTN FileNameLength; + UINTN ModeLength; + UINTN OptionStrLength; + UINTN ValueStrLength; + + Token = Instance->Token; + Options = Token->OptionList; + Mode = Token->ModeStr; + + if (Mode == NULL) { + Mode = (UINT8 *) "octet"; + } + + // + // The header format of RRQ/WRQ packet is: + // + // 2 bytes string 1 byte string 1 byte + // ------------------------------------------------ + // | Opcode | Filename | 0 | Mode | 0 | + // ------------------------------------------------ + // + // The common option format is: + // + // string 1 byte string 1 byte + // --------------------------------------- + // | OptionStr | 0 | ValueStr | 0 | + // --------------------------------------- + // + + // + // Compute the size of new Mtftp6 packet. + // + FileNameLength = AsciiStrLen ((CHAR8 *) Token->Filename); + ModeLength = AsciiStrLen ((CHAR8 *) Mode); + BufferLength = (UINT32) FileNameLength + (UINT32) ModeLength + 4; + + for (Index = 0; Index < Token->OptionCount; Index++) { + OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr); + ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr); + BufferLength += (UINT32) OptionStrLength + (UINT32) ValueStrLength + 2; + } + + // + // Allocate a packet then copy the data. + // + if ((Nbuf = NetbufAlloc (BufferLength)) == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Copy the opcode, filename and mode into packet. + // + Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, BufferLength, FALSE); + ASSERT (Packet != NULL); + + Packet->OpCode = HTONS (Operation); + BufferLength -= sizeof (Packet->OpCode); + + Cur = Packet->Rrq.Filename; + Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Token->Filename); + ASSERT_EFI_ERROR (Status); + BufferLength -= (UINT32) (FileNameLength + 1); + Cur += FileNameLength + 1; + Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Mode); + ASSERT_EFI_ERROR (Status); + BufferLength -= (UINT32) (ModeLength + 1); + Cur += ModeLength + 1; + + // + // Copy all the extension options into the packet. + // + for (Index = 0; Index < Token->OptionCount; ++Index) { + OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr); + ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr); + + Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].OptionStr); + ASSERT_EFI_ERROR (Status); + BufferLength -= (UINT32) (OptionStrLength + 1); + Cur += OptionStrLength + 1; + + Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].ValueStr); + ASSERT_EFI_ERROR (Status); + BufferLength -= (UINT32) (ValueStrLength + 1); + Cur += ValueStrLength + 1; + + } + + // + // Save the packet buf for retransmit + // + if (Instance->LastPacket != NULL) { + NetbufFree (Instance->LastPacket); + } + + Instance->LastPacket = Nbuf; + Instance->CurRetry = 0; + + return Mtftp6TransmitPacket (Instance, Nbuf); +} + + +/** + Build and send an error packet. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] ErrCode The error code in the packet. + @param[in] ErrInfo The error message in the packet. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet. + @retval EFI_SUCCESS The error packet is transmitted. + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp6SendError ( + IN MTFTP6_INSTANCE *Instance, + IN UINT16 ErrCode, + IN UINT8* ErrInfo + ) +{ + NET_BUF *Nbuf; + EFI_MTFTP6_PACKET *TftpError; + UINT32 Len; + + // + // Allocate a packet then copy the data. + // + Len = (UINT32) (AsciiStrLen ((CHAR8 *) ErrInfo) + sizeof (EFI_MTFTP6_ERROR_HEADER)); + Nbuf = NetbufAlloc (Len); + + if (Nbuf == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + TftpError = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE); + + if (TftpError == NULL) { + NetbufFree (Nbuf); + return EFI_OUT_OF_RESOURCES; + } + + TftpError->OpCode = HTONS (EFI_MTFTP6_OPCODE_ERROR); + TftpError->Error.ErrorCode = HTONS (ErrCode); + + AsciiStrCpyS ((CHAR8 *) TftpError->Error.ErrorMessage, AsciiStrLen ((CHAR8 *) ErrInfo) + 1 , (CHAR8 *) ErrInfo); + + // + // Save the packet buf for retransmit + // + if (Instance->LastPacket != NULL) { + NetbufFree (Instance->LastPacket); + } + + Instance->LastPacket = Nbuf; + Instance->CurRetry = 0; + + return Mtftp6TransmitPacket (Instance, Nbuf); +} + + +/** + The callback function called when the packet is transmitted. + + @param[in] Packet The pointer to the packet. + @param[in] UdpEpt The pointer to the Udp6 access point. + @param[in] IoStatus The result of the transmission. + @param[in] Context The pointer to the context. + +**/ +VOID +EFIAPI +Mtftp6OnPacketSent ( + IN NET_BUF *Packet, + IN UDP_END_POINT *UdpEpt, + IN EFI_STATUS IoStatus, + IN VOID *Context + ) +{ + NetbufFree (Packet); + *(BOOLEAN *) Context = TRUE; +} + + +/** + Send the packet for the Mtftp6 instance. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Packet The pointer to the packet to be sent. + + @retval EFI_SUCCESS The packet was sent out + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp6TransmitPacket ( + IN MTFTP6_INSTANCE *Instance, + IN NET_BUF *Packet + ) +{ + EFI_UDP6_PROTOCOL *Udp6; + EFI_UDP6_CONFIG_DATA Udp6CfgData; + EFI_STATUS Status; + UINT16 *Temp; + UINT16 Value; + UINT16 OpCode; + + ZeroMem (&Udp6CfgData, sizeof(EFI_UDP6_CONFIG_DATA)); + Udp6 = Instance->UdpIo->Protocol.Udp6; + + // + // Set the live time of the packet. + // + Instance->PacketToLive = Instance->IsMaster ? Instance->Timeout : (Instance->Timeout * 2); + + Temp = (UINT16 *) NetbufGetByte (Packet, 0, NULL); + ASSERT (Temp != NULL); + + Value = *Temp; + OpCode = NTOHS (Value); + + if (OpCode == EFI_MTFTP6_OPCODE_RRQ || OpCode == EFI_MTFTP6_OPCODE_DIR || OpCode == EFI_MTFTP6_OPCODE_WRQ) { + // + // For the Rrq, Dir, Wrq requests of the operation, configure the Udp6Io as + // (serverip, 69, localip, localport) to send. + // Usually local address and local port are both default as zero. + // + Status = Udp6->Configure (Udp6, NULL); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status = Mtftp6ConfigUdpIo ( + Instance->UdpIo, + &Instance->ServerIp, + Instance->ServerCmdPort, + &Instance->Config->StationIp, + Instance->Config->LocalPort + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Get the current local address and port by get Udp6 mode data. + // + Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL); + if (EFI_ERROR (Status)) { + return Status; + } + + NET_GET_REF (Packet); + + Instance->IsTransmitted = FALSE; + + Status = UdpIoSendDatagram ( + Instance->UdpIo, + Packet, + NULL, + NULL, + Mtftp6OnPacketSent, + &Instance->IsTransmitted + ); + + if (EFI_ERROR (Status)) { + NET_PUT_REF (Packet); + return Status; + } + + // + // Poll till the packet sent out from the ip6 queue. + // + gBS->RestoreTPL (Instance->OldTpl); + + while (!Instance->IsTransmitted) { + Udp6->Poll (Udp6); + } + + Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // For the subsequent exchange of such requests, reconfigure the Udp6Io as + // (serverip, 0, localip, localport) to receive. + // Currently local address and local port are specified by Udp6 mode data. + // + Status = Udp6->Configure (Udp6, NULL); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status = Mtftp6ConfigUdpIo ( + Instance->UdpIo, + &Instance->ServerIp, + Instance->ServerDataPort, + &Udp6CfgData.StationAddress, + Udp6CfgData.StationPort + ); + } else { + // + // For the data exchange, configure the Udp6Io as (serverip, dataport, + // localip, localport) to send/receive. + // Currently local address and local port are specified by Udp6 mode data. + // + Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Udp6CfgData.RemotePort != Instance->ServerDataPort) { + + Status = Udp6->Configure (Udp6, NULL); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status = Mtftp6ConfigUdpIo ( + Instance->UdpIo, + &Instance->ServerIp, + Instance->ServerDataPort, + &Udp6CfgData.StationAddress, + Udp6CfgData.StationPort + ); + + if (EFI_ERROR (Status)) { + return Status; + } + } + + NET_GET_REF (Packet); + + Instance->IsTransmitted = FALSE; + + Status = UdpIoSendDatagram ( + Instance->UdpIo, + Packet, + NULL, + NULL, + Mtftp6OnPacketSent, + &Instance->IsTransmitted + ); + + if (EFI_ERROR (Status)) { + NET_PUT_REF (Packet); + } + + // + // Poll till the packet sent out from the ip6 queue. + // + gBS->RestoreTPL (Instance->OldTpl); + + while (!Instance->IsTransmitted) { + Udp6->Poll (Udp6); + } + + Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + } + + return Status; +} + + +/** + Check packet for GetInfo callback routine. + + GetInfo is implemented with EfiMtftp6ReadFile. It's used to inspect + the first packet from server, then abort the session. + + @param[in] This The pointer to the Mtftp6 protocol. + @param[in] Token The pointer to the Mtftp6 token. + @param[in] PacketLen The length of the packet. + @param[in] Packet The pointer to the received packet. + + @retval EFI_ABORTED Abort the Mtftp6 operation. + +**/ +EFI_STATUS +EFIAPI +Mtftp6CheckPacket ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_TOKEN *Token, + IN UINT16 PacketLen, + IN EFI_MTFTP6_PACKET *Packet + ) +{ + MTFTP6_GETINFO_CONTEXT *Context; + UINT16 OpCode; + + Context = (MTFTP6_GETINFO_CONTEXT *) Token->Context; + OpCode = NTOHS (Packet->OpCode); + + // + // Set the GetInfo's return status according to the OpCode. + // + switch (OpCode) { + case EFI_MTFTP6_OPCODE_ERROR: + Context->Status = EFI_TFTP_ERROR; + break; + + case EFI_MTFTP6_OPCODE_OACK: + Context->Status = EFI_SUCCESS; + break; + + default: + Context->Status = EFI_PROTOCOL_ERROR; + } + + // + // Allocate buffer then copy the packet over. Use gBS->AllocatePool + // in case NetAllocatePool will implements something tricky. + // + *(Context->Packet) = AllocateZeroPool (PacketLen); + + if (*(Context->Packet) == NULL) { + Context->Status = EFI_OUT_OF_RESOURCES; + return EFI_ABORTED; + } + + *(Context->PacketLen) = PacketLen; + CopyMem (*(Context->Packet), Packet, PacketLen); + + return EFI_ABORTED; +} + + +/** + Clean up the current Mtftp6 operation. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Result The result to be returned to the user. + +**/ +VOID +Mtftp6OperationClean ( + IN MTFTP6_INSTANCE *Instance, + IN EFI_STATUS Result + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + MTFTP6_BLOCK_RANGE *Block; + + // + // Clean up the current token and event. + // + if (Instance->Token != NULL) { + Instance->Token->Status = Result; + if (Instance->Token->Event != NULL) { + gBS->SignalEvent (Instance->Token->Event); + } + Instance->Token = NULL; + } + + // + // Clean up the corresponding Udp6Io. + // + if (Instance->UdpIo != NULL) { + UdpIoCleanIo (Instance->UdpIo); + } + + if (Instance->McastUdpIo != NULL) { + gBS->CloseProtocol ( + Instance->McastUdpIo->UdpHandle, + &gEfiUdp6ProtocolGuid, + Instance->McastUdpIo->Image, + Instance->Handle + ); + UdpIoFreeIo (Instance->McastUdpIo); + Instance->McastUdpIo = NULL; + } + + // + // Clean up the stored last packet. + // + if (Instance->LastPacket != NULL) { + NetbufFree (Instance->LastPacket); + Instance->LastPacket = NULL; + } + + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Instance->BlkList) { + Block = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link); + RemoveEntryList (Entry); + FreePool (Block); + } + + // + // Reinitialize the corresponding fields of the Mtftp6 operation. + // + ZeroMem (&Instance->ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO)); + ZeroMem (&Instance->ServerIp, sizeof (EFI_IPv6_ADDRESS)); + ZeroMem (&Instance->McastIp, sizeof (EFI_IPv6_ADDRESS)); + + Instance->ServerCmdPort = 0; + Instance->ServerDataPort = 0; + Instance->McastPort = 0; + Instance->BlkSize = 0; + Instance->Operation = 0; + Instance->WindowSize = 1; + Instance->TotalBlock = 0; + Instance->AckedBlock = 0; + Instance->LastBlk = 0; + Instance->PacketToLive = 0; + Instance->MaxRetry = 0; + Instance->CurRetry = 0; + Instance->Timeout = 0; + Instance->IsMaster = TRUE; +} + + +/** + Start the Mtftp6 instance to perform the operation, such as read file, + write file, and read directory. + + @param[in] This The MTFTP session. + @param[in] Token The token than encapsues the user's request. + @param[in] OpCode The operation to perform. + + @retval EFI_INVALID_PARAMETER Some of the parameters are invalid. + @retval EFI_NOT_STARTED The MTFTP session hasn't been configured. + @retval EFI_ALREADY_STARTED There is pending operation for the session. + @retval EFI_SUCCESS The operation is successfully started. + +**/ +EFI_STATUS +Mtftp6OperationStart ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_TOKEN *Token, + IN UINT16 OpCode + ) +{ + MTFTP6_INSTANCE *Instance; + EFI_STATUS Status; + + if (This == NULL || + Token == NULL || + Token->Filename == NULL || + (Token->OptionCount != 0 && Token->OptionList == NULL) || + (Token->OverrideData != NULL && !NetIp6IsValidUnicast (&Token->OverrideData->ServerIp)) + ) { + return EFI_INVALID_PARAMETER; + } + + // + // At least define one method to collect the data for download. + // + if ((OpCode == EFI_MTFTP6_OPCODE_RRQ || OpCode == EFI_MTFTP6_OPCODE_DIR) && + Token->Buffer == NULL && + Token->CheckPacket == NULL + ) { + return EFI_INVALID_PARAMETER; + } + + // + // At least define one method to provide the data for upload. + // + if (OpCode == EFI_MTFTP6_OPCODE_WRQ && Token->Buffer == NULL && Token->PacketNeeded == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = MTFTP6_INSTANCE_FROM_THIS (This); + + if (Instance->Config == NULL) { + return EFI_NOT_STARTED; + } + + if (Instance->Token != NULL) { + return EFI_ACCESS_DENIED; + } + + Status = EFI_SUCCESS; + Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance->Operation = OpCode; + + // + // Parse the extension options in the request packet. + // + if (Token->OptionCount != 0) { + + Status = Mtftp6ParseExtensionOption ( + Token->OptionList, + Token->OptionCount, + TRUE, + Instance->Operation, + &Instance->ExtInfo + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + } + + // + // Initialize runtime data from config data or override data. + // + Instance->Token = Token; + Instance->ServerCmdPort = Instance->Config->InitialServerPort; + Instance->ServerDataPort = 0; + Instance->MaxRetry = Instance->Config->TryCount; + Instance->Timeout = Instance->Config->TimeoutValue; + Instance->IsMaster = TRUE; + + CopyMem ( + &Instance->ServerIp, + &Instance->Config->ServerIp, + sizeof (EFI_IPv6_ADDRESS) + ); + + if (Token->OverrideData != NULL) { + Instance->ServerCmdPort = Token->OverrideData->ServerPort; + Instance->MaxRetry = Token->OverrideData->TryCount; + Instance->Timeout = Token->OverrideData->TimeoutValue; + + CopyMem ( + &Instance->ServerIp, + &Token->OverrideData->ServerIp, + sizeof (EFI_IPv6_ADDRESS) + ); + } + + // + // Set default value for undefined parameters. + // + if (Instance->ServerCmdPort == 0) { + Instance->ServerCmdPort = MTFTP6_DEFAULT_SERVER_CMD_PORT; + } + if (Instance->BlkSize == 0) { + Instance->BlkSize = MTFTP6_DEFAULT_BLK_SIZE; + } + if (Instance->WindowSize == 0) { + Instance->WindowSize = MTFTP6_DEFAULT_WINDOWSIZE; + } + if (Instance->MaxRetry == 0) { + Instance->MaxRetry = MTFTP6_DEFAULT_MAX_RETRY; + } + if (Instance->Timeout == 0) { + Instance->Timeout = MTFTP6_DEFAULT_TIMEOUT; + } + + Token->Status = EFI_NOT_READY; + + // + // Switch the routines by the operation code. + // + switch (OpCode) { + case EFI_MTFTP6_OPCODE_RRQ: + Status = Mtftp6RrqStart (Instance, OpCode); + break; + + case EFI_MTFTP6_OPCODE_DIR: + Status = Mtftp6RrqStart (Instance, OpCode); + break; + + case EFI_MTFTP6_OPCODE_WRQ: + Status = Mtftp6WrqStart (Instance, OpCode); + break; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_ERROR; + } + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Return immediately for asynchronous or poll the instance for synchronous. + // + gBS->RestoreTPL (Instance->OldTpl); + + if (Token->Event == NULL) { + while (Token->Status == EFI_NOT_READY) { + This->Poll (This); + } + return Token->Status; + } + + return EFI_SUCCESS; + +ON_ERROR: + + Mtftp6OperationClean (Instance, Status); + gBS->RestoreTPL (Instance->OldTpl); + + return Status; +} + + +/** + The timer ticking routine for the Mtftp6 instance. + + @param[in] Event The pointer to the ticking event. + @param[in] Context The pointer to the context. + +**/ +VOID +EFIAPI +Mtftp6OnTimerTick ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + MTFTP6_SERVICE *Service; + MTFTP6_INSTANCE *Instance; + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + EFI_MTFTP6_TOKEN *Token; + EFI_STATUS Status; + + Service = (MTFTP6_SERVICE *) Context; + + // + // Iterate through all the children of the Mtftp service instance. Time + // out the packet. If maximum retries reached, clean the session up. + // + NET_LIST_FOR_EACH_SAFE (Entry, Next, &Service->Children) { + + Instance = NET_LIST_USER_STRUCT (Entry, MTFTP6_INSTANCE, Link); + + if (Instance->Token == NULL) { + continue; + } + + if (Instance->PacketToLive > 0) { + Instance->PacketToLive--; + continue; + } + + Instance->CurRetry++; + Token = Instance->Token; + + if (Token->TimeoutCallback != NULL) { + // + // Call the timeout callback routine if has. + // + Status = Token->TimeoutCallback (&Instance->Mtftp6, Token); + + if (EFI_ERROR (Status)) { + Mtftp6SendError ( + Instance, + EFI_MTFTP6_ERRORCODE_REQUEST_DENIED, + (UINT8 *) "User aborted the transfer in time out" + ); + Mtftp6OperationClean (Instance, EFI_ABORTED); + continue; + } + } + + // + // Retransmit the packet if haven't reach the maxmium retry count, + // otherwise exit the transfer. + // + if (Instance->CurRetry < Instance->MaxRetry) { + Mtftp6TransmitPacket (Instance, Instance->LastPacket); + } else { + Mtftp6OperationClean (Instance, EFI_TIMEOUT); + continue; + } + } +} diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Support.h b/NetworkPkg/Mtftp6Dxe/Mtftp6Support.h new file mode 100644 index 000000000..5712601fc --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Support.h @@ -0,0 +1,353 @@ +/** @file + Mtftp6 support functions declaration. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_MTFTP6_SUPPORT_H__ +#define __EFI_MTFTP6_SUPPORT_H__ + +// +// The structure representing a range of block numbers, [Start, End]. +// It is used to remember the holes in the MTFTP block space. If all +// the holes are filled in, then the download or upload has completed. +// +typedef struct { + LIST_ENTRY Link; + INTN Start; + INTN End; + INTN Round; + INTN Bound; +} MTFTP6_BLOCK_RANGE; + + +/** + Initialize the block range for either RRQ or WRQ. RRQ and WRQ have + different requirements for Start and End. For example, during startup, + WRQ initializes its whole valid block range to [0, 0xffff]. This + is because the server will send an ACK0 to inform the user to start the + upload. When the client receives an ACK0, it will remove 0 from the range, + get the next block number, which is 1, then upload the BLOCK1. For RRQ + without option negotiation, the server will directly send us the BLOCK1 + in response to the client's RRQ. When BLOCK1 is received, the client will + remove it from the block range and send an ACK. It also works if there + is option negotiation. + + @param[in] Head The block range head to initialize. + @param[in] Start The Start block number. + @param[in] End The last block number. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range. + @retval EFI_SUCCESS The initial block range is created. + +**/ +EFI_STATUS +Mtftp6InitBlockRange ( + IN LIST_ENTRY *Head, + IN UINT16 Start, + IN UINT16 End + ); + + +/** + Get the first valid block number on the range list. + + @param[in] Head The block range head. + + @retval ==-1 If the block range is empty. + @retval >-1 The first valid block number. + +**/ +INTN +Mtftp6GetNextBlockNum ( + IN LIST_ENTRY *Head + ); + + +/** + Set the last block number of the block range list. It + removes all the blocks after the Last. MTFTP initialize the + block range to the maximum possible range, such as [0, 0xffff] + for WRQ. When it gets the last block number, it calls + this function to set the last block number. + + @param[in] Head The block range list. + @param[in] Last The last block number. + +**/ +VOID +Mtftp6SetLastBlockNum ( + IN LIST_ENTRY *Head, + IN UINT16 Last + ); + + +/** + Remove the block number from the block range list. + + @param[in] Head The block range list to remove from. + @param[in] Num The block number to remove. + @param[in] Completed Whether Num is the last block number. + @param[out] BlockCounter The continuous block counter instead of the value after roll-over. + + @retval EFI_NOT_FOUND The block number isn't in the block range list. + @retval EFI_SUCCESS The block number has been removed from the list. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + +**/ +EFI_STATUS +Mtftp6RemoveBlockNum ( + IN LIST_ENTRY *Head, + IN UINT16 Num, + IN BOOLEAN Completed, + OUT UINT64 *BlockCounter + ); + + +/** + Build and transmit the request packet for the Mtftp6 instance. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Operation The operation code of this packet. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request. + @retval EFI_SUCCESS The request was built and sent. + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp6SendRequest ( + IN MTFTP6_INSTANCE *Instance, + IN UINT16 Operation + ); + + +/** + Build and send an error packet. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] ErrCode The error code in the packet. + @param[in] ErrInfo The error message in the packet. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet. + @retval EFI_SUCCESS The error packet was transmitted. + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp6SendError ( + IN MTFTP6_INSTANCE *Instance, + IN UINT16 ErrCode, + IN UINT8* ErrInfo + ); + + +/** + Send the packet for the Mtftp6 instance. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Packet The pointer to the packet to be sent. + + @retval EFI_SUCCESS The packet was sent out + @retval Others Failed to transmit the packet. + +**/ +EFI_STATUS +Mtftp6TransmitPacket ( + IN MTFTP6_INSTANCE *Instance, + IN NET_BUF *Packet + ); + + +/** + Check packet for GetInfo callback routine. + + @param[in] This The pointer to the Mtftp6 protocol. + @param[in] Token The pointer to the Mtftp6 token. + @param[in] PacketLen The length of the packet + @param[in] Packet The pointer to the received packet. + + @retval EFI_SUCCESS The check process passed successfully. + @retval EFI_ABORTED Abort the Mtftp6 operation. + +**/ +EFI_STATUS +EFIAPI +Mtftp6CheckPacket ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_TOKEN *Token, + IN UINT16 PacketLen, + IN EFI_MTFTP6_PACKET *Packet + ); + + +/** + The dummy configure routine for create a new Udp6 Io. + + @param[in] UdpIo The pointer to the Udp6 Io. + @param[in] Context The pointer to the context. + + @retval EFI_SUCCESS The value is always returned. + +**/ +EFI_STATUS +EFIAPI +Mtftp6ConfigDummyUdpIo ( + IN UDP_IO *UdpIo, + IN VOID *Context + ); + + +/** + The configure routine for the Mtftp6 instance to transmit/receive. + + @param[in] UdpIo The pointer to the Udp6 Io. + @param[in] ServerIp The pointer to the server address. + @param[in] ServerPort The pointer to the server port. + @param[in] LocalIp The pointer to the local address. + @param[in] LocalPort The pointer to the local port. + + @retval EFI_SUCCESS Configure the Udp6 Io for Mtftp6 successfully. + @retval EFI_NO_MAPPING The corresponding Ip6 instance has not been + configured yet. + +**/ +EFI_STATUS +Mtftp6ConfigUdpIo ( + IN UDP_IO *UdpIo, + IN EFI_IPv6_ADDRESS *ServerIp, + IN UINT16 ServerPort, + IN EFI_IPv6_ADDRESS *LocalIp, + IN UINT16 LocalPort + ); + + +/** + Clean up the current Mtftp6 operation. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Result The result to be returned to the user. + +**/ +VOID +Mtftp6OperationClean ( + IN MTFTP6_INSTANCE *Instance, + IN EFI_STATUS Result + ); + + +/** + Start the Mtftp6 instance to perform the operation, such as read file, + write file, and read directory. + + @param[in] This The MTFTP session + @param[in] Token The token that encapsulates the user's request. + @param[in] OpCode The operation to perform. + + @retval EFI_INVALID_PARAMETER Some of the parameters are invalid. + @retval EFI_NOT_STARTED The MTFTP session hasn't been configured. + @retval EFI_ALREADY_STARTED There is pending operation for the session. + @retval EFI_SUCCESS The operation was successfully started. + +**/ +EFI_STATUS +Mtftp6OperationStart ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_TOKEN *Token, + IN UINT16 OpCode + ); + + +/** + The timer ticking routine for the Mtftp6 instance. + + @param[in] Event The pointer to the ticking event. + @param[in] Context The pointer to the context. + +**/ +VOID +EFIAPI +Mtftp6OnTimerTick ( + IN EFI_EVENT Event, + IN VOID *Context + ); + + +/** + The packet process callback for Mtftp6 upload. + + @param[in] UdpPacket The pointer to the packet received. + @param[in] UdpEpt The pointer to the Udp6 access point. + @param[in] IoStatus The status from the Udp6 instance. + @param[in] Context The pointer to the context. + +**/ +VOID +EFIAPI +Mtftp6WrqInput ( + IN NET_BUF *UdpPacket, + IN UDP_END_POINT *UdpEpt, + IN EFI_STATUS IoStatus, + IN VOID *Context + ); + + +/** + Start the Mtftp6 instance to upload. It will first init some states, + then send the WRQ request packet, and start to receive the packet. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Operation The operation code of current packet. + + @retval EFI_SUCCESS The Mtftp6 was started to upload. + @retval Others Failed to start to upload. + +**/ +EFI_STATUS +Mtftp6WrqStart ( + IN MTFTP6_INSTANCE *Instance, + IN UINT16 Operation + ); + + +/** + The packet process callback for Mtftp6 download. + + @param[in] UdpPacket The pointer to the packet received. + @param[in] UdpEpt The pointer to the Udp6 access point. + @param[in] IoStatus The status from Udp6 instance. + @param[in] Context The pointer to the context. + +**/ +VOID +EFIAPI +Mtftp6RrqInput ( + IN NET_BUF *UdpPacket, + IN UDP_END_POINT *UdpEpt, + IN EFI_STATUS IoStatus, + IN VOID *Context + ); + + +/** + Start the Mtftp6 instance to download. It first initializes some + of the internal states then builds and sends an RRQ reqeuest packet. + Finally, it starts receive for the downloading. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Operation The operation code of current packet. + + @retval EFI_SUCCESS The Mtftp6 was started to download. + @retval Others Failed to start to download. + +**/ +EFI_STATUS +Mtftp6RrqStart ( + IN MTFTP6_INSTANCE *Instance, + IN UINT16 Operation + ); + +#endif diff --git a/NetworkPkg/Mtftp6Dxe/Mtftp6Wrq.c b/NetworkPkg/Mtftp6Dxe/Mtftp6Wrq.c new file mode 100644 index 000000000..2860f3870 --- /dev/null +++ b/NetworkPkg/Mtftp6Dxe/Mtftp6Wrq.c @@ -0,0 +1,598 @@ +/** @file + Mtftp6 Wrq process functions implementation. + + Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Mtftp6Impl.h" + + + +/** + Build and send a Mtftp6 data packet for upload. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] BlockNum The block num to be sent. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet. + @retval EFI_SUCCESS The data packet was sent. + @retval EFI_ABORTED The user aborted this process. + +**/ +EFI_STATUS +Mtftp6WrqSendBlock ( + IN MTFTP6_INSTANCE *Instance, + IN UINT16 BlockNum + ) +{ + EFI_MTFTP6_PACKET *Packet; + EFI_MTFTP6_TOKEN *Token; + NET_BUF *UdpPacket; + EFI_STATUS Status; + UINT16 DataLen; + UINT8 *DataBuf; + UINT64 Start; + + // + // Allocate net buffer to create data packet. + // + UdpPacket = NetbufAlloc (Instance->BlkSize + MTFTP6_DATA_HEAD_LEN); + + if (UdpPacket == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace ( + UdpPacket, + MTFTP6_DATA_HEAD_LEN, + FALSE + ); + ASSERT (Packet != NULL); + + Packet->Data.OpCode = HTONS (EFI_MTFTP6_OPCODE_DATA); + Packet->Data.Block = HTONS (BlockNum); + + // + // Read the block from either the buffer or PacketNeeded callback + // + Token = Instance->Token; + DataLen = Instance->BlkSize; + + if (Token->Buffer != NULL) { + Start = MultU64x32 (BlockNum - 1, Instance->BlkSize); + + if (Token->BufferSize < Start + Instance->BlkSize) { + DataLen = (UINT16) (Token->BufferSize - Start); + Instance->LastBlk = BlockNum; + Mtftp6SetLastBlockNum (&Instance->BlkList, BlockNum); + } + + if (DataLen > 0) { + NetbufAllocSpace (UdpPacket, DataLen, FALSE); + CopyMem (Packet->Data.Data, (UINT8 *) Token->Buffer + Start, DataLen); + } + + } else { + // + // Get data from PacketNeeded + // + DataBuf = NULL; + Status = Token->PacketNeeded (&Instance->Mtftp6, Token, &DataLen, (VOID*) &DataBuf); + + if (EFI_ERROR (Status) || (DataLen > Instance->BlkSize)) { + if (DataBuf != NULL) { + gBS->FreePool (DataBuf); + } + // + // The received packet has already been freed. + // + Mtftp6SendError ( + Instance, + EFI_MTFTP6_ERRORCODE_REQUEST_DENIED, + (UINT8 *) "User aborted the transfer" + ); + + return EFI_ABORTED; + } + + if (DataLen < Instance->BlkSize) { + Instance->LastBlk = BlockNum; + Mtftp6SetLastBlockNum (&Instance->BlkList, BlockNum); + } + + if (DataLen > 0) { + NetbufAllocSpace (UdpPacket, DataLen, FALSE); + CopyMem (Packet->Data.Data, DataBuf, DataLen); + gBS->FreePool (DataBuf); + } + } + + // + // Reset current retry count of the instance. + // + Instance->CurRetry = 0; + + return Mtftp6TransmitPacket (Instance, UdpPacket); +} + + +/** + Function to handle received ACK packet. If the ACK number matches the + expected block number, with more data pending, send the next + block. Otherwise, tell the caller that we are done. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Packet The pointer to the received packet. + @param[in] Len The length of the packet. + @param[out] UdpPacket The net buf of received packet. + @param[out] IsCompleted If TRUE, the upload has been completed. + Otherwise, the upload has not been completed. + + @retval EFI_SUCCESS The ACK packet successfully processed. + @retval EFI_TFTP_ERROR The block number loops back. + @retval Others Failed to transmit the next data packet. + +**/ +EFI_STATUS +Mtftp6WrqHandleAck ( + IN MTFTP6_INSTANCE *Instance, + IN EFI_MTFTP6_PACKET *Packet, + IN UINT32 Len, + OUT NET_BUF **UdpPacket, + OUT BOOLEAN *IsCompleted + ) +{ + UINT16 AckNum; + INTN Expected; + UINT64 BlockCounter; + + *IsCompleted = FALSE; + AckNum = NTOHS (Packet->Ack.Block[0]); + Expected = Mtftp6GetNextBlockNum (&Instance->BlkList); + + ASSERT (Expected >= 0); + + // + // Get an unwanted ACK, return EFI_SUCCESS to let Mtftp6WrqInput + // restart receive. + // + if (Expected != AckNum) { + return EFI_SUCCESS; + } + + // + // Remove the acked block number, if this is the last block number, + // tell the Mtftp6WrqInput to finish the transfer. This is the last + // block number if the block range are empty. + // + Mtftp6RemoveBlockNum (&Instance->BlkList, AckNum, *IsCompleted, &BlockCounter); + + Expected = Mtftp6GetNextBlockNum (&Instance->BlkList); + + if (Expected < 0) { + // + // The block range is empty. It may either because the the last + // block has been ACKed, or the sequence number just looped back, + // that is, there is more than 0xffff blocks. + // + if (Instance->LastBlk == AckNum) { + ASSERT (Instance->LastBlk >= 1); + *IsCompleted = TRUE; + return EFI_SUCCESS; + + } else { + // + // Free the received packet before send new packet in ReceiveNotify, + // since the udpio might need to be reconfigured. + // + NetbufFree (*UdpPacket); + *UdpPacket = NULL; + // + // Send the Mtftp6 error message if block number rolls back. + // + Mtftp6SendError ( + Instance, + EFI_MTFTP6_ERRORCODE_REQUEST_DENIED, + (UINT8 *) "Block number rolls back, not supported, try blksize option" + ); + + return EFI_TFTP_ERROR; + } + } + + // + // Free the receive buffer before send new packet since it might need + // reconfigure udpio. + // + NetbufFree (*UdpPacket); + *UdpPacket = NULL; + + return Mtftp6WrqSendBlock (Instance, (UINT16) Expected); +} + + +/** + Check whether the received OACK is valid. The OACK is valid + only if: + 1. It only include options requested by us. + 2. It can only include a smaller block size. + 3. It can't change the proposed time out value. + 4. Other requirements of the individal MTFTP6 options as required. + + @param[in] ReplyInfo The pointer to options information in reply packet. + @param[in] RequestInfo The pointer to requested options information. + + @retval TRUE If the option in OACK is valid. + @retval FALSE If the option is invalid. + +**/ +BOOLEAN +Mtftp6WrqOackValid ( + IN MTFTP6_EXT_OPTION_INFO *ReplyInfo, + IN MTFTP6_EXT_OPTION_INFO *RequestInfo + ) +{ + // + // It is invalid for server to return options we don't request + // + if ((ReplyInfo->BitMap & ~RequestInfo->BitMap) != 0) { + return FALSE; + } + + // + // Server can only specify a smaller block size to be used and + // return the timeout matches that requested. + // + if ((((ReplyInfo->BitMap & MTFTP6_OPT_BLKSIZE_BIT) != 0) && (ReplyInfo->BlkSize > RequestInfo->BlkSize)) || + (((ReplyInfo->BitMap & MTFTP6_OPT_TIMEOUT_BIT) != 0) && (ReplyInfo->Timeout != RequestInfo->Timeout)) + ) { + + return FALSE; + } + + return TRUE; +} + + +/** + Process the OACK packet for Wrq. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Packet The pointer to the received packet. + @param[in] Len The length of the packet. + @param[out] UdpPacket The net buf of received packet. + @param[out] IsCompleted If TRUE, the upload has been completed. + Otherwise, the upload has not been completed. + + @retval EFI_SUCCESS The OACK packet successfully processed. + @retval EFI_TFTP_ERROR An TFTP communication error happened. + @retval Others Failed to process the OACK packet. + +**/ +EFI_STATUS +Mtftp6WrqHandleOack ( + IN MTFTP6_INSTANCE *Instance, + IN EFI_MTFTP6_PACKET *Packet, + IN UINT32 Len, + OUT NET_BUF **UdpPacket, + OUT BOOLEAN *IsCompleted + ) +{ + EFI_MTFTP6_OPTION *Options; + UINT32 Count; + MTFTP6_EXT_OPTION_INFO ExtInfo; + EFI_MTFTP6_PACKET Dummy; + EFI_STATUS Status; + INTN Expected; + + *IsCompleted = FALSE; + Options = NULL; + + // + // Ignore the OACK if already started the upload + // + Expected = Mtftp6GetNextBlockNum (&Instance->BlkList); + + if (Expected != 0) { + return EFI_SUCCESS; + } + + // + // Parse and validate the options from server + // + ZeroMem (&ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO)); + + Status = Mtftp6ParseStart (Packet, Len, &Count, &Options); + + if (EFI_ERROR (Status)) { + return Status; + } + ASSERT (Options != NULL); + + Status = Mtftp6ParseExtensionOption (Options, Count, FALSE, Instance->Operation, &ExtInfo); + + if (EFI_ERROR(Status) || !Mtftp6WrqOackValid (&ExtInfo, &Instance->ExtInfo)) { + // + // Don't send a MTFTP error packet when out of resource, it can + // only make it worse. + // + if (Status != EFI_OUT_OF_RESOURCES) { + // + // Free the received packet before send new packet in ReceiveNotify, + // since the udpio might need to be reconfigured. + // + NetbufFree (*UdpPacket); + *UdpPacket = NULL; + // + // Send the Mtftp6 error message if invalid Oack packet received. + // + Mtftp6SendError ( + Instance, + EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION, + (UINT8 *) "Mal-formated OACK packet" + ); + } + + return EFI_TFTP_ERROR; + } + + if (ExtInfo.BlkSize != 0) { + Instance->BlkSize = ExtInfo.BlkSize; + } + + if (ExtInfo.Timeout != 0) { + Instance->Timeout = ExtInfo.Timeout; + } + + // + // Build a bogus ACK0 packet then pass it to the Mtftp6WrqHandleAck, + // which will start the transmission of the first data block. + // + Dummy.Ack.OpCode = HTONS (EFI_MTFTP6_OPCODE_ACK); + Dummy.Ack.Block[0] = 0; + + return Mtftp6WrqHandleAck ( + Instance, + &Dummy, + sizeof (EFI_MTFTP6_ACK_HEADER), + UdpPacket, + IsCompleted + ); +} + + +/** + The packet process callback for Mtftp6 upload. + + @param[in] UdpPacket The pointer to the packet received. + @param[in] UdpEpt The pointer to the Udp6 access point. + @param[in] IoStatus The status from Udp6 instance. + @param[in] Context The pointer to the context. + +**/ +VOID +EFIAPI +Mtftp6WrqInput ( + IN NET_BUF *UdpPacket, + IN UDP_END_POINT *UdpEpt, + IN EFI_STATUS IoStatus, + IN VOID *Context + ) +{ + MTFTP6_INSTANCE *Instance; + EFI_MTFTP6_PACKET *Packet; + BOOLEAN IsCompleted; + EFI_STATUS Status; + UINT32 TotalNum; + UINT32 Len; + UINT16 Opcode; + + Instance = (MTFTP6_INSTANCE *) Context; + + NET_CHECK_SIGNATURE (Instance, MTFTP6_INSTANCE_SIGNATURE); + + IsCompleted = FALSE; + Packet = NULL; + Status = EFI_SUCCESS; + TotalNum = 0; + + // + // Return error status if Udp6 instance failed to receive. + // + if (EFI_ERROR (IoStatus)) { + Status = IoStatus; + goto ON_EXIT; + } + + ASSERT (UdpPacket != NULL); + + if (UdpPacket->TotalSize < MTFTP6_OPCODE_LEN) { + goto ON_EXIT; + } + + // + // Client send initial request to server's listening port. Server + // will select a UDP port to communicate with the client. + // + if (UdpEpt->RemotePort != Instance->ServerDataPort) { + if (Instance->ServerDataPort != 0) { + goto ON_EXIT; + } else { + Instance->ServerDataPort = UdpEpt->RemotePort; + } + } + + // + // Copy the MTFTP packet to a continuous buffer if it isn't already so. + // + Len = UdpPacket->TotalSize; + TotalNum = UdpPacket->BlockOpNum; + + if (TotalNum > 1) { + Packet = AllocateZeroPool (Len); + + if (Packet == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet); + + } else { + Packet = (EFI_MTFTP6_PACKET *) NetbufGetByte (UdpPacket, 0, NULL); + ASSERT (Packet != NULL); + } + + Opcode = NTOHS (Packet->OpCode); + + // + // Callback to the user's CheckPacket if provided. Abort the transmission + // if CheckPacket returns an EFI_ERROR code. + // + if (Instance->Token->CheckPacket != NULL && + (Opcode == EFI_MTFTP6_OPCODE_OACK || Opcode == EFI_MTFTP6_OPCODE_ERROR) + ) { + + Status = Instance->Token->CheckPacket ( + &Instance->Mtftp6, + Instance->Token, + (UINT16) Len, + Packet + ); + + if (EFI_ERROR (Status)) { + // + // Send an error message to the server to inform it + // + if (Opcode != EFI_MTFTP6_OPCODE_ERROR) { + // + // Free the received packet before send new packet in ReceiveNotify, + // since the udpio might need to be reconfigured. + // + NetbufFree (UdpPacket); + UdpPacket = NULL; + // + // Send the Mtftp6 error message if user aborted the current session. + // + Mtftp6SendError ( + Instance, + EFI_MTFTP6_ERRORCODE_REQUEST_DENIED, + (UINT8 *) "User aborted the transfer" + ); + } + + Status = EFI_ABORTED; + goto ON_EXIT; + } + } + + // + // Switch the process routines by the operation code. + // + switch (Opcode) { + case EFI_MTFTP6_OPCODE_ACK: + if (Len != MTFTP6_OPCODE_LEN + MTFTP6_BLKNO_LEN) { + goto ON_EXIT; + } + // + // Handle the Ack packet of Wrq. + // + Status = Mtftp6WrqHandleAck (Instance, Packet, Len, &UdpPacket, &IsCompleted); + break; + + case EFI_MTFTP6_OPCODE_OACK: + if (Len <= MTFTP6_OPCODE_LEN) { + goto ON_EXIT; + } + // + // Handle the Oack packet of Wrq. + // + Status = Mtftp6WrqHandleOack (Instance, Packet, Len, &UdpPacket, &IsCompleted); + break; + + default: + // + // Drop and return eror if received error message. + // + Status = EFI_TFTP_ERROR; + break; + } + +ON_EXIT: + // + // Free the resources, then if !EFI_ERROR (Status) and not completed, + // restart the receive, otherwise end the session. + // + if (Packet != NULL && TotalNum > 1) { + FreePool (Packet); + } + + if (UdpPacket != NULL) { + NetbufFree (UdpPacket); + } + + if (!EFI_ERROR (Status) && !IsCompleted) { + Status = UdpIoRecvDatagram ( + Instance->UdpIo, + Mtftp6WrqInput, + Instance, + 0 + ); + } + // + // Clean up the current session if failed to continue. + // + if (EFI_ERROR (Status) || IsCompleted) { + Mtftp6OperationClean (Instance, Status); + } +} + + +/** + Start the Mtftp6 instance to upload. It will first init some states, + then send the WRQ request packet, and start to receive the packet. + + @param[in] Instance The pointer to the Mtftp6 instance. + @param[in] Operation The operation code of the current packet. + + @retval EFI_SUCCESS The Mtftp6 was started to upload. + @retval Others Failed to start to upload. + +**/ +EFI_STATUS +Mtftp6WrqStart ( + IN MTFTP6_INSTANCE *Instance, + IN UINT16 Operation + ) +{ + EFI_STATUS Status; + + // + // The valid block number range are [0, 0xffff]. For example: + // the client sends an WRQ request to the server, the server + // ACK with an ACK0 to let client start transfer the first + // packet. + // + Status = Mtftp6InitBlockRange (&Instance->BlkList, 0, 0xffff); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status = Mtftp6SendRequest (Instance, Operation); + + if (EFI_ERROR (Status)) { + return Status; + } + + return UdpIoRecvDatagram ( + Instance->UdpIo, + Mtftp6WrqInput, + Instance, + 0 + ); +} + diff --git a/NetworkPkg/Network.dsc.inc b/NetworkPkg/Network.dsc.inc new file mode 100644 index 000000000..c7f43282e --- /dev/null +++ b/NetworkPkg/Network.dsc.inc @@ -0,0 +1,40 @@ +## @file +# Network DSC include file for Platform DSC +# +# This file includes all required information to enable Network features. +# It can be included to a platform DSC file by using "!include NetworkPkg/Network.dsc.inc". +# +# This file defines one build flag PLATFORMX64_ENABLE to support +# IA32 PEI and X64 DXE platform. Its default value is FALSE. +# +# Copyright (c) 2019, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] +!include NetworkPkg/NetworkDefines.dsc.inc + +!ifndef PLATFORMX64_ENABLE + # + # PLATFORMX64_ENABLE is set to TRUE when PEI is IA32 and DXE is X64 platform + # + DEFINE PLATFORMX64_ENABLE = FALSE +!endif + +[PcdsFixedAtBuild] +!include NetworkPkg/NetworkPcds.dsc.inc + +[LibraryClasses] +!include NetworkPkg/NetworkLibs.dsc.inc + +!if $(PLATFORMX64_ENABLE) == TRUE +[Components.X64] +!include NetworkPkg/NetworkComponents.dsc.inc + +!else +[Components.IA32, Components.X64, Components.ARM, Components.AARCH64] +!include NetworkPkg/NetworkComponents.dsc.inc + +!endif diff --git a/NetworkPkg/Network.fdf.inc b/NetworkPkg/Network.fdf.inc new file mode 100644 index 000000000..803a0d64f --- /dev/null +++ b/NetworkPkg/Network.fdf.inc @@ -0,0 +1,60 @@ +## @file +# Network FDF include file for All Architectures. +# +# This file can be included to a platform FDF by using +# "!include NetworkPkg/Network.fdf.inc" to add EDKII network stack drivers +# according to the value of flags described in "NetworkPkg/Network.dsc.inc". +# +# Copyright (c) 2019, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# + +!if $(NETWORK_ENABLE) == TRUE + INF NetworkPkg/DpcDxe/DpcDxe.inf + + !if $(NETWORK_SNP_ENABLE) == TRUE + INF NetworkPkg/SnpDxe/SnpDxe.inf + !endif + + !if $(NETWORK_VLAN_ENABLE) == TRUE + INF NetworkPkg/VlanConfigDxe/VlanConfigDxe.inf + !endif + + INF NetworkPkg/MnpDxe/MnpDxe.inf + + !if $(NETWORK_IP4_ENABLE) == TRUE + INF NetworkPkg/ArpDxe/ArpDxe.inf + INF NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.inf + INF NetworkPkg/Ip4Dxe/Ip4Dxe.inf + INF NetworkPkg/Udp4Dxe/Udp4Dxe.inf + INF NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.inf + !endif + + !if $(NETWORK_IP6_ENABLE) == TRUE + INF NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.inf + INF NetworkPkg/Ip6Dxe/Ip6Dxe.inf + INF NetworkPkg/Udp6Dxe/Udp6Dxe.inf + INF NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.inf + !endif + + INF NetworkPkg/TcpDxe/TcpDxe.inf + INF NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.inf + + !if $(NETWORK_TLS_ENABLE) == TRUE + INF NetworkPkg/TlsDxe/TlsDxe.inf + INF NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.inf + !endif + + !if $(NETWORK_HTTP_BOOT_ENABLE) == TRUE + INF NetworkPkg/DnsDxe/DnsDxe.inf + INF NetworkPkg/HttpDxe/HttpDxe.inf + INF NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.inf + INF NetworkPkg/HttpBootDxe/HttpBootDxe.inf + !endif + + !if $(NETWORK_ISCSI_ENABLE) == TRUE + INF NetworkPkg/IScsiDxe/IScsiDxe.inf + !endif + +!endif diff --git a/NetworkPkg/NetworkComponents.dsc.inc b/NetworkPkg/NetworkComponents.dsc.inc new file mode 100644 index 000000000..40cb8ee18 --- /dev/null +++ b/NetworkPkg/NetworkComponents.dsc.inc @@ -0,0 +1,61 @@ +## @file +# Network DSC include file for [Components*] section of all Architectures. +# +# This file can be included to the [Components*] section(s) of a platform DSC file +# by using "!include NetworkPkg/NetworkComponents.dsc.inc" to specify the INF files +# of EDKII network drivers according to the value of flags described in +# "NetworkDefines.dsc.inc". +# +# Copyright (c) 2019, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +!if $(NETWORK_ENABLE) == TRUE + NetworkPkg/DpcDxe/DpcDxe.inf + + !if $(NETWORK_SNP_ENABLE) == TRUE + NetworkPkg/SnpDxe/SnpDxe.inf + !endif + + !if $(NETWORK_VLAN_ENABLE) == TRUE + NetworkPkg/VlanConfigDxe/VlanConfigDxe.inf + !endif + + NetworkPkg/MnpDxe/MnpDxe.inf + + !if $(NETWORK_IP4_ENABLE) == TRUE + NetworkPkg/ArpDxe/ArpDxe.inf + NetworkPkg/Dhcp4Dxe/Dhcp4Dxe.inf + NetworkPkg/Ip4Dxe/Ip4Dxe.inf + NetworkPkg/Udp4Dxe/Udp4Dxe.inf + NetworkPkg/Mtftp4Dxe/Mtftp4Dxe.inf + !endif + + !if $(NETWORK_IP6_ENABLE) == TRUE + NetworkPkg/Dhcp6Dxe/Dhcp6Dxe.inf + NetworkPkg/Ip6Dxe/Ip6Dxe.inf + NetworkPkg/Udp6Dxe/Udp6Dxe.inf + NetworkPkg/Mtftp6Dxe/Mtftp6Dxe.inf + !endif + + NetworkPkg/TcpDxe/TcpDxe.inf + NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.inf + + !if $(NETWORK_TLS_ENABLE) == TRUE + NetworkPkg/TlsDxe/TlsDxe.inf + NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.inf + !endif + + !if $(NETWORK_HTTP_BOOT_ENABLE) == TRUE + NetworkPkg/DnsDxe/DnsDxe.inf + NetworkPkg/HttpDxe/HttpDxe.inf + NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesDxe.inf + NetworkPkg/HttpBootDxe/HttpBootDxe.inf + !endif + + !if $(NETWORK_ISCSI_ENABLE) == TRUE + NetworkPkg/IScsiDxe/IScsiDxe.inf + !endif +!endif diff --git a/NetworkPkg/NetworkDefines.dsc.inc b/NetworkPkg/NetworkDefines.dsc.inc new file mode 100644 index 000000000..a442d1b15 --- /dev/null +++ b/NetworkPkg/NetworkDefines.dsc.inc @@ -0,0 +1,118 @@ +## @file +# Network DSC include file for [Defines] section of all Architectures. +# +# This file can be included to the [Defines] section of a platform DSC file by +# using "!include NetworkPkg/NetworkDefines.dsc.inc" to set default value of +# flags if they are not defined somewhere else, and also check the value to see +# if there is any conflict. +# +# These flags can be defined before the !include line, or changed on the command +# line to enable or disable related feature support. +# -D FLAG=VALUE +# The default value of these flags are: +# DEFINE NETWORK_ENABLE = TRUE +# DEFINE NETWORK_SNP_ENABLE = TRUE +# DEFINE NETWORK_IP4_ENABLE = TRUE +# DEFINE NETWORK_IP6_ENABLE = TRUE +# DEFINE NETWORK_TLS_ENABLE = TRUE +# DEFINE NETWORK_HTTP_BOOT_ENABLE = TRUE +# DEFINE NETWORK_ALLOW_HTTP_CONNECTIONS = FALSE +# DEFINE NETWORK_ISCSI_ENABLE = TRUE +# DEFINE NETWORK_VLAN_ENABLE = TRUE +# +# Copyright (c) 2019, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +!ifndef NETWORK_ENABLE + # + # This flag is to enable or disable the whole network stack. + # + DEFINE NETWORK_ENABLE = TRUE +!endif + +!ifndef NETWORK_SNP_ENABLE + # + # This flag is to include the common SNP driver or not. + # + DEFINE NETWORK_SNP_ENABLE = TRUE +!endif + +!ifndef NETWORK_VLAN_ENABLE + # + # This flag is to enable or disable VLAN feature. + # + DEFINE NETWORK_VLAN_ENABLE = TRUE +!endif + +!ifndef NETWORK_IP4_ENABLE + # + # This flag is to enable or disable IPv4 network stack. + # + DEFINE NETWORK_IP4_ENABLE = TRUE +!endif + +!ifndef NETWORK_IP6_ENABLE + # + # This flag is to enable or disable IPv6 network stack. + # + DEFINE NETWORK_IP6_ENABLE = TRUE +!endif + +!ifndef NETWORK_TLS_ENABLE + # + # This flag is to enable or disable TLS feature. + # + # Note: This feature depends on the OpenSSL building. To enable this feature, please + # follow the instructions found in the file "OpenSSL-HOWTO.txt" located in + # CryptoPkg\Library\OpensslLib to enable the OpenSSL building first. + # The OpensslLib.inf library instance should be used since libssl is required. + # + DEFINE NETWORK_TLS_ENABLE = TRUE +!endif + +!ifndef NETWORK_HTTP_BOOT_ENABLE + # + # This flag is to enable or disable HTTP(S) boot feature. + # + DEFINE NETWORK_HTTP_BOOT_ENABLE = TRUE +!endif + +!ifndef NETWORK_ALLOW_HTTP_CONNECTIONS + # + # Indicates whether HTTP connections (i.e., unsecured) are permitted or not. + # + # Note: If NETWORK_ALLOW_HTTP_CONNECTIONS is TRUE, HTTP connections are allowed. + # Both the "https://" and "http://" URI schemes are permitted. Otherwise, HTTP + # connections are denied. Only the "https://" URI scheme is permitted. + # + DEFINE NETWORK_ALLOW_HTTP_CONNECTIONS = FALSE +!endif + +!ifndef NETWORK_ISCSI_ENABLE + # + # This flag is to enable or disable iSCSI feature. + # + # Note: This feature depends on the OpenSSL building. To enable this feature, please + # follow the instructions found in the file "OpenSSL-HOWTO.txt" located in + # CryptoPkg\Library\OpensslLib to enable the OpenSSL building first. + # Both OpensslLib.inf and OpensslLibCrypto.inf library instance can be used + # since libssl is not required for iSCSI. + # + DEFINE NETWORK_ISCSI_ENABLE = TRUE +!endif + +!if $(NETWORK_ENABLE) == TRUE + # + # Check the flags to see if there is any conflict. + # + !if ($(NETWORK_IP4_ENABLE) == FALSE) AND ($(NETWORK_IP6_ENABLE) == FALSE) + !error "Must enable at least IP4 or IP6 stack if NETWORK_ENABLE is set to TRUE!" + !endif + + !if ($(NETWORK_HTTP_BOOT_ENABLE) == TRUE) AND ($(NETWORK_TLS_ENABLE) == FALSE) AND ($(NETWORK_ALLOW_HTTP_CONNECTIONS) == FALSE) + !error "Must enable TLS to support HTTPS, or allow unsecured HTTP connection, if NETWORK_HTTP_BOOT_ENABLE is set to TRUE!" + !endif +!endif diff --git a/NetworkPkg/NetworkLibs.dsc.inc b/NetworkPkg/NetworkLibs.dsc.inc new file mode 100644 index 000000000..4b99f4808 --- /dev/null +++ b/NetworkPkg/NetworkLibs.dsc.inc @@ -0,0 +1,20 @@ +## @file +# Network DSC include file for [LibraryClasses*] section of all Architectures. +# +# This file can be included to the [LibraryClasses*] section(s) of a platform DSC file +# by using "!include NetworkPkg/NetworkLibs.dsc.inc" to specify the library instances +# of EDKII network library classes. +# +# Copyright (c) 2019, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + + DpcLib|NetworkPkg/Library/DxeDpcLib/DxeDpcLib.inf + NetLib|NetworkPkg/Library/DxeNetLib/DxeNetLib.inf + IpIoLib|NetworkPkg/Library/DxeIpIoLib/DxeIpIoLib.inf + UdpIoLib|NetworkPkg/Library/DxeUdpIoLib/DxeUdpIoLib.inf + TcpIoLib|NetworkPkg/Library/DxeTcpIoLib/DxeTcpIoLib.inf + # HttpLib is used for Http Boot + HttpLib|NetworkPkg/Library/DxeHttpLib/DxeHttpLib.inf diff --git a/NetworkPkg/NetworkPcds.dsc.inc b/NetworkPkg/NetworkPcds.dsc.inc new file mode 100644 index 000000000..f874b382e --- /dev/null +++ b/NetworkPkg/NetworkPcds.dsc.inc @@ -0,0 +1,16 @@ +## @file +# Network DSC include file for [Pcds*] section of all Architectures. +# +# This file can be included to the [Pcds*] section(s) of a platform DSC file +# by using "!include NetworkPkg/NetworkPcds.dsc.inc" to specify PCD settings +# according to the value of flags described in "NetworkDefines.dsc.inc". +# +# Copyright (c) 2019, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +!if $(NETWORK_ALLOW_HTTP_CONNECTIONS) == TRUE + gEfiNetworkPkgTokenSpaceGuid.PcdAllowHttpConnections|TRUE +!endif diff --git a/NetworkPkg/NetworkPkg.dec b/NetworkPkg/NetworkPkg.dec new file mode 100644 index 000000000..2bdb3f566 --- /dev/null +++ b/NetworkPkg/NetworkPkg.dec @@ -0,0 +1,128 @@ +## @file +# Network Package. +# +# This package provides network modules that conform to UEFI 2.4 specification. +# +# Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.
+# (C) Copyright 2015-2017 Hewlett Packard Enterprise Development LP
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + DEC_SPECIFICATION = 0x00010005 + PACKAGE_NAME = NetworkPkg + PACKAGE_GUID = 947988BE-8D5C-471a-893D-AD181C46BEBB + PACKAGE_VERSION = 0.98 + PACKAGE_UNI_FILE = NetworkPkg.uni + +[Includes] + Include + +[LibraryClasses] + ## @libraryclass IpIo layer upon EFI IP4 Protocol. + # This library is only intended to be used by UEFI network stack modules. + IpIoLib|Include/Library/IpIoLib.h + + ## @libraryclass Basic function for UEFI network stack. + # This library is only intended to be used by UEFI network stack modules. + NetLib|Include/Library/NetLib.h + + ## @libraryclass The helper routines to access UDP service. + # This library is only intended to be used by UEFI network stack modules. + UdpIoLib|Include/Library/UdpIoLib.h + + ## @libraryclass The helper routines to access TCP service. + # This library is only intended to be used by UEFI network stack modules. + TcpIoLib|Include/Library/TcpIoLib.h + + ## @libraryclass The helper routines to access HTTP service. + # This library is only intended to be used by UEFI network stack modules. + HttpLib|Include/Library/HttpLib.h + + ## @libraryclass Library for Deferred Procedure Calls. + DpcLib|Include/Library/DpcLib.h + +[Guids] + ## Network package token space guid. + # Include/Guid/NetworkPkgTokenSpace.h + gEfiNetworkPkgTokenSpaceGuid = { 0x40e064b2, 0x0ae0, 0x48b1, { 0xa0, 0x7d, 0xf8, 0xcf, 0x1e, 0x1a, 0x23, 0x10}} + + # Include/Guid/Ip6ConfigHii.h + gIp6ConfigNvDataGuid = { 0x2eea107, 0x98db, 0x400e, { 0x98, 0x30, 0x46, 0xa, 0x15, 0x42, 0xd7, 0x99}} + + # Include/Guid/IscsiConfigHii.h + gIScsiConfigGuid = { 0x4b47d616, 0xa8d6, 0x4552, { 0x9d, 0x44, 0xcc, 0xad, 0x2e, 0xf, 0x4c, 0xf9}} + + # Include/Guid/HttpBootConfigHii.h + gHttpBootConfigGuid = { 0x4d20583a, 0x7765, 0x4e7a, { 0x8a, 0x67, 0xdc, 0xde, 0x74, 0xee, 0x3e, 0xc5 }} + + # Include/Guid/TlsAuthConfigHii.h + gTlsAuthConfigGuid = { 0xb0eae4f8, 0x9a04, 0x4c6d, { 0xa7, 0x48, 0x79, 0x3d, 0xaa, 0xf, 0x65, 0xdf }} + + # Include/Guid/TlsAuthentication.h + gEfiTlsCaCertificateGuid = { 0xfd2340D0, 0x3dab, 0x4349, { 0xa6, 0xc7, 0x3b, 0x4f, 0x12, 0xb4, 0x8e, 0xae }} + + # Include/Guid/HttpTlsCipherList.h + gEdkiiHttpTlsCipherListGuid = { 0x46ddb415, 0x5244, 0x49c7, { 0x93, 0x74, 0xf0, 0xe2, 0x98, 0xe7, 0xd3, 0x86 }} + + # Include/Guid/WifiConnectionManagerConfigHii.h + gWifiConfigGuid = { 0x9f94d327, 0x0b18, 0x4245, { 0x8f, 0xf2, 0x83, 0x2e, 0x30, 0xd, 0x2c, 0xef }} + +[Protocols] + ## Include/Protocol/Dpc.h + gEfiDpcProtocolGuid = {0x480f8ae9, 0xc46, 0x4aa9, { 0xbc, 0x89, 0xdb, 0x9f, 0xba, 0x61, 0x98, 0x6 }} + +[PcdsFixedAtBuild] + ## The max attempt number will be created by iSCSI driver. + # @Prompt Max attempt number. + gEfiNetworkPkgTokenSpaceGuid.PcdMaxIScsiAttemptNumber|0x08|UINT8|0x0000000D + +[PcdsFixedAtBuild, PcdsPatchableInModule] + ## Indicates whether HTTP connections (i.e., unsecured) are permitted or not. + # TRUE - HTTP connections are allowed. Both the "https://" and "http://" URI schemes are permitted. + # FALSE - HTTP connections are denied. Only the "https://" URI scheme is permitted. + # @Prompt Indicates whether HTTP connections are permitted or not. + gEfiNetworkPkgTokenSpaceGuid.PcdAllowHttpConnections|FALSE|BOOLEAN|0x00000008 + + ## This setting is to specify the MTFTP windowsize used by UEFI PXE driver. + # A value of 0 indicates the default value of windowsize(1). + # A non-zero value will be used as windowsize. + # @Prompt PXE TFTP windowsize. + gEfiNetworkPkgTokenSpaceGuid.PcdPxeTftpWindowSize|0x4|UINT64|0x10000008 + +[PcdsFixedAtBuild, PcdsPatchableInModule, PcdsDynamic, PcdsDynamicEx] + ## IPv6 DHCP Unique Identifier (DUID) Type configuration (From RFCs 3315 and 6355). + # 01 = DUID Based on Link-layer Address Plus Time [DUID-LLT] + # 04 = UUID-Based DHCPv6 Unique Identifier (DUID-UUID) + # 02 = DUID Assigned by Vendor Based on Enterprise Number [DUID-EN] (not supported) + # 03 = DUID Based on Link-layer Address [DUID-LL] (not supported) + # @Prompt Type Value of Dhcp6 Unique Identifier (DUID). + gEfiNetworkPkgTokenSpaceGuid.PcdDhcp6UidType|4|UINT8|0x10000001 + + ## Network boot policy to stop UEFI iSCSI if applicable. + # 0x00 = Always use UEFI iSCSI and ignore iSCSI HBA. + # 0x01 = Stop UEFI iSCSI if iSCSI HBA adapter produces AIP protocol with Network Boot. + # 0x02 = Stop UEFI iSCSI if iSCSI HBA adapter supports booting from iSCSI IPv4 targets. + # 0x04 = Stop UEFI iSCSI if iSCSI HBA adapter supports booting from iSCSI IPv6 targets. + # 0x08 = Stop UEFI iSCSI if iSCSI HBA adapter supports an offload engine for iSCSI boot. + # 0x10 = Stop UEFI iSCSI if iSCSI HBA adapter supports multipath I/O for iSCSI boot. + # 0x20 = Stop UEFI iSCSI if iSCSI HBA adapter is currently configured to boot from iSCSI IPv4 targets. + # 0x40 = Stop UEFI iSCSI if iSCSI HBA adapter is currently configured to boot from iSCSI IPv6 targets. + # 0xFF = Always use iSCSI HBA and ignore UEFI iSCSI. + # @Prompt Type Value of network boot policy used in iSCSI. + gEfiNetworkPkgTokenSpaceGuid.PcdIScsiAIPNetworkBootPolicy|0x08|UINT8|0x10000007 + + ## IPv4 PXE support + # 0x01 = PXE Enabled + # 0x00 = PXE Disabled + gEfiNetworkPkgTokenSpaceGuid.PcdIPv4PXESupport|0x01|UINT8|0x10000009 + + ## IPv6 PXE support + # 0x01 = PXE Enabled + # 0x00 = PXE Disabled + gEfiNetworkPkgTokenSpaceGuid.PcdIPv6PXESupport|0x01|UINT8|0x1000000a + +[UserExtensions.TianoCore."ExtraFiles"] + NetworkPkgExtra.uni diff --git a/NetworkPkg/NetworkPkg.dsc b/NetworkPkg/NetworkPkg.dsc new file mode 100644 index 000000000..fe2fcf7b3 --- /dev/null +++ b/NetworkPkg/NetworkPkg.dsc @@ -0,0 +1,106 @@ +## @file +# UEFI 2.4 Network Module Package for All Architectures +# +# (C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + PLATFORM_NAME = NetworkPkg + PLATFORM_GUID = 3FD34E9B-E90C-44e1-B510-1F632A509F10 + PLATFORM_VERSION = 0.98 + DSC_SPECIFICATION = 0x00010005 + OUTPUT_DIRECTORY = Build/NetworkPkg + SUPPORTED_ARCHITECTURES = IA32|X64|EBC|ARM|AARCH64 + BUILD_TARGETS = DEBUG|RELEASE|NOOPT + SKUID_IDENTIFIER = DEFAULT + +[LibraryClasses] + DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf + BaseLib|MdePkg/Library/BaseLib/BaseLib.inf + BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf + DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf + HiiLib|MdeModulePkg/Library/UefiHiiLib/UefiHiiLib.inf + MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf + PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf + PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf + UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf + UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf + UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf + UefiLib|MdePkg/Library/UefiLib/UefiLib.inf + UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf + UefiHiiServicesLib|MdeModulePkg/Library/UefiHiiServicesLib/UefiHiiServicesLib.inf + UefiBootManagerLib|MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf + TimerLib|MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf + PerformanceLib|MdePkg/Library/BasePerformanceLibNull/BasePerformanceLibNull.inf + PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/BasePeCoffGetEntryPointLib.inf + DxeServicesLib|MdePkg/Library/DxeServicesLib/DxeServicesLib.inf + DxeServicesTableLib|MdePkg/Library/DxeServicesTableLib/DxeServicesTableLib.inf + SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf + + BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf + OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLib.inf + IntrinsicLib|CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf + TlsLib|CryptoPkg/Library/TlsLib/TlsLib.inf + DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf + FileHandleLib|MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.inf + FileExplorerLib|MdeModulePkg/Library/FileExplorerLib/FileExplorerLib.inf + SortLib|MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf + +[LibraryClasses.common.UEFI_DRIVER] + HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf + ReportStatusCodeLib|MdeModulePkg/Library/DxeReportStatusCodeLib/DxeReportStatusCodeLib.inf + DebugLib|MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf + +[LibraryClasses.common.UEFI_APPLICATION] + DebugLib|MdePkg/Library/UefiDebugLibStdErr/UefiDebugLibStdErr.inf + ShellLib|ShellPkg/Library/UefiShellLib/UefiShellLib.inf + +[LibraryClasses.ARM, LibraryClasses.AARCH64] + # + # It is not possible to prevent ARM compiler calls to generic intrinsic functions. + # This library provides the instrinsic functions generated by a given compiler. + # [LibraryClasses.ARM] and NULL mean link this library into all ARM images. + # + NULL|ArmPkg/Library/CompilerIntrinsicsLib/CompilerIntrinsicsLib.inf + NULL|MdePkg/Library/BaseStackCheckLib/BaseStackCheckLib.inf + ArmSoftFloatLib|ArmPkg/Library/ArmSoftFloatLib/ArmSoftFloatLib.inf + +[PcdsFeatureFlag] + gEfiMdePkgTokenSpaceGuid.PcdComponentName2Disable|TRUE + gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnostics2Disable|TRUE + +[PcdsFixedAtBuild] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x2f + gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000000 + +################################################################################################### +# +# Components Section - list of the modules and components that will be processed by compilation +# tools and the EDK II tools to generate PE32/PE32+/Coff image files. +# +# Note: The EDK II DSC file is not used to specify how compiled binary images get placed +# into firmware volume images. This section is just a list of modules to compile from +# source into UEFI-compliant binaries. +# It is the FDF file that contains information on combining binary files into firmware +# volume images, whose concept is beyond UEFI and is described in PI specification. +# Binary modules do not need to be listed in this section, as they should be +# specified in the FDF file. For example: Shell binary (Shell_Full.efi), FAT binary (Fat.efi), +# Logo (Logo.bmp), and etc. +# There may also be modules listed in this section that are not required in the FDF file, +# When a module listed here is excluded from FDF file, then UEFI-compliant binary will be +# generated for it, but the binary will not be put into any firmware volume. +# +################################################################################################### + +[Components] + NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxe.inf + NetworkPkg/Application/VConfig/VConfig.inf + + !include NetworkPkg/Network.dsc.inc + +[BuildOptions] + *_*_*_CC_FLAGS = -D DISABLE_NEW_DEPRECATED_INTERFACES diff --git a/NetworkPkg/NetworkPkg.uni b/NetworkPkg/NetworkPkg.uni new file mode 100644 index 000000000..ab9b4d21d --- /dev/null +++ b/NetworkPkg/NetworkPkg.uni @@ -0,0 +1,94 @@ +// /** @file +// This package provides network modules that conform to UEFI 2.4 specification. +// +// This package provides network modules that conform to UEFI 2.4 specification. +// +// Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_PACKAGE_ABSTRACT #language en-US "This package provides network modules that conform to UEFI 2.4 specification." + +#string STR_PACKAGE_DESCRIPTION #language en-US "This package provides network modules that conform to UEFI 2.4 specification." + + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdMaxIScsiAttemptNumber_PROMPT #language en-US "Max attempt number." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdMaxIScsiAttemptNumber_HELP #language en-US "Max attempt number created by iSCSI." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCaFile_PROMPT #language en-US "CA file." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCaFile_HELP #language en-US "CA certificate used by IPsec." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCaFileSize_PROMPT #language en-US "CA file's size." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCaFileSize_HELP #language en-US "CA certificate file's size." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCertificate_PROMPT #language en-US "Pubic Key for remote peer." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCertificate_HELP #language en-US "X509 certificate as Public Key which is used by IPsec (DER format)" + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCertificateSize_PROMPT #language en-US "Pubic Key's size." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCertificateSize_HELP #language en-US "X509 certificate as Public Key's size." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCertificateKey_PROMPT #language en-US "Private Key." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCertificateKey_HELP #language en-US "Private Key used by IPsec (PEM format)." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCertificateKeySize_PROMPT #language en-US "Private Key's size." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecUefiCertificateKeySize_HELP #language en-US "Private Key's size." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdAllowHttpConnections_PROMPT #language en-US "Indicates whether HTTP connections are permitted or not." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdAllowHttpConnections_HELP #language en-US "Indicates whether HTTP connections are permitted or not.\n" + "TRUE - HTTP connections are allowed.\n" + "FALSE - HTTP connections are denied." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdPxeTftpWindowSize_PROMPT #language en-US "This setting is to specify the MTFTP windowsize used by UEFI PXE driver." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdPxeTftpWindowSize_HELP #language en-US "Specify MTFTP windowsize used by UEFI PXE driver.\n" + "A value of 0 indicates the default value of windowsize(1).\n" + "A non-zero value will be used as windowsize." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecCertificateEnabled_PROMPT #language en-US "Enable IPsec IKEv2 Certificate Authentication." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIpsecCertificateEnabled_HELP #language en-US "Indicates if the IPsec IKEv2 Certificate Authentication feature is enabled or not.

\n" + "TRUE - Certificate Authentication feature is enabled.
\n" + "FALSE - Does not support Certificate Authentication.
" + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdDhcp6UidType_PROMPT #language en-US "Type Value of Dhcp6 Unique Identifier (DUID)." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdDhcp6UidType_HELP #language en-US "IPv6 DHCP Unique Identifier (DUID) Type configuration (From RFCs 3315 and 6355).\n" + "01 = DUID Based on Link-layer Address Plus Time [DUID-LLT]\n" + "04 = UUID-Based DHCPv6 Unique Identifier (DUID-UUID)\n" + "02 = DUID Assigned by Vendor Based on Enterprise Number [DUID-EN] (not supported)\n" + "03 = DUID Based on Link-layer Address [DUID-LL] (not supported)" + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIScsiAIPNetworkBootPolicy_PROMPT #language en-US "Type Value of network boot policy used in iSCSI." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIScsiAIPNetworkBootPolicy_HELP #language en-US "Network boot policy to stop UEFI iSCSI if applicable.\n" + "0x00 = Always use UEFI iSCSI and ignore AIP.\n" + "0x01 = Stop UEFI iSCSI if iSCSI HBA adapter produces AIP protocol with Network Boot.\n" + "0x02 = Stop UEFI iSCSI if iSCSI HBA adapter supports booting from iSCSI IPv4 targets.\n" + "0x04 = Stop UEFI iSCSI if iSCSI HBA adapter supports booting from iSCSI IPv6 targets.\n" + "0x08 = Stop UEFI iSCSI if iSCSI HBA adapter supports an offload engine for iSCSI boot.\n" + "0x10 = Stop UEFI iSCSI if iSCSI HBA adapter supports multipath I/O for iSCSI boot.\n" + "0x20 = Stop UEFI iSCSI if iSCSI HBA adapter is currently configured to boot from iSCSI IPv4 targets.\n" + "0x40 = Stop UEFI iSCSI if iSCSI HBA adapter is currently configured to boot from iSCSI IPv6 targets." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIPv4PXESupport_PROMPT #language en-US "Enable IPV4 PXE Function." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIPv4PXESupport_HELP #language en-US "Indicates IPV4 PXE Function is enabled or not.\n" + "A value of 0 indicates the IPV4 PXE Function is disabled.\n" + "A value of 1 indicates the IPV4 PXE Function is enabled." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIPv6PXESupport_PROMPT #language en-US "Enable IPV6 PXE Function." + +#string STR_gEfiNetworkPkgTokenSpaceGuid_PcdIPv6PXESupport_HELP #language en-US "Indicates IPV6 PXE Function is enabled or not.\n" + "A value of 0 indicates the IPV6 PXE Function is disabled.\n" + "A value of 1 indicates the IPV6 PXE Function is enabled." diff --git a/NetworkPkg/NetworkPkgExtra.uni b/NetworkPkg/NetworkPkgExtra.uni new file mode 100644 index 000000000..e7049a115 --- /dev/null +++ b/NetworkPkg/NetworkPkgExtra.uni @@ -0,0 +1,13 @@ +// /** @file +// Network Package Localized Strings and Content. +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_PACKAGE_NAME +#language en-US +"Network package" + diff --git a/NetworkPkg/SnpDxe/Callback.c b/NetworkPkg/SnpDxe/Callback.c new file mode 100644 index 000000000..6387dbdb3 --- /dev/null +++ b/NetworkPkg/SnpDxe/Callback.c @@ -0,0 +1,354 @@ +/** @file + This file contains the callback routines for undi3.1. + the callback routines for Undi3.1 have an extra parameter UniqueId which + stores the interface context for the NIC that snp is trying to talk. + +Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + +/** + Acquire or release a lock of the exclusive access to a critical section of the + code/data. + + This is a callback routine supplied to UNDI3.1 at undi_start time. + New callbacks for 3.1: there won't be a virtual2physical callback for UNDI 3.1 + because undi3.1 uses the MemMap call to map the required address by itself! + + @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to + store Undi interface context (Undi does not read or write + this variable). + @param Enable Non-zero indicates acquire; Zero indicates release. + +**/ +VOID +EFIAPI +SnpUndi32CallbackBlock ( + IN UINT64 UniqueId, + IN UINT32 Enable + ) +{ + SNP_DRIVER *Snp; + + Snp = (SNP_DRIVER *) (UINTN) UniqueId; + // + // tcpip was calling snp at tpl_notify and when we acquire a lock that was + // created at a lower level (TPL_CALLBACK) it gives an assert! + // + if (Enable != 0) { + EfiAcquireLock (&Snp->Lock); + } else { + EfiReleaseLock (&Snp->Lock); + } +} + +/** + Delay MicroSeconds of micro seconds. + + This is a callback routine supplied to UNDI at undi_start time. + + @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to + store Undi interface context (Undi does not read or write + this variable). + @param MicroSeconds Number of micro seconds to pause, ususlly multiple of 10. + +**/ +VOID +EFIAPI +SnpUndi32CallbackDelay ( + IN UINT64 UniqueId, + IN UINT64 MicroSeconds + ) +{ + if (MicroSeconds != 0) { + gBS->Stall ((UINTN) MicroSeconds); + } +} + +/** + IO routine for UNDI3.1. + + This is a callback routine supplied to UNDI at undi_start time. + + @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this + to store Undi interface context (Undi does not read or + write this variable). + @param ReadOrWrite Indicates read or write, IO or Memory. + @param NumBytes Number of bytes to read or write. + @param MemOrPortAddr IO or memory address to read from or write to. + @param BufferPtr Memory location to read into or that contains the bytes + to write. + +**/ +VOID +EFIAPI +SnpUndi32CallbackMemio ( + IN UINT64 UniqueId, + IN UINT8 ReadOrWrite, + IN UINT8 NumBytes, + IN UINT64 MemOrPortAddr, + IN OUT UINT64 BufferPtr + ) +{ + SNP_DRIVER *Snp; + EFI_PCI_IO_PROTOCOL_WIDTH Width; + + Snp = (SNP_DRIVER *) (UINTN) UniqueId; + + Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 0; + switch (NumBytes) { + case 2: + Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 1; + break; + + case 4: + Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 2; + break; + + case 8: + Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 3; + break; + } + + switch (ReadOrWrite) { + case PXE_IO_READ: + Snp->PciIo->Io.Read ( + Snp->PciIo, + Width, + Snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address + MemOrPortAddr, + 1, // count + (VOID *) (UINTN) BufferPtr + ); + break; + + case PXE_IO_WRITE: + Snp->PciIo->Io.Write ( + Snp->PciIo, + Width, + Snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address + MemOrPortAddr, + 1, // count + (VOID *) (UINTN) BufferPtr + ); + break; + + case PXE_MEM_READ: + Snp->PciIo->Mem.Read ( + Snp->PciIo, + Width, + Snp->MemoryBarIndex, // BAR 0, Memory base address + MemOrPortAddr, + 1, // count + (VOID *) (UINTN) BufferPtr + ); + break; + + case PXE_MEM_WRITE: + Snp->PciIo->Mem.Write ( + Snp->PciIo, + Width, + Snp->MemoryBarIndex, // BAR 0, Memory base address + MemOrPortAddr, + 1, // count + (VOID *) (UINTN) BufferPtr + ); + break; + } + + return ; +} + +/** + Map a CPU address to a device address. + + This is a callback routine supplied to UNDI at undi_start time. + + @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to + store Undi interface context (Undi does not read or write + this variable). + @param CpuAddr Virtual address to be mapped. + @param NumBytes Size of memory to be mapped. + @param Direction Direction of data flow for this memory's usage: + cpu->device, device->cpu or both ways. + @param DeviceAddrPtr Pointer to return the mapped device address. + +**/ +VOID +EFIAPI +SnpUndi32CallbackMap ( + IN UINT64 UniqueId, + IN UINT64 CpuAddr, + IN UINT32 NumBytes, + IN UINT32 Direction, + IN OUT UINT64 DeviceAddrPtr + ) +{ + EFI_PHYSICAL_ADDRESS *DevAddrPtr; + EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag; + UINTN BuffSize; + SNP_DRIVER *Snp; + UINTN Index; + EFI_STATUS Status; + + BuffSize = (UINTN) NumBytes; + Snp = (SNP_DRIVER *) (UINTN) UniqueId; + DevAddrPtr = (EFI_PHYSICAL_ADDRESS *) (UINTN) DeviceAddrPtr; + + if (CpuAddr == 0) { + *DevAddrPtr = 0; + return ; + } + + switch (Direction) { + case TO_AND_FROM_DEVICE: + DirectionFlag = EfiPciIoOperationBusMasterCommonBuffer; + break; + + case FROM_DEVICE: + DirectionFlag = EfiPciIoOperationBusMasterWrite; + break; + + case TO_DEVICE: + DirectionFlag = EfiPciIoOperationBusMasterRead; + break; + + default: + *DevAddrPtr = 0; + // + // any non zero indicates error! + // + return ; + } + // + // find an unused map_list entry + // + for (Index = 0; Index < MAX_MAP_LENGTH; Index++) { + if (Snp->MapList[Index].VirtualAddress == 0) { + break; + } + } + + if (Index >= MAX_MAP_LENGTH) { + DEBUG ((EFI_D_INFO, "SNP maplist is FULL\n")); + *DevAddrPtr = 0; + return ; + } + + Snp->MapList[Index].VirtualAddress = (EFI_PHYSICAL_ADDRESS) CpuAddr; + + Status = Snp->PciIo->Map ( + Snp->PciIo, + DirectionFlag, + (VOID *) (UINTN) CpuAddr, + &BuffSize, + DevAddrPtr, + &(Snp->MapList[Index].MapCookie) + ); + if (Status != EFI_SUCCESS) { + *DevAddrPtr = 0; + Snp->MapList[Index].VirtualAddress = 0; + } + + return ; +} + +/** + Unmap an address that was previously mapped using map callback. + + This is a callback routine supplied to UNDI at undi_start time. + + @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to + store. Undi interface context (Undi does not read or write + this variable). + @param CpuAddr Virtual address that was mapped. + @param NumBytes Size of memory mapped. + @param Direction Direction of data flow for this memory's usage: + cpu->device, device->cpu or both ways. + @param DeviceAddr The mapped device address. + +**/ +VOID +EFIAPI +SnpUndi32CallbackUnmap ( + IN UINT64 UniqueId, + IN UINT64 CpuAddr, + IN UINT32 NumBytes, + IN UINT32 Direction, + IN UINT64 DeviceAddr + ) +{ + SNP_DRIVER *Snp; + UINT16 Index; + + Snp = (SNP_DRIVER *) (UINTN) UniqueId; + + for (Index = 0; Index < MAX_MAP_LENGTH; Index++) { + if (Snp->MapList[Index].VirtualAddress == CpuAddr) { + break; + } + } + + if (Index >= MAX_MAP_LENGTH) { + DEBUG ((EFI_D_ERROR, "SNP could not find a mapping, failed to unmap.\n")); + return ; + } + + Snp->PciIo->Unmap (Snp->PciIo, Snp->MapList[Index].MapCookie); + Snp->MapList[Index].VirtualAddress = 0; + Snp->MapList[Index].MapCookie = NULL; + return ; +} + +/** + Synchronize the virtual buffer contents with the mapped buffer contents. + + This is a callback routine supplied to UNDI at undi_start time. The virtual + and mapped buffers need not correspond to the same physical memory (especially + if the virtual address is > 4GB). Depending on the direction for which the + buffer is mapped, undi will need to synchronize their contents whenever it + writes to/reads from the buffer using either the cpu address or the device + address. + EFI does not provide a sync call since virt=physical, we should just do the + synchronization ourselves here. + + @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to + store Undi interface context (Undi does not read or write + this variable). + @param CpuAddr Virtual address that was mapped. + @param NumBytes Size of memory mapped. + @param Direction Direction of data flow for this memory's usage: + cpu->device, device->cpu or both ways. + @param DeviceAddr The mapped device address. + +**/ +VOID +EFIAPI +SnpUndi32CallbackSync ( + IN UINT64 UniqueId, + IN UINT64 CpuAddr, + IN UINT32 NumBytes, + IN UINT32 Direction, + IN UINT64 DeviceAddr + ) +{ + if ((CpuAddr == 0) || (DeviceAddr == 0) || (NumBytes == 0)) { + return ; + + } + + switch (Direction) { + case FROM_DEVICE: + CopyMem ((UINT8 *) (UINTN) CpuAddr, (UINT8 *) (UINTN) DeviceAddr, NumBytes); + break; + + case TO_DEVICE: + CopyMem ((UINT8 *) (UINTN) DeviceAddr, (UINT8 *) (UINTN) CpuAddr, NumBytes); + break; + } + + return ; +} diff --git a/NetworkPkg/SnpDxe/ComponentName.c b/NetworkPkg/SnpDxe/ComponentName.c new file mode 100644 index 000000000..2139e2a47 --- /dev/null +++ b/NetworkPkg/SnpDxe/ComponentName.c @@ -0,0 +1,430 @@ +/** @file + UEFI Component Name(2) protocol implementation for SnpDxe driver. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Snp.h" + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param DriverName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SimpleNetworkComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param ControllerHandle[in] The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param ChildHandle[in] The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param ControllerName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SimpleNetworkComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gSimpleNetworkComponentName = { + SimpleNetworkComponentNameGetDriverName, + SimpleNetworkComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gSimpleNetworkComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) SimpleNetworkComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) SimpleNetworkComponentNameGetControllerName, + "en" +}; + + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mSimpleNetworkDriverNameTable[] = { + { + "eng;en", + L"Simple Network Protocol Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gSimpleNetworkControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param DriverName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SimpleNetworkComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mSimpleNetworkDriverNameTable, + DriverName, + (BOOLEAN)(This == &gSimpleNetworkComponentName) + ); +} + +/** + Update the component name for the Snp child handle. + + @param Snp[in] A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateName ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[80]; + UINTN OffSet; + UINTN Index; + + if (Snp == NULL) { + return EFI_INVALID_PARAMETER; + } + + OffSet = 0; + OffSet += UnicodeSPrint ( + HandleName, + sizeof (HandleName), + L"SNP (MAC=" + ); + for (Index = 0; Index < Snp->Mode->HwAddressSize; Index++) { + OffSet += UnicodeSPrint ( + HandleName + OffSet, + sizeof (HandleName) - OffSet * sizeof (CHAR16), + L"%02X-", + Snp->Mode->CurrentAddress.Addr[Index] + ); + } + ASSERT (OffSet > 0); + // + // Remove the last '-' + // + OffSet--; + OffSet += UnicodeSPrint ( + HandleName + OffSet, + sizeof (HandleName) - OffSet * sizeof (CHAR16), + L")" + ); + if (gSimpleNetworkControllerNameTable != NULL) { + FreeUnicodeStringTable (gSimpleNetworkControllerNameTable); + gSimpleNetworkControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gSimpleNetworkComponentName.SupportedLanguages, + &gSimpleNetworkControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gSimpleNetworkComponentName2.SupportedLanguages, + &gSimpleNetworkControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + Currently not implemented. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param ControllerHandle[in] The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param ChildHandle[in] The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param ControllerName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +SimpleNetworkComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + + if (ChildHandle != NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver is currently managing ControllHandle + // + Status = EfiTestManagedDevice ( + ControllerHandle, + gSimpleNetworkDriverBinding.DriverBindingHandle, + &gEfiSimpleNetworkProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ControllerHandle + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiSimpleNetworkProtocolGuid, + (VOID **)&Snp, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + // + // Update the component name for this child handle. + // + Status = UpdateName (Snp); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gSimpleNetworkControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gSimpleNetworkComponentName) + ); +} diff --git a/NetworkPkg/SnpDxe/Get_status.c b/NetworkPkg/SnpDxe/Get_status.c new file mode 100644 index 000000000..be6608a0b --- /dev/null +++ b/NetworkPkg/SnpDxe/Get_status.c @@ -0,0 +1,257 @@ +/** @file + Implementation of reading the current interrupt status and recycled transmit + buffer status from a network interface. + +Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + +/** + Call undi to get the status of the interrupts, get the list of recycled transmit + buffers that completed transmitting. The recycled transmit buffer address will + be saved into Snp->RecycledTxBuf. This function will also update the MediaPresent + field of EFI_SIMPLE_NETWORK_MODE if UNDI support it. + + @param[in] Snp Pointer to snp driver structure. + @param[out] InterruptStatusPtr A non null pointer to contain the interrupt + status. + @param[in] GetTransmittedBuf Set to TRUE to retrieve the recycled transmit + buffer address. + + @retval EFI_SUCCESS The status of the network interface was retrieved. + @retval EFI_DEVICE_ERROR The command could not be sent to the network + interface. + +**/ +EFI_STATUS +PxeGetStatus ( + IN SNP_DRIVER *Snp, + OUT UINT32 *InterruptStatusPtr, + IN BOOLEAN GetTransmittedBuf + ) +{ + PXE_DB_GET_STATUS *Db; + UINT16 InterruptFlags; + UINT32 Index; + UINT64 *Tmp; + + Tmp = NULL; + Db = Snp->Db; + Snp->Cdb.OpCode = PXE_OPCODE_GET_STATUS; + + Snp->Cdb.OpFlags = 0; + + if (GetTransmittedBuf) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_GET_TRANSMITTED_BUFFERS; + ZeroMem (Db->TxBuffer, sizeof (Db->TxBuffer)); + } + + if (InterruptStatusPtr != NULL) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_GET_INTERRUPT_STATUS; + } + + if (Snp->MediaStatusSupported) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_GET_MEDIA_STATUS; + } + + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + + Snp->Cdb.DBsize = (UINT16) sizeof (PXE_DB_GET_STATUS); + Snp->Cdb.DBaddr = (UINT64)(UINTN) Db; + + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nSnp->undi.get_status() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) { + DEBUG ( + (EFI_D_NET, + "\nSnp->undi.get_status() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_DEVICE_ERROR; + } + // + // report the values back.. + // + if (InterruptStatusPtr != NULL) { + InterruptFlags = (UINT16) (Snp->Cdb.StatFlags & PXE_STATFLAGS_GET_STATUS_INTERRUPT_MASK); + + *InterruptStatusPtr = 0; + + if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_RECEIVE) == PXE_STATFLAGS_GET_STATUS_RECEIVE) { + *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT; + } + + if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_TRANSMIT) == PXE_STATFLAGS_GET_STATUS_TRANSMIT) { + *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT; + } + + if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_COMMAND) == PXE_STATFLAGS_GET_STATUS_COMMAND) { + *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_COMMAND_INTERRUPT; + } + + if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_SOFTWARE) == PXE_STATFLAGS_GET_STATUS_SOFTWARE) { + *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_COMMAND_INTERRUPT; + } + + } + + if (GetTransmittedBuf) { + if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_GET_STATUS_NO_TXBUFS_WRITTEN) == 0) { + // + // UNDI has written some transmitted buffer addresses into the DB. Store them into Snp->RecycledTxBuf. + // + for (Index = 0; Index < MAX_XMIT_BUFFERS; Index++) { + if (Db->TxBuffer[Index] != 0) { + if (Snp->RecycledTxBufCount == Snp->MaxRecycledTxBuf) { + // + // Snp->RecycledTxBuf is full, reallocate a new one. + // + if ((Snp->MaxRecycledTxBuf + SNP_TX_BUFFER_INCREASEMENT) >= SNP_MAX_TX_BUFFER_NUM) { + return EFI_DEVICE_ERROR; + } + Tmp = AllocatePool (sizeof (UINT64) * (Snp->MaxRecycledTxBuf + SNP_TX_BUFFER_INCREASEMENT)); + if (Tmp == NULL) { + return EFI_DEVICE_ERROR; + } + CopyMem (Tmp, Snp->RecycledTxBuf, sizeof (UINT64) * Snp->RecycledTxBufCount); + FreePool (Snp->RecycledTxBuf); + Snp->RecycledTxBuf = Tmp; + Snp->MaxRecycledTxBuf += SNP_TX_BUFFER_INCREASEMENT; + } + Snp->RecycledTxBuf[Snp->RecycledTxBufCount] = Db->TxBuffer[Index]; + Snp->RecycledTxBufCount++; + } + } + } + } + + // + // Update MediaPresent field of EFI_SIMPLE_NETWORK_MODE if the UNDI support + // returning media status from GET_STATUS command + // + if (Snp->MediaStatusSupported) { + Snp->Snp.Mode->MediaPresent = + (BOOLEAN) (((Snp->Cdb.StatFlags & PXE_STATFLAGS_GET_STATUS_NO_MEDIA) != 0) ? FALSE : TRUE); + } + + return EFI_SUCCESS; +} + +/** + Reads the current interrupt status and recycled transmit buffer status from a + network interface. + + This function gets the current interrupt and recycled transmit buffer status + from the network interface. The interrupt status is returned as a bit mask in + InterruptStatus. If InterruptStatus is NULL, the interrupt status will not be + read. If TxBuf is not NULL, a recycled transmit buffer address will be retrieved. + If a recycled transmit buffer address is returned in TxBuf, then the buffer has + been successfully transmitted, and the status for that buffer is cleared. If + the status of the network interface is successfully collected, EFI_SUCCESS + will be returned. If the driver has not been initialized, EFI_DEVICE_ERROR will + be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param InterruptStatus A pointer to the bit mask of the currently active + interrupts (see "Related Definitions"). If this is NULL, + the interrupt status will not be read from the device. + If this is not NULL, the interrupt status will be read + from the device. When the interrupt status is read, it + will also be cleared. Clearing the transmit interrupt does + not empty the recycled transmit buffer array. + @param TxBuf Recycled transmit buffer address. The network interface + will not transmit if its internal recycled transmit + buffer array is full. Reading the transmit buffer does + not clear the transmit interrupt. If this is NULL, then + the transmit buffer status will not be read. If there + are no transmit buffers to recycle and TxBuf is not NULL, + TxBuf will be set to NULL. + + @retval EFI_SUCCESS The status of the network interface was retrieved. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure. + @retval EFI_DEVICE_ERROR The command could not be sent to the network + interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32GetStatus ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + OUT UINT32 *InterruptStatus, OPTIONAL + OUT VOID **TxBuf OPTIONAL + ) +{ + SNP_DRIVER *Snp; + EFI_TPL OldTpl; + EFI_STATUS Status; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (InterruptStatus == NULL && TxBuf == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (Snp == NULL) { + return EFI_DEVICE_ERROR; + } + + switch (Snp->Mode.State) { + case EfiSimpleNetworkInitialized: + break; + + case EfiSimpleNetworkStopped: + Status = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + if (Snp->RecycledTxBufCount == 0 && TxBuf != NULL) { + Status = PxeGetStatus (Snp, InterruptStatus, TRUE); + } else { + Status = PxeGetStatus (Snp, InterruptStatus, FALSE); + } + + if (TxBuf != NULL) { + // + // Get a recycled buf from Snp->RecycledTxBuf + // + if (Snp->RecycledTxBufCount == 0) { + *TxBuf = NULL; + } else { + Snp->RecycledTxBufCount--; + *TxBuf = (VOID *) (UINTN) Snp->RecycledTxBuf[Snp->RecycledTxBufCount]; + } + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/Initialize.c b/NetworkPkg/SnpDxe/Initialize.c new file mode 100644 index 000000000..4d3315424 --- /dev/null +++ b/NetworkPkg/SnpDxe/Initialize.c @@ -0,0 +1,277 @@ +/** @file + Implementation of initializing a network adapter. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Snp.h" + +/** + Call UNDI to initialize the interface. + + @param Snp Pointer to snp driver structure. + @param CableDetectFlag Do/don't detect the cable (depending on what + undi supports). + + @retval EFI_SUCCESS UNDI is initialized successfully. + @retval EFI_DEVICE_ERROR UNDI could not be initialized. + @retval Other Other errors as indicated. + +**/ +EFI_STATUS +PxeInit ( + SNP_DRIVER *Snp, + UINT16 CableDetectFlag + ) +{ + PXE_CPB_INITIALIZE *Cpb; + VOID *Addr; + EFI_STATUS Status; + + Status = EFI_SUCCESS; + + Cpb = Snp->Cpb; + if (Snp->TxRxBufferSize != 0) { + Status = Snp->PciIo->AllocateBuffer ( + Snp->PciIo, + AllocateAnyPages, + EfiBootServicesData, + SNP_MEM_PAGES (Snp->TxRxBufferSize), + &Addr, + 0 + ); + + if (Status != EFI_SUCCESS) { + DEBUG ( + (EFI_D_ERROR, + "\nSnp->PxeInit() AllocateBuffer %xh (%r)\n", + Status, + Status) + ); + + return Status; + } + + ASSERT (Addr); + + Snp->TxRxBuffer = Addr; + } + + Cpb->MemoryAddr = (UINT64)(UINTN) Snp->TxRxBuffer; + + Cpb->MemoryLength = Snp->TxRxBufferSize; + + // + // let UNDI decide/detect these values + // + Cpb->LinkSpeed = 0; + Cpb->TxBufCnt = 0; + Cpb->TxBufSize = 0; + Cpb->RxBufCnt = 0; + Cpb->RxBufSize = 0; + + Cpb->DuplexMode = PXE_DUPLEX_DEFAULT; + + Cpb->LoopBackMode = LOOPBACK_NORMAL; + + Snp->Cdb.OpCode = PXE_OPCODE_INITIALIZE; + Snp->Cdb.OpFlags = CableDetectFlag; + + Snp->Cdb.CPBsize = (UINT16) sizeof (PXE_CPB_INITIALIZE); + Snp->Cdb.DBsize = (UINT16) sizeof (PXE_DB_INITIALIZE); + + Snp->Cdb.CPBaddr = (UINT64)(UINTN) Snp->Cpb; + Snp->Cdb.DBaddr = (UINT64)(UINTN) Snp->Db; + + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + DEBUG ((EFI_D_NET, "\nSnp->undi.initialize() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + // + // There are two fields need to be checked here: + // First is the upper two bits (14 & 15) in the CDB.StatFlags field. Until these bits change to report + // PXE_STATFLAGS_COMMAND_COMPLETE or PXE_STATFLAGS_COMMAND_FAILED, the command has not been executed by the UNDI. + // Second is the CDB.StatCode field. After command execution completes, either successfully or not, + // the CDB.StatCode field contains the result of the command execution. + // + if ((((Snp->Cdb.StatFlags) & PXE_STATFLAGS_STATUS_MASK) == PXE_STATFLAGS_COMMAND_COMPLETE) && + (Snp->Cdb.StatCode == PXE_STATCODE_SUCCESS)) { + // + // If cable detect feature is enabled in CDB.OpFlags, check the CDB.StatFlags to see if there is an + // active connection to this network device. If the no media StatFlag is set, the UNDI and network + // device are still initialized. + // + if (CableDetectFlag == PXE_OPFLAGS_INITIALIZE_DETECT_CABLE) { + if(((Snp->Cdb.StatFlags) & PXE_STATFLAGS_INITIALIZED_NO_MEDIA) != PXE_STATFLAGS_INITIALIZED_NO_MEDIA) { + Snp->Mode.MediaPresent = TRUE; + } else { + Snp->Mode.MediaPresent = FALSE; + } + } + + Snp->Mode.State = EfiSimpleNetworkInitialized; + Status = EFI_SUCCESS; + } else { + DEBUG ( + (EFI_D_WARN, + "\nSnp->undi.initialize() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + if (Snp->TxRxBuffer != NULL) { + Snp->PciIo->FreeBuffer ( + Snp->PciIo, + SNP_MEM_PAGES (Snp->TxRxBufferSize), + (VOID *) Snp->TxRxBuffer + ); + } + + Snp->TxRxBuffer = NULL; + + Status = EFI_DEVICE_ERROR; + } + + return Status; +} + + +/** + Resets a network adapter and allocates the transmit and receive buffers + required by the network interface; optionally, also requests allocation of + additional transmit and receive buffers. + + This function allocates the transmit and receive buffers required by the network + interface. If this allocation fails, then EFI_OUT_OF_RESOURCES is returned. + If the allocation succeeds and the network interface is successfully initialized, + then EFI_SUCCESS will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + + @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer space + that the driver should allocate for the network interface. + Some network interfaces will not be able to use the + extra buffer, and the caller will not know if it is + actually being used. + @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space + that the driver should allocate for the network interface. + Some network interfaces will not be able to use the + extra buffer, and the caller will not know if it is + actually being used. + + @retval EFI_SUCCESS The network interface was initialized. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit and + receive buffers. + @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure. + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + @retval EFI_UNSUPPORTED The increased buffer size feature is not supported. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Initialize ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN UINTN ExtraRxBufferSize OPTIONAL, + IN UINTN ExtraTxBufferSize OPTIONAL + ) +{ + EFI_STATUS EfiStatus; + SNP_DRIVER *Snp; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (Snp == NULL) { + EfiStatus = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + switch (Snp->Mode.State) { + case EfiSimpleNetworkStarted: + break; + + case EfiSimpleNetworkStopped: + EfiStatus = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + EfiStatus = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + EfiStatus = gBS->CreateEvent ( + EVT_NOTIFY_WAIT, + TPL_NOTIFY, + &SnpWaitForPacketNotify, + Snp, + &Snp->Snp.WaitForPacket + ); + + if (EFI_ERROR (EfiStatus)) { + Snp->Snp.WaitForPacket = NULL; + EfiStatus = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + // + // + // + Snp->Mode.MCastFilterCount = 0; + Snp->Mode.ReceiveFilterSetting = 0; + ZeroMem (Snp->Mode.MCastFilter, sizeof Snp->Mode.MCastFilter); + CopyMem ( + &Snp->Mode.CurrentAddress, + &Snp->Mode.PermanentAddress, + sizeof (EFI_MAC_ADDRESS) + ); + + // + // Compute tx/rx buffer sizes based on UNDI init info and parameters. + // + Snp->TxRxBufferSize = (UINT32) (Snp->InitInfo.MemoryRequired + ExtraRxBufferSize + ExtraTxBufferSize); + + // + // If UNDI support cable detect for INITIALIZE command, try it first. + // + if (Snp->CableDetectSupported) { + if (PxeInit (Snp, PXE_OPFLAGS_INITIALIZE_DETECT_CABLE) == EFI_SUCCESS) { + goto ON_EXIT; + } + } + + Snp->Mode.MediaPresent = FALSE; + + EfiStatus = PxeInit (Snp, PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE); + + if (EFI_ERROR (EfiStatus)) { + gBS->CloseEvent (Snp->Snp.WaitForPacket); + goto ON_EXIT; + } + + // + // Try to update the MediaPresent field of EFI_SIMPLE_NETWORK_MODE if the UNDI support it. + // + if (Snp->MediaStatusSupported) { + PxeGetStatus (Snp, NULL, FALSE); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return EfiStatus; +} diff --git a/NetworkPkg/SnpDxe/Mcast_ip_to_mac.c b/NetworkPkg/SnpDxe/Mcast_ip_to_mac.c new file mode 100644 index 000000000..9863c1f9e --- /dev/null +++ b/NetworkPkg/SnpDxe/Mcast_ip_to_mac.c @@ -0,0 +1,173 @@ +/** @file + Implementation of converting an multicast IP address to multicast HW MAC + address. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + +/** + Call undi to convert an multicast IP address to a MAC address. + + @param Snp Pointer to snp driver structure. + @param IPv6 Flag to indicate if this is an ipv6 address. + @param IP Multicast IP address. + @param MAC Pointer to hold the return MAC address. + + @retval EFI_SUCCESS The multicast IP address was mapped to the + multicast HW MAC address. + @retval EFI_INVALID_PARAMETER Invalid UNDI command. + @retval EFI_UNSUPPORTED Command is not supported by UNDI. + @retval EFI_DEVICE_ERROR Fail to execute UNDI command. + +**/ +EFI_STATUS +PxeIp2Mac ( + IN SNP_DRIVER *Snp, + IN BOOLEAN IPv6, + IN EFI_IP_ADDRESS *IP, + IN OUT EFI_MAC_ADDRESS *MAC + ) +{ + PXE_CPB_MCAST_IP_TO_MAC *Cpb; + PXE_DB_MCAST_IP_TO_MAC *Db; + + Cpb = Snp->Cpb; + Db = Snp->Db; + Snp->Cdb.OpCode = PXE_OPCODE_MCAST_IP_TO_MAC; + Snp->Cdb.OpFlags = (UINT16) (IPv6 ? PXE_OPFLAGS_MCAST_IPV6_TO_MAC : PXE_OPFLAGS_MCAST_IPV4_TO_MAC); + Snp->Cdb.CPBsize = (UINT16) sizeof (PXE_CPB_MCAST_IP_TO_MAC); + Snp->Cdb.DBsize = (UINT16) sizeof (PXE_DB_MCAST_IP_TO_MAC); + + Snp->Cdb.CPBaddr = (UINT64)(UINTN) Cpb; + Snp->Cdb.DBaddr = (UINT64)(UINTN) Db; + + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + CopyMem (&Cpb->IP, IP, sizeof (PXE_IP_ADDR)); + + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nSnp->undi.mcast_ip_to_mac() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + switch (Snp->Cdb.StatCode) { + case PXE_STATCODE_SUCCESS: + break; + + case PXE_STATCODE_INVALID_CPB: + return EFI_INVALID_PARAMETER; + + case PXE_STATCODE_UNSUPPORTED: + DEBUG ( + (EFI_D_NET, + "\nSnp->undi.mcast_ip_to_mac() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + return EFI_UNSUPPORTED; + + default: + // + // UNDI command failed. Return EFI_DEVICE_ERROR + // to caller. + // + DEBUG ( + (EFI_D_NET, + "\nSnp->undi.mcast_ip_to_mac() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_DEVICE_ERROR; + } + + CopyMem (MAC, &Db->MAC, sizeof (PXE_MAC_ADDR)); + return EFI_SUCCESS; +} + + +/** + Converts a multicast IP address to a multicast HW MAC address. + + This function converts a multicast IP address to a multicast HW MAC address + for all packet transactions. If the mapping is accepted, then EFI_SUCCESS will + be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param IPv6 Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. + Set to FALSE if the multicast IP address is IPv4 [RFC 791]. + @param IP The multicast IP address that is to be converted to a multicast + HW MAC address. + @param MAC The multicast HW MAC address that is to be generated from IP. + + @retval EFI_SUCCESS The multicast IP address was mapped to the + multicast HW MAC address. + @retval EFI_NOT_STARTED The Simple Network Protocol interface has not + been started by calling Start(). + @retval EFI_INVALID_PARAMETER IP is NULL. + @retval EFI_INVALID_PARAMETER MAC is NULL. + @retval EFI_INVALID_PARAMETER IP does not point to a valid IPv4 or IPv6 + multicast address. + @retval EFI_DEVICE_ERROR The Simple Network Protocol interface has not + been initialized by calling Initialize(). + @retval EFI_UNSUPPORTED IPv6 is TRUE and the implementation does not + support IPv6 multicast to MAC address conversion. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32McastIpToMac ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN BOOLEAN IPv6, + IN EFI_IP_ADDRESS *IP, + OUT EFI_MAC_ADDRESS *MAC + ) +{ + SNP_DRIVER *Snp; + EFI_TPL OldTpl; + EFI_STATUS Status; + + // + // Get pointer to SNP driver instance for *this. + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (IP == NULL || MAC == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + switch (Snp->Mode.State) { + case EfiSimpleNetworkInitialized: + break; + + case EfiSimpleNetworkStopped: + Status = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + Status = PxeIp2Mac (Snp, IPv6, IP, MAC); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/Nvdata.c b/NetworkPkg/SnpDxe/Nvdata.c new file mode 100644 index 000000000..7010b63a5 --- /dev/null +++ b/NetworkPkg/SnpDxe/Nvdata.c @@ -0,0 +1,217 @@ +/** @file + Implementation of reading and writing operations on the NVRAM device + attached to a network interface. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + + +/** + This routine calls Undi to read the desired number of eeprom bytes. + + @param Snp pointer to the snp driver structure + @param Offset eeprom register value relative to the base address + @param BufferSize number of bytes to read + @param Buffer pointer where to read into + + @retval EFI_SUCCESS The NVRAM access was performed. + @retval EFI_INVALID_PARAMETER Invalid UNDI command. + @retval EFI_UNSUPPORTED Command is not supported by UNDI. + @retval EFI_DEVICE_ERROR Fail to execute UNDI command. + +**/ +EFI_STATUS +PxeNvDataRead ( + IN SNP_DRIVER *Snp, + IN UINTN Offset, + IN UINTN BufferSize, + IN OUT VOID *Buffer + ) +{ + PXE_DB_NVDATA *Db; + + Db = Snp->Db; + Snp->Cdb.OpCode = PXE_OPCODE_NVDATA; + + Snp->Cdb.OpFlags = PXE_OPFLAGS_NVDATA_READ; + + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + + Snp->Cdb.DBsize = (UINT16) sizeof (PXE_DB_NVDATA); + Snp->Cdb.DBaddr = (UINT64)(UINTN) Db; + + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nsnp->undi.nvdata () ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + switch (Snp->Cdb.StatCode) { + case PXE_STATCODE_SUCCESS: + break; + + case PXE_STATCODE_UNSUPPORTED: + DEBUG ( + (EFI_D_NET, + "\nsnp->undi.nvdata() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_UNSUPPORTED; + + default: + DEBUG ( + (EFI_D_NET, + "\nsnp->undi.nvdata() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_DEVICE_ERROR; + } + + ASSERT (Offset < sizeof (Db->Data)); + + CopyMem (Buffer, &Db->Data.Byte[Offset], BufferSize); + + return EFI_SUCCESS; +} + + +/** + Performs read and write operations on the NVRAM device attached to a network + interface. + + This function performs read and write operations on the NVRAM device attached + to a network interface. If ReadWrite is TRUE, a read operation is performed. + If ReadWrite is FALSE, a write operation is performed. Offset specifies the + byte offset at which to start either operation. Offset must be a multiple of + NvRamAccessSize , and it must have a value between zero and NvRamSize. + BufferSize specifies the length of the read or write operation. BufferSize must + also be a multiple of NvRamAccessSize, and Offset + BufferSize must not exceed + NvRamSize. + If any of the above conditions is not met, then EFI_INVALID_PARAMETER will be + returned. + If all the conditions are met and the operation is "read," the NVRAM device + attached to the network interface will be read into Buffer and EFI_SUCCESS + will be returned. If this is a write operation, the contents of Buffer will be + used to update the contents of the NVRAM device attached to the network + interface and EFI_SUCCESS will be returned. + + It does the basic checking on the input parameters and retrieves snp structure + and then calls the read_nvdata() call which does the actual reading + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param ReadWrite TRUE for read operations, FALSE for write operations. + @param Offset Byte offset in the NVRAM device at which to start the read or + write operation. This must be a multiple of NvRamAccessSize + and less than NvRamSize. (See EFI_SIMPLE_NETWORK_MODE) + @param BufferSize The number of bytes to read or write from the NVRAM device. + This must also be a multiple of NvramAccessSize. + @param Buffer A pointer to the data buffer. + + @retval EFI_SUCCESS The NVRAM access was performed. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + * The This parameter is NULL + * The This parameter does not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure + * The Offset parameter is not a multiple of + EFI_SIMPLE_NETWORK_MODE.NvRamAccessSize + * The Offset parameter is not less than + EFI_SIMPLE_NETWORK_MODE.NvRamSize + * The BufferSize parameter is not a multiple of + EFI_SIMPLE_NETWORK_MODE.NvRamAccessSize + * The Buffer parameter is NULL + @retval EFI_DEVICE_ERROR The command could not be sent to the network + interface. + @retval EFI_UNSUPPORTED This function is not supported by the network + interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32NvData ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN BOOLEAN ReadWrite, + IN UINTN Offset, + IN UINTN BufferSize, + IN OUT VOID *Buffer + ) +{ + SNP_DRIVER *Snp; + EFI_TPL OldTpl; + EFI_STATUS Status; + + // + // Get pointer to SNP driver instance for *this. + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Return error if the SNP is not initialized. + // + switch (Snp->Mode.State) { + case EfiSimpleNetworkInitialized: + break; + + case EfiSimpleNetworkStopped: + Status = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + // + // Return error if non-volatile memory variables are not valid. + // + if (Snp->Mode.NvRamSize == 0 || Snp->Mode.NvRamAccessSize == 0) { + Status = EFI_UNSUPPORTED; + goto ON_EXIT; + } + // + // Check for invalid parameter combinations. + // + if ((BufferSize == 0) || + (Buffer == NULL) || + (Offset >= Snp->Mode.NvRamSize) || + (Offset + BufferSize > Snp->Mode.NvRamSize) || + (BufferSize % Snp->Mode.NvRamAccessSize != 0) || + (Offset % Snp->Mode.NvRamAccessSize != 0) + ) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + // + // check the implementation flags of undi if we can write the nvdata! + // + if (!ReadWrite) { + Status = EFI_UNSUPPORTED; + } else { + Status = PxeNvDataRead (Snp, Offset, BufferSize, Buffer); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/Receive.c b/NetworkPkg/SnpDxe/Receive.c new file mode 100644 index 000000000..28cea0d2e --- /dev/null +++ b/NetworkPkg/SnpDxe/Receive.c @@ -0,0 +1,251 @@ +/** @file + Implementation of receiving a packet from a network interface. + +Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Snp.h" + +/** + Call UNDI to receive a packet and fills in the data in the input pointers. + + @param Snp Pointer to snp driver structure + @param Buffer Pointer to the memory for the received data + @param BufferSize Pointer to the length of the buffer on entry and contains + the length of the received data on return + @param HeaderSize Pointer to the header portion of the data received. + @param SrcAddr Pointer to contain the source ethernet address on return + @param DestAddr Pointer to contain the destination ethernet address on + return + @param Protocol Pointer to contain the protocol type from the ethernet + header on return + + + @retval EFI_SUCCESS The received data was stored in Buffer, and + BufferSize has been updated to the number of + bytes received. + @retval EFI_DEVICE_ERROR Fail to execute UNDI command. + @retval EFI_NOT_READY No packets have been received on the network + interface. + @retval EFI_BUFFER_TOO_SMALL BufferSize is too small for the received + packets. BufferSize has been updated to the + required size. + +**/ +EFI_STATUS +PxeReceive ( + SNP_DRIVER *Snp, + VOID *Buffer, + UINTN *BufferSize, + UINTN *HeaderSize, + EFI_MAC_ADDRESS *SrcAddr, + EFI_MAC_ADDRESS *DestAddr, + UINT16 *Protocol + ) +{ + PXE_CPB_RECEIVE *Cpb; + PXE_DB_RECEIVE *Db; + UINTN BuffSize; + + Cpb = Snp->Cpb; + Db = Snp->Db; + BuffSize = *BufferSize; + + Cpb->BufferAddr = (UINT64)(UINTN) Buffer; + Cpb->BufferLen = (UINT32) *BufferSize; + + Cpb->reserved = 0; + + Snp->Cdb.OpCode = PXE_OPCODE_RECEIVE; + Snp->Cdb.OpFlags = PXE_OPFLAGS_NOT_USED; + + Snp->Cdb.CPBsize = (UINT16) sizeof (PXE_CPB_RECEIVE); + Snp->Cdb.CPBaddr = (UINT64)(UINTN) Cpb; + + Snp->Cdb.DBsize = (UINT16) sizeof (PXE_DB_RECEIVE); + Snp->Cdb.DBaddr = (UINT64)(UINTN) Db; + + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nsnp->undi.receive () ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + switch (Snp->Cdb.StatCode) { + case PXE_STATCODE_SUCCESS: + break; + + case PXE_STATCODE_NO_DATA: + DEBUG ( + (EFI_D_NET, + "\nsnp->undi.receive () %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_NOT_READY; + + default: + DEBUG ( + (EFI_D_ERROR, + "\nsnp->undi.receive() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_DEVICE_ERROR; + } + + *BufferSize = Db->FrameLen; + + if (HeaderSize != NULL) { + *HeaderSize = Db->MediaHeaderLen; + } + + if (SrcAddr != NULL) { + CopyMem (SrcAddr, &Db->SrcAddr, Snp->Mode.HwAddressSize); + } + + if (DestAddr != NULL) { + CopyMem (DestAddr, &Db->DestAddr, Snp->Mode.HwAddressSize); + } + + if (Protocol != NULL) { + // + // We need to do the byte swapping + // + *Protocol = (UINT16) PXE_SWAP_UINT16 (Db->Protocol); + } + + // + // We have received a packet from network interface, which implies that the + // network cable should be present. While, some UNDI driver may not report + // correct media status during Snp->Initialize(). So, we need ensure + // MediaPresent in SNP mode data is set to correct value. + // + if (Snp->Mode.MediaPresentSupported && !Snp->Mode.MediaPresent) { + Snp->Mode.MediaPresent = TRUE; + } + + return (*BufferSize <= BuffSize) ? EFI_SUCCESS : EFI_BUFFER_TOO_SMALL; +} + +/** + Receives a packet from a network interface. + + This function retrieves one packet from the receive queue of a network interface. + If there are no packets on the receive queue, then EFI_NOT_READY will be + returned. If there is a packet on the receive queue, and the size of the packet + is smaller than BufferSize, then the contents of the packet will be placed in + Buffer, and BufferSize will be updated with the actual size of the packet. + In addition, if SrcAddr, DestAddr, and Protocol are not NULL, then these values + will be extracted from the media header and returned. EFI_SUCCESS will be + returned if a packet was successfully received. + If BufferSize is smaller than the received packet, then the size of the receive + packet will be placed in BufferSize and EFI_BUFFER_TOO_SMALL will be returned. + If the driver has not been initialized, EFI_DEVICE_ERROR will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param HeaderSize The size, in bytes, of the media header received on the network + interface. If this parameter is NULL, then the media header size + will not be returned. + @param BufferSize On entry, the size, in bytes, of Buffer. On exit, the size, in + bytes, of the packet that was received on the network interface. + @param Buffer A pointer to the data buffer to receive both the media + header and the data. + @param SrcAddr The source HW MAC address. If this parameter is NULL, the HW + MAC source address will not be extracted from the media header. + @param DestAddr The destination HW MAC address. If this parameter is NULL, + the HW MAC destination address will not be extracted from + the media header. + @param Protocol The media header type. If this parameter is NULL, then the + protocol will not be extracted from the media header. See + RFC 1700 section "Ether Types" for examples. + + @retval EFI_SUCCESS The received data was stored in Buffer, and + BufferSize has been updated to the number of + bytes received. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_NOT_READY No packets have been received on the network interface. + @retval EFI_BUFFER_TOO_SMALL BufferSize is too small for the received packets. + BufferSize has been updated to the required size. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + * The This parameter is NULL + * The This parameter does not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure. + * The BufferSize parameter is NULL + * The Buffer parameter is NULL + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Receive ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + OUT UINTN *HeaderSize OPTIONAL, + IN OUT UINTN *BufferSize, + OUT VOID *Buffer, + OUT EFI_MAC_ADDRESS *SrcAddr OPTIONAL, + OUT EFI_MAC_ADDRESS *DestAddr OPTIONAL, + OUT UINT16 *Protocol OPTIONAL + ) +{ + SNP_DRIVER *Snp; + EFI_TPL OldTpl; + EFI_STATUS Status; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + switch (Snp->Mode.State) { + case EfiSimpleNetworkInitialized: + break; + + case EfiSimpleNetworkStopped: + Status = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + if ((BufferSize == NULL) || (Buffer == NULL)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + if (Snp->Mode.ReceiveFilterSetting == 0) { + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + Status = PxeReceive ( + Snp, + Buffer, + BufferSize, + HeaderSize, + SrcAddr, + DestAddr, + Protocol + ); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/Receive_filters.c b/NetworkPkg/SnpDxe/Receive_filters.c new file mode 100644 index 000000000..494d2709b --- /dev/null +++ b/NetworkPkg/SnpDxe/Receive_filters.c @@ -0,0 +1,478 @@ +/** @file + Implementation of managing the multicast receive filters of a network + interface. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + + +#include "Snp.h" + +/** + Call undi to enable the receive filters. + + @param Snp Pointer to snp driver structure. + @param EnableFlags Bit mask for enabling the receive filters. + @param MCastAddressCount Multicast address count for a new multicast address + list. + @param MCastAddressList List of new multicast addresses. + + @retval EFI_SUCCESS The multicast receive filter list was updated. + @retval EFI_INVALID_PARAMETER Invalid UNDI command. + @retval EFI_UNSUPPORTED Command is not supported by UNDI. + @retval EFI_DEVICE_ERROR Fail to execute UNDI command. + +**/ +EFI_STATUS +PxeRecvFilterEnable ( + SNP_DRIVER *Snp, + UINT32 EnableFlags, + UINTN MCastAddressCount, + EFI_MAC_ADDRESS *MCastAddressList + ) +{ + Snp->Cdb.OpCode = PXE_OPCODE_RECEIVE_FILTERS; + Snp->Cdb.OpFlags = PXE_OPFLAGS_RECEIVE_FILTER_ENABLE; + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED; + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED; + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_UNICAST) != 0) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_UNICAST; + } + + if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST) != 0) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST; + } + + if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS) != 0) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS; + } + + if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) != 0) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST; + } + + if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) != 0) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST; + } + + if (MCastAddressCount != 0) { + Snp->Cdb.CPBsize = (UINT16) (MCastAddressCount * sizeof (EFI_MAC_ADDRESS)); + Snp->Cdb.CPBaddr = (UINT64)(UINTN)Snp->Cpb; + CopyMem (Snp->Cpb, MCastAddressList, Snp->Cdb.CPBsize); + } + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nsnp->undi.receive_filters() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) { + // + // UNDI command failed. Return UNDI status to caller. + // + DEBUG ( + (EFI_D_ERROR, + "\nsnp->undi.receive_filters() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + switch (Snp->Cdb.StatCode) { + case PXE_STATCODE_INVALID_CDB: + case PXE_STATCODE_INVALID_CPB: + case PXE_STATCODE_INVALID_PARAMETER: + return EFI_INVALID_PARAMETER; + + case PXE_STATCODE_UNSUPPORTED: + return EFI_UNSUPPORTED; + } + + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + +/** + Call undi to disable the receive filters. + + @param Snp Pointer to snp driver structure + @param DisableFlags Bit mask for disabling the receive filters + @param ResetMCastList Boolean flag to reset/delete the multicast filter + list. + + @retval EFI_SUCCESS The multicast receive filter list was updated. + @retval EFI_DEVICE_ERROR Fail to execute UNDI command. + +**/ +EFI_STATUS +PxeRecvFilterDisable ( + SNP_DRIVER *Snp, + UINT32 DisableFlags, + BOOLEAN ResetMCastList + ) +{ + Snp->Cdb.OpCode = PXE_OPCODE_RECEIVE_FILTERS; + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED; + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED; + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + Snp->Cdb.OpFlags = (UINT16) ((DisableFlags != 0) ? PXE_OPFLAGS_RECEIVE_FILTER_DISABLE : PXE_OPFLAGS_NOT_USED); + + if (ResetMCastList) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_RESET_MCAST_LIST; + } + + if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_UNICAST) != 0) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_UNICAST; + } + + if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST) != 0) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST; + } + + if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS) != 0) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS; + } + + if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) != 0) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST; + } + + if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) != 0) { + Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST; + } + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nsnp->undi.receive_filters() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) { + // + // UNDI command failed. Return UNDI status to caller. + // + DEBUG ( + (EFI_D_ERROR, + "\nsnp->undi.receive_filters() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + +/** + Call undi to read the receive filters. + + @param Snp Pointer to snp driver structure. + + @retval EFI_SUCCESS The receive filter was read. + @retval EFI_DEVICE_ERROR Fail to execute UNDI command. + +**/ +EFI_STATUS +PxeRecvFilterRead ( + SNP_DRIVER *Snp + ) +{ + Snp->Cdb.OpCode = PXE_OPCODE_RECEIVE_FILTERS; + Snp->Cdb.OpFlags = PXE_OPFLAGS_RECEIVE_FILTER_READ; + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.DBsize = (UINT16) (Snp->Mode.MaxMCastFilterCount * sizeof (EFI_MAC_ADDRESS)); + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + if (Snp->Cdb.DBsize == 0) { + Snp->Cdb.DBaddr = (UINT64)(UINTN) NULL; + } else { + Snp->Cdb.DBaddr = (UINT64)(UINTN) Snp->Db; + ZeroMem (Snp->Db, Snp->Cdb.DBsize); + } + + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + DEBUG ((EFI_D_NET, "\nsnp->undi.receive_filters() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) { + // + // UNDI command failed. Return UNDI status to caller. + // + DEBUG ( + (EFI_D_ERROR, + "\nsnp->undi.receive_filters() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_DEVICE_ERROR; + } + // + // Convert UNDI32 StatFlags to EFI SNP filter flags. + // + Snp->Mode.ReceiveFilterSetting = 0; + + if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_UNICAST) != 0) { + Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST; + } + + if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_BROADCAST) != 0) { + Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST; + } + + if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_PROMISCUOUS) != 0) { + Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS; + } + + if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_ALL_MULTICAST) != 0) { + Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST; + } + + if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST) != 0) { + Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST; + } + + CopyMem (Snp->Mode.MCastFilter, Snp->Db, Snp->Cdb.DBsize); + + // + // Count number of active entries in multicast filter list. + // + { + EFI_MAC_ADDRESS ZeroMacAddr; + + SetMem (&ZeroMacAddr, sizeof ZeroMacAddr, 0); + + for (Snp->Mode.MCastFilterCount = 0; + Snp->Mode.MCastFilterCount < Snp->Mode.MaxMCastFilterCount; + Snp->Mode.MCastFilterCount++ + ) { + if (CompareMem ( + &Snp->Mode.MCastFilter[Snp->Mode.MCastFilterCount], + &ZeroMacAddr, + sizeof ZeroMacAddr + ) == 0) { + break; + } + } + } + + return EFI_SUCCESS; +} + + +/** + Manages the multicast receive filters of a network interface. + + This function is used enable and disable the hardware and software receive + filters for the underlying network device. + The receive filter change is broken down into three steps: + * The filter mask bits that are set (ON) in the Enable parameter are added to + the current receive filter settings. + * The filter mask bits that are set (ON) in the Disable parameter are subtracted + from the updated receive filter settings. + * If the resulting receive filter setting is not supported by the hardware a + more liberal setting is selected. + If the same bits are set in the Enable and Disable parameters, then the bits + in the Disable parameter takes precedence. + If the ResetMCastFilter parameter is TRUE, then the multicast address list + filter is disabled (irregardless of what other multicast bits are set in the + Enable and Disable parameters). The SNP->Mode->MCastFilterCount field is set + to zero. The Snp->Mode->MCastFilter contents are undefined. + After enabling or disabling receive filter settings, software should verify + the new settings by checking the Snp->Mode->ReceiveFilterSettings, + Snp->Mode->MCastFilterCount and Snp->Mode->MCastFilter fields. + Note: Some network drivers and/or devices will automatically promote receive + filter settings if the requested setting can not be honored. For example, if + a request for four multicast addresses is made and the underlying hardware + only supports two multicast addresses the driver might set the promiscuous + or promiscuous multicast receive filters instead. The receiving software is + responsible for discarding any extra packets that get through the hardware + receive filters. + Note: Note: To disable all receive filter hardware, the network driver must + be Shutdown() and Stopped(). Calling ReceiveFilters() with Disable set to + Snp->Mode->ReceiveFilterSettings will make it so no more packets are + returned by the Receive() function, but the receive hardware may still be + moving packets into system memory before inspecting and discarding them. + Unexpected system errors, reboots and hangs can occur if an OS is loaded + and the network devices are not Shutdown() and Stopped(). + If ResetMCastFilter is TRUE, then the multicast receive filter list on the + network interface will be reset to the default multicast receive filter list. + If ResetMCastFilter is FALSE, and this network interface allows the multicast + receive filter list to be modified, then the MCastFilterCnt and MCastFilter + are used to update the current multicast receive filter list. The modified + receive filter list settings can be found in the MCastFilter field of + EFI_SIMPLE_NETWORK_MODE. If the network interface does not allow the multicast + receive filter list to be modified, then EFI_INVALID_PARAMETER will be returned. + If the driver has not been initialized, EFI_DEVICE_ERROR will be returned. + If the receive filter mask and multicast receive filter list have been + successfully updated on the network interface, EFI_SUCCESS will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param Enable A bit mask of receive filters to enable on the network + interface. + @param Disable A bit mask of receive filters to disable on the network + interface. For backward compatibility with EFI 1.1 + platforms, the EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST bit + must be set when the ResetMCastFilter parameter is TRUE. + @param ResetMCastFilter Set to TRUE to reset the contents of the multicast + receive filters on the network interface to their + default values. + @param MCastFilterCnt Number of multicast HW MAC addresses in the new MCastFilter + list. This value must be less than or equal to the + MCastFilterCnt field of EFI_SIMPLE_NETWORK_MODE. + This field is optional if ResetMCastFilter is TRUE. + @param MCastFilter A pointer to a list of new multicast receive filter HW + MAC addresses. This list will replace any existing + multicast HW MAC address list. This field is optional + if ResetMCastFilter is TRUE. + + @retval EFI_SUCCESS The multicast receive filter list was updated. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + * This is NULL + * There are bits set in Enable that are not set + in Snp->Mode->ReceiveFilterMask + * There are bits set in Disable that are not set + in Snp->Mode->ReceiveFilterMask + * Multicast is being enabled (the + EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST bit is + set in Enable, it is not set in Disable, and + ResetMCastFilter is FALSE) and MCastFilterCount + is zero + * Multicast is being enabled and MCastFilterCount + is greater than Snp->Mode->MaxMCastFilterCount + * Multicast is being enabled and MCastFilter is NULL + * Multicast is being enabled and one or more of + the addresses in the MCastFilter list are not + valid multicast MAC addresses + @retval EFI_DEVICE_ERROR One or more of the following conditions is TRUE: + * The network interface has been started but has + not been initialized + * An unexpected error was returned by the + underlying network driver or device + @retval EFI_UNSUPPORTED This function is not supported by the network + interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32ReceiveFilters ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN UINT32 Enable, + IN UINT32 Disable, + IN BOOLEAN ResetMCastFilter, + IN UINTN MCastFilterCnt, OPTIONAL + IN EFI_MAC_ADDRESS *MCastFilter OPTIONAL + ) +{ + SNP_DRIVER *Snp; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + switch (Snp->Mode.State) { + case EfiSimpleNetworkInitialized: + break; + + case EfiSimpleNetworkStopped: + Status = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + // + // check if we are asked to enable or disable something that the UNDI + // does not even support! + // + if (((Enable &~Snp->Mode.ReceiveFilterMask) != 0) || + ((Disable &~Snp->Mode.ReceiveFilterMask) != 0)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + if (ResetMCastFilter) { + + Disable |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST & Snp->Mode.ReceiveFilterMask; + MCastFilterCnt = 0; + MCastFilter = NULL; + } else { + if (MCastFilterCnt != 0) { + if ((MCastFilterCnt > Snp->Mode.MaxMCastFilterCount) || + (MCastFilter == NULL)) { + + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + } + } + + if (Enable == 0 && Disable == 0 && !ResetMCastFilter && MCastFilterCnt == 0) { + Status = EFI_SUCCESS; + goto ON_EXIT; + } + + if ((Enable & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) != 0 && MCastFilterCnt == 0) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + if ((Enable != 0) || (MCastFilterCnt != 0)) { + Status = PxeRecvFilterEnable ( + Snp, + Enable, + MCastFilterCnt, + MCastFilter + ); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + if ((Disable != 0) || ResetMCastFilter) { + Status = PxeRecvFilterDisable (Snp, Disable, ResetMCastFilter); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + Status = PxeRecvFilterRead (Snp); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/Reset.c b/NetworkPkg/SnpDxe/Reset.c new file mode 100644 index 000000000..3069bfde6 --- /dev/null +++ b/NetworkPkg/SnpDxe/Reset.c @@ -0,0 +1,130 @@ +/** @file + Implementation of resetting a network adapter. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + + +/** + Call UNDI to reset the NIC. + + @param Snp Pointer to the snp driver structure. + + @return EFI_SUCCESSFUL The NIC was reset. + @retval EFI_DEVICE_ERROR The NIC cannot be reset. + +**/ +EFI_STATUS +PxeReset ( + SNP_DRIVER *Snp + ) +{ + Snp->Cdb.OpCode = PXE_OPCODE_RESET; + Snp->Cdb.OpFlags = PXE_OPFLAGS_NOT_USED; + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED; + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED; + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nsnp->undi.reset() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) { + DEBUG ( + (EFI_D_WARN, + "\nsnp->undi32.reset() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + // + // UNDI could not be reset. Return UNDI error. + // + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + + +/** + Resets a network adapter and reinitializes it with the parameters that were + provided in the previous call to Initialize(). + + This function resets a network adapter and reinitializes it with the parameters + that were provided in the previous call to Initialize(). The transmit and + receive queues are emptied and all pending interrupts are cleared. + Receive filters, the station address, the statistics, and the multicast-IP-to-HW + MAC addresses are not reset by this call. If the network interface was + successfully reset, then EFI_SUCCESS will be returned. If the driver has not + been initialized, EFI_DEVICE_ERROR will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param ExtendedVerification Indicates that the driver may perform a more + exhaustive verification operation of the device + during reset. + + @retval EFI_SUCCESS The network interface was reset. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value. + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + @retval EFI_UNSUPPORTED This function is not supported by the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Reset ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN BOOLEAN ExtendedVerification + ) +{ + SNP_DRIVER *Snp; + EFI_TPL OldTpl; + EFI_STATUS Status; + + // + // Resolve Warning 4 unreferenced parameter problem + // + ExtendedVerification = 0; + DEBUG ((EFI_D_WARN, "ExtendedVerification = %d is not implemented!\n", ExtendedVerification)); + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + switch (Snp->Mode.State) { + case EfiSimpleNetworkInitialized: + break; + + case EfiSimpleNetworkStopped: + Status = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + Status = PxeReset (Snp); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/Shutdown.c b/NetworkPkg/SnpDxe/Shutdown.c new file mode 100644 index 000000000..aad0fe849 --- /dev/null +++ b/NetworkPkg/SnpDxe/Shutdown.c @@ -0,0 +1,146 @@ +/** @file + Implementation of shuting down a network adapter. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + + +/** + Call UNDI to shut down the interface. + + @param Snp Pointer to snp driver structure. + + @retval EFI_SUCCESS UNDI is shut down successfully. + @retval EFI_DEVICE_ERROR UNDI could not be shut down. + +**/ +EFI_STATUS +PxeShutdown ( + IN SNP_DRIVER *Snp + ) +{ + Snp->Cdb.OpCode = PXE_OPCODE_SHUTDOWN; + Snp->Cdb.OpFlags = PXE_OPFLAGS_NOT_USED; + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED; + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED; + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nsnp->undi.shutdown() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) { + // + // UNDI could not be shutdown. Return UNDI error. + // + DEBUG ((EFI_D_WARN, "\nsnp->undi.shutdown() %xh:%xh\n", Snp->Cdb.StatFlags, Snp->Cdb.StatCode)); + + return EFI_DEVICE_ERROR; + } + // + // Free allocated memory. + // + if (Snp->TxRxBuffer != NULL) { + Snp->PciIo->FreeBuffer ( + Snp->PciIo, + SNP_MEM_PAGES (Snp->TxRxBufferSize), + (VOID *) Snp->TxRxBuffer + ); + } + + Snp->TxRxBuffer = NULL; + Snp->TxRxBufferSize = 0; + + return EFI_SUCCESS; +} + + +/** + Resets a network adapter and leaves it in a state that is safe for another + driver to initialize. + + This function releases the memory buffers assigned in the Initialize() call. + Pending transmits and receives are lost, and interrupts are cleared and disabled. + After this call, only the Initialize() and Stop() calls may be used. If the + network interface was successfully shutdown, then EFI_SUCCESS will be returned. + If the driver has not been initialized, EFI_DEVICE_ERROR will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + + @retval EFI_SUCCESS The network interface was shutdown. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure. + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Shutdown ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This + ) +{ + SNP_DRIVER *Snp; + EFI_STATUS Status; + EFI_TPL OldTpl; + + // + // Get pointer to SNP driver instance for *This. + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Return error if the SNP is not initialized. + // + switch (Snp->Mode.State) { + case EfiSimpleNetworkInitialized: + break; + + case EfiSimpleNetworkStopped: + Status = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + Status = PxeShutdown (Snp); + + Snp->Mode.State = EfiSimpleNetworkStarted; + Snp->Mode.ReceiveFilterSetting = 0; + + Snp->Mode.MCastFilterCount = 0; + Snp->Mode.ReceiveFilterSetting = 0; + ZeroMem (Snp->Mode.MCastFilter, sizeof Snp->Mode.MCastFilter); + CopyMem ( + &Snp->Mode.CurrentAddress, + &Snp->Mode.PermanentAddress, + sizeof (EFI_MAC_ADDRESS) + ); + + gBS->CloseEvent (Snp->Snp.WaitForPacket); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/Snp.c b/NetworkPkg/SnpDxe/Snp.c new file mode 100644 index 000000000..a23af0507 --- /dev/null +++ b/NetworkPkg/SnpDxe/Snp.c @@ -0,0 +1,862 @@ +/** @file + Implementation of driver entry point and driver binding protocol. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + +/** + One notified function to stop UNDI device when gBS->ExitBootServices() called. + + @param Event Pointer to this event + @param Context Event handler private data + +**/ +VOID +EFIAPI +SnpNotifyExitBootServices ( + EFI_EVENT Event, + VOID *Context + ) +{ + SNP_DRIVER *Snp; + + Snp = (SNP_DRIVER *)Context; + + // + // Shutdown and stop UNDI driver + // + PxeShutdown (Snp); + PxeStop (Snp); +} + +/** + Send command to UNDI. It does nothing currently. + + @param Cdb command to be sent to UNDI. + + @retval EFI_INVALID_PARAMETER The command is 0. + @retval EFI_UNSUPPORTED Default return status because it's not + supported currently. + +**/ +EFI_STATUS +EFIAPI +IssueHwUndiCommand ( + UINT64 Cdb + ) +{ + DEBUG ((EFI_D_ERROR, "\nIssueHwUndiCommand() - This should not be called!")); + + if (Cdb == 0) { + return EFI_INVALID_PARAMETER; + + } + // + // %%TBD - For now, nothing is done. + // + return EFI_UNSUPPORTED; +} + + +/** + Compute 8-bit checksum of a buffer. + + @param Buffer Pointer to buffer. + @param Length Length of buffer in bytes. + + @return 8-bit checksum of all bytes in buffer, or zero if ptr is NULL or len + is zero. + +**/ +UINT8 +Calc8BitCksum ( + VOID *Buffer, + UINTN Length + ) +{ + UINT8 *Ptr; + UINT8 Cksum; + + Ptr = Buffer; + Cksum = 0; + + if (Ptr == NULL || Length == 0) { + return 0; + } + + while (Length-- != 0) { + Cksum = (UINT8) (Cksum + *Ptr++); + } + + return Cksum; +} + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param This Protocol instance pointer. + @param ControllerHandle Handle of device to test. + @param RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +SimpleNetworkDriverSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS Status; + EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *NiiProtocol; + PXE_UNDI *Pxe; + + Status = gBS->OpenProtocol ( + Controller, + &gEfiDevicePathProtocolGuid, + NULL, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + Controller, + &gEfiNetworkInterfaceIdentifierProtocolGuid_31, + (VOID **) &NiiProtocol, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + if (Status == EFI_ALREADY_STARTED) { + DEBUG ((EFI_D_INFO, "Support(): Already Started. on handle %p\n", Controller)); + } + return Status; + } + + DEBUG ((EFI_D_INFO, "Support(): UNDI3.1 found on handle %p\n", Controller)); + + // + // check the version, we don't want to connect to the undi16 + // + if (NiiProtocol->Type != EfiNetworkInterfaceUndi) { + Status = EFI_UNSUPPORTED; + goto Done; + } + // + // Check to see if !PXE structure is valid. Paragraph alignment of !PXE structure is required. + // + if ((NiiProtocol->Id & 0x0F) != 0) { + DEBUG ((EFI_D_NET, "\n!PXE structure is not paragraph aligned.\n")); + Status = EFI_UNSUPPORTED; + goto Done; + } + + Pxe = (PXE_UNDI *) (UINTN) (NiiProtocol->Id); + + // + // Verify !PXE revisions. + // + if (Pxe->hw.Signature != PXE_ROMID_SIGNATURE) { + DEBUG ((EFI_D_NET, "\n!PXE signature is not valid.\n")); + Status = EFI_UNSUPPORTED; + goto Done; + } + + if (Pxe->hw.Rev < PXE_ROMID_REV) { + DEBUG ((EFI_D_NET, "\n!PXE.Rev is not supported.\n")); + Status = EFI_UNSUPPORTED; + goto Done; + } + + if (Pxe->hw.MajorVer < PXE_ROMID_MAJORVER) { + + DEBUG ((EFI_D_NET, "\n!PXE.MajorVer is not supported.\n")); + Status = EFI_UNSUPPORTED; + goto Done; + + } else if (Pxe->hw.MajorVer == PXE_ROMID_MAJORVER && Pxe->hw.MinorVer < PXE_ROMID_MINORVER) { + DEBUG ((EFI_D_NET, "\n!PXE.MinorVer is not supported.")); + Status = EFI_UNSUPPORTED; + goto Done; + } + // + // Do S/W UNDI specific checks. + // + if ((Pxe->hw.Implementation & PXE_ROMID_IMP_HW_UNDI) == 0) { + if (Pxe->sw.EntryPoint < Pxe->sw.Len) { + DEBUG ((EFI_D_NET, "\n!PXE S/W entry point is not valid.")); + Status = EFI_UNSUPPORTED; + goto Done; + } + + if (Pxe->sw.BusCnt == 0) { + DEBUG ((EFI_D_NET, "\n!PXE.BusCnt is zero.")); + Status = EFI_UNSUPPORTED; + goto Done; + } + } + + Status = EFI_SUCCESS; + DEBUG ((EFI_D_INFO, "Support(): supported on %p\n", Controller)); + +Done: + gBS->CloseProtocol ( + Controller, + &gEfiNetworkInterfaceIdentifierProtocolGuid_31, + This->DriverBindingHandle, + Controller + ); + + return Status; +} + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param This Protocol instance pointer. + @param ControllerHandle Handle of device to bind driver to. + @param RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle + @retval EFI_DEVICE_ERROR This driver could not be started due to a device error + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +SimpleNetworkDriverStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *Nii; + EFI_DEVICE_PATH_PROTOCOL *NiiDevicePath; + EFI_STATUS Status; + PXE_UNDI *Pxe; + SNP_DRIVER *Snp; + VOID *Address; + EFI_HANDLE Handle; + UINT8 BarIndex; + PXE_STATFLAGS InitStatFlags; + EFI_PCI_IO_PROTOCOL *PciIo; + EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *BarDesc; + BOOLEAN FoundIoBar; + BOOLEAN FoundMemoryBar; + + DEBUG ((EFI_D_NET, "\nSnpNotifyNetworkInterfaceIdentifier() ")); + + Status = gBS->OpenProtocol ( + Controller, + &gEfiDevicePathProtocolGuid, + (VOID **) &NiiDevicePath, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->LocateDevicePath ( + &gEfiPciIoProtocolGuid, + &NiiDevicePath, + &Handle + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + Handle, + &gEfiPciIoProtocolGuid, + (VOID **) &PciIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + // + // Get the NII interface. + // + Status = gBS->OpenProtocol ( + Controller, + &gEfiNetworkInterfaceIdentifierProtocolGuid_31, + (VOID **) &Nii, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + gBS->CloseProtocol ( + Controller, + &gEfiDevicePathProtocolGuid, + This->DriverBindingHandle, + Controller + ); + return Status; + } + + DEBUG ((EFI_D_INFO, "Start(): UNDI3.1 found\n")); + + Pxe = (PXE_UNDI *) (UINTN) (Nii->Id); + + if (Calc8BitCksum (Pxe, Pxe->hw.Len) != 0) { + DEBUG ((EFI_D_NET, "\n!PXE checksum is not correct.\n")); + goto NiiError; + } + + if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED) != 0) { + // + // We can get any packets. + // + } else if ((Pxe->hw.Implementation & PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED) != 0) { + // + // We need to be able to get broadcast packets for DHCP. + // If we do not have promiscuous support, we must at least have + // broadcast support or we cannot do DHCP! + // + } else { + DEBUG ((EFI_D_NET, "\nUNDI does not have promiscuous or broadcast support.")); + goto NiiError; + } + // + // OK, we like this UNDI, and we know snp is not already there on this handle + // Allocate and initialize a new simple network protocol structure. + // + Status = PciIo->AllocateBuffer ( + PciIo, + AllocateAnyPages, + EfiBootServicesData, + SNP_MEM_PAGES (sizeof (SNP_DRIVER)), + &Address, + 0 + ); + + if (Status != EFI_SUCCESS) { + DEBUG ((EFI_D_NET, "\nCould not allocate SNP_DRIVER structure.\n")); + goto NiiError; + } + + Snp = (SNP_DRIVER *) (UINTN) Address; + + ZeroMem (Snp, sizeof (SNP_DRIVER)); + + Snp->PciIo = PciIo; + Snp->Signature = SNP_DRIVER_SIGNATURE; + + EfiInitializeLock (&Snp->Lock, TPL_NOTIFY); + + Snp->Snp.Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION; + Snp->Snp.Start = SnpUndi32Start; + Snp->Snp.Stop = SnpUndi32Stop; + Snp->Snp.Initialize = SnpUndi32Initialize; + Snp->Snp.Reset = SnpUndi32Reset; + Snp->Snp.Shutdown = SnpUndi32Shutdown; + Snp->Snp.ReceiveFilters = SnpUndi32ReceiveFilters; + Snp->Snp.StationAddress = SnpUndi32StationAddress; + Snp->Snp.Statistics = SnpUndi32Statistics; + Snp->Snp.MCastIpToMac = SnpUndi32McastIpToMac; + Snp->Snp.NvData = SnpUndi32NvData; + Snp->Snp.GetStatus = SnpUndi32GetStatus; + Snp->Snp.Transmit = SnpUndi32Transmit; + Snp->Snp.Receive = SnpUndi32Receive; + Snp->Snp.WaitForPacket = NULL; + + Snp->Snp.Mode = &Snp->Mode; + + Snp->TxRxBufferSize = 0; + Snp->TxRxBuffer = NULL; + + Snp->RecycledTxBuf = AllocatePool (sizeof (UINT64) * SNP_TX_BUFFER_INCREASEMENT); + if (Snp->RecycledTxBuf == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error_DeleteSNP; + } + Snp->MaxRecycledTxBuf = SNP_TX_BUFFER_INCREASEMENT; + Snp->RecycledTxBufCount = 0; + + if (Nii->Revision >= EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_REVISION) { + Snp->IfNum = Nii->IfNum; + + } else { + Snp->IfNum = (UINT8) (Nii->IfNum & 0xFF); + } + + if ((Pxe->hw.Implementation & PXE_ROMID_IMP_HW_UNDI) != 0) { + Snp->IsSwUndi = FALSE; + Snp->IssueUndi32Command = &IssueHwUndiCommand; + } else { + Snp->IsSwUndi = TRUE; + + if ((Pxe->sw.Implementation & PXE_ROMID_IMP_SW_VIRT_ADDR) != 0) { + Snp->IssueUndi32Command = (ISSUE_UNDI32_COMMAND) (UINTN) Pxe->sw.EntryPoint; + } else { + Snp->IssueUndi32Command = (ISSUE_UNDI32_COMMAND) (UINTN) ((UINT8) (UINTN) Pxe + Pxe->sw.EntryPoint); + } + } + // + // Allocate a global CPB and DB buffer for this UNDI interface. + // we do this because: + // + // -UNDI 3.0 wants all the addresses passed to it (even the cpb and db) to be + // within 2GB limit, create them here and map them so that when undi calls + // v2p callback to check if the physical address is < 2gb, we will pass. + // + // -This is not a requirement for 3.1 or later UNDIs but the code looks + // simpler if we use the same cpb, db variables for both old and new undi + // interfaces from all the SNP interface calls (we don't map the buffers + // for the newer undi interfaces though) + // . + // -it is OK to allocate one global set of CPB, DB pair for each UNDI + // interface as EFI does not multi-task and so SNP will not be re-entered! + // + Status = PciIo->AllocateBuffer ( + PciIo, + AllocateAnyPages, + EfiBootServicesData, + SNP_MEM_PAGES (4096), + &Address, + 0 + ); + + if (Status != EFI_SUCCESS) { + DEBUG ((EFI_D_NET, "\nCould not allocate CPB and DB structures.\n")); + goto Error_DeleteSNP; + } + + Snp->Cpb = (VOID *) (UINTN) Address; + Snp->Db = (VOID *) ((UINTN) Address + 2048); + + // + // Find the correct BAR to do IO. + // + // Enumerate through the PCI BARs for the device to determine which one is + // the IO BAR. Save the index of the BAR into the adapter info structure. + // for regular 32bit BARs, 0 is memory mapped, 1 is io mapped + // + Snp->MemoryBarIndex = 0; + Snp->IoBarIndex = 1; + FoundMemoryBar = FALSE; + FoundIoBar = FALSE; + for (BarIndex = 0; BarIndex < PCI_MAX_BAR; BarIndex++) { + Status = PciIo->GetBarAttributes ( + PciIo, + BarIndex, + NULL, + (VOID**) &BarDesc + ); + if (Status == EFI_UNSUPPORTED) { + continue; + } else if (EFI_ERROR (Status)) { + goto Error_DeleteSNP; + } + + if ((!FoundMemoryBar) && (BarDesc->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM)) { + Snp->MemoryBarIndex = BarIndex; + FoundMemoryBar = TRUE; + } else if ((!FoundIoBar) && (BarDesc->ResType == ACPI_ADDRESS_SPACE_TYPE_IO)) { + Snp->IoBarIndex = BarIndex; + FoundIoBar = TRUE; + } + + FreePool (BarDesc); + + if (FoundMemoryBar && FoundIoBar) { + break; + } + } + + Status = PxeStart (Snp); + + if (Status != EFI_SUCCESS) { + goto Error_DeleteSNP; + } + + Snp->Cdb.OpCode = PXE_OPCODE_GET_INIT_INFO; + Snp->Cdb.OpFlags = PXE_OPFLAGS_NOT_USED; + + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.CPBaddr = PXE_DBADDR_NOT_USED; + + Snp->Cdb.DBsize = (UINT16) sizeof (Snp->InitInfo); + Snp->Cdb.DBaddr = (UINT64)(UINTN) (&Snp->InitInfo); + + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + DEBUG ((EFI_D_NET, "\nSnp->undi.get_init_info() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + // + // Save the INIT Stat Code... + // + InitStatFlags = Snp->Cdb.StatFlags; + + if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) { + DEBUG ((EFI_D_NET, "\nSnp->undi.init_info() %xh:%xh\n", Snp->Cdb.StatFlags, Snp->Cdb.StatCode)); + PxeStop (Snp); + goto Error_DeleteSNP; + } + + // + // Initialize simple network protocol mode structure + // + Snp->Mode.State = EfiSimpleNetworkStopped; + Snp->Mode.HwAddressSize = Snp->InitInfo.HWaddrLen; + Snp->Mode.MediaHeaderSize = Snp->InitInfo.MediaHeaderLen; + Snp->Mode.MaxPacketSize = Snp->InitInfo.FrameDataLen; + Snp->Mode.NvRamAccessSize = Snp->InitInfo.NvWidth; + Snp->Mode.NvRamSize = Snp->InitInfo.NvCount * Snp->Mode.NvRamAccessSize; + Snp->Mode.IfType = Snp->InitInfo.IFtype; + Snp->Mode.MaxMCastFilterCount = Snp->InitInfo.MCastFilterCnt; + Snp->Mode.MCastFilterCount = 0; + + switch (InitStatFlags & PXE_STATFLAGS_CABLE_DETECT_MASK) { + case PXE_STATFLAGS_CABLE_DETECT_SUPPORTED: + Snp->CableDetectSupported = TRUE; + break; + + case PXE_STATFLAGS_CABLE_DETECT_NOT_SUPPORTED: + default: + Snp->CableDetectSupported = FALSE; + } + + switch (InitStatFlags & PXE_STATFLAGS_GET_STATUS_NO_MEDIA_MASK) { + case PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED: + Snp->MediaStatusSupported = TRUE; + break; + + case PXE_STATFLAGS_GET_STATUS_NO_MEDIA_NOT_SUPPORTED: + default: + Snp->MediaStatusSupported = FALSE; + } + + if (Snp->CableDetectSupported || Snp->MediaStatusSupported) { + Snp->Mode.MediaPresentSupported = TRUE; + } + + if ((Pxe->hw.Implementation & PXE_ROMID_IMP_STATION_ADDR_SETTABLE) != 0) { + Snp->Mode.MacAddressChangeable = TRUE; + } else { + Snp->Mode.MacAddressChangeable = FALSE; + } + + if ((Pxe->hw.Implementation & PXE_ROMID_IMP_MULTI_FRAME_SUPPORTED) != 0) { + Snp->Mode.MultipleTxSupported = TRUE; + } else { + Snp->Mode.MultipleTxSupported = FALSE; + } + + Snp->Mode.ReceiveFilterMask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST; + + if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED) != 0) { + Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST; + + } + + if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED) != 0) { + Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS; + + } + + if ((Pxe->hw.Implementation & PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED) != 0) { + Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST; + + } + + if ((Pxe->hw.Implementation & PXE_ROMID_IMP_FILTERED_MULTICAST_RX_SUPPORTED) != 0) { + Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST; + + } + + if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED) != 0) { + Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST; + + } + + Snp->Mode.ReceiveFilterSetting = 0; + + // + // need to get the station address to save in the mode structure. we need to + // initialize the UNDI first for this. + // + Snp->TxRxBufferSize = Snp->InitInfo.MemoryRequired; + Status = PxeInit (Snp, PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE); + + if (EFI_ERROR (Status)) { + PxeStop (Snp); + goto Error_DeleteSNP; + } + + Status = PxeGetStnAddr (Snp); + + if (Status != EFI_SUCCESS) { + DEBUG ((EFI_D_ERROR, "\nSnp->undi.get_station_addr() failed.\n")); + PxeShutdown (Snp); + PxeStop (Snp); + goto Error_DeleteSNP; + } + + Snp->Mode.MediaPresent = FALSE; + + // + // We should not leave UNDI started and initialized here. this DriverStart() + // routine must only find and attach the SNP interface to UNDI layer that it + // finds on the given handle! + // The UNDI layer will be started when upper layers call Snp->start. + // How ever, this DriverStart() must fill up the snp mode structure which + // contains the MAC address of the NIC. For this reason we started and + // initialized UNDI here, now we are done, do a shutdown and stop of the + // UNDI interface! + // + PxeShutdown (Snp); + PxeStop (Snp); + + // + // Create EXIT_BOOT_SERIVES Event + // + Status = gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + SnpNotifyExitBootServices, + Snp, + &gEfiEventExitBootServicesGuid, + &Snp->ExitBootServicesEvent + ); + if (EFI_ERROR (Status)) { + goto Error_DeleteSNP; + } + + // + // add SNP to the undi handle + // + Status = gBS->InstallProtocolInterface ( + &Controller, + &gEfiSimpleNetworkProtocolGuid, + EFI_NATIVE_INTERFACE, + &(Snp->Snp) + ); + + if (!EFI_ERROR (Status)) { + return Status; + } + + PciIo->FreeBuffer ( + PciIo, + SNP_MEM_PAGES (4096), + Snp->Cpb + ); + +Error_DeleteSNP: + + if (Snp->RecycledTxBuf != NULL) { + FreePool (Snp->RecycledTxBuf); + } + + PciIo->FreeBuffer ( + PciIo, + SNP_MEM_PAGES (sizeof (SNP_DRIVER)), + Snp + ); +NiiError: + gBS->CloseProtocol ( + Controller, + &gEfiNetworkInterfaceIdentifierProtocolGuid_31, + This->DriverBindingHandle, + Controller + ); + + gBS->CloseProtocol ( + Controller, + &gEfiDevicePathProtocolGuid, + This->DriverBindingHandle, + Controller + ); + + // + // If we got here that means we are in error state. + // + if (!EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + } + + return Status; +} + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param This Protocol instance pointer. + @param ControllerHandle Handle of device to stop driver on + @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +SimpleNetworkDriverStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *SnpProtocol; + SNP_DRIVER *Snp; + EFI_PCI_IO_PROTOCOL *PciIo; + + // + // Get our context back. + // + Status = gBS->OpenProtocol ( + Controller, + &gEfiSimpleNetworkProtocolGuid, + (VOID **) &SnpProtocol, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (SnpProtocol); + + Status = gBS->UninstallProtocolInterface ( + Controller, + &gEfiSimpleNetworkProtocolGuid, + &Snp->Snp + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Close EXIT_BOOT_SERIVES Event + // + gBS->CloseEvent (Snp->ExitBootServicesEvent); + + Status = gBS->CloseProtocol ( + Controller, + &gEfiNetworkInterfaceIdentifierProtocolGuid_31, + This->DriverBindingHandle, + Controller + ); + + Status = gBS->CloseProtocol ( + Controller, + &gEfiDevicePathProtocolGuid, + This->DriverBindingHandle, + Controller + ); + + PxeShutdown (Snp); + PxeStop (Snp); + + FreePool (Snp->RecycledTxBuf); + + PciIo = Snp->PciIo; + PciIo->FreeBuffer ( + PciIo, + SNP_MEM_PAGES (4096), + Snp->Cpb + ); + + PciIo->FreeBuffer ( + PciIo, + SNP_MEM_PAGES (sizeof (SNP_DRIVER)), + Snp + ); + + return Status; +} + +// +// Simple Network Protocol Driver Global Variables +// +EFI_DRIVER_BINDING_PROTOCOL gSimpleNetworkDriverBinding = { + SimpleNetworkDriverSupported, + SimpleNetworkDriverStart, + SimpleNetworkDriverStop, + 0xa, + NULL, + NULL +}; + +/** + The SNP driver entry point. + + @param ImageHandle The driver image handle. + @param SystemTable The system table. + + @retval EFI_SUCEESS Initialization routine has found UNDI hardware, + loaded it's ROM, and installed a notify event for + the Network Indentifier Interface Protocol + successfully. + @retval Other Return value from HandleProtocol for + DeviceIoProtocol or LoadedImageProtocol + +**/ +EFI_STATUS +EFIAPI +InitializeSnpNiiDriver ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gSimpleNetworkDriverBinding, + ImageHandle, + &gSimpleNetworkComponentName, + &gSimpleNetworkComponentName2 + ); +} diff --git a/NetworkPkg/SnpDxe/Snp.h b/NetworkPkg/SnpDxe/Snp.h new file mode 100644 index 000000000..e6b629303 --- /dev/null +++ b/NetworkPkg/SnpDxe/Snp.h @@ -0,0 +1,1033 @@ +/** @file + Declaration of strctures and functions for SnpDxe driver. + +Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#ifndef _SNP_H_ +#define _SNP_H_ + + +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define FOUR_GIGABYTES (UINT64) 0x100000000ULL + + +#define SNP_DRIVER_SIGNATURE SIGNATURE_32 ('s', 'n', 'd', 's') +#define MAX_MAP_LENGTH 100 + +#define PCI_BAR_IO_MASK 0x00000003 +#define PCI_BAR_IO_MODE 0x00000001 + +#define PCI_BAR_MEM_MASK 0x0000000F +#define PCI_BAR_MEM_MODE 0x00000000 +#define PCI_BAR_MEM_64BIT 0x00000004 + +#define SNP_TX_BUFFER_INCREASEMENT MAX_XMIT_BUFFERS +#define SNP_MAX_TX_BUFFER_NUM 65536 + +typedef +EFI_STATUS +(EFIAPI *ISSUE_UNDI32_COMMAND) ( + UINT64 Cdb + ); + +typedef struct { + UINT32 Signature; + EFI_LOCK Lock; + + EFI_SIMPLE_NETWORK_PROTOCOL Snp; + EFI_SIMPLE_NETWORK_MODE Mode; + + EFI_HANDLE DeviceHandle; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + + // + // Local instance data needed by SNP driver + // + // Pointer to S/W UNDI API entry point + // This will be NULL for H/W UNDI + // + ISSUE_UNDI32_COMMAND IssueUndi32Command; + + BOOLEAN IsSwUndi; + + // + // undi interface number, if one undi manages more nics + // + PXE_IFNUM IfNum; + + // + // Allocated tx/rx buffer that was passed to UNDI Initialize. + // + UINT32 TxRxBufferSize; + VOID *TxRxBuffer; + // + // mappable buffers for receive and fill header for undi3.0 + // these will be used if the user buffers are above 4GB limit (instead of + // mapping the user buffers) + // + UINT8 *ReceiveBufffer; + VOID *ReceiveBufferUnmap; + UINT8 *FillHeaderBuffer; + VOID *FillHeaderBufferUnmap; + + EFI_PCI_IO_PROTOCOL *PciIo; + UINT8 IoBarIndex; + UINT8 MemoryBarIndex; + + // + // Buffers for command descriptor block, command parameter block + // and data block. + // + PXE_CDB Cdb; + VOID *Cpb; + VOID *CpbUnmap; + VOID *Db; + + // + // UNDI structure, we need to remember the init info for a long time! + // + PXE_DB_GET_INIT_INFO InitInfo; + + VOID *SnpDriverUnmap; + // + // when ever we map an address, we must remember it's address and the un-map + // cookie so that we can unmap later + // + struct MAP_LIST { + EFI_PHYSICAL_ADDRESS VirtualAddress; + VOID *MapCookie; + } MapList[MAX_MAP_LENGTH]; + + EFI_EVENT ExitBootServicesEvent; + + // + // Whether UNDI support reporting media status from GET_STATUS command, + // i.e. PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED or + // PXE_STATFLAGS_GET_STATUS_NO_MEDIA_NOT_SUPPORTED + // + BOOLEAN MediaStatusSupported; + + // + // Whether UNDI support cable detect for INITIALIZE command, + // i.e. PXE_STATFLAGS_CABLE_DETECT_SUPPORTED or + // PXE_STATFLAGS_CABLE_DETECT_NOT_SUPPORTED + // + BOOLEAN CableDetectSupported; + + // + // Array of the recycled transmit buffer address from UNDI. + // + UINT64 *RecycledTxBuf; + // + // The maximum number of recycled buffer pointers in RecycledTxBuf. + // + UINT32 MaxRecycledTxBuf; + // + // Current number of recycled buffer pointers in RecycledTxBuf. + // + UINT32 RecycledTxBufCount; +} SNP_DRIVER; + +#define EFI_SIMPLE_NETWORK_DEV_FROM_THIS(a) CR (a, SNP_DRIVER, Snp, SNP_DRIVER_SIGNATURE) + +// +// Global Variables +// +extern EFI_DRIVER_BINDING_PROTOCOL gSimpleNetworkDriverBinding; +extern EFI_COMPONENT_NAME_PROTOCOL gSimpleNetworkComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gSimpleNetworkComponentName2; + +/** + this routine calls undi to start the interface and changes the snp state. + + @param Snp pointer to snp driver structure + + @retval EFI_DEVICE_ERROR UNDI could not be started + @retval EFI_SUCCESS UNDI is started successfully + +**/ +EFI_STATUS +PxeStart ( + IN SNP_DRIVER *Snp + ); + +/** + this routine calls undi to stop the interface and changes the snp state. + + @param Snp pointer to snp driver structure + + @retval EFI_INVALID_PARAMETER invalid parameter + @retval EFI_NOT_STARTED SNP is not started + @retval EFI_DEVICE_ERROR SNP is not initialized + @retval EFI_UNSUPPORTED operation unsupported + +**/ +EFI_STATUS +PxeStop ( + SNP_DRIVER *Snp + ); + +/** + this routine calls undi to initialize the interface. + + @param Snp pointer to snp driver structure + @param CableDetectFlag Do/don't detect the cable (depending on what undi supports) + + @retval EFI_SUCCESS UNDI is initialized successfully + @retval EFI_DEVICE_ERROR UNDI could not be initialized + @retval Other other errors + +**/ +EFI_STATUS +PxeInit ( + SNP_DRIVER *Snp, + UINT16 CableDetectFlag + ); + +/** + this routine calls undi to shut down the interface. + + @param Snp pointer to snp driver structure + + @retval EFI_SUCCESS UNDI is shut down successfully + @retval EFI_DEVICE_ERROR UNDI could not be shut down + +**/ +EFI_STATUS +PxeShutdown ( + IN SNP_DRIVER *Snp + ); + +/** + this routine calls undi to read the MAC address of the NIC and updates the + mode structure with the address. + + @param Snp pointer to snp driver structure. + + @retval EFI_SUCCESS the MAC address of the NIC is read successfully. + @retval EFI_DEVICE_ERROR failed to read the MAC address of the NIC. + +**/ +EFI_STATUS +PxeGetStnAddr ( + SNP_DRIVER *Snp + ); + +/** + Call undi to get the status of the interrupts, get the list of recycled transmit + buffers that completed transmitting. The recycled transmit buffer address will + be saved into Snp->RecycledTxBuf. This function will also update the MediaPresent + field of EFI_SIMPLE_NETWORK_MODE if UNDI support it. + + @param[in] Snp Pointer to snp driver structure. + @param[out] InterruptStatusPtr A non null pointer to contain the interrupt + status. + @param[in] GetTransmittedBuf Set to TRUE to retrieve the recycled transmit + buffer address. + + @retval EFI_SUCCESS The status of the network interface was retrieved. + @retval EFI_DEVICE_ERROR The command could not be sent to the network + interface. + +**/ +EFI_STATUS +PxeGetStatus ( + IN SNP_DRIVER *Snp, + OUT UINT32 *InterruptStatusPtr, + IN BOOLEAN GetTransmittedBuf + ); + +/** + This is a callback routine supplied to UNDI3.1 at undi_start time. + UNDI call this routine when it wants to have exclusive access to a critical + section of the code/data. + New callbacks for 3.1: + there won't be a virtual2physical callback for UNDI 3.1 because undi3.1 uses + the MemMap call to map the required address by itself! + + @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to + store Undi interface context (Undi does not read or write + this variable) + @param Enable non-zero indicates acquire + zero indicates release +**/ +VOID +EFIAPI +SnpUndi32CallbackBlock ( + IN UINT64 UniqueId, + IN UINT32 Enable + ); + +/** + This is a callback routine supplied to UNDI at undi_start time. + UNDI call this routine with the number of micro seconds when it wants to + pause. + + @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to + store Undi interface context (Undi does not read or write + this variable) + @param MicroSeconds number of micro seconds to pause, ususlly multiple of 10. +**/ +VOID +EFIAPI +SnpUndi32CallbackDelay ( + IN UINT64 UniqueId, + IN UINT64 MicroSeconds + ); + +/** + This is a callback routine supplied to UNDI at undi_start time. + This is the IO routine for UNDI3.1 to start CPB. + + @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this + to store Undi interface context (Undi does not read or + write this variable) + @param ReadOrWrite indicates read or write, IO or Memory. + @param NumBytes number of bytes to read or write. + @param MemOrPortAddr IO or memory address to read from or write to. + @param BufferPtr memory location to read into or that contains the bytes + to write. +**/ +VOID +EFIAPI +SnpUndi32CallbackMemio ( + IN UINT64 UniqueId, + IN UINT8 ReadOrWrite, + IN UINT8 NumBytes, + IN UINT64 MemOrPortAddr, + IN OUT UINT64 BufferPtr + ); + +/** + This is a callback routine supplied to UNDI at undi_start time. + UNDI call this routine when it has to map a CPU address to a device + address. + + @param UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store + Undi interface context (Undi does not read or write this variable) + @param CpuAddr - Virtual address to be mapped! + @param NumBytes - size of memory to be mapped + @param Direction - direction of data flow for this memory's usage: + cpu->device, device->cpu or both ways + @param DeviceAddrPtr - pointer to return the mapped device address + +**/ +VOID +EFIAPI +SnpUndi32CallbackMap ( + IN UINT64 UniqueId, + IN UINT64 CpuAddr, + IN UINT32 NumBytes, + IN UINT32 Direction, + IN OUT UINT64 DeviceAddrPtr + ); + +/** + This is a callback routine supplied to UNDI at undi_start time. + UNDI call this routine when it wants to unmap an address that was previously + mapped using map callback. + + @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to store. + Undi interface context (Undi does not read or write this variable) + @param CpuAddr Virtual address that was mapped! + @param NumBytes size of memory mapped + @param Direction direction of data flow for this memory's usage: + cpu->device, device->cpu or both ways + @param DeviceAddr the mapped device address + +**/ +VOID +EFIAPI +SnpUndi32CallbackUnmap ( + IN UINT64 UniqueId, + IN UINT64 CpuAddr, + IN UINT32 NumBytes, + IN UINT32 Direction, + IN UINT64 DeviceAddr + ); + +/** + This is a callback routine supplied to UNDI at undi_start time. + UNDI call this routine when it wants synchronize the virtual buffer contents + with the mapped buffer contents. The virtual and mapped buffers need not + correspond to the same physical memory (especially if the virtual address is + > 4GB). Depending on the direction for which the buffer is mapped, undi will + need to synchronize their contents whenever it writes to/reads from the buffer + using either the cpu address or the device address. + + EFI does not provide a sync call, since virt=physical, we sould just do + the synchronization ourself here! + + @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to store + Undi interface context (Undi does not read or write this variable) + @param CpuAddr Virtual address that was mapped! + @param NumBytes size of memory mapped. + @param Direction direction of data flow for this memory's usage: + cpu->device, device->cpu or both ways. + @param DeviceAddr the mapped device address. + +**/ +VOID +EFIAPI +SnpUndi32CallbackSync ( + IN UINT64 UniqueId, + IN UINT64 CpuAddr, + IN UINT32 NumBytes, + IN UINT32 Direction, + IN UINT64 DeviceAddr + ); + +/** + Changes the state of a network interface from "stopped" to "started". + + This function starts a network interface. If the network interface successfully + starts, then EFI_SUCCESS will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + + @retval EFI_SUCCESS The network interface was started. + @retval EFI_ALREADY_STARTED The network interface is already in the started state. + @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure. + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + @retval EFI_UNSUPPORTED This function is not supported by the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Start ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This + ); + +/** + Changes the state of a network interface from "started" to "stopped". + + This function stops a network interface. This call is only valid if the network + interface is in the started state. If the network interface was successfully + stopped, then EFI_SUCCESS will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + + + @retval EFI_SUCCESS The network interface was stopped. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure. + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + @retval EFI_UNSUPPORTED This function is not supported by the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Stop ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This + ); + +/** + Resets a network adapter and allocates the transmit and receive buffers + required by the network interface; optionally, also requests allocation of + additional transmit and receive buffers. + + This function allocates the transmit and receive buffers required by the network + interface. If this allocation fails, then EFI_OUT_OF_RESOURCES is returned. + If the allocation succeeds and the network interface is successfully initialized, + then EFI_SUCCESS will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + + @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer space + that the driver should allocate for the network interface. + Some network interfaces will not be able to use the + extra buffer, and the caller will not know if it is + actually being used. + @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space + that the driver should allocate for the network interface. + Some network interfaces will not be able to use the + extra buffer, and the caller will not know if it is + actually being used. + + @retval EFI_SUCCESS The network interface was initialized. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit and + receive buffers. + @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure. + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + @retval EFI_UNSUPPORTED The increased buffer size feature is not supported. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Initialize ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN UINTN ExtraRxBufferSize OPTIONAL, + IN UINTN ExtraTxBufferSize OPTIONAL + ); + +/** + Resets a network adapter and reinitializes it with the parameters that were + provided in the previous call to Initialize(). + + This function resets a network adapter and reinitializes it with the parameters + that were provided in the previous call to Initialize(). The transmit and + receive queues are emptied and all pending interrupts are cleared. + Receive filters, the station address, the statistics, and the multicast-IP-to-HW + MAC addresses are not reset by this call. If the network interface was + successfully reset, then EFI_SUCCESS will be returned. If the driver has not + been initialized, EFI_DEVICE_ERROR will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param ExtendedVerification Indicates that the driver may perform a more + exhaustive verification operation of the device + during reset. + + @retval EFI_SUCCESS The network interface was reset. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value. + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + @retval EFI_UNSUPPORTED This function is not supported by the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Reset ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN BOOLEAN ExtendedVerification + ); + +/** + Resets a network adapter and leaves it in a state that is safe for another + driver to initialize. + + This function releases the memory buffers assigned in the Initialize() call. + Pending transmits and receives are lost, and interrupts are cleared and disabled. + After this call, only the Initialize() and Stop() calls may be used. If the + network interface was successfully shutdown, then EFI_SUCCESS will be returned. + If the driver has not been initialized, EFI_DEVICE_ERROR will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + + @retval EFI_SUCCESS The network interface was shutdown. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure. + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Shutdown ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This + ); + +/** + Manages the multicast receive filters of a network interface. + + This function is used enable and disable the hardware and software receive + filters for the underlying network device. + The receive filter change is broken down into three steps: + * The filter mask bits that are set (ON) in the Enable parameter are added to + the current receive filter settings. + * The filter mask bits that are set (ON) in the Disable parameter are subtracted + from the updated receive filter settings. + * If the resulting receive filter setting is not supported by the hardware a + more liberal setting is selected. + If the same bits are set in the Enable and Disable parameters, then the bits + in the Disable parameter takes precedence. + If the ResetMCastFilter parameter is TRUE, then the multicast address list + filter is disabled (irregardless of what other multicast bits are set in the + Enable and Disable parameters). The SNP->Mode->MCastFilterCount field is set + to zero. The Snp->Mode->MCastFilter contents are undefined. + After enabling or disabling receive filter settings, software should verify + the new settings by checking the Snp->Mode->ReceiveFilterSettings, + Snp->Mode->MCastFilterCount and Snp->Mode->MCastFilter fields. + Note: Some network drivers and/or devices will automatically promote receive + filter settings if the requested setting can not be honored. For example, if + a request for four multicast addresses is made and the underlying hardware + only supports two multicast addresses the driver might set the promiscuous + or promiscuous multicast receive filters instead. The receiving software is + responsible for discarding any extra packets that get through the hardware + receive filters. + Note: Note: To disable all receive filter hardware, the network driver must + be Shutdown() and Stopped(). Calling ReceiveFilters() with Disable set to + Snp->Mode->ReceiveFilterSettings will make it so no more packets are + returned by the Receive() function, but the receive hardware may still be + moving packets into system memory before inspecting and discarding them. + Unexpected system errors, reboots and hangs can occur if an OS is loaded + and the network devices are not Shutdown() and Stopped(). + If ResetMCastFilter is TRUE, then the multicast receive filter list on the + network interface will be reset to the default multicast receive filter list. + If ResetMCastFilter is FALSE, and this network interface allows the multicast + receive filter list to be modified, then the MCastFilterCnt and MCastFilter + are used to update the current multicast receive filter list. The modified + receive filter list settings can be found in the MCastFilter field of + EFI_SIMPLE_NETWORK_MODE. If the network interface does not allow the multicast + receive filter list to be modified, then EFI_INVALID_PARAMETER will be returned. + If the driver has not been initialized, EFI_DEVICE_ERROR will be returned. + If the receive filter mask and multicast receive filter list have been + successfully updated on the network interface, EFI_SUCCESS will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param Enable A bit mask of receive filters to enable on the network + interface. + @param Disable A bit mask of receive filters to disable on the network + interface. For backward compatibility with EFI 1.1 + platforms, the EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST bit + must be set when the ResetMCastFilter parameter is TRUE. + @param ResetMCastFilter Set to TRUE to reset the contents of the multicast + receive filters on the network interface to their + default values. + @param MCastFilterCnt Number of multicast HW MAC addresses in the new MCastFilter + list. This value must be less than or equal to the + MCastFilterCnt field of EFI_SIMPLE_NETWORK_MODE. + This field is optional if ResetMCastFilter is TRUE. + @param MCastFilter A pointer to a list of new multicast receive filter HW + MAC addresses. This list will replace any existing + multicast HW MAC address list. This field is optional + if ResetMCastFilter is TRUE. + + @retval EFI_SUCCESS The multicast receive filter list was updated. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + * This is NULL + * There are bits set in Enable that are not set + in Snp->Mode->ReceiveFilterMask + * There are bits set in Disable that are not set + in Snp->Mode->ReceiveFilterMask + * Multicast is being enabled (the + EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST bit is + set in Enable, it is not set in Disable, and + ResetMCastFilter is FALSE) and MCastFilterCount + is zero + * Multicast is being enabled and MCastFilterCount + is greater than Snp->Mode->MaxMCastFilterCount + * Multicast is being enabled and MCastFilter is NULL + * Multicast is being enabled and one or more of + the addresses in the MCastFilter list are not + valid multicast MAC addresses + @retval EFI_DEVICE_ERROR One or more of the following conditions is TRUE: + * The network interface has been started but has + not been initialized + * An unexpected error was returned by the + underlying network driver or device + @retval EFI_UNSUPPORTED This function is not supported by the network + interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32ReceiveFilters ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN UINT32 Enable, + IN UINT32 Disable, + IN BOOLEAN ResetMCastFilter, + IN UINTN MCastFilterCnt, OPTIONAL + IN EFI_MAC_ADDRESS *MCastFilter OPTIONAL + ); + +/** + Modifies or resets the current station address, if supported. + + This function modifies or resets the current station address of a network + interface, if supported. If Reset is TRUE, then the current station address is + set to the network interface's permanent address. If Reset is FALSE, and the + network interface allows its station address to be modified, then the current + station address is changed to the address specified by New. If the network + interface does not allow its station address to be modified, then + EFI_INVALID_PARAMETER will be returned. If the station address is successfully + updated on the network interface, EFI_SUCCESS will be returned. If the driver + has not been initialized, EFI_DEVICE_ERROR will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param Reset Flag used to reset the station address to the network interface's + permanent address. + @param New New station address to be used for the network interface. + + + @retval EFI_SUCCESS The network interface's station address was updated. + @retval EFI_NOT_STARTED The Simple Network Protocol interface has not been + started by calling Start(). + @retval EFI_INVALID_PARAMETER The New station address was not accepted by the NIC. + @retval EFI_INVALID_PARAMETER Reset is FALSE and New is NULL. + @retval EFI_DEVICE_ERROR The Simple Network Protocol interface has not + been initialized by calling Initialize(). + @retval EFI_DEVICE_ERROR An error occurred attempting to set the new + station address. + @retval EFI_UNSUPPORTED The NIC does not support changing the network + interface's station address. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32StationAddress ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN BOOLEAN Reset, + IN EFI_MAC_ADDRESS *New OPTIONAL + ); + +/** + Resets or collects the statistics on a network interface. + + This function resets or collects the statistics on a network interface. If the + size of the statistics table specified by StatisticsSize is not big enough for + all the statistics that are collected by the network interface, then a partial + buffer of statistics is returned in StatisticsTable, StatisticsSize is set to + the size required to collect all the available statistics, and + EFI_BUFFER_TOO_SMALL is returned. + If StatisticsSize is big enough for all the statistics, then StatisticsTable + will be filled, StatisticsSize will be set to the size of the returned + StatisticsTable structure, and EFI_SUCCESS is returned. + If the driver has not been initialized, EFI_DEVICE_ERROR will be returned. + If Reset is FALSE, and both StatisticsSize and StatisticsTable are NULL, then + no operations will be performed, and EFI_SUCCESS will be returned. + If Reset is TRUE, then all of the supported statistics counters on this network + interface will be reset to zero. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param Reset Set to TRUE to reset the statistics for the network interface. + @param StatisticsSize On input the size, in bytes, of StatisticsTable. On output + the size, in bytes, of the resulting table of statistics. + @param StatisticsTable A pointer to the EFI_NETWORK_STATISTICS structure that + contains the statistics. Type EFI_NETWORK_STATISTICS is + defined in "Related Definitions" below. + + @retval EFI_SUCCESS The requested operation succeeded. + @retval EFI_NOT_STARTED The Simple Network Protocol interface has not been + started by calling Start(). + @retval EFI_BUFFER_TOO_SMALL StatisticsSize is not NULL and StatisticsTable is + NULL. The current buffer size that is needed to + hold all the statistics is returned in StatisticsSize. + @retval EFI_BUFFER_TOO_SMALL StatisticsSize is not NULL and StatisticsTable is + not NULL. The current buffer size that is needed + to hold all the statistics is returned in + StatisticsSize. A partial set of statistics is + returned in StatisticsTable. + @retval EFI_INVALID_PARAMETER StatisticsSize is NULL and StatisticsTable is not + NULL. + @retval EFI_DEVICE_ERROR The Simple Network Protocol interface has not + been initialized by calling Initialize(). + @retval EFI_DEVICE_ERROR An error was encountered collecting statistics + from the NIC. + @retval EFI_UNSUPPORTED The NIC does not support collecting statistics + from the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Statistics ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN BOOLEAN Reset, + IN OUT UINTN *StatisticsSize, OPTIONAL + IN OUT EFI_NETWORK_STATISTICS *StatisticsTable OPTIONAL + ); + +/** + Converts a multicast IP address to a multicast HW MAC address. + + This function converts a multicast IP address to a multicast HW MAC address + for all packet transactions. If the mapping is accepted, then EFI_SUCCESS will + be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param IPv6 Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. + Set to FALSE if the multicast IP address is IPv4 [RFC 791]. + @param IP The multicast IP address that is to be converted to a multicast + HW MAC address. + @param MAC The multicast HW MAC address that is to be generated from IP. + + @retval EFI_SUCCESS The multicast IP address was mapped to the + multicast HW MAC address. + @retval EFI_NOT_STARTED The Simple Network Protocol interface has not + been started by calling Start(). + @retval EFI_INVALID_PARAMETER IP is NULL. + @retval EFI_INVALID_PARAMETER MAC is NULL. + @retval EFI_INVALID_PARAMETER IP does not point to a valid IPv4 or IPv6 + multicast address. + @retval EFI_DEVICE_ERROR The Simple Network Protocol interface has not + been initialized by calling Initialize(). + @retval EFI_UNSUPPORTED IPv6 is TRUE and the implementation does not + support IPv6 multicast to MAC address conversion. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32McastIpToMac ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN BOOLEAN IPv6, + IN EFI_IP_ADDRESS *IP, + OUT EFI_MAC_ADDRESS *MAC + ); + +/** + Performs read and write operations on the NVRAM device attached to a network + interface. + + This function performs read and write operations on the NVRAM device attached + to a network interface. If ReadWrite is TRUE, a read operation is performed. + If ReadWrite is FALSE, a write operation is performed. Offset specifies the + byte offset at which to start either operation. Offset must be a multiple of + NvRamAccessSize , and it must have a value between zero and NvRamSize. + BufferSize specifies the length of the read or write operation. BufferSize must + also be a multiple of NvRamAccessSize, and Offset + BufferSize must not exceed + NvRamSize. + If any of the above conditions is not met, then EFI_INVALID_PARAMETER will be + returned. + If all the conditions are met and the operation is "read," the NVRAM device + attached to the network interface will be read into Buffer and EFI_SUCCESS + will be returned. If this is a write operation, the contents of Buffer will be + used to update the contents of the NVRAM device attached to the network + interface and EFI_SUCCESS will be returned. + + It does the basic checking on the input parameters and retrieves snp structure + and then calls the read_nvdata() call which does the actual reading + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param ReadWrite TRUE for read operations, FALSE for write operations. + @param Offset Byte offset in the NVRAM device at which to start the read or + write operation. This must be a multiple of NvRamAccessSize + and less than NvRamSize. (See EFI_SIMPLE_NETWORK_MODE) + @param BufferSize The number of bytes to read or write from the NVRAM device. + This must also be a multiple of NvramAccessSize. + @param Buffer A pointer to the data buffer. + + @retval EFI_SUCCESS The NVRAM access was performed. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + * The This parameter is NULL + * The This parameter does not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure + * The Offset parameter is not a multiple of + EFI_SIMPLE_NETWORK_MODE.NvRamAccessSize + * The Offset parameter is not less than + EFI_SIMPLE_NETWORK_MODE.NvRamSize + * The BufferSize parameter is not a multiple of + EFI_SIMPLE_NETWORK_MODE.NvRamAccessSize + * The Buffer parameter is NULL + @retval EFI_DEVICE_ERROR The command could not be sent to the network + interface. + @retval EFI_UNSUPPORTED This function is not supported by the network + interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32NvData ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN BOOLEAN ReadWrite, + IN UINTN Offset, + IN UINTN BufferSize, + IN OUT VOID *Buffer + ); + +/** + Reads the current interrupt status and recycled transmit buffer status from a + network interface. + + This function gets the current interrupt and recycled transmit buffer status + from the network interface. The interrupt status is returned as a bit mask in + InterruptStatus. If InterruptStatus is NULL, the interrupt status will not be + read. If TxBuf is not NULL, a recycled transmit buffer address will be retrieved. + If a recycled transmit buffer address is returned in TxBuf, then the buffer has + been successfully transmitted, and the status for that buffer is cleared. If + the status of the network interface is successfully collected, EFI_SUCCESS + will be returned. If the driver has not been initialized, EFI_DEVICE_ERROR will + be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param InterruptStatus A pointer to the bit mask of the currently active + interrupts (see "Related Definitions"). If this is NULL, + the interrupt status will not be read from the device. + If this is not NULL, the interrupt status will be read + from the device. When the interrupt status is read, it + will also be cleared. Clearing the transmit interrupt does + not empty the recycled transmit buffer array. + @param TxBuf Recycled transmit buffer address. The network interface + will not transmit if its internal recycled transmit + buffer array is full. Reading the transmit buffer does + not clear the transmit interrupt. If this is NULL, then + the transmit buffer status will not be read. If there + are no transmit buffers to recycle and TxBuf is not NULL, + TxBuf will be set to NULL. + + @retval EFI_SUCCESS The status of the network interface was retrieved. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure. + @retval EFI_DEVICE_ERROR The command could not be sent to the network + interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32GetStatus ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + OUT UINT32 *InterruptStatus, OPTIONAL + OUT VOID **TxBuf OPTIONAL + ); + +/** + Places a packet in the transmit queue of a network interface. + + This function places the packet specified by Header and Buffer on the transmit + queue. If HeaderSize is nonzero and HeaderSize is not equal to + This->Mode->MediaHeaderSize, then EFI_INVALID_PARAMETER will be returned. If + BufferSize is less than This->Mode->MediaHeaderSize, then EFI_BUFFER_TOO_SMALL + will be returned. If Buffer is NULL, then EFI_INVALID_PARAMETER will be + returned. If HeaderSize is nonzero and DestAddr or Protocol is NULL, then + EFI_INVALID_PARAMETER will be returned. If the transmit engine of the network + interface is busy, then EFI_NOT_READY will be returned. If this packet can be + accepted by the transmit engine of the network interface, the packet contents + specified by Buffer will be placed on the transmit queue of the network + interface, and EFI_SUCCESS will be returned. GetStatus() can be used to + determine when the packet has actually been transmitted. The contents of the + Buffer must not be modified until the packet has actually been transmitted. + The Transmit() function performs nonblocking I/O. A caller who wants to perform + blocking I/O, should call Transmit(), and then GetStatus() until the + transmitted buffer shows up in the recycled transmit buffer. + If the driver has not been initialized, EFI_DEVICE_ERROR will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param HeaderSize The size, in bytes, of the media header to be filled in by the + Transmit() function. If HeaderSize is nonzero, then it must + be equal to This->Mode->MediaHeaderSize and the DestAddr and + Protocol parameters must not be NULL. + @param BufferSize The size, in bytes, of the entire packet (media header and + data) to be transmitted through the network interface. + @param Buffer A pointer to the packet (media header followed by data) to be + transmitted. This parameter cannot be NULL. If HeaderSize is + zero, then the media header in Buffer must already be filled + in by the caller. If HeaderSize is nonzero, then the media + header will be filled in by the Transmit() function. + @param SrcAddr The source HW MAC address. If HeaderSize is zero, then this + parameter is ignored. If HeaderSize is nonzero and SrcAddr + is NULL, then This->Mode->CurrentAddress is used for the + source HW MAC address. + @param DestAddr The destination HW MAC address. If HeaderSize is zero, then + this parameter is ignored. + @param Protocol The type of header to build. If HeaderSize is zero, then this + parameter is ignored. See RFC 1700, section "Ether Types," + for examples. + + @retval EFI_SUCCESS The packet was placed on the transmit queue. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_NOT_READY The network interface is too busy to accept this + transmit request. + @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small. + @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported + value. + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + @retval EFI_UNSUPPORTED This function is not supported by the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Transmit ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN UINTN HeaderSize, + IN UINTN BufferSize, + IN VOID *Buffer, + IN EFI_MAC_ADDRESS *SrcAddr, OPTIONAL + IN EFI_MAC_ADDRESS *DestAddr, OPTIONAL + IN UINT16 *Protocol OPTIONAL + ); + +/** + Receives a packet from a network interface. + + This function retrieves one packet from the receive queue of a network interface. + If there are no packets on the receive queue, then EFI_NOT_READY will be + returned. If there is a packet on the receive queue, and the size of the packet + is smaller than BufferSize, then the contents of the packet will be placed in + Buffer, and BufferSize will be updated with the actual size of the packet. + In addition, if SrcAddr, DestAddr, and Protocol are not NULL, then these values + will be extracted from the media header and returned. EFI_SUCCESS will be + returned if a packet was successfully received. + If BufferSize is smaller than the received packet, then the size of the receive + packet will be placed in BufferSize and EFI_BUFFER_TOO_SMALL will be returned. + If the driver has not been initialized, EFI_DEVICE_ERROR will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param HeaderSize The size, in bytes, of the media header received on the network + interface. If this parameter is NULL, then the media header size + will not be returned. + @param BufferSize On entry, the size, in bytes, of Buffer. On exit, the size, in + bytes, of the packet that was received on the network interface. + @param Buffer A pointer to the data buffer to receive both the media + header and the data. + @param SrcAddr The source HW MAC address. If this parameter is NULL, the HW + MAC source address will not be extracted from the media header. + @param DestAddr The destination HW MAC address. If this parameter is NULL, + the HW MAC destination address will not be extracted from + the media header. + @param Protocol The media header type. If this parameter is NULL, then the + protocol will not be extracted from the media header. See + RFC 1700 section "Ether Types" for examples. + + @retval EFI_SUCCESS The received data was stored in Buffer, and + BufferSize has been updated to the number of + bytes received. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_NOT_READY No packets have been received on the network interface. + @retval EFI_BUFFER_TOO_SMALL BufferSize is too small for the received packets. + BufferSize has been updated to the required size. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + * The This parameter is NULL + * The This parameter does not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure. + * The BufferSize parameter is NULL + * The Buffer parameter is NULL + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Receive ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + OUT UINTN *HeaderSize OPTIONAL, + IN OUT UINTN *BufferSize, + OUT VOID *Buffer, + OUT EFI_MAC_ADDRESS *SrcAddr OPTIONAL, + OUT EFI_MAC_ADDRESS *DestAddr OPTIONAL, + OUT UINT16 *Protocol OPTIONAL + ); + +/** + Nofication call back function for WaitForPacket event. + + @param Event EFI Event. + @param SnpPtr Pointer to SNP_DRIVER structure. + +**/ +VOID +EFIAPI +SnpWaitForPacketNotify ( + EFI_EVENT Event, + VOID *SnpPtr + ); + +#define SNP_MEM_PAGES(x) (((x) - 1) / 4096 + 1) + + +#endif /* _SNP_H_ */ diff --git a/NetworkPkg/SnpDxe/SnpDxe.inf b/NetworkPkg/SnpDxe/SnpDxe.inf new file mode 100644 index 000000000..9a37b12e5 --- /dev/null +++ b/NetworkPkg/SnpDxe/SnpDxe.inf @@ -0,0 +1,78 @@ +## @file +# This module produces EFI SNP Protocol. +# +# This module produces Simple Network Protocol upon EFI Network Interface +# Identifier Protocol, to provide a packet level interface to a network adapter. +# +# Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = SnpDxe + MODULE_UNI_FILE = SnpDxe.uni + FILE_GUID = A2f436EA-A127-4EF8-957C-8048606FF670 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = InitializeSnpNiiDriver + UNLOAD_IMAGE = NetLibDefaultUnload + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = mSimpleNetworkDriverBinding +# COMPONENT_NAME = gSimpleNetworkComponentName +# COMPONENT_NAME2 = gSimpleNetworkComponentName2 +# + +[Sources] + Receive.c + Snp.h + Nvdata.c + Get_status.c + Start.c + Snp.c + Stop.c + Statistics.c + Reset.c + Shutdown.c + Mcast_ip_to_mac.c + Transmit.c + WaitForPacket.c + Receive_filters.c + Initialize.c + ComponentName.c + Callback.c + Station_address.c + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + UefiLib + BaseLib + UefiBootServicesTableLib + UefiDriverEntryPoint + BaseMemoryLib + DebugLib + NetLib + +[Guids] + gEfiEventExitBootServicesGuid ## SOMETIMES_CONSUMES ## Event + +[Protocols] + gEfiSimpleNetworkProtocolGuid ## BY_START + gEfiDevicePathProtocolGuid ## TO_START + gEfiNetworkInterfaceIdentifierProtocolGuid_31 ## TO_START + gEfiPciIoProtocolGuid ## TO_START + +[UserExtensions.TianoCore."ExtraFiles"] + SnpDxeExtra.uni diff --git a/NetworkPkg/SnpDxe/SnpDxe.uni b/NetworkPkg/SnpDxe/SnpDxe.uni new file mode 100644 index 000000000..f37bb17fe --- /dev/null +++ b/NetworkPkg/SnpDxe/SnpDxe.uni @@ -0,0 +1,17 @@ +// /** @file +// This module produces EFI SNP Protocol. +// +// This module produces Simple Network Protocol upon EFI Network Interface +// Identifier Protocol, to provide a packet level interface to a network adapter. +// +// Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Produces EFI SNP Protocol" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces Simple Network Protocol upon EFI Network Interface Identifier Protocol to provide a packet level interface to a network adapter." + diff --git a/NetworkPkg/SnpDxe/SnpDxeExtra.uni b/NetworkPkg/SnpDxe/SnpDxeExtra.uni new file mode 100644 index 000000000..5cf6e9d18 --- /dev/null +++ b/NetworkPkg/SnpDxe/SnpDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// SnpDxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"SNP DXE Driver" + + diff --git a/NetworkPkg/SnpDxe/Start.c b/NetworkPkg/SnpDxe/Start.c new file mode 100644 index 000000000..033ca22f6 --- /dev/null +++ b/NetworkPkg/SnpDxe/Start.c @@ -0,0 +1,162 @@ +/** @file + Implementation of starting a network adapter. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + + +/** + Call UNDI to start the interface and changes the snp state. + + @param Snp pointer to snp driver structure. + + @retval EFI_SUCCESS UNDI is started successfully. + @retval EFI_DEVICE_ERROR UNDI could not be started. + +**/ +EFI_STATUS +PxeStart ( + IN SNP_DRIVER *Snp + ) +{ + PXE_CPB_START_31 *Cpb31; + + Cpb31 = Snp->Cpb; + // + // Initialize UNDI Start CDB for H/W UNDI + // + Snp->Cdb.OpCode = PXE_OPCODE_START; + Snp->Cdb.OpFlags = PXE_OPFLAGS_NOT_USED; + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED; + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED; + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Make changes to H/W UNDI Start CDB if this is + // a S/W UNDI. + // + if (Snp->IsSwUndi) { + Snp->Cdb.CPBsize = (UINT16) sizeof (PXE_CPB_START_31); + Snp->Cdb.CPBaddr = (UINT64)(UINTN) Cpb31; + + Cpb31->Delay = (UINT64)(UINTN) &SnpUndi32CallbackDelay; + Cpb31->Block = (UINT64)(UINTN) &SnpUndi32CallbackBlock; + + // + // Virtual == Physical. This can be set to zero. + // + Cpb31->Virt2Phys = (UINT64)(UINTN) 0; + Cpb31->Mem_IO = (UINT64)(UINTN) &SnpUndi32CallbackMemio; + + Cpb31->Map_Mem = (UINT64)(UINTN) &SnpUndi32CallbackMap; + Cpb31->UnMap_Mem = (UINT64)(UINTN) &SnpUndi32CallbackUnmap; + Cpb31->Sync_Mem = (UINT64)(UINTN) &SnpUndi32CallbackSync; + + Cpb31->Unique_ID = (UINT64)(UINTN) Snp; + } + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nsnp->undi.start() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) { + // + // UNDI could not be started. Return UNDI error. + // + DEBUG ( + (EFI_D_ERROR, + "\nsnp->undi.start() %xh:%xh\n", + Snp->Cdb.StatCode, + Snp->Cdb.StatFlags) + ); + + return EFI_DEVICE_ERROR; + } + // + // Set simple network state to Started and return success. + // + Snp->Mode.State = EfiSimpleNetworkStarted; + + return EFI_SUCCESS; +} + + +/** + Change the state of a network interface from "stopped" to "started." + + This function starts a network interface. If the network interface successfully + starts, then EFI_SUCCESS will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + + @retval EFI_SUCCESS The network interface was started. + @retval EFI_ALREADY_STARTED The network interface is already in the started state. + @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid + EFI_SIMPLE_NETWORK_PROTOCOL structure. + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + @retval EFI_UNSUPPORTED This function is not supported by the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Start ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This + ) +{ + SNP_DRIVER *Snp; + EFI_STATUS Status; + UINTN Index; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + switch (Snp->Mode.State) { + case EfiSimpleNetworkStopped: + break; + + case EfiSimpleNetworkStarted: + case EfiSimpleNetworkInitialized: + Status = EFI_ALREADY_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + Status = PxeStart (Snp); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + // + // clear the map_list in SNP structure + // + for (Index = 0; Index < MAX_MAP_LENGTH; Index++) { + Snp->MapList[Index].VirtualAddress = 0; + Snp->MapList[Index].MapCookie = 0; + } + + Snp->Mode.MCastFilterCount = 0; + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/Station_address.c b/NetworkPkg/SnpDxe/Station_address.c new file mode 100644 index 000000000..a5c87d479 --- /dev/null +++ b/NetworkPkg/SnpDxe/Station_address.c @@ -0,0 +1,243 @@ +/** @file + Implementation of reading the MAC address of a network adapter. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + + +/** + Call UNDI to read the MAC address of the NIC and update the mode structure + with the address. + + @param Snp Pointer to snp driver structure. + + @retval EFI_SUCCESS The MAC address of the NIC is read successfully. + @retval EFI_DEVICE_ERROR Failed to read the MAC address of the NIC. + +**/ +EFI_STATUS +PxeGetStnAddr ( + SNP_DRIVER *Snp + ) +{ + PXE_DB_STATION_ADDRESS *Db; + + Db = Snp->Db; + Snp->Cdb.OpCode = PXE_OPCODE_STATION_ADDRESS; + Snp->Cdb.OpFlags = PXE_OPFLAGS_STATION_ADDRESS_READ; + + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + + Snp->Cdb.DBsize = (UINT16) sizeof (PXE_DB_STATION_ADDRESS); + Snp->Cdb.DBaddr = (UINT64)(UINTN) Db; + + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nsnp->undi.station_addr() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) { + DEBUG ( + (EFI_D_ERROR, + "\nsnp->undi.station_addr() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_DEVICE_ERROR; + } + // + // Set new station address in SNP->Mode structure and return success. + // + CopyMem ( + &(Snp->Mode.CurrentAddress), + &Db->StationAddr, + Snp->Mode.HwAddressSize + ); + + CopyMem ( + &Snp->Mode.BroadcastAddress, + &Db->BroadcastAddr, + Snp->Mode.HwAddressSize + ); + + CopyMem ( + &Snp->Mode.PermanentAddress, + &Db->PermanentAddr, + Snp->Mode.HwAddressSize + ); + + return EFI_SUCCESS; +} + + +/** + Call UNDI to set a new MAC address for the NIC. + + @param Snp Pointer to Snp driver structure. + @param NewMacAddr Pointer to a MAC address to be set for the NIC, if this is + NULL then this routine resets the mac address to the NIC's + original address. + + +**/ +EFI_STATUS +PxeSetStnAddr ( + SNP_DRIVER *Snp, + EFI_MAC_ADDRESS *NewMacAddr + ) +{ + PXE_CPB_STATION_ADDRESS *Cpb; + PXE_DB_STATION_ADDRESS *Db; + + Cpb = Snp->Cpb; + Db = Snp->Db; + Snp->Cdb.OpCode = PXE_OPCODE_STATION_ADDRESS; + + if (NewMacAddr == NULL) { + Snp->Cdb.OpFlags = PXE_OPFLAGS_STATION_ADDRESS_RESET; + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + } else { + Snp->Cdb.OpFlags = PXE_OPFLAGS_STATION_ADDRESS_WRITE; + // + // Supplying a new address in the CPB will make undi change the mac address to the new one. + // + CopyMem (&Cpb->StationAddr, NewMacAddr, Snp->Mode.HwAddressSize); + + Snp->Cdb.CPBsize = (UINT16) sizeof (PXE_CPB_STATION_ADDRESS); + Snp->Cdb.CPBaddr = (UINT64)(UINTN) Cpb; + } + + Snp->Cdb.DBsize = (UINT16) sizeof (PXE_DB_STATION_ADDRESS); + Snp->Cdb.DBaddr = (UINT64)(UINTN) Db; + + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nsnp->undi.station_addr() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) { + DEBUG ( + (EFI_D_ERROR, + "\nsnp->undi.station_addr() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + // + // UNDI command failed. Return UNDI status to caller. + // + return EFI_DEVICE_ERROR; + } + // + // read the changed address and save it in SNP->Mode structure + // + PxeGetStnAddr (Snp); + + return EFI_SUCCESS; +} + + +/** + Modifies or resets the current station address, if supported. + + This function modifies or resets the current station address of a network + interface, if supported. If Reset is TRUE, then the current station address is + set to the network interface's permanent address. If Reset is FALSE, and the + network interface allows its station address to be modified, then the current + station address is changed to the address specified by New. If the network + interface does not allow its station address to be modified, then + EFI_INVALID_PARAMETER will be returned. If the station address is successfully + updated on the network interface, EFI_SUCCESS will be returned. If the driver + has not been initialized, EFI_DEVICE_ERROR will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param Reset Flag used to reset the station address to the network interface's + permanent address. + @param New New station address to be used for the network interface. + + + @retval EFI_SUCCESS The network interface's station address was updated. + @retval EFI_NOT_STARTED The Simple Network Protocol interface has not been + started by calling Start(). + @retval EFI_INVALID_PARAMETER The New station address was not accepted by the NIC. + @retval EFI_INVALID_PARAMETER Reset is FALSE and New is NULL. + @retval EFI_DEVICE_ERROR The Simple Network Protocol interface has not + been initialized by calling Initialize(). + @retval EFI_DEVICE_ERROR An error occurred attempting to set the new + station address. + @retval EFI_UNSUPPORTED The NIC does not support changing the network + interface's station address. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32StationAddress ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN BOOLEAN Reset, + IN EFI_MAC_ADDRESS *New OPTIONAL + ) +{ + SNP_DRIVER *Snp; + EFI_STATUS Status; + EFI_TPL OldTpl; + + // + // Check for invalid parameter combinations. + // + if ((This == NULL) || + (!Reset && (New == NULL))) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Return error if the SNP is not initialized. + // + switch (Snp->Mode.State) { + case EfiSimpleNetworkInitialized: + break; + + case EfiSimpleNetworkStopped: + Status = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + if (Reset) { + Status = PxeSetStnAddr (Snp, NULL); + } else { + Status = PxeSetStnAddr (Snp, New); + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/Statistics.c b/NetworkPkg/SnpDxe/Statistics.c new file mode 100644 index 000000000..480e26178 --- /dev/null +++ b/NetworkPkg/SnpDxe/Statistics.c @@ -0,0 +1,224 @@ +/** @file + Implementation of collecting the statistics on a network interface. + +Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Snp.h" + + +/** + Resets or collects the statistics on a network interface. + + This function resets or collects the statistics on a network interface. If the + size of the statistics table specified by StatisticsSize is not big enough for + all the statistics that are collected by the network interface, then a partial + buffer of statistics is returned in StatisticsTable, StatisticsSize is set to + the size required to collect all the available statistics, and + EFI_BUFFER_TOO_SMALL is returned. + If StatisticsSize is big enough for all the statistics, then StatisticsTable + will be filled, StatisticsSize will be set to the size of the returned + StatisticsTable structure, and EFI_SUCCESS is returned. + If the driver has not been initialized, EFI_DEVICE_ERROR will be returned. + If Reset is FALSE, and both StatisticsSize and StatisticsTable are NULL, then + no operations will be performed, and EFI_SUCCESS will be returned. + If Reset is TRUE, then all of the supported statistics counters on this network + interface will be reset to zero. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param Reset Set to TRUE to reset the statistics for the network interface. + @param StatisticsSize On input the size, in bytes, of StatisticsTable. On output + the size, in bytes, of the resulting table of statistics. + @param StatisticsTable A pointer to the EFI_NETWORK_STATISTICS structure that + contains the statistics. Type EFI_NETWORK_STATISTICS is + defined in "Related Definitions" below. + + @retval EFI_SUCCESS The requested operation succeeded. + @retval EFI_NOT_STARTED The Simple Network Protocol interface has not been + started by calling Start(). + @retval EFI_BUFFER_TOO_SMALL StatisticsSize is not NULL and StatisticsTable is + NULL. The current buffer size that is needed to + hold all the statistics is returned in StatisticsSize. + @retval EFI_BUFFER_TOO_SMALL StatisticsSize is not NULL and StatisticsTable is + not NULL. The current buffer size that is needed + to hold all the statistics is returned in + StatisticsSize. A partial set of statistics is + returned in StatisticsTable. + @retval EFI_INVALID_PARAMETER StatisticsSize is NULL and StatisticsTable is not + NULL. + @retval EFI_DEVICE_ERROR The Simple Network Protocol interface has not + been initialized by calling Initialize(). + @retval EFI_DEVICE_ERROR An error was encountered collecting statistics + from the NIC. + @retval EFI_UNSUPPORTED The NIC does not support collecting statistics + from the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Statistics ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN BOOLEAN Reset, + IN OUT UINTN *StatisticsSize, OPTIONAL + IN OUT EFI_NETWORK_STATISTICS *StatisticsTable OPTIONAL + ) +{ + SNP_DRIVER *Snp; + PXE_DB_STATISTICS *Db; + UINT64 *Stp; + UINT64 Mask; + UINTN Size; + UINTN Index; + EFI_TPL OldTpl; + EFI_STATUS Status; + + // + // Get pointer to SNP driver instance for *This. + // + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Return error if the SNP is not initialized. + // + switch (Snp->Mode.State) { + case EfiSimpleNetworkInitialized: + break; + + case EfiSimpleNetworkStopped: + Status = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + // + // if we are not resetting the counters, we have to have a valid stat table + // with >0 size. if no reset, no table and no size, return success. + // + if (!Reset && StatisticsSize == NULL) { + Status = (StatisticsTable != NULL) ? EFI_INVALID_PARAMETER : EFI_SUCCESS; + goto ON_EXIT; + } + // + // Initialize UNDI Statistics CDB + // + Snp->Cdb.OpCode = PXE_OPCODE_STATISTICS; + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + if (Reset) { + Snp->Cdb.OpFlags = PXE_OPFLAGS_STATISTICS_RESET; + Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED; + Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED; + Db = Snp->Db; + } else { + Snp->Cdb.OpFlags = PXE_OPFLAGS_STATISTICS_READ; + Snp->Cdb.DBsize = (UINT16) sizeof (PXE_DB_STATISTICS); + Snp->Cdb.DBaddr = (UINT64)(UINTN) (Db = Snp->Db); + } + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nsnp->undi.statistics() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + switch (Snp->Cdb.StatCode) { + case PXE_STATCODE_SUCCESS: + break; + + case PXE_STATCODE_UNSUPPORTED: + DEBUG ( + (EFI_D_ERROR, + "\nsnp->undi.statistics() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + Status = EFI_UNSUPPORTED; + goto ON_EXIT; + + default: + DEBUG ( + (EFI_D_ERROR, + "\nsnp->undi.statistics() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + if (Reset) { + Status = EFI_SUCCESS; + goto ON_EXIT; + } + + if (StatisticsTable == NULL) { + *StatisticsSize = sizeof (EFI_NETWORK_STATISTICS); + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + // + // Convert the UNDI statistics information to SNP statistics + // information. + // + ZeroMem (StatisticsTable, *StatisticsSize); + Stp = (UINT64 *) StatisticsTable; + Size = 0; + + for (Index = 0, Mask = 1; Index < 64; Index++, Mask = LShiftU64 (Mask, 1), Stp++) { + // + // There must be room for a full UINT64. Partial + // numbers will not be stored. + // + if ((Index + 1) * sizeof (UINT64) > *StatisticsSize) { + break; + } + + if ((Db->Supported & Mask) != 0) { + *Stp = Db->Data[Index]; + Size = Index + 1; + } else { + SetMem (Stp, sizeof (UINT64), 0xFF); + } + } + // + // Compute size up to last supported statistic. + // + while (++Index < 64) { + if ((Db->Supported & (Mask = LShiftU64 (Mask, 1))) != 0) { + Size = Index; + } + } + + Size *= sizeof (UINT64); + + if (*StatisticsSize >= Size) { + *StatisticsSize = Size; + Status = EFI_SUCCESS; + } else { + *StatisticsSize = Size; + Status = EFI_BUFFER_TOO_SMALL; + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/Stop.c b/NetworkPkg/SnpDxe/Stop.c new file mode 100644 index 000000000..f1c1bd847 --- /dev/null +++ b/NetworkPkg/SnpDxe/Stop.c @@ -0,0 +1,120 @@ +/** @file + Implementation of stopping a network interface. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + + +/** + Call UNDI to stop the interface and changes the snp state. + + @param Snp Pointer to snp driver structure + + @retval EFI_SUCCESS The network interface was stopped. + @retval EFI_DEVICE_ERROR SNP is not initialized. + +**/ +EFI_STATUS +PxeStop ( + SNP_DRIVER *Snp + ) +{ + Snp->Cdb.OpCode = PXE_OPCODE_STOP; + Snp->Cdb.OpFlags = PXE_OPFLAGS_NOT_USED; + Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED; + Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED; + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Issue UNDI command + // + DEBUG ((EFI_D_NET, "\nsnp->undi.stop() ")); + + (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb); + + if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) { + DEBUG ( + (EFI_D_WARN, + "\nsnp->undi.stop() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_DEVICE_ERROR; + } + // + // Set simple network state to Started and return success. + // + Snp->Mode.State = EfiSimpleNetworkStopped; + return EFI_SUCCESS; +} + + +/** + Changes the state of a network interface from "started" to "stopped." + + This function stops a network interface. This call is only valid if the network + interface is in the started state. If the network interface was successfully + stopped, then EFI_SUCCESS will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL + instance. + + + @retval EFI_SUCCESS The network interface was stopped. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a + valid EFI_SIMPLE_NETWORK_PROTOCOL structure. + @retval EFI_DEVICE_ERROR The command could not be sent to the network + interface. + @retval EFI_UNSUPPORTED This function is not supported by the network + interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Stop ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This + ) +{ + SNP_DRIVER *Snp; + EFI_TPL OldTpl; + EFI_STATUS Status; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + switch (Snp->Mode.State) { + case EfiSimpleNetworkStarted: + break; + + case EfiSimpleNetworkStopped: + Status = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + Status = PxeStop (Snp); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/Transmit.c b/NetworkPkg/SnpDxe/Transmit.c new file mode 100644 index 000000000..44fdd71f4 --- /dev/null +++ b/NetworkPkg/SnpDxe/Transmit.c @@ -0,0 +1,353 @@ +/** @file + Implementation of transmitting a packet. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + + +/** + Call UNDI to create the meadia header for the given data buffer. + + @param Snp Pointer to SNP driver structure. + @param MacHeaderPtr Address where the media header will be filled in. + @param HeaderSize Size of the memory at MacHeaderPtr. + @param Buffer Data buffer pointer. + @param BufferSize Size of data in the Buffer + @param DestAddr Address of the destination mac address buffer. + @param SrcAddr Address of the source mac address buffer. + @param ProtocolPtr Address of the protocol type. + + @retval EFI_SUCCESS Successfully completed the undi call. + @retval Other Error return from undi call. + +**/ +EFI_STATUS +PxeFillHeader ( + SNP_DRIVER *Snp, + VOID *MacHeaderPtr, + UINTN HeaderSize, + VOID *Buffer, + UINTN BufferSize, + EFI_MAC_ADDRESS *DestAddr, + EFI_MAC_ADDRESS *SrcAddr, + UINT16 *ProtocolPtr + ) +{ + PXE_CPB_FILL_HEADER_FRAGMENTED *Cpb; + + Cpb = Snp->Cpb; + if (SrcAddr != NULL) { + CopyMem ( + (VOID *) Cpb->SrcAddr, + (VOID *) SrcAddr, + Snp->Mode.HwAddressSize + ); + } else { + CopyMem ( + (VOID *) Cpb->SrcAddr, + (VOID *) &(Snp->Mode.CurrentAddress), + Snp->Mode.HwAddressSize + ); + } + + CopyMem ( + (VOID *) Cpb->DestAddr, + (VOID *) DestAddr, + Snp->Mode.HwAddressSize + ); + + // + // we need to do the byte swapping + // + Cpb->Protocol = (UINT16) PXE_SWAP_UINT16 (*ProtocolPtr); + + Cpb->PacketLen = (UINT32) (BufferSize); + Cpb->MediaHeaderLen = (UINT16) HeaderSize; + + Cpb->FragCnt = 2; + Cpb->reserved = 0; + + Cpb->FragDesc[0].FragAddr = (UINT64)(UINTN) MacHeaderPtr; + Cpb->FragDesc[0].FragLen = (UINT32) HeaderSize; + Cpb->FragDesc[1].FragAddr = (UINT64)(UINTN) Buffer; + Cpb->FragDesc[1].FragLen = (UINT32) BufferSize; + + Cpb->FragDesc[0].reserved = Cpb->FragDesc[1].reserved = 0; + + Snp->Cdb.OpCode = PXE_OPCODE_FILL_HEADER; + Snp->Cdb.OpFlags = PXE_OPFLAGS_FILL_HEADER_FRAGMENTED; + + Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED; + Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED; + + Snp->Cdb.CPBsize = (UINT16) sizeof (PXE_CPB_FILL_HEADER_FRAGMENTED); + Snp->Cdb.CPBaddr = (UINT64)(UINTN) Cpb; + + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nSnp->undi.fill_header() ")); + + (*Snp->IssueUndi32Command) ((UINT64) (UINTN) &Snp->Cdb); + + switch (Snp->Cdb.StatCode) { + case PXE_STATCODE_SUCCESS: + return EFI_SUCCESS; + + case PXE_STATCODE_INVALID_PARAMETER: + DEBUG ( + (EFI_D_ERROR, + "\nSnp->undi.fill_header() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_INVALID_PARAMETER; + + default: + DEBUG ( + (EFI_D_ERROR, + "\nSnp->undi.fill_header() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + + return EFI_DEVICE_ERROR; + } +} + + +/** + This routine calls undi to transmit the given data buffer + + @param Snp pointer to SNP driver structure + @param Buffer data buffer pointer + @param BufferSize Size of data in the Buffer + + @retval EFI_SUCCESS if successfully completed the undi call + @retval Other error return from undi call. + +**/ +EFI_STATUS +PxeTransmit ( + SNP_DRIVER *Snp, + VOID *Buffer, + UINTN BufferSize + ) +{ + PXE_CPB_TRANSMIT *Cpb; + EFI_STATUS Status; + + Cpb = Snp->Cpb; + Cpb->FrameAddr = (UINT64) (UINTN) Buffer; + Cpb->DataLen = (UINT32) BufferSize; + + Cpb->MediaheaderLen = 0; + Cpb->reserved = 0; + + Snp->Cdb.OpFlags = PXE_OPFLAGS_TRANSMIT_WHOLE; + + Snp->Cdb.CPBsize = (UINT16) sizeof (PXE_CPB_TRANSMIT); + Snp->Cdb.CPBaddr = (UINT64)(UINTN) Cpb; + + Snp->Cdb.OpCode = PXE_OPCODE_TRANSMIT; + Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED; + Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED; + + Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + Snp->Cdb.IFnum = Snp->IfNum; + Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Issue UNDI command and check result. + // + DEBUG ((EFI_D_NET, "\nSnp->undi.transmit() ")); + DEBUG ((EFI_D_NET, "\nSnp->Cdb.OpCode == %x", Snp->Cdb.OpCode)); + DEBUG ((EFI_D_NET, "\nSnp->Cdb.CPBaddr == %LX", Snp->Cdb.CPBaddr)); + DEBUG ((EFI_D_NET, "\nSnp->Cdb.DBaddr == %LX", Snp->Cdb.DBaddr)); + DEBUG ((EFI_D_NET, "\nCpb->FrameAddr == %LX\n", Cpb->FrameAddr)); + + (*Snp->IssueUndi32Command) ((UINT64) (UINTN) &Snp->Cdb); + + DEBUG ((EFI_D_NET, "\nexit Snp->undi.transmit() ")); + + // + // we will unmap the buffers in get_status call, not here + // + switch (Snp->Cdb.StatCode) { + case PXE_STATCODE_SUCCESS: + return EFI_SUCCESS; + + case PXE_STATCODE_BUFFER_FULL: + case PXE_STATCODE_QUEUE_FULL: + case PXE_STATCODE_BUSY: + Status = EFI_NOT_READY; + DEBUG ( + (EFI_D_NET, + "\nSnp->undi.transmit() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + break; + + default: + DEBUG ( + (EFI_D_ERROR, + "\nSnp->undi.transmit() %xh:%xh\n", + Snp->Cdb.StatFlags, + Snp->Cdb.StatCode) + ); + Status = EFI_DEVICE_ERROR; + } + + return Status; +} + +/** + Places a packet in the transmit queue of a network interface. + + This function places the packet specified by Header and Buffer on the transmit + queue. If HeaderSize is nonzero and HeaderSize is not equal to + This->Mode->MediaHeaderSize, then EFI_INVALID_PARAMETER will be returned. If + BufferSize is less than This->Mode->MediaHeaderSize, then EFI_BUFFER_TOO_SMALL + will be returned. If Buffer is NULL, then EFI_INVALID_PARAMETER will be + returned. If HeaderSize is nonzero and DestAddr or Protocol is NULL, then + EFI_INVALID_PARAMETER will be returned. If the transmit engine of the network + interface is busy, then EFI_NOT_READY will be returned. If this packet can be + accepted by the transmit engine of the network interface, the packet contents + specified by Buffer will be placed on the transmit queue of the network + interface, and EFI_SUCCESS will be returned. GetStatus() can be used to + determine when the packet has actually been transmitted. The contents of the + Buffer must not be modified until the packet has actually been transmitted. + The Transmit() function performs nonblocking I/O. A caller who wants to perform + blocking I/O, should call Transmit(), and then GetStatus() until the + transmitted buffer shows up in the recycled transmit buffer. + If the driver has not been initialized, EFI_DEVICE_ERROR will be returned. + + @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance. + @param HeaderSize The size, in bytes, of the media header to be filled in by the + Transmit() function. If HeaderSize is nonzero, then it must + be equal to This->Mode->MediaHeaderSize and the DestAddr and + Protocol parameters must not be NULL. + @param BufferSize The size, in bytes, of the entire packet (media header and + data) to be transmitted through the network interface. + @param Buffer A pointer to the packet (media header followed by data) to be + transmitted. This parameter cannot be NULL. If HeaderSize is + zero, then the media header in Buffer must already be filled + in by the caller. If HeaderSize is nonzero, then the media + header will be filled in by the Transmit() function. + @param SrcAddr The source HW MAC address. If HeaderSize is zero, then this + parameter is ignored. If HeaderSize is nonzero and SrcAddr + is NULL, then This->Mode->CurrentAddress is used for the + source HW MAC address. + @param DestAddr The destination HW MAC address. If HeaderSize is zero, then + this parameter is ignored. + @param Protocol The type of header to build. If HeaderSize is zero, then this + parameter is ignored. See RFC 1700, section "Ether Types," + for examples. + + @retval EFI_SUCCESS The packet was placed on the transmit queue. + @retval EFI_NOT_STARTED The network interface has not been started. + @retval EFI_NOT_READY The network interface is too busy to accept this + transmit request. + @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small. + @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported + value. + @retval EFI_DEVICE_ERROR The command could not be sent to the network interface. + @retval EFI_UNSUPPORTED This function is not supported by the network interface. + +**/ +EFI_STATUS +EFIAPI +SnpUndi32Transmit ( + IN EFI_SIMPLE_NETWORK_PROTOCOL *This, + IN UINTN HeaderSize, + IN UINTN BufferSize, + IN VOID *Buffer, + IN EFI_MAC_ADDRESS *SrcAddr, OPTIONAL + IN EFI_MAC_ADDRESS *DestAddr, OPTIONAL + IN UINT16 *Protocol OPTIONAL + ) +{ + SNP_DRIVER *Snp; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (Snp == NULL) { + return EFI_DEVICE_ERROR; + } + + switch (Snp->Mode.State) { + case EfiSimpleNetworkInitialized: + break; + + case EfiSimpleNetworkStopped: + Status = EFI_NOT_STARTED; + goto ON_EXIT; + + default: + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + if (Buffer == NULL) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + if (BufferSize < Snp->Mode.MediaHeaderSize) { + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + + // + // if the HeaderSize is non-zero, we need to fill up the header and for that + // we need the destination address and the protocol + // + if (HeaderSize != 0) { + if (HeaderSize != Snp->Mode.MediaHeaderSize || DestAddr == 0 || Protocol == 0) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + Status = PxeFillHeader ( + Snp, + Buffer, + HeaderSize, + (UINT8 *) Buffer + HeaderSize, + BufferSize - HeaderSize, + DestAddr, + SrcAddr, + Protocol + ); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + Status = PxeTransmit (Snp, Buffer, BufferSize); + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + + return Status; +} diff --git a/NetworkPkg/SnpDxe/WaitForPacket.c b/NetworkPkg/SnpDxe/WaitForPacket.c new file mode 100644 index 000000000..866a1e8c0 --- /dev/null +++ b/NetworkPkg/SnpDxe/WaitForPacket.c @@ -0,0 +1,86 @@ +/** @file + Event handler to check for available packet. + +Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Snp.h" + + +/** + Nofication call back function for WaitForPacket event. + + @param Event EFI Event. + @param SnpPtr Pointer to SNP_DRIVER structure. + +**/ +VOID +EFIAPI +SnpWaitForPacketNotify ( + EFI_EVENT Event, + VOID *SnpPtr + ) +{ + PXE_DB_GET_STATUS PxeDbGetStatus; + + // + // Do nothing if either parameter is a NULL pointer. + // + if (Event == NULL || SnpPtr == NULL) { + return ; + } + // + // Do nothing if the SNP interface is not initialized. + // + switch (((SNP_DRIVER *) SnpPtr)->Mode.State) { + case EfiSimpleNetworkInitialized: + break; + + case EfiSimpleNetworkStopped: + case EfiSimpleNetworkStarted: + default: + return ; + } + // + // Fill in CDB for UNDI GetStatus(). + // + ((SNP_DRIVER *) SnpPtr)->Cdb.OpCode = PXE_OPCODE_GET_STATUS; + ((SNP_DRIVER *) SnpPtr)->Cdb.OpFlags = 0; + ((SNP_DRIVER *) SnpPtr)->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED; + ((SNP_DRIVER *) SnpPtr)->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED; + ((SNP_DRIVER *) SnpPtr)->Cdb.DBsize = (UINT16) (sizeof (UINT32) * 2); + ((SNP_DRIVER *) SnpPtr)->Cdb.DBaddr = (UINT64)(UINTN) (((SNP_DRIVER *) SnpPtr)->Db); + ((SNP_DRIVER *) SnpPtr)->Cdb.StatCode = PXE_STATCODE_INITIALIZE; + ((SNP_DRIVER *) SnpPtr)->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE; + ((SNP_DRIVER *) SnpPtr)->Cdb.IFnum = ((SNP_DRIVER *) SnpPtr)->IfNum; + ((SNP_DRIVER *) SnpPtr)->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST; + + // + // Clear contents of DB buffer. + // + ZeroMem (((SNP_DRIVER *) SnpPtr)->Db, sizeof (UINT32) * 2); + + // + // Issue UNDI command and check result. + // + (*((SNP_DRIVER *) SnpPtr)->IssueUndi32Command) ((UINT64)(UINTN) &((SNP_DRIVER *) SnpPtr)->Cdb); + + if (((SNP_DRIVER *) SnpPtr)->Cdb.StatCode != EFI_SUCCESS) { + return ; + } + // + // We might have a packet. Check the receive length and signal + // the event if the length is not zero. + // + CopyMem ( + &PxeDbGetStatus, + ((SNP_DRIVER *) SnpPtr)->Db, + sizeof (UINT32) * 2 + ); + + if (PxeDbGetStatus.RxFrameLen != 0) { + gBS->SignalEvent (Event); + } +} diff --git a/NetworkPkg/TcpDxe/ComponentName.c b/NetworkPkg/TcpDxe/ComponentName.c new file mode 100644 index 000000000..ddc7116e5 --- /dev/null +++ b/NetworkPkg/TcpDxe/ComponentName.c @@ -0,0 +1,522 @@ +/** @file + Implementation of protocols EFI_COMPONENT_NAME_PROTOCOL and + EFI_COMPONENT_NAME2_PROTOCOL. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TcpMain.h" + +// +// EFI Component Name Functions +// + +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This, and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language or DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +TcpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is being managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language, from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language or ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +TcpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +/// +/// EFI Component Name Protocol +/// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gTcpComponentName = { + TcpComponentNameGetDriverName, + TcpComponentNameGetControllerName, + "eng" +}; + +/// +/// EFI Component Name 2 Protocol +/// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gTcpComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) TcpComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) TcpComponentNameGetControllerName, + "en" +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mTcpDriverNameTable[] = { + { + "eng;en", + L"TCP Network Service Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gTcpControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This, and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language or DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +TcpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mTcpDriverNameTable, + DriverName, + (BOOLEAN) (This == &gTcpComponentName) + ); +} + +/** + Update the component name for the Tcp4 child handle. + + @param Tcp4[in] A pointer to the EFI_TCP4_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateTcp4Name ( + IN EFI_TCP4_PROTOCOL *Tcp4 + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[80]; + EFI_TCP4_CONFIG_DATA Tcp4ConfigData; + + if (Tcp4 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer as: + // TCPv4 (SrcPort=59, DestPort=60, ActiveFlag=TRUE) + // + ZeroMem (&Tcp4ConfigData, sizeof (Tcp4ConfigData)); + Status = Tcp4->GetModeData (Tcp4, NULL, &Tcp4ConfigData, NULL, NULL, NULL); + if (!EFI_ERROR (Status)) { + UnicodeSPrint (HandleName, sizeof (HandleName), + L"TCPv4 (SrcPort=%d, DestPort=%d, ActiveFlag=%s)", + Tcp4ConfigData.AccessPoint.StationPort, + Tcp4ConfigData.AccessPoint.RemotePort, + (Tcp4ConfigData.AccessPoint.ActiveFlag ? L"TRUE" : L"FALSE") + ); + } else if (Status == EFI_NOT_STARTED) { + UnicodeSPrint ( + HandleName, + sizeof (HandleName), + L"TCPv4 (Not started)" + ); + } else { + return Status; + } + + if (gTcpControllerNameTable != NULL) { + FreeUnicodeStringTable (gTcpControllerNameTable); + gTcpControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gTcpComponentName.SupportedLanguages, + &gTcpControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gTcpComponentName2.SupportedLanguages, + &gTcpControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Update the component name for the Tcp6 child handle. + + @param Tcp6[in] A pointer to the EFI_TCP6_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateTcp6Name ( + IN EFI_TCP6_PROTOCOL *Tcp6 + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[80]; + EFI_TCP6_CONFIG_DATA Tcp6ConfigData; + + if (Tcp6 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer. + // + ZeroMem (&Tcp6ConfigData, sizeof (Tcp6ConfigData)); + Status = Tcp6->GetModeData (Tcp6, NULL, &Tcp6ConfigData, NULL, NULL, NULL); + if (!EFI_ERROR (Status)) { + UnicodeSPrint (HandleName, sizeof (HandleName), + L"TCPv6(SrcPort=%d, DestPort=%d, ActiveFlag=%d)", + Tcp6ConfigData.AccessPoint.StationPort, + Tcp6ConfigData.AccessPoint.RemotePort, + Tcp6ConfigData.AccessPoint.ActiveFlag + ); + } else if (Status == EFI_NOT_STARTED) { + UnicodeSPrint (HandleName, sizeof (HandleName), L"TCPv6(Not started)"); + } else { + return Status; + } + + + if (gTcpControllerNameTable != NULL) { + FreeUnicodeStringTable (gTcpControllerNameTable); + gTcpControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gTcpComponentName.SupportedLanguages, + &gTcpControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gTcpComponentName2.SupportedLanguages, + &gTcpControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is being managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language, from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language or ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +TcpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_TCP4_PROTOCOL *Tcp4; + EFI_TCP6_PROTOCOL *Tcp6; + + // + // Only provide names for child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiIp6ProtocolGuid + ); + if (!EFI_ERROR (Status)) { + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiTcp6ProtocolGuid, + (VOID **)&Tcp6, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateTcp6Name (Tcp6); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Make sure this driver is currently managing ControllHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiIp4ProtocolGuid + ); + if (!EFI_ERROR (Status)) { + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiTcp4ProtocolGuid, + (VOID **)&Tcp4, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateTcp4Name (Tcp4); + if (EFI_ERROR (Status)) { + return Status; + } + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gTcpControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gTcpComponentName) + ); +} + diff --git a/NetworkPkg/TcpDxe/SockImpl.c b/NetworkPkg/TcpDxe/SockImpl.c new file mode 100644 index 000000000..f5e01771e --- /dev/null +++ b/NetworkPkg/TcpDxe/SockImpl.c @@ -0,0 +1,1233 @@ +/** @file + Implementation of the Socket. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "SockImpl.h" + +/** + Get the first buffer block in the specific socket buffer. + + @param[in] Sockbuf Pointer to the socket buffer. + + @return Pointer to the first buffer in the queue. NULL if the queue is empty. + +**/ +NET_BUF * +SockBufFirst ( + IN SOCK_BUFFER *Sockbuf + ) +{ + LIST_ENTRY *NetbufList; + + NetbufList = &(Sockbuf->DataQueue->BufList); + + if (IsListEmpty (NetbufList)) { + return NULL; + } + + return NET_LIST_HEAD (NetbufList, NET_BUF, List); +} + +/** + Get the next buffer block in the specific socket buffer. + + @param[in] Sockbuf Pointer to the socket buffer. + @param[in] SockEntry Pointer to the buffer block prior to the required one. + + @return Pointer to the buffer block next to SockEntry. NULL if SockEntry is + the tail or head entry. + +**/ +NET_BUF * +SockBufNext ( + IN SOCK_BUFFER *Sockbuf, + IN NET_BUF *SockEntry + ) +{ + LIST_ENTRY *NetbufList; + + NetbufList = &(Sockbuf->DataQueue->BufList); + + if ((SockEntry->List.ForwardLink == NetbufList) || + (SockEntry->List.BackLink == &SockEntry->List) || + (SockEntry->List.ForwardLink == &SockEntry->List) + ) { + + return NULL; + } + + return NET_LIST_USER_STRUCT (SockEntry->List.ForwardLink, NET_BUF, List); +} + +/** + User provided callback function for NetbufFromExt. + + @param[in] Event The Event this notify function registered to, ignored. + +**/ +VOID +EFIAPI +SockFreeFoo ( + IN EFI_EVENT Event + ) +{ + return; +} + +/** + Get the length of the data that can be retrieved from the socket + receive buffer. + + @param[in] SockBuffer Pointer to the socket receive buffer. + @param[out] IsUrg Pointer to a BOOLEAN variable. + If TRUE the data is OOB. + @param[in] BufLen The maximum length of the data buffer to + store the received data in the socket layer. + + @return The length of the data can be retreived. + +**/ +UINT32 +SockTcpDataToRcv ( + IN SOCK_BUFFER *SockBuffer, + OUT BOOLEAN *IsUrg, + IN UINT32 BufLen + ) +{ + NET_BUF *RcvBufEntry; + UINT32 DataLen; + TCP_RSV_DATA *TcpRsvData; + BOOLEAN Urg; + + ASSERT ((SockBuffer != NULL) && (IsUrg != NULL) && (BufLen > 0)); + + // + // Get the first socket receive buffer + // + RcvBufEntry = SockBufFirst (SockBuffer); + ASSERT (RcvBufEntry != NULL); + + TcpRsvData = (TCP_RSV_DATA *) RcvBufEntry->ProtoData; + + // + // Check whether the receive data is out of bound. If yes, calculate the maximum + // allowed length of the urgent data and output it. + // + *IsUrg = (BOOLEAN) ((TcpRsvData->UrgLen > 0) ? TRUE : FALSE); + + if (*IsUrg && (TcpRsvData->UrgLen < RcvBufEntry->TotalSize)) { + + DataLen = MIN (TcpRsvData->UrgLen, BufLen); + + if (DataLen < TcpRsvData->UrgLen) { + TcpRsvData->UrgLen = TcpRsvData->UrgLen - DataLen; + } else { + TcpRsvData->UrgLen = 0; + } + + return DataLen; + + } + + // + // Process the next socket receive buffer to get the maximum allowed length + // of the received data. + // + DataLen = RcvBufEntry->TotalSize; + + RcvBufEntry = SockBufNext (SockBuffer, RcvBufEntry); + + while ((BufLen > DataLen) && (RcvBufEntry != NULL)) { + + TcpRsvData = (TCP_RSV_DATA *) RcvBufEntry->ProtoData; + + Urg = (BOOLEAN) ((TcpRsvData->UrgLen > 0) ? TRUE : FALSE); + + if (*IsUrg != Urg) { + break; + } + + if (*IsUrg && TcpRsvData->UrgLen < RcvBufEntry->TotalSize) { + + if (TcpRsvData->UrgLen + DataLen < BufLen) { + TcpRsvData->UrgLen = 0; + } else { + TcpRsvData->UrgLen = TcpRsvData->UrgLen - (BufLen - DataLen); + } + + return MIN (TcpRsvData->UrgLen + DataLen, BufLen); + + } + + DataLen += RcvBufEntry->TotalSize; + + RcvBufEntry = SockBufNext (SockBuffer, RcvBufEntry); + } + + DataLen = MIN (BufLen, DataLen); + return DataLen; +} + +/** + Copy data from socket buffer to an application provided receive buffer. + + @param[in] Sock Pointer to the socket. + @param[in] TcpRxData Pointer to the application provided receive buffer. + @param[in] RcvdBytes The maximum length of the data can be copied. + @param[in] IsUrg If TRUE the data is Out of Bound, FALSE the data is normal. + +**/ +VOID +SockSetTcpRxData ( + IN SOCKET *Sock, + IN VOID *TcpRxData, + IN UINT32 RcvdBytes, + IN BOOLEAN IsUrg + ) +{ + UINT32 Index; + UINT32 CopyBytes; + UINT32 OffSet; + EFI_TCP4_RECEIVE_DATA *RxData; + EFI_TCP4_FRAGMENT_DATA *Fragment; + + RxData = (EFI_TCP4_RECEIVE_DATA *) TcpRxData; + + OffSet = 0; + + ASSERT (RxData->DataLength >= RcvdBytes); + + RxData->DataLength = RcvdBytes; + RxData->UrgentFlag = IsUrg; + + // + // Copy the CopyBytes data from socket receive buffer to RxData. + // + for (Index = 0; (Index < RxData->FragmentCount) && (RcvdBytes > 0); Index++) { + + Fragment = &RxData->FragmentTable[Index]; + CopyBytes = MIN ((UINT32) (Fragment->FragmentLength), RcvdBytes); + + NetbufQueCopy ( + Sock->RcvBuffer.DataQueue, + OffSet, + CopyBytes, + Fragment->FragmentBuffer + ); + + Fragment->FragmentLength = CopyBytes; + RcvdBytes -= CopyBytes; + OffSet += CopyBytes; + } +} + +/** + Process the send token. + + @param[in, out] Sock Pointer to the socket. + +**/ +VOID +SockProcessSndToken ( + IN OUT SOCKET *Sock + ) +{ + UINT32 FreeSpace; + SOCK_TOKEN *SockToken; + UINT32 DataLen; + SOCK_IO_TOKEN *SndToken; + EFI_TCP4_TRANSMIT_DATA *TxData; + EFI_STATUS Status; + + ASSERT ((Sock != NULL) && (SockStream == Sock->Type)); + + FreeSpace = SockGetFreeSpace (Sock, SOCK_SND_BUF); + + // + // to determine if process a send token using + // socket layer flow control policy + // + while ((FreeSpace >= Sock->SndBuffer.LowWater) && !IsListEmpty (&Sock->SndTokenList)) { + + SockToken = NET_LIST_HEAD ( + &(Sock->SndTokenList), + SOCK_TOKEN, + TokenList + ); + + // + // process this token + // + RemoveEntryList (&(SockToken->TokenList)); + InsertTailList ( + &(Sock->ProcessingSndTokenList), + &(SockToken->TokenList) + ); + + // + // Proceess it in the light of SockType + // + SndToken = (SOCK_IO_TOKEN *) SockToken->Token; + TxData = SndToken->Packet.TxData; + + DataLen = TxData->DataLength; + Status = SockProcessTcpSndData (Sock, TxData); + + if (EFI_ERROR (Status)) { + goto OnError; + } + + if (DataLen >= FreeSpace) { + FreeSpace = 0; + + } else { + FreeSpace -= DataLen; + + } + } + + return; + +OnError: + + RemoveEntryList (&SockToken->TokenList); + SIGNAL_TOKEN (SockToken->Token, Status); + FreePool (SockToken); +} + +/** + Get received data from the socket layer to the receive token. + + @param[in, out] Sock Pointer to the socket. + @param[in, out] RcvToken Pointer to the application provided receive token. + + @return The length of data received in this token. + +**/ +UINT32 +SockProcessRcvToken ( + IN OUT SOCKET *Sock, + IN OUT SOCK_IO_TOKEN *RcvToken + ) +{ + UINT32 TokenRcvdBytes; + EFI_TCP4_RECEIVE_DATA *RxData; + BOOLEAN IsUrg; + + ASSERT (Sock != NULL); + + ASSERT (SockStream == Sock->Type); + + RxData = RcvToken->Packet.RxData; + + TokenRcvdBytes = SockTcpDataToRcv ( + &Sock->RcvBuffer, + &IsUrg, + RxData->DataLength + ); + + // + // Copy data from RcvBuffer of socket to user + // provided RxData and set the fields in TCP RxData + // + SockSetTcpRxData (Sock, RxData, TokenRcvdBytes, IsUrg); + + NetbufQueTrim (Sock->RcvBuffer.DataQueue, TokenRcvdBytes); + SIGNAL_TOKEN (&(RcvToken->Token), EFI_SUCCESS); + + return TokenRcvdBytes; +} + +/** + Process the TCP send data, buffer the tcp txdata, and append + the buffer to socket send buffer, then try to send it. + + @param[in] Sock Pointer to the socket. + @param[in] TcpTxData Pointer to the application provided send buffer. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES Failed due to resource limits. + +**/ +EFI_STATUS +SockProcessTcpSndData ( + IN SOCKET *Sock, + IN VOID *TcpTxData + ) +{ + NET_BUF *SndData; + EFI_STATUS Status; + EFI_TCP4_TRANSMIT_DATA *TxData; + + TxData = (EFI_TCP4_TRANSMIT_DATA *) TcpTxData; + + // + // transform this TxData into a NET_BUFFER + // and insert it into Sock->SndBuffer + // + SndData = NetbufFromExt ( + (NET_FRAGMENT *) TxData->FragmentTable, + TxData->FragmentCount, + 0, + 0, + SockFreeFoo, + NULL + ); + + if (NULL == SndData) { + DEBUG ( + (EFI_D_ERROR, + "SockKProcessSndData: Failed to call NetBufferFromExt\n") + ); + + return EFI_OUT_OF_RESOURCES; + } + + NetbufQueAppend (Sock->SndBuffer.DataQueue, SndData); + + // + // notify the low layer protocol to handle this send token + // + if (TxData->Urgent) { + Status = Sock->ProtoHandler (Sock, SOCK_SNDURG, NULL); + + if (EFI_ERROR (Status)) { + return Status; + } + } + + if (TxData->Push) { + Status = Sock->ProtoHandler (Sock, SOCK_SNDPUSH, NULL); + + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // low layer protocol should really handle the sending + // process when catching SOCK_SND request + // + Status = Sock->ProtoHandler (Sock, SOCK_SND, NULL); + + if (EFI_ERROR (Status)) { + return Status; + } + + return EFI_SUCCESS; +} + +/** + Flush the tokens in the specific token list. + + @param[in] Sock Pointer to the socket. + @param[in, out] PendingTokenList Pointer to the token list to be flushed. + +**/ +VOID +SockFlushPendingToken ( + IN SOCKET *Sock, + IN OUT LIST_ENTRY *PendingTokenList + ) +{ + SOCK_TOKEN *SockToken; + SOCK_COMPLETION_TOKEN *Token; + + ASSERT ((Sock != NULL) && (PendingTokenList != NULL)); + + while (!IsListEmpty (PendingTokenList)) { + SockToken = NET_LIST_HEAD ( + PendingTokenList, + SOCK_TOKEN, + TokenList + ); + + Token = SockToken->Token; + SIGNAL_TOKEN (Token, Sock->SockError); + + RemoveEntryList (&(SockToken->TokenList)); + FreePool (SockToken); + } +} + +/** + Wake up the connection token while the connection is successfully established, + then try to process any pending send token. + + @param[in, out] Sock Pointer to the socket. + +**/ +VOID +SockWakeConnToken ( + IN OUT SOCKET *Sock + ) +{ + ASSERT (Sock->ConnectionToken != NULL); + + SIGNAL_TOKEN (Sock->ConnectionToken, EFI_SUCCESS); + Sock->ConnectionToken = NULL; + + // + // check to see if some pending send token existed? + // + SockProcessSndToken (Sock); +} + +/** + Wake up the listen token while the connection is established successfully. + + @param[in, out] Sock Pointer to the socket. + +**/ +VOID +SockWakeListenToken ( + IN OUT SOCKET *Sock + ) +{ + SOCKET *Parent; + SOCK_TOKEN *SockToken; + EFI_TCP4_LISTEN_TOKEN *ListenToken; + + Parent = Sock->Parent; + + ASSERT ((Parent != NULL) && SOCK_IS_LISTENING (Parent) && SOCK_IS_CONNECTED (Sock)); + + if (!IsListEmpty (&Parent->ListenTokenList)) { + SockToken = NET_LIST_HEAD ( + &Parent->ListenTokenList, + SOCK_TOKEN, + TokenList + ); + + ListenToken = (EFI_TCP4_LISTEN_TOKEN *) SockToken->Token; + ListenToken->NewChildHandle = Sock->SockHandle; + + SIGNAL_TOKEN (&(ListenToken->CompletionToken), EFI_SUCCESS); + + RemoveEntryList (&SockToken->TokenList); + FreePool (SockToken); + + RemoveEntryList (&Sock->ConnectionList); + + Parent->ConnCnt--; + DEBUG ( + (EFI_D_NET, + "SockWakeListenToken: accept a socket, now conncnt is %d", + Parent->ConnCnt) + ); + + Sock->Parent = NULL; + } +} + +/** + Wake up the receive token while some data is received. + + @param[in, out] Sock Pointer to the socket. + +**/ +VOID +SockWakeRcvToken ( + IN OUT SOCKET *Sock + ) +{ + UINT32 RcvdBytes; + UINT32 TokenRcvdBytes; + SOCK_TOKEN *SockToken; + SOCK_IO_TOKEN *RcvToken; + + ASSERT (Sock->RcvBuffer.DataQueue != NULL); + + RcvdBytes = (Sock->RcvBuffer.DataQueue)->BufSize; + + ASSERT (RcvdBytes > 0); + + while (RcvdBytes > 0 && !IsListEmpty (&Sock->RcvTokenList)) { + + SockToken = NET_LIST_HEAD ( + &Sock->RcvTokenList, + SOCK_TOKEN, + TokenList + ); + + RcvToken = (SOCK_IO_TOKEN *) SockToken->Token; + TokenRcvdBytes = SockProcessRcvToken (Sock, RcvToken); + + if (0 == TokenRcvdBytes) { + return ; + } + + RemoveEntryList (&(SockToken->TokenList)); + FreePool (SockToken); + RcvdBytes -= TokenRcvdBytes; + } +} + +/** + Cancel the tokens in the specific token list. + + @param[in] Token Pointer to the Token. If NULL, all tokens + in SpecifiedTokenList will be canceled. + @param[in, out] SpecifiedTokenList Pointer to the token list to be checked. + + @retval EFI_SUCCESS Cancel the tokens in the specific token listsuccessfully. + @retval EFI_NOT_FOUND The Token is not found in SpecifiedTokenList. + +**/ +EFI_STATUS +SockCancelToken ( + IN SOCK_COMPLETION_TOKEN *Token, + IN OUT LIST_ENTRY *SpecifiedTokenList + ) +{ + EFI_STATUS Status; + LIST_ENTRY *Entry; + SOCK_TOKEN *SockToken; + + Status = EFI_SUCCESS; + Entry = NULL; + SockToken = NULL; + + if (IsListEmpty (SpecifiedTokenList) && Token != NULL) { + return EFI_NOT_FOUND; + } + + // + // Iterate through the SpecifiedTokenList. + // + Entry = SpecifiedTokenList->ForwardLink; + while (Entry != SpecifiedTokenList) { + SockToken = NET_LIST_USER_STRUCT (Entry, SOCK_TOKEN, TokenList); + + if (Token == NULL) { + SIGNAL_TOKEN (SockToken->Token, EFI_ABORTED); + RemoveEntryList (&SockToken->TokenList); + FreePool (SockToken); + + Entry = SpecifiedTokenList->ForwardLink; + Status = EFI_SUCCESS; + } else { + if (Token == (VOID *) SockToken->Token) { + SIGNAL_TOKEN (Token, EFI_ABORTED); + RemoveEntryList (&(SockToken->TokenList)); + FreePool (SockToken); + + return EFI_SUCCESS; + } + + Status = EFI_NOT_FOUND; + + Entry = Entry->ForwardLink; + } + } + + ASSERT (IsListEmpty (SpecifiedTokenList) || Token != NULL); + + return Status; +} + +/** + Create a socket with initial data SockInitData. + + @param[in] SockInitData Pointer to the initial data of the socket. + + @return Pointer to the newly created socket, return NULL when an exception occurs. + +**/ +SOCKET * +SockCreate ( + IN SOCK_INIT_DATA *SockInitData + ) +{ + SOCKET *Sock; + SOCKET *Parent; + EFI_STATUS Status; + EFI_GUID *TcpProtocolGuid; + UINTN ProtocolLength; + + ASSERT ((SockInitData != NULL) && (SockInitData->ProtoHandler != NULL)); + ASSERT (SockInitData->Type == SockStream); + ASSERT ((SockInitData->ProtoData != NULL) && (SockInitData->DataSize <= PROTO_RESERVED_LEN)); + + if (SockInitData->IpVersion == IP_VERSION_4) { + TcpProtocolGuid = &gEfiTcp4ProtocolGuid; + ProtocolLength = sizeof (EFI_TCP4_PROTOCOL); + } else { + TcpProtocolGuid = &gEfiTcp6ProtocolGuid; + ProtocolLength = sizeof (EFI_TCP6_PROTOCOL); + } + + + Parent = SockInitData->Parent; + + if ((Parent != NULL) && (Parent->ConnCnt == Parent->BackLog)) { + DEBUG ( + (EFI_D_ERROR, + "SockCreate: Socket parent has reached its connection limit with %d ConnCnt and %d BackLog\n", + Parent->ConnCnt, + Parent->BackLog) + ); + + return NULL; + } + + Sock = AllocateZeroPool (sizeof (SOCKET)); + if (NULL == Sock) { + + DEBUG ((EFI_D_ERROR, "SockCreate: No resource to create a new socket\n")); + return NULL; + } + + InitializeListHead (&Sock->Link); + InitializeListHead (&Sock->ConnectionList); + InitializeListHead (&Sock->ListenTokenList); + InitializeListHead (&Sock->RcvTokenList); + InitializeListHead (&Sock->SndTokenList); + InitializeListHead (&Sock->ProcessingSndTokenList); + + EfiInitializeLock (&(Sock->Lock), TPL_CALLBACK); + + Sock->SndBuffer.DataQueue = NetbufQueAlloc (); + if (NULL == Sock->SndBuffer.DataQueue) { + DEBUG ( + (EFI_D_ERROR, + "SockCreate: No resource to allocate SndBuffer for new socket\n") + ); + + goto OnError; + } + + Sock->RcvBuffer.DataQueue = NetbufQueAlloc (); + if (NULL == Sock->RcvBuffer.DataQueue) { + DEBUG ( + (EFI_D_ERROR, + "SockCreate: No resource to allocate RcvBuffer for new socket\n") + ); + + goto OnError; + } + + Sock->Signature = SOCK_SIGNATURE; + + Sock->Parent = Parent; + Sock->BackLog = SockInitData->BackLog; + Sock->ProtoHandler = SockInitData->ProtoHandler; + Sock->SndBuffer.HighWater = SockInitData->SndBufferSize; + Sock->RcvBuffer.HighWater = SockInitData->RcvBufferSize; + Sock->Type = SockInitData->Type; + Sock->DriverBinding = SockInitData->DriverBinding; + Sock->State = SockInitData->State; + Sock->CreateCallback = SockInitData->CreateCallback; + Sock->DestroyCallback = SockInitData->DestroyCallback; + Sock->Context = SockInitData->Context; + + Sock->SockError = EFI_ABORTED; + Sock->SndBuffer.LowWater = SOCK_BUFF_LOW_WATER; + Sock->RcvBuffer.LowWater = SOCK_BUFF_LOW_WATER; + + Sock->IpVersion = SockInitData->IpVersion; + + // + // Install protocol on Sock->SockHandle + // + CopyMem (&Sock->NetProtocol, SockInitData->Protocol, ProtocolLength); + + // + // copy the protodata into socket + // + CopyMem (Sock->ProtoReserved, SockInitData->ProtoData, SockInitData->DataSize); + + Status = gBS->InstallMultipleProtocolInterfaces ( + &Sock->SockHandle, + TcpProtocolGuid, + &Sock->NetProtocol, + NULL + ); + + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "SockCreate: Install TCP protocol in socket failed with %r\n", + Status) + ); + + goto OnError; + } + + if (Parent != NULL) { + ASSERT (Parent->BackLog > 0); + ASSERT (SOCK_IS_LISTENING (Parent)); + + // + // need to add it into Parent->ConnectionList + // if the Parent->ConnCnt < Parent->BackLog + // + Parent->ConnCnt++; + + DEBUG ( + (EFI_D_NET, + "SockCreate: Create a new socket and add to parent, now conncnt is %d\n", + Parent->ConnCnt) + ); + + InsertTailList (&Parent->ConnectionList, &Sock->ConnectionList); + } + + if (Sock->CreateCallback != NULL) { + Status = Sock->CreateCallback (Sock, Sock->Context); + if (EFI_ERROR (Status)) { + goto OnError; + } + } + + return Sock; + +OnError: + + if (Sock->SockHandle != NULL) { + gBS->UninstallMultipleProtocolInterfaces ( + Sock->SockHandle, + TcpProtocolGuid, + &Sock->NetProtocol, + NULL + ); + } + + if (NULL != Sock->SndBuffer.DataQueue) { + NetbufQueFree (Sock->SndBuffer.DataQueue); + } + + if (NULL != Sock->RcvBuffer.DataQueue) { + NetbufQueFree (Sock->RcvBuffer.DataQueue); + } + + FreePool (Sock); + + return NULL; +} + +/** + Destroy a socket. + + @param[in, out] Sock Pointer to the socket. + +**/ +VOID +SockDestroy ( + IN OUT SOCKET *Sock + ) +{ + ASSERT (SockStream == Sock->Type); + + // + // Flush the completion token buffered + // by sock and rcv, snd buffer + // + if (!SOCK_IS_UNCONFIGURED (Sock)) { + + SockConnFlush (Sock); + SockSetState (Sock, SO_CLOSED); + Sock->ConfigureState = SO_UNCONFIGURED; + + } + // + // Destroy the RcvBuffer Queue and SendBuffer Queue + // + NetbufQueFree (Sock->RcvBuffer.DataQueue); + NetbufQueFree (Sock->SndBuffer.DataQueue); + + // + // Remove it from parent connection list if needed + // + if (Sock->Parent != NULL) { + + RemoveEntryList (&(Sock->ConnectionList)); + (Sock->Parent->ConnCnt)--; + + DEBUG ( + (EFI_D_WARN, + "SockDestroy: Delete a unaccepted socket from parent now conncnt is %d\n", + Sock->Parent->ConnCnt) + ); + + Sock->Parent = NULL; + } + + FreePool (Sock); +} + +/** + Flush the sndBuffer and rcvBuffer of socket. + + @param[in, out] Sock Pointer to the socket. + +**/ +VOID +SockConnFlush ( + IN OUT SOCKET *Sock + ) +{ + SOCKET *Child; + + ASSERT (Sock != NULL); + + // + // Clear the flag in this socket + // + Sock->Flag = 0; + + // + // Flush the SndBuffer and RcvBuffer of Sock + // + NetbufQueFlush (Sock->SndBuffer.DataQueue); + NetbufQueFlush (Sock->RcvBuffer.DataQueue); + + // + // Signal the pending token + // + if (Sock->ConnectionToken != NULL) { + SIGNAL_TOKEN (Sock->ConnectionToken, Sock->SockError); + Sock->ConnectionToken = NULL; + } + + if (Sock->CloseToken != NULL) { + SIGNAL_TOKEN (Sock->CloseToken, Sock->SockError); + Sock->CloseToken = NULL; + } + + SockFlushPendingToken (Sock, &(Sock->ListenTokenList)); + SockFlushPendingToken (Sock, &(Sock->RcvTokenList)); + SockFlushPendingToken (Sock, &(Sock->SndTokenList)); + SockFlushPendingToken (Sock, &(Sock->ProcessingSndTokenList)); + + // + // Destroy the pending connection, if it is a listening socket + // + if (SOCK_IS_LISTENING (Sock)) { + while (!IsListEmpty (&Sock->ConnectionList)) { + Child = NET_LIST_HEAD ( + &Sock->ConnectionList, + SOCKET, + ConnectionList + ); + + SockDestroyChild (Child); + } + + Sock->ConnCnt = 0; + } + +} + +/** + Set the state of the socket. + + @param[in, out] Sock Pointer to the socket. + @param[in] State The new socket state to be set. + +**/ +VOID +SockSetState ( + IN OUT SOCKET *Sock, + IN UINT8 State + ) +{ + Sock->State = State; +} + +/** + Clone a new socket, including its associated protocol control block. + + @param[in] Sock Pointer to the socket to be cloned. + + @return Pointer to the newly cloned socket. If NULL, an error condition occurred. + +**/ +SOCKET * +SockClone ( + IN SOCKET *Sock + ) +{ + SOCKET *ClonedSock; + SOCK_INIT_DATA InitData; + + InitData.BackLog = Sock->BackLog; + InitData.Parent = Sock; + InitData.State = Sock->State; + InitData.ProtoHandler = Sock->ProtoHandler; + InitData.Type = Sock->Type; + InitData.RcvBufferSize = Sock->RcvBuffer.HighWater; + InitData.SndBufferSize = Sock->SndBuffer.HighWater; + InitData.DriverBinding = Sock->DriverBinding; + InitData.IpVersion = Sock->IpVersion; + InitData.Protocol = &(Sock->NetProtocol); + InitData.CreateCallback = Sock->CreateCallback; + InitData.DestroyCallback = Sock->DestroyCallback; + InitData.Context = Sock->Context; + InitData.ProtoData = Sock->ProtoReserved; + InitData.DataSize = sizeof (Sock->ProtoReserved); + + ClonedSock = SockCreate (&InitData); + + if (NULL == ClonedSock) { + DEBUG ((EFI_D_ERROR, "SockClone: no resource to create a cloned sock\n")); + return NULL; + } + + SockSetState (ClonedSock, SO_CONNECTING); + ClonedSock->ConfigureState = Sock->ConfigureState; + + return ClonedSock; +} + +/** + Called by the low layer protocol to indicate the socket a connection is + established. + + This function just changes the socket's state to SO_CONNECTED + and signals the token used for connection establishment. + + @param[in, out] Sock Pointer to the socket associated with the + established connection. + +**/ +VOID +SockConnEstablished ( + IN OUT SOCKET *Sock + ) +{ + + ASSERT (SO_CONNECTING == Sock->State); + + SockSetState (Sock, SO_CONNECTED); + + if (NULL == Sock->Parent) { + SockWakeConnToken (Sock); + } else { + SockWakeListenToken (Sock); + } + +} + +/** + Called by the low layer protocol to indicate the connection is closed. + + This function flushes the socket, sets the state to SO_CLOSED, and signals + the close token. + + @param[in, out] Sock Pointer to the socket associated with the closed + connection. + +**/ +VOID +SockConnClosed ( + IN OUT SOCKET *Sock + ) +{ + if (Sock->CloseToken != NULL) { + SIGNAL_TOKEN (Sock->CloseToken, EFI_SUCCESS); + Sock->CloseToken = NULL; + } + + SockConnFlush (Sock); + SockSetState (Sock, SO_CLOSED); + + if (Sock->Parent != NULL) { + SockDestroyChild (Sock); + } + +} + +/** + Called by low layer protocol to indicate that some data was sent or processed. + + This function trims the sent data in the socket send buffer, and signals the data + token if proper. + + @param[in, out] Sock Pointer to the socket. + @param[in] Count The length of the data processed or sent, in bytes. + +**/ +VOID +SockDataSent ( + IN OUT SOCKET *Sock, + IN UINT32 Count + ) +{ + SOCK_TOKEN *SockToken; + SOCK_COMPLETION_TOKEN *SndToken; + + ASSERT (!IsListEmpty (&Sock->ProcessingSndTokenList)); + ASSERT (Count <= (Sock->SndBuffer.DataQueue)->BufSize); + + NetbufQueTrim (Sock->SndBuffer.DataQueue, Count); + + // + // To check if we can signal some snd token in this socket + // + while (Count > 0) { + SockToken = NET_LIST_HEAD ( + &(Sock->ProcessingSndTokenList), + SOCK_TOKEN, + TokenList + ); + + SndToken = SockToken->Token; + + if (SockToken->RemainDataLen <= Count) { + + RemoveEntryList (&(SockToken->TokenList)); + SIGNAL_TOKEN (SndToken, EFI_SUCCESS); + Count -= SockToken->RemainDataLen; + FreePool (SockToken); + } else { + + SockToken->RemainDataLen -= Count; + Count = 0; + } + } + + // + // to judge if we can process some send token in + // Sock->SndTokenList, if so process those send token + // + SockProcessSndToken (Sock); +} + +/** + Called by the low layer protocol to copy some data in the socket send + buffer starting from the specific offset to a buffer provided by + the caller. + + @param[in] Sock Pointer to the socket. + @param[in] Offset The start point of the data to be copied. + @param[in] Len The length of the data to be copied. + @param[out] Dest Pointer to the destination to copy the data. + + @return The data size copied. + +**/ +UINT32 +SockGetDataToSend ( + IN SOCKET *Sock, + IN UINT32 Offset, + IN UINT32 Len, + OUT UINT8 *Dest + ) +{ + ASSERT ((Sock != NULL) && SockStream == Sock->Type); + + return NetbufQueCopy ( + Sock->SndBuffer.DataQueue, + Offset, + Len, + Dest + ); +} + +/** + Called by the low layer protocol to deliver received data to socket layer. + + This function will append the data to the socket receive buffer, set the + urgent data length, and then check if any receive token can be signaled. + + @param[in, out] Sock Pointer to the socket. + @param[in, out] NetBuffer Pointer to the buffer that contains the received data. + @param[in] UrgLen The length of the urgent data in the received data. + +**/ +VOID +SockDataRcvd ( + IN OUT SOCKET *Sock, + IN OUT NET_BUF *NetBuffer, + IN UINT32 UrgLen + ) +{ + ASSERT ((Sock != NULL) && (Sock->RcvBuffer.DataQueue != NULL) && + UrgLen <= NetBuffer->TotalSize); + + NET_GET_REF (NetBuffer); + + ((TCP_RSV_DATA *) (NetBuffer->ProtoData))->UrgLen = UrgLen; + + NetbufQueAppend (Sock->RcvBuffer.DataQueue, NetBuffer); + + SockWakeRcvToken (Sock); +} + +/** + Get the length of the free space of the specific socket buffer. + + @param[in] Sock Pointer to the socket. + @param[in] Which Flag to indicate which socket buffer to check: + either send buffer or receive buffer. + + @return The length of the free space, in bytes. + +**/ +UINT32 +SockGetFreeSpace ( + IN SOCKET *Sock, + IN UINT32 Which + ) +{ + UINT32 BufferCC; + SOCK_BUFFER *SockBuffer; + + ASSERT (Sock != NULL && ((SOCK_SND_BUF == Which) || (SOCK_RCV_BUF == Which))); + + if (SOCK_SND_BUF == Which) { + SockBuffer = &(Sock->SndBuffer); + } else { + SockBuffer = &(Sock->RcvBuffer); + } + + BufferCC = (SockBuffer->DataQueue)->BufSize; + + if (BufferCC >= SockBuffer->HighWater) { + + return 0; + } + + return SockBuffer->HighWater - BufferCC; +} + +/** + Called by the low layer protocol to indicate that there will be no more data + from the communication peer. + + This function sets the socket's state to SO_NO_MORE_DATA and signals all queued + IO tokens with the error status EFI_CONNECTION_FIN. + + @param[in, out] Sock Pointer to the socket. + +**/ +VOID +SockNoMoreData ( + IN OUT SOCKET *Sock + ) +{ + EFI_STATUS Err; + + SOCK_NO_MORE_DATA (Sock); + + if (!IsListEmpty (&Sock->RcvTokenList)) { + + ASSERT (0 == GET_RCV_DATASIZE (Sock)); + + Err = Sock->SockError; + + SOCK_ERROR (Sock, EFI_CONNECTION_FIN); + + SockFlushPendingToken (Sock, &Sock->RcvTokenList); + + SOCK_ERROR (Sock, Err); + + } +} + diff --git a/NetworkPkg/TcpDxe/SockImpl.h b/NetworkPkg/TcpDxe/SockImpl.h new file mode 100644 index 000000000..f255b2fb5 --- /dev/null +++ b/NetworkPkg/TcpDxe/SockImpl.h @@ -0,0 +1,115 @@ +/** @file + The function declaration that provided for Socket Interface. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _SOCK_IMPL_H_ +#define _SOCK_IMPL_H_ + +#include "Socket.h" +#include "TcpMain.h" + +/** + Signal a event with the given status. + + @param[in] Token The token's event is to be signaled. + @param[in] TokenStatus The status to be sent with the event. + +**/ +#define SIGNAL_TOKEN(Token, TokenStatus) \ + do { \ + (Token)->Status = (TokenStatus); \ + gBS->SignalEvent ((Token)->Event); \ + } while (0) + +#define SOCK_HEADER_SPACE (60 + 60 + 72) + +/** + Process the TCP send data, buffer the tcp txdata and append + the buffer to socket send buffer, then try to send it. + + @param[in] Sock Pointer to the socket. + @param[in] TcpTxData Pointer to the application provided send buffer. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES Failed due to resource limits. + +**/ +EFI_STATUS +SockProcessTcpSndData ( + IN SOCKET *Sock, + IN VOID *TcpTxData + ); + +/** + Get received data from the socket layer to the receive token. + + @param[in, out] Sock Pointer to the socket. + @param[in, out] RcvToken Pointer to the application provided receive token. + + @return The length of data received in this token. + +**/ +UINT32 +SockProcessRcvToken ( + IN OUT SOCKET *Sock, + IN OUT SOCK_IO_TOKEN *RcvToken + ); + +/** + Flush the sndBuffer and rcvBuffer of socket. + + @param[in, out] Sock Pointer to the socket. + +**/ +VOID +SockConnFlush ( + IN OUT SOCKET *Sock + ); + +/** + Cancel the tokens in the specific token list. + + @param[in] Token Pointer to the Token. If NULL, all tokens + in SpecifiedTokenList will be canceled. + @param[in, out] SpecifiedTokenList Pointer to the token list to be checked. + + @retval EFI_SUCCESS Cancel the tokens in the specific token listsuccessfully. + @retval EFI_NOT_FOUND The Token is not found in SpecifiedTokenList. + +**/ +EFI_STATUS +SockCancelToken ( + IN SOCK_COMPLETION_TOKEN *Token, + IN OUT LIST_ENTRY *SpecifiedTokenList + ); + +/** + Create a socket with initial data SockInitData. + + @param[in] SockInitData Pointer to the initial data of the socket. + + @return Pointer to the newly created socket, return NULL when exception occured. + +**/ +SOCKET * +SockCreate ( + IN SOCK_INIT_DATA *SockInitData + ); + +/** + Destroy a socket. + + @param[in, out] Sock Pointer to the socket. + +**/ +VOID +SockDestroy ( + IN OUT SOCKET *Sock + ); + +#endif diff --git a/NetworkPkg/TcpDxe/SockInterface.c b/NetworkPkg/TcpDxe/SockInterface.c new file mode 100644 index 000000000..ed0a031d3 --- /dev/null +++ b/NetworkPkg/TcpDxe/SockInterface.c @@ -0,0 +1,1126 @@ +/** @file + Interface function of the Socket. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "SockImpl.h" + +/** + Check whether the Event is in the List. + + @param[in] List Pointer to the token list to be searched. + @param[in] Event The event to be checked. + + @retval TRUE The specific Event exists in the List. + @retval FALSE The specific Event is not in the List. + +**/ +BOOLEAN +SockTokenExistedInList ( + IN LIST_ENTRY *List, + IN EFI_EVENT Event + ) +{ + LIST_ENTRY *ListEntry; + SOCK_TOKEN *SockToken; + + NET_LIST_FOR_EACH (ListEntry, List) { + SockToken = NET_LIST_USER_STRUCT ( + ListEntry, + SOCK_TOKEN, + TokenList + ); + + if (Event == SockToken->Token->Event) { + return TRUE; + } + } + + return FALSE; +} + +/** + Call SockTokenExistedInList() to check whether the Event is + in the related socket's lists. + + @param[in] Sock Pointer to the instance's socket. + @param[in] Event The event to be checked. + + @retval TRUE The Event exists in related socket's lists. + @retval FALSE The Event is not in related socket's lists. + +**/ +BOOLEAN +SockTokenExisted ( + IN SOCKET *Sock, + IN EFI_EVENT Event + ) +{ + + if (SockTokenExistedInList (&Sock->SndTokenList, Event) || + SockTokenExistedInList (&Sock->ProcessingSndTokenList, Event) || + SockTokenExistedInList (&Sock->RcvTokenList, Event) || + SockTokenExistedInList (&Sock->ListenTokenList, Event) + ) { + + return TRUE; + } + + if ((Sock->ConnectionToken != NULL) && (Sock->ConnectionToken->Event == Event)) { + + return TRUE; + } + + if ((Sock->CloseToken != NULL) && (Sock->CloseToken->Event == Event)) { + return TRUE; + } + + return FALSE; +} + +/** + Buffer a token into the specific list of the socket Sock. + + @param[in] Sock Pointer to the instance's socket. + @param[in] List Pointer to the list to store the token. + @param[in] Token Pointer to the token to be buffered. + @param[in] DataLen The data length of the buffer contained in Token. + + @return Pointer to the token that wraps Token. If NULL, an error condition occurred. + +**/ +SOCK_TOKEN * +SockBufferToken ( + IN SOCKET *Sock, + IN LIST_ENTRY *List, + IN VOID *Token, + IN UINT32 DataLen + ) +{ + SOCK_TOKEN *SockToken; + + SockToken = AllocateZeroPool (sizeof (SOCK_TOKEN)); + if (NULL == SockToken) { + + DEBUG ( + (EFI_D_ERROR, + "SockBufferIOToken: No Memory to allocate SockToken\n") + ); + + return NULL; + } + + SockToken->Sock = Sock; + SockToken->Token = (SOCK_COMPLETION_TOKEN *) Token; + SockToken->RemainDataLen = DataLen; + InsertTailList (List, &SockToken->TokenList); + + return SockToken; +} + +/** + Destroy the socket Sock and its associated protocol control block. + + @param[in, out] Sock The socket to be destroyed. + + @retval EFI_SUCCESS The socket Sock was destroyed successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket. + +**/ +EFI_STATUS +SockDestroyChild ( + IN OUT SOCKET *Sock + ) +{ + EFI_STATUS Status; + TCP_PROTO_DATA *ProtoData; + TCP_CB *Tcb; + EFI_GUID *IpProtocolGuid; + EFI_GUID *TcpProtocolGuid; + VOID *SockProtocol; + + ASSERT ((Sock != NULL) && (Sock->ProtoHandler != NULL)); + + if (Sock->InDestroy) { + return EFI_SUCCESS; + } + + Sock->InDestroy = TRUE; + + if (Sock->IpVersion == IP_VERSION_4) { + IpProtocolGuid = &gEfiIp4ProtocolGuid; + TcpProtocolGuid = &gEfiTcp4ProtocolGuid; + } else { + IpProtocolGuid = &gEfiIp6ProtocolGuid; + TcpProtocolGuid = &gEfiTcp6ProtocolGuid; + } + ProtoData = (TCP_PROTO_DATA *) Sock->ProtoReserved; + Tcb = ProtoData->TcpPcb; + + ASSERT (Tcb != NULL); + + // + // Close the IP protocol. + // + gBS->CloseProtocol ( + Tcb->IpInfo->ChildHandle, + IpProtocolGuid, + ProtoData->TcpService->IpIo->Image, + Sock->SockHandle + ); + + if (Sock->DestroyCallback != NULL) { + Sock->DestroyCallback (Sock, Sock->Context); + } + + // + // Retrieve the protocol installed on this sock + // + Status = gBS->OpenProtocol ( + Sock->SockHandle, + TcpProtocolGuid, + &SockProtocol, + Sock->DriverBinding, + Sock->SockHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockDestroyChild: Open protocol installed on socket failed with %r\n", + Status) + ); + } + + // + // Uninstall the protocol installed on this sock + // + gBS->UninstallMultipleProtocolInterfaces ( + Sock->SockHandle, + TcpProtocolGuid, + SockProtocol, + NULL + ); + + + Status = EfiAcquireLockOrFail (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockDestroyChild: Get the lock to access socket failed with %r\n", + Status) + ); + + return EFI_ACCESS_DENIED; + } + + // + // force protocol layer to detach the PCB + // + Status = Sock->ProtoHandler (Sock, SOCK_DETACH, NULL); + + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockDestroyChild: Protocol detach socket failed with %r\n", + Status) + ); + + Sock->InDestroy = FALSE; + } else if (SOCK_IS_CONFIGURED (Sock)) { + + SockConnFlush (Sock); + SockSetState (Sock, SO_CLOSED); + + Sock->ConfigureState = SO_UNCONFIGURED; + } + + EfiReleaseLock (&(Sock->Lock)); + + if (EFI_ERROR (Status)) { + return Status; + } + + SockDestroy (Sock); + return EFI_SUCCESS; +} + +/** + Create a socket and its associated protocol control block + with the intial data SockInitData and protocol specific + data ProtoData. + + @param[in] SockInitData Inital data to setting the socket. + + @return Pointer to the newly created socket. If NULL, an error condition occured. + +**/ +SOCKET * +SockCreateChild ( + IN SOCK_INIT_DATA *SockInitData + ) +{ + SOCKET *Sock; + EFI_STATUS Status; + VOID *SockProtocol; + EFI_GUID *TcpProtocolGuid; + + // + // create a new socket + // + Sock = SockCreate (SockInitData); + if (NULL == Sock) { + + DEBUG ( + (EFI_D_ERROR, + "SockCreateChild: No resource to create a new socket\n") + ); + + return NULL; + } + + Status = EfiAcquireLockOrFail (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockCreateChild: Get the lock to access socket failed with %r\n", + Status) + ); + goto ERROR; + } + // + // inform the protocol layer to attach the socket + // with a new protocol control block + // + Status = Sock->ProtoHandler (Sock, SOCK_ATTACH, NULL); + EfiReleaseLock (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockCreateChild: Protocol failed to attach a socket with %r\n", + Status) + ); + goto ERROR; + } + + return Sock; + +ERROR: + + if (Sock->DestroyCallback != NULL) { + Sock->DestroyCallback (Sock, Sock->Context); + } + + if (Sock->IpVersion == IP_VERSION_4) { + TcpProtocolGuid = &gEfiTcp4ProtocolGuid; + } else { + TcpProtocolGuid = &gEfiTcp6ProtocolGuid; + } + + gBS->OpenProtocol ( + Sock->SockHandle, + TcpProtocolGuid, + &SockProtocol, + Sock->DriverBinding, + Sock->SockHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + // + // Uninstall the protocol installed on this sock + // + gBS->UninstallMultipleProtocolInterfaces ( + Sock->SockHandle, + TcpProtocolGuid, + SockProtocol, + NULL + ); + SockDestroy (Sock); + return NULL; +} + +/** + Configure the specific socket Sock using configuration data ConfigData. + + @param[in] Sock Pointer to the socket to be configured. + @param[in] ConfigData Pointer to the configuration data. + + @retval EFI_SUCCESS The socket configured successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is already configured. + +**/ +EFI_STATUS +SockConfigure ( + IN SOCKET *Sock, + IN VOID *ConfigData + ) +{ + EFI_STATUS Status; + + Status = EfiAcquireLockOrFail (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockConfigure: Get the access for socket failed with %r", + Status) + ); + + return EFI_ACCESS_DENIED; + } + + if (SOCK_IS_CONFIGURED (Sock)) { + Status = EFI_ACCESS_DENIED; + goto OnExit; + } + + ASSERT (Sock->State == SO_CLOSED); + + Status = Sock->ProtoHandler (Sock, SOCK_CONFIGURE, ConfigData); + +OnExit: + EfiReleaseLock (&(Sock->Lock)); + + return Status; +} + +/** + Initiate a connection establishment process. + + @param[in] Sock Pointer to the socket to initiate the initate the + connection. + @param[in] Token Pointer to the token used for the connection + operation. + + @retval EFI_SUCCESS The connection initialized successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is closed, or the socket is not configured to + be an active one, or the token is already in one of + this socket's lists. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + +**/ +EFI_STATUS +SockConnect ( + IN SOCKET *Sock, + IN VOID *Token + ) +{ + EFI_STATUS Status; + EFI_EVENT Event; + + Status = EfiAcquireLockOrFail (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockConnect: Get the access for socket failed with %r", + Status) + ); + + return EFI_ACCESS_DENIED; + } + + if (SOCK_IS_NO_MAPPING (Sock)) { + Status = EFI_NO_MAPPING; + goto OnExit; + } + + if (SOCK_IS_UNCONFIGURED (Sock)) { + + Status = EFI_NOT_STARTED; + goto OnExit; + } + + if (!SOCK_IS_CLOSED (Sock) || !SOCK_IS_CONFIGURED_ACTIVE (Sock)) { + + Status = EFI_ACCESS_DENIED; + goto OnExit; + } + + Event = ((SOCK_COMPLETION_TOKEN *) Token)->Event; + + if (SockTokenExisted (Sock, Event)) { + + Status = EFI_ACCESS_DENIED; + goto OnExit; + } + + Sock->ConnectionToken = (SOCK_COMPLETION_TOKEN *) Token; + SockSetState (Sock, SO_CONNECTING); + Status = Sock->ProtoHandler (Sock, SOCK_CONNECT, NULL); + +OnExit: + EfiReleaseLock (&(Sock->Lock)); + return Status; +} + +/** + Issue a listen token to get an existed connected network instance + or wait for a connection if there is none. + + @param[in] Sock Pointer to the socket to accept connections. + @param[in] Token The token to accept a connection. + + @retval EFI_SUCCESS Either a connection is accpeted or the Token is + buffered for further acception. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is closed, or the socket is not configured to + be a passive one, or the token is already in one of + this socket's lists. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + @retval EFI_OUT_OF_RESOURCE Failed to buffer the Token due to memory limits. + +**/ +EFI_STATUS +SockAccept ( + IN SOCKET *Sock, + IN VOID *Token + ) +{ + EFI_TCP4_LISTEN_TOKEN *ListenToken; + LIST_ENTRY *ListEntry; + EFI_STATUS Status; + SOCKET *Socket; + EFI_EVENT Event; + + ASSERT (SockStream == Sock->Type); + + Status = EfiAcquireLockOrFail (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockAccept: Get the access for socket failed with %r", + Status) + ); + + return EFI_ACCESS_DENIED; + } + + if (SOCK_IS_NO_MAPPING (Sock)) { + Status = EFI_NO_MAPPING; + goto Exit; + } + + if (SOCK_IS_UNCONFIGURED (Sock)) { + + Status = EFI_NOT_STARTED; + goto Exit; + } + + if (!SOCK_IS_LISTENING (Sock)) { + + Status = EFI_ACCESS_DENIED; + goto Exit; + } + + Event = ((SOCK_COMPLETION_TOKEN *) Token)->Event; + + if (SockTokenExisted (Sock, Event)) { + + Status = EFI_ACCESS_DENIED; + goto Exit; + } + + ListenToken = (EFI_TCP4_LISTEN_TOKEN *) Token; + + // + // Check if a connection has already in this Sock->ConnectionList + // + NET_LIST_FOR_EACH (ListEntry, &Sock->ConnectionList) { + + Socket = NET_LIST_USER_STRUCT (ListEntry, SOCKET, ConnectionList); + + if (SOCK_IS_CONNECTED (Socket)) { + ListenToken->NewChildHandle = Socket->SockHandle; + SIGNAL_TOKEN (&(ListenToken->CompletionToken), EFI_SUCCESS); + + RemoveEntryList (ListEntry); + + ASSERT (Socket->Parent != NULL); + + Socket->Parent->ConnCnt--; + + DEBUG ( + (EFI_D_NET, + "SockAccept: Accept a socket, now conncount is %d", + Socket->Parent->ConnCnt) + ); + Socket->Parent = NULL; + + goto Exit; + } + } + + // + // Buffer this token for latter incoming connection request + // + if (NULL == SockBufferToken (Sock, &(Sock->ListenTokenList), Token, 0)) { + + Status = EFI_OUT_OF_RESOURCES; + } + +Exit: + EfiReleaseLock (&(Sock->Lock)); + + return Status; +} + +/** + Issue a token with data to the socket to send out. + + @param[in] Sock Pointer to the socket to process the token with + data. + @param[in] Token The token with data that needs to send out. + + @retval EFI_SUCCESS The token processed successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is closed, or the socket is not in a + synchronized state , or the token is already in one + of this socket's lists. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limits. + +**/ +EFI_STATUS +SockSend ( + IN SOCKET *Sock, + IN VOID *Token + ) +{ + SOCK_IO_TOKEN *SndToken; + EFI_EVENT Event; + UINT32 FreeSpace; + EFI_TCP4_TRANSMIT_DATA *TxData; + EFI_STATUS Status; + SOCK_TOKEN *SockToken; + UINT32 DataLen; + + ASSERT (SockStream == Sock->Type); + + Status = EfiAcquireLockOrFail (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockSend: Get the access for socket failed with %r", + Status) + ); + + return EFI_ACCESS_DENIED; + } + + if (SOCK_IS_NO_MAPPING (Sock)) { + Status = EFI_NO_MAPPING; + goto Exit; + } + + SndToken = (SOCK_IO_TOKEN *) Token; + TxData = (EFI_TCP4_TRANSMIT_DATA *) SndToken->Packet.TxData; + + if (SOCK_IS_UNCONFIGURED (Sock)) { + Status = EFI_NOT_STARTED; + goto Exit; + } + + if (!(SOCK_IS_CONNECTING (Sock) || SOCK_IS_CONNECTED (Sock))) { + + Status = EFI_ACCESS_DENIED; + goto Exit; + } + + // + // check if a token is already in the token buffer + // + Event = SndToken->Token.Event; + + if (SockTokenExisted (Sock, Event)) { + Status = EFI_ACCESS_DENIED; + goto Exit; + } + + DataLen = TxData->DataLength; + + // + // process this sending token now or buffer it only? + // + FreeSpace = SockGetFreeSpace (Sock, SOCK_SND_BUF); + + if ((FreeSpace < Sock->SndBuffer.LowWater) || !SOCK_IS_CONNECTED (Sock)) { + + SockToken = SockBufferToken ( + Sock, + &Sock->SndTokenList, + SndToken, + DataLen + ); + + if (NULL == SockToken) { + Status = EFI_OUT_OF_RESOURCES; + } + } else { + + SockToken = SockBufferToken ( + Sock, + &Sock->ProcessingSndTokenList, + SndToken, + DataLen + ); + + if (NULL == SockToken) { + DEBUG ( + (EFI_D_ERROR, + "SockSend: Failed to buffer IO token into socket processing SndToken List\n", + Status) + ); + + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + Status = SockProcessTcpSndData (Sock, TxData); + + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "SockSend: Failed to process Snd Data\n", + Status) + ); + + RemoveEntryList (&(SockToken->TokenList)); + FreePool (SockToken); + } + } + +Exit: + EfiReleaseLock (&(Sock->Lock)); + return Status; +} + +/** + Issue a token to get data from the socket. + + @param[in] Sock Pointer to the socket to get data from. + @param[in] Token The token to store the received data from the + socket. + + @retval EFI_SUCCESS The token processed successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is closed, or the socket is not in a + synchronized state , or the token is already in one + of this socket's lists. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + @retval EFI_CONNECTION_FIN The connection is closed and there is no more data. + @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit. + +**/ +EFI_STATUS +SockRcv ( + IN SOCKET *Sock, + IN VOID *Token + ) +{ + SOCK_IO_TOKEN *RcvToken; + UINT32 RcvdBytes; + EFI_STATUS Status; + EFI_EVENT Event; + + ASSERT (SockStream == Sock->Type); + + Status = EfiAcquireLockOrFail (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockRcv: Get the access for socket failed with %r", + Status) + ); + + return EFI_ACCESS_DENIED; + } + + if (SOCK_IS_NO_MAPPING (Sock)) { + + Status = EFI_NO_MAPPING; + goto Exit; + } + + if (SOCK_IS_UNCONFIGURED (Sock)) { + + Status = EFI_NOT_STARTED; + goto Exit; + } + + if (!(SOCK_IS_CONNECTED (Sock) || SOCK_IS_CONNECTING (Sock))) { + + Status = EFI_ACCESS_DENIED; + goto Exit; + } + + RcvToken = (SOCK_IO_TOKEN *) Token; + + // + // check if a token is already in the token buffer of this socket + // + Event = RcvToken->Token.Event; + if (SockTokenExisted (Sock, Event)) { + Status = EFI_ACCESS_DENIED; + goto Exit; + } + + RcvToken = (SOCK_IO_TOKEN *) Token; + RcvdBytes = GET_RCV_DATASIZE (Sock); + + // + // check whether an error has happened before + // + if (EFI_ABORTED != Sock->SockError) { + + SIGNAL_TOKEN (&(RcvToken->Token), Sock->SockError); + Sock->SockError = EFI_ABORTED; + goto Exit; + } + + // + // check whether can not receive and there is no any + // data buffered in Sock->RcvBuffer + // + if (SOCK_IS_NO_MORE_DATA (Sock) && (0 == RcvdBytes)) { + + Status = EFI_CONNECTION_FIN; + goto Exit; + } + + if (RcvdBytes != 0) { + SockProcessRcvToken (Sock, RcvToken); + + Status = Sock->ProtoHandler (Sock, SOCK_CONSUMED, NULL); + } else { + + if (NULL == SockBufferToken (Sock, &Sock->RcvTokenList, RcvToken, 0)) { + Status = EFI_OUT_OF_RESOURCES; + } + } + +Exit: + EfiReleaseLock (&(Sock->Lock)); + return Status; +} + +/** + Reset the socket and its associated protocol control block. + + @param[in, out] Sock Pointer to the socket to be flushed. + + @retval EFI_SUCCESS The socket is flushed successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket. + +**/ +EFI_STATUS +SockFlush ( + IN OUT SOCKET *Sock + ) +{ + EFI_STATUS Status; + + ASSERT (SockStream == Sock->Type); + + Status = EfiAcquireLockOrFail (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockFlush: Get the access for socket failed with %r", + Status) + ); + + return EFI_ACCESS_DENIED; + } + + if (!SOCK_IS_CONFIGURED (Sock)) { + + Status = EFI_ACCESS_DENIED; + goto Exit; + } + + Status = Sock->ProtoHandler (Sock, SOCK_FLUSH, NULL); + if (EFI_ERROR (Status)) { + + DEBUG ( + (EFI_D_ERROR, + "SockFlush: Protocol failed handling SOCK_FLUSH with %r", + Status) + ); + + goto Exit; + } + + SOCK_ERROR (Sock, EFI_ABORTED); + SockConnFlush (Sock); + SockSetState (Sock, SO_CLOSED); + + Sock->ConfigureState = SO_UNCONFIGURED; + +Exit: + EfiReleaseLock (&(Sock->Lock)); + return Status; +} + +/** + Close or abort the socket associated connection. + + @param[in, out] Sock Pointer to the socket of the connection to close + or abort. + @param[in] Token The token for a close operation. + @param[in] OnAbort TRUE for aborting the connection; FALSE to close it. + + @retval EFI_SUCCESS The close or abort operation initialized + successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is closed, or the socket is not in a + synchronized state , or the token is already in one + of this socket's lists. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + +**/ +EFI_STATUS +SockClose ( + IN OUT SOCKET *Sock, + IN VOID *Token, + IN BOOLEAN OnAbort + ) +{ + EFI_STATUS Status; + EFI_EVENT Event; + + ASSERT (SockStream == Sock->Type); + + Status = EfiAcquireLockOrFail (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "SockClose: Get the access for socket failed with %r", + Status) + ); + + return EFI_ACCESS_DENIED; + } + + if (SOCK_IS_NO_MAPPING (Sock)) { + Status = EFI_NO_MAPPING; + goto Exit; + } + + if (SOCK_IS_UNCONFIGURED (Sock)) { + Status = EFI_NOT_STARTED; + goto Exit; + } + + if (SOCK_IS_DISCONNECTING (Sock)) { + Status = EFI_ACCESS_DENIED; + goto Exit; + } + + Event = ((SOCK_COMPLETION_TOKEN *) Token)->Event; + + if (SockTokenExisted (Sock, Event)) { + Status = EFI_ACCESS_DENIED; + goto Exit; + } + + Sock->CloseToken = Token; + SockSetState (Sock, SO_DISCONNECTING); + + if (OnAbort) { + Status = Sock->ProtoHandler (Sock, SOCK_ABORT, NULL); + } else { + Status = Sock->ProtoHandler (Sock, SOCK_CLOSE, NULL); + } + +Exit: + EfiReleaseLock (&(Sock->Lock)); + return Status; +} + +/** + Abort the socket associated connection, listen, transmission or receive request. + + @param[in, out] Sock Pointer to the socket to abort. + @param[in] Token Pointer to a token that has been issued by + Connect(), Accept(), Transmit() or Receive(). If + NULL, all pending tokens issued by the four + functions listed above will be aborted. + + @retval EFI_UNSUPPORTED The operation is not supported in the current + implementation. +**/ +EFI_STATUS +SockCancel ( + IN OUT SOCKET *Sock, + IN VOID *Token + ) +{ + EFI_STATUS Status; + + Status = EFI_SUCCESS; + + ASSERT (SockStream == Sock->Type); + + Status = EfiAcquireLockOrFail (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "SockCancel: Get the access for socket failed with %r", + Status) + ); + + return EFI_ACCESS_DENIED; + } + + if (SOCK_IS_UNCONFIGURED (Sock)) { + Status = EFI_NOT_STARTED; + goto Exit; + } + + // + // 1. Check ConnectionToken. + // + if (Token == NULL || (SOCK_COMPLETION_TOKEN *) Token == Sock->ConnectionToken) { + if (Sock->ConnectionToken != NULL) { + SIGNAL_TOKEN (Sock->ConnectionToken, EFI_ABORTED); + Sock->ConnectionToken = NULL; + } + + if (Token != NULL) { + Status = EFI_SUCCESS; + goto Exit; + } + } + + // + // 2. Check ListenTokenList. + // + Status = SockCancelToken (Token, &Sock->ListenTokenList); + if (Token != NULL && !EFI_ERROR (Status)) { + goto Exit; + } + + // + // 3. Check RcvTokenList. + // + Status = SockCancelToken (Token, &Sock->RcvTokenList); + if (Token != NULL && !EFI_ERROR (Status)) { + goto Exit; + } + + // + // 4. Check SndTokenList. + // + Status = SockCancelToken (Token, &Sock->SndTokenList); + if (Token != NULL && !EFI_ERROR (Status)) { + goto Exit; + } + + // + // 5. Check ProcessingSndTokenList. + // + Status = SockCancelToken (Token, &Sock->ProcessingSndTokenList); + +Exit: + EfiReleaseLock (&(Sock->Lock)); + return Status; +} + + +/** + Get the mode data of the low layer protocol. + + @param[in] Sock Pointer to the socket to get mode data from. + @param[in, out] Mode Pointer to the data to store the low layer mode + information. + + @retval EFI_SUCCESS The mode data was obtained successfully. + @retval EFI_NOT_STARTED The socket is not configured. + +**/ +EFI_STATUS +SockGetMode ( + IN SOCKET *Sock, + IN OUT VOID *Mode + ) +{ + return Sock->ProtoHandler (Sock, SOCK_MODE, Mode); +} + +/** + Add or remove route information in IP route table associated + with this socket. + + @param[in] Sock Pointer to the socket associated with the IP route + table to operate on. + @param[in] RouteInfo Pointer to the route information to be processed. + + @retval EFI_SUCCESS The route table updated successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + +**/ +EFI_STATUS +SockRoute ( + IN SOCKET *Sock, + IN VOID *RouteInfo + ) +{ + EFI_STATUS Status; + + Status = EfiAcquireLockOrFail (&(Sock->Lock)); + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "SockRoute: Get the access for socket failed with %r", + Status) + ); + + return EFI_ACCESS_DENIED; + } + + if (SOCK_IS_NO_MAPPING (Sock)) { + Status = EFI_NO_MAPPING; + goto Exit; + } + + if (SOCK_IS_UNCONFIGURED (Sock)) { + Status = EFI_NOT_STARTED; + goto Exit; + } + + Status = Sock->ProtoHandler (Sock, SOCK_ROUTE, RouteInfo); + +Exit: + EfiReleaseLock (&(Sock->Lock)); + return Status; +} + diff --git a/NetworkPkg/TcpDxe/Socket.h b/NetworkPkg/TcpDxe/Socket.h new file mode 100644 index 000000000..874708ea5 --- /dev/null +++ b/NetworkPkg/TcpDxe/Socket.h @@ -0,0 +1,917 @@ +/** @file + Common head file for TCP socket. + + Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _SOCKET_H_ +#define _SOCKET_H_ + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#define SOCK_SND_BUF 0 +#define SOCK_RCV_BUF 1 + +#define SOCK_BUFF_LOW_WATER (2 * 1024) +#define SOCK_RCV_BUFF_SIZE (8 * 1024) +#define SOCK_SND_BUFF_SIZE (8 * 1024) +#define SOCK_BACKLOG 5 + +#define PROTO_RESERVED_LEN 20 + +#define SO_NO_MORE_DATA 0x0001 + +// +// +// +// When a socket is created it enters into SO_UNCONFIGURED, +// no actions can be taken on this socket, only after calling +// SockConfigure. The state transition diagram of socket is +// as following: +// +// SO_UNCONFIGURED --- SO_CONFIGURED --- SO_CONNECTING +// ^ | | +// | ---> SO_LISTENING | +// | | +// |------------------SO_DISCONNECTING<-- SO_CONNECTED +// +// A passive socket can only go into SO_LISTENING and +// SO_UNCONFIGURED state. SO_XXXING state is a middle state +// when a socket is undergoing a protocol procedure such +// as requesting a TCP connection. +// +// +// + +/// +/// Socket state +/// +#define SO_CLOSED 0 +#define SO_LISTENING 1 +#define SO_CONNECTING 2 +#define SO_CONNECTED 3 +#define SO_DISCONNECTING 4 + +/// +/// Socket configure state +/// +#define SO_UNCONFIGURED 0 +#define SO_CONFIGURED_ACTIVE 1 +#define SO_CONFIGURED_PASSIVE 2 +#define SO_NO_MAPPING 3 + +/// +/// The request issued from socket layer to protocol layer. +/// +#define SOCK_ATTACH 0 ///< Attach current socket to a new PCB +#define SOCK_DETACH 1 ///< Detach current socket from the PCB +#define SOCK_CONFIGURE 2 ///< Configure attached PCB +#define SOCK_FLUSH 3 ///< Flush attached PCB +#define SOCK_SND 4 ///< Need protocol to send something +#define SOCK_SNDPUSH 5 ///< Need protocol to send pushed data +#define SOCK_SNDURG 6 ///< Need protocol to send urgent data +#define SOCK_CONSUMED 7 ///< Application has retrieved data from socket +#define SOCK_CONNECT 8 ///< Need to connect to a peer +#define SOCK_CLOSE 9 ///< Need to close the protocol process +#define SOCK_ABORT 10 ///< Need to reset the protocol process +#define SOCK_POLL 11 ///< Need to poll to the protocol layer +#define SOCK_ROUTE 12 ///< Need to add a route information +#define SOCK_MODE 13 ///< Need to get the mode data of the protocol +#define SOCK_GROUP 14 ///< Need to join a mcast group + +/** + Set socket SO_NO_MORE_DATA flag. + + @param[in] Sock Pointer to the socket + +**/ +#define SOCK_NO_MORE_DATA(Sock) ((Sock)->Flag |= SO_NO_MORE_DATA) + +/** + Check whether the socket is unconfigured. + + @param[in] Sock Pointer to the socket. + + @retval TRUE The socket is unconfigued. + @retval FALSE The socket is not unconfigued. + +**/ +#define SOCK_IS_UNCONFIGURED(Sock) ((Sock)->ConfigureState == SO_UNCONFIGURED) + +/** + Check whether the socket is configured. + + @param[in] Sock Pointer to the socket + + @retval TRUE The socket is configued + @retval FALSE The socket is not configued + +**/ +#define SOCK_IS_CONFIGURED(Sock) \ + (((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \ + ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)) + +/** + Check whether the socket is configured to active mode. + + @param[in] Sock Pointer to the socket. + + @retval TRUE The socket is configued to active mode. + @retval FALSE The socket is not configued to active mode. + +**/ +#define SOCK_IS_CONFIGURED_ACTIVE(Sock) ((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) + +/** + Check whether the socket is configured to passive mode. + + @param[in] Sock Pointer to the socket. + + @retval TRUE The socket is configued to passive mode. + @retval FALSE The socket is not configued to passive mode. + +**/ +#define SOCK_IS_CONNECTED_PASSIVE(Sock) ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE) + +/** + Check whether the socket is mapped. + + @param[in] Sock Pointer to the socket. + + @retval TRUE The socket is not mapping. + @retval FALSE The socket is mapped. + +**/ +#define SOCK_IS_NO_MAPPING(Sock) ((Sock)->ConfigureState == SO_NO_MAPPING) + +/** + Check whether the socket is closed. + + @param[in] Sock Pointer to the socket. + + @retval TRUE The socket is closed. + @retval FALSE The socket is not closed. + +**/ +#define SOCK_IS_CLOSED(Sock) ((Sock)->State == SO_CLOSED) + +/** + Check whether the socket is listening. + + @param[in] Sock Pointer to the socket. + + @retval TRUE The socket is listening. + @retval FALSE The socket is not listening. + +**/ +#define SOCK_IS_LISTENING(Sock) ((Sock)->State == SO_LISTENING) + +/** + Check whether the socket is connecting. + + @param[in] Sock Pointer to the socket. + + @retval TRUE The socket is connecting. + @retval FALSE The socket is not connecting. + +**/ +#define SOCK_IS_CONNECTING(Sock) ((Sock)->State == SO_CONNECTING) + +/** + Check whether the socket has connected. + + @param[in] Sock Pointer to the socket. + + @retval TRUE The socket has connected. + @retval FALSE The socket has not connected. + +**/ +#define SOCK_IS_CONNECTED(Sock) ((Sock)->State == SO_CONNECTED) + +/** + Check whether the socket is disconnecting. + + @param[in] Sock Pointer to the socket. + + @retval TRUE The socket is disconnecting. + @retval FALSE The socket is not disconnecting. + +**/ +#define SOCK_IS_DISCONNECTING(Sock) ((Sock)->State == SO_DISCONNECTING) + +/** + Check whether the socket is no more data. + + @param[in] Sock Pointer to the socket. + + @retval TRUE The socket is no more data. + @retval FALSE The socket still has data. + +**/ +#define SOCK_IS_NO_MORE_DATA(Sock) (0 != ((Sock)->Flag & SO_NO_MORE_DATA)) + +/** + Set the size of the receive buffer. + + @param[in] Sock Pointer to the socket. + @param[in] Size The size to set. + +**/ +#define SET_RCV_BUFFSIZE(Sock, Size) ((Sock)->RcvBuffer.HighWater = (Size)) + +/** + Get the size of the receive buffer. + + @param[in] Sock Pointer to the socket. + + @return The receive buffer size. + +**/ +#define GET_RCV_BUFFSIZE(Sock) ((Sock)->RcvBuffer.HighWater) + +/** + Get the size of the receive data. + + @param[in] Sock Pointer to the socket. + + @return The received data size. + +**/ +#define GET_RCV_DATASIZE(Sock) (((Sock)->RcvBuffer.DataQueue)->BufSize) + +/** + Set the size of the send buffer. + + @param[in] Sock Pointer to the socket. + @param[in] Size The size to set. + +**/ +#define SET_SND_BUFFSIZE(Sock, Size) ((Sock)->SndBuffer.HighWater = (Size)) + +/** + Get the size of the send buffer. + + @param[in] Sock Pointer to the socket. + + @return The send buffer size. + +**/ +#define GET_SND_BUFFSIZE(Sock) ((Sock)->SndBuffer.HighWater) + +/** + Get the size of the send data. + + @param[in] Sock Pointer to the socket. + + @return The send data size. + +**/ +#define GET_SND_DATASIZE(Sock) (((Sock)->SndBuffer.DataQueue)->BufSize) + +/** + Set the backlog value of the socket. + + @param[in] Sock Pointer to the socket. + @param[in] Value The value to set. + +**/ +#define SET_BACKLOG(Sock, Value) ((Sock)->BackLog = (Value)) + +/** + Get the backlog value of the socket. + + @param[in] Sock Pointer to the socket. + + @return The backlog value. + +**/ +#define GET_BACKLOG(Sock) ((Sock)->BackLog) + +/** + Set the socket with error state. + + @param[in] Sock Pointer to the socket. + @param[in] Error The error state. + +**/ +#define SOCK_ERROR(Sock, Error) ((Sock)->SockError = (Error)) + +#define SOCK_SIGNATURE SIGNATURE_32 ('S', 'O', 'C', 'K') + +#define SOCK_FROM_THIS(a) CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE) + +#define SOCK_FROM_TOKEN(Token) (((SOCK_TOKEN *) (Token))->Sock) + +#define PROTO_TOKEN_FORM_SOCK(SockToken, Type) ((Type *) (((SOCK_TOKEN *) (SockToken))->Token)) + +typedef struct _TCP_SOCKET SOCKET; + +/// +/// Socket completion token +/// +typedef struct _SOCK_COMPLETION_TOKEN { + EFI_EVENT Event; ///< The event to be issued + EFI_STATUS Status; ///< The status to be issued +} SOCK_COMPLETION_TOKEN; + +typedef union { + VOID *RxData; + VOID *TxData; +} SOCK_IO_DATA; + +/// +/// The application token with data packet +/// +typedef struct _SOCK_IO_TOKEN { + SOCK_COMPLETION_TOKEN Token; + SOCK_IO_DATA Packet; +} SOCK_IO_TOKEN; + +/// +/// The socket type. +/// +typedef enum { + SockDgram, ///< This socket providing datagram service + SockStream ///< This socket providing stream service +} SOCK_TYPE; + +/// +/// The buffer structure of rcvd data and send data used by socket. +/// +typedef struct _SOCK_BUFFER { + UINT32 HighWater; ///< The buffersize upper limit of sock_buffer + UINT32 LowWater; ///< The low water mark of sock_buffer + NET_BUF_QUEUE *DataQueue; ///< The queue to buffer data +} SOCK_BUFFER; + +/** + The handler of protocol for request from socket. + + @param[in] Socket The socket issuing the request to protocol. + @param[in] Request The request issued by socket. + @param[in] RequestData The request related data. + + @retval EFI_SUCCESS The socket request is completed successfully. + @retval other The error status returned by the corresponding TCP + layer function. + +**/ +typedef +EFI_STATUS +(*SOCK_PROTO_HANDLER) ( + IN SOCKET *Socket, + IN UINT8 Request, + IN VOID *RequestData + ); + +/** + The Callback funtion called after the TCP socket is created. + + @param[in] This Pointer to the socket just created. + @param[in] Context Context of the socket. + + @retval EFI_SUCCESS This protocol installed successfully. + @retval other Some error occured. + +**/ +typedef +EFI_STATUS +(*SOCK_CREATE_CALLBACK) ( + IN SOCKET *This, + IN VOID *Context + ); + +/** + The callback function called before the TCP socket is to be destroyed. + + @param[in] This The TCP socket to be destroyed. + @param[in] Context The context. + +**/ +typedef +VOID +(*SOCK_DESTROY_CALLBACK) ( + IN SOCKET *This, + IN VOID *Context + ); + +/// +/// The initialize data for create a new socket. +/// +typedef struct _SOCK_INIT_DATA { + SOCK_TYPE Type; + UINT8 State; + + SOCKET *Parent; ///< The parent of this socket + UINT32 BackLog; ///< The connection limit for listening socket + UINT32 SndBufferSize; ///< The high water mark of send buffer + UINT32 RcvBufferSize; ///< The high water mark of receive buffer + UINT8 IpVersion; + VOID *Protocol; ///< The pointer to protocol function template + ///< wanted to install on socket + + // + // Callbacks after socket is created and before socket is to be destroyed. + // + SOCK_CREATE_CALLBACK CreateCallback; ///< Callback after created + SOCK_DESTROY_CALLBACK DestroyCallback; ///< Callback before destroied + VOID *Context; ///< The context of the callback + + // + // Opaque protocol data. + // + VOID *ProtoData; + UINT32 DataSize; + + SOCK_PROTO_HANDLER ProtoHandler; ///< The handler of protocol for socket request + + EFI_HANDLE DriverBinding; ///< The driver binding handle +} SOCK_INIT_DATA; + +/// +/// The union type of TCP4 and TCP6 protocol. +/// +typedef union _NET_PROTOCOL { + EFI_TCP4_PROTOCOL Tcp4Protocol; ///< Tcp4 protocol + EFI_TCP6_PROTOCOL Tcp6Protocol; ///< Tcp6 protocol +} NET_PROTOCOL; +/// +/// The socket structure representing a network service access point. +/// +struct _TCP_SOCKET { + // + // Socket description information + // + UINT32 Signature; ///< Signature of the socket + EFI_HANDLE SockHandle; ///< The virtual handle of the socket + EFI_HANDLE DriverBinding; ///< Socket's driver binding protocol + EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + LIST_ENTRY Link; + UINT8 ConfigureState; + SOCK_TYPE Type; + UINT8 State; + UINT16 Flag; + EFI_LOCK Lock; ///< The lock of socket + SOCK_BUFFER SndBuffer; ///< Send buffer of application's data + SOCK_BUFFER RcvBuffer; ///< Receive buffer of received data + EFI_STATUS SockError; ///< The error returned by low layer protocol + BOOLEAN InDestroy; + + // + // Fields used to manage the connection request + // + UINT32 BackLog; ///< the limit of connection to this socket + UINT32 ConnCnt; ///< the current count of connections to it + SOCKET *Parent; ///< listening parent that accept the connection + LIST_ENTRY ConnectionList; ///< the connections maintained by this socket + // + // The queue to buffer application's asynchronous token + // + LIST_ENTRY ListenTokenList; + LIST_ENTRY RcvTokenList; + LIST_ENTRY SndTokenList; + LIST_ENTRY ProcessingSndTokenList; + + SOCK_COMPLETION_TOKEN *ConnectionToken; ///< app's token to signal if connected + SOCK_COMPLETION_TOKEN *CloseToken; ///< app's token to signal if closed + // + // Interface for low level protocol + // + SOCK_PROTO_HANDLER ProtoHandler; ///< The request handler of protocol + UINT8 ProtoReserved[PROTO_RESERVED_LEN]; ///< Data fields reserved for protocol + UINT8 IpVersion; + NET_PROTOCOL NetProtocol; ///< TCP4 or TCP6 protocol socket used + // + // Callbacks after socket is created and before socket is to be destroyed. + // + SOCK_CREATE_CALLBACK CreateCallback; ///< Callback after created + SOCK_DESTROY_CALLBACK DestroyCallback; ///< Callback before destroied + VOID *Context; ///< The context of the callback +}; + +/// +/// The token structure buffered in socket layer. +/// +typedef struct _SOCK_TOKEN { + LIST_ENTRY TokenList; ///< The entry to add in the token list + SOCK_COMPLETION_TOKEN *Token; ///< The application's token + UINT32 RemainDataLen; ///< Unprocessed data length + SOCKET *Sock; ///< The poninter to the socket this token + ///< belongs to +} SOCK_TOKEN; + +/// +/// Reserved data to access the NET_BUF delivered by TCP driver. +/// +typedef struct _TCP_RSV_DATA { + UINT32 UrgLen; +} TCP_RSV_DATA; + +// +// Socket provided oprerations for low layer protocol implemented in SockImpl.c +// + +/** + Set the state of the socket. + + @param[in, out] Sock Pointer to the socket. + @param[in] State The new socket state to be set. + +**/ +VOID +SockSetState ( + IN OUT SOCKET *Sock, + IN UINT8 State + ); + +/** + Clone a new socket including its associated protocol control block. + + @param[in] Sock Pointer to the socket to be cloned. + + @return Pointer to the newly cloned socket. If NULL, an error condition occurred. + +**/ +SOCKET * +SockClone ( + IN SOCKET *Sock + ); + +/** + Called by the low layer protocol to indicate the socket a connection is + established. + + This function just changes the socket's state to SO_CONNECTED + and signals the token used for connection establishment. + + @param[in, out] Sock Pointer to the socket associated with the + established connection. + +**/ +VOID +SockConnEstablished ( + IN OUT SOCKET *Sock + ); + +/** + Called by the low layer protocol to indicate that the connection is closed. + + This function flushes the socket, sets the state to SO_CLOSED, and signals + the close token. + + @param[in, out] Sock Pointer to the socket associated with the closed + connection. + +**/ +VOID +SockConnClosed ( + IN OUT SOCKET *Sock + ); + +/** + Called by low layer protocol to indicate that some data is sent or processed. + + This function trims the sent data in the socket send buffer and signals the data + token, if proper. + + @param[in, out] Sock Pointer to the socket. + @param[in] Count The length of the data processed or sent, in bytes. + +**/ +VOID +SockDataSent ( + IN OUT SOCKET *Sock, + IN UINT32 Count + ); + +/** + Called by the low layer protocol to copy some data in socket send + buffer starting from the specific offset to a buffer provided by + the caller. + + @param[in] Sock Pointer to the socket. + @param[in] Offset The start point of the data to be copied. + @param[in] Len The length of the data to be copied. + @param[out] Dest Pointer to the destination to copy the data. + + @return The data size copied. + +**/ +UINT32 +SockGetDataToSend ( + IN SOCKET *Sock, + IN UINT32 Offset, + IN UINT32 Len, + OUT UINT8 *Dest + ); + +/** + Called by the low layer protocol to deliver received data to socket layer. + + This function appends the data to the socket receive buffer, set the + urgent data length, then checks if any receive token can be signaled. + + @param[in, out] Sock Pointer to the socket. + @param[in, out] NetBuffer Pointer to the buffer that contains the received data. + @param[in] UrgLen The length of the urgent data in the received data. + +**/ +VOID +SockDataRcvd ( + IN OUT SOCKET *Sock, + IN OUT NET_BUF *NetBuffer, + IN UINT32 UrgLen + ); + +/** + Get the length of the free space of the specific socket buffer. + + @param[in] Sock Pointer to the socket. + @param[in] Which Flag to indicate which socket buffer to check: + either send buffer or receive buffer. + + @return The length of the free space, in bytes. + +**/ +UINT32 +SockGetFreeSpace ( + IN SOCKET *Sock, + IN UINT32 Which + ); + +/** + Called by the low layer protocol to indicate that there will be no more data + from the communication peer. + + This function sets the socket's state to SO_NO_MORE_DATA and signals all queued + IO tokens with the error status EFI_CONNECTION_FIN. + + @param[in, out] Sock Pointer to the socket. + +**/ +VOID +SockNoMoreData ( + IN OUT SOCKET *Sock + ); + +// +// Socket provided operations for user interface implemented in SockInterface.c +// + +/** + Create a socket and its associated protocol control block + with the intial data SockInitData and protocol specific + data ProtoData. + + @param[in] SockInitData Inital data to setting the socket. + + @return Pointer to the newly created socket. If NULL, an error condition occured. + +**/ +SOCKET * +SockCreateChild ( + IN SOCK_INIT_DATA *SockInitData + ); + +/** + Destroy the socket Sock and its associated protocol control block. + + @param[in, out] Sock The socket to be destroyed. + + @retval EFI_SUCCESS The socket Sock was destroyed successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket. + +**/ +EFI_STATUS +SockDestroyChild ( + IN OUT SOCKET *Sock + ); + +/** + Configure the specific socket Sock using configuration data ConfigData. + + @param[in] Sock Pointer to the socket to be configured. + @param[in] ConfigData Pointer to the configuration data. + + @retval EFI_SUCCESS The socket configured successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is already configured. + +**/ +EFI_STATUS +SockConfigure ( + IN SOCKET *Sock, + IN VOID *ConfigData + ); + +/** + Initiate a connection establishment process. + + @param[in] Sock Pointer to the socket to initiate the initate the + connection. + @param[in] Token Pointer to the token used for the connection + operation. + + @retval EFI_SUCCESS The connection initialized successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is closed, or the socket is not configured to + be an active one, or the token is already in one of + this socket's lists. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + +**/ +EFI_STATUS +SockConnect ( + IN SOCKET *Sock, + IN VOID *Token + ); + +/** + Issue a listen token to get an existed connected network instance, + or wait for a connection if there is none. + + @param[in] Sock Pointer to the socket to accept connections. + @param[in] Token The token to accept a connection. + + @retval EFI_SUCCESS Either a connection is accepted or the Token is + buffered for further acceptance. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is closed, or the socket is not configured to + be a passive one, or the token is already in one of + this socket's lists. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + @retval EFI_OUT_OF_RESOURCE Failed to buffer the Token due to memory limit. + +**/ +EFI_STATUS +SockAccept ( + IN SOCKET *Sock, + IN VOID *Token + ); + +/** + Issue a token with data to the socket to send out. + + @param[in] Sock Pointer to the socket to process the token with + data. + @param[in] Token The token with data that needs to send out. + + @retval EFI_SUCCESS The token processed successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is closed, or the socket is not in a + synchronized state , or the token is already in one + of this socket's lists. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to a memory limit. + +**/ +EFI_STATUS +SockSend ( + IN SOCKET *Sock, + IN VOID *Token + ); + +/** + Issue a token to get data from the socket. + + @param[in] Sock Pointer to the socket to get data from. + @param[in] Token The token to store the received data from the + socket. + + @retval EFI_SUCCESS The token processed successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is closed, or the socket is not in a + synchronized state , or the token is already in one + of this socket's lists. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + @retval EFI_CONNECTION_FIN The connection is closed and there is no more data. + @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to a memory limit. + +**/ +EFI_STATUS +SockRcv ( + IN SOCKET *Sock, + IN VOID *Token + ); + +/** + Reset the socket and its associated protocol control block. + + @param[in, out] Sock Pointer to the socket to be flushed. + + @retval EFI_SUCCESS The socket flushed successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket. + +**/ +EFI_STATUS +SockFlush ( + IN OUT SOCKET *Sock + ); + +/** + Close or abort the socket associated connection. + + @param[in, out] Sock Pointer to the socket of the connection to close + or abort. + @param[in] Token The token for close operation. + @param[in] OnAbort TRUE for aborting the connection, FALSE to close it. + + @retval EFI_SUCCESS The close or abort operation initialized + successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the + socket is closed, or the socket is not in a + synchronized state , or the token is already in one + of this socket's lists. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + +**/ +EFI_STATUS +SockClose ( + IN OUT SOCKET *Sock, + IN VOID *Token, + IN BOOLEAN OnAbort + ); + +/** + Abort the socket associated connection, listen, transmission or receive request. + + @param[in, out] Sock Pointer to the socket to abort. + @param[in] Token Pointer to a token that has been issued by + Connect(), Accept(), Transmit() or Receive(). If + NULL, all pending tokens issued by the four + functions listed above will be aborted. + + @retval EFI_UNSUPPORTED The operation is not supported in the current + implementation. +**/ +EFI_STATUS +SockCancel ( + IN OUT SOCKET *Sock, + IN VOID *Token + ); + +/** + Get the mode data of the low layer protocol. + + @param[in] Sock Pointer to the socket to get mode data from. + @param[in, out] Mode Pointer to the data to store the low layer mode + information. + + @retval EFI_SUCCESS The mode data was obtained successfully. + @retval EFI_NOT_STARTED The socket is not configured. + +**/ +EFI_STATUS +SockGetMode ( + IN SOCKET *Sock, + IN OUT VOID *Mode + ); + +/** + Add or remove route information in IP route table associated + with this socket. + + @param[in] Sock Pointer to the socket associated with the IP route + table to operate on. + @param[in] RouteInfo Pointer to the route information to be processed. + + @retval EFI_SUCCESS The route table updated successfully. + @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket. + @retval EFI_NO_MAPPING The IP address configuration operation is not + finished. + @retval EFI_NOT_STARTED The socket is not configured. + +**/ +EFI_STATUS +SockRoute ( + IN SOCKET *Sock, + IN VOID *RouteInfo + ); + +#endif diff --git a/NetworkPkg/TcpDxe/TcpDispatcher.c b/NetworkPkg/TcpDxe/TcpDispatcher.c new file mode 100644 index 000000000..86beaf8cc --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpDispatcher.c @@ -0,0 +1,890 @@ +/** @file + The implementation of a dispatch routine for processing TCP requests. + + (C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TcpMain.h" + +/** + Add or remove a route entry in the IP route table associated with this TCP instance. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] RouteInfo Pointer to the route information to be processed. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The driver instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration(DHCP, + BOOTP, RARP, etc.) is not finished yet. + @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table. + @retval EFI_NOT_FOUND This route is not in the routing table + (when RouteInfo->DeleteRoute is TRUE). + @retval EFI_ACCESS_DENIED The route is already defined in the routing table + (when RouteInfo->DeleteRoute is FALSE). +**/ +EFI_STATUS +Tcp4Route ( + IN TCP_CB *Tcb, + IN TCP4_ROUTE_INFO *RouteInfo + ) +{ + IP_IO_IP_PROTOCOL Ip; + + Ip = Tcb->IpInfo->Ip; + + ASSERT (Ip.Ip4!= NULL); + + return Ip.Ip4->Routes ( + Ip.Ip4, + RouteInfo->DeleteRoute, + RouteInfo->SubnetAddress, + RouteInfo->SubnetMask, + RouteInfo->GatewayAddress + ); + +} + +/** + Get the operational settings of this TCPv4 instance. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in, out] Mode Pointer to the buffer to store the operational + settings. + + @retval EFI_SUCCESS The mode data was read. + @retval EFI_NOT_STARTED No configuration data is available because this + instance hasn't been started. + +**/ +EFI_STATUS +Tcp4GetMode ( + IN TCP_CB *Tcb, + IN OUT TCP4_MODE_DATA *Mode + ) +{ + SOCKET *Sock; + EFI_TCP4_CONFIG_DATA *ConfigData; + EFI_TCP4_ACCESS_POINT *AccessPoint; + EFI_TCP4_OPTION *Option; + EFI_IP4_PROTOCOL *Ip; + + Sock = Tcb->Sk; + + if (!SOCK_IS_CONFIGURED (Sock) && (Mode->Tcp4ConfigData != NULL)) { + return EFI_NOT_STARTED; + } + + if (Mode->Tcp4State != NULL) { + *(Mode->Tcp4State) = (EFI_TCP4_CONNECTION_STATE) Tcb->State; + } + + if (Mode->Tcp4ConfigData != NULL) { + + ConfigData = Mode->Tcp4ConfigData; + AccessPoint = &(ConfigData->AccessPoint); + Option = ConfigData->ControlOption; + + ConfigData->TypeOfService = Tcb->Tos; + ConfigData->TimeToLive = Tcb->Ttl; + + AccessPoint->UseDefaultAddress = Tcb->UseDefaultAddr; + + IP4_COPY_ADDRESS (&AccessPoint->StationAddress, &Tcb->LocalEnd.Ip); + + IP4_COPY_ADDRESS (&AccessPoint->SubnetMask, &Tcb->SubnetMask); + AccessPoint->StationPort = NTOHS (Tcb->LocalEnd.Port); + + IP4_COPY_ADDRESS (&AccessPoint->RemoteAddress, &Tcb->RemoteEnd.Ip); + + AccessPoint->RemotePort = NTOHS (Tcb->RemoteEnd.Port); + AccessPoint->ActiveFlag = (BOOLEAN) (Tcb->State != TCP_LISTEN); + + if (Option != NULL) { + Option->ReceiveBufferSize = GET_RCV_BUFFSIZE (Tcb->Sk); + Option->SendBufferSize = GET_SND_BUFFSIZE (Tcb->Sk); + Option->MaxSynBackLog = GET_BACKLOG (Tcb->Sk); + + Option->ConnectionTimeout = Tcb->ConnectTimeout / TCP_TICK_HZ; + Option->DataRetries = Tcb->MaxRexmit; + Option->FinTimeout = Tcb->FinWait2Timeout / TCP_TICK_HZ; + Option->TimeWaitTimeout = Tcb->TimeWaitTimeout / TCP_TICK_HZ; + Option->KeepAliveProbes = Tcb->MaxKeepAlive; + Option->KeepAliveTime = Tcb->KeepAliveIdle / TCP_TICK_HZ; + Option->KeepAliveInterval = Tcb->KeepAlivePeriod / TCP_TICK_HZ; + + Option->EnableNagle = (BOOLEAN) (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_NAGLE)); + Option->EnableTimeStamp = (BOOLEAN) (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_TS)); + Option->EnableWindowScaling = (BOOLEAN) (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_WS)); + + Option->EnableSelectiveAck = FALSE; + Option->EnablePathMtuDiscovery = FALSE; + } + } + + Ip = Tcb->IpInfo->Ip.Ip4; + ASSERT (Ip != NULL); + + return Ip->GetModeData (Ip, Mode->Ip4ModeData, Mode->MnpConfigData, Mode->SnpModeData); +} + +/** + Get the operational settings of this TCPv6 instance. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in, out] Mode Pointer to the buffer to store the operational + settings. + + @retval EFI_SUCCESS The mode data was read. + @retval EFI_NOT_STARTED No configuration data is available because this + instance hasn't been started. + +**/ +EFI_STATUS +Tcp6GetMode ( + IN TCP_CB *Tcb, + IN OUT TCP6_MODE_DATA *Mode + ) +{ + SOCKET *Sock; + EFI_TCP6_CONFIG_DATA *ConfigData; + EFI_TCP6_ACCESS_POINT *AccessPoint; + EFI_TCP6_OPTION *Option; + EFI_IP6_PROTOCOL *Ip; + + Sock = Tcb->Sk; + + if (!SOCK_IS_CONFIGURED (Sock) && (Mode->Tcp6ConfigData != NULL)) { + return EFI_NOT_STARTED; + } + + if (Mode->Tcp6State != NULL) { + *(Mode->Tcp6State) = (EFI_TCP6_CONNECTION_STATE) (Tcb->State); + } + + if (Mode->Tcp6ConfigData != NULL) { + + ConfigData = Mode->Tcp6ConfigData; + AccessPoint = &(ConfigData->AccessPoint); + Option = ConfigData->ControlOption; + + ConfigData->TrafficClass = Tcb->Tos; + ConfigData->HopLimit = Tcb->Ttl; + + AccessPoint->StationPort = NTOHS (Tcb->LocalEnd.Port); + AccessPoint->RemotePort = NTOHS (Tcb->RemoteEnd.Port); + AccessPoint->ActiveFlag = (BOOLEAN) (Tcb->State != TCP_LISTEN); + + IP6_COPY_ADDRESS (&AccessPoint->StationAddress, &Tcb->LocalEnd.Ip); + IP6_COPY_ADDRESS (&AccessPoint->RemoteAddress, &Tcb->RemoteEnd.Ip); + + if (Option != NULL) { + Option->ReceiveBufferSize = GET_RCV_BUFFSIZE (Tcb->Sk); + Option->SendBufferSize = GET_SND_BUFFSIZE (Tcb->Sk); + Option->MaxSynBackLog = GET_BACKLOG (Tcb->Sk); + + Option->ConnectionTimeout = Tcb->ConnectTimeout / TCP_TICK_HZ; + Option->DataRetries = Tcb->MaxRexmit; + Option->FinTimeout = Tcb->FinWait2Timeout / TCP_TICK_HZ; + Option->TimeWaitTimeout = Tcb->TimeWaitTimeout / TCP_TICK_HZ; + Option->KeepAliveProbes = Tcb->MaxKeepAlive; + Option->KeepAliveTime = Tcb->KeepAliveIdle / TCP_TICK_HZ; + Option->KeepAliveInterval = Tcb->KeepAlivePeriod / TCP_TICK_HZ; + + Option->EnableNagle = (BOOLEAN) (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_NAGLE)); + Option->EnableTimeStamp = (BOOLEAN) (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_TS)); + Option->EnableWindowScaling = (BOOLEAN) (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_WS)); + + Option->EnableSelectiveAck = FALSE; + Option->EnablePathMtuDiscovery = FALSE; + } + } + + Ip = Tcb->IpInfo->Ip.Ip6; + ASSERT (Ip != NULL); + + return Ip->GetModeData (Ip, Mode->Ip6ModeData, Mode->MnpConfigData, Mode->SnpModeData); +} + +/** + If TcpAp->StationPort isn't zero, check whether the access point + is registered, else generate a random station port for this + access point. + + @param[in] TcpAp Pointer to the access point. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6 + + @retval EFI_SUCCESS The check passed or the port is assigned. + @retval EFI_INVALID_PARAMETER The non-zero station port is already used. + @retval EFI_OUT_OF_RESOURCES No port can be allocated. + +**/ +EFI_STATUS +TcpBind ( + IN TCP_ACCESS_POINT *TcpAp, + IN UINT8 IpVersion + ) +{ + BOOLEAN Cycle; + EFI_IP_ADDRESS Local; + UINT16 *Port; + UINT16 *RandomPort; + + if (IpVersion == IP_VERSION_4) { + IP4_COPY_ADDRESS (&Local, &TcpAp->Tcp4Ap.StationAddress); + Port = &TcpAp->Tcp4Ap.StationPort; + RandomPort = &mTcp4RandomPort; + } else { + IP6_COPY_ADDRESS (&Local, &TcpAp->Tcp6Ap.StationAddress); + Port = &TcpAp->Tcp6Ap.StationPort; + RandomPort = &mTcp6RandomPort; + } + + if (0 != *Port) { + // + // Check if a same endpoing is bound. + // + if (TcpFindTcbByPeer (&Local, *Port, IpVersion)) { + + return EFI_INVALID_PARAMETER; + } + } else { + // + // generate a random port + // + Cycle = FALSE; + + if (TCP_PORT_USER_RESERVED == *RandomPort) { + *RandomPort = TCP_PORT_KNOWN; + } + + (*RandomPort)++; + + while (TcpFindTcbByPeer (&Local, *RandomPort, IpVersion)) { + (*RandomPort)++; + + if (*RandomPort <= TCP_PORT_KNOWN) { + if (Cycle) { + DEBUG ( + (EFI_D_ERROR, + "TcpBind: no port can be allocated for this pcb\n") + ); + return EFI_OUT_OF_RESOURCES; + } + + *RandomPort = TCP_PORT_KNOWN + 1; + + Cycle = TRUE; + } + } + + *Port = *RandomPort; + } + + return EFI_SUCCESS; +} + +/** + Flush the Tcb add its associated protocols. + + @param[in, out] Tcb Pointer to the TCP_CB to be flushed. + +**/ +VOID +TcpFlushPcb ( + IN OUT TCP_CB *Tcb + ) +{ + SOCKET *Sock; + + IpIoConfigIp (Tcb->IpInfo, NULL); + + Sock = Tcb->Sk; + + if (SOCK_IS_CONFIGURED (Sock)) { + RemoveEntryList (&Tcb->List); + + if (Sock->DevicePath != NULL) { + // + // Uninstall the device path protocl. + // + gBS->UninstallProtocolInterface ( + Sock->SockHandle, + &gEfiDevicePathProtocolGuid, + Sock->DevicePath + ); + + FreePool (Sock->DevicePath); + Sock->DevicePath = NULL; + } + } + + NetbufFreeList (&Tcb->SndQue); + NetbufFreeList (&Tcb->RcvQue); + Tcb->State = TCP_CLOSED; + Tcb->RemoteIpZero = FALSE; +} + +/** + Attach a Pcb to the socket. + + @param[in] Sk Pointer to the socket of this TCP instance. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES Failed due to resource limits. + +**/ +EFI_STATUS +TcpAttachPcb ( + IN SOCKET *Sk + ) +{ + TCP_CB *Tcb; + TCP_PROTO_DATA *ProtoData; + IP_IO *IpIo; + EFI_STATUS Status; + VOID *Ip; + EFI_GUID *IpProtocolGuid; + + if (Sk->IpVersion == IP_VERSION_4) { + IpProtocolGuid = &gEfiIp4ProtocolGuid; + } else { + IpProtocolGuid = &gEfiIp6ProtocolGuid; + } + + Tcb = AllocateZeroPool (sizeof (TCP_CB)); + + if (Tcb == NULL) { + + DEBUG ((EFI_D_ERROR, "TcpConfigurePcb: failed to allocate a TCB\n")); + + return EFI_OUT_OF_RESOURCES; + } + + ProtoData = (TCP_PROTO_DATA *) Sk->ProtoReserved; + IpIo = ProtoData->TcpService->IpIo; + + // + // Create an IpInfo for this Tcb. + // + Tcb->IpInfo = IpIoAddIp (IpIo); + if (Tcb->IpInfo == NULL) { + + FreePool (Tcb); + return EFI_OUT_OF_RESOURCES; + } + + // + // Open the new created IP instance BY_CHILD. + // + Status = gBS->OpenProtocol ( + Tcb->IpInfo->ChildHandle, + IpProtocolGuid, + &Ip, + IpIo->Image, + Sk->SockHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + IpIoRemoveIp (IpIo, Tcb->IpInfo); + return Status; + } + + InitializeListHead (&Tcb->List); + InitializeListHead (&Tcb->SndQue); + InitializeListHead (&Tcb->RcvQue); + + Tcb->State = TCP_CLOSED; + Tcb->Sk = Sk; + ProtoData->TcpPcb = Tcb; + + return EFI_SUCCESS; +} + +/** + Detach the Pcb of the socket. + + @param[in, out] Sk Pointer to the socket of this TCP instance. + +**/ +VOID +TcpDetachPcb ( + IN OUT SOCKET *Sk + ) +{ + TCP_PROTO_DATA *ProtoData; + TCP_CB *Tcb; + + ProtoData = (TCP_PROTO_DATA *) Sk->ProtoReserved; + Tcb = ProtoData->TcpPcb; + + ASSERT (Tcb != NULL); + + TcpFlushPcb (Tcb); + + IpIoRemoveIp (ProtoData->TcpService->IpIo, Tcb->IpInfo); + + FreePool (Tcb); + + ProtoData->TcpPcb = NULL; +} + +/** + Configure the Pcb using CfgData. + + @param[in] Sk Pointer to the socket of this TCP instance. + @param[in] CfgData Pointer to the TCP configuration data. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER A same access point has been configured in + another TCP instance. + @retval EFI_OUT_OF_RESOURCES Failed due to resource limits. + +**/ +EFI_STATUS +TcpConfigurePcb ( + IN SOCKET *Sk, + IN TCP_CONFIG_DATA *CfgData + ) +{ + IP_IO_IP_CONFIG_DATA IpCfgData; + EFI_STATUS Status; + EFI_TCP4_OPTION *Option; + TCP_PROTO_DATA *TcpProto; + TCP_CB *Tcb; + TCP_ACCESS_POINT *TcpAp; + + ASSERT ((CfgData != NULL) && (Sk != NULL) && (Sk->SockHandle != NULL)); + + TcpProto = (TCP_PROTO_DATA *) Sk->ProtoReserved; + Tcb = TcpProto->TcpPcb; + + ASSERT (Tcb != NULL); + + if (Sk->IpVersion == IP_VERSION_4) { + // + // Add Ip for send pkt to the peer + // + CopyMem (&IpCfgData.Ip4CfgData, &mIp4IoDefaultIpConfigData, sizeof (EFI_IP4_CONFIG_DATA)); + IpCfgData.Ip4CfgData.DefaultProtocol = EFI_IP_PROTO_TCP; + IpCfgData.Ip4CfgData.TypeOfService = CfgData->Tcp4CfgData.TypeOfService; + IpCfgData.Ip4CfgData.TimeToLive = CfgData->Tcp4CfgData.TimeToLive; + IpCfgData.Ip4CfgData.UseDefaultAddress = CfgData->Tcp4CfgData.AccessPoint.UseDefaultAddress; + IP4_COPY_ADDRESS ( + &IpCfgData.Ip4CfgData.SubnetMask, + &CfgData->Tcp4CfgData.AccessPoint.SubnetMask + ); + IpCfgData.Ip4CfgData.ReceiveTimeout = (UINT32) (-1); + IP4_COPY_ADDRESS ( + &IpCfgData.Ip4CfgData.StationAddress, + &CfgData->Tcp4CfgData.AccessPoint.StationAddress + ); + + } else { + ASSERT (Sk->IpVersion == IP_VERSION_6); + + CopyMem (&IpCfgData.Ip6CfgData, &mIp6IoDefaultIpConfigData, sizeof (EFI_IP6_CONFIG_DATA)); + IpCfgData.Ip6CfgData.DefaultProtocol = EFI_IP_PROTO_TCP; + IpCfgData.Ip6CfgData.TrafficClass = CfgData->Tcp6CfgData.TrafficClass; + IpCfgData.Ip6CfgData.HopLimit = CfgData->Tcp6CfgData.HopLimit; + IpCfgData.Ip6CfgData.ReceiveTimeout = (UINT32) (-1); + IP6_COPY_ADDRESS ( + &IpCfgData.Ip6CfgData.StationAddress, + &CfgData->Tcp6CfgData.AccessPoint.StationAddress + ); + IP6_COPY_ADDRESS ( + &IpCfgData.Ip6CfgData.DestinationAddress, + &CfgData->Tcp6CfgData.AccessPoint.RemoteAddress + ); + } + + // + // Configure the IP instance this Tcb consumes. + // + Status = IpIoConfigIp (Tcb->IpInfo, &IpCfgData); + if (EFI_ERROR (Status)) { + goto OnExit; + } + + if (Sk->IpVersion == IP_VERSION_4) { + // + // Get the default address information if the instance is configured to use default address. + // + IP4_COPY_ADDRESS ( + &CfgData->Tcp4CfgData.AccessPoint.StationAddress, + &IpCfgData.Ip4CfgData.StationAddress + ); + IP4_COPY_ADDRESS ( + &CfgData->Tcp4CfgData.AccessPoint.SubnetMask, + &IpCfgData.Ip4CfgData.SubnetMask + ); + + TcpAp = (TCP_ACCESS_POINT *) &CfgData->Tcp4CfgData.AccessPoint; + } else { + IP6_COPY_ADDRESS ( + &CfgData->Tcp6CfgData.AccessPoint.StationAddress, + &IpCfgData.Ip6CfgData.StationAddress + ); + + TcpAp = (TCP_ACCESS_POINT *) &CfgData->Tcp6CfgData.AccessPoint; + } + + // + // check if we can bind this endpoint in CfgData + // + Status = TcpBind (TcpAp, Sk->IpVersion); + + if (EFI_ERROR (Status)) { + DEBUG ( + (EFI_D_ERROR, + "TcpConfigurePcb: Bind endpoint failed with %r\n", + Status) + ); + + goto OnExit; + } + + // + // Initalize the operating information in this Tcb + // + ASSERT (Tcb->State == TCP_CLOSED && + IsListEmpty (&Tcb->SndQue) && + IsListEmpty (&Tcb->RcvQue)); + + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_NO_KEEPALIVE); + Tcb->State = TCP_CLOSED; + + Tcb->SndMss = 536; + Tcb->RcvMss = TcpGetRcvMss (Sk); + + Tcb->SRtt = 0; + Tcb->Rto = 3 * TCP_TICK_HZ; + + Tcb->CWnd = Tcb->SndMss; + Tcb->Ssthresh = 0xffffffff; + + Tcb->CongestState = TCP_CONGEST_OPEN; + + Tcb->KeepAliveIdle = TCP_KEEPALIVE_IDLE_MIN; + Tcb->KeepAlivePeriod = TCP_KEEPALIVE_PERIOD; + Tcb->MaxKeepAlive = TCP_MAX_KEEPALIVE; + Tcb->MaxRexmit = TCP_MAX_LOSS; + Tcb->FinWait2Timeout = TCP_FIN_WAIT2_TIME; + Tcb->TimeWaitTimeout = TCP_TIME_WAIT_TIME; + Tcb->ConnectTimeout = TCP_CONNECT_TIME; + + if (Sk->IpVersion == IP_VERSION_4) { + // + // initialize Tcb in the light of CfgData + // + Tcb->Ttl = CfgData->Tcp4CfgData.TimeToLive; + Tcb->Tos = CfgData->Tcp4CfgData.TypeOfService; + + Tcb->UseDefaultAddr = CfgData->Tcp4CfgData.AccessPoint.UseDefaultAddress; + + CopyMem (&Tcb->LocalEnd.Ip, &CfgData->Tcp4CfgData.AccessPoint.StationAddress, sizeof (IP4_ADDR)); + Tcb->LocalEnd.Port = HTONS (CfgData->Tcp4CfgData.AccessPoint.StationPort); + IP4_COPY_ADDRESS (&Tcb->SubnetMask, &CfgData->Tcp4CfgData.AccessPoint.SubnetMask); + + CopyMem (&Tcb->RemoteEnd.Ip, &CfgData->Tcp4CfgData.AccessPoint.RemoteAddress, sizeof (IP4_ADDR)); + Tcb->RemoteEnd.Port = HTONS (CfgData->Tcp4CfgData.AccessPoint.RemotePort); + + Option = CfgData->Tcp4CfgData.ControlOption; + } else { + Tcb->Ttl = CfgData->Tcp6CfgData.HopLimit; + Tcb->Tos = CfgData->Tcp6CfgData.TrafficClass; + + IP6_COPY_ADDRESS (&Tcb->LocalEnd.Ip, &CfgData->Tcp6CfgData.AccessPoint.StationAddress); + Tcb->LocalEnd.Port = HTONS (CfgData->Tcp6CfgData.AccessPoint.StationPort); + + IP6_COPY_ADDRESS (&Tcb->RemoteEnd.Ip, &CfgData->Tcp6CfgData.AccessPoint.RemoteAddress); + Tcb->RemoteEnd.Port = HTONS (CfgData->Tcp6CfgData.AccessPoint.RemotePort); + + // + // Type EFI_TCP4_OPTION and EFI_TCP6_OPTION are the same. + // + Option = (EFI_TCP4_OPTION *) CfgData->Tcp6CfgData.ControlOption; + } + + if (Option != NULL) { + SET_RCV_BUFFSIZE ( + Sk, + (UINT32) (TCP_COMP_VAL ( + TCP_RCV_BUF_SIZE_MIN, + TCP_RCV_BUF_SIZE, + TCP_RCV_BUF_SIZE, + Option->ReceiveBufferSize + ) + ) + ); + SET_SND_BUFFSIZE ( + Sk, + (UINT32) (TCP_COMP_VAL ( + TCP_SND_BUF_SIZE_MIN, + TCP_SND_BUF_SIZE, + TCP_SND_BUF_SIZE, + Option->SendBufferSize + ) + ) + ); + + SET_BACKLOG ( + Sk, + (UINT32) (TCP_COMP_VAL ( + TCP_BACKLOG_MIN, + TCP_BACKLOG, + TCP_BACKLOG, + Option->MaxSynBackLog + ) + ) + ); + + Tcb->MaxRexmit = (UINT16) TCP_COMP_VAL ( + TCP_MAX_LOSS_MIN, + TCP_MAX_LOSS, + TCP_MAX_LOSS, + Option->DataRetries + ); + Tcb->FinWait2Timeout = TCP_COMP_VAL ( + TCP_FIN_WAIT2_TIME, + TCP_FIN_WAIT2_TIME_MAX, + TCP_FIN_WAIT2_TIME, + (UINT32) (Option->FinTimeout * TCP_TICK_HZ) + ); + + if (Option->TimeWaitTimeout != 0) { + Tcb->TimeWaitTimeout = TCP_COMP_VAL ( + TCP_TIME_WAIT_TIME, + TCP_TIME_WAIT_TIME_MAX, + TCP_TIME_WAIT_TIME, + (UINT32) (Option->TimeWaitTimeout * TCP_TICK_HZ) + ); + } else { + Tcb->TimeWaitTimeout = 0; + } + + if (Option->KeepAliveProbes != 0) { + TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_NO_KEEPALIVE); + + Tcb->MaxKeepAlive = (UINT8) TCP_COMP_VAL ( + TCP_MAX_KEEPALIVE_MIN, + TCP_MAX_KEEPALIVE, + TCP_MAX_KEEPALIVE, + Option->KeepAliveProbes + ); + Tcb->KeepAliveIdle = TCP_COMP_VAL ( + TCP_KEEPALIVE_IDLE_MIN, + TCP_KEEPALIVE_IDLE_MAX, + TCP_KEEPALIVE_IDLE_MIN, + (UINT32) (Option->KeepAliveTime * TCP_TICK_HZ) + ); + Tcb->KeepAlivePeriod = TCP_COMP_VAL ( + TCP_KEEPALIVE_PERIOD_MIN, + TCP_KEEPALIVE_PERIOD, + TCP_KEEPALIVE_PERIOD, + (UINT32) (Option->KeepAliveInterval * TCP_TICK_HZ) + ); + } + + Tcb->ConnectTimeout = TCP_COMP_VAL ( + TCP_CONNECT_TIME_MIN, + TCP_CONNECT_TIME, + TCP_CONNECT_TIME, + (UINT32) (Option->ConnectionTimeout * TCP_TICK_HZ) + ); + + if (!Option->EnableNagle) { + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_NO_NAGLE); + } + + if (!Option->EnableTimeStamp) { + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_NO_TS); + } + + if (!Option->EnableWindowScaling) { + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_NO_WS); + } + } + + // + // The socket is bound, the is + // determined, construct the IP device path and install it. + // + Status = TcpInstallDevicePath (Sk); + if (EFI_ERROR (Status)) { + goto OnExit; + } + + // + // update state of Tcb and socket + // + if (((Sk->IpVersion == IP_VERSION_4) && !CfgData->Tcp4CfgData.AccessPoint.ActiveFlag) || + ((Sk->IpVersion == IP_VERSION_6) && !CfgData->Tcp6CfgData.AccessPoint.ActiveFlag) + ) { + + TcpSetState (Tcb, TCP_LISTEN); + SockSetState (Sk, SO_LISTENING); + + Sk->ConfigureState = SO_CONFIGURED_PASSIVE; + } else { + + Sk->ConfigureState = SO_CONFIGURED_ACTIVE; + } + + if (Sk->IpVersion == IP_VERSION_6) { + Tcb->Tick = TCP6_REFRESH_NEIGHBOR_TICK; + + if (NetIp6IsUnspecifiedAddr (&Tcb->RemoteEnd.Ip.v6)) { + Tcb->RemoteIpZero = TRUE; + } + } + + TcpInsertTcb (Tcb); + +OnExit: + + return Status; +} + +/** + The procotol handler provided to the socket layer, which is used to + dispatch the socket level requests by calling the corresponding + TCP layer functions. + + @param[in] Sock Pointer to the socket of this TCP instance. + @param[in] Request The code of this operation request. + @param[in] Data Pointer to the operation specific data passed in + together with the operation request. This is an + optional parameter that may be NULL. + + @retval EFI_SUCCESS The socket request completed successfully. + @retval other The error status returned by the corresponding TCP + layer function. + +**/ +EFI_STATUS +TcpDispatcher ( + IN SOCKET *Sock, + IN UINT8 Request, + IN VOID *Data OPTIONAL + ) +{ + TCP_CB *Tcb; + TCP_PROTO_DATA *ProtoData; + + ProtoData = (TCP_PROTO_DATA *) Sock->ProtoReserved; + Tcb = ProtoData->TcpPcb; + + switch (Request) { + case SOCK_POLL: + if (Tcb->Sk->IpVersion == IP_VERSION_4) { + ProtoData->TcpService->IpIo->Ip.Ip4->Poll (ProtoData->TcpService->IpIo->Ip.Ip4); + } else { + ProtoData->TcpService->IpIo->Ip.Ip6->Poll (ProtoData->TcpService->IpIo->Ip.Ip6); + } + + break; + + case SOCK_CONSUMED: + // + // After user received data from socket buffer, socket will + // notify TCP using this message to give it a chance to send out + // window update information + // + ASSERT (Tcb != NULL); + TcpOnAppConsume (Tcb); + break; + + case SOCK_SND: + + ASSERT (Tcb != NULL); + TcpOnAppSend (Tcb); + break; + + case SOCK_CLOSE: + + TcpOnAppClose (Tcb); + + break; + + case SOCK_ABORT: + + TcpOnAppAbort (Tcb); + + break; + + case SOCK_SNDPUSH: + Tcb->SndPsh = TcpGetMaxSndNxt (Tcb) + GET_SND_DATASIZE (Tcb->Sk); + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_SND_PSH); + + break; + + case SOCK_SNDURG: + Tcb->SndUp = TcpGetMaxSndNxt (Tcb) + GET_SND_DATASIZE (Tcb->Sk) - 1; + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_SND_URG); + + break; + + case SOCK_CONNECT: + + TcpOnAppConnect (Tcb); + + break; + + case SOCK_ATTACH: + + return TcpAttachPcb (Sock); + + break; + + case SOCK_FLUSH: + + TcpFlushPcb (Tcb); + + break; + + case SOCK_DETACH: + + TcpDetachPcb (Sock); + + break; + + case SOCK_CONFIGURE: + + return TcpConfigurePcb ( + Sock, + (TCP_CONFIG_DATA *) Data + ); + + break; + + case SOCK_MODE: + + ASSERT ((Data != NULL) && (Tcb != NULL)); + + if (Tcb->Sk->IpVersion == IP_VERSION_4) { + + return Tcp4GetMode (Tcb, (TCP4_MODE_DATA *) Data); + } else { + + return Tcp6GetMode (Tcb, (TCP6_MODE_DATA *) Data); + } + + break; + + case SOCK_ROUTE: + + ASSERT ((Data != NULL) && (Tcb != NULL) && (Tcb->Sk->IpVersion == IP_VERSION_4)); + + return Tcp4Route (Tcb, (TCP4_ROUTE_INFO *) Data); + + default: + + return EFI_UNSUPPORTED; + } + + return EFI_SUCCESS; +} diff --git a/NetworkPkg/TcpDxe/TcpDriver.c b/NetworkPkg/TcpDxe/TcpDriver.c new file mode 100644 index 000000000..1817afa38 --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpDriver.c @@ -0,0 +1,995 @@ +/** @file + The driver binding and service binding protocol for the TCP driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TcpMain.h" + +UINT16 mTcp4RandomPort; +UINT16 mTcp6RandomPort; + +TCP_HEARTBEAT_TIMER mTcpTimer = { + NULL, + 0 +}; + +EFI_TCP4_PROTOCOL gTcp4ProtocolTemplate = { + Tcp4GetModeData, + Tcp4Configure, + Tcp4Routes, + Tcp4Connect, + Tcp4Accept, + Tcp4Transmit, + Tcp4Receive, + Tcp4Close, + Tcp4Cancel, + Tcp4Poll +}; + +EFI_TCP6_PROTOCOL gTcp6ProtocolTemplate = { + Tcp6GetModeData, + Tcp6Configure, + Tcp6Connect, + Tcp6Accept, + Tcp6Transmit, + Tcp6Receive, + Tcp6Close, + Tcp6Cancel, + Tcp6Poll +}; + +SOCK_INIT_DATA mTcpDefaultSockData = { + SockStream, + SO_CLOSED, + NULL, + TCP_BACKLOG, + TCP_SND_BUF_SIZE, + TCP_RCV_BUF_SIZE, + IP_VERSION_4, + NULL, + TcpCreateSocketCallback, + TcpDestroySocketCallback, + NULL, + NULL, + 0, + TcpDispatcher, + NULL, +}; + +EFI_DRIVER_BINDING_PROTOCOL gTcp4DriverBinding = { + Tcp4DriverBindingSupported, + Tcp4DriverBindingStart, + Tcp4DriverBindingStop, + 0xa, + NULL, + NULL +}; + +EFI_DRIVER_BINDING_PROTOCOL gTcp6DriverBinding = { + Tcp6DriverBindingSupported, + Tcp6DriverBindingStart, + Tcp6DriverBindingStop, + 0xa, + NULL, + NULL +}; + +EFI_SERVICE_BINDING_PROTOCOL gTcpServiceBinding = { + TcpServiceBindingCreateChild, + TcpServiceBindingDestroyChild +}; + + +/** + Create and start the heartbeat timer for the TCP driver. + + @retval EFI_SUCCESS The timer was successfully created and started. + @retval other The timer was not created. + +**/ +EFI_STATUS +TcpCreateTimer ( + VOID + ) +{ + EFI_STATUS Status; + + Status = EFI_SUCCESS; + + if (mTcpTimer.RefCnt == 0) { + + Status = gBS->CreateEvent ( + EVT_TIMER | EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + TcpTicking, + NULL, + &mTcpTimer.TimerEvent + ); + if (!EFI_ERROR (Status)) { + + Status = gBS->SetTimer ( + mTcpTimer.TimerEvent, + TimerPeriodic, + (UINT64) (TICKS_PER_SECOND / TCP_TICK_HZ) + ); + } + } + + if (!EFI_ERROR (Status)) { + + mTcpTimer.RefCnt++; + } + + return Status; +} + +/** + Stop and destroy the heartbeat timer for TCP driver. + +**/ +VOID +TcpDestroyTimer ( + VOID + ) +{ + ASSERT (mTcpTimer.RefCnt > 0); + + mTcpTimer.RefCnt--; + + if (mTcpTimer.RefCnt > 0) { + return; + } + + gBS->SetTimer (mTcpTimer.TimerEvent, TimerCancel, 0); + gBS->CloseEvent (mTcpTimer.TimerEvent); + mTcpTimer.TimerEvent = NULL; +} + +/** + The entry point for Tcp driver, which is used to install Tcp driver on the ImageHandle. + + @param[in] ImageHandle The firmware allocated handle for this driver image. + @param[in] SystemTable Pointer to the EFI system table. + + @retval EFI_SUCCESS The driver loaded. + @retval other The driver did not load. + +**/ +EFI_STATUS +EFIAPI +TcpDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + UINT32 Seed; + + // + // Install the TCP Driver Binding Protocol + // + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gTcp4DriverBinding, + ImageHandle, + &gTcpComponentName, + &gTcpComponentName2 + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Install the TCP Driver Binding Protocol + // + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gTcp6DriverBinding, + NULL, + &gTcpComponentName, + &gTcpComponentName2 + ); + if (EFI_ERROR (Status)) { + EfiLibUninstallDriverBindingComponentName2 ( + &gTcp4DriverBinding, + &gTcpComponentName, + &gTcpComponentName2 + ); + return Status; + } + + // + // Initialize ISS and random port. + // + Seed = NetRandomInitSeed (); + mTcpGlobalIss = NET_RANDOM (Seed) % mTcpGlobalIss; + mTcp4RandomPort = (UINT16) (TCP_PORT_KNOWN + (NET_RANDOM (Seed) % TCP_PORT_KNOWN)); + mTcp6RandomPort = mTcp4RandomPort; + + return EFI_SUCCESS; +} + +/** + Create a new TCP4 or TCP6 driver service binding protocol + + @param[in] Controller Controller handle of device to bind driver to. + @param[in] Image The TCP driver's image handle. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources. + @retval EFI_SUCCESS A new IP6 service binding private was created. + +**/ +EFI_STATUS +TcpCreateService ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE Image, + IN UINT8 IpVersion + ) +{ + EFI_STATUS Status; + EFI_GUID *IpServiceBindingGuid; + EFI_GUID *TcpServiceBindingGuid; + TCP_SERVICE_DATA *TcpServiceData; + IP_IO_OPEN_DATA OpenData; + + if (IpVersion == IP_VERSION_4) { + IpServiceBindingGuid = &gEfiIp4ServiceBindingProtocolGuid; + TcpServiceBindingGuid = &gEfiTcp4ServiceBindingProtocolGuid; + } else { + IpServiceBindingGuid = &gEfiIp6ServiceBindingProtocolGuid; + TcpServiceBindingGuid = &gEfiTcp6ServiceBindingProtocolGuid; + } + + Status = gBS->OpenProtocol ( + Controller, + TcpServiceBindingGuid, + NULL, + Image, + Controller, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + Status = gBS->OpenProtocol ( + Controller, + IpServiceBindingGuid, + NULL, + Image, + Controller, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + // + // Create the TCP service data. + // + TcpServiceData = AllocateZeroPool (sizeof (TCP_SERVICE_DATA)); + if (TcpServiceData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + TcpServiceData->Signature = TCP_DRIVER_SIGNATURE; + TcpServiceData->ControllerHandle = Controller; + TcpServiceData->DriverBindingHandle = Image; + TcpServiceData->IpVersion = IpVersion; + CopyMem ( + &TcpServiceData->ServiceBinding, + &gTcpServiceBinding, + sizeof (EFI_SERVICE_BINDING_PROTOCOL) + ); + + TcpServiceData->IpIo = IpIoCreate (Image, Controller, IpVersion); + if (TcpServiceData->IpIo == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + + InitializeListHead (&TcpServiceData->SocketList); + ZeroMem (&OpenData, sizeof (IP_IO_OPEN_DATA)); + + if (IpVersion == IP_VERSION_4) { + CopyMem ( + &OpenData.IpConfigData.Ip4CfgData, + &mIp4IoDefaultIpConfigData, + sizeof (EFI_IP4_CONFIG_DATA) + ); + OpenData.IpConfigData.Ip4CfgData.DefaultProtocol = EFI_IP_PROTO_TCP; + } else { + CopyMem ( + &OpenData.IpConfigData.Ip6CfgData, + &mIp6IoDefaultIpConfigData, + sizeof (EFI_IP6_CONFIG_DATA) + ); + OpenData.IpConfigData.Ip6CfgData.DefaultProtocol = EFI_IP_PROTO_TCP; + } + + OpenData.PktRcvdNotify = TcpRxCallback; + Status = IpIoOpen (TcpServiceData->IpIo, &OpenData); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = TcpCreateTimer (); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->InstallMultipleProtocolInterfaces ( + &Controller, + TcpServiceBindingGuid, + &TcpServiceData->ServiceBinding, + NULL + ); + if (EFI_ERROR (Status)) { + TcpDestroyTimer (); + + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + + if (TcpServiceData->IpIo != NULL) { + IpIoDestroy (TcpServiceData->IpIo); + TcpServiceData->IpIo = NULL; + } + + FreePool (TcpServiceData); + + return Status; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +TcpDestroyChildEntryInHandleBuffer ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + SOCKET *Sock; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + Sock = NET_LIST_USER_STRUCT_S (Entry, SOCKET, Link, SOCK_SIGNATURE); + ServiceBinding = ((TCP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding; + NumberOfChildren = ((TCP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren; + ChildHandleBuffer = ((TCP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer; + + if (!NetIsInHandleBuffer (Sock->SockHandle, NumberOfChildren, ChildHandleBuffer)) { + return EFI_SUCCESS; + } + + return ServiceBinding->DestroyChild (ServiceBinding, Sock->SockHandle); +} + +/** + Destroy a TCP6 or TCP4 service binding instance. It will release all + the resources allocated by the instance. + + @param[in] Controller Controller handle of device to bind driver to. + @param[in] ImageHandle The TCP driver's image handle. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number + of children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6 + + @retval EFI_SUCCESS The resources used by the instance were cleaned up. + @retval Others Failed to clean up some of the resources. + +**/ +EFI_STATUS +TcpDestroyService ( + IN EFI_HANDLE Controller, + IN EFI_HANDLE ImageHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer, OPTIONAL + IN UINT8 IpVersion + ) +{ + EFI_HANDLE NicHandle; + EFI_GUID *IpProtocolGuid; + EFI_GUID *ServiceBindingGuid; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + TCP_SERVICE_DATA *TcpServiceData; + EFI_STATUS Status; + LIST_ENTRY *List; + TCP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context; + + ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6)); + + if (IpVersion == IP_VERSION_4) { + IpProtocolGuid = &gEfiIp4ProtocolGuid; + ServiceBindingGuid = &gEfiTcp4ServiceBindingProtocolGuid; + } else { + IpProtocolGuid = &gEfiIp6ProtocolGuid; + ServiceBindingGuid = &gEfiTcp6ServiceBindingProtocolGuid; + } + + NicHandle = NetLibGetNicHandle (Controller, IpProtocolGuid); + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + Status = gBS->OpenProtocol ( + NicHandle, + ServiceBindingGuid, + (VOID **) &ServiceBinding, + ImageHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + TcpServiceData = TCP_SERVICE_FROM_THIS (ServiceBinding); + + if (NumberOfChildren != 0) { + List = &TcpServiceData->SocketList; + Context.ServiceBinding = ServiceBinding; + Context.NumberOfChildren = NumberOfChildren; + Context.ChildHandleBuffer = ChildHandleBuffer; + Status = NetDestroyLinkList ( + List, + TcpDestroyChildEntryInHandleBuffer, + &Context, + NULL + ); + } else if (IsListEmpty (&TcpServiceData->SocketList)) { + // + // Uninstall TCP servicebinding protocol + // + gBS->UninstallMultipleProtocolInterfaces ( + NicHandle, + ServiceBindingGuid, + ServiceBinding, + NULL + ); + + // + // Destroy the IpIO consumed by TCP driver + // + IpIoDestroy (TcpServiceData->IpIo); + TcpServiceData->IpIo = NULL; + + // + // Destroy the heartbeat timer. + // + TcpDestroyTimer (); + + // + // Release the TCP service data + // + FreePool (TcpServiceData); + + Status = EFI_SUCCESS; + } + + return Status; +} + +/** + Test to see if this driver supports ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Tcp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // Test for the Tcp4ServiceBinding Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiTcp4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + // + // Test for the Ip4ServiceBinding Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiIp4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + return Status; +} + +/** + Start this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS The driver is added to ControllerHandle. + @retval EFI_OUT_OF_RESOURCES There are not enough resources to start the + driver. + @retval other The driver cannot be added to ControllerHandle. + +**/ +EFI_STATUS +EFIAPI +Tcp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + Status = TcpCreateService (ControllerHandle, This->DriverBindingHandle, IP_VERSION_4); + if ((Status == EFI_ALREADY_STARTED) || (Status == EFI_UNSUPPORTED)) { + Status = EFI_SUCCESS; + } + + return Status; +} + +/** + Stop this driver on ControllerHandle. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +Tcp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + return TcpDestroyService ( + ControllerHandle, + This->DriverBindingHandle, + NumberOfChildren, + ChildHandleBuffer, + IP_VERSION_4 + ); +} + +/** + Test to see if this driver supports ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Tcp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // Test for the Tcp6ServiceBinding Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiTcp6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + // + // Test for the Ip6ServiceBinding Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiIp6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + return Status; +} + +/** + Start this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS The driver is added to ControllerHandle. + @retval EFI_OUT_OF_RESOURCES There are not enough resources to start the + driver. + @retval other The driver cannot be added to ControllerHandle. + +**/ +EFI_STATUS +EFIAPI +Tcp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + Status = TcpCreateService (ControllerHandle, This->DriverBindingHandle, IP_VERSION_6); + if ((Status == EFI_ALREADY_STARTED) || (Status == EFI_UNSUPPORTED)) { + Status = EFI_SUCCESS; + } + + return Status; +} + +/** + Stop this driver on ControllerHandle. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +Tcp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + return TcpDestroyService ( + ControllerHandle, + This->DriverBindingHandle, + NumberOfChildren, + ChildHandleBuffer, + IP_VERSION_6 + ); +} + +/** + The Callback funtion called after the TCP socket was created. + + @param[in] This Pointer to the socket just created + @param[in] Context Context of the socket + + @retval EFI_SUCCESS This protocol installed successfully. + @retval other An error occured. + +**/ +EFI_STATUS +TcpCreateSocketCallback ( + IN SOCKET *This, + IN VOID *Context + ) +{ + EFI_STATUS Status; + TCP_SERVICE_DATA *TcpServiceData; + EFI_GUID *IpProtocolGuid; + VOID *Ip; + + if (This->IpVersion == IP_VERSION_4) { + IpProtocolGuid = &gEfiIp4ProtocolGuid; + } else { + IpProtocolGuid = &gEfiIp6ProtocolGuid; + } + + TcpServiceData = ((TCP_PROTO_DATA *) This->ProtoReserved)->TcpService; + + // + // Open the default IP protocol of IP_IO BY_DRIVER. + // + Status = gBS->OpenProtocol ( + TcpServiceData->IpIo->ChildHandle, + IpProtocolGuid, + &Ip, + TcpServiceData->DriverBindingHandle, + This->SockHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Open the device path on the handle where service binding resides on. + // + Status = gBS->OpenProtocol ( + TcpServiceData->ControllerHandle, + &gEfiDevicePathProtocolGuid, + (VOID **) &This->ParentDevicePath, + TcpServiceData->DriverBindingHandle, + This->SockHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + gBS->CloseProtocol ( + TcpServiceData->IpIo->ChildHandle, + IpProtocolGuid, + TcpServiceData->DriverBindingHandle, + This->SockHandle + ); + } else { + // + // Insert this socket into the SocketList. + // + InsertTailList (&TcpServiceData->SocketList, &This->Link); + } + + return Status; +} + +/** + The callback function called before the TCP socket was to be destroyed. + + @param[in] This The TCP socket to be destroyed. + @param[in] Context The context of the socket. + +**/ +VOID +TcpDestroySocketCallback ( + IN SOCKET *This, + IN VOID *Context + ) +{ + TCP_SERVICE_DATA *TcpServiceData; + EFI_GUID *IpProtocolGuid; + + if (This->IpVersion == IP_VERSION_4) { + IpProtocolGuid = &gEfiIp4ProtocolGuid; + } else { + IpProtocolGuid = &gEfiIp6ProtocolGuid; + } + + TcpServiceData = ((TCP_PROTO_DATA *) This->ProtoReserved)->TcpService; + + // + // Remove this node from the list. + // + RemoveEntryList (&This->Link); + + // + // Close the IP protocol. + // + gBS->CloseProtocol ( + TcpServiceData->IpIo->ChildHandle, + IpProtocolGuid, + TcpServiceData->DriverBindingHandle, + This->SockHandle + ); +} + +/** + Creates a child handle with a set of TCP services. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in, out] ChildHandle Pointer to the handle of the child to create. + If it is NULL, then a new handle is created. + If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +TcpServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ) +{ + SOCKET *Sock; + TCP_SERVICE_DATA *TcpServiceData; + TCP_PROTO_DATA TcpProto; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if (NULL == This || NULL == ChildHandle) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Status = EFI_SUCCESS; + TcpServiceData = TCP_SERVICE_FROM_THIS (This); + TcpProto.TcpService = TcpServiceData; + TcpProto.TcpPcb = NULL; + + // + // Create a tcp instance with defualt Tcp default + // sock init data and TcpProto + // + mTcpDefaultSockData.ProtoData = &TcpProto; + mTcpDefaultSockData.DataSize = sizeof (TCP_PROTO_DATA); + mTcpDefaultSockData.DriverBinding = TcpServiceData->DriverBindingHandle; + mTcpDefaultSockData.IpVersion = TcpServiceData->IpVersion; + + if (TcpServiceData->IpVersion == IP_VERSION_4) { + mTcpDefaultSockData.Protocol = &gTcp4ProtocolTemplate; + } else { + mTcpDefaultSockData.Protocol = &gTcp6ProtocolTemplate; + } + + Sock = SockCreateChild (&mTcpDefaultSockData); + if (NULL == Sock) { + DEBUG ( + (EFI_D_ERROR, + "TcpDriverBindingCreateChild: No resource to create a Tcp Child\n") + ); + + Status = EFI_OUT_OF_RESOURCES; + } else { + *ChildHandle = Sock->SockHandle; + } + + mTcpDefaultSockData.ProtoData = NULL; + + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Destroys a child handle with a set of TCP services. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to be destroyed. + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +TcpServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + EFI_STATUS Status; + VOID *Tcp; + SOCKET *Sock; + + if (NULL == This || NULL == ChildHandle) { + return EFI_INVALID_PARAMETER; + } + + // + // retrieve the Tcp4 protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiTcp4ProtocolGuid, + &Tcp, + gTcp4DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + // + // No Tcp4, try the Tcp6 protocol + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiTcp6ProtocolGuid, + &Tcp, + gTcp6DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + Status = EFI_UNSUPPORTED; + } + } + + if (!EFI_ERROR (Status)) { + // + // destroy this sock and related Tcp protocol control + // block + // + Sock = SOCK_FROM_THIS (Tcp); + + SockDestroyChild (Sock); + } + + return Status; +} diff --git a/NetworkPkg/TcpDxe/TcpDriver.h b/NetworkPkg/TcpDxe/TcpDriver.h new file mode 100644 index 000000000..1ac412be6 --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpDriver.h @@ -0,0 +1,290 @@ +/** @file + The prototype of driver binding and service binding protocol for TCP driver. + + Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _TCP_DRIVER_H_ +#define _TCP_DRIVER_H_ + +#define TCP_DRIVER_SIGNATURE SIGNATURE_32 ('T', 'C', 'P', 'D') + +#define TCP_PORT_KNOWN 1024 +#define TCP_PORT_USER_RESERVED 65535 + +typedef struct _TCP_HEARTBEAT_TIMER { + EFI_EVENT TimerEvent; + INTN RefCnt; +} TCP_HEARTBEAT_TIMER; + +typedef struct _TCP_SERVICE_DATA { + UINT32 Signature; + EFI_HANDLE ControllerHandle; + EFI_HANDLE DriverBindingHandle; + UINT8 IpVersion; + IP_IO *IpIo; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + LIST_ENTRY SocketList; +} TCP_SERVICE_DATA; + +typedef struct _TCP_PROTO_DATA { + TCP_SERVICE_DATA *TcpService; + TCP_CB *TcpPcb; +} TCP_PROTO_DATA; + +#define TCP_SERVICE_FROM_THIS(a) \ + CR ( \ + (a), \ + TCP_SERVICE_DATA, \ + ServiceBinding, \ + TCP_DRIVER_SIGNATURE \ + ) + +// +// Function prototype for the driver's entry point +// + +/** + The entry point for Tcp driver, used to install Tcp driver on the ImageHandle. + + @param[in] ImageHandle The firmware allocated handle for this driver image. + @param[in] SystemTable Pointer to the EFI system table. + + @retval EFI_SUCCESS The driver loaded. + @retval other The driver did not load. + +**/ +EFI_STATUS +EFIAPI +TcpDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ); + +// +// Function prototypes for the Driver Binding Protocol +// + +/** + Test to see if this driver supports ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of the device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Tcp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS The driver was added to ControllerHandle. + @retval EFI_OUT_OF_RESOURCES There are not enough resources to start the + driver. + @retval other The driver cannot be added to ControllerHandle. + +**/ +EFI_STATUS +EFIAPI +Tcp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +Tcp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + Test to see if this driver supports ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of the device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a specific + child device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Tcp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS The driver was added to ControllerHandle. + @retval EFI_OUT_OF_RESOURCES There are not enough resources to start the + driver. + @retval other The driver cannot be added to ControllerHandle. + +**/ +EFI_STATUS +EFIAPI +Tcp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +Tcp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + The Callback funtion called after the TCP socket is created. + + @param[in] This Pointer to the socket just created. + @param[in] Context The context of the socket. + + @retval EFI_SUCCESS This protocol is installed successfully. + @retval other An error occured. + +**/ +EFI_STATUS +TcpCreateSocketCallback ( + IN SOCKET *This, + IN VOID *Context + ); + +/** + The callback function called before the TCP socket is to be destroyed. + + @param[in] This The TCP socket to be destroyed. + @param[in] Context The context of the socket. + +**/ +VOID +TcpDestroySocketCallback ( + IN SOCKET *This, + IN VOID *Context + ); + +// +// Function prototypes for the ServiceBinding Protocol +// + +/** + Creates a child handle with a set of TCP services. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in, out] ChildHandle Pointer to the handle of the child to create. + If it is NULL, then a new handle is created. + If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +TcpServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a set of TCP services. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER The child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +TcpServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +#endif diff --git a/NetworkPkg/TcpDxe/TcpDxe.inf b/NetworkPkg/TcpDxe/TcpDxe.inf new file mode 100644 index 000000000..4865acb0d --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpDxe.inf @@ -0,0 +1,87 @@ +## @file +# TCPv4 I/O and TCPv6 I/O services. +# +# This module provides EFI TCPv4 Protocol and EFI TCPv6 Protocol to send and receive data stream. +# It might provide TCPv4 Protocol or TCPv6 Protocol or both of them that depends on which network +# stack has been loaded in system. This driver supports both IPv4 and IPv6 network stack. +# +# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = TcpDxe + FILE_GUID = 1A7E4468-2F55-4a56-903C-01265EB7622B + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = TcpDriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload + MODULE_UNI_FILE = TcpDxe.uni + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources] + TcpDriver.c + SockImpl.c + SockInterface.c + TcpDispatcher.c + TcpOutput.c + TcpMain.c + SockImpl.h + TcpMisc.c + TcpProto.h + TcpOption.c + TcpInput.c + TcpFunc.h + TcpOption.h + TcpTimer.c + TcpMain.h + Socket.h + ComponentName.c + TcpIo.c + TcpDriver.h + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + BaseLib + BaseMemoryLib + DevicePathLib + DebugLib + MemoryAllocationLib + UefiLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiRuntimeServicesTableLib + DpcLib + NetLib + IpIoLib + + +[Protocols] + ## SOMETIMES_CONSUMES + ## SOMETIMES_PRODUCES + gEfiDevicePathProtocolGuid + gEfiIp4ProtocolGuid ## TO_START + gEfiIp4ServiceBindingProtocolGuid ## TO_START + gEfiTcp4ProtocolGuid ## BY_START + gEfiTcp4ServiceBindingProtocolGuid ## BY_START + gEfiIp6ProtocolGuid ## TO_START + gEfiIp6ServiceBindingProtocolGuid ## TO_START + gEfiTcp6ProtocolGuid ## BY_START + gEfiTcp6ServiceBindingProtocolGuid ## BY_START + +[UserExtensions.TianoCore."ExtraFiles"] + TcpDxeExtra.uni diff --git a/NetworkPkg/TcpDxe/TcpDxe.uni b/NetworkPkg/TcpDxe/TcpDxe.uni new file mode 100644 index 000000000..9b9bf0293 --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpDxe.uni @@ -0,0 +1,18 @@ +// /** @file +// TCPv4 I/O and TCPv6 I/O services. +// +// This module provides EFI TCPv4 Protocol and EFI TCPv6 Protocol to send and receive data stream. +// It might provide TCPv4 Protocol or TCPv6 Protocol or both of them that depends on +// which network stack has been loaded in system. +// +// Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "TCPv4 I/O and TCPv6 I/O services" + +#string STR_MODULE_DESCRIPTION #language en-US "This module provides EFI TCPv4 Protocol and EFI TCPv6 Protocol to send and receive data stream. It might provide TCPv4 Protocol or TCPv6 Protocol or both of them that depends on which network stack has been loaded in system." + diff --git a/NetworkPkg/TcpDxe/TcpDxeExtra.uni b/NetworkPkg/TcpDxe/TcpDxeExtra.uni new file mode 100644 index 000000000..65a03c598 --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// TcpDxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"TCP DXE" + + diff --git a/NetworkPkg/TcpDxe/TcpFunc.h b/NetworkPkg/TcpDxe/TcpFunc.h new file mode 100644 index 000000000..5b8043e8a --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpFunc.h @@ -0,0 +1,693 @@ +/** @file + Declaration of external functions shared in TCP driver. + + Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _TCP_FUNC_H_ +#define _TCP_FUNC_H_ + +#include "TcpOption.h" + +#define TCP_COMP_VAL(Min, Max, Default, Val) \ + ((((Val) <= (Max)) && ((Val) >= (Min))) ? (Val) : (Default)) + +/** + Timeout handler prototype. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +typedef +VOID +(*TCP_TIMER_HANDLER) ( + IN OUT TCP_CB *Tcb + ); + +// +// Functions in TcpMisc.c +// + +/** + Initialize the Tcb locally related members. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpInitTcbLocal ( + IN OUT TCP_CB *Tcb + ); + +/** + Initialize the peer related members. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Seg Pointer to the segment that contains the peer's intial information. + @param[in] Opt Pointer to the options announced by the peer. + +**/ +VOID +TcpInitTcbPeer ( + IN OUT TCP_CB *Tcb, + IN TCP_SEG *Seg, + IN TCP_OPTION *Opt + ); + +/** + Try to find one Tcb whose equals to . + + @param[in] Addr Pointer to the IP address needs to match. + @param[in] Port The port number needs to match. + @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack. + IP_VERSION_6 indicates TCP is running on IP6 stack. + + + @retval TRUE The Tcb which matches the pairs exists. + @retval FALSE Otherwise + +**/ +BOOLEAN +TcpFindTcbByPeer ( + IN EFI_IP_ADDRESS *Addr, + IN TCP_PORTNO Port, + IN UINT8 Version + ); + +/** + Locate the TCP_CB related to the socket pair. + + @param[in] LocalPort The local port number. + @param[in] LocalIp The local IP address. + @param[in] RemotePort The remote port number. + @param[in] RemoteIp The remote IP address. + @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack, + IP_VERSION_6 indicates TCP is running on IP6 stack. + @param[in] Syn If TRUE, the listen sockets are searched. + + @return Pointer to the related TCP_CB. If NULL, no match is found. + +**/ +TCP_CB * +TcpLocateTcb ( + IN TCP_PORTNO LocalPort, + IN EFI_IP_ADDRESS *LocalIp, + IN TCP_PORTNO RemotePort, + IN EFI_IP_ADDRESS *RemoteIp, + IN UINT8 Version, + IN BOOLEAN Syn + ); + +/** + Insert a Tcb into the proper queue. + + @param[in] Tcb Pointer to the TCP_CB to be inserted. + + @retval 0 The Tcb was inserted successfully. + @retval -1 An error condition occurred. + +**/ +INTN +TcpInsertTcb ( + IN TCP_CB *Tcb + ); + +/** + Clone a TCP_CB from Tcb. + + @param[in] Tcb Pointer to the TCP_CB to be cloned. + + @return Pointer to the new cloned TCP_CB. If NULL, an error condition occurred. + +**/ +TCP_CB * +TcpCloneTcb ( + IN TCP_CB *Tcb + ); + +/** + Compute an ISS to be used by a new connection. + + @return The result ISS. + +**/ +TCP_SEQNO +TcpGetIss ( + VOID + ); + +/** + Get the local mss. + + @param[in] Sock Pointer to the socket to get mss. + + @return The mss size. + +**/ +UINT16 +TcpGetRcvMss ( + IN SOCKET *Sock + ); + +/** + Set the Tcb's state. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] State The state to be set. + +**/ +VOID +TcpSetState ( + IN TCP_CB *Tcb, + IN UINT8 State + ); + +/** + Compute the TCP segment's checksum. + + @param[in] Nbuf Pointer to the buffer that contains the TCP segment. + @param[in] HeadSum The checksum value of the fixed part of pseudo header. + + @return The checksum value. + +**/ +UINT16 +TcpChecksum ( + IN NET_BUF *Nbuf, + IN UINT16 HeadSum + ); + +/** + Translate the information from the head of the received TCP + segment Nbuf contains, and fill it into a TCP_SEG structure. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in, out] Nbuf Pointer to the buffer contains the TCP segment. + + @return Pointer to the TCP_SEG that contains the translated TCP head information. + +**/ +TCP_SEG * +TcpFormatNetbuf ( + IN TCP_CB *Tcb, + IN OUT NET_BUF *Nbuf + ); + +/** + Initialize an active connection, + + @param[in, out] Tcb Pointer to the TCP_CB that wants to initiate a + connection. + +**/ +VOID +TcpOnAppConnect ( + IN OUT TCP_CB *Tcb + ); + +/** + Application has consumed some data, check whether + to send a window update ack or a delayed ack. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpOnAppConsume ( + IN TCP_CB *Tcb + ); + +/** + Initiate the connection close procedure, called when + applications want to close the connection. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpOnAppClose ( + IN OUT TCP_CB *Tcb + ); + +/** + Check whether the application's newly delivered data can be sent out. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + + @retval 0 The data has been sent out successfully. + @retval -1 The Tcb is not in a state that data is permitted to + be sent out. + +**/ +INTN +TcpOnAppSend ( + IN OUT TCP_CB *Tcb + ); + +/** + Abort the connection by sending a reset segment: called + when the application wants to abort the connection. + + @param[in] Tcb Pointer to the TCP_CB of the TCP instance. + +**/ +VOID +TcpOnAppAbort ( + IN TCP_CB *Tcb + ); + +/** + Reset the connection related with Tcb. + + @param[in] Tcb Pointer to the TCP_CB of the connection to be reset. + +**/ +VOID +TcpResetConnection ( + IN TCP_CB *Tcb + ); + +/** + Install the device path protocol on the TCP instance. + + @param[in] Sock Pointer to the socket representing the TCP instance. + + @retval EFI_SUCCESS The device path protocol installed. + @retval other Failed to install the device path protocol. + +**/ +EFI_STATUS +TcpInstallDevicePath ( + IN SOCKET *Sock + ); + + +// +// Functions in TcpOutput.c +// + +/** + Compute the sequence space left in the old receive window. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + + @return The sequence space left in the old receive window. + +**/ +UINT32 +TcpRcvWinOld ( + IN TCP_CB *Tcb + ); + +/** + Compute the current receive window. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + + @return The size of the current receive window, in bytes. + +**/ +UINT32 +TcpRcvWinNow ( + IN TCP_CB *Tcb + ); + +/** + Get the maximum SndNxt. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + + @return The sequence number of the maximum SndNxt. + +**/ +TCP_SEQNO +TcpGetMaxSndNxt ( + IN TCP_CB *Tcb + ); + +/** + Compute how much data to send. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Force If TRUE, ignore the sender's SWS avoidance algorithm + and send out data by force. + + @return The length of the data that can be sent. If 0, no data can be sent. + +**/ +UINT32 +TcpDataToSend ( + IN TCP_CB *Tcb, + IN INTN Force + ); + +/** + Retransmit the segment from sequence Seq. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Seq The sequence number of the segment to be retransmitted. + + @retval 0 The retransmission succeeded. + @retval -1 An error condition occurred. + +**/ +INTN +TcpRetransmit ( + IN TCP_CB *Tcb, + IN TCP_SEQNO Seq + ); + +/** + Check whether to send data/SYN/FIN and piggyback an ACK. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Force If TRUE, ignore the sender's SWS avoidance algorithm + and send out data by force. + + @return The number of bytes sent. + +**/ +INTN +TcpToSendData ( + IN OUT TCP_CB *Tcb, + IN INTN Force + ); + +/** + Check whether to send an ACK or delayed ACK. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpToSendAck ( + IN OUT TCP_CB *Tcb + ); + +/** + Send an ACK immediately. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpSendAck ( + IN OUT TCP_CB *Tcb + ); + +/** + Send a zero probe segment. It can be used by keepalive and zero window probe. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + + @retval 0 The zero probe segment was sent out successfully. + @retval other An error condition occurred. + +**/ +INTN +TcpSendZeroProbe ( + IN OUT TCP_CB *Tcb + ); + +/** + Send a RESET segment in response to the segment received. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance, may be NULL. + @param[in] Head TCP header of the segment that triggers the reset. + @param[in] Len Length of the segment that triggers the reset. + @param[in] Local Local IP address. + @param[in] Remote Remote peer's IP address. + @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack, + IP_VERSION_6 indicates TCP is running on IP6 stack. + + @retval 0 A reset is sent or no need to send it. + @retval -1 No reset is sent. + +**/ +INTN +TcpSendReset ( + IN TCP_CB *Tcb, + IN TCP_HEAD *Head, + IN INT32 Len, + IN EFI_IP_ADDRESS *Local, + IN EFI_IP_ADDRESS *Remote, + IN UINT8 Version + ); + +/** + Verify that the segment is in good shape. + + @param[in] Nbuf Buffer that contains the segment to be checked. + + @retval 0 The segment is broken. + @retval 1 The segment is in good shape. + +**/ +INTN +TcpVerifySegment ( + IN NET_BUF *Nbuf + ); + +// +// Functions from TcpInput.c +// + +/** + Process the received ICMP error messages for TCP. + + @param[in] Nbuf Buffer that contains part of the TCP segment without IP header + truncated from the ICMP error packet. + @param[in] IcmpErr The ICMP error code interpreted from an ICMP error packet. + @param[in] Src Source address of the ICMP error message. + @param[in] Dst Destination address of the ICMP error message. + @param[in] Version IP_VERSION_4 indicates IP4 stack, IP_VERSION_6 indicates + IP6 stack. + +**/ +VOID +TcpIcmpInput ( + IN NET_BUF *Nbuf, + IN UINT8 IcmpErr, + IN EFI_IP_ADDRESS *Src, + IN EFI_IP_ADDRESS *Dst, + IN UINT8 Version + ); + +/** + Process the received TCP segments. + + @param[in] Nbuf Buffer that contains received TCP segment without an IP header. + @param[in] Src Source address of the segment, or the peer's IP address. + @param[in] Dst Destination address of the segment, or the local end's IP + address. + @param[in] Version IP_VERSION_4 indicates IP4 stack, IP_VERSION_6 indicates + IP6 stack. + + @retval 0 The segment processed successfully. It is either accepted or + discarded. But no connection is reset by the segment. + @retval -1 A connection is reset by the segment. + +**/ +INTN +TcpInput ( + IN NET_BUF *Nbuf, + IN EFI_IP_ADDRESS *Src, + IN EFI_IP_ADDRESS *Dst, + IN UINT8 Version + ); + +// +// Functions in TcpTimer.c +// + +/** + Close the TCP connection. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpClose ( + IN OUT TCP_CB *Tcb + ); + +/** + Heart beat timer handler, queues the DPC at TPL_CALLBACK. + + @param[in] Event Timer event signaled, ignored. + @param[in] Context Context of the timer event, ignored. + +**/ +VOID +EFIAPI +TcpTicking ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Enable a TCP timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Timer The index of the timer to be enabled. + @param[in] TimeOut The timeout value of this timer. + +**/ +VOID +TcpSetTimer ( + IN OUT TCP_CB *Tcb, + IN UINT16 Timer, + IN UINT32 TimeOut + ); + +/** + Clear one TCP timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Timer The index of the timer to be cleared. + +**/ +VOID +TcpClearTimer ( + IN OUT TCP_CB *Tcb, + IN UINT16 Timer + ); + +/** + Clear all TCP timers. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpClearAllTimer ( + IN OUT TCP_CB *Tcb + ); + +/** + Enable the window prober timer and set the timeout value. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpSetProbeTimer ( + IN OUT TCP_CB *Tcb + ); + +/** + Enable the keepalive timer and set the timeout value. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpSetKeepaliveTimer ( + IN OUT TCP_CB *Tcb + ); + +// +// Functions in TcpIo.c +// + +/** + Packet receive callback function provided to IP_IO. Used to call + the proper function to handle the packet received by IP. + + @param[in] Status Result of the receive request. + @param[in] IcmpErr Valid when Status is EFI_ICMP_ERROR. + @param[in] NetSession The IP session for the received packet. + @param[in] Pkt Packet received. + @param[in] Context The data provided by the user for the received packet when + the callback is registered in IP_IO_OPEN_DATA::RcvdContext. + This is an optional parameter that may be NULL. + +**/ +VOID +EFIAPI +TcpRxCallback ( + IN EFI_STATUS Status, + IN UINT8 IcmpErr, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Pkt, + IN VOID *Context OPTIONAL + ); + +/** + Send the segment to IP via IpIo function. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Nbuf Pointer to the TCP segment to be sent. + @param[in] Src Source address of the TCP segment. + @param[in] Dest Destination address of the TCP segment. + @param[in] Version IP_VERSION_4 or IP_VERSION_6 + + @retval 0 The segment was sent out successfully. + @retval -1 The segment failed to be sent. + +**/ +INTN +TcpSendIpPacket ( + IN TCP_CB *Tcb, + IN NET_BUF *Nbuf, + IN EFI_IP_ADDRESS *Src, + IN EFI_IP_ADDRESS *Dest, + IN UINT8 Version + ); + +/** + Refresh the remote peer's Neighbor Cache State if already exists. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Neighbor Source address of the TCP segment. + @param[in] Timeout Time in 100-ns units that this entry will remain + in the neighbor cache. A value of zero means that + the entry is permanent. A value of non-zero means + that the entry is dynamic and will be deleted + after Timeout. + + @retval EFI_SUCCESS Successfully updated the neighbor relationship. + @retval EFI_NOT_STARTED The IpIo is not configured. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources. + @retval EFI_NOT_FOUND This entry is not in the neighbor table. + +**/ +EFI_STATUS +Tcp6RefreshNeighbor ( + IN TCP_CB *Tcb, + IN EFI_IP_ADDRESS *Neighbor, + IN UINT32 Timeout + ); + +// +// Functions in TcpDispatcher.c +// + +/** + The procotol handler provided to the socket layer, used to + dispatch the socket level requests by calling the corresponding + TCP layer functions. + + @param[in] Sock Pointer to the socket of this TCP instance. + @param[in] Request The code of this operation request. + @param[in] Data Pointer to the operation specific data passed in + together with the operation request. This is an + optional parameter that may be NULL. + + @retval EFI_SUCCESS The socket request completed successfully. + @retval other The error status returned by the corresponding TCP + layer function. + +**/ +EFI_STATUS +TcpDispatcher ( + IN SOCKET *Sock, + IN UINT8 Request, + IN VOID *Data OPTIONAL + ); + +#endif diff --git a/NetworkPkg/TcpDxe/TcpInput.c b/NetworkPkg/TcpDxe/TcpInput.c new file mode 100644 index 000000000..42079671e --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpInput.c @@ -0,0 +1,1707 @@ +/** @file + TCP input process routines. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TcpMain.h" + +/** + Check whether the sequence number of the incoming segment is acceptable. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Seg Pointer to the incoming segment. + + @retval 1 The sequence number is acceptable. + @retval 0 The sequence number is not acceptable. + +**/ +INTN +TcpSeqAcceptable ( + IN TCP_CB *Tcb, + IN TCP_SEG *Seg + ) +{ + return (TCP_SEQ_LEQ (Tcb->RcvNxt, Seg->End) && + TCP_SEQ_LT (Seg->Seq, Tcb->RcvWl2 + Tcb->RcvWnd)); +} + +/** + NewReno fast recovery defined in RFC3782. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Seg Segment that triggers the fast recovery. + +**/ +VOID +TcpFastRecover ( + IN OUT TCP_CB *Tcb, + IN TCP_SEG *Seg + ) +{ + UINT32 FlightSize; + UINT32 Acked; + + // + // Step 1: Three duplicate ACKs and not in fast recovery + // + if (Tcb->CongestState != TCP_CONGEST_RECOVER) { + + // + // Step 1A: Invoking fast retransmission. + // + FlightSize = TCP_SUB_SEQ (Tcb->SndNxt, Tcb->SndUna); + + Tcb->Ssthresh = MAX (FlightSize >> 1, (UINT32) (2 * Tcb->SndMss)); + Tcb->Recover = Tcb->SndNxt; + + Tcb->CongestState = TCP_CONGEST_RECOVER; + TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON); + + // + // Step 2: Entering fast retransmission + // + TcpRetransmit (Tcb, Tcb->SndUna); + Tcb->CWnd = Tcb->Ssthresh + 3 * Tcb->SndMss; + + DEBUG ( + (EFI_D_NET, + "TcpFastRecover: enter fast retransmission for TCB %p, recover point is %d\n", + Tcb, + Tcb->Recover) + ); + return; + } + + // + // During fast recovery, execute Step 3, 4, 5 of RFC3782 + // + if (Seg->Ack == Tcb->SndUna) { + + // + // Step 3: Fast Recovery, + // If this is a duplicated ACK, increse Cwnd by SMSS. + // + + // Step 4 is skipped here only to be executed later + // by TcpToSendData + // + Tcb->CWnd += Tcb->SndMss; + DEBUG ( + (EFI_D_NET, + "TcpFastRecover: received another duplicated ACK (%d) for TCB %p\n", + Seg->Ack, + Tcb) + ); + + } else { + + // + // New data is ACKed, check whether it is a + // full ACK or partial ACK + // + if (TCP_SEQ_GEQ (Seg->Ack, Tcb->Recover)) { + + // + // Step 5 - Full ACK: + // deflate the congestion window, and exit fast recovery + // + FlightSize = TCP_SUB_SEQ (Tcb->SndNxt, Tcb->SndUna); + + Tcb->CWnd = MIN (Tcb->Ssthresh, FlightSize + Tcb->SndMss); + + Tcb->CongestState = TCP_CONGEST_OPEN; + DEBUG ( + (EFI_D_NET, + "TcpFastRecover: received a full ACK(%d) for TCB %p, exit fast recovery\n", + Seg->Ack, + Tcb) + ); + + } else { + + // + // Step 5 - Partial ACK: + // fast retransmit the first unacknowledge field + // , then deflate the CWnd + // + TcpRetransmit (Tcb, Seg->Ack); + Acked = TCP_SUB_SEQ (Seg->Ack, Tcb->SndUna); + + // + // Deflate the CWnd by the amount of new data + // ACKed by SEG.ACK. If more than one SMSS data + // is ACKed, add back SMSS byte to CWnd after + // + if (Acked >= Tcb->SndMss) { + Acked -= Tcb->SndMss; + + } + + Tcb->CWnd -= Acked; + + DEBUG ( + (EFI_D_NET, + "TcpFastRecover: received a partial ACK(%d) for TCB %p\n", + Seg->Ack, + Tcb) + ); + + } + } +} + +/** + NewReno fast loss recovery defined in RFC3792. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Seg Segment that triggers the fast loss recovery. + +**/ +VOID +TcpFastLossRecover ( + IN OUT TCP_CB *Tcb, + IN TCP_SEG *Seg + ) +{ + if (TCP_SEQ_GT (Seg->Ack, Tcb->SndUna)) { + + // + // New data is ACKed, check whether it is a + // full ACK or partial ACK + // + if (TCP_SEQ_GEQ (Seg->Ack, Tcb->LossRecover)) { + + // + // Full ACK: exit the loss recovery. + // + Tcb->LossTimes = 0; + Tcb->CongestState = TCP_CONGEST_OPEN; + + DEBUG ( + (EFI_D_NET, + "TcpFastLossRecover: received a full ACK(%d) for TCB %p\n", + Seg->Ack, + Tcb) + ); + + } else { + + // + // Partial ACK: + // fast retransmit the first unacknowledge field. + // + TcpRetransmit (Tcb, Seg->Ack); + DEBUG ( + (EFI_D_NET, + "TcpFastLossRecover: received a partial ACK(%d) for TCB %p\n", + Seg->Ack, + Tcb) + ); + } + } +} + +/** + Compute the RTT as specified in RFC2988. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Measure Currently measured RTT in heartbeats. + +**/ +VOID +TcpComputeRtt ( + IN OUT TCP_CB *Tcb, + IN UINT32 Measure + ) +{ + INT32 Var; + + // + // Step 2.3: Compute the RTO for subsequent RTT measurement. + // + if (Tcb->SRtt != 0) { + + Var = Tcb->SRtt - (Measure << TCP_RTT_SHIFT); + + if (Var < 0) { + Var = -Var; + } + + Tcb->RttVar = (3 * Tcb->RttVar + Var) >> 2; + Tcb->SRtt = 7 * (Tcb->SRtt >> 3) + Measure; + + } else { + // + // Step 2.2: compute the first RTT measure + // + Tcb->SRtt = Measure << TCP_RTT_SHIFT; + Tcb->RttVar = Measure << (TCP_RTT_SHIFT - 1); + } + + Tcb->Rto = (Tcb->SRtt + MAX (8, 4 * Tcb->RttVar)) >> TCP_RTT_SHIFT; + + // + // Step 2.4: Limit the RTO to at least 1 second + // Step 2.5: Limit the RTO to a maxium value that + // is at least 60 second + // + if (Tcb->Rto < TCP_RTO_MIN) { + Tcb->Rto = TCP_RTO_MIN; + + } else if (Tcb->Rto > TCP_RTO_MAX) { + Tcb->Rto = TCP_RTO_MAX; + + } + + DEBUG ( + (EFI_D_NET, + "TcpComputeRtt: new RTT for TCB %p computed SRTT: %d RTTVAR: %d RTO: %d\n", + Tcb, + Tcb->SRtt, + Tcb->RttVar, + Tcb->Rto) + ); + +} + +/** + Trim the data; SYN and FIN to fit into the window defined by Left and Right. + + @param[in] Nbuf The buffer that contains a received TCP segment without an IP header. + @param[in] Left The sequence number of the window's left edge. + @param[in] Right The sequence number of the window's right edge. + + @retval 0 The segment is broken. + @retval 1 The segment is in good shape. + +**/ +INTN +TcpTrimSegment ( + IN NET_BUF *Nbuf, + IN TCP_SEQNO Left, + IN TCP_SEQNO Right + ) +{ + TCP_SEG *Seg; + TCP_SEQNO Urg; + UINT32 Drop; + + Seg = TCPSEG_NETBUF (Nbuf); + + // + // If the segment is completely out of window, + // truncate every thing, include SYN and FIN. + // + if (TCP_SEQ_LEQ (Seg->End, Left) || TCP_SEQ_LEQ (Right, Seg->Seq)) { + + TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_SYN); + TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_FIN); + + Seg->Seq = Seg->End; + NetbufTrim (Nbuf, Nbuf->TotalSize, NET_BUF_HEAD); + return 1; + } + + // + // Adjust the buffer header + // + if (TCP_SEQ_LT (Seg->Seq, Left)) { + + Drop = TCP_SUB_SEQ (Left, Seg->Seq); + Urg = Seg->Seq + Seg->Urg; + Seg->Seq = Left; + + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) { + TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_SYN); + Drop--; + } + + // + // Adjust the urgent point + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_URG)) { + + if (TCP_SEQ_LT (Urg, Seg->Seq)) { + + TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_URG); + } else { + Seg->Urg = (UINT16) TCP_SUB_SEQ (Urg, Seg->Seq); + } + } + + if (Drop != 0) { + NetbufTrim (Nbuf, Drop, NET_BUF_HEAD); + } + } + + // + // Adjust the buffer tail + // + if (TCP_SEQ_GT (Seg->End, Right)) { + + Drop = TCP_SUB_SEQ (Seg->End, Right); + Seg->End = Right; + + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) { + TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_FIN); + Drop--; + } + + if (Drop != 0) { + NetbufTrim (Nbuf, Drop, NET_BUF_TAIL); + } + } + + return TcpVerifySegment (Nbuf); +} + +/** + Trim off the data outside the tcb's receive window. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Nbuf Pointer to the NET_BUF containing the received tcp segment. + + @retval 0 The segment is broken. + @retval 1 The segment is in good shape. + +**/ +INTN +TcpTrimInWnd ( + IN TCP_CB *Tcb, + IN NET_BUF *Nbuf + ) +{ + return TcpTrimSegment (Nbuf, Tcb->RcvNxt, Tcb->RcvWl2 + Tcb->RcvWnd); +} + +/** + Process the data and FIN flag, and check whether to deliver + data to the socket layer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + + @retval 0 No error occurred to deliver data. + @retval -1 An error condition occurred. The proper response is to reset the + connection. + +**/ +INTN +TcpDeliverData ( + IN OUT TCP_CB *Tcb + ) +{ + LIST_ENTRY *Entry; + NET_BUF *Nbuf; + TCP_SEQNO Seq; + TCP_SEG *Seg; + UINT32 Urgent; + + ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL)); + + // + // make sure there is some data queued, + // and TCP is in a proper state + // + if (IsListEmpty (&Tcb->RcvQue) || !TCP_CONNECTED (Tcb->State)) { + + return 0; + } + + // + // Deliver data to the socket layer + // + Entry = Tcb->RcvQue.ForwardLink; + Seq = Tcb->RcvNxt; + + while (Entry != &Tcb->RcvQue) { + Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + Seg = TCPSEG_NETBUF (Nbuf); + + if (TcpVerifySegment (Nbuf) == 0) { + DEBUG ( + (EFI_D_ERROR, + "TcpToSendData: discard a broken segment for TCB %p\n", + Tcb) + ); + NetbufFree (Nbuf); + return -1; + } + + ASSERT (Nbuf->Tcp == NULL); + + if (TCP_SEQ_GT (Seg->Seq, Seq)) { + break; + } + + Entry = Entry->ForwardLink; + Seq = Seg->End; + Tcb->RcvNxt = Seq; + + RemoveEntryList (&Nbuf->List); + + // + // RFC793 Eighth step: process FIN in sequence + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) { + + // + // The peer sends to us junky data after FIN, + // reset the connection. + // + if (!IsListEmpty (&Tcb->RcvQue)) { + DEBUG ( + (EFI_D_ERROR, + "TcpDeliverData: data received after FIN from peer of TCB %p, reset connection\n", + Tcb) + ); + + NetbufFree (Nbuf); + return -1; + } + + DEBUG ( + (EFI_D_NET, + "TcpDeliverData: processing FIN from peer of TCB %p\n", + Tcb) + ); + + switch (Tcb->State) { + case TCP_SYN_RCVD: + case TCP_ESTABLISHED: + + TcpSetState (Tcb, TCP_CLOSE_WAIT); + break; + + case TCP_FIN_WAIT_1: + + if (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) { + + TcpSetState (Tcb, TCP_CLOSING); + break; + } + + // + // fall through + // + case TCP_FIN_WAIT_2: + + TcpSetState (Tcb, TCP_TIME_WAIT); + TcpClearAllTimer (Tcb); + + if (Tcb->TimeWaitTimeout != 0) { + + TcpSetTimer (Tcb, TCP_TIMER_2MSL, Tcb->TimeWaitTimeout); + } else { + + DEBUG ( + (EFI_D_WARN, + "Connection closed immediately because app disables TIME_WAIT timer for %p\n", + Tcb) + ); + + TcpSendAck (Tcb); + TcpClose (Tcb); + } + break; + + case TCP_CLOSE_WAIT: + case TCP_CLOSING: + case TCP_LAST_ACK: + case TCP_TIME_WAIT: + // + // The peer sends to us junk FIN byte. Discard + // the buffer then reset the connection + // + NetbufFree (Nbuf); + return -1; + break; + default: + break; + } + + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW); + + Seg->End--; + } + + // + // Don't delay the ack if PUSH flag is on. + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_PSH)) { + + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW); + } + + if (Nbuf->TotalSize != 0) { + Urgent = 0; + + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RCVD_URG) && + TCP_SEQ_LEQ (Seg->Seq, Tcb->RcvUp)) + { + + if (TCP_SEQ_LEQ (Seg->End, Tcb->RcvUp)) { + Urgent = Nbuf->TotalSize; + } else { + Urgent = TCP_SUB_SEQ (Tcb->RcvUp, Seg->Seq) + 1; + } + } + + SockDataRcvd (Tcb->Sk, Nbuf, Urgent); + } + + if (TCP_FIN_RCVD (Tcb->State)) { + + SockNoMoreData (Tcb->Sk); + } + + NetbufFree (Nbuf); + } + + return 0; +} + +/** + Store the data into the reassemble queue. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Nbuf Pointer to the buffer containing the data to be queued. + + @retval 0 An error condition occurred. + @retval 1 No error occurred to queue data. + +**/ +INTN +TcpQueueData ( + IN OUT TCP_CB *Tcb, + IN NET_BUF *Nbuf + ) +{ + TCP_SEG *Seg; + LIST_ENTRY *Head; + LIST_ENTRY *Prev; + LIST_ENTRY *Cur; + NET_BUF *Node; + + ASSERT ((Tcb != NULL) && (Nbuf != NULL) && (Nbuf->Tcp == NULL)); + + NET_GET_REF (Nbuf); + + Seg = TCPSEG_NETBUF (Nbuf); + Head = &Tcb->RcvQue; + + // + // Fast path to process normal case. That is, + // no out-of-order segments are received. + // + if (IsListEmpty (Head)) { + + InsertTailList (Head, &Nbuf->List); + return 1; + } + + // + // Find the point to insert the buffer + // + for (Prev = Head, Cur = Head->ForwardLink; + Cur != Head; + Prev = Cur, Cur = Cur->ForwardLink) { + + Node = NET_LIST_USER_STRUCT (Cur, NET_BUF, List); + + if (TCP_SEQ_LT (Seg->Seq, TCPSEG_NETBUF (Node)->Seq)) { + break; + } + } + + // + // Check whether the current segment overlaps with the + // previous segment. + // + if (Prev != Head) { + Node = NET_LIST_USER_STRUCT (Prev, NET_BUF, List); + + if (TCP_SEQ_LT (Seg->Seq, TCPSEG_NETBUF (Node)->End)) { + + if (TCP_SEQ_LEQ (Seg->End, TCPSEG_NETBUF (Node)->End)) { + return 1; + } + + if (TcpTrimSegment (Nbuf, TCPSEG_NETBUF (Node)->End, Seg->End) == 0) { + return 0; + } + } + } + + InsertHeadList (Prev, &Nbuf->List); + + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW); + + // + // Check the segments after the insert point. + // + while (Cur != Head) { + Node = NET_LIST_USER_STRUCT (Cur, NET_BUF, List); + + if (TCP_SEQ_LEQ (TCPSEG_NETBUF (Node)->End, Seg->End)) { + + Cur = Cur->ForwardLink; + + RemoveEntryList (&Node->List); + NetbufFree (Node); + continue; + } + + if (TCP_SEQ_LT (TCPSEG_NETBUF (Node)->Seq, Seg->End)) { + + if (TCP_SEQ_LEQ (TCPSEG_NETBUF (Node)->Seq, Seg->Seq)) { + + RemoveEntryList (&Nbuf->List); + return 1; + } + + if (TcpTrimSegment (Nbuf, Seg->Seq, TCPSEG_NETBUF (Node)->Seq) == 0) { + RemoveEntryList (&Nbuf->List); + return 0; + } + break; + } + + Cur = Cur->ForwardLink; + } + + return 1; +} + + +/** + Adjust the send queue or the retransmit queue. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Ack The acknowledge seuqence number of the received segment. + + @retval 0 An error condition occurred. + @retval 1 No error occurred. + +**/ +INTN +TcpAdjustSndQue ( + IN TCP_CB *Tcb, + IN TCP_SEQNO Ack + ) +{ + LIST_ENTRY *Head; + LIST_ENTRY *Cur; + NET_BUF *Node; + TCP_SEG *Seg; + + Head = &Tcb->SndQue; + Cur = Head->ForwardLink; + + while (Cur != Head) { + Node = NET_LIST_USER_STRUCT (Cur, NET_BUF, List); + Seg = TCPSEG_NETBUF (Node); + + if (TCP_SEQ_GEQ (Seg->Seq, Ack)) { + break; + } + + // + // Remove completely ACKed segments + // + if (TCP_SEQ_LEQ (Seg->End, Ack)) { + Cur = Cur->ForwardLink; + + RemoveEntryList (&Node->List); + NetbufFree (Node); + continue; + } + + return TcpTrimSegment (Node, Ack, Seg->End); + } + + return 1; +} + +/** + Process the received TCP segments. + + @param[in] Nbuf Buffer that contains received a TCP segment without an IP header. + @param[in] Src Source address of the segment, or the peer's IP address. + @param[in] Dst Destination address of the segment, or the local end's IP + address. + @param[in] Version IP_VERSION_4 indicates IP4 stack. IP_VERSION_6 indicates + IP6 stack. + + @retval 0 Segment processed successfully. It is either accepted or + discarded. However, no connection is reset by the segment. + @retval -1 A connection is reset by the segment. + +**/ +INTN +TcpInput ( + IN NET_BUF *Nbuf, + IN EFI_IP_ADDRESS *Src, + IN EFI_IP_ADDRESS *Dst, + IN UINT8 Version + ) +{ + TCP_CB *Tcb; + TCP_CB *Parent; + TCP_OPTION Option; + TCP_HEAD *Head; + INT32 Len; + TCP_SEG *Seg; + TCP_SEQNO Right; + TCP_SEQNO Urg; + UINT16 Checksum; + INT32 Usable; + + ASSERT ((Version == IP_VERSION_4) || (Version == IP_VERSION_6)); + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + + Parent = NULL; + Tcb = NULL; + + Head = (TCP_HEAD *) NetbufGetByte (Nbuf, 0, NULL); + ASSERT (Head != NULL); + + if (Nbuf->TotalSize < sizeof (TCP_HEAD)) { + DEBUG ((EFI_D_NET, "TcpInput: received a malformed packet\n")); + goto DISCARD; + } + + Len = Nbuf->TotalSize - (Head->HeadLen << 2); + + if ((Head->HeadLen < 5) || (Len < 0)) { + + DEBUG ((EFI_D_NET, "TcpInput: received a malformed packet\n")); + + goto DISCARD; + } + + if (Version == IP_VERSION_4) { + Checksum = NetPseudoHeadChecksum (Src->Addr[0], Dst->Addr[0], 6, 0); + } else { + Checksum = NetIp6PseudoHeadChecksum (&Src->v6, &Dst->v6, 6, 0); + } + + Checksum = TcpChecksum (Nbuf, Checksum); + + if (Checksum != 0) { + DEBUG ((EFI_D_ERROR, "TcpInput: received a checksum error packet\n")); + goto DISCARD; + } + + if (TCP_FLG_ON (Head->Flag, TCP_FLG_SYN)) { + Len++; + } + + if (TCP_FLG_ON (Head->Flag, TCP_FLG_FIN)) { + Len++; + } + + Tcb = TcpLocateTcb ( + Head->DstPort, + Dst, + Head->SrcPort, + Src, + Version, + (BOOLEAN) TCP_FLG_ON (Head->Flag, TCP_FLG_SYN) + ); + + if ((Tcb == NULL) || (Tcb->State == TCP_CLOSED)) { + DEBUG ((EFI_D_NET, "TcpInput: send reset because no TCB found\n")); + + Tcb = NULL; + goto SEND_RESET; + } + + Seg = TcpFormatNetbuf (Tcb, Nbuf); + + // + // RFC1122 recommended reaction to illegal option + // (in fact, an illegal option length) is reset. + // + if (TcpParseOption (Nbuf->Tcp, &Option) == -1) { + DEBUG ( + (EFI_D_ERROR, + "TcpInput: reset the peer because of malformed option for TCB %p\n", + Tcb) + ); + + goto SEND_RESET; + } + + // + // From now on, the segment is headless + // + NetbufTrim (Nbuf, (Head->HeadLen << 2), NET_BUF_HEAD); + Nbuf->Tcp = NULL; + + // + // Process the segment in LISTEN state. + // + if (Tcb->State == TCP_LISTEN) { + // + // First step: Check RST + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) { + DEBUG ( + (EFI_D_WARN, + "TcpInput: discard a reset segment for TCB %p in listening\n", + Tcb) + ); + + goto DISCARD; + } + + // + // Second step: Check ACK. + // Any ACK sent to TCP in LISTEN is reseted. + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) { + DEBUG ( + (EFI_D_WARN, + "TcpInput: send reset because of segment with ACK for TCB %p in listening\n", + Tcb) + ); + + goto SEND_RESET; + } + + // + // Third step: Check SYN + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) { + // + // create a child TCB to handle the data + // + Parent = Tcb; + + Tcb = TcpCloneTcb (Parent); + if (Tcb == NULL) { + DEBUG ( + (EFI_D_ERROR, + "TcpInput: discard a segment because failed to clone a child for TCB %p\n", + Tcb) + ); + + goto DISCARD; + } + + DEBUG ( + (EFI_D_NET, + "TcpInput: create a child for TCB %p in listening\n", + Tcb) + ); + + // + // init the TCB structure + // + IP6_COPY_ADDRESS (&Tcb->LocalEnd.Ip, Dst); + IP6_COPY_ADDRESS (&Tcb->RemoteEnd.Ip, Src); + Tcb->LocalEnd.Port = Head->DstPort; + Tcb->RemoteEnd.Port = Head->SrcPort; + + TcpInitTcbLocal (Tcb); + TcpInitTcbPeer (Tcb, Seg, &Option); + + TcpSetState (Tcb, TCP_SYN_RCVD); + TcpSetTimer (Tcb, TCP_TIMER_CONNECT, Tcb->ConnectTimeout); + if (TcpTrimInWnd (Tcb, Nbuf) == 0) { + DEBUG ( + (EFI_D_ERROR, + "TcpInput: discard a broken segment for TCB %p\n", + Tcb) + ); + + goto DISCARD; + } + + goto StepSix; + } + + goto DISCARD; + + } else if (Tcb->State == TCP_SYN_SENT) { + // + // First step: Check ACK bit + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK) && (Seg->Ack != Tcb->Iss + 1)) { + + DEBUG ( + (EFI_D_WARN, + "TcpInput: send reset because of wrong ACK received for TCB %p in SYN_SENT\n", + Tcb) + ); + + goto SEND_RESET; + } + + // + // Second step: Check RST bit + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) { + + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) { + + DEBUG ( + (EFI_D_WARN, + "TcpInput: connection reset by peer for TCB %p in SYN_SENT\n", + Tcb) + ); + + SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET); + goto DROP_CONNECTION; + } else { + + DEBUG ( + (EFI_D_WARN, + "TcpInput: discard a reset segment because of no ACK for TCB %p in SYN_SENT\n", + Tcb) + ); + + goto DISCARD; + } + } + + // + // Third step: Check security and precedence. Skipped + // + + // + // Fourth step: Check SYN. Pay attention to simultaneous open + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) { + + TcpInitTcbPeer (Tcb, Seg, &Option); + + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) { + + Tcb->SndUna = Seg->Ack; + } + + TcpClearTimer (Tcb, TCP_TIMER_REXMIT); + + if (TCP_SEQ_GT (Tcb->SndUna, Tcb->Iss)) { + + TcpSetState (Tcb, TCP_ESTABLISHED); + + TcpClearTimer (Tcb, TCP_TIMER_CONNECT); + TcpDeliverData (Tcb); + + if ((Tcb->CongestState == TCP_CONGEST_OPEN) && + TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RTT_ON)) + { + + TcpComputeRtt (Tcb, Tcb->RttMeasure); + TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON); + } + + if (TcpTrimInWnd (Tcb, Nbuf) == 0) { + DEBUG ( + (EFI_D_ERROR, + "TcpInput: discard a broken segment for TCB %p\n", + Tcb) + ); + + goto DISCARD; + } + + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW); + + DEBUG ( + (EFI_D_NET, + "TcpInput: connection established for TCB %p in SYN_SENT\n", + Tcb) + ); + + goto StepSix; + } else { + // + // Received a SYN segment without ACK, simultanous open. + // + TcpSetState (Tcb, TCP_SYN_RCVD); + + ASSERT (Tcb->SndNxt == Tcb->Iss + 1); + + if (TcpAdjustSndQue (Tcb, Tcb->SndNxt) == 0 || TcpTrimInWnd (Tcb, Nbuf) == 0) { + DEBUG ( + (EFI_D_ERROR, + "TcpInput: discard a broken segment for TCB %p\n", + Tcb) + ); + + goto DISCARD; + } + + DEBUG ( + (EFI_D_WARN, + "TcpInput: simultaneous open for TCB %p in SYN_SENT\n", + Tcb) + ); + + goto StepSix; + } + } + + goto DISCARD; + } + + // + // Process segment in SYN_RCVD or TCP_CONNECTED states + // + + // + // Clear probe timer since the RecvWindow is opened. + // + if (Tcb->ProbeTimerOn && (Seg->Wnd != 0)) { + TcpClearTimer (Tcb, TCP_TIMER_PROBE); + Tcb->ProbeTimerOn = FALSE; + } + + // + // First step: Check whether SEG.SEQ is acceptable + // + if (TcpSeqAcceptable (Tcb, Seg) == 0) { + DEBUG ( + (EFI_D_WARN, + "TcpInput: sequence acceptance test failed for segment of TCB %p\n", + Tcb) + ); + + if (!TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) { + TcpSendAck (Tcb); + } + + goto DISCARD; + } + + if ((TCP_SEQ_LT (Seg->Seq, Tcb->RcvWl2)) && + (Tcb->RcvWl2 == Seg->End) && + !TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN | TCP_FLG_FIN)) + { + + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW); + } + + // + // Second step: Check the RST + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) { + + DEBUG ((EFI_D_WARN, "TcpInput: connection reset for TCB %p\n", Tcb)); + + if (Tcb->State == TCP_SYN_RCVD) { + + SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_REFUSED); + + // + // This TCB comes from either a LISTEN TCB, + // or active open TCB with simultanous open. + // Do NOT signal user CONNECTION refused + // if it comes from a LISTEN TCB. + // + } else if ((Tcb->State == TCP_ESTABLISHED) || + (Tcb->State == TCP_FIN_WAIT_1) || + (Tcb->State == TCP_FIN_WAIT_2) || + (Tcb->State == TCP_CLOSE_WAIT)) + { + + SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET); + + } else { + } + + goto DROP_CONNECTION; + } + + // + // Trim the data and flags. + // + if (TcpTrimInWnd (Tcb, Nbuf) == 0) { + DEBUG ( + (EFI_D_ERROR, + "TcpInput: discard a broken segment for TCB %p\n", + Tcb) + ); + + goto DISCARD; + } + + // + // Third step: Check security and precedence, Ignored + // + + // + // Fourth step: Check the SYN bit. + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) { + + DEBUG ( + (EFI_D_WARN, + "TcpInput: connection reset because received extra SYN for TCB %p\n", + Tcb) + ); + + SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET); + goto RESET_THEN_DROP; + } + // + // Fifth step: Check the ACK + // + if (!TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) { + DEBUG ( + (EFI_D_WARN, + "TcpInput: segment discard because of no ACK for connected TCB %p\n", + Tcb) + ); + + goto DISCARD; + } else { + if (Tcb->IpInfo->IpVersion == IP_VERSION_6 && Tcb->Tick == 0) { + Tcp6RefreshNeighbor (Tcb, Src, TCP6_KEEP_NEIGHBOR_TIME * TICKS_PER_SECOND); + Tcb->Tick = TCP6_REFRESH_NEIGHBOR_TICK; + } + } + + if (Tcb->State == TCP_SYN_RCVD) { + + if (TCP_SEQ_LT (Tcb->SndUna, Seg->Ack) && + TCP_SEQ_LEQ (Seg->Ack, Tcb->SndNxt)) + { + + Tcb->SndWnd = Seg->Wnd; + Tcb->SndWndMax = MAX (Tcb->SndWnd, Tcb->SndWndMax); + Tcb->SndWl1 = Seg->Seq; + Tcb->SndWl2 = Seg->Ack; + TcpSetState (Tcb, TCP_ESTABLISHED); + + TcpClearTimer (Tcb, TCP_TIMER_CONNECT); + TcpDeliverData (Tcb); + + DEBUG ( + (EFI_D_NET, + "TcpInput: connection established for TCB %p in SYN_RCVD\n", + Tcb) + ); + + // + // Continue the process as ESTABLISHED state + // + } else { + DEBUG ( + (EFI_D_WARN, + "TcpInput: send reset because of wrong ACK for TCB %p in SYN_RCVD\n", + Tcb) + ); + + goto SEND_RESET; + } + } + + if (TCP_SEQ_LT (Seg->Ack, Tcb->SndUna)) { + + DEBUG ( + (EFI_D_WARN, + "TcpInput: ignore the out-of-data ACK for connected TCB %p\n", + Tcb) + ); + + goto StepSix; + + } else if (TCP_SEQ_GT (Seg->Ack, Tcb->SndNxt)) { + + DEBUG ( + (EFI_D_WARN, + "TcpInput: discard segment for future ACK for connected TCB %p\n", + Tcb) + ); + + TcpSendAck (Tcb); + goto DISCARD; + } + + // + // From now on: SND.UNA <= SEG.ACK <= SND.NXT. + // + if (TCP_FLG_ON (Option.Flag, TCP_OPTION_RCVD_TS)) { + // + // update TsRecent as specified in page 16 RFC1323. + // RcvWl2 equals to the variable "LastAckSent" + // defined there. + // + if (TCP_SEQ_LEQ (Seg->Seq, Tcb->RcvWl2) && + TCP_SEQ_LT (Tcb->RcvWl2, Seg->End)) + { + + Tcb->TsRecent = Option.TSVal; + Tcb->TsRecentAge = mTcpTick; + } + + TcpComputeRtt (Tcb, TCP_SUB_TIME (mTcpTick, Option.TSEcr)); + + } else if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RTT_ON)) { + + ASSERT (Tcb->CongestState == TCP_CONGEST_OPEN); + + TcpComputeRtt (Tcb, Tcb->RttMeasure); + TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON); + } + + if (Seg->Ack == Tcb->SndNxt) { + + TcpClearTimer (Tcb, TCP_TIMER_REXMIT); + } else { + + TcpSetTimer (Tcb, TCP_TIMER_REXMIT, Tcb->Rto); + } + + // + // Count duplicate acks. + // + if ((Seg->Ack == Tcb->SndUna) && + (Tcb->SndUna != Tcb->SndNxt) && + (Seg->Wnd == Tcb->SndWnd) && + (0 == Len)) + { + + Tcb->DupAck++; + } else { + + Tcb->DupAck = 0; + } + + // + // Congestion avoidance, fast recovery and fast retransmission. + // + if (((Tcb->CongestState == TCP_CONGEST_OPEN) && (Tcb->DupAck < 3)) || + (Tcb->CongestState == TCP_CONGEST_LOSS)) + { + + if (TCP_SEQ_GT (Seg->Ack, Tcb->SndUna)) { + + if (Tcb->CWnd < Tcb->Ssthresh) { + + Tcb->CWnd += Tcb->SndMss; + } else { + + Tcb->CWnd += MAX (Tcb->SndMss * Tcb->SndMss / Tcb->CWnd, 1); + } + + Tcb->CWnd = MIN (Tcb->CWnd, TCP_MAX_WIN << Tcb->SndWndScale); + } + + if (Tcb->CongestState == TCP_CONGEST_LOSS) { + TcpFastLossRecover (Tcb, Seg); + } + } else { + + TcpFastRecover (Tcb, Seg); + } + + if (TCP_SEQ_GT (Seg->Ack, Tcb->SndUna)) { + + if (TcpAdjustSndQue (Tcb, Seg->Ack) == 0) { + DEBUG ( + (EFI_D_ERROR, + "TcpInput: discard a broken segment for TCB %p\n", + Tcb) + ); + + goto DISCARD; + } + + Tcb->SndUna = Seg->Ack; + + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_SND_URG) && + TCP_SEQ_LT (Tcb->SndUp, Seg->Ack)) + { + + TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_SND_URG); + } + } + + // + // Update window info + // + if (TCP_SEQ_LT (Tcb->SndWl1, Seg->Seq) || + ((Tcb->SndWl1 == Seg->Seq) && TCP_SEQ_LEQ (Tcb->SndWl2, Seg->Ack))) + { + + Right = Seg->Ack + Seg->Wnd; + + if (TCP_SEQ_LT (Right, Tcb->SndWl2 + Tcb->SndWnd)) { + + if ((Tcb->SndWl1 == Seg->Seq) && + (Tcb->SndWl2 == Seg->Ack) && + (Len == 0)) + { + + goto NO_UPDATE; + } + + DEBUG ( + (EFI_D_WARN, + "TcpInput: peer shrinks the window for connected TCB %p\n", + Tcb) + ); + + if ((Tcb->CongestState == TCP_CONGEST_RECOVER) && + (TCP_SEQ_LT (Right, Tcb->Recover))) + { + + Tcb->Recover = Right; + } + + if ((Tcb->CongestState == TCP_CONGEST_LOSS) && + (TCP_SEQ_LT (Right, Tcb->LossRecover))) + { + + Tcb->LossRecover = Right; + } + + if (TCP_SEQ_LT (Right, Tcb->SndNxt)) { + // + // Check for Window Retraction in RFC7923 section 2.4. + // The lower n bits of the peer's actual receive window is wiped out if TCP + // window scale is enabled, it will look like the peer is shrinking the window. + // Check whether the SndNxt is out of the advertised receive window by more than + // 2^Rcv.Wind.Shift before moving the SndNxt to the left. + // + DEBUG ( + (EFI_D_WARN, + "TcpInput: peer advise negative useable window for connected TCB %p\n", + Tcb) + ); + Usable = TCP_SUB_SEQ (Tcb->SndNxt, Right); + if ((Usable >> Tcb->SndWndScale) > 0) { + DEBUG ( + (EFI_D_WARN, + "TcpInput: SndNxt is out of window by more than window scale for TCB %p\n", + Tcb) + ); + Tcb->SndNxt = Right; + } + if (Right == Tcb->SndUna) { + + TcpClearTimer (Tcb, TCP_TIMER_REXMIT); + TcpSetProbeTimer (Tcb); + } + } + } + + Tcb->SndWnd = Seg->Wnd; + Tcb->SndWndMax = MAX (Tcb->SndWnd, Tcb->SndWndMax); + Tcb->SndWl1 = Seg->Seq; + Tcb->SndWl2 = Seg->Ack; + } + +NO_UPDATE: + + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_SENT) && + (Tcb->SndUna == Tcb->SndNxt)) + { + + DEBUG ( + (EFI_D_NET, + "TcpInput: local FIN is ACKed by peer for connected TCB %p\n", + Tcb) + ); + + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED); + } + + // + // Transit the state if proper. + // + switch (Tcb->State) { + case TCP_FIN_WAIT_1: + + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) { + + TcpSetState (Tcb, TCP_FIN_WAIT_2); + + TcpClearAllTimer (Tcb); + TcpSetTimer (Tcb, TCP_TIMER_FINWAIT2, Tcb->FinWait2Timeout); + } + + case TCP_FIN_WAIT_2: + + break; + + case TCP_CLOSE_WAIT: + break; + + case TCP_CLOSING: + + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) { + + TcpSetState (Tcb, TCP_TIME_WAIT); + + TcpClearAllTimer (Tcb); + + if (Tcb->TimeWaitTimeout != 0) { + + TcpSetTimer (Tcb, TCP_TIMER_2MSL, Tcb->TimeWaitTimeout); + } else { + + DEBUG ( + (EFI_D_WARN, + "Connection closed immediately because app disables TIME_WAIT timer for %p\n", + Tcb) + ); + + TcpClose (Tcb); + } + } + break; + + case TCP_LAST_ACK: + + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) { + + TcpSetState (Tcb, TCP_CLOSED); + } + + break; + + case TCP_TIME_WAIT: + + TcpSendAck (Tcb); + + if (Tcb->TimeWaitTimeout != 0) { + + TcpSetTimer (Tcb, TCP_TIMER_2MSL, Tcb->TimeWaitTimeout); + } else { + + DEBUG ( + (EFI_D_WARN, + "Connection closed immediately because app disables TIME_WAIT timer for %p\n", + Tcb) + ); + + TcpClose (Tcb); + } + break; + + default: + break; + } + // + // Sixth step: Check the URG bit.update the Urg point + // if in TCP_CAN_RECV, otherwise, leave the RcvUp intact. + // +StepSix: + + Tcb->Idle = 0; + TcpSetKeepaliveTimer (Tcb); + + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_URG) && !TCP_FIN_RCVD (Tcb->State)) { + + DEBUG ( + (EFI_D_NET, + "TcpInput: received urgent data from peer for connected TCB %p\n", + Tcb) + ); + + Urg = Seg->Seq + Seg->Urg; + + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RCVD_URG) && + TCP_SEQ_GT (Urg, Tcb->RcvUp)) + { + + Tcb->RcvUp = Urg; + } else { + + Tcb->RcvUp = Urg; + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_RCVD_URG); + } + } + // + // Seventh step: Process the segment data + // + if (Seg->End != Seg->Seq) { + + if (TCP_FIN_RCVD (Tcb->State)) { + + DEBUG ( + (EFI_D_WARN, + "TcpInput: connection reset because data is lost for connected TCB %p\n", + Tcb) + ); + + goto RESET_THEN_DROP; + } + + if (TCP_LOCAL_CLOSED (Tcb->State) && (Nbuf->TotalSize != 0)) { + DEBUG ( + (EFI_D_WARN, + "TcpInput: connection reset because data is lost for connected TCB %p\n", + Tcb) + ); + + goto RESET_THEN_DROP; + } + + if (TcpQueueData (Tcb, Nbuf) == 0) { + DEBUG ( + (EFI_D_ERROR, + "TcpInput: discard a broken segment for TCB %p\n", + Tcb) + ); + + goto DISCARD; + } + + if (TcpDeliverData (Tcb) == -1) { + goto RESET_THEN_DROP; + } + + if (!IsListEmpty (&Tcb->RcvQue)) { + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW); + } + } + + // + // Eighth step: check the FIN. + // This step is moved to TcpDeliverData. FIN will be + // processed in sequence there. Check the comments in + // the beginning of the file header for information. + // + + // + // Tcb is a new child of the listening Parent, + // commit it. + // + if (Parent != NULL) { + Tcb->Parent = Parent; + TcpInsertTcb (Tcb); + } + + if ((Tcb->State != TCP_CLOSED) && + (TcpToSendData (Tcb, 0) == 0) && + (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW) || (Nbuf->TotalSize != 0))) + { + + TcpToSendAck (Tcb); + } + + NetbufFree (Nbuf); + return 0; + +RESET_THEN_DROP: + TcpSendReset (Tcb, Head, Len, Dst, Src, Version); + +DROP_CONNECTION: + ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL)); + + NetbufFree (Nbuf); + TcpClose (Tcb); + + return -1; + +SEND_RESET: + + TcpSendReset (Tcb, Head, Len, Dst, Src, Version); + +DISCARD: + + // + // Tcb is a child of Parent, and it doesn't survive + // + DEBUG ((EFI_D_WARN, "TcpInput: Discard a packet\n")); + NetbufFree (Nbuf); + + if ((Parent != NULL) && (Tcb != NULL)) { + + ASSERT (Tcb->Sk != NULL); + TcpClose (Tcb); + } + + return 0; +} + +/** + Process the received ICMP error messages for TCP. + + @param[in] Nbuf The buffer that contains part of the TCP segment without an IP header + truncated from the ICMP error packet. + @param[in] IcmpErr The ICMP error code interpreted from an ICMP error packet. + @param[in] Src Source address of the ICMP error message. + @param[in] Dst Destination address of the ICMP error message. + @param[in] Version IP_VERSION_4 indicates IP4 stack. IP_VERSION_6 indicates + IP6 stack. + +**/ +VOID +TcpIcmpInput ( + IN NET_BUF *Nbuf, + IN UINT8 IcmpErr, + IN EFI_IP_ADDRESS *Src, + IN EFI_IP_ADDRESS *Dst, + IN UINT8 Version + ) +{ + TCP_HEAD *Head; + TCP_CB *Tcb; + TCP_SEQNO Seq; + EFI_STATUS IcmpErrStatus; + BOOLEAN IcmpErrIsHard; + BOOLEAN IcmpErrNotify; + + if (Nbuf->TotalSize < sizeof (TCP_HEAD)) { + goto CLEAN_EXIT; + } + + Head = (TCP_HEAD *) NetbufGetByte (Nbuf, 0, NULL); + ASSERT (Head != NULL); + + Tcb = TcpLocateTcb ( + Head->DstPort, + Dst, + Head->SrcPort, + Src, + Version, + FALSE + ); + if (Tcb == NULL || Tcb->State == TCP_CLOSED) { + + goto CLEAN_EXIT; + } + + // + // Validate the sequence number. + // + Seq = NTOHL (Head->Seq); + if (!(TCP_SEQ_LEQ (Tcb->SndUna, Seq) && TCP_SEQ_LT (Seq, Tcb->SndNxt))) { + + goto CLEAN_EXIT; + } + + IcmpErrStatus = IpIoGetIcmpErrStatus ( + IcmpErr, + Tcb->Sk->IpVersion, + &IcmpErrIsHard, + &IcmpErrNotify + ); + + if (IcmpErrNotify) { + + SOCK_ERROR (Tcb->Sk, IcmpErrStatus); + } + + if (IcmpErrIsHard) { + + TcpClose (Tcb); + } + +CLEAN_EXIT: + + NetbufFree (Nbuf); +} diff --git a/NetworkPkg/TcpDxe/TcpIo.c b/NetworkPkg/TcpDxe/TcpIo.c new file mode 100644 index 000000000..c7c2977f5 --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpIo.c @@ -0,0 +1,186 @@ +/** @file + Implementation of I/O interfaces between TCP and IpIoLib. + + Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TcpMain.h" + +/** + Packet receive callback function provided to IP_IO, used to call + the proper function to handle the packet received by IP. + + @param[in] Status Result of the receive request. + @param[in] IcmpErr Valid when Status is EFI_ICMP_ERROR. + @param[in] NetSession The IP session for the received packet. + @param[in] Pkt Packet received. + @param[in] Context The data provided by the user for the received packet when + the callback is registered in IP_IO_OPEN_DATA::RcvdContext. + This is an optional parameter that may be NULL. + +**/ +VOID +EFIAPI +TcpRxCallback ( + IN EFI_STATUS Status, + IN UINT8 IcmpErr, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Pkt, + IN VOID *Context OPTIONAL + ) +{ + if (EFI_SUCCESS == Status) { + TcpInput (Pkt, &NetSession->Source, &NetSession->Dest, NetSession->IpVersion); + } else { + TcpIcmpInput ( + Pkt, + IcmpErr, + &NetSession->Source, + &NetSession->Dest, + NetSession->IpVersion + ); + } +} + +/** + Send the segment to IP via IpIo function. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Nbuf Pointer to the TCP segment to be sent. + @param[in] Src Source address of the TCP segment. + @param[in] Dest Destination address of the TCP segment. + @param[in] Version IP_VERSION_4 or IP_VERSION_6 + + @retval 0 The segment was sent out successfully. + @retval -1 The segment failed to send. + +**/ +INTN +TcpSendIpPacket ( + IN TCP_CB *Tcb, + IN NET_BUF *Nbuf, + IN EFI_IP_ADDRESS *Src, + IN EFI_IP_ADDRESS *Dest, + IN UINT8 Version + ) +{ + EFI_STATUS Status; + IP_IO *IpIo; + IP_IO_OVERRIDE Override; + SOCKET *Sock; + VOID *IpSender; + TCP_PROTO_DATA *TcpProto; + + if (NULL == Tcb) { + + IpIo = NULL; + IpSender = IpIoFindSender (&IpIo, Version, Src); + + if (IpSender == NULL) { + DEBUG ((EFI_D_WARN, "TcpSendIpPacket: No appropriate IpSender.\n")); + return -1; + } + + if (Version == IP_VERSION_6) { + // + // It's tricky here. EFI IPv6 Spec don't allow an instance overriding the + // destination address if the dest is already specified through the + // configuration data. Here we get the IpIo we need and use the default IP + // instance in this IpIo to send the packet. The dest address is configured + // to be the unspecified address for the default IP instance. + // + IpSender = NULL; + } + } else { + + Sock = Tcb->Sk; + TcpProto = (TCP_PROTO_DATA *) Sock->ProtoReserved; + IpIo = TcpProto->TcpService->IpIo; + IpSender = Tcb->IpInfo; + + if (Version == IP_VERSION_6) { + // + // It's IPv6 and this TCP segment belongs to a solid TCB, in such case + // the destination address can't be overridden, so reset the Dest to NULL. + // + if (!Tcb->RemoteIpZero) { + Dest = NULL; + } + } + } + + ASSERT (Version == IpIo->IpVersion); + + if (Version == IP_VERSION_4) { + Override.Ip4OverrideData.TypeOfService = 0; + Override.Ip4OverrideData.TimeToLive = 255; + Override.Ip4OverrideData.DoNotFragment = FALSE; + Override.Ip4OverrideData.Protocol = EFI_IP_PROTO_TCP; + ZeroMem (&Override.Ip4OverrideData.GatewayAddress, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Override.Ip4OverrideData.SourceAddress, Src, sizeof (EFI_IPv4_ADDRESS)); + } else { + Override.Ip6OverrideData.Protocol = EFI_IP_PROTO_TCP; + Override.Ip6OverrideData.HopLimit = 255; + Override.Ip6OverrideData.FlowLabel = 0; + } + + Status = IpIoSend (IpIo, Nbuf, IpSender, NULL, NULL, Dest, &Override); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "TcpSendIpPacket: return %r error\n", Status)); + return -1; + } + + return 0; +} + +/** + Refresh the remote peer's Neighbor Cache State if already exists. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Neighbor Source address of the TCP segment. + @param[in] Timeout Time in 100-ns units that this entry will remain + in the neighbor cache. A value of zero means that + the entry is permanent. A value of non-zero means + that the entry is dynamic and will be deleted + after Timeout. + + @retval EFI_SUCCESS Successfully updated the neighbor relationship. + @retval EFI_NOT_STARTED The IpIo is not configured. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources. + @retval EFI_NOT_FOUND This entry is not in the neighbor table. + +**/ +EFI_STATUS +Tcp6RefreshNeighbor ( + IN TCP_CB *Tcb, + IN EFI_IP_ADDRESS *Neighbor, + IN UINT32 Timeout + ) +{ + IP_IO *IpIo; + SOCKET *Sock; + TCP_PROTO_DATA *TcpProto; + + if (NULL == Tcb) { + IpIo = NULL; + IpIoFindSender (&IpIo, IP_VERSION_6, Neighbor); + + if (IpIo == NULL) { + DEBUG ((EFI_D_WARN, "Tcp6AddNeighbor: No appropriate IpIo.\n")); + return EFI_NOT_STARTED; + } + + } else { + Sock = Tcb->Sk; + TcpProto = (TCP_PROTO_DATA *) Sock->ProtoReserved; + IpIo = TcpProto->TcpService->IpIo; + } + + return IpIoRefreshNeighbor (IpIo, Neighbor, Timeout); +} + diff --git a/NetworkPkg/TcpDxe/TcpMain.c b/NetworkPkg/TcpDxe/TcpMain.c new file mode 100644 index 000000000..fa357cd01 --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpMain.c @@ -0,0 +1,1106 @@ +/** @file + Implementation of EFI_TCP4_PROTOCOL and EFI_TCP6_PROTOCOL. + + (C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TcpMain.h" + +/** + Check the integrity of the data buffer. + + @param[in] DataLen The total length of the data buffer. + @param[in] FragmentCount The fragment count of the fragment table. + @param[in] FragmentTable Pointer to the fragment table of the data + buffer. + + @retval EFI_SUCCESS The integrity check passed. + @retval EFI_INVALID_PARAMETER The integrity check failed. + +**/ +EFI_STATUS +TcpChkDataBuf ( + IN UINT32 DataLen, + IN UINT32 FragmentCount, + IN EFI_TCP4_FRAGMENT_DATA *FragmentTable + ) +{ + UINT32 Index; + + UINT32 Len; + + for (Index = 0, Len = 0; Index < FragmentCount; Index++) { + if (FragmentTable[Index].FragmentBuffer == NULL) { + return EFI_INVALID_PARAMETER; + } + Len = Len + FragmentTable[Index].FragmentLength; + } + + if (DataLen != Len) { + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + +/** + Get the current operational status. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[out] Tcp4State Pointer to the buffer to receive the current TCP + state. Optional parameter that may be NULL. + @param[out] Tcp4ConfigData Pointer to the buffer to receive the current TCP + configuration. Optional parameter that may be NULL. + @param[out] Ip4ModeData Pointer to the buffer to receive the current + IPv4 configuration. Optional parameter that may be NULL. + @param[out] MnpConfigData Pointer to the buffer to receive the current MNP + configuration data indirectly used by the TCPv4 + Instance. Optional parameter that may be NULL. + @param[out] SnpModeData Pointer to the buffer to receive the current SNP + configuration data indirectly used by the TCPv4 + Instance. Optional parameter that may be NULL. + + @retval EFI_SUCCESS The mode data was read. + @retval EFI_NOT_STARTED No configuration data is available because this + instance hasn't been started. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +Tcp4GetModeData ( + IN EFI_TCP4_PROTOCOL *This, + OUT EFI_TCP4_CONNECTION_STATE *Tcp4State OPTIONAL, + OUT EFI_TCP4_CONFIG_DATA *Tcp4ConfigData OPTIONAL, + OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ) +{ + TCP4_MODE_DATA TcpMode; + SOCKET *Sock; + + if (NULL == This) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + TcpMode.Tcp4State = Tcp4State; + TcpMode.Tcp4ConfigData = Tcp4ConfigData; + TcpMode.Ip4ModeData = Ip4ModeData; + TcpMode.MnpConfigData = MnpConfigData; + TcpMode.SnpModeData = SnpModeData; + + return SockGetMode (Sock, &TcpMode); +} + +/** + Initialize or brutally reset the operational parameters for + this EFI TCPv4 instance. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] TcpConfigData Pointer to the configure data to configure the + instance. Optional parameter that may be NULL. + + @retval EFI_SUCCESS The operational settings were set, changed, or + reset successfully. + @retval EFI_NO_MAPPING When using a default address, configuration + (through DHCP, BOOTP, RARP, etc.) is not + finished. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_ACCESS_DENIED Configuring TCP instance when it is already + configured. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources. + +**/ +EFI_STATUS +EFIAPI +Tcp4Configure ( + IN EFI_TCP4_PROTOCOL * This, + IN EFI_TCP4_CONFIG_DATA * TcpConfigData OPTIONAL + ) +{ + EFI_TCP4_OPTION *Option; + SOCKET *Sock; + EFI_STATUS Status; + IP4_ADDR Ip; + IP4_ADDR SubnetMask; + + if (NULL == This) { + return EFI_INVALID_PARAMETER; + } + + // + // Tcp protocol related parameter check will be conducted here + // + if (NULL != TcpConfigData) { + + CopyMem (&Ip, &TcpConfigData->AccessPoint.RemoteAddress, sizeof (IP4_ADDR)); + if (IP4_IS_LOCAL_BROADCAST (NTOHL (Ip))) { + return EFI_INVALID_PARAMETER; + } + + if (TcpConfigData->AccessPoint.ActiveFlag && (0 == TcpConfigData->AccessPoint.RemotePort || (Ip == 0))) { + return EFI_INVALID_PARAMETER; + } + + if (!TcpConfigData->AccessPoint.UseDefaultAddress) { + + CopyMem (&Ip, &TcpConfigData->AccessPoint.StationAddress, sizeof (IP4_ADDR)); + CopyMem (&SubnetMask, &TcpConfigData->AccessPoint.SubnetMask, sizeof (IP4_ADDR)); + if (!IP4_IS_VALID_NETMASK (NTOHL (SubnetMask)) || + (SubnetMask != 0 && !NetIp4IsUnicast (NTOHL (Ip), NTOHL (SubnetMask)))) { + return EFI_INVALID_PARAMETER; + } + } + + Option = TcpConfigData->ControlOption; + if ((NULL != Option) && (Option->EnableSelectiveAck || Option->EnablePathMtuDiscovery)) { + return EFI_UNSUPPORTED; + } + } + + Sock = SOCK_FROM_THIS (This); + + if (NULL == TcpConfigData) { + return SockFlush (Sock); + } + + Status = SockConfigure (Sock, TcpConfigData); + + if (EFI_NO_MAPPING == Status) { + Sock->ConfigureState = SO_NO_MAPPING; + } + + return Status; +} + +/** + Add or delete routing entries. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] DeleteRoute If TRUE, delete the specified route from routing + table; if FALSE, add the specified route to + routing table. + @param[in] SubnetAddress The destination network. + @param[in] SubnetMask The subnet mask for the destination network. + @param[in] GatewayAddress The gateway address for this route. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance has not been + configured. + @retval EFI_NO_MAPPING When using a default address, configuration + (through DHCP, BOOTP, RARP, etc.) is not + finished. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to add the + entry to the routing table. + @retval EFI_NOT_FOUND This route is not in the routing table. + @retval EFI_ACCESS_DENIED This route is already in the routing table. + @retval EFI_UNSUPPORTED The TCP driver does not support this operation. + +**/ +EFI_STATUS +EFIAPI +Tcp4Routes ( + IN EFI_TCP4_PROTOCOL *This, + IN BOOLEAN DeleteRoute, + IN EFI_IPv4_ADDRESS *SubnetAddress, + IN EFI_IPv4_ADDRESS *SubnetMask, + IN EFI_IPv4_ADDRESS *GatewayAddress + ) +{ + SOCKET *Sock; + TCP4_ROUTE_INFO RouteInfo; + + if (NULL == This) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + RouteInfo.DeleteRoute = DeleteRoute; + RouteInfo.SubnetAddress = SubnetAddress; + RouteInfo.SubnetMask = SubnetMask; + RouteInfo.GatewayAddress = GatewayAddress; + + return SockRoute (Sock, &RouteInfo); +} + +/** + Initiate a non-blocking TCP connection request for an active TCP instance. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] ConnectionToken Pointer to the connection token to return when + the TCP three way handshake finishes. + + @retval EFI_SUCCESS The connection request successfully + initiated. + @retval EFI_NOT_STARTED This EFI_TCP4_PROTOCOL instance hasn't been + configured. + @retval EFI_ACCESS_DENIED The instance is not configured as an active one, + or it is not in Tcp4StateClosed state. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES The driver can't allocate enough resources to + initiate the active open. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +Tcp4Connect ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_CONNECTION_TOKEN *ConnectionToken + ) +{ + SOCKET *Sock; + + if (NULL == This || NULL == ConnectionToken || NULL == ConnectionToken->CompletionToken.Event) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + return SockConnect (Sock, ConnectionToken); +} + +/** + Listen on the passive instance to accept an incoming connection request. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] ListenToken Pointer to the listen token to return when + operation finishes. + + @retval EFI_SUCCESS The listen token was queued successfully. + @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been + configured. + @retval EFI_ACCESS_DENIED The instatnce is not a passive one or it is not + in Tcp4StateListen state or a same listen token + has already existed in the listen token queue of + this TCP instance. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish + the operation. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +Tcp4Accept ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_LISTEN_TOKEN *ListenToken + ) +{ + SOCKET *Sock; + + if (NULL == This || NULL == ListenToken || NULL == ListenToken->CompletionToken.Event) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + return SockAccept (Sock, ListenToken); +} + +/** + Queues outgoing data into the transmit queue + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] Token Pointer to the completion token to queue to the + transmit queue. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been + configured. + @retval EFI_NO_MAPPING When using a default address, configuration + (DHCP, BOOTP, RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid + @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE: + * A transmit completion token with the same + Token-> CompletionToken.Event was already in the + transmission queue. * The current instance is in + Tcp4StateClosed state. * The current instance is + a passive one and it is in Tcp4StateListen + state. * User has called Close() to disconnect + this connection. + @retval EFI_NOT_READY The completion token could not be queued because + the transmit queue is full. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data because of a + resource shortage. + @retval EFI_NETWORK_UNREACHABLE There is no route to the destination network or + address. + +**/ +EFI_STATUS +EFIAPI +Tcp4Transmit ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_IO_TOKEN *Token + ) +{ + SOCKET *Sock; + EFI_STATUS Status; + + if (NULL == This || + NULL == Token || + NULL == Token->CompletionToken.Event || + NULL == Token->Packet.TxData || + 0 == Token->Packet.TxData->FragmentCount || + 0 == Token->Packet.TxData->DataLength + ) { + return EFI_INVALID_PARAMETER; + } + + Status = TcpChkDataBuf ( + Token->Packet.TxData->DataLength, + Token->Packet.TxData->FragmentCount, + Token->Packet.TxData->FragmentTable + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Sock = SOCK_FROM_THIS (This); + + return SockSend (Sock, Token); +} + +/** + Place an asynchronous receive request into the receiving queue. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] Token Pointer to a token that is associated with the + receive data descriptor. + + @retval EFI_SUCCESS The receive completion token was cached + @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been + configured. + @retval EFI_NO_MAPPING When using a default address, configuration + (DHCP, BOOTP, RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued + due to a lack of system resources. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE: + * A receive completion token with the same + Token->CompletionToken.Event was already in the + receive queue. * The current instance is in + Tcp4StateClosed state. * The current instance is + a passive one and it is in Tcp4StateListen + state. * User has called Close() to disconnect + this connection. + @retval EFI_CONNECTION_FIN The communication peer has closed the connection, + and there is no any buffered data in the receive + buffer of this instance. + @retval EFI_NOT_READY The receive request could not be queued because + the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +Tcp4Receive ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_IO_TOKEN *Token + ) +{ + SOCKET *Sock; + EFI_STATUS Status; + + if (NULL == This || + NULL == Token || + NULL == Token->CompletionToken.Event || + NULL == Token->Packet.RxData || + 0 == Token->Packet.RxData->FragmentCount || + 0 == Token->Packet.RxData->DataLength + ) { + return EFI_INVALID_PARAMETER; + } + + Status = TcpChkDataBuf ( + Token->Packet.RxData->DataLength, + Token->Packet.RxData->FragmentCount, + Token->Packet.RxData->FragmentTable + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Sock = SOCK_FROM_THIS (This); + + return SockRcv (Sock, Token); + +} + +/** + Disconnecting a TCP connection gracefully or reset a TCP connection. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] CloseToken Pointer to the close token to return when + operation finishes. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been + configured. + @retval EFI_ACCESS_DENIED One or more of the following are TRUE: * + Configure() has been called with TcpConfigData + set to NULL, and this function has not returned. + * Previous Close() call on this instance has not + finished. + @retval EFI_INVALID_PARAMETER One ore more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish the + operation. + @retval EFI_DEVICE_ERROR Any unexpected category error not belonging to those + listed above. + +**/ +EFI_STATUS +EFIAPI +Tcp4Close ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_CLOSE_TOKEN *CloseToken + ) +{ + SOCKET *Sock; + + if (NULL == This || NULL == CloseToken || NULL == CloseToken->CompletionToken.Event) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + return SockClose (Sock, CloseToken, CloseToken->AbortOnClose); +} + +/** + Abort an asynchronous connection, listen, transmission or receive request. + + @param This The pointer to the EFI_TCP4_PROTOCOL instance. + @param Token The pointer to a token that has been issued by + EFI_TCP4_PROTOCOL.Connect(), + EFI_TCP4_PROTOCOL.Accept(), + EFI_TCP4_PROTOCOL.Transmit() or + EFI_TCP4_PROTOCOL.Receive(). If NULL, all pending + tokens issued by above four functions will be aborted. Type + EFI_TCP4_COMPLETION_TOKEN is defined in + EFI_TCP4_PROTOCOL.Connect(). + + @retval EFI_SUCCESS The asynchronous I/O request is aborted and Token->Event + is signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance hasn't been configured. + @retval EFI_NO_MAPPING When using the default address, configuration + (DHCP, BOOTP,RARP, etc.) hasn't finished yet. + @retval EFI_NOT_FOUND The asynchronous I/O request isn't found in the + transmission or receive queue. It has either + completed or wasn't issued by Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +Tcp4Cancel ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + SOCKET *Sock; + + if (NULL == This) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + return SockCancel (Sock, Token); +} + +/** + Poll to receive incoming data and transmit outgoing segments. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_NOT_READY No incoming or outgoing data was processed. + @retval EFI_TIMEOUT Data was dropped out of the transmission or + receive queue. Consider increasing the polling + rate. + +**/ +EFI_STATUS +EFIAPI +Tcp4Poll ( + IN EFI_TCP4_PROTOCOL *This + ) +{ + SOCKET *Sock; + EFI_STATUS Status; + + if (NULL == This) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + Status = Sock->ProtoHandler (Sock, SOCK_POLL, NULL); + + return Status; +} + +/** + Get the current operational status. + + The GetModeData() function copies the current operational settings of this EFI TCPv6 + Protocol instance into user-supplied buffers. This function can also be used to retrieve + the operational setting of underlying drivers such as IPv6, MNP, or SNP. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[out] Tcp6State The buffer in which the current TCP state is + returned. Optional parameter that may be NULL. + @param[out] Tcp6ConfigData The buffer in which the current TCP configuration + is returned. Optional parameter that may be NULL. + @param[out] Ip6ModeData The buffer in which the current IPv6 configuration + data used by the TCP instance is returned. + Optional parameter that may be NULL. + @param[out] MnpConfigData The buffer in which the current MNP configuration + data indirectly used by the TCP instance is returned. + Optional parameter that may be NULL. + @param[out] SnpModeData The buffer in which the current SNP mode data + indirectly used by the TCP instance is returned. + Optional parameter that may be NULL. + + @retval EFI_SUCCESS The mode data was read. + @retval EFI_NOT_STARTED No configuration data is available because this instance hasn't + been started. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +Tcp6GetModeData ( + IN EFI_TCP6_PROTOCOL *This, + OUT EFI_TCP6_CONNECTION_STATE *Tcp6State OPTIONAL, + OUT EFI_TCP6_CONFIG_DATA *Tcp6ConfigData OPTIONAL, + OUT EFI_IP6_MODE_DATA *Ip6ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ) +{ + TCP6_MODE_DATA TcpMode; + SOCKET *Sock; + + if (NULL == This) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + TcpMode.Tcp6State = Tcp6State; + TcpMode.Tcp6ConfigData = Tcp6ConfigData; + TcpMode.Ip6ModeData = Ip6ModeData; + TcpMode.MnpConfigData = MnpConfigData; + TcpMode.SnpModeData = SnpModeData; + + return SockGetMode (Sock, &TcpMode); +} + +/** + Initialize or brutally reset the operational parameters for this EFI TCPv6 instance. + + The Configure() function does the following: + - Initialize this TCP instance, i.e., initialize the communication end settings and + specify active open or passive open for an instance. + - Reset this TCP instance brutally, i.e., cancel all pending asynchronous tokens, flush + transmission and receiving buffer directly without informing the communication peer. + + No other TCPv6 Protocol operation except Poll() can be executed by this instance until + it is configured properly. For an active TCP instance, after a proper configuration it + may call Connect() to initiate a three-way handshake. For a passive TCP instance, + its state transits to Tcp6StateListen after configuration, and Accept() may be + called to listen the incoming TCP connection requests. If Tcp6ConfigData is set to NULL, + the instance is reset. The resetting process will be done brutally, the state machine will + be set to Tcp6StateClosed directly, the receive queue and transmit queue will be flushed, + and no traffic is allowed through this instance. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] Tcp6ConfigData Pointer to the configure data to configure the instance. + If Tcp6ConfigData is set to NULL, the instance is reset. + + @retval EFI_SUCCESS The operational settings were set, changed, or reset + successfully. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for + use. + @retval EFI_INVALID_PARAMETER One or more of the following conditions are TRUE: + - This is NULL. + - Tcp6ConfigData->AccessPoint.StationAddress is neither zero nor + one of the configured IP addresses in the underlying IPv6 driver. + - Tcp6ConfigData->AccessPoint.RemoteAddress isn't a valid unicast + IPv6 address. + - Tcp6ConfigData->AccessPoint.RemoteAddress is zero or + Tcp6ConfigData->AccessPoint.RemotePort is zero when + Tcp6ConfigData->AccessPoint.ActiveFlag is TRUE. + - A same access point has been configured in other TCP + instance properly. + @retval EFI_ACCESS_DENIED Configuring a TCP instance when it is configured without + calling Configure() with NULL to reset it. + @retval EFI_UNSUPPORTED One or more of the control options are not supported in + the implementation. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when + executing Configure(). + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + +**/ +EFI_STATUS +EFIAPI +Tcp6Configure ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_CONFIG_DATA *Tcp6ConfigData OPTIONAL + ) +{ + EFI_TCP6_OPTION *Option; + SOCKET *Sock; + EFI_STATUS Status; + EFI_IPv6_ADDRESS *Ip; + + if (NULL == This) { + return EFI_INVALID_PARAMETER; + } + + // + // Tcp protocol related parameter check will be conducted here + // + if (NULL != Tcp6ConfigData) { + + Ip = &Tcp6ConfigData->AccessPoint.RemoteAddress; + if (!NetIp6IsUnspecifiedAddr (Ip) && !NetIp6IsValidUnicast (Ip)) { + return EFI_INVALID_PARAMETER; + } + + if (Tcp6ConfigData->AccessPoint.ActiveFlag && + (0 == Tcp6ConfigData->AccessPoint.RemotePort || NetIp6IsUnspecifiedAddr (Ip)) + ) { + return EFI_INVALID_PARAMETER; + } + + Ip = &Tcp6ConfigData->AccessPoint.StationAddress; + if (!NetIp6IsUnspecifiedAddr (Ip) && !NetIp6IsValidUnicast (Ip)) { + return EFI_INVALID_PARAMETER; + } + + Option = Tcp6ConfigData->ControlOption; + if ((NULL != Option) && (Option->EnableSelectiveAck || Option->EnablePathMtuDiscovery)) { + return EFI_UNSUPPORTED; + } + } + + Sock = SOCK_FROM_THIS (This); + + if (NULL == Tcp6ConfigData) { + return SockFlush (Sock); + } + + Status = SockConfigure (Sock, Tcp6ConfigData); + + if (EFI_NO_MAPPING == Status) { + Sock->ConfigureState = SO_NO_MAPPING; + } + + return Status; +} + +/** + Initiate a nonblocking TCP connection request for an active TCP instance. + + The Connect() function will initiate an active open to the remote peer configured + in a current TCP instance if it is configured active. If the connection succeeds or + fails due to any error, the ConnectionToken->CompletionToken.Event will be signaled + and ConnectionToken->CompletionToken.Status will be updated accordingly. This + function can only be called for the TCP instance in the Tcp6StateClosed state. The + instance will transfer into Tcp6StateSynSent if the function returns EFI_SUCCESS. + If a TCP three-way handshake succeeds, its state will become Tcp6StateEstablished. + Otherwise, the state will return to Tcp6StateClosed. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] ConnectionToken Pointer to the connection token to return when the TCP three + way handshake finishes. + + @retval EFI_SUCCESS The connection request successfully initiated and the state of + this TCP instance has been changed to Tcp6StateSynSent. + @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured. + @retval EFI_ACCESS_DENIED One or more of the following conditions are TRUE: + - This instance is not configured as an active one. + - This instance is not in Tcp6StateClosed state. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + - This is NULL. + - ConnectionToken is NULL. + - ConnectionToken->CompletionToken.Event is NULL. + @retval EFI_OUT_OF_RESOURCES The driver can't allocate enough resources to initiate the active open. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +Tcp6Connect ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_CONNECTION_TOKEN *ConnectionToken + ) +{ + SOCKET *Sock; + + if (NULL == This || NULL == ConnectionToken || NULL == ConnectionToken->CompletionToken.Event) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + return SockConnect (Sock, ConnectionToken); +} + +/** + Listen on the passive instance to accept an incoming connection request. This is a + nonblocking operation. + + The Accept() function initiates an asynchronous accept request to wait for an incoming + connection on the passive TCP instance. If a remote peer successfully establishes a + connection with this instance, a new TCP instance will be created and its handle will + be returned in ListenToken->NewChildHandle. The newly created instance is configured + by inheriting the passive instance's configuration and is ready for use upon return. + The new instance is in the Tcp6StateEstablished state. + + The ListenToken->CompletionToken.Event will be signaled when a new connection is + accepted, when a user aborts the listen or when a connection is reset. + + This function only can be called when a current TCP instance is in Tcp6StateListen state. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] ListenToken Pointer to the listen token to return when operation finishes. + + + @retval EFI_SUCCESS The listen token queued successfully. + @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured. + @retval EFI_ACCESS_DENIED One or more of the following are TRUE: + - This instance is not a passive instance. + - This instance is not in Tcp6StateListen state. + - The same listen token has already existed in the listen + token queue of this TCP instance. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + - This is NULL. + - ListenToken is NULL. + - ListentToken->CompletionToken.Event is NULL. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough resource to finish the operation. + @retval EFI_DEVICE_ERROR Any unexpected error not belonging to a category listed above. + +**/ +EFI_STATUS +EFIAPI +Tcp6Accept ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_LISTEN_TOKEN *ListenToken + ) +{ + SOCKET *Sock; + + if (NULL == This || NULL == ListenToken || NULL == ListenToken->CompletionToken.Event) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + return SockAccept (Sock, ListenToken); +} + +/** + Queues outgoing data into the transmit queue. + + The Transmit() function queues a sending request to this TCP instance along with the + user data. The status of the token is updated and the event in the token will be + signaled once the data is sent out or an error occurs. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] Token Pointer to the completion token to queue to the transmit queue. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a + source address for this instance, but no source address was + available for use. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + - This is NULL. + - Token is NULL. + - Token->CompletionToken.Event is NULL. + - Token->Packet.TxData is NULL. + - Token->Packet.FragmentCount is zero. + - Token->Packet.DataLength is not equal to the sum of fragment lengths. + @retval EFI_ACCESS_DENIED One or more of the following conditions are TRUE: + - A transmit completion token with the same Token-> + CompletionToken.Event was already in the + transmission queue. + - The current instance is in Tcp6StateClosed state. + - The current instance is a passive one and it is in + Tcp6StateListen state. + - User has called Close() to disconnect this connection. + @retval EFI_NOT_READY The completion token could not be queued because the + transmit queue is full. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data because of resource + shortage. + @retval EFI_NETWORK_UNREACHABLE There is no route to the destination network or address. + +**/ +EFI_STATUS +EFIAPI +Tcp6Transmit ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_IO_TOKEN *Token + ) +{ + SOCKET *Sock; + EFI_STATUS Status; + + if (NULL == This || + NULL == Token || + NULL == Token->CompletionToken.Event || + NULL == Token->Packet.TxData || + 0 == Token->Packet.TxData->FragmentCount || + 0 == Token->Packet.TxData->DataLength + ) { + return EFI_INVALID_PARAMETER; + } + + Status = TcpChkDataBuf ( + Token->Packet.TxData->DataLength, + Token->Packet.TxData->FragmentCount, + (EFI_TCP4_FRAGMENT_DATA *) Token->Packet.TxData->FragmentTable + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Sock = SOCK_FROM_THIS (This); + + return SockSend (Sock, Token); +} + +/** + Places an asynchronous receive request into the receiving queue. + + The Receive() function places a completion token into the receive packet queue. This + function is always asynchronous. The caller must allocate the Token->CompletionToken.Event + and the FragmentBuffer used to receive data. The caller also must fill the DataLength that + represents the whole length of all FragmentBuffer. When the receive operation completes, the + EFI TCPv6 Protocol driver updates the Token->CompletionToken.Status and Token->Packet.RxData + fields, and the Token->CompletionToken.Event is signaled. If data obtained, the data and its length + will be copied into the FragmentTable; at the same time the full length of received data will + be recorded in the DataLength fields. Providing a proper notification function and context + for the event enables the user to receive the notification and receiving status. That + notification function is guaranteed to not be re-entered. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] Token Pointer to a token that is associated with the receive data + descriptor. + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for use. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Token is NULL. + - Token->CompletionToken.Event is NULL. + - Token->Packet.RxData is NULL. + - Token->Packet.RxData->DataLength is 0. + - The Token->Packet.RxData->DataLength is not the + sum of all FragmentBuffer length in FragmentTable. + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of + system resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The EFI TCPv6 Protocol instance has been reset to startup defaults. + @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE: + - A receive completion token with the same Token->CompletionToken.Event + was already in the receive queue. + - The current instance is in Tcp6StateClosed state. + - The current instance is a passive one and it is in + Tcp6StateListen state. + - User has called Close() to disconnect this connection. + @retval EFI_CONNECTION_FIN The communication peer has closed the connection and there is no + buffered data in the receive buffer of this instance. + @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +Tcp6Receive ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_IO_TOKEN *Token + ) +{ + SOCKET *Sock; + EFI_STATUS Status; + + if (NULL == This || + NULL == Token || + NULL == Token->CompletionToken.Event || + NULL == Token->Packet.RxData || + 0 == Token->Packet.RxData->FragmentCount || + 0 == Token->Packet.RxData->DataLength + ) { + return EFI_INVALID_PARAMETER; + } + + Status = TcpChkDataBuf ( + Token->Packet.RxData->DataLength, + Token->Packet.RxData->FragmentCount, + (EFI_TCP4_FRAGMENT_DATA *) Token->Packet.RxData->FragmentTable + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Sock = SOCK_FROM_THIS (This); + + return SockRcv (Sock, Token); +} + +/** + Disconnecting a TCP connection gracefully or reset a TCP connection. This function is a + nonblocking operation. + + Initiate an asynchronous close token to the TCP driver. After Close() is called, any buffered + transmission data will be sent by the TCP driver, and the current instance will have a graceful close + working flow described as RFC 793 if AbortOnClose is set to FALSE. Otherwise, a rest packet + will be sent by TCP driver to fast disconnect this connection. When the close operation completes + successfully the TCP instance is in Tcp6StateClosed state, all pending asynchronous + operations are signaled, and any buffers used for TCP network traffic are flushed. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] CloseToken Pointer to the close token to return when operation finishes. + + @retval EFI_SUCCESS The Close() was called successfully. + @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured. + @retval EFI_ACCESS_DENIED One or more of the following are TRUE: + - CloseToken or CloseToken->CompletionToken.Event is already in use. + - Previous Close() call on this instance has not finished. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + - This is NULL. + - CloseToken is NULL. + - CloseToken->CompletionToken.Event is NULL. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough resource to finish the operation. + @retval EFI_DEVICE_ERROR Any unexpected error not belonging to error categories given above. + +**/ +EFI_STATUS +EFIAPI +Tcp6Close ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_CLOSE_TOKEN *CloseToken + ) +{ + SOCKET *Sock; + + if (NULL == This || NULL == CloseToken || NULL == CloseToken->CompletionToken.Event) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + return SockClose (Sock, CloseToken, CloseToken->AbortOnClose); +} + +/** + Abort an asynchronous connection, listen, transmission or receive request. + + The Cancel() function aborts a pending connection, listen, transmit or + receive request. + + If Token is not NULL and the token is in the connection, listen, transmission + or receive queue when it is being cancelled, its Token->Status will be set + to EFI_ABORTED and then Token->Event will be signaled. + + If the token is not in one of the queues, which usually means that the + asynchronous operation has completed, EFI_NOT_FOUND is returned. + + If Token is NULL all asynchronous token issued by Connect(), Accept(), + Transmit() and Receive() will be aborted. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_TCP6_PROTOCOL.Connect(), + EFI_TCP6_PROTOCOL.Accept(), + EFI_TCP6_PROTOCOL.Transmit() or + EFI_TCP6_PROTOCOL.Receive(). If NULL, all pending + tokens issued by above four functions will be aborted. Type + EFI_TCP6_COMPLETION_TOKEN is defined in + EFI_TCP_PROTOCOL.Connect(). + + @retval EFI_SUCCESS The asynchronous I/O request is aborted and Token->Event + is signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance hasn't been configured. + @retval EFI_NOT_FOUND The asynchronous I/O request isn't found in the transmission or + receive queue. It has either completed or wasn't issued by + Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +Tcp6Cancel ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + SOCKET *Sock; + + if (NULL == This) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + return SockCancel (Sock, Token); +} + +/** + Poll to receive incoming data and transmit outgoing segments. + + The Poll() function increases the rate that data is moved between the network + and application, and can be called when the TCP instance is created successfully. + Its use is optional. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_NOT_READY No incoming or outgoing data is processed. + @retval EFI_TIMEOUT Data was dropped out of the transmission or receive queue. + Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +Tcp6Poll ( + IN EFI_TCP6_PROTOCOL *This + ) +{ + SOCKET *Sock; + EFI_STATUS Status; + + if (NULL == This) { + return EFI_INVALID_PARAMETER; + } + + Sock = SOCK_FROM_THIS (This); + + Status = Sock->ProtoHandler (Sock, SOCK_POLL, NULL); + + return Status; +} + diff --git a/NetworkPkg/TcpDxe/TcpMain.h b/NetworkPkg/TcpDxe/TcpMain.h new file mode 100644 index 000000000..69b35427a --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpMain.h @@ -0,0 +1,777 @@ +/** @file + Declaration of protocol interfaces in EFI_TCP4_PROTOCOL and EFI_TCP6_PROTOCOL. + It is the common head file for all Tcp*.c in TCP driver. + + Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _TCP_MAIN_H_ +#define _TCP_MAIN_H_ + +#include +#include +#include +#include +#include + +#include "Socket.h" +#include "TcpProto.h" +#include "TcpDriver.h" +#include "TcpFunc.h" + +extern UINT16 mTcp4RandomPort; +extern UINT16 mTcp6RandomPort; +extern CHAR16 *mTcpStateName[]; +extern EFI_COMPONENT_NAME_PROTOCOL gTcpComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gTcpComponentName2; +extern EFI_UNICODE_STRING_TABLE *gTcpControllerNameTable; + +extern LIST_ENTRY mTcpRunQue; +extern LIST_ENTRY mTcpListenQue; +extern TCP_SEQNO mTcpGlobalIss; +extern UINT32 mTcpTick; + +/// +/// 30 seconds. +/// +#define TCP6_KEEP_NEIGHBOR_TIME 30 +/// +/// 5 seconds, since 1 tick equals 200ms. +/// +#define TCP6_REFRESH_NEIGHBOR_TICK 25 + +#define TCP_EXPIRE_TIME 65535 + +/// +/// The implementation selects the initial send sequence number and the unit to +/// be added when it is increased. +/// +#define TCP_BASE_ISS 0x4d7e980b +#define TCP_ISS_INCREMENT_1 2048 +#define TCP_ISS_INCREMENT_2 100 + +typedef union { + EFI_TCP4_CONFIG_DATA Tcp4CfgData; + EFI_TCP6_CONFIG_DATA Tcp6CfgData; +} TCP_CONFIG_DATA; + +typedef union { + EFI_TCP4_ACCESS_POINT Tcp4Ap; + EFI_TCP6_ACCESS_POINT Tcp6Ap; +} TCP_ACCESS_POINT; + +typedef struct _TCP4_MODE_DATA { + EFI_TCP4_CONNECTION_STATE *Tcp4State; + EFI_TCP4_CONFIG_DATA *Tcp4ConfigData; + EFI_IP4_MODE_DATA *Ip4ModeData; + EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData; + EFI_SIMPLE_NETWORK_MODE *SnpModeData; +} TCP4_MODE_DATA; + +typedef struct _TCP6_MODE_DATA { + EFI_TCP6_CONNECTION_STATE *Tcp6State; + EFI_TCP6_CONFIG_DATA *Tcp6ConfigData; + EFI_IP6_MODE_DATA *Ip6ModeData; + EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData; + EFI_SIMPLE_NETWORK_MODE *SnpModeData; +} TCP6_MODE_DATA; + +typedef struct _TCP4_ROUTE_INFO { + BOOLEAN DeleteRoute; + EFI_IPv4_ADDRESS *SubnetAddress; + EFI_IPv4_ADDRESS *SubnetMask; + EFI_IPv4_ADDRESS *GatewayAddress; +} TCP4_ROUTE_INFO; + +typedef struct { + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; +} TCP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT; + +// +// EFI_TCP4_PROTOCOL definitions. +// + +/** + Get the current operational status. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[out] Tcp4State Pointer to the buffer to receive the current TCP + state. Optional parameter that may be NULL. + @param[out] Tcp4ConfigData Pointer to the buffer to receive the current TCP + configuration. Optional parameter that may be NULL. + @param[out] Ip4ModeData Pointer to the buffer to receive the current + IPv4 configuration. Optional parameter that may be NULL. + @param[out] MnpConfigData Pointer to the buffer to receive the current MNP + configuration data indirectly used by the TCPv4 + Instance. Optional parameter that may be NULL. + @param[out] SnpModeData Pointer to the buffer to receive the current SNP + configuration data indirectly used by the TCPv4 + Instance. Optional parameter that may be NULL. + + @retval EFI_SUCCESS The mode data was read. + @retval EFI_NOT_STARTED No configuration data is available because this + instance hasn't been started. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +Tcp4GetModeData ( + IN EFI_TCP4_PROTOCOL *This, + OUT EFI_TCP4_CONNECTION_STATE *Tcp4State OPTIONAL, + OUT EFI_TCP4_CONFIG_DATA *Tcp4ConfigData OPTIONAL, + OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ); + +/** + Initialize or brutally reset the operational parameters for + this EFI TCPv4 instance. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] TcpConfigData Pointer to the configure data to configure the + instance. Optional parameter that may be NULL. + + @retval EFI_SUCCESS The operational settings are set, changed, or + reset successfully. + @retval EFI_NO_MAPPING When using a default address, configuration + (through DHCP, BOOTP, RARP, etc.) is not + finished. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_ACCESS_DENIED Configuring the TCP instance when it is already + configured. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + @retval EFI_UNSUPPORTED One or more of the control options are not + supported in the implementation. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources. + +**/ +EFI_STATUS +EFIAPI +Tcp4Configure ( + IN EFI_TCP4_PROTOCOL * This, + IN EFI_TCP4_CONFIG_DATA * TcpConfigData OPTIONAL + ); + +/** + Add or delete routing entries. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] DeleteRoute If TRUE, delete the specified route from routing + table; if FALSE, add the specified route to + routing table. + @param[in] SubnetAddress The destination network. + @param[in] SubnetMask The subnet mask for the destination network. + @param[in] GatewayAddress The gateway address for this route. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance has not been + configured. + @retval EFI_NO_MAPPING When using a default address, configuration + (through DHCP, BOOTP, RARP, etc.) is not + finished. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to add the + entry to the routing table. + @retval EFI_NOT_FOUND This route is not in the routing table. + @retval EFI_ACCESS_DENIED This route is already in the routing table. + @retval EFI_UNSUPPORTED The TCP driver does not support this operation. + +**/ +EFI_STATUS +EFIAPI +Tcp4Routes ( + IN EFI_TCP4_PROTOCOL *This, + IN BOOLEAN DeleteRoute, + IN EFI_IPv4_ADDRESS *SubnetAddress, + IN EFI_IPv4_ADDRESS *SubnetMask, + IN EFI_IPv4_ADDRESS *GatewayAddress + ); + +/** + Initiate a nonblocking TCP connection request for an active TCP instance. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] ConnectionToken Pointer to the connection token to return when + the TCP three way handshake finishes. + + @retval EFI_SUCCESS The connection request is successfully + initiated. + @retval EFI_NOT_STARTED This EFI_TCP4_PROTOCOL instance hasn't been + configured. + @retval EFI_ACCESS_DENIED The instance is not configured as an active one + or it is not in Tcp4StateClosed state. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES The driver can't allocate enough resources to + initiate the active open. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +Tcp4Connect ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_CONNECTION_TOKEN *ConnectionToken + ); + +/** + Listen on the passive instance to accept an incoming connection request. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] ListenToken Pointer to the listen token to return when + operation finishes. + + @retval EFI_SUCCESS The listen token has been queued successfully. + @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been + configured. + @retval EFI_ACCESS_DENIED The instatnce is not a passive one or it is not + in Tcp4StateListen state, or a same listen token + has already existed in the listen token queue of + this TCP instance. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish + the operation. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +Tcp4Accept ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_LISTEN_TOKEN *ListenToken + ); + +/** + Queues outgoing data into the transmit queue + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance + @param[in] Token Pointer to the completion token to queue to the + transmit queue + + @retval EFI_SUCCESS The data has been queued for transmission + @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been + configured. + @retval EFI_NO_MAPPING When using a default address, configuration + (DHCP, BOOTP, RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid + @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE: + * A transmit completion token with the same + Token-> CompletionToken.Event was already in the + transmission queue. * The current instance is in + Tcp4StateClosed state * The current instance is + a passive one and it is in Tcp4StateListen + state. * User has called Close() to disconnect + this connection. + @retval EFI_NOT_READY The completion token could not be queued because + the transmit queue is full. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data because of a + resource shortage. + @retval EFI_NETWORK_UNREACHABLE There is no route to the destination network or + address. + +**/ +EFI_STATUS +EFIAPI +Tcp4Transmit ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_IO_TOKEN *Token + ); + +/** + Place an asynchronous receive request into the receiving queue. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] Token Pointer to a token that is associated with the + receive data descriptor. + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been + configured. + @retval EFI_NO_MAPPING When using a default address, configuration + (DHCP, BOOTP, RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued + due to a lack of system resources. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE: + * A receive completion token with the same + Token->CompletionToken.Event was already in the + receive queue. * The current instance is in + Tcp4StateClosed state. * The current instance is + a passive one and it is in Tcp4StateListen + state. * User has called Close() to disconnect + this connection. + @retval EFI_CONNECTION_FIN The communication peer has closed the connection + and there is no buffered data in the receive + buffer of this instance. + @retval EFI_NOT_READY The receive request could not be queued because + the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +Tcp4Receive ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_IO_TOKEN *Token + ); + +/** + Disconnecting a TCP connection gracefully or reset a TCP connection. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + @param[in] CloseToken Pointer to the close token to return when + operation finishes. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been + configured. + @retval EFI_ACCESS_DENIED One or more of the following are TRUE: * + Configure() has been called with TcpConfigData + set to NULL and this function has not returned. + * Previous Close() call on this instance has not + finished. + @retval EFI_INVALID_PARAMETER One ore more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish the + operation. + @retval EFI_DEVICE_ERROR Any unexpected error not belonging to the error + categories given above. + +**/ +EFI_STATUS +EFIAPI +Tcp4Close ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_CLOSE_TOKEN *CloseToken + ); + +/** + Abort an asynchronous connection, listen, transmission or receive request. + + @param This The pointer to the EFI_TCP4_PROTOCOL instance. + @param Token The pointer to a token that has been issued by + EFI_TCP4_PROTOCOL.Connect(), + EFI_TCP4_PROTOCOL.Accept(), + EFI_TCP4_PROTOCOL.Transmit() or + EFI_TCP4_PROTOCOL.Receive(). If NULL, all pending + tokens issued by above four functions will be aborted. Type + EFI_TCP4_COMPLETION_TOKEN is defined in + EFI_TCP4_PROTOCOL.Connect(). + + @retval EFI_SUCCESS The asynchronous I/O request is aborted and Token->Event + is signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance hasn't been configured. + @retval EFI_NO_MAPPING When using the default address, configuration + (DHCP, BOOTP,RARP, etc.) hasn't finished yet. + @retval EFI_NOT_FOUND The asynchronous I/O request isn't found in the + transmission or receive queue. It has either + completed or wasn't issued by Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +Tcp4Cancel ( + IN EFI_TCP4_PROTOCOL *This, + IN EFI_TCP4_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + Poll to receive incoming data and transmit outgoing segments. + + @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_NOT_READY No incoming or outgoing data was processed. + @retval EFI_TIMEOUT Data was dropped out of the transmission or + receive queue. Consider increasing the polling + rate. + +**/ +EFI_STATUS +EFIAPI +Tcp4Poll ( + IN EFI_TCP4_PROTOCOL *This + ); + +// +// EFI_TCP6_PROTOCOL definitions. +// + +/** + Get the current operational status. + + The GetModeData() function copies the current operational settings of this EFI TCPv6 + Protocol instance into user-supplied buffers. This function can also be used to retrieve + the operational setting of underlying drivers such as IPv6, MNP, or SNP. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[out] Tcp6State The buffer in which the current TCP state is + returned. Optional parameter that may be NULL. + @param[out] Tcp6ConfigData The buffer in which the current TCP configuration + is returned. Optional parameter that may be NULL. + @param[out] Ip6ModeData The buffer in which the current IPv6 configuration + data used by the TCP instance is returned. + Optional parameter that may be NULL. + @param[out] MnpConfigData The buffer in which the current MNP configuration + data used indirectly by the TCP instance is returned. + Optional parameter that may be NULL. + @param[out] SnpModeData The buffer in which the current SNP mode data + used indirectly by the TCP instance is returned. + Optional parameter that may be NULL. + + @retval EFI_SUCCESS The mode data was read. + @retval EFI_NOT_STARTED No configuration data is available because this instance hasn't + been started. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +Tcp6GetModeData ( + IN EFI_TCP6_PROTOCOL *This, + OUT EFI_TCP6_CONNECTION_STATE *Tcp6State OPTIONAL, + OUT EFI_TCP6_CONFIG_DATA *Tcp6ConfigData OPTIONAL, + OUT EFI_IP6_MODE_DATA *Ip6ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ); + +/** + Initialize or brutally reset the operational parameters for this EFI TCPv6 instance. + + The Configure() function does the following: + - Initialize this TCP instance, i.e., initialize the communication end settings and + specify active open or passive open for an instance. + - Reset this TCP instance brutally, i.e., cancel all pending asynchronous tokens, flush + transmission and receiving buffer directly without informing the communication peer. + + No other TCPv6 Protocol operation except Poll() can be executed by this instance until + it is configured properly. For an active TCP instance, after a proper configuration it + may call Connect() to initiates the three-way handshake. For a passive TCP instance, + its state will transit to Tcp6StateListen after configuration, and Accept() may be + called to listen the incoming TCP connection requests. If Tcp6ConfigData is set to NULL, + the instance is reset. Resetting process will be done brutally, the state machine will + be set to Tcp6StateClosed directly, the receive queue and transmit queue will be flushed, + and no traffic is allowed through this instance. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] Tcp6ConfigData Pointer to the configure data to configure the instance. + If Tcp6ConfigData is set to NULL, the instance is reset. + + @retval EFI_SUCCESS The operational settings were set, changed, or reset + successfully. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for + use. + @retval EFI_INVALID_PARAMETER One or more of the following conditions are TRUE: + - This is NULL. + - Tcp6ConfigData->AccessPoint.StationAddress is neither zero nor + one of the configured IP addresses in the underlying IPv6 driver. + - Tcp6ConfigData->AccessPoint.RemoteAddress isn't a valid unicast + IPv6 address. + - Tcp6ConfigData->AccessPoint.RemoteAddress is zero or + Tcp6ConfigData->AccessPoint.RemotePort is zero when + Tcp6ConfigData->AccessPoint.ActiveFlag is TRUE. + - A same access point has been configured in other TCP + instance properly. + @retval EFI_ACCESS_DENIED Configuring TCP instance when it is configured without + calling Configure() with NULL to reset it. + @retval EFI_UNSUPPORTED One or more of the control options are not supported in + the implementation. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when + executing Configure(). + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred. + +**/ +EFI_STATUS +EFIAPI +Tcp6Configure ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_CONFIG_DATA *Tcp6ConfigData OPTIONAL + ); + +/** + Initiate a nonblocking TCP connection request for an active TCP instance. + + The Connect() function will initiate an active open to the remote peer configured + in current TCP instance if it is configured active. If the connection succeeds or + fails due to an error, the ConnectionToken->CompletionToken.Event will be signaled, + and ConnectionToken->CompletionToken.Status will be updated accordingly. This + function can only be called for the TCP instance in Tcp6StateClosed state. The + instance will transfer into Tcp6StateSynSent if the function returns EFI_SUCCESS. + If TCP three-way handshake succeeds, its state will become Tcp6StateEstablished; + otherwise, the state will return to Tcp6StateClosed. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] ConnectionToken Pointer to the connection token to return when the TCP + three-way handshake finishes. + + @retval EFI_SUCCESS The connection request successfully initiated and the state of + this TCP instance has been changed to Tcp6StateSynSent. + @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured. + @retval EFI_ACCESS_DENIED One or more of the following conditions are TRUE: + - This instance is not configured as an active instance. + - This instance is not in Tcp6StateClosed state. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + - This is NULL. + - ConnectionToken is NULL. + - ConnectionToken->CompletionToken.Event is NULL. + @retval EFI_OUT_OF_RESOURCES The driver can't allocate enough resources to initiate the active open. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +Tcp6Connect ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_CONNECTION_TOKEN *ConnectionToken + ); + +/** + Listen on the passive instance to accept an incoming connection request. This is a + nonblocking operation. + + The Accept() function initiates an asynchronous accept request to wait for an incoming + connection on the passive TCP instance. If a remote peer successfully establishes a + connection with this instance, a new TCP instance will be created and its handle will + be returned in ListenToken->NewChildHandle. The newly created instance is configured + by inheriting the passive instance's configuration, and is ready for use upon return. + The new instance is in the Tcp6StateEstablished state. + + The ListenToken->CompletionToken.Event will be signaled when a new connection is + accepted, user aborts the listen or connection is reset. + + This function only can be called when the current TCP instance is in Tcp6StateListen state. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] ListenToken Pointer to the listen token to return when the operation finishes. + + + @retval EFI_SUCCESS The listen token was been queued successfully. + @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured. + @retval EFI_ACCESS_DENIED One or more of the following are TRUE: + - This instance is not a passive instance. + - This instance is not in Tcp6StateListen state. + - The same listen token has already existed in the listen + token queue of this TCP instance. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + - This is NULL. + - ListenToken is NULL. + - ListentToken->CompletionToken.Event is NULL. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish the operation. + @retval EFI_DEVICE_ERROR Any unexpected error not belonging to the error + categories given above. + +**/ +EFI_STATUS +EFIAPI +Tcp6Accept ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_LISTEN_TOKEN *ListenToken + ); + +/** + Queues outgoing data into the transmit queue. + + The Transmit() function queues a sending request to this TCP instance along with the + user data. The status of the token is updated and the event in the token will be + signaled once the data is sent out or some error occurs. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] Token Pointer to the completion token to queue to the transmit queue. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a + source address for this instance, but no source address was + available for use. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + - This is NULL. + - Token is NULL. + - Token->CompletionToken.Event is NULL. + - Token->Packet.TxData is NULL. + - Token->Packet.FragmentCount is zero. + - Token->Packet.DataLength is not equal to the sum of fragment lengths. + @retval EFI_ACCESS_DENIED One or more of the following conditions are TRUE: + - A transmit completion token with the same Token-> + CompletionToken.Event was already in the + transmission queue. + - The current instance is in Tcp6StateClosed state. + - The current instance is a passive one and it is in + Tcp6StateListen state. + - User has called Close() to disconnect this connection. + @retval EFI_NOT_READY The completion token could not be queued because the + transmit queue is full. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data because of a resource + shortage. + @retval EFI_NETWORK_UNREACHABLE There is no route to the destination network or address. + +**/ +EFI_STATUS +EFIAPI +Tcp6Transmit ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_IO_TOKEN *Token + ); + +/** + Places an asynchronous receive request into the receiving queue. + + The Receive() function places a completion token into the receive packet queue. This + function is always asynchronous. The caller must allocate the Token->CompletionToken.Event + and the FragmentBuffer used to receive data. The caller also must fill the DataLength, which + represents the whole length of all FragmentBuffer. When the receive operation completes, the + EFI TCPv6 Protocol driver updates the Token->CompletionToken.Status and Token->Packet.RxData + fields, and the Token->CompletionToken.Event is signaled. If data is obtained, the data and its length + will be copied into the FragmentTable. At the same time the full length of received data will + be recorded in the DataLength fields. Providing a proper notification function and context + for the event enables the user to receive the notification and receiving status. That + notification function is guaranteed to not be re-entered. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] Token Pointer to a token that is associated with the receive data + descriptor. + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured. + @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source + address for this instance, but no source address was available for use. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - Token is NULL. + - Token->CompletionToken.Event is NULL. + - Token->Packet.RxData is NULL. + - Token->Packet.RxData->DataLength is 0. + - The Token->Packet.RxData->DataLength is not the + sum of all FragmentBuffer length in FragmentTable. + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of + system resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The EFI TCPv6 Protocol instance has been reset to startup defaults. + @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE: + - A receive completion token with the same Token->CompletionToken.Event + was already in the receive queue. + - The current instance is in Tcp6StateClosed state. + - The current instance is a passive one and it is in + Tcp6StateListen state. + - The user has called Close() to disconnect this connection. + @retval EFI_CONNECTION_FIN The communication peer has closed the connection, and there is no + buffered data in the receive buffer of this instance. + @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +Tcp6Receive ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_IO_TOKEN *Token + ); + +/** + Disconnecting a TCP connection gracefully or reset a TCP connection. This function is a + nonblocking operation. + + Initiate an asynchronous close token to the TCP driver. After Close() is called, any buffered + transmission data will be sent by the TCP driver, and the current instance will have a graceful close + working flow described as RFC 793 if AbortOnClose is set to FALSE, otherwise, a rest packet + will be sent by TCP driver to fast disconnect this connection. When the close operation completes + successfully the TCP instance is in Tcp6StateClosed state, all pending asynchronous + operations are signaled, and any buffers used for TCP network traffic are flushed. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] CloseToken Pointer to the close token to return when operation finishes. + + @retval EFI_SUCCESS The Close() was called successfully. + @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured. + @retval EFI_ACCESS_DENIED One or more of the following are TRUE: + - CloseToken or CloseToken->CompletionToken.Event is already in use. + - Previous Close() call on this instance has not finished. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + - This is NULL. + - CloseToken is NULL. + - CloseToken->CompletionToken.Event is NULL. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish the operation. + @retval EFI_DEVICE_ERROR Any unexpected error not belonging to the error categories given above. + +**/ +EFI_STATUS +EFIAPI +Tcp6Close ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_CLOSE_TOKEN *CloseToken + ); + +/** + Abort an asynchronous connection, listen, transmission or receive request. + + The Cancel() function aborts a pending connection, listen, transmit or + receive request. + + If Token is not NULL and the token is in the connection, listen, transmission + or receive queue when it is being cancelled, its Token->Status will be set + to EFI_ABORTED and then Token->Event will be signaled. + + If the token is not in one of the queues, which usually means that the + asynchronous operation has completed, EFI_NOT_FOUND is returned. + + If Token is NULL all asynchronous token issued by Connect(), Accept(), + Transmit() and Receive() will be aborted. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_TCP6_PROTOCOL.Connect(), + EFI_TCP6_PROTOCOL.Accept(), + EFI_TCP6_PROTOCOL.Transmit() or + EFI_TCP6_PROTOCOL.Receive(). If NULL, all pending + tokens issued by above four functions will be aborted. Type + EFI_TCP6_COMPLETION_TOKEN is defined in + EFI_TCP_PROTOCOL.Connect(). + + @retval EFI_SUCCESS The asynchronous I/O request is aborted and Token->Event + is signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance hasn't been configured. + @retval EFI_NOT_FOUND The asynchronous I/O request isn't found in the transmission or + receive queue. It has either completed or wasn't issued by + Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +Tcp6Cancel ( + IN EFI_TCP6_PROTOCOL *This, + IN EFI_TCP6_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + Poll to receive incoming data and transmit outgoing segments. + + The Poll() function increases the rate that data is moved between the network + and application and can be called when the TCP instance is created successfully. + Its use is optional. + + @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_NOT_READY No incoming or outgoing data is processed. + @retval EFI_TIMEOUT Data was dropped out of the transmission or receive queue. + Consider increasing the polling rate. + +**/ +EFI_STATUS +EFIAPI +Tcp6Poll ( + IN EFI_TCP6_PROTOCOL *This + ); + +#endif diff --git a/NetworkPkg/TcpDxe/TcpMisc.c b/NetworkPkg/TcpDxe/TcpMisc.c new file mode 100644 index 000000000..8b0313a02 --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpMisc.c @@ -0,0 +1,1042 @@ +/** @file + Misc support routines for TCP driver. + + (C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TcpMain.h" + +LIST_ENTRY mTcpRunQue = { + &mTcpRunQue, + &mTcpRunQue +}; + +LIST_ENTRY mTcpListenQue = { + &mTcpListenQue, + &mTcpListenQue +}; + +TCP_SEQNO mTcpGlobalIss = TCP_BASE_ISS; + +CHAR16 *mTcpStateName[] = { + L"TCP_CLOSED", + L"TCP_LISTEN", + L"TCP_SYN_SENT", + L"TCP_SYN_RCVD", + L"TCP_ESTABLISHED", + L"TCP_FIN_WAIT_1", + L"TCP_FIN_WAIT_2", + L"TCP_CLOSING", + L"TCP_TIME_WAIT", + L"TCP_CLOSE_WAIT", + L"TCP_LAST_ACK" +}; + + +/** + Initialize the Tcb local related members. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpInitTcbLocal ( + IN OUT TCP_CB *Tcb + ) +{ + // + // Compute the checksum of the fixed parts of pseudo header + // + if (Tcb->Sk->IpVersion == IP_VERSION_4) { + Tcb->HeadSum = NetPseudoHeadChecksum ( + Tcb->LocalEnd.Ip.Addr[0], + Tcb->RemoteEnd.Ip.Addr[0], + 0x06, + 0 + ); + } else { + Tcb->HeadSum = NetIp6PseudoHeadChecksum ( + &Tcb->LocalEnd.Ip.v6, + &Tcb->RemoteEnd.Ip.v6, + 0x06, + 0 + ); + } + + Tcb->Iss = TcpGetIss (); + Tcb->SndUna = Tcb->Iss; + Tcb->SndNxt = Tcb->Iss; + + Tcb->SndWl2 = Tcb->Iss; + Tcb->SndWnd = 536; + + Tcb->RcvWnd = GET_RCV_BUFFSIZE (Tcb->Sk); + + // + // First window size is never scaled + // + Tcb->RcvWndScale = 0; + Tcb->RetxmitSeqMax = 0; + + Tcb->ProbeTimerOn = FALSE; +} + +/** + Initialize the peer related members. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Seg Pointer to the segment that contains the peer's intial info. + @param[in] Opt Pointer to the options announced by the peer. + +**/ +VOID +TcpInitTcbPeer ( + IN OUT TCP_CB *Tcb, + IN TCP_SEG *Seg, + IN TCP_OPTION *Opt + ) +{ + UINT16 RcvMss; + + ASSERT ((Tcb != NULL) && (Seg != NULL) && (Opt != NULL)); + ASSERT (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)); + + Tcb->SndWnd = Seg->Wnd; + Tcb->SndWndMax = Tcb->SndWnd; + Tcb->SndWl1 = Seg->Seq; + + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) { + Tcb->SndWl2 = Seg->Ack; + } else { + Tcb->SndWl2 = Tcb->Iss + 1; + } + + if (TCP_FLG_ON (Opt->Flag, TCP_OPTION_RCVD_MSS)) { + Tcb->SndMss = (UINT16) MAX (64, Opt->Mss); + + RcvMss = TcpGetRcvMss (Tcb->Sk); + if (Tcb->SndMss > RcvMss) { + Tcb->SndMss = RcvMss; + } + + } else { + // + // One end doesn't support MSS option, use default. + // + Tcb->RcvMss = 536; + } + + Tcb->CWnd = Tcb->SndMss; + + Tcb->Irs = Seg->Seq; + Tcb->RcvNxt = Tcb->Irs + 1; + + Tcb->RcvWl2 = Tcb->RcvNxt; + + if (TCP_FLG_ON (Opt->Flag, TCP_OPTION_RCVD_WS) && !TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_WS)) { + + Tcb->SndWndScale = Opt->WndScale; + + Tcb->RcvWndScale = TcpComputeScale (Tcb); + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_RCVD_WS); + + } else { + // + // One end doesn't support window scale option. use zero. + // + Tcb->RcvWndScale = 0; + } + + if (TCP_FLG_ON (Opt->Flag, TCP_OPTION_RCVD_TS) && !TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_TS)) { + + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_SND_TS); + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_RCVD_TS); + + Tcb->TsRecent = Opt->TSVal; + + // + // Compute the effective SndMss per RFC1122 + // section 4.2.2.6. If timestamp option is + // enabled, it will always occupy 12 bytes. + // + Tcb->SndMss -= TCP_OPTION_TS_ALIGNED_LEN; + } +} + +/** + Check whether one IP address equals the other. + + @param[in] Ip1 Pointer to IP address to be checked. + @param[in] Ip2 Pointer to IP address to be checked. + @param[in] Version IP_VERSION_4 indicates the IP address is an IPv4 address, + IP_VERSION_6 indicates the IP address is an IPv6 address. + + @retval TRUE Ip1 equals Ip2. + @retval FALSE Ip1 does not equal Ip2. + +**/ +BOOLEAN +TcpIsIpEqual ( + IN EFI_IP_ADDRESS *Ip1, + IN EFI_IP_ADDRESS *Ip2, + IN UINT8 Version + ) +{ + ASSERT ((Version == IP_VERSION_4) || (Version == IP_VERSION_6)); + + if (Version == IP_VERSION_4) { + return (BOOLEAN) (Ip1->Addr[0] == Ip2->Addr[0]); + } else { + return (BOOLEAN) EFI_IP6_EQUAL (&Ip1->v6, &Ip2->v6); + } +} + +/** + Check whether one IP address is filled with ZERO. + + @param[in] Ip Pointer to the IP address to be checked. + @param[in] Version IP_VERSION_4 indicates the IP address is an IPv4 address, + IP_VERSION_6 indicates the IP address is an IPv6 address. + + @retval TRUE Ip is all zero address. + @retval FALSE Ip is not all zero address. + +**/ +BOOLEAN +TcpIsIpZero ( + IN EFI_IP_ADDRESS *Ip, + IN UINT8 Version + ) +{ + ASSERT ((Version == IP_VERSION_4) || (Version == IP_VERSION_6)); + + if (Version == IP_VERSION_4) { + return (BOOLEAN) (Ip->Addr[0] == 0); + } else { + return (BOOLEAN) ((Ip->Addr[0] == 0) && (Ip->Addr[1] == 0) && + (Ip->Addr[2] == 0) && (Ip->Addr[3] == 0)); + } +} + +/** + Locate a listen TCB that matchs the Local and Remote. + + @param[in] Local Pointer to the local (IP, Port). + @param[in] Remote Pointer to the remote (IP, Port). + @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack, + IP_VERSION_6 indicates TCP is running on IP6 stack. + + @return Pointer to the TCP_CB with the least number of wildcards, + if NULL no match is found. + +**/ +TCP_CB * +TcpLocateListenTcb ( + IN TCP_PEER *Local, + IN TCP_PEER *Remote, + IN UINT8 Version + ) +{ + LIST_ENTRY *Entry; + TCP_CB *Node; + TCP_CB *Match; + INTN Last; + INTN Cur; + + Last = 4; + Match = NULL; + + NET_LIST_FOR_EACH (Entry, &mTcpListenQue) { + Node = NET_LIST_USER_STRUCT (Entry, TCP_CB, List); + + if ((Version != Node->Sk->IpVersion) || + (Local->Port != Node->LocalEnd.Port) || + !TCP_PEER_MATCH (Remote, &Node->RemoteEnd, Version) || + !TCP_PEER_MATCH (Local, &Node->LocalEnd, Version) + ) { + + continue; + } + + // + // Compute the number of wildcard + // + Cur = 0; + if (TcpIsIpZero (&Node->RemoteEnd.Ip, Version)) { + Cur++; + } + + if (Node->RemoteEnd.Port == 0) { + Cur++; + } + + if (TcpIsIpZero (&Node->LocalEnd.Ip, Version)) { + Cur++; + } + + if (Cur < Last) { + if (Cur == 0) { + return Node; + } + + Last = Cur; + Match = Node; + } + } + + return Match; +} + +/** + Try to find one Tcb whose equals to . + + @param[in] Addr Pointer to the IP address needs to match. + @param[in] Port The port number needs to match. + @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack, + IP_VERSION_6 indicates TCP is running on IP6 stack. + + + @retval TRUE The Tcb which matches the pair exists. + @retval FALSE Otherwise + +**/ +BOOLEAN +TcpFindTcbByPeer ( + IN EFI_IP_ADDRESS *Addr, + IN TCP_PORTNO Port, + IN UINT8 Version + ) +{ + TCP_PORTNO LocalPort; + LIST_ENTRY *Entry; + TCP_CB *Tcb; + + ASSERT ((Addr != NULL) && (Port != 0)); + + LocalPort = HTONS (Port); + + NET_LIST_FOR_EACH (Entry, &mTcpListenQue) { + Tcb = NET_LIST_USER_STRUCT (Entry, TCP_CB, List); + + if ((Version == Tcb->Sk->IpVersion) && + TcpIsIpEqual (Addr, &Tcb->LocalEnd.Ip, Version) && + (LocalPort == Tcb->LocalEnd.Port) + ) { + + return TRUE; + } + } + + NET_LIST_FOR_EACH (Entry, &mTcpRunQue) { + Tcb = NET_LIST_USER_STRUCT (Entry, TCP_CB, List); + + if ((Version == Tcb->Sk->IpVersion) && + TcpIsIpEqual (Addr, &Tcb->LocalEnd.Ip, Version) && + (LocalPort == Tcb->LocalEnd.Port) + ) { + + return TRUE; + } + } + + return FALSE; +} + +/** + Locate the TCP_CB related to the socket pair. + + @param[in] LocalPort The local port number. + @param[in] LocalIp The local IP address. + @param[in] RemotePort The remote port number. + @param[in] RemoteIp The remote IP address. + @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack, + IP_VERSION_6 indicates TCP is running on IP6 stack. + @param[in] Syn If TRUE, the listen sockets are searched. + + @return Pointer to the related TCP_CB. If NULL, no match is found. + +**/ +TCP_CB * +TcpLocateTcb ( + IN TCP_PORTNO LocalPort, + IN EFI_IP_ADDRESS *LocalIp, + IN TCP_PORTNO RemotePort, + IN EFI_IP_ADDRESS *RemoteIp, + IN UINT8 Version, + IN BOOLEAN Syn + ) +{ + TCP_PEER Local; + TCP_PEER Remote; + LIST_ENTRY *Entry; + TCP_CB *Tcb; + + Local.Port = LocalPort; + Remote.Port = RemotePort; + + CopyMem (&Local.Ip, LocalIp, sizeof (EFI_IP_ADDRESS)); + CopyMem (&Remote.Ip, RemoteIp, sizeof (EFI_IP_ADDRESS)); + + // + // First check for exact match. + // + NET_LIST_FOR_EACH (Entry, &mTcpRunQue) { + Tcb = NET_LIST_USER_STRUCT (Entry, TCP_CB, List); + + if ((Version == Tcb->Sk->IpVersion) && + TCP_PEER_EQUAL (&Remote, &Tcb->RemoteEnd, Version) && + TCP_PEER_EQUAL (&Local, &Tcb->LocalEnd, Version) + ) { + + RemoveEntryList (&Tcb->List); + InsertHeadList (&mTcpRunQue, &Tcb->List); + + return Tcb; + } + } + + // + // Only check the listen queue when the SYN flag is on. + // + if (Syn) { + return TcpLocateListenTcb (&Local, &Remote, Version); + } + + return NULL; +} + +/** + Insert a Tcb into the proper queue. + + @param[in] Tcb Pointer to the TCP_CB to be inserted. + + @retval 0 The Tcb was inserted successfully. + @retval -1 Error condition occurred. + +**/ +INTN +TcpInsertTcb ( + IN TCP_CB *Tcb + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Head; + TCP_CB *Node; + + ASSERT ( + (Tcb != NULL) && + ( + (Tcb->State == TCP_LISTEN) || + (Tcb->State == TCP_SYN_SENT) || + (Tcb->State == TCP_SYN_RCVD) || + (Tcb->State == TCP_CLOSED) + ) + ); + + if (Tcb->LocalEnd.Port == 0) { + return -1; + } + + Head = &mTcpRunQue; + + if (Tcb->State == TCP_LISTEN) { + Head = &mTcpListenQue; + } + + // + // Check that the Tcb isn't already on the list. + // + NET_LIST_FOR_EACH (Entry, Head) { + Node = NET_LIST_USER_STRUCT (Entry, TCP_CB, List); + + if (TCP_PEER_EQUAL (&Tcb->LocalEnd, &Node->LocalEnd, Tcb->Sk->IpVersion) && + TCP_PEER_EQUAL (&Tcb->RemoteEnd, &Node->RemoteEnd, Tcb->Sk->IpVersion) + ) { + + return -1; + } + } + + InsertHeadList (Head, &Tcb->List); + + + return 0; +} + +/** + Clone a TCP_CB from Tcb. + + @param[in] Tcb Pointer to the TCP_CB to be cloned. + + @return Pointer to the new cloned TCP_CB; if NULL, error condition occurred. + +**/ +TCP_CB * +TcpCloneTcb ( + IN TCP_CB *Tcb + ) +{ + TCP_CB *Clone; + + Clone = AllocateZeroPool (sizeof (TCP_CB)); + + if (Clone == NULL) { + return NULL; + } + + CopyMem (Clone, Tcb, sizeof (TCP_CB)); + + // + // Increase the reference count of the shared IpInfo. + // + NET_GET_REF (Tcb->IpInfo); + + InitializeListHead (&Clone->List); + InitializeListHead (&Clone->SndQue); + InitializeListHead (&Clone->RcvQue); + + Clone->Sk = SockClone (Tcb->Sk); + if (Clone->Sk == NULL) { + DEBUG ((EFI_D_ERROR, "TcpCloneTcb: failed to clone a sock\n")); + FreePool (Clone); + return NULL; + } + + ((TCP_PROTO_DATA *) (Clone->Sk->ProtoReserved))->TcpPcb = Clone; + + return Clone; +} + +/** + Compute an ISS to be used by a new connection. + + @return The resulting ISS. + +**/ +TCP_SEQNO +TcpGetIss ( + VOID + ) +{ + mTcpGlobalIss += TCP_ISS_INCREMENT_1; + return mTcpGlobalIss; +} + +/** + Get the local mss. + + @param[in] Sock Pointer to the socket to get mss. + + @return The mss size. + +**/ +UINT16 +TcpGetRcvMss ( + IN SOCKET *Sock + ) +{ + EFI_IP4_MODE_DATA Ip4Mode; + EFI_IP6_MODE_DATA Ip6Mode; + EFI_IP4_PROTOCOL *Ip4; + EFI_IP6_PROTOCOL *Ip6; + TCP_PROTO_DATA *TcpProto; + + ASSERT (Sock != NULL); + + ZeroMem (&Ip4Mode, sizeof (EFI_IP4_MODE_DATA)); + ZeroMem (&Ip6Mode, sizeof (EFI_IP6_MODE_DATA)); + + TcpProto = (TCP_PROTO_DATA *) Sock->ProtoReserved; + + if (Sock->IpVersion == IP_VERSION_4) { + Ip4 = TcpProto->TcpService->IpIo->Ip.Ip4; + ASSERT (Ip4 != NULL); + Ip4->GetModeData (Ip4, &Ip4Mode, NULL, NULL); + + return (UINT16) (Ip4Mode.MaxPacketSize - sizeof (TCP_HEAD)); + } else { + Ip6 = TcpProto->TcpService->IpIo->Ip.Ip6; + ASSERT (Ip6 != NULL); + if (!EFI_ERROR (Ip6->GetModeData (Ip6, &Ip6Mode, NULL, NULL))) { + if (Ip6Mode.AddressList != NULL) { + FreePool (Ip6Mode.AddressList); + } + + if (Ip6Mode.GroupTable != NULL) { + FreePool (Ip6Mode.GroupTable); + } + + if (Ip6Mode.RouteTable != NULL) { + FreePool (Ip6Mode.RouteTable); + } + + if (Ip6Mode.NeighborCache != NULL) { + FreePool (Ip6Mode.NeighborCache); + } + + if (Ip6Mode.PrefixTable != NULL) { + FreePool (Ip6Mode.PrefixTable); + } + + if (Ip6Mode.IcmpTypeList != NULL) { + FreePool (Ip6Mode.IcmpTypeList); + } + } + + return (UINT16) (Ip6Mode.MaxPacketSize - sizeof (TCP_HEAD)); + } +} + +/** + Set the Tcb's state. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] State The state to be set. + +**/ +VOID +TcpSetState ( + IN TCP_CB *Tcb, + IN UINT8 State + ) +{ + ASSERT (Tcb->State < (sizeof (mTcpStateName) / sizeof (CHAR16 *))); + ASSERT (State < (sizeof (mTcpStateName) / sizeof (CHAR16 *))); + + DEBUG ( + (EFI_D_NET, + "Tcb (%p) state %s --> %s\n", + Tcb, + mTcpStateName[Tcb->State], + mTcpStateName[State]) + ); + + Tcb->State = State; + + switch (State) { + case TCP_ESTABLISHED: + + SockConnEstablished (Tcb->Sk); + + if (Tcb->Parent != NULL) { + // + // A new connection is accepted by a listening socket. Install + // the device path. + // + TcpInstallDevicePath (Tcb->Sk); + } + + break; + + case TCP_CLOSED: + + SockConnClosed (Tcb->Sk); + + break; + default: + break; + } +} + +/** + Compute the TCP segment's checksum. + + @param[in] Nbuf Pointer to the buffer that contains the TCP segment. + @param[in] HeadSum The checksum value of the fixed part of pseudo header. + + @return The checksum value. + +**/ +UINT16 +TcpChecksum ( + IN NET_BUF *Nbuf, + IN UINT16 HeadSum + ) +{ + UINT16 Checksum; + + Checksum = NetbufChecksum (Nbuf); + Checksum = NetAddChecksum (Checksum, HeadSum); + + Checksum = NetAddChecksum ( + Checksum, + HTONS ((UINT16) Nbuf->TotalSize) + ); + + return (UINT16) (~Checksum); +} + +/** + Translate the information from the head of the received TCP + segment Nbuf contents and fill it into a TCP_SEG structure. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in, out] Nbuf Pointer to the buffer contains the TCP segment. + + @return Pointer to the TCP_SEG that contains the translated TCP head information. + +**/ +TCP_SEG * +TcpFormatNetbuf ( + IN TCP_CB *Tcb, + IN OUT NET_BUF *Nbuf + ) +{ + TCP_SEG *Seg; + TCP_HEAD *Head; + + Seg = TCPSEG_NETBUF (Nbuf); + Head = (TCP_HEAD *) NetbufGetByte (Nbuf, 0, NULL); + ASSERT (Head != NULL); + + Nbuf->Tcp = Head; + + Seg->Seq = NTOHL (Head->Seq); + Seg->Ack = NTOHL (Head->Ack); + Seg->End = Seg->Seq + (Nbuf->TotalSize - (Head->HeadLen << 2)); + + Seg->Urg = NTOHS (Head->Urg); + Seg->Wnd = (NTOHS (Head->Wnd) << Tcb->SndWndScale); + Seg->Flag = Head->Flag; + + // + // SYN and FIN flag occupy one sequence space each. + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) { + // + // RFC requires that the initial window not be scaled. + // + Seg->Wnd = NTOHS (Head->Wnd); + Seg->End++; + } + + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) { + Seg->End++; + } + + return Seg; +} + +/** + Initialize an active connection. + + @param[in, out] Tcb Pointer to the TCP_CB that wants to initiate a + connection. + +**/ +VOID +TcpOnAppConnect ( + IN OUT TCP_CB *Tcb + ) +{ + TcpInitTcbLocal (Tcb); + TcpSetState (Tcb, TCP_SYN_SENT); + + TcpSetTimer (Tcb, TCP_TIMER_CONNECT, Tcb->ConnectTimeout); + TcpToSendData (Tcb, 1); +} + +/** + Initiate the connection close procedure, called when + applications want to close the connection. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpOnAppClose ( + IN OUT TCP_CB *Tcb + ) +{ + ASSERT (Tcb != NULL); + + if (!IsListEmpty (&Tcb->RcvQue) || GET_RCV_DATASIZE (Tcb->Sk) != 0) { + + DEBUG ( + (EFI_D_WARN, + "TcpOnAppClose: connection reset because data is lost for TCB %p\n", + Tcb) + ); + + TcpResetConnection (Tcb); + TcpClose (Tcb); + return; + } + + switch (Tcb->State) { + case TCP_CLOSED: + case TCP_LISTEN: + case TCP_SYN_SENT: + TcpSetState (Tcb, TCP_CLOSED); + break; + + case TCP_SYN_RCVD: + case TCP_ESTABLISHED: + TcpSetState (Tcb, TCP_FIN_WAIT_1); + break; + + case TCP_CLOSE_WAIT: + TcpSetState (Tcb, TCP_LAST_ACK); + break; + default: + break; + } + + TcpToSendData (Tcb, 1); +} + +/** + Check whether the application's newly delivered data can be sent out. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + + @retval 0 The data has been sent out successfully. + @retval -1 The Tcb is not in a state that data is permitted to + be sent out. + +**/ +INTN +TcpOnAppSend ( + IN OUT TCP_CB *Tcb + ) +{ + + switch (Tcb->State) { + case TCP_CLOSED: + return -1; + + case TCP_LISTEN: + return -1; + + case TCP_SYN_SENT: + case TCP_SYN_RCVD: + return 0; + + case TCP_ESTABLISHED: + case TCP_CLOSE_WAIT: + TcpToSendData (Tcb, 0); + return 0; + + case TCP_FIN_WAIT_1: + case TCP_FIN_WAIT_2: + case TCP_CLOSING: + case TCP_LAST_ACK: + case TCP_TIME_WAIT: + return -1; + + default: + break; + } + + return 0; +} + +/** + Application has consumed some data. Check whether + to send a window update ack or a delayed ack. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpOnAppConsume ( + IN TCP_CB *Tcb + ) +{ + UINT32 TcpOld; + + switch (Tcb->State) { + case TCP_ESTABLISHED: + TcpOld = TcpRcvWinOld (Tcb); + if (TcpRcvWinNow (Tcb) > TcpOld) { + + if (TcpOld < Tcb->RcvMss) { + + DEBUG ( + (EFI_D_NET, + "TcpOnAppConsume: send a window update for a window closed Tcb %p\n", + Tcb) + ); + + TcpSendAck (Tcb); + } else if (Tcb->DelayedAck == 0) { + + DEBUG ( + (EFI_D_NET, + "TcpOnAppConsume: scheduled a delayed ACK to update window for Tcb %p\n", + Tcb) + ); + + Tcb->DelayedAck = 1; + } + } + + break; + + default: + break; + } +} + +/** + Abort the connection by sending a reset segment. Called + when the application wants to abort the connection. + + @param[in] Tcb Pointer to the TCP_CB of the TCP instance. + +**/ +VOID +TcpOnAppAbort ( + IN TCP_CB *Tcb + ) +{ + DEBUG ( + (EFI_D_WARN, + "TcpOnAppAbort: connection reset issued by application for TCB %p\n", + Tcb) + ); + + switch (Tcb->State) { + case TCP_SYN_RCVD: + case TCP_ESTABLISHED: + case TCP_FIN_WAIT_1: + case TCP_FIN_WAIT_2: + case TCP_CLOSE_WAIT: + TcpResetConnection (Tcb); + break; + default: + break; + } + + TcpSetState (Tcb, TCP_CLOSED); +} + +/** + Reset the connection related with Tcb. + + @param[in] Tcb Pointer to the TCP_CB of the connection to be reset. + +**/ +VOID +TcpResetConnection ( + IN TCP_CB *Tcb + ) +{ + NET_BUF *Nbuf; + TCP_HEAD *Nhead; + + Nbuf = NetbufAlloc (TCP_MAX_HEAD); + + if (Nbuf == NULL) { + return ; + } + + Nhead = (TCP_HEAD *) NetbufAllocSpace ( + Nbuf, + sizeof (TCP_HEAD), + NET_BUF_TAIL + ); + + ASSERT (Nhead != NULL); + + Nbuf->Tcp = Nhead; + + Nhead->Flag = TCP_FLG_RST; + Nhead->Seq = HTONL (Tcb->SndNxt); + Nhead->Ack = HTONL (Tcb->RcvNxt); + Nhead->SrcPort = Tcb->LocalEnd.Port; + Nhead->DstPort = Tcb->RemoteEnd.Port; + Nhead->HeadLen = (UINT8) (sizeof (TCP_HEAD) >> 2); + Nhead->Res = 0; + Nhead->Wnd = HTONS (0xFFFF); + Nhead->Checksum = 0; + Nhead->Urg = 0; + Nhead->Checksum = TcpChecksum (Nbuf, Tcb->HeadSum); + + TcpSendIpPacket (Tcb, Nbuf, &Tcb->LocalEnd.Ip, &Tcb->RemoteEnd.Ip, Tcb->Sk->IpVersion); + + NetbufFree (Nbuf); +} + +/** + Install the device path protocol on the TCP instance. + + @param[in] Sock Pointer to the socket representing the TCP instance. + + @retval EFI_SUCCESS The device path protocol was installed. + @retval other Failed to install the device path protocol. + +**/ +EFI_STATUS +TcpInstallDevicePath ( + IN SOCKET *Sock + ) +{ + TCP_PROTO_DATA *TcpProto; + TCP_SERVICE_DATA *TcpService; + TCP_CB *Tcb; + IPv4_DEVICE_PATH Ip4DPathNode; + IPv6_DEVICE_PATH Ip6DPathNode; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_STATUS Status; + TCP_PORTNO LocalPort; + TCP_PORTNO RemotePort; + + TcpProto = (TCP_PROTO_DATA *) Sock->ProtoReserved; + TcpService = TcpProto->TcpService; + Tcb = TcpProto->TcpPcb; + + LocalPort = NTOHS (Tcb->LocalEnd.Port); + RemotePort = NTOHS (Tcb->RemoteEnd.Port); + if (Sock->IpVersion == IP_VERSION_4) { + NetLibCreateIPv4DPathNode ( + &Ip4DPathNode, + TcpService->ControllerHandle, + Tcb->LocalEnd.Ip.Addr[0], + LocalPort, + Tcb->RemoteEnd.Ip.Addr[0], + RemotePort, + EFI_IP_PROTO_TCP, + Tcb->UseDefaultAddr + ); + + IP4_COPY_ADDRESS (&Ip4DPathNode.SubnetMask, &Tcb->SubnetMask); + + DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) &Ip4DPathNode; + } else { + NetLibCreateIPv6DPathNode ( + &Ip6DPathNode, + TcpService->ControllerHandle, + &Tcb->LocalEnd.Ip.v6, + LocalPort, + &Tcb->RemoteEnd.Ip.v6, + RemotePort, + EFI_IP_PROTO_TCP + ); + + DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) &Ip6DPathNode; + } + + Sock->DevicePath = AppendDevicePathNode (Sock->ParentDevicePath, DevicePath); + if (Sock->DevicePath == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = gBS->InstallProtocolInterface ( + &Sock->SockHandle, + &gEfiDevicePathProtocolGuid, + EFI_NATIVE_INTERFACE, + Sock->DevicePath + ); + if (EFI_ERROR (Status)) { + FreePool (Sock->DevicePath); + Sock->DevicePath = NULL; + } + + return Status; +} + diff --git a/NetworkPkg/TcpDxe/TcpOption.c b/NetworkPkg/TcpDxe/TcpOption.c new file mode 100644 index 000000000..bce814da6 --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpOption.c @@ -0,0 +1,338 @@ +/** @file + Routines to process TCP option. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TcpMain.h" + +/** + Get a UINT16 value from buffer. + + @param[in] Buf Pointer to input buffer. + + @return The UINT16 value obtained from the buffer. + +**/ +UINT16 +TcpGetUint16 ( + IN UINT8 *Buf + ) +{ + UINT16 Value; + CopyMem (&Value, Buf, sizeof (UINT16)); + return NTOHS (Value); +} + +/** + Get a UINT32 value from buffer. + + @param[in] Buf Pointer to input buffer. + + @return The UINT32 value obtained from the buffer. + +**/ +UINT32 +TcpGetUint32 ( + IN UINT8 *Buf + ) +{ + UINT32 Value; + CopyMem (&Value, Buf, sizeof (UINT32)); + return NTOHL (Value); +} + +/** + Put a UINT32 value in buffer. + + @param[out] Buf Pointer to the buffer. + @param[in] Data The UINT32 Date to put in the buffer. + +**/ +VOID +TcpPutUint32 ( + OUT UINT8 *Buf, + IN UINT32 Data + ) +{ + Data = HTONL (Data); + CopyMem (Buf, &Data, sizeof (UINT32)); +} + +/** + Compute the window scale value according to the given buffer size. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + + @return The scale value. + +**/ +UINT8 +TcpComputeScale ( + IN TCP_CB *Tcb + ) +{ + UINT8 Scale; + UINT32 BufSize; + + ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL)); + + BufSize = GET_RCV_BUFFSIZE (Tcb->Sk); + + Scale = 0; + while ((Scale < TCP_OPTION_MAX_WS) && ((UINT32) (TCP_OPTION_MAX_WIN << Scale) < BufSize)) { + + Scale++; + } + + return Scale; +} + +/** + Build the TCP option in three-way handshake. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Nbuf Pointer to the buffer to store the options. + + @return The total length of the TCP option field. + +**/ +UINT16 +TcpSynBuildOption ( + IN TCP_CB *Tcb, + IN NET_BUF *Nbuf + ) +{ + UINT8 *Data; + UINT16 Len; + + ASSERT ((Tcb != NULL) && (Nbuf != NULL) && (Nbuf->Tcp == NULL)); + + Len = 0; + + // + // Add a timestamp option if not disabled by the application + // and it is the first SYN segment, or the peer has sent + // us its timestamp. + // + if (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_TS) && + (!TCP_FLG_ON (TCPSEG_NETBUF (Nbuf)->Flag, TCP_FLG_ACK) || + TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RCVD_TS)) + ) { + + Data = NetbufAllocSpace ( + Nbuf, + TCP_OPTION_TS_ALIGNED_LEN, + NET_BUF_HEAD + ); + + ASSERT (Data != NULL); + Len += TCP_OPTION_TS_ALIGNED_LEN; + + TcpPutUint32 (Data, TCP_OPTION_TS_FAST); + TcpPutUint32 (Data + 4, mTcpTick); + TcpPutUint32 (Data + 8, 0); + } + + // + // Build window scale option, only when configured + // to send WS option, and either we are doing active + // open or we have received WS option from peer. + // + if (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_WS) && + (!TCP_FLG_ON (TCPSEG_NETBUF (Nbuf)->Flag, TCP_FLG_ACK) || + TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RCVD_WS)) + ) { + + Data = NetbufAllocSpace ( + Nbuf, + TCP_OPTION_WS_ALIGNED_LEN, + NET_BUF_HEAD + ); + + ASSERT (Data != NULL); + + Len += TCP_OPTION_WS_ALIGNED_LEN; + TcpPutUint32 (Data, TCP_OPTION_WS_FAST | TcpComputeScale (Tcb)); + } + + // + // Build the MSS option. + // + Data = NetbufAllocSpace (Nbuf, TCP_OPTION_MSS_LEN, 1); + ASSERT (Data != NULL); + + Len += TCP_OPTION_MSS_LEN; + TcpPutUint32 (Data, TCP_OPTION_MSS_FAST | Tcb->RcvMss); + + return Len; +} + +/** + Build the TCP option in synchronized states. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Nbuf Pointer to the buffer to store the options. + + @return The total length of the TCP option field. + +**/ +UINT16 +TcpBuildOption ( + IN TCP_CB *Tcb, + IN NET_BUF *Nbuf + ) +{ + UINT8 *Data; + UINT16 Len; + + ASSERT ((Tcb != NULL) && (Nbuf != NULL) && (Nbuf->Tcp == NULL)); + Len = 0; + + // + // Build the Timestamp option. + // + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_SND_TS) && + !TCP_FLG_ON (TCPSEG_NETBUF (Nbuf)->Flag, TCP_FLG_RST) + ) { + + Data = NetbufAllocSpace ( + Nbuf, + TCP_OPTION_TS_ALIGNED_LEN, + NET_BUF_HEAD + ); + + ASSERT (Data != NULL); + Len += TCP_OPTION_TS_ALIGNED_LEN; + + TcpPutUint32 (Data, TCP_OPTION_TS_FAST); + TcpPutUint32 (Data + 4, mTcpTick); + TcpPutUint32 (Data + 8, Tcb->TsRecent); + } + + return Len; +} + +/** + Parse the supported options. + + @param[in] Tcp Pointer to the TCP_CB of this TCP instance. + @param[in, out] Option Pointer to the TCP_OPTION used to store the + successfully pasrsed options. + + @retval 0 The options are successfully pasrsed. + @retval -1 Ilegal option was found. + +**/ +INTN +TcpParseOption ( + IN TCP_HEAD *Tcp, + IN OUT TCP_OPTION *Option + ) +{ + UINT8 *Head; + UINT8 TotalLen; + UINT8 Cur; + UINT8 Type; + UINT8 Len; + + ASSERT ((Tcp != NULL) && (Option != NULL)); + + Option->Flag = 0; + + TotalLen = (UINT8) ((Tcp->HeadLen << 2) - sizeof (TCP_HEAD)); + if (TotalLen <= 0) { + return 0; + } + + Head = (UINT8 *) (Tcp + 1); + + // + // Fast process of the timestamp option. + // + if ((TotalLen == TCP_OPTION_TS_ALIGNED_LEN) && (TcpGetUint32 (Head) == TCP_OPTION_TS_FAST)) { + + Option->TSVal = TcpGetUint32 (Head + 4); + Option->TSEcr = TcpGetUint32 (Head + 8); + Option->Flag = TCP_OPTION_RCVD_TS; + + return 0; + } + // + // Slow path to process the options. + // + Cur = 0; + + while (Cur < TotalLen) { + Type = Head[Cur]; + + switch (Type) { + case TCP_OPTION_MSS: + Len = Head[Cur + 1]; + + if ((Len != TCP_OPTION_MSS_LEN) || (TotalLen - Cur < TCP_OPTION_MSS_LEN)) { + + return -1; + } + + Option->Mss = TcpGetUint16 (&Head[Cur + 2]); + TCP_SET_FLG (Option->Flag, TCP_OPTION_RCVD_MSS); + + Cur += TCP_OPTION_MSS_LEN; + break; + + case TCP_OPTION_WS: + Len = Head[Cur + 1]; + + if ((Len != TCP_OPTION_WS_LEN) || (TotalLen - Cur < TCP_OPTION_WS_LEN)) { + + return -1; + } + + Option->WndScale = (UINT8) MIN (14, Head[Cur + 2]); + TCP_SET_FLG (Option->Flag, TCP_OPTION_RCVD_WS); + + Cur += TCP_OPTION_WS_LEN; + break; + + case TCP_OPTION_TS: + Len = Head[Cur + 1]; + + if ((Len != TCP_OPTION_TS_LEN) || (TotalLen - Cur < TCP_OPTION_TS_LEN)) { + + return -1; + } + + Option->TSVal = TcpGetUint32 (&Head[Cur + 2]); + Option->TSEcr = TcpGetUint32 (&Head[Cur + 6]); + TCP_SET_FLG (Option->Flag, TCP_OPTION_RCVD_TS); + + Cur += TCP_OPTION_TS_LEN; + break; + + case TCP_OPTION_NOP: + Cur++; + break; + + case TCP_OPTION_EOP: + Cur = TotalLen; + break; + + default: + Len = Head[Cur + 1]; + + if ((TotalLen - Cur) < Len || Len < 2) { + return -1; + } + + Cur = (UINT8) (Cur + Len); + break; + } + + } + + return 0; +} diff --git a/NetworkPkg/TcpDxe/TcpOption.h b/NetworkPkg/TcpDxe/TcpOption.h new file mode 100644 index 000000000..b28f4e633 --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpOption.h @@ -0,0 +1,123 @@ +/** @file + Tcp option's routine header file. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _TCP_OPTION_H_ +#define _TCP_OPTION_H_ + +// +// Supported TCP option types and their length. +// +#define TCP_OPTION_EOP 0 ///< End Of oPtion +#define TCP_OPTION_NOP 1 ///< No-Option. +#define TCP_OPTION_MSS 2 ///< Maximum Segment Size +#define TCP_OPTION_WS 3 ///< Window scale +#define TCP_OPTION_TS 8 ///< Timestamp +#define TCP_OPTION_MSS_LEN 4 ///< Length of MSS option +#define TCP_OPTION_WS_LEN 3 ///< Length of window scale option +#define TCP_OPTION_TS_LEN 10 ///< Length of timestamp option +#define TCP_OPTION_WS_ALIGNED_LEN 4 ///< Length of window scale option, aligned +#define TCP_OPTION_TS_ALIGNED_LEN 12 ///< Length of timestamp option, aligned + +// +// recommend format of timestamp window scale +// option for fast process. +// +#define TCP_OPTION_TS_FAST ((TCP_OPTION_NOP << 24) | \ + (TCP_OPTION_NOP << 16) | \ + (TCP_OPTION_TS << 8) | \ + (TCP_OPTION_TS_LEN)) + +#define TCP_OPTION_WS_FAST ((TCP_OPTION_NOP << 24) | \ + (TCP_OPTION_WS << 16) | \ + (TCP_OPTION_WS_LEN << 8)) + +#define TCP_OPTION_MSS_FAST ((TCP_OPTION_MSS << 24) | (TCP_OPTION_MSS_LEN << 16)) + +// +// Other misc definations +// +#define TCP_OPTION_RCVD_MSS 0x01 +#define TCP_OPTION_RCVD_WS 0x02 +#define TCP_OPTION_RCVD_TS 0x04 +#define TCP_OPTION_MAX_WS 14 ///< Maxium window scale value +#define TCP_OPTION_MAX_WIN 0xffff ///< Max window size in TCP header + +/// +/// The structure to store the parse option value. +/// ParseOption only parses the options, doesn't process them. +/// +typedef struct _TCP_OPTION { + UINT8 Flag; ///< Flag such as TCP_OPTION_RCVD_MSS + UINT8 WndScale; ///< The WndScale received + UINT16 Mss; ///< The Mss received + UINT32 TSVal; ///< The TSVal field in a timestamp option + UINT32 TSEcr; ///< The TSEcr field in a timestamp option +} TCP_OPTION; + +/** + Compute the window scale value according to the given buffer size. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + + @return The scale value. + +**/ +UINT8 +TcpComputeScale ( + IN TCP_CB *Tcb + ); + +/** + Build the TCP option in three-way handshake. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Nbuf Pointer to the buffer to store the options. + + @return The total length of the TCP option field. + +**/ +UINT16 +TcpSynBuildOption ( + IN TCP_CB *Tcb, + IN NET_BUF *Nbuf + ); + +/** + Build the TCP option in synchronized states. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Nbuf Pointer to the buffer to store the options. + + @return The total length of the TCP option field. + +**/ +UINT16 +TcpBuildOption ( + IN TCP_CB *Tcb, + IN NET_BUF *Nbuf + ); + +/** + Parse the supported options. + + @param[in] Tcp Pointer to the TCP_CB of this TCP instance. + @param[in, out] Option Pointer to the TCP_OPTION used to store the + successfully pasrsed options. + + @retval 0 The options successfully pasrsed. + @retval -1 Ilegal option was found. + +**/ +INTN +TcpParseOption ( + IN TCP_HEAD *Tcp, + IN OUT TCP_OPTION *Option + ); + +#endif diff --git a/NetworkPkg/TcpDxe/TcpOutput.c b/NetworkPkg/TcpDxe/TcpOutput.c new file mode 100644 index 000000000..96aada66c --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpOutput.c @@ -0,0 +1,1251 @@ +/** @file + TCP output process routines. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TcpMain.h" + +UINT8 mTcpOutFlag[] = { + 0, // TCP_CLOSED + 0, // TCP_LISTEN + TCP_FLG_SYN, // TCP_SYN_SENT + TCP_FLG_SYN | TCP_FLG_ACK, // TCP_SYN_RCVD + TCP_FLG_ACK, // TCP_ESTABLISHED + TCP_FLG_FIN | TCP_FLG_ACK, // TCP_FIN_WAIT_1 + TCP_FLG_ACK, // TCP_FIN_WAIT_2 + TCP_FLG_ACK | TCP_FLG_FIN, // TCP_CLOSING + TCP_FLG_ACK, // TCP_TIME_WAIT + TCP_FLG_ACK, // TCP_CLOSE_WAIT + TCP_FLG_FIN | TCP_FLG_ACK // TCP_LAST_ACK +}; + +/** + Compute the sequence space left in the old receive window. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + + @return The sequence space left in the old receive window. + +**/ +UINT32 +TcpRcvWinOld ( + IN TCP_CB *Tcb + ) +{ + UINT32 OldWin; + + OldWin = 0; + + if (TCP_SEQ_GT (Tcb->RcvWl2 + Tcb->RcvWnd, Tcb->RcvNxt)) { + + OldWin = TCP_SUB_SEQ ( + Tcb->RcvWl2 + Tcb->RcvWnd, + Tcb->RcvNxt + ); + } + + return OldWin; +} + +/** + Compute the current receive window. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + + @return The size of the current receive window, in bytes. + +**/ +UINT32 +TcpRcvWinNow ( + IN TCP_CB *Tcb + ) +{ + SOCKET *Sk; + UINT32 Win; + UINT32 Increase; + UINT32 OldWin; + + Sk = Tcb->Sk; + ASSERT (Sk != NULL); + + OldWin = TcpRcvWinOld (Tcb); + + Win = SockGetFreeSpace (Sk, SOCK_RCV_BUF); + + Increase = 0; + if (Win > OldWin) { + Increase = Win - OldWin; + } + + // + // Receiver's SWS: don't advertise a bigger window + // unless it can be increased by at least one Mss or + // half of the receive buffer. + // + if ((Increase > Tcb->SndMss) || (2 * Increase >= GET_RCV_BUFFSIZE (Sk))) { + + return Win; + } + + return OldWin; +} + +/** + Compute the value to fill in the window size field of the outgoing segment. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Syn The flag to indicate whether the outgoing segment + is a SYN segment. + + @return The value of the local receive window size used to fill the outgoing segment. + +**/ +UINT16 +TcpComputeWnd ( + IN OUT TCP_CB *Tcb, + IN BOOLEAN Syn + ) +{ + UINT32 Wnd; + + // + // RFC requires that initial window not be scaled + // + if (Syn) { + + Wnd = GET_RCV_BUFFSIZE (Tcb->Sk); + } else { + + Wnd = TcpRcvWinNow (Tcb); + + Tcb->RcvWnd = Wnd; + } + + Wnd = MIN (Wnd >> Tcb->RcvWndScale, 0xffff); + return NTOHS ((UINT16) Wnd); +} + +/** + Get the maximum SndNxt. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + + @return The sequence number of the maximum SndNxt. + +**/ +TCP_SEQNO +TcpGetMaxSndNxt ( + IN TCP_CB *Tcb + ) +{ + LIST_ENTRY *Entry; + NET_BUF *Nbuf; + + if (IsListEmpty (&Tcb->SndQue)) { + return Tcb->SndNxt; + } + + Entry = Tcb->SndQue.BackLink; + Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + + ASSERT (TCP_SEQ_GEQ (TCPSEG_NETBUF (Nbuf)->End, Tcb->SndNxt)); + return TCPSEG_NETBUF (Nbuf)->End; +} + +/** + Compute how much data to send. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Force If TRUE, to ignore the sender's SWS avoidance algorithm and send + out data by force. + + @return The length of the data can be sent. If 0, no data can be sent. + +**/ +UINT32 +TcpDataToSend ( + IN TCP_CB *Tcb, + IN INTN Force + ) +{ + SOCKET *Sk; + UINT32 Win; + UINT32 Len; + UINT32 Left; + UINT32 Limit; + + Sk = Tcb->Sk; + ASSERT (Sk != NULL); + + // + // TCP should NOT send data beyond the send window + // and congestion window. The right edge of send + // window is defined as SND.WL2 + SND.WND. The right + // edge of congestion window is defined as SND.UNA + + // CWND. + // + Win = 0; + Limit = Tcb->SndWl2 + Tcb->SndWnd; + + if (TCP_SEQ_GT (Limit, Tcb->SndUna + Tcb->CWnd)) { + + Limit = Tcb->SndUna + Tcb->CWnd; + } + + if (TCP_SEQ_GT (Limit, Tcb->SndNxt)) { + Win = TCP_SUB_SEQ (Limit, Tcb->SndNxt); + } + + // + // The data to send contains two parts: the data on the + // socket send queue, and the data on the TCB's send + // buffer. The later can be non-zero if the peer shrinks + // its advertised window. + // + Left = GET_SND_DATASIZE (Sk) + TCP_SUB_SEQ (TcpGetMaxSndNxt (Tcb), Tcb->SndNxt); + + Len = MIN (Win, Left); + + if (Len > Tcb->SndMss) { + Len = Tcb->SndMss; + } + + if ((Force != 0)|| (Len == 0 && Left == 0)) { + return Len; + } + + if (Len == 0 && Left != 0) { + goto SetPersistTimer; + } + + // + // Sender's SWS avoidance: Don't send a small segment unless + // a)A full-sized segment can be sent, + // b)At least one-half of the maximum sized windows that + // the other end has ever advertised. + // c)It can send everything it has, and either it isn't + // expecting an ACK, or the Nagle algorithm is disabled. + // + if ((Len == Tcb->SndMss) || (2 * Len >= Tcb->SndWndMax)) { + + return Len; + } + + if ((Len == Left) && + ((Tcb->SndNxt == Tcb->SndUna) || TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_NAGLE)) + ) { + + return Len; + } + + // + // RFC1122 suggests to set a timer when SWSA forbids TCP + // sending more data, and combines it with a probe timer. + // +SetPersistTimer: + if (!TCP_TIMER_ON (Tcb->EnabledTimer, TCP_TIMER_REXMIT)) { + + DEBUG ( + (EFI_D_WARN, + "TcpDataToSend: enter persistent state for TCB %p\n", + Tcb) + ); + + if (!Tcb->ProbeTimerOn) { + TcpSetProbeTimer (Tcb); + } + } + + return 0; +} + +/** + Build the TCP header of the TCP segment and transmit the segment by IP. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Nbuf Pointer to the buffer containing the segment to be + sent out. + + @retval 0 The segment was sent out successfully. + @retval -1 An error condition occurred. + +**/ +INTN +TcpTransmitSegment ( + IN OUT TCP_CB *Tcb, + IN NET_BUF *Nbuf + ) +{ + UINT16 Len; + TCP_HEAD *Head; + TCP_SEG *Seg; + BOOLEAN Syn; + UINT32 DataLen; + + ASSERT ((Nbuf != NULL) && (Nbuf->Tcp == NULL)); + + if (TcpVerifySegment (Nbuf) == 0) { + return -1; + } + + DataLen = Nbuf->TotalSize; + + Seg = TCPSEG_NETBUF (Nbuf); + Syn = TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN); + + if (Syn) { + + Len = TcpSynBuildOption (Tcb, Nbuf); + } else { + + Len = TcpBuildOption (Tcb, Nbuf); + } + + ASSERT ((Len % 4 == 0) && (Len <= 40)); + + Len += sizeof (TCP_HEAD); + + Head = (TCP_HEAD *) NetbufAllocSpace ( + Nbuf, + sizeof (TCP_HEAD), + NET_BUF_HEAD + ); + + ASSERT (Head != NULL); + + Nbuf->Tcp = Head; + + Head->SrcPort = Tcb->LocalEnd.Port; + Head->DstPort = Tcb->RemoteEnd.Port; + Head->Seq = NTOHL (Seg->Seq); + Head->Ack = NTOHL (Tcb->RcvNxt); + Head->HeadLen = (UINT8) (Len >> 2); + Head->Res = 0; + Head->Wnd = TcpComputeWnd (Tcb, Syn); + Head->Checksum = 0; + + // + // Check whether to set the PSH flag. + // + TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_PSH); + + if (DataLen != 0) { + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_SND_PSH) && + TCP_SEQ_BETWEEN (Seg->Seq, Tcb->SndPsh, Seg->End) + ) { + + TCP_SET_FLG (Seg->Flag, TCP_FLG_PSH); + TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_SND_PSH); + + } else if ((Seg->End == Tcb->SndNxt) && (GET_SND_DATASIZE (Tcb->Sk) == 0)) { + + TCP_SET_FLG (Seg->Flag, TCP_FLG_PSH); + } + } + + // + // Check whether to set the URG flag and the urgent pointer. + // + TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_URG); + + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_SND_URG) && TCP_SEQ_LEQ (Seg->Seq, Tcb->SndUp)) { + + TCP_SET_FLG (Seg->Flag, TCP_FLG_URG); + + if (TCP_SEQ_LT (Tcb->SndUp, Seg->End)) { + + Seg->Urg = (UINT16) TCP_SUB_SEQ (Tcb->SndUp, Seg->Seq); + } else { + + Seg->Urg = (UINT16) MIN ( + TCP_SUB_SEQ (Tcb->SndUp, + Seg->Seq), + 0xffff + ); + } + } + + Head->Flag = Seg->Flag; + Head->Urg = NTOHS (Seg->Urg); + Head->Checksum = TcpChecksum (Nbuf, Tcb->HeadSum); + + // + // Update the TCP session's control information. + // + Tcb->RcvWl2 = Tcb->RcvNxt; + if (Syn) { + Tcb->RcvWnd = NTOHS (Head->Wnd); + } + + // + // Clear the delayedack flag. + // + Tcb->DelayedAck = 0; + + return TcpSendIpPacket (Tcb, Nbuf, &Tcb->LocalEnd.Ip, &Tcb->RemoteEnd.Ip, Tcb->Sk->IpVersion); +} + +/** + Get a segment from the Tcb's SndQue. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Seq The sequence number of the segment. + @param[in] Len The maximum length of the segment. + + @return Pointer to the segment. If NULL, some error occurred. + +**/ +NET_BUF * +TcpGetSegmentSndQue ( + IN TCP_CB *Tcb, + IN TCP_SEQNO Seq, + IN UINT32 Len + ) +{ + LIST_ENTRY *Head; + LIST_ENTRY *Cur; + NET_BUF *Node; + TCP_SEG *Seg; + NET_BUF *Nbuf; + TCP_SEQNO End; + UINT8 *Data; + UINT8 Flag; + INT32 Offset; + INT32 CopyLen; + + ASSERT ((Tcb != NULL) && TCP_SEQ_LEQ (Seq, Tcb->SndNxt) && (Len > 0)); + + // + // Find the segment that contains the Seq. + // + Head = &Tcb->SndQue; + + Node = NULL; + Seg = NULL; + + NET_LIST_FOR_EACH (Cur, Head) { + Node = NET_LIST_USER_STRUCT (Cur, NET_BUF, List); + Seg = TCPSEG_NETBUF (Node); + + if (TCP_SEQ_LT (Seq, Seg->End) && TCP_SEQ_LEQ (Seg->Seq, Seq)) { + + break; + } + } + + if ((Cur == Head) || (Seg == NULL) || (Node == NULL)) { + return NULL; + } + + // + // Return the buffer if it can be returned without + // adjustment: + // + if ((Seg->Seq == Seq) && + TCP_SEQ_LEQ (Seg->End, Seg->Seq + Len) && + !NET_BUF_SHARED (Node) + ) { + + NET_GET_REF (Node); + return Node; + } + + // + // Create a new buffer and copy data there. + // + Nbuf = NetbufAlloc (Len + TCP_MAX_HEAD); + + if (Nbuf == NULL) { + return NULL; + } + + NetbufReserve (Nbuf, TCP_MAX_HEAD); + + Flag = Seg->Flag; + End = Seg->End; + + if (TCP_SEQ_LT (Seq + Len, Seg->End)) { + End = Seq + Len; + } + + CopyLen = TCP_SUB_SEQ (End, Seq); + Offset = TCP_SUB_SEQ (Seq, Seg->Seq); + + // + // If SYN is set and out of the range, clear the flag. + // Becuase the sequence of the first byte is SEG.SEQ+1, + // adjust Offset by -1. If SYN is in the range, copy + // one byte less. + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) { + + if (TCP_SEQ_LT (Seg->Seq, Seq)) { + + TCP_CLEAR_FLG (Flag, TCP_FLG_SYN); + Offset--; + } else { + + CopyLen--; + } + } + + // + // If FIN is set and in the range, copy one byte less, + // and if it is out of the range, clear the flag. + // + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) { + + if (Seg->End == End) { + + CopyLen--; + } else { + + TCP_CLEAR_FLG (Flag, TCP_FLG_FIN); + } + } + + ASSERT (CopyLen >= 0); + + // + // Copy data to the segment + // + if (CopyLen != 0) { + Data = NetbufAllocSpace (Nbuf, CopyLen, NET_BUF_TAIL); + ASSERT (Data != NULL); + + if ((INT32) NetbufCopy (Node, Offset, CopyLen, Data) != CopyLen) { + goto OnError; + } + } + + CopyMem (TCPSEG_NETBUF (Nbuf), Seg, sizeof (TCP_SEG)); + + TCPSEG_NETBUF (Nbuf)->Seq = Seq; + TCPSEG_NETBUF (Nbuf)->End = End; + TCPSEG_NETBUF (Nbuf)->Flag = Flag; + + return Nbuf; + +OnError: + NetbufFree (Nbuf); + return NULL; +} + +/** + Get a segment from the Tcb's socket buffer. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Seq The sequence number of the segment. + @param[in] Len The maximum length of the segment. + + @return Pointer to the segment. If NULL, some error occurred. + +**/ +NET_BUF * +TcpGetSegmentSock ( + IN TCP_CB *Tcb, + IN TCP_SEQNO Seq, + IN UINT32 Len + ) +{ + NET_BUF *Nbuf; + UINT8 *Data; + UINT32 DataGet; + + ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL)); + + Nbuf = NetbufAlloc (Len + TCP_MAX_HEAD); + + if (Nbuf == NULL) { + DEBUG ( + (EFI_D_ERROR, + "TcpGetSegmentSock: failed to allocate a netbuf for TCB %p\n", + Tcb) + ); + + return NULL; + } + + NetbufReserve (Nbuf, TCP_MAX_HEAD); + + DataGet = 0; + + if (Len != 0) { + // + // copy data to the segment. + // + Data = NetbufAllocSpace (Nbuf, Len, NET_BUF_TAIL); + ASSERT (Data != NULL); + + DataGet = SockGetDataToSend (Tcb->Sk, 0, Len, Data); + } + + NET_GET_REF (Nbuf); + + TCPSEG_NETBUF (Nbuf)->Seq = Seq; + TCPSEG_NETBUF (Nbuf)->End = Seq + Len; + + InsertTailList (&(Tcb->SndQue), &(Nbuf->List)); + + if (DataGet != 0) { + + SockDataSent (Tcb->Sk, DataGet); + } + + return Nbuf; +} + +/** + Get a segment starting from sequence Seq of a maximum + length of Len. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Seq The sequence number of the segment. + @param[in] Len The maximum length of the segment. + + @return Pointer to the segment. If NULL, some error occurred. + +**/ +NET_BUF * +TcpGetSegment ( + IN TCP_CB *Tcb, + IN TCP_SEQNO Seq, + IN UINT32 Len + ) +{ + NET_BUF *Nbuf; + + ASSERT (Tcb != NULL); + + // + // Compare the SndNxt with the max sequence number sent. + // + if ((Len != 0) && TCP_SEQ_LT (Seq, TcpGetMaxSndNxt (Tcb))) { + + Nbuf = TcpGetSegmentSndQue (Tcb, Seq, Len); + } else { + + Nbuf = TcpGetSegmentSock (Tcb, Seq, Len); + } + + if (TcpVerifySegment (Nbuf) == 0) { + NetbufFree (Nbuf); + return NULL; + } + + return Nbuf; +} + +/** + Retransmit the segment from sequence Seq. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Seq The sequence number of the segment to be retransmitted. + + @retval 0 Retransmission succeeded. + @retval -1 Error condition occurred. + +**/ +INTN +TcpRetransmit ( + IN TCP_CB *Tcb, + IN TCP_SEQNO Seq + ) +{ + NET_BUF *Nbuf; + UINT32 Len; + + // + // Compute the maxium length of retransmission. It is + // limited by three factors: + // 1. Less than SndMss + // 2. Must in the current send window + // 3. Will not change the boundaries of queued segments. + // + + // + // Handle the Window Retraction if TCP window scale is enabled according to RFC7323: + // On first retransmission, or if the sequence number is out of + // window by less than 2^Rcv.Wind.Shift, then do normal + // retransmission(s) without regard to the receiver window as long + // as the original segment was in window when it was sent. + // + if ((Tcb->SndWndScale != 0) && + (TCP_SEQ_GT (Seq, Tcb->RetxmitSeqMax) || TCP_SEQ_BETWEEN (Tcb->SndWl2 + Tcb->SndWnd, Seq, Tcb->SndWl2 + Tcb->SndWnd + (1 << Tcb->SndWndScale)))) { + Len = TCP_SUB_SEQ (Tcb->SndNxt, Seq); + DEBUG ( + (EFI_D_WARN, + "TcpRetransmit: retransmission without regard to the receiver window for TCB %p\n", + Tcb) + ); + + } else if (TCP_SEQ_GEQ (Tcb->SndWl2 + Tcb->SndWnd, Seq)) { + Len = TCP_SUB_SEQ (Tcb->SndWl2 + Tcb->SndWnd, Seq); + + } else { + DEBUG ( + (EFI_D_WARN, + "TcpRetransmit: retransmission cancelled because send window too small for TCB %p\n", + Tcb) + ); + + return 0; + } + + Len = MIN (Len, Tcb->SndMss); + + Nbuf = TcpGetSegmentSndQue (Tcb, Seq, Len); + if (Nbuf == NULL) { + return -1; + } + + if (TcpVerifySegment (Nbuf) == 0) { + goto OnError; + } + + if (TcpTransmitSegment (Tcb, Nbuf) != 0) { + goto OnError; + } + + if (TCP_SEQ_GT (Seq, Tcb->RetxmitSeqMax)) { + Tcb->RetxmitSeqMax = Seq; + } + + // + // The retransmitted buffer may be on the SndQue, + // trim TCP head because all the buffers on SndQue + // are headless. + // + ASSERT (Nbuf->Tcp != NULL); + NetbufTrim (Nbuf, (Nbuf->Tcp->HeadLen << 2), NET_BUF_HEAD); + Nbuf->Tcp = NULL; + + NetbufFree (Nbuf); + return 0; + +OnError: + if (Nbuf != NULL) { + NetbufFree (Nbuf); + } + + return -1; +} + +/** + Verify that all the segments in SndQue are in good shape. + + @param[in] Head Pointer to the head node of the SndQue. + + @retval 0 At least one segment is broken. + @retval 1 All segments in the specific queue are in good shape. + +**/ +INTN +TcpCheckSndQue ( + IN LIST_ENTRY *Head + ) +{ + LIST_ENTRY *Entry; + NET_BUF *Nbuf; + TCP_SEQNO Seq; + + if (IsListEmpty (Head)) { + return 1; + } + // + // Initialize the Seq. + // + Entry = Head->ForwardLink; + Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + Seq = TCPSEG_NETBUF (Nbuf)->Seq; + + NET_LIST_FOR_EACH (Entry, Head) { + Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List); + + if (TcpVerifySegment (Nbuf) == 0) { + return 0; + } + + // + // All the node in the SndQue should has: + // SEG.SEQ = LAST_SEG.END + // + if (Seq != TCPSEG_NETBUF (Nbuf)->Seq) { + return 0; + } + + Seq = TCPSEG_NETBUF (Nbuf)->End; + } + + return 1; +} + +/** + Check whether to send data/SYN/FIN and piggyback an ACK. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Force If TRUE, ignore the sender's SWS avoidance algorithm + and send out data by force. + + @return The number of bytes sent. + +**/ +INTN +TcpToSendData ( + IN OUT TCP_CB *Tcb, + IN INTN Force + ) +{ + UINT32 Len; + INTN Sent; + UINT8 Flag; + NET_BUF *Nbuf; + TCP_SEG *Seg; + TCP_SEQNO Seq; + TCP_SEQNO End; + + ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL) && (Tcb->State != TCP_LISTEN)); + + Sent = 0; + + if ((Tcb->State == TCP_CLOSED) || TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_SENT)) { + + return 0; + } + + do { + // + // Compute how much data can be sent + // + Len = TcpDataToSend (Tcb, Force); + Seq = Tcb->SndNxt; + + ASSERT ((Tcb->State) < (ARRAY_SIZE (mTcpOutFlag))); + Flag = mTcpOutFlag[Tcb->State]; + + if ((Flag & TCP_FLG_SYN) != 0) { + + Seq = Tcb->Iss; + Len = 0; + } + + // + // Only send a segment without data if SYN or + // FIN is set. + // + if ((Len == 0) && ((Flag & (TCP_FLG_SYN | TCP_FLG_FIN)) == 0)) { + return Sent; + } + + Nbuf = TcpGetSegment (Tcb, Seq, Len); + + if (Nbuf == NULL) { + DEBUG ( + (EFI_D_ERROR, + "TcpToSendData: failed to get a segment for TCB %p\n", + Tcb) + ); + + goto OnError; + } + + Seg = TCPSEG_NETBUF (Nbuf); + + // + // Set the TcpSeg in Nbuf. + // + Len = Nbuf->TotalSize; + End = Seq + Len; + if (TCP_FLG_ON (Flag, TCP_FLG_SYN)) { + End++; + } + + if ((Flag & TCP_FLG_FIN) != 0) { + // + // Send FIN if all data is sent, and FIN is + // in the window + // + if ((TcpGetMaxSndNxt (Tcb) == Tcb->SndNxt) && + (GET_SND_DATASIZE (Tcb->Sk) == 0) && + TCP_SEQ_LT (End + 1, Tcb->SndWnd + Tcb->SndWl2) + ) { + DEBUG ( + (EFI_D_NET, + "TcpToSendData: send FIN to peer for TCB %p in state %s\n", + Tcb, + mTcpStateName[Tcb->State]) + ); + + End++; + } else { + TCP_CLEAR_FLG (Flag, TCP_FLG_FIN); + } + } + + Seg->Seq = Seq; + Seg->End = End; + Seg->Flag = Flag; + + if (TcpVerifySegment (Nbuf) == 0 || TcpCheckSndQue (&Tcb->SndQue) == 0) { + DEBUG ( + (EFI_D_ERROR, + "TcpToSendData: discard a broken segment for TCB %p\n", + Tcb) + ); + goto OnError; + } + + // + // Don't send an empty segment here. + // + if (Seg->End == Seg->Seq) { + DEBUG ( + (EFI_D_WARN, + "TcpToSendData: created a empty segment for TCB %p, free it now\n", + Tcb) + ); + + goto OnError; + } + + if (TcpTransmitSegment (Tcb, Nbuf) != 0) { + NetbufTrim (Nbuf, (Nbuf->Tcp->HeadLen << 2), NET_BUF_HEAD); + Nbuf->Tcp = NULL; + + if ((Flag & TCP_FLG_FIN) != 0) { + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_FIN_SENT); + } + + goto OnError; + } + + Sent += TCP_SUB_SEQ (End, Seq); + + // + // All the buffers in the SndQue are headless. + // + ASSERT (Nbuf->Tcp != NULL); + + NetbufTrim (Nbuf, (Nbuf->Tcp->HeadLen << 2), NET_BUF_HEAD); + Nbuf->Tcp = NULL; + + NetbufFree (Nbuf); + + // + // Update the status in TCB. + // + Tcb->DelayedAck = 0; + + if ((Flag & TCP_FLG_FIN) != 0) { + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_FIN_SENT); + } + + if (TCP_SEQ_GT (End, Tcb->SndNxt)) { + Tcb->SndNxt = End; + } + + if (!TCP_TIMER_ON (Tcb->EnabledTimer, TCP_TIMER_REXMIT)) { + TcpSetTimer (Tcb, TCP_TIMER_REXMIT, Tcb->Rto); + } + + // + // Enable RTT measurement only if not in retransmit. + // Karn's algorithm requires not to update RTT when in loss. + // + if ((Tcb->CongestState == TCP_CONGEST_OPEN) && !TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RTT_ON)) { + + DEBUG ( + (EFI_D_NET, + "TcpToSendData: set RTT measure sequence %d for TCB %p\n", + Seq, + Tcb) + ); + + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON); + Tcb->RttSeq = Seq; + Tcb->RttMeasure = 0; + } + + } while (Len == Tcb->SndMss); + + return Sent; + +OnError: + if (Nbuf != NULL) { + NetbufFree (Nbuf); + } + + return Sent; +} + +/** + Send an ACK immediately. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpSendAck ( + IN OUT TCP_CB *Tcb + ) +{ + NET_BUF *Nbuf; + TCP_SEG *Seg; + + Nbuf = NetbufAlloc (TCP_MAX_HEAD); + + if (Nbuf == NULL) { + return; + } + + NetbufReserve (Nbuf, TCP_MAX_HEAD); + + Seg = TCPSEG_NETBUF (Nbuf); + Seg->Seq = Tcb->SndNxt; + Seg->End = Tcb->SndNxt; + Seg->Flag = TCP_FLG_ACK; + + if (TcpTransmitSegment (Tcb, Nbuf) == 0) { + TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW); + Tcb->DelayedAck = 0; + } + + NetbufFree (Nbuf); +} + +/** + Send a zero probe segment. It can be used by keepalive and zero window probe. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + + @retval 0 The zero probe segment was sent out successfully. + @retval other An error condition occurred. + +**/ +INTN +TcpSendZeroProbe ( + IN OUT TCP_CB *Tcb + ) +{ + NET_BUF *Nbuf; + TCP_SEG *Seg; + INTN Result; + + Nbuf = NetbufAlloc (TCP_MAX_HEAD); + + if (Nbuf == NULL) { + return -1; + } + + NetbufReserve (Nbuf, TCP_MAX_HEAD); + + // + // SndNxt-1 is out of window. The peer should respond + // with an ACK. + // + Seg = TCPSEG_NETBUF (Nbuf); + Seg->Seq = Tcb->SndNxt - 1; + Seg->End = Tcb->SndNxt - 1; + Seg->Flag = TCP_FLG_ACK; + + Result = TcpTransmitSegment (Tcb, Nbuf); + NetbufFree (Nbuf); + + return Result; +} + +/** + Check whether to send an ACK or delayed ACK. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpToSendAck ( + IN OUT TCP_CB *Tcb + ) +{ + UINT32 TcpNow; + + // + // Generally, TCP should send a delayed ACK unless: + // 1. ACK at least every other FULL sized segment received. + // 2. Packets received out of order. + // 3. Receiving window is open. + // + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW) || (Tcb->DelayedAck >= 1)) { + TcpSendAck (Tcb); + return; + } + + TcpNow = TcpRcvWinNow (Tcb); + + if (TcpNow > TcpRcvWinOld (Tcb)) { + TcpSendAck (Tcb); + return; + } + + DEBUG ( + (EFI_D_NET, + "TcpToSendAck: scheduled a delayed ACK for TCB %p\n", + Tcb) + ); + + // + // Schedule a delayed ACK. + // + Tcb->DelayedAck++; +} + +/** + Send a RESET segment in response to the segment received. + + @param[in] Tcb Pointer to the TCP_CB of this TCP instance. May be NULL. + @param[in] Head TCP header of the segment that triggers the reset. + @param[in] Len Length of the segment that triggers the reset. + @param[in] Local Local IP address. + @param[in] Remote Remote peer's IP address. + @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack, + IP_VERSION_6 indicates TCP is running on IP6 stack. + + @retval 0 A reset was sent or there is no need to send it. + @retval -1 No reset is sent. + +**/ +INTN +TcpSendReset ( + IN TCP_CB *Tcb, + IN TCP_HEAD *Head, + IN INT32 Len, + IN EFI_IP_ADDRESS *Local, + IN EFI_IP_ADDRESS *Remote, + IN UINT8 Version + ) +{ + NET_BUF *Nbuf; + TCP_HEAD *Nhead; + UINT16 HeadSum; + + // + // Don't respond to a Reset with reset. + // + if ((Head->Flag & TCP_FLG_RST) != 0) { + return 0; + } + + Nbuf = NetbufAlloc (TCP_MAX_HEAD); + + if (Nbuf == NULL) { + return -1; + } + + Nhead = (TCP_HEAD *) NetbufAllocSpace ( + Nbuf, + sizeof (TCP_HEAD), + NET_BUF_TAIL + ); + + ASSERT (Nhead != NULL); + + Nbuf->Tcp = Nhead; + Nhead->Flag = TCP_FLG_RST; + + // + // Derive Seq/ACK from the segment if no TCB + // is associated with it, otherwise derive from the Tcb. + // + if (Tcb == NULL) { + + if (TCP_FLG_ON (Head->Flag, TCP_FLG_ACK)) { + Nhead->Seq = Head->Ack; + Nhead->Ack = 0; + } else { + Nhead->Seq = 0; + TCP_SET_FLG (Nhead->Flag, TCP_FLG_ACK); + Nhead->Ack = HTONL (NTOHL (Head->Seq) + Len); + } + } else { + + Nhead->Seq = HTONL (Tcb->SndNxt); + Nhead->Ack = HTONL (Tcb->RcvNxt); + TCP_SET_FLG (Nhead->Flag, TCP_FLG_ACK); + } + + Nhead->SrcPort = Head->DstPort; + Nhead->DstPort = Head->SrcPort; + Nhead->HeadLen = (UINT8) (sizeof (TCP_HEAD) >> 2); + Nhead->Res = 0; + Nhead->Wnd = HTONS (0xFFFF); + Nhead->Checksum = 0; + Nhead->Urg = 0; + + if (Version == IP_VERSION_4) { + HeadSum = NetPseudoHeadChecksum (Local->Addr[0], Remote->Addr[0], 6, 0); + } else { + HeadSum = NetIp6PseudoHeadChecksum (&Local->v6, &Remote->v6, 6, 0); + } + + Nhead->Checksum = TcpChecksum (Nbuf, HeadSum); + + TcpSendIpPacket (Tcb, Nbuf, Local, Remote, Version); + + NetbufFree (Nbuf); + + return 0; +} + +/** + Verify that the segment is in good shape. + + @param[in] Nbuf The buffer that contains the segment to be checked. + + @retval 0 The segment is broken. + @retval 1 The segment is in good shape. + +**/ +INTN +TcpVerifySegment ( + IN NET_BUF *Nbuf + ) +{ + TCP_HEAD *Head; + TCP_SEG *Seg; + UINT32 Len; + + if (Nbuf == NULL) { + return 1; + } + + NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE); + + Seg = TCPSEG_NETBUF (Nbuf); + Len = Nbuf->TotalSize; + Head = Nbuf->Tcp; + + if (Head != NULL) { + if (Head->Flag != Seg->Flag) { + return 0; + } + + Len -= (Head->HeadLen << 2); + } + + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) { + Len++; + } + + if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) { + Len++; + } + + if (Seg->Seq + Len != Seg->End) { + return 0; + } + + return 1; +} + diff --git a/NetworkPkg/TcpDxe/TcpProto.h b/NetworkPkg/TcpDxe/TcpProto.h new file mode 100644 index 000000000..403ac98a5 --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpProto.h @@ -0,0 +1,344 @@ +/** @file + TCP protocol header file. + + Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _TCP_PROTO_H_ +#define _TCP_PROTO_H_ + +/// +/// Tcp states don't change their order. It is used as an +/// index to mTcpOutFlag and other macros. +/// +#define TCP_CLOSED 0 +#define TCP_LISTEN 1 +#define TCP_SYN_SENT 2 +#define TCP_SYN_RCVD 3 +#define TCP_ESTABLISHED 4 +#define TCP_FIN_WAIT_1 5 +#define TCP_FIN_WAIT_2 6 +#define TCP_CLOSING 7 +#define TCP_TIME_WAIT 8 +#define TCP_CLOSE_WAIT 9 +#define TCP_LAST_ACK 10 + + +/// +/// Flags in the TCP header +/// +#define TCP_FLG_FIN 0x01 +#define TCP_FLG_SYN 0x02 +#define TCP_FLG_RST 0x04 +#define TCP_FLG_PSH 0x08 +#define TCP_FLG_ACK 0x10 +#define TCP_FLG_URG 0x20 + + // + // mask for all the flags + // +#define TCP_FLG_FLAG 0x3F + + +#define TCP_CONNECT_REFUSED (-1) ///< TCP error status +#define TCP_CONNECT_RESET (-2) ///< TCP error status +#define TCP_CONNECT_CLOSED (-3) ///< TCP error status + +// +// Current congestion status as suggested by RFC3782. +// +#define TCP_CONGEST_RECOVER 1 ///< During the NewReno fast recovery. +#define TCP_CONGEST_LOSS 2 ///< Retxmit because of retxmit time out. +#define TCP_CONGEST_OPEN 3 ///< TCP is opening its congestion window. + +// +// TCP control flags +// +#define TCP_CTRL_NO_NAGLE 0x0001 ///< Disable Nagle algorithm +#define TCP_CTRL_NO_KEEPALIVE 0x0002 ///< Disable keepalive timer. +#define TCP_CTRL_NO_WS 0x0004 ///< Disable window scale option. +#define TCP_CTRL_RCVD_WS 0x0008 ///< Received a wnd scale option in syn. +#define TCP_CTRL_NO_TS 0x0010 ///< Disable Timestamp option. +#define TCP_CTRL_RCVD_TS 0x0020 ///< Received a Timestamp option in syn. +#define TCP_CTRL_SND_TS 0x0040 ///< Send Timestamp option to remote. +#define TCP_CTRL_SND_URG 0x0080 ///< In urgent send mode. +#define TCP_CTRL_RCVD_URG 0x0100 ///< In urgent receive mode. +#define TCP_CTRL_SND_PSH 0x0200 ///< In PUSH send mode. +#define TCP_CTRL_FIN_SENT 0x0400 ///< FIN is sent. +#define TCP_CTRL_FIN_ACKED 0x0800 ///< FIN is ACKed. +#define TCP_CTRL_TIMER_ON 0x1000 ///< At least one of the timer is on. +#define TCP_CTRL_RTT_ON 0x2000 ///< The RTT measurement is on. +#define TCP_CTRL_ACK_NOW 0x4000 ///< Send the ACK now, don't delay. + +// +// Timer related values +// +#define TCP_TIMER_CONNECT 0 ///< Connection establishment timer. +#define TCP_TIMER_REXMIT 1 ///< Retransmit timer. +#define TCP_TIMER_PROBE 2 ///< Window probe timer. +#define TCP_TIMER_KEEPALIVE 3 ///< Keepalive timer. +#define TCP_TIMER_FINWAIT2 4 ///< FIN_WAIT_2 timer. +#define TCP_TIMER_2MSL 5 ///< TIME_WAIT timer. +#define TCP_TIMER_NUMBER 6 ///< The total number of the TCP timer. +#define TCP_TICK 200 ///< Every TCP tick is 200ms. +#define TCP_TICK_HZ 5 ///< The frequence of TCP tick. +#define TCP_RTT_SHIFT 3 ///< SRTT & RTTVAR scaled by 8. +#define TCP_RTO_MIN TCP_TICK_HZ ///< The minium value of RTO. +#define TCP_RTO_MAX (TCP_TICK_HZ * 60) ///< The maxium value of RTO. +#define TCP_FOLD_RTT 4 ///< Timeout threshod to fold RTT. + +// +// Default values for some timers +// +#define TCP_MAX_LOSS 12 ///< Default max times to retxmit. +#define TCP_KEEPALIVE_IDLE_MIN (TCP_TICK_HZ * 60 * 60 * 2) ///< First keepalive. +#define TCP_KEEPALIVE_PERIOD (TCP_TICK_HZ * 60) +#define TCP_MAX_KEEPALIVE 8 +#define TCP_FIN_WAIT2_TIME (2 * TCP_TICK_HZ) +#define TCP_TIME_WAIT_TIME (2 * TCP_TICK_HZ) +#define TCP_PAWS_24DAY (24 * 24 * 60 * 60 * TCP_TICK_HZ) +#define TCP_CONNECT_TIME (75 * TCP_TICK_HZ) + +// +// The header space to be reserved before TCP data to accomodate : +// 60byte IP head + 60byte TCP head + link layer head +// +#define TCP_MAX_HEAD 192 + +// +// Value ranges for some control option +// +#define TCP_RCV_BUF_SIZE (2 * 1024 * 1024) +#define TCP_RCV_BUF_SIZE_MIN (8 * 1024) +#define TCP_SND_BUF_SIZE (2 * 1024 * 1024) +#define TCP_SND_BUF_SIZE_MIN (8 * 1024) +#define TCP_BACKLOG 10 +#define TCP_BACKLOG_MIN 5 +#define TCP_MAX_LOSS_MIN 6 +#define TCP_CONNECT_TIME_MIN (60 * TCP_TICK_HZ) +#define TCP_MAX_KEEPALIVE_MIN 4 +#define TCP_KEEPALIVE_IDLE_MAX (TCP_TICK_HZ * 60 * 60 * 4) +#define TCP_KEEPALIVE_PERIOD_MIN (TCP_TICK_HZ * 30) +#define TCP_FIN_WAIT2_TIME_MAX (4 * TCP_TICK_HZ) +#define TCP_TIME_WAIT_TIME_MAX (60 * TCP_TICK_HZ) + +/// +/// TCP_CONNECTED: both ends have synchronized their ISN. +/// +#define TCP_CONNECTED(state) ((state) > TCP_SYN_RCVD) + +#define TCP_FIN_RCVD(State) \ + ( \ + ((State) == TCP_CLOSE_WAIT) || \ + ((State) == TCP_LAST_ACK) || \ + ((State) == TCP_CLOSING) || \ + ((State) == TCP_TIME_WAIT) \ + ) + +#define TCP_LOCAL_CLOSED(State) \ + ( \ + ((State) == TCP_FIN_WAIT_1) || \ + ((State) == TCP_FIN_WAIT_2) || \ + ((State) == TCP_CLOSING) || \ + ((State) == TCP_TIME_WAIT) || \ + ((State) == TCP_LAST_ACK) \ + ) + +// +// Get the TCP_SEG point from a net buffer's ProtoData. +// +#define TCPSEG_NETBUF(NBuf) ((TCP_SEG *) ((NBuf)->ProtoData)) + +// +// Macros to compare sequence no +// +#define TCP_SEQ_LT(SeqA, SeqB) ((INT32) ((SeqA) - (SeqB)) < 0) +#define TCP_SEQ_LEQ(SeqA, SeqB) ((INT32) ((SeqA) - (SeqB)) <= 0) +#define TCP_SEQ_GT(SeqA, SeqB) ((INT32) ((SeqB) - (SeqA)) < 0) +#define TCP_SEQ_GEQ(SeqA, SeqB) ((INT32) ((SeqB) - (SeqA)) <= 0) + +// +// TCP_SEQ_BETWEEN return whether b <= m <= e +// +#define TCP_SEQ_BETWEEN(b, m, e) ((e) - (b) >= (m) - (b)) + +// +// TCP_SUB_SEQ returns Seq1 - Seq2. Make sure Seq1 >= Seq2 +// +#define TCP_SUB_SEQ(Seq1, Seq2) ((UINT32) ((Seq1) - (Seq2))) + +// +// Check whether Flag is on +// +#define TCP_FLG_ON(Value, Flag) ((BOOLEAN) (((Value) & (Flag)) != 0)) +// +// Set and Clear operation on a Flag +// +#define TCP_SET_FLG(Value, Flag) ((Value) |= (Flag)) +#define TCP_CLEAR_FLG(Value, Flag) ((Value) &= ~(Flag)) + +// +// Test whether two peers are equal +// +#define TCP_PEER_EQUAL(Pa, Pb, Ver) \ + (((Pa)->Port == (Pb)->Port) && TcpIsIpEqual(&((Pa)->Ip), &((Pb)->Ip), Ver)) + +// +// Test whether Pa matches Pb, or Pa is more specific +// than pb. Zero means wildcard. +// +#define TCP_PEER_MATCH(Pa, Pb, Ver) \ + ( \ + (((Pb)->Port == 0) || ((Pb)->Port == (Pa)->Port)) && \ + (TcpIsIpZero (&((Pb)->Ip), Ver) || TcpIsIpEqual (&((Pb)->Ip), &((Pa)->Ip), Ver)) \ + ) + +#define TCP_TIMER_ON(Flag, Timer) ((Flag) & (1 << (Timer))) +#define TCP_SET_TIMER(Flag, Timer) ((Flag) = (UINT16) ((Flag) | (1 << (Timer)))) +#define TCP_CLEAR_TIMER(Flag, Timer) ((Flag) = (UINT16) ((Flag) & (~(1 << (Timer))))) + + +#define TCP_TIME_LT(Ta, Tb) ((INT32) ((Ta) - (Tb)) < 0) +#define TCP_TIME_LEQ(Ta, Tb) ((INT32) ((Ta) - (Tb)) <= 0) +#define TCP_SUB_TIME(Ta, Tb) ((UINT32) ((Ta) - (Tb))) + +#define TCP_MAX_WIN 0xFFFFU + +/// +/// TCP segmentation data. +/// +typedef struct _TCP_SEG { + TCP_SEQNO Seq; ///< Starting sequence number. + TCP_SEQNO End; ///< The sequence of the last byte + 1, include SYN/FIN. End-Seq = SEG.LEN. + TCP_SEQNO Ack; ///< ACK field in the segment. + UINT8 Flag; ///< TCP header flags. + UINT16 Urg; ///< Valid if URG flag is set. + UINT32 Wnd; ///< TCP window size field. +} TCP_SEG; + +/// +/// Network endpoint, IP plus Port structure. +/// +typedef struct _TCP_PEER { + EFI_IP_ADDRESS Ip; ///< IP address, in network byte order. + TCP_PORTNO Port; ///< Port number, in network byte order. +} TCP_PEER; + +typedef struct _TCP_CONTROL_BLOCK TCP_CB; + +/// +/// TCP control block: it includes various states. +/// +struct _TCP_CONTROL_BLOCK { + LIST_ENTRY List; ///< Back and forward link entry + TCP_CB *Parent; ///< The parent TCP_CB structure + + SOCKET *Sk; ///< The socket it controled. + TCP_PEER LocalEnd; ///< Local endpoint. + TCP_PEER RemoteEnd;///< Remote endpoint. + + LIST_ENTRY SndQue; ///< Retxmission queue. + LIST_ENTRY RcvQue; ///< Reassemble queue. + UINT32 CtrlFlag; ///< Control flags, such as NO_NAGLE. + INT32 Error; ///< Soft error status, such as TCP_CONNECT_RESET. + + // + // RFC793 and RFC1122 defined variables + // + UINT8 State; ///< TCP state, such as SYN_SENT, LISTEN. + UINT8 DelayedAck; ///< Number of delayed ACKs. + UINT16 HeadSum; ///< Checksum of the fixed parts of pesudo + ///< header: Src IP, Dst IP, 0, Protocol, + ///< do not include the TCP length. + + TCP_SEQNO Iss; ///< Initial Sending Sequence. + TCP_SEQNO SndUna; ///< First unacknowledged data. + TCP_SEQNO SndNxt; ///< Next data sequence to send. + TCP_SEQNO SndPsh; ///< Send PUSH point. + TCP_SEQNO SndUp; ///< Send urgent point. + UINT32 SndWnd; ///< Window advertised by the remote peer. + UINT32 SndWndMax; ///< Max send window advertised by the peer. + TCP_SEQNO SndWl1; ///< Seq number used for last window update. + TCP_SEQNO SndWl2; ///< Ack no of last window update. + UINT16 SndMss; ///< Max send segment size. + TCP_SEQNO RcvNxt; ///< Next sequence no to receive. + UINT32 RcvWnd; ///< Window advertised by the local peer. + TCP_SEQNO RcvWl2; ///< The RcvNxt (or ACK) of last window update. + ///< It is necessary because of delayed ACK. + + TCP_SEQNO RcvUp; ///< Urgent point; + TCP_SEQNO Irs; ///< Initial Receiving Sequence. + UINT16 RcvMss; ///< Max receive segment size. + UINT16 EnabledTimer; ///< Which timer is currently enabled. + UINT32 Timer[TCP_TIMER_NUMBER]; ///< When the timer will expire. + INT32 NextExpire; ///< Countdown offset for the nearest timer. + UINT32 Idle; ///< How long the connection is in idle. + UINT32 ProbeTime; ///< The time out value for current window prober. + BOOLEAN ProbeTimerOn;///< If TRUE, the probe time is on. + + // + // RFC1323 defined variables, about window scale, + // timestamp and PAWS + // + UINT8 SndWndScale; ///< Wndscale received from the peer. + UINT8 RcvWndScale; ///< Wndscale used to scale local buffer. + UINT32 TsRecent; ///< TsRecent to echo to the remote peer. + UINT32 TsRecentAge; ///< When this TsRecent is updated. + + // + // RFC2988 defined variables. about RTT measurement + // + TCP_SEQNO RttSeq; ///< The seq of measured segment now. + UINT32 RttMeasure; ///< Currently measured RTT in heartbeats. + UINT32 SRtt; ///< Smoothed RTT, scaled by 8. + UINT32 RttVar; ///< RTT variance, scaled by 8. + UINT32 Rto; ///< Current RTO, not scaled. + + // + // RFC2581, and 3782 variables. + // Congestion control + NewReno fast recovery. + // + UINT32 CWnd; ///< Sender's congestion window. + UINT32 Ssthresh; ///< Slow start threshold. + TCP_SEQNO Recover; ///< Recover point for NewReno. + UINT16 DupAck; ///< Number of duplicate ACKs. + UINT8 CongestState; ///< The current congestion state(RFC3782). + UINT8 LossTimes; ///< Number of retxmit timeouts in a row. + TCP_SEQNO LossRecover; ///< Recover point for retxmit. + + // + // RFC7323 + // Addressing Window Retraction for TCP Window Scale Option. + // + TCP_SEQNO RetxmitSeqMax; ///< Max Seq number in previous retransmission. + + // + // configuration parameters, for EFI_TCP4_PROTOCOL specification + // + UINT32 KeepAliveIdle; ///< Idle time before sending first probe. + UINT32 KeepAlivePeriod; ///< Interval for subsequent keep alive probe. + UINT8 MaxKeepAlive; ///< Maxium keep alive probe times. + UINT8 KeepAliveProbes; ///< The number of keep alive probe. + UINT16 MaxRexmit; ///< The maxium number of retxmit before abort. + UINT32 FinWait2Timeout; ///< The FIN_WAIT_2 timeout. + UINT32 TimeWaitTimeout; ///< The TIME_WAIT timeout. + UINT32 ConnectTimeout; ///< The connect establishment timeout. + + // + // configuration for tcp provided by user + // + BOOLEAN UseDefaultAddr; + UINT8 Tos; + UINT8 Ttl; + EFI_IPv4_ADDRESS SubnetMask; + + + BOOLEAN RemoteIpZero; ///< RemoteEnd.Ip is ZERO when configured. + IP_IO_IP_INFO *IpInfo; ///< Pointer reference to Ip used to send pkt + UINT32 Tick; ///< 1 tick = 200ms +}; + +#endif diff --git a/NetworkPkg/TcpDxe/TcpTimer.c b/NetworkPkg/TcpDxe/TcpTimer.c new file mode 100644 index 000000000..2df89094b --- /dev/null +++ b/NetworkPkg/TcpDxe/TcpTimer.c @@ -0,0 +1,587 @@ +/** @file + TCP timer related functions. + + Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TcpMain.h" + +UINT32 mTcpTick = 1000; + +/** + Connect timeout handler. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpConnectTimeout ( + IN OUT TCP_CB *Tcb + ); + +/** + Timeout handler for TCP retransmission timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpRexmitTimeout ( + IN OUT TCP_CB *Tcb + ); + +/** + Timeout handler for window probe timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpProbeTimeout ( + IN OUT TCP_CB *Tcb + ); + +/** + Timeout handler for keepalive timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpKeepaliveTimeout ( + IN OUT TCP_CB *Tcb + ); + +/** + Timeout handler for FIN_WAIT_2 timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpFinwait2Timeout ( + IN OUT TCP_CB *Tcb + ); + +/** + Timeout handler for 2MSL timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +Tcp2MSLTimeout ( + IN OUT TCP_CB *Tcb + ); + +TCP_TIMER_HANDLER mTcpTimerHandler[TCP_TIMER_NUMBER] = { + TcpConnectTimeout, + TcpRexmitTimeout, + TcpProbeTimeout, + TcpKeepaliveTimeout, + TcpFinwait2Timeout, + Tcp2MSLTimeout, +}; + +/** + Close the TCP connection. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpClose ( + IN OUT TCP_CB *Tcb + ) +{ + NetbufFreeList (&Tcb->SndQue); + NetbufFreeList (&Tcb->RcvQue); + + TcpSetState (Tcb, TCP_CLOSED); +} + +/** + Backoff the RTO. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpBackoffRto ( + IN OUT TCP_CB *Tcb + ) +{ + // + // Fold the RTT estimate if too many times, the estimate + // may be wrong, fold it. So the next time a valid + // measurement is sampled, we can start fresh. + // + if ((Tcb->LossTimes >= TCP_FOLD_RTT) && (Tcb->SRtt != 0)) { + Tcb->RttVar += Tcb->SRtt >> 2; + Tcb->SRtt = 0; + } + + Tcb->Rto <<= 1; + + if (Tcb->Rto < TCP_RTO_MIN) { + + Tcb->Rto = TCP_RTO_MIN; + } else if (Tcb->Rto > TCP_RTO_MAX) { + + Tcb->Rto = TCP_RTO_MAX; + } +} + +/** + Connect timeout handler. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpConnectTimeout ( + IN OUT TCP_CB *Tcb + ) +{ + if (!TCP_CONNECTED (Tcb->State)) { + DEBUG ( + (EFI_D_ERROR, + "TcpConnectTimeout: connection closed because conenction timer timeout for TCB %p\n", + Tcb) + ); + + if (EFI_ABORTED == Tcb->Sk->SockError) { + SOCK_ERROR (Tcb->Sk, EFI_TIMEOUT); + } + + if (TCP_SYN_RCVD == Tcb->State) { + DEBUG ( + (EFI_D_WARN, + "TcpConnectTimeout: send reset because connection timer timeout for TCB %p\n", + Tcb) + ); + + TcpResetConnection (Tcb); + + } + + TcpClose (Tcb); + } +} + + +/** + Timeout handler for TCP retransmission timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpRexmitTimeout ( + IN OUT TCP_CB *Tcb + ) +{ + UINT32 FlightSize; + + DEBUG ( + (EFI_D_WARN, + "TcpRexmitTimeout: transmission timeout for TCB %p\n", + Tcb) + ); + + // + // Set the congestion window. FlightSize is the + // amount of data that has been sent but not + // yet ACKed. + // + FlightSize = TCP_SUB_SEQ (Tcb->SndNxt, Tcb->SndUna); + Tcb->Ssthresh = MAX ((UINT32) (2 * Tcb->SndMss), FlightSize / 2); + + Tcb->CWnd = Tcb->SndMss; + Tcb->LossRecover = Tcb->SndNxt; + + Tcb->LossTimes++; + if ((Tcb->LossTimes > Tcb->MaxRexmit) && !TCP_TIMER_ON (Tcb->EnabledTimer, TCP_TIMER_CONNECT)) { + + DEBUG ( + (EFI_D_ERROR, + "TcpRexmitTimeout: connection closed because too many timeouts for TCB %p\n", + Tcb) + ); + + if (EFI_ABORTED == Tcb->Sk->SockError) { + SOCK_ERROR (Tcb->Sk, EFI_TIMEOUT); + } + + TcpClose (Tcb); + return ; + } + + TcpBackoffRto (Tcb); + TcpRetransmit (Tcb, Tcb->SndUna); + TcpSetTimer (Tcb, TCP_TIMER_REXMIT, Tcb->Rto); + + Tcb->CongestState = TCP_CONGEST_LOSS; + + TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON); +} + +/** + Timeout handler for window probe timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpProbeTimeout ( + IN OUT TCP_CB *Tcb + ) +{ + // + // This is the timer for sender's SWSA. RFC1122 requires + // a timer set for sender's SWSA, and suggest combine it + // with window probe timer. If data is sent, don't set + // the probe timer, since retransmit timer is on. + // + if ((TcpDataToSend (Tcb, 1) != 0) && (TcpToSendData (Tcb, 1) > 0)) { + + ASSERT (TCP_TIMER_ON (Tcb->EnabledTimer, TCP_TIMER_REXMIT) != 0); + Tcb->ProbeTimerOn = FALSE; + return ; + } + + TcpSendZeroProbe (Tcb); + TcpSetProbeTimer (Tcb); +} + +/** + Timeout handler for keepalive timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpKeepaliveTimeout ( + IN OUT TCP_CB *Tcb + ) +{ + Tcb->KeepAliveProbes++; + + // + // Too many Keep-alive probes, drop the connection + // + if (Tcb->KeepAliveProbes > Tcb->MaxKeepAlive) { + + if (EFI_ABORTED == Tcb->Sk->SockError) { + SOCK_ERROR (Tcb->Sk, EFI_TIMEOUT); + } + + TcpClose (Tcb); + return ; + } + + TcpSendZeroProbe (Tcb); + TcpSetKeepaliveTimer (Tcb); +} + +/** + Timeout handler for FIN_WAIT_2 timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpFinwait2Timeout ( + IN OUT TCP_CB *Tcb + ) +{ + DEBUG ( + (EFI_D_WARN, + "TcpFinwait2Timeout: connection closed because FIN_WAIT2 timer timeouts for TCB %p\n", + Tcb) + ); + + TcpClose (Tcb); +} + +/** + Timeout handler for 2MSL timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +Tcp2MSLTimeout ( + IN OUT TCP_CB *Tcb + ) +{ + DEBUG ( + (EFI_D_WARN, + "Tcp2MSLTimeout: connection closed because TIME_WAIT timer timeouts for TCB %p\n", + Tcb) + ); + + TcpClose (Tcb); +} + +/** + Update the timer status and the next expire time according to the timers + to expire in a specific future time slot. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpUpdateTimer ( + IN OUT TCP_CB *Tcb + ) +{ + UINT16 Index; + + // + // Don't use a too large value to init NextExpire + // since mTcpTick wraps around as sequence no does. + // + Tcb->NextExpire = TCP_EXPIRE_TIME; + TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_TIMER_ON); + + for (Index = 0; Index < TCP_TIMER_NUMBER; Index++) { + + if (TCP_TIMER_ON (Tcb->EnabledTimer, Index) && + TCP_TIME_LT (Tcb->Timer[Index], mTcpTick + Tcb->NextExpire) + ) { + + Tcb->NextExpire = TCP_SUB_TIME (Tcb->Timer[Index], mTcpTick); + TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_TIMER_ON); + } + } +} + +/** + Enable a TCP timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Timer The index of the timer to be enabled. + @param[in] TimeOut The timeout value of this timer. + +**/ +VOID +TcpSetTimer ( + IN OUT TCP_CB *Tcb, + IN UINT16 Timer, + IN UINT32 TimeOut + ) +{ + TCP_SET_TIMER (Tcb->EnabledTimer, Timer); + Tcb->Timer[Timer] = mTcpTick + TimeOut; + + TcpUpdateTimer (Tcb); +} + +/** + Clear one TCP timer. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + @param[in] Timer The index of the timer to be cleared. + +**/ +VOID +TcpClearTimer ( + IN OUT TCP_CB *Tcb, + IN UINT16 Timer + ) +{ + TCP_CLEAR_TIMER (Tcb->EnabledTimer, Timer); + TcpUpdateTimer (Tcb); +} + +/** + Clear all TCP timers. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpClearAllTimer ( + IN OUT TCP_CB *Tcb + ) +{ + Tcb->EnabledTimer = 0; + TcpUpdateTimer (Tcb); +} + +/** + Enable the window prober timer and set the timeout value. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpSetProbeTimer ( + IN OUT TCP_CB *Tcb + ) +{ + if (!Tcb->ProbeTimerOn) { + Tcb->ProbeTime = Tcb->Rto; + Tcb->ProbeTimerOn = TRUE; + + } else { + Tcb->ProbeTime <<= 1; + } + + if (Tcb->ProbeTime < TCP_RTO_MIN) { + + Tcb->ProbeTime = TCP_RTO_MIN; + } else if (Tcb->ProbeTime > TCP_RTO_MAX) { + + Tcb->ProbeTime = TCP_RTO_MAX; + } + + TcpSetTimer (Tcb, TCP_TIMER_PROBE, Tcb->ProbeTime); +} + +/** + Enable the keepalive timer and set the timeout value. + + @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance. + +**/ +VOID +TcpSetKeepaliveTimer ( + IN OUT TCP_CB *Tcb + ) +{ + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_KEEPALIVE)) { + return ; + + } + + // + // Set the timer to KeepAliveIdle if either + // 1. the keepalive timer is off + // 2. The keepalive timer is on, but the idle + // is less than KeepAliveIdle, that means the + // connection is alive since our last probe. + // + if (!TCP_TIMER_ON (Tcb->EnabledTimer, TCP_TIMER_KEEPALIVE) || + (Tcb->Idle < Tcb->KeepAliveIdle) + ) { + + TcpSetTimer (Tcb, TCP_TIMER_KEEPALIVE, Tcb->KeepAliveIdle); + Tcb->KeepAliveProbes = 0; + + } else { + + TcpSetTimer (Tcb, TCP_TIMER_KEEPALIVE, Tcb->KeepAlivePeriod); + } +} + +/** + Heart beat timer handler. + + @param[in] Context Context of the timer event, ignored. + +**/ +VOID +EFIAPI +TcpTickingDpc ( + IN VOID *Context + ) +{ + LIST_ENTRY *Entry; + LIST_ENTRY *Next; + TCP_CB *Tcb; + INT16 Index; + + mTcpTick++; + mTcpGlobalIss += TCP_ISS_INCREMENT_2; + + // + // Don't use LIST_FOR_EACH, which isn't delete safe. + // + for (Entry = mTcpRunQue.ForwardLink; Entry != &mTcpRunQue; Entry = Next) { + + Next = Entry->ForwardLink; + + Tcb = NET_LIST_USER_STRUCT (Entry, TCP_CB, List); + + if (Tcb->State == TCP_CLOSED) { + continue; + } + // + // The connection is doing RTT measurement. + // + if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RTT_ON)) { + Tcb->RttMeasure++; + } + + Tcb->Idle++; + + if (Tcb->DelayedAck != 0) { + TcpSendAck (Tcb); + } + + if (Tcb->IpInfo->IpVersion == IP_VERSION_6 && Tcb->Tick > 0) { + Tcb->Tick--; + } + + // + // No timer is active or no timer expired + // + if (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_TIMER_ON) || ((--Tcb->NextExpire) > 0)) { + + continue; + } + + // + // Call the timeout handler for each expired timer. + // + for (Index = 0; Index < TCP_TIMER_NUMBER; Index++) { + + if (TCP_TIMER_ON (Tcb->EnabledTimer, Index) && TCP_TIME_LEQ (Tcb->Timer[Index], mTcpTick)) { + // + // disable the timer before calling the handler + // in case the handler enables it again. + // + TCP_CLEAR_TIMER (Tcb->EnabledTimer, Index); + mTcpTimerHandler[Index](Tcb); + + // + // The Tcb may have been deleted by the timer, or + // no other timer is set. + // + if ((Next->BackLink != Entry) || (Tcb->EnabledTimer == 0)) { + break; + } + } + } + + // + // If the Tcb still exist or some timer is set, update the timer + // + if (Index == TCP_TIMER_NUMBER) { + TcpUpdateTimer (Tcb); + } + } +} + +/** + Heart beat timer handler, queues the DPC at TPL_CALLBACK. + + @param[in] Event Timer event signaled, ignored. + @param[in] Context Context of the timer event, ignored. + +**/ +VOID +EFIAPI +TcpTicking ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + QueueDpc (TPL_CALLBACK, TcpTickingDpc, Context); +} + diff --git a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.c b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.c new file mode 100644 index 000000000..18ee76300 --- /dev/null +++ b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.c @@ -0,0 +1,128 @@ +/** @file + The DriverEntryPoint for TlsAuthConfigDxe driver. + + Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TlsAuthConfigImpl.h" + +/** + Unloads an image. + + @param ImageHandle Handle that identifies the image to be unloaded. + + @retval EFI_SUCCESS The image has been unloaded. + @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle. + +**/ +EFI_STATUS +EFIAPI +TlsAuthConfigDxeUnload ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_STATUS Status; + TLS_AUTH_CONFIG_PRIVATE_DATA *PrivateData; + + Status = gBS->HandleProtocol ( + ImageHandle, + &gEfiCallerIdGuid, + (VOID **) &PrivateData + ); + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (PrivateData->Signature == TLS_AUTH_CONFIG_PRIVATE_DATA_SIGNATURE); + + gBS->UninstallMultipleProtocolInterfaces ( + &ImageHandle, + &gEfiCallerIdGuid, + PrivateData, + NULL + ); + + TlsAuthConfigFormUnload (PrivateData); + + return EFI_SUCCESS; +} + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + @param ImageHandle The firmware allocated handle for the UEFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval Others An unexpected error occurred. +**/ +EFI_STATUS +EFIAPI +TlsAuthConfigDxeDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + TLS_AUTH_CONFIG_PRIVATE_DATA *PrivateData; + + PrivateData = NULL; + + // + // If already started, return. + // + Status = gBS->OpenProtocol ( + ImageHandle, + &gEfiCallerIdGuid, + NULL, + ImageHandle, + ImageHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + // + // Initialize the private data structure. + // + PrivateData = AllocateZeroPool (sizeof (TLS_AUTH_CONFIG_PRIVATE_DATA)); + if (PrivateData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Initialize the HII configuration form. + // + Status = TlsAuthConfigFormInit (PrivateData); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Install private GUID. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ImageHandle, + &gEfiCallerIdGuid, + PrivateData, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + TlsAuthConfigFormUnload (PrivateData); + + return Status; +} + diff --git a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.inf b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.inf new file mode 100644 index 000000000..3fc924a1d --- /dev/null +++ b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.inf @@ -0,0 +1,68 @@ +## @file +# Provides the capability to configure Tls Authentication in a setup browser +# By this module, user may change the content of TlsCaCertificate. +# +# Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = TlsAuthConfigDxe + MODULE_UNI_FILE = TlsAuthConfigDxe.uni + FILE_GUID = 7ca1024f-eb17-11e5-9dba-28d2447c4829 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = TlsAuthConfigDxeDriverEntryPoint + UNLOAD_IMAGE = TlsAuthConfigDxeUnload + +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[Sources] + TlsAuthConfigImpl.c + TlsAuthConfigImpl.h + TlsAuthConfigNvData.h + TlsAuthConfigDxe.c + TlsAuthConfigDxeStrings.uni + TlsAuthConfigVfr.vfr + +[LibraryClasses] + BaseLib + BaseMemoryLib + MemoryAllocationLib + UefiLib + UefiBootServicesTableLib + UefiRuntimeServicesTableLib + UefiDriverEntryPoint + DebugLib + HiiLib + DevicePathLib + UefiHiiServicesLib + FileExplorerLib + PrintLib + +[Protocols] + gEfiDevicePathProtocolGuid ## PRODUCES + gEfiHiiConfigAccessProtocolGuid ## PRODUCES + +[Guids] + gTlsAuthConfigGuid ## PRODUCES ## GUID + gEfiCertX509Guid ## SOMETIMES_CONSUMES ## GUID # Indicate the cert type + gEfiIfrTianoGuid ## SOMETIMES_PRODUCES ## HII + gEfiTlsCaCertificateGuid ## PRODUCES ## Variable:L"TlsCaCertificate" + +[Depex] + gEfiHiiConfigRoutingProtocolGuid AND + gEfiHiiDatabaseProtocolGuid + +[UserExtensions.TianoCore."ExtraFiles"] + TlsAuthConfigDxeExtra.uni + diff --git a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.uni b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.uni new file mode 100644 index 000000000..b235868d5 --- /dev/null +++ b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxe.uni @@ -0,0 +1,16 @@ +// /** @file +// Provides the capability to configure Tls Authentication in a setup browser +// +// By this module, user may change the content of TlsCaCertificate. +// +// Copyright (c) 2016, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Provides the capability to configure Tls Authentication in a setup browser" + +#string STR_MODULE_DESCRIPTION #language en-US "By this module, user may change the content of TlsCaCertificate." + diff --git a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxeExtra.uni b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxeExtra.uni new file mode 100644 index 000000000..57ba1c74c --- /dev/null +++ b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// TlsAuthConfigDxe Localized Strings and Content +// +// Copyright (c) 2016, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"TLS Auth Config DXE" + + diff --git a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxeStrings.uni b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxeStrings.uni new file mode 100644 index 000000000..973b8b771 --- /dev/null +++ b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigDxeStrings.uni @@ -0,0 +1,33 @@ +/** @file + String definitions for Tls Authentication Configuration form. + +Copyright (c) 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#langdef en-US "English" + +#string STR_TLS_AUTH_CONFIG_TITLE #language en-US "Tls Auth Configuration" +#string STR_TLS_AUTH_CONFIG_HELP #language en-US "Press to select Tls Auth Configuration." + +#string STR_TLS_AUTH_CONFIG_SERVER_CA #language en-US "Server CA Configuration" +#string STR_TLS_AUTH_CONFIG_SERVER_CA_HELP #language en-US "Press to configure Server CA." +#string STR_TLS_AUTH_CONFIG_CLIENT_CERT #language en-US "Client Cert Configuration" +#string STR_TLS_AUTH_CONFIG_CLIENT_CERT_HELP #language en-US "Client cert configuration is unsupported currently." + +#string STR_TLS_AUTH_CONFIG_ENROLL_CERT #language en-US "Enroll Cert" +#string STR_TLS_AUTH_CONFIG_ENROLL_CERT_HELP #language en-US "Press to enroll cert." +#string STR_TLS_AUTH_CONFIG_DELETE_CERT #language en-US "Delete Cert" +#string STR_TLS_AUTH_CONFIG_DELETE_CERT_HELP #language en-US "Press to delete cert." + +#string STR_TLS_AUTH_CONFIG_ADD_CERT_FILE #language en-US "Enroll Cert Using File" + +#string STR_TLS_AUTH_CONFIG_CERT_GUID #language en-US "Cert GUID" +#string STR_TLS_AUTH_CONFIG_CERT_GUID_HELP #language en-US "Input digit character in 11111111-2222-3333-4444-1234567890ab format." +#string STR_TLS_AUTH_CONFIG_SAVE_AND_EXIT #language en-US "Commit Changes and Exit" +#string STR_TLS_AUTH_CONFIG_NO_SAVE_AND_EXIT #language en-US "Discard Changes and Exit" + +#string STR_CERT_TYPE_PCKS_GUID #language en-US "GUID for CERT" + +#string STR_NULL #language en-US "" diff --git a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c new file mode 100644 index 000000000..666216545 --- /dev/null +++ b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c @@ -0,0 +1,1571 @@ +/** @file + The Miscellaneous Routines for TlsAuthConfigDxe driver. + +Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+ +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TlsAuthConfigImpl.h" + +VOID *mStartOpCodeHandle = NULL; +VOID *mEndOpCodeHandle = NULL; +EFI_IFR_GUID_LABEL *mStartLabel = NULL; +EFI_IFR_GUID_LABEL *mEndLabel = NULL; + + +CHAR16 mTlsAuthConfigStorageName[] = L"TLS_AUTH_CONFIG_IFR_NVDATA"; + +TLS_AUTH_CONFIG_PRIVATE_DATA *mTlsAuthPrivateData = NULL; + +HII_VENDOR_DEVICE_PATH mTlsAuthConfigHiiVendorDevicePath = { + { + { + HARDWARE_DEVICE_PATH, + HW_VENDOR_DP, + { + (UINT8) (sizeof (VENDOR_DEVICE_PATH)), + (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8) + } + }, + TLS_AUTH_CONFIG_GUID + }, + { + END_DEVICE_PATH_TYPE, + END_ENTIRE_DEVICE_PATH_SUBTYPE, + { + (UINT8) (END_DEVICE_PATH_LENGTH), + (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8) + } + } +}; + +// +// Possible DER-encoded certificate file suffixes, end with NULL pointer. +// +CHAR16* mDerPemEncodedSuffix[] = { + L".cer", + L".der", + L".crt", + L".pem", + NULL +}; + +/** + This code checks if the FileSuffix is one of the possible DER/PEM-encoded certificate suffix. + + @param[in] FileSuffix The suffix of the input certificate file + + @retval TRUE It's a DER/PEM-encoded certificate. + @retval FALSE It's NOT a DER/PEM-encoded certificate. + +**/ +BOOLEAN +IsDerPemEncodeCertificate ( + IN CONST CHAR16 *FileSuffix +) +{ + UINTN Index; + for (Index = 0; mDerPemEncodedSuffix[Index] != NULL; Index++) { + if (StrCmp (FileSuffix, mDerPemEncodedSuffix[Index]) == 0) { + return TRUE; + } + } + return FALSE; +} + +/** + Worker function that prints an EFI_GUID into specified Buffer. + + @param[in] Guid Pointer to GUID to print. + @param[in] Buffer Buffer to print Guid into. + @param[in] BufferSize Size of Buffer. + + @retval Number of characters printed. + +**/ +UINTN +GuidToString ( + IN EFI_GUID *Guid, + IN CHAR16 *Buffer, + IN UINTN BufferSize + ) +{ + return UnicodeSPrint ( + Buffer, + BufferSize, + L"%g", + Guid + ); +} + +/** + List all cert in specified database by GUID in the page + for user to select and delete as needed. + + @param[in] PrivateData Module's private data. + @param[in] VariableName The variable name of the vendor's signature database. + @param[in] VendorGuid A unique identifier for the vendor. + @param[in] LabelNumber Label number to insert opcodes. + @param[in] FormId Form ID of current page. + @param[in] QuestionIdBase Base question id of the signature list. + + @retval EFI_SUCCESS Success to update the signature list page + @retval EFI_OUT_OF_RESOURCES Unable to allocate required resources. + +**/ +EFI_STATUS +UpdateDeletePage ( + IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private, + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN UINT16 LabelNumber, + IN EFI_FORM_ID FormId, + IN EFI_QUESTION_ID QuestionIdBase + ) +{ + EFI_STATUS Status; + UINT32 Index; + UINTN CertCount; + UINTN GuidIndex; + VOID *StartOpCodeHandle; + VOID *EndOpCodeHandle; + EFI_IFR_GUID_LABEL *StartLabel; + EFI_IFR_GUID_LABEL *EndLabel; + UINTN DataSize; + UINT8 *Data; + EFI_SIGNATURE_LIST *CertList; + EFI_SIGNATURE_DATA *Cert; + UINT32 ItemDataSize; + CHAR16 *GuidStr; + EFI_STRING_ID GuidID; + EFI_STRING_ID Help; + + Data = NULL; + CertList = NULL; + Cert = NULL; + GuidStr = NULL; + StartOpCodeHandle = NULL; + EndOpCodeHandle = NULL; + + // + // Initialize the container for dynamic opcodes. + // + StartOpCodeHandle = HiiAllocateOpCodeHandle (); + if (StartOpCodeHandle == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + EndOpCodeHandle = HiiAllocateOpCodeHandle (); + if (EndOpCodeHandle == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Create Hii Extend Label OpCode. + // + StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + StartOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + StartLabel->Number = LabelNumber; + + EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + EndOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + EndLabel->Number = LABEL_END; + + // + // Read Variable. + // + DataSize = 0; + Status = gRT->GetVariable (VariableName, VendorGuid, NULL, &DataSize, Data); + if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) { + goto ON_EXIT; + } + + Data = (UINT8 *) AllocateZeroPool (DataSize); + if (Data == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Status = gRT->GetVariable (VariableName, VendorGuid, NULL, &DataSize, Data); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + GuidStr = AllocateZeroPool (100); + if (GuidStr == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Enumerate all data. + // + ItemDataSize = (UINT32) DataSize; + CertList = (EFI_SIGNATURE_LIST *) Data; + GuidIndex = 0; + + while ((ItemDataSize > 0) && (ItemDataSize >= CertList->SignatureListSize)) { + + if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) { + Help = STRING_TOKEN (STR_CERT_TYPE_PCKS_GUID); + } else { + // + // The signature type is not supported in current implementation. + // + ItemDataSize -= CertList->SignatureListSize; + CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize); + continue; + } + + CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize; + for (Index = 0; Index < CertCount; Index++) { + Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + + sizeof (EFI_SIGNATURE_LIST) + + CertList->SignatureHeaderSize + + Index * CertList->SignatureSize); + // + // Display GUID and help + // + GuidToString (&Cert->SignatureOwner, GuidStr, 100); + GuidID = HiiSetString (Private->RegisteredHandle, 0, GuidStr, NULL); + HiiCreateCheckBoxOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (QuestionIdBase + GuidIndex++), + 0, + 0, + GuidID, + Help, + EFI_IFR_FLAG_CALLBACK, + 0, + NULL + ); + } + + ItemDataSize -= CertList->SignatureListSize; + CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize); + } + +ON_EXIT: + HiiUpdateForm ( + Private->RegisteredHandle, + &gTlsAuthConfigGuid, + FormId, + StartOpCodeHandle, + EndOpCodeHandle + ); + + if (StartOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (StartOpCodeHandle); + } + + if (EndOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (EndOpCodeHandle); + } + + if (Data != NULL) { + FreePool (Data); + } + + if (GuidStr != NULL) { + FreePool (GuidStr); + } + + return EFI_SUCCESS; +} + +/** + Delete one entry from cert database. + + @param[in] Private Module's private data. + @param[in] VariableName The variable name of the database. + @param[in] VendorGuid A unique identifier for the vendor. + @param[in] LabelNumber Label number to insert opcodes. + @param[in] FormId Form ID of current page. + @param[in] QuestionIdBase Base question id of the cert list. + @param[in] DeleteIndex Cert index to delete. + + @retval EFI_SUCCESS Delete siganture successfully. + @retval EFI_NOT_FOUND Can't find the signature item, + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. +**/ +EFI_STATUS +DeleteCert ( + IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private, + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN UINT16 LabelNumber, + IN EFI_FORM_ID FormId, + IN EFI_QUESTION_ID QuestionIdBase, + IN UINTN DeleteIndex + ) +{ + EFI_STATUS Status; + UINTN DataSize; + UINT8 *Data; + UINT8 *OldData; + UINT32 Attr; + UINT32 Index; + EFI_SIGNATURE_LIST *CertList; + EFI_SIGNATURE_LIST *NewCertList; + EFI_SIGNATURE_DATA *Cert; + UINTN CertCount; + UINT32 Offset; + BOOLEAN IsItemFound; + UINT32 ItemDataSize; + UINTN GuidIndex; + + Data = NULL; + OldData = NULL; + CertList = NULL; + Cert = NULL; + Attr = 0; + + // + // Get original signature list data. + // + DataSize = 0; + Status = gRT->GetVariable (VariableName, VendorGuid, NULL, &DataSize, NULL); + if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) { + goto ON_EXIT; + } + + OldData = (UINT8 *) AllocateZeroPool (DataSize); + if (OldData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + Status = gRT->GetVariable (VariableName, VendorGuid, &Attr, &DataSize, OldData); + if (EFI_ERROR(Status)) { + goto ON_EXIT; + } + + // + // Allocate space for new variable. + // + Data = (UINT8*) AllocateZeroPool (DataSize); + if (Data == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Enumerate all data and erasing the target item. + // + IsItemFound = FALSE; + ItemDataSize = (UINT32) DataSize; + CertList = (EFI_SIGNATURE_LIST *) OldData; + Offset = 0; + GuidIndex = 0; + while ((ItemDataSize > 0) && (ItemDataSize >= CertList->SignatureListSize)) { + if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) { + // + // Copy EFI_SIGNATURE_LIST header then calculate the signature count in this list. + // + CopyMem (Data + Offset, CertList, (sizeof(EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize)); + NewCertList = (EFI_SIGNATURE_LIST*) (Data + Offset); + Offset += (sizeof(EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize); + Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize); + CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize; + for (Index = 0; Index < CertCount; Index++) { + if (GuidIndex == DeleteIndex) { + // + // Find it! Skip it! + // + NewCertList->SignatureListSize -= CertList->SignatureSize; + IsItemFound = TRUE; + } else { + // + // This item doesn't match. Copy it to the Data buffer. + // + CopyMem (Data + Offset, (UINT8*)(Cert), CertList->SignatureSize); + Offset += CertList->SignatureSize; + } + GuidIndex++; + Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize); + } + } else { + // + // This List doesn't match. Just copy it to the Data buffer. + // + CopyMem (Data + Offset, (UINT8*)(CertList), CertList->SignatureListSize); + Offset += CertList->SignatureListSize; + } + + ItemDataSize -= CertList->SignatureListSize; + CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize); + } + + if (!IsItemFound) { + // + // Doesn't find the signature Item! + // + Status = EFI_NOT_FOUND; + goto ON_EXIT; + } + + // + // Delete the EFI_SIGNATURE_LIST header if there is no signature in the list. + // + ItemDataSize = Offset; + CertList = (EFI_SIGNATURE_LIST *) Data; + Offset = 0; + ZeroMem (OldData, ItemDataSize); + while ((ItemDataSize > 0) && (ItemDataSize >= CertList->SignatureListSize)) { + CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize; + DEBUG ((DEBUG_INFO, " CertCount = %x\n", CertCount)); + if (CertCount != 0) { + CopyMem (OldData + Offset, (UINT8*)(CertList), CertList->SignatureListSize); + Offset += CertList->SignatureListSize; + } + ItemDataSize -= CertList->SignatureListSize; + CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize); + } + + DataSize = Offset; + + Status = gRT->SetVariable( + VariableName, + VendorGuid, + Attr, + DataSize, + OldData + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed to set variable, Status = %r\n", Status)); + goto ON_EXIT; + } + +ON_EXIT: + if (Data != NULL) { + FreePool(Data); + } + + if (OldData != NULL) { + FreePool(OldData); + } + + return UpdateDeletePage ( + Private, + VariableName, + VendorGuid, + LabelNumber, + FormId, + QuestionIdBase + ); +} + + +/** + Clean the file related resource. + + @param[in] Private Module's private data. + +**/ +VOID +CleanFileContext ( + IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private + ) +{ + if (Private->FileContext->FHandle != NULL) { + Private->FileContext->FHandle->Close (Private->FileContext->FHandle); + Private->FileContext->FHandle = NULL; + if (Private->FileContext->FileName!= NULL){ + FreePool(Private->FileContext->FileName); + Private->FileContext->FileName = NULL; + } + } +} + +/** + Read file content into BufferPtr, the size of the allocate buffer + is *FileSize plus AddtionAllocateSize. + + @param[in] FileHandle The file to be read. + @param[in, out] BufferPtr Pointers to the pointer of allocated buffer. + @param[out] FileSize Size of input file + @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated. + In case the buffer need to contain others besides the file content. + + @retval EFI_SUCCESS The file was read into the buffer. + @retval EFI_INVALID_PARAMETER A parameter was invalid. + @retval EFI_OUT_OF_RESOURCES A memory allocation failed. + @retval others Unexpected error. + +**/ +EFI_STATUS +ReadFileContent ( + IN EFI_FILE_HANDLE FileHandle, + IN OUT VOID **BufferPtr, + OUT UINTN *FileSize, + IN UINTN AddtionAllocateSize + ) + +{ + UINTN BufferSize; + UINT64 SourceFileSize; + VOID *Buffer; + EFI_STATUS Status; + + if ((FileHandle == NULL) || (FileSize == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Buffer = NULL; + + // + // Get the file size + // + Status = FileHandle->SetPosition (FileHandle, (UINT64) -1); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = FileHandle->GetPosition (FileHandle, &SourceFileSize); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = FileHandle->SetPosition (FileHandle, 0); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + BufferSize = (UINTN) SourceFileSize + AddtionAllocateSize; + Buffer = AllocateZeroPool(BufferSize); + if (Buffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + BufferSize = (UINTN) SourceFileSize; + *FileSize = BufferSize; + + Status = FileHandle->Read (FileHandle, &BufferSize, Buffer); + if (EFI_ERROR (Status) || BufferSize != *FileSize) { + FreePool (Buffer); + Buffer = NULL; + Status = EFI_BAD_BUFFER_SIZE; + goto ON_EXIT; + } + +ON_EXIT: + + *BufferPtr = Buffer; + return Status; +} + +/** + This function converts an input device structure to a Unicode string. + + @param[in] DevPath A pointer to the device path structure. + + @return A new allocated Unicode string that represents the device path. + +**/ +CHAR16 * +EFIAPI +DevicePathToStr ( + IN EFI_DEVICE_PATH_PROTOCOL *DevPath + ) +{ + return ConvertDevicePathToText ( + DevPath, + FALSE, + TRUE + ); +} + + +/** + Extract filename from device path. The returned buffer is allocated using AllocateCopyPool. + The caller is responsible for freeing the allocated buffer using FreePool(). If return NULL + means not enough memory resource. + + @param DevicePath Device path. + + @retval NULL Not enough memory resourece for AllocateCopyPool. + @retval Other A new allocated string that represents the file name. + +**/ +CHAR16 * +ExtractFileNameFromDevicePath ( + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath + ) +{ + CHAR16 *String; + CHAR16 *MatchString; + CHAR16 *LastMatch; + CHAR16 *FileName; + UINTN Length; + + ASSERT(DevicePath != NULL); + + String = DevicePathToStr(DevicePath); + MatchString = String; + LastMatch = String; + FileName = NULL; + + while(MatchString != NULL){ + LastMatch = MatchString + 1; + MatchString = StrStr(LastMatch,L"\\"); + } + + Length = StrLen(LastMatch); + FileName = AllocateCopyPool ((Length + 1) * sizeof(CHAR16), LastMatch); + if (FileName != NULL) { + *(FileName + Length) = 0; + } + + FreePool(String); + + return FileName; +} + +/** + Enroll a new X509 certificate into Variable. + + @param[in] PrivateData The module's private data. + @param[in] VariableName Variable name of CA database. + + @retval EFI_SUCCESS New X509 is enrolled successfully. + @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources. + +**/ +EFI_STATUS +EnrollX509toVariable ( + IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private, + IN CHAR16 *VariableName + ) +{ + EFI_STATUS Status; + UINTN X509DataSize; + VOID *X509Data; + EFI_SIGNATURE_LIST *CACert; + EFI_SIGNATURE_DATA *CACertData; + VOID *Data; + VOID *CurrentData; + UINTN DataSize; + UINTN SigDataSize; + UINT32 Attr; + + X509DataSize = 0; + SigDataSize = 0; + DataSize = 0; + X509Data = NULL; + CACert = NULL; + CACertData = NULL; + Data = NULL; + CurrentData = NULL; + Attr = 0; + + Status = ReadFileContent ( + Private->FileContext->FHandle, + &X509Data, + &X509DataSize, + 0 + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + ASSERT (X509Data != NULL); + + SigDataSize = sizeof(EFI_SIGNATURE_LIST) + sizeof(EFI_SIGNATURE_DATA) - 1 + X509DataSize; + + Data = AllocateZeroPool (SigDataSize); + if (Data == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Fill Certificate Database parameters. + // + CACert = (EFI_SIGNATURE_LIST*) Data; + CACert->SignatureListSize = (UINT32) SigDataSize; + CACert->SignatureHeaderSize = 0; + CACert->SignatureSize = (UINT32) (sizeof(EFI_SIGNATURE_DATA) - 1 + X509DataSize); + CopyGuid (&CACert->SignatureType, &gEfiCertX509Guid); + + CACertData = (EFI_SIGNATURE_DATA*) ((UINT8* ) CACert + sizeof (EFI_SIGNATURE_LIST)); + CopyGuid (&CACertData->SignatureOwner, Private->CertGuid); + CopyMem ((UINT8* ) (CACertData->SignatureData), X509Data, X509DataSize); + + // + // Check if the signature database entry already exists. If it does, use the + // EFI_VARIABLE_APPEND_WRITE attribute to append the new signature data to + // the original variable, plus preserve the original variable attributes. + // + Status = gRT->GetVariable( + VariableName, + &gEfiTlsCaCertificateGuid, + NULL, + &DataSize, + NULL + ); + if (Status == EFI_BUFFER_TOO_SMALL) { + // + // Per spec, we have to fetch the variable's contents, even though we're + // only interested in the variable's attributes. + // + CurrentData = AllocatePool (DataSize); + if (CurrentData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + Status = gRT->GetVariable( + VariableName, + &gEfiTlsCaCertificateGuid, + &Attr, + &DataSize, + CurrentData + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + Attr |= EFI_VARIABLE_APPEND_WRITE; + } else if (Status == EFI_NOT_FOUND) { + Attr = TLS_AUTH_CONFIG_VAR_BASE_ATTR; + } else { + goto ON_EXIT; + } + + Status = gRT->SetVariable( + VariableName, + &gEfiTlsCaCertificateGuid, + Attr, + SigDataSize, + Data + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + +ON_EXIT: + CleanFileContext (Private); + + if (Private->CertGuid != NULL) { + FreePool (Private->CertGuid); + Private->CertGuid = NULL; + } + + if (Data != NULL) { + FreePool (Data); + } + + if (CurrentData != NULL) { + FreePool (CurrentData); + } + + if (X509Data != NULL) { + FreePool (X509Data); + } + + return Status; +} + +/** + Enroll Cert into TlsCaCertificate. The GUID will be Private->CertGuid. + + @param[in] PrivateData The module's private data. + @param[in] VariableName Variable name of signature database. + + @retval EFI_SUCCESS New Cert enrolled successfully. + @retval EFI_INVALID_PARAMETER The parameter is invalid. + @retval EFI_UNSUPPORTED The Cert file is unsupported type. + @retval others Fail to enroll Cert data. + +**/ +EFI_STATUS +EnrollCertDatabase ( + IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private, + IN CHAR16 *VariableName + ) +{ + UINT16* FilePostFix; + UINTN NameLength; + + if ((Private->FileContext->FileName == NULL) || (Private->FileContext->FHandle == NULL) || (Private->CertGuid == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Parse the file's postfix. + // + NameLength = StrLen (Private->FileContext->FileName); + if (NameLength <= 4) { + return EFI_INVALID_PARAMETER; + } + FilePostFix = Private->FileContext->FileName + NameLength - 4; + + if (IsDerPemEncodeCertificate (FilePostFix)) { + // + // Supports DER-encoded X509 certificate. + // + return EnrollX509toVariable (Private, VariableName); + } + + return EFI_UNSUPPORTED; +} + +/** + Refresh the global UpdateData structure. + +**/ +VOID +RefreshUpdateData ( + VOID + ) +{ + // + // Free current updated date + // + if (mStartOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (mStartOpCodeHandle); + } + + // + // Create new OpCode Handle + // + mStartOpCodeHandle = HiiAllocateOpCodeHandle (); + + // + // Create Hii Extend Label OpCode as the start opcode + // + mStartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + mStartOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + mStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; +} + +/** + Clean up the dynamic opcode at label and form specified by both LabelId. + + @param[in] LabelId It is both the Form ID and Label ID for opcode deletion. + @param[in] PrivateData Module private data. + +**/ +VOID +CleanUpPage ( + IN UINT16 LabelId, + IN TLS_AUTH_CONFIG_PRIVATE_DATA *PrivateData + ) +{ + RefreshUpdateData (); + + // + // Remove all op-codes from dynamic page + // + mStartLabel->Number = LabelId; + HiiUpdateForm ( + PrivateData->RegisteredHandle, + &gTlsAuthConfigGuid, + LabelId, + mStartOpCodeHandle, // Label LabelId + mEndOpCodeHandle // LABEL_END + ); +} + +/** + Update the form base on the selected file. + + @param FilePath Point to the file path. + @param FormId The form need to display. + + @retval TRUE Exit caller function. + @retval FALSE Not exit caller function. + +**/ +BOOLEAN +UpdatePage( + IN EFI_DEVICE_PATH_PROTOCOL *FilePath, + IN EFI_FORM_ID FormId + ) +{ + CHAR16 *FileName; + EFI_STRING_ID StringToken; + + FileName = NULL; + + if (FilePath != NULL) { + FileName = ExtractFileNameFromDevicePath(FilePath); + } + if (FileName == NULL) { + // + // FileName = NULL has two case: + // 1. FilePath == NULL, not select file. + // 2. FilePath != NULL, but ExtractFileNameFromDevicePath return NULL not enough memory resource. + // In these two case, no need to update the form, and exit the caller function. + // + return TRUE; + } + StringToken = HiiSetString (mTlsAuthPrivateData->RegisteredHandle, 0, FileName, NULL); + + mTlsAuthPrivateData->FileContext->FileName = FileName; + + EfiOpenFileByDevicePath ( + &FilePath, + &mTlsAuthPrivateData->FileContext->FHandle, + EFI_FILE_MODE_READ, + 0 + ); + // + // Create Subtitle op-code for the display string of the option. + // + RefreshUpdateData (); + mStartLabel->Number = FormId; + + HiiCreateSubTitleOpCode ( + mStartOpCodeHandle, + StringToken, + 0, + 0, + 0 + ); + + HiiUpdateForm ( + mTlsAuthPrivateData->RegisteredHandle, + &gTlsAuthConfigGuid, + FormId, + mStartOpCodeHandle, /// Label FormId + mEndOpCodeHandle /// LABEL_END + ); + + return TRUE; +} + +/** + Update the form base on the input file path info. + + @param FilePath Point to the file path. + + @retval TRUE Exit caller function. + @retval FALSE Not exit caller function. +**/ +BOOLEAN +EFIAPI +UpdateCAFromFile ( + IN EFI_DEVICE_PATH_PROTOCOL *FilePath + ) +{ + return UpdatePage(FilePath, TLS_AUTH_CONFIG_FORMID4_FORM); +} + +/** + Unload the configuration form, this includes: delete all the configuration + entries, uninstall the form callback protocol, and free the resources used. + + @param[in] Private Pointer to the driver private data. + + @retval EFI_SUCCESS The configuration form is unloaded. + @retval Others Failed to unload the form. + +**/ +EFI_STATUS +TlsAuthConfigFormUnload ( + IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private + ) +{ + if (Private->DriverHandle != NULL) { + // + // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL + // + gBS->UninstallMultipleProtocolInterfaces ( + Private->DriverHandle, + &gEfiDevicePathProtocolGuid, + &mTlsAuthConfigHiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &Private->ConfigAccess, + NULL + ); + Private->DriverHandle = NULL; + } + + if (Private->RegisteredHandle != NULL) { + // + // Remove HII package list + // + HiiRemovePackages (Private->RegisteredHandle); + Private->RegisteredHandle = NULL; + } + + if (Private->CertGuid != NULL) { + FreePool (Private->CertGuid); + } + + if (Private->FileContext != NULL) { + FreePool (Private->FileContext); + } + + FreePool (Private); + + if (mStartOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (mStartOpCodeHandle); + } + + if (mEndOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (mEndOpCodeHandle); + } + + return EFI_SUCCESS; +} + + +/** + Initialize the configuration form. + + @param[in] Private Pointer to the driver private data. + + @retval EFI_SUCCESS The configuration form is initialized. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +TlsAuthConfigFormInit ( + IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private + ) +{ + EFI_STATUS Status; + + Private->Signature = TLS_AUTH_CONFIG_PRIVATE_DATA_SIGNATURE; + + Private->ConfigAccess.ExtractConfig = TlsAuthConfigAccessExtractConfig; + Private->ConfigAccess.RouteConfig = TlsAuthConfigAccessRouteConfig; + Private->ConfigAccess.Callback = TlsAuthConfigAccessCallback; + + // + // Install Device Path Protocol and Config Access protocol to driver handle. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &Private->DriverHandle, + &gEfiDevicePathProtocolGuid, + &mTlsAuthConfigHiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &Private->ConfigAccess, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Publish our HII data. + // + Private->RegisteredHandle = HiiAddPackages ( + &gTlsAuthConfigGuid, + Private->DriverHandle, + TlsAuthConfigDxeStrings, + TlsAuthConfigVfrBin, + NULL + ); + if (Private->RegisteredHandle == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + Private->FileContext = AllocateZeroPool (sizeof (TLS_AUTH_CONFIG_FILE_CONTEXT)); + if (Private->FileContext == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + // + // Init OpCode Handle and Allocate space for creation of Buffer + // + mStartOpCodeHandle = HiiAllocateOpCodeHandle (); + if (mStartOpCodeHandle == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + mEndOpCodeHandle = HiiAllocateOpCodeHandle (); + if (mEndOpCodeHandle == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Error; + } + + // + // Create Hii Extend Label OpCode as the start opcode + // + mStartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + mStartOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + mStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + + // + // Create Hii Extend Label OpCode as the end opcode + // + mEndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + mEndOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + mEndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + mEndLabel->Number = LABEL_END; + + return EFI_SUCCESS; + +Error: + TlsAuthConfigFormUnload (Private); + return Status; +} + +/** + + This function allows the caller to request the current + configuration for one or more named elements. The resulting + string is in format. Any and all alternative + configuration strings shall also be appended to the end of the + current configuration string. If they are, they must appear + after the current configuration. They must contain the same + routing (GUID, NAME, PATH) as the current configuration string. + They must have an additional description indicating the type of + alternative configuration the string represents, + "ALTCFG=". That (when + converted from Hex UNICODE to binary) is a reference to a + string in the associated string pack. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param Request A null-terminated Unicode string in + format. Note that this + includes the routing information as well as + the configurable name / value pairs. It is + invalid for this string to be in + format. + If a NULL is passed in for the Request field, + all of the settings being abstracted by this function + will be returned in the Results field. In addition, + if a ConfigHdr is passed in with no request elements, + all of the settings being abstracted for that particular + ConfigHdr reference will be returned in the Results Field. + + @param Progress On return, points to a character in the + Request string. Points to the string's null + terminator if request was successful. Points + to the most recent "&" before the first + failing name / value pair (or the beginning + of the string if the failure is in the first + name / value pair) if the request was not + successful. + + @param Results A null-terminated Unicode string in + format which has all values + filled in for the names in the Request string. + String to be allocated by the called function. + + @retval EFI_SUCCESS The Results string is filled with the + values corresponding to all requested + names. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_NOT_FOUND Routing data doesn't match any + known driver. Progress set to the + first character in the routing header. + Note: There is no requirement that the + driver validate the routing data. It + must skip the in order to + process the names. + + @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set + to most recent "&" before the + error or the beginning of the + string. + + @retval EFI_INVALID_PARAMETER Unknown name. Progress points + to the & before the name in + question. + +**/ +EFI_STATUS +EFIAPI +TlsAuthConfigAccessExtractConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Request, + OUT EFI_STRING *Progress, + OUT EFI_STRING *Results + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + UINTN Size; + EFI_STRING ConfigRequest; + EFI_STRING ConfigRequestHdr; + TLS_AUTH_CONFIG_PRIVATE_DATA *Private; + BOOLEAN AllocatedRequest; + + if (Progress == NULL || Results == NULL) { + return EFI_INVALID_PARAMETER; + } + + AllocatedRequest = FALSE; + ConfigRequestHdr = NULL; + ConfigRequest = NULL; + Size = 0; + + Private = TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This); + + BufferSize = sizeof (TLS_AUTH_CONFIG_IFR_NVDATA); + ZeroMem (&Private->TlsAuthConfigNvData, BufferSize); + + *Progress = Request; + + if ((Request != NULL) && !HiiIsConfigHdrMatch (Request, &gTlsAuthConfigGuid, mTlsAuthConfigStorageName)) { + return EFI_NOT_FOUND; + } + + ConfigRequest = Request; + if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) { + // + // Request is set to NULL or OFFSET is NULL, construct full request string. + // + // Allocate and fill a buffer large enough to hold the template + // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator + // + ConfigRequestHdr = HiiConstructConfigHdr (&gTlsAuthConfigGuid, mTlsAuthConfigStorageName, Private->DriverHandle); + Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16); + ConfigRequest = AllocateZeroPool (Size); + ASSERT (ConfigRequest != NULL); + AllocatedRequest = TRUE; + UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize); + FreePool (ConfigRequestHdr); + ConfigRequestHdr = NULL; + } + + Status = gHiiConfigRouting->BlockToConfig ( + gHiiConfigRouting, + ConfigRequest, + (UINT8 *) &Private->TlsAuthConfigNvData, + BufferSize, + Results, + Progress + ); + + // + // Free the allocated config request string. + // + if (AllocatedRequest) { + FreePool (ConfigRequest); + } + + // + // Set Progress string to the original request string. + // + if (Request == NULL) { + *Progress = NULL; + } else if (StrStr (Request, L"OFFSET") == NULL) { + *Progress = Request + StrLen (Request); + } + + return Status; +} + +/** + + This function applies changes in a driver's configuration. + Input is a Configuration, which has the routing data for this + driver followed by name / value configuration pairs. The driver + must apply those pairs to its configurable storage. If the + driver's configuration is stored in a linear block of data + and the driver's name / value pairs are in + format, it may use the ConfigToBlock helper function (above) to + simplify the job. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param Configuration A null-terminated Unicode string in + format. + + @param Progress A pointer to a string filled in with the + offset of the most recent '&' before the + first failing name / value pair (or the + beginn ing of the string if the failure + is in the first name / value pair) or + the terminating NULL if all was + successful. + + @retval EFI_SUCCESS The results have been distributed or are + awaiting distribution. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_INVALID_PARAMETERS Passing in a NULL for the + Results parameter would result + in this type of error. + + @retval EFI_NOT_FOUND Target for the specified routing data + was not found + +**/ +EFI_STATUS +EFIAPI +TlsAuthConfigAccessRouteConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Configuration, + OUT EFI_STRING *Progress + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + TLS_AUTH_CONFIG_PRIVATE_DATA *Private; + + if (Progress == NULL) { + return EFI_INVALID_PARAMETER; + } + *Progress = Configuration; + + if (Configuration == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Check routing data in . + // Note: there is no name for Name/Value storage, only GUID will be checked + // + if (!HiiIsConfigHdrMatch (Configuration, &gTlsAuthConfigGuid, mTlsAuthConfigStorageName)) { + return EFI_NOT_FOUND; + } + + Private = TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This); + + BufferSize = sizeof (TLS_AUTH_CONFIG_IFR_NVDATA); + ZeroMem (&Private->TlsAuthConfigNvData, BufferSize); + + Status = gHiiConfigRouting->ConfigToBlock ( + gHiiConfigRouting, + Configuration, + (UINT8 *) &Private->TlsAuthConfigNvData, + &BufferSize, + Progress + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return Status; +} + +/** + + This function is called to provide results data to the driver. + This data consists of a unique key that is used to identify + which data is either being passed back or being asked for. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param Action Specifies the type of action taken by the browser. + @param QuestionId A unique value which is sent to the original + exporting driver so that it can identify the type + of data to expect. The format of the data tends to + vary based on the opcode that generated the callback. + @param Type The type of value for the question. + @param Value A pointer to the data being sent to the original + exporting driver. + @param ActionRequest On return, points to the action requested by the + callback function. + + @retval EFI_SUCCESS The callback successfully handled the action. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the + variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be saved. + @retval EFI_UNSUPPORTED The specified Action is not supported by the + callback. +**/ +EFI_STATUS +EFIAPI +TlsAuthConfigAccessCallback ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN EFI_BROWSER_ACTION Action, + IN EFI_QUESTION_ID QuestionId, + IN UINT8 Type, + IN OUT EFI_IFR_TYPE_VALUE *Value, + OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest + ) +{ + EFI_INPUT_KEY Key; + EFI_STATUS Status; + RETURN_STATUS RStatus; + TLS_AUTH_CONFIG_PRIVATE_DATA *Private; + UINTN BufferSize; + TLS_AUTH_CONFIG_IFR_NVDATA *IfrNvData; + UINT16 LabelId; + EFI_DEVICE_PATH_PROTOCOL *File; + + Status = EFI_SUCCESS; + File = NULL; + + if ((This == NULL) || (Value == NULL) || (ActionRequest == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Private = TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This); + + mTlsAuthPrivateData = Private; + + // + // Retrieve uncommitted data from Browser + // + BufferSize = sizeof (TLS_AUTH_CONFIG_IFR_NVDATA); + IfrNvData = AllocateZeroPool (BufferSize); + if (IfrNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + HiiGetBrowserData (&gTlsAuthConfigGuid, mTlsAuthConfigStorageName, BufferSize, (UINT8 *) IfrNvData); + + if ((Action != EFI_BROWSER_ACTION_CHANGED) && + (Action != EFI_BROWSER_ACTION_CHANGING) && + (Action != EFI_BROWSER_ACTION_FORM_CLOSE)) { + Status = EFI_UNSUPPORTED; + goto EXIT; + } + + if (Action == EFI_BROWSER_ACTION_CHANGING) { + switch (QuestionId) { + case KEY_TLS_AUTH_CONFIG_CLIENT_CERT: + case KEY_TLS_AUTH_CONFIG_SERVER_CA: + // + // Clear Cert GUID. + // + ZeroMem (IfrNvData->CertGuid, sizeof (IfrNvData->CertGuid)); + if (Private->CertGuid == NULL) { + Private->CertGuid = (EFI_GUID *) AllocateZeroPool (sizeof (EFI_GUID)); + if (Private->CertGuid == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + if (QuestionId == KEY_TLS_AUTH_CONFIG_CLIENT_CERT) { + LabelId = TLS_AUTH_CONFIG_FORMID3_FORM; + } else { + LabelId = TLS_AUTH_CONFIG_FORMID4_FORM; + } + + // + // Refresh selected file. + // + CleanUpPage (LabelId, Private); + break; + case KEY_TLS_AUTH_CONFIG_ENROLL_CERT_FROM_FILE: + // + // If the file is already opened, clean the file related resource first. + // + CleanFileContext (Private); + + ChooseFile( NULL, NULL, UpdateCAFromFile, &File); + break; + + case KEY_TLS_AUTH_CONFIG_VALUE_SAVE_AND_EXIT: + Status = EnrollCertDatabase (Private, EFI_TLS_CA_CERTIFICATE_VARIABLE); + if (EFI_ERROR (Status)) { + CleanFileContext (Private); + + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"ERROR: Enroll Cert Failure!", + NULL + ); + } + break; + + case KEY_TLS_AUTH_CONFIG_VALUE_NO_SAVE_AND_EXIT: + CleanFileContext (Private); + + if (Private->CertGuid!= NULL) { + FreePool (Private->CertGuid); + Private->CertGuid = NULL; + } + break; + + case KEY_TLS_AUTH_CONFIG_DELETE_CERT: + UpdateDeletePage ( + Private, + EFI_TLS_CA_CERTIFICATE_VARIABLE, + &gEfiTlsCaCertificateGuid, + LABEL_CA_DELETE, + TLS_AUTH_CONFIG_FORMID5_FORM, + OPTION_DEL_CA_ESTION_ID + ); + break; + + default: + if ((QuestionId >= OPTION_DEL_CA_ESTION_ID) && + (QuestionId < (OPTION_DEL_CA_ESTION_ID + OPTION_CONFIG_RANGE))) { + DeleteCert ( + Private, + EFI_TLS_CA_CERTIFICATE_VARIABLE, + &gEfiTlsCaCertificateGuid, + LABEL_CA_DELETE, + TLS_AUTH_CONFIG_FORMID5_FORM, + OPTION_DEL_CA_ESTION_ID, + QuestionId - OPTION_DEL_CA_ESTION_ID + ); + } + break; + } + } else if (Action == EFI_BROWSER_ACTION_CHANGED) { + switch (QuestionId) { + case KEY_TLS_AUTH_CONFIG_CERT_GUID: + ASSERT (Private->CertGuid != NULL); + RStatus = StrToGuid ( + IfrNvData->CertGuid, + Private->CertGuid + ); + if (RETURN_ERROR (RStatus) || (IfrNvData->CertGuid[GUID_STRING_LENGTH] != L'\0')) { + Status = EFI_INVALID_PARAMETER; + break; + } + + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY; + break; + default: + break; + } + } else if (Action == EFI_BROWSER_ACTION_FORM_CLOSE) { + CleanFileContext (Private); + } + +EXIT: + + if (!EFI_ERROR (Status)) { + BufferSize = sizeof (TLS_AUTH_CONFIG_IFR_NVDATA); + HiiSetBrowserData (&gTlsAuthConfigGuid, mTlsAuthConfigStorageName, BufferSize, (UINT8*) IfrNvData, NULL); + } + + FreePool (IfrNvData); + + if (File != NULL){ + FreePool(File); + File = NULL; + } + + return EFI_SUCCESS; + +} + diff --git a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.h b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.h new file mode 100644 index 000000000..89d02e39a --- /dev/null +++ b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.h @@ -0,0 +1,276 @@ +/** @file + Header file of Miscellaneous Routines for TlsAuthConfigDxe driver. + +Copyright (c) 2016, Intel Corporation. All rights reserved.
+ +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __TLS_AUTH_CONFIG_IMPL_H__ +#define __TLS_AUTH_CONFIG_IMPL_H__ + +#include + +#include +#include + +// +// Libraries +// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + +// +// Include files with function prototypes +// +#include "TlsAuthConfigNvData.h" + +extern UINT8 TlsAuthConfigDxeStrings[]; +extern UINT8 TlsAuthConfigVfrBin[]; + +#define TLS_AUTH_CONFIG_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('T', 'A', 'C', 'D') +#define TLS_AUTH_CONFIG_PRIVATE_FROM_THIS(a) CR (a, TLS_AUTH_CONFIG_PRIVATE_DATA, ConfigAccess, TLS_AUTH_CONFIG_PRIVATE_DATA_SIGNATURE) + +#define TLS_AUTH_CONFIG_VAR_BASE_ATTR (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS) + +typedef struct _TLS_AUTH_CONFIG_PRIVATE_DATA TLS_AUTH_CONFIG_PRIVATE_DATA; +typedef struct _TLS_AUTH_CONFIG_FILE_CONTEXT TLS_AUTH_CONFIG_FILE_CONTEXT; + +/// +/// HII specific Vendor Device Path definition. +/// +typedef struct { + VENDOR_DEVICE_PATH VendorDevicePath; + EFI_DEVICE_PATH_PROTOCOL End; +} HII_VENDOR_DEVICE_PATH; + +struct _TLS_AUTH_CONFIG_FILE_CONTEXT { + EFI_FILE_HANDLE FHandle; + UINT16 *FileName; +}; + +struct _TLS_AUTH_CONFIG_PRIVATE_DATA { + UINTN Signature; + + EFI_HANDLE DriverHandle; + EFI_HII_HANDLE RegisteredHandle; + EFI_HII_CONFIG_ACCESS_PROTOCOL ConfigAccess; + TLS_AUTH_CONFIG_IFR_NVDATA TlsAuthConfigNvData; + + TLS_AUTH_CONFIG_FILE_CONTEXT *FileContext; + + EFI_GUID *CertGuid; +}; + +/** + Unload the configuration form, this includes: delete all the configuration + entries, uninstall the form callback protocol, and free the resources used. + The form will only be unload completely when both IP4 and IP6 stack are stopped. + + @param[in] Private Pointer to the driver private data. + + @retval EFI_SUCCESS The configuration form is unloaded. + @retval Others Failed to unload the form. + +**/ +EFI_STATUS +TlsAuthConfigFormUnload ( + IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private + ); + +/** + Initialize the configuration form. + + @param[in] Private Pointer to the driver private data. + + @retval EFI_SUCCESS The configuration form is initialized. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + +**/ +EFI_STATUS +TlsAuthConfigFormInit ( + IN TLS_AUTH_CONFIG_PRIVATE_DATA *Private + ); + +/** + + This function allows the caller to request the current + configuration for one or more named elements. The resulting + string is in format. Any and all alternative + configuration strings shall also be appended to the end of the + current configuration string. If they are, they must appear + after the current configuration. They must contain the same + routing (GUID, NAME, PATH) as the current configuration string. + They must have an additional description indicating the type of + alternative configuration the string represents, + "ALTCFG=". That (when + converted from Hex UNICODE to binary) is a reference to a + string in the associated string pack. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param Request A null-terminated Unicode string in + format. Note that this + includes the routing information as well as + the configurable name / value pairs. It is + invalid for this string to be in + format. + If a NULL is passed in for the Request field, + all of the settings being abstracted by this function + will be returned in the Results field. In addition, + if a ConfigHdr is passed in with no request elements, + all of the settings being abstracted for that particular + ConfigHdr reference will be returned in the Results Field. + + @param Progress On return, points to a character in the + Request string. Points to the string's null + terminator if request was successful. Points + to the most recent "&" before the first + failing name / value pair (or the beginning + of the string if the failure is in the first + name / value pair) if the request was not + successful. + + @param Results A null-terminated Unicode string in + format which has all values + filled in for the names in the Request string. + String to be allocated by the called function. + + @retval EFI_SUCCESS The Results string is filled with the + values corresponding to all requested + names. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_NOT_FOUND Routing data doesn't match any + known driver. Progress set to the + first character in the routing header. + Note: There is no requirement that the + driver validate the routing data. It + must skip the in order to + process the names. + + @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set + to most recent "&" before the + error or the beginning of the + string. + + @retval EFI_INVALID_PARAMETER Unknown name. Progress points + to the & before the name in + question. + +**/ +EFI_STATUS +EFIAPI +TlsAuthConfigAccessExtractConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Request, + OUT EFI_STRING *Progress, + OUT EFI_STRING *Results + ); + +/** + + This function applies changes in a driver's configuration. + Input is a Configuration, which has the routing data for this + driver followed by name / value configuration pairs. The driver + must apply those pairs to its configurable storage. If the + driver's configuration is stored in a linear block of data + and the driver's name / value pairs are in + format, it may use the ConfigToBlock helper function (above) to + simplify the job. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param Configuration A null-terminated Unicode string in + format. + + @param Progress A pointer to a string filled in with the + offset of the most recent '&' before the + first failing name / value pair (or the + beginn ing of the string if the failure + is in the first name / value pair) or + the terminating NULL if all was + successful. + + @retval EFI_SUCCESS The results have been distributed or are + awaiting distribution. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_INVALID_PARAMETERS Passing in a NULL for the + Results parameter would result + in this type of error. + + @retval EFI_NOT_FOUND Target for the specified routing data + was not found + +**/ +EFI_STATUS +EFIAPI +TlsAuthConfigAccessRouteConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Configuration, + OUT EFI_STRING *Progress + ); + +/** + + This function is called to provide results data to the driver. + This data consists of a unique key that is used to identify + which data is either being passed back or being asked for. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param Action Specifies the type of action taken by the browser. + @param QuestionId A unique value which is sent to the original + exporting driver so that it can identify the type + of data to expect. The format of the data tends to + vary based on the opcode that generated the callback. + @param Type The type of value for the question. + @param Value A pointer to the data being sent to the original + exporting driver. + @param ActionRequest On return, points to the action requested by the + callback function. + + @retval EFI_SUCCESS The callback successfully handled the action. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the + variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be saved. + @retval EFI_UNSUPPORTED The specified Action is not supported by the + callback. +**/ +EFI_STATUS +EFIAPI +TlsAuthConfigAccessCallback ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN EFI_BROWSER_ACTION Action, + IN EFI_QUESTION_ID QuestionId, + IN UINT8 Type, + IN OUT EFI_IFR_TYPE_VALUE *Value, + OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest + ); + +#endif + diff --git a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigNvData.h b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigNvData.h new file mode 100644 index 000000000..13813873e --- /dev/null +++ b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigNvData.h @@ -0,0 +1,44 @@ +/** @file + Header file for NV data structure definition. + +Copyright (c) 2016, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __TLS_AUTH_CONFIG_NV_DATA_H__ +#define __TLS_AUTH_CONFIG_NV_DATA_H__ + +#include + +#define TLS_AUTH_CONFIG_GUID_SIZE 36 +#define TLS_AUTH_CONFIG_GUID_STORAGE_SIZE 37 + +#define TLS_AUTH_CONFIG_FORMID1_FORM 1 +#define TLS_AUTH_CONFIG_FORMID2_FORM 2 +#define TLS_AUTH_CONFIG_FORMID3_FORM 3 +#define TLS_AUTH_CONFIG_FORMID4_FORM 4 +#define TLS_AUTH_CONFIG_FORMID5_FORM 5 + + +#define KEY_TLS_AUTH_CONFIG_SERVER_CA 0x1000 +#define KEY_TLS_AUTH_CONFIG_CLIENT_CERT 0x1001 +#define KEY_TLS_AUTH_CONFIG_ENROLL_CERT 0x1002 +#define KEY_TLS_AUTH_CONFIG_DELETE_CERT 0x1003 +#define KEY_TLS_AUTH_CONFIG_ENROLL_CERT_FROM_FILE 0x1004 +#define KEY_TLS_AUTH_CONFIG_CERT_GUID 0x1005 +#define KEY_TLS_AUTH_CONFIG_VALUE_SAVE_AND_EXIT 0x1006 +#define KEY_TLS_AUTH_CONFIG_VALUE_NO_SAVE_AND_EXIT 0x1007 + +#define OPTION_DEL_CA_ESTION_ID 0x2000 +#define OPTION_CONFIG_RANGE 0x1000 + +#define LABEL_CA_DELETE 0x1101 +#define LABEL_END 0xffff + +typedef struct { + CHAR16 CertGuid[TLS_AUTH_CONFIG_GUID_STORAGE_SIZE]; +} TLS_AUTH_CONFIG_IFR_NVDATA; + +#endif + diff --git a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigVfr.vfr b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigVfr.vfr new file mode 100644 index 000000000..c622489af --- /dev/null +++ b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigVfr.vfr @@ -0,0 +1,147 @@ +/** @file + VFR file used by TlsAuthConfigDxe driver. + + Copyright (c) 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TlsAuthConfigNvData.h" + +formset + guid = TLS_AUTH_CONFIG_GUID, + title = STRING_TOKEN(STR_TLS_AUTH_CONFIG_TITLE), + help = STRING_TOKEN(STR_TLS_AUTH_CONFIG_HELP), + + varstore TLS_AUTH_CONFIG_IFR_NVDATA, + name = TLS_AUTH_CONFIG_IFR_NVDATA, + guid = TLS_AUTH_CONFIG_GUID; + + // + // ##1 Form1: Main form for Tls Auth configration + // + form formid = TLS_AUTH_CONFIG_FORMID1_FORM, + title = STRING_TOKEN(STR_TLS_AUTH_CONFIG_TITLE); + + subtitle text = STRING_TOKEN(STR_NULL); + + // + // Display Server CA configration + // + goto TLS_AUTH_CONFIG_FORMID2_FORM, + prompt = STRING_TOKEN(STR_TLS_AUTH_CONFIG_SERVER_CA), + help = STRING_TOKEN(STR_TLS_AUTH_CONFIG_SERVER_CA_HELP), + flags = INTERACTIVE, + key = KEY_TLS_AUTH_CONFIG_SERVER_CA; + + subtitle text = STRING_TOKEN(STR_NULL); + + // + // Display Client cert configration + // + grayoutif TRUE; /// Current unsupported. + goto TLS_AUTH_CONFIG_FORMID3_FORM, + prompt = STRING_TOKEN(STR_TLS_AUTH_CONFIG_CLIENT_CERT), + help = STRING_TOKEN(STR_TLS_AUTH_CONFIG_CLIENT_CERT_HELP), + flags = INTERACTIVE, + key = KEY_TLS_AUTH_CONFIG_CLIENT_CERT; + endif; + endform; + + // + // ##2 Form2: CA configuration + // + form formid = TLS_AUTH_CONFIG_FORMID2_FORM, + title = STRING_TOKEN(STR_TLS_AUTH_CONFIG_SERVER_CA); + + subtitle text = STRING_TOKEN(STR_NULL); + + goto TLS_AUTH_CONFIG_FORMID4_FORM, + prompt = STRING_TOKEN(STR_TLS_AUTH_CONFIG_ENROLL_CERT), + help = STRING_TOKEN(STR_TLS_AUTH_CONFIG_ENROLL_CERT_HELP), + flags = INTERACTIVE, + key = KEY_TLS_AUTH_CONFIG_ENROLL_CERT; + + subtitle text = STRING_TOKEN(STR_NULL); + + goto TLS_AUTH_CONFIG_FORMID5_FORM, + prompt = STRING_TOKEN(STR_TLS_AUTH_CONFIG_DELETE_CERT), + help = STRING_TOKEN(STR_TLS_AUTH_CONFIG_DELETE_CERT_HELP), + flags = INTERACTIVE, + key = KEY_TLS_AUTH_CONFIG_DELETE_CERT; + endform; + + // + // ##3 Form3 : Client cert configuration + // + form formid = TLS_AUTH_CONFIG_FORMID3_FORM, + title = STRING_TOKEN(STR_TLS_AUTH_CONFIG_CLIENT_CERT); + + subtitle text = STRING_TOKEN(STR_NULL); + + // + // TODO... + // + endform; + + // + // ##4 Form4: Enroll cert for CA + // + form formid = TLS_AUTH_CONFIG_FORMID4_FORM, + title = STRING_TOKEN(STR_TLS_AUTH_CONFIG_ENROLL_CERT); + + subtitle text = STRING_TOKEN(STR_NULL); + + goto TLS_AUTH_CONFIG_FORMID4_FORM, + prompt = STRING_TOKEN(STR_TLS_AUTH_CONFIG_ADD_CERT_FILE), + help = STRING_TOKEN(STR_TLS_AUTH_CONFIG_ADD_CERT_FILE), + flags = INTERACTIVE, + key = KEY_TLS_AUTH_CONFIG_ENROLL_CERT_FROM_FILE; + + subtitle text = STRING_TOKEN(STR_NULL); + label TLS_AUTH_CONFIG_FORMID4_FORM; + label LABEL_END; + subtitle text = STRING_TOKEN(STR_NULL); + + string varid = TLS_AUTH_CONFIG_IFR_NVDATA.CertGuid, + prompt = STRING_TOKEN(STR_TLS_AUTH_CONFIG_CERT_GUID), + help = STRING_TOKEN(STR_TLS_AUTH_CONFIG_CERT_GUID_HELP), + flags = INTERACTIVE, + key = KEY_TLS_AUTH_CONFIG_CERT_GUID, + minsize = TLS_AUTH_CONFIG_GUID_SIZE, + maxsize = TLS_AUTH_CONFIG_GUID_SIZE, + endstring; + + subtitle text = STRING_TOKEN(STR_NULL); + subtitle text = STRING_TOKEN(STR_NULL); + + goto TLS_AUTH_CONFIG_FORMID1_FORM, + prompt = STRING_TOKEN(STR_TLS_AUTH_CONFIG_SAVE_AND_EXIT), + help = STRING_TOKEN(STR_TLS_AUTH_CONFIG_SAVE_AND_EXIT), + flags = INTERACTIVE, + key = KEY_TLS_AUTH_CONFIG_VALUE_SAVE_AND_EXIT; + + goto TLS_AUTH_CONFIG_FORMID1_FORM, + prompt = STRING_TOKEN(STR_TLS_AUTH_CONFIG_NO_SAVE_AND_EXIT), + help = STRING_TOKEN(STR_TLS_AUTH_CONFIG_NO_SAVE_AND_EXIT), + flags = INTERACTIVE, + key = KEY_TLS_AUTH_CONFIG_VALUE_NO_SAVE_AND_EXIT; + + endform; + + // + // ##5 Form5: Delete cert for CA + // + form formid = TLS_AUTH_CONFIG_FORMID5_FORM, + title = STRING_TOKEN(STR_TLS_AUTH_CONFIG_DELETE_CERT); + + label LABEL_CA_DELETE; + label LABEL_END; + + subtitle text = STRING_TOKEN(STR_NULL); + + endform; + +endformset; + diff --git a/NetworkPkg/TlsDxe/TlsConfigProtocol.c b/NetworkPkg/TlsDxe/TlsConfigProtocol.c new file mode 100644 index 000000000..b01e4cdee --- /dev/null +++ b/NetworkPkg/TlsDxe/TlsConfigProtocol.c @@ -0,0 +1,147 @@ +/** @file + Implementation of EFI TLS Configuration Protocol Interfaces. + + Copyright (c) 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TlsImpl.h" + +EFI_TLS_CONFIGURATION_PROTOCOL mTlsConfigurationProtocol = { + TlsConfigurationSetData, + TlsConfigurationGetData +}; + +/** + Set TLS configuration data. + + The SetData() function sets TLS configuration to non-volatile storage or volatile + storage. + + @param[in] This Pointer to the EFI_TLS_CONFIGURATION_PROTOCOL instance. + @param[in] DataType Configuration data type. + @param[in] Data Pointer to configuration data. + @param[in] DataSize Total size of configuration data. + + @retval EFI_SUCCESS The TLS configuration data is set successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Data is NULL. + DataSize is 0. + @retval EFI_UNSUPPORTED The DataType is unsupported. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. + +**/ +EFI_STATUS +EFIAPI +TlsConfigurationSetData ( + IN EFI_TLS_CONFIGURATION_PROTOCOL *This, + IN EFI_TLS_CONFIG_DATA_TYPE DataType, + IN VOID *Data, + IN UINTN DataSize + ) +{ + EFI_STATUS Status; + TLS_INSTANCE *Instance; + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + + if (This == NULL || Data == NULL || DataSize == 0) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = TLS_INSTANCE_FROM_CONFIGURATION (This); + + switch (DataType) { + case EfiTlsConfigDataTypeCACertificate: + Status = TlsSetCaCertificate (Instance->TlsConn, Data, DataSize); + break; + case EfiTlsConfigDataTypeHostPublicCert: + Status = TlsSetHostPublicCert (Instance->TlsConn, Data, DataSize); + break; + case EfiTlsConfigDataTypeHostPrivateKey: + Status = TlsSetHostPrivateKey (Instance->TlsConn, Data, DataSize); + break; + case EfiTlsConfigDataTypeCertRevocationList: + Status = TlsSetCertRevocationList (Data, DataSize); + break; + default: + Status = EFI_UNSUPPORTED; + } + + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Get TLS configuration data. + + The GetData() function gets TLS configuration. + + @param[in] This Pointer to the EFI_TLS_CONFIGURATION_PROTOCOL instance. + @param[in] DataType Configuration data type. + @param[in, out] Data Pointer to configuration data. + @param[in, out] DataSize Total size of configuration data. On input, it means + the size of Data buffer. On output, it means the size + of copied Data buffer if EFI_SUCCESS, and means the + size of desired Data buffer if EFI_BUFFER_TOO_SMALL. + + @retval EFI_SUCCESS The TLS configuration data is got successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + DataSize is NULL. + Data is NULL if *DataSize is not zero. + @retval EFI_UNSUPPORTED The DataType is unsupported. + @retval EFI_NOT_FOUND The TLS configuration data is not found. + @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the data. +**/ +EFI_STATUS +EFIAPI +TlsConfigurationGetData ( + IN EFI_TLS_CONFIGURATION_PROTOCOL *This, + IN EFI_TLS_CONFIG_DATA_TYPE DataType, + IN OUT VOID *Data, OPTIONAL + IN OUT UINTN *DataSize + ) +{ + EFI_STATUS Status; + TLS_INSTANCE *Instance; + + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + + if (This == NULL || DataSize == NULL || (Data == NULL && *DataSize != 0)) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = TLS_INSTANCE_FROM_CONFIGURATION (This); + + switch (DataType) { + case EfiTlsConfigDataTypeCACertificate: + Status = TlsGetCaCertificate (Instance->TlsConn, Data, DataSize); + break; + case EfiTlsConfigDataTypeHostPublicCert: + Status = TlsGetHostPublicCert (Instance->TlsConn, Data, DataSize); + break; + case EfiTlsConfigDataTypeHostPrivateKey: + Status = TlsGetHostPrivateKey (Instance->TlsConn, Data, DataSize); + break; + case EfiTlsConfigDataTypeCertRevocationList: + Status = TlsGetCertRevocationList (Data, DataSize); + break; + default: + Status = EFI_UNSUPPORTED; + } + + gBS->RestoreTPL (OldTpl); + return Status; +} + diff --git a/NetworkPkg/TlsDxe/TlsDriver.c b/NetworkPkg/TlsDxe/TlsDriver.c new file mode 100644 index 000000000..60ad39664 --- /dev/null +++ b/NetworkPkg/TlsDxe/TlsDriver.c @@ -0,0 +1,491 @@ +/** @file + The Driver Binding and Service Binding Protocol for TlsDxe driver. + + Copyright (c) 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TlsImpl.h" + +EFI_SERVICE_BINDING_PROTOCOL mTlsServiceBinding = { + TlsServiceBindingCreateChild, + TlsServiceBindingDestroyChild +}; + +/** + Release all the resources used by the TLS instance. + + @param[in] Instance The TLS instance data. + +**/ +VOID +TlsCleanInstance ( + IN TLS_INSTANCE *Instance + ) +{ + if (Instance != NULL) { + if (Instance->TlsConn != NULL) { + TlsFree (Instance->TlsConn); + } + + FreePool (Instance); + } +} + +/** + Create the TLS instance and initialize it. + + @param[in] Service The pointer to the TLS service. + @param[out] Instance The pointer to the TLS instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_SUCCESS The TLS instance is created. + +**/ +EFI_STATUS +TlsCreateInstance ( + IN TLS_SERVICE *Service, + OUT TLS_INSTANCE **Instance + ) +{ + TLS_INSTANCE *TlsInstance; + + *Instance = NULL; + + TlsInstance = AllocateZeroPool (sizeof (TLS_INSTANCE)); + if (TlsInstance == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + TlsInstance->Signature = TLS_INSTANCE_SIGNATURE; + InitializeListHead (&TlsInstance->Link); + TlsInstance->InDestroy = FALSE; + TlsInstance->Service = Service; + + CopyMem (&TlsInstance->Tls, &mTlsProtocol, sizeof (TlsInstance->Tls)); + CopyMem (&TlsInstance->TlsConfig, &mTlsConfigurationProtocol, sizeof (TlsInstance->TlsConfig)); + + TlsInstance->TlsSessionState = EfiTlsSessionNotStarted; + + *Instance = TlsInstance; + + return EFI_SUCCESS; +} + +/** + Release all the resources used by the TLS service binding instance. + + @param[in] Service The TLS service data. + +**/ +VOID +TlsCleanService ( + IN TLS_SERVICE *Service + ) +{ + if (Service != NULL) { + if (Service->TlsCtx != NULL) { + TlsCtxFree (Service->TlsCtx); + } + + FreePool (Service); + } +} + +/** + Create then initialize a TLS service. + + @param[in] Image ImageHandle of the TLS driver + @param[out] Service The service for TLS driver + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to create the service. + @retval EFI_SUCCESS The service is created for the driver. + +**/ +EFI_STATUS +TlsCreateService ( + IN EFI_HANDLE Image, + OUT TLS_SERVICE **Service + ) +{ + TLS_SERVICE *TlsService; + + ASSERT (Service != NULL); + + *Service = NULL; + + // + // Allocate a TLS Service Data + // + TlsService = AllocateZeroPool (sizeof (TLS_SERVICE)); + if (TlsService == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Initialize TLS Service Data + // + TlsService->Signature = TLS_SERVICE_SIGNATURE; + CopyMem (&TlsService->ServiceBinding, &mTlsServiceBinding, sizeof (TlsService->ServiceBinding)); + TlsService->TlsChildrenNum = 0; + InitializeListHead (&TlsService->TlsChildrenList); + TlsService->ImageHandle = Image; + + *Service = TlsService; + + return EFI_SUCCESS; +} + +/** + Unloads an image. + + @param[in] ImageHandle Handle that identifies the image to be unloaded. + + @retval EFI_SUCCESS The image has been unloaded. + @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle. + +**/ +EFI_STATUS +EFIAPI +TlsUnload ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_STATUS Status; + UINTN HandleNum; + EFI_HANDLE *HandleBuffer; + UINT32 Index; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + TLS_SERVICE *TlsService; + + HandleBuffer = NULL; + ServiceBinding = NULL; + TlsService = NULL; + + // + // Locate all the handles with Tls service binding protocol. + // + Status = gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiTlsServiceBindingProtocolGuid, + NULL, + &HandleNum, + &HandleBuffer + ); + if (EFI_ERROR (Status)) { + return Status; + } + + for (Index = 0; Index < HandleNum; Index++) { + // + // Firstly, find ServiceBinding interface + // + Status = gBS->OpenProtocol ( + HandleBuffer[Index], + &gEfiTlsServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + ImageHandle, + NULL, + EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + TlsService = TLS_SERVICE_FROM_THIS (ServiceBinding); + + // + // Then, uninstall ServiceBinding interface + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + HandleBuffer[Index], + &gEfiTlsServiceBindingProtocolGuid, ServiceBinding, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + TlsCleanService (TlsService); + } + + if (HandleBuffer != NULL) { + FreePool (HandleBuffer); + } + + return EFI_SUCCESS; +} + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + @param ImageHandle The firmware allocated handle for the UEFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval Others An unexpected error occurred. +**/ +EFI_STATUS +EFIAPI +TlsDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + TLS_SERVICE *TlsService; + + // + // Create TLS Service + // + Status = TlsCreateService (ImageHandle, &TlsService); + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (TlsService != NULL); + + // + // Initializes the OpenSSL library. + // + TlsInitialize (); + + // + // Create a new SSL_CTX object as framework to establish TLS/SSL enabled + // connections. TLS 1.0 is used as the default version. + // + TlsService->TlsCtx = TlsCtxNew (TLS10_PROTOCOL_VERSION_MAJOR, TLS10_PROTOCOL_VERSION_MINOR); + if (TlsService->TlsCtx == NULL) { + FreePool (TlsService); + return EFI_ABORTED; + } + + // + // Install the TlsServiceBinding Protocol onto Handle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &TlsService->Handle, + &gEfiTlsServiceBindingProtocolGuid, + &TlsService->ServiceBinding, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_CLEAN_SERVICE; + } + + return Status; + +ON_CLEAN_SERVICE: + TlsCleanService (TlsService); + + return Status; +} + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +TlsServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ) +{ + TLS_SERVICE *TlsService; + TLS_INSTANCE *TlsInstance; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + TlsService = TLS_SERVICE_FROM_THIS (This); + + Status = TlsCreateInstance (TlsService, &TlsInstance); + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (TlsInstance != NULL); + + // + // Create a new TLS connection object. + // + TlsInstance->TlsConn = TlsNew (TlsService->TlsCtx); + if (TlsInstance->TlsConn == NULL) { + Status = EFI_ABORTED; + goto ON_ERROR; + } + + // + // Set default ConnectionEnd to EfiTlsClient + // + Status = TlsSetConnectionEnd (TlsInstance->TlsConn, EfiTlsClient); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Install TLS protocol and configuration protocol onto ChildHandle + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiTlsProtocolGuid, + &TlsInstance->Tls, + &gEfiTlsConfigurationProtocolGuid, + &TlsInstance->TlsConfig, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + TlsInstance->ChildHandle = *ChildHandle; + + // + // Add it to the TLS service's child list. + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + InsertTailList (&TlsService->TlsChildrenList, &TlsInstance->Link); + TlsService->TlsChildrenNum++; + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; + +ON_ERROR: + TlsCleanInstance (TlsInstance); + return Status; +} + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +TlsServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + TLS_SERVICE *TlsService; + TLS_INSTANCE *TlsInstance; + + EFI_TLS_PROTOCOL *Tls; + EFI_TLS_CONFIGURATION_PROTOCOL *TlsConfig; + EFI_STATUS Status; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + TlsService = TLS_SERVICE_FROM_THIS (This); + + // + // Find TLS protocol interface installed in ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiTlsProtocolGuid, + (VOID **) &Tls, + TlsService->ImageHandle, + NULL, + EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Find TLS configuration protocol interface installed in ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiTlsConfigurationProtocolGuid, + (VOID **) &TlsConfig, + TlsService->ImageHandle, + NULL, + EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + TlsInstance = TLS_INSTANCE_FROM_PROTOCOL (Tls); + + if (TlsInstance->Service != TlsService) { + return EFI_INVALID_PARAMETER; + } + + if (TlsInstance->InDestroy) { + return EFI_SUCCESS; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + TlsInstance->InDestroy = TRUE; + + // + // Uninstall the TLS protocol and TLS Configuration Protocol interface installed in ChildHandle. + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiTlsProtocolGuid, + Tls, + &gEfiTlsConfigurationProtocolGuid, + TlsConfig, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + RemoveEntryList (&TlsInstance->Link); + TlsService->TlsChildrenNum--; + + gBS->RestoreTPL (OldTpl); + + TlsCleanInstance (TlsInstance); + + return EFI_SUCCESS; +} + diff --git a/NetworkPkg/TlsDxe/TlsDriver.h b/NetworkPkg/TlsDxe/TlsDriver.h new file mode 100644 index 000000000..e9b581d05 --- /dev/null +++ b/NetworkPkg/TlsDxe/TlsDriver.h @@ -0,0 +1,232 @@ +/** @file + Header file of the Driver Binding and Service Binding Protocol for TlsDxe driver. + + Copyright (c) 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_TLS_DRIVER_H__ +#define __EFI_TLS_DRIVER_H__ + +#include + +// +// Driver Protocols +// +#include + +// +// Driver Version +// +#define TLS_VERSION 0x00000000 + +#define TLS_SERVICE_SIGNATURE SIGNATURE_32 ('T', 'L', 'S', 'S') + +#define TLS_INSTANCE_SIGNATURE SIGNATURE_32 ('T', 'L', 'S', 'I') + +/// +/// TLS Service Data +/// +typedef struct _TLS_SERVICE TLS_SERVICE; + +/// +/// TLS Instance Data +/// +typedef struct _TLS_INSTANCE TLS_INSTANCE; + + +struct _TLS_SERVICE { + UINT32 Signature; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + + UINT16 TlsChildrenNum; + LIST_ENTRY TlsChildrenList; + + // + // Handle to install TlsServiceBinding protocol. + // + EFI_HANDLE Handle; + EFI_HANDLE ImageHandle; + + // + // Main SSL Context object which is created by a server or client once per program + // life-time and which holds mainly default values for the SSL object which are later + // created for the connections. + // + VOID *TlsCtx; +}; + +struct _TLS_INSTANCE { + UINT32 Signature; + LIST_ENTRY Link; + + BOOLEAN InDestroy; + + TLS_SERVICE *Service; + EFI_HANDLE ChildHandle; + + EFI_TLS_PROTOCOL Tls; + EFI_TLS_CONFIGURATION_PROTOCOL TlsConfig; + + EFI_TLS_SESSION_STATE TlsSessionState; + + // + // Main SSL Connection which is created by a server or a client + // per established connection. + // + VOID *TlsConn; +}; + + +#define TLS_SERVICE_FROM_THIS(a) \ + CR (a, TLS_SERVICE, ServiceBinding, TLS_SERVICE_SIGNATURE) + +#define TLS_INSTANCE_FROM_PROTOCOL(a) \ + CR (a, TLS_INSTANCE, Tls, TLS_INSTANCE_SIGNATURE) + +#define TLS_INSTANCE_FROM_CONFIGURATION(a) \ + CR (a, TLS_INSTANCE, TlsConfig, TLS_INSTANCE_SIGNATURE) + + +/** + Release all the resources used by the TLS instance. + + @param[in] Instance The TLS instance data. + +**/ +VOID +TlsCleanInstance ( + IN TLS_INSTANCE *Instance + ); + +/** + Create the TLS instance and initialize it. + + @param[in] Service The pointer to the TLS service. + @param[out] Instance The pointer to the TLS instance. + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_SUCCESS The TLS instance is created. + +**/ +EFI_STATUS +TlsCreateInstance ( + IN TLS_SERVICE *Service, + OUT TLS_INSTANCE **Instance + ); + +/** + Release all the resources used by the TLS service binding instance. + + @param[in] Service The TLS service data. + +**/ +VOID +TlsCleanService ( + IN TLS_SERVICE *Service + ); + +/** + Create then initialize a TLS service. + + @param[in] Image ImageHandle of the TLS driver + @param[out] Service The service for TLS driver + + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to create the service. + @retval EFI_SUCCESS The service is created for the driver. + +**/ +EFI_STATUS +TlsCreateService ( + IN EFI_HANDLE Image, + OUT TLS_SERVICE **Service + ); + +/** + Unloads an image. + + @param[in] ImageHandle Handle that identifies the image to be unloaded. + + @retval EFI_SUCCESS The image has been unloaded. + @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle. + +**/ +EFI_STATUS +EFIAPI +TlsUnload ( + IN EFI_HANDLE ImageHandle + ); + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + @param ImageHandle The firmware allocated handle for the UEFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval Others An unexpected error occurred. +**/ +EFI_STATUS +EFIAPI +TlsDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ); + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +TlsServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +TlsServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +#endif + diff --git a/NetworkPkg/TlsDxe/TlsDxe.inf b/NetworkPkg/TlsDxe/TlsDxe.inf new file mode 100644 index 000000000..f64046180 --- /dev/null +++ b/NetworkPkg/TlsDxe/TlsDxe.inf @@ -0,0 +1,61 @@ +## @file +# This module produces EFI TLS Protocol, EFI TLS Service Binding Protocol and +# EFI TLS Configuration Protocol. +# +# This module produces EFI TLS (Transport Layer Security) Protocol and EFI TLS +# Service Binding Protocol, to provide TLS services. +# +# Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = TlsDxe + FILE_GUID = 3aceb0c0-3c72-11e4-9a56-74d435052646 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = TlsDriverEntryPoint + UNLOAD_IMAGE = TlsUnload + MODULE_UNI_FILE = TlsDxe.uni + +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + CryptoPkg/CryptoPkg.dec + +[Sources] + TlsDriver.h + TlsDriver.c + TlsProtocol.c + TlsConfigProtocol.c + TlsImpl.h + TlsImpl.c + +[LibraryClasses] + UefiDriverEntryPoint + UefiBootServicesTableLib + MemoryAllocationLib + BaseMemoryLib + BaseLib + UefiLib + DebugLib + BaseCryptLib + TlsLib + +[Protocols] + gEfiTlsServiceBindingProtocolGuid ## PRODUCES + gEfiTlsProtocolGuid ## PRODUCES + gEfiTlsConfigurationProtocolGuid ## PRODUCES + +[UserExtensions.TianoCore."ExtraFiles"] + TlsDxeExtra.uni + diff --git a/NetworkPkg/TlsDxe/TlsDxe.uni b/NetworkPkg/TlsDxe/TlsDxe.uni new file mode 100644 index 000000000..f5b5ffae6 --- /dev/null +++ b/NetworkPkg/TlsDxe/TlsDxe.uni @@ -0,0 +1,19 @@ +// /** @file +// This module produces EFI TLS Protocol, EFI TLS Service Binding Protocol and +// EFI TLS Configuration Protocol. +// +// This module produces EFI TLS (Transport Layer Security) Protocol, EFI TLS +// Service Binding Protocol, and EFI TLS Configuration Protocol to provide TLS +// services. +// +// Copyright (c) 2016, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "UEFI TLS service" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces EFI TLS Protocol, EFI TLS Service Binding Protocol and EFI TLS Configuration Protocol to provide EFI TLS services." + diff --git a/NetworkPkg/TlsDxe/TlsDxeExtra.uni b/NetworkPkg/TlsDxe/TlsDxeExtra.uni new file mode 100644 index 000000000..043ef80c5 --- /dev/null +++ b/NetworkPkg/TlsDxe/TlsDxeExtra.uni @@ -0,0 +1,13 @@ +// /** @file +// TlsDxe Localized Strings and Content +// +// Copyright (c) 2016, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"EFI TLS DXE Driver" + diff --git a/NetworkPkg/TlsDxe/TlsImpl.c b/NetworkPkg/TlsDxe/TlsImpl.c new file mode 100644 index 000000000..637fad239 --- /dev/null +++ b/NetworkPkg/TlsDxe/TlsImpl.c @@ -0,0 +1,349 @@ +/** @file + The Miscellaneous Routines for TlsDxe driver. + +Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+ +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TlsImpl.h" + +/** + Encrypt the message listed in fragment. + + @param[in] TlsInstance The pointer to the TLS instance. + @param[in, out] FragmentTable Pointer to a list of fragment. + On input these fragments contain the TLS header and + plain text TLS payload; + On output these fragments contain the TLS header and + cipher text TLS payload. + @param[in] FragmentCount Number of fragment. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_ABORTED TLS session state is incorrect. + @retval Others Other errors as indicated. +**/ +EFI_STATUS +TlsEncryptPacket ( + IN TLS_INSTANCE *TlsInstance, + IN OUT EFI_TLS_FRAGMENT_DATA **FragmentTable, + IN UINT32 *FragmentCount + ) +{ + EFI_STATUS Status; + UINTN Index; + UINT32 BytesCopied; + UINT32 BufferInSize; + UINT8 *BufferIn; + UINT8 *BufferInPtr; + TLS_RECORD_HEADER *RecordHeaderIn; + UINT16 ThisPlainMessageSize; + TLS_RECORD_HEADER *TempRecordHeader; + UINT16 ThisMessageSize; + UINT32 BufferOutSize; + UINT8 *BufferOut; + UINT32 RecordCount; + INTN Ret; + + Status = EFI_SUCCESS; + BytesCopied = 0; + BufferInSize = 0; + BufferIn = NULL; + BufferInPtr = NULL; + RecordHeaderIn = NULL; + TempRecordHeader = NULL; + BufferOutSize = 0; + BufferOut = NULL; + RecordCount = 0; + Ret = 0; + + // + // Calculate the size according to the fragment table. + // + for (Index = 0; Index < *FragmentCount; Index++) { + BufferInSize += (*FragmentTable)[Index].FragmentLength; + } + + // + // Allocate buffer for processing data. + // + BufferIn = AllocateZeroPool (BufferInSize); + if (BufferIn == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR; + } + + // + // Copy all TLS plain record header and payload into BufferIn. + // + for (Index = 0; Index < *FragmentCount; Index++) { + CopyMem ( + (BufferIn + BytesCopied), + (*FragmentTable)[Index].FragmentBuffer, + (*FragmentTable)[Index].FragmentLength + ); + BytesCopied += (*FragmentTable)[Index].FragmentLength; + } + + // + // Count TLS record number. + // + BufferInPtr = BufferIn; + while ((UINTN) BufferInPtr < (UINTN) BufferIn + BufferInSize) { + RecordHeaderIn = (TLS_RECORD_HEADER *) BufferInPtr; + if (RecordHeaderIn->ContentType != TlsContentTypeApplicationData || RecordHeaderIn->Length > TLS_PLAINTEXT_RECORD_MAX_PAYLOAD_LENGTH) { + Status = EFI_INVALID_PARAMETER; + goto ERROR; + } + BufferInPtr += TLS_RECORD_HEADER_LENGTH + RecordHeaderIn->Length; + RecordCount ++; + } + + // + // Allocate enough buffer to hold TLS Ciphertext. + // + BufferOut = AllocateZeroPool (RecordCount * (TLS_RECORD_HEADER_LENGTH + TLS_CIPHERTEXT_RECORD_MAX_PAYLOAD_LENGTH)); + if (BufferOut == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR; + } + + // + // Parsing buffer. Received packet may have multiple TLS record messages. + // + BufferInPtr = BufferIn; + TempRecordHeader = (TLS_RECORD_HEADER *) BufferOut; + while ((UINTN) BufferInPtr < (UINTN) BufferIn + BufferInSize) { + RecordHeaderIn = (TLS_RECORD_HEADER *) BufferInPtr; + + ThisPlainMessageSize = RecordHeaderIn->Length; + + TlsWrite (TlsInstance->TlsConn, (UINT8 *) (RecordHeaderIn + 1), ThisPlainMessageSize); + + Ret = TlsCtrlTrafficOut (TlsInstance->TlsConn, (UINT8 *)(TempRecordHeader), TLS_RECORD_HEADER_LENGTH + TLS_CIPHERTEXT_RECORD_MAX_PAYLOAD_LENGTH); + + if (Ret > 0) { + ThisMessageSize = (UINT16) Ret; + } else { + // + // No data was successfully encrypted, continue to encrypt other messages. + // + DEBUG ((EFI_D_WARN, "TlsEncryptPacket: No data read from TLS object.\n")); + + ThisMessageSize = 0; + } + + BufferOutSize += ThisMessageSize; + + BufferInPtr += TLS_RECORD_HEADER_LENGTH + ThisPlainMessageSize; + TempRecordHeader = (TLS_RECORD_HEADER *)((UINT8 *)TempRecordHeader + ThisMessageSize); + } + + FreePool (BufferIn); + BufferIn = NULL; + + // + // The caller will be responsible to handle the original fragment table. + // + *FragmentTable = AllocateZeroPool (sizeof (EFI_TLS_FRAGMENT_DATA)); + if (*FragmentTable == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR; + } + + (*FragmentTable)[0].FragmentBuffer = BufferOut; + (*FragmentTable)[0].FragmentLength = BufferOutSize; + *FragmentCount = 1; + + return Status; + +ERROR: + + if (BufferIn != NULL) { + FreePool (BufferIn); + BufferIn = NULL; + } + + if (BufferOut != NULL) { + FreePool (BufferOut); + BufferOut = NULL; + } + + return Status; +} + +/** + Decrypt the message listed in fragment. + + @param[in] TlsInstance The pointer to the TLS instance. + @param[in, out] FragmentTable Pointer to a list of fragment. + On input these fragments contain the TLS header and + cipher text TLS payload; + On output these fragments contain the TLS header and + plain text TLS payload. + @param[in] FragmentCount Number of fragment. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_ABORTED TLS session state is incorrect. + @retval Others Other errors as indicated. +**/ +EFI_STATUS +TlsDecryptPacket ( + IN TLS_INSTANCE *TlsInstance, + IN OUT EFI_TLS_FRAGMENT_DATA **FragmentTable, + IN UINT32 *FragmentCount + ) +{ + EFI_STATUS Status; + UINTN Index; + UINT32 BytesCopied; + UINT8 *BufferIn; + UINT32 BufferInSize; + UINT8 *BufferInPtr; + TLS_RECORD_HEADER *RecordHeaderIn; + UINT16 ThisCipherMessageSize; + TLS_RECORD_HEADER *TempRecordHeader; + UINT16 ThisPlainMessageSize; + UINT8 *BufferOut; + UINT32 BufferOutSize; + UINT32 RecordCount; + INTN Ret; + + Status = EFI_SUCCESS; + BytesCopied = 0; + BufferIn = NULL; + BufferInSize = 0; + BufferInPtr = NULL; + RecordHeaderIn = NULL; + TempRecordHeader = NULL; + BufferOut = NULL; + BufferOutSize = 0; + RecordCount = 0; + Ret = 0; + + // + // Calculate the size according to the fragment table. + // + for (Index = 0; Index < *FragmentCount; Index++) { + BufferInSize += (*FragmentTable)[Index].FragmentLength; + } + + // + // Allocate buffer for processing data + // + BufferIn = AllocateZeroPool (BufferInSize); + if (BufferIn == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR; + } + + // + // Copy all TLS plain record header and payload to BufferIn + // + for (Index = 0; Index < *FragmentCount; Index++) { + CopyMem ( + (BufferIn + BytesCopied), + (*FragmentTable)[Index].FragmentBuffer, + (*FragmentTable)[Index].FragmentLength + ); + BytesCopied += (*FragmentTable)[Index].FragmentLength; + } + + // + // Count TLS record number. + // + BufferInPtr = BufferIn; + while ((UINTN) BufferInPtr < (UINTN) BufferIn + BufferInSize) { + RecordHeaderIn = (TLS_RECORD_HEADER *) BufferInPtr; + if (RecordHeaderIn->ContentType != TlsContentTypeApplicationData || NTOHS (RecordHeaderIn->Length) > TLS_CIPHERTEXT_RECORD_MAX_PAYLOAD_LENGTH) { + Status = EFI_INVALID_PARAMETER; + goto ERROR; + } + BufferInPtr += TLS_RECORD_HEADER_LENGTH + NTOHS (RecordHeaderIn->Length); + RecordCount ++; + } + + // + // Allocate enough buffer to hold TLS Plaintext. + // + BufferOut = AllocateZeroPool (RecordCount * (TLS_RECORD_HEADER_LENGTH + TLS_PLAINTEXT_RECORD_MAX_PAYLOAD_LENGTH)); + if (BufferOut == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR; + } + + // + // Parsing buffer. Received packet may have multiple TLS record messages. + // + BufferInPtr = BufferIn; + TempRecordHeader = (TLS_RECORD_HEADER *) BufferOut; + while ((UINTN) BufferInPtr < (UINTN) BufferIn + BufferInSize) { + RecordHeaderIn = (TLS_RECORD_HEADER *) BufferInPtr; + + ThisCipherMessageSize = NTOHS (RecordHeaderIn->Length); + + Ret = TlsCtrlTrafficIn (TlsInstance->TlsConn, (UINT8 *) (RecordHeaderIn), TLS_RECORD_HEADER_LENGTH + ThisCipherMessageSize); + if (Ret != TLS_RECORD_HEADER_LENGTH + ThisCipherMessageSize) { + TlsInstance->TlsSessionState = EfiTlsSessionError; + Status = EFI_ABORTED; + goto ERROR; + } + + Ret = 0; + Ret = TlsRead (TlsInstance->TlsConn, (UINT8 *) (TempRecordHeader + 1), TLS_PLAINTEXT_RECORD_MAX_PAYLOAD_LENGTH); + + if (Ret > 0) { + ThisPlainMessageSize = (UINT16) Ret; + } else { + // + // No data was successfully decrypted, continue to decrypt other messages. + // + DEBUG ((EFI_D_WARN, "TlsDecryptPacket: No data read from TLS object.\n")); + + ThisPlainMessageSize = 0; + } + + CopyMem (TempRecordHeader, RecordHeaderIn, TLS_RECORD_HEADER_LENGTH); + TempRecordHeader->Length = ThisPlainMessageSize; + BufferOutSize += TLS_RECORD_HEADER_LENGTH + ThisPlainMessageSize; + + BufferInPtr += TLS_RECORD_HEADER_LENGTH + ThisCipherMessageSize; + TempRecordHeader = (TLS_RECORD_HEADER *)((UINT8 *)TempRecordHeader + TLS_RECORD_HEADER_LENGTH + ThisPlainMessageSize); + } + + FreePool (BufferIn); + BufferIn = NULL; + + // + // The caller will be responsible to handle the original fragment table + // + *FragmentTable = AllocateZeroPool (sizeof (EFI_TLS_FRAGMENT_DATA)); + if (*FragmentTable == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR; + } + + (*FragmentTable)[0].FragmentBuffer = BufferOut; + (*FragmentTable)[0].FragmentLength = BufferOutSize; + *FragmentCount = 1; + + return Status; + +ERROR: + + if (BufferIn != NULL) { + FreePool (BufferIn); + BufferIn = NULL; + } + + if (BufferOut != NULL) { + FreePool (BufferOut); + BufferOut = NULL; + } + + return Status; +} + diff --git a/NetworkPkg/TlsDxe/TlsImpl.h b/NetworkPkg/TlsDxe/TlsImpl.h new file mode 100644 index 000000000..ce9f1e8b8 --- /dev/null +++ b/NetworkPkg/TlsDxe/TlsImpl.h @@ -0,0 +1,306 @@ +/** @file + Header file of Miscellaneous Routines for TlsDxe driver. + +Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+ +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_TLS_IMPL_H__ +#define __EFI_TLS_IMPL_H__ + +// +// Libraries +// +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +// Consumed Protocols +// +#include +#include + +#include + +#include "TlsDriver.h" + +// +// Protocol instances +// +extern EFI_SERVICE_BINDING_PROTOCOL mTlsServiceBinding; +extern EFI_TLS_PROTOCOL mTlsProtocol; +extern EFI_TLS_CONFIGURATION_PROTOCOL mTlsConfigurationProtocol; + +/** + Encrypt the message listed in fragment. + + @param[in] TlsInstance The pointer to the TLS instance. + @param[in, out] FragmentTable Pointer to a list of fragment. + On input these fragments contain the TLS header and + plain text TLS payload; + On output these fragments contain the TLS header and + cipher text TLS payload. + @param[in] FragmentCount Number of fragment. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_ABORTED TLS session state is incorrect. + @retval Others Other errors as indicated. +**/ +EFI_STATUS +TlsEncryptPacket ( + IN TLS_INSTANCE *TlsInstance, + IN OUT EFI_TLS_FRAGMENT_DATA **FragmentTable, + IN UINT32 *FragmentCount + ); + +/** + Decrypt the message listed in fragment. + + @param[in] TlsInstance The pointer to the TLS instance. + @param[in, out] FragmentTable Pointer to a list of fragment. + On input these fragments contain the TLS header and + cipher text TLS payload; + On output these fragments contain the TLS header and + plain text TLS payload. + @param[in] FragmentCount Number of fragment. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources. + @retval EFI_ABORTED TLS session state is incorrect. + @retval Others Other errors as indicated. +**/ +EFI_STATUS +TlsDecryptPacket ( + IN TLS_INSTANCE *TlsInstance, + IN OUT EFI_TLS_FRAGMENT_DATA **FragmentTable, + IN UINT32 *FragmentCount + ); + +/** + Set TLS session data. + + The SetSessionData() function set data for a new TLS session. All session data should + be set before BuildResponsePacket() invoked. + + @param[in] This Pointer to the EFI_TLS_PROTOCOL instance. + @param[in] DataType TLS session data type. + @param[in] Data Pointer to session data. + @param[in] DataSize Total size of session data. + + @retval EFI_SUCCESS The TLS session data is set successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Data is NULL. + DataSize is 0. + @retval EFI_UNSUPPORTED The DataType is unsupported. + @retval EFI_ACCESS_DENIED If the DataType is one of below: + EfiTlsClientRandom + EfiTlsServerRandom + EfiTlsKeyMaterial + @retval EFI_NOT_READY Current TLS session state is NOT + EfiTlsSessionStateNotStarted. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. +**/ +EFI_STATUS +EFIAPI +TlsSetSessionData ( + IN EFI_TLS_PROTOCOL *This, + IN EFI_TLS_SESSION_DATA_TYPE DataType, + IN VOID *Data, + IN UINTN DataSize + ); + +/** + Get TLS session data. + + The GetSessionData() function return the TLS session information. + + @param[in] This Pointer to the EFI_TLS_PROTOCOL instance. + @param[in] DataType TLS session data type. + @param[in, out] Data Pointer to session data. + @param[in, out] DataSize Total size of session data. On input, it means + the size of Data buffer. On output, it means the size + of copied Data buffer if EFI_SUCCESS, and means the + size of desired Data buffer if EFI_BUFFER_TOO_SMALL. + + @retval EFI_SUCCESS The TLS session data is got successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + DataSize is NULL. + Data is NULL if *DataSize is not zero. + @retval EFI_UNSUPPORTED The DataType is unsupported. + @retval EFI_NOT_FOUND The TLS session data is not found. + @retval EFI_NOT_READY The DataType is not ready in current session state. + @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the data. +**/ +EFI_STATUS +EFIAPI +TlsGetSessionData ( + IN EFI_TLS_PROTOCOL *This, + IN EFI_TLS_SESSION_DATA_TYPE DataType, + IN OUT VOID *Data, OPTIONAL + IN OUT UINTN *DataSize + ); + +/** + Build response packet according to TLS state machine. This function is only valid for + alert, handshake and change_cipher_spec content type. + + The BuildResponsePacket() function builds TLS response packet in response to the TLS + request packet specified by RequestBuffer and RequestSize. If RequestBuffer is NULL and + RequestSize is 0, and TLS session status is EfiTlsSessionNotStarted, the TLS session + will be initiated and the response packet needs to be ClientHello. If RequestBuffer is + NULL and RequestSize is 0, and TLS session status is EfiTlsSessionClosing, the TLS + session will be closed and response packet needs to be CloseNotify. If RequestBuffer is + NULL and RequestSize is 0, and TLS session status is EfiTlsSessionError, the TLS + session has errors and the response packet needs to be Alert message based on error + type. + + @param[in] This Pointer to the EFI_TLS_PROTOCOL instance. + @param[in] RequestBuffer Pointer to the most recently received TLS packet. NULL + means TLS need initiate the TLS session and response + packet need to be ClientHello. + @param[in] RequestSize Packet size in bytes for the most recently received TLS + packet. 0 is only valid when RequestBuffer is NULL. + @param[out] Buffer Pointer to the buffer to hold the built packet. + @param[in, out] BufferSize Pointer to the buffer size in bytes. On input, it is + the buffer size provided by the caller. On output, it + is the buffer size in fact needed to contain the + packet. + + @retval EFI_SUCCESS The required TLS packet is built successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + RequestBuffer is NULL but RequestSize is NOT 0. + RequestSize is 0 but RequestBuffer is NOT NULL. + BufferSize is NULL. + Buffer is NULL if *BufferSize is not zero. + @retval EFI_BUFFER_TOO_SMALL BufferSize is too small to hold the response packet. + @retval EFI_NOT_READY Current TLS session state is NOT ready to build + ResponsePacket. + @retval EFI_ABORTED Something wrong build response packet. +**/ +EFI_STATUS +EFIAPI +TlsBuildResponsePacket ( + IN EFI_TLS_PROTOCOL *This, + IN UINT8 *RequestBuffer, OPTIONAL + IN UINTN RequestSize, OPTIONAL + OUT UINT8 *Buffer, OPTIONAL + IN OUT UINTN *BufferSize + ); + +/** + Decrypt or encrypt TLS packet during session. This function is only valid after + session connected and for application_data content type. + + The ProcessPacket () function process each inbound or outbound TLS APP packet. + + @param[in] This Pointer to the EFI_TLS_PROTOCOL instance. + @param[in, out] FragmentTable Pointer to a list of fragment. The caller will take + responsible to handle the original FragmentTable while + it may be reallocated in TLS driver. If CryptMode is + EfiTlsEncrypt, on input these fragments contain the TLS + header and plain text TLS APP payload; on output these + fragments contain the TLS header and cipher text TLS + APP payload. If CryptMode is EfiTlsDecrypt, on input + these fragments contain the TLS header and cipher text + TLS APP payload; on output these fragments contain the + TLS header and plain text TLS APP payload. + @param[in] FragmentCount Number of fragment. + @param[in] CryptMode Crypt mode. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + FragmentTable is NULL. + FragmentCount is NULL. + CryptoMode is invalid. + @retval EFI_NOT_READY Current TLS session state is NOT + EfiTlsSessionDataTransferring. + @retval EFI_ABORTED Something wrong decryption the message. TLS session + status will become EfiTlsSessionError. The caller need + call BuildResponsePacket() to generate Error Alert + message and send it out. + @retval EFI_OUT_OF_RESOURCES No enough resource to finish the operation. +**/ +EFI_STATUS +EFIAPI +TlsProcessPacket ( + IN EFI_TLS_PROTOCOL *This, + IN OUT EFI_TLS_FRAGMENT_DATA **FragmentTable, + IN UINT32 *FragmentCount, + IN EFI_TLS_CRYPT_MODE CryptMode + ); + +/** + Set TLS configuration data. + + The SetData() function sets TLS configuration to non-volatile storage or volatile + storage. + + @param[in] This Pointer to the EFI_TLS_CONFIGURATION_PROTOCOL instance. + @param[in] DataType Configuration data type. + @param[in] Data Pointer to configuration data. + @param[in] DataSize Total size of configuration data. + + @retval EFI_SUCCESS The TLS configuration data is set successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Data is NULL. + DataSize is 0. + @retval EFI_UNSUPPORTED The DataType is unsupported. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. +**/ +EFI_STATUS +EFIAPI +TlsConfigurationSetData ( + IN EFI_TLS_CONFIGURATION_PROTOCOL *This, + IN EFI_TLS_CONFIG_DATA_TYPE DataType, + IN VOID *Data, + IN UINTN DataSize + ); + +/** + Get TLS configuration data. + + The GetData() function gets TLS configuration. + + @param[in] This Pointer to the EFI_TLS_CONFIGURATION_PROTOCOL instance. + @param[in] DataType Configuration data type. + @param[in, out] Data Pointer to configuration data. + @param[in, out] DataSize Total size of configuration data. On input, it means + the size of Data buffer. On output, it means the size + of copied Data buffer if EFI_SUCCESS, and means the + size of desired Data buffer if EFI_BUFFER_TOO_SMALL. + + @retval EFI_SUCCESS The TLS configuration data is got successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + DataSize is NULL. + Data is NULL if *DataSize is not zero. + @retval EFI_UNSUPPORTED The DataType is unsupported. + @retval EFI_NOT_FOUND The TLS configuration data is not found. + @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the data. +**/ +EFI_STATUS +EFIAPI +TlsConfigurationGetData ( + IN EFI_TLS_CONFIGURATION_PROTOCOL *This, + IN EFI_TLS_CONFIG_DATA_TYPE DataType, + IN OUT VOID *Data, OPTIONAL + IN OUT UINTN *DataSize + ); + +#endif + diff --git a/NetworkPkg/TlsDxe/TlsProtocol.c b/NetworkPkg/TlsDxe/TlsProtocol.c new file mode 100644 index 000000000..a7a993fc6 --- /dev/null +++ b/NetworkPkg/TlsDxe/TlsProtocol.c @@ -0,0 +1,638 @@ +/** @file + Implementation of EFI TLS Protocol Interfaces. + + Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TlsImpl.h" + +EFI_TLS_PROTOCOL mTlsProtocol = { + TlsSetSessionData, + TlsGetSessionData, + TlsBuildResponsePacket, + TlsProcessPacket +}; + +/** + Set TLS session data. + + The SetSessionData() function set data for a new TLS session. All session data should + be set before BuildResponsePacket() invoked. + + @param[in] This Pointer to the EFI_TLS_PROTOCOL instance. + @param[in] DataType TLS session data type. + @param[in] Data Pointer to session data. + @param[in] DataSize Total size of session data. + + @retval EFI_SUCCESS The TLS session data is set successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Data is NULL. + DataSize is 0. + DataSize is invalid for DataType. + @retval EFI_UNSUPPORTED The DataType is unsupported. + @retval EFI_ACCESS_DENIED If the DataType is one of below: + EfiTlsClientRandom + EfiTlsServerRandom + EfiTlsKeyMaterial + @retval EFI_NOT_READY Current TLS session state is NOT + EfiTlsSessionStateNotStarted. + @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. +**/ +EFI_STATUS +EFIAPI +TlsSetSessionData ( + IN EFI_TLS_PROTOCOL *This, + IN EFI_TLS_SESSION_DATA_TYPE DataType, + IN VOID *Data, + IN UINTN DataSize + ) +{ + EFI_STATUS Status; + TLS_INSTANCE *Instance; + UINT16 *CipherId; + CONST EFI_TLS_CIPHER *TlsCipherList; + UINTN CipherCount; + UINTN Index; + + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + CipherId = NULL; + + if (This == NULL || Data == NULL || DataSize == 0) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = TLS_INSTANCE_FROM_PROTOCOL (This); + + if (DataType != EfiTlsSessionState && Instance->TlsSessionState != EfiTlsSessionNotStarted){ + Status = EFI_NOT_READY; + goto ON_EXIT; + } + + switch (DataType) { + // + // Session Configuration + // + case EfiTlsVersion: + if (DataSize != sizeof (EFI_TLS_VERSION)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + Status = TlsSetVersion (Instance->TlsConn, ((EFI_TLS_VERSION *) Data)->Major, ((EFI_TLS_VERSION *) Data)->Minor); + break; + case EfiTlsConnectionEnd: + if (DataSize != sizeof (EFI_TLS_CONNECTION_END)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + Status = TlsSetConnectionEnd (Instance->TlsConn, *((EFI_TLS_CONNECTION_END *) Data)); + break; + case EfiTlsCipherList: + if (DataSize % sizeof (EFI_TLS_CIPHER) != 0) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + CipherId = AllocatePool (DataSize); + if (CipherId == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + TlsCipherList = (CONST EFI_TLS_CIPHER *) Data; + CipherCount = DataSize / sizeof (EFI_TLS_CIPHER); + for (Index = 0; Index < CipherCount; Index++) { + CipherId[Index] = ((TlsCipherList[Index].Data1 << 8) | + TlsCipherList[Index].Data2); + } + + Status = TlsSetCipherList (Instance->TlsConn, CipherId, CipherCount); + + FreePool (CipherId); + break; + case EfiTlsCompressionMethod: + // + // TLS seems only define one CompressionMethod.null, which specifies that data exchanged via the + // record protocol will not be compressed. + // More information from OpenSSL: http://www.openssl.org/docs/manmaster/ssl/SSL_COMP_add_compression_method.html + // The TLS RFC does however not specify compression methods or their corresponding identifiers, + // so there is currently no compatible way to integrate compression with unknown peers. + // It is therefore currently not recommended to integrate compression into applications. + // Applications for non-public use may agree on certain compression methods. + // Using different compression methods with the same identifier will lead to connection failure. + // + for (Index = 0; Index < DataSize / sizeof (EFI_TLS_COMPRESSION); Index++) { + Status = TlsSetCompressionMethod (*((UINT8 *) Data + Index)); + if (EFI_ERROR (Status)) { + break; + } + } + + break; + case EfiTlsExtensionData: + Status = EFI_UNSUPPORTED; + goto ON_EXIT; + case EfiTlsVerifyMethod: + if (DataSize != sizeof (EFI_TLS_VERIFY)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + TlsSetVerify (Instance->TlsConn, *((UINT32 *) Data)); + break; + case EfiTlsSessionID: + if (DataSize != sizeof (EFI_TLS_SESSION_ID)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + Status = TlsSetSessionId ( + Instance->TlsConn, + ((EFI_TLS_SESSION_ID *) Data)->Data, + ((EFI_TLS_SESSION_ID *) Data)->Length + ); + break; + case EfiTlsSessionState: + if (DataSize != sizeof (EFI_TLS_SESSION_STATE)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + Instance->TlsSessionState = *(EFI_TLS_SESSION_STATE *) Data; + break; + // + // Session information + // + case EfiTlsClientRandom: + Status = EFI_ACCESS_DENIED; + break; + case EfiTlsServerRandom: + Status = EFI_ACCESS_DENIED; + break; + case EfiTlsKeyMaterial: + Status = EFI_ACCESS_DENIED; + break; + // + // Unsupported type. + // + default: + Status = EFI_UNSUPPORTED; + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Get TLS session data. + + The GetSessionData() function return the TLS session information. + + @param[in] This Pointer to the EFI_TLS_PROTOCOL instance. + @param[in] DataType TLS session data type. + @param[in, out] Data Pointer to session data. + @param[in, out] DataSize Total size of session data. On input, it means + the size of Data buffer. On output, it means the size + of copied Data buffer if EFI_SUCCESS, and means the + size of desired Data buffer if EFI_BUFFER_TOO_SMALL. + + @retval EFI_SUCCESS The TLS session data is got successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + DataSize is NULL. + Data is NULL if *DataSize is not zero. + @retval EFI_UNSUPPORTED The DataType is unsupported. + @retval EFI_NOT_FOUND The TLS session data is not found. + @retval EFI_NOT_READY The DataType is not ready in current session state. + @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the data. +**/ +EFI_STATUS +EFIAPI +TlsGetSessionData ( + IN EFI_TLS_PROTOCOL *This, + IN EFI_TLS_SESSION_DATA_TYPE DataType, + IN OUT VOID *Data, OPTIONAL + IN OUT UINTN *DataSize + ) +{ + EFI_STATUS Status; + TLS_INSTANCE *Instance; + + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + + if (This == NULL || DataSize == NULL || (Data == NULL && *DataSize != 0)) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = TLS_INSTANCE_FROM_PROTOCOL (This); + + if (Instance->TlsSessionState == EfiTlsSessionNotStarted && + (DataType == EfiTlsSessionID || DataType == EfiTlsClientRandom || + DataType == EfiTlsServerRandom || DataType == EfiTlsKeyMaterial)) { + Status = EFI_NOT_READY; + goto ON_EXIT; + } + + switch (DataType) { + case EfiTlsVersion: + if (*DataSize < sizeof (EFI_TLS_VERSION)) { + *DataSize = sizeof (EFI_TLS_VERSION); + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + *DataSize = sizeof (EFI_TLS_VERSION); + *((UINT16 *) Data) = HTONS (TlsGetVersion (Instance->TlsConn)); + break; + case EfiTlsConnectionEnd: + if (*DataSize < sizeof (EFI_TLS_CONNECTION_END)) { + *DataSize = sizeof (EFI_TLS_CONNECTION_END); + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + *DataSize = sizeof (EFI_TLS_CONNECTION_END); + *((UINT8 *) Data) = TlsGetConnectionEnd (Instance->TlsConn); + break; + case EfiTlsCipherList: + // + // Get the current session cipher suite. + // + if (*DataSize < sizeof (EFI_TLS_CIPHER)) { + *DataSize = sizeof (EFI_TLS_CIPHER); + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + *DataSize = sizeof(EFI_TLS_CIPHER); + Status = TlsGetCurrentCipher (Instance->TlsConn, (UINT16 *) Data); + *((UINT16 *) Data) = HTONS (*((UINT16 *) Data)); + break; + case EfiTlsCompressionMethod: + // + // Get the current session compression method. + // + if (*DataSize < sizeof (EFI_TLS_COMPRESSION)) { + *DataSize = sizeof (EFI_TLS_COMPRESSION); + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + *DataSize = sizeof (EFI_TLS_COMPRESSION); + Status = TlsGetCurrentCompressionId (Instance->TlsConn, (UINT8 *) Data); + break; + case EfiTlsExtensionData: + Status = EFI_UNSUPPORTED; + goto ON_EXIT; + case EfiTlsVerifyMethod: + if (*DataSize < sizeof (EFI_TLS_VERIFY)) { + *DataSize = sizeof (EFI_TLS_VERIFY); + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + *DataSize = sizeof (EFI_TLS_VERIFY); + *((UINT32 *) Data) = TlsGetVerify (Instance->TlsConn); + break; + case EfiTlsSessionID: + if (*DataSize < sizeof (EFI_TLS_SESSION_ID)) { + *DataSize = sizeof (EFI_TLS_SESSION_ID); + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + *DataSize = sizeof (EFI_TLS_SESSION_ID); + Status = TlsGetSessionId ( + Instance->TlsConn, + ((EFI_TLS_SESSION_ID *) Data)->Data, + &(((EFI_TLS_SESSION_ID *) Data)->Length) + ); + break; + case EfiTlsSessionState: + if (*DataSize < sizeof (EFI_TLS_SESSION_STATE)) { + *DataSize = sizeof (EFI_TLS_SESSION_STATE); + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + *DataSize = sizeof (EFI_TLS_SESSION_STATE); + CopyMem (Data, &Instance->TlsSessionState, *DataSize); + break; + case EfiTlsClientRandom: + if (*DataSize < sizeof (EFI_TLS_RANDOM)) { + *DataSize = sizeof (EFI_TLS_RANDOM); + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + *DataSize = sizeof (EFI_TLS_RANDOM); + TlsGetClientRandom (Instance->TlsConn, (UINT8 *) Data); + break; + case EfiTlsServerRandom: + if (*DataSize < sizeof (EFI_TLS_RANDOM)) { + *DataSize = sizeof (EFI_TLS_RANDOM); + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + *DataSize = sizeof (EFI_TLS_RANDOM); + TlsGetServerRandom (Instance->TlsConn, (UINT8 *) Data); + break; + case EfiTlsKeyMaterial: + if (*DataSize < sizeof (EFI_TLS_MASTER_SECRET)) { + *DataSize = sizeof (EFI_TLS_MASTER_SECRET); + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + *DataSize = sizeof (EFI_TLS_MASTER_SECRET); + Status = TlsGetKeyMaterial (Instance->TlsConn, (UINT8 *) Data); + break; + // + // Unsupported type. + // + default: + Status = EFI_UNSUPPORTED; + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Build response packet according to TLS state machine. This function is only valid for + alert, handshake and change_cipher_spec content type. + + The BuildResponsePacket() function builds TLS response packet in response to the TLS + request packet specified by RequestBuffer and RequestSize. If RequestBuffer is NULL and + RequestSize is 0, and TLS session status is EfiTlsSessionNotStarted, the TLS session + will be initiated and the response packet needs to be ClientHello. If RequestBuffer is + NULL and RequestSize is 0, and TLS session status is EfiTlsSessionClosing, the TLS + session will be closed and response packet needs to be CloseNotify. If RequestBuffer is + NULL and RequestSize is 0, and TLS session status is EfiTlsSessionError, the TLS + session has errors and the response packet needs to be Alert message based on error + type. + + @param[in] This Pointer to the EFI_TLS_PROTOCOL instance. + @param[in] RequestBuffer Pointer to the most recently received TLS packet. NULL + means TLS need initiate the TLS session and response + packet need to be ClientHello. + @param[in] RequestSize Packet size in bytes for the most recently received TLS + packet. 0 is only valid when RequestBuffer is NULL. + @param[out] Buffer Pointer to the buffer to hold the built packet. + @param[in, out] BufferSize Pointer to the buffer size in bytes. On input, it is + the buffer size provided by the caller. On output, it + is the buffer size in fact needed to contain the + packet. + + @retval EFI_SUCCESS The required TLS packet is built successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + RequestBuffer is NULL but RequestSize is NOT 0. + RequestSize is 0 but RequestBuffer is NOT NULL. + BufferSize is NULL. + Buffer is NULL if *BufferSize is not zero. + @retval EFI_BUFFER_TOO_SMALL BufferSize is too small to hold the response packet. + @retval EFI_NOT_READY Current TLS session state is NOT ready to build + ResponsePacket. + @retval EFI_ABORTED Something wrong build response packet. +**/ +EFI_STATUS +EFIAPI +TlsBuildResponsePacket ( + IN EFI_TLS_PROTOCOL *This, + IN UINT8 *RequestBuffer, OPTIONAL + IN UINTN RequestSize, OPTIONAL + OUT UINT8 *Buffer, OPTIONAL + IN OUT UINTN *BufferSize + ) +{ + EFI_STATUS Status; + TLS_INSTANCE *Instance; + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + + if ((This == NULL) || (BufferSize == NULL) || + (RequestBuffer == NULL && RequestSize != 0) || + (RequestBuffer != NULL && RequestSize == 0) || + (Buffer == NULL && *BufferSize !=0)) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = TLS_INSTANCE_FROM_PROTOCOL (This); + + if(RequestBuffer == NULL && RequestSize == 0) { + switch (Instance->TlsSessionState) { + case EfiTlsSessionNotStarted: + // + // ClientHello. + // + Status = TlsDoHandshake ( + Instance->TlsConn, + NULL, + 0, + Buffer, + BufferSize + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // *BufferSize should not be zero when ClientHello. + // + if (*BufferSize == 0) { + Status = EFI_ABORTED; + goto ON_EXIT; + } + + Instance->TlsSessionState = EfiTlsSessionHandShaking; + + break; + case EfiTlsSessionClosing: + // + // TLS session will be closed and response packet needs to be CloseNotify. + // + Status = TlsCloseNotify ( + Instance->TlsConn, + Buffer, + BufferSize + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // *BufferSize should not be zero when build CloseNotify message. + // + if (*BufferSize == 0) { + Status = EFI_ABORTED; + goto ON_EXIT; + } + + break; + case EfiTlsSessionError: + // + // TLS session has errors and the response packet needs to be Alert + // message based on error type. + // + Status = TlsHandleAlert ( + Instance->TlsConn, + NULL, + 0, + Buffer, + BufferSize + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + break; + default: + // + // Current TLS session state is NOT ready to build ResponsePacket. + // + Status = EFI_NOT_READY; + } + } else { + // + // 1. Received packet may have multiple TLS record messages. + // 2. One TLS record message may have multiple handshake protocol. + // 3. Some errors may be happened in handshake. + // TlsDoHandshake() can handle all of those cases. + // + if (TlsInHandshake (Instance->TlsConn)) { + Status = TlsDoHandshake ( + Instance->TlsConn, + RequestBuffer, + RequestSize, + Buffer, + BufferSize + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + if (!TlsInHandshake (Instance->TlsConn)) { + Instance->TlsSessionState = EfiTlsSessionDataTransferring; + } + } else { + // + // Must be alert message, Decrypt it and build the ResponsePacket. + // + ASSERT (((TLS_RECORD_HEADER *) RequestBuffer)->ContentType == TlsContentTypeAlert); + + Status = TlsHandleAlert ( + Instance->TlsConn, + RequestBuffer, + RequestSize, + Buffer, + BufferSize + ); + if (EFI_ERROR (Status)) { + if (Status != EFI_BUFFER_TOO_SMALL) { + Instance->TlsSessionState = EfiTlsSessionError; + } + + goto ON_EXIT; + } + } + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Decrypt or encrypt TLS packet during session. This function is only valid after + session connected and for application_data content type. + + The ProcessPacket () function process each inbound or outbound TLS APP packet. + + @param[in] This Pointer to the EFI_TLS_PROTOCOL instance. + @param[in, out] FragmentTable Pointer to a list of fragment. The caller will take + responsible to handle the original FragmentTable while + it may be reallocated in TLS driver. If CryptMode is + EfiTlsEncrypt, on input these fragments contain the TLS + header and plain text TLS APP payload; on output these + fragments contain the TLS header and cipher text TLS + APP payload. If CryptMode is EfiTlsDecrypt, on input + these fragments contain the TLS header and cipher text + TLS APP payload; on output these fragments contain the + TLS header and plain text TLS APP payload. + @param[in] FragmentCount Number of fragment. + @param[in] CryptMode Crypt mode. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + FragmentTable is NULL. + FragmentCount is NULL. + CryptoMode is invalid. + @retval EFI_NOT_READY Current TLS session state is NOT + EfiTlsSessionDataTransferring. + @retval EFI_ABORTED Something wrong decryption the message. TLS session + status will become EfiTlsSessionError. The caller need + call BuildResponsePacket() to generate Error Alert + message and send it out. + @retval EFI_OUT_OF_RESOURCES No enough resource to finish the operation. +**/ +EFI_STATUS +EFIAPI +TlsProcessPacket ( + IN EFI_TLS_PROTOCOL *This, + IN OUT EFI_TLS_FRAGMENT_DATA **FragmentTable, + IN UINT32 *FragmentCount, + IN EFI_TLS_CRYPT_MODE CryptMode + ) +{ + EFI_STATUS Status; + TLS_INSTANCE *Instance; + + EFI_TPL OldTpl; + + Status = EFI_SUCCESS; + + if (This == NULL || FragmentTable == NULL || FragmentCount == NULL) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + Instance = TLS_INSTANCE_FROM_PROTOCOL (This); + + if (Instance->TlsSessionState != EfiTlsSessionDataTransferring) { + Status = EFI_NOT_READY; + goto ON_EXIT; + } + + // + // Packet sent or received may have multiple TLS record messages (Application data type). + // So,on input these fragments contain the TLS header and TLS APP payload; + // on output these fragments also contain the TLS header and TLS APP payload. + // + switch (CryptMode) { + case EfiTlsEncrypt: + Status = TlsEncryptPacket (Instance, FragmentTable, FragmentCount); + break; + case EfiTlsDecrypt: + Status = TlsDecryptPacket (Instance, FragmentTable, FragmentCount); + break; + default: + return EFI_INVALID_PARAMETER; + } + +ON_EXIT: + gBS->RestoreTPL (OldTpl); + return Status; +} + diff --git a/NetworkPkg/Udp4Dxe/ComponentName.c b/NetworkPkg/Udp4Dxe/ComponentName.c new file mode 100644 index 000000000..41bd834c6 --- /dev/null +++ b/NetworkPkg/Udp4Dxe/ComponentName.c @@ -0,0 +1,429 @@ +/** @file + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Udp4Impl.h" + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +UdpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +UdpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gUdp4ComponentName = { + UdpComponentNameGetDriverName, + UdpComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gUdp4ComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) UdpComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) UdpComponentNameGetControllerName, + "en" +}; + + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mUdpDriverNameTable[] = { + { + "eng;en", + L"UDP Network Service Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gUdpControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +UdpComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mUdpDriverNameTable, + DriverName, + (BOOLEAN)(This == &gUdp4ComponentName) + ); +} + +/** + Update the component name for the Udp4 child handle. + + @param Udp4[in] A pointer to the EFI_UDP4_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateName ( + EFI_UDP4_PROTOCOL *Udp4 + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[64]; + EFI_UDP4_CONFIG_DATA Udp4ConfigData; + + if (Udp4 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer as: + // UDPv4 (SrcPort=59, DestPort=60) + // + Status = Udp4->GetModeData (Udp4, &Udp4ConfigData, NULL, NULL, NULL); + if (!EFI_ERROR (Status)) { + UnicodeSPrint (HandleName, sizeof (HandleName), + L"UDPv4 (SrcPort=%d, DestPort=%d)", + Udp4ConfigData.StationPort, + Udp4ConfigData.RemotePort + ); + } else if (Status == EFI_NOT_STARTED) { + UnicodeSPrint ( + HandleName, + sizeof (HandleName), + L"UDPv4 (Not started)" + ); + } else { + return Status; + } + + if (gUdpControllerNameTable != NULL) { + FreeUnicodeStringTable (gUdpControllerNameTable); + gUdpControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gUdp4ComponentName.SupportedLanguages, + &gUdpControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gUdp4ComponentName2.SupportedLanguages, + &gUdpControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +UdpComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_UDP4_PROTOCOL *Udp4; + + // + // Only provide names for child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiIp4ProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiUdp4ProtocolGuid, + (VOID **)&Udp4, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateName (Udp4); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gUdpControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gUdp4ComponentName) + ); +} + diff --git a/NetworkPkg/Udp4Dxe/Udp4Driver.c b/NetworkPkg/Udp4Dxe/Udp4Driver.c new file mode 100644 index 000000000..63b103b8e --- /dev/null +++ b/NetworkPkg/Udp4Dxe/Udp4Driver.c @@ -0,0 +1,584 @@ +/** @file + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Udp4Impl.h" + +EFI_DRIVER_BINDING_PROTOCOL gUdp4DriverBinding = { + Udp4DriverBindingSupported, + Udp4DriverBindingStart, + Udp4DriverBindingStop, + 0xa, + NULL, + NULL +}; + +EFI_SERVICE_BINDING_PROTOCOL mUdp4ServiceBinding = { + Udp4ServiceBindingCreateChild, + Udp4ServiceBindingDestroyChild +}; + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +Udp4DestroyChildEntryInHandleBuffer ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + UDP4_INSTANCE_DATA *Instance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = NET_LIST_USER_STRUCT_S (Entry, UDP4_INSTANCE_DATA, Link, UDP4_INSTANCE_DATA_SIGNATURE); + ServiceBinding = ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding; + NumberOfChildren = ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren; + ChildHandleBuffer = ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer; + + if (!NetIsInHandleBuffer (Instance->ChildHandle, NumberOfChildren, ChildHandleBuffer)) { + return EFI_SUCCESS; + } + + return ServiceBinding->DestroyChild (ServiceBinding, Instance->ChildHandle); +} + + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device + @retval EFI_ALREADY_STARTED This driver is already running on this device + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Udp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // Test for the Udp4ServiceBinding Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + // + // Test for the Ip4 Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiIp4ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + return Status; +} + + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Udp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + UDP4_SERVICE_DATA *Udp4Service; + + // + // Allocate Private Context Data Structure. + // + Udp4Service = AllocatePool (sizeof (UDP4_SERVICE_DATA)); + if (Udp4Service == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = Udp4CreateService (Udp4Service, This->DriverBindingHandle, ControllerHandle); + if (EFI_ERROR (Status)) { + FreePool (Udp4Service); + return Status; + } + + // + // Install the Udp4ServiceBindingProtocol on the ControllerHandle. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + &Udp4Service->ServiceBinding, + NULL + ); + if (EFI_ERROR (Status)) { + Udp4CleanService (Udp4Service); + FreePool (Udp4Service); + } + + return Status; +} + + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +Udp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_STATUS Status; + EFI_HANDLE NicHandle; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UDP4_SERVICE_DATA *Udp4Service; + UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context; + LIST_ENTRY *List; + + // + // Find the NicHandle where UDP4 ServiceBinding Protocol is installed. + // + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiIp4ProtocolGuid); + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + // + // Retrieve the UDP4 ServiceBinding Protocol. + // + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + NicHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + Udp4Service = UDP4_SERVICE_DATA_FROM_THIS (ServiceBinding); + if (NumberOfChildren != 0) { + // + // NumberOfChildren is not zero, destroy the children instances in ChildHandleBuffer. + // + List = &Udp4Service->ChildrenList; + Context.ServiceBinding = ServiceBinding; + Context.NumberOfChildren = NumberOfChildren; + Context.ChildHandleBuffer = ChildHandleBuffer; + Status = NetDestroyLinkList ( + List, + Udp4DestroyChildEntryInHandleBuffer, + &Context, + NULL + ); + } else { + gBS->UninstallMultipleProtocolInterfaces ( + NicHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + &Udp4Service->ServiceBinding, + NULL + ); + + Udp4CleanService (Udp4Service); + + if (gUdpControllerNameTable != NULL) { + FreeUnicodeStringTable (gUdpControllerNameTable); + gUdpControllerNameTable = NULL; + } + FreePool (Udp4Service); + } + + return Status; +} + + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +Udp4ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ) +{ + EFI_STATUS Status; + UDP4_SERVICE_DATA *Udp4Service; + UDP4_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + VOID *Ip4; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Udp4Service = UDP4_SERVICE_DATA_FROM_THIS (This); + + // + // Allocate the instance private data structure. + // + Instance = AllocateZeroPool (sizeof (UDP4_INSTANCE_DATA)); + if (Instance == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Udp4InitInstance (Udp4Service, Instance); + + // + // Add an IpInfo for this instance. + // + Instance->IpInfo = IpIoAddIp (Udp4Service->IpIo); + if (Instance->IpInfo == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + // + // Install the Udp4Protocol for this instance. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiUdp4ProtocolGuid, + &Instance->Udp4Proto, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Instance->ChildHandle = *ChildHandle; + + // + // Open the default Ip4 protocol in the IP_IO BY_CHILD. + // + Status = gBS->OpenProtocol ( + Udp4Service->IpIo->ChildHandle, + &gEfiIp4ProtocolGuid, + (VOID **) &Ip4, + gUdp4DriverBinding.DriverBindingHandle, + Instance->ChildHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Open this instance's Ip4 protocol in the IpInfo BY_CHILD. + // + Status = gBS->OpenProtocol ( + Instance->IpInfo->ChildHandle, + &gEfiIp4ProtocolGuid, + (VOID **) &Ip4, + gUdp4DriverBinding.DriverBindingHandle, + Instance->ChildHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Link this instance into the service context data and increase the ChildrenNumber. + // + InsertTailList (&Udp4Service->ChildrenList, &Instance->Link); + Udp4Service->ChildrenNumber++; + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; + +ON_ERROR: + + if (Instance->ChildHandle != NULL) { + gBS->UninstallMultipleProtocolInterfaces ( + Instance->ChildHandle, + &gEfiUdp4ProtocolGuid, + &Instance->Udp4Proto, + NULL + ); + } + + if (Instance->IpInfo != NULL) { + IpIoRemoveIp (Udp4Service->IpIo, Instance->IpInfo); + } + + Udp4CleanInstance (Instance); + + FreePool (Instance); + + return Status; +} + + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Udp4ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + EFI_STATUS Status; + UDP4_SERVICE_DATA *Udp4Service; + EFI_UDP4_PROTOCOL *Udp4Proto; + UDP4_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Udp4Service = UDP4_SERVICE_DATA_FROM_THIS (This); + + // + // Try to get the Udp4 protocol from the ChildHandle. + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiUdp4ProtocolGuid, + (VOID **) &Udp4Proto, + gUdp4DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Instance = UDP4_INSTANCE_DATA_FROM_THIS (Udp4Proto); + + if (Instance->InDestroy) { + return EFI_SUCCESS; + } + + // + // Use the Destroyed flag to avoid the re-entering of the following code. + // + Instance->InDestroy = TRUE; + + // + // Close the Ip4 protocol. + // + gBS->CloseProtocol ( + Udp4Service->IpIo->ChildHandle, + &gEfiIp4ProtocolGuid, + gUdp4DriverBinding.DriverBindingHandle, + Instance->ChildHandle + ); + // + // Close the Ip4 protocol on this instance's IpInfo. + // + gBS->CloseProtocol ( + Instance->IpInfo->ChildHandle, + &gEfiIp4ProtocolGuid, + gUdp4DriverBinding.DriverBindingHandle, + Instance->ChildHandle + ); + + // + // Uninstall the Udp4Protocol previously installed on the ChildHandle. + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiUdp4ProtocolGuid, + (VOID *) &Instance->Udp4Proto, + NULL + ); + if (EFI_ERROR (Status)) { + Instance->InDestroy = FALSE; + return Status; + } + + // + // Reset the configuration in case the instance's consumer forgets to do this. + // + Udp4Proto->Configure (Udp4Proto, NULL); + + // + // Remove the IpInfo this instance consumes. + // + IpIoRemoveIp (Udp4Service->IpIo, Instance->IpInfo); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Remove this instance from the service context data's ChildrenList. + // + RemoveEntryList (&Instance->Link); + Udp4Service->ChildrenNumber--; + + // + // Clean the instance. + // + Udp4CleanInstance (Instance); + + gBS->RestoreTPL (OldTpl); + + FreePool (Instance); + + return EFI_SUCCESS; +} + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + The entry point for Udp4 driver which installs the driver binding + and component name protocol on its ImageHandle. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +Udp4DriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + // + // Install the Udp4DriverBinding and Udp4ComponentName protocols. + // + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gUdp4DriverBinding, + ImageHandle, + &gUdp4ComponentName, + &gUdp4ComponentName2 + ); + if (!EFI_ERROR (Status)) { + // + // Initialize the UDP random port. + // + mUdp4RandomPort = (UINT16) (((UINT16) NetRandomInitSeed ()) % UDP4_PORT_KNOWN + UDP4_PORT_KNOWN); + } + + return Status; +} + diff --git a/NetworkPkg/Udp4Dxe/Udp4Driver.h b/NetworkPkg/Udp4Dxe/Udp4Driver.h new file mode 100644 index 000000000..4e9a0c735 --- /dev/null +++ b/NetworkPkg/Udp4Dxe/Udp4Driver.h @@ -0,0 +1,148 @@ +/** @file + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _UDP4_DRIVER_H_ +#define _UDP4_DRIVER_H_ + +#include +#include +#include +#include +#include + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device + @retval EFI_ALREADY_STARTED This driver is already running on this device + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Udp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +Udp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +Udp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child + @retval other The child handle was not created + +**/ +EFI_STATUS +EFIAPI +Udp4ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a protocol installed on it. + + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in] ChildHandle Handle of the child to destroy + + @retval EFI_SUCCES The protocol was removed from ChildHandle. + @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle + because its services are being used. + @retval other The child handle was not destroyed + +**/ +EFI_STATUS +EFIAPI +Udp4ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +#endif + diff --git a/NetworkPkg/Udp4Dxe/Udp4Dxe.inf b/NetworkPkg/Udp4Dxe/Udp4Dxe.inf new file mode 100644 index 000000000..6a71ed701 --- /dev/null +++ b/NetworkPkg/Udp4Dxe/Udp4Dxe.inf @@ -0,0 +1,65 @@ +## @file +# This module produces EFI UDP Protocol and EFI UDPv4 Service Binding Protocol. +# +# This module produces EFI UDP(User Datagram Protocol) Protocol upon EFI IPv4 +# Protocol, to provide basic UDPv4 I/O services. +# +# Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = Udp4Dxe + MODULE_UNI_FILE = Udp4Dxe.uni + FILE_GUID = 6d6963ab-906d-4a65-a7ca-bd40e5d6af2b + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = Udp4DriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = gUdp4DriverBinding +# COMPONENT_NAME = gUdp4ComponentName +# COMPONENT_NAME2 = gUdp4ComponentName2 +# + +[Sources] + Udp4Impl.h + Udp4Main.c + ComponentName.c + Udp4Impl.c + Udp4Driver.h + Udp4Driver.c + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + UefiLib + BaseLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiRuntimeServicesTableLib + DebugLib + IpIoLib + NetLib + DpcLib + +[Protocols] + gEfiUdp4ServiceBindingProtocolGuid ## BY_START + gEfiIp4ServiceBindingProtocolGuid ## TO_START + gEfiUdp4ProtocolGuid ## BY_START + gEfiIp4ProtocolGuid ## TO_START + +[UserExtensions.TianoCore."ExtraFiles"] + Udp4DxeExtra.uni diff --git a/NetworkPkg/Udp4Dxe/Udp4Dxe.uni b/NetworkPkg/Udp4Dxe/Udp4Dxe.uni new file mode 100644 index 000000000..1e3e4aafe --- /dev/null +++ b/NetworkPkg/Udp4Dxe/Udp4Dxe.uni @@ -0,0 +1,17 @@ +// /** @file +// This module produces EFI UDP Protocol and EFI UDPv4 Service Binding Protocol. +// +// This module produces EFI UDP(User Datagram Protocol) Protocol upon EFI IPv4 +// Protocol, to provide basic UDPv4 I/O services. +// +// Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Produces EFI UDP Protocol and EFI UDPv4 Service Binding Protocol" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces EFI UDP(User Datagram Protocol) Protocol upon EFI IPv4 Protocol to provide basic UDPv4 I/O services." + diff --git a/NetworkPkg/Udp4Dxe/Udp4DxeExtra.uni b/NetworkPkg/Udp4Dxe/Udp4DxeExtra.uni new file mode 100644 index 000000000..b048ce31a --- /dev/null +++ b/NetworkPkg/Udp4Dxe/Udp4DxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// Udp4Dxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"UDP v4 DXE Driver" + + diff --git a/NetworkPkg/Udp4Dxe/Udp4Impl.c b/NetworkPkg/Udp4Dxe/Udp4Impl.c new file mode 100644 index 000000000..fb1951fb1 --- /dev/null +++ b/NetworkPkg/Udp4Dxe/Udp4Impl.c @@ -0,0 +1,1908 @@ +/** @file + The implementation of the Udp4 protocol. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + + +#include "Udp4Impl.h" + +UINT16 mUdp4RandomPort; + +/** + This function checks and timeouts the I/O datagrams holding by the corresponding + service context. + + @param[in] Event The event this function registered to. + @param[in] Context The context data registered during the creation of + the Event. + +**/ +VOID +EFIAPI +Udp4CheckTimeout ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + This function finds the udp instance by the specified pair. + + @param[in] InstanceList Pointer to the head of the list linking the udp + instances. + @param[in] Address Pointer to the specified IPv4 address. + @param[in] Port The udp port number. + + @retval TRUE The specified pair is found. + @retval FALSE Otherwise. + +**/ +BOOLEAN +Udp4FindInstanceByPort ( + IN LIST_ENTRY *InstanceList, + IN EFI_IPv4_ADDRESS *Address, + IN UINT16 Port + ); + +/** + This function is the packet transmitting notify function registered to the IpIo + interface. It's called to signal the udp TxToken when IpIo layer completes the + transmitting of the udp datagram. + + @param[in] Status The completion status of the output udp datagram. + @param[in] Context Pointer to the context data. + @param[in] Sender Specify a pointer of EFI_IP4_PROTOCOL for sending. + @param[in] NotifyData Pointer to the notify data. + +**/ +VOID +EFIAPI +Udp4DgramSent ( + IN EFI_STATUS Status, + IN VOID *Context, + IN IP_IO_IP_PROTOCOL Sender, + IN VOID *NotifyData + ); + +/** + This function processes the received datagram passed up by the IpIo layer. + + @param[in] Status The status of this udp datagram. + @param[in] IcmpError The IcmpError code, only available when Status is + EFI_ICMP_ERROR. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA. + @param[in] Packet Pointer to the NET_BUF containing the received udp + datagram. + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp4DgramRcvd ( + IN EFI_STATUS Status, + IN UINT8 IcmpError, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Packet, + IN VOID *Context + ); + +/** + This function cancels the token specified by Arg in the Map. This is a callback + used by Udp4InstanceCancelToken(). + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the token to be cancelled, if NULL, + the token specified by Item is cancelled. + + @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token + is not the same as that in the Item if Arg is not + NULL. + @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is + cancelled. + +**/ +EFI_STATUS +EFIAPI +Udp4CancelTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ); + +/** + This function matches the received udp datagram with the Instance. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted + from the received udp datagram. + + @retval TRUE The udp datagram matches the receiving requirments of the + udp Instance. + @retval FALSE Otherwise. + +**/ +BOOLEAN +Udp4MatchDgram ( + IN UDP4_INSTANCE_DATA *Instance, + IN EFI_UDP4_SESSION_DATA *Udp4Session + ); + +/** + This function removes the Wrap specified by Context and release relevant resources. + + @param[in] Event The Event this notify function registered to. + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp4RecycleRxDataWrap ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + This function wraps the Packet and the RxData. + + @param[in] Instance Pointer to the instance context data. + @param[in] Packet Pointer to the buffer containing the received + datagram. + @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this + datagram. + + @return Pointer to the structure wrapping the RxData and the Packet. + +**/ +UDP4_RXDATA_WRAP * +Udp4WrapRxData ( + IN UDP4_INSTANCE_DATA *Instance, + IN NET_BUF *Packet, + IN EFI_UDP4_RECEIVE_DATA *RxData + ); + +/** + This function enqueues the received datagram into the instances' receiving queues. + + @param[in] Udp4Service Pointer to the udp service context data. + @param[in] Packet Pointer to the buffer containing the received + datagram. + @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this + datagram. + + @return The times this datagram is enqueued. + +**/ +UINTN +Udp4EnqueueDgram ( + IN UDP4_SERVICE_DATA *Udp4Service, + IN NET_BUF *Packet, + IN EFI_UDP4_RECEIVE_DATA *RxData + ); + +/** + This function delivers the datagrams enqueued in the instances. + + @param[in] Udp4Service Pointer to the udp service context data. + +**/ +VOID +Udp4DeliverDgram ( + IN UDP4_SERVICE_DATA *Udp4Service + ); + +/** + This function demultiplexes the received udp datagram to the appropriate instances. + + @param[in] Udp4Service Pointer to the udp service context data. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from + the received datagram. + @param[in] Packet Pointer to the buffer containing the received udp + datagram. + +**/ +VOID +Udp4Demultiplex ( + IN UDP4_SERVICE_DATA *Udp4Service, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Packet + ); + +/** + This function handles the received Icmp Error message and demultiplexes it to the + instance. + + @param[in] Udp4Service Pointer to the udp service context data. + @param[in] IcmpError The icmp error code. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted + from the received Icmp Error packet. + @param[in] Packet Pointer to the Icmp Error packet. + +**/ +VOID +Udp4IcmpHandler ( + IN UDP4_SERVICE_DATA *Udp4Service, + IN UINT8 IcmpError, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Packet + ); + +/** + This function builds and sends out a icmp port unreachable message. + + @param[in] IpIo Pointer to the IP_IO instance. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet + causes this icmp error message. + @param[in] Udp4Header Pointer to the udp header of the datagram causes + this icmp error message. + +**/ +VOID +Udp4SendPortUnreach ( + IN IP_IO *IpIo, + IN EFI_NET_SESSION_DATA *NetSession, + IN VOID *Udp4Header + ); + + +/** + Create the Udp service context data. + + @param[in, out] Udp4Service Pointer to the UDP4_SERVICE_DATA. + @param[in] ImageHandle The image handle of this udp4 driver. + @param[in] ControllerHandle The controller handle this udp4 driver binds on. + + @retval EFI_SUCCESS The udp4 service context data is created and + initialized. + @retval EFI_OUT_OF_RESOURCES Cannot allocate memory. + @retval other Other error occurs. + +**/ +EFI_STATUS +Udp4CreateService ( + IN OUT UDP4_SERVICE_DATA *Udp4Service, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle + ) +{ + EFI_STATUS Status; + IP_IO_OPEN_DATA OpenData; + EFI_IP4_CONFIG_DATA *Ip4ConfigData; + + ZeroMem (Udp4Service, sizeof (UDP4_SERVICE_DATA)); + + Udp4Service->Signature = UDP4_SERVICE_DATA_SIGNATURE; + Udp4Service->ServiceBinding = mUdp4ServiceBinding; + Udp4Service->ImageHandle = ImageHandle; + Udp4Service->ControllerHandle = ControllerHandle; + Udp4Service->ChildrenNumber = 0; + + InitializeListHead (&Udp4Service->ChildrenList); + + // + // Create the IpIo for this service context. + // + Udp4Service->IpIo = IpIoCreate (ImageHandle, ControllerHandle, IP_VERSION_4); + if (Udp4Service->IpIo == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Set the OpenData used to open the IpIo. + // + Ip4ConfigData = &OpenData.IpConfigData.Ip4CfgData; + CopyMem (Ip4ConfigData, &mIp4IoDefaultIpConfigData, sizeof (EFI_IP4_CONFIG_DATA)); + Ip4ConfigData->AcceptBroadcast = TRUE; + OpenData.RcvdContext = (VOID *) Udp4Service; + OpenData.SndContext = NULL; + OpenData.PktRcvdNotify = Udp4DgramRcvd; + OpenData.PktSentNotify = Udp4DgramSent; + + // + // Configure and start the IpIo. + // + Status = IpIoOpen (Udp4Service->IpIo, &OpenData); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create the event for Udp timeout checking. + // + Status = gBS->CreateEvent ( + EVT_TIMER | EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + Udp4CheckTimeout, + Udp4Service, + &Udp4Service->TimeoutEvent + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Start the timeout timer event. + // + Status = gBS->SetTimer ( + Udp4Service->TimeoutEvent, + TimerPeriodic, + UDP4_TIMEOUT_INTERVAL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + + if (Udp4Service->TimeoutEvent != NULL) { + gBS->CloseEvent (Udp4Service->TimeoutEvent); + } + + IpIoDestroy (Udp4Service->IpIo); + + return Status; +} + + +/** + Clean the Udp service context data. + + @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA. + +**/ +VOID +Udp4CleanService ( + IN UDP4_SERVICE_DATA *Udp4Service + ) +{ + // + // Cancel the TimeoutEvent timer. + // + gBS->SetTimer (Udp4Service->TimeoutEvent, TimerCancel, 0); + + // + // Close the TimeoutEvent timer. + // + gBS->CloseEvent (Udp4Service->TimeoutEvent); + + // + // Destroy the IpIo. + // + IpIoDestroy (Udp4Service->IpIo); +} + + +/** + This function checks and timeouts the I/O datagrams holding by the corresponding + service context. + + @param[in] Event The event this function registered to. + @param[in] Context The context data registered during the creation of + the Event. + +**/ +VOID +EFIAPI +Udp4CheckTimeout ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + UDP4_SERVICE_DATA *Udp4Service; + LIST_ENTRY *Entry; + UDP4_INSTANCE_DATA *Instance; + LIST_ENTRY *WrapEntry; + LIST_ENTRY *NextEntry; + UDP4_RXDATA_WRAP *Wrap; + + Udp4Service = (UDP4_SERVICE_DATA *) Context; + NET_CHECK_SIGNATURE (Udp4Service, UDP4_SERVICE_DATA_SIGNATURE); + + NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) { + // + // Iterate all the instances belonging to this service context. + // + Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link); + NET_CHECK_SIGNATURE (Instance, UDP4_INSTANCE_DATA_SIGNATURE); + + if (!Instance->Configured || (Instance->ConfigData.ReceiveTimeout == 0)) { + // + // Skip this instance if it's not configured or no receive timeout. + // + continue; + } + + NET_LIST_FOR_EACH_SAFE (WrapEntry, NextEntry, &Instance->RcvdDgramQue) { + // + // Iterate all the rxdatas belonging to this udp instance. + // + Wrap = NET_LIST_USER_STRUCT (WrapEntry, UDP4_RXDATA_WRAP, Link); + + // + // TimeoutTick unit is microsecond, MNP_TIMEOUT_CHECK_INTERVAL unit is 100ns. + // + if (Wrap->TimeoutTick < (UDP4_TIMEOUT_INTERVAL / 10)) { + // + // Remove this RxData if it timeouts. + // + Udp4RecycleRxDataWrap (NULL, (VOID *) Wrap); + } else { + Wrap->TimeoutTick -= (UDP4_TIMEOUT_INTERVAL / 10); + } + } + } +} + + +/** + This function intializes the new created udp instance. + + @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA. + @param[in, out] Instance Pointer to the un-initialized UDP4_INSTANCE_DATA. + +**/ +VOID +Udp4InitInstance ( + IN UDP4_SERVICE_DATA *Udp4Service, + IN OUT UDP4_INSTANCE_DATA *Instance + ) +{ + // + // Set the signature. + // + Instance->Signature = UDP4_INSTANCE_DATA_SIGNATURE; + + // + // Init the lists. + // + InitializeListHead (&Instance->Link); + InitializeListHead (&Instance->RcvdDgramQue); + InitializeListHead (&Instance->DeliveredDgramQue); + + // + // Init the NET_MAPs. + // + NetMapInit (&Instance->TxTokens); + NetMapInit (&Instance->RxTokens); + NetMapInit (&Instance->McastIps); + + // + // Save the pointer to the UDP4_SERVICE_DATA, and initialize other members. + // + Instance->Udp4Service = Udp4Service; + CopyMem (&Instance->Udp4Proto, &mUdp4Protocol, sizeof (Instance->Udp4Proto)); + Instance->IcmpError = EFI_SUCCESS; + Instance->Configured = FALSE; + Instance->IsNoMapping = FALSE; + Instance->InDestroy = FALSE; +} + + +/** + This function cleans the udp instance. + + @param[in] Instance Pointer to the UDP4_INSTANCE_DATA to clean. + +**/ +VOID +Udp4CleanInstance ( + IN UDP4_INSTANCE_DATA *Instance + ) +{ + NetMapClean (&Instance->McastIps); + NetMapClean (&Instance->RxTokens); + NetMapClean (&Instance->TxTokens); +} + + +/** + This function finds the udp instance by the specified pair. + + @param[in] InstanceList Pointer to the head of the list linking the udp + instances. + @param[in] Address Pointer to the specified IPv4 address. + @param[in] Port The udp port number. + + @retval TRUE The specified pair is found. + @retval FALSE Otherwise. + +**/ +BOOLEAN +Udp4FindInstanceByPort ( + IN LIST_ENTRY *InstanceList, + IN EFI_IPv4_ADDRESS *Address, + IN UINT16 Port + ) +{ + LIST_ENTRY *Entry; + UDP4_INSTANCE_DATA *Instance; + EFI_UDP4_CONFIG_DATA *ConfigData; + + NET_LIST_FOR_EACH (Entry, InstanceList) { + // + // Iterate all the udp instances. + // + Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link); + ConfigData = &Instance->ConfigData; + + if (!Instance->Configured || ConfigData->AcceptAnyPort) { + // + // If the instance is not configured or the configdata of the instance indicates + // this instance accepts any port, skip it. + // + continue; + } + + if (EFI_IP4_EQUAL (&ConfigData->StationAddress, Address) && + (ConfigData->StationPort == Port)) { + // + // if both the address and the port are the same, return TRUE. + // + return TRUE; + } + } + + // + // return FALSE when matching fails. + // + return FALSE; +} + + +/** + This function tries to bind the udp instance according to the configured port + allocation strategy. + + @param[in] InstanceList Pointer to the head of the list linking the udp + instances. + @param[in, out] ConfigData Pointer to the ConfigData of the instance to be + bound. ConfigData->StationPort will be assigned + with an available port value on success. + + @retval EFI_SUCCESS The bound operation is completed successfully. + @retval EFI_ACCESS_DENIED The specified by the ConfigData is + already used by other instance. + @retval EFI_OUT_OF_RESOURCES No available port resources. + +**/ +EFI_STATUS +Udp4Bind ( + IN LIST_ENTRY *InstanceList, + IN OUT EFI_UDP4_CONFIG_DATA *ConfigData + ) +{ + EFI_IPv4_ADDRESS *StationAddress; + UINT16 StartPort; + + if (ConfigData->AcceptAnyPort) { + return EFI_SUCCESS; + } + + StationAddress = &ConfigData->StationAddress; + + if (ConfigData->StationPort != 0) { + + if (!ConfigData->AllowDuplicatePort && + Udp4FindInstanceByPort (InstanceList, StationAddress, ConfigData->StationPort)) { + // + // Do not allow duplicate port and the port is already used by other instance. + // + return EFI_ACCESS_DENIED; + } + } else { + // + // select a random port for this instance; + // + + if (ConfigData->AllowDuplicatePort) { + // + // Just pick up the random port if the instance allows duplicate port. + // + ConfigData->StationPort = mUdp4RandomPort; + } else { + + StartPort = mUdp4RandomPort; + + while (Udp4FindInstanceByPort(InstanceList, StationAddress, mUdp4RandomPort)) { + + mUdp4RandomPort++; + if (mUdp4RandomPort == 0) { + mUdp4RandomPort = UDP4_PORT_KNOWN; + } + + if (mUdp4RandomPort == StartPort) { + // + // No available port. + // + return EFI_OUT_OF_RESOURCES; + } + } + + ConfigData->StationPort = mUdp4RandomPort; + } + + mUdp4RandomPort++; + if (mUdp4RandomPort == 0) { + mUdp4RandomPort = UDP4_PORT_KNOWN; + } + } + + return EFI_SUCCESS; +} + + +/** + This function is used to check whether the NewConfigData has any un-reconfigurable + parameters changed compared to the OldConfigData. + + @param[in] OldConfigData Pointer to the current ConfigData the udp instance + uses. + @param[in] NewConfigData Pointer to the new ConfigData. + + @retval TRUE The instance is reconfigurable. + @retval FALSE Otherwise. + +**/ +BOOLEAN +Udp4IsReconfigurable ( + IN EFI_UDP4_CONFIG_DATA *OldConfigData, + IN EFI_UDP4_CONFIG_DATA *NewConfigData + ) +{ + if ((NewConfigData->AcceptAnyPort != OldConfigData->AcceptAnyPort) || + (NewConfigData->AcceptBroadcast != OldConfigData->AcceptBroadcast) || + (NewConfigData->AcceptPromiscuous != OldConfigData->AcceptPromiscuous) || + (NewConfigData->AllowDuplicatePort != OldConfigData->AllowDuplicatePort) + ) { + // + // The receiving filter parameters cannot be changed. + // + return FALSE; + } + + if ((!NewConfigData->AcceptAnyPort) && + (NewConfigData->StationPort != OldConfigData->StationPort) + ) { + // + // The port is not changeable. + // + return FALSE; + } + + if (!NewConfigData->AcceptPromiscuous) { + + if (NewConfigData->UseDefaultAddress != OldConfigData->UseDefaultAddress) { + // + // The NewConfigData differs to the old one on the UseDefaultAddress. + // + return FALSE; + } + + if (!NewConfigData->UseDefaultAddress && + (!EFI_IP4_EQUAL (&NewConfigData->StationAddress, &OldConfigData->StationAddress) || + !EFI_IP4_EQUAL (&NewConfigData->SubnetMask, &OldConfigData->SubnetMask)) + ) { + // + // If the instance doesn't use the default address, and the new address or + // new subnet mask is different from the old values. + // + return FALSE; + } + } + + if (!EFI_IP4_EQUAL (&NewConfigData->RemoteAddress, &OldConfigData->RemoteAddress)) { + // + // The remoteaddress is not the same. + // + return FALSE; + } + + if (!EFI_IP4_EQUAL (&NewConfigData->RemoteAddress, &mZeroIp4Addr) && + NewConfigData->RemotePort != OldConfigData->RemotePort + ) { + // + // The RemotePort differs if it's designated in the configdata. + // + return FALSE; + } + + // + // All checks pass, return TRUE. + // + return TRUE; +} + + +/** + This function builds the Ip4 configdata from the Udp4ConfigData. + + @param[in] Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA. + @param[in, out] Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA. + +**/ +VOID +Udp4BuildIp4ConfigData ( + IN EFI_UDP4_CONFIG_DATA *Udp4ConfigData, + IN OUT EFI_IP4_CONFIG_DATA *Ip4ConfigData + ) +{ + CopyMem (Ip4ConfigData, &mIp4IoDefaultIpConfigData, sizeof (*Ip4ConfigData)); + + Ip4ConfigData->DefaultProtocol = EFI_IP_PROTO_UDP; + Ip4ConfigData->AcceptBroadcast = Udp4ConfigData->AcceptBroadcast; + Ip4ConfigData->AcceptPromiscuous = Udp4ConfigData->AcceptPromiscuous; + Ip4ConfigData->UseDefaultAddress = Udp4ConfigData->UseDefaultAddress; + CopyMem (&Ip4ConfigData->StationAddress, &Udp4ConfigData->StationAddress, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Ip4ConfigData->SubnetMask, &Udp4ConfigData->SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + + // + // use the -1 magic number to disable the receiving process of the ip instance. + // + Ip4ConfigData->ReceiveTimeout = (UINT32) (-1); +} + + +/** + This function validates the TxToken, it returns the error code according to the spec. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] TxToken Pointer to the token to be checked. + + @retval EFI_SUCCESS The TxToken is valid. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is + NULL. Token is NULL. Token.Event is NULL. + Token.Packet.TxData is NULL. + Token.Packet.TxData.FragmentCount is zero. + Token.Packet.TxData.DataLength is not equal to the + sum of fragment lengths. One or more of the + Token.Packet.TxData.FragmentTable[]. + FragmentLength fields is zero. One or more of the + Token.Packet.TxData.FragmentTable[]. + FragmentBuffer fields is NULL. + Token.Packet.TxData. GatewayAddress is not a + unicast IPv4 address if it is not NULL. One or + more IPv4 addresses in Token.Packet.TxData. + UdpSessionData are not valid unicast IPv4 + addresses if the UdpSessionData is not NULL. + @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP + packet size. + +**/ +EFI_STATUS +Udp4ValidateTxToken ( + IN UDP4_INSTANCE_DATA *Instance, + IN EFI_UDP4_COMPLETION_TOKEN *TxToken + ) +{ + EFI_UDP4_TRANSMIT_DATA *TxData; + UINT32 Index; + UINT32 TotalLen; + EFI_UDP4_CONFIG_DATA *ConfigData; + EFI_UDP4_SESSION_DATA *UdpSessionData; + IP4_ADDR SourceAddress; + IP4_ADDR GatewayAddress; + + if (TxToken->Event == NULL) { + return EFI_INVALID_PARAMETER; + } + + TxData = TxToken->Packet.TxData; + + if ((TxData == NULL) || (TxData->FragmentCount == 0)) { + return EFI_INVALID_PARAMETER; + } + + TotalLen = 0; + for (Index = 0; Index < TxData->FragmentCount; Index++) { + + if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) || + (TxData->FragmentTable[Index].FragmentLength == 0)) { + // + // if the FragmentBuffer is NULL or the FragmentLeng is zero. + // + return EFI_INVALID_PARAMETER; + } + + TotalLen += TxData->FragmentTable[Index].FragmentLength; + } + + if (TotalLen != TxData->DataLength) { + // + // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the + // DataLength. + // + return EFI_INVALID_PARAMETER; + } + + if (TxData->GatewayAddress != NULL) { + CopyMem (&GatewayAddress, TxData->GatewayAddress, sizeof (IP4_ADDR)); + + if (!Instance->ConfigData.UseDefaultAddress && + (EFI_NTOHL(Instance->ConfigData.SubnetMask) != 0) && + !NetIp4IsUnicast (NTOHL (GatewayAddress), EFI_NTOHL(Instance->ConfigData.SubnetMask))) { + // + // The specified GatewayAddress is not a unicast IPv4 address while it's not 0. + // + return EFI_INVALID_PARAMETER; + } + } + + ConfigData = &Instance->ConfigData; + UdpSessionData = TxData->UdpSessionData; + + if (UdpSessionData != NULL) { + + CopyMem (&SourceAddress, &UdpSessionData->SourceAddress, sizeof (IP4_ADDR)); + + if ((SourceAddress != 0) && + !Instance->ConfigData.UseDefaultAddress && + (EFI_NTOHL(Instance->ConfigData.SubnetMask) != 0) && + !NetIp4IsUnicast (HTONL (SourceAddress), EFI_NTOHL(Instance->ConfigData.SubnetMask))) { + // + // Check whether SourceAddress is a valid IPv4 address in case it's not zero. + // The configured station address is used if SourceAddress is zero. + // + return EFI_INVALID_PARAMETER; + } + + if ((UdpSessionData->DestinationPort == 0) && (ConfigData->RemotePort == 0)) { + // + // Ambiguous, no avalaible DestinationPort for this token. + // + return EFI_INVALID_PARAMETER; + } + + if (EFI_IP4_EQUAL (&UdpSessionData->DestinationAddress, &mZeroIp4Addr)) { + // + // The DestinationAddress specified in the UdpSessionData is 0. + // + return EFI_INVALID_PARAMETER; + } + } else if (EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &mZeroIp4Addr)) { + // + // the configured RemoteAddress is all zero, and the user doens't override the + // destination address. + // + return EFI_INVALID_PARAMETER; + } + + if (TxData->DataLength > UDP4_MAX_DATA_SIZE) { + return EFI_BAD_BUFFER_SIZE; + } + + return EFI_SUCCESS; +} + + +/** + This function checks whether the specified Token duplicates with the one in the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to + the Token. + @param[in] Context Pointer to the Token to be checked. + + @retval EFI_SUCCESS The Token specified by Context differs from the + one in the Item. + @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item. + +**/ +EFI_STATUS +EFIAPI +Udp4TokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + EFI_UDP4_COMPLETION_TOKEN *Token; + EFI_UDP4_COMPLETION_TOKEN *TokenInItem; + + Token = (EFI_UDP4_COMPLETION_TOKEN*) Context; + TokenInItem = (EFI_UDP4_COMPLETION_TOKEN*) Item->Key; + + if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) { + // + // The Token duplicates with the TokenInItem in case either the two pointers are the + // same or the Events of these two tokens are the same. + // + return EFI_ACCESS_DENIED; + } + + return EFI_SUCCESS; +} + + +/** + This function calculates the checksum for the Packet, utilizing the pre-calculated + pseudo HeadSum to reduce some overhead. + + @param[in] Packet Pointer to the NET_BUF contains the udp datagram. + @param[in] HeadSum Checksum of the pseudo header execpt the length + field. + + @retval The 16-bit checksum of this udp datagram. + +**/ +UINT16 +Udp4Checksum ( + IN NET_BUF *Packet, + IN UINT16 HeadSum + ) +{ + UINT16 Checksum; + + Checksum = NetbufChecksum (Packet); + Checksum = NetAddChecksum (Checksum, HeadSum); + + Checksum = NetAddChecksum (Checksum, HTONS ((UINT16) Packet->TotalSize)); + + return (UINT16) ~Checksum; +} + + +/** + This function removes the specified Token from the TokenMap. + + @param[in, out] TokenMap Pointer to the NET_MAP containing the tokens. + @param[in] Token Pointer to the Token to be removed. + + @retval EFI_SUCCESS The specified Token is removed from the TokenMap. + @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap. + +**/ +EFI_STATUS +Udp4RemoveToken ( + IN OUT NET_MAP *TokenMap, + IN EFI_UDP4_COMPLETION_TOKEN *Token + ) +{ + NET_MAP_ITEM *Item; + + // + // Find the Token first. + // + Item = NetMapFindKey (TokenMap, (VOID *) Token); + + if (Item != NULL) { + // + // Remove the token if it's found in the map. + // + NetMapRemoveItem (TokenMap, Item, NULL); + + return EFI_SUCCESS; + } + + return EFI_NOT_FOUND; +} + + +/** + This function is the packet transmitting notify function registered to the IpIo + interface. It's called to signal the udp TxToken when IpIo layer completes the + transmitting of the udp datagram. + + @param[in] Status The completion status of the output udp datagram. + @param[in] Context Pointer to the context data. + @param[in] Sender Specify a pointer of EFI_IP4_PROTOCOL for sending. + @param[in] NotifyData Pointer to the notify data. + +**/ +VOID +EFIAPI +Udp4DgramSent ( + IN EFI_STATUS Status, + IN VOID *Context, + IN IP_IO_IP_PROTOCOL Sender, + IN VOID *NotifyData + ) +{ + UDP4_INSTANCE_DATA *Instance; + EFI_UDP4_COMPLETION_TOKEN *Token; + + Instance = (UDP4_INSTANCE_DATA *) Context; + Token = (EFI_UDP4_COMPLETION_TOKEN *) NotifyData; + + if (Udp4RemoveToken (&Instance->TxTokens, Token) == EFI_SUCCESS) { + // + // The token may be cancelled. Only signal it if the remove operation succeeds. + // + Token->Status = Status; + gBS->SignalEvent (Token->Event); + DispatchDpc (); + } +} + + +/** + This function processes the received datagram passed up by the IpIo layer. + + @param[in] Status The status of this udp datagram. + @param[in] IcmpError The IcmpError code, only available when Status is + EFI_ICMP_ERROR. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA. + @param[in] Packet Pointer to the NET_BUF containing the received udp + datagram. + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp4DgramRcvd ( + IN EFI_STATUS Status, + IN UINT8 IcmpError, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Packet, + IN VOID *Context + ) +{ + NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE); + + // + // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR. + // + if (Status == EFI_SUCCESS) { + // + // Demultiplex the received datagram. + // + Udp4Demultiplex ((UDP4_SERVICE_DATA *) Context, NetSession, Packet); + } else { + // + // Handle the ICMP_ERROR packet. + // + Udp4IcmpHandler ((UDP4_SERVICE_DATA *) Context, IcmpError, NetSession, Packet); + } + + // + // Dispatch the DPC queued by the NotifyFunction of the rx token's events + // which are signaled with received data. + // + DispatchDpc (); +} + + +/** + This function removes the multicast group specified by Arg from the Map. + + @param[in, out] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg, it's the pointer to a + multicast IPv4 Address. + + @retval EFI_SUCCESS The multicast address is removed. + @retval EFI_ABORTED The specified multicast address is removed and the + Arg is not NULL. + +**/ +EFI_STATUS +EFIAPI +Udp4LeaveGroup ( + IN OUT NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ) +{ + EFI_IPv4_ADDRESS *McastIp; + + McastIp = Arg; + + if ((McastIp != NULL) && (!EFI_IP4_EQUAL (McastIp, &(Item->Key)))) { + // + // McastIp is not NULL and the multicast address contained in the Item + // is not the same as McastIp. + // + return EFI_SUCCESS; + } + + // + // Remove this Item. + // + NetMapRemoveItem (Map, Item, NULL); + + if (McastIp != NULL) { + // + // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration. + // + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + + +/** + This function cancels the token specified by Arg in the Map. This is a callback + used by Udp4InstanceCancelToken(). + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the token to be cancelled, if NULL, + the token specified by Item is cancelled. + + @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token + is not the same as that in the Item if Arg is not + NULL. + @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is + cancelled. + +**/ +EFI_STATUS +EFIAPI +Udp4CancelTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ) +{ + EFI_UDP4_COMPLETION_TOKEN *TokenToCancel; + NET_BUF *Packet; + IP_IO *IpIo; + + if ((Arg != NULL) && (Item->Key != Arg)) { + return EFI_SUCCESS; + } + + if (Item->Value != NULL) { + // + // If the token is a transmit token, the corresponding Packet is recorded in + // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken + // will invoke Udp4DgramSent, the token will be signaled and this Item will + // be removed from the Map there. + // + Packet = (NET_BUF *) (Item->Value); + IpIo = (IP_IO *) (*((UINTN *) &Packet->ProtoData[0])); + + IpIoCancelTxToken (IpIo, Packet); + } else { + // + // The token is a receive token. Abort it and remove it from the Map. + // + TokenToCancel = (EFI_UDP4_COMPLETION_TOKEN *) Item->Key; + NetMapRemoveItem (Map, Item, NULL); + + TokenToCancel->Status = EFI_ABORTED; + gBS->SignalEvent (TokenToCancel->Event); + } + + if (Arg != NULL) { + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + + +/** + This function removes all the Wrap datas in the RcvdDgramQue. + + @param[in] Instance Pointer to the udp instance context data. + +**/ +VOID +Udp4FlushRcvdDgram ( + IN UDP4_INSTANCE_DATA *Instance + ) +{ + UDP4_RXDATA_WRAP *Wrap; + + while (!IsListEmpty (&Instance->RcvdDgramQue)) { + // + // Iterate all the Wraps in the RcvdDgramQue. + // + Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP4_RXDATA_WRAP, Link); + + // + // The Wrap will be removed from the RcvdDgramQue by this function call. + // + Udp4RecycleRxDataWrap (NULL, (VOID *) Wrap); + } +} + + + +/** + Cancel Udp4 tokens from the Udp4 instance. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] Token Pointer to the token to be canceled, if NULL, all + tokens in this instance will be cancelled. + + @retval EFI_SUCCESS The Token is cancelled. + @retval EFI_NOT_FOUND The Token is not found. + +**/ +EFI_STATUS +Udp4InstanceCancelToken ( + IN UDP4_INSTANCE_DATA *Instance, + IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // Cancel this token from the TxTokens map. + // + Status = NetMapIterate (&Instance->TxTokens, Udp4CancelTokens, Token); + + if ((Token != NULL) && (Status == EFI_ABORTED)) { + // + // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from + // the TxTokens, just return success. + // + return EFI_SUCCESS; + } + + // + // Try to cancel this token from the RxTokens map in condition either the Token + // is NULL or the specified Token is not in TxTokens. + // + Status = NetMapIterate (&Instance->RxTokens, Udp4CancelTokens, Token); + + if ((Token != NULL) && (Status == EFI_SUCCESS)) { + // + // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the + // TxTokens nor the RxTokens, or say, it's not found. + // + return EFI_NOT_FOUND; + } + + ASSERT ((Token != NULL) || ((0 == NetMapGetCount (&Instance->TxTokens)) + && (0 == NetMapGetCount (&Instance->RxTokens)))); + + return EFI_SUCCESS; +} + + +/** + This function matches the received udp datagram with the Instance. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted + from the received udp datagram. + + @retval TRUE The udp datagram matches the receiving requirments of the + udp Instance. + @retval FALSE Otherwise. + +**/ +BOOLEAN +Udp4MatchDgram ( + IN UDP4_INSTANCE_DATA *Instance, + IN EFI_UDP4_SESSION_DATA *Udp4Session + ) +{ + EFI_UDP4_CONFIG_DATA *ConfigData; + IP4_ADDR Destination; + + ConfigData = &Instance->ConfigData; + + if (ConfigData->AcceptPromiscuous) { + // + // Always matches if this instance is in the promiscuous state. + // + return TRUE; + } + + if ((!ConfigData->AcceptAnyPort && (Udp4Session->DestinationPort != ConfigData->StationPort)) || + ((ConfigData->RemotePort != 0) && (Udp4Session->SourcePort != ConfigData->RemotePort)) + ) { + // + // The local port or the remote port doesn't match. + // + return FALSE; + } + + if (!EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &mZeroIp4Addr) && + !EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &Udp4Session->SourceAddress) + ) { + // + // This datagram doesn't come from the instance's specified sender. + // + return FALSE; + } + + if (EFI_IP4_EQUAL (&ConfigData->StationAddress, &mZeroIp4Addr) || + EFI_IP4_EQUAL (&Udp4Session->DestinationAddress, &ConfigData->StationAddress) + ) { + // + // The instance is configured to receive datagrams destined to any station IP or + // the destination address of this datagram matches the configured station IP. + // + return TRUE; + } + + CopyMem (&Destination, &Udp4Session->DestinationAddress, sizeof (IP4_ADDR)); + + if (IP4_IS_LOCAL_BROADCAST (Destination) && ConfigData->AcceptBroadcast) { + // + // The instance is configured to receive broadcast and this is a broadcast packet. + // + return TRUE; + } + + if (IP4_IS_MULTICAST (NTOHL (Destination)) && + NetMapFindKey (&Instance->McastIps, (VOID *) (UINTN) Destination) != NULL + ) { + // + // It's a multicast packet and the multicast address is accepted by this instance. + // + return TRUE; + } + + return FALSE; +} + + +/** + This function removes the Wrap specified by Context and release relevant resources. + + @param[in] Event The Event this notify function registered to. + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp4RecycleRxDataWrap ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + UDP4_RXDATA_WRAP *Wrap; + + Wrap = (UDP4_RXDATA_WRAP *) Context; + + // + // Remove the Wrap from the list it belongs to. + // + RemoveEntryList (&Wrap->Link); + + // + // Free the Packet associated with this Wrap. + // + NetbufFree (Wrap->Packet); + + // + // Close the event. + // + gBS->CloseEvent (Wrap->RxData.RecycleSignal); + + FreePool (Wrap); +} + + +/** + This function wraps the Packet and the RxData. + + @param[in] Instance Pointer to the instance context data. + @param[in] Packet Pointer to the buffer containing the received + datagram. + @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this + datagram. + + @return Pointer to the structure wrapping the RxData and the Packet. + +**/ +UDP4_RXDATA_WRAP * +Udp4WrapRxData ( + IN UDP4_INSTANCE_DATA *Instance, + IN NET_BUF *Packet, + IN EFI_UDP4_RECEIVE_DATA *RxData + ) +{ + EFI_STATUS Status; + UDP4_RXDATA_WRAP *Wrap; + + // + // Allocate buffer for the Wrap. + // + Wrap = AllocatePool (sizeof (UDP4_RXDATA_WRAP) + + (Packet->BlockOpNum - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA)); + if (Wrap == NULL) { + return NULL; + } + + InitializeListHead (&Wrap->Link); + + CopyMem (&Wrap->RxData, RxData, sizeof (Wrap->RxData)); + + // + // Create the Recycle event. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Udp4RecycleRxDataWrap, + Wrap, + &Wrap->RxData.RecycleSignal + ); + if (EFI_ERROR (Status)) { + FreePool (Wrap); + return NULL; + } + + Wrap->Packet = Packet; + Wrap->TimeoutTick = Instance->ConfigData.ReceiveTimeout; + + return Wrap; +} + + +/** + This function enqueues the received datagram into the instances' receiving queues. + + @param[in] Udp4Service Pointer to the udp service context data. + @param[in] Packet Pointer to the buffer containing the received + datagram. + @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this + datagram. + + @return The times this datagram is enqueued. + +**/ +UINTN +Udp4EnqueueDgram ( + IN UDP4_SERVICE_DATA *Udp4Service, + IN NET_BUF *Packet, + IN EFI_UDP4_RECEIVE_DATA *RxData + ) +{ + LIST_ENTRY *Entry; + UDP4_INSTANCE_DATA *Instance; + UDP4_RXDATA_WRAP *Wrap; + UINTN Enqueued; + + Enqueued = 0; + + NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) { + // + // Iterate the instances. + // + Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link); + + if (!Instance->Configured) { + continue; + } + + if (Udp4MatchDgram (Instance, &RxData->UdpSession)) { + // + // Wrap the RxData and put this Wrap into the instances RcvdDgramQue. + // + Wrap = Udp4WrapRxData (Instance, Packet, RxData); + if (Wrap == NULL) { + continue; + } + + NET_GET_REF (Packet); + + InsertTailList (&Instance->RcvdDgramQue, &Wrap->Link); + + Enqueued++; + } + } + + return Enqueued; +} + + +/** + This function delivers the received datagrams for the specified instance. + + @param[in] Instance Pointer to the instance context data. + +**/ +VOID +Udp4InstanceDeliverDgram ( + IN UDP4_INSTANCE_DATA *Instance + ) +{ + UDP4_RXDATA_WRAP *Wrap; + EFI_UDP4_COMPLETION_TOKEN *Token; + NET_BUF *Dup; + EFI_UDP4_RECEIVE_DATA *RxData; + EFI_TPL OldTpl; + + if (!IsListEmpty (&Instance->RcvdDgramQue) && + !NetMapIsEmpty (&Instance->RxTokens)) { + + Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP4_RXDATA_WRAP, Link); + + if (NET_BUF_SHARED (Wrap->Packet)) { + // + // Duplicate the Packet if it is shared between instances. + // + Dup = NetbufDuplicate (Wrap->Packet, NULL, 0); + if (Dup == NULL) { + return; + } + + NetbufFree (Wrap->Packet); + + Wrap->Packet = Dup; + } + + NetListRemoveHead (&Instance->RcvdDgramQue); + + Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL); + + // + // Build the FragmentTable and set the FragmentCount in RxData. + // + RxData = &Wrap->RxData; + RxData->FragmentCount = Wrap->Packet->BlockOpNum; + + NetbufBuildExt ( + Wrap->Packet, + (NET_FRAGMENT *) RxData->FragmentTable, + &RxData->FragmentCount + ); + + Token->Status = EFI_SUCCESS; + Token->Packet.RxData = &Wrap->RxData; + + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + InsertTailList (&Instance->DeliveredDgramQue, &Wrap->Link); + gBS->RestoreTPL (OldTpl); + + gBS->SignalEvent (Token->Event); + } +} + + +/** + This function delivers the datagrams enqueued in the instances. + + @param[in] Udp4Service Pointer to the udp service context data. + +**/ +VOID +Udp4DeliverDgram ( + IN UDP4_SERVICE_DATA *Udp4Service + ) +{ + LIST_ENTRY *Entry; + UDP4_INSTANCE_DATA *Instance; + + NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) { + // + // Iterate the instances. + // + Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link); + + if (!Instance->Configured) { + continue; + } + + // + // Deliver the datagrams of this instance. + // + Udp4InstanceDeliverDgram (Instance); + } +} + + +/** + This function demultiplexes the received udp datagram to the appropriate instances. + + @param[in] Udp4Service Pointer to the udp service context data. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from + the received datagram. + @param[in] Packet Pointer to the buffer containing the received udp + datagram. + +**/ +VOID +Udp4Demultiplex ( + IN UDP4_SERVICE_DATA *Udp4Service, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Packet + ) +{ + EFI_UDP_HEADER *Udp4Header; + UINT16 HeadSum; + EFI_UDP4_RECEIVE_DATA RxData; + EFI_UDP4_SESSION_DATA *Udp4Session; + UINTN Enqueued; + + if (Packet->TotalSize < sizeof (EFI_UDP_HEADER)) { + NetbufFree (Packet); + return; + } + + // + // Get the datagram header from the packet buffer. + // + Udp4Header = (EFI_UDP_HEADER *) NetbufGetByte (Packet, 0, NULL); + ASSERT (Udp4Header != NULL); + + if (Udp4Header->Checksum != 0) { + // + // check the checksum. + // + HeadSum = NetPseudoHeadChecksum ( + NetSession->Source.Addr[0], + NetSession->Dest.Addr[0], + EFI_IP_PROTO_UDP, + 0 + ); + + if (Udp4Checksum (Packet, HeadSum) != 0) { + // + // Wrong checksum. + // + NetbufFree (Packet); + return; + } + } + + Udp4Session = &RxData.UdpSession; + Udp4Session->SourcePort = NTOHS (Udp4Header->SrcPort); + Udp4Session->DestinationPort = NTOHS (Udp4Header->DstPort); + + CopyMem (&Udp4Session->SourceAddress, &NetSession->Source, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Udp4Session->DestinationAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS)); + + // + // Trim the UDP header. + // + NetbufTrim (Packet, UDP4_HEADER_SIZE, TRUE); + + RxData.DataLength = (UINT32) Packet->TotalSize; + + // + // Try to enqueue this datagram into the instances. + // + Enqueued = Udp4EnqueueDgram (Udp4Service, Packet, &RxData); + + if (Enqueued == 0) { + // + // Send the port unreachable ICMP packet before we free this NET_BUF + // + Udp4SendPortUnreach (Udp4Service->IpIo, NetSession, Udp4Header); + } + + // + // Try to free the packet before deliver it. + // + NetbufFree (Packet); + + if (Enqueued > 0) { + // + // Deliver the datagram. + // + Udp4DeliverDgram (Udp4Service); + } +} + + +/** + This function builds and sends out a icmp port unreachable message. + + @param[in] IpIo Pointer to the IP_IO instance. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet + causes this icmp error message. + @param[in] Udp4Header Pointer to the udp header of the datagram causes + this icmp error message. + +**/ +VOID +Udp4SendPortUnreach ( + IN IP_IO *IpIo, + IN EFI_NET_SESSION_DATA *NetSession, + IN VOID *Udp4Header + ) +{ + NET_BUF *Packet; + UINT32 Len; + IP4_ICMP_ERROR_HEAD *IcmpErrHdr; + EFI_IP4_HEADER *IpHdr; + UINT8 *Ptr; + IP_IO_OVERRIDE Override; + IP_IO_IP_INFO *IpSender; + + IpSender = IpIoFindSender (&IpIo, NetSession->IpVersion, &NetSession->Dest); + if (IpSender == NULL) { + // + // No appropriate sender, since we cannot send out the ICMP message through + // the default zero station address IP instance, abort. + // + return; + } + + IpHdr = NetSession->IpHdr.Ip4Hdr; + + // + // Calculate the required length of the icmp error message. + // + Len = sizeof (IP4_ICMP_ERROR_HEAD) + (EFI_IP4_HEADER_LEN (IpHdr) - + sizeof (IP4_HEAD)) + ICMP_ERROR_PACKET_LENGTH; + + // + // Allocate buffer for the icmp error message. + // + Packet = NetbufAlloc (Len); + if (Packet == NULL) { + return; + } + + // + // Allocate space for the IP4_ICMP_ERROR_HEAD. + // + IcmpErrHdr = (IP4_ICMP_ERROR_HEAD *) NetbufAllocSpace (Packet, Len, FALSE); + ASSERT (IcmpErrHdr != NULL); + + // + // Set the required fields for the icmp port unreachable message. + // + IcmpErrHdr->Head.Type = ICMP_TYPE_UNREACH; + IcmpErrHdr->Head.Code = ICMP_CODE_UNREACH_PORT; + IcmpErrHdr->Head.Checksum = 0; + IcmpErrHdr->Fourth = 0; + + // + // Copy the IP header of the datagram tragged the error. + // + CopyMem (&IcmpErrHdr->IpHead, IpHdr, EFI_IP4_HEADER_LEN (IpHdr)); + + // + // Copy the UDP header. + // + Ptr = (UINT8 *) &IcmpErrHdr->IpHead + EFI_IP4_HEADER_LEN (IpHdr); + CopyMem (Ptr, Udp4Header, ICMP_ERROR_PACKET_LENGTH); + + // + // Calculate the checksum. + // + IcmpErrHdr->Head.Checksum = (UINT16) ~(NetbufChecksum (Packet)); + + // + // Fill the override data. + // + Override.Ip4OverrideData.DoNotFragment = FALSE; + Override.Ip4OverrideData.TypeOfService = 0; + Override.Ip4OverrideData.TimeToLive = 255; + Override.Ip4OverrideData.Protocol = EFI_IP_PROTO_ICMP; + + CopyMem (&Override.Ip4OverrideData.SourceAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS)); + ZeroMem (&Override.Ip4OverrideData.GatewayAddress, sizeof (EFI_IPv4_ADDRESS)); + + // + // Send out this icmp packet. + // + IpIoSend (IpIo, Packet, IpSender, NULL, NULL, &NetSession->Source, &Override); + + NetbufFree (Packet); +} + + +/** + This function handles the received Icmp Error message and demultiplexes it to the + instance. + + @param[in] Udp4Service Pointer to the udp service context data. + @param[in] IcmpError The icmp error code. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted + from the received Icmp Error packet. + @param[in] Packet Pointer to the Icmp Error packet. + +**/ +VOID +Udp4IcmpHandler ( + IN UDP4_SERVICE_DATA *Udp4Service, + IN UINT8 IcmpError, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Packet + ) +{ + EFI_UDP_HEADER *Udp4Header; + EFI_UDP4_SESSION_DATA Udp4Session; + LIST_ENTRY *Entry; + UDP4_INSTANCE_DATA *Instance; + + if (Packet->TotalSize < sizeof (EFI_UDP_HEADER)) { + NetbufFree (Packet); + return; + } + + Udp4Header = (EFI_UDP_HEADER *) NetbufGetByte (Packet, 0, NULL); + ASSERT (Udp4Header != NULL); + + CopyMem (&Udp4Session.SourceAddress, &NetSession->Source, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Udp4Session.DestinationAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS)); + + Udp4Session.SourcePort = NTOHS (Udp4Header->DstPort); + Udp4Session.DestinationPort = NTOHS (Udp4Header->SrcPort); + + NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) { + // + // Iterate all the instances. + // + Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link); + + if (!Instance->Configured) { + continue; + } + + if (Udp4MatchDgram (Instance, &Udp4Session)) { + // + // Translate the Icmp Error code according to the udp spec. + // + Instance->IcmpError = IpIoGetIcmpErrStatus (IcmpError, IP_VERSION_4, NULL, NULL); + + if (IcmpError > ICMP_ERR_UNREACH_PORT) { + Instance->IcmpError = EFI_ICMP_ERROR; + } + + // + // Notify the instance with the received Icmp Error. + // + Udp4ReportIcmpError (Instance); + + break; + } + } + + NetbufFree (Packet); +} + + +/** + This function reports the received ICMP error. + + @param[in] Instance Pointer to the udp instance context data. + +**/ +VOID +Udp4ReportIcmpError ( + IN UDP4_INSTANCE_DATA *Instance + ) +{ + EFI_UDP4_COMPLETION_TOKEN *Token; + + if (NetMapIsEmpty (&Instance->RxTokens)) { + // + // There are no receive tokens to deliver the ICMP error. + // + return; + } + + if (EFI_ERROR (Instance->IcmpError)) { + // + // Try to get a RxToken from the RxTokens map. + // + Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL); + + if (Token != NULL) { + // + // Report the error through the Token. + // + Token->Status = Instance->IcmpError; + gBS->SignalEvent (Token->Event); + + // + // Clear the IcmpError. + // + Instance->IcmpError = EFI_SUCCESS; + } + } +} + + +/** + This function is a dummy ext-free function for the NET_BUF created for the output + udp datagram. + + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp4NetVectorExtFree ( + VOID *Context + ) +{ +} + diff --git a/NetworkPkg/Udp4Dxe/Udp4Impl.h b/NetworkPkg/Udp4Dxe/Udp4Impl.h new file mode 100644 index 000000000..a5dd1ecab --- /dev/null +++ b/NetworkPkg/Udp4Dxe/Udp4Impl.h @@ -0,0 +1,689 @@ +/** @file + EFI UDPv4 protocol implementation. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _UDP4_IMPL_H_ +#define _UDP4_IMPL_H_ + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Udp4Driver.h" + + +extern EFI_COMPONENT_NAME_PROTOCOL gUdp4ComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gUdp4ComponentName2; +extern EFI_UNICODE_STRING_TABLE *gUdpControllerNameTable; +extern EFI_SERVICE_BINDING_PROTOCOL mUdp4ServiceBinding; +extern EFI_UDP4_PROTOCOL mUdp4Protocol; +extern UINT16 mUdp4RandomPort; + +#define ICMP_ERROR_PACKET_LENGTH 8 + +#define UDP4_TIMEOUT_INTERVAL (50 * TICKS_PER_MS) // 50 milliseconds + +#define UDP4_HEADER_SIZE sizeof (EFI_UDP_HEADER) +#define UDP4_MAX_DATA_SIZE 65507 + +#define UDP4_PORT_KNOWN 1024 + +#define UDP4_SERVICE_DATA_SIGNATURE SIGNATURE_32('U', 'd', 'p', '4') + +#define UDP4_SERVICE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + UDP4_SERVICE_DATA, \ + ServiceBinding, \ + UDP4_SERVICE_DATA_SIGNATURE \ + ) + +typedef struct _UDP4_SERVICE_DATA_ { + UINT32 Signature; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + EFI_HANDLE ImageHandle; + EFI_HANDLE ControllerHandle; + LIST_ENTRY ChildrenList; + UINTN ChildrenNumber; + IP_IO *IpIo; + + EFI_EVENT TimeoutEvent; +} UDP4_SERVICE_DATA; + +#define UDP4_INSTANCE_DATA_SIGNATURE SIGNATURE_32('U', 'd', 'p', 'I') + +#define UDP4_INSTANCE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + UDP4_INSTANCE_DATA, \ + Udp4Proto, \ + UDP4_INSTANCE_DATA_SIGNATURE \ + ) + +typedef struct _UDP4_INSTANCE_DATA_ { + UINT32 Signature; + LIST_ENTRY Link; + + UDP4_SERVICE_DATA *Udp4Service; + EFI_UDP4_PROTOCOL Udp4Proto; + EFI_UDP4_CONFIG_DATA ConfigData; + EFI_HANDLE ChildHandle; + BOOLEAN Configured; + BOOLEAN IsNoMapping; + + NET_MAP TxTokens; + NET_MAP RxTokens; + + NET_MAP McastIps; + + LIST_ENTRY RcvdDgramQue; + LIST_ENTRY DeliveredDgramQue; + + UINT16 HeadSum; + + EFI_STATUS IcmpError; + + IP_IO_IP_INFO *IpInfo; + + BOOLEAN InDestroy; +} UDP4_INSTANCE_DATA; + +typedef struct _UDP4_RXDATA_WRAP_ { + LIST_ENTRY Link; + NET_BUF *Packet; + UINT32 TimeoutTick; + EFI_UDP4_RECEIVE_DATA RxData; +} UDP4_RXDATA_WRAP; + +typedef struct { + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; +} UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT; + +/** + Reads the current operational settings. + + The GetModeData() function copies the current operational settings of this EFI + UDPv4 Protocol instance into user-supplied buffers. This function is used + optionally to retrieve the operational mode data of underlying networks or + drivers. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[out] Udp4ConfigData Pointer to the buffer to receive the current configuration data. + @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure. + @param[out] MnpConfigData Pointer to the managed network configuration data structure. + @param[out] SnpModeData Pointer to the simple network mode data structure. + + @retval EFI_SUCCESS The mode data was read. + @retval EFI_NOT_STARTED When Udp4ConfigData is queried, no configuration data is + available because this instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +Udp4GetModeData ( + IN EFI_UDP4_PROTOCOL *This, + OUT EFI_UDP4_CONFIG_DATA *Udp4ConfigData OPTIONAL, + OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ); + +/** + Initializes, changes, or resets the operational parameters for this instance of the EFI UDPv4 + Protocol. + + The Configure() function is used to do the following: + * Initialize and start this instance of the EFI UDPv4 Protocol. + * Change the filtering rules and operational parameters. + * Reset this instance of the EFI UDPv4 Protocol. + Until these parameters are initialized, no network traffic can be sent or + received by this instance. This instance can be also reset by calling Configure() + with UdpConfigData set to NULL. Once reset, the receiving queue and transmitting + queue are flushed and no traffic is allowed through this instance. + With different parameters in UdpConfigData, Configure() can be used to bind + this instance to specified port. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] UdpConfigData Pointer to the buffer to receive the current configuration data. + + @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE: + @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured + and must be stopped/reset before it can be reconfigured. + @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE + and UdpConfigData.StationPort is already used by + other instance. + @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this + EFI UDPv4 Protocol instance. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance + was not opened. + +**/ +EFI_STATUS +EFIAPI +Udp4Configure ( + IN EFI_UDP4_PROTOCOL *This, + IN EFI_UDP4_CONFIG_DATA *UdpConfigData OPTIONAL + ); + +/** + Joins and leaves multicast groups. + + The Groups() function is used to enable and disable the multicast group + filtering. If the JoinFlag is FALSE and the MulticastAddress is NULL, then all + currently joined groups are left. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join a multicast group. Set to FALSE to leave one + or all multicast groups. + @param[in] MulticastAddress Pointer to multicast group address to join or leave. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - JoinFlag is TRUE and MulticastAddress is NULL. + - JoinFlag is TRUE and *MulticastAddress is not + a valid multicast address. + @retval EFI_ALREADY_STARTED The group address is already in the group table (when + JoinFlag is TRUE). + @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is + FALSE). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +Udp4Groups ( + IN EFI_UDP4_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_IPv4_ADDRESS *MulticastAddress OPTIONAL + ); + +/** + Adds and deletes routing table entries. + + The Routes() function adds a route to or deletes a route from the routing table. + Routes are determined by comparing the SubnetAddress with the destination IP + address and arithmetically AND-ing it with the SubnetMask. The gateway address + must be on the same subnet as the configured station address. + The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0. + The default route matches all destination IP addresses that do not match any + other routes. + A zero GatewayAddress is a nonroute. Packets are sent to the destination IP + address if it can be found in the Address Resolution Protocol (ARP) cache or + on the local subnet. One automatic nonroute entry will be inserted into the + routing table for outgoing packets that are addressed to a local subnet + (gateway address of 0.0.0.0). + Each instance of the EFI UDPv4 Protocol has its own independent routing table. + Instances of the EFI UDPv4 Protocol that use the default IP address will also + have copies of the routing table provided by the EFI_IP4_CONFIG_PROTOCOL. These + copies will be updated automatically whenever the IP driver reconfigures its + instances; as a result, the previous modification to these copies will be lost. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. + Set to FALSE to add this route to the routing table. + @param[in] SubnetAddress The destination network address that needs to be routed. + @param[in] SubnetMask The subnet mask of SubnetAddress. + @param[in] GatewayAddress The gateway IP address for this route. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, + - RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table. + @retval EFI_NOT_FOUND This route is not in the routing table. + @retval EFI_ACCESS_DENIED The route is already defined in the routing table. + +**/ +EFI_STATUS +EFIAPI +Udp4Routes ( + IN EFI_UDP4_PROTOCOL *This, + IN BOOLEAN DeleteRoute, + IN EFI_IPv4_ADDRESS *SubnetAddress, + IN EFI_IPv4_ADDRESS *SubnetMask, + IN EFI_IPv4_ADDRESS *GatewayAddress + ); + +/** + Queues outgoing data packets into the transmit queue. + + The Transmit() function places a sending request to this instance of the EFI + UDPv4 Protocol, alongside the transmit data that was filled by the user. Whenever + the packet in the token is sent out or some errors occur, the Token.Event will + be signaled and Token.Status is updated. Providing a proper notification function + and context for the event will enable the user to receive the notification and + transmitting status. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] Token Pointer to the completion token that will be placed into the + transmit queue. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_ACCESS_DENIED The transmit completion token with the same + Token.Event was already in the transmit queue. + @retval EFI_NOT_READY The completion token could not be queued because the + transmit queue is full. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data. + @retval EFI_NOT_FOUND There is no route to the destination network or address. + @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP packet + size. Or the length of the IP header + UDP header + data + length is greater than MTU if DoNotFragment is TRUE. + +**/ +EFI_STATUS +EFIAPI +Udp4Transmit ( + IN EFI_UDP4_PROTOCOL *This, + IN EFI_UDP4_COMPLETION_TOKEN *Token + ); + +/** + Places an asynchronous receive request into the receiving queue. + + The Receive() function places a completion token into the receive packet queue. + This function is always asynchronous. + The caller must fill in the Token.Event field in the completion token, and this + field cannot be NULL. When the receive operation completes, the EFI UDPv4 Protocol + driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event + is signaled. Providing a proper notification function and context for the event + will enable the user to receive the notification and receiving status. That + notification function is guaranteed to not be re-entered. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] Token Pointer to a token that is associated with + the receive data descriptor. + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, RARP, etc.) + is not finished yet. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system + resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_ACCESS_DENIED A receive completion token with the same Token.Event was already in + the receive queue. + @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +Udp4Receive ( + IN EFI_UDP4_PROTOCOL *This, + IN EFI_UDP4_COMPLETION_TOKEN *Token + ); + +/** + Aborts an asynchronous transmit or receive request. + + The Cancel() function is used to abort a pending transmit or receive request. + If the token is in the transmit or receive request queues, after calling this + function, Token.Status will be set to EFI_ABORTED and then Token.Event will be + signaled. If the token is not in one of the queues, which usually means that + the asynchronous operation has completed, this function will not signal the + token and EFI_NOT_FOUND is returned. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_UDP4_PROTOCOL.Transmit() or + EFI_UDP4_PROTOCOL.Receive().If NULL, all pending + tokens are aborted. + + @retval EFI_SUCCESS The asynchronous I/O request was aborted and Token.Event + was signaled. When Token is NULL, all pending requests are + aborted and their events are signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was + not found in the transmit or receive queue. It has either completed + or was not issued by Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +Udp4Cancel ( + IN EFI_UDP4_PROTOCOL *This, + IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications to increase + the rate that data packets are moved between the communications device and the + transmit and receive queues. + In some systems, the periodic timer event in the managed network driver may not + poll the underlying communications device fast enough to transmit and/or receive + all data packets without missing incoming packets or dropping outgoing packets. + Drivers and applications that are experiencing packet loss should try calling + the Poll() function more often. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue. + +**/ +EFI_STATUS +EFIAPI +Udp4Poll ( + IN EFI_UDP4_PROTOCOL *This + ); + +/** + Create the Udp service context data. + + @param[in, out] Udp4Service Pointer to the UDP4_SERVICE_DATA. + @param[in] ImageHandle The image handle of this udp4 driver. + @param[in] ControllerHandle The controller handle this udp4 driver binds on. + + @retval EFI_SUCCESS The udp4 service context data is created and + initialized. + @retval EFI_OUT_OF_RESOURCES Cannot allocate memory. + @retval other Other error occurs. + +**/ +EFI_STATUS +Udp4CreateService ( + IN OUT UDP4_SERVICE_DATA *Udp4Service, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle + ); + +/** + Clean the Udp service context data. + + @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA. + +**/ +VOID +Udp4CleanService ( + IN UDP4_SERVICE_DATA *Udp4Service + ); + +/** + This function intializes the new created udp instance. + + @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA. + @param[in, out] Instance Pointer to the un-initialized UDP4_INSTANCE_DATA. + +**/ +VOID +Udp4InitInstance ( + IN UDP4_SERVICE_DATA *Udp4Service, + IN OUT UDP4_INSTANCE_DATA *Instance + ); + +/** + This function cleans the udp instance. + + @param[in] Instance Pointer to the UDP4_INSTANCE_DATA to clean. + +**/ +VOID +Udp4CleanInstance ( + IN UDP4_INSTANCE_DATA *Instance + ); + +/** + This function tries to bind the udp instance according to the configured port + allocation strategy. + + @param[in] InstanceList Pointer to the head of the list linking the udp + instances. + @param[in, out] ConfigData Pointer to the ConfigData of the instance to be + bound. ConfigData->StationPort will be assigned + with an available port value on success. + + @retval EFI_SUCCESS The bound operation is completed successfully. + @retval EFI_ACCESS_DENIED The specified by the ConfigData is + already used by other instance. + @retval EFI_OUT_OF_RESOURCES No available port resources. + +**/ +EFI_STATUS +Udp4Bind ( + IN LIST_ENTRY *InstanceList, + IN OUT EFI_UDP4_CONFIG_DATA *ConfigData + ); + +/** + This function is used to check whether the NewConfigData has any un-reconfigurable + parameters changed compared to the OldConfigData. + + @param[in] OldConfigData Pointer to the current ConfigData the udp instance + uses. + @param[in] NewConfigData Pointer to the new ConfigData. + + @retval TRUE The instance is reconfigurable. + @retval FALSE Otherwise. + +**/ +BOOLEAN +Udp4IsReconfigurable ( + IN EFI_UDP4_CONFIG_DATA *OldConfigData, + IN EFI_UDP4_CONFIG_DATA *NewConfigData + ); + +/** + This function builds the Ip4 configdata from the Udp4ConfigData. + + @param[in] Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA. + @param[in, out] Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA. + +**/ +VOID +Udp4BuildIp4ConfigData ( + IN EFI_UDP4_CONFIG_DATA *Udp4ConfigData, + IN OUT EFI_IP4_CONFIG_DATA *Ip4ConfigData + ); + +/** + This function validates the TxToken, it returns the error code according to the spec. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] TxToken Pointer to the token to be checked. + + @retval EFI_SUCCESS The TxToken is valid. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is + NULL. Token is NULL. Token.Event is NULL. + Token.Packet.TxData is NULL. + Token.Packet.TxData.FragmentCount is zero. + Token.Packet.TxData.DataLength is not equal to the + sum of fragment lengths. One or more of the + Token.Packet.TxData.FragmentTable[]. + FragmentLength fields is zero. One or more of the + Token.Packet.TxData.FragmentTable[]. + FragmentBuffer fields is NULL. + Token.Packet.TxData. GatewayAddress is not a + unicast IPv4 address if it is not NULL. One or + more IPv4 addresses in Token.Packet.TxData. + UdpSessionData are not valid unicast IPv4 + addresses if the UdpSessionData is not NULL. + @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP + packet size. + +**/ +EFI_STATUS +Udp4ValidateTxToken ( + IN UDP4_INSTANCE_DATA *Instance, + IN EFI_UDP4_COMPLETION_TOKEN *TxToken + ); + +/** + This function checks whether the specified Token duplicates with the one in the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to + the Token. + @param[in] Context Pointer to the Token to be checked. + + @retval EFI_SUCCESS The Token specified by Context differs from the + one in the Item. + @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item. + +**/ +EFI_STATUS +EFIAPI +Udp4TokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ); + +/** + This function calculates the checksum for the Packet, utilizing the pre-calculated + pseudo HeadSum to reduce some overhead. + + @param[in] Packet Pointer to the NET_BUF contains the udp datagram. + @param[in] HeadSum Checksum of the pseudo header execpt the length + field. + + @retval The 16-bit checksum of this udp datagram. + +**/ +UINT16 +Udp4Checksum ( + IN NET_BUF *Packet, + IN UINT16 HeadSum + ); + +/** + This function removes the specified Token from the TokenMap. + + @param[in, out] TokenMap Pointer to the NET_MAP containing the tokens. + @param[in] Token Pointer to the Token to be removed. + + @retval EFI_SUCCESS The specified Token is removed from the TokenMap. + @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap. + +**/ +EFI_STATUS +Udp4RemoveToken ( + IN OUT NET_MAP *TokenMap, + IN EFI_UDP4_COMPLETION_TOKEN *Token + ); + +/** + This function removes the multicast group specified by Arg from the Map. + + @param[in, out] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg, it's the pointer to a + multicast IPv4 Address. + + @retval EFI_SUCCESS The multicast address is removed. + @retval EFI_ABORTED The specified multicast address is removed and the + Arg is not NULL. + +**/ +EFI_STATUS +EFIAPI +Udp4LeaveGroup ( + IN OUT NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ); + +/** + This function removes all the Wrap datas in the RcvdDgramQue. + + @param[in] Instance Pointer to the udp instance context data. + +**/ +VOID +Udp4FlushRcvdDgram ( + IN UDP4_INSTANCE_DATA *Instance + ); + +/** + Cancel Udp4 tokens from the Udp4 instance. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] Token Pointer to the token to be canceled, if NULL, all + tokens in this instance will be cancelled. + + @retval EFI_SUCCESS The Token is cancelled. + @retval EFI_NOT_FOUND The Token is not found. + +**/ +EFI_STATUS +Udp4InstanceCancelToken ( + IN UDP4_INSTANCE_DATA *Instance, + IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + This function delivers the received datagrams for the specified instance. + + @param[in] Instance Pointer to the instance context data. + +**/ +VOID +Udp4InstanceDeliverDgram ( + IN UDP4_INSTANCE_DATA *Instance + ); + +/** + This function reports the received ICMP error. + + @param[in] Instance Pointer to the udp instance context data. + +**/ +VOID +Udp4ReportIcmpError ( + IN UDP4_INSTANCE_DATA *Instance + ); + +/** + This function is a dummy ext-free function for the NET_BUF created for the output + udp datagram. + + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp4NetVectorExtFree ( + VOID *Context + ); + +#endif diff --git a/NetworkPkg/Udp4Dxe/Udp4Main.c b/NetworkPkg/Udp4Dxe/Udp4Main.c new file mode 100644 index 000000000..aa1956cd4 --- /dev/null +++ b/NetworkPkg/Udp4Dxe/Udp4Main.c @@ -0,0 +1,902 @@ +/** @file + +(C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Udp4Impl.h" + +EFI_UDP4_PROTOCOL mUdp4Protocol = { + Udp4GetModeData, + Udp4Configure, + Udp4Groups, + Udp4Routes, + Udp4Transmit, + Udp4Receive, + Udp4Cancel, + Udp4Poll +}; + + +/** + Reads the current operational settings. + + The GetModeData() function copies the current operational settings of this EFI + UDPv4 Protocol instance into user-supplied buffers. This function is used + optionally to retrieve the operational mode data of underlying networks or + drivers. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[out] Udp4ConfigData Pointer to the buffer to receive the current configuration data. + @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure. + @param[out] MnpConfigData Pointer to the managed network configuration data structure. + @param[out] SnpModeData Pointer to the simple network mode data structure. + + @retval EFI_SUCCESS The mode data was read. + @retval EFI_NOT_STARTED When Udp4ConfigData is queried, no configuration data is + available because this instance has not been started. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +Udp4GetModeData ( + IN EFI_UDP4_PROTOCOL *This, + OUT EFI_UDP4_CONFIG_DATA *Udp4ConfigData OPTIONAL, + OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ) +{ + UDP4_INSTANCE_DATA *Instance; + EFI_IP4_PROTOCOL *Ip; + EFI_TPL OldTpl; + EFI_STATUS Status; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP4_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured && (Udp4ConfigData != NULL)) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (Udp4ConfigData != NULL) { + // + // Set the Udp4ConfigData. + // + CopyMem (Udp4ConfigData, &Instance->ConfigData, sizeof (*Udp4ConfigData)); + } + + Ip = Instance->IpInfo->Ip.Ip4; + + // + // Get the underlying Ip4ModeData, MnpConfigData and SnpModeData. + // + Status = Ip->GetModeData (Ip, Ip4ModeData, MnpConfigData, SnpModeData); + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Initializes, changes, or resets the operational parameters for this instance of the EFI UDPv4 + Protocol. + + The Configure() function is used to do the following: + * Initialize and start this instance of the EFI UDPv4 Protocol. + * Change the filtering rules and operational parameters. + * Reset this instance of the EFI UDPv4 Protocol. + Until these parameters are initialized, no network traffic can be sent or + received by this instance. This instance can be also reset by calling Configure() + with UdpConfigData set to NULL. Once reset, the receiving queue and transmitting + queue are flushed and no traffic is allowed through this instance. + With different parameters in UdpConfigData, Configure() can be used to bind + this instance to specified port. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] UdpConfigData Pointer to the buffer to receive the current configuration data. + + @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE: + @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured + and must be stopped/reset before it can be reconfigured. + @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE + and UdpConfigData.StationPort is already used by + other instance. + @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this + EFI UDPv4 Protocol instance. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance + was not opened. + +**/ +EFI_STATUS +EFIAPI +Udp4Configure ( + IN EFI_UDP4_PROTOCOL *This, + IN EFI_UDP4_CONFIG_DATA *UdpConfigData OPTIONAL + ) +{ + EFI_STATUS Status; + UDP4_INSTANCE_DATA *Instance; + UDP4_SERVICE_DATA *Udp4Service; + EFI_TPL OldTpl; + IP4_ADDR StationAddress; + IP4_ADDR SubnetMask; + IP4_ADDR RemoteAddress; + EFI_IP4_CONFIG_DATA Ip4ConfigData; + IP4_ADDR LocalAddr; + IP4_ADDR RemoteAddr; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP4_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured && (UdpConfigData == NULL)) { + return EFI_SUCCESS; + } + + Udp4Service = Instance->Udp4Service; + Status = EFI_SUCCESS; + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (UdpConfigData != NULL) { + + CopyMem (&StationAddress, &UdpConfigData->StationAddress, sizeof (IP4_ADDR)); + CopyMem (&SubnetMask, &UdpConfigData->SubnetMask, sizeof (IP4_ADDR)); + CopyMem (&RemoteAddress, &UdpConfigData->RemoteAddress, sizeof (IP4_ADDR)); + + StationAddress = NTOHL (StationAddress); + SubnetMask = NTOHL (SubnetMask); + RemoteAddress = NTOHL (RemoteAddress); + + + if (!UdpConfigData->UseDefaultAddress && + (!IP4_IS_VALID_NETMASK (SubnetMask) || + !((StationAddress == 0) || (SubnetMask != 0 && NetIp4IsUnicast (StationAddress, SubnetMask))) || + IP4_IS_LOCAL_BROADCAST (RemoteAddress))) { + // + // Don't use default address, and subnet mask is invalid or StationAddress is not + // a valid unicast IPv4 address or RemoteAddress is not a valid unicast IPv4 address + // if it is not 0. + // + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + if (Instance->Configured) { + // + // The instance is already configured, try to do the re-configuration. + // + if (!Udp4IsReconfigurable (&Instance->ConfigData, UdpConfigData)) { + // + // If the new configuration data wants to change some unreconfigurable + // settings, return EFI_ALREADY_STARTED. + // + Status = EFI_ALREADY_STARTED; + goto ON_EXIT; + } + + // + // Save the reconfigurable parameters. + // + Instance->ConfigData.TypeOfService = UdpConfigData->TypeOfService; + Instance->ConfigData.TimeToLive = UdpConfigData->TimeToLive; + Instance->ConfigData.DoNotFragment = UdpConfigData->DoNotFragment; + Instance->ConfigData.ReceiveTimeout = UdpConfigData->ReceiveTimeout; + Instance->ConfigData.TransmitTimeout = UdpConfigData->TransmitTimeout; + } else { + // + // Construct the Ip configuration data from the UdpConfigData. + // + Udp4BuildIp4ConfigData (UdpConfigData, &Ip4ConfigData); + + // + // Configure the Ip instance wrapped in the IpInfo. + // + Status = IpIoConfigIp (Instance->IpInfo, &Ip4ConfigData); + if (EFI_ERROR (Status)) { + if (Status == EFI_NO_MAPPING) { + Instance->IsNoMapping = TRUE; + } + + goto ON_EXIT; + } + + Instance->IsNoMapping = FALSE; + + // + // Save the configuration data. + // + CopyMem (&Instance->ConfigData, UdpConfigData, sizeof (Instance->ConfigData)); + IP4_COPY_ADDRESS (&Instance->ConfigData.StationAddress, &Ip4ConfigData.StationAddress); + IP4_COPY_ADDRESS (&Instance->ConfigData.SubnetMask, &Ip4ConfigData.SubnetMask); + + // + // Try to allocate the required port resource. + // + Status = Udp4Bind (&Udp4Service->ChildrenList, &Instance->ConfigData); + if (EFI_ERROR (Status)) { + // + // Reset the ip instance if bind fails. + // + IpIoConfigIp (Instance->IpInfo, NULL); + goto ON_EXIT; + } + + // + // Pre calculate the checksum for the pseudo head, ignore the UDP length first. + // + CopyMem (&LocalAddr, &Instance->ConfigData.StationAddress, sizeof (IP4_ADDR)); + CopyMem (&RemoteAddr, &Instance->ConfigData.RemoteAddress, sizeof (IP4_ADDR)); + Instance->HeadSum = NetPseudoHeadChecksum ( + LocalAddr, + RemoteAddr, + EFI_IP_PROTO_UDP, + 0 + ); + + Instance->Configured = TRUE; + } + } else { + // + // UdpConfigData is NULL, reset the instance. + // + Instance->Configured = FALSE; + Instance->IsNoMapping = FALSE; + + // + // Reset the Ip instance wrapped in the IpInfo. + // + IpIoConfigIp (Instance->IpInfo, NULL); + + // + // Cancel all the user tokens. + // + Instance->Udp4Proto.Cancel (&Instance->Udp4Proto, NULL); + + // + // Remove the buffered RxData for this instance. + // + Udp4FlushRcvdDgram (Instance); + + ASSERT (IsListEmpty (&Instance->DeliveredDgramQue)); + } + +ON_EXIT: + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Joins and leaves multicast groups. + + The Groups() function is used to enable and disable the multicast group + filtering. If the JoinFlag is FALSE and the MulticastAddress is NULL, then all + currently joined groups are left. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join a multicast group. Set to FALSE to leave one + or all multicast groups. + @param[in] MulticastAddress Pointer to multicast group address to join or leave. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + - This is NULL. + - JoinFlag is TRUE and MulticastAddress is NULL. + - JoinFlag is TRUE and *MulticastAddress is not + a valid multicast address. + @retval EFI_ALREADY_STARTED The group address is already in the group table (when + JoinFlag is TRUE). + @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is + FALSE). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +Udp4Groups ( + IN EFI_UDP4_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_IPv4_ADDRESS *MulticastAddress OPTIONAL + ) +{ + EFI_STATUS Status; + UDP4_INSTANCE_DATA *Instance; + EFI_IP4_PROTOCOL *Ip; + EFI_TPL OldTpl; + IP4_ADDR McastIp; + + if ((This == NULL) || (JoinFlag && (MulticastAddress == NULL))) { + return EFI_INVALID_PARAMETER; + } + + McastIp = 0; + if (JoinFlag) { + CopyMem (&McastIp, MulticastAddress, sizeof (IP4_ADDR)); + + if (!IP4_IS_MULTICAST (NTOHL (McastIp))) { + return EFI_INVALID_PARAMETER; + } + } + + Instance = UDP4_INSTANCE_DATA_FROM_THIS (This); + + if (Instance->IsNoMapping) { + return EFI_NO_MAPPING; + } + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + Ip = Instance->IpInfo->Ip.Ip4; + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Invoke the Ip instance the Udp4 instance consumes to do the group operation. + // + Status = Ip->Groups (Ip, JoinFlag, MulticastAddress); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Keep a local copy of the configured multicast IPs because IpIo receives + // datagrams from the 0 station address IP instance and then UDP delivers to + // the matched instance. This copy of multicast IPs is used to avoid receive + // the mutlicast datagrams destined to multicast IPs the other instances configured. + // + if (JoinFlag) { + + NetMapInsertTail (&Instance->McastIps, (VOID *) (UINTN) McastIp, NULL); + } else { + + NetMapIterate (&Instance->McastIps, Udp4LeaveGroup, MulticastAddress); + } + +ON_EXIT: + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Adds and deletes routing table entries. + + The Routes() function adds a route to or deletes a route from the routing table. + Routes are determined by comparing the SubnetAddress with the destination IP + address and arithmetically AND-ing it with the SubnetMask. The gateway address + must be on the same subnet as the configured station address. + The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0. + The default route matches all destination IP addresses that do not match any + other routes. + A zero GatewayAddress is a nonroute. Packets are sent to the destination IP + address if it can be found in the Address Resolution Protocol (ARP) cache or + on the local subnet. One automatic nonroute entry will be inserted into the + routing table for outgoing packets that are addressed to a local subnet + (gateway address of 0.0.0.0). + Each instance of the EFI UDPv4 Protocol has its own independent routing table. + Instances of the EFI UDPv4 Protocol that use the default IP address will also + have copies of the routing table provided by the EFI_IP4_CONFIG_PROTOCOL. These + copies will be updated automatically whenever the IP driver reconfigures its + instances; as a result, the previous modification to these copies will be lost. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. + Set to FALSE to add this route to the routing table. + @param[in] SubnetAddress The destination network address that needs to be routed. + @param[in] SubnetMask The subnet mask of SubnetAddress. + @param[in] GatewayAddress The gateway IP address for this route. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, + - RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table. + @retval EFI_NOT_FOUND This route is not in the routing table. + @retval EFI_ACCESS_DENIED The route is already defined in the routing table. + +**/ +EFI_STATUS +EFIAPI +Udp4Routes ( + IN EFI_UDP4_PROTOCOL *This, + IN BOOLEAN DeleteRoute, + IN EFI_IPv4_ADDRESS *SubnetAddress, + IN EFI_IPv4_ADDRESS *SubnetMask, + IN EFI_IPv4_ADDRESS *GatewayAddress + ) +{ + UDP4_INSTANCE_DATA *Instance; + EFI_IP4_PROTOCOL *Ip; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP4_INSTANCE_DATA_FROM_THIS (This); + + if (Instance->IsNoMapping) { + return EFI_NO_MAPPING; + } + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + Ip = Instance->IpInfo->Ip.Ip4; + + // + // Invoke the Ip instance the Udp4 instance consumes to do the actual operation. + // + return Ip->Routes (Ip, DeleteRoute, SubnetAddress, SubnetMask, GatewayAddress); +} + + +/** + Queues outgoing data packets into the transmit queue. + + The Transmit() function places a sending request to this instance of the EFI + UDPv4 Protocol, alongside the transmit data that was filled by the user. Whenever + the packet in the token is sent out or some errors occur, the Token.Event will + be signaled and Token.Status is updated. Providing a proper notification function + and context for the event will enable the user to receive the notification and + transmitting status. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] Token Pointer to the completion token that will be placed into the + transmit queue. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_ACCESS_DENIED The transmit completion token with the same + Token.Event was already in the transmit queue. + @retval EFI_NOT_READY The completion token could not be queued because the + transmit queue is full. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data. + @retval EFI_NOT_FOUND There is no route to the destination network or address. + @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP packet + size. Or the length of the IP header + UDP header + data + length is greater than MTU if DoNotFragment is TRUE. + +**/ +EFI_STATUS +EFIAPI +Udp4Transmit ( + IN EFI_UDP4_PROTOCOL *This, + IN EFI_UDP4_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + UDP4_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + NET_BUF *Packet; + EFI_UDP_HEADER *Udp4Header; + EFI_UDP4_CONFIG_DATA *ConfigData; + IP4_ADDR Source; + IP4_ADDR Destination; + EFI_UDP4_TRANSMIT_DATA *TxData; + EFI_UDP4_SESSION_DATA *UdpSessionData; + UDP4_SERVICE_DATA *Udp4Service; + IP_IO_OVERRIDE Override; + UINT16 HeadSum; + EFI_IP_ADDRESS IpDestAddr; + + if ((This == NULL) || (Token == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP4_INSTANCE_DATA_FROM_THIS (This); + + if (Instance->IsNoMapping) { + return EFI_NO_MAPPING; + } + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Validate the Token, if the token is invalid return the error code. + // + Status = Udp4ValidateTxToken (Instance, Token); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + if (EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp4TokenExist, Token)) || + EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp4TokenExist, Token))) { + // + // Try to find a duplicate token in the two token maps, if found, return + // EFI_ACCESS_DENIED. + // + Status = EFI_ACCESS_DENIED; + goto ON_EXIT; + } + + TxData = Token->Packet.TxData; + + // + // Create a net buffer to hold the user buffer and the udp header. + // + Packet = NetbufFromExt ( + (NET_FRAGMENT *)TxData->FragmentTable, + TxData->FragmentCount, + UDP4_HEADER_SIZE, + 0, + Udp4NetVectorExtFree, + NULL + ); + if (Packet == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Store the IpIo in ProtoData. + // + Udp4Service = Instance->Udp4Service; + *((UINTN *) &Packet->ProtoData[0]) = (UINTN) (Udp4Service->IpIo); + + Udp4Header = (EFI_UDP_HEADER *) NetbufAllocSpace (Packet, UDP4_HEADER_SIZE, TRUE); + ASSERT (Udp4Header != NULL); + + ConfigData = &Instance->ConfigData; + + // + // Fill the udp header. + // + Udp4Header->SrcPort = HTONS (ConfigData->StationPort); + Udp4Header->DstPort = HTONS (ConfigData->RemotePort); + Udp4Header->Length = HTONS ((UINT16) Packet->TotalSize); + Udp4Header->Checksum = 0; + + UdpSessionData = TxData->UdpSessionData; + IP4_COPY_ADDRESS (&Override.Ip4OverrideData.SourceAddress, &ConfigData->StationAddress); + + if (UdpSessionData != NULL) { + // + // Set the SourceAddress, SrcPort and Destination according to the specified + // UdpSessionData. + // + if (!EFI_IP4_EQUAL (&UdpSessionData->SourceAddress, &mZeroIp4Addr)) { + IP4_COPY_ADDRESS (&Override.Ip4OverrideData.SourceAddress, &UdpSessionData->SourceAddress); + } + + if (UdpSessionData->SourcePort != 0) { + Udp4Header->SrcPort = HTONS (UdpSessionData->SourcePort); + } + + if (UdpSessionData->DestinationPort != 0) { + Udp4Header->DstPort = HTONS (UdpSessionData->DestinationPort); + } + + CopyMem (&Source, &Override.Ip4OverrideData.SourceAddress, sizeof (IP4_ADDR)); + CopyMem (&Destination, &UdpSessionData->DestinationAddress, sizeof (IP4_ADDR)); + + // + // calculate the pseudo head checksum using the overridden parameters. + // + HeadSum = NetPseudoHeadChecksum ( + Source, + Destination, + EFI_IP_PROTO_UDP, + 0 + ); + } else { + // + // UdpSessionData is NULL, use the address and port information previously configured. + // + CopyMem (&Destination, &ConfigData->RemoteAddress, sizeof (IP4_ADDR)); + + HeadSum = Instance->HeadSum; + } + + // + // calculate the checksum. + // + Udp4Header->Checksum = Udp4Checksum (Packet, HeadSum); + if (Udp4Header->Checksum == 0) { + // + // If the calculated checksum is 0, fill the Checksum field with all ones. + // + Udp4Header->Checksum = 0xffff; + } + + // + // Fill the IpIo Override data. + // + if (TxData->GatewayAddress != NULL) { + IP4_COPY_ADDRESS (&Override.Ip4OverrideData.GatewayAddress, TxData->GatewayAddress); + } else { + ZeroMem (&Override.Ip4OverrideData.GatewayAddress, sizeof (EFI_IPv4_ADDRESS)); + } + + Override.Ip4OverrideData.Protocol = EFI_IP_PROTO_UDP; + Override.Ip4OverrideData.TypeOfService = ConfigData->TypeOfService; + Override.Ip4OverrideData.TimeToLive = ConfigData->TimeToLive; + Override.Ip4OverrideData.DoNotFragment = ConfigData->DoNotFragment; + + // + // Save the token into the TxToken map. + // + Status = NetMapInsertTail (&Instance->TxTokens, Token, Packet); + if (EFI_ERROR (Status)) { + goto FREE_PACKET; + } + + // + // Send out this datagram through IpIo. + // + IpDestAddr.Addr[0] = Destination; + Status = IpIoSend ( + Udp4Service->IpIo, + Packet, + Instance->IpInfo, + Instance, + Token, + &IpDestAddr, + &Override + ); + if (EFI_ERROR (Status)) { + // + // Remove this token from the TxTokens. + // + Udp4RemoveToken (&Instance->TxTokens, Token); + } + +FREE_PACKET: + + NetbufFree (Packet); + +ON_EXIT: + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Places an asynchronous receive request into the receiving queue. + + The Receive() function places a completion token into the receive packet queue. + This function is always asynchronous. + The caller must fill in the Token.Event field in the completion token, and this + field cannot be NULL. When the receive operation completes, the EFI UDPv4 Protocol + driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event + is signaled. Providing a proper notification function and context for the event + will enable the user to receive the notification and receiving status. That + notification function is guaranteed to not be re-entered. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] Token Pointer to a token that is associated with + the receive data descriptor. + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, RARP, etc.) + is not finished yet. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system + resources (usually memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_ACCESS_DENIED A receive completion token with the same Token.Event was already in + the receive queue. + @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +Udp4Receive ( + IN EFI_UDP4_PROTOCOL *This, + IN EFI_UDP4_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + UDP4_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP4_INSTANCE_DATA_FROM_THIS (This); + + if (Instance->IsNoMapping) { + return EFI_NO_MAPPING; + } + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp4TokenExist, Token))|| + EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp4TokenExist, Token))) { + // + // Return EFI_ACCESS_DENIED if the specified token is already in the TxTokens or + // RxTokens map. + // + Status = EFI_ACCESS_DENIED; + goto ON_EXIT; + } + + Token->Packet.RxData = NULL; + + // + // Save the token into the RxTokens map. + // + Status = NetMapInsertTail (&Instance->RxTokens, Token, NULL); + if (EFI_ERROR (Status)) { + Status = EFI_NOT_READY; + goto ON_EXIT; + } + + // + // If there is an icmp error, report it. + // + Udp4ReportIcmpError (Instance); + + // + // Try to deliver the received datagrams. + // + Udp4InstanceDeliverDgram (Instance); + + // + // Dispatch the DPC queued by the NotifyFunction of Token->Event. + // + DispatchDpc (); + +ON_EXIT: + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Aborts an asynchronous transmit or receive request. + + The Cancel() function is used to abort a pending transmit or receive request. + If the token is in the transmit or receive request queues, after calling this + function, Token.Status will be set to EFI_ABORTED and then Token.Event will be + signaled. If the token is not in one of the queues, which usually means that + the asynchronous operation has completed, this function will not signal the + token and EFI_NOT_FOUND is returned. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_UDP4_PROTOCOL.Transmit() or + EFI_UDP4_PROTOCOL.Receive().If NULL, all pending + tokens are aborted. + + @retval EFI_SUCCESS The asynchronous I/O request was aborted and Token.Event + was signaled. When Token is NULL, all pending requests are + aborted and their events are signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, + RARP, etc.) is not finished yet. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was + not found in the transmit or receive queue. It has either completed + or was not issued by Transmit() and Receive(). + +**/ +EFI_STATUS +EFIAPI +Udp4Cancel ( + IN EFI_UDP4_PROTOCOL *This, + IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + EFI_STATUS Status; + UDP4_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP4_INSTANCE_DATA_FROM_THIS (This); + + if (Instance->IsNoMapping) { + return EFI_NO_MAPPING; + } + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Cancle the tokens specified by Token for this instance. + // + Status = Udp4InstanceCancelToken (Instance, Token); + + // + // Dispatch the DPC queued by the NotifyFunction of the cancelled token's events. + // + DispatchDpc (); + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + Polls for incoming data packets and processes outgoing data packets. + + The Poll() function can be used by network drivers and applications to increase + the rate that data packets are moved between the communications device and the + transmit and receive queues. + In some systems, the periodic timer event in the managed network driver may not + poll the underlying communications device fast enough to transmit and/or receive + all data packets without missing incoming packets or dropping outgoing packets. + Drivers and applications that are experiencing packet loss should try calling + the Poll() function more often. + + @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue. + +**/ +EFI_STATUS +EFIAPI +Udp4Poll ( + IN EFI_UDP4_PROTOCOL *This + ) +{ + UDP4_INSTANCE_DATA *Instance; + EFI_IP4_PROTOCOL *Ip; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP4_INSTANCE_DATA_FROM_THIS (This); + Ip = Instance->IpInfo->Ip.Ip4; + + // + // Invode the Ip instance consumed by the udp instance to do the poll operation. + // + return Ip->Poll (Ip); +} diff --git a/NetworkPkg/Udp6Dxe/ComponentName.c b/NetworkPkg/Udp6Dxe/ComponentName.c new file mode 100644 index 000000000..49d8d5309 --- /dev/null +++ b/NetworkPkg/Udp6Dxe/ComponentName.c @@ -0,0 +1,423 @@ +/** @file + UEFI Component Name(2) protocol implementation for UDP6 driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Udp6Impl.h" + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Udp6ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is being managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Udp6ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gUdp6ComponentName = { + Udp6ComponentNameGetDriverName, + Udp6ComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gUdp6ComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) Udp6ComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) Udp6ComponentNameGetControllerName, + "en" +}; + + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mUdp6DriverNameTable[] = { + { + "eng;en", + L"UDP6 Network Service Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gUdp6ControllerNameTable = NULL; + +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Udp6ComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mUdp6DriverNameTable, + DriverName, + (BOOLEAN) (This == &gUdp6ComponentName) + ); +} + +/** + Update the component name for the Udp6 child handle. + + @param Udp6[in] A pointer to the EFI_UDP6_PROTOCOL. + + + @retval EFI_SUCCESS Update the ControllerNameTable of this instance successfully. + @retval EFI_INVALID_PARAMETER The input parameter is invalid. + +**/ +EFI_STATUS +UpdateName ( + IN EFI_UDP6_PROTOCOL *Udp6 + ) +{ + EFI_STATUS Status; + CHAR16 HandleName[64]; + EFI_UDP6_CONFIG_DATA Udp6ConfigData; + + if (Udp6 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Format the child name into the string buffer. + // + Status = Udp6->GetModeData (Udp6, &Udp6ConfigData, NULL, NULL, NULL); + if (!EFI_ERROR (Status)) { + UnicodeSPrint (HandleName, sizeof (HandleName), + L"UDPv6 (SrcPort=%d, DestPort=%d)", + Udp6ConfigData.StationPort, + Udp6ConfigData.RemotePort + ); + } else if (Status == EFI_NOT_STARTED) { + UnicodeSPrint (HandleName, sizeof (HandleName), L"UDPv6 (Not started)"); + } else { + UnicodeSPrint (HandleName, sizeof (HandleName), L"UDPv6 (%r)", Status); + } + + if (gUdp6ControllerNameTable != NULL) { + FreeUnicodeStringTable (gUdp6ControllerNameTable); + gUdp6ControllerNameTable = NULL; + } + + Status = AddUnicodeString2 ( + "eng", + gUdp6ComponentName.SupportedLanguages, + &gUdp6ControllerNameTable, + HandleName, + TRUE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return AddUnicodeString2 ( + "en", + gUdp6ComponentName2.SupportedLanguages, + &gUdp6ControllerNameTable, + HandleName, + FALSE + ); +} + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is being managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +Udp6ComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_UDP6_PROTOCOL *Udp6; + + // + // Only provide names for child handles. + // + if (ChildHandle == NULL) { + return EFI_UNSUPPORTED; + } + + // + // Make sure this driver produced ChildHandle + // + Status = EfiTestChildHandle ( + ControllerHandle, + ChildHandle, + &gEfiIp6ProtocolGuid + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Retrieve an instance of a produced protocol from ChildHandle + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiUdp6ProtocolGuid, + (VOID **)&Udp6, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Update the component name for this child handle. + // + Status = UpdateName (Udp6); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gUdp6ControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gUdp6ComponentName) + ); +} diff --git a/NetworkPkg/Udp6Dxe/Udp6Driver.c b/NetworkPkg/Udp6Dxe/Udp6Driver.c new file mode 100644 index 000000000..5334fd173 --- /dev/null +++ b/NetworkPkg/Udp6Dxe/Udp6Driver.c @@ -0,0 +1,623 @@ +/** @file + Driver Binding functions and Service Binding functions for the Network driver module. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Udp6Impl.h" + +EFI_DRIVER_BINDING_PROTOCOL gUdp6DriverBinding = { + Udp6DriverBindingSupported, + Udp6DriverBindingStart, + Udp6DriverBindingStop, + 0xa, + NULL, + NULL +}; + +EFI_SERVICE_BINDING_PROTOCOL mUdp6ServiceBinding = { + Udp6ServiceBindingCreateChild, + Udp6ServiceBindingDestroyChild +}; + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +Udp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + // + // Test for the Udp6ServiceBinding Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + // + // Test for the Ip6ServiceBinding Protocol + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiIp6ServiceBindingProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + + return Status; +} + +/** + Start this driver on ControllerHandle. + + This service is called by the EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind the driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCES This driver is added to ControllerHandle. + @retval EFI_OUT_OF_RESOURCES The required system resource can't be allocated. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Udp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + UDP6_SERVICE_DATA *Udp6Service; + + // + // Allocate Private Context Data Structure. + // + Udp6Service = AllocateZeroPool (sizeof (UDP6_SERVICE_DATA)); + if (Udp6Service == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + Status = Udp6CreateService (Udp6Service, This->DriverBindingHandle, ControllerHandle); + if (EFI_ERROR (Status)) { + goto EXIT; + } + + // + // Install the Udp6ServiceBindingProtocol on the ControllerHandle. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + &Udp6Service->ServiceBinding, + NULL + ); + if (EFI_ERROR (Status)) { + Udp6CleanService (Udp6Service); + } + +EXIT: + if (EFI_ERROR (Status)) { + if (Udp6Service != NULL) { + FreePool (Udp6Service); + } + } + return Status; +} + +/** + Callback function which provided by user to remove one node in NetDestroyLinkList process. + + @param[in] Entry The entry to be removed. + @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList. + + @retval EFI_INVALID_PARAMETER Entry is NULL or Context is NULL. + @retval EFI_SUCCESS The entry has been removed successfully. + @retval Others Fail to remove the entry. + +**/ +EFI_STATUS +EFIAPI +Udp6DestroyChildEntryInHandleBuffer ( + IN LIST_ENTRY *Entry, + IN VOID *Context + ) +{ + UDP6_INSTANCE_DATA *Instance; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; + + if (Entry == NULL || Context == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = NET_LIST_USER_STRUCT_S (Entry, UDP6_INSTANCE_DATA, Link, UDP6_INSTANCE_DATA_SIGNATURE); + ServiceBinding = ((UDP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding; + NumberOfChildren = ((UDP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren; + ChildHandleBuffer = ((UDP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer; + + if (!NetIsInHandleBuffer (Instance->ChildHandle, NumberOfChildren, ChildHandleBuffer)) { + return EFI_SUCCESS; + } + + return ServiceBinding->DestroyChild (ServiceBinding, Instance->ChildHandle); +} + +/** + Stop this driver on ControllerHandle. + + This service is called by the EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop(), it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop the driver on. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If the number + of children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. It is optional. + + @retval EFI_SUCCES This driver is removed ControllerHandle. + @retval EFI_DEVICE_ERROR Can't find the NicHandle from the ControllerHandle and specified GUID. + @retval other This driver was not removed from this device. + +**/ +EFI_STATUS +EFIAPI +Udp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_HANDLE NicHandle; + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UDP6_SERVICE_DATA *Udp6Service; + LIST_ENTRY *List; + UDP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context; + + // + // Find the NicHandle where UDP6 ServiceBinding Protocol is installed. + // + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiIp6ProtocolGuid); + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + // + // Retrieve the UDP6 ServiceBinding Protocol. + // + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + (VOID **) &ServiceBinding, + This->DriverBindingHandle, + NicHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + Udp6Service = UDP6_SERVICE_DATA_FROM_THIS (ServiceBinding); + + if (NumberOfChildren != 0) { + // + // NumberOfChildren is not zero, destroy the children instances in ChildHandleBuffer. + // + List = &Udp6Service->ChildrenList; + Context.ServiceBinding = ServiceBinding; + Context.NumberOfChildren = NumberOfChildren; + Context.ChildHandleBuffer = ChildHandleBuffer; + Status = NetDestroyLinkList ( + List, + Udp6DestroyChildEntryInHandleBuffer, + &Context, + NULL + ); + } else if (IsListEmpty (&Udp6Service->ChildrenList)) { + Status = gBS->UninstallMultipleProtocolInterfaces ( + NicHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + &Udp6Service->ServiceBinding, + NULL + ); + + Udp6CleanService (Udp6Service); + FreePool (Udp6Service); + } + + return Status; +} + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER This is NULL or ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +Udp6ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ) +{ + EFI_STATUS Status; + UDP6_SERVICE_DATA *Udp6Service; + UDP6_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + VOID *Ip6; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Udp6Service = UDP6_SERVICE_DATA_FROM_THIS (This); + + // + // Allocate the instance private data structure. + // + Instance = AllocateZeroPool (sizeof (UDP6_INSTANCE_DATA)); + if (Instance == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Udp6InitInstance (Udp6Service, Instance); + + // + // Add an IpInfo for this instance. + // + Instance->IpInfo = IpIoAddIp (Udp6Service->IpIo); + if (Instance->IpInfo == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + // + // Install the Udp6Protocol for this instance. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiUdp6ProtocolGuid, + &Instance->Udp6Proto, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Instance->ChildHandle = *ChildHandle; + + // + // Open the default Ip6 protocol in the IP_IO BY_CHILD. + // + Status = gBS->OpenProtocol ( + Udp6Service->IpIo->ChildHandle, + &gEfiIp6ProtocolGuid, + (VOID **) &Ip6, + gUdp6DriverBinding.DriverBindingHandle, + Instance->ChildHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Open this instance's Ip6 protocol in the IpInfo BY_CHILD. + // + Status = gBS->OpenProtocol ( + Instance->IpInfo->ChildHandle, + &gEfiIp6ProtocolGuid, + (VOID **) &Ip6, + gUdp6DriverBinding.DriverBindingHandle, + Instance->ChildHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Link this instance into the service context data and increase the ChildrenNumber. + // + InsertTailList (&Udp6Service->ChildrenList, &Instance->Link); + Udp6Service->ChildrenNumber++; + + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; + +ON_ERROR: + + if (Instance->ChildHandle != NULL) { + gBS->UninstallMultipleProtocolInterfaces ( + Instance->ChildHandle, + &gEfiUdp6ProtocolGuid, + &Instance->Udp6Proto, + NULL + ); + } + + if (Instance->IpInfo != NULL) { + IpIoRemoveIp (Udp6Service->IpIo, Instance->IpInfo); + } + + Udp6CleanInstance (Instance); + + FreePool (Instance); + + return Status; +} + +/** + Destroys a child handle with a set of I/O services. + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Protocol instance pointer. + @param[in] ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The I/O services were removed from the child + handle. + @retval EFI_UNSUPPORTED The child handle does not support the I/O services + that are being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The child handle could not be destroyed because + its I/O services are being used. + @retval other The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +Udp6ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ) +{ + EFI_STATUS Status; + UDP6_SERVICE_DATA *Udp6Service; + EFI_UDP6_PROTOCOL *Udp6Proto; + UDP6_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if ((This == NULL) || (ChildHandle == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Udp6Service = UDP6_SERVICE_DATA_FROM_THIS (This); + + // + // Try to get the Udp6 protocol from the ChildHandle. + // + Status = gBS->OpenProtocol ( + ChildHandle, + &gEfiUdp6ProtocolGuid, + (VOID **) &Udp6Proto, + gUdp6DriverBinding.DriverBindingHandle, + ChildHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + Instance = UDP6_INSTANCE_DATA_FROM_THIS (Udp6Proto); + + if (Instance->InDestroy) { + return EFI_SUCCESS; + } + + // + // Use the Destroyed flag to avoid the re-entering of the following code. + // + Instance->InDestroy = TRUE; + + // + // Close the Ip6 protocol on the default IpIo. + // + Status = gBS->CloseProtocol ( + Udp6Service->IpIo->ChildHandle, + &gEfiIp6ProtocolGuid, + gUdp6DriverBinding.DriverBindingHandle, + Instance->ChildHandle + ); + if (EFI_ERROR (Status)) { + Instance->InDestroy = FALSE; + return Status; + } + + // + // Close the Ip6 protocol on this instance's IpInfo. + // + Status = gBS->CloseProtocol ( + Instance->IpInfo->ChildHandle, + &gEfiIp6ProtocolGuid, + gUdp6DriverBinding.DriverBindingHandle, + Instance->ChildHandle + ); + if (EFI_ERROR (Status)) { + Instance->InDestroy = FALSE; + return Status; + } + + // + // Uninstall the Udp6Protocol previously installed on the ChildHandle. + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + ChildHandle, + &gEfiUdp6ProtocolGuid, + (VOID *) &Instance->Udp6Proto, + NULL + ); + if (EFI_ERROR (Status)) { + Instance->InDestroy = FALSE; + return Status; + } + + // + // Reset the configuration in case the instance's consumer forgets to do this. + // + Udp6Proto->Configure (Udp6Proto, NULL); + + // + // Remove the IpInfo this instance consumes. + // + IpIoRemoveIp (Udp6Service->IpIo, Instance->IpInfo); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Remove this instance from the service context data's ChildrenList. + // + RemoveEntryList (&Instance->Link); + Udp6Service->ChildrenNumber--; + + // + // Clean the instance. + // + Udp6CleanInstance (Instance); + + gBS->RestoreTPL (OldTpl); + + FreePool (Instance); + + return EFI_SUCCESS; +} + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers, including + both device drivers and bus drivers. + + The entry point for Udp6 driver that installs the driver binding + and component name protocol on its ImageHandle. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +Udp6DriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + // + // Install the Udp6DriverBinding and Udp6ComponentName protocols. + // + + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gUdp6DriverBinding, + ImageHandle, + &gUdp6ComponentName, + &gUdp6ComponentName2 + ); + if (!EFI_ERROR (Status)) { + // + // Initialize the UDP random port. + // + mUdp6RandomPort = (UINT16)( + ((UINT16) NetRandomInitSeed ()) % + UDP6_PORT_KNOWN + + UDP6_PORT_KNOWN + ); + } + + return Status; +} + + diff --git a/NetworkPkg/Udp6Dxe/Udp6Driver.h b/NetworkPkg/Udp6Dxe/Udp6Driver.h new file mode 100644 index 000000000..a08623979 --- /dev/null +++ b/NetworkPkg/Udp6Dxe/Udp6Driver.h @@ -0,0 +1,176 @@ +/** @file + Driver Binding functions and Service Binding functions for the Network driver module. + + Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _UDP6_DRIVER_H_ +#define _UDP6_DRIVER_H_ + +#include +#include +#include + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Since ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. +**/ +EFI_STATUS +EFIAPI +Udp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. + + This service is called by the EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind a driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCES This driver is added to ControllerHandle. + @retval EFI_OUT_OF_RESOURCES The required system resource can't be allocated. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +Udp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle. + + This service is called by the EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop(), it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop the driver on. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number + of children is zero, stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. It is optional. + + @retval EFI_SUCCESS This driver removed ControllerHandle. + @retval EFI_DEVICE_ERROR Can't find the NicHandle from the ControllerHandle and specified GUID. + @retval other This driver was not removed from this device. + +**/ +EFI_STATUS +EFIAPI +Udp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +/** + Creates a child handle and installs a protocol. + + The CreateChild() function installs a protocol on ChildHandle. + If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. + If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle. + + @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance. + @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL, + then a new handle is created. If it is a pointer to an existing UEFI handle, + then the protocol is added to the existing UEFI handle. + + @retval EFI_SUCCES The protocol was added to ChildHandle. + @retval EFI_INVALID_PARAMETER This is NULL or ChildHandle is NULL. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create + the child. + @retval other The child handle was not created. + +**/ +EFI_STATUS +EFIAPI +Udp6ServiceBindingCreateChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN OUT EFI_HANDLE *ChildHandle + ); + +/** + Destroys a child handle with a set of I/O services. + The DestroyChild() function does the opposite of CreateChild(). It removes a protocol + that was installed by CreateChild() from ChildHandle. If the removed protocol is the + last protocol on ChildHandle, then ChildHandle is destroyed. + + @param[in] This Protocol instance pointer. + @param[in] ChildHandle Handle of the child to destroy. + + @retval EFI_SUCCES The I/O services were removed from the child + handle. + @retval EFI_UNSUPPORTED The child handle does not support the I/O services + that are being removed. + @retval EFI_INVALID_PARAMETER Child handle is NULL. + @retval EFI_ACCESS_DENIED The child handle could not be destroyed because + its I/O services are being used. + @retval other The child handle was not destroyed. + +**/ +EFI_STATUS +EFIAPI +Udp6ServiceBindingDestroyChild ( + IN EFI_SERVICE_BINDING_PROTOCOL *This, + IN EFI_HANDLE ChildHandle + ); + +#endif + diff --git a/NetworkPkg/Udp6Dxe/Udp6Dxe.inf b/NetworkPkg/Udp6Dxe/Udp6Dxe.inf new file mode 100644 index 000000000..7830d2de2 --- /dev/null +++ b/NetworkPkg/Udp6Dxe/Udp6Dxe.inf @@ -0,0 +1,64 @@ +## @file Udp6Dxe.inf +# UDP packet service based on IPv6 stack. +# +# This module produces EFI UDPv6 Protocol which provides simple packet-oriented +# services to transmit and receive UDP packets. +# +# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = Udp6Dxe + FILE_GUID = D912C7BC-F098-4367-92BA-E911083C7B0E + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + + ENTRY_POINT = Udp6DriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload + MODULE_UNI_FILE = Udp6Dxe.uni + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources] + Udp6Driver.h + Udp6Driver.c + Udp6Impl.c + Udp6Impl.h + ComponentName.c + Udp6Main.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + MemoryAllocationLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiRuntimeServicesTableLib + UefiLib + DebugLib + IpIoLib + NetLib + DpcLib + + +[Protocols] + gEfiIp6ProtocolGuid ## TO_START + gEfiIp6ServiceBindingProtocolGuid ## TO_START + gEfiUdp6ServiceBindingProtocolGuid ## BY_START + gEfiUdp6ProtocolGuid ## BY_START + +[UserExtensions.TianoCore."ExtraFiles"] + Udp6DxeExtra.uni diff --git a/NetworkPkg/Udp6Dxe/Udp6Dxe.uni b/NetworkPkg/Udp6Dxe/Udp6Dxe.uni new file mode 100644 index 000000000..570cdfff0 --- /dev/null +++ b/NetworkPkg/Udp6Dxe/Udp6Dxe.uni @@ -0,0 +1,17 @@ +// /** @file +// UDP packet service based on IPv6 stack. +// +// This module produces EFI UDPv6 Protocol which provides simple packet-oriented +// services to transmit and receive UDP packets. +// +// Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "UDP packet service based on IPv6 stack" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces EFI UDPv6 Protocol which provides simple packet-oriented services to transmit and receive UDP packets." + diff --git a/NetworkPkg/Udp6Dxe/Udp6DxeExtra.uni b/NetworkPkg/Udp6Dxe/Udp6DxeExtra.uni new file mode 100644 index 000000000..491c720eb --- /dev/null +++ b/NetworkPkg/Udp6Dxe/Udp6DxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// Udp6Dxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"UDP6 DXE" + + diff --git a/NetworkPkg/Udp6Dxe/Udp6Impl.c b/NetworkPkg/Udp6Dxe/Udp6Impl.c new file mode 100644 index 000000000..aefcd3396 --- /dev/null +++ b/NetworkPkg/Udp6Dxe/Udp6Impl.c @@ -0,0 +1,2003 @@ +/** @file + Udp6 driver's whole implementation. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Udp6Impl.h" + +UINT16 mUdp6RandomPort; + +/** + This function checks and timeouts the I/O datagrams holding by the corresponding + service context. + + @param[in] Event The event this function is registered to. + @param[in] Context The context data registered during the creation of + the Event. + +**/ +VOID +EFIAPI +Udp6CheckTimeout ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + This function finds the udp instance by the specified pair. + + @param[in] InstanceList Pointer to the head of the list linking the udp + instances. + @param[in] Address Pointer to the specified IPv6 address. + @param[in] Port The udp port number. + + @retval TRUE The specified pair is found. + @retval FALSE Otherwise. + +**/ +BOOLEAN +Udp6FindInstanceByPort ( + IN LIST_ENTRY *InstanceList, + IN EFI_IPv6_ADDRESS *Address, + IN UINT16 Port + ); + +/** + This function is the packet transmitting notify function registered to the IpIo + interface. It's called to signal the udp TxToken when the IpIo layer completes + transmitting of the udp datagram. + + If Context is NULL, then ASSERT(). + If NotifyData is NULL, then ASSERT(). + + @param[in] Status The completion status of the output udp datagram. + @param[in] Context Pointer to the context data. + @param[in] Sender Specify a EFI_IP6_PROTOCOL for sending. + @param[in] NotifyData Pointer to the notify data. + +**/ +VOID +EFIAPI +Udp6DgramSent ( + IN EFI_STATUS Status, + IN VOID *Context, + IN IP_IO_IP_PROTOCOL Sender, + IN VOID *NotifyData + ); + +/** + This function processes the received datagram passed up by the IpIo layer. + + If NetSession is NULL, then ASSERT(). + If Packet is NULL, then ASSERT(). + If Context is NULL, then ASSERT(). + + @param[in] Status The status of this udp datagram. + @param[in] IcmpError The IcmpError code, only available when Status is + EFI_ICMP_ERROR. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA. + @param[in] Packet Pointer to the NET_BUF containing the received udp + datagram. + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp6DgramRcvd ( + IN EFI_STATUS Status, + IN UINT8 IcmpError, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Packet, + IN VOID *Context + ); + +/** + This function cancle the token specified by Arg in the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the token to be cancelled, if NULL, all + the tokens in this Map will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token + is not the same as that in the Item if Arg is not + NULL. + @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is + cancelled. + +**/ +EFI_STATUS +EFIAPI +Udp6CancelTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ); + +/** + This function check if the received udp datagram matches with the Instance. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] Udp6Session Pointer to the EFI_UDP6_SESSION_DATA abstracted + from the received udp datagram. + + @retval TRUE The udp datagram matches the receiving requirements of the Instance. + @retval FALSE The udp datagram doe not match the receiving requirements of the Instance. + +**/ +BOOLEAN +Udp6MatchDgram ( + IN UDP6_INSTANCE_DATA *Instance, + IN EFI_UDP6_SESSION_DATA *Udp6Session + ); + +/** + This function removes the Wrap specified by Context and releases relevant resources. + + @param[in] Event The Event this notify function is registered to. + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp6RecycleRxDataWrap ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + This function wraps the Packet into RxData. + + @param[in] Instance Pointer to the instance context data. + @param[in] Packet Pointer to the buffer containing the received + datagram. + @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this + datagram. + + @return Pointer to the structure wrapping the RxData and the Packet. NULL will + be returned if any error occurs. + +**/ +UDP6_RXDATA_WRAP * +Udp6WrapRxData ( + IN UDP6_INSTANCE_DATA *Instance, + IN NET_BUF *Packet, + IN EFI_UDP6_RECEIVE_DATA *RxData + ); + +/** + This function enqueues the received datagram into the instances' receiving queues. + + @param[in] Udp6Service Pointer to the udp service context data. + @param[in] Packet Pointer to the buffer containing the received + datagram. + @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this + datagram. + + @return The times this datagram is enqueued. + +**/ +UINTN +Udp6EnqueueDgram ( + IN UDP6_SERVICE_DATA *Udp6Service, + IN NET_BUF *Packet, + IN EFI_UDP6_RECEIVE_DATA *RxData + ); + +/** + This function delivers the datagrams enqueued in the instances. + + @param[in] Udp6Service Pointer to the udp service context data. + +**/ +VOID +Udp6DeliverDgram ( + IN UDP6_SERVICE_DATA *Udp6Service + ); + +/** + This function demultiplexes the received udp datagram to the appropriate instances. + + @param[in] Udp6Service Pointer to the udp service context data. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from + the received datagram. + @param[in] Packet Pointer to the buffer containing the received udp + datagram. + +**/ +VOID +Udp6Demultiplex ( + IN UDP6_SERVICE_DATA *Udp6Service, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Packet + ); + +/** + This function handles the received Icmp Error message and demultiplexes it to the + instance. + + @param[in] Udp6Service Pointer to the udp service context data. + @param[in] IcmpError The icmp error code. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted + from the received Icmp Error packet. + @param[in, out] Packet Pointer to the Icmp Error packet. + +**/ +VOID +Udp6IcmpHandler ( + IN UDP6_SERVICE_DATA *Udp6Service, + IN UINT8 IcmpError, + IN EFI_NET_SESSION_DATA *NetSession, + IN OUT NET_BUF *Packet + ); + +/** + This function builds and sends out a icmp port unreachable message. + + @param[in] IpIo Pointer to the IP_IO instance. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet + causes this icmp error message. + @param[in] Udp6Header Pointer to the udp header of the datagram causes + this icmp error message. + +**/ +VOID +Udp6SendPortUnreach ( + IN IP_IO *IpIo, + IN EFI_NET_SESSION_DATA *NetSession, + IN VOID *Udp6Header + ); + +/** + Find the key in the netmap + + @param[in] Map The netmap to search within. + @param[in] Key The key to search. + + @return The point to the item contains the Key, or NULL if Key isn't in the map. + +**/ +NET_MAP_ITEM * +Udp6MapMultiCastAddr ( + IN NET_MAP *Map, + IN VOID *Key + ); + +/** + Create the Udp service context data. + + @param[in] Udp6Service Pointer to the UDP6_SERVICE_DATA. + @param[in] ImageHandle The image handle of this udp6 driver. + @param[in] ControllerHandle The controller handle this udp6 driver binds on. + + @retval EFI_SUCCESS The udp6 service context data was created and + initialized. + @retval EFI_OUT_OF_RESOURCES Cannot allocate memory. + @retval Others An error condition occurred. + +**/ +EFI_STATUS +Udp6CreateService ( + IN UDP6_SERVICE_DATA *Udp6Service, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle + ) +{ + EFI_STATUS Status; + IP_IO_OPEN_DATA OpenData; + + ZeroMem (Udp6Service, sizeof (UDP6_SERVICE_DATA)); + + Udp6Service->Signature = UDP6_SERVICE_DATA_SIGNATURE; + Udp6Service->ServiceBinding = mUdp6ServiceBinding; + Udp6Service->ImageHandle = ImageHandle; + Udp6Service->ControllerHandle = ControllerHandle; + Udp6Service->ChildrenNumber = 0; + + InitializeListHead (&Udp6Service->ChildrenList); + + // + // Create the IpIo for this service context. + // + Udp6Service->IpIo = IpIoCreate (ImageHandle, ControllerHandle, IP_VERSION_6); + if (Udp6Service->IpIo == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Set the OpenData used to open the IpIo. + // + CopyMem ( + &OpenData.IpConfigData.Ip6CfgData, + &mIp6IoDefaultIpConfigData, + sizeof (EFI_IP6_CONFIG_DATA) + ); + OpenData.RcvdContext = (VOID *) Udp6Service; + OpenData.SndContext = NULL; + OpenData.PktRcvdNotify = Udp6DgramRcvd; + OpenData.PktSentNotify = Udp6DgramSent; + + // + // Configure and start the IpIo. + // + Status = IpIoOpen (Udp6Service->IpIo, &OpenData); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create the event for Udp timeout checking. + // + Status = gBS->CreateEvent ( + EVT_TIMER | EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + Udp6CheckTimeout, + Udp6Service, + &Udp6Service->TimeoutEvent + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Start the timeout timer event. + // + Status = gBS->SetTimer ( + Udp6Service->TimeoutEvent, + TimerPeriodic, + UDP6_TIMEOUT_INTERVAL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + + if (Udp6Service->TimeoutEvent != NULL) { + gBS->CloseEvent (Udp6Service->TimeoutEvent); + } + + IpIoDestroy (Udp6Service->IpIo); + Udp6Service->IpIo = NULL; + + return Status; +} + + +/** + Clean the Udp service context data. + + @param[in, out] Udp6Service Pointer to the UDP6_SERVICE_DATA. + +**/ +VOID +Udp6CleanService ( + IN OUT UDP6_SERVICE_DATA *Udp6Service + ) +{ + // + // Close the TimeoutEvent timer. + // + gBS->CloseEvent (Udp6Service->TimeoutEvent); + + // + // Destroy the IpIo. + // + IpIoDestroy (Udp6Service->IpIo); + Udp6Service->IpIo = NULL; + + ZeroMem (Udp6Service, sizeof (UDP6_SERVICE_DATA)); +} + + +/** + This function checks and times out the I/O datagrams listed in the + UDP6_SERVICE_DATA which is specified by the input parameter Context. + + + @param[in] Event The event this function registered to. + @param[in] Context The context data registered during the creation of + the Event. + +**/ +VOID +EFIAPI +Udp6CheckTimeout ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + UDP6_SERVICE_DATA *Udp6Service; + LIST_ENTRY *Entry; + UDP6_INSTANCE_DATA *Instance; + LIST_ENTRY *WrapEntry; + LIST_ENTRY *NextEntry; + UDP6_RXDATA_WRAP *Wrap; + + Udp6Service = (UDP6_SERVICE_DATA *) Context; + NET_CHECK_SIGNATURE (Udp6Service, UDP6_SERVICE_DATA_SIGNATURE); + + NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) { + // + // Iterate all the instances belonging to this service context. + // + Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link); + NET_CHECK_SIGNATURE (Instance, UDP6_INSTANCE_DATA_SIGNATURE); + + if (!Instance->Configured || (Instance->ConfigData.ReceiveTimeout == 0)) { + // + // Skip this instance if it's not configured or no receive timeout. + // + continue; + } + + NET_LIST_FOR_EACH_SAFE (WrapEntry, NextEntry, &Instance->RcvdDgramQue) { + // + // Iterate all the rxdatas belonging to this udp instance. + // + Wrap = NET_LIST_USER_STRUCT (WrapEntry, UDP6_RXDATA_WRAP, Link); + + if (Wrap->TimeoutTick < UDP6_TIMEOUT_INTERVAL / 10) { + // + // Remove this RxData if it timeouts. + // + Udp6RecycleRxDataWrap (NULL, (VOID *) Wrap); + } else { + Wrap->TimeoutTick -= UDP6_TIMEOUT_INTERVAL / 10; + } + } + } +} + + +/** + This function intializes the new created udp instance. + + @param[in] Udp6Service Pointer to the UDP6_SERVICE_DATA. + @param[in, out] Instance Pointer to the un-initialized UDP6_INSTANCE_DATA. + +**/ +VOID +Udp6InitInstance ( + IN UDP6_SERVICE_DATA *Udp6Service, + IN OUT UDP6_INSTANCE_DATA *Instance + ) +{ + // + // Set the signature. + // + Instance->Signature = UDP6_INSTANCE_DATA_SIGNATURE; + + // + // Init the lists. + // + InitializeListHead (&Instance->Link); + InitializeListHead (&Instance->RcvdDgramQue); + InitializeListHead (&Instance->DeliveredDgramQue); + + // + // Init the NET_MAPs. + // + NetMapInit (&Instance->TxTokens); + NetMapInit (&Instance->RxTokens); + NetMapInit (&Instance->McastIps); + + // + // Save the pointer to the UDP6_SERVICE_DATA, and initialize other members. + // + Instance->Udp6Service = Udp6Service; + CopyMem (&Instance->Udp6Proto, &mUdp6Protocol, sizeof (EFI_UDP6_PROTOCOL)); + Instance->IcmpError = EFI_SUCCESS; + Instance->Configured = FALSE; + Instance->IsNoMapping = FALSE; + Instance->InDestroy = FALSE; +} + + +/** + This function cleans the udp instance. + + @param[in, out] Instance Pointer to the UDP6_INSTANCE_DATA to clean. + +**/ +VOID +Udp6CleanInstance ( + IN OUT UDP6_INSTANCE_DATA *Instance + ) +{ + NetMapClean (&Instance->McastIps); + NetMapClean (&Instance->RxTokens); + NetMapClean (&Instance->TxTokens); +} + + +/** + This function finds the udp instance by the specified pair. + + @param[in] InstanceList Pointer to the head of the list linking the udp + instances. + @param[in] Address Pointer to the specified IPv6 address. + @param[in] Port The udp port number. + + @retval TRUE The specified pair is found. + @retval FALSE Otherwise. + +**/ +BOOLEAN +Udp6FindInstanceByPort ( + IN LIST_ENTRY *InstanceList, + IN EFI_IPv6_ADDRESS *Address, + IN UINT16 Port + ) +{ + LIST_ENTRY *Entry; + UDP6_INSTANCE_DATA *Instance; + EFI_UDP6_CONFIG_DATA *ConfigData; + + NET_LIST_FOR_EACH (Entry, InstanceList) { + // + // Iterate all the udp instances. + // + Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link); + ConfigData = &Instance->ConfigData; + + if (!Instance->Configured || ConfigData->AcceptAnyPort) { + // + // If the instance is not configured, or the configdata of the instance indicates + // this instance accepts any port, skip it. + // + continue; + } + + if (EFI_IP6_EQUAL (&ConfigData->StationAddress, Address) && + (ConfigData->StationPort == Port) + ) { + // + // If both the address and the port are the same, return TRUE. + // + return TRUE; + } + } + + // + // Return FALSE when matching fails. + // + return FALSE; +} + + +/** + This function tries to bind the udp instance according to the configured port + allocation stragety. + + @param[in] InstanceList Pointer to the head of the list linking the udp + instances. + @param[in] ConfigData Pointer to the ConfigData of the instance to be + bound. + + @retval EFI_SUCCESS The bound operation completed successfully. + @retval EFI_ACCESS_DENIED The specified by the ConfigData is + already used by other instance. + @retval EFI_OUT_OF_RESOURCES No available port resources. + +**/ +EFI_STATUS +Udp6Bind ( + IN LIST_ENTRY *InstanceList, + IN EFI_UDP6_CONFIG_DATA *ConfigData + ) +{ + EFI_IPv6_ADDRESS *StationAddress; + UINT16 StartPort; + + if (ConfigData->AcceptAnyPort) { + return EFI_SUCCESS; + } + + StationAddress = &ConfigData->StationAddress; + + if (ConfigData->StationPort != 0) { + + if (!ConfigData->AllowDuplicatePort && + Udp6FindInstanceByPort (InstanceList, StationAddress, ConfigData->StationPort) + ) { + // + // Do not allow duplicate ports and the port is already used by other instance. + // + return EFI_ACCESS_DENIED; + } + } else { + // + // Select a random port for this instance. + // + if (ConfigData->AllowDuplicatePort) { + // + // Just pick up the random port if the instance allows duplicate port. + // + ConfigData->StationPort = mUdp6RandomPort; + } else { + + StartPort = mUdp6RandomPort; + + while (Udp6FindInstanceByPort (InstanceList, StationAddress, mUdp6RandomPort)) { + + mUdp6RandomPort++; + if (mUdp6RandomPort == 0) { + mUdp6RandomPort = UDP6_PORT_KNOWN; + } + + if (mUdp6RandomPort == StartPort) { + // + // No available port. + // + return EFI_OUT_OF_RESOURCES; + } + } + + ConfigData->StationPort = mUdp6RandomPort; + } + + mUdp6RandomPort++; + if (mUdp6RandomPort == 0) { + mUdp6RandomPort = UDP6_PORT_KNOWN; + } + } + return EFI_SUCCESS; +} + + +/** + This function is used to check whether the NewConfigData has any un-reconfigurable + parameters changed compared to the OldConfigData. + + @param[in] OldConfigData Pointer to the current ConfigData the udp instance + uses. + @param[in] NewConfigData Pointer to the new ConfigData. + + @retval TRUE The instance is reconfigurable according to the NewConfigData. + @retval FALSE Otherwise. + +**/ +BOOLEAN +Udp6IsReconfigurable ( + IN EFI_UDP6_CONFIG_DATA *OldConfigData, + IN EFI_UDP6_CONFIG_DATA *NewConfigData + ) +{ + if ((NewConfigData->AcceptAnyPort != OldConfigData->AcceptAnyPort) || + (NewConfigData->AcceptPromiscuous != OldConfigData->AcceptPromiscuous) || + (NewConfigData->AllowDuplicatePort != OldConfigData->AllowDuplicatePort) + ) { + // + // The receiving filter parameters cannot be changed. + // + return FALSE; + } + + if ((!NewConfigData->AcceptAnyPort) && + (NewConfigData->StationPort != OldConfigData->StationPort) + ) { + // + // The port is not changeable. + // + return FALSE; + } + + if (!EFI_IP6_EQUAL (&NewConfigData->StationAddress, &OldConfigData->StationAddress)) { + // + // The StationAddress is not the same. + // + return FALSE; + } + + + if (!EFI_IP6_EQUAL (&NewConfigData->RemoteAddress, &OldConfigData->RemoteAddress)) { + // + // The remoteaddress is not the same. + // + return FALSE; + } + + if (!NetIp6IsUnspecifiedAddr (&NewConfigData->RemoteAddress) && + (NewConfigData->RemotePort != OldConfigData->RemotePort) + ) { + // + // The RemotePort differs if it's designated in the configdata. + // + return FALSE; + } + + // + // All checks pass, return TRUE. + // + return TRUE; +} + + +/** + This function builds the Ip6 configdata from the Udp6ConfigData. + + @param[in] Udp6ConfigData Pointer to the EFI_UDP6_CONFIG_DATA. + @param[in, out] Ip6ConfigData Pointer to the EFI_IP6_CONFIG_DATA. + +**/ +VOID +Udp6BuildIp6ConfigData ( + IN EFI_UDP6_CONFIG_DATA *Udp6ConfigData, + IN OUT EFI_IP6_CONFIG_DATA *Ip6ConfigData + ) +{ + CopyMem ( + Ip6ConfigData, + &mIp6IoDefaultIpConfigData, + sizeof (EFI_IP6_CONFIG_DATA) + ); + Ip6ConfigData->DefaultProtocol = EFI_IP_PROTO_UDP; + Ip6ConfigData->AcceptPromiscuous = Udp6ConfigData->AcceptPromiscuous; + IP6_COPY_ADDRESS (&Ip6ConfigData->StationAddress, &Udp6ConfigData->StationAddress); + IP6_COPY_ADDRESS (&Ip6ConfigData->DestinationAddress, &Udp6ConfigData->RemoteAddress); + // + // Use the -1 magic number to disable the receiving process of the ip instance. + // + Ip6ConfigData->ReceiveTimeout = (UINT32) (-1); +} + + +/** + This function validates the TxToken. It returns the error code according to the spec. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] TxToken Pointer to the token to be checked. + + @retval EFI_SUCCESS The TxToken is valid. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + Token.Event is NULL; + Token.Packet.TxData is NULL; + Token.Packet.TxData.FragmentCount is zero; + Token.Packet.TxData.DataLength is not equal to the + sum of fragment lengths; + One or more of the + Token.Packet.TxData.FragmentTable[].FragmentLength + fields is zero; + One or more of the + Token.Packet.TxData.FragmentTable[].FragmentBuffer + fields is NULL; + UdpSessionData.DestinationAddress are not valid + unicast IPv6 addresses if the UdpSessionData is + not NULL; + UdpSessionData.DestinationPort and + ConfigData.RemotePort are all zero if the + UdpSessionData is not NULL. + @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP + packet size. + +**/ +EFI_STATUS +Udp6ValidateTxToken ( + IN UDP6_INSTANCE_DATA *Instance, + IN EFI_UDP6_COMPLETION_TOKEN *TxToken + ) +{ + EFI_UDP6_TRANSMIT_DATA *TxData; + UINT32 Index; + UINT32 TotalLen; + EFI_UDP6_CONFIG_DATA *ConfigData; + EFI_UDP6_SESSION_DATA *UdpSessionData; + + + if (TxToken->Event == NULL) { + return EFI_INVALID_PARAMETER; + } + + TxData = TxToken->Packet.TxData; + + if ((TxData == NULL) || (TxData->FragmentCount == 0)) { + return EFI_INVALID_PARAMETER; + } + + TotalLen = 0; + for (Index = 0; Index < TxData->FragmentCount; Index++) { + + if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) || + (TxData->FragmentTable[Index].FragmentLength == 0) + ) { + // + // If the FragmentBuffer is NULL, or the FragmentLeng is zero. + // + return EFI_INVALID_PARAMETER; + } + + TotalLen += TxData->FragmentTable[Index].FragmentLength; + } + + if (TotalLen != TxData->DataLength) { + // + // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the + // DataLength. + // + return EFI_INVALID_PARAMETER; + } + + ConfigData = &Instance->ConfigData; + UdpSessionData = TxData->UdpSessionData; + + if (UdpSessionData != NULL) { + + if ((UdpSessionData->DestinationPort == 0) && (ConfigData->RemotePort == 0)) { + // + // Ambiguous; no avalaible DestinationPort for this token. + // + return EFI_INVALID_PARAMETER; + } + + if (NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress) && + NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress) + ) { + // + // The DestinationAddress is not specificed. + // + return EFI_INVALID_PARAMETER; + } + + if (!NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress) && + !NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress) + ) { + // + // The ConfigData.RemoteAddress is not zero and the UdpSessionData.DestinationAddress + // is not zero too. + // + return EFI_INVALID_PARAMETER; + } + } else if (NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress)) { + // + // The configured RemoteAddress is all zero, and the user doesn't override the + // destination address. + // + return EFI_INVALID_PARAMETER; + } + + if (TxData->DataLength > UDP6_MAX_DATA_SIZE) { + return EFI_BAD_BUFFER_SIZE; + } + + return EFI_SUCCESS; +} + + +/** + This function checks whether the specified Token duplicates the one in the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to + the Token. + @param[in] Context Pointer to the Token to be checked. + + @retval EFI_SUCCESS The Token specified by Context differs from the + one in the Item. + @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item. + +**/ +EFI_STATUS +EFIAPI +Udp6TokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ) +{ + EFI_UDP6_COMPLETION_TOKEN *Token; + EFI_UDP6_COMPLETION_TOKEN *TokenInItem; + + Token = (EFI_UDP6_COMPLETION_TOKEN *) Context; + TokenInItem = (EFI_UDP6_COMPLETION_TOKEN *) Item->Key; + + if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) { + // + // The Token duplicates with the TokenInItem in case either the two pointers are the + // same, or the Events of these two tokens are the same. + // + return EFI_ACCESS_DENIED; + } + + return EFI_SUCCESS; +} + + +/** + This function calculates the checksum for the Packet, utilizing the pre-calculated + pseudo HeadSum to reduce some overhead. + + @param[in] Packet Pointer to the NET_BUF contains the udp datagram. + @param[in] HeadSum Checksum of the pseudo header, execpt the length + field. + + @return The 16-bit checksum of this udp datagram. + +**/ +UINT16 +Udp6Checksum ( + IN NET_BUF *Packet, + IN UINT16 HeadSum + ) +{ + UINT16 Checksum; + + Checksum = NetbufChecksum (Packet); + Checksum = NetAddChecksum (Checksum, HeadSum); + + Checksum = NetAddChecksum (Checksum, HTONS ((UINT16) Packet->TotalSize)); + Checksum = (UINT16) (~Checksum); + return Checksum; +} + + +/** + This function removes the specified Token from the TokenMap. + + @param[in] TokenMap Pointer to the NET_MAP containing the tokens. + @param[in] Token Pointer to the Token to be removed. + + @retval EFI_SUCCESS The specified Token is removed from the TokenMap. + @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap. + +**/ +EFI_STATUS +Udp6RemoveToken ( + IN NET_MAP *TokenMap, + IN EFI_UDP6_COMPLETION_TOKEN *Token + ) +{ + NET_MAP_ITEM *Item; + + // + // Find the Token first. + // + Item = NetMapFindKey (TokenMap, (VOID *) Token); + + if (Item != NULL) { + // + // Remove the token if it's found in the map. + // + NetMapRemoveItem (TokenMap, Item, NULL); + + return EFI_SUCCESS; + } + return EFI_NOT_FOUND; +} + + +/** + This function is the packet transmitting notify function registered to the IpIo + interface. It's called to signal the udp TxToken when IpIo layer completes the + transmitting of the udp datagram. + + If Context is NULL, then ASSERT(). + If NotifyData is NULL, then ASSERT(). + + @param[in] Status The completion status of the output udp datagram. + @param[in] Context Pointer to the context data. + @param[in] Sender Specify a EFI_IP6_PROTOCOL for sending. + @param[in] NotifyData Pointer to the notify data. + +**/ +VOID +EFIAPI +Udp6DgramSent ( + IN EFI_STATUS Status, + IN VOID *Context, + IN IP_IO_IP_PROTOCOL Sender, + IN VOID *NotifyData + ) +{ + UDP6_INSTANCE_DATA *Instance; + EFI_UDP6_COMPLETION_TOKEN *Token; + + ASSERT (Context != NULL && NotifyData != NULL); + + Instance = (UDP6_INSTANCE_DATA *) Context; + Token = (EFI_UDP6_COMPLETION_TOKEN *) NotifyData; + + if (Udp6RemoveToken (&Instance->TxTokens, Token) == EFI_SUCCESS) { + // + // The token may be cancelled. Only signal it if the remove operation succeeds. + // + Token->Status = Status; + gBS->SignalEvent (Token->Event); + DispatchDpc (); + } +} + + +/** + This function processes the received datagram passed up by the IpIo layer. + + If NetSession is NULL, then ASSERT(). + If Packet is NULL, then ASSERT(). + If Context is NULL, then ASSERT(). + + @param[in] Status The status of this udp datagram. + @param[in] IcmpError The IcmpError code, only available when Status is + EFI_ICMP_ERROR. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA. + @param[in] Packet Pointer to the NET_BUF containing the received udp + datagram. + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp6DgramRcvd ( + IN EFI_STATUS Status, + IN UINT8 IcmpError, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Packet, + IN VOID *Context + ) +{ + ASSERT (NetSession != NULL && Packet != NULL && Context != NULL); + NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE); + + // + // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR. + // + if (Status == EFI_SUCCESS) { + + // + // Demultiplex the received datagram. + // + Udp6Demultiplex ((UDP6_SERVICE_DATA *) Context, NetSession, Packet); + } else { + // + // Handle the ICMP6 Error packet. + // + Udp6IcmpHandler ((UDP6_SERVICE_DATA *) Context, IcmpError, NetSession, Packet); + } + + // + // Dispatch the DPC queued by the NotifyFunction of the rx token's events + // that are signaled with received data. + // + DispatchDpc (); +} + + +/** + This function removes the multicast group specified by Arg from the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg, it's the pointer to a + multicast IPv6 Address. This parameter is + optional and may be NULL. + + @retval EFI_SUCCESS The multicast address is removed. + @retval EFI_ABORTED The specified multicast address is removed, and the + Arg is not NULL. + +**/ +EFI_STATUS +EFIAPI +Udp6LeaveGroup ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ) +{ + EFI_IPv6_ADDRESS *McastIp; + + McastIp = Arg; + + if ((McastIp != NULL) && + !EFI_IP6_EQUAL (McastIp, ((EFI_IPv6_ADDRESS *)Item->Key)) + ) { + // + // McastIp is not NULL and the multicast address contained in the Item + // is not the same as McastIp. + // + return EFI_SUCCESS; + } + + FreePool (Item->Key); + + // + // Remove this Item. + // + NetMapRemoveItem (Map, Item, NULL); + + if (McastIp != NULL) { + // + // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration. + // + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + + +/** + This function cancle the token specified by Arg in the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the token to be cancelled. If NULL, all + the tokens in this Map will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token + is not the same as that in the Item, if Arg is not + NULL. + @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is + cancelled. + +**/ +EFI_STATUS +EFIAPI +Udp6CancelTokens ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ) +{ + EFI_UDP6_COMPLETION_TOKEN *TokenToCancel; + NET_BUF *Packet; + IP_IO *IpIo; + + if ((Arg != NULL) && (Item->Key != Arg)) { + return EFI_SUCCESS; + } + + if (Item->Value != NULL) { + // + // If the token is a transmit token, the corresponding Packet is recorded in + // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken + // will invoke Udp6DgramSent, the token will be signaled and this Item will + // be removed from the Map there. + // + Packet = (NET_BUF *) (Item->Value); + IpIo = (IP_IO *) (*((UINTN *) &Packet->ProtoData[0])); + + IpIoCancelTxToken (IpIo, Packet); + } else { + // + // The token is a receive token. Abort it and remove it from the Map. + // + TokenToCancel = (EFI_UDP6_COMPLETION_TOKEN *) Item->Key; + NetMapRemoveItem (Map, Item, NULL); + + TokenToCancel->Status = EFI_ABORTED; + gBS->SignalEvent (TokenToCancel->Event); + } + + if (Arg != NULL) { + return EFI_ABORTED; + } + + return EFI_SUCCESS; +} + + +/** + This function removes all the Wrap datas in the RcvdDgramQue. + + @param[in] Instance Pointer to the Udp6 Instance. + +**/ +VOID +Udp6FlushRcvdDgram ( + IN UDP6_INSTANCE_DATA *Instance + ) +{ + UDP6_RXDATA_WRAP *Wrap; + + while (!IsListEmpty (&Instance->RcvdDgramQue)) { + // + // Iterate all the Wraps in the RcvdDgramQue. + // + Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP6_RXDATA_WRAP, Link); + + // + // The Wrap will be removed from the RcvdDgramQue by this function call. + // + Udp6RecycleRxDataWrap (NULL, (VOID *) Wrap); + } +} + + + +/** + Cancel Udp6 tokens from the Udp6 instance. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] Token Pointer to the token to be canceled. If NULL, all + tokens in this instance will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The Token is cancelled. + @retval EFI_NOT_FOUND The Token is not found. + +**/ +EFI_STATUS +Udp6InstanceCancelToken ( + IN UDP6_INSTANCE_DATA *Instance, + IN EFI_UDP6_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + EFI_STATUS Status; + + // + // Cancel this token from the TxTokens map. + // + Status = NetMapIterate (&Instance->TxTokens, Udp6CancelTokens, Token); + + if ((Token != NULL) && (Status == EFI_ABORTED)) { + // + // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from + // the TxTokens and returns success. + // + return EFI_SUCCESS; + } + + // + // Try to cancel this token from the RxTokens map in condition either the Token + // is NULL or the specified Token is not in TxTokens. + // + Status = NetMapIterate (&Instance->RxTokens, Udp6CancelTokens, Token); + + if ((Token != NULL) && (Status == EFI_SUCCESS)) { + // + // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the + // TxTokens nor the RxTokens, or say, it's not found. + // + return EFI_NOT_FOUND; + } + + ASSERT ((Token != NULL) || + ((0 == NetMapGetCount (&Instance->TxTokens)) && + (0 == NetMapGetCount (&Instance->RxTokens))) + ); + + return EFI_SUCCESS; +} + + +/** + This function checks if the received udp datagram matches with the Instance. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] Udp6Session Pointer to the EFI_UDP6_SESSION_DATA abstracted + from the received udp datagram. + + @retval TRUE The udp datagram matches the receiving requirements of the Instance. + @retval FALSE The udp datagram does not matche the receiving requirements of the Instance. + +**/ +BOOLEAN +Udp6MatchDgram ( + IN UDP6_INSTANCE_DATA *Instance, + IN EFI_UDP6_SESSION_DATA *Udp6Session + ) +{ + EFI_UDP6_CONFIG_DATA *ConfigData; + EFI_IPv6_ADDRESS Destination; + + ConfigData = &Instance->ConfigData; + + if (ConfigData->AcceptPromiscuous) { + // + // Always matches if this instance is in the promiscuous state. + // + return TRUE; + } + + if ((!ConfigData->AcceptAnyPort && (Udp6Session->DestinationPort != ConfigData->StationPort)) || + ((ConfigData->RemotePort != 0) && (Udp6Session->SourcePort != ConfigData->RemotePort)) + ) { + // + // The local port or the remote port doesn't match. + // + return FALSE; + } + + if (!NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress) && + !EFI_IP6_EQUAL (&ConfigData->RemoteAddress, &Udp6Session->SourceAddress) + ) { + // + // This datagram doesn't come from the instance's specified sender. + // + return FALSE; + } + + if (NetIp6IsUnspecifiedAddr (&ConfigData->StationAddress) || + EFI_IP6_EQUAL (&Udp6Session->DestinationAddress, &ConfigData->StationAddress) + ) { + // + // The instance is configured to receive datagrams destinated to any station IP or + // the destination address of this datagram matches the configured station IP. + // + return TRUE; + } + + IP6_COPY_ADDRESS (&Destination, &Udp6Session->DestinationAddress); + + if (IP6_IS_MULTICAST (&Destination) && + (NULL != Udp6MapMultiCastAddr (&Instance->McastIps, &Destination)) + ) { + // + // It's a multicast packet and the multicast address is accepted by this instance. + // + return TRUE; + } + + return FALSE; +} + + +/** + This function removes the Wrap specified by Context and release relevant resources. + + @param[in] Event The Event this notify function registered to. + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp6RecycleRxDataWrap ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + UDP6_RXDATA_WRAP *Wrap; + + Wrap = (UDP6_RXDATA_WRAP *) Context; + + // + // Remove the Wrap from the list it belongs to. + // + RemoveEntryList (&Wrap->Link); + + // + // Free the Packet associated with this Wrap. + // + NetbufFree (Wrap->Packet); + + // + // Close the event. + // + gBS->CloseEvent (Wrap->RxData.RecycleSignal); + + FreePool (Wrap); +} + + +/** + This function wraps the Packet into RxData. + + @param[in] Instance Pointer to the instance context data. + @param[in] Packet Pointer to the buffer containing the received + datagram. + @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this + datagram. + + @return Pointer to the structure wrapping the RxData and the Packet. NULL will + be returned if any error occurs. + +**/ +UDP6_RXDATA_WRAP * +Udp6WrapRxData ( + IN UDP6_INSTANCE_DATA *Instance, + IN NET_BUF *Packet, + IN EFI_UDP6_RECEIVE_DATA *RxData + ) +{ + EFI_STATUS Status; + UDP6_RXDATA_WRAP *Wrap; + + // + // Allocate buffer for the Wrap. + // + Wrap = AllocateZeroPool (sizeof (UDP6_RXDATA_WRAP) + + (Packet->BlockOpNum - 1) * sizeof (EFI_UDP6_FRAGMENT_DATA)); + if (Wrap == NULL) { + return NULL; + } + + InitializeListHead (&Wrap->Link); + + CopyMem (&Wrap->RxData, RxData, sizeof(EFI_UDP6_RECEIVE_DATA)); + // + // Create the Recycle event. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + Udp6RecycleRxDataWrap, + Wrap, + &Wrap->RxData.RecycleSignal + ); + if (EFI_ERROR (Status)) { + FreePool (Wrap); + return NULL; + } + + Wrap->Packet = Packet; + Wrap->TimeoutTick = Instance->ConfigData.ReceiveTimeout; + + return Wrap; +} + + +/** + This function enqueues the received datagram into the instances' receiving queues. + + @param[in] Udp6Service Pointer to the udp service context data. + @param[in] Packet Pointer to the buffer containing the received + datagram. + @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this + datagram. + + @return The times this datagram is enqueued. + +**/ +UINTN +Udp6EnqueueDgram ( + IN UDP6_SERVICE_DATA *Udp6Service, + IN NET_BUF *Packet, + IN EFI_UDP6_RECEIVE_DATA *RxData + ) +{ + LIST_ENTRY *Entry; + UDP6_INSTANCE_DATA *Instance; + UDP6_RXDATA_WRAP *Wrap; + UINTN Enqueued; + + Enqueued = 0; + + NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) { + // + // Iterate the instances. + // + Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link); + + if (!Instance->Configured) { + continue; + } + + if (Udp6MatchDgram (Instance, &RxData->UdpSession)) { + // + // Wrap the RxData and put this Wrap into the instances RcvdDgramQue. + // + Wrap = Udp6WrapRxData (Instance, Packet, RxData); + if (Wrap == NULL) { + continue; + } + + NET_GET_REF (Packet); + + InsertTailList (&Instance->RcvdDgramQue, &Wrap->Link); + + Enqueued++; + } + } + + return Enqueued; +} + + +/** + This function delivers the received datagrams to the specified instance. + + @param[in] Instance Pointer to the instance context data. + +**/ +VOID +Udp6InstanceDeliverDgram ( + IN UDP6_INSTANCE_DATA *Instance + ) +{ + UDP6_RXDATA_WRAP *Wrap; + EFI_UDP6_COMPLETION_TOKEN *Token; + NET_BUF *Dup; + EFI_UDP6_RECEIVE_DATA *RxData; + EFI_TPL OldTpl; + + if (!IsListEmpty (&Instance->RcvdDgramQue) && + !NetMapIsEmpty (&Instance->RxTokens) + ) { + + Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP6_RXDATA_WRAP, Link); + + if (NET_BUF_SHARED (Wrap->Packet)) { + // + // Duplicate the Packet if it is shared between instances. + // + Dup = NetbufDuplicate (Wrap->Packet, NULL, 0); + if (Dup == NULL) { + return; + } + + NetbufFree (Wrap->Packet); + + Wrap->Packet = Dup; + } + + NetListRemoveHead (&Instance->RcvdDgramQue); + + Token = (EFI_UDP6_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL); + + // + // Build the FragmentTable and set the FragmentCount in RxData. + // + RxData = &Wrap->RxData; + RxData->FragmentCount = Wrap->Packet->BlockOpNum; + + NetbufBuildExt ( + Wrap->Packet, + (NET_FRAGMENT *) RxData->FragmentTable, + &RxData->FragmentCount + ); + + Token->Status = EFI_SUCCESS; + Token->Packet.RxData = &Wrap->RxData; + + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + InsertTailList (&Instance->DeliveredDgramQue, &Wrap->Link); + gBS->RestoreTPL (OldTpl); + + gBS->SignalEvent (Token->Event); + } +} + + +/** + This function delivers the datagrams enqueued in the instances. + + @param[in] Udp6Service Pointer to the udp service context data. + +**/ +VOID +Udp6DeliverDgram ( + IN UDP6_SERVICE_DATA *Udp6Service + ) +{ + LIST_ENTRY *Entry; + UDP6_INSTANCE_DATA *Instance; + + NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) { + // + // Iterate the instances. + // + Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link); + + if (!Instance->Configured) { + continue; + } + + // + // Deliver the datagrams of this instance. + // + Udp6InstanceDeliverDgram (Instance); + } +} + + +/** + This function demultiplexes the received udp datagram to the appropriate instances. + + @param[in] Udp6Service Pointer to the udp service context data. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from + the received datagram. + @param[in] Packet Pointer to the buffer containing the received udp + datagram. + +**/ +VOID +Udp6Demultiplex ( + IN UDP6_SERVICE_DATA *Udp6Service, + IN EFI_NET_SESSION_DATA *NetSession, + IN NET_BUF *Packet + ) +{ + EFI_UDP_HEADER *Udp6Header; + UINT16 HeadSum; + EFI_UDP6_RECEIVE_DATA RxData; + EFI_UDP6_SESSION_DATA *Udp6Session; + UINTN Enqueued; + + if (Packet->TotalSize < UDP6_HEADER_SIZE) { + NetbufFree (Packet); + return; + } + + // + // Get the datagram header from the packet buffer. + // + Udp6Header = (EFI_UDP_HEADER *) NetbufGetByte (Packet, 0, NULL); + ASSERT (Udp6Header != NULL); + if (Udp6Header == NULL) { + NetbufFree (Packet); + return; + } + + if (Udp6Header->Checksum != 0) { + // + // check the checksum. + // + HeadSum = NetIp6PseudoHeadChecksum ( + &NetSession->Source.v6, + &NetSession->Dest.v6, + EFI_IP_PROTO_UDP, + 0 + ); + + if (Udp6Checksum (Packet, HeadSum) != 0) { + // + // Wrong checksum. + // + NetbufFree (Packet); + return; + } + } + + Udp6Session = &RxData.UdpSession; + Udp6Session->SourcePort = NTOHS (Udp6Header->SrcPort); + Udp6Session->DestinationPort = NTOHS (Udp6Header->DstPort); + + IP6_COPY_ADDRESS (&Udp6Session->SourceAddress, &NetSession->Source); + IP6_COPY_ADDRESS (&Udp6Session->DestinationAddress, &NetSession->Dest); + + // + // Trim the UDP header. + // + NetbufTrim (Packet, UDP6_HEADER_SIZE, TRUE); + + RxData.DataLength = (UINT32) Packet->TotalSize; + + // + // Try to enqueue this datagram into the instances. + // + Enqueued = Udp6EnqueueDgram (Udp6Service, Packet, &RxData); + + if (Enqueued == 0) { + // + // Send the port unreachable ICMP packet before we free this NET_BUF + // + Udp6SendPortUnreach (Udp6Service->IpIo, NetSession, Udp6Header); + } + + // + // Try to free the packet before deliver it. + // + NetbufFree (Packet); + + if (Enqueued > 0) { + // + // Deliver the datagram. + // + Udp6DeliverDgram (Udp6Service); + } +} + + +/** + This function builds and sends out a icmp port unreachable message. + + @param[in] IpIo Pointer to the IP_IO instance. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet + causes this icmp error message. + @param[in] Udp6Header Pointer to the udp header of the datagram causes + this icmp error message. + +**/ +VOID +Udp6SendPortUnreach ( + IN IP_IO *IpIo, + IN EFI_NET_SESSION_DATA *NetSession, + IN VOID *Udp6Header + ) +{ + NET_BUF *Packet; + UINT32 Len; + IP6_ICMP_ERROR_HEAD *IcmpErrHdr; + UINT8 *Ptr; + IP_IO_OVERRIDE Override; + IP_IO_IP_INFO *IpSender; + EFI_IP6_MODE_DATA *Ip6ModeData; + EFI_STATUS Status; + EFI_IP6_PROTOCOL *Ip6Protocol; + + Ip6ModeData = NULL; + + // + // An ICMPv6 error message MUST NOT be originated as A packet destined to + // 1) an IPv6 multicast address 2) The IPv6 Unspecified Address + // + if (NetSession->IpVersion == IP_VERSION_6) { + if (NetIp6IsUnspecifiedAddr (&NetSession->Dest.v6) || + IP6_IS_MULTICAST (&NetSession->Dest.v6) + ) { + goto EXIT; + } + } + + + IpSender = IpIoFindSender (&IpIo, NetSession->IpVersion, &NetSession->Dest); + + // + // Get the Ipv6 Mode Data. + // + Ip6ModeData = AllocateZeroPool (sizeof (EFI_IP6_MODE_DATA)); + ASSERT (Ip6ModeData != NULL); + if (Ip6ModeData == NULL) { + goto EXIT; + } + + // + // If not finding the related IpSender use the default IpIo to send out + // the port unreachable ICMP message. + // + if (IpSender == NULL) { + Ip6Protocol = IpIo->Ip.Ip6; + } else { + Ip6Protocol = IpSender->Ip.Ip6; + } + + Status = Ip6Protocol->GetModeData ( + Ip6Protocol, + Ip6ModeData, + NULL, + NULL + ); + + if (EFI_ERROR (Status)) { + goto EXIT; + } + // + // The ICMP6 packet length, includes whole invoking packet and ICMP6 error header. + // + Len = NetSession->IpHdrLen + + NTOHS(((EFI_UDP_HEADER *) Udp6Header)->Length) + + sizeof (IP6_ICMP_ERROR_HEAD); + + // + // If the ICMP6 packet length larger than IP MTU, adjust its length to MTU. + // + if (Ip6ModeData->MaxPacketSize < Len) { + Len = Ip6ModeData->MaxPacketSize; + } + + // + // Allocate buffer for the icmp error message. + // + Packet = NetbufAlloc (Len); + if (Packet == NULL) { + goto EXIT; + } + + // + // Allocate space for the IP6_ICMP_ERROR_HEAD. + // + IcmpErrHdr = (IP6_ICMP_ERROR_HEAD *) NetbufAllocSpace (Packet, Len, FALSE); + ASSERT (IcmpErrHdr != NULL); + if (IcmpErrHdr == NULL) { + goto EXIT; + } + + // + // Set the required fields for the icmp port unreachable message. + // + IcmpErrHdr->Head.Type = ICMP_V6_DEST_UNREACHABLE; + IcmpErrHdr->Head.Code = ICMP_V6_PORT_UNREACHABLE; + IcmpErrHdr->Head.Checksum = 0; + IcmpErrHdr->Fourth = 0; + + // + // Copy as much of invoking Packet as possible without the ICMPv6 packet + // exceeding the minimum Ipv6 MTU. The length of IP6_ICMP_ERROR_HEAD contains + // the length of EFI_IP6_HEADER, so when using the length of IP6_ICMP_ERROR_HEAD + // for pointer movement that fact should be considered. + // + Ptr = (VOID *) &IcmpErrHdr->Head; + Ptr = (UINT8 *) (UINTN) ((UINTN) Ptr + sizeof (IP6_ICMP_ERROR_HEAD) - sizeof (EFI_IP6_HEADER)); + CopyMem (Ptr, NetSession->IpHdr.Ip6Hdr, NetSession->IpHdrLen); + CopyMem ( + Ptr + NetSession->IpHdrLen, + Udp6Header, + Len - NetSession->IpHdrLen - sizeof (IP6_ICMP_ERROR_HEAD) + sizeof (EFI_IP6_HEADER) + ); + + // + // Set the checksum as zero, and IP6 driver will calcuate it with pseudo header. + // + IcmpErrHdr->Head.Checksum = 0; + + // + // Fill the override data. + // + Override.Ip6OverrideData.FlowLabel = 0; + Override.Ip6OverrideData.HopLimit = 255; + Override.Ip6OverrideData.Protocol = IP6_ICMP; + + // + // Send out this icmp packet. + // + IpIoSend (IpIo, Packet, IpSender, NULL, NULL, &NetSession->Source, &Override); + + NetbufFree (Packet); + +EXIT: + if (Ip6ModeData != NULL) { + FreePool (Ip6ModeData); + } +} + + +/** + This function handles the received Icmp Error message and de-multiplexes it to the + instance. + + @param[in] Udp6Service Pointer to the udp service context data. + @param[in] IcmpError The icmp error code. + @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted + from the received Icmp Error packet. + @param[in, out] Packet Pointer to the Icmp Error packet. + +**/ +VOID +Udp6IcmpHandler ( + IN UDP6_SERVICE_DATA *Udp6Service, + IN UINT8 IcmpError, + IN EFI_NET_SESSION_DATA *NetSession, + IN OUT NET_BUF *Packet + ) +{ + EFI_UDP_HEADER *Udp6Header; + EFI_UDP6_SESSION_DATA Udp6Session; + LIST_ENTRY *Entry; + UDP6_INSTANCE_DATA *Instance; + + if (Packet->TotalSize < UDP6_HEADER_SIZE) { + NetbufFree (Packet); + return; + } + + Udp6Header = (EFI_UDP_HEADER *) NetbufGetByte (Packet, 0, NULL); + ASSERT (Udp6Header != NULL); + if (Udp6Header == NULL) { + NetbufFree (Packet); + return; + } + + IP6_COPY_ADDRESS (&Udp6Session.SourceAddress, &NetSession->Source); + IP6_COPY_ADDRESS (&Udp6Session.DestinationAddress, &NetSession->Dest); + + Udp6Session.SourcePort = NTOHS (Udp6Header->DstPort); + Udp6Session.DestinationPort = NTOHS (Udp6Header->SrcPort); + + NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) { + // + // Iterate all the instances. + // + Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link); + + if (!Instance->Configured) { + continue; + } + + if (Udp6MatchDgram (Instance, &Udp6Session)) { + // + // Translate the Icmp Error code according to the udp spec. + // + Instance->IcmpError = IpIoGetIcmpErrStatus (IcmpError, IP_VERSION_6, NULL, NULL); + + if (IcmpError > ICMP_ERR_UNREACH_PORT) { + Instance->IcmpError = EFI_ICMP_ERROR; + } + + // + // Notify the instance with the received Icmp Error. + // + Udp6ReportIcmpError (Instance); + + break; + } + } + + NetbufFree (Packet); +} + + +/** + This function reports the received ICMP error. + + @param[in] Instance Pointer to the udp instance context data. + +**/ +VOID +Udp6ReportIcmpError ( + IN UDP6_INSTANCE_DATA *Instance + ) +{ + EFI_UDP6_COMPLETION_TOKEN *Token; + + if (NetMapIsEmpty (&Instance->RxTokens)) { + // + // There are no receive tokens to deliver the ICMP error. + // + return; + } + + if (EFI_ERROR (Instance->IcmpError)) { + // + // Try to get a RxToken from the RxTokens map. + // + Token = (EFI_UDP6_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL); + + if (Token != NULL) { + // + // Report the error through the Token. + // + Token->Status = Instance->IcmpError; + gBS->SignalEvent (Token->Event); + + // + // Clear the IcmpError. + // + Instance->IcmpError = EFI_SUCCESS; + } + } +} + + +/** + This function is a dummy ext-free function for the NET_BUF created for the output + udp datagram. + + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp6NetVectorExtFree ( + IN VOID *Context + ) +{ +} + +/** + Find the key in the netmap. + + @param[in] Map The netmap to search within. + @param[in] Key The key to search. + + @return The point to the item contains the Key, or NULL, if Key isn't in the map. + +**/ +NET_MAP_ITEM * +Udp6MapMultiCastAddr ( + IN NET_MAP *Map, + IN VOID *Key + ) +{ + LIST_ENTRY *Entry; + NET_MAP_ITEM *Item; + EFI_IPv6_ADDRESS *Addr; + + ASSERT (Map != NULL); + NET_LIST_FOR_EACH (Entry, &Map->Used) { + Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link); + Addr = (EFI_IPv6_ADDRESS *) Item->Key; + if (EFI_IP6_EQUAL (Addr, Key)) { + return Item; + } + } + return NULL; +} + diff --git a/NetworkPkg/Udp6Dxe/Udp6Impl.h b/NetworkPkg/Udp6Dxe/Udp6Impl.h new file mode 100644 index 000000000..5ccbcebfd --- /dev/null +++ b/NetworkPkg/Udp6Dxe/Udp6Impl.h @@ -0,0 +1,648 @@ +/** @file + Udp6 driver's whole implementation and internal data structures. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _UDP6_IMPL_H_ +#define _UDP6_IMPL_H_ + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Udp6Driver.h" + +extern EFI_COMPONENT_NAME2_PROTOCOL gUdp6ComponentName2; +extern EFI_COMPONENT_NAME_PROTOCOL gUdp6ComponentName; +extern EFI_UNICODE_STRING_TABLE *gUdp6ControllerNameTable; +extern EFI_SERVICE_BINDING_PROTOCOL mUdp6ServiceBinding; +extern EFI_UDP6_PROTOCOL mUdp6Protocol; +extern UINT16 mUdp6RandomPort; + +// +// Define time out 50 milliseconds +// +#define UDP6_TIMEOUT_INTERVAL (50 * TICKS_PER_MS) +#define UDP6_HEADER_SIZE sizeof (EFI_UDP_HEADER) +#define UDP6_MAX_DATA_SIZE 65507 +#define UDP6_PORT_KNOWN 1024 + +#define UDP6_SERVICE_DATA_SIGNATURE SIGNATURE_32 ('U', 'd', 'p', '6') +#define UDP6_INSTANCE_DATA_SIGNATURE SIGNATURE_32 ('U', 'd', 'p', 'S') + +#define UDP6_SERVICE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + UDP6_SERVICE_DATA, \ + ServiceBinding, \ + UDP6_SERVICE_DATA_SIGNATURE \ + ) + +#define UDP6_INSTANCE_DATA_FROM_THIS(a) \ + CR ( \ + (a), \ + UDP6_INSTANCE_DATA, \ + Udp6Proto, \ + UDP6_INSTANCE_DATA_SIGNATURE \ + ) +// +// Udp6 service contest data +// +typedef struct _UDP6_SERVICE_DATA { + UINT32 Signature; + EFI_SERVICE_BINDING_PROTOCOL ServiceBinding; + EFI_HANDLE ImageHandle; + EFI_HANDLE ControllerHandle; + LIST_ENTRY ChildrenList; + UINTN ChildrenNumber; + IP_IO *IpIo; + EFI_EVENT TimeoutEvent; + } UDP6_SERVICE_DATA; + +typedef struct _UDP6_INSTANCE_DATA { + UINT32 Signature; + LIST_ENTRY Link; + UDP6_SERVICE_DATA *Udp6Service; + EFI_UDP6_PROTOCOL Udp6Proto; + EFI_UDP6_CONFIG_DATA ConfigData; + EFI_HANDLE ChildHandle; + BOOLEAN Configured; + BOOLEAN IsNoMapping; + NET_MAP TxTokens; + NET_MAP RxTokens; + NET_MAP McastIps; + LIST_ENTRY RcvdDgramQue; + LIST_ENTRY DeliveredDgramQue; + UINT16 HeadSum; + EFI_STATUS IcmpError; + IP_IO_IP_INFO *IpInfo; + BOOLEAN InDestroy; +} UDP6_INSTANCE_DATA; + +typedef struct _UDP6_RXDATA_WRAP { + LIST_ENTRY Link; + NET_BUF *Packet; + UINT32 TimeoutTick; + EFI_UDP6_RECEIVE_DATA RxData; +} UDP6_RXDATA_WRAP; + +typedef struct { + EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding; + UINTN NumberOfChildren; + EFI_HANDLE *ChildHandleBuffer; +} UDP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT; + +/** + Clean the Udp service context data. + + @param[in, out] Udp6Service Pointer to the UDP6_SERVICE_DATA. + +**/ +VOID +Udp6CleanService ( + IN OUT UDP6_SERVICE_DATA *Udp6Service + ); + +/** + Create the Udp service context data. + + @param[in] Udp6Service Pointer to the UDP6_SERVICE_DATA. + @param[in] ImageHandle The image handle of this udp6 driver. + @param[in] ControllerHandle The controller handle this udp6 driver binds on. + + @retval EFI_SUCCESS The udp6 service context data was created and + initialized. + @retval EFI_OUT_OF_RESOURCES Cannot allocate memory. + @retval Others An error condition occurred. + +**/ +EFI_STATUS +Udp6CreateService ( + IN UDP6_SERVICE_DATA *Udp6Service, + IN EFI_HANDLE ImageHandle, + IN EFI_HANDLE ControllerHandle + ); + +/** + This function cleans the udp instance. + + @param[in, out] Instance Pointer to the UDP6_INSTANCE_DATA to clean. + +**/ +VOID +Udp6CleanInstance ( + IN OUT UDP6_INSTANCE_DATA *Instance + ); + +/** + This function intializes the new created udp instance. + + @param[in] Udp6Service Pointer to the UDP6_SERVICE_DATA. + @param[in, out] Instance Pointer to the un-initialized UDP6_INSTANCE_DATA. + +**/ +VOID +Udp6InitInstance ( + IN UDP6_SERVICE_DATA *Udp6Service, + IN OUT UDP6_INSTANCE_DATA *Instance + ); + +/** + This function reports the received ICMP error. + + @param[in] Instance Pointer to the udp instance context data. + +**/ +VOID +Udp6ReportIcmpError ( + IN UDP6_INSTANCE_DATA *Instance + ); + +/** + This function copies the current operational settings of this EFI UDPv6 Protocol + instance into user-supplied buffers. This function is used optionally to retrieve + the operational mode data of underlying networks or drivers. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[out] Udp6ConfigData The buffer in which the current UDP configuration + data is returned. This parameter is optional and + may be NULL. + @param[out] Ip6ModeData The buffer in which the current EFI IPv6 Protocol + mode data is returned. This parameter is optional + and may be NULL. + @param[out] MnpConfigData The buffer in which the current managed network + configuration data is returned. This parameter + is optional and may be NULL. + @param[out] SnpModeData The buffer in which the simple network mode data + is returned. This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The mode data was read. + @retval EFI_NOT_STARTED When Udp6ConfigData is queried, no configuration + data is available because this instance has not + been started. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +Udp6GetModeData ( + IN EFI_UDP6_PROTOCOL *This, + OUT EFI_UDP6_CONFIG_DATA *Udp6ConfigData OPTIONAL, + OUT EFI_IP6_MODE_DATA *Ip6ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ); + +/** + This function is used to do the following: + Initialize and start this instance of the EFI UDPv6 Protocol. + Change the filtering rules and operational parameters. + Reset this instance of the EFI UDPv6 Protocol. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[in] UdpConfigData Pointer to the buffer to set the configuration + data. This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The configuration settings were set, changed, or + reset successfully. + @retval EFI_NO_MAPPING When the UdpConifgData.UseAnyStationAddress is set + to true and there is no address available for IP6 + driver to binding source address to this + instance. + @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE: + This is NULL. + UdpConfigData.StationAddress is not a valid + unicast IPv6 address. + UdpConfigData.RemoteAddress is not a valid unicast + IPv6 address, if it is not zero. + @retval EFI_ALREADY_STARTED The EFI UDPv6 Protocol instance is already + started/configured and must be stopped/reset + before it can be reconfigured. Only TrafficClass, + HopLimit, ReceiveTimeout, and TransmitTimeout can + be reconfigured without stopping the current + instance of the EFI UDPv6 Protocol. + @retval EFI_ACCESS_DENIED UdpConfigData.AllowDuplicatePort is FALSE, and + UdpConfigData.StationPort is already used by another + instance. + @retval EFI_OUT_OF_RESOURCES The EFI UDPv6 Protocol driver cannot allocate + memory for this EFI UDPv6 Protocol instance. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred, and + this instance was not opened. + +**/ +EFI_STATUS +EFIAPI +Udp6Configure ( + IN EFI_UDP6_PROTOCOL *This, + IN EFI_UDP6_CONFIG_DATA *UdpConfigData OPTIONAL + ); + +/** + This function places a sending request to this instance of the EFI UDPv6 Protocol, + alongside the transmit data that was filled by the user. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[in] Token Pointer to the completion token that will be + placed into the transmit queue. + + @retval EFI_SUCCESS The data has been queued for transmission. + @retval EFI_NOT_STARTED This EFI UDPv6 Protocol instance has not been + started. + @retval EFI_NO_MAPPING The under-layer IPv6 driver was responsible for + choosing a source address for this instance, but + no source address was available for use. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + This is NULL. Token is NULL. Token.Event is NULL. + Token.Packet.TxData is NULL. + Token.Packet.TxData.FragmentCount is zero. + Token.Packet.TxData.DataLength is not equal to the + sum of fragment lengths. + One or more of the + Token.Packet.TxData.FragmentTable[] + .FragmentLength fields is zero. + One or more of the + Token.Packet.TxData.FragmentTable[] + .FragmentBuffer fields is NULL. + One or more of the + Token.Packet.TxData.UdpSessionData. + DestinationAddres are not valid unicast IPv6 + addresses, if the UdpSessionData is not NULL. + Token.Packet.TxData.UdpSessionData. + DestinationAddres is NULL + Token.Packet.TxData.UdpSessionData. + DestinatioPort is zero. + Token.Packet.TxData.UdpSessionData is + NULL and this instance's + UdpConfigData.RemoteAddress is unspecified. + @retval EFI_ACCESS_DENIED The transmit completion token with the same + Token.Event is already in the transmit queue. + @retval EFI_NOT_READY The completion token could not be queued because + the transmit queue is full. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data. + @retval EFI_NOT_FOUND There is no route to the destination network or + address. + @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP + packet size. Or the length of the IP header + UDP + header + data length is greater than MTU if + DoNotFragment is TRUE. + +**/ +EFI_STATUS +EFIAPI +Udp6Transmit ( + IN EFI_UDP6_PROTOCOL *This, + IN EFI_UDP6_COMPLETION_TOKEN *Token + ); + +/** + This function places a completion token into the receive packet queue. This function + is always asynchronous. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[in] Token Pointer to a token that is associated with the + receive data descriptor. + + @retval EFI_SUCCESS The receive completion token is cached. + @retval EFI_NOT_STARTED This EFI UDPv6 Protocol instance has not been + started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, + BOOTP, RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + Token is NULL. + Token.Event is NULL. + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued + due to a lack of system resources (usually + memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The EFI UDPv6 Protocol instance has been reset to + startup defaults. + @retval EFI_ACCESS_DENIED A receive completion token with the same + Token.Event is already in the receive queue. + @retval EFI_NOT_READY The receive request could not be queued because + the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +Udp6Receive ( + IN EFI_UDP6_PROTOCOL *This, + IN EFI_UDP6_COMPLETION_TOKEN *Token + ); + +/** + This function is used to abort a pending transmit or receive request. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_UDP6_PROTOCOL.Transmit() or + EFI_UDP6_PROTOCOL.Receive(). This parameter is + optional and may be NULL. + + @retval EFI_SUCCESS The asynchronous I/O request is aborted and + Token.Event is signaled. When Token is NULL, all + pending requests are aborted and their events are + signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration + (DHCP, BOOTP, RARP, etc.) is not finished yet. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O + request is not found in the transmit or receive + queue. It either completed or was not issued by + Transmit() or Receive(). + +**/ +EFI_STATUS +EFIAPI +Udp6Cancel ( + IN EFI_UDP6_PROTOCOL *This, + IN EFI_UDP6_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + This function can be used by network drivers and applications to increase the rate that + data packets are moved between the communications device and the transmit/receive queues. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or + receive queue. + +**/ +EFI_STATUS +EFIAPI +Udp6Poll ( + IN EFI_UDP6_PROTOCOL *This + ); + +/** + This function is used to enable and disable the multicast group filtering. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join a multicast group. Set to + FALSE to leave one or all multicast groups. + @param[in] MulticastAddress Pointer to multicast group address to join or + leave. This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The EFI UDPv6 Protocol instance has not been + started. + @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. JoinFlag is TRUE and + MulticastAddress is NULL. JoinFlag is TRUE and + *MulticastAddress is not a valid multicast + address. + @retval EFI_ALREADY_STARTED The group address is already in the group table + (when JoinFlag is TRUE). + @retval EFI_NOT_FOUND The group address is not in the group table (when + JoinFlag is FALSE). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +Udp6Groups ( + IN EFI_UDP6_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_IPv6_ADDRESS *MulticastAddress OPTIONAL + ); + +/** + This function tries to bind the udp instance according to the configured port + allocation stragety. + + @param[in] InstanceList Pointer to the head of the list linking the udp + instances. + @param[in] ConfigData Pointer to the ConfigData of the instance to be + bound. + + @retval EFI_SUCCESS The bound operation completed successfully. + @retval EFI_ACCESS_DENIED The specified by the ConfigData is + already used by another instance. + @retval EFI_OUT_OF_RESOURCES No available port resources. + +**/ +EFI_STATUS +Udp6Bind ( + IN LIST_ENTRY *InstanceList, + IN EFI_UDP6_CONFIG_DATA *ConfigData + ); + +/** + This function builds the Ip6 configdata from the Udp6ConfigData. + + @param[in] Udp6ConfigData Pointer to the EFI_UDP6_CONFIG_DATA. + @param[in, out] Ip6ConfigData Pointer to the EFI_IP6_CONFIG_DATA. + +**/ +VOID +Udp6BuildIp6ConfigData ( + IN EFI_UDP6_CONFIG_DATA *Udp6ConfigData, + IN OUT EFI_IP6_CONFIG_DATA *Ip6ConfigData + ); + +/** + This function checks whether the specified Token duplicates with the one in the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to + the Token. + @param[in] Context Pointer to the Token to be checked. + + @retval EFI_SUCCESS The Token specified by Context differs from the + one in the Item. + @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item. + +**/ +EFI_STATUS +EFIAPI +Udp6TokenExist ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Context + ); + +/** + This function removes the specified Token from the TokenMap. + + @param[in] TokenMap Pointer to the NET_MAP containing the tokens. + @param[in] Token Pointer to the Token to be removed. + + @retval EFI_SUCCESS The specified Token is removed from the TokenMap. + @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap. + +**/ +EFI_STATUS +Udp6RemoveToken ( + IN NET_MAP *TokenMap, + IN EFI_UDP6_COMPLETION_TOKEN *Token + ); + +/** + This function is used to check whether the NewConfigData has any un-reconfigurable + parameters changed compared to the OldConfigData. + + @param[in] OldConfigData Pointer to the current ConfigData the udp instance + uses. + @param[in] NewConfigData Pointer to the new ConfigData. + + @retval TRUE The instance is reconfigurable according to NewConfigData. + @retval FALSE The instance is not reconfigurable according to NewConfigData. + +**/ +BOOLEAN +Udp6IsReconfigurable ( + IN EFI_UDP6_CONFIG_DATA *OldConfigData, + IN EFI_UDP6_CONFIG_DATA *NewConfigData + ); + +/** + This function removes the multicast group specified by Arg from the Map. + + @param[in] Map Pointer to the NET_MAP. + @param[in] Item Pointer to the NET_MAP_ITEM. + @param[in] Arg Pointer to the Arg. It is the pointer to a + multicast IPv6 Address. This parameter is + optional and may be NULL. + + @retval EFI_SUCCESS The multicast address is removed. + @retval EFI_ABORTED The specified multicast address is removed, and the + Arg is not NULL. + +**/ +EFI_STATUS +EFIAPI +Udp6LeaveGroup ( + IN NET_MAP *Map, + IN NET_MAP_ITEM *Item, + IN VOID *Arg OPTIONAL + ); + +/** + This function validates the TxToken, it returns the error code according to the spec. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] TxToken Pointer to the token to be checked. + + @retval EFI_SUCCESS The TxToken is valid. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + Token.Event is NULL. + Token.Packet.TxData is NULL. + Token.Packet.TxData.FragmentCount is zero. + Token.Packet.TxData.DataLength is not equal to the + sum of fragment lengths. + One or more of the + Token.Packet.TxData.FragmentTable[].FragmentLength + fields is zero. + One or more of the + Token.Packet.TxData.FragmentTable[].FragmentBuffer + fields is NULL. + UdpSessionData.DestinationAddress are not valid + unicast IPv6 addresses if the UdpSessionData is + not NULL. + UdpSessionData.DestinationPort and + ConfigData.RemotePort are all zero if the + UdpSessionData is not NULL. + @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP + packet size. + +**/ +EFI_STATUS +Udp6ValidateTxToken ( + IN UDP6_INSTANCE_DATA *Instance, + IN EFI_UDP6_COMPLETION_TOKEN *TxToken + ); + +/** + This function is a dummy ext-free function for the NET_BUF created for the output + udp datagram. + + @param[in] Context Pointer to the context data. + +**/ +VOID +EFIAPI +Udp6NetVectorExtFree ( + IN VOID *Context + ); + +/** + This function calculates the checksum for the Packet, utilizing the pre-calculated + pseudo header to reduce overhead. + + @param[in] Packet Pointer to the NET_BUF contains the udp datagram. + @param[in] HeadSum Checksum of the pseudo header execpt the length + field. + + @return The 16-bit checksum of this udp datagram. + +**/ +UINT16 +Udp6Checksum ( + IN NET_BUF *Packet, + IN UINT16 HeadSum + ); + +/** + This function delivers the received datagrams to the specified instance. + + @param[in] Instance Pointer to the instance context data. + +**/ +VOID +Udp6InstanceDeliverDgram ( + IN UDP6_INSTANCE_DATA *Instance + ); + +/** + Cancel Udp6 tokens from the Udp6 instance. + + @param[in] Instance Pointer to the udp instance context data. + @param[in] Token Pointer to the token to be canceled. If NULL, all + tokens in this instance will be cancelled. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The Token is cancelled. + @retval EFI_NOT_FOUND The Token is not found. + +**/ +EFI_STATUS +Udp6InstanceCancelToken ( + IN UDP6_INSTANCE_DATA *Instance, + IN EFI_UDP6_COMPLETION_TOKEN *Token OPTIONAL + ); + +/** + This function removes all the Wrap datas in the RcvdDgramQue. + + @param[in] Instance Pointer to the Udp6 Instance. + +**/ +VOID +Udp6FlushRcvdDgram ( + IN UDP6_INSTANCE_DATA *Instance + ); + +#endif + diff --git a/NetworkPkg/Udp6Dxe/Udp6Main.c b/NetworkPkg/Udp6Dxe/Udp6Main.c new file mode 100644 index 000000000..0829b7b2a --- /dev/null +++ b/NetworkPkg/Udp6Dxe/Udp6Main.c @@ -0,0 +1,858 @@ +/** @file + Contains all EFI_UDP6_PROTOCOL interfaces. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "Udp6Impl.h" + +EFI_UDP6_PROTOCOL mUdp6Protocol = { + Udp6GetModeData, + Udp6Configure, + Udp6Groups, + Udp6Transmit, + Udp6Receive, + Udp6Cancel, + Udp6Poll +}; + + +/** + This function copies the current operational settings of this EFI UDPv6 Protocol + instance into user-supplied buffers. This function is used optionally to retrieve + the operational mode data of underlying networks or drivers. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[out] Udp6ConfigData The buffer in which the current UDP configuration + data is returned. This parameter is optional and + may be NULL. + @param[out] Ip6ModeData The buffer in which the current EFI IPv6 Protocol + mode data is returned. This parameter is optional + and may be NULL. + @param[out] MnpConfigData The buffer in which the current managed network + configuration data is returned. This parameter is + optional and may be NULL. + @param[out] SnpModeData The buffer in which the simple network mode data + is returned. This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The mode data was read. + @retval EFI_NOT_STARTED When Udp6ConfigData is queried, no configuration + data is available because this instance has not + been started. + @retval EFI_INVALID_PARAMETER This is NULL. + +**/ +EFI_STATUS +EFIAPI +Udp6GetModeData ( + IN EFI_UDP6_PROTOCOL *This, + OUT EFI_UDP6_CONFIG_DATA *Udp6ConfigData OPTIONAL, + OUT EFI_IP6_MODE_DATA *Ip6ModeData OPTIONAL, + OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL, + OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL + ) +{ + UDP6_INSTANCE_DATA *Instance; + EFI_IP6_PROTOCOL *Ip; + EFI_TPL OldTpl; + EFI_STATUS Status; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP6_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured && (Udp6ConfigData != NULL)) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (Udp6ConfigData != NULL) { + // + // Set the Udp6ConfigData. + // + CopyMem (Udp6ConfigData, &Instance->ConfigData, sizeof (EFI_UDP6_CONFIG_DATA)); + } + + Ip = Instance->IpInfo->Ip.Ip6; + + // + // Get the underlying Ip6ModeData, MnpConfigData and SnpModeData. + // + Status = Ip->GetModeData (Ip, Ip6ModeData, MnpConfigData, SnpModeData); + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + This function is used to do the following: + Initialize and start this instance of the EFI UDPv6 Protocol. + Change the filtering rules and operational parameters. + Reset this instance of the EFI UDPv6 Protocol. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[in] UdpConfigData Pointer to the buffer to set the configuration + data. This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The configuration settings were set, changed, or + reset successfully. + @retval EFI_NO_MAPPING When the UdpConifgData.UseAnyStationAddress is set + to true and there is no address available for the IP6 + driver to bind a source address to this instance. + @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE: + This is NULL. + UdpConfigData.StationAddress is not a valid + unicast IPv6 address. + UdpConfigData.RemoteAddress is not a valid unicast + IPv6 address if it is not zero. + @retval EFI_ALREADY_STARTED The EFI UDPv6 Protocol instance is already + started/configured and must be stopped/reset + before it can be reconfigured. Only TrafficClass, + HopLimit, ReceiveTimeout, and TransmitTimeout can + be reconfigured without stopping the current + instance of the EFI UDPv6 Protocol. + @retval EFI_ACCESS_DENIED UdpConfigData.AllowDuplicatePort is FALSE and + UdpConfigData.StationPort is already used by another + instance. + @retval EFI_OUT_OF_RESOURCES The EFI UDPv6 Protocol driver cannot allocate + memory for this EFI UDPv6 Protocol instance. + @retval EFI_DEVICE_ERROR An unexpected network or system error occurred, and + this instance was not opened. + +**/ +EFI_STATUS +EFIAPI +Udp6Configure ( + IN EFI_UDP6_PROTOCOL *This, + IN EFI_UDP6_CONFIG_DATA *UdpConfigData OPTIONAL + ) +{ + EFI_STATUS Status; + UDP6_INSTANCE_DATA *Instance; + UDP6_SERVICE_DATA *Udp6Service; + EFI_TPL OldTpl; + EFI_IPv6_ADDRESS StationAddress; + EFI_IPv6_ADDRESS RemoteAddress; + EFI_IP6_CONFIG_DATA Ip6ConfigData; + EFI_IPv6_ADDRESS LocalAddr; + EFI_IPv6_ADDRESS RemoteAddr; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP6_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured && (UdpConfigData == NULL)) { + return EFI_SUCCESS; + } + + Udp6Service = Instance->Udp6Service; + Status = EFI_SUCCESS; + ASSERT (Udp6Service != NULL); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (UdpConfigData != NULL) { + + IP6_COPY_ADDRESS (&StationAddress, &UdpConfigData->StationAddress); + IP6_COPY_ADDRESS (&RemoteAddress, &UdpConfigData->RemoteAddress); + + if ((!NetIp6IsUnspecifiedAddr (&StationAddress) && !NetIp6IsValidUnicast (&StationAddress)) || + (!NetIp6IsUnspecifiedAddr (&RemoteAddress) && !NetIp6IsValidUnicast (&RemoteAddress)) + ){ + // + // If not use default address, and StationAddress is not a valid unicast + // if it is not IPv6 address or RemoteAddress is not a valid unicast IPv6 + // address if it is not 0. + // + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + if (Instance->Configured) { + // + // The instance is already configured, try to do the re-configuration. + // + if (!Udp6IsReconfigurable (&Instance->ConfigData, UdpConfigData)) { + // + // If the new configuration data wants to change some unreconfigurable + // settings, return EFI_ALREADY_STARTED. + // + Status = EFI_ALREADY_STARTED; + goto ON_EXIT; + } + + // + // Save the reconfigurable parameters. + // + Instance->ConfigData.TrafficClass = UdpConfigData->TrafficClass; + Instance->ConfigData.HopLimit = UdpConfigData->HopLimit; + Instance->ConfigData.ReceiveTimeout = UdpConfigData->ReceiveTimeout; + Instance->ConfigData.TransmitTimeout = UdpConfigData->TransmitTimeout; + } else { + // + // Construct the Ip configuration data from the UdpConfigData. + // + Udp6BuildIp6ConfigData (UdpConfigData, &Ip6ConfigData); + + // + // Configure the Ip instance wrapped in the IpInfo. + // + Status = IpIoConfigIp (Instance->IpInfo, &Ip6ConfigData); + if (EFI_ERROR (Status)) { + if (Status == EFI_NO_MAPPING) { + Instance->IsNoMapping = TRUE; + } + + goto ON_EXIT; + } + + Instance->IsNoMapping = FALSE; + + // + // Save the configuration data. + // + CopyMem ( + &Instance->ConfigData, + UdpConfigData, + sizeof (EFI_UDP6_CONFIG_DATA) + ); + IP6_COPY_ADDRESS (&Instance->ConfigData.StationAddress, &Ip6ConfigData.StationAddress); + // + // Try to allocate the required port resource. + // + Status = Udp6Bind (&Udp6Service->ChildrenList, &Instance->ConfigData); + if (EFI_ERROR (Status)) { + // + // Reset the ip instance if bind fails. + // + IpIoConfigIp (Instance->IpInfo, NULL); + goto ON_EXIT; + } + + // + // Pre calculate the checksum for the pseudo head, ignore the UDP length first. + // + IP6_COPY_ADDRESS (&LocalAddr, &Instance->ConfigData.StationAddress); + IP6_COPY_ADDRESS (&RemoteAddr, &Instance->ConfigData.RemoteAddress); + + Instance->HeadSum = NetIp6PseudoHeadChecksum ( + &LocalAddr, + &RemoteAddr, + EFI_IP_PROTO_UDP, + 0 + ); + + Instance->Configured = TRUE; + } + } else { + // + // UdpConfigData is NULL, reset the instance. + // + Instance->Configured = FALSE; + Instance->IsNoMapping = FALSE; + + // + // Reset the Ip instance wrapped in the IpInfo. + // + IpIoConfigIp (Instance->IpInfo, NULL); + + // + // Cancel all the user tokens. + // + Instance->Udp6Proto.Cancel (&Instance->Udp6Proto, NULL); + + // + // Remove the buffered RxData for this instance. + // + Udp6FlushRcvdDgram (Instance); + + ASSERT (IsListEmpty (&Instance->DeliveredDgramQue)); + } + +ON_EXIT: + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + This function is used to enable and disable the multicast group filtering. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[in] JoinFlag Set to TRUE to join a multicast group. Set to + FALSE to leave one or all multicast groups. + @param[in] MulticastAddress Pointer to multicast group address to join or + leave. This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_NOT_STARTED The EFI UDPv6 Protocol instance has not been + started. + @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. + JoinFlag is TRUE and MulticastAddress is NULL. + JoinFlag is TRUE and *MulticastAddress is not a + valid multicast address. + @retval EFI_ALREADY_STARTED The group address is already in the group table + (when JoinFlag is TRUE). + @retval EFI_NOT_FOUND The group address is not in the group table (when + JoinFlag is FALSE). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + +**/ +EFI_STATUS +EFIAPI +Udp6Groups ( + IN EFI_UDP6_PROTOCOL *This, + IN BOOLEAN JoinFlag, + IN EFI_IPv6_ADDRESS *MulticastAddress OPTIONAL + ) +{ + EFI_STATUS Status; + UDP6_INSTANCE_DATA *Instance; + EFI_IP6_PROTOCOL *Ip; + EFI_TPL OldTpl; + EFI_IPv6_ADDRESS *McastIp; + + if ((This == NULL) || (JoinFlag && (MulticastAddress == NULL))) { + return EFI_INVALID_PARAMETER; + } + + McastIp = NULL; + + if (JoinFlag) { + if (!IP6_IS_MULTICAST (MulticastAddress)) { + return EFI_INVALID_PARAMETER; + } + + McastIp = AllocateCopyPool (sizeof (EFI_IPv6_ADDRESS), MulticastAddress); + if (McastIp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + + Instance = UDP6_INSTANCE_DATA_FROM_THIS (This); + if (!Instance->Configured) { + if (McastIp != NULL) { + FreePool (McastIp); + } + return EFI_NOT_STARTED; + } + + Ip = Instance->IpInfo->Ip.Ip6; + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Invoke the Ip instance the Udp6 instance consumes to do the group operation. + // + Status = Ip->Groups (Ip, JoinFlag, MulticastAddress); + + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Keep a local copy of the configured multicast IPs because IpIo receives + // datagrams from the 0 station address IP instance and then UDP delivers to + // the matched instance. This copy of multicast IPs is used to avoid receive + // the mutlicast datagrams destinated to multicast IPs the other instances configured. + // + if (JoinFlag) { + + Status = NetMapInsertTail (&Instance->McastIps, (VOID *) McastIp, NULL); + } else { + + Status = NetMapIterate (&Instance->McastIps, Udp6LeaveGroup, MulticastAddress); + if ((MulticastAddress != NULL) && (Status == EFI_ABORTED)) { + Status = EFI_SUCCESS; + } + } + +ON_EXIT: + + gBS->RestoreTPL (OldTpl); + + if (EFI_ERROR (Status)) { + if (McastIp != NULL) { + FreePool (McastIp); + } + } + + return Status; +} + + + +/** + This function places a sending request to this instance of the EFI UDPv6 Protocol, + alongside the transmit data that was filled by the user. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[in] Token Pointer to the completion token that will be + placed into the transmit queue. + + @retval EFI_SUCCESS The data was queued for transmission. + @retval EFI_NOT_STARTED This EFI UDPv6 Protocol instance has not been + started. + @retval EFI_NO_MAPPING The under-layer IPv6 driver was responsible for + choosing a source address for this instance, but + no source address was available for use. + @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: + This is NULL. + Token is NULL. Token.Event is NULL. + Token.Packet.TxData is NULL. + Token.Packet.TxData.FragmentCount is zero. + Token.Packet.TxData.DataLength is not equal to the + sum of fragment lengths. + One or more of the + Token.Packet.TxData.FragmentTable[].FragmentLength + fields is zero. + One or more of the + Token.Packet.TxData.FragmentTable[].FragmentBuffer + fields is NULL. One or more of the + Token.Packet.TxData.UdpSessionData.DestinationAddres + are not valid unicast IPv6 + addresses if the UdpSessionData is not NULL. + Token.Packet.TxData.UdpSessionData. + DestinationAddress is NULL + Token.Packet.TxData.UdpSessionData. + DestinatioPort + is zero. + Token.Packet.TxData.UdpSessionData is NULL and this + instance's UdpConfigData.RemoteAddress is unspecified. + @retval EFI_ACCESS_DENIED The transmit completion token with the same + Token.Event is already in the transmit queue. + @retval EFI_NOT_READY The completion token could not be queued because + the transmit queue is full. + @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data. + @retval EFI_NOT_FOUND There is no route to the destination network or + address. + @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP + packet size. Or, the length of the IP header + UDP + header + data length is greater than MTU if + DoNotFragment is TRUE. + +**/ +EFI_STATUS +EFIAPI +Udp6Transmit ( + IN EFI_UDP6_PROTOCOL *This, + IN EFI_UDP6_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + UDP6_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + NET_BUF *Packet; + EFI_UDP_HEADER *Udp6Header; + EFI_UDP6_CONFIG_DATA *ConfigData; + EFI_IPv6_ADDRESS Source; + EFI_IPv6_ADDRESS Destination; + EFI_UDP6_TRANSMIT_DATA *TxData; + EFI_UDP6_SESSION_DATA *UdpSessionData; + UDP6_SERVICE_DATA *Udp6Service; + IP_IO_OVERRIDE Override; + UINT16 HeadSum; + EFI_IP_ADDRESS IpDestAddr; + + if ((This == NULL) || (Token == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP6_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Validate the Token, if the token is invalid return the error code. + // + Status = Udp6ValidateTxToken (Instance, Token); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + if (EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp6TokenExist, Token)) || + EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp6TokenExist, Token)) + ){ + // + // Try to find a duplicate token in the two token maps, if found, return + // EFI_ACCESS_DENIED. + // + Status = EFI_ACCESS_DENIED; + goto ON_EXIT; + } + + TxData = Token->Packet.TxData; + + // + // Create a net buffer to hold the user buffer and the udp header. + // + Packet = NetbufFromExt ( + (NET_FRAGMENT *) TxData->FragmentTable, + TxData->FragmentCount, + UDP6_HEADER_SIZE, + 0, + Udp6NetVectorExtFree, + NULL + ); + if (Packet == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + // + // Store the IpIo in ProtoData. + // + Udp6Service = Instance->Udp6Service; + *((UINTN *) &Packet->ProtoData[0]) = (UINTN) (Udp6Service->IpIo); + + Udp6Header = (EFI_UDP_HEADER *) NetbufAllocSpace (Packet, UDP6_HEADER_SIZE, TRUE); + ASSERT (Udp6Header != NULL); + if (Udp6Header == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_EXIT; + } + + ConfigData = &Instance->ConfigData; + + // + // Fill the udp header. + // + Udp6Header->SrcPort = HTONS (ConfigData->StationPort); + Udp6Header->DstPort = HTONS (ConfigData->RemotePort); + Udp6Header->Length = HTONS ((UINT16) Packet->TotalSize); + Udp6Header->Checksum = 0; + // + // Set the UDP Header in NET_BUF, this UDP header is for IP6 can fast get the + // Udp header for pseudoHeadCheckSum. + // + Packet->Udp = Udp6Header; + UdpSessionData = TxData->UdpSessionData; + + if (UdpSessionData != NULL) { + // + // Set the Destination according to the specified + // UdpSessionData. + // + + if (UdpSessionData->DestinationPort != 0) { + Udp6Header->DstPort = HTONS (UdpSessionData->DestinationPort); + } + + IP6_COPY_ADDRESS (&Source, &ConfigData->StationAddress); + if (!NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress)) { + IP6_COPY_ADDRESS (&Destination, &UdpSessionData->DestinationAddress); + } else { + IP6_COPY_ADDRESS (&Destination, &ConfigData->RemoteAddress); + } + + // + //Calculate the pseudo head checksum using the overridden parameters. + // + if (!NetIp6IsUnspecifiedAddr (&ConfigData->StationAddress)) { + HeadSum = NetIp6PseudoHeadChecksum ( + &Source, + &Destination, + EFI_IP_PROTO_UDP, + 0 + ); + + // + // calculate the checksum. + // + Udp6Header->Checksum = Udp6Checksum (Packet, HeadSum); + if (Udp6Header->Checksum == 0) { + // + // If the calculated checksum is 0, fill the Checksum field with all ones. + // + Udp6Header->Checksum = 0xffff; + } + } else { + // + // Set the checksum is zero if the ConfigData->StationAddress is unspcified + // and the Ipv6 will fill the correct value of this checksum. + // + Udp6Header->Checksum = 0; + + } + } else { + // + // UdpSessionData is NULL, use the address and port information previously configured. + // + IP6_COPY_ADDRESS (&Destination, &ConfigData->RemoteAddress); + + HeadSum = Instance->HeadSum; + // + // calculate the checksum. + // + Udp6Header->Checksum = Udp6Checksum (Packet, HeadSum); + if (Udp6Header->Checksum == 0) { + // + // If the calculated checksum is 0, fill the Checksum field with all ones. + // + Udp6Header->Checksum = 0xffff; + } + } + + + + // + // Fill the IpIo Override data. + // + Override.Ip6OverrideData.Protocol = EFI_IP_PROTO_UDP; + Override.Ip6OverrideData.HopLimit = ConfigData->HopLimit; + Override.Ip6OverrideData.FlowLabel = 0; + + // + // Save the token into the TxToken map. + // + Status = NetMapInsertTail (&Instance->TxTokens, Token, Packet); + if (EFI_ERROR (Status)) { + goto FREE_PACKET; + } + + // + // Send out this datagram through IpIo. + // + if (UdpSessionData != NULL){ + IP6_COPY_ADDRESS (&(IpDestAddr.v6), &Destination); + } else { + ZeroMem (&IpDestAddr.v6, sizeof (EFI_IPv6_ADDRESS)); + } + + Status = IpIoSend ( + Udp6Service->IpIo, + Packet, + Instance->IpInfo, + Instance, + Token, + &IpDestAddr, + &Override + ); + if (EFI_ERROR (Status)) { + // + // Remove this token from the TxTokens. + // + Udp6RemoveToken (&Instance->TxTokens, Token); + } + +FREE_PACKET: + + NetbufFree (Packet); + +ON_EXIT: + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + This function places a completion token into the receive packet queue. This function + is always asynchronous. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[in] Token Pointer to a token that is associated with the + receive data descriptor. + + @retval EFI_SUCCESS The receive completion token was cached. + @retval EFI_NOT_STARTED This EFI UDPv6 Protocol instance has not been + started. + @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, + BOOTP, RARP, etc.) is not finished yet. + @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: + This is NULL. Token is NULL. Token.Event is NULL. + @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued + due to a lack of system resources (usually + memory). + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + The EFI UDPv6 Protocol instance has been reset to + startup defaults. + @retval EFI_ACCESS_DENIED A receive completion token with the same + Token.Event is already in the receive queue. + @retval EFI_NOT_READY The receive request could not be queued because + the receive queue is full. + +**/ +EFI_STATUS +EFIAPI +Udp6Receive ( + IN EFI_UDP6_PROTOCOL *This, + IN EFI_UDP6_COMPLETION_TOKEN *Token + ) +{ + EFI_STATUS Status; + UDP6_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP6_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + if (EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp6TokenExist, Token)) || + EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp6TokenExist, Token)) + ){ + // + // Return EFI_ACCESS_DENIED if the specified token is already in the TxTokens or + // RxTokens map. + // + Status = EFI_ACCESS_DENIED; + goto ON_EXIT; + } + + Token->Packet.RxData = NULL; + + // + // Save the token into the RxTokens map. + // + Status = NetMapInsertTail (&Instance->RxTokens, Token, NULL); + if (EFI_ERROR (Status)) { + Status = EFI_NOT_READY; + goto ON_EXIT; + } + + // + // If there is an icmp error, report it. + // + Udp6ReportIcmpError (Instance); + + // + // Try to delivered the received datagrams. + // + Udp6InstanceDeliverDgram (Instance); + + // + // Dispatch the DPC queued by the NotifyFunction of Token->Event. + // + DispatchDpc (); + +ON_EXIT: + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + This function is used to abort a pending transmit or receive request. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + @param[in] Token Pointer to a token that has been issued by + EFI_UDP6_PROTOCOL.Transmit() or + EFI_UDP6_PROTOCOL.Receive(). This parameter is + optional and may be NULL. + + @retval EFI_SUCCESS The asynchronous I/O request was aborted, and + Token.Event was signaled. When Token is NULL, all + pending requests are aborted and their events are + signaled. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_NOT_STARTED This instance has not been started. + @retval EFI_NO_MAPPING When using the default address, configuration + (DHCP, BOOTP, RARP, etc.) is not finished yet. + @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O + request is not found in the transmit or receive + queue. It is either completed or not issued by + Transmit() or Receive(). + +**/ +EFI_STATUS +EFIAPI +Udp6Cancel ( + IN EFI_UDP6_PROTOCOL *This, + IN EFI_UDP6_COMPLETION_TOKEN *Token OPTIONAL + ) +{ + EFI_STATUS Status; + UDP6_INSTANCE_DATA *Instance; + EFI_TPL OldTpl; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP6_INSTANCE_DATA_FROM_THIS (This); + + if (!Instance->Configured) { + return EFI_NOT_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Cancle the tokens specified by Token for this instance. + // + Status = Udp6InstanceCancelToken (Instance, Token); + + // + // Dispatch the DPC queued by the NotifyFunction of the canceled token's events. + // + DispatchDpc (); + + gBS->RestoreTPL (OldTpl); + + return Status; +} + + +/** + This function can be used by network drivers and applications to increase the rate that + data packets are moved between the communications device and the transmit/receive queues. + + @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance. + + @retval EFI_SUCCESS Incoming or outgoing data was processed. + @retval EFI_INVALID_PARAMETER This is NULL. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval EFI_TIMEOUT Data was dropped out of the transmit and/or + receive queue. + +**/ +EFI_STATUS +EFIAPI +Udp6Poll ( + IN EFI_UDP6_PROTOCOL *This + ) +{ + UDP6_INSTANCE_DATA *Instance; + EFI_IP6_PROTOCOL *Ip; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Instance = UDP6_INSTANCE_DATA_FROM_THIS (This); + Ip = Instance->IpInfo->Ip.Ip6; + + // + // Invode the Ip instance consumed by the udp instance to do the poll operation. + // + return Ip->Poll (Ip); +} diff --git a/NetworkPkg/UefiPxeBcDxe/ComponentName.c b/NetworkPkg/UefiPxeBcDxe/ComponentName.c new file mode 100644 index 000000000..75d46b368 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/ComponentName.c @@ -0,0 +1,352 @@ +/** @file + UEFI Component Name(2) protocol implementation for UefiPxeBc driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "PxeBcImpl.h" + +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +PxeBcComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is being managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +PxeBcComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gPxeBcComponentName = { + PxeBcComponentNameGetDriverName, + PxeBcComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gPxeBcComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) PxeBcComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) PxeBcComponentNameGetControllerName, + "en" +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mPxeBcDriverNameTable[] = { + { + "eng;en", + L"UEFI PXE Base Code Driver" + }, + { + NULL, + NULL + } +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mPxeBcControllerNameTable[] = { + { + "eng;en", + L"PXE Controller" + }, + { + NULL, + NULL + } +}; + +/** + Retrieves a Unicode string that is the user-readable name of the driver. + + This function retrieves the user-readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user-readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +PxeBcComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2( + Language, + This->SupportedLanguages, + mPxeBcDriverNameTable, + DriverName, + (BOOLEAN)(This == &gPxeBcComponentName) + ); +} + + +/** + Retrieves a Unicode string that is the user-readable name of the controller + that is being managed by a driver. + + This function retrieves the user-readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user-readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param[in] ControllerHandle The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param[in] ChildHandle The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param[in] Language A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +PxeBcComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + EFI_HANDLE NicHandle; + PXEBC_PRIVATE_PROTOCOL *Id; + + if (ControllerHandle == NULL || ChildHandle != NULL) { + return EFI_UNSUPPORTED; + } + + NicHandle = PxeBcGetNicByIp4Children (ControllerHandle); + if (NicHandle == NULL) { + NicHandle = PxeBcGetNicByIp6Children (ControllerHandle); + if (NicHandle == NULL) { + return EFI_UNSUPPORTED; + } + } + + // + // Try to retrieve the private data by PxeBcPrivate protocol. + // + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mPxeBcControllerNameTable, + ControllerName, + (BOOLEAN)(This == &gPxeBcComponentName) + ); +} diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcBoot.c b/NetworkPkg/UefiPxeBcDxe/PxeBcBoot.c new file mode 100644 index 000000000..a982d3a29 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcBoot.c @@ -0,0 +1,1256 @@ +/** @file + Boot functions implementation for UefiPxeBc Driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "PxeBcImpl.h" + + +/** + Display the string of the boot item. + + If the length of the boot item string beyond 70 Char, just display 70 Char. + + @param[in] Str The pointer to the string. + @param[in] Len The length of the string. + +**/ +VOID +PxeBcDisplayBootItem ( + IN UINT8 *Str, + IN UINT8 Len + ) +{ + UINT8 Tmp; + + // + // Cut off the chars behind 70th. + // + Len = (UINT8) MIN (PXEBC_DISPLAY_MAX_LINE, Len); + Tmp = Str[Len]; + Str[Len] = 0; + AsciiPrint ("%a \n", Str); + + // + // Restore the original 70th char. + // + Str[Len] = Tmp; +} + + +/** + Select and maintain the boot prompt if needed. + + @param[in] Private Pointer to PxeBc private data. + + @retval EFI_SUCCESS Selected boot prompt done. + @retval EFI_TIMEOUT Selected boot prompt timed out. + @retval EFI_NOT_FOUND The proxy offer is not Pxe10. + @retval EFI_ABORTED User cancelled the operation. + @retval EFI_NOT_READY Reading the input key from the keyboard has not finish. + +**/ +EFI_STATUS +PxeBcSelectBootPrompt ( + IN PXEBC_PRIVATE_DATA *Private + ) +{ + PXEBC_DHCP_PACKET_CACHE *Cache; + PXEBC_VENDOR_OPTION *VendorOpt; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_EVENT TimeoutEvent; + EFI_EVENT DescendEvent; + EFI_INPUT_KEY InputKey; + EFI_STATUS Status; + UINT32 OfferType; + UINT8 Timeout; + UINT8 *Prompt; + UINT8 PromptLen; + INT32 SecCol; + INT32 SecRow; + + TimeoutEvent = NULL; + DescendEvent = NULL; + Mode = Private->PxeBc.Mode; + Cache = Mode->ProxyOfferReceived ? &Private->ProxyOffer : &Private->DhcpAck; + OfferType = Mode->UsingIpv6 ? Cache->Dhcp6.OfferType : Cache->Dhcp4.OfferType; + + // + // Only DhcpPxe10 and ProxyPxe10 offer needs boot prompt. + // + if (OfferType != PxeOfferTypeProxyPxe10 && OfferType != PxeOfferTypeDhcpPxe10) { + return EFI_NOT_FOUND; + } + + // + // There is no specified ProxyPxe10 for IPv6 in PXE and UEFI spec. + // + ASSERT (!Mode->UsingIpv6); + + VendorOpt = &Cache->Dhcp4.VendorOpt; + // + // According to the PXE specification 2.1, Table 2-1 PXE DHCP Options, + // we must not consider a boot prompt or boot menu if all of the following hold: + // - the PXE_DISCOVERY_CONTROL tag(6) is present inside the Vendor Options(43), and has bit 3 set + // - a boot file name has been presented in the initial DHCP or ProxyDHCP offer packet. + // + if (IS_DISABLE_PROMPT_MENU (VendorOpt->DiscoverCtrl) && + Cache->Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) { + return EFI_ABORTED; + } + + if (!IS_VALID_BOOT_PROMPT (VendorOpt->BitMap)) { + return EFI_TIMEOUT; + } + + Timeout = VendorOpt->MenuPrompt->Timeout; + Prompt = VendorOpt->MenuPrompt->Prompt; + PromptLen = (UINT8) (VendorOpt->MenuPromptLen - 1); + + // + // The valid scope of Timeout refers to PXE2.1 spec. + // + if (Timeout == 0) { + return EFI_TIMEOUT; + } + if (Timeout == 255) { + return EFI_SUCCESS; + } + + // + // Create and start a timer as timeout event. + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &TimeoutEvent + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->SetTimer ( + TimeoutEvent, + TimerRelative, + MultU64x32 (Timeout, TICKS_PER_SECOND) + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Create and start a periodic timer as descend event by second. + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &DescendEvent + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = gBS->SetTimer ( + DescendEvent, + TimerPeriodic, + TICKS_PER_SECOND + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Display the boot item and cursor on the screen. + // + SecCol = gST->ConOut->Mode->CursorColumn; + SecRow = gST->ConOut->Mode->CursorRow; + + PxeBcDisplayBootItem (Prompt, PromptLen); + + gST->ConOut->SetCursorPosition (gST->ConOut, SecCol + PromptLen, SecRow); + AsciiPrint ("(%d) ", Timeout--); + + Status = EFI_TIMEOUT; + while (EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) { + if (!EFI_ERROR (gBS->CheckEvent (DescendEvent))) { + gST->ConOut->SetCursorPosition (gST->ConOut, SecCol + PromptLen, SecRow); + AsciiPrint ("(%d) ", Timeout--); + } + if (gST->ConIn->ReadKeyStroke (gST->ConIn, &InputKey) == EFI_NOT_READY) { + gBS->Stall (10 * TICKS_PER_MS); + continue; + } + // + // Parse the input key by user. + // If or + is pressed, return success to display the boot menu. + // + if (InputKey.ScanCode == 0) { + + switch (InputKey.UnicodeChar) { + + case CTRL ('c'): + Status = EFI_ABORTED; + break; + + case CTRL ('m'): + case 'm': + case 'M': + Status = EFI_SUCCESS; + break; + + default: + continue; + } + + } else { + + switch (InputKey.ScanCode) { + + case SCAN_F8: + Status = EFI_SUCCESS; + break; + + case SCAN_ESC: + Status = EFI_ABORTED; + break; + + default: + continue; + } + } + + break; + } + + // + // Reset the cursor on the screen. + // + gST->ConOut->SetCursorPosition (gST->ConOut, 0 , SecRow + 1); + +ON_EXIT: + if (DescendEvent != NULL) { + gBS->CloseEvent (DescendEvent); + } + if (TimeoutEvent != NULL) { + gBS->CloseEvent (TimeoutEvent); + } + + return Status; +} + + +/** + Select the boot menu by user's input. + + @param[in] Private Pointer to PxeBc private data. + @param[out] Type The type of the menu. + @param[in] UseDefaultItem Use default item or not. + + @retval EFI_ABORTED User cancel operation. + @retval EFI_SUCCESS Select the boot menu success. + @retval EFI_NOT_READY Read the input key from the keybroad has not finish. + +**/ +EFI_STATUS +PxeBcSelectBootMenu ( + IN PXEBC_PRIVATE_DATA *Private, + OUT UINT16 *Type, + IN BOOLEAN UseDefaultItem + ) +{ + EFI_PXE_BASE_CODE_MODE *Mode; + PXEBC_DHCP_PACKET_CACHE *Cache; + PXEBC_VENDOR_OPTION *VendorOpt; + EFI_INPUT_KEY InputKey; + UINT32 OfferType; + UINT8 MenuSize; + UINT8 MenuNum; + INT32 TopRow; + UINT16 Select; + UINT16 LastSelect; + UINT8 Index; + BOOLEAN Finish; + CHAR8 Blank[PXEBC_DISPLAY_MAX_LINE]; + PXEBC_BOOT_MENU_ENTRY *MenuItem; + PXEBC_BOOT_MENU_ENTRY *MenuArray[PXEBC_MENU_MAX_NUM]; + + Finish = FALSE; + Select = 0; + Index = 0; + *Type = 0; + Mode = Private->PxeBc.Mode; + Cache = Mode->ProxyOfferReceived ? &Private->ProxyOffer : &Private->DhcpAck; + OfferType = Mode->UsingIpv6 ? Cache->Dhcp6.OfferType : Cache->Dhcp4.OfferType; + + // + // There is no specified DhcpPxe10/ProxyPxe10 for IPv6 in PXE and UEFI spec. + // + ASSERT (!Mode->UsingIpv6); + ASSERT (OfferType == PxeOfferTypeProxyPxe10 || OfferType == PxeOfferTypeDhcpPxe10); + + VendorOpt = &Cache->Dhcp4.VendorOpt; + if (!IS_VALID_BOOT_MENU (VendorOpt->BitMap)) { + return EFI_SUCCESS; + } + + // + // Display the boot menu on the screen. + // + SetMem (Blank, sizeof(Blank), ' '); + + MenuSize = VendorOpt->BootMenuLen; + MenuItem = VendorOpt->BootMenu; + + if (MenuSize == 0) { + return EFI_DEVICE_ERROR; + } + + while (MenuSize > 0 && Index < PXEBC_MENU_MAX_NUM) { + ASSERT (MenuItem != NULL); + MenuArray[Index] = MenuItem; + MenuSize = (UINT8) (MenuSize - (MenuItem->DescLen + 3)); + MenuItem = (PXEBC_BOOT_MENU_ENTRY *) ((UINT8 *) MenuItem + MenuItem->DescLen + 3); + Index++; + } + + if (UseDefaultItem) { + ASSERT (MenuArray[0] != NULL); + CopyMem (Type, &MenuArray[0]->Type, sizeof (UINT16)); + *Type = NTOHS (*Type); + return EFI_SUCCESS; + } + + MenuNum = Index; + + for (Index = 0; Index < MenuNum; Index++) { + ASSERT (MenuArray[Index] != NULL); + PxeBcDisplayBootItem (MenuArray[Index]->DescStr, MenuArray[Index]->DescLen); + } + + TopRow = gST->ConOut->Mode->CursorRow - MenuNum; + + // + // Select the boot item by user in the boot menu. + // + do { + // + // Highlight selected row. + // + gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_BLACK, EFI_LIGHTGRAY)); + gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + Select); + ASSERT (Select < PXEBC_MENU_MAX_NUM); + ASSERT (MenuArray[Select] != NULL); + Blank[MenuArray[Select]->DescLen] = 0; + AsciiPrint ("%a\r", Blank); + PxeBcDisplayBootItem (MenuArray[Select]->DescStr, MenuArray[Select]->DescLen); + gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + MenuNum); + LastSelect = Select; + + while (gST->ConIn->ReadKeyStroke (gST->ConIn, &InputKey) == EFI_NOT_READY) { + gBS->Stall (10 * TICKS_PER_MS); + } + + if (InputKey.ScanCode == 0) { + switch (InputKey.UnicodeChar) { + case CTRL ('c'): + InputKey.ScanCode = SCAN_ESC; + break; + + case CTRL ('j'): /* linefeed */ + case CTRL ('m'): /* return */ + Finish = TRUE; + break; + + case CTRL ('i'): /* tab */ + case ' ': + case 'd': + case 'D': + InputKey.ScanCode = SCAN_DOWN; + break; + + case CTRL ('h'): /* backspace */ + case 'u': + case 'U': + InputKey.ScanCode = SCAN_UP; + break; + + default: + InputKey.ScanCode = 0; + } + } + + switch (InputKey.ScanCode) { + case SCAN_LEFT: + case SCAN_UP: + if (Select != 0) { + Select--; + } + break; + + case SCAN_DOWN: + case SCAN_RIGHT: + if (++Select == MenuNum) { + Select--; + } + break; + + case SCAN_PAGE_UP: + case SCAN_HOME: + Select = 0; + break; + + case SCAN_PAGE_DOWN: + case SCAN_END: + Select = (UINT16) (MenuNum - 1); + break; + + case SCAN_ESC: + return EFI_ABORTED; + } + + // + // Unhighlight the last selected row. + // + gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK)); + gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + LastSelect); + ASSERT (LastSelect < PXEBC_MENU_MAX_NUM); + ASSERT (MenuArray[LastSelect] != NULL); + Blank[MenuArray[LastSelect]->DescLen] = 0; + AsciiPrint ("%a\r", Blank); + PxeBcDisplayBootItem (MenuArray[LastSelect]->DescStr, MenuArray[LastSelect]->DescLen); + gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + MenuNum); + } while (!Finish); + + // + // Swap the byte order. + // + ASSERT (Select < PXEBC_MENU_MAX_NUM); + ASSERT (MenuArray[Select] != NULL); + CopyMem (Type, &MenuArray[Select]->Type, sizeof (UINT16)); + *Type = NTOHS (*Type); + + return EFI_SUCCESS; +} + + +/** + Parse out the boot information from the last Dhcp4 reply packet. + + @param[in, out] Private Pointer to PxeBc private data. + @param[out] BufferSize Size of the boot file to be downloaded. + + @retval EFI_SUCCESS Successfully parsed out all the boot information. + @retval Others Failed to parse out the boot information. + +**/ +EFI_STATUS +PxeBcDhcp4BootInfo ( + IN OUT PXEBC_PRIVATE_DATA *Private, + OUT UINT64 *BufferSize + ) +{ + EFI_PXE_BASE_CODE_PROTOCOL *PxeBc; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_STATUS Status; + PXEBC_DHCP4_PACKET_CACHE *Cache4; + UINT16 Value; + PXEBC_VENDOR_OPTION *VendorOpt; + PXEBC_BOOT_SVR_ENTRY *Entry; + + PxeBc = &Private->PxeBc; + Mode = PxeBc->Mode; + Status = EFI_SUCCESS; + *BufferSize = 0; + + // + // Get the last received Dhcp4 reply packet. + // + if (Mode->PxeReplyReceived) { + Cache4 = &Private->PxeReply.Dhcp4; + } else if (Mode->ProxyOfferReceived) { + Cache4 = &Private->ProxyOffer.Dhcp4; + } else { + Cache4 = &Private->DhcpAck.Dhcp4; + } + + ASSERT (Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL); + + // + // Parse the boot server address. + // If prompt/discover is disabled, get the first boot server from the boot servers list. + // Otherwise, parse the boot server Ipv4 address from next server address field in DHCP header. + // If all these fields are not available, use option 54 instead. + // + VendorOpt = &Cache4->VendorOpt; + if (IS_DISABLE_PROMPT_MENU (VendorOpt->DiscoverCtrl) && IS_VALID_BOOT_SERVERS (VendorOpt->BitMap)) { + Entry = VendorOpt->BootSvr; + if (VendorOpt->BootSvrLen >= sizeof (PXEBC_BOOT_SVR_ENTRY) && Entry->IpCnt > 0) { + CopyMem ( + &Private->ServerIp, + &Entry->IpAddr[0], + sizeof (EFI_IPv4_ADDRESS) + ); + } + } + if (Private->ServerIp.Addr[0] == 0) { + // + // ServerIp.Addr[0] equals zero means we failed to get IP address from boot server list. + // Try to use next server address field. + // + CopyMem ( + &Private->ServerIp, + &Cache4->Packet.Offer.Dhcp4.Header.ServerAddr, + sizeof (EFI_IPv4_ADDRESS) + ); + } + if (Private->ServerIp.Addr[0] == 0) { + // + // Still failed , use the IP address from option 54. + // + CopyMem ( + &Private->ServerIp, + Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data, + sizeof (EFI_IPv4_ADDRESS) + ); + } + + // + // Parse the boot file name by option. + // + Private->BootFileName = Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Data; + + if (Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN] != NULL) { + // + // Parse the boot file size by option. + // + CopyMem (&Value, Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN]->Data, sizeof (Value)); + Value = NTOHS (Value); + // + // The field of boot file size is 512 bytes in unit. + // + *BufferSize = 512 * Value; + } else { + // + // Get the bootfile size by tftp command if no option available. + // + Status = PxeBc->Mtftp ( + PxeBc, + EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE, + NULL, + FALSE, + BufferSize, + &Private->BlockSize, + &Private->ServerIp, + Private->BootFileName, + NULL, + FALSE + ); + } + + // + // Save the value of boot file size. + // + Private->BootFileSize = (UINTN) *BufferSize; + + // + // Display all the information: boot server address, boot file name and boot file size. + // + AsciiPrint ("\n Server IP address is "); + PxeBcShowIp4Addr (&Private->ServerIp.v4); + AsciiPrint ("\n NBP filename is %a", Private->BootFileName); + AsciiPrint ("\n NBP filesize is %d Bytes", Private->BootFileSize); + + return Status; +} + + +/** + Parse out the boot information from the last Dhcp6 reply packet. + + @param[in, out] Private Pointer to PxeBc private data. + @param[out] BufferSize Size of the boot file to be downloaded. + + @retval EFI_SUCCESS Successfully parsed out all the boot information. + @retval EFI_BUFFER_TOO_SMALL + @retval Others Failed to parse out the boot information. + +**/ +EFI_STATUS +PxeBcDhcp6BootInfo ( + IN OUT PXEBC_PRIVATE_DATA *Private, + OUT UINT64 *BufferSize + ) +{ + EFI_PXE_BASE_CODE_PROTOCOL *PxeBc; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_STATUS Status; + PXEBC_DHCP6_PACKET_CACHE *Cache6; + UINT16 Value; + + PxeBc = &Private->PxeBc; + Mode = PxeBc->Mode; + Status = EFI_SUCCESS; + *BufferSize = 0; + + // + // Get the last received Dhcp6 reply packet. + // + if (Mode->PxeReplyReceived) { + Cache6 = &Private->PxeReply.Dhcp6; + } else if (Mode->ProxyOfferReceived) { + Cache6 = &Private->ProxyOffer.Dhcp6; + } else { + Cache6 = &Private->DhcpAck.Dhcp6; + } + + ASSERT (Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] != NULL); + + // + // Set the station address to IP layer. + // + Status = PxeBcSetIp6Address (Private); + if (EFI_ERROR (Status)) { + return Status; + } + + + // + // Parse (m)tftp server ip address and bootfile name. + // + Status = PxeBcExtractBootFileUrl ( + Private, + &Private->BootFileName, + &Private->ServerIp.v6, + (CHAR8 *) (Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL]->Data), + NTOHS (Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL]->OpLen) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Parse the value of boot file size. + // + if (Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM] != NULL) { + // + // Parse it out if have the boot file parameter option. + // + Status = PxeBcExtractBootFileParam ((CHAR8 *) Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM]->Data, &Value); + if (EFI_ERROR (Status)) { + return Status; + } + // + // The field of boot file size is 512 bytes in unit. + // + *BufferSize = 512 * Value; + } else { + // + // Send get file size command by tftp if option unavailable. + // + Status = PxeBc->Mtftp ( + PxeBc, + EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE, + NULL, + FALSE, + BufferSize, + &Private->BlockSize, + &Private->ServerIp, + Private->BootFileName, + NULL, + FALSE + ); + } + + // + // Save the value of boot file size. + // + Private->BootFileSize = (UINTN) *BufferSize; + + // + // Display all the information: boot server address, boot file name and boot file size. + // + AsciiPrint ("\n Server IP address is "); + PxeBcShowIp6Addr (&Private->ServerIp.v6); + AsciiPrint ("\n NBP filename is %a", Private->BootFileName); + AsciiPrint ("\n NBP filesize is %d Bytes", Private->BootFileSize); + + return Status; +} + + +/** + Extract the discover information and boot server entry from the + cached packets if unspecified. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Type The type of bootstrap to perform. + @param[in, out] DiscoverInfo Pointer to EFI_PXE_BASE_CODE_DISCOVER_INFO. + @param[out] BootEntry Pointer to PXEBC_BOOT_SVR_ENTRY. + @param[out] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST. + + @retval EFI_SUCCESS Successfully extracted the information. + @retval EFI_DEVICE_ERROR Failed to extract the information. + +**/ +EFI_STATUS +PxeBcExtractDiscoverInfo ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT16 Type, + IN OUT EFI_PXE_BASE_CODE_DISCOVER_INFO **DiscoverInfo, + OUT PXEBC_BOOT_SVR_ENTRY **BootEntry, + OUT EFI_PXE_BASE_CODE_SRVLIST **SrvList + ) +{ + EFI_PXE_BASE_CODE_MODE *Mode; + PXEBC_DHCP4_PACKET_CACHE *Cache4; + PXEBC_VENDOR_OPTION *VendorOpt; + PXEBC_BOOT_SVR_ENTRY *Entry; + BOOLEAN IsFound; + EFI_PXE_BASE_CODE_DISCOVER_INFO *Info; + UINT16 Index; + + Mode = Private->PxeBc.Mode; + Info = *DiscoverInfo; + + if (Mode->UsingIpv6) { + Info->IpCnt = 1; + Info->UseUCast = TRUE; + + Info->SrvList[0].Type = Type; + Info->SrvList[0].AcceptAnyResponse = FALSE; + + // + // There is no vendor options specified in DHCPv6, so take BootFileUrl in the last cached packet. + // + CopyMem (&Info->SrvList[0].IpAddr, &Private->ServerIp, sizeof (EFI_IP_ADDRESS)); + + *SrvList = Info->SrvList; + } else { + Entry = NULL; + IsFound = FALSE; + Cache4 = (Mode->ProxyOfferReceived) ? &Private->ProxyOffer.Dhcp4 : &Private->DhcpAck.Dhcp4; + VendorOpt = &Cache4->VendorOpt; + + if (!Mode->DhcpAckReceived || !IS_VALID_DISCOVER_VENDOR_OPTION (VendorOpt->BitMap)) { + // + // Address is not acquired or no discovery options. + // + return EFI_INVALID_PARAMETER; + } + + // + // Parse the boot server entry from the vendor option in the last cached packet. + // + Info->UseMCast = (BOOLEAN) !IS_DISABLE_MCAST_DISCOVER (VendorOpt->DiscoverCtrl); + Info->UseBCast = (BOOLEAN) !IS_DISABLE_BCAST_DISCOVER (VendorOpt->DiscoverCtrl); + Info->MustUseList = (BOOLEAN) IS_ENABLE_USE_SERVER_LIST (VendorOpt->DiscoverCtrl); + Info->UseUCast = (BOOLEAN) IS_VALID_BOOT_SERVERS (VendorOpt->BitMap); + + if (Info->UseMCast) { + // + // Get the multicast discover ip address from vendor option if has. + // + CopyMem (&Info->ServerMCastIp.v4, &VendorOpt->DiscoverMcastIp, sizeof (EFI_IPv4_ADDRESS)); + } + + Info->IpCnt = 0; + + if (Info->UseUCast) { + Entry = VendorOpt->BootSvr; + + while (((UINT8) (Entry - VendorOpt->BootSvr)) < VendorOpt->BootSvrLen) { + if (Entry->Type == HTONS (Type)) { + IsFound = TRUE; + break; + } + Entry = GET_NEXT_BOOT_SVR_ENTRY (Entry); + } + + if (!IsFound) { + return EFI_DEVICE_ERROR; + } + + Info->IpCnt = Entry->IpCnt; + if (Info->IpCnt >= 1) { + *DiscoverInfo = AllocatePool (sizeof (*Info) + (Info->IpCnt - 1) * sizeof (**SrvList)); + if (*DiscoverInfo == NULL) { + return EFI_OUT_OF_RESOURCES; + } + CopyMem (*DiscoverInfo, Info, sizeof (*Info)); + Info = *DiscoverInfo; + } + + for (Index = 0; Index < Info->IpCnt; Index++) { + CopyMem (&Info->SrvList[Index].IpAddr, &Entry->IpAddr[Index], sizeof (EFI_IPv4_ADDRESS)); + Info->SrvList[Index].AcceptAnyResponse = !Info->MustUseList; + Info->SrvList[Index].Type = NTOHS (Entry->Type); + } + } + + *BootEntry = Entry; + *SrvList = Info->SrvList; + } + + return EFI_SUCCESS; +} + + +/** + Build the discover packet and send out for boot server. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Type PxeBc option boot item type. + @param[in] Layer Pointer to option boot item layer. + @param[in] UseBis Use BIS or not. + @param[in] DestIp Pointer to the destination address. + @param[in] IpCount The count of the server address. + @param[in] SrvList Pointer to the server address list. + + @retval EFI_SUCCESS Successfully discovered boot file. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource. + @retval EFI_NOT_FOUND Can't get the PXE reply packet. + @retval Others Failed to discover boot file. + +**/ +EFI_STATUS +PxeBcDiscoverBootServer ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT16 Type, + IN UINT16 *Layer, + IN BOOLEAN UseBis, + IN EFI_IP_ADDRESS *DestIp, + IN UINT16 IpCount, + IN EFI_PXE_BASE_CODE_SRVLIST *SrvList + ) +{ + if (Private->PxeBc.Mode->UsingIpv6) { + return PxeBcDhcp6Discover ( + Private, + Type, + Layer, + UseBis, + DestIp + ); + } else { + return PxeBcDhcp4Discover ( + Private, + Type, + Layer, + UseBis, + DestIp, + IpCount, + SrvList + ); + } +} + + +/** + Discover all the boot information for boot file. + + @param[in, out] Private Pointer to PxeBc private data. + @param[out] BufferSize Size of the boot file to be downloaded. + + @retval EFI_SUCCESS Successfully obtained all the boot information . + @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file. + @retval EFI_ABORTED User cancel current operation. + @retval Others Failed to parse out the boot information. + +**/ +EFI_STATUS +PxeBcDiscoverBootFile ( + IN OUT PXEBC_PRIVATE_DATA *Private, + OUT UINT64 *BufferSize + ) +{ + EFI_PXE_BASE_CODE_PROTOCOL *PxeBc; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_STATUS Status; + UINT16 Type; + UINT16 Layer; + BOOLEAN UseBis; + + PxeBc = &Private->PxeBc; + Mode = PxeBc->Mode; + Type = EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP; + Layer = EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL; + + // + // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and + // other pxe boot information. + // + Status = PxeBc->Dhcp (PxeBc, TRUE); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Select a boot server from boot server list. + // + Status = PxeBcSelectBootPrompt (Private); + + if (Status == EFI_SUCCESS) { + // + // Choose by user's input. + // + Status = PxeBcSelectBootMenu (Private, &Type, FALSE); + } else if (Status == EFI_TIMEOUT) { + // + // Choose by default item. + // + Status = PxeBcSelectBootMenu (Private, &Type, TRUE); + } + + if (!EFI_ERROR (Status)) { + + if (Type == EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP) { + // + // Local boot(PXE bootstrap server) need abort + // + return EFI_ABORTED; + } + + // + // Start to discover the boot server to get (m)tftp server ip address, bootfile + // name and bootfile size. + // + UseBis = (BOOLEAN) (Mode->BisSupported && Mode->BisDetected); + Status = PxeBc->Discover (PxeBc, Type, &Layer, UseBis, NULL); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Mode->PxeReplyReceived && !Mode->ProxyOfferReceived) { + // + // Some network boot loader only search the packet in Mode.ProxyOffer to get its server + // IP address, so we need to store a copy of Mode.PxeReply packet into Mode.ProxyOffer. + // + if (Mode->UsingIpv6) { + CopyMem ( + &Mode->ProxyOffer.Dhcpv6, + &Mode->PxeReply.Dhcpv6, + Private->PxeReply.Dhcp6.Packet.Ack.Length + ); + } else { + CopyMem ( + &Mode->ProxyOffer.Dhcpv4, + &Mode->PxeReply.Dhcpv4, + Private->PxeReply.Dhcp4.Packet.Ack.Length + ); + } + Mode->ProxyOfferReceived = TRUE; + } + } + + // + // Parse the boot information. + // + if (Mode->UsingIpv6) { + Status = PxeBcDhcp6BootInfo (Private, BufferSize); + } else { + Status = PxeBcDhcp4BootInfo (Private, BufferSize); + } + + return Status; +} + + +/** + Install PxeBaseCodeCallbackProtocol if not installed before. + + @param[in, out] Private Pointer to PxeBc private data. + @param[out] NewMakeCallback If TRUE, it is a new callback. + Otherwise, it is not new callback. + @retval EFI_SUCCESS PxeBaseCodeCallbackProtocol installed succesfully. + @retval Others Failed to install PxeBaseCodeCallbackProtocol. + +**/ +EFI_STATUS +PxeBcInstallCallback ( + IN OUT PXEBC_PRIVATE_DATA *Private, + OUT BOOLEAN *NewMakeCallback + ) +{ + EFI_PXE_BASE_CODE_PROTOCOL *PxeBc; + EFI_STATUS Status; + + // + // Check whether PxeBaseCodeCallbackProtocol already installed. + // + PxeBc = &Private->PxeBc; + Status = gBS->HandleProtocol ( + Private->Mode.UsingIpv6 ? Private->Ip6Nic->Controller : Private->Ip4Nic->Controller, + &gEfiPxeBaseCodeCallbackProtocolGuid, + (VOID **) &Private->PxeBcCallback + ); + if (Status == EFI_UNSUPPORTED) { + + CopyMem ( + &Private->LoadFileCallback, + &gPxeBcCallBackTemplate, + sizeof (EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL) + ); + + // + // Install a default callback if user didn't offer one. + // + Status = gBS->InstallProtocolInterface ( + Private->Mode.UsingIpv6 ? &Private->Ip6Nic->Controller : &Private->Ip4Nic->Controller, + &gEfiPxeBaseCodeCallbackProtocolGuid, + EFI_NATIVE_INTERFACE, + &Private->LoadFileCallback + ); + + (*NewMakeCallback) = (BOOLEAN) (Status == EFI_SUCCESS); + + Status = PxeBc->SetParameters (PxeBc, NULL, NULL, NULL, NULL, NewMakeCallback); + if (EFI_ERROR (Status)) { + PxeBc->Stop (PxeBc); + return Status; + } + } + + return EFI_SUCCESS; +} + + +/** + Uninstall PxeBaseCodeCallbackProtocol. + + @param[in] Private Pointer to PxeBc private data. + @param[in] NewMakeCallback If TRUE, it is a new callback. + Otherwise, it is not new callback. + +**/ +VOID +PxeBcUninstallCallback ( + IN PXEBC_PRIVATE_DATA *Private, + IN BOOLEAN NewMakeCallback + ) +{ + EFI_PXE_BASE_CODE_PROTOCOL *PxeBc; + + PxeBc = &Private->PxeBc; + + if (NewMakeCallback) { + + NewMakeCallback = FALSE; + + PxeBc->SetParameters (PxeBc, NULL, NULL, NULL, NULL, &NewMakeCallback); + + gBS->UninstallProtocolInterface ( + Private->Mode.UsingIpv6 ? Private->Ip6Nic->Controller : Private->Ip4Nic->Controller, + &gEfiPxeBaseCodeCallbackProtocolGuid, + &Private->LoadFileCallback + ); + } +} + + +/** + Download one of boot file in the list, and it's special for IPv6. + + @param[in] Private Pointer to PxeBc private data. + @param[in, out] BufferSize Size of user buffer for input; + required buffer size for output. + @param[in] Buffer Pointer to user buffer. + + @retval EFI_SUCCESS Read one of boot file in the list successfully. + @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file. + @retval EFI_NOT_FOUND There is no proper boot file available. + @retval Others Failed to download boot file in the list. + +**/ +EFI_STATUS +PxeBcReadBootFileList ( + IN PXEBC_PRIVATE_DATA *Private, + IN OUT UINT64 *BufferSize, + IN VOID *Buffer OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_PXE_BASE_CODE_PROTOCOL *PxeBc; + + PxeBc = &Private->PxeBc; + + // + // Try to download the boot file if everything is ready. + // + if (Buffer != NULL) { + Status = PxeBc->Mtftp ( + PxeBc, + EFI_PXE_BASE_CODE_TFTP_READ_FILE, + Buffer, + FALSE, + BufferSize, + &Private->BlockSize, + &Private->ServerIp, + Private->BootFileName, + NULL, + FALSE + ); + + + } else { + Status = EFI_BUFFER_TOO_SMALL; + } + + return Status; +} + + +/** + Load boot file into user buffer. + + @param[in] Private Pointer to PxeBc private data. + @param[in, out] BufferSize Size of user buffer for input; + required buffer size for output. + @param[in] Buffer Pointer to user buffer. + + @retval EFI_SUCCESS Get all the boot information successfully. + @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file. + @retval EFI_ABORTED User cancelled the current operation. + @retval Others Failed to parse out the boot information. + +**/ +EFI_STATUS +PxeBcLoadBootFile ( + IN PXEBC_PRIVATE_DATA *Private, + IN OUT UINTN *BufferSize, + IN VOID *Buffer OPTIONAL + ) +{ + BOOLEAN NewMakeCallback; + UINT64 RequiredSize; + UINT64 CurrentSize; + EFI_STATUS Status; + EFI_PXE_BASE_CODE_PROTOCOL *PxeBc; + EFI_PXE_BASE_CODE_MODE *PxeBcMode; + + NewMakeCallback = FALSE; + PxeBc = &Private->PxeBc; + PxeBcMode = &Private->Mode; + CurrentSize = *BufferSize; + RequiredSize = 0; + + // + // Install pxebc callback protocol if hasn't been installed yet. + // + Status = PxeBcInstallCallback (Private, &NewMakeCallback); + if (EFI_ERROR(Status)) { + return Status; + } + + if (Private->BootFileSize == 0) { + // + // Discover the boot information about the bootfile if hasn't. + // + Status = PxeBcDiscoverBootFile (Private, &RequiredSize); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + if (PXEBC_IS_SIZE_OVERFLOWED (RequiredSize)) { + // + // It's error if the required buffer size is beyond the system scope. + // + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } else if (RequiredSize > 0) { + // + // Get the right buffer size of the bootfile required. + // + if (CurrentSize < RequiredSize || Buffer == NULL) { + // + // It's buffer too small if the size of user buffer is smaller than the required. + // + CurrentSize = RequiredSize; + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + CurrentSize = RequiredSize; + } else if (RequiredSize == 0 && PxeBcMode->UsingIpv6) { + // + // Try to download another bootfile in list if failed to get the filesize of the last one. + // It's special for the case of IPv6. + // + Status = PxeBcReadBootFileList (Private, &CurrentSize, Buffer); + goto ON_EXIT; + } + } else if (CurrentSize < Private->BootFileSize || Buffer == NULL ) { + // + // It's buffer too small if the size of user buffer is smaller than the required. + // + CurrentSize = Private->BootFileSize; + Status = EFI_BUFFER_TOO_SMALL; + goto ON_EXIT; + } + + // + // Begin to download the bootfile if everything is ready. + // + AsciiPrint ("\n Downloading NBP file...\n"); + if (PxeBcMode->UsingIpv6) { + Status = PxeBcReadBootFileList ( + Private, + &CurrentSize, + Buffer + ); + } else { + Status = PxeBc->Mtftp ( + PxeBc, + EFI_PXE_BASE_CODE_TFTP_READ_FILE, + Buffer, + FALSE, + &CurrentSize, + &Private->BlockSize, + &Private->ServerIp, + Private->BootFileName, + NULL, + FALSE + ); + } + +ON_EXIT: + *BufferSize = (UINTN) CurrentSize; + PxeBcUninstallCallback(Private, NewMakeCallback); + + if (Status == EFI_SUCCESS) { + AsciiPrint ("\n NBP file downloaded successfully.\n"); + return EFI_SUCCESS; + } else if (Status == EFI_BUFFER_TOO_SMALL && Buffer != NULL) { + AsciiPrint ("\n PXE-E05: Buffer size is smaller than the requested file.\n"); + } else if (Status == EFI_DEVICE_ERROR) { + AsciiPrint ("\n PXE-E07: Network device error.\n"); + } else if (Status == EFI_OUT_OF_RESOURCES) { + AsciiPrint ("\n PXE-E09: Could not allocate I/O buffers.\n"); + } else if (Status == EFI_NO_MEDIA) { + AsciiPrint ("\n PXE-E12: Could not detect network connection.\n"); + } else if (Status == EFI_NO_RESPONSE) { + AsciiPrint ("\n PXE-E16: No valid offer received.\n"); + } else if (Status == EFI_TIMEOUT) { + AsciiPrint ("\n PXE-E18: Server response timeout.\n"); + } else if (Status == EFI_ABORTED) { + AsciiPrint ("\n PXE-E21: Remote boot cancelled.\n"); + } else if (Status == EFI_ICMP_ERROR) { + AsciiPrint ("\n PXE-E22: Client received ICMP error from server.\n"); + } else if (Status == EFI_TFTP_ERROR) { + AsciiPrint ("\n PXE-E23: Client received TFTP error from server.\n"); + } else if (Status == EFI_NOT_FOUND) { + AsciiPrint ("\n PXE-E53: No boot filename received.\n"); + } else if (Status != EFI_BUFFER_TOO_SMALL) { + AsciiPrint ("\n PXE-E99: Unexpected network error.\n"); + } + + return Status; +} + diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcBoot.h b/NetworkPkg/UefiPxeBcDxe/PxeBcBoot.h new file mode 100644 index 000000000..3c73740b9 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcBoot.h @@ -0,0 +1,94 @@ +/** @file + Boot functions declaration for UefiPxeBc Driver. + + Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_PXEBC_BOOT_H__ +#define __EFI_PXEBC_BOOT_H__ + +#define PXEBC_DISPLAY_MAX_LINE 70 +#define PXEBC_DEFAULT_UDP_OVERHEAD_SIZE 8 +#define PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE 4 + +#define PXEBC_IS_SIZE_OVERFLOWED(x) ((sizeof (UINTN) < sizeof (UINT64)) && ((x) > 0xFFFFFFFF)) + + +/** + Extract the discover information and boot server entry from the + cached packets if unspecified. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Type The type of bootstrap to perform. + @param[in, out] DiscoverInfo Pointer to EFI_PXE_BASE_CODE_DISCOVER_INFO. + @param[out] BootEntry Pointer to PXEBC_BOOT_SVR_ENTRY. + @param[out] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST. + + @retval EFI_SUCCESS Successfully extracted the information. + @retval EFI_DEVICE_ERROR Failed to extract the information. + +**/ +EFI_STATUS +PxeBcExtractDiscoverInfo ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT16 Type, + IN OUT EFI_PXE_BASE_CODE_DISCOVER_INFO **DiscoverInfo, + OUT PXEBC_BOOT_SVR_ENTRY **BootEntry, + OUT EFI_PXE_BASE_CODE_SRVLIST **SrvList + ); + + +/** + Build the discover packet and send out for boot. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Type PxeBc option boot item type. + @param[in] Layer Pointer to option boot item layer. + @param[in] UseBis Use BIS or not. + @param[in] DestIp Pointer to the server address. + @param[in] IpCount The total count of the server address. + @param[in] SrvList Pointer to the server address list. + + @retval EFI_SUCCESS Successfully discovered boot file. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_NOT_FOUND Can't get the PXE reply packet. + @retval Others Failed to discover boot file. + +**/ +EFI_STATUS +PxeBcDiscoverBootServer ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT16 Type, + IN UINT16 *Layer, + IN BOOLEAN UseBis, + IN EFI_IP_ADDRESS *DestIp, + IN UINT16 IpCount, + IN EFI_PXE_BASE_CODE_SRVLIST *SrvList + ); + + +/** + Load boot file into user buffer. + + @param[in] Private Pointer to PxeBc private data. + @param[in, out] BufferSize Size of user buffer for input; + required buffer size for output. + @param[in] Buffer Pointer to user buffer. + + @retval EFI_SUCCESS Successfully obtained all the boot information. + @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file. + @retval EFI_ABORTED User cancelled the current operation. + @retval Others Failed to parse out the boot information. + +**/ +EFI_STATUS +PxeBcLoadBootFile ( + IN PXEBC_PRIVATE_DATA *Private, + IN OUT UINTN *BufferSize, + IN VOID *Buffer OPTIONAL + ); + +#endif diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.c b/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.c new file mode 100644 index 000000000..bb5e53b5b --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.c @@ -0,0 +1,1756 @@ +/** @file + Functions implementation related with DHCPv4 for UefiPxeBc Driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "PxeBcImpl.h" + +// +// This is a map from the interested DHCP4 option tags' index to the tag value. +// +UINT8 mInterestedDhcp4Tags[PXEBC_DHCP4_TAG_INDEX_MAX] = { + DHCP4_TAG_BOOTFILE_LEN, + DHCP4_TAG_VENDOR, + DHCP4_TAG_OVERLOAD, + DHCP4_TAG_MSG_TYPE, + DHCP4_TAG_SERVER_ID, + DHCP4_TAG_VENDOR_CLASS_ID, + DHCP4_TAG_BOOTFILE +}; + +// +// There are 4 times retries with the value of 4, 8, 16 and 32, refers to PXE2.1 spec. +// +UINT32 mPxeDhcpTimeout[4] = {4, 8, 16, 32}; + + +/** + Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer. + + @param[in] Buffer Pointer to the option buffer. + @param[in] Length Length of the option buffer. + @param[in] OptTag Tag of the required option. + + @retval NULL Failed to find the required option. + @retval Others The position of the required option. + +**/ +EFI_DHCP4_PACKET_OPTION * +PxeBcParseDhcp4Options ( + IN UINT8 *Buffer, + IN UINT32 Length, + IN UINT8 OptTag + ) +{ + EFI_DHCP4_PACKET_OPTION *Option; + UINT32 Offset; + + Option = (EFI_DHCP4_PACKET_OPTION *) Buffer; + Offset = 0; + + while (Offset < Length && Option->OpCode != DHCP4_TAG_EOP) { + + if (Option->OpCode == OptTag) { + // + // Found the required option. + // + return Option; + } + + // + // Skip the current option to the next. + // + if (Option->OpCode == DHCP4_TAG_PAD) { + Offset++; + } else { + Offset += Option->Length + 2; + } + + Option = (EFI_DHCP4_PACKET_OPTION *) (Buffer + Offset); + } + + return NULL; +} + + +/** + Parse the PXE vender options and extract the information from them. + + @param[in] Dhcp4Option Pointer to vendor options in buffer. + @param[in] VendorOption Pointer to structure to store information in vendor options. + +**/ +VOID +PxeBcParseVendorOptions ( + IN EFI_DHCP4_PACKET_OPTION *Dhcp4Option, + IN PXEBC_VENDOR_OPTION *VendorOption + ) +{ + UINT32 *BitMap; + UINT8 VendorOptionLen; + EFI_DHCP4_PACKET_OPTION *PxeOption; + UINT8 Offset; + + BitMap = VendorOption->BitMap; + VendorOptionLen = Dhcp4Option->Length; + PxeOption = (EFI_DHCP4_PACKET_OPTION *) &Dhcp4Option->Data[0]; + Offset = 0; + + ASSERT (PxeOption != NULL); + + while ((Offset < VendorOptionLen) && (PxeOption->OpCode != DHCP4_TAG_EOP)) { + // + // Parse all the interesting PXE vendor options one by one. + // + switch (PxeOption->OpCode) { + + case PXEBC_VENDOR_TAG_MTFTP_IP: + + CopyMem (&VendorOption->MtftpIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS)); + break; + + case PXEBC_VENDOR_TAG_MTFTP_CPORT: + + CopyMem (&VendorOption->MtftpCPort, PxeOption->Data, sizeof (VendorOption->MtftpCPort)); + break; + + case PXEBC_VENDOR_TAG_MTFTP_SPORT: + + CopyMem (&VendorOption->MtftpSPort, PxeOption->Data, sizeof (VendorOption->MtftpSPort)); + break; + + case PXEBC_VENDOR_TAG_MTFTP_TIMEOUT: + + VendorOption->MtftpTimeout = *PxeOption->Data; + break; + + case PXEBC_VENDOR_TAG_MTFTP_DELAY: + + VendorOption->MtftpDelay = *PxeOption->Data; + break; + + case PXEBC_VENDOR_TAG_DISCOVER_CTRL: + + VendorOption->DiscoverCtrl = *PxeOption->Data; + break; + + case PXEBC_VENDOR_TAG_DISCOVER_MCAST: + + CopyMem (&VendorOption->DiscoverMcastIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS)); + break; + + case PXEBC_VENDOR_TAG_BOOT_SERVERS: + + VendorOption->BootSvrLen = PxeOption->Length; + VendorOption->BootSvr = (PXEBC_BOOT_SVR_ENTRY *) PxeOption->Data; + break; + + case PXEBC_VENDOR_TAG_BOOT_MENU: + + VendorOption->BootMenuLen = PxeOption->Length; + VendorOption->BootMenu = (PXEBC_BOOT_MENU_ENTRY *) PxeOption->Data; + break; + + case PXEBC_VENDOR_TAG_MENU_PROMPT: + + VendorOption->MenuPromptLen = PxeOption->Length; + VendorOption->MenuPrompt = (PXEBC_MENU_PROMPT *) PxeOption->Data; + break; + + case PXEBC_VENDOR_TAG_MCAST_ALLOC: + + CopyMem (&VendorOption->McastIpBase, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&VendorOption->McastIpBlock, PxeOption->Data + 4, sizeof (VendorOption->McastIpBlock)); + CopyMem (&VendorOption->McastIpRange, PxeOption->Data + 6, sizeof (VendorOption->McastIpRange)); + break; + + case PXEBC_VENDOR_TAG_CREDENTIAL_TYPES: + + VendorOption->CredTypeLen = PxeOption->Length; + VendorOption->CredType = (UINT32 *) PxeOption->Data; + break; + + case PXEBC_VENDOR_TAG_BOOT_ITEM: + + CopyMem (&VendorOption->BootSrvType, PxeOption->Data, sizeof (VendorOption->BootSrvType)); + CopyMem (&VendorOption->BootSrvLayer, PxeOption->Data + 2, sizeof (VendorOption->BootSrvLayer)); + break; + + default: + // + // Not interesting PXE vendor options. + // + break; + } + + // + // Set the bit map for the special PXE options. + // + SET_VENDOR_OPTION_BIT_MAP (BitMap, PxeOption->OpCode); + + // + // Continue to the next option. + // + if (PxeOption->OpCode == DHCP4_TAG_PAD) { + Offset++; + } else { + Offset = (UINT8) (Offset + PxeOption->Length + 2); + } + + PxeOption = (EFI_DHCP4_PACKET_OPTION *) (Dhcp4Option->Data + Offset); + } +} + + +/** + Build the options buffer for the DHCPv4 request packet. + + @param[in] Private Pointer to PxeBc private data. + @param[out] OptList Pointer to the option pointer array. + @param[in] Buffer Pointer to the buffer to contain the option list. + @param[in] NeedMsgType If TRUE, it is necessary to include the Msg type option. + Otherwise, it is not necessary. + + @return Index The count of the built-in options. + +**/ +UINT32 +PxeBcBuildDhcp4Options ( + IN PXEBC_PRIVATE_DATA *Private, + OUT EFI_DHCP4_PACKET_OPTION **OptList, + IN UINT8 *Buffer, + IN BOOLEAN NeedMsgType + ) +{ + UINT32 Index; + PXEBC_DHCP4_OPTION_ENTRY OptEnt; + UINT16 Value; + + Index = 0; + OptList[0] = (EFI_DHCP4_PACKET_OPTION *) Buffer; + + if (NeedMsgType) { + // + // Append message type. + // + OptList[Index]->OpCode = DHCP4_TAG_MSG_TYPE; + OptList[Index]->Length = 1; + OptEnt.Mesg = (PXEBC_DHCP4_OPTION_MESG *) OptList[Index]->Data; + OptEnt.Mesg->Type = PXEBC_DHCP4_MSG_TYPE_REQUEST; + Index++; + OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]); + + // + // Append max message size. + // + OptList[Index]->OpCode = DHCP4_TAG_MAXMSG; + OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE); + OptEnt.MaxMesgSize = (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE *) OptList[Index]->Data; + Value = NTOHS (PXEBC_DHCP4_PACKET_MAX_SIZE); + CopyMem (&OptEnt.MaxMesgSize->Size, &Value, sizeof (UINT16)); + Index++; + OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]); + } + + // + // Append parameter request list option. + // + OptList[Index]->OpCode = DHCP4_TAG_PARA_LIST; + OptList[Index]->Length = 35; + OptEnt.Para = (PXEBC_DHCP4_OPTION_PARA *) OptList[Index]->Data; + OptEnt.Para->ParaList[0] = DHCP4_TAG_NETMASK; + OptEnt.Para->ParaList[1] = DHCP4_TAG_TIME_OFFSET; + OptEnt.Para->ParaList[2] = DHCP4_TAG_ROUTER; + OptEnt.Para->ParaList[3] = DHCP4_TAG_TIME_SERVER; + OptEnt.Para->ParaList[4] = DHCP4_TAG_NAME_SERVER; + OptEnt.Para->ParaList[5] = DHCP4_TAG_DNS_SERVER; + OptEnt.Para->ParaList[6] = DHCP4_TAG_HOSTNAME; + OptEnt.Para->ParaList[7] = DHCP4_TAG_BOOTFILE_LEN; + OptEnt.Para->ParaList[8] = DHCP4_TAG_DOMAINNAME; + OptEnt.Para->ParaList[9] = DHCP4_TAG_ROOTPATH; + OptEnt.Para->ParaList[10] = DHCP4_TAG_EXTEND_PATH; + OptEnt.Para->ParaList[11] = DHCP4_TAG_EMTU; + OptEnt.Para->ParaList[12] = DHCP4_TAG_TTL; + OptEnt.Para->ParaList[13] = DHCP4_TAG_BROADCAST; + OptEnt.Para->ParaList[14] = DHCP4_TAG_NIS_DOMAIN; + OptEnt.Para->ParaList[15] = DHCP4_TAG_NIS_SERVER; + OptEnt.Para->ParaList[16] = DHCP4_TAG_NTP_SERVER; + OptEnt.Para->ParaList[17] = DHCP4_TAG_VENDOR; + OptEnt.Para->ParaList[18] = DHCP4_TAG_REQUEST_IP; + OptEnt.Para->ParaList[19] = DHCP4_TAG_LEASE; + OptEnt.Para->ParaList[20] = DHCP4_TAG_SERVER_ID; + OptEnt.Para->ParaList[21] = DHCP4_TAG_T1; + OptEnt.Para->ParaList[22] = DHCP4_TAG_T2; + OptEnt.Para->ParaList[23] = DHCP4_TAG_VENDOR_CLASS_ID; + OptEnt.Para->ParaList[24] = DHCP4_TAG_TFTP; + OptEnt.Para->ParaList[25] = DHCP4_TAG_BOOTFILE; + OptEnt.Para->ParaList[26] = DHCP4_TAG_UUID; + OptEnt.Para->ParaList[27] = 0x80; + OptEnt.Para->ParaList[28] = 0x81; + OptEnt.Para->ParaList[29] = 0x82; + OptEnt.Para->ParaList[30] = 0x83; + OptEnt.Para->ParaList[31] = 0x84; + OptEnt.Para->ParaList[32] = 0x85; + OptEnt.Para->ParaList[33] = 0x86; + OptEnt.Para->ParaList[34] = 0x87; + Index++; + OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]); + + // + // Append UUID/Guid-based client identifier option + // + OptList[Index]->OpCode = DHCP4_TAG_UUID; + OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_UUID); + OptEnt.Uuid = (PXEBC_DHCP4_OPTION_UUID *) OptList[Index]->Data; + OptEnt.Uuid->Type = 0; + Index++; + OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]); + + if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) OptEnt.Uuid->Guid))) { + // + // Zero the Guid to indicate NOT programable if failed to get system Guid. + // + DEBUG ((EFI_D_WARN, "PXE: Failed to read system GUID from the smbios table!\n")); + ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID)); + } + + // + // Append client network device interface option + // + OptList[Index]->OpCode = DHCP4_TAG_UNDI; + OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_UNDI); + OptEnt.Undi = (PXEBC_DHCP4_OPTION_UNDI *) OptList[Index]->Data; + + if (Private->Nii != NULL) { + OptEnt.Undi->Type = Private->Nii->Type; + OptEnt.Undi->MajorVer = Private->Nii->MajorVer; + OptEnt.Undi->MinorVer = Private->Nii->MinorVer; + } else { + OptEnt.Undi->Type = DEFAULT_UNDI_TYPE; + OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR; + OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR; + } + + Index++; + OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]); + + // + // Append client system architecture option + // + OptList[Index]->OpCode = DHCP4_TAG_ARCH; + OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_ARCH); + OptEnt.Arch = (PXEBC_DHCP4_OPTION_ARCH *) OptList[Index]->Data; + Value = HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE); + CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16)); + Index++; + OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]); + + // + // Append vendor class identify option + // + OptList[Index]->OpCode = DHCP4_TAG_VENDOR_CLASS_ID; + OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_CLID); + OptEnt.Clid = (PXEBC_DHCP4_OPTION_CLID *) OptList[Index]->Data; + CopyMem ( + OptEnt.Clid, + DEFAULT_CLASS_ID_DATA, + sizeof (PXEBC_DHCP4_OPTION_CLID) + ); + PxeBcUintnToAscDecWithFormat ( + EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE, + OptEnt.Clid->ArchitectureType, + sizeof (OptEnt.Clid->ArchitectureType) + ); + + if (Private->Nii != NULL) { + CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName)); + PxeBcUintnToAscDecWithFormat (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor)); + PxeBcUintnToAscDecWithFormat (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor)); + } + + Index++; + + return Index; +} + + +/** + Create a template DHCPv4 packet as a seed. + + @param[out] Seed Pointer to the seed packet. + @param[in] Udp4 Pointer to EFI_UDP4_PROTOCOL. + +**/ +VOID +PxeBcSeedDhcp4Packet ( + OUT EFI_DHCP4_PACKET *Seed, + IN EFI_UDP4_PROTOCOL *Udp4 + ) +{ + EFI_SIMPLE_NETWORK_MODE Mode; + EFI_DHCP4_HEADER *Header; + + // + // Get IfType and HwAddressSize from SNP mode data. + // + Udp4->GetModeData (Udp4, NULL, NULL, NULL, &Mode); + + Seed->Size = sizeof (EFI_DHCP4_PACKET); + Seed->Length = sizeof (Seed->Dhcp4); + Header = &Seed->Dhcp4.Header; + ZeroMem (Header, sizeof (EFI_DHCP4_HEADER)); + Header->OpCode = PXEBC_DHCP4_OPCODE_REQUEST; + Header->HwType = Mode.IfType; + Header->HwAddrLen = (UINT8) Mode.HwAddressSize; + CopyMem (Header->ClientHwAddr, &Mode.CurrentAddress, Header->HwAddrLen); + + Seed->Dhcp4.Magik = PXEBC_DHCP4_MAGIC; + Seed->Dhcp4.Option[0] = DHCP4_TAG_EOP; +} + + +/** + Cache the DHCPv4 packet. + + @param[in] Dst Pointer to the cache buffer for DHCPv4 packet. + @param[in] Src Pointer to the DHCPv4 packet to be cached. + + @retval EFI_SUCCESS Packet is copied. + @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet. + +**/ +EFI_STATUS +PxeBcCacheDhcp4Packet ( + IN EFI_DHCP4_PACKET *Dst, + IN EFI_DHCP4_PACKET *Src + ) +{ + if (Dst->Size < Src->Length) { + return EFI_BUFFER_TOO_SMALL; + } + + CopyMem (&Dst->Dhcp4, &Src->Dhcp4, Src->Length); + Dst->Length = Src->Length; + + return EFI_SUCCESS; +} + + +/** + Parse the cached DHCPv4 packet, including all the options. + + @param[in] Cache4 Pointer to cached DHCPv4 packet. + + @retval EFI_SUCCESS Parsed the DHCPv4 packet successfully. + @retval EFI_DEVICE_ERROR Failed to parse and invalid packet. + +**/ +EFI_STATUS +PxeBcParseDhcp4Packet ( + IN PXEBC_DHCP4_PACKET_CACHE *Cache4 + ) +{ + EFI_DHCP4_PACKET *Offer; + EFI_DHCP4_PACKET_OPTION **Options; + EFI_DHCP4_PACKET_OPTION *Option; + PXEBC_OFFER_TYPE OfferType; + UINTN Index; + BOOLEAN IsProxyOffer; + BOOLEAN IsPxeOffer; + UINT8 *Ptr8; + BOOLEAN FileFieldOverloaded; + + IsProxyOffer = FALSE; + IsPxeOffer = FALSE; + FileFieldOverloaded = FALSE; + + ZeroMem (Cache4->OptList, sizeof (Cache4->OptList)); + ZeroMem (&Cache4->VendorOpt, sizeof (Cache4->VendorOpt)); + + Offer = &Cache4->Packet.Offer; + Options = Cache4->OptList; + + // + // Parse DHCPv4 options in this offer, and store the pointers. + // First, try to parse DHCPv4 options from the DHCP optional parameters field. + // + for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) { + Options[Index] = PxeBcParseDhcp4Options ( + Offer->Dhcp4.Option, + GET_OPTION_BUFFER_LEN (Offer), + mInterestedDhcp4Tags[Index] + ); + } + // + // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132. + // If yes, try to parse options from the BootFileName field, then ServerName field. + // + Option = Options[PXEBC_DHCP4_TAG_INDEX_OVERLOAD]; + if (Option != NULL) { + if ((Option->Data[0] & PXEBC_DHCP4_OVERLOAD_FILE) != 0) { + FileFieldOverloaded = TRUE; + for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) { + if (Options[Index] == NULL) { + Options[Index] = PxeBcParseDhcp4Options ( + (UINT8 *) Offer->Dhcp4.Header.BootFileName, + sizeof (Offer->Dhcp4.Header.BootFileName), + mInterestedDhcp4Tags[Index] + ); + } + } + } + if ((Option->Data[0] & PXEBC_DHCP4_OVERLOAD_SERVER_NAME) != 0) { + for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) { + if (Options[Index] == NULL) { + Options[Index] = PxeBcParseDhcp4Options ( + (UINT8 *) Offer->Dhcp4.Header.ServerName, + sizeof (Offer->Dhcp4.Header.ServerName), + mInterestedDhcp4Tags[Index] + ); + } + } + } + } + + // + // The offer with zero "yiaddr" is a proxy offer. + // + if (Offer->Dhcp4.Header.YourAddr.Addr[0] == 0) { + IsProxyOffer = TRUE; + } + + // + // The offer with "PXEClient" is a PXE offer. + // + Option = Options[PXEBC_DHCP4_TAG_INDEX_CLASS_ID]; + if ((Option != NULL) && (Option->Length >= 9) && + (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 9) == 0)) { + IsPxeOffer = TRUE; + } + + // + // Parse PXE vendor options in this offer, and store the contents/pointers. + // + Option = Options[PXEBC_DHCP4_TAG_INDEX_VENDOR]; + if (IsPxeOffer && Option != NULL) { + PxeBcParseVendorOptions (Option, &Cache4->VendorOpt); + } + + // + // Parse PXE boot file name: + // According to PXE spec, boot file name should be read from DHCP option 67 (bootfile name) if present. + // Otherwise, read from boot file field in DHCP header. + // + if (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) { + // + // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null + // terminated string. So force to append null terminated character at the end of string. + // + Ptr8 = (UINT8*)&Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Data[0]; + Ptr8 += Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Length; + if (*(Ptr8 - 1) != '\0') { + *Ptr8 = '\0'; + } + } else if (!FileFieldOverloaded && Offer->Dhcp4.Header.BootFileName[0] != 0) { + // + // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it. + // Do not count dhcp option header here, or else will destroy the serverhostname. + // + Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *) + (&Offer->Dhcp4.Header.BootFileName[0] - + OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0])); + + } + + // + // Determine offer type of the DHCPv4 packet. + // + Option = Options[PXEBC_DHCP4_TAG_INDEX_MSG_TYPE]; + if (Option == NULL || Option->Data[0] == 0) { + // + // It's a Bootp offer. + // + OfferType = PxeOfferTypeBootp; + + Option = Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]; + if (Option == NULL) { + // + // If the Bootp offer without bootfilename, discard it. + // + return EFI_DEVICE_ERROR; + } + } else { + + if (IS_VALID_DISCOVER_VENDOR_OPTION (Cache4->VendorOpt.BitMap)) { + // + // It's a PXE10 offer with PXEClient and discover vendor option. + // + OfferType = IsProxyOffer ? PxeOfferTypeProxyPxe10 : PxeOfferTypeDhcpPxe10; + } else if (IS_VALID_MTFTP_VENDOR_OPTION (Cache4->VendorOpt.BitMap)) { + // + // It's a WFM11a offer with PXEClient and mtftp vendor option. + // But multi-cast download is not supported currently, so discard it. + // + return EFI_DEVICE_ERROR; + } else if (IsPxeOffer) { + // + // It's a BINL offer only with PXEClient. + // + OfferType = IsProxyOffer ? PxeOfferTypeProxyBinl : PxeOfferTypeDhcpBinl; + } else { + // + // It's a DHCPv4 only offer, which is a pure DHCPv4 offer packet. + // + OfferType = PxeOfferTypeDhcpOnly; + } + } + + Cache4->OfferType = OfferType; + + return EFI_SUCCESS; +} + + +/** + Cache the DHCPv4 ack packet, and parse it on demand. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Ack Pointer to the DHCPv4 ack packet. + @param[in] Verified If TRUE, parse the ACK packet and store info into mode data. + + @retval EFI_SUCCESS Cache and parse the packet successfully. + @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet. + +**/ +EFI_STATUS +PxeBcCopyDhcp4Ack ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_DHCP4_PACKET *Ack, + IN BOOLEAN Verified + ) +{ + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_STATUS Status; + + Mode = Private->PxeBc.Mode; + + Status = PxeBcCacheDhcp4Packet (&Private->DhcpAck.Dhcp4.Packet.Ack, Ack); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Verified) { + // + // Parse the ack packet and store it into mode data if needed. + // + PxeBcParseDhcp4Packet (&Private->DhcpAck.Dhcp4); + CopyMem (&Mode->DhcpAck.Dhcpv4, &Ack->Dhcp4, Ack->Length); + Mode->DhcpAckReceived = TRUE; + } + + return EFI_SUCCESS; +} + + +/** + Cache the DHCPv4 proxy offer packet according to the received order. + + @param[in] Private Pointer to PxeBc private data. + @param[in] OfferIndex The received order of offer packets. + + @retval EFI_SUCCESS Cache and parse the packet successfully. + @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet. + +**/ +EFI_STATUS +PxeBcCopyProxyOffer ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT32 OfferIndex + ) +{ + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_DHCP4_PACKET *Offer; + EFI_STATUS Status; + + ASSERT (OfferIndex < Private->OfferNum); + ASSERT (OfferIndex < PXEBC_OFFER_MAX_NUM); + + Mode = Private->PxeBc.Mode; + Offer = &Private->OfferBuffer[OfferIndex].Dhcp4.Packet.Offer; + + // + // Cache the proxy offer packet and parse it. + // + Status = PxeBcCacheDhcp4Packet (&Private->ProxyOffer.Dhcp4.Packet.Offer, Offer); + if (EFI_ERROR(Status)) { + return Status; + } + + PxeBcParseDhcp4Packet (&Private->ProxyOffer.Dhcp4); + + // + // Store this packet into mode data. + // + CopyMem (&Mode->ProxyOffer.Dhcpv4, &Offer->Dhcp4, Offer->Length); + Mode->ProxyOfferReceived = TRUE; + + return EFI_SUCCESS; +} + + +/** + Retry to request bootfile name by the BINL offer. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Index The received order of offer packets. + + @retval EFI_SUCCESS Successfully retried to request bootfile name. + @retval EFI_DEVICE_ERROR Failed to retry bootfile name. + +**/ +EFI_STATUS +PxeBcRetryBinlOffer ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT32 Index + ) +{ + EFI_DHCP4_PACKET *Offer; + EFI_IP_ADDRESS ServerIp; + EFI_STATUS Status; + PXEBC_DHCP4_PACKET_CACHE *Cache4; + EFI_DHCP4_PACKET *Reply; + + ASSERT (Index < PXEBC_OFFER_MAX_NUM); + ASSERT (Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeDhcpBinl || + Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeProxyBinl); + + Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer; + + // + // Prefer to siaddr in header as next server address. If it's zero, then use option 54. + // + if (Offer->Dhcp4.Header.ServerAddr.Addr[0] == 0) { + CopyMem ( + &ServerIp.Addr[0], + Private->OfferBuffer[Index].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data, + sizeof (EFI_IPv4_ADDRESS) + ); + } else { + CopyMem ( + &ServerIp.Addr[0], + &Offer->Dhcp4.Header.ServerAddr, + sizeof (EFI_IPv4_ADDRESS) + ); + } + + Private->IsDoDiscover = FALSE; + Cache4 = &Private->ProxyOffer.Dhcp4; + Reply = &Cache4->Packet.Offer; + + // + // Send another request packet for bootfile name. + // + Status = PxeBcDhcp4Discover ( + Private, + 0, + NULL, + FALSE, + &ServerIp, + 0, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Parse the reply for the last request packet. + // + Status = PxeBcParseDhcp4Packet (Cache4); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Cache4->OfferType != PxeOfferTypeProxyPxe10 && + Cache4->OfferType != PxeOfferTypeProxyWfm11a && + Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) { + // + // This BINL ack doesn't have discovery option set or multicast option set + // or bootfile name specified. + // + return EFI_DEVICE_ERROR; + } + + // + // Store the reply into mode data. + // + Private->PxeBc.Mode->ProxyOfferReceived = TRUE; + CopyMem (&Private->PxeBc.Mode->ProxyOffer.Dhcpv4, &Reply->Dhcp4, Reply->Length); + + return EFI_SUCCESS; +} + + +/** + Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount. + + @param[in] Private Pointer to PxeBc private data. + @param[in] RcvdOffer Pointer to the received offer packet. + + @retval EFI_SUCCESS Cache and parse the packet successfully. + @retval Others Operation failed. + +**/ +EFI_STATUS +PxeBcCacheDhcp4Offer ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_DHCP4_PACKET *RcvdOffer + ) +{ + PXEBC_DHCP4_PACKET_CACHE *Cache4; + EFI_DHCP4_PACKET *Offer; + PXEBC_OFFER_TYPE OfferType; + EFI_STATUS Status; + + ASSERT (Private->OfferNum < PXEBC_OFFER_MAX_NUM); + Cache4 = &Private->OfferBuffer[Private->OfferNum].Dhcp4; + Offer = &Cache4->Packet.Offer; + + // + // Cache the content of DHCPv4 packet firstly. + // + Status = PxeBcCacheDhcp4Packet (Offer, RcvdOffer); + if (EFI_ERROR(Status)) { + return Status; + } + + // + // Validate the DHCPv4 packet, and parse the options and offer type. + // + if (EFI_ERROR (PxeBcParseDhcp4Packet (Cache4))) { + return EFI_ABORTED; + } + + // + // Determine whether cache the current offer by type, and record OfferIndex and OfferCount. + // + OfferType = Cache4->OfferType; + ASSERT (OfferType < PxeOfferTypeMax); + + if (OfferType == PxeOfferTypeBootp) { + // + // It's a Bootp offer, only cache the first one, and discard the others. + // + if (Private->OfferCount[OfferType] == 0) { + Private->OfferIndex[OfferType][0] = Private->OfferNum; + Private->OfferCount[OfferType] = 1; + } else { + return EFI_ABORTED; + } + } else { + ASSERT (Private->OfferCount[OfferType] < PXEBC_OFFER_MAX_NUM); + if (IS_PROXY_DHCP_OFFER (Offer)) { + // + // It's a proxy offer without yiaddr, including PXE10, WFM11a or BINL offer. + // + Private->IsProxyRecved = TRUE; + + if (OfferType == PxeOfferTypeProxyBinl) { + // + // Cache all proxy BINL offers. + // + Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum; + Private->OfferCount[OfferType]++; + } else if ((OfferType == PxeOfferTypeProxyPxe10 || OfferType == PxeOfferTypeProxyWfm11a) && + Private->OfferCount[OfferType] < 1) { + // + // Only cache the first PXE10/WFM11a offer, and discard the others. + // + Private->OfferIndex[OfferType][0] = Private->OfferNum; + Private->OfferCount[OfferType] = 1; + } else { + return EFI_ABORTED; + } + } else { + // + // It's a DHCPv4 offer with yiaddr, and cache them all. + // + Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum; + Private->OfferCount[OfferType]++; + } + } + + Private->OfferNum++; + + return EFI_SUCCESS; +} + + +/** + Select an DHCPv4 offer, and record SelectIndex and SelectProxyType. + + @param[in] Private Pointer to PxeBc private data. + +**/ +VOID +PxeBcSelectDhcp4Offer ( + IN PXEBC_PRIVATE_DATA *Private + ) +{ + UINT32 Index; + UINT32 OfferIndex; + EFI_DHCP4_PACKET *Offer; + + Private->SelectIndex = 0; + + if (Private->IsOfferSorted) { + // + // Select offer by default policy. + // + if (Private->OfferCount[PxeOfferTypeDhcpPxe10] > 0) { + // + // 1. DhcpPxe10 offer + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpPxe10][0] + 1; + + } else if (Private->OfferCount[PxeOfferTypeDhcpWfm11a] > 0) { + // + // 2. DhcpWfm11a offer + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpWfm11a][0] + 1; + + } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 && + Private->OfferCount[PxeOfferTypeProxyPxe10] > 0) { + // + // 3. DhcpOnly offer and ProxyPxe10 offer. + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1; + Private->SelectProxyType = PxeOfferTypeProxyPxe10; + + } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 && + Private->OfferCount[PxeOfferTypeProxyWfm11a] > 0) { + // + // 4. DhcpOnly offer and ProxyWfm11a offer. + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1; + Private->SelectProxyType = PxeOfferTypeProxyWfm11a; + + } else if (Private->OfferCount[PxeOfferTypeDhcpBinl] > 0) { + // + // 5. DhcpBinl offer. + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpBinl][0] + 1; + + } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 && + Private->OfferCount[PxeOfferTypeProxyBinl] > 0) { + // + // 6. DhcpOnly offer and ProxyBinl offer. + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1; + Private->SelectProxyType = PxeOfferTypeProxyBinl; + + } else { + // + // 7. DhcpOnly offer with bootfilename. + // + for (Index = 0; Index < Private->OfferCount[PxeOfferTypeDhcpOnly]; Index++) { + OfferIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][Index]; + if (Private->OfferBuffer[OfferIndex].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) { + Private->SelectIndex = OfferIndex + 1; + break; + } + } + // + // 8. Bootp offer with bootfilename. + // + OfferIndex = Private->OfferIndex[PxeOfferTypeBootp][0]; + if (Private->SelectIndex == 0 && + Private->OfferCount[PxeOfferTypeBootp] > 0 && + Private->OfferBuffer[OfferIndex].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) { + Private->SelectIndex = OfferIndex + 1; + } + } + } else { + // + // Select offer by received order. + // + for (Index = 0; Index < Private->OfferNum; Index++) { + + Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer; + + if (IS_PROXY_DHCP_OFFER (Offer)) { + // + // Skip proxy offers + // + continue; + } + + if (!Private->IsProxyRecved && + Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeDhcpOnly && + Private->OfferBuffer[Index].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) { + // + // Skip if DhcpOnly offer without any other proxy offers or bootfilename. + // + continue; + } + + // + // Record the index of the select offer. + // + Private->SelectIndex = Index + 1; + break; + } + } +} + + +/** + Handle the DHCPv4 offer packet. + + @param[in] Private Pointer to PxeBc private data. + + @retval EFI_SUCCESS Handled the DHCPv4 offer packet successfully. + @retval EFI_NO_RESPONSE No response to the following request packet. + @retval EFI_NOT_FOUND No boot filename received. + @retval EFI_BUFFER_TOO_SMALL Can't cache the offer pacet. + +**/ +EFI_STATUS +PxeBcHandleDhcp4Offer ( + IN PXEBC_PRIVATE_DATA *Private + ) +{ + PXEBC_DHCP4_PACKET_CACHE *Cache4; + EFI_DHCP4_PACKET_OPTION **Options; + UINT32 Index; + EFI_DHCP4_PACKET *Offer; + PXEBC_OFFER_TYPE OfferType; + UINT32 ProxyIndex; + UINT32 SelectIndex; + EFI_STATUS Status; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_DHCP4_PACKET *Ack; + + ASSERT (Private->SelectIndex > 0); + SelectIndex = (UINT32) (Private->SelectIndex - 1); + ASSERT (SelectIndex < PXEBC_OFFER_MAX_NUM); + Cache4 = &Private->OfferBuffer[SelectIndex].Dhcp4; + Options = Cache4->OptList; + Status = EFI_SUCCESS; + + if (Cache4->OfferType == PxeOfferTypeDhcpBinl) { + // + // DhcpBinl offer is selected, so need try to request bootfilename by this offer. + // + if (EFI_ERROR (PxeBcRetryBinlOffer (Private, SelectIndex))) { + Status = EFI_NO_RESPONSE; + } + } else if (Cache4->OfferType == PxeOfferTypeDhcpOnly) { + + if (Private->IsProxyRecved) { + // + // DhcpOnly offer is selected, so need try to request bootfile name. + // + ProxyIndex = 0; + if (Private->IsOfferSorted) { + // + // The proxy offer should be determined if select by default policy. + // IsOfferSorted means all offers are labeled by OfferIndex. + // + ASSERT (Private->SelectProxyType < PxeOfferTypeMax); + ASSERT (Private->OfferCount[Private->SelectProxyType] > 0); + + if (Private->SelectProxyType == PxeOfferTypeProxyBinl) { + // + // Try all the cached ProxyBinl offer one by one to request bootfile name. + // + for (Index = 0; Index < Private->OfferCount[Private->SelectProxyType]; Index++) { + ASSERT (Index < PXEBC_OFFER_MAX_NUM); + ProxyIndex = Private->OfferIndex[Private->SelectProxyType][Index]; + if (!EFI_ERROR (PxeBcRetryBinlOffer (Private, ProxyIndex))) { + break; + } + } + if (Index == Private->OfferCount[Private->SelectProxyType]) { + Status = EFI_NO_RESPONSE; + } + } else { + // + // For other proxy offers, only one is buffered. + // + ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0]; + } + } else { + // + // The proxy offer should not be determined if select by received order. + // + Status = EFI_NO_RESPONSE; + + for (Index = 0; Index < Private->OfferNum; Index++) { + ASSERT (Index < PXEBC_OFFER_MAX_NUM); + Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer; + OfferType = Private->OfferBuffer[Index].Dhcp4.OfferType; + if (!IS_PROXY_DHCP_OFFER (Offer)) { + // + // Skip non proxy DHCPv4 offers. + // + continue; + } + + if (OfferType == PxeOfferTypeProxyBinl) { + // + // Try all the cached ProxyBinl offer one by one to request bootfile name. + // + if (EFI_ERROR (PxeBcRetryBinlOffer (Private, Index))) { + continue; + } + } + + Private->SelectProxyType = OfferType; + ProxyIndex = Index; + Status = EFI_SUCCESS; + break; + } + } + + if (!EFI_ERROR (Status) && Private->SelectProxyType != PxeOfferTypeProxyBinl) { + // + // Success to try to request by a ProxyPxe10 or ProxyWfm11a offer, copy and parse it. + // + Status = PxeBcCopyProxyOffer (Private, ProxyIndex); + } + } else { + // + // Othewise, the bootfile name must be included in DhcpOnly offer. + // + if (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) { + Status = EFI_NOT_FOUND; + } + } + } + + if (!EFI_ERROR (Status)) { + // + // All PXE boot information is ready by now. + // + Mode = Private->PxeBc.Mode; + Offer = &Cache4->Packet.Offer; + Ack = &Private->DhcpAck.Dhcp4.Packet.Ack; + if (Cache4->OfferType == PxeOfferTypeBootp) { + // + // Bootp is a special case that only 2 packets involved instead of 4. So the bootp's reply + // should be taken as ack. + // + Ack = Offer; + } + + Status = PxeBcCopyDhcp4Ack (Private, Ack, TRUE); + if (EFI_ERROR (Status)) { + return Status; + } + Mode->DhcpDiscoverValid = TRUE; + } + + return Status; +} + + +/** + EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver + to intercept events that occurred in the configuration process. + + @param[in] This Pointer to the EFI DHCPv4 Protocol. + @param[in] Context Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure(). + @param[in] CurrentState The current operational state of the EFI DHCPv4 Protocol driver. + @param[in] Dhcp4Event The event that occurs in the current state, which usually means a + state transition. + @param[in] Packet The DHCPv4 packet that is going to be sent or already received. + @param[out] NewPacket The packet that is used to replace the above Packet. + + @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process. + @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol + driver will continue to wait for more DHCPOFFER packets until the + retry timeout expires. + @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process + and return to the Dhcp4Init or Dhcp4InitReboot state. + +**/ +EFI_STATUS +EFIAPI +PxeBcDhcp4CallBack ( + IN EFI_DHCP4_PROTOCOL *This, + IN VOID *Context, + IN EFI_DHCP4_STATE CurrentState, + IN EFI_DHCP4_EVENT Dhcp4Event, + IN EFI_DHCP4_PACKET *Packet OPTIONAL, + OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback; + EFI_DHCP4_PACKET_OPTION *MaxMsgSize; + UINT16 Value; + EFI_STATUS Status; + BOOLEAN Received; + + if ((Dhcp4Event != Dhcp4RcvdOffer) && + (Dhcp4Event != Dhcp4SelectOffer) && + (Dhcp4Event != Dhcp4SendDiscover) && + (Dhcp4Event != Dhcp4RcvdAck)) { + return EFI_SUCCESS; + } + + ASSERT (Packet != NULL); + + Private = (PXEBC_PRIVATE_DATA *) Context; + Mode = Private->PxeBc.Mode; + Callback = Private->PxeBcCallback; + + // + // Override the Maximum DHCP Message Size. + // + MaxMsgSize = PxeBcParseDhcp4Options ( + Packet->Dhcp4.Option, + GET_OPTION_BUFFER_LEN (Packet), + DHCP4_TAG_MAXMSG + ); + if (MaxMsgSize != NULL) { + Value = HTONS (PXEBC_DHCP4_PACKET_MAX_SIZE); + CopyMem (MaxMsgSize->Data, &Value, sizeof (Value)); + } + + // + // Callback to user if any packets sent or received. + // + if (Dhcp4Event != Dhcp4SelectOffer && Callback != NULL) { + Received = (BOOLEAN) (Dhcp4Event == Dhcp4RcvdOffer || Dhcp4Event == Dhcp4RcvdAck); + Status = Callback->Callback ( + Callback, + Private->Function, + Received, + Packet->Length, + (EFI_PXE_BASE_CODE_PACKET *) &Packet->Dhcp4 + ); + if (Status != EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE) { + return EFI_ABORTED; + } + } + + Status = EFI_SUCCESS; + + switch (Dhcp4Event) { + + case Dhcp4SendDiscover: + if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) { + // + // If the to be sent packet exceeds the maximum length, abort the DHCP process. + // + Status = EFI_ABORTED; + break; + } + + // + // Cache the DHCPv4 discover packet to mode data directly. + // It need to check SendGuid as well as Dhcp4SendRequest. + // + CopyMem (&Mode->DhcpDiscover.Dhcpv4, &Packet->Dhcp4, Packet->Length); + + case Dhcp4SendRequest: + if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) { + // + // If the to be sent packet exceeds the maximum length, abort the DHCP process. + // + Status = EFI_ABORTED; + break; + } + + if (Mode->SendGUID) { + // + // Send the system Guid instead of the MAC address as the hardware address if required. + // + if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Packet->Dhcp4.Header.ClientHwAddr))) { + // + // Zero the Guid to indicate NOT programable if failed to get system Guid. + // + DEBUG ((EFI_D_WARN, "PXE: Failed to read system GUID from the smbios table!\n")); + ZeroMem (Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID)); + } + Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID); + } + break; + + case Dhcp4RcvdOffer: + Status = EFI_NOT_READY; + if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) { + // + // Ignore the incoming packets which exceed the maximum length. + // + break; + } + if (Private->OfferNum < PXEBC_OFFER_MAX_NUM) { + // + // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record + // the OfferIndex and OfferCount. + // If error happens, just ignore this packet and continue to wait more offer. + // + PxeBcCacheDhcp4Offer (Private, Packet); + } + break; + + case Dhcp4SelectOffer: + ASSERT (NewPacket != NULL); + + // + // Select offer by the default policy or by order, and record the SelectIndex + // and SelectProxyType. + // + PxeBcSelectDhcp4Offer (Private); + + if (Private->SelectIndex == 0) { + Status = EFI_ABORTED; + } else { + *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer; + } + break; + + case Dhcp4RcvdAck: + // + // Cache the DHCPv4 ack to Private->Dhcp4Ack, but it's not the final ack in mode data + // without verification. + // + ASSERT (Private->SelectIndex != 0); + + Status = PxeBcCopyDhcp4Ack (Private, Packet, FALSE); + if (EFI_ERROR (Status)) { + Status = EFI_ABORTED; + } + break; + + default: + break; + } + + return Status; +} + + +/** + Build and send out the request packet for the bootfile, and parse the reply. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Type PxeBc option boot item type. + @param[in] Layer Pointer to option boot item layer. + @param[in] UseBis Use BIS or not. + @param[in] DestIp Pointer to the server address. + @param[in] IpCount The total count of the server address. + @param[in] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST. + + @retval EFI_SUCCESS Successfully discovered boot file. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource. + @retval EFI_NOT_FOUND Can't get the PXE reply packet. + @retval Others Failed to discover boot file. + +**/ +EFI_STATUS +PxeBcDhcp4Discover ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT16 Type, + IN UINT16 *Layer, + IN BOOLEAN UseBis, + IN EFI_IP_ADDRESS *DestIp, + IN UINT16 IpCount, + IN EFI_PXE_BASE_CODE_SRVLIST *SrvList + ) +{ + EFI_PXE_BASE_CODE_UDP_PORT Sport; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_DHCP4_PROTOCOL *Dhcp4; + EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token; + BOOLEAN IsBCast; + EFI_STATUS Status; + UINT16 RepIndex; + UINT16 SrvIndex; + UINT16 TryIndex; + EFI_DHCP4_LISTEN_POINT ListenPoint; + EFI_DHCP4_PACKET *Response; + UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE]; + EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM]; + UINT32 OptCount; + EFI_DHCP4_PACKET_OPTION *PxeOpt; + PXEBC_OPTION_BOOT_ITEM *PxeBootItem; + UINT8 VendorOptLen; + UINT32 Xid; + + Mode = Private->PxeBc.Mode; + Dhcp4 = Private->Dhcp4; + Status = EFI_SUCCESS; + + ZeroMem (&Token, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN)); + + // + // Use broadcast if destination address not specified. + // + if (DestIp == NULL) { + Sport = PXEBC_DHCP4_S_PORT; + IsBCast = TRUE; + } else { + Sport = PXEBC_BS_DISCOVER_PORT; + IsBCast = FALSE; + } + + if (!UseBis && Layer != NULL) { + *Layer &= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK; + } + + // + // Build all the options for the request packet. + // + OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, TRUE); + + if (Private->IsDoDiscover) { + // + // Add vendor option of PXE_BOOT_ITEM + // + VendorOptLen = (UINT8) ((sizeof (EFI_DHCP4_PACKET_OPTION) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM) + 1); + OptList[OptCount] = AllocateZeroPool (VendorOptLen); + if (OptList[OptCount] == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + OptList[OptCount]->OpCode = DHCP4_TAG_VENDOR; + OptList[OptCount]->Length = (UINT8) (VendorOptLen - 2); + PxeOpt = (EFI_DHCP4_PACKET_OPTION *) OptList[OptCount]->Data; + PxeOpt->OpCode = PXEBC_VENDOR_TAG_BOOT_ITEM; + PxeOpt->Length = (UINT8) sizeof (PXEBC_OPTION_BOOT_ITEM); + PxeBootItem = (PXEBC_OPTION_BOOT_ITEM *) PxeOpt->Data; + PxeBootItem->Type = HTONS (Type); + PxeOpt->Data[PxeOpt->Length] = DHCP4_TAG_EOP; + + if (Layer != NULL) { + PxeBootItem->Layer = HTONS (*Layer); + } + + OptCount++; + } + + // + // Build the request packet with seed packet and option list. + // + Status = Dhcp4->Build ( + Dhcp4, + &Private->SeedPacket, + 0, + NULL, + OptCount, + OptList, + &Token.Packet + ); + // + // Free the vendor option of PXE_BOOT_ITEM. + // + if (Private->IsDoDiscover) { + FreePool (OptList[OptCount - 1]); + } + + if (EFI_ERROR (Status)) { + return Status; + } + + if (Mode->SendGUID) { + if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Token.Packet->Dhcp4.Header.ClientHwAddr))) { + // + // Zero the Guid to indicate NOT programable if failed to get system Guid. + // + DEBUG ((EFI_D_WARN, "PXE: Failed to read system GUID from the smbios table!\n")); + ZeroMem (Token.Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID)); + } + Token.Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID); + } + + // + // Set fields of the token for the request packet. + // + Xid = NET_RANDOM (NetRandomInitSeed ()); + Token.Packet->Dhcp4.Header.Xid = HTONL (Xid); + Token.Packet->Dhcp4.Header.Reserved = HTONS ((UINT16) ((IsBCast) ? 0x8000 : 0x0)); + CopyMem (&Token.Packet->Dhcp4.Header.ClientAddr, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS)); + + Token.RemotePort = Sport; + + if (IsBCast) { + SetMem (&Token.RemoteAddress, sizeof (EFI_IPv4_ADDRESS), 0xff); + } else { + CopyMem (&Token.RemoteAddress, DestIp, sizeof (EFI_IPv4_ADDRESS)); + } + + CopyMem (&Token.GatewayAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS)); + + if (!IsBCast) { + Token.ListenPointCount = 1; + Token.ListenPoints = &ListenPoint; + Token.ListenPoints[0].ListenPort = PXEBC_BS_DISCOVER_PORT; + CopyMem (&Token.ListenPoints[0].ListenAddress, &Private->StationIp, sizeof(EFI_IPv4_ADDRESS)); + CopyMem (&Token.ListenPoints[0].SubnetMask, &Private->SubnetMask, sizeof(EFI_IPv4_ADDRESS)); + } + + // + // Send out the request packet to discover the bootfile. + // + for (TryIndex = 1; TryIndex <= PXEBC_BOOT_REQUEST_RETRIES; TryIndex++) { + + Token.TimeoutValue = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * TryIndex); + Token.Packet->Dhcp4.Header.Seconds = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * (TryIndex - 1)); + + Status = Dhcp4->TransmitReceive (Dhcp4, &Token); + if (Token.Status != EFI_TIMEOUT) { + break; + } + } + + if (TryIndex > PXEBC_BOOT_REQUEST_RETRIES) { + // + // No server response our PXE request + // + Status = EFI_TIMEOUT; + } + + if (!EFI_ERROR (Status)) { + + RepIndex = 0; + SrvIndex = 0; + Response = Token.ResponseList; + // + // Find the right PXE Reply according to server address. + // + while (RepIndex < Token.ResponseCount) { + if (Response->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) { + SrvIndex = 0; + RepIndex++; + Response = (EFI_DHCP4_PACKET *) ((UINT8 *) Response + Response->Size); + continue; + } + + while (SrvIndex < IpCount) { + if (SrvList[SrvIndex].AcceptAnyResponse) { + break; + } + if ((SrvList[SrvIndex].Type == Type) && + EFI_IP4_EQUAL (&Response->Dhcp4.Header.ServerAddr, &SrvList[SrvIndex].IpAddr)) { + break; + } + SrvIndex++; + } + + if ((IpCount != SrvIndex) || (IpCount == 0)) { + break; + } + + SrvIndex = 0; + RepIndex++; + Response = (EFI_DHCP4_PACKET *) ((UINT8 *) Response + Response->Size); + } + + if (RepIndex < Token.ResponseCount) { + // + // Cache the right PXE reply packet here, set valid flag later. + // Especially for PXE discover packet, store it into mode data here. + // + if (Private->IsDoDiscover) { + Status = PxeBcCacheDhcp4Packet (&Private->PxeReply.Dhcp4.Packet.Ack, Response); + if (EFI_ERROR(Status)) { + goto ON_EXIT; + } + CopyMem (&Mode->PxeDiscover, &Token.Packet->Dhcp4, Token.Packet->Length); + } else { + Status = PxeBcCacheDhcp4Packet (&Private->ProxyOffer.Dhcp4.Packet.Offer, Response); + if (EFI_ERROR(Status)) { + goto ON_EXIT; + } + } + } else { + // + // Not found the right PXE reply packet. + // + Status = EFI_NOT_FOUND; + } + } +ON_EXIT: + + if (Token.ResponseList != NULL) { + FreePool (Token.ResponseList); + } + if (Token.Packet != NULL) { + FreePool (Token.Packet); + } + return Status; +} + +/** + Switch the Ip4 policy to static. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + + @retval EFI_SUCCESS The policy is already configured to static. + @retval Others Other error as indicated.. + +**/ +EFI_STATUS +PxeBcSetIp4Policy ( + IN PXEBC_PRIVATE_DATA *Private + ) +{ + EFI_STATUS Status; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + EFI_IP4_CONFIG2_POLICY Policy; + UINTN DataSize; + + Ip4Config2 = Private->Ip4Config2; + DataSize = sizeof (EFI_IP4_CONFIG2_POLICY); + Status = Ip4Config2->GetData ( + Ip4Config2, + Ip4Config2DataTypePolicy, + &DataSize, + &Policy + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Policy != Ip4Config2PolicyStatic) { + Policy = Ip4Config2PolicyStatic; + Status= Ip4Config2->SetData ( + Ip4Config2, + Ip4Config2DataTypePolicy, + sizeof (EFI_IP4_CONFIG2_POLICY), + &Policy + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + return EFI_SUCCESS; +} + +/** + Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other PXE boot information. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Dhcp4 Pointer to the EFI_DHCP4_PROTOCOL + + @retval EFI_SUCCESS The D.O.R.A process successfully finished. + @retval Others Failed to finish the D.O.R.A process. + +**/ +EFI_STATUS +PxeBcDhcp4Dora ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_DHCP4_PROTOCOL *Dhcp4 + ) +{ + EFI_PXE_BASE_CODE_MODE *PxeMode; + EFI_DHCP4_CONFIG_DATA Config; + EFI_DHCP4_MODE_DATA Mode; + EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM]; + UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE]; + UINT32 OptCount; + EFI_STATUS Status; + + ASSERT (Dhcp4 != NULL); + + Status = EFI_SUCCESS; + PxeMode = Private->PxeBc.Mode; + + // + // Build option list for the request packet. + // + OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, FALSE); + ASSERT (OptCount> 0); + + ZeroMem (&Mode, sizeof (EFI_DHCP4_MODE_DATA)); + ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA)); + + Config.OptionCount = OptCount; + Config.OptionList = OptList; + Config.Dhcp4Callback = PxeBcDhcp4CallBack; + Config.CallbackContext = Private; + Config.DiscoverTryCount = PXEBC_DHCP_RETRIES; + Config.DiscoverTimeout = mPxeDhcpTimeout; + + // + // Configure the DHCPv4 instance for PXE boot. + // + Status = Dhcp4->Configure (Dhcp4, &Config); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Initialize the record fields for DHCPv4 offer in private data. + // + Private->IsProxyRecved = FALSE; + Private->OfferNum = 0; + ZeroMem (Private->OfferCount, sizeof (Private->OfferCount)); + ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex)); + + Status = Dhcp4->Start (Dhcp4, NULL); + if (EFI_ERROR (Status)) { + if (Status == EFI_ICMP_ERROR) { + PxeMode->IcmpErrorReceived = TRUE; + } + + if (Status == EFI_TIMEOUT && Private->OfferNum > 0) { + Status = EFI_NO_RESPONSE; + } + + goto ON_EXIT; + } + + // + // Get the acquired IPv4 address and store them. + // + Status = Dhcp4->GetModeData (Dhcp4, &Mode); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + ASSERT (Mode.State == Dhcp4Bound); + + CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&PxeMode->StationIp, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&PxeMode->SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + + Status = PxeBcFlushStationIp (Private, &Private->StationIp, &Private->SubnetMask); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Check the selected offer whether BINL retry is needed. + // + Status = PxeBcHandleDhcp4Offer (Private); + + AsciiPrint ("\n Station IP address is "); + + PxeBcShowIp4Addr (&Private->StationIp.v4); + AsciiPrint ("\n"); + +ON_EXIT: + if (EFI_ERROR (Status)) { + Dhcp4->Stop (Dhcp4); + Dhcp4->Configure (Dhcp4, NULL); + } else { + ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA)); + Dhcp4->Configure (Dhcp4, &Config); + Private->IsAddressOk = TRUE; + } + + return Status; +} diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.h b/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.h new file mode 100644 index 000000000..69b0502bb --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.h @@ -0,0 +1,365 @@ +/** @file + Functions declaration related with DHCPv4 for UefiPxeBc Driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_PXEBC_DHCP4_H__ +#define __EFI_PXEBC_DHCP4_H__ + +#define PXEBC_DHCP4_OPTION_MAX_NUM 16 +#define PXEBC_DHCP4_OPTION_MAX_SIZE 312 +#define PXEBC_DHCP4_PACKET_MAX_SIZE (sizeof (EFI_PXE_BASE_CODE_PACKET)) +#define PXEBC_DHCP4_S_PORT 67 +#define PXEBC_DHCP4_C_PORT 68 +#define PXEBC_BS_DOWNLOAD_PORT 69 +#define PXEBC_BS_DISCOVER_PORT 4011 +#define PXEBC_DHCP4_OPCODE_REQUEST 1 +#define PXEBC_DHCP4_OPCODE_REPLY 2 +#define PXEBC_DHCP4_MSG_TYPE_REQUEST 3 +#define PXEBC_DHCP4_MAGIC 0x63538263 // network byte order + +// +// Sub-Options in Dhcp Vendor Option +// +#define PXEBC_VENDOR_TAG_MTFTP_IP 1 +#define PXEBC_VENDOR_TAG_MTFTP_CPORT 2 +#define PXEBC_VENDOR_TAG_MTFTP_SPORT 3 +#define PXEBC_VENDOR_TAG_MTFTP_TIMEOUT 4 +#define PXEBC_VENDOR_TAG_MTFTP_DELAY 5 +#define PXEBC_VENDOR_TAG_DISCOVER_CTRL 6 +#define PXEBC_VENDOR_TAG_DISCOVER_MCAST 7 +#define PXEBC_VENDOR_TAG_BOOT_SERVERS 8 +#define PXEBC_VENDOR_TAG_BOOT_MENU 9 +#define PXEBC_VENDOR_TAG_MENU_PROMPT 10 +#define PXEBC_VENDOR_TAG_MCAST_ALLOC 11 +#define PXEBC_VENDOR_TAG_CREDENTIAL_TYPES 12 +#define PXEBC_VENDOR_TAG_BOOT_ITEM 71 + +#define PXEBC_BOOT_REQUEST_TIMEOUT 1 +#define PXEBC_BOOT_REQUEST_RETRIES 4 + +#define PXEBC_DHCP4_OVERLOAD_FILE 1 +#define PXEBC_DHCP4_OVERLOAD_SERVER_NAME 2 + + +// +// The array index of the DHCP4 option tag interested +// +#define PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN 0 +#define PXEBC_DHCP4_TAG_INDEX_VENDOR 1 +#define PXEBC_DHCP4_TAG_INDEX_OVERLOAD 2 +#define PXEBC_DHCP4_TAG_INDEX_MSG_TYPE 3 +#define PXEBC_DHCP4_TAG_INDEX_SERVER_ID 4 +#define PXEBC_DHCP4_TAG_INDEX_CLASS_ID 5 +#define PXEBC_DHCP4_TAG_INDEX_BOOTFILE 6 +#define PXEBC_DHCP4_TAG_INDEX_MAX 7 + +// +// Dhcp4 and Dhcp6 share this definition, and corresponding +// relatioinship is as follows: +// +// Dhcp4Discover <> Dhcp6Solicit +// Dhcp4Offer <> Dhcp6Advertise +// Dhcp4Request <> Dhcp6Request +// Dhcp4Ack <> DHcp6Reply +// +typedef enum { + PxeOfferTypeDhcpOnly, + PxeOfferTypeDhcpPxe10, + PxeOfferTypeDhcpWfm11a, + PxeOfferTypeDhcpBinl, + PxeOfferTypeProxyPxe10, + PxeOfferTypeProxyWfm11a, + PxeOfferTypeProxyBinl, + PxeOfferTypeBootp, + PxeOfferTypeMax +} PXEBC_OFFER_TYPE; + +#define BIT(x) (1 << x) +#define CTRL(x) (0x1F & (x)) +#define DEFAULT_CLASS_ID_DATA "PXEClient:Arch:xxxxx:UNDI:003000" +#define DEFAULT_UNDI_TYPE 1 +#define DEFAULT_UNDI_MAJOR 3 +#define DEFAULT_UNDI_MINOR 0 + +#define MTFTP_VENDOR_OPTION_BIT_MAP \ + (BIT (PXEBC_VENDOR_TAG_MTFTP_IP) | \ + BIT (PXEBC_VENDOR_TAG_MTFTP_CPORT) | \ + BIT (PXEBC_VENDOR_TAG_MTFTP_SPORT) | \ + BIT (PXEBC_VENDOR_TAG_MTFTP_TIMEOUT) | \ + BIT (PXEBC_VENDOR_TAG_MTFTP_DELAY)) + +#define DISCOVER_VENDOR_OPTION_BIT_MAP \ + (BIT (PXEBC_VENDOR_TAG_DISCOVER_CTRL) | \ + BIT (PXEBC_VENDOR_TAG_DISCOVER_MCAST) | \ + BIT (PXEBC_VENDOR_TAG_BOOT_SERVERS) | \ + BIT (PXEBC_VENDOR_TAG_BOOT_MENU) | \ + BIT (PXEBC_VENDOR_TAG_MENU_PROMPT)) + +#define IS_VALID_BOOT_SERVERS(x) \ + ((((x)[0]) & BIT (PXEBC_VENDOR_TAG_BOOT_SERVERS)) \ + == BIT (PXEBC_VENDOR_TAG_BOOT_SERVERS)) + +#define IS_VALID_BOOT_PROMPT(x) \ + ((((x)[0]) & BIT (PXEBC_VENDOR_TAG_MENU_PROMPT)) \ + == BIT (PXEBC_VENDOR_TAG_MENU_PROMPT)) + +#define IS_VALID_BOOT_MENU(x) \ + ((((x)[0]) & BIT (PXEBC_VENDOR_TAG_BOOT_MENU)) \ + == BIT (PXEBC_VENDOR_TAG_BOOT_MENU)) + +#define IS_VALID_MTFTP_VENDOR_OPTION(x) \ + (((UINT32) ((x)[0]) & MTFTP_VENDOR_OPTION_BIT_MAP) \ + == MTFTP_VENDOR_OPTION_BIT_MAP) + +#define IS_VALID_DISCOVER_VENDOR_OPTION(x) \ + (((UINT32) ((x)[0]) & DISCOVER_VENDOR_OPTION_BIT_MAP) != 0) + +#define IS_VALID_CREDENTIAL_VENDOR_OPTION(x) \ + (((UINT32) ((x)[0]) & BIT (PXEBC_VENDOR_TAG_CREDENTIAL_TYPES)) \ + == BIT (PXEBC_VENDOR_TAG_CREDENTIAL_TYPES)) + +#define IS_VALID_BOOTITEM_VENDOR_OPTION(x) \ + (((UINT32) ((x)[PXEBC_VENDOR_TAG_BOOT_ITEM / 32]) & \ + BIT (PXEBC_VENDOR_TAG_BOOT_ITEM % 32)) \ + == BIT (PXEBC_VENDOR_TAG_BOOT_ITEM % 32)) + +#define SET_VENDOR_OPTION_BIT_MAP(x, y) \ + (*(x + ((y) / 32)) = (UINT32) ((UINT32) ((x)[(y) / 32]) | BIT ((y) % 32))) + +#define GET_NEXT_DHCP_OPTION(Opt) \ + (EFI_DHCP4_PACKET_OPTION *) ((UINT8 *) (Opt) + \ + sizeof (EFI_DHCP4_PACKET_OPTION) + (Opt)->Length - 1) + +#define GET_OPTION_BUFFER_LEN(Pkt) \ + ((Pkt)->Length - sizeof (EFI_DHCP4_HEADER) - 4) + +#define GET_NEXT_BOOT_SVR_ENTRY(Ent) \ + (PXEBC_BOOT_SVR_ENTRY *) ((UINT8 *) Ent + sizeof (*(Ent)) + \ + ((Ent)->IpCnt - 1) * sizeof (EFI_IPv4_ADDRESS)) + +#define IS_PROXY_DHCP_OFFER(Offer) \ + EFI_IP4_EQUAL (&(Offer)->Dhcp4.Header.YourAddr, &mZeroIp4Addr) + +#define IS_DISABLE_BCAST_DISCOVER(x) \ + (((x) & BIT (0)) == BIT (0)) + +#define IS_DISABLE_MCAST_DISCOVER(x) \ + (((x) & BIT (1)) == BIT (1)) + +#define IS_ENABLE_USE_SERVER_LIST(x) \ + (((x) & BIT (2)) == BIT (2)) + +#define IS_DISABLE_PROMPT_MENU(x) \ + (((x) & BIT (3)) == BIT (3)) + + +#pragma pack(1) +typedef struct { + UINT8 ParaList[135]; +} PXEBC_DHCP4_OPTION_PARA; + +typedef struct { + UINT16 Size; +} PXEBC_DHCP4_OPTION_MAX_MESG_SIZE; + +typedef struct { + UINT8 Type; + UINT8 MajorVer; + UINT8 MinorVer; +} PXEBC_DHCP4_OPTION_UNDI; + +typedef struct { + UINT8 Type; +} PXEBC_DHCP4_OPTION_MESG; + +typedef struct { + UINT16 Type; +} PXEBC_DHCP4_OPTION_ARCH; + +typedef struct { + UINT8 ClassIdentifier[10]; + UINT8 ArchitecturePrefix[5]; + UINT8 ArchitectureType[5]; + UINT8 Lit3[1]; + UINT8 InterfaceName[4]; + UINT8 Lit4[1]; + UINT8 UndiMajor[3]; + UINT8 UndiMinor[3]; +} PXEBC_DHCP4_OPTION_CLID; + +typedef struct { + UINT8 Type; + UINT8 Guid[16]; +} PXEBC_DHCP4_OPTION_UUID; + +typedef struct { + UINT16 Type; + UINT16 Layer; +} PXEBC_OPTION_BOOT_ITEM; + +#pragma pack() + +typedef union { + PXEBC_DHCP4_OPTION_PARA *Para; + PXEBC_DHCP4_OPTION_UNDI *Undi; + PXEBC_DHCP4_OPTION_ARCH *Arch; + PXEBC_DHCP4_OPTION_CLID *Clid; + PXEBC_DHCP4_OPTION_UUID *Uuid; + PXEBC_DHCP4_OPTION_MESG *Mesg; + PXEBC_DHCP4_OPTION_MAX_MESG_SIZE *MaxMesgSize; +} PXEBC_DHCP4_OPTION_ENTRY; + +#pragma pack(1) +typedef struct { + UINT16 Type; + UINT8 IpCnt; + EFI_IPv4_ADDRESS IpAddr[1]; +} PXEBC_BOOT_SVR_ENTRY; + +typedef struct { + UINT16 Type; + UINT8 DescLen; + UINT8 DescStr[1]; +} PXEBC_BOOT_MENU_ENTRY; + +typedef struct { + UINT8 Timeout; + UINT8 Prompt[1]; +} PXEBC_MENU_PROMPT; +#pragma pack() + +typedef struct { + UINT32 BitMap[8]; + EFI_IPv4_ADDRESS MtftpIp; + UINT16 MtftpCPort; + UINT16 MtftpSPort; + UINT8 MtftpTimeout; + UINT8 MtftpDelay; + UINT8 DiscoverCtrl; + EFI_IPv4_ADDRESS DiscoverMcastIp; + EFI_IPv4_ADDRESS McastIpBase; + UINT16 McastIpBlock; + UINT16 McastIpRange; + UINT16 BootSrvType; + UINT16 BootSrvLayer; + PXEBC_BOOT_SVR_ENTRY *BootSvr; + UINT8 BootSvrLen; + PXEBC_BOOT_MENU_ENTRY *BootMenu; + UINT8 BootMenuLen; + PXEBC_MENU_PROMPT *MenuPrompt; + UINT8 MenuPromptLen; + UINT32 *CredType; + UINT8 CredTypeLen; +} PXEBC_VENDOR_OPTION; + +#define PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE (OFFSET_OF (EFI_DHCP4_PACKET, Dhcp4) + PXEBC_DHCP4_PACKET_MAX_SIZE) + +typedef union { + EFI_DHCP4_PACKET Offer; + EFI_DHCP4_PACKET Ack; + UINT8 Buffer[PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE]; +} PXEBC_DHCP4_PACKET; + +typedef struct { + PXEBC_DHCP4_PACKET Packet; + PXEBC_OFFER_TYPE OfferType; + EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_TAG_INDEX_MAX]; + PXEBC_VENDOR_OPTION VendorOpt; +} PXEBC_DHCP4_PACKET_CACHE; + + +/** + Create a template DHCPv4 packet as a seed. + + @param[out] Seed Pointer to the seed packet. + @param[in] Udp4 Pointer to EFI_UDP4_PROTOCOL. + +**/ +VOID +PxeBcSeedDhcp4Packet ( + OUT EFI_DHCP4_PACKET *Seed, + IN EFI_UDP4_PROTOCOL *Udp4 + ); + + +/** + Parse the cached DHCPv4 packet, including all the options. + + @param[in] Cache4 Pointer to cached DHCPv4 packet. + + @retval EFI_SUCCESS Parsed the DHCPv4 packet successfully. + @retval EFI_DEVICE_ERROR Failed to parse and invalid packet. + +**/ +EFI_STATUS +PxeBcParseDhcp4Packet ( + IN PXEBC_DHCP4_PACKET_CACHE *Cache4 + ); + + +/** + Build and send out the request packet for the bootfile, and parse the reply. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Type PxeBc option boot item type. + @param[in] Layer Pointer to option boot item layer. + @param[in] UseBis Use BIS or not. + @param[in] DestIp Pointer to the server address. + @param[in] IpCount The total count of the server address. + @param[in] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST. + + @retval EFI_SUCCESS Successfully discovered boot file. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource. + @retval EFI_NOT_FOUND Can't get the PXE reply packet. + @retval Others Failed to discover boot file. + +**/ +EFI_STATUS +PxeBcDhcp4Discover ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT16 Type, + IN UINT16 *Layer, + IN BOOLEAN UseBis, + IN EFI_IP_ADDRESS *DestIp, + IN UINT16 IpCount, + IN EFI_PXE_BASE_CODE_SRVLIST *SrvList + ); + +/** + Switch the Ip4 policy to static. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + + @retval EFI_SUCCESS The policy is already configured to static. + @retval Others Other error as indicated.. + +**/ +EFI_STATUS +PxeBcSetIp4Policy ( + IN PXEBC_PRIVATE_DATA *Private + ); + + +/** + Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other PXE boot information. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Dhcp4 Pointer to the EFI_DHCP4_PROTOCOL + + @retval EFI_SUCCESS The D.O.R.A process successfully finished. + @retval Others Failed to finish the D.O.R.A process. + +**/ +EFI_STATUS +PxeBcDhcp4Dora ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_DHCP4_PROTOCOL *Dhcp4 + ); + +#endif + diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.c b/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.c new file mode 100644 index 000000000..1164fbbde --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.c @@ -0,0 +1,2370 @@ +/** @file + Functions implementation related with DHCPv6 for UefiPxeBc Driver. + + (C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "PxeBcImpl.h" + +// +// Well-known multi-cast address defined in section-24.1 of rfc-3315 +// +// ALL_DHCP_Relay_Agents_and_Servers address: FF02::1:2 +// +EFI_IPv6_ADDRESS mAllDhcpRelayAndServersAddress = {{0xFF, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2}}; + +/** + Parse out a DHCPv6 option by OptTag, and find the position in buffer. + + @param[in] Buffer The pointer to the option buffer. + @param[in] Length Length of the option buffer. + @param[in] OptTag The required option tag. + + @retval NULL Failed to parse the required option. + @retval Others The postion of the required option in buffer. + +**/ +EFI_DHCP6_PACKET_OPTION * +PxeBcParseDhcp6Options ( + IN UINT8 *Buffer, + IN UINT32 Length, + IN UINT16 OptTag + ) +{ + EFI_DHCP6_PACKET_OPTION *Option; + UINT32 Offset; + + Option = (EFI_DHCP6_PACKET_OPTION *) Buffer; + Offset = 0; + + // + // OpLen and OpCode here are both stored in network order. + // + while (Offset < Length) { + + if (NTOHS (Option->OpCode) == OptTag) { + + return Option; + } + + Offset += (NTOHS(Option->OpLen) + 4); + Option = (EFI_DHCP6_PACKET_OPTION *) (Buffer + Offset); + } + + return NULL; +} + + +/** + Build the options buffer for the DHCPv6 request packet. + + @param[in] Private The pointer to PxeBc private data. + @param[out] OptList The pointer to the option pointer array. + @param[in] Buffer The pointer to the buffer to contain the option list. + + @return Index The count of the built-in options. + +**/ +UINT32 +PxeBcBuildDhcp6Options ( + IN PXEBC_PRIVATE_DATA *Private, + OUT EFI_DHCP6_PACKET_OPTION **OptList, + IN UINT8 *Buffer + ) +{ + PXEBC_DHCP6_OPTION_ENTRY OptEnt; + UINT32 Index; + UINT16 Value; + + Index = 0; + OptList[0] = (EFI_DHCP6_PACKET_OPTION *) Buffer; + + // + // Append client option request option + // + OptList[Index]->OpCode = HTONS (DHCP6_OPT_ORO); + OptList[Index]->OpLen = HTONS (8); + OptEnt.Oro = (PXEBC_DHCP6_OPTION_ORO *) OptList[Index]->Data; + OptEnt.Oro->OpCode[0] = HTONS(DHCP6_OPT_BOOT_FILE_URL); + OptEnt.Oro->OpCode[1] = HTONS(DHCP6_OPT_BOOT_FILE_PARAM); + OptEnt.Oro->OpCode[2] = HTONS(DHCP6_OPT_DNS_SERVERS); + OptEnt.Oro->OpCode[3] = HTONS(DHCP6_OPT_VENDOR_CLASS); + Index++; + OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]); + + // + // Append client network device interface option + // + OptList[Index]->OpCode = HTONS (DHCP6_OPT_UNDI); + OptList[Index]->OpLen = HTONS ((UINT16)3); + OptEnt.Undi = (PXEBC_DHCP6_OPTION_UNDI *) OptList[Index]->Data; + + if (Private->Nii != NULL) { + OptEnt.Undi->Type = Private->Nii->Type; + OptEnt.Undi->MajorVer = Private->Nii->MajorVer; + OptEnt.Undi->MinorVer = Private->Nii->MinorVer; + } else { + OptEnt.Undi->Type = DEFAULT_UNDI_TYPE; + OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR; + OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR; + } + + Index++; + OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]); + + // + // Append client system architecture option + // + OptList[Index]->OpCode = HTONS (DHCP6_OPT_ARCH); + OptList[Index]->OpLen = HTONS ((UINT16) sizeof (PXEBC_DHCP6_OPTION_ARCH)); + OptEnt.Arch = (PXEBC_DHCP6_OPTION_ARCH *) OptList[Index]->Data; + Value = HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE); + CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16)); + Index++; + OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]); + + // + // Append vendor class option to store the PXE class identifier. + // + OptList[Index]->OpCode = HTONS (DHCP6_OPT_VENDOR_CLASS); + OptList[Index]->OpLen = HTONS ((UINT16) sizeof (PXEBC_DHCP6_OPTION_VENDOR_CLASS)); + OptEnt.VendorClass = (PXEBC_DHCP6_OPTION_VENDOR_CLASS *) OptList[Index]->Data; + OptEnt.VendorClass->Vendor = HTONL (PXEBC_DHCP6_ENTERPRISE_NUM); + OptEnt.VendorClass->ClassLen = HTONS ((UINT16) sizeof (PXEBC_CLASS_ID)); + CopyMem ( + &OptEnt.VendorClass->ClassId, + DEFAULT_CLASS_ID_DATA, + sizeof (PXEBC_CLASS_ID) + ); + PxeBcUintnToAscDecWithFormat ( + EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE, + OptEnt.VendorClass->ClassId.ArchitectureType, + sizeof (OptEnt.VendorClass->ClassId.ArchitectureType) + ); + + if (Private->Nii != NULL) { + CopyMem ( + OptEnt.VendorClass->ClassId.InterfaceName, + Private->Nii->StringId, + sizeof (OptEnt.VendorClass->ClassId.InterfaceName) + ); + PxeBcUintnToAscDecWithFormat ( + Private->Nii->MajorVer, + OptEnt.VendorClass->ClassId.UndiMajor, + sizeof (OptEnt.VendorClass->ClassId.UndiMajor) + ); + PxeBcUintnToAscDecWithFormat ( + Private->Nii->MinorVer, + OptEnt.VendorClass->ClassId.UndiMinor, + sizeof (OptEnt.VendorClass->ClassId.UndiMinor) + ); + } + + Index++; + + return Index; +} + + +/** + Cache the DHCPv6 packet. + + @param[in] Dst The pointer to the cache buffer for DHCPv6 packet. + @param[in] Src The pointer to the DHCPv6 packet to be cached. + + @retval EFI_SUCCESS Packet is copied. + @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet. + +**/ +EFI_STATUS +PxeBcCacheDhcp6Packet ( + IN EFI_DHCP6_PACKET *Dst, + IN EFI_DHCP6_PACKET *Src + ) +{ + if (Dst->Size < Src->Length) { + return EFI_BUFFER_TOO_SMALL; + } + + CopyMem (&Dst->Dhcp6, &Src->Dhcp6, Src->Length); + Dst->Length = Src->Length; + + return EFI_SUCCESS; +} + +/** + Retrieve the boot server address using the EFI_DNS6_PROTOCOL. + + @param[in] Private Pointer to PxeBc private data. + @param[in] HostName Pointer to buffer containing hostname. + @param[out] IpAddress On output, pointer to buffer containing IPv6 address. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources. + @retval EFI_DEVICE_ERROR An unexpected network error occurred. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +PxeBcDns6 ( + IN PXEBC_PRIVATE_DATA *Private, + IN CHAR16 *HostName, + OUT EFI_IPv6_ADDRESS *IpAddress + ) +{ + EFI_STATUS Status; + EFI_DNS6_PROTOCOL *Dns6; + EFI_DNS6_CONFIG_DATA Dns6ConfigData; + EFI_DNS6_COMPLETION_TOKEN Token; + EFI_HANDLE Dns6Handle; + EFI_IPv6_ADDRESS *DnsServerList; + BOOLEAN IsDone; + + Dns6 = NULL; + Dns6Handle = NULL; + DnsServerList = Private->DnsServer; + ZeroMem (&Token, sizeof (EFI_DNS6_COMPLETION_TOKEN)); + + // + // Create a DNSv6 child instance and get the protocol. + // + Status = NetLibCreateServiceChild ( + Private->Controller, + Private->Image, + &gEfiDns6ServiceBindingProtocolGuid, + &Dns6Handle + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Status = gBS->OpenProtocol ( + Dns6Handle, + &gEfiDns6ProtocolGuid, + (VOID **) &Dns6, + Private->Image, + Private->Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Configure DNS6 instance for the DNS server address and protocol. + // + ZeroMem (&Dns6ConfigData, sizeof (EFI_DNS6_CONFIG_DATA)); + Dns6ConfigData.DnsServerCount = 1; + Dns6ConfigData.DnsServerList = DnsServerList; + Dns6ConfigData.EnableDnsCache = TRUE; + Dns6ConfigData.Protocol = EFI_IP_PROTO_UDP; + IP6_COPY_ADDRESS (&Dns6ConfigData.StationIp, &Private->TmpStationIp.v6); + Status = Dns6->Configure ( + Dns6, + &Dns6ConfigData + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Token.Status = EFI_NOT_READY; + IsDone = FALSE; + // + // Create event to set the IsDone flag when name resolution is finished. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + PxeBcCommonNotify, + &IsDone, + &Token.Event + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Start asynchronous name resolution. + // + Status = Dns6->HostNameToIp (Dns6, HostName, &Token); + if (EFI_ERROR (Status)) { + goto Exit; + } + + while (!IsDone) { + Dns6->Poll (Dns6); + } + + // + // Name resolution is done, check result. + // + Status = Token.Status; + if (!EFI_ERROR (Status)) { + if (Token.RspData.H2AData == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + if (Token.RspData.H2AData->IpCount == 0 || Token.RspData.H2AData->IpList == NULL) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + // + // We just return the first IPv6 address from DNS protocol. + // + IP6_COPY_ADDRESS (IpAddress, Token.RspData.H2AData->IpList); + Status = EFI_SUCCESS; + } + +Exit: + FreePool (HostName); + + if (Token.Event != NULL) { + gBS->CloseEvent (Token.Event); + } + if (Token.RspData.H2AData != NULL) { + if (Token.RspData.H2AData->IpList != NULL) { + FreePool (Token.RspData.H2AData->IpList); + } + FreePool (Token.RspData.H2AData); + } + + if (Dns6 != NULL) { + Dns6->Configure (Dns6, NULL); + + gBS->CloseProtocol ( + Dns6Handle, + &gEfiDns6ProtocolGuid, + Private->Image, + Private->Controller + ); + } + + if (Dns6Handle != NULL) { + NetLibDestroyServiceChild ( + Private->Controller, + Private->Image, + &gEfiDns6ServiceBindingProtocolGuid, + Dns6Handle + ); + } + + if (DnsServerList != NULL) { + FreePool (DnsServerList); + } + + return Status; +} + +/** + Parse the Boot File URL option. + + @param[in] Private Pointer to PxeBc private data. + @param[out] FileName The pointer to the boot file name. + @param[in, out] SrvAddr The pointer to the boot server address. + @param[in] BootFile The pointer to the boot file URL option data. + @param[in] Length The length of the boot file URL option data. + + @retval EFI_ABORTED User cancel operation. + @retval EFI_SUCCESS Selected the boot menu successfully. + @retval EFI_NOT_READY Read the input key from the keybroad has not finish. + +**/ +EFI_STATUS +PxeBcExtractBootFileUrl ( + IN PXEBC_PRIVATE_DATA *Private, + OUT UINT8 **FileName, + IN OUT EFI_IPv6_ADDRESS *SrvAddr, + IN CHAR8 *BootFile, + IN UINT16 Length + ) +{ + UINT16 PrefixLen; + CHAR8 *BootFileNamePtr; + CHAR8 *BootFileName; + UINT16 BootFileNameLen; + CHAR8 *TmpStr; + CHAR8 TmpChar; + CHAR8 *ServerAddressOption; + CHAR8 *ServerAddress; + CHAR8 *ModeStr; + CHAR16 *HostName; + BOOLEAN IpExpressedUrl; + UINTN Len; + EFI_STATUS Status; + + IpExpressedUrl = TRUE; + // + // The format of the Boot File URL option is: + // + // 0 1 2 3 + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | OPT_BOOTFILE_URL | option-len | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | | + // . bootfile-url (variable length) . + // | | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + + // + // Based upon RFC 5970 and UEFI 2.6, bootfile-url format can be + // tftp://[SERVER_ADDRESS]/BOOTFILE_NAME or tftp://domain_name/BOOTFILE_NAME + // As an example where the BOOTFILE_NAME is the EFI loader and + // SERVER_ADDRESS is the ASCII encoding of an IPV6 address. + // + PrefixLen = (UINT16) AsciiStrLen (PXEBC_DHCP6_BOOT_FILE_URL_PREFIX); + + if (Length <= PrefixLen || + CompareMem (BootFile, PXEBC_DHCP6_BOOT_FILE_URL_PREFIX, PrefixLen) != 0) { + return EFI_NOT_FOUND; + } + + BootFile = BootFile + PrefixLen; + Length = (UINT16) (Length - PrefixLen); + + TmpStr = (CHAR8 *) AllocateZeroPool (Length + 1); + if (TmpStr == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (TmpStr, BootFile, Length); + TmpStr[Length] = '\0'; + + // + // Get the part of SERVER_ADDRESS string. + // + ServerAddressOption = TmpStr; + if (*ServerAddressOption == PXEBC_ADDR_START_DELIMITER) { + ServerAddressOption ++; + ServerAddress = ServerAddressOption; + while (*ServerAddress != '\0' && *ServerAddress != PXEBC_ADDR_END_DELIMITER) { + ServerAddress++; + } + + if (*ServerAddress != PXEBC_ADDR_END_DELIMITER) { + FreePool (TmpStr); + return EFI_INVALID_PARAMETER; + } + + *ServerAddress = '\0'; + + // + // Convert the string of server address to Ipv6 address format and store it. + // + Status = NetLibAsciiStrToIp6 (ServerAddressOption, SrvAddr); + if (EFI_ERROR (Status)) { + FreePool (TmpStr); + return Status; + } + + } else { + IpExpressedUrl = FALSE; + ServerAddress = ServerAddressOption; + while (*ServerAddress != '\0' && *ServerAddress != PXEBC_TFTP_URL_SEPARATOR) { + ServerAddress++; + } + + if (*ServerAddress != PXEBC_TFTP_URL_SEPARATOR) { + FreePool (TmpStr); + return EFI_INVALID_PARAMETER; + } + *ServerAddress = '\0'; + + Len = AsciiStrSize (ServerAddressOption); + HostName = AllocateZeroPool (Len * sizeof (CHAR16)); + if (HostName == NULL) { + FreePool (TmpStr); + return EFI_OUT_OF_RESOURCES; + } + AsciiStrToUnicodeStrS ( + ServerAddressOption, + HostName, + Len + ); + + // + // Perform DNS resolution. + // + Status = PxeBcDns6 (Private,HostName, SrvAddr); + if (EFI_ERROR (Status)) { + FreePool (TmpStr); + return Status; + } + } + + // + // Get the part of BOOTFILE_NAME string. + // + BootFileNamePtr = (CHAR8*)((UINTN)ServerAddress + 1); + if (IpExpressedUrl) { + if (*BootFileNamePtr != PXEBC_TFTP_URL_SEPARATOR) { + FreePool (TmpStr); + return EFI_INVALID_PARAMETER; + } + ++BootFileNamePtr; + } + + BootFileNameLen = (UINT16)(Length - (UINT16) ((UINTN)BootFileNamePtr - (UINTN)TmpStr) + 1); + if (BootFileNameLen != 0 || FileName != NULL) { + // + // Remove trailing mode=octet if present and ignore. All other modes are + // invalid for netboot6, so reject them. + // + ModeStr = AsciiStrStr (BootFileNamePtr, ";mode=octet"); + if (ModeStr != NULL && *(ModeStr + AsciiStrLen (";mode=octet")) == '\0') { + *ModeStr = '\0'; + } else if (AsciiStrStr (BootFileNamePtr, ";mode=") != NULL) { + FreePool (TmpStr); + return EFI_INVALID_PARAMETER; + } + + // + // Extract boot file name from URL. + // + BootFileName = (CHAR8 *) AllocateZeroPool (BootFileNameLen); + if (BootFileName == NULL) { + FreePool (TmpStr); + return EFI_OUT_OF_RESOURCES; + } + *FileName = (UINT8*) BootFileName; + + // + // Decode percent-encoding in boot file name. + // + while (*BootFileNamePtr != '\0') { + if (*BootFileNamePtr == '%') { + TmpChar = *(BootFileNamePtr+ 3); + *(BootFileNamePtr+ 3) = '\0'; + *BootFileName = (UINT8) AsciiStrHexToUintn ((CHAR8*)(BootFileNamePtr + 1)); + BootFileName++; + *(BootFileNamePtr+ 3) = TmpChar; + BootFileNamePtr += 3; + } else { + *BootFileName = *BootFileNamePtr; + BootFileName++; + BootFileNamePtr++; + } + } + *BootFileName = '\0'; + } + + FreePool (TmpStr); + + return EFI_SUCCESS; +} + + +/** + Parse the Boot File Parameter option. + + @param[in] BootFilePara The pointer to boot file parameter option data. + @param[out] BootFileSize The pointer to the parsed boot file size. + + @retval EFI_SUCCESS Successfully obtained the boot file size from parameter option. + @retval EFI_NOT_FOUND Failed to extract the boot file size from parameter option. + +**/ +EFI_STATUS +PxeBcExtractBootFileParam ( + IN CHAR8 *BootFilePara, + OUT UINT16 *BootFileSize + ) +{ + UINT16 Length; + UINT8 Index; + UINT8 Digit; + UINT32 Size; + + CopyMem (&Length, BootFilePara, sizeof (UINT16)); + Length = NTOHS (Length); + + // + // The BootFile Size should be 1~5 byte ASCII strings + // + if (Length < 1 || Length > 5) { + return EFI_NOT_FOUND; + } + + // + // Extract the value of BootFile Size. + // + BootFilePara = BootFilePara + sizeof (UINT16); + Size = 0; + for (Index = 0; Index < Length; Index++) { + if (EFI_ERROR (PxeBcUniHexToUint8 (&Digit, *(BootFilePara + Index)))) { + return EFI_NOT_FOUND; + } + + Size = (Size + Digit) * 10; + } + + Size = Size / 10; + if (Size > PXEBC_DHCP6_MAX_BOOT_FILE_SIZE) { + return EFI_NOT_FOUND; + } + + *BootFileSize = (UINT16) Size; + return EFI_SUCCESS; +} + + +/** + Parse the cached DHCPv6 packet, including all the options. + + @param[in] Cache6 The pointer to a cached DHCPv6 packet. + + @retval EFI_SUCCESS Parsed the DHCPv6 packet successfully. + @retval EFI_DEVICE_ERROR Failed to parse and invalid the packet. + +**/ +EFI_STATUS +PxeBcParseDhcp6Packet ( + IN PXEBC_DHCP6_PACKET_CACHE *Cache6 + ) +{ + EFI_DHCP6_PACKET *Offer; + EFI_DHCP6_PACKET_OPTION **Options; + EFI_DHCP6_PACKET_OPTION *Option; + PXEBC_OFFER_TYPE OfferType; + BOOLEAN IsProxyOffer; + BOOLEAN IsPxeOffer; + UINT32 Offset; + UINT32 Length; + UINT32 EnterpriseNum; + + IsProxyOffer = TRUE; + IsPxeOffer = FALSE; + Offer = &Cache6->Packet.Offer; + Options = Cache6->OptList; + + ZeroMem (Cache6->OptList, sizeof (Cache6->OptList)); + + Option = (EFI_DHCP6_PACKET_OPTION *) (Offer->Dhcp6.Option); + Offset = 0; + Length = GET_DHCP6_OPTION_SIZE (Offer); + + // + // OpLen and OpCode here are both stored in network order, since they are from original packet. + // + while (Offset < Length) { + + if (NTOHS (Option->OpCode) == DHCP6_OPT_IA_NA) { + Options[PXEBC_DHCP6_IDX_IA_NA] = Option; + } else if (NTOHS (Option->OpCode) == DHCP6_OPT_BOOT_FILE_URL) { + // + // The server sends this option to inform the client about an URL to a boot file. + // + Options[PXEBC_DHCP6_IDX_BOOT_FILE_URL] = Option; + } else if (NTOHS (Option->OpCode) == DHCP6_OPT_BOOT_FILE_PARAM) { + Options[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM] = Option; + } else if (NTOHS (Option->OpCode) == DHCP6_OPT_VENDOR_CLASS) { + Options[PXEBC_DHCP6_IDX_VENDOR_CLASS] = Option; + } else if (NTOHS (Option->OpCode) == DHCP6_OPT_DNS_SERVERS) { + Options[PXEBC_DHCP6_IDX_DNS_SERVER] = Option; + } + + Offset += (NTOHS (Option->OpLen) + 4); + Option = (EFI_DHCP6_PACKET_OPTION *) (Offer->Dhcp6.Option + Offset); + } + + // + // The offer with assigned client address is NOT a proxy offer. + // An ia_na option, embeded with valid ia_addr option and a status_code of success. + // + Option = Options[PXEBC_DHCP6_IDX_IA_NA]; + if (Option != NULL) { + Option = PxeBcParseDhcp6Options ( + Option->Data + 12, + NTOHS (Option->OpLen), + DHCP6_OPT_STATUS_CODE + ); + if ((Option != NULL && Option->Data[0] == 0) || (Option == NULL)) { + IsProxyOffer = FALSE; + } + } + + // + // The offer with "PXEClient" is a pxe offer. + // + Option = Options[PXEBC_DHCP6_IDX_VENDOR_CLASS]; + EnterpriseNum = HTONL(PXEBC_DHCP6_ENTERPRISE_NUM); + + if (Option != NULL && + NTOHS(Option->OpLen) >= 13 && + CompareMem (Option->Data, &EnterpriseNum, sizeof (UINT32)) == 0 && + CompareMem (&Option->Data[6], DEFAULT_CLASS_ID_DATA, 9) == 0) { + IsPxeOffer = TRUE; + } + + // + // Determine offer type of the dhcp6 packet. + // + if (IsPxeOffer) { + // + // It's a binl offer only with PXEClient. + // + OfferType = IsProxyOffer ? PxeOfferTypeProxyBinl : PxeOfferTypeDhcpBinl; + } else { + // + // It's a dhcp only offer, which is a pure dhcp6 offer packet. + // + OfferType = PxeOfferTypeDhcpOnly; + } + + Cache6->OfferType = OfferType; + + return EFI_SUCCESS; +} + + +/** + Cache the DHCPv6 ack packet, and parse it on demand. + + @param[in] Private The pointer to PxeBc private data. + @param[in] Ack The pointer to the DHCPv6 ack packet. + @param[in] Verified If TRUE, parse the ACK packet and store info into mode data. + + @retval EFI_SUCCESS Cache and parse the packet successfully. + @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet. + +**/ +EFI_STATUS +PxeBcCopyDhcp6Ack ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_DHCP6_PACKET *Ack, + IN BOOLEAN Verified + ) +{ + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_STATUS Status; + + Mode = Private->PxeBc.Mode; + + Status = PxeBcCacheDhcp6Packet (&Private->DhcpAck.Dhcp6.Packet.Ack, Ack); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Verified) { + // + // Parse the ack packet and store it into mode data if needed. + // + PxeBcParseDhcp6Packet (&Private->DhcpAck.Dhcp6); + CopyMem (&Mode->DhcpAck.Dhcpv6, &Ack->Dhcp6, Ack->Length); + Mode->DhcpAckReceived = TRUE; + } + + return EFI_SUCCESS; +} + + +/** + Cache the DHCPv6 proxy offer packet according to the received order. + + @param[in] Private The pointer to PxeBc private data. + @param[in] OfferIndex The received order of offer packets. + + @retval EFI_SUCCESS Cache and parse the packet successfully. + @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet. + +**/ +EFI_STATUS +PxeBcCopyDhcp6Proxy ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT32 OfferIndex + ) +{ + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_DHCP6_PACKET *Offer; + EFI_STATUS Status; + + ASSERT (OfferIndex < Private->OfferNum); + ASSERT (OfferIndex < PXEBC_OFFER_MAX_NUM); + + Mode = Private->PxeBc.Mode; + Offer = &Private->OfferBuffer[OfferIndex].Dhcp6.Packet.Offer; + + // + // Cache the proxy offer packet and parse it. + // + Status = PxeBcCacheDhcp6Packet (&Private->ProxyOffer.Dhcp6.Packet.Offer, Offer); + if (EFI_ERROR(Status)) { + return Status; + } + PxeBcParseDhcp6Packet (&Private->ProxyOffer.Dhcp6); + + // + // Store this packet into mode data. + // + CopyMem (&Mode->ProxyOffer.Dhcpv6, &Offer->Dhcp6, Offer->Length); + Mode->ProxyOfferReceived = TRUE; + + return EFI_SUCCESS; +} + +/** + Seek the address of the first byte of the option header. + + @param[in] Buf The pointer to the buffer. + @param[in] SeekLen The length to seek. + @param[in] OptType The option type. + + @retval NULL If it failed to seek the option. + @retval others The position to the option. + +**/ +UINT8 * +PxeBcDhcp6SeekOption ( + IN UINT8 *Buf, + IN UINT32 SeekLen, + IN UINT16 OptType + ) +{ + UINT8 *Cursor; + UINT8 *Option; + UINT16 DataLen; + UINT16 OpCode; + + Option = NULL; + Cursor = Buf; + + while (Cursor < Buf + SeekLen) { + OpCode = ReadUnaligned16 ((UINT16 *) Cursor); + if (OpCode == HTONS (OptType)) { + Option = Cursor; + break; + } + DataLen = NTOHS (ReadUnaligned16 ((UINT16 *) (Cursor + 2))); + Cursor += (DataLen + 4); + } + + return Option; +} + + +/** + Build and send out the request packet for the bootfile, and parse the reply. + + @param[in] Private The pointer to PxeBc private data. + @param[in] Index PxeBc option boot item type. + + @retval EFI_SUCCESS Successfully discovered the boot file. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_NOT_FOUND Can't get the PXE reply packet. + @retval Others Failed to discover the boot file. + +**/ +EFI_STATUS +PxeBcRequestBootService ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT32 Index + ) +{ + EFI_PXE_BASE_CODE_UDP_PORT SrcPort; + EFI_PXE_BASE_CODE_UDP_PORT DestPort; + EFI_PXE_BASE_CODE_PROTOCOL *PxeBc; + EFI_PXE_BASE_CODE_DHCPV6_PACKET *Discover; + UINTN DiscoverLen; + EFI_DHCP6_PACKET *Request; + UINTN RequestLen; + EFI_DHCP6_PACKET *Reply; + UINT8 *RequestOpt; + UINT8 *DiscoverOpt; + UINTN ReadSize; + UINT16 OpFlags; + UINT16 OpCode; + UINT16 OpLen; + EFI_STATUS Status; + EFI_DHCP6_PACKET *IndexOffer; + UINT8 *Option; + + PxeBc = &Private->PxeBc; + Request = Private->Dhcp6Request; + IndexOffer = &Private->OfferBuffer[Index].Dhcp6.Packet.Offer; + SrcPort = PXEBC_BS_DISCOVER_PORT; + DestPort = PXEBC_BS_DISCOVER_PORT; + OpFlags = 0; + + if (Request == NULL) { + return EFI_DEVICE_ERROR; + } + + Discover = AllocateZeroPool (sizeof (EFI_PXE_BASE_CODE_DHCPV6_PACKET)); + if (Discover == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Build the request packet by the cached request packet before. + // + Discover->TransactionId = IndexOffer->Dhcp6.Header.TransactionId; + Discover->MessageType = Request->Dhcp6.Header.MessageType; + RequestOpt = Request->Dhcp6.Option; + DiscoverOpt = Discover->DhcpOptions; + DiscoverLen = sizeof (EFI_DHCP6_HEADER); + RequestLen = DiscoverLen; + + // + // Find Server ID Option from ProxyOffer. + // + if (Private->OfferBuffer[Index].Dhcp6.OfferType == PxeOfferTypeProxyBinl) { + Option = PxeBcDhcp6SeekOption ( + IndexOffer->Dhcp6.Option, + IndexOffer->Length - 4, + DHCP6_OPT_SERVER_ID + ); + if (Option == NULL) { + return EFI_NOT_FOUND; + } + + // + // Add Server ID Option. + // + OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) Option)->OpLen); + CopyMem (DiscoverOpt, Option, OpLen + 4); + DiscoverOpt += (OpLen + 4); + DiscoverLen += (OpLen + 4); + } + + while (RequestLen < Request->Length) { + OpCode = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpCode); + OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpLen); + if (OpCode != EFI_DHCP6_IA_TYPE_NA && + OpCode != EFI_DHCP6_IA_TYPE_TA && + OpCode != DHCP6_OPT_SERVER_ID + ) { + // + // Copy all the options except IA option and Server ID + // + CopyMem (DiscoverOpt, RequestOpt, OpLen + 4); + DiscoverOpt += (OpLen + 4); + DiscoverLen += (OpLen + 4); + } + RequestOpt += (OpLen + 4); + RequestLen += (OpLen + 4); + } + + // + // Update Elapsed option in the package + // + Option = PxeBcDhcp6SeekOption ( + Discover->DhcpOptions, + (UINT32)(RequestLen - 4), + DHCP6_OPT_ELAPSED_TIME + ); + if (Option != NULL) { + CalcElapsedTime (Private); + WriteUnaligned16 ((UINT16*)(Option + 4), HTONS((UINT16) Private->ElapsedTime)); + } + + Status = PxeBc->UdpWrite ( + PxeBc, + OpFlags, + &Private->ServerIp, + &DestPort, + NULL, + &Private->StationIp, + &SrcPort, + NULL, + NULL, + &DiscoverLen, + (VOID *) Discover + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Cache the right PXE reply packet here, set valid flag later. + // Especially for PXE discover packet, store it into mode data here. + // + Reply = &Private->ProxyOffer.Dhcp6.Packet.Offer; + ReadSize = (UINTN) Reply->Size; + + // + // Start Udp6Read instance + // + Status = Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = PxeBc->UdpRead ( + PxeBc, + EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP | EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP, + NULL, + &SrcPort, + &Private->ServerIp, + &DestPort, + NULL, + NULL, + &ReadSize, + (VOID *) &Reply->Dhcp6 + ); + // + // Stop Udp6Read instance + // + Private->Udp6Read->Configure (Private->Udp6Read, NULL); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Update length + // + Reply->Length = (UINT32) ReadSize; + + return EFI_SUCCESS; + +ON_ERROR: + if (Discover != NULL) { + FreePool (Discover); + } + + return Status; +} + + +/** + Retry to request bootfile name by the BINL offer. + + @param[in] Private The pointer to PxeBc private data. + @param[in] Index The received order of offer packets. + + @retval EFI_SUCCESS Successfully retried a request for the bootfile name. + @retval EFI_DEVICE_ERROR Failed to retry the bootfile name. + +**/ +EFI_STATUS +PxeBcRetryDhcp6Binl ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT32 Index + ) +{ + EFI_PXE_BASE_CODE_MODE *Mode; + PXEBC_DHCP6_PACKET_CACHE *Offer; + PXEBC_DHCP6_PACKET_CACHE *Cache6; + EFI_STATUS Status; + + ASSERT (Index < PXEBC_OFFER_MAX_NUM); + ASSERT (Private->OfferBuffer[Index].Dhcp6.OfferType == PxeOfferTypeDhcpBinl || + Private->OfferBuffer[Index].Dhcp6.OfferType == PxeOfferTypeProxyBinl); + + Mode = Private->PxeBc.Mode; + Private->IsDoDiscover = FALSE; + Offer = &Private->OfferBuffer[Index].Dhcp6; + if (Offer->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] == NULL) { + // + // There is no BootFileUrl option in dhcp6 offer, so use servers multi-cast address instead. + // + CopyMem ( + &Private->ServerIp.v6, + &mAllDhcpRelayAndServersAddress, + sizeof (EFI_IPv6_ADDRESS) + ); + } else { + ASSERT (Offer->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] != NULL); + // + // Parse out the next server address from the last offer, and store it + // + Status = PxeBcExtractBootFileUrl ( + Private, + &Private->BootFileName, + &Private->ServerIp.v6, + (CHAR8 *) (Offer->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL]->Data), + NTOHS (Offer->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL]->OpLen) + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Retry Dhcp6Binl again for the bootfile, and the reply cached into Private->ProxyOffer. + // + Status = PxeBcRequestBootService (Private, Index); + + if (EFI_ERROR (Status)) { + return Status; + } + + Cache6 = &Private->ProxyOffer.Dhcp6; + Status = PxeBcParseDhcp6Packet (Cache6); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Cache6->OfferType != PxeOfferTypeProxyPxe10 && + Cache6->OfferType != PxeOfferTypeProxyWfm11a && + Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] == NULL) { + // + // This BINL ack doesn't have discovery option set or multicast option set + // or bootfile name specified. + // + return EFI_DEVICE_ERROR; + } + + Mode->ProxyOfferReceived = TRUE; + CopyMem ( + &Mode->ProxyOffer.Dhcpv6, + &Cache6->Packet.Offer.Dhcp6, + Cache6->Packet.Offer.Length + ); + + return EFI_SUCCESS; +} + + +/** + Cache all the received DHCPv6 offers, and set OfferIndex and OfferCount. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + @param[in] RcvdOffer The pointer to the received offer packet. + + @retval EFI_SUCCESS Cache and parse the packet successfully. + @retval Others Operation failed. +**/ +EFI_STATUS +PxeBcCacheDhcp6Offer ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_DHCP6_PACKET *RcvdOffer + ) +{ + PXEBC_DHCP6_PACKET_CACHE *Cache6; + EFI_DHCP6_PACKET *Offer; + PXEBC_OFFER_TYPE OfferType; + EFI_STATUS Status; + + Cache6 = &Private->OfferBuffer[Private->OfferNum].Dhcp6; + Offer = &Cache6->Packet.Offer; + + // + // Cache the content of DHCPv6 packet firstly. + // + Status = PxeBcCacheDhcp6Packet (Offer, RcvdOffer); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Validate the DHCPv6 packet, and parse the options and offer type. + // + if (EFI_ERROR (PxeBcParseDhcp6Packet (Cache6))) { + return EFI_ABORTED; + } + + // + // Determine whether cache the current offer by type, and record OfferIndex and OfferCount. + // + OfferType = Cache6->OfferType; + ASSERT (OfferType < PxeOfferTypeMax); + ASSERT (Private->OfferCount[OfferType] < PXEBC_OFFER_MAX_NUM); + + if (IS_PROXY_OFFER (OfferType)) { + // + // It's a proxy offer without yiaddr, including PXE10, WFM11a or BINL offer. + // + Private->IsProxyRecved = TRUE; + + if (OfferType == PxeOfferTypeProxyBinl) { + // + // Cache all proxy BINL offers. + // + Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum; + Private->OfferCount[OfferType]++; + } else if ((OfferType == PxeOfferTypeProxyPxe10 || OfferType == PxeOfferTypeProxyWfm11a) && + Private->OfferCount[OfferType] < 1) { + // + // Only cache the first PXE10/WFM11a offer, and discard the others. + // + Private->OfferIndex[OfferType][0] = Private->OfferNum; + Private->OfferCount[OfferType] = 1; + } else { + return EFI_ABORTED; + } + } else { + // + // It's a DHCPv6 offer with yiaddr, and cache them all. + // + Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum; + Private->OfferCount[OfferType]++; + } + + Private->OfferNum++; + + return EFI_SUCCESS; +} + + +/** + Select an DHCPv6 offer, and record SelectIndex and SelectProxyType. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + +**/ +VOID +PxeBcSelectDhcp6Offer ( + IN PXEBC_PRIVATE_DATA *Private + ) +{ + UINT32 Index; + UINT32 OfferIndex; + PXEBC_OFFER_TYPE OfferType; + + Private->SelectIndex = 0; + + if (Private->IsOfferSorted) { + // + // Select offer by default policy. + // + if (Private->OfferCount[PxeOfferTypeDhcpPxe10] > 0) { + // + // 1. DhcpPxe10 offer + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpPxe10][0] + 1; + + } else if (Private->OfferCount[PxeOfferTypeDhcpWfm11a] > 0) { + // + // 2. DhcpWfm11a offer + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpWfm11a][0] + 1; + + } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 && + Private->OfferCount[PxeOfferTypeProxyPxe10] > 0) { + // + // 3. DhcpOnly offer and ProxyPxe10 offer. + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1; + Private->SelectProxyType = PxeOfferTypeProxyPxe10; + + } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 && + Private->OfferCount[PxeOfferTypeProxyWfm11a] > 0) { + // + // 4. DhcpOnly offer and ProxyWfm11a offer. + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1; + Private->SelectProxyType = PxeOfferTypeProxyWfm11a; + + } else if (Private->OfferCount[PxeOfferTypeDhcpBinl] > 0) { + // + // 5. DhcpBinl offer. + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpBinl][0] + 1; + + } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 && + Private->OfferCount[PxeOfferTypeProxyBinl] > 0) { + // + // 6. DhcpOnly offer and ProxyBinl offer. + // + Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1; + Private->SelectProxyType = PxeOfferTypeProxyBinl; + + } else { + // + // 7. DhcpOnly offer with bootfilename. + // + for (Index = 0; Index < Private->OfferCount[PxeOfferTypeDhcpOnly]; Index++) { + OfferIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][Index]; + if (Private->OfferBuffer[OfferIndex].Dhcp6.OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] != NULL) { + Private->SelectIndex = OfferIndex + 1; + break; + } + } + } + } else { + // + // Select offer by received order. + // + for (Index = 0; Index < Private->OfferNum; Index++) { + + OfferType = Private->OfferBuffer[Index].Dhcp6.OfferType; + + if (IS_PROXY_OFFER (OfferType)) { + // + // Skip proxy offers + // + continue; + } + + if (!Private->IsProxyRecved && + OfferType == PxeOfferTypeDhcpOnly && + Private->OfferBuffer[Index].Dhcp6.OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] == NULL) { + // + // Skip if DhcpOnly offer without any other proxy offers or bootfilename. + // + continue; + } + + Private->SelectIndex = Index + 1; + break; + } + } +} + + +/** + Handle the DHCPv6 offer packet. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + + @retval EFI_SUCCESS Handled the DHCPv6 offer packet successfully. + @retval EFI_NO_RESPONSE No response to the following request packet. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_BUFFER_TOO_SMALL Can't cache the offer pacet. + +**/ +EFI_STATUS +PxeBcHandleDhcp6Offer ( + IN PXEBC_PRIVATE_DATA *Private + ) +{ + PXEBC_DHCP6_PACKET_CACHE *Cache6; + EFI_STATUS Status; + PXEBC_OFFER_TYPE OfferType; + UINT32 ProxyIndex; + UINT32 SelectIndex; + UINT32 Index; + + ASSERT (Private->SelectIndex > 0); + SelectIndex = (UINT32) (Private->SelectIndex - 1); + ASSERT (SelectIndex < PXEBC_OFFER_MAX_NUM); + Cache6 = &Private->OfferBuffer[SelectIndex].Dhcp6; + Status = EFI_SUCCESS; + + // + // First try to cache DNS server address if DHCP6 offer provides. + // + if (Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER] != NULL) { + Private->DnsServer = AllocateZeroPool (NTOHS (Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpLen)); + if (Private->DnsServer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + CopyMem (Private->DnsServer, Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->Data, sizeof (EFI_IPv6_ADDRESS)); + } + + if (Cache6->OfferType == PxeOfferTypeDhcpBinl) { + // + // DhcpBinl offer is selected, so need try to request bootfilename by this offer. + // + if (EFI_ERROR (PxeBcRetryDhcp6Binl (Private, SelectIndex))) { + Status = EFI_NO_RESPONSE; + } + } else if (Cache6->OfferType == PxeOfferTypeDhcpOnly) { + + if (Private->IsProxyRecved) { + // + // DhcpOnly offer is selected, so need try to request bootfilename. + // + ProxyIndex = 0; + if (Private->IsOfferSorted) { + // + // The proxy offer should be determined if select by default policy. + // IsOfferSorted means all offers are labeled by OfferIndex. + // + ASSERT (Private->OfferCount[Private->SelectProxyType] > 0); + + if (Private->SelectProxyType == PxeOfferTypeProxyBinl) { + // + // Try all the cached ProxyBinl offer one by one to request bootfilename. + // + for (Index = 0; Index < Private->OfferCount[Private->SelectProxyType]; Index++) { + + ProxyIndex = Private->OfferIndex[Private->SelectProxyType][Index]; + if (!EFI_ERROR (PxeBcRetryDhcp6Binl (Private, ProxyIndex))) { + break; + } + } + if (Index == Private->OfferCount[Private->SelectProxyType]) { + Status = EFI_NO_RESPONSE; + } + } else { + // + // For other proxy offers (pxe10 or wfm11a), only one is buffered. + // + ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0]; + } + } else { + // + // The proxy offer should not be determined if select by received order. + // + Status = EFI_NO_RESPONSE; + + for (Index = 0; Index < Private->OfferNum; Index++) { + + OfferType = Private->OfferBuffer[Index].Dhcp6.OfferType; + + if (!IS_PROXY_OFFER (OfferType)) { + // + // Skip non proxy dhcp offers. + // + continue; + } + + if (OfferType == PxeOfferTypeProxyBinl) { + // + // Try all the cached ProxyBinl offer one by one to request bootfilename. + // + if (EFI_ERROR (PxeBcRetryDhcp6Binl (Private, Index))) { + continue; + } + } + + Private->SelectProxyType = OfferType; + ProxyIndex = Index; + Status = EFI_SUCCESS; + break; + } + } + + if (!EFI_ERROR (Status) && Private->SelectProxyType != PxeOfferTypeProxyBinl) { + // + // Success to try to request by a ProxyPxe10 or ProxyWfm11a offer, copy and parse it. + // + Status = PxeBcCopyDhcp6Proxy (Private, ProxyIndex); + } + } else { + // + // Othewise, the bootfilename must be included in DhcpOnly offer. + // + ASSERT (Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] != NULL); + } + } + + if (!EFI_ERROR (Status)) { + // + // All PXE boot information is ready by now. + // + Status = PxeBcCopyDhcp6Ack (Private, &Private->DhcpAck.Dhcp6.Packet.Ack, TRUE); + Private->PxeBc.Mode->DhcpDiscoverValid = TRUE; + } + + return Status; +} + + +/** + Unregister the address by Ip6Config protocol. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + +**/ +VOID +PxeBcUnregisterIp6Address ( + IN PXEBC_PRIVATE_DATA *Private + ) +{ + if (Private->Ip6Policy != PXEBC_IP6_POLICY_MAX) { + // + // PXE driver change the policy of IP6 driver, it's a chance to recover. + // Keep the point and there is no enough requirements to do recovery. + // + } +} + +/** + Check whether IP driver could route the message which will be sent to ServerIp address. + + This function will check the IP6 route table every 1 seconds until specified timeout is expired, if a valid + route is found in IP6 route table, the address will be filed in GatewayAddr and return. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + @param[in] TimeOutInSecond Timeout value in seconds. + @param[out] GatewayAddr Pointer to store the gateway IP address. + + @retval EFI_SUCCESS Found a valid gateway address successfully. + @retval EFI_TIMEOUT The operation is time out. + @retval Other Unexpect error happened. + +**/ +EFI_STATUS +PxeBcCheckRouteTable ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINTN TimeOutInSecond, + OUT EFI_IPv6_ADDRESS *GatewayAddr + ) +{ + EFI_STATUS Status; + EFI_IP6_PROTOCOL *Ip6; + EFI_IP6_MODE_DATA Ip6ModeData; + UINTN Index; + EFI_EVENT TimeOutEvt; + UINTN RetryCount; + BOOLEAN GatewayIsFound; + + ASSERT (GatewayAddr != NULL); + ASSERT (Private != NULL); + + Ip6 = Private->Ip6; + GatewayIsFound = FALSE; + RetryCount = 0; + TimeOutEvt = NULL; + ZeroMem (GatewayAddr, sizeof (EFI_IPv6_ADDRESS)); + + while (TRUE) { + Status = Ip6->GetModeData (Ip6, &Ip6ModeData, NULL, NULL); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Find out the gateway address which can route the message which send to ServerIp. + // + for (Index = 0; Index < Ip6ModeData.RouteCount; Index++) { + if (NetIp6IsNetEqual (&Private->ServerIp.v6, &Ip6ModeData.RouteTable[Index].Destination, Ip6ModeData.RouteTable[Index].PrefixLength)) { + IP6_COPY_ADDRESS (GatewayAddr, &Ip6ModeData.RouteTable[Index].Gateway); + GatewayIsFound = TRUE; + break; + } + } + + if (Ip6ModeData.AddressList != NULL) { + FreePool (Ip6ModeData.AddressList); + } + if (Ip6ModeData.GroupTable != NULL) { + FreePool (Ip6ModeData.GroupTable); + } + if (Ip6ModeData.RouteTable != NULL) { + FreePool (Ip6ModeData.RouteTable); + } + if (Ip6ModeData.NeighborCache != NULL) { + FreePool (Ip6ModeData.NeighborCache); + } + if (Ip6ModeData.PrefixTable != NULL) { + FreePool (Ip6ModeData.PrefixTable); + } + if (Ip6ModeData.IcmpTypeList != NULL) { + FreePool (Ip6ModeData.IcmpTypeList); + } + + if (GatewayIsFound || RetryCount == TimeOutInSecond) { + break; + } + + RetryCount++; + + // + // Delay 1 second then recheck it again. + // + if (TimeOutEvt == NULL) { + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &TimeOutEvt + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + Status = gBS->SetTimer (TimeOutEvt, TimerRelative, TICKS_PER_SECOND); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + while (EFI_ERROR (gBS->CheckEvent (TimeOutEvt))) { + Ip6->Poll (Ip6); + } + } + +ON_EXIT: + if (TimeOutEvt != NULL) { + gBS->CloseEvent (TimeOutEvt); + } + + if (GatewayIsFound) { + Status = EFI_SUCCESS; + } else if (RetryCount == TimeOutInSecond) { + Status = EFI_TIMEOUT; + } + + return Status; +} + +/** + Register the ready station address and gateway by Ip6Config protocol. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + @param[in] Address The pointer to the ready address. + + @retval EFI_SUCCESS Registered the address succesfully. + @retval Others Failed to register the address. + +**/ +EFI_STATUS +PxeBcRegisterIp6Address ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_IPv6_ADDRESS *Address + ) +{ + EFI_IP6_PROTOCOL *Ip6; + EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg; + EFI_IP6_CONFIG_POLICY Policy; + EFI_IP6_CONFIG_MANUAL_ADDRESS CfgAddr; + EFI_IPv6_ADDRESS GatewayAddr; + UINTN DataSize; + EFI_EVENT MappedEvt; + EFI_STATUS Status; + BOOLEAN NoGateway; + EFI_IPv6_ADDRESS *Ip6Addr; + UINTN Index; + + Status = EFI_SUCCESS; + MappedEvt = NULL; + Ip6Addr = NULL; + DataSize = sizeof (EFI_IP6_CONFIG_POLICY); + Ip6Cfg = Private->Ip6Cfg; + Ip6 = Private->Ip6; + NoGateway = FALSE; + + ZeroMem (&CfgAddr, sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS)); + CopyMem (&CfgAddr.Address, Address, sizeof (EFI_IPv6_ADDRESS)); + + Status = Ip6->Configure (Ip6, &Private->Ip6CfgData); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Retrieve the gateway address from IP6 route table. + // + Status = PxeBcCheckRouteTable (Private, PXEBC_IP6_ROUTE_TABLE_TIMEOUT, &GatewayAddr); + if (EFI_ERROR (Status)) { + NoGateway = TRUE; + } + + // + // There is no channel between IP6 and PXE driver about address setting, + // so it has to set the new address by Ip6ConfigProtocol manually. + // + Policy = Ip6ConfigPolicyManual; + Status = Ip6Cfg->SetData ( + Ip6Cfg, + Ip6ConfigDataTypePolicy, + sizeof(EFI_IP6_CONFIG_POLICY), + &Policy + ); + if (EFI_ERROR (Status)) { + // + // There is no need to recover later. + // + Private->Ip6Policy = PXEBC_IP6_POLICY_MAX; + goto ON_EXIT; + } + + // + // Create a notify event to set address flag when DAD if IP6 driver succeeded. + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + PxeBcCommonNotify, + &Private->IsAddressOk, + &MappedEvt + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Private->IsAddressOk = FALSE; + Status = Ip6Cfg->RegisterDataNotify ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + MappedEvt + ); + if (EFI_ERROR(Status)) { + goto ON_EXIT; + } + + Status = Ip6Cfg->SetData ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + sizeof(EFI_IP6_CONFIG_MANUAL_ADDRESS), + &CfgAddr + ); + if (EFI_ERROR(Status) && Status != EFI_NOT_READY) { + goto ON_EXIT; + } else if (Status == EFI_NOT_READY) { + // + // Poll the network until the asynchronous process is finished. + // + while (!Private->IsAddressOk) { + Ip6->Poll (Ip6); + } + // + // Check whether the IP6 address setting is successed. + // + DataSize = 0; + Status = Ip6Cfg->GetData ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + &DataSize, + NULL + ); + if (Status != EFI_BUFFER_TOO_SMALL || DataSize == 0) { + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + Ip6Addr = AllocatePool (DataSize); + if (Ip6Addr == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Status = Ip6Cfg->GetData ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + &DataSize, + (VOID*) Ip6Addr + ); + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + + for (Index = 0; Index < DataSize / sizeof (EFI_IPv6_ADDRESS); Index++) { + if (CompareMem (Ip6Addr + Index, Address, sizeof (EFI_IPv6_ADDRESS)) == 0) { + break; + } + } + if (Index == DataSize / sizeof (EFI_IPv6_ADDRESS)) { + Status = EFI_ABORTED; + goto ON_EXIT; + } + } + + // + // Set the default gateway address back if needed. + // + if (!NoGateway && !NetIp6IsUnspecifiedAddr (&GatewayAddr)) { + Status = Ip6Cfg->SetData ( + Ip6Cfg, + Ip6ConfigDataTypeGateway, + sizeof (EFI_IPv6_ADDRESS), + &GatewayAddr + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + +ON_EXIT: + if (MappedEvt != NULL) { + Ip6Cfg->UnregisterDataNotify ( + Ip6Cfg, + Ip6ConfigDataTypeManualAddress, + MappedEvt + ); + gBS->CloseEvent (MappedEvt); + } + if (Ip6Addr != NULL) { + FreePool (Ip6Addr); + } + return Status; +} + +/** + Set the IP6 policy to Automatic. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + + @retval EFI_SUCCESS Switch the IP policy succesfully. + @retval Others Unexpect error happened. + +**/ +EFI_STATUS +PxeBcSetIp6Policy ( + IN PXEBC_PRIVATE_DATA *Private + ) +{ + EFI_IP6_CONFIG_POLICY Policy; + EFI_STATUS Status; + EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg; + UINTN DataSize; + + Ip6Cfg = Private->Ip6Cfg; + DataSize = sizeof (EFI_IP6_CONFIG_POLICY); + + // + // Get and store the current policy of IP6 driver. + // + Status = Ip6Cfg->GetData ( + Ip6Cfg, + Ip6ConfigDataTypePolicy, + &DataSize, + &Private->Ip6Policy + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Private->Ip6Policy == Ip6ConfigPolicyManual) { + Policy = Ip6ConfigPolicyAutomatic; + Status = Ip6Cfg->SetData ( + Ip6Cfg, + Ip6ConfigDataTypePolicy, + sizeof(EFI_IP6_CONFIG_POLICY), + &Policy + ); + if (EFI_ERROR (Status)) { + // + // There is no need to recover later. + // + Private->Ip6Policy = PXEBC_IP6_POLICY_MAX; + } + } + + return Status; +} + +/** + This function will register the station IP address and flush IP instance to start using the new IP address. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + + @retval EFI_SUCCESS The new IP address has been configured successfully. + @retval Others Failed to configure the address. + +**/ +EFI_STATUS +PxeBcSetIp6Address ( + IN PXEBC_PRIVATE_DATA *Private + ) +{ + EFI_STATUS Status; + EFI_DHCP6_PROTOCOL *Dhcp6; + + Dhcp6 = Private->Dhcp6; + + CopyMem (&Private->StationIp.v6, &Private->TmpStationIp.v6, sizeof (EFI_IPv6_ADDRESS)); + CopyMem (&Private->PxeBc.Mode->StationIp.v6, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS)); + + Status = PxeBcRegisterIp6Address (Private, &Private->StationIp.v6); + if (EFI_ERROR (Status)) { + Dhcp6->Stop (Dhcp6); + return Status; + } + + Status = PxeBcFlushStationIp (Private, &Private->StationIp, NULL); + if (EFI_ERROR (Status)) { + PxeBcUnregisterIp6Address (Private); + Dhcp6->Stop (Dhcp6); + return Status; + } + + AsciiPrint ("\n Station IP address is "); + PxeBcShowIp6Addr (&Private->StationIp.v6); + + return EFI_SUCCESS; +} + +/** + EFI_DHCP6_CALLBACK is provided by the consumer of the EFI DHCPv6 Protocol driver + to intercept events that occurred in the configuration process. + + @param[in] This The pointer to the EFI DHCPv6 Protocol. + @param[in] Context The pointer to the context set by EFI_DHCP6_PROTOCOL.Configure(). + @param[in] CurrentState The current operational state of the EFI DHCPv Protocol driver. + @param[in] Dhcp6Event The event that occurs in the current state, which usually means a + state transition. + @param[in] Packet The DHCPv6 packet that is going to be sent or was already received. + @param[out] NewPacket The packet that is used to replace the Packet above. + + @retval EFI_SUCCESS Told the EFI DHCPv6 Protocol driver to continue the DHCP process. + @retval EFI_NOT_READY Only used in the Dhcp6Selecting state. The EFI DHCPv6 Protocol + driver will continue to wait for more packets. + @retval EFI_ABORTED Told the EFI DHCPv6 Protocol driver to abort the current process. + +**/ +EFI_STATUS +EFIAPI +PxeBcDhcp6CallBack ( + IN EFI_DHCP6_PROTOCOL *This, + IN VOID *Context, + IN EFI_DHCP6_STATE CurrentState, + IN EFI_DHCP6_EVENT Dhcp6Event, + IN EFI_DHCP6_PACKET *Packet, + OUT EFI_DHCP6_PACKET **NewPacket OPTIONAL + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback; + EFI_DHCP6_PACKET *SelectAd; + EFI_STATUS Status; + BOOLEAN Received; + + if ((Dhcp6Event != Dhcp6RcvdAdvertise) && + (Dhcp6Event != Dhcp6SelectAdvertise) && + (Dhcp6Event != Dhcp6SendSolicit) && + (Dhcp6Event != Dhcp6SendRequest) && + (Dhcp6Event != Dhcp6RcvdReply)) { + return EFI_SUCCESS; + } + + ASSERT (Packet != NULL); + + Private = (PXEBC_PRIVATE_DATA *) Context; + Mode = Private->PxeBc.Mode; + Callback = Private->PxeBcCallback; + + // + // Callback to user when any traffic ocurred if has. + // + if (Dhcp6Event != Dhcp6SelectAdvertise && Callback != NULL) { + Received = (BOOLEAN) (Dhcp6Event == Dhcp6RcvdAdvertise || Dhcp6Event == Dhcp6RcvdReply); + Status = Callback->Callback ( + Callback, + Private->Function, + Received, + Packet->Length, + (EFI_PXE_BASE_CODE_PACKET *) &Packet->Dhcp6 + ); + if (Status != EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE) { + return EFI_ABORTED; + } + } + + Status = EFI_SUCCESS; + + switch (Dhcp6Event) { + + case Dhcp6SendSolicit: + if (Packet->Length > PXEBC_DHCP6_PACKET_MAX_SIZE) { + // + // If the to be sent packet exceeds the maximum length, abort the DHCP process. + // + Status = EFI_ABORTED; + break; + } + + // + // Record the first Solicate msg time + // + if (Private->SolicitTimes == 0) { + CalcElapsedTime (Private); + Private->SolicitTimes++; + } + // + // Cache the dhcp discover packet to mode data directly. + // + CopyMem (&Mode->DhcpDiscover.Dhcpv4, &Packet->Dhcp6, Packet->Length); + break; + + case Dhcp6RcvdAdvertise: + Status = EFI_NOT_READY; + if (Packet->Length > PXEBC_DHCP6_PACKET_MAX_SIZE) { + // + // Ignore the incoming packets which exceed the maximum length. + // + break; + } + if (Private->OfferNum < PXEBC_OFFER_MAX_NUM) { + // + // Cache the dhcp offers to OfferBuffer[] for select later, and record + // the OfferIndex and OfferCount. + // + PxeBcCacheDhcp6Offer (Private, Packet); + } + break; + + case Dhcp6SendRequest: + if (Packet->Length > PXEBC_DHCP6_PACKET_MAX_SIZE) { + // + // If the to be sent packet exceeds the maximum length, abort the DHCP process. + // + Status = EFI_ABORTED; + break; + } + + // + // Store the request packet as seed packet for discover. + // + if (Private->Dhcp6Request != NULL) { + FreePool (Private->Dhcp6Request); + } + Private->Dhcp6Request = AllocateZeroPool (Packet->Size); + if (Private->Dhcp6Request != NULL) { + CopyMem (Private->Dhcp6Request, Packet, Packet->Size); + } + break; + + case Dhcp6SelectAdvertise: + // + // Select offer by the default policy or by order, and record the SelectIndex + // and SelectProxyType. + // + PxeBcSelectDhcp6Offer (Private); + + if (Private->SelectIndex == 0) { + Status = EFI_ABORTED; + } else { + ASSERT (NewPacket != NULL); + SelectAd = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp6.Packet.Offer; + *NewPacket = AllocateZeroPool (SelectAd->Size); + ASSERT (*NewPacket != NULL); + if (*NewPacket == NULL) { + return EFI_ABORTED; + } + CopyMem (*NewPacket, SelectAd, SelectAd->Size); + } + break; + + case Dhcp6RcvdReply: + // + // Cache the dhcp ack to Private->Dhcp6Ack, but it's not the final ack in mode data + // without verification. + // + ASSERT (Private->SelectIndex != 0); + Status = PxeBcCopyDhcp6Ack (Private, Packet, FALSE); + if (EFI_ERROR (Status)) { + Status = EFI_ABORTED; + } + break; + + default: + ASSERT (0); + } + + return Status; +} + + +/** + Build and send out the request packet for the bootfile, and parse the reply. + + @param[in] Private The pointer to PxeBc private data. + @param[in] Type PxeBc option boot item type. + @param[in] Layer The pointer to option boot item layer. + @param[in] UseBis Use BIS or not. + @param[in] DestIp The pointer to the server address. + + @retval EFI_SUCCESS Successfully discovered the boot file. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resources. + @retval EFI_NOT_FOUND Can't get the PXE reply packet. + @retval Others Failed to discover the boot file. + +**/ +EFI_STATUS +PxeBcDhcp6Discover ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT16 Type, + IN UINT16 *Layer, + IN BOOLEAN UseBis, + IN EFI_IP_ADDRESS *DestIp + ) +{ + EFI_PXE_BASE_CODE_UDP_PORT SrcPort; + EFI_PXE_BASE_CODE_UDP_PORT DestPort; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_PXE_BASE_CODE_PROTOCOL *PxeBc; + EFI_PXE_BASE_CODE_DHCPV6_PACKET *Discover; + UINTN DiscoverLen; + EFI_DHCP6_PACKET *Request; + UINTN RequestLen; + EFI_DHCP6_PACKET *Reply; + UINT8 *RequestOpt; + UINT8 *DiscoverOpt; + UINTN ReadSize; + UINT16 OpCode; + UINT16 OpLen; + UINT32 Xid; + EFI_STATUS Status; + + PxeBc = &Private->PxeBc; + Mode = PxeBc->Mode; + Request = Private->Dhcp6Request; + SrcPort = PXEBC_BS_DISCOVER_PORT; + DestPort = PXEBC_BS_DISCOVER_PORT; + + if (!UseBis && Layer != NULL) { + *Layer &= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK; + } + + if (Request == NULL) { + return EFI_DEVICE_ERROR; + } + + Discover = AllocateZeroPool (sizeof (EFI_PXE_BASE_CODE_DHCPV6_PACKET)); + if (Discover == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Build the discover packet by the cached request packet before. + // + Xid = NET_RANDOM (NetRandomInitSeed ()); + Discover->TransactionId = HTONL (Xid); + Discover->MessageType = Request->Dhcp6.Header.MessageType; + RequestOpt = Request->Dhcp6.Option; + DiscoverOpt = Discover->DhcpOptions; + DiscoverLen = sizeof (EFI_DHCP6_HEADER); + RequestLen = DiscoverLen; + + while (RequestLen < Request->Length) { + OpCode = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpCode); + OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpLen); + if (OpCode != EFI_DHCP6_IA_TYPE_NA && + OpCode != EFI_DHCP6_IA_TYPE_TA) { + // + // Copy all the options except IA option. + // + CopyMem (DiscoverOpt, RequestOpt, OpLen + 4); + DiscoverOpt += (OpLen + 4); + DiscoverLen += (OpLen + 4); + } + RequestOpt += (OpLen + 4); + RequestLen += (OpLen + 4); + } + + Status = PxeBc->UdpWrite ( + PxeBc, + 0, + &Private->ServerIp, + &DestPort, + NULL, + &Private->StationIp, + &SrcPort, + NULL, + NULL, + &DiscoverLen, + (VOID *) Discover + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Cache the right PXE reply packet here, set valid flag later. + // Especially for PXE discover packet, store it into mode data here. + // + if (Private->IsDoDiscover) { + CopyMem (&Mode->PxeDiscover.Dhcpv6, Discover, DiscoverLen); + Reply = &Private->PxeReply.Dhcp6.Packet.Ack; + } else { + Reply = &Private->ProxyOffer.Dhcp6.Packet.Offer; + } + ReadSize = (UINTN) Reply->Size; + + // + // Start Udp6Read instance + // + Status = Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = PxeBc->UdpRead ( + PxeBc, + EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP, + NULL, + &SrcPort, + &Private->ServerIp, + &DestPort, + NULL, + NULL, + &ReadSize, + (VOID *) &Reply->Dhcp6 + ); + // + // Stop Udp6Read instance + // + Private->Udp6Read->Configure (Private->Udp6Read, NULL); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + if (Discover != NULL) { + FreePool (Discover); + } + + return Status; +} + + +/** + Start the DHCPv6 S.A.R.R. process to acquire the IPv6 address and other PXE boot information. + + @param[in] Private The pointer to PxeBc private data. + @param[in] Dhcp6 The pointer to the EFI_DHCP6_PROTOCOL + + @retval EFI_SUCCESS The S.A.R.R. process successfully finished. + @retval Others Failed to finish the S.A.R.R. process. + +**/ +EFI_STATUS +PxeBcDhcp6Sarr ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_DHCP6_PROTOCOL *Dhcp6 + ) +{ + EFI_PXE_BASE_CODE_MODE *PxeMode; + EFI_DHCP6_CONFIG_DATA Config; + EFI_DHCP6_MODE_DATA Mode; + EFI_DHCP6_RETRANSMISSION *Retransmit; + EFI_DHCP6_PACKET_OPTION *OptList[PXEBC_DHCP6_OPTION_MAX_NUM]; + UINT8 Buffer[PXEBC_DHCP6_OPTION_MAX_SIZE]; + UINT32 OptCount; + EFI_STATUS Status; + EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg; + EFI_STATUS TimerStatus; + EFI_EVENT Timer; + UINT64 GetMappingTimeOut; + UINTN DataSize; + EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS DadXmits; + + Status = EFI_SUCCESS; + PxeMode = Private->PxeBc.Mode; + Ip6Cfg = Private->Ip6Cfg; + Timer = NULL; + + // + // Build option list for the request packet. + // + OptCount = PxeBcBuildDhcp6Options (Private, OptList, Buffer); + ASSERT (OptCount> 0); + + Retransmit = AllocateZeroPool (sizeof (EFI_DHCP6_RETRANSMISSION)); + if (Retransmit == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + ZeroMem (&Mode, sizeof (EFI_DHCP6_MODE_DATA)); + ZeroMem (&Config, sizeof (EFI_DHCP6_CONFIG_DATA)); + + Config.OptionCount = OptCount; + Config.OptionList = OptList; + Config.Dhcp6Callback = PxeBcDhcp6CallBack; + Config.CallbackContext = Private; + Config.IaInfoEvent = NULL; + Config.RapidCommit = FALSE; + Config.ReconfigureAccept = FALSE; + Config.IaDescriptor.IaId = Private->IaId; + Config.IaDescriptor.Type = EFI_DHCP6_IA_TYPE_NA; + Config.SolicitRetransmission = Retransmit; + Retransmit->Irt = 4; + Retransmit->Mrc = 4; + Retransmit->Mrt = 32; + Retransmit->Mrd = 60; + + // + // Configure the DHCPv6 instance for PXE boot. + // + Status = Dhcp6->Configure (Dhcp6, &Config); + FreePool (Retransmit); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Initialize the record fields for DHCPv6 offer in private data. + // + Private->IsProxyRecved = FALSE; + Private->OfferNum = 0; + Private->SelectIndex = 0; + ZeroMem (Private->OfferCount, sizeof (Private->OfferCount)); + ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex)); + + + // + // Start DHCPv6 S.A.R.R. process to acquire IPv6 address. + // + Status = Dhcp6->Start (Dhcp6); + if (Status == EFI_NO_MAPPING) { + // + // IP6 Linklocal address is not available for use, so stop current Dhcp process + // and wait for duplicate address detection to finish. + // + Dhcp6->Stop (Dhcp6); + + // + // Get Duplicate Address Detection Transmits count. + // + DataSize = sizeof (EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS); + Status = Ip6Cfg->GetData ( + Ip6Cfg, + Ip6ConfigDataTypeDupAddrDetectTransmits, + &DataSize, + &DadXmits + ); + if (EFI_ERROR (Status)) { + Dhcp6->Configure (Dhcp6, NULL); + return Status; + } + + Status = gBS->CreateEvent (EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Timer); + if (EFI_ERROR (Status)) { + Dhcp6->Configure (Dhcp6, NULL); + return Status; + } + + GetMappingTimeOut = TICKS_PER_SECOND * DadXmits.DupAddrDetectTransmits + PXEBC_DAD_ADDITIONAL_DELAY; + Status = gBS->SetTimer (Timer, TimerRelative, GetMappingTimeOut); + if (EFI_ERROR (Status)) { + gBS->CloseEvent (Timer); + Dhcp6->Configure (Dhcp6, NULL); + return Status; + } + + do { + + TimerStatus = gBS->CheckEvent (Timer); + if (!EFI_ERROR (TimerStatus)) { + Status = Dhcp6->Start (Dhcp6); + } + } while (TimerStatus == EFI_NOT_READY); + + gBS->CloseEvent (Timer); + } + if (EFI_ERROR (Status)) { + if (Status == EFI_ICMP_ERROR) { + PxeMode->IcmpErrorReceived = TRUE; + } + Dhcp6->Configure (Dhcp6, NULL); + return Status; + } + + // + // Get the acquired IPv6 address and store them. + // + Status = Dhcp6->GetModeData (Dhcp6, &Mode, NULL); + if (EFI_ERROR (Status)) { + Dhcp6->Stop (Dhcp6); + return Status; + } + + ASSERT ((Mode.Ia != NULL) && (Mode.Ia->State == Dhcp6Bound)); + // + // DHCP6 doesn't have an option to specify the router address on the subnet, the only way to get the + // router address in IP6 is the router discovery mechanism (the RS and RA, which only be handled when + // the IP policy is Automatic). So we just hold the station IP address here and leave the IP policy as + // Automatic, until we get the server IP address. This could let IP6 driver finish the router discovery + // to find a valid router address. + // + CopyMem (&Private->TmpStationIp.v6, &Mode.Ia->IaAddress[0].IpAddress, sizeof (EFI_IPv6_ADDRESS)); + if (Mode.ClientId != NULL) { + FreePool (Mode.ClientId); + } + if (Mode.Ia != NULL) { + FreePool (Mode.Ia); + } + // + // Check the selected offer whether BINL retry is needed. + // + Status = PxeBcHandleDhcp6Offer (Private); + if (EFI_ERROR (Status)) { + Dhcp6->Stop (Dhcp6); + return Status; + } + + return EFI_SUCCESS; +} diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.h b/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.h new file mode 100644 index 000000000..ca40fdc9d --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.h @@ -0,0 +1,269 @@ +/** @file + Functions declaration related with DHCPv6 for UefiPxeBc Driver. + + Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_PXEBC_DHCP6_H__ +#define __EFI_PXEBC_DHCP6_H__ + +#define PXEBC_DHCP6_OPTION_MAX_NUM 16 +#define PXEBC_DHCP6_OPTION_MAX_SIZE 312 +#define PXEBC_DHCP6_PACKET_MAX_SIZE (sizeof (EFI_PXE_BASE_CODE_PACKET)) +#define PXEBC_IP6_POLICY_MAX 0xff +#define PXEBC_IP6_ROUTE_TABLE_TIMEOUT 10 + +#define PXEBC_DHCP6_S_PORT 547 +#define PXEBC_DHCP6_C_PORT 546 + +#define PXEBC_DHCP6_ENTERPRISE_NUM 343 // TODO: IANA TBD: temporarily using Intel's +#define PXEBC_DHCP6_MAX_BOOT_FILE_SIZE 65535 // It's a limitation of bit length, 65535*512 bytes. + + +#define PXEBC_DHCP6_IDX_IA_NA 0 +#define PXEBC_DHCP6_IDX_BOOT_FILE_URL 1 +#define PXEBC_DHCP6_IDX_BOOT_FILE_PARAM 2 +#define PXEBC_DHCP6_IDX_VENDOR_CLASS 3 +#define PXEBC_DHCP6_IDX_DNS_SERVER 4 +#define PXEBC_DHCP6_IDX_MAX 5 + +#define PXEBC_DHCP6_BOOT_FILE_URL_PREFIX "tftp://" +#define PXEBC_TFTP_URL_SEPARATOR '/' +#define PXEBC_ADDR_START_DELIMITER '[' +#define PXEBC_ADDR_END_DELIMITER ']' + +#define GET_NEXT_DHCP6_OPTION(Opt) \ + (EFI_DHCP6_PACKET_OPTION *) ((UINT8 *) (Opt) + \ + sizeof (EFI_DHCP6_PACKET_OPTION) + (NTOHS ((Opt)->OpLen)) - 1) + +#define GET_DHCP6_OPTION_SIZE(Pkt) \ + ((Pkt)->Length - sizeof (EFI_DHCP6_HEADER)) + +#define IS_PROXY_OFFER(Type) \ + ((Type) == PxeOfferTypeProxyBinl || \ + (Type) == PxeOfferTypeProxyPxe10 || \ + (Type) == PxeOfferTypeProxyWfm11a) + + +#pragma pack(1) +typedef struct { + UINT16 OpCode[256]; +} PXEBC_DHCP6_OPTION_ORO; + +typedef struct { + UINT8 Type; + UINT8 MajorVer; + UINT8 MinorVer; +} PXEBC_DHCP6_OPTION_UNDI; + +typedef struct { + UINT16 Type; +} PXEBC_DHCP6_OPTION_ARCH; + +typedef struct { + UINT8 ClassIdentifier[10]; + UINT8 ArchitecturePrefix[5]; + UINT8 ArchitectureType[5]; + UINT8 Lit3[1]; + UINT8 InterfaceName[4]; + UINT8 Lit4[1]; + UINT8 UndiMajor[3]; + UINT8 UndiMinor[3]; +} PXEBC_CLASS_ID; + +typedef struct { + UINT32 Vendor; + UINT16 ClassLen; + PXEBC_CLASS_ID ClassId; +} PXEBC_DHCP6_OPTION_VENDOR_CLASS; + +#pragma pack() + +typedef union { + PXEBC_DHCP6_OPTION_ORO *Oro; + PXEBC_DHCP6_OPTION_UNDI *Undi; + PXEBC_DHCP6_OPTION_ARCH *Arch; + PXEBC_DHCP6_OPTION_VENDOR_CLASS *VendorClass; +} PXEBC_DHCP6_OPTION_ENTRY; + +typedef struct { + LIST_ENTRY Link; + EFI_DHCP6_PACKET_OPTION *Option; + UINT8 Precedence; +} PXEBC_DHCP6_OPTION_NODE; + +#define PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE (OFFSET_OF (EFI_DHCP6_PACKET, Dhcp6) + PXEBC_DHCP6_PACKET_MAX_SIZE) + +typedef union { + EFI_DHCP6_PACKET Offer; + EFI_DHCP6_PACKET Ack; + UINT8 Buffer[PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE]; +} PXEBC_DHCP6_PACKET; + +typedef struct { + PXEBC_DHCP6_PACKET Packet; + PXEBC_OFFER_TYPE OfferType; + EFI_DHCP6_PACKET_OPTION *OptList[PXEBC_DHCP6_IDX_MAX]; +} PXEBC_DHCP6_PACKET_CACHE; + + + + +/** + Parse the Boot File URL option. + + @param[in] Private Pointer to PxeBc private data. + @param[out] FileName The pointer to the boot file name. + @param[in, out] SrvAddr The pointer to the boot server address. + @param[in] BootFile The pointer to the boot file URL option data. + @param[in] Length Length of the boot file URL option data. + + @retval EFI_ABORTED User canceled the operation. + @retval EFI_SUCCESS Selected the boot menu successfully. + @retval EFI_NOT_READY Read the input key from the keybroad has not finish. + +**/ +EFI_STATUS +PxeBcExtractBootFileUrl ( + IN PXEBC_PRIVATE_DATA *Private, + OUT UINT8 **FileName, + IN OUT EFI_IPv6_ADDRESS *SrvAddr, + IN CHAR8 *BootFile, + IN UINT16 Length + ); + + +/** + Parse the Boot File Parameter option. + + @param[in] BootFilePara The pointer to the boot file parameter option data. + @param[out] BootFileSize The pointer to the parsed boot file size. + + @retval EFI_SUCCESS Successfully obtained the boot file size from parameter option. + @retval EFI_NOT_FOUND Failed to extract the boot file size from parameter option. + +**/ +EFI_STATUS +PxeBcExtractBootFileParam ( + IN CHAR8 *BootFilePara, + OUT UINT16 *BootFileSize + ); + + +/** + Parse the cached DHCPv6 packet, including all the options. + + @param[in] Cache6 The pointer to a cached DHCPv6 packet. + + @retval EFI_SUCCESS Parsed the DHCPv6 packet successfully. + @retval EFI_DEVICE_ERROR Failed to parse and invalid packet. + +**/ +EFI_STATUS +PxeBcParseDhcp6Packet ( + IN PXEBC_DHCP6_PACKET_CACHE *Cache6 + ); + + +/** + Register the ready address by Ip6Config protocol. + + @param[in] Private The pointer to the PxeBc private data. + @param[in] Address The pointer to the ready address. + + @retval EFI_SUCCESS Registered the address succesfully. + @retval Others Failed to register the address. + +**/ +EFI_STATUS +PxeBcRegisterIp6Address ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_IPv6_ADDRESS *Address + ); + + +/** + Unregister the address by Ip6Config protocol. + + @param[in] Private The pointer to the PxeBc private data. + +**/ +VOID +PxeBcUnregisterIp6Address ( + IN PXEBC_PRIVATE_DATA *Private + ); + + +/** + Build and send out the request packet for the bootfile, and parse the reply. + + @param[in] Private The pointer to the PxeBc private data. + @param[in] Type PxeBc option boot item type. + @param[in] Layer The pointer to the option boot item layer. + @param[in] UseBis Use BIS or not. + @param[in] DestIp The pointer to the server address. + + @retval EFI_SUCCESS Successfully discovered theboot file. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource. + @retval EFI_NOT_FOUND Can't get the PXE reply packet. + @retval Others Failed to discover boot file. + +**/ +EFI_STATUS +PxeBcDhcp6Discover ( + IN PXEBC_PRIVATE_DATA *Private, + IN UINT16 Type, + IN UINT16 *Layer, + IN BOOLEAN UseBis, + IN EFI_IP_ADDRESS *DestIp + ); + +/** + Set the IP6 policy to Automatic. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + + @retval EFI_SUCCESS Switch the IP policy succesfully. + @retval Others Unexpect error happened. + +**/ +EFI_STATUS +PxeBcSetIp6Policy ( + IN PXEBC_PRIVATE_DATA *Private + ); + +/** + This function will register the station IP address and flush IP instance to start using the new IP address. + + @param[in] Private The pointer to PXEBC_PRIVATE_DATA. + + @retval EFI_SUCCESS The new IP address has been configured successfully. + @retval Others Failed to configure the address. + +**/ +EFI_STATUS +PxeBcSetIp6Address ( + IN PXEBC_PRIVATE_DATA *Private + ); + +/** + Start the DHCPv6 S.A.R.R. process to acquire the IPv6 address and other PXE boot information. + + @param[in] Private The pointer to the PxeBc private data. + @param[in] Dhcp6 The pointer to EFI_DHCP6_PROTOCOL. + + @retval EFI_SUCCESS The S.A.R.R. process successfully finished. + @retval Others Failed to finish the S.A.R.R. process. + +**/ +EFI_STATUS +PxeBcDhcp6Sarr ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_DHCP6_PROTOCOL *Dhcp6 + ); + +#endif + diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcDriver.c b/NetworkPkg/UefiPxeBcDxe/PxeBcDriver.c new file mode 100644 index 000000000..b35edb687 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcDriver.c @@ -0,0 +1,1851 @@ +/** @file + Driver Binding functions implementationfor for UefiPxeBc Driver. + + (C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "PxeBcImpl.h" + + +EFI_DRIVER_BINDING_PROTOCOL gPxeBcIp4DriverBinding = { + PxeBcIp4DriverBindingSupported, + PxeBcIp4DriverBindingStart, + PxeBcIp4DriverBindingStop, + 0xa, + NULL, + NULL +}; + +EFI_DRIVER_BINDING_PROTOCOL gPxeBcIp6DriverBinding = { + PxeBcIp6DriverBindingSupported, + PxeBcIp6DriverBindingStart, + PxeBcIp6DriverBindingStop, + 0xa, + NULL, + NULL +}; + +/** + Get the Nic handle using any child handle in the IPv4 stack. + + @param[in] ControllerHandle Pointer to child handle over IPv4. + + @return NicHandle The pointer to the Nic handle. + +**/ +EFI_HANDLE +PxeBcGetNicByIp4Children ( + IN EFI_HANDLE ControllerHandle + ) +{ + EFI_HANDLE NicHandle; + + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiIp4ProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp4ProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp4ProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiMtftp4ProtocolGuid); + if (NicHandle == NULL) { + return NULL; + } + } + } + } + } + + return NicHandle; +} + + +/** + Get the Nic handle using any child handle in the IPv6 stack. + + @param[in] ControllerHandle Pointer to child handle over IPv6. + + @return NicHandle The pointer to the Nic handle. + +**/ +EFI_HANDLE +PxeBcGetNicByIp6Children ( + IN EFI_HANDLE ControllerHandle + ) +{ + EFI_HANDLE NicHandle; + + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiIp6ProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp6ProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp6ProtocolGuid); + if (NicHandle == NULL) { + NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiMtftp6ProtocolGuid); + if (NicHandle == NULL) { + return NULL; + } + } + } + } + + return NicHandle; +} + + +/** + Destroy the opened instances based on IPv4. + + @param[in] This Pointer to the EFI_DRIVER_BINDING_PROTOCOL. + @param[in] Private Pointer to PXEBC_PRIVATE_DATA. + +**/ +VOID +PxeBcDestroyIp4Children ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN PXEBC_PRIVATE_DATA *Private + ) +{ + ASSERT(Private != NULL); + + if (Private->ArpChild != NULL) { + // + // Close Arp for PxeBc->Arp and destroy the instance. + // + gBS->CloseProtocol ( + Private->ArpChild, + &gEfiArpProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiArpServiceBindingProtocolGuid, + Private->ArpChild + ); + } + + if (Private->Ip4Child != NULL) { + // + // Close Ip4 for background ICMP error message and destroy the instance. + // + gBS->CloseProtocol ( + Private->Ip4Child, + &gEfiIp4ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiIp4ServiceBindingProtocolGuid, + Private->Ip4Child + ); + } + + if (Private->Udp4WriteChild != NULL) { + // + // Close Udp4 for PxeBc->UdpWrite and destroy the instance. + // + gBS->CloseProtocol ( + Private->Udp4WriteChild, + &gEfiUdp4ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + Private->Udp4WriteChild + ); + } + + if (Private->Udp4ReadChild != NULL) { + // + // Close Udp4 for PxeBc->UdpRead and destroy the instance. + // + gBS->CloseProtocol ( + Private->Udp4ReadChild, + &gEfiUdp4ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + Private->Udp4ReadChild + ); + } + + if (Private->Mtftp4Child != NULL) { + // + // Close Mtftp4 for PxeBc->Mtftp4 and destroy the instance. + // + gBS->CloseProtocol ( + Private->Mtftp4Child, + &gEfiMtftp4ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiMtftp4ServiceBindingProtocolGuid, + Private->Mtftp4Child + ); + } + + if (Private->Dhcp4Child != NULL) { + // + // Close Dhcp4 for PxeBc->Dhcp4 and destroy the instance. + // + gBS->CloseProtocol ( + Private->Dhcp4Child, + &gEfiDhcp4ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiDhcp4ServiceBindingProtocolGuid, + Private->Dhcp4Child + ); + } + + if (Private->Ip4Nic != NULL) { + // + // Close PxeBcPrivate from the parent Nic handle and destroy the virtual handle. + // + gBS->CloseProtocol ( + Private->Controller, + &gEfiCallerIdGuid, + This->DriverBindingHandle, + Private->Ip4Nic->Controller + ); + + gBS->UninstallMultipleProtocolInterfaces ( + Private->Ip4Nic->Controller, + &gEfiDevicePathProtocolGuid, + Private->Ip4Nic->DevicePath, + &gEfiLoadFileProtocolGuid, + &Private->Ip4Nic->LoadFile, + &gEfiPxeBaseCodeProtocolGuid, + &Private->PxeBc, + NULL + ); + FreePool (Private->Ip4Nic->DevicePath); + + if (Private->Snp != NULL) { + // + // Close SNP from the child virtual handle + // + gBS->CloseProtocol ( + Private->Ip4Nic->Controller, + &gEfiSimpleNetworkProtocolGuid, + This->DriverBindingHandle, + Private->Ip4Nic->Controller + ); + + gBS->UninstallProtocolInterface ( + Private->Ip4Nic->Controller, + &gEfiSimpleNetworkProtocolGuid, + Private->Snp + ); + } + FreePool (Private->Ip4Nic); + } + + Private->ArpChild = NULL; + Private->Ip4Child = NULL; + Private->Udp4WriteChild = NULL; + Private->Udp4ReadChild = NULL; + Private->Mtftp4Child = NULL; + Private->Dhcp4Child = NULL; + Private->Ip4Nic = NULL; +} + + +/** + Destroy the opened instances based on IPv6. + + @param[in] This Pointer to the EFI_DRIVER_BINDING_PROTOCOL. + @param[in] Private Pointer to PXEBC_PRIVATE_DATA. + +**/ +VOID +PxeBcDestroyIp6Children ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN PXEBC_PRIVATE_DATA *Private + ) +{ + ASSERT(Private != NULL); + + if (Private->Ip6Child != NULL) { + // + // Close Ip6 for Ip6->Ip6Config and destroy the instance. + // + gBS->CloseProtocol ( + Private->Ip6Child, + &gEfiIp6ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiIp6ServiceBindingProtocolGuid, + Private->Ip6Child + ); + } + + if (Private->Udp6WriteChild != NULL) { + // + // Close Udp6 for PxeBc->UdpWrite and destroy the instance. + // + gBS->CloseProtocol ( + Private->Udp6WriteChild, + &gEfiUdp6ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + Private->Udp6WriteChild + ); + } + + if (Private->Udp6ReadChild != NULL) { + // + // Close Udp6 for PxeBc->UdpRead and destroy the instance. + // + gBS->CloseProtocol ( + Private->Udp6ReadChild, + &gEfiUdp6ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + Private->Udp6ReadChild + ); + } + + if (Private->Mtftp6Child != NULL) { + // + // Close Mtftp6 for PxeBc->Mtftp and destroy the instance. + // + gBS->CloseProtocol ( + Private->Mtftp6Child, + &gEfiMtftp6ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiMtftp6ServiceBindingProtocolGuid, + Private->Mtftp6Child + ); + } + + if (Private->Dhcp6Child != NULL) { + // + // Close Dhcp6 for PxeBc->Dhcp and destroy the instance. + // + gBS->CloseProtocol ( + Private->Dhcp6Child, + &gEfiDhcp6ProtocolGuid, + This->DriverBindingHandle, + Private->Controller + ); + + NetLibDestroyServiceChild ( + Private->Controller, + This->DriverBindingHandle, + &gEfiDhcp6ServiceBindingProtocolGuid, + Private->Dhcp6Child + ); + } + + if (Private->Ip6Nic != NULL) { + // + // Close PxeBcPrivate from the parent Nic handle and destroy the virtual handle. + // + gBS->CloseProtocol ( + Private->Controller, + &gEfiCallerIdGuid, + This->DriverBindingHandle, + Private->Ip6Nic->Controller + ); + + gBS->UninstallMultipleProtocolInterfaces ( + Private->Ip6Nic->Controller, + &gEfiDevicePathProtocolGuid, + Private->Ip6Nic->DevicePath, + &gEfiLoadFileProtocolGuid, + &Private->Ip6Nic->LoadFile, + &gEfiPxeBaseCodeProtocolGuid, + &Private->PxeBc, + NULL + ); + FreePool (Private->Ip6Nic->DevicePath); + + if (Private->Snp != NULL) { + // + // Close SNP from the child virtual handle + // + gBS->CloseProtocol ( + Private->Ip6Nic->Controller, + &gEfiSimpleNetworkProtocolGuid, + This->DriverBindingHandle, + Private->Ip6Nic->Controller + ); + gBS->UninstallProtocolInterface ( + Private->Ip6Nic->Controller, + &gEfiSimpleNetworkProtocolGuid, + Private->Snp + ); + } + FreePool (Private->Ip6Nic); + } + + Private->Ip6Child = NULL; + Private->Udp6WriteChild = NULL; + Private->Udp6ReadChild = NULL; + Private->Mtftp6Child = NULL; + Private->Dhcp6Child = NULL; + Private->Ip6Nic = NULL; + Private->Mode.Ipv6Available = FALSE; +} + +/** + Check whether UNDI protocol supports IPv6. + + @param[in] ControllerHandle Controller handle. + @param[in] Private Pointer to PXEBC_PRIVATE_DATA. + @param[out] Ipv6Support TRUE if UNDI supports IPv6. + + @retval EFI_SUCCESS Get the result whether UNDI supports IPv6 by NII or AIP protocol successfully. + @retval EFI_NOT_FOUND Don't know whether UNDI supports IPv6 since NII or AIP is not available. + +**/ +EFI_STATUS +PxeBcCheckIpv6Support ( + IN EFI_HANDLE ControllerHandle, + IN PXEBC_PRIVATE_DATA *Private, + OUT BOOLEAN *Ipv6Support + ) +{ + EFI_HANDLE Handle; + EFI_ADAPTER_INFORMATION_PROTOCOL *Aip; + EFI_STATUS Status; + EFI_GUID *InfoTypesBuffer; + UINTN InfoTypeBufferCount; + UINTN TypeIndex; + BOOLEAN Supported; + VOID *InfoBlock; + UINTN InfoBlockSize; + + ASSERT (Private != NULL && Ipv6Support != NULL); + + // + // Check whether the UNDI supports IPv6 by NII protocol. + // + if (Private->Nii != NULL) { + *Ipv6Support = Private->Nii->Ipv6Supported; + return EFI_SUCCESS; + } + + // + // Check whether the UNDI supports IPv6 by AIP protocol. + // + + // + // Get the NIC handle by SNP protocol. + // + Handle = NetLibGetSnpHandle (ControllerHandle, NULL); + if (Handle == NULL) { + return EFI_NOT_FOUND; + } + + Aip = NULL; + Status = gBS->HandleProtocol ( + Handle, + &gEfiAdapterInformationProtocolGuid, + (VOID *) &Aip + ); + if (EFI_ERROR (Status) || Aip == NULL) { + return EFI_NOT_FOUND; + } + + InfoTypesBuffer = NULL; + InfoTypeBufferCount = 0; + Status = Aip->GetSupportedTypes (Aip, &InfoTypesBuffer, &InfoTypeBufferCount); + if (EFI_ERROR (Status) || InfoTypesBuffer == NULL) { + FreePool (InfoTypesBuffer); + return EFI_NOT_FOUND; + } + + Supported = FALSE; + for (TypeIndex = 0; TypeIndex < InfoTypeBufferCount; TypeIndex++) { + if (CompareGuid (&InfoTypesBuffer[TypeIndex], &gEfiAdapterInfoUndiIpv6SupportGuid)) { + Supported = TRUE; + break; + } + } + + FreePool (InfoTypesBuffer); + if (!Supported) { + return EFI_NOT_FOUND; + } + + // + // We now have adapter information block. + // + InfoBlock = NULL; + InfoBlockSize = 0; + Status = Aip->GetInformation (Aip, &gEfiAdapterInfoUndiIpv6SupportGuid, &InfoBlock, &InfoBlockSize); + if (EFI_ERROR (Status) || InfoBlock == NULL) { + FreePool (InfoBlock); + return EFI_NOT_FOUND; + } + + *Ipv6Support = ((EFI_ADAPTER_INFO_UNDI_IPV6_SUPPORT *) InfoBlock)->Ipv6Support; + FreePool (InfoBlock); + return EFI_SUCCESS; + +} + +/** + Create the opened instances based on IPv4. + + @param[in] This Pointer to EFI_DRIVER_BINDING_PROTOCOL. + @param[in] ControllerHandle Handle of the child to destroy. + @param[in] Private Handle Pointer to PXEBC_PRIVATE_DATA. + + @retval EFI_SUCCESS The instances based on IPv4 were all created successfully. + @retval Others An unexpected error occurred. + +**/ +EFI_STATUS +PxeBcCreateIp4Children ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN PXEBC_PRIVATE_DATA *Private + ) +{ + EFI_STATUS Status; + IPv4_DEVICE_PATH Ip4Node; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_UDP4_CONFIG_DATA *Udp4CfgData; + EFI_IP4_CONFIG_DATA *Ip4CfgData; + EFI_IP4_MODE_DATA Ip4ModeData; + PXEBC_PRIVATE_PROTOCOL *Id; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + + if (Private->Ip4Nic != NULL) { + // + // Already created before. + // + return EFI_SUCCESS; + } + + // + // Create Dhcp4 child and open Dhcp4 protocol for PxeBc->Dhcp. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiDhcp4ServiceBindingProtocolGuid, + &Private->Dhcp4Child + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Dhcp4Child, + &gEfiDhcp4ProtocolGuid, + (VOID **) &Private->Dhcp4, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create Mtftp4 child and open Mtftp4 protocol for PxeBc->Mtftp. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiMtftp4ServiceBindingProtocolGuid, + &Private->Mtftp4Child + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Mtftp4Child, + &gEfiMtftp4ProtocolGuid, + (VOID **) &Private->Mtftp4, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create Udp4 child and open Udp4 protocol for PxeBc->UdpRead. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + &Private->Udp4ReadChild + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Udp4ReadChild, + &gEfiUdp4ProtocolGuid, + (VOID **) &Private->Udp4Read, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create Udp4 child and open Udp4 protocol for PxeBc->UdpWrite. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiUdp4ServiceBindingProtocolGuid, + &Private->Udp4WriteChild + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Udp4WriteChild, + &gEfiUdp4ProtocolGuid, + (VOID **) &Private->Udp4Write, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create Arp child and open Arp protocol for PxeBc->Arp. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiArpServiceBindingProtocolGuid, + &Private->ArpChild + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->ArpChild, + &gEfiArpProtocolGuid, + (VOID **) &Private->Arp, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create Ip4 child and open Ip4 protocol for background ICMP packets. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiIp4ServiceBindingProtocolGuid, + &Private->Ip4Child + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Ip4Child, + &gEfiIp4ProtocolGuid, + (VOID **) &Private->Ip4, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Get max packet size from Ip4 to calculate block size for Tftp later. + // + Status = Private->Ip4->GetModeData (Private->Ip4, &Ip4ModeData, NULL, NULL); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Private->Ip4MaxPacketSize = Ip4ModeData.MaxPacketSize; + + Private->Ip4Nic = AllocateZeroPool (sizeof (PXEBC_VIRTUAL_NIC)); + if (Private->Ip4Nic == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Private->Ip4Nic->Private = Private; + Private->Ip4Nic->Signature = PXEBC_VIRTUAL_NIC_SIGNATURE; + + // + // Locate Ip4->Ip4Config2 and store it for set IPv4 Policy. + // + Status = gBS->HandleProtocol ( + ControllerHandle, + &gEfiIp4Config2ProtocolGuid, + (VOID **) &Private->Ip4Config2 + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create a device path node for Ipv4 virtual nic, and append it. + // + ZeroMem (&Ip4Node, sizeof (IPv4_DEVICE_PATH)); + Ip4Node.Header.Type = MESSAGING_DEVICE_PATH; + Ip4Node.Header.SubType = MSG_IPv4_DP; + Ip4Node.StaticIpAddress = FALSE; + + SetDevicePathNodeLength (&Ip4Node.Header, sizeof (Ip4Node)); + + Private->Ip4Nic->DevicePath = AppendDevicePathNode (Private->DevicePath, &Ip4Node.Header); + + if (Private->Ip4Nic->DevicePath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + CopyMem ( + &Private->Ip4Nic->LoadFile, + &gLoadFileProtocolTemplate, + sizeof (EFI_LOAD_FILE_PROTOCOL) + ); + + // + // Create a new handle for IPv4 virtual nic, + // and install PxeBaseCode, LoadFile and DevicePath protocols. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &Private->Ip4Nic->Controller, + &gEfiDevicePathProtocolGuid, + Private->Ip4Nic->DevicePath, + &gEfiLoadFileProtocolGuid, + &Private->Ip4Nic->LoadFile, + &gEfiPxeBaseCodeProtocolGuid, + &Private->PxeBc, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + if (Private->Snp != NULL) { + // + // Install SNP protocol on purpose is for some OS loader backward + // compatibility consideration. + // + Status = gBS->InstallProtocolInterface ( + &Private->Ip4Nic->Controller, + &gEfiSimpleNetworkProtocolGuid, + EFI_NATIVE_INTERFACE, + Private->Snp + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Open SNP on the child handle BY_DRIVER|EXCLUSIVE. It will prevent any additionally + // layering to perform the experiment. + // + Status = gBS->OpenProtocol ( + Private->Ip4Nic->Controller, + &gEfiSimpleNetworkProtocolGuid, + (VOID **) &Snp, + This->DriverBindingHandle, + Private->Ip4Nic->Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER|EFI_OPEN_PROTOCOL_EXCLUSIVE + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + } + + // + // Open PxeBaseCodePrivate protocol by child to setup a parent-child relationship between + // real NIC handle and the virtual IPv4 NIC handle. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + This->DriverBindingHandle, + Private->Ip4Nic->Controller, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Set default configure data for Udp4Read and Ip4 instance. + // + Mode = Private->PxeBc.Mode; + Udp4CfgData = &Private->Udp4CfgData; + Ip4CfgData = &Private->Ip4CfgData; + + Udp4CfgData->AcceptBroadcast = FALSE; + Udp4CfgData->AcceptAnyPort = TRUE; + Udp4CfgData->AllowDuplicatePort = TRUE; + Udp4CfgData->TypeOfService = Mode->ToS; + Udp4CfgData->TimeToLive = Mode->TTL; + Udp4CfgData->ReceiveTimeout = PXEBC_DEFAULT_LIFETIME; + Udp4CfgData->TransmitTimeout = PXEBC_DEFAULT_LIFETIME; + + Ip4CfgData->AcceptIcmpErrors = TRUE; + Ip4CfgData->DefaultProtocol = EFI_IP_PROTO_ICMP; + Ip4CfgData->TypeOfService = Mode->ToS; + Ip4CfgData->TimeToLive = Mode->TTL; + Ip4CfgData->ReceiveTimeout = PXEBC_DEFAULT_LIFETIME; + Ip4CfgData->TransmitTimeout = PXEBC_DEFAULT_LIFETIME; + + return EFI_SUCCESS; + +ON_ERROR: + PxeBcDestroyIp4Children (This, Private); + return Status; +} + + +/** + Create the opened instances based on IPv6. + + @param[in] This Pointer to EFI_DRIVER_BINDING_PROTOCOL. + @param[in] ControllerHandle Handle of the child to destroy. + @param[in] Private Handle Pointer to PXEBC_PRIVATE_DATA. + + @retval EFI_SUCCESS The instances based on IPv6 were all created successfully. + @retval Others An unexpected error occurred. + +**/ +EFI_STATUS +PxeBcCreateIp6Children ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN PXEBC_PRIVATE_DATA *Private + ) +{ + EFI_STATUS Status; + IPv6_DEVICE_PATH Ip6Node; + EFI_UDP6_CONFIG_DATA *Udp6CfgData; + EFI_IP6_CONFIG_DATA *Ip6CfgData; + EFI_IP6_MODE_DATA Ip6ModeData; + PXEBC_PRIVATE_PROTOCOL *Id; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + UINTN Index; + + if (Private->Ip6Nic != NULL) { + // + // Already created before. + // + return EFI_SUCCESS; + } + + Private->Ip6Nic = AllocateZeroPool (sizeof (PXEBC_VIRTUAL_NIC)); + + if (Private->Ip6Nic == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Private->Ip6Nic->Private = Private; + Private->Ip6Nic->Signature = PXEBC_VIRTUAL_NIC_SIGNATURE; + + // + // Create Dhcp6 child and open Dhcp6 protocol for PxeBc->Dhcp. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiDhcp6ServiceBindingProtocolGuid, + &Private->Dhcp6Child + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Dhcp6Child, + &gEfiDhcp6ProtocolGuid, + (VOID **) &Private->Dhcp6, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Generate a random IAID for the Dhcp6 assigned address. + // + Private->IaId = NET_RANDOM (NetRandomInitSeed ()); + if (Private->Snp != NULL) { + for (Index = 0; Index < Private->Snp->Mode->HwAddressSize; Index++) { + Private->IaId |= (Private->Snp->Mode->CurrentAddress.Addr[Index] << ((Index << 3) & 31)); + } + } + + // + // Create Mtftp6 child and open Mtftp6 protocol for PxeBc->Mtftp. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiMtftp6ServiceBindingProtocolGuid, + &Private->Mtftp6Child + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Mtftp6Child, + &gEfiMtftp6ProtocolGuid, + (VOID **) &Private->Mtftp6, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create Udp6 child and open Udp6 protocol for PxeBc->UdpRead. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + &Private->Udp6ReadChild + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Udp6ReadChild, + &gEfiUdp6ProtocolGuid, + (VOID **) &Private->Udp6Read, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create Udp6 child and open Udp6 protocol for PxeBc->UdpWrite. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiUdp6ServiceBindingProtocolGuid, + &Private->Udp6WriteChild + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Udp6WriteChild, + &gEfiUdp6ProtocolGuid, + (VOID **) &Private->Udp6Write, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create Ip6 child and open Ip6 protocol for background ICMP6 packets. + // + Status = NetLibCreateServiceChild ( + ControllerHandle, + This->DriverBindingHandle, + &gEfiIp6ServiceBindingProtocolGuid, + &Private->Ip6Child + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Status = gBS->OpenProtocol ( + Private->Ip6Child, + &gEfiIp6ProtocolGuid, + (VOID **) &Private->Ip6, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Get max packet size from Ip6 to calculate block size for Tftp later. + // + Status = Private->Ip6->GetModeData (Private->Ip6, &Ip6ModeData, NULL, NULL); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Private->Ip6MaxPacketSize = Ip6ModeData.MaxPacketSize; + + if (Ip6ModeData.AddressList != NULL) { + FreePool (Ip6ModeData.AddressList); + } + + if (Ip6ModeData.GroupTable != NULL) { + FreePool (Ip6ModeData.GroupTable); + } + + if (Ip6ModeData.RouteTable != NULL) { + FreePool (Ip6ModeData.RouteTable); + } + + if (Ip6ModeData.NeighborCache != NULL) { + FreePool (Ip6ModeData.NeighborCache); + } + + if (Ip6ModeData.PrefixTable != NULL) { + FreePool (Ip6ModeData.PrefixTable); + } + + if (Ip6ModeData.IcmpTypeList != NULL) { + FreePool (Ip6ModeData.IcmpTypeList); + } + + // + // Locate Ip6->Ip6Config and store it for set IPv6 address. + // + Status = gBS->HandleProtocol ( + ControllerHandle, + &gEfiIp6ConfigProtocolGuid, + (VOID **) &Private->Ip6Cfg + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create a device path node for Ipv6 virtual nic, and append it. + // + ZeroMem (&Ip6Node, sizeof (IPv6_DEVICE_PATH)); + Ip6Node.Header.Type = MESSAGING_DEVICE_PATH; + Ip6Node.Header.SubType = MSG_IPv6_DP; + Ip6Node.PrefixLength = IP6_PREFIX_LENGTH; + + SetDevicePathNodeLength (&Ip6Node.Header, sizeof (Ip6Node)); + + Private->Ip6Nic->DevicePath = AppendDevicePathNode (Private->DevicePath, &Ip6Node.Header); + + if (Private->Ip6Nic->DevicePath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ON_ERROR; + } + + CopyMem ( + &Private->Ip6Nic->LoadFile, + &gLoadFileProtocolTemplate, + sizeof (EFI_LOAD_FILE_PROTOCOL) + ); + + // + // Create a new handle for IPv6 virtual nic, + // and install PxeBaseCode, LoadFile and DevicePath protocols. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &Private->Ip6Nic->Controller, + &gEfiDevicePathProtocolGuid, + Private->Ip6Nic->DevicePath, + &gEfiLoadFileProtocolGuid, + &Private->Ip6Nic->LoadFile, + &gEfiPxeBaseCodeProtocolGuid, + &Private->PxeBc, + NULL + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + if (Private->Snp != NULL) { + // + // Install SNP protocol on purpose is for some OS loader backward + // compatibility consideration. + // + Status = gBS->InstallProtocolInterface ( + &Private->Ip6Nic->Controller, + &gEfiSimpleNetworkProtocolGuid, + EFI_NATIVE_INTERFACE, + Private->Snp + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Open SNP on the child handle BY_DRIVER|EXCLUSIVE. It will prevent any additionally + // layering to perform the experiment. + // + Status = gBS->OpenProtocol ( + Private->Ip6Nic->Controller, + &gEfiSimpleNetworkProtocolGuid, + (VOID **) &Snp, + This->DriverBindingHandle, + Private->Ip6Nic->Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER|EFI_OPEN_PROTOCOL_EXCLUSIVE + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + } + + // + // Open PxeBaseCodePrivate protocol by child to setup a parent-child relationship between + // real NIC handle and the virtual IPv6 NIC handle. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + This->DriverBindingHandle, + Private->Ip6Nic->Controller, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Set IPv6 avaiable flag and set default configure data for + // Udp6Read and Ip6 instance. + // + Status = PxeBcCheckIpv6Support (ControllerHandle, Private, &Private->Mode.Ipv6Available); + if (EFI_ERROR (Status)) { + // + // Fail to get the data whether UNDI supports IPv6. Set default value. + // + Private->Mode.Ipv6Available = TRUE; + } + + if (!Private->Mode.Ipv6Available) { + goto ON_ERROR; + } + + Udp6CfgData = &Private->Udp6CfgData; + Ip6CfgData = &Private->Ip6CfgData; + + Udp6CfgData->AcceptAnyPort = TRUE; + Udp6CfgData->AllowDuplicatePort = TRUE; + Udp6CfgData->HopLimit = PXEBC_DEFAULT_HOPLIMIT; + Udp6CfgData->ReceiveTimeout = PXEBC_DEFAULT_LIFETIME; + Udp6CfgData->TransmitTimeout = PXEBC_DEFAULT_LIFETIME; + + Ip6CfgData->AcceptIcmpErrors = TRUE; + Ip6CfgData->DefaultProtocol = IP6_ICMP; + Ip6CfgData->HopLimit = PXEBC_DEFAULT_HOPLIMIT; + Ip6CfgData->ReceiveTimeout = PXEBC_DEFAULT_LIFETIME; + Ip6CfgData->TransmitTimeout = PXEBC_DEFAULT_LIFETIME; + + return EFI_SUCCESS; + +ON_ERROR: + PxeBcDestroyIp6Children (This, Private); + return Status; +} + + +/** + The entry point for UefiPxeBc driver that installs the driver + binding and component name protocol on its image. + + @param[in] ImageHandle The Image handle of the driver. + @param[in] SystemTable The system table. + + @return EFI_SUCCESS + @return Others + +**/ +EFI_STATUS +EFIAPI +PxeBcDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + if ((PcdGet8(PcdIPv4PXESupport) == PXE_DISABLED) && (PcdGet8(PcdIPv6PXESupport) == PXE_DISABLED)) { + return EFI_UNSUPPORTED; + } + + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gPxeBcIp4DriverBinding, + ImageHandle, + &gPxeBcComponentName, + &gPxeBcComponentName2 + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gPxeBcIp6DriverBinding, + NULL, + &gPxeBcComponentName, + &gPxeBcComponentName2 + ); + if (EFI_ERROR (Status)) { + EfiLibUninstallDriverBindingComponentName2 ( + &gPxeBcIp4DriverBinding, + &gPxeBcComponentName, + &gPxeBcComponentName2 + ); + } + + return Status; +} + +/** + Test to see if this driver supports ControllerHandle. This is the worker function for + PxeBcIp4(6)DriverBindingSupported. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be tested. + @param[in] RemainingDevicePath Optional parameter used to pick a specific child + device to be started. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL, + IN UINT8 IpVersion + ) +{ + EFI_STATUS Status; + EFI_GUID *DhcpServiceBindingGuid; + EFI_GUID *MtftpServiceBindingGuid; + + if (IpVersion == IP_VERSION_4) { + if (PcdGet8(PcdIPv4PXESupport) == PXE_DISABLED) { + return EFI_UNSUPPORTED; + } + DhcpServiceBindingGuid = &gEfiDhcp4ServiceBindingProtocolGuid; + MtftpServiceBindingGuid = &gEfiMtftp4ServiceBindingProtocolGuid; + } else { + if (PcdGet8(PcdIPv6PXESupport) == PXE_DISABLED) { + return EFI_UNSUPPORTED; + } + DhcpServiceBindingGuid = &gEfiDhcp6ServiceBindingProtocolGuid; + MtftpServiceBindingGuid = &gEfiMtftp6ServiceBindingProtocolGuid; + } + + // + // Try to open the Mtftp and Dhcp protocol to test whether IP stack is ready. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + DhcpServiceBindingGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + Status = gBS->OpenProtocol ( + ControllerHandle, + MtftpServiceBindingGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + } + + // + // It's unsupported case if IP stack are not ready. + // + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + return EFI_SUCCESS; +} + +/** + Start this driver on ControllerHandle. This is the worker function for + PxeBcIp4(6)DriverBindingStart. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be started. + @param[in] RemainingDevicePath Optional parameter used to pick a specific child + device to be started. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + + @retval EFI_SUCCESS This driver is installed to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL, + IN UINT8 IpVersion + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_STATUS Status; + PXEBC_PRIVATE_PROTOCOL *Id; + BOOLEAN FirstStart; + + FirstStart = FALSE; + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + // + // Skip the initialization if the driver has been started already. + // + Private = PXEBC_PRIVATE_DATA_FROM_ID (Id); + } else { + FirstStart = TRUE; + // + // If the driver has not been started yet, it should do initialization. + // + Private = AllocateZeroPool (sizeof (PXEBC_PRIVATE_DATA)); + if (Private == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + CopyMem ( + &Private->PxeBc, + &gPxeBcProtocolTemplate, + sizeof (EFI_PXE_BASE_CODE_PROTOCOL) + ); + + Private->Signature = PXEBC_PRIVATE_DATA_SIGNATURE; + Private->Controller = ControllerHandle; + Private->Image = This->ImageHandle; + Private->PxeBc.Mode = &Private->Mode; + Private->Mode.Ipv6Supported = TRUE; + Private->Mode.AutoArp = TRUE; + Private->Mode.TTL = DEFAULT_TTL; + Private->Mode.ToS = DEFAULT_ToS; + + // + // Open device path to prepare for appending virtual NIC node. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDevicePathProtocolGuid, + (VOID **) &Private->DevicePath, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Get the NII interface if it exists, it's not required. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiNetworkInterfaceIdentifierProtocolGuid_31, + (VOID **) &Private->Nii, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + Private->Nii = NULL; + } + + // + // Install PxeBaseCodePrivate protocol onto the real NIC handler. + // PxeBaseCodePrivate protocol is only used to keep the relationship between + // NIC handle and virtual child handles. + // gEfiCallerIdGuid will be used as its protocol guid. + // + Status = gBS->InstallProtocolInterface ( + &ControllerHandle, + &gEfiCallerIdGuid, + EFI_NATIVE_INTERFACE, + &Private->Id + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Try to locate SNP protocol. + // + NetLibGetSnpHandle(ControllerHandle, &Private->Snp); + } + + if (IpVersion == IP_VERSION_4) { + // + // Try to create virtual NIC handle for IPv4. + // + Status = PxeBcCreateIp4Children (This, ControllerHandle, Private); + } else { + // + // Try to create virtual NIC handle for IPv6. + // + Status = PxeBcCreateIp6Children (This, ControllerHandle, Private); + } + if (EFI_ERROR (Status)) { + // + // Failed to start PXE driver if IPv4 and IPv6 stack are both not available. + // + Status = EFI_DEVICE_ERROR; + goto ON_ERROR; + } + + return EFI_SUCCESS; + +ON_ERROR: + if (FirstStart) { + gBS->UninstallProtocolInterface ( + ControllerHandle, + &gEfiCallerIdGuid, + &Private->Id + ); + } + + if (IpVersion == IP_VERSION_4) { + PxeBcDestroyIp4Children (This, Private); + } else { + PxeBcDestroyIp6Children (This, Private); + } + + if (FirstStart && Private != NULL) { + FreePool (Private); + } + + return Status; +} + + +/** + Stop this driver on ControllerHandle. This is the worker function for + PxeBcIp4(6)DriverBindingStop. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on. + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6. + + @retval EFI_SUCCESS This driver was removed ControllerHandle. + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval Others This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +PxeBcStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer, + IN UINT8 IpVersion + ) +{ + PXEBC_PRIVATE_DATA *Private; + PXEBC_VIRTUAL_NIC *VirtualNic; + EFI_LOAD_FILE_PROTOCOL *LoadFile; + EFI_STATUS Status; + EFI_HANDLE NicHandle; + PXEBC_PRIVATE_PROTOCOL *Id; + + Private = NULL; + NicHandle = NULL; + VirtualNic = NULL; + LoadFile = NULL; + Id = NULL; + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiLoadFileProtocolGuid, + (VOID **) &LoadFile, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + // + // Get the Nic handle by any pass-over service child handle. + // + if (IpVersion == IP_VERSION_4) { + NicHandle = PxeBcGetNicByIp4Children (ControllerHandle); + } else { + NicHandle = PxeBcGetNicByIp6Children (ControllerHandle); + } + if (NicHandle == NULL) { + return EFI_SUCCESS; + } + + // + // Try to retrieve the private data by PxeBcPrivate protocol. + // + Status = gBS->OpenProtocol ( + NicHandle, + &gEfiCallerIdGuid, + (VOID **) &Id, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + Private = PXEBC_PRIVATE_DATA_FROM_ID (Id); + + } else { + // + // It's a virtual handle with LoadFileProtocol. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiLoadFileProtocolGuid, + (VOID **) &LoadFile, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + VirtualNic = PXEBC_VIRTUAL_NIC_FROM_LOADFILE (LoadFile); + Private = VirtualNic->Private; + NicHandle = Private->Controller; + } + + // + // Stop functionality of PXE Base Code protocol + // + Status = Private->PxeBc.Stop (&Private->PxeBc); + if (Status != EFI_SUCCESS && Status != EFI_NOT_STARTED) { + return Status; + } + + + if (Private->Ip4Nic != NULL && IpVersion == IP_VERSION_4) { + PxeBcDestroyIp4Children (This, Private); + } + + if (Private->Ip6Nic != NULL && IpVersion == IP_VERSION_6) { + PxeBcDestroyIp6Children (This, Private); + } + + if (Private->Ip4Nic == NULL && Private->Ip6Nic == NULL) { + gBS->UninstallProtocolInterface ( + NicHandle, + &gEfiCallerIdGuid, + &Private->Id + ); + FreePool (Private); + } + + return EFI_SUCCESS; +} + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be tested. + @param[in] RemainingDevicePath Optional parameter used to pick a specific child + device to be started. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return PxeBcSupported ( + This, + ControllerHandle, + RemainingDevicePath, + IP_VERSION_4 + ); +} + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be started. + @param[in] RemainingDevicePath Optional parameter used to pick a specific child + device to be started. + + @retval EFI_SUCCESS This driver is installed to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return PxeBcStart ( + This, + ControllerHandle, + RemainingDevicePath, + IP_VERSION_4 + ); +} + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval Others This driver was not removed from this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + return PxeBcStop ( + This, + ControllerHandle, + NumberOfChildren, + ChildHandleBuffer, + IP_VERSION_4 + ); +} + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be tested. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to be started. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return PxeBcSupported ( + This, + ControllerHandle, + RemainingDevicePath, + IP_VERSION_6 + ); +} + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be started. + @param[in] RemainingDevicePath Optional parameter used to pick a specific child + device to be started. + + @retval EFI_SUCCESS This driver is installed to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return PxeBcStart ( + This, + ControllerHandle, + RemainingDevicePath, + IP_VERSION_6 + ); +} + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval Others This driver was not removed from this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + return PxeBcStop ( + This, + ControllerHandle, + NumberOfChildren, + ChildHandleBuffer, + IP_VERSION_6 + ); +} diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcDriver.h b/NetworkPkg/UefiPxeBcDxe/PxeBcDriver.h new file mode 100644 index 000000000..6bb907732 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcDriver.h @@ -0,0 +1,175 @@ +/** @file + Driver Binding functions declaration for UefiPxeBc Driver. + + Copyright (c) 2007 - 2013, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_PXEBC_DRIVER_H__ +#define __EFI_PXEBC_DRIVER_H__ + +extern EFI_COMPONENT_NAME_PROTOCOL gPxeBcComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gPxeBcComponentName2; + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be tested. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to be started. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp4DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be started. + @param[in] RemainingDevicePath Optional parameter used to pick a specific child + device to be started. + + @retval EFI_SUCCESS This driver is installed to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp4DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval Others This driver was not removed from this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp4DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +/** + Test to see if this driver supports ControllerHandle. This service + is called by the EFI boot service ConnectController(). In + order to make drivers as small as possible, there are a few calling + restrictions for this service. ConnectController() must + follow these calling restrictions. If any other agent wishes to call + Supported() it must also follow these calling restrictions. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be tested. + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to be started. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_UNSUPPORTED This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp6DriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. This service is called by the + EFI boot service ConnectController(). In order to make + drivers as small as possible, there are a few calling restrictions for + this service. ConnectController() must follow these + calling restrictions. If any other agent wishes to call Start() it + must also follow these calling restrictions. + + @param[in] This The pointer to the driver binding protocol. + @param[in] ControllerHandle The handle of device to be started. + @param[in] RemainingDevicePath Optional parameter used to pick a specific child + device to be started. + + @retval EFI_SUCCESS This driver is installed to ControllerHandle. + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp6DriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle. This service is called by the + EFI boot service DisconnectController(). In order to + make drivers as small as possible, there are a few calling + restrictions for this service. DisconnectController() + must follow these calling restrictions. If any other agent wishes + to call Stop() it must also follow these calling restrictions. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. + @retval Others This driver was not removed from this device. + +**/ +EFI_STATUS +EFIAPI +PxeBcIp6DriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); +#endif + diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcImpl.c b/NetworkPkg/UefiPxeBcDxe/PxeBcImpl.c new file mode 100644 index 000000000..639415b28 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcImpl.c @@ -0,0 +1,2421 @@ +/** @file + This implementation of EFI_PXE_BASE_CODE_PROTOCOL and EFI_LOAD_FILE_PROTOCOL. + + Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "PxeBcImpl.h" + + +/** + Enables the use of the PXE Base Code Protocol functions. + + This function enables the use of the PXE Base Code Protocol functions. If the + Started field of the EFI_PXE_BASE_CODE_MODE structure is already TRUE, then + EFI_ALREADY_STARTED will be returned. If UseIpv6 is TRUE, then IPv6 formatted + addresses will be used in this session. If UseIpv6 is FALSE, then IPv4 formatted + addresses will be used in this session. If UseIpv6 is TRUE, and the Ipv6Supported + field of the EFI_PXE_BASE_CODE_MODE structure is FALSE, then EFI_UNSUPPORTED will + be returned. If there is not enough memory or other resources to start the PXE + Base Code Protocol, then EFI_OUT_OF_RESOURCES will be returned. Otherwise, the + PXE Base Code Protocol will be started. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + @param[in] UseIpv6 Specifies the type of IP addresses that are to be + used during the session that is being started. + Set to TRUE for IPv6, and FALSE for IPv4. + + @retval EFI_SUCCESS The PXE Base Code Protocol was started. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval EFI_UNSUPPORTED UseIpv6 is TRUE, but the Ipv6Supported field of the + EFI_PXE_BASE_CODE_MODE structure is FALSE. + @retval EFI_ALREADY_STARTED The PXE Base Code Protocol is already in the started state. + @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid + EFI_PXE_BASE_CODE_PROTOCOL structure. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory or other resources to start the + PXE Base Code Protocol. + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcStart ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This, + IN BOOLEAN UseIpv6 + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + UINTN Index; + EFI_STATUS Status; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + + if (Mode->Started) { + return EFI_ALREADY_STARTED; + } + + // + // Detect whether using IPv6 or not, and set it into mode data. + // + if (UseIpv6 && Mode->Ipv6Available && Mode->Ipv6Supported && Private->Ip6Nic != NULL) { + Mode->UsingIpv6 = TRUE; + } else if (!UseIpv6 && Private->Ip4Nic != NULL) { + Mode->UsingIpv6 = FALSE; + } else { + return EFI_UNSUPPORTED; + } + + if (Mode->UsingIpv6) { + AsciiPrint ("\n>>Start PXE over IPv6"); + // + // Configure udp6 instance to receive data. + // + Status = Private->Udp6Read->Configure ( + Private->Udp6Read, + &Private->Udp6CfgData + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Configure block size for TFTP as a default value to handle all link layers. + // + Private->BlockSize = Private->Ip6MaxPacketSize - + PXEBC_DEFAULT_UDP_OVERHEAD_SIZE - PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE; + + // + // PXE over IPv6 starts here, initialize the fields and list header. + // + Private->Ip6Policy = PXEBC_IP6_POLICY_MAX; + Private->ProxyOffer.Dhcp6.Packet.Offer.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE; + Private->DhcpAck.Dhcp6.Packet.Ack.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE; + Private->PxeReply.Dhcp6.Packet.Ack.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE; + + for (Index = 0; Index < PXEBC_OFFER_MAX_NUM; Index++) { + Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE; + } + + // + // Create event and set status for token to capture ICMP6 error message. + // + Private->Icmp6Token.Status = EFI_NOT_READY; + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + PxeBcIcmp6ErrorUpdate, + Private, + &Private->Icmp6Token.Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Set Ip6 policy to Automatic to start the IP6 router discovery. + // + Status = PxeBcSetIp6Policy (Private); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + } else { + AsciiPrint ("\n>>Start PXE over IPv4"); + // + // Configure udp4 instance to receive data. + // + Status = Private->Udp4Read->Configure ( + Private->Udp4Read, + &Private->Udp4CfgData + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Configure block size for TFTP as a default value to handle all link layers. + // + Private->BlockSize = Private->Ip4MaxPacketSize - + PXEBC_DEFAULT_UDP_OVERHEAD_SIZE - PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE; + + // + // PXE over IPv4 starts here, initialize the fields. + // + Private->ProxyOffer.Dhcp4.Packet.Offer.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE; + Private->DhcpAck.Dhcp4.Packet.Ack.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE; + Private->PxeReply.Dhcp4.Packet.Ack.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE; + + for (Index = 0; Index < PXEBC_OFFER_MAX_NUM; Index++) { + Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE; + } + + PxeBcSeedDhcp4Packet (&Private->SeedPacket, Private->Udp4Read); + + // + // Create the event for Arp cache update. + // + Status = gBS->CreateEvent ( + EVT_TIMER | EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + PxeBcArpCacheUpdate, + Private, + &Private->ArpUpdateEvent + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Start a periodic timer by second to update Arp cache. + // + Status = gBS->SetTimer ( + Private->ArpUpdateEvent, + TimerPeriodic, + TICKS_PER_SECOND + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Create event and set status for token to capture ICMP error message. + // + Private->Icmp6Token.Status = EFI_NOT_READY; + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + PxeBcIcmpErrorUpdate, + Private, + &Private->IcmpToken.Event + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + //DHCP4 service allows only one of its children to be configured in + //the active state, If the DHCP4 D.O.R.A started by IP4 auto + //configuration and has not been completed, the Dhcp4 state machine + //will not be in the right state for the PXE to start a new round D.O.R.A. + //so we need to switch it's policy to static. + // + Status = PxeBcSetIp4Policy (Private); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + } + + // + // If PcdTftpBlockSize is set to non-zero, override the default value. + // + if (PcdGet64 (PcdTftpBlockSize) != 0) { + Private->BlockSize = (UINTN) PcdGet64 (PcdTftpBlockSize); + } + + // + // Create event for UdpRead/UdpWrite timeout since they are both blocking API. + // + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &Private->UdpTimeOutEvent + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Private->IsAddressOk = FALSE; + Mode->Started = TRUE; + + return EFI_SUCCESS; + +ON_ERROR: + if (Mode->UsingIpv6) { + if (Private->Icmp6Token.Event != NULL) { + gBS->CloseEvent (Private->Icmp6Token.Event); + Private->Icmp6Token.Event = NULL; + } + Private->Udp6Read->Configure (Private->Udp6Read, NULL); + Private->Ip6->Configure (Private->Ip6, NULL); + } else { + if (Private->ArpUpdateEvent != NULL) { + gBS->CloseEvent (Private->ArpUpdateEvent); + Private->ArpUpdateEvent = NULL; + } + if (Private->IcmpToken.Event != NULL) { + gBS->CloseEvent (Private->IcmpToken.Event); + Private->IcmpToken.Event = NULL; + } + Private->Udp4Read->Configure (Private->Udp4Read, NULL); + Private->Ip4->Configure (Private->Ip4, NULL); + } + return Status; +} + + +/** + Disable the use of the PXE Base Code Protocol functions. + + This function stops all activity on the network device. All the resources allocated + in Start() are released, the Started field of the EFI_PXE_BASE_CODE_MODE structure is + set to FALSE, and EFI_SUCCESS is returned. If the Started field of the EFI_PXE_BASE_CODE_MODE + structure is already FALSE, then EFI_NOT_STARTED will be returned. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + + @retval EFI_SUCCESS The PXE Base Code Protocol was stopped. + @retval EFI_NOT_STARTED The PXE Base Code Protocol is already in the stopped state. + @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid + EFI_PXE_BASE_CODE_PROTOCOL structure. + @retval Others + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcStop ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + BOOLEAN Ipv6Supported; + BOOLEAN Ipv6Available; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + Ipv6Supported = Mode->Ipv6Supported; + Ipv6Available = Mode->Ipv6Available; + + if (!Mode->Started) { + return EFI_NOT_STARTED; + } + + if (Mode->UsingIpv6) { + // + // Configure all the instances for IPv6 as NULL. + // + ZeroMem (&Private->Udp6CfgData.StationAddress, sizeof (EFI_IPv6_ADDRESS)); + ZeroMem (&Private->Ip6CfgData.StationAddress, sizeof (EFI_IPv6_ADDRESS)); + Private->Dhcp6->Stop (Private->Dhcp6); + Private->Dhcp6->Configure (Private->Dhcp6, NULL); + Private->Udp6Write->Configure (Private->Udp6Write, NULL); + Private->Udp6Read->Groups (Private->Udp6Read, FALSE, NULL); + Private->Udp6Read->Configure (Private->Udp6Read, NULL); + Private->Ip6->Cancel (Private->Ip6, &Private->Icmp6Token); + Private->Ip6->Configure (Private->Ip6, NULL); + PxeBcUnregisterIp6Address (Private); + if (Private->Icmp6Token.Event != NULL) { + gBS->CloseEvent (Private->Icmp6Token.Event); + Private->Icmp6Token.Event = NULL; + } + if (Private->Dhcp6Request != NULL) { + FreePool (Private->Dhcp6Request); + Private->Dhcp6Request = NULL; + } + if (Private->BootFileName != NULL) { + FreePool (Private->BootFileName); + Private->BootFileName = NULL; + } + } else { + // + // Configure all the instances for IPv4 as NULL. + // + ZeroMem (&Private->Udp4CfgData.StationAddress, sizeof (EFI_IPv4_ADDRESS)); + ZeroMem (&Private->Udp4CfgData.SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + ZeroMem (&Private->Ip4CfgData.StationAddress, sizeof (EFI_IPv4_ADDRESS)); + ZeroMem (&Private->Ip4CfgData.SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + Private->Dhcp4->Stop (Private->Dhcp4); + Private->Dhcp4->Configure (Private->Dhcp4, NULL); + Private->Udp4Write->Configure (Private->Udp4Write, NULL); + Private->Udp4Read->Groups (Private->Udp4Read, FALSE, NULL); + Private->Udp4Read->Configure (Private->Udp4Read, NULL); + Private->Ip4->Cancel (Private->Ip4, &Private->IcmpToken); + Private->Ip4->Configure (Private->Ip4, NULL); + if (Private->ArpUpdateEvent != NULL) { + gBS->CloseEvent (Private->ArpUpdateEvent); + Private->ArpUpdateEvent = NULL; + } + if (Private->IcmpToken.Event != NULL) { + gBS->CloseEvent (Private->IcmpToken.Event); + Private->IcmpToken.Event = NULL; + } + Private->BootFileName = NULL; + } + + gBS->CloseEvent (Private->UdpTimeOutEvent); + Private->CurSrcPort = 0; + Private->BootFileSize = 0; + Private->SolicitTimes = 0; + Private->ElapsedTime = 0; + ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS)); + ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS)); + ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS)); + ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS)); + + // + // Reset the mode data. + // + ZeroMem (Mode, sizeof (EFI_PXE_BASE_CODE_MODE)); + Mode->Ipv6Available = Ipv6Available; + Mode->Ipv6Supported = Ipv6Supported; + Mode->AutoArp = TRUE; + Mode->TTL = DEFAULT_TTL; + Mode->ToS = DEFAULT_ToS; + + return EFI_SUCCESS; +} + + +/** + Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / acknowledge) or DHCPv6 + S.A.R.R (solicit / advertise / request / reply) sequence. + + If SortOffers is TRUE, then the cached DHCP offer packets will be sorted before + they are tried. If SortOffers is FALSE, then the cached DHCP offer packets will + be tried in the order in which they are received. Please see the Preboot Execution + Environment (PXE) Specification and Unified Extensible Firmware Interface (UEFI) + Specification for additional details on the implementation of DHCP. + If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, + then the DHCP sequence will be stopped and EFI_ABORTED will be returned. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + @param[in] SortOffers TRUE if the offers received should be sorted. Set to FALSE to + try the offers in the order that they are received. + + @retval EFI_SUCCESS Valid DHCP has completed. + @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state. + @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid + EFI_PXE_BASE_CODE_PROTOCOL structure. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete the DHCP Protocol. + @retval EFI_ABORTED The callback function aborted the DHCP Protocol. + @retval EFI_TIMEOUT The DHCP Protocol timed out. + @retval EFI_ICMP_ERROR An ICMP error packet was received during the DHCP session. + @retval EFI_NO_RESPONSE Valid PXE offer was not received. + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcDhcp ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This, + IN BOOLEAN SortOffers + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_STATUS Status; + EFI_PXE_BASE_CODE_IP_FILTER IpFilter; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = EFI_SUCCESS; + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + Mode->IcmpErrorReceived = FALSE; + Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DHCP; + Private->IsOfferSorted = SortOffers; + Private->SolicitTimes = 0; + Private->ElapsedTime = 0; + + if (!Mode->Started) { + return EFI_NOT_STARTED; + } + + if (Mode->UsingIpv6) { + + // + // Stop Udp6Read instance + // + Private->Udp6Read->Configure (Private->Udp6Read, NULL); + + // + // Start S.A.R.R. process to get a IPv6 address and other boot information. + // + Status = PxeBcDhcp6Sarr (Private, Private->Dhcp6); + } else { + + // + // Stop Udp4Read instance + // + Private->Udp4Read->Configure (Private->Udp4Read, NULL); + + // + // Start D.O.R.A. process to get a IPv4 address and other boot information. + // + Status = PxeBcDhcp4Dora (Private, Private->Dhcp4); + } + + // + // Reconfigure the UDP instance with the default configuration. + // + if (Mode->UsingIpv6) { + Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData); + } else { + Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData); + } + // + // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP + // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP. + // + ZeroMem(&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER)); + IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP; + This->SetIpFilter (This, &IpFilter); + + return Status; +} + + +/** + Attempts to complete the PXE Boot Server and/or boot image discovery sequence. + + This function attempts to complete the PXE Boot Server and/or boot image discovery + sequence. If this sequence is completed, then EFI_SUCCESS is returned, and the + PxeDiscoverValid, PxeDiscover, PxeReplyReceived, and PxeReply fields of the + EFI_PXE_BASE_CODE_MODE structure are filled in. If UseBis is TRUE, then the + PxeBisReplyReceived and PxeBisReply fields of the EFI_PXE_BASE_CODE_MODE structure + will also be filled in. If UseBis is FALSE, then PxeBisReplyValid will be set to FALSE. + In the structure referenced by parameter Info, the PXE Boot Server list, SrvList[], + has two uses: It is the Boot Server IP address list used for unicast discovery + (if the UseUCast field is TRUE), and it is the list used for Boot Server verification + (if the MustUseList field is TRUE). Also, if the MustUseList field in that structure + is TRUE and the AcceptAnyResponse field in the SrvList[] array is TRUE, any Boot + Server reply of that type will be accepted. If the AcceptAnyResponse field is + FALSE, only responses from Boot Servers with matching IP addresses will be accepted. + This function can take at least 10 seconds to timeout and return control to the + caller. If the Discovery sequence does not complete, then EFI_TIMEOUT will be + returned. Please see the Preboot Execution Environment (PXE) Specification for + additional details on the implementation of the Discovery sequence. + If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, + then the Discovery sequence is stopped and EFI_ABORTED will be returned. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + @param[in] Type The type of bootstrap to perform. + @param[in] Layer Pointer to the boot server layer number to discover, which must be + PXE_BOOT_LAYER_INITIAL when a new server type is being + discovered. + @param[in] UseBis TRUE if Boot Integrity Services are to be used. FALSE otherwise. + @param[in] Info Pointer to a data structure that contains additional information + on the type of discovery operation that is to be performed. + It is optional. + + @retval EFI_SUCCESS The Discovery sequence has been completed. + @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete Discovery. + @retval EFI_ABORTED The callback function aborted the Discovery sequence. + @retval EFI_TIMEOUT The Discovery sequence timed out. + @retval EFI_ICMP_ERROR An ICMP error packet was received during the PXE discovery + session. + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcDiscover ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This, + IN UINT16 Type, + IN UINT16 *Layer, + IN BOOLEAN UseBis, + IN EFI_PXE_BASE_CODE_DISCOVER_INFO *Info OPTIONAL + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo; + EFI_PXE_BASE_CODE_SRVLIST *SrvList; + PXEBC_BOOT_SVR_ENTRY *BootSvrEntry; + UINT16 Index; + EFI_STATUS Status; + EFI_PXE_BASE_CODE_IP_FILTER IpFilter; + EFI_PXE_BASE_CODE_DISCOVER_INFO *NewCreatedInfo; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + Mode->IcmpErrorReceived = FALSE; + BootSvrEntry = NULL; + SrvList = NULL; + Status = EFI_DEVICE_ERROR; + Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER; + NewCreatedInfo = NULL; + + if (!Mode->Started) { + return EFI_NOT_STARTED; + } + + // + // Station address should be ready before do discover. + // + if (!Private->IsAddressOk) { + return EFI_INVALID_PARAMETER; + } + + if (Mode->UsingIpv6) { + + // + // Stop Udp6Read instance + // + Private->Udp6Read->Configure (Private->Udp6Read, NULL); + } else { + + // + // Stop Udp4Read instance + // + Private->Udp4Read->Configure (Private->Udp4Read, NULL); + } + + // + // There are 3 methods to get the information for discover. + // + ZeroMem (&DefaultInfo, sizeof (EFI_PXE_BASE_CODE_DISCOVER_INFO)); + if (*Layer != EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL) { + // + // 1. Take the previous setting as the discover info. + // + if (!Mode->PxeDiscoverValid || + !Mode->PxeReplyReceived || + (!Mode->PxeBisReplyReceived && UseBis)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + Info = &DefaultInfo; + Info->IpCnt = 1; + Info->UseUCast = TRUE; + SrvList = Info->SrvList; + SrvList[0].Type = Type; + SrvList[0].AcceptAnyResponse = FALSE; + + CopyMem (&SrvList->IpAddr, &Private->ServerIp, sizeof (EFI_IP_ADDRESS)); + + } else if (Info == NULL) { + // + // 2. Extract the discover information from the cached packets if unspecified. + // + NewCreatedInfo = &DefaultInfo; + Status = PxeBcExtractDiscoverInfo (Private, Type, &NewCreatedInfo, &BootSvrEntry, &SrvList); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + ASSERT (NewCreatedInfo != NULL); + Info = NewCreatedInfo; + } else { + // + // 3. Take the pass-in information as the discover info, and validate the server list. + // + SrvList = Info->SrvList; + + if (!SrvList[0].AcceptAnyResponse) { + for (Index = 1; Index < Info->IpCnt; Index++) { + if (SrvList[Index].AcceptAnyResponse) { + break; + } + } + if (Index != Info->IpCnt) { + // + // It's invalid if the first server doesn't accecpt any response + // but any of the other servers does accept any response. + // + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + } + } + + // + // Info and BootSvrEntry/SrvList are all ready by now, so execute discover by UniCast/BroadCast/MultiCast. + // + if ((!Info->UseUCast && !Info->UseBCast && !Info->UseMCast) || + (Info->MustUseList && Info->IpCnt == 0)) { + Status = EFI_INVALID_PARAMETER; + goto ON_EXIT; + } + + Private->IsDoDiscover = TRUE; + + if (Info->UseMCast) { + // + // Do discover by multicast. + // + Status = PxeBcDiscoverBootServer ( + Private, + Type, + Layer, + UseBis, + &Info->ServerMCastIp, + Info->IpCnt, + SrvList + ); + + } else if (Info->UseBCast) { + // + // Do discover by broadcast, but only valid for IPv4. + // + ASSERT (!Mode->UsingIpv6); + Status = PxeBcDiscoverBootServer ( + Private, + Type, + Layer, + UseBis, + NULL, + Info->IpCnt, + SrvList + ); + + } else if (Info->UseUCast) { + // + // Do discover by unicast. + // + for (Index = 0; Index < Info->IpCnt; Index++) { + if (BootSvrEntry == NULL) { + CopyMem (&Private->ServerIp, &SrvList[Index].IpAddr, sizeof (EFI_IP_ADDRESS)); + } else { + ASSERT (!Mode->UsingIpv6); + ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS)); + CopyMem (&Private->ServerIp, &BootSvrEntry->IpAddr[Index], sizeof (EFI_IPv4_ADDRESS)); + } + + Status = PxeBcDiscoverBootServer ( + Private, + Type, + Layer, + UseBis, + &Private->ServerIp, + Info->IpCnt, + SrvList + ); + } + } + + if (!EFI_ERROR (Status)) { + // + // Parse the cached PXE reply packet, and store it into mode data if valid. + // + if (Mode->UsingIpv6) { + Status = PxeBcParseDhcp6Packet (&Private->PxeReply.Dhcp6); + if (!EFI_ERROR (Status)) { + CopyMem ( + &Mode->PxeReply.Dhcpv6, + &Private->PxeReply.Dhcp6.Packet.Ack.Dhcp6, + Private->PxeReply.Dhcp6.Packet.Ack.Length + ); + Mode->PxeReplyReceived = TRUE; + Mode->PxeDiscoverValid = TRUE; + } + } else { + Status = PxeBcParseDhcp4Packet (&Private->PxeReply.Dhcp4); + if (!EFI_ERROR (Status)) { + CopyMem ( + &Mode->PxeReply.Dhcpv4, + &Private->PxeReply.Dhcp4.Packet.Ack.Dhcp4, + Private->PxeReply.Dhcp4.Packet.Ack.Length + ); + Mode->PxeReplyReceived = TRUE; + Mode->PxeDiscoverValid = TRUE; + } + } + } + +ON_EXIT: + + if (NewCreatedInfo != NULL && NewCreatedInfo != &DefaultInfo) { + FreePool (NewCreatedInfo); + } + + if (Mode->UsingIpv6) { + Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData); + } else { + Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData); + } + + // + // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP + // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP. + // + ZeroMem(&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER)); + IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP; + This->SetIpFilter (This, &IpFilter); + + return Status; +} + + +/** + Used to perform TFTP and MTFTP services. + + This function is used to perform TFTP and MTFTP services. This includes the + TFTP operations to get the size of a file, read a directory, read a file, and + write a file. It also includes the MTFTP operations to get the size of a file, + read a directory, and read a file. The type of operation is specified by Operation. + If the callback function that is invoked during the TFTP/MTFTP operation does + not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will + be returned. + For read operations, the return data will be placed in the buffer specified by + BufferPtr. If BufferSize is too small to contain the entire downloaded file, + then EFI_BUFFER_TOO_SMALL will be returned and BufferSize will be set to zero, + or the size of the requested file. (NOTE: the size of the requested file is only returned + if the TFTP server supports TFTP options). If BufferSize is large enough for the + read operation, then BufferSize will be set to the size of the downloaded file, + and EFI_SUCCESS will be returned. Applications using the PxeBc.Mtftp() services + should use the get-file-size operations to determine the size of the downloaded + file prior to using the read-file operations-especially when downloading large + (greater than 64 MB) files-instead of making two calls to the read-file operation. + Following this recommendation will save time if the file is larger than expected + and the TFTP server does not support TFTP option extensions. Without TFTP option + extension support, the client must download the entire file, counting and discarding + the received packets, to determine the file size. + For write operations, the data to be sent is in the buffer specified by BufferPtr. + BufferSize specifies the number of bytes to send. If the write operation completes + successfully, then EFI_SUCCESS will be returned. + For TFTP "get file size" operations, the size of the requested file or directory + is returned in BufferSize, and EFI_SUCCESS will be returned. If the TFTP server + does not support options, the file will be downloaded into a bit bucket and the + length of the downloaded file will be returned. For MTFTP "get file size" operations, + if the MTFTP server does not support the "get file size" option, EFI_UNSUPPORTED + will be returned. + This function can take up to 10 seconds to timeout and return control to the caller. + If the TFTP sequence does not complete, EFI_TIMEOUT will be returned. + If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, + then the TFTP sequence is stopped and EFI_ABORTED will be returned. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + @param[in] Operation The type of operation to perform. + @param[in, out] BufferPtr A pointer to the data buffer. + @param[in] Overwrite Only used on write file operations. TRUE if a file on a remote + server can be overwritten. + @param[in, out] BufferSize For get-file-size operations, *BufferSize returns the size of the + requested file. + @param[in] BlockSize The requested block size to be used during a TFTP transfer. + @param[in] ServerIp The TFTP / MTFTP server IP address. + @param[in] Filename A Null-terminated ASCII string that specifies a directory name + or a file name. + @param[in] Info Pointer to the MTFTP information. + @param[in] DontUseBuffer Set to FALSE for normal TFTP and MTFTP read file operation. + + @retval EFI_SUCCESS The TFTP/MTFTP operation was completed. + @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval EFI_BUFFER_TOO_SMALL The buffer is not large enough to complete the read operation. + @retval EFI_ABORTED The callback function aborted the TFTP/MTFTP operation. + @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out. + @retval EFI_ICMP_ERROR An ICMP error packet was received during the MTFTP session. + @retval EFI_TFTP_ERROR A TFTP error packet was received during the MTFTP session. + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcMtftp ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This, + IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation, + IN OUT VOID *BufferPtr OPTIONAL, + IN BOOLEAN Overwrite, + IN OUT UINT64 *BufferSize, + IN UINTN *BlockSize OPTIONAL, + IN EFI_IP_ADDRESS *ServerIp, + IN UINT8 *Filename, + IN EFI_PXE_BASE_CODE_MTFTP_INFO *Info OPTIONAL, + IN BOOLEAN DontUseBuffer + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_MTFTP4_CONFIG_DATA Mtftp4Config; + EFI_MTFTP6_CONFIG_DATA Mtftp6Config; + VOID *Config; + EFI_STATUS Status; + EFI_PXE_BASE_CODE_IP_FILTER IpFilter; + UINTN WindowSize; + + if ((This == NULL) || + (Filename == NULL) || + (BufferSize == NULL) || + (ServerIp == NULL) || + ((BlockSize != NULL) && (*BlockSize < PXE_MTFTP_DEFAULT_BLOCK_SIZE))) { + return EFI_INVALID_PARAMETER; + } + + if (Operation == EFI_PXE_BASE_CODE_TFTP_READ_FILE || + Operation == EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY || + Operation == EFI_PXE_BASE_CODE_MTFTP_READ_FILE || + Operation == EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY) { + if (BufferPtr == NULL && !DontUseBuffer) { + return EFI_INVALID_PARAMETER; + } + } + + Config = NULL; + Status = EFI_DEVICE_ERROR; + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + + // + // Get PcdPxeTftpWindowSize. + // + WindowSize = (UINTN) PcdGet64 (PcdPxeTftpWindowSize); + + if (Mode->UsingIpv6) { + if (!NetIp6IsValidUnicast (&ServerIp->v6)) { + return EFI_INVALID_PARAMETER; + } + } else { + if (IP4_IS_UNSPECIFIED (NTOHL (ServerIp->Addr[0])) || IP4_IS_LOCAL_BROADCAST (NTOHL (ServerIp->Addr[0]))) { + return EFI_INVALID_PARAMETER; + } + } + + if (Mode->UsingIpv6) { + // + // Set configuration data for Mtftp6 instance. + // + ZeroMem (&Mtftp6Config, sizeof (EFI_MTFTP6_CONFIG_DATA)); + Config = &Mtftp6Config; + Mtftp6Config.TimeoutValue = PXEBC_MTFTP_TIMEOUT; + Mtftp6Config.TryCount = PXEBC_MTFTP_RETRIES; + CopyMem (&Mtftp6Config.StationIp, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS)); + CopyMem (&Mtftp6Config.ServerIp, &ServerIp->v6, sizeof (EFI_IPv6_ADDRESS)); + // + // Stop Udp6Read instance + // + Private->Udp6Read->Configure (Private->Udp6Read, NULL); + } else { + // + // Set configuration data for Mtftp4 instance. + // + ZeroMem (&Mtftp4Config, sizeof (EFI_MTFTP4_CONFIG_DATA)); + Config = &Mtftp4Config; + Mtftp4Config.UseDefaultSetting = FALSE; + Mtftp4Config.TimeoutValue = PXEBC_MTFTP_TIMEOUT; + Mtftp4Config.TryCount = PXEBC_MTFTP_RETRIES; + CopyMem (&Mtftp4Config.StationIp, &Private->StationIp.v4, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Mtftp4Config.SubnetMask, &Private->SubnetMask.v4, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Mtftp4Config.GatewayIp, &Private->GatewayIp.v4, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Mtftp4Config.ServerIp, &ServerIp->v4, sizeof (EFI_IPv4_ADDRESS)); + // + // Stop Udp4Read instance + // + Private->Udp4Read->Configure (Private->Udp4Read, NULL); + } + + Mode->TftpErrorReceived = FALSE; + Mode->IcmpErrorReceived = FALSE; + + switch (Operation) { + + case EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE: + // + // Send TFTP request to get file size. + // + Status = PxeBcTftpGetFileSize ( + Private, + Config, + Filename, + BlockSize, + (WindowSize > 1) ? &WindowSize : NULL, + BufferSize + ); + + break; + + case EFI_PXE_BASE_CODE_TFTP_READ_FILE: + // + // Send TFTP request to read file. + // + Status = PxeBcTftpReadFile ( + Private, + Config, + Filename, + BlockSize, + (WindowSize > 1) ? &WindowSize : NULL, + BufferPtr, + BufferSize, + DontUseBuffer + ); + + break; + + case EFI_PXE_BASE_CODE_TFTP_WRITE_FILE: + // + // Send TFTP request to write file. + // + Status = PxeBcTftpWriteFile ( + Private, + Config, + Filename, + Overwrite, + BlockSize, + BufferPtr, + BufferSize + ); + + break; + + case EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY: + // + // Send TFTP request to read directory. + // + Status = PxeBcTftpReadDirectory ( + Private, + Config, + Filename, + BlockSize, + (WindowSize > 1) ? &WindowSize : NULL, + BufferPtr, + BufferSize, + DontUseBuffer + ); + + break; + + case EFI_PXE_BASE_CODE_MTFTP_GET_FILE_SIZE: + case EFI_PXE_BASE_CODE_MTFTP_READ_FILE: + case EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY: + Status = EFI_UNSUPPORTED; + + break; + + default: + Status = EFI_INVALID_PARAMETER; + + break; + } + + if (Status == EFI_ICMP_ERROR) { + Mode->IcmpErrorReceived = TRUE; + } + + // + // Reconfigure the UDP instance with the default configuration. + // + if (Mode->UsingIpv6) { + Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData); + } else { + Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData); + } + // + // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP + // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP. + // + ZeroMem(&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER)); + IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP; + This->SetIpFilter (This, &IpFilter); + + return Status; +} + + +/** + Writes a UDP packet to the network interface. + + This function writes a UDP packet specified by the (optional HeaderPtr and) + BufferPtr parameters to the network interface. The UDP header is automatically + built by this routine. It uses the parameters OpFlags, DestIp, DestPort, GatewayIp, + SrcIp, and SrcPort to build this header. If the packet is successfully built and + transmitted through the network interface, then EFI_SUCCESS will be returned. + If a timeout occurs during the transmission of the packet, then EFI_TIMEOUT will + be returned. If an ICMP error occurs during the transmission of the packet, then + the IcmpErrorReceived field is set to TRUE, the IcmpError field is filled in and + EFI_ICMP_ERROR will be returned. If the Callback Protocol does not return + EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will be returned. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + @param[in] OpFlags The UDP operation flags. + @param[in] DestIp The destination IP address. + @param[in] DestPort The destination UDP port number. + @param[in] GatewayIp The gateway IP address. + @param[in] SrcIp The source IP address. + @param[in, out] SrcPort The source UDP port number. + @param[in] HeaderSize An optional field which may be set to the length of a header + at HeaderPtr to be prefixed to the data at BufferPtr. + @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be + prefixed to the data at BufferPtr. + @param[in] BufferSize A pointer to the size of the data at BufferPtr. + @param[in] BufferPtr A pointer to the data to be written. + + @retval EFI_SUCCESS The UDP Write operation completed. + @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_BAD_BUFFER_SIZE The buffer is too long to be transmitted. + @retval EFI_ABORTED The callback function aborted the UDP Write operation. + @retval EFI_TIMEOUT The UDP Write operation timed out. + @retval EFI_ICMP_ERROR An ICMP error packet was received during the UDP write session. + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcUdpWrite ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This, + IN UINT16 OpFlags, + IN EFI_IP_ADDRESS *DestIp, + IN EFI_PXE_BASE_CODE_UDP_PORT *DestPort, + IN EFI_IP_ADDRESS *GatewayIp OPTIONAL, + IN EFI_IP_ADDRESS *SrcIp OPTIONAL, + IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL, + IN UINTN *HeaderSize OPTIONAL, + IN VOID *HeaderPtr OPTIONAL, + IN UINTN *BufferSize, + IN VOID *BufferPtr + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_UDP4_SESSION_DATA Udp4Session; + EFI_UDP6_SESSION_DATA Udp6Session; + EFI_STATUS Status; + BOOLEAN DoNotFragment; + + if (This == NULL || DestIp == NULL || DestPort == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + + if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_MAY_FRAGMENT) != 0) { + DoNotFragment = FALSE; + } else { + DoNotFragment = TRUE; + } + + if (!Mode->UsingIpv6 && GatewayIp != NULL && Mode->SubnetMask.Addr[0] != 0 && + !NetIp4IsUnicast (NTOHL (GatewayIp->Addr[0]), EFI_NTOHL(Mode->SubnetMask))) { + // + // Gateway is provided but it's not a unicast IPv4 address, while it will be ignored for IPv6. + // + return EFI_INVALID_PARAMETER; + } + + if (HeaderSize != NULL && (*HeaderSize == 0 || HeaderPtr == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (BufferSize == NULL || (*BufferSize != 0 && BufferPtr == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (!Mode->Started) { + return EFI_NOT_STARTED; + } + + if (!Private->IsAddressOk && SrcIp == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (Private->CurSrcPort == 0 || + (SrcPort != NULL && *SrcPort != Private->CurSrcPort)) { + // + // Reconfigure UDPv4/UDPv6 for UdpWrite if the source port changed. + // + if (SrcPort != NULL) { + Private->CurSrcPort = *SrcPort; + } + } + + if (Mode->UsingIpv6) { + Status = PxeBcConfigUdp6Write ( + Private->Udp6Write, + &Private->StationIp.v6, + &Private->CurSrcPort + ); + } else { + // + // Configure the UDPv4 instance with gateway information from DHCP server as default. + // + Status = PxeBcConfigUdp4Write ( + Private->Udp4Write, + &Private->StationIp.v4, + &Private->SubnetMask.v4, + &Private->GatewayIp.v4, + &Private->CurSrcPort, + DoNotFragment, + Private->Mode.TTL, + Private->Mode.ToS + ); + } + + if (EFI_ERROR (Status)) { + Private->CurSrcPort = 0; + return EFI_INVALID_PARAMETER; + } else if (SrcPort != NULL) { + *SrcPort = Private->CurSrcPort; + } + + // + // Start a timer as timeout event for this blocking API. + // + gBS->SetTimer (Private->UdpTimeOutEvent, TimerRelative, PXEBC_UDP_TIMEOUT); + + if (Mode->UsingIpv6) { + // + // Construct UDPv6 session data. + // + ZeroMem (&Udp6Session, sizeof (EFI_UDP6_SESSION_DATA)); + CopyMem (&Udp6Session.DestinationAddress, DestIp, sizeof (EFI_IPv6_ADDRESS)); + Udp6Session.DestinationPort = *DestPort; + if (SrcIp != NULL) { + CopyMem (&Udp6Session.SourceAddress, SrcIp, sizeof (EFI_IPv6_ADDRESS)); + } + if (SrcPort != NULL) { + Udp6Session.SourcePort = *SrcPort; + } + + Status = PxeBcUdp6Write ( + Private->Udp6Write, + &Udp6Session, + Private->UdpTimeOutEvent, + HeaderSize, + HeaderPtr, + BufferSize, + BufferPtr + ); + } else { + // + // Construct UDPv4 session data. + // + ZeroMem (&Udp4Session, sizeof (EFI_UDP4_SESSION_DATA)); + CopyMem (&Udp4Session.DestinationAddress, DestIp, sizeof (EFI_IPv4_ADDRESS)); + Udp4Session.DestinationPort = *DestPort; + if (SrcIp != NULL) { + CopyMem (&Udp4Session.SourceAddress, SrcIp, sizeof (EFI_IPv4_ADDRESS)); + } + if (SrcPort != NULL) { + Udp4Session.SourcePort = *SrcPort; + } + // + // Override the gateway information if user specified. + // + Status = PxeBcUdp4Write ( + Private->Udp4Write, + &Udp4Session, + Private->UdpTimeOutEvent, + (EFI_IPv4_ADDRESS *) GatewayIp, + HeaderSize, + HeaderPtr, + BufferSize, + BufferPtr + ); + } + + gBS->SetTimer (Private->UdpTimeOutEvent, TimerCancel, 0); + + + // + // Reset the UdpWrite instance. + // + if (Mode->UsingIpv6) { + Private->Udp6Write->Configure (Private->Udp6Write, NULL); + } else { + Private->Udp4Write->Configure (Private->Udp4Write, NULL); + } + + return Status; +} + + +/** + Reads a UDP packet from the network interface. ++ + This function reads a UDP packet from a network interface. The data contents + are returned in (the optional HeaderPtr and) BufferPtr, and the size of the + buffer received is returned in BufferSize . If the input BufferSize is smaller + than the UDP packet received (less optional HeaderSize), it will be set to the + required size, and EFI_BUFFER_TOO_SMALL will be returned. In this case, the + contents of BufferPtr are undefined, and the packet is lost. If a UDP packet is + successfully received, then EFI_SUCCESS will be returned, and the information + from the UDP header will be returned in DestIp, DestPort, SrcIp, and SrcPort if + they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort, + SrcIp, and SrcPort input values, different types of UDP packet receive filtering + will be performed. The following tables summarize these receive filter operations. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + @param[in] OpFlags The UDP operation flags. + @param[in, out] DestIp The destination IP address. + @param[in, out] DestPort The destination UDP port number. + @param[in, out] SrcIp The source IP address. + @param[in, out] SrcPort The source UDP port number. + @param[in] HeaderSize An optional field which may be set to the length of a + header at HeaderPtr to be prefixed to the data at BufferPtr. + @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be + prefixed to the data at BufferPtr. + @param[in, out] BufferSize A pointer to the size of the data at BufferPtr. + @param[in] BufferPtr A pointer to the data to be read. + + @retval EFI_SUCCESS The UDP Read operation was completed. + @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval EFI_BUFFER_TOO_SMALL The packet is larger than Buffer can hold. + @retval EFI_ABORTED The callback function aborted the UDP Read operation. + @retval EFI_TIMEOUT The UDP Read operation timed out. + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcUdpRead ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This, + IN UINT16 OpFlags, + IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL, + IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL, + IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL, + IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL, + IN UINTN *HeaderSize OPTIONAL, + IN VOID *HeaderPtr OPTIONAL, + IN OUT UINTN *BufferSize, + IN VOID *BufferPtr + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_UDP4_COMPLETION_TOKEN Udp4Token; + EFI_UDP6_COMPLETION_TOKEN Udp6Token; + EFI_UDP4_RECEIVE_DATA *Udp4Rx; + EFI_UDP6_RECEIVE_DATA *Udp6Rx; + EFI_STATUS Status; + BOOLEAN IsDone; + BOOLEAN IsMatched; + UINTN CopiedLen; + UINTN HeaderLen; + UINTN HeaderCopiedLen; + UINTN BufferCopiedLen; + UINT32 FragmentLength; + UINTN FragmentIndex; + UINT8 *FragmentBuffer; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + IsDone = FALSE; + IsMatched = FALSE; + Udp4Rx = NULL; + Udp6Rx = NULL; + + if (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0 && DestPort == NULL) || + ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) == 0 && SrcIp == NULL) || + ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) == 0 && SrcPort == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if ((HeaderSize != NULL && *HeaderSize == 0) || (HeaderSize != NULL && HeaderPtr == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if ((BufferSize == NULL) || (BufferPtr == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (!Mode->Started) { + return EFI_NOT_STARTED; + } + + ZeroMem (&Udp6Token, sizeof (EFI_UDP6_COMPLETION_TOKEN)); + ZeroMem (&Udp4Token, sizeof (EFI_UDP4_COMPLETION_TOKEN)); + + if (Mode->UsingIpv6) { + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + PxeBcCommonNotify, + &IsDone, + &Udp6Token.Event + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + } else { + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + PxeBcCommonNotify, + &IsDone, + &Udp4Token.Event + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + } + + // + // Start a timer as timeout event for this blocking API. + // + gBS->SetTimer (Private->UdpTimeOutEvent, TimerRelative, PXEBC_UDP_TIMEOUT); + Mode->IcmpErrorReceived = FALSE; + + // + // Read packet by Udp4Read/Udp6Read until matched or timeout. + // + while (!IsMatched && !EFI_ERROR (Status)) { + if (Mode->UsingIpv6) { + Status = PxeBcUdp6Read ( + Private->Udp6Read, + &Udp6Token, + Mode, + Private->UdpTimeOutEvent, + OpFlags, + &IsDone, + &IsMatched, + DestIp, + DestPort, + SrcIp, + SrcPort + ); + } else { + Status = PxeBcUdp4Read ( + Private->Udp4Read, + &Udp4Token, + Mode, + Private->UdpTimeOutEvent, + OpFlags, + &IsDone, + &IsMatched, + DestIp, + DestPort, + SrcIp, + SrcPort + ); + } + } + + if (Status == EFI_ICMP_ERROR || + Status == EFI_NETWORK_UNREACHABLE || + Status == EFI_HOST_UNREACHABLE || + Status == EFI_PROTOCOL_UNREACHABLE || + Status == EFI_PORT_UNREACHABLE) { + // + // Get different return status for icmp error from Udp, refers to UEFI spec. + // + Mode->IcmpErrorReceived = TRUE; + } + gBS->SetTimer (Private->UdpTimeOutEvent, TimerCancel, 0); + + if (IsMatched) { + // + // Copy the rececived packet to user if matched by filter. + // + if (Mode->UsingIpv6) { + Udp6Rx = Udp6Token.Packet.RxData; + ASSERT (Udp6Rx != NULL); + + HeaderLen = 0; + if (HeaderSize != NULL) { + HeaderLen = MIN (*HeaderSize, Udp6Rx->DataLength); + } + + if (Udp6Rx->DataLength - HeaderLen > *BufferSize) { + Status = EFI_BUFFER_TOO_SMALL; + } else { + if (HeaderSize != NULL) { + *HeaderSize = HeaderLen; + } + *BufferSize = Udp6Rx->DataLength - HeaderLen; + + HeaderCopiedLen = 0; + BufferCopiedLen = 0; + for (FragmentIndex = 0; FragmentIndex < Udp6Rx->FragmentCount; FragmentIndex++) { + FragmentLength = Udp6Rx->FragmentTable[FragmentIndex].FragmentLength; + FragmentBuffer = Udp6Rx->FragmentTable[FragmentIndex].FragmentBuffer; + if (HeaderCopiedLen + FragmentLength < HeaderLen) { + // + // Copy the header part of received data. + // + CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, FragmentLength); + HeaderCopiedLen += FragmentLength; + } else if (HeaderCopiedLen < HeaderLen) { + // + // Copy the header part of received data. + // + CopiedLen = HeaderLen - HeaderCopiedLen; + CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, CopiedLen); + HeaderCopiedLen += CopiedLen; + + // + // Copy the other part of received data. + // + CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer + CopiedLen, FragmentLength - CopiedLen); + BufferCopiedLen += (FragmentLength - CopiedLen); + } else { + // + // Copy the other part of received data. + // + CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer, FragmentLength); + BufferCopiedLen += FragmentLength; + } + } + } + // + // Recycle the receiving buffer after copy to user. + // + gBS->SignalEvent (Udp6Rx->RecycleSignal); + } else { + Udp4Rx = Udp4Token.Packet.RxData; + ASSERT (Udp4Rx != NULL); + + HeaderLen = 0; + if (HeaderSize != NULL) { + HeaderLen = MIN (*HeaderSize, Udp4Rx->DataLength); + } + + if (Udp4Rx->DataLength - HeaderLen > *BufferSize) { + Status = EFI_BUFFER_TOO_SMALL; + } else { + if (HeaderSize != NULL) { + *HeaderSize = HeaderLen; + } + *BufferSize = Udp4Rx->DataLength - HeaderLen; + + HeaderCopiedLen = 0; + BufferCopiedLen = 0; + for (FragmentIndex = 0; FragmentIndex < Udp4Rx->FragmentCount; FragmentIndex++) { + FragmentLength = Udp4Rx->FragmentTable[FragmentIndex].FragmentLength; + FragmentBuffer = Udp4Rx->FragmentTable[FragmentIndex].FragmentBuffer; + if (HeaderCopiedLen + FragmentLength < HeaderLen) { + // + // Copy the header part of received data. + // + CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, FragmentLength); + HeaderCopiedLen += FragmentLength; + } else if (HeaderCopiedLen < HeaderLen) { + // + // Copy the header part of received data. + // + CopiedLen = HeaderLen - HeaderCopiedLen; + CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, CopiedLen); + HeaderCopiedLen += CopiedLen; + + // + // Copy the other part of received data. + // + CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer + CopiedLen, FragmentLength - CopiedLen); + BufferCopiedLen += (FragmentLength - CopiedLen); + } else { + // + // Copy the other part of received data. + // + CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer, FragmentLength); + BufferCopiedLen += FragmentLength; + } + } + } + // + // Recycle the receiving buffer after copy to user. + // + gBS->SignalEvent (Udp4Rx->RecycleSignal); + } + } + + if (Mode->UsingIpv6) { + Private->Udp6Read->Cancel (Private->Udp6Read, &Udp6Token); + gBS->CloseEvent (Udp6Token.Event); + } else { + Private->Udp4Read->Cancel (Private->Udp4Read, &Udp4Token); + gBS->CloseEvent (Udp4Token.Event); + } + + return Status; +} + + +/** + Updates the IP receive filters of a network device and enables software filtering. + + The NewFilter field is used to modify the network device's current IP receive + filter settings and to enable a software filter. This function updates the IpFilter + field of the EFI_PXE_BASE_CODE_MODE structure with the contents of NewIpFilter. + The software filter is used when the USE_FILTER in OpFlags is set to UdpRead(). + The current hardware filter remains in effect no matter what the settings of OpFlags. + This is so that the meaning of ANY_DEST_IP set in OpFlags to UdpRead() is from those + packets whose reception is enabled in hardware-physical NIC address (unicast), + broadcast address, logical address or addresses (multicast), or all (promiscuous). + UdpRead() does not modify the IP filter settings. + Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP receive + filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP. + If an application or driver wishes to preserve the IP receive filter settings, + it will have to preserve the IP receive filter settings before these calls, and + use SetIpFilter() to restore them after the calls. If incompatible filtering is + requested (for example, PROMISCUOUS with anything else), or if the device does not + support a requested filter setting and it cannot be accommodated in software + (for example, PROMISCUOUS not supported), EFI_INVALID_PARAMETER will be returned. + The IPlist field is used to enable IPs other than the StationIP. They may be + multicast or unicast. If IPcnt is set as well as EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP, + then both the StationIP and the IPs from the IPlist will be used. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + @param[in] NewFilter Pointer to the new set of IP receive filters. + + @retval EFI_SUCCESS The IP receive filter settings were updated. + @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcSetIpFilter ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This, + IN EFI_PXE_BASE_CODE_IP_FILTER *NewFilter + ) +{ + EFI_STATUS Status; + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_UDP4_CONFIG_DATA *Udp4Cfg; + EFI_UDP6_CONFIG_DATA *Udp6Cfg; + UINTN Index; + BOOLEAN NeedPromiscuous; + BOOLEAN AcceptPromiscuous; + BOOLEAN AcceptBroadcast; + BOOLEAN MultiCastUpdate; + + if (This == NULL || NewFilter == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + Status = EFI_SUCCESS; + NeedPromiscuous = FALSE; + + if (!Mode->Started) { + return EFI_NOT_STARTED; + } + + for (Index = 0; Index < NewFilter->IpCnt; Index++) { + ASSERT (Index < EFI_PXE_BASE_CODE_MAX_IPCNT); + if (!Mode->UsingIpv6 && + IP4_IS_LOCAL_BROADCAST (EFI_IP4 (NewFilter->IpList[Index].v4))) { + // + // IPv4 broadcast address should not be in IP filter. + // + return EFI_INVALID_PARAMETER; + } + if (Mode->UsingIpv6) { + if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0 && + NetIp6IsValidUnicast (&NewFilter->IpList[Index].v6)) { + NeedPromiscuous = TRUE; + } + } else if ((EFI_NTOHL(Mode->StationIp) != 0) && + (EFI_NTOHL(Mode->SubnetMask) != 0) && + IP4_NET_EQUAL(EFI_NTOHL(Mode->StationIp), EFI_NTOHL(NewFilter->IpList[Index].v4), EFI_NTOHL(Mode->SubnetMask.v4)) && + NetIp4IsUnicast (EFI_IP4 (NewFilter->IpList[Index].v4), EFI_NTOHL(Mode->SubnetMask)) && + ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0)) { + NeedPromiscuous = TRUE; + } + } + + AcceptPromiscuous = FALSE; + AcceptBroadcast = FALSE; + MultiCastUpdate = FALSE; + + if (NeedPromiscuous || + (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0 || + (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) != 0) { + // + // Configure UDPv4/UDPv6 as promiscuous mode to receive all packets. + // + AcceptPromiscuous = TRUE; + } else if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) != 0) { + // + // Configure UDPv4 to receive all broadcast packets. + // + AcceptBroadcast = TRUE; + } + + // + // In multicast condition when Promiscuous FALSE and IpCnt no-zero. + // Here check if there is any update of the multicast ip address. If yes, + // we need leave the old multicast group (by Config UDP instance to NULL), + // and join the new multicast group. + // + if (!AcceptPromiscuous) { + if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0) { + if (Mode->IpFilter.IpCnt != NewFilter->IpCnt) { + MultiCastUpdate = TRUE; + } else if (CompareMem (Mode->IpFilter.IpList, NewFilter->IpList, NewFilter->IpCnt * sizeof (EFI_IP_ADDRESS)) != 0 ) { + MultiCastUpdate = TRUE; + } + } + } + + if (!Mode->UsingIpv6) { + // + // Check whether we need reconfigure the UDP4 instance. + // + Udp4Cfg = &Private->Udp4CfgData; + if ((AcceptPromiscuous != Udp4Cfg->AcceptPromiscuous) || + (AcceptBroadcast != Udp4Cfg->AcceptBroadcast) || MultiCastUpdate) { + // + // Clear the UDP4 instance configuration, all joined groups will be left + // during the operation. + // + Private->Udp4Read->Configure (Private->Udp4Read, NULL); + + // + // Configure the UDP instance with the new configuration. + // + Udp4Cfg->AcceptPromiscuous = AcceptPromiscuous; + Udp4Cfg->AcceptBroadcast = AcceptBroadcast; + Status = Private->Udp4Read->Configure (Private->Udp4Read, Udp4Cfg); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // In not Promiscuous mode, need to join the new multicast group. + // + if (!AcceptPromiscuous) { + for (Index = 0; Index < NewFilter->IpCnt; ++Index) { + if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter->IpList[Index].v4))) { + // + // Join the mutilcast group. + // + Status = Private->Udp4Read->Groups (Private->Udp4Read, TRUE, &NewFilter->IpList[Index].v4); + if (EFI_ERROR (Status)) { + return Status; + } + } + } + } + } + } else { + // + // Check whether we need reconfigure the UDP6 instance. + // + Udp6Cfg = &Private->Udp6CfgData; + if ((AcceptPromiscuous != Udp6Cfg->AcceptPromiscuous) || MultiCastUpdate) { + // + // Clear the UDP6 instance configuration, all joined groups will be left + // during the operation. + // + Private->Udp6Read->Configure (Private->Udp6Read, NULL); + + // + // Configure the UDP instance with the new configuration. + // + Udp6Cfg->AcceptPromiscuous = AcceptPromiscuous; + Status = Private->Udp6Read->Configure (Private->Udp6Read, Udp6Cfg); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // In not Promiscuous mode, need to join the new multicast group. + // + if (!AcceptPromiscuous) { + for (Index = 0; Index < NewFilter->IpCnt; ++Index) { + if (IP6_IS_MULTICAST (&NewFilter->IpList[Index].v6)) { + // + // Join the mutilcast group. + // + Status = Private->Udp6Read->Groups (Private->Udp6Read, TRUE, &NewFilter->IpList[Index].v6); + if (EFI_ERROR (Status)) { + return Status; + } + } + } + } + } + } + + // + // Save the new IP filter into mode data. + // + CopyMem (&Mode->IpFilter, NewFilter, sizeof (Mode->IpFilter)); + + return Status; +} + + +/** + Uses the ARP protocol to resolve a MAC address. It is not supported for IPv6. + + This function uses the ARP protocol to resolve a MAC address. The IP address specified + by IpAddr is used to resolve a MAC address. If the ARP protocol succeeds in resolving + the specified address, then the ArpCacheEntries and ArpCache fields of the mode data + are updated, and EFI_SUCCESS is returned. If MacAddr is not NULL, the resolved + MAC address is placed there as well. If the PXE Base Code protocol is in the + stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters + a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is + returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, + then EFI_ABORTED is returned. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + @param[in] IpAddr Pointer to the IP address that is used to resolve a MAC address. + @param[in] MacAddr If not NULL, a pointer to the MAC address that was resolved with the + ARP protocol. + + @retval EFI_SUCCESS The IP or MAC address was resolved. + @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval EFI_ICMP_ERROR An error occur with the ICMP packet message. + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcArp ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This, + IN EFI_IP_ADDRESS *IpAddr, + IN EFI_MAC_ADDRESS *MacAddr OPTIONAL + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_EVENT ResolvedEvent; + EFI_STATUS Status; + EFI_MAC_ADDRESS TempMac; + EFI_MAC_ADDRESS ZeroMac; + BOOLEAN IsResolved; + + if (This == NULL || IpAddr == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + ResolvedEvent = NULL; + Status = EFI_SUCCESS; + IsResolved = FALSE; + + if (!Mode->Started) { + return EFI_NOT_STARTED; + } + + if (Mode->UsingIpv6) { + return EFI_UNSUPPORTED; + } + + // + // Station address should be ready before do arp. + // + if (!Private->IsAddressOk) { + return EFI_INVALID_PARAMETER; + } + + Mode->IcmpErrorReceived = FALSE; + ZeroMem (&TempMac, sizeof (EFI_MAC_ADDRESS)); + ZeroMem (&ZeroMac, sizeof (EFI_MAC_ADDRESS)); + + if (!Mode->AutoArp) { + // + // If AutoArp is FALSE, only search in the current Arp cache. + // + PxeBcArpCacheUpdate (NULL, Private); + if (!PxeBcCheckArpCache (Mode, &IpAddr->v4, &TempMac)) { + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + } else { + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + PxeBcCommonNotify, + &IsResolved, + &ResolvedEvent + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // If AutoArp is TRUE, try to send Arp request on initiative. + // + Status = Private->Arp->Request (Private->Arp, &IpAddr->v4, ResolvedEvent, &TempMac); + if (EFI_ERROR (Status) && Status != EFI_NOT_READY) { + goto ON_EXIT; + } + + while (!IsResolved) { + if (CompareMem (&TempMac, &ZeroMac, sizeof (EFI_MAC_ADDRESS)) != 0) { + break; + } + } + if (CompareMem (&TempMac, &ZeroMac, sizeof (EFI_MAC_ADDRESS)) != 0) { + Status = EFI_SUCCESS; + } else { + Status = EFI_TIMEOUT; + } + } + + // + // Copy the Mac address to user if needed. + // + if (MacAddr != NULL && !EFI_ERROR (Status)) { + CopyMem (MacAddr, &TempMac, sizeof (EFI_MAC_ADDRESS)); + } + +ON_EXIT: + if (ResolvedEvent != NULL) { + gBS->CloseEvent (ResolvedEvent); + } + return Status; +} + + +/** + Updates the parameters that affect the operation of the PXE Base Code Protocol. + + This function sets parameters that affect the operation of the PXE Base Code Protocol. + The parameter specified by NewAutoArp is used to control the generation of ARP + protocol packets. If NewAutoArp is TRUE, then ARP Protocol packets will be generated + as required by the PXE Base Code Protocol. If NewAutoArp is FALSE, then no ARP + Protocol packets will be generated. In this case, the only mappings that are + available are those stored in the ArpCache of the EFI_PXE_BASE_CODE_MODE structure. + If there are not enough mappings in the ArpCache to perform a PXE Base Code Protocol + service, then the service will fail. This function updates the AutoArp field of + the EFI_PXE_BASE_CODE_MODE structure to NewAutoArp. + The SetParameters() call must be invoked after a Callback Protocol is installed + to enable the use of callbacks. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + @param[in] NewAutoArp If not NULL, a pointer to a value that specifies whether to replace the + current value of AutoARP. + @param[in] NewSendGUID If not NULL, a pointer to a value that specifies whether to replace the + current value of SendGUID. + @param[in] NewTTL If not NULL, a pointer to be used in place of the current value of TTL, + the "time to live" field of the IP header. + @param[in] NewToS If not NULL, a pointer to be used in place of the current value of ToS, + the "type of service" field of the IP header. + @param[in] NewMakeCallback If not NULL, a pointer to a value that specifies whether to replace the + current value of the MakeCallback field of the Mode structure. + + @retval EFI_SUCCESS The new parameters values were updated. + @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcSetParameters ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This, + IN BOOLEAN *NewAutoArp OPTIONAL, + IN BOOLEAN *NewSendGUID OPTIONAL, + IN UINT8 *NewTTL OPTIONAL, + IN UINT8 *NewToS OPTIONAL, + IN BOOLEAN *NewMakeCallback OPTIONAL + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_GUID SystemGuid; + EFI_STATUS Status; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + + if (!Mode->Started) { + return EFI_NOT_STARTED; + } + + if (NewMakeCallback != NULL) { + if (*NewMakeCallback) { + // + // Update the previous PxeBcCallback protocol. + // + Status = gBS->HandleProtocol ( + Mode->UsingIpv6 ? Private->Ip6Nic->Controller : Private->Ip4Nic->Controller, + &gEfiPxeBaseCodeCallbackProtocolGuid, + (VOID **) &Private->PxeBcCallback + ); + + if (EFI_ERROR (Status) || (Private->PxeBcCallback->Callback == NULL)) { + return EFI_INVALID_PARAMETER; + } + } else { + Private->PxeBcCallback = NULL; + } + Mode->MakeCallbacks = *NewMakeCallback; + } + + if (NewSendGUID != NULL) { + if (*NewSendGUID && EFI_ERROR (NetLibGetSystemGuid (&SystemGuid))) { + DEBUG ((EFI_D_WARN, "PXE: Failed to read system GUID from the smbios table!\n")); + return EFI_INVALID_PARAMETER; + } + Mode->SendGUID = *NewSendGUID; + } + + if (NewAutoArp != NULL) { + Mode->AutoArp = *NewAutoArp; + } + + if (NewTTL != NULL) { + Mode->TTL = *NewTTL; + } + + if (NewToS != NULL) { + Mode->ToS = *NewToS; + } + + return EFI_SUCCESS; +} + + +/** + Updates the station IP address and/or subnet mask values of a network device. + + This function updates the station IP address and/or subnet mask values of a network + device. The NewStationIp field is used to modify the network device's current IP address. + If NewStationIP is NULL, then the current IP address will not be modified. Otherwise, + this function updates the StationIp field of the EFI_PXE_BASE_CODE_MODE structure + with NewStationIp. The NewSubnetMask field is used to modify the network device's current subnet + mask. If NewSubnetMask is NULL, then the current subnet mask will not be modified. + Otherwise, this function updates the SubnetMask field of the EFI_PXE_BASE_CODE_MODE + structure with NewSubnetMask. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + @param[in] NewStationIp Pointer to the new IP address to be used by the network device. + @param[in] NewSubnetMask Pointer to the new subnet mask to be used by the network device. + + @retval EFI_SUCCESS The new station IP address and/or subnet mask were updated. + @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcSetStationIP ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This, + IN EFI_IP_ADDRESS *NewStationIp OPTIONAL, + IN EFI_IP_ADDRESS *NewSubnetMask OPTIONAL + ) +{ + EFI_STATUS Status; + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (NewStationIp != NULL && !NetIp6IsValidUnicast (&NewStationIp->v6)) { + return EFI_INVALID_PARAMETER; + } + + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + Status = EFI_SUCCESS; + + if (!Mode->UsingIpv6 && + NewSubnetMask != NULL && + !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask->Addr[0]))) { + return EFI_INVALID_PARAMETER; + } + + if (!Mode->UsingIpv6 && NewStationIp != NULL) { + if (IP4_IS_UNSPECIFIED(NTOHL (NewStationIp->Addr[0])) || + IP4_IS_LOCAL_BROADCAST(NTOHL (NewStationIp->Addr[0])) || + (NewSubnetMask != NULL && NewSubnetMask->Addr[0] != 0 && !NetIp4IsUnicast (NTOHL (NewStationIp->Addr[0]), NTOHL (NewSubnetMask->Addr[0])))) { + return EFI_INVALID_PARAMETER; + } + } + + if (!Mode->Started) { + return EFI_NOT_STARTED; + } + + if (Mode->UsingIpv6 && NewStationIp != NULL) { + // + // Set the IPv6 address by Ip6Config protocol. + // + Status = PxeBcRegisterIp6Address (Private, &NewStationIp->v6); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + } + + if (NewStationIp != NULL) { + CopyMem (&Mode->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS)); + CopyMem (&Private->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS)); + } + + if (!Mode->UsingIpv6 && NewSubnetMask != NULL) { + CopyMem (&Mode->SubnetMask, NewSubnetMask, sizeof (EFI_IP_ADDRESS)); + CopyMem (&Private->SubnetMask ,NewSubnetMask, sizeof (EFI_IP_ADDRESS)); + } + + Status = PxeBcFlushStationIp (Private, NewStationIp, NewSubnetMask); + if (!EFI_ERROR (Status)) { + Private->IsAddressOk = TRUE; + } + +ON_EXIT: + return Status; +} + + +/** + Updates the contents of the cached DHCP and Discover packets. + + The pointers to the new packets are used to update the contents of the cached + packets in the EFI_PXE_BASE_CODE_MODE structure. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance. + @param[in] NewDhcpDiscoverValid Pointer to a value that will replace the current + DhcpDiscoverValid field. + @param[in] NewDhcpAckReceived Pointer to a value that will replace the current + DhcpAckReceived field. + @param[in] NewProxyOfferReceived Pointer to a value that will replace the current + ProxyOfferReceived field. + @param[in] NewPxeDiscoverValid Pointer to a value that will replace the current + ProxyOfferReceived field. + @param[in] NewPxeReplyReceived Pointer to a value that will replace the current + PxeReplyReceived field. + @param[in] NewPxeBisReplyReceived Pointer to a value that will replace the current + PxeBisReplyReceived field. + @param[in] NewDhcpDiscover Pointer to the new cached DHCP Discover packet contents. + @param[in] NewDhcpAck Pointer to the new cached DHCP Ack packet contents. + @param[in] NewProxyOffer Pointer to the new cached Proxy Offer packet contents. + @param[in] NewPxeDiscover Pointer to the new cached PXE Discover packet contents. + @param[in] NewPxeReply Pointer to the new cached PXE Reply packet contents. + @param[in] NewPxeBisReply Pointer to the new cached PXE BIS Reply packet contents. + + @retval EFI_SUCCESS The cached packet contents were updated. + @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state. + @retval EFI_INVALID_PARAMETER This is NULL or does not point to a valid + EFI_PXE_BASE_CODE_PROTOCOL structure. + +**/ +EFI_STATUS +EFIAPI +EfiPxeBcSetPackets ( + IN EFI_PXE_BASE_CODE_PROTOCOL *This, + IN BOOLEAN *NewDhcpDiscoverValid OPTIONAL, + IN BOOLEAN *NewDhcpAckReceived OPTIONAL, + IN BOOLEAN *NewProxyOfferReceived OPTIONAL, + IN BOOLEAN *NewPxeDiscoverValid OPTIONAL, + IN BOOLEAN *NewPxeReplyReceived OPTIONAL, + IN BOOLEAN *NewPxeBisReplyReceived OPTIONAL, + IN EFI_PXE_BASE_CODE_PACKET *NewDhcpDiscover OPTIONAL, + IN EFI_PXE_BASE_CODE_PACKET *NewDhcpAck OPTIONAL, + IN EFI_PXE_BASE_CODE_PACKET *NewProxyOffer OPTIONAL, + IN EFI_PXE_BASE_CODE_PACKET *NewPxeDiscover OPTIONAL, + IN EFI_PXE_BASE_CODE_PACKET *NewPxeReply OPTIONAL, + IN EFI_PXE_BASE_CODE_PACKET *NewPxeBisReply OPTIONAL + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + + if (This == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This); + Mode = Private->PxeBc.Mode; + + if (!Mode->Started) { + return EFI_NOT_STARTED; + } + + if (NewDhcpDiscoverValid != NULL) { + Mode->DhcpDiscoverValid = *NewDhcpDiscoverValid; + } + + if (NewDhcpAckReceived != NULL) { + Mode->DhcpAckReceived = *NewDhcpAckReceived; + } + + if (NewProxyOfferReceived != NULL) { + Mode->ProxyOfferReceived = *NewProxyOfferReceived; + } + + if (NewPxeDiscoverValid != NULL) { + Mode->PxeDiscoverValid = *NewPxeDiscoverValid; + } + + if (NewPxeReplyReceived != NULL) { + Mode->PxeReplyReceived = *NewPxeReplyReceived; + } + + if (NewPxeBisReplyReceived != NULL) { + Mode->PxeBisReplyReceived = *NewPxeBisReplyReceived; + } + + if (NewDhcpDiscover != NULL) { + CopyMem (&Mode->DhcpDiscover, NewDhcpDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET)); + } + + if (NewDhcpAck != NULL) { + CopyMem (&Mode->DhcpAck, NewDhcpAck, sizeof (EFI_PXE_BASE_CODE_PACKET)); + } + + if (NewProxyOffer != NULL) { + CopyMem (&Mode->ProxyOffer, NewProxyOffer, sizeof (EFI_PXE_BASE_CODE_PACKET)); + } + + if (NewPxeDiscover != NULL) { + CopyMem (&Mode->PxeDiscover, NewPxeDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET)); + } + + if (NewPxeReply != NULL) { + CopyMem (&Mode->PxeReply, NewPxeReply, sizeof (EFI_PXE_BASE_CODE_PACKET)); + } + + if (NewPxeBisReply != NULL) { + CopyMem (&Mode->PxeBisReply, NewPxeBisReply, sizeof (EFI_PXE_BASE_CODE_PACKET)); + } + + return EFI_SUCCESS; +} + +EFI_PXE_BASE_CODE_PROTOCOL gPxeBcProtocolTemplate = { + EFI_PXE_BASE_CODE_PROTOCOL_REVISION, + EfiPxeBcStart, + EfiPxeBcStop, + EfiPxeBcDhcp, + EfiPxeBcDiscover, + EfiPxeBcMtftp, + EfiPxeBcUdpWrite, + EfiPxeBcUdpRead, + EfiPxeBcSetIpFilter, + EfiPxeBcArp, + EfiPxeBcSetParameters, + EfiPxeBcSetStationIP, + EfiPxeBcSetPackets, + NULL +}; + + +/** + Callback function that is invoked when the PXE Base Code Protocol is about to transmit, has + received, or is waiting to receive a packet. + + This function is invoked when the PXE Base Code Protocol is about to transmit, has received, + or is waiting to receive a packet. Parameters Function and Received specify the type of event. + Parameters PacketLen and Packet specify the packet that generated the event. If these fields + are zero and NULL respectively, then this is a status update callback. If the operation specified + by Function is to continue, then CALLBACK_STATUS_CONTINUE should be returned. If the operation + specified by Function should be aborted, then CALLBACK_STATUS_ABORT should be returned. Due to + the polling nature of UEFI device drivers, a callback function should not execute for more than 5 ms. + The SetParameters() function must be called after a Callback Protocol is installed to enable the + use of callbacks. + + @param[in] This Pointer to the EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL instance. + @param[in] Function The PXE Base Code Protocol function that is waiting for an event. + @param[in] Received TRUE if the callback is being invoked due to a receive event. FALSE if + the callback is being invoked due to a transmit event. + @param[in] PacketLength The length, in bytes, of Packet. This field will have a value of zero if + this is a wait for receive event. + @param[in] PacketPtr If Received is TRUE, a pointer to the packet that was just received; + otherwise a pointer to the packet that is about to be transmitted. + + @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE If Function specifies a continue operation. + @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT If Function specifies an abort operation. + +**/ +EFI_PXE_BASE_CODE_CALLBACK_STATUS +EFIAPI +EfiPxeLoadFileCallback ( + IN EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *This, + IN EFI_PXE_BASE_CODE_FUNCTION Function, + IN BOOLEAN Received, + IN UINT32 PacketLength, + IN EFI_PXE_BASE_CODE_PACKET *PacketPtr OPTIONAL + ) +{ + EFI_INPUT_KEY Key; + EFI_STATUS Status; + + // + // Catch Ctrl-C or ESC to abort. + // + Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); + + if (!EFI_ERROR (Status)) { + + if (Key.ScanCode == SCAN_ESC || Key.UnicodeChar == (0x1F & 'c')) { + + return EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT; + } + } + // + // No print if receive packet + // + if (Received) { + return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE; + } + // + // Print only for three functions + // + switch (Function) { + + case EFI_PXE_BASE_CODE_FUNCTION_MTFTP: + // + // Print only for open MTFTP packets, not every MTFTP packets + // + if (PacketLength != 0 && PacketPtr != NULL) { + if (PacketPtr->Raw[0x1C] != 0x00 || PacketPtr->Raw[0x1D] != 0x01) { + return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE; + } + } + break; + + case EFI_PXE_BASE_CODE_FUNCTION_DHCP: + case EFI_PXE_BASE_CODE_FUNCTION_DISCOVER: + break; + + default: + return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE; + } + + if (PacketLength != 0 && PacketPtr != NULL) { + // + // Print '.' when transmit a packet + // + AsciiPrint ("."); + } + + return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE; +} + +EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL gPxeBcCallBackTemplate = { + EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL_REVISION, + EfiPxeLoadFileCallback +}; + + +/** + Causes the driver to load a specified file. + + @param[in] This Protocol instance pointer. + @param[in] FilePath The device specific path of the file to load. + @param[in] BootPolicy If TRUE, indicates that the request originates from the + boot manager is attempting to load FilePath as a boot + selection. If FALSE, then FilePath must match an exact file + to be loaded. + @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return + code of EFI_SUCCESS, the amount of data transferred to + Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL, + the size of Buffer required to retrieve the requested file. + @param[in] Buffer The memory buffer to transfer the file to. IF Buffer is NULL, + then no the size of the requested file is returned in + BufferSize. + + @retval EFI_SUCCESS The file was loaded. + @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy. + @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or + BufferSize is NULL. + @retval EFI_NO_MEDIA No medium was present to load the file. + @retval EFI_DEVICE_ERROR The file was not loaded due to a device error. + @retval EFI_NO_RESPONSE The remote system did not respond. + @retval EFI_NOT_FOUND The file was not found. + @retval EFI_ABORTED The file load process was manually cancelled. + +**/ +EFI_STATUS +EFIAPI +EfiPxeLoadFile ( + IN EFI_LOAD_FILE_PROTOCOL *This, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath, + IN BOOLEAN BootPolicy, + IN OUT UINTN *BufferSize, + IN VOID *Buffer OPTIONAL + ) +{ + PXEBC_PRIVATE_DATA *Private; + PXEBC_VIRTUAL_NIC *VirtualNic; + EFI_PXE_BASE_CODE_PROTOCOL *PxeBc; + BOOLEAN UsingIpv6; + EFI_STATUS Status; + EFI_STATUS MediaStatus; + + if (This == NULL || BufferSize == NULL || FilePath == NULL || !IsDevicePathEnd (FilePath)) { + return EFI_INVALID_PARAMETER; + } + + // + // Only support BootPolicy + // + if (!BootPolicy) { + return EFI_UNSUPPORTED; + } + + VirtualNic = PXEBC_VIRTUAL_NIC_FROM_LOADFILE (This); + Private = VirtualNic->Private; + PxeBc = &Private->PxeBc; + UsingIpv6 = FALSE; + Status = EFI_DEVICE_ERROR; + + // + // Check media status before PXE start + // + MediaStatus = EFI_SUCCESS; + NetLibDetectMediaWaitTimeout (Private->Controller, PXEBC_CHECK_MEDIA_WAITING_TIME, &MediaStatus); + if (MediaStatus != EFI_SUCCESS) { + return EFI_NO_MEDIA; + } + + // + // Check whether the virtual nic is using IPv6 or not. + // + if (VirtualNic == Private->Ip6Nic) { + UsingIpv6 = TRUE; + } + + // + // Start Pxe Base Code to initialize PXE boot. + // + Status = PxeBc->Start (PxeBc, UsingIpv6); + if (Status == EFI_ALREADY_STARTED && UsingIpv6 != PxeBc->Mode->UsingIpv6) { + // + // PxeBc protocol has already been started but not on the required IP version, restart it. + // + Status = PxeBc->Stop (PxeBc); + if (!EFI_ERROR (Status)) { + Status = PxeBc->Start (PxeBc, UsingIpv6); + } + } + if (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED) { + Status = PxeBcLoadBootFile (Private, BufferSize, Buffer); + } + + if (Status != EFI_SUCCESS && + Status != EFI_UNSUPPORTED && + Status != EFI_BUFFER_TOO_SMALL) { + // + // There are three cases, which needn't stop pxebc here. + // 1. success to download file. + // 2. success to get file size. + // 3. unsupported. + // + PxeBc->Stop (PxeBc); + } else { + // + // The DHCP4 can have only one configured child instance so we need to stop + // reset the DHCP4 child before we return. Otherwise these programs which + // also need to use DHCP4 will be impacted. + // + if (!PxeBc->Mode->UsingIpv6) { + Private->Dhcp4->Stop (Private->Dhcp4); + Private->Dhcp4->Configure (Private->Dhcp4, NULL); + } + } + + return Status; +} + +EFI_LOAD_FILE_PROTOCOL gLoadFileProtocolTemplate = { EfiPxeLoadFile }; + diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcImpl.h b/NetworkPkg/UefiPxeBcDxe/PxeBcImpl.h new file mode 100644 index 000000000..fa4950c62 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcImpl.h @@ -0,0 +1,228 @@ +/** @file + This EFI_PXE_BASE_CODE_PROTOCOL and EFI_LOAD_FILE_PROTOCOL. + interfaces declaration. + + Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_PXEBC_IMPL_H__ +#define __EFI_PXEBC_IMPL_H__ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct _PXEBC_PRIVATE_DATA PXEBC_PRIVATE_DATA; +typedef struct _PXEBC_PRIVATE_PROTOCOL PXEBC_PRIVATE_PROTOCOL; +typedef struct _PXEBC_VIRTUAL_NIC PXEBC_VIRTUAL_NIC; + +#include "PxeBcDriver.h" +#include "PxeBcDhcp4.h" +#include "PxeBcDhcp6.h" +#include "PxeBcMtftp.h" +#include "PxeBcBoot.h" +#include "PxeBcSupport.h" + +#define PXEBC_DEFAULT_HOPLIMIT 64 +#define PXEBC_DEFAULT_LIFETIME 50000 // 50 ms, unit is microsecond +#define PXEBC_UDP_TIMEOUT 30000000 // 3 seconds, unit is 100nanosecond +#define PXEBC_DAD_ADDITIONAL_DELAY 30000000 // 3 seconds +#define PXEBC_MTFTP_TIMEOUT 4 +#define PXEBC_MTFTP_RETRIES 6 +#define PXEBC_DHCP_RETRIES 4 // refers to mPxeDhcpTimeout, also by PXE2.1 spec. +#define PXEBC_MENU_MAX_NUM 24 +#define PXEBC_OFFER_MAX_NUM 16 + +#define PXEBC_CHECK_MEDIA_WAITING_TIME EFI_TIMER_PERIOD_SECONDS(20) + +#define PXEBC_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('P', 'X', 'E', 'P') +#define PXEBC_VIRTUAL_NIC_SIGNATURE SIGNATURE_32 ('P', 'X', 'E', 'V') +#define PXEBC_PRIVATE_DATA_FROM_PXEBC(a) CR (a, PXEBC_PRIVATE_DATA, PxeBc, PXEBC_PRIVATE_DATA_SIGNATURE) +#define PXEBC_PRIVATE_DATA_FROM_ID(a) CR (a, PXEBC_PRIVATE_DATA, Id, PXEBC_PRIVATE_DATA_SIGNATURE) +#define PXEBC_VIRTUAL_NIC_FROM_LOADFILE(a) CR (a, PXEBC_VIRTUAL_NIC, LoadFile, PXEBC_VIRTUAL_NIC_SIGNATURE) + +#define PXE_ENABLED 0x01 +#define PXE_DISABLED 0x00 + +typedef union { + PXEBC_DHCP4_PACKET_CACHE Dhcp4; + PXEBC_DHCP6_PACKET_CACHE Dhcp6; +} PXEBC_DHCP_PACKET_CACHE; + +struct _PXEBC_PRIVATE_PROTOCOL { + UINT64 Reserved; +}; + +struct _PXEBC_VIRTUAL_NIC { + UINT32 Signature; + EFI_HANDLE Controller; + EFI_LOAD_FILE_PROTOCOL LoadFile; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + PXEBC_PRIVATE_DATA *Private; +}; + +struct _PXEBC_PRIVATE_DATA { + UINT32 Signature; + EFI_HANDLE Controller; + EFI_HANDLE Image; + + PXEBC_PRIVATE_PROTOCOL Id; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + + PXEBC_VIRTUAL_NIC *Ip4Nic; + PXEBC_VIRTUAL_NIC *Ip6Nic; + + EFI_HANDLE ArpChild; + EFI_HANDLE Ip4Child; + EFI_HANDLE Dhcp4Child; + EFI_HANDLE Mtftp4Child; + EFI_HANDLE Udp4ReadChild; + EFI_HANDLE Udp4WriteChild; + + EFI_ARP_PROTOCOL *Arp; + EFI_IP4_PROTOCOL *Ip4; + EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2; + EFI_DHCP4_PROTOCOL *Dhcp4; + EFI_MTFTP4_PROTOCOL *Mtftp4; + EFI_UDP4_PROTOCOL *Udp4Read; + EFI_UDP4_PROTOCOL *Udp4Write; + + EFI_HANDLE Ip6Child; + EFI_HANDLE Dhcp6Child; + EFI_HANDLE Mtftp6Child; + EFI_HANDLE Udp6ReadChild; + EFI_HANDLE Udp6WriteChild; + + EFI_IP6_PROTOCOL *Ip6; + EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg; + EFI_DHCP6_PROTOCOL *Dhcp6; + EFI_MTFTP6_PROTOCOL *Mtftp6; + EFI_UDP6_PROTOCOL *Udp6Read; + EFI_UDP6_PROTOCOL *Udp6Write; + EFI_DNS6_PROTOCOL *Dns6; + + EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *Nii; + EFI_PXE_BASE_CODE_PROTOCOL PxeBc; + EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL LoadFileCallback; + EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *PxeBcCallback; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + + EFI_PXE_BASE_CODE_MODE Mode; + EFI_PXE_BASE_CODE_FUNCTION Function; + UINT32 Ip6Policy; + UINT32 SolicitTimes; + UINT64 ElapsedTime; + + EFI_UDP4_CONFIG_DATA Udp4CfgData; + EFI_UDP6_CONFIG_DATA Udp6CfgData; + EFI_IP4_CONFIG_DATA Ip4CfgData; + EFI_IP6_CONFIG_DATA Ip6CfgData; + + EFI_EVENT UdpTimeOutEvent; + EFI_EVENT ArpUpdateEvent; + EFI_IP4_COMPLETION_TOKEN IcmpToken; + EFI_IP6_COMPLETION_TOKEN Icmp6Token; + + BOOLEAN IsAddressOk; + BOOLEAN IsOfferSorted; + BOOLEAN IsProxyRecved; + BOOLEAN IsDoDiscover; + + EFI_IP_ADDRESS TmpStationIp; + EFI_IP_ADDRESS StationIp; + EFI_IP_ADDRESS SubnetMask; + EFI_IP_ADDRESS GatewayIp; + EFI_IP_ADDRESS ServerIp; + EFI_IPv6_ADDRESS *DnsServer; + UINT16 CurSrcPort; + UINT32 IaId; + + UINT32 Ip4MaxPacketSize; + UINT32 Ip6MaxPacketSize; + UINT8 *BootFileName; + UINTN BootFileSize; + UINTN BlockSize; + + PXEBC_DHCP_PACKET_CACHE ProxyOffer; + PXEBC_DHCP_PACKET_CACHE DhcpAck; + PXEBC_DHCP_PACKET_CACHE PxeReply; + EFI_DHCP6_PACKET *Dhcp6Request; + EFI_DHCP4_PACKET SeedPacket; + + // + // OfferIndex records the index of DhcpOffer[] buffer, and OfferCount records the num of each type of offer. + // + // It supposed that + // + // OfferNum: 8 + // OfferBuffer: [ProxyBinl, ProxyBinl, DhcpOnly, ProxyPxe10, DhcpOnly, DhcpPxe10, DhcpBinl, ProxyBinl] + // (OfferBuffer is 0-based.) + // + // And assume that (DhcpPxe10 is the first priority actually.) + // + // SelectIndex: 2 + // SelectProxyType: PXEBC_OFFER_TYPE_PROXY_BINL + // (SelectIndex is 1-based, and 0 means no one is selected.) + // + // So it should be + // + // DhcpOnly DhcpPxe10 DhcpWfm11a DhcpBinl ProxyPxe10 ProxyWfm11a ProxyBinl Bootp + // OfferCount: [ 2(n), 1(n), 0(n), 1(n), 1(1), 0(1), 3(n), 1(1)] + // + // OfferIndex: {[ 2, 5, 0, 6, 3, 0, *0, 0] + // [ 4, 0, 0, 0, 0, 0, 1, 0] + // [ 0, 0, 0, 0, 0, 0, 7, 0] + // ... ]} + // (OfferIndex is 0-based.) + // + // + UINT32 SelectIndex; + UINT32 SelectProxyType; + PXEBC_DHCP_PACKET_CACHE OfferBuffer[PXEBC_OFFER_MAX_NUM]; + UINT32 OfferNum; + UINT32 OfferCount[PxeOfferTypeMax]; + UINT32 OfferIndex[PxeOfferTypeMax][PXEBC_OFFER_MAX_NUM]; +}; + +extern EFI_PXE_BASE_CODE_PROTOCOL gPxeBcProtocolTemplate; +extern EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL gPxeBcCallBackTemplate; +extern EFI_LOAD_FILE_PROTOCOL gLoadFileProtocolTemplate; + +#endif diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcMtftp.c b/NetworkPkg/UefiPxeBcDxe/PxeBcMtftp.c new file mode 100644 index 000000000..6a390c249 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcMtftp.c @@ -0,0 +1,1187 @@ +/** @file + Functions implementation related with Mtftp for UefiPxeBc Driver. + + Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "PxeBcImpl.h" + +CHAR8 *mMtftpOptions[PXE_MTFTP_OPTION_MAXIMUM_INDEX] = { + "blksize", + "timeout", + "tsize", + "multicast", + "windowsize" +}; + + +/** + This is a callback function when packets are received or transmitted in Mtftp driver. + + A callback function that is provided by the caller to intercept + the EFI_MTFTP6_OPCODE_DATA or EFI_MTFTP6_OPCODE_DATA8 packets processed in the + EFI_MTFTP6_PROTOCOL.ReadFile() function, and alternatively to intercept + EFI_MTFTP6_OPCODE_OACK or EFI_MTFTP6_OPCODE_ERROR packets during a call to + EFI_MTFTP6_PROTOCOL.ReadFile(), WriteFile() or ReadDirectory(). + + @param[in] This Pointer to EFI_MTFTP6_PROTOCOL. + @param[in] Token Pointer to EFI_MTFTP6_TOKEN. + @param[in] PacketLen Length of EFI_MTFTP6_PACKET. + @param[in] Packet Pointer to EFI_MTFTP6_PACKET to be checked. + + @retval EFI_SUCCESS The current operation succeeded. + @retval EFI_ABORTED Abort the current transfer process. + +**/ +EFI_STATUS +EFIAPI +PxeBcMtftp6CheckPacket ( + IN EFI_MTFTP6_PROTOCOL *This, + IN EFI_MTFTP6_TOKEN *Token, + IN UINT16 PacketLen, + IN EFI_MTFTP6_PACKET *Packet + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback; + EFI_STATUS Status; + + Private = (PXEBC_PRIVATE_DATA *) Token->Context; + Callback = Private->PxeBcCallback; + Status = EFI_SUCCESS; + + if (Packet->OpCode == EFI_MTFTP6_OPCODE_ERROR) { + // + // Store the tftp error message into mode data and set the received flag. + // + Private->Mode.TftpErrorReceived = TRUE; + Private->Mode.TftpError.ErrorCode = (UINT8) Packet->Error.ErrorCode; + AsciiStrnCpyS ( + Private->Mode.TftpError.ErrorString, + PXE_MTFTP_ERROR_STRING_LENGTH, + (CHAR8 *) Packet->Error.ErrorMessage, + PXE_MTFTP_ERROR_STRING_LENGTH - 1 + ); + Private->Mode.TftpError.ErrorString[PXE_MTFTP_ERROR_STRING_LENGTH - 1] = '\0'; + } + + if (Callback != NULL) { + // + // Callback to user if has when received any tftp packet. + // + Status = Callback->Callback ( + Callback, + Private->Function, + TRUE, + PacketLen, + (EFI_PXE_BASE_CODE_PACKET *) Packet + ); + if (Status != EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE) { + // + // User wants to abort current process if not EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE. + // + Status = EFI_ABORTED; + } else { + // + // User wants to continue current process if EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE. + // + Status = EFI_SUCCESS; + } + } + + return Status; +} + + +/** + This function is to get the size of a file using Tftp. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to EFI_MTFTP6_CONFIG_DATA. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in, out] BufferSize Pointer to buffer size. + + @retval EFI_SUCCESS Sucessfully obtained the size of file. + @retval EFI_NOT_FOUND Parse the tftp ptions failed. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Has not obtained the size of the file. + +**/ +EFI_STATUS +PxeBcMtftp6GetFileSize ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_MTFTP6_CONFIG_DATA *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN OUT UINT64 *BufferSize + ) +{ + EFI_MTFTP6_PROTOCOL *Mtftp6; + EFI_MTFTP6_OPTION ReqOpt[3]; + EFI_MTFTP6_PACKET *Packet; + EFI_MTFTP6_OPTION *Option; + UINT32 PktLen; + UINT8 OptBuf[PXE_MTFTP_OPTBUF_MAXNUM_INDEX]; + UINTN OptBufSize; + UINT32 OptCnt; + EFI_STATUS Status; + + *BufferSize = 0; + Status = EFI_DEVICE_ERROR; + Mtftp6 = Private->Mtftp6; + Packet = NULL; + Option = NULL; + PktLen = 0; + OptBufSize = PXE_MTFTP_OPTBUF_MAXNUM_INDEX; + OptCnt = 1; + Config->InitialServerPort = PXEBC_BS_DOWNLOAD_PORT; + + Status = Mtftp6->Configure (Mtftp6, Config); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Build the required options for get info. + // + ReqOpt[0].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_TSIZE_INDEX]; + PxeBcUintnToAscDec (0, OptBuf, OptBufSize); + ReqOpt[0].ValueStr = OptBuf; + + if (BlockSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = (UINT8 *) (ReqOpt[OptCnt-1].ValueStr + AsciiStrLen ((CHAR8 *) ReqOpt[OptCnt-1].ValueStr) + 1); + OptBufSize -= (AsciiStrLen ((CHAR8 *) ReqOpt[OptCnt-1].ValueStr) + 1); + PxeBcUintnToAscDec (*BlockSize, ReqOpt[OptCnt].ValueStr, OptBufSize); + OptCnt++; + } + + if (WindowSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = (UINT8 *) (ReqOpt[OptCnt-1].ValueStr + AsciiStrLen ((CHAR8 *) ReqOpt[OptCnt-1].ValueStr) + 1); + OptBufSize -= (AsciiStrLen ((CHAR8 *) ReqOpt[OptCnt-1].ValueStr) + 1); + PxeBcUintnToAscDec (*WindowSize, ReqOpt[OptCnt].ValueStr, OptBufSize); + OptCnt++; + } + + Status = Mtftp6->GetInfo ( + Mtftp6, + NULL, + Filename, + NULL, + (UINT8) OptCnt, + ReqOpt, + &PktLen, + &Packet + ); + if (EFI_ERROR (Status)) { + if (Status == EFI_TFTP_ERROR) { + // + // Store the tftp error message into mode data and set the received flag. + // + Private->Mode.TftpErrorReceived = TRUE; + Private->Mode.TftpError.ErrorCode = (UINT8) Packet->Error.ErrorCode; + AsciiStrnCpyS ( + Private->Mode.TftpError.ErrorString, + PXE_MTFTP_ERROR_STRING_LENGTH, + (CHAR8 *) Packet->Error.ErrorMessage, + PXE_MTFTP_ERROR_STRING_LENGTH - 1 + ); + Private->Mode.TftpError.ErrorString[PXE_MTFTP_ERROR_STRING_LENGTH - 1] = '\0'; + } + goto ON_ERROR; + } + + // + // Parse the options in the reply packet. + // + OptCnt = 0; + Status = Mtftp6->ParseOptions ( + Mtftp6, + PktLen, + Packet, + (UINT32 *) &OptCnt, + &Option + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Parse out the value of "tsize" option. + // + Status = EFI_NOT_FOUND; + while (OptCnt != 0) { + if (AsciiStrnCmp ((CHAR8 *) Option[OptCnt - 1].OptionStr, "tsize", 5) == 0) { + *BufferSize = AsciiStrDecimalToUint64 ((CHAR8 *) (Option[OptCnt - 1].ValueStr)); + Status = EFI_SUCCESS; + } + OptCnt--; + } + FreePool (Option); + +ON_ERROR: + if (Packet != NULL) { + FreePool (Packet); + } + Mtftp6->Configure (Mtftp6, NULL); + + return Status; +} + + +/** + This function is to get data of a file using Tftp. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to EFI_MTFTP6_CONFIG_DATA. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + @param[in] DontUseBuffer Indicates whether with a receive buffer. + + @retval EFI_SUCCESS Successfully read the data from the special file. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Read data from file failed. + +**/ +EFI_STATUS +PxeBcMtftp6ReadFile ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_MTFTP6_CONFIG_DATA *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize, + IN BOOLEAN DontUseBuffer + ) +{ + EFI_MTFTP6_PROTOCOL *Mtftp6; + EFI_MTFTP6_TOKEN Token; + EFI_MTFTP6_OPTION ReqOpt[2]; + UINT32 OptCnt; + UINT8 BlksizeBuf[10]; + UINT8 WindowsizeBuf[10]; + EFI_STATUS Status; + + Status = EFI_DEVICE_ERROR; + Mtftp6 = Private->Mtftp6; + OptCnt = 0; + Config->InitialServerPort = PXEBC_BS_DOWNLOAD_PORT; + + Status = Mtftp6->Configure (Mtftp6, Config); + if (EFI_ERROR (Status)) { + return Status; + } + + if (BlockSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = BlksizeBuf; + PxeBcUintnToAscDec (*BlockSize, ReqOpt[OptCnt].ValueStr, sizeof (BlksizeBuf)); + OptCnt++; + } + + if (WindowSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = WindowsizeBuf; + PxeBcUintnToAscDec (*WindowSize, ReqOpt[OptCnt].ValueStr, sizeof (WindowsizeBuf)); + OptCnt++; + } + + + Token.Event = NULL; + Token.OverrideData = NULL; + Token.Filename = Filename; + Token.ModeStr = NULL; + Token.OptionCount = OptCnt; + Token.OptionList = ReqOpt; + Token.Context = Private; + + if (DontUseBuffer) { + Token.BufferSize = 0; + Token.Buffer = NULL; + } else { + Token.BufferSize = *BufferSize; + Token.Buffer = BufferPtr; + } + + Token.CheckPacket = PxeBcMtftp6CheckPacket; + Token.TimeoutCallback = NULL; + Token.PacketNeeded = NULL; + + Status = Mtftp6->ReadFile (Mtftp6, &Token); + // + // Get the real size of received buffer. + // + *BufferSize = Token.BufferSize; + + Mtftp6->Configure (Mtftp6, NULL); + + return Status; +} + + +/** + This function is used to write the data of a file using Tftp. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to EFI_MTFTP6_CONFIG_DATA. + @param[in] Filename Pointer to boot file name. + @param[in] Overwrite Indicate whether with overwrite attribute. + @param[in] BlockSize Pointer to required block size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + + @retval EFI_SUCCESS Successfully wrote the data into a special file. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval other Write data into file failed. + +**/ +EFI_STATUS +PxeBcMtftp6WriteFile ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_MTFTP6_CONFIG_DATA *Config, + IN UINT8 *Filename, + IN BOOLEAN Overwrite, + IN UINTN *BlockSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize + ) +{ + EFI_MTFTP6_PROTOCOL *Mtftp6; + EFI_MTFTP6_TOKEN Token; + EFI_MTFTP6_OPTION ReqOpt[1]; + UINT32 OptCnt; + UINT8 OptBuf[128]; + EFI_STATUS Status; + + Status = EFI_DEVICE_ERROR; + Mtftp6 = Private->Mtftp6; + OptCnt = 0; + Config->InitialServerPort = PXEBC_BS_DOWNLOAD_PORT; + + Status = Mtftp6->Configure (Mtftp6, Config); + if (EFI_ERROR (Status)) { + return Status; + } + + if (BlockSize != NULL) { + ReqOpt[0].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; + ReqOpt[0].ValueStr = OptBuf; + PxeBcUintnToAscDec (*BlockSize, ReqOpt[0].ValueStr, PXE_MTFTP_OPTBUF_MAXNUM_INDEX); + OptCnt++; + } + + Token.Event = NULL; + Token.OverrideData = NULL; + Token.Filename = Filename; + Token.ModeStr = NULL; + Token.OptionCount = OptCnt; + Token.OptionList = ReqOpt; + Token.BufferSize = *BufferSize; + Token.Buffer = BufferPtr; + Token.CheckPacket = PxeBcMtftp6CheckPacket; + Token.TimeoutCallback = NULL; + Token.PacketNeeded = NULL; + + Status = Mtftp6->WriteFile (Mtftp6, &Token); + // + // Get the real size of transmitted buffer. + // + *BufferSize = Token.BufferSize; + + Mtftp6->Configure (Mtftp6, NULL); + + return Status; +} + + +/** + This function is to read the data (file) from a directory using Tftp. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to EFI_MTFTP6_CONFIG_DATA. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + @param[in] DontUseBuffer Indicates whether to use a receive buffer. + + @retval EFI_SUCCESS Successfully obtained the data from the file included in directory. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Operation failed. + +**/ +EFI_STATUS +PxeBcMtftp6ReadDirectory ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_MTFTP6_CONFIG_DATA *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize, + IN BOOLEAN DontUseBuffer + ) +{ + EFI_MTFTP6_PROTOCOL *Mtftp6; + EFI_MTFTP6_TOKEN Token; + EFI_MTFTP6_OPTION ReqOpt[2]; + UINT32 OptCnt; + UINT8 BlksizeBuf[10]; + UINT8 WindowsizeBuf[10]; + EFI_STATUS Status; + + Status = EFI_DEVICE_ERROR; + Mtftp6 = Private->Mtftp6; + OptCnt = 0; + Config->InitialServerPort = PXEBC_BS_DOWNLOAD_PORT; + + Status = Mtftp6->Configure (Mtftp6, Config); + if (EFI_ERROR (Status)) { + return Status; + } + + if (BlockSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = BlksizeBuf; + PxeBcUintnToAscDec (*BlockSize, ReqOpt[OptCnt].ValueStr, sizeof (BlksizeBuf)); + OptCnt++; + } + + if (WindowSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = WindowsizeBuf; + PxeBcUintnToAscDec (*WindowSize, ReqOpt[OptCnt].ValueStr, sizeof (WindowsizeBuf)); + OptCnt++; + } + + Token.Event = NULL; + Token.OverrideData = NULL; + Token.Filename = Filename; + Token.ModeStr = NULL; + Token.OptionCount = OptCnt; + Token.OptionList = ReqOpt; + Token.Context = Private; + + if (DontUseBuffer) { + Token.BufferSize = 0; + Token.Buffer = NULL; + } else { + Token.BufferSize = *BufferSize; + Token.Buffer = BufferPtr; + } + + Token.CheckPacket = PxeBcMtftp6CheckPacket; + Token.TimeoutCallback = NULL; + Token.PacketNeeded = NULL; + + Status = Mtftp6->ReadDirectory (Mtftp6, &Token); + // + // Get the real size of received buffer. + // + *BufferSize = Token.BufferSize; + + Mtftp6->Configure (Mtftp6, NULL); + + return Status; +} + + +/** + This is a callback function when packets are received or transmitted in Mtftp driver. + + A callback function that is provided by the caller to intercept + the EFI_MTFTP6_OPCODE_DATA or EFI_MTFTP4_OPCODE_DATA8 packets processed in the + EFI_MTFTP4_PROTOCOL.ReadFile() function, and alternatively to intercept + EFI_MTFTP4_OPCODE_OACK or EFI_MTFTP4_OPCODE_ERROR packets during a call to + EFI_MTFTP4_PROTOCOL.ReadFile(), WriteFile() or ReadDirectory(). + + @param[in] This Pointer to EFI_MTFTP4_PROTOCOL. + @param[in] Token Pointer to EFI_MTFTP4_TOKEN. + @param[in] PacketLen Length of EFI_MTFTP4_PACKET. + @param[in] Packet Pointer to EFI_MTFTP4_PACKET to be checked. + + @retval EFI_SUCCESS The current operation succeeeded. + @retval EFI_ABORTED Abort the current transfer process. + +**/ +EFI_STATUS +EFIAPI +PxeBcMtftp4CheckPacket ( + IN EFI_MTFTP4_PROTOCOL *This, + IN EFI_MTFTP4_TOKEN *Token, + IN UINT16 PacketLen, + IN EFI_MTFTP4_PACKET *Packet + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback; + EFI_STATUS Status; + + Private = (PXEBC_PRIVATE_DATA *) Token->Context; + Callback = Private->PxeBcCallback; + Status = EFI_SUCCESS; + + if (Packet->OpCode == EFI_MTFTP4_OPCODE_ERROR) { + // + // Store the tftp error message into mode data and set the received flag. + // + Private->Mode.TftpErrorReceived = TRUE; + Private->Mode.TftpError.ErrorCode = (UINT8) Packet->Error.ErrorCode; + AsciiStrnCpyS ( + Private->Mode.TftpError.ErrorString, + PXE_MTFTP_ERROR_STRING_LENGTH, + (CHAR8 *) Packet->Error.ErrorMessage, + PXE_MTFTP_ERROR_STRING_LENGTH - 1 + ); + Private->Mode.TftpError.ErrorString[PXE_MTFTP_ERROR_STRING_LENGTH - 1] = '\0'; + } + + if (Callback != NULL) { + // + // Callback to user if has when received any tftp packet. + // + Status = Callback->Callback ( + Callback, + Private->Function, + TRUE, + PacketLen, + (EFI_PXE_BASE_CODE_PACKET *) Packet + ); + if (Status != EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE) { + // + // User wants to abort current process if not EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE. + // + Status = EFI_ABORTED; + } else { + // + // User wants to continue current process if EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE. + // + Status = EFI_SUCCESS; + } + } + + return Status; +} + + +/** + This function is to get size of a file using Tftp. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to EFI_MTFTP4_CONFIG_DATA. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in, out] BufferSize Pointer to buffer size. + + @retval EFI_SUCCESS Successfully obtained the size of file. + @retval EFI_NOT_FOUND Parse the tftp options failed. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Did not obtain the size of the file. + +**/ +EFI_STATUS +PxeBcMtftp4GetFileSize ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_MTFTP4_CONFIG_DATA *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN OUT UINT64 *BufferSize + ) +{ + EFI_MTFTP4_PROTOCOL *Mtftp4; + EFI_MTFTP4_OPTION ReqOpt[3]; + EFI_MTFTP4_PACKET *Packet; + EFI_MTFTP4_OPTION *Option; + UINT32 PktLen; + UINT8 OptBuf[PXE_MTFTP_OPTBUF_MAXNUM_INDEX]; + UINTN OptBufSize; + UINT32 OptCnt; + EFI_STATUS Status; + + *BufferSize = 0; + Status = EFI_DEVICE_ERROR; + Mtftp4 = Private->Mtftp4; + Packet = NULL; + Option = NULL; + PktLen = 0; + OptBufSize = PXE_MTFTP_OPTBUF_MAXNUM_INDEX; + OptCnt = 1; + Config->InitialServerPort = PXEBC_BS_DOWNLOAD_PORT; + + Status = Mtftp4->Configure (Mtftp4, Config); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Build the required options for get info. + // + ReqOpt[0].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_TSIZE_INDEX]; + PxeBcUintnToAscDec (0, OptBuf, OptBufSize); + ReqOpt[0].ValueStr = OptBuf; + + if (BlockSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = (UINT8 *) (ReqOpt[OptCnt-1].ValueStr + AsciiStrLen ((CHAR8 *) ReqOpt[OptCnt-1].ValueStr) + 1); + OptBufSize -= (AsciiStrLen ((CHAR8 *) ReqOpt[OptCnt-1].ValueStr) + 1); + PxeBcUintnToAscDec (*BlockSize, ReqOpt[OptCnt].ValueStr, OptBufSize); + OptCnt++; + } + + if (WindowSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = (UINT8 *) (ReqOpt[OptCnt-1].ValueStr + AsciiStrLen ((CHAR8 *) ReqOpt[OptCnt-1].ValueStr) + 1); + OptBufSize -= (AsciiStrLen ((CHAR8 *) ReqOpt[OptCnt-1].ValueStr) + 1); + PxeBcUintnToAscDec (*WindowSize, ReqOpt[OptCnt].ValueStr, OptBufSize); + OptCnt++; + } + + Status = Mtftp4->GetInfo ( + Mtftp4, + NULL, + Filename, + NULL, + (UINT8) OptCnt, + ReqOpt, + &PktLen, + &Packet + ); + if (EFI_ERROR (Status)) { + if (Status == EFI_TFTP_ERROR) { + // + // Store the tftp error message into mode data and set the received flag. + // + Private->Mode.TftpErrorReceived = TRUE; + Private->Mode.TftpError.ErrorCode = (UINT8) Packet->Error.ErrorCode; + AsciiStrnCpyS ( + Private->Mode.TftpError.ErrorString, + PXE_MTFTP_ERROR_STRING_LENGTH, + (CHAR8 *) Packet->Error.ErrorMessage, + PXE_MTFTP_ERROR_STRING_LENGTH - 1 + ); + Private->Mode.TftpError.ErrorString[PXE_MTFTP_ERROR_STRING_LENGTH - 1] = '\0'; + } + goto ON_ERROR; + } + + // + // Parse the options in the reply packet. + // + OptCnt = 0; + Status = Mtftp4->ParseOptions ( + Mtftp4, + PktLen, + Packet, + (UINT32 *) &OptCnt, + &Option + ); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + // + // Parse out the value of "tsize" option. + // + Status = EFI_NOT_FOUND; + while (OptCnt != 0) { + if (AsciiStrnCmp ((CHAR8 *) Option[OptCnt - 1].OptionStr, "tsize", 5) == 0) { + *BufferSize = AsciiStrDecimalToUint64 ((CHAR8 *) (Option[OptCnt - 1].ValueStr)); + Status = EFI_SUCCESS; + } + OptCnt--; + } + FreePool (Option); + +ON_ERROR: + if (Packet != NULL) { + FreePool (Packet); + } + Mtftp4->Configure (Mtftp4, NULL); + + return Status; +} + + +/** + This function is to read the data of a file using Tftp. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to EFI_MTFTP4_CONFIG_DATA. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + @param[in] DontUseBuffer Indicates whether to use a receive buffer. + + @retval EFI_SUCCESS Successfully read the data from the special file. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Read data from file failed. + +**/ +EFI_STATUS +PxeBcMtftp4ReadFile ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_MTFTP4_CONFIG_DATA *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize, + IN BOOLEAN DontUseBuffer + ) +{ + EFI_MTFTP4_PROTOCOL *Mtftp4; + EFI_MTFTP4_TOKEN Token; + EFI_MTFTP4_OPTION ReqOpt[2]; + UINT32 OptCnt; + UINT8 BlksizeBuf[10]; + UINT8 WindowsizeBuf[10]; + EFI_STATUS Status; + + Status = EFI_DEVICE_ERROR; + Mtftp4 = Private->Mtftp4; + OptCnt = 0; + Config->InitialServerPort = PXEBC_BS_DOWNLOAD_PORT; + + Status = Mtftp4->Configure (Mtftp4, Config); + if (EFI_ERROR (Status)) { + return Status; + } + + if (BlockSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = BlksizeBuf; + PxeBcUintnToAscDec (*BlockSize, ReqOpt[OptCnt].ValueStr, sizeof (BlksizeBuf)); + OptCnt++; + } + + if (WindowSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = WindowsizeBuf; + PxeBcUintnToAscDec (*WindowSize, ReqOpt[OptCnt].ValueStr, sizeof (WindowsizeBuf)); + OptCnt++; + } + + Token.Event = NULL; + Token.OverrideData = NULL; + Token.Filename = Filename; + Token.ModeStr = NULL; + Token.OptionCount = OptCnt; + Token.OptionList = ReqOpt; + Token.Context = Private; + + if (DontUseBuffer) { + Token.BufferSize = 0; + Token.Buffer = NULL; + } else { + Token.BufferSize = *BufferSize; + Token.Buffer = BufferPtr; + } + + Token.CheckPacket = PxeBcMtftp4CheckPacket; + Token.TimeoutCallback = NULL; + Token.PacketNeeded = NULL; + + Status = Mtftp4->ReadFile (Mtftp4, &Token); + // + // Get the real size of received buffer. + // + *BufferSize = Token.BufferSize; + + Mtftp4->Configure (Mtftp4, NULL); + + return Status; +} + + +/** + This function is to write the data of a file using Tftp. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to EFI_MTFTP4_CONFIG_DATA. + @param[in] Filename Pointer to boot file name. + @param[in] Overwrite Indicates whether to use the overwrite attribute. + @param[in] BlockSize Pointer to required block size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + + @retval EFI_SUCCESS Successfully write the data into the special file. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval other Write data into file failed. + +**/ +EFI_STATUS +PxeBcMtftp4WriteFile ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_MTFTP4_CONFIG_DATA *Config, + IN UINT8 *Filename, + IN BOOLEAN Overwrite, + IN UINTN *BlockSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize + ) +{ + EFI_MTFTP4_PROTOCOL *Mtftp4; + EFI_MTFTP4_TOKEN Token; + EFI_MTFTP4_OPTION ReqOpt[1]; + UINT32 OptCnt; + UINT8 OptBuf[128]; + EFI_STATUS Status; + + Status = EFI_DEVICE_ERROR; + Mtftp4 = Private->Mtftp4; + OptCnt = 0; + Config->InitialServerPort = PXEBC_BS_DOWNLOAD_PORT; + + Status = Mtftp4->Configure (Mtftp4, Config); + if (EFI_ERROR (Status)) { + return Status; + } + + if (BlockSize != NULL) { + ReqOpt[0].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; + ReqOpt[0].ValueStr = OptBuf; + PxeBcUintnToAscDec (*BlockSize, ReqOpt[0].ValueStr, PXE_MTFTP_OPTBUF_MAXNUM_INDEX); + OptCnt++; + } + + Token.Event = NULL; + Token.OverrideData = NULL; + Token.Filename = Filename; + Token.ModeStr = NULL; + Token.OptionCount = OptCnt; + Token.OptionList = ReqOpt; + Token.BufferSize = *BufferSize; + Token.Buffer = BufferPtr; + Token.CheckPacket = PxeBcMtftp4CheckPacket; + Token.TimeoutCallback = NULL; + Token.PacketNeeded = NULL; + + Status = Mtftp4->WriteFile (Mtftp4, &Token); + // + // Get the real size of transmitted buffer. + // + *BufferSize = Token.BufferSize; + + Mtftp4->Configure (Mtftp4, NULL); + + return Status; +} + + +/** + This function is to get data (file) from a directory using Tftp. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to EFI_MTFTP4_CONFIG_DATA. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + @param[in] DontUseBuffer Indicates whether to use a receive buffer. + + @retval EFI_SUCCES Successfully obtained the data from the file included in the directory. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Operation failed. + +**/ +EFI_STATUS +PxeBcMtftp4ReadDirectory ( + IN PXEBC_PRIVATE_DATA *Private, + IN EFI_MTFTP4_CONFIG_DATA *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize, + IN BOOLEAN DontUseBuffer + ) +{ + EFI_MTFTP4_PROTOCOL *Mtftp4; + EFI_MTFTP4_TOKEN Token; + EFI_MTFTP4_OPTION ReqOpt[2]; + UINT32 OptCnt; + UINT8 BlksizeBuf[10]; + UINT8 WindowsizeBuf[10]; + EFI_STATUS Status; + + Status = EFI_DEVICE_ERROR; + Mtftp4 = Private->Mtftp4; + OptCnt = 0; + Config->InitialServerPort = PXEBC_BS_DOWNLOAD_PORT; + + Status = Mtftp4->Configure (Mtftp4, Config); + if (EFI_ERROR (Status)) { + return Status; + } + + if (BlockSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = BlksizeBuf; + PxeBcUintnToAscDec (*BlockSize, ReqOpt[OptCnt].ValueStr, sizeof (BlksizeBuf)); + OptCnt++; + } + + if (WindowSize != NULL) { + ReqOpt[OptCnt].OptionStr = (UINT8 *) mMtftpOptions[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX]; + ReqOpt[OptCnt].ValueStr = WindowsizeBuf; + PxeBcUintnToAscDec (*WindowSize, ReqOpt[OptCnt].ValueStr, sizeof (WindowsizeBuf)); + OptCnt++; + } + + Token.Event = NULL; + Token.OverrideData = NULL; + Token.Filename = Filename; + Token.ModeStr = NULL; + Token.OptionCount = OptCnt; + Token.OptionList = ReqOpt; + Token.Context = Private; + + if (DontUseBuffer) { + Token.BufferSize = 0; + Token.Buffer = NULL; + } else { + Token.BufferSize = *BufferSize; + Token.Buffer = BufferPtr; + } + + Token.CheckPacket = PxeBcMtftp4CheckPacket; + Token.TimeoutCallback = NULL; + Token.PacketNeeded = NULL; + + Status = Mtftp4->ReadDirectory (Mtftp4, &Token); + // + // Get the real size of received buffer. + // + *BufferSize = Token.BufferSize; + + Mtftp4->Configure (Mtftp4, NULL); + + return Status; +} + + +/** + This function is wrapper to get the file size using TFTP. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to configure data. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in, out] BufferSize Pointer to buffer size. + + @retval EFI_SUCCESS Successfully obtained the size of file. + @retval EFI_NOT_FOUND Parse the tftp options failed. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Did not obtain the size of the file. + +**/ +EFI_STATUS +PxeBcTftpGetFileSize ( + IN PXEBC_PRIVATE_DATA *Private, + IN VOID *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN OUT UINT64 *BufferSize + ) +{ + if (Private->PxeBc.Mode->UsingIpv6) { + return PxeBcMtftp6GetFileSize ( + Private, + (EFI_MTFTP6_CONFIG_DATA *) Config, + Filename, + BlockSize, + WindowSize, + BufferSize + ); + } else { + return PxeBcMtftp4GetFileSize ( + Private, + (EFI_MTFTP4_CONFIG_DATA *) Config, + Filename, + BlockSize, + WindowSize, + BufferSize + ); + } +} + + +/** + This function is a wrapper to get file using TFTP. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to config data. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + @param[in] DontUseBuffer Indicates whether to use a receive buffer. + + @retval EFI_SUCCESS Sucessfully read the data from the special file. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Read data from file failed. + +**/ +EFI_STATUS +PxeBcTftpReadFile ( + IN PXEBC_PRIVATE_DATA *Private, + IN VOID *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize, + IN BOOLEAN DontUseBuffer + ) +{ + if (Private->PxeBc.Mode->UsingIpv6) { + return PxeBcMtftp6ReadFile ( + Private, + (EFI_MTFTP6_CONFIG_DATA *) Config, + Filename, + BlockSize, + WindowSize, + BufferPtr, + BufferSize, + DontUseBuffer + ); + } else { + return PxeBcMtftp4ReadFile ( + Private, + (EFI_MTFTP4_CONFIG_DATA *) Config, + Filename, + BlockSize, + WindowSize, + BufferPtr, + BufferSize, + DontUseBuffer + ); + } +} + + +/** + This function is a wrapper to write file using TFTP. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to config data. + @param[in] Filename Pointer to boot file name. + @param[in] Overwrite Indicate whether with overwrite attribute. + @param[in] BlockSize Pointer to required block size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + + @retval EFI_SUCCESS Successfully wrote the data into a special file. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval other Write data into file failed. + +**/ +EFI_STATUS +PxeBcTftpWriteFile ( + IN PXEBC_PRIVATE_DATA *Private, + IN VOID *Config, + IN UINT8 *Filename, + IN BOOLEAN Overwrite, + IN UINTN *BlockSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize + ) +{ + if (Private->PxeBc.Mode->UsingIpv6) { + return PxeBcMtftp6WriteFile ( + Private, + (EFI_MTFTP6_CONFIG_DATA *) Config, + Filename, + Overwrite, + BlockSize, + BufferPtr, + BufferSize + ); + } else { + return PxeBcMtftp4WriteFile ( + Private, + (EFI_MTFTP4_CONFIG_DATA *) Config, + Filename, + Overwrite, + BlockSize, + BufferPtr, + BufferSize + ); + } +} + + +/** + This function is a wrapper to get the data (file) from a directory using TFTP. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to config data. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + @param[in] DontUseBuffer Indicatse whether to use a receive buffer. + + @retval EFI_SUCCES Successfully obtained the data from the file included in the directory. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Operation failed. + +**/ +EFI_STATUS +PxeBcTftpReadDirectory ( + IN PXEBC_PRIVATE_DATA *Private, + IN VOID *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize, + IN BOOLEAN DontUseBuffer + ) +{ + if (Private->PxeBc.Mode->UsingIpv6) { + return PxeBcMtftp6ReadDirectory ( + Private, + (EFI_MTFTP6_CONFIG_DATA *) Config, + Filename, + BlockSize, + WindowSize, + BufferPtr, + BufferSize, + DontUseBuffer + ); + } else { + return PxeBcMtftp4ReadDirectory ( + Private, + (EFI_MTFTP4_CONFIG_DATA *) Config, + Filename, + BlockSize, + WindowSize, + BufferPtr, + BufferSize, + DontUseBuffer + ); + } +} + diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcMtftp.h b/NetworkPkg/UefiPxeBcDxe/PxeBcMtftp.h new file mode 100644 index 000000000..9a011e046 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcMtftp.h @@ -0,0 +1,138 @@ +/** @file + Functions declaration related with Mtftp for UefiPxeBc Driver. + + Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_PXEBC_MTFTP_H__ +#define __EFI_PXEBC_MTFTP_H__ + +#define PXE_MTFTP_OPTION_BLKSIZE_INDEX 0 +#define PXE_MTFTP_OPTION_TIMEOUT_INDEX 1 +#define PXE_MTFTP_OPTION_TSIZE_INDEX 2 +#define PXE_MTFTP_OPTION_MULTICAST_INDEX 3 +#define PXE_MTFTP_OPTION_WINDOWSIZE_INDEX 4 +#define PXE_MTFTP_OPTION_MAXIMUM_INDEX 5 +#define PXE_MTFTP_OPTBUF_MAXNUM_INDEX 128 + +#define PXE_MTFTP_ERROR_STRING_LENGTH 127 // refer to definition of struct EFI_PXE_BASE_CODE_TFTP_ERROR. +#define PXE_MTFTP_DEFAULT_BLOCK_SIZE 512 // refer to rfc-1350. + + +/** + This function is wrapper to get the file size using TFTP. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to configure data. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in, out] BufferSize Pointer to buffer size. + + @retval EFI_SUCCESS Successfully obtained the size of file. + @retval EFI_NOT_FOUND Parse the tftp ptions failed. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Did not obtain the size of the file. + +**/ +EFI_STATUS +PxeBcTftpGetFileSize ( + IN PXEBC_PRIVATE_DATA *Private, + IN VOID *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN OUT UINT64 *BufferSize + ); + + +/** + This function is a wrapper to get a file using TFTP. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to config data. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + @param[in] DontUseBuffer Indicates whether to use a receive buffer. + + @retval EFI_SUCCESS Successfully read the data from the special file. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Read data from file failed. + +**/ +EFI_STATUS +PxeBcTftpReadFile ( + IN PXEBC_PRIVATE_DATA *Private, + IN VOID *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize, + IN BOOLEAN DontUseBuffer + ); + + +/** + This function is a wrapper to put file with TFTP. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to config data. + @param[in] Filename Pointer to boot file name. + @param[in] Overwrite Indicates whether to use an overwrite attribute. + @param[in] BlockSize Pointer to required block size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + + @retval EFI_SUCCESS Successfully wrote the data into the special file. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval other Write data into file failed. + +**/ +EFI_STATUS +PxeBcTftpWriteFile ( + IN PXEBC_PRIVATE_DATA *Private, + IN VOID *Config, + IN UINT8 *Filename, + IN BOOLEAN Overwrite, + IN UINTN *BlockSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize + ); + + +/** + This function is a wrapper to get the data (file) from a directory using TFTP. + + @param[in] Private Pointer to PxeBc private data. + @param[in] Config Pointer to config data. + @param[in] Filename Pointer to boot file name. + @param[in] BlockSize Pointer to required block size. + @param[in] WindowSize Pointer to required window size. + @param[in] BufferPtr Pointer to buffer. + @param[in, out] BufferSize Pointer to buffer size. + @param[in] DontUseBuffer Indicates whether with a receive buffer. + + @retval EFI_SUCCES Successfully obtained the data from the file included in directory. + @retval EFI_DEVICE_ERROR The network device encountered an error during this operation. + @retval Others Operation failed. + +**/ +EFI_STATUS +PxeBcTftpReadDirectory ( + IN PXEBC_PRIVATE_DATA *Private, + IN VOID *Config, + IN UINT8 *Filename, + IN UINTN *BlockSize, + IN UINTN *WindowSize, + IN UINT8 *BufferPtr, + IN OUT UINT64 *BufferSize, + IN BOOLEAN DontUseBuffer + ); +#endif diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcSupport.c b/NetworkPkg/UefiPxeBcDxe/PxeBcSupport.c new file mode 100644 index 000000000..ae8eb4894 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcSupport.c @@ -0,0 +1,1538 @@ +/** @file + Support functions implementation for UefiPxeBc Driver. + + Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "PxeBcImpl.h" + + +/** + Flush the previous configration using the new station Ip address. + + @param[in] Private The pointer to the PxeBc private data. + @param[in] StationIp The pointer to the station Ip address. + @param[in] SubnetMask The pointer to the subnet mask address for v4. + + @retval EFI_SUCCESS Successfully flushed the previous configuration. + @retval Others Failed to flush using the new station Ip. + +**/ +EFI_STATUS +PxeBcFlushStationIp ( + PXEBC_PRIVATE_DATA *Private, + EFI_IP_ADDRESS *StationIp, OPTIONAL + EFI_IP_ADDRESS *SubnetMask OPTIONAL + ) +{ + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_STATUS Status; + EFI_ARP_CONFIG_DATA ArpConfigData; + + Mode = Private->PxeBc.Mode; + Status = EFI_SUCCESS; + ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA)); + + if (Mode->UsingIpv6 && StationIp != NULL) { + // + // Overwrite Udp6CfgData/Ip6CfgData StationAddress. + // + CopyMem (&Private->Udp6CfgData.StationAddress, StationIp, sizeof (EFI_IPv6_ADDRESS)); + CopyMem (&Private->Ip6CfgData.StationAddress, StationIp, sizeof (EFI_IPv6_ADDRESS)); + + // + // Reconfigure the Ip6 instance to capture background ICMP6 packets with new station Ip address. + // + Private->Ip6->Cancel (Private->Ip6, &Private->Icmp6Token); + Private->Ip6->Configure (Private->Ip6, NULL); + + Status = Private->Ip6->Configure (Private->Ip6, &Private->Ip6CfgData); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = Private->Ip6->Receive (Private->Ip6, &Private->Icmp6Token); + } else { + if (StationIp != NULL) { + // + // Reconfigure the ARP instance with station Ip address. + // + ArpConfigData.SwAddressType = 0x0800; + ArpConfigData.SwAddressLength = (UINT8) sizeof (EFI_IPv4_ADDRESS); + ArpConfigData.StationAddress = StationIp; + + Private->Arp->Configure (Private->Arp, NULL); + Private->Arp->Configure (Private->Arp, &ArpConfigData); + + // + // Overwrite Udp4CfgData/Ip4CfgData StationAddress. + // + CopyMem (&Private->Udp4CfgData.StationAddress, StationIp, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Private->Ip4CfgData.StationAddress, StationIp, sizeof (EFI_IPv4_ADDRESS)); + } + + if (SubnetMask != NULL) { + // + // Overwrite Udp4CfgData/Ip4CfgData SubnetMask. + // + CopyMem (&Private->Udp4CfgData.SubnetMask, SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + CopyMem (&Private->Ip4CfgData.SubnetMask, SubnetMask, sizeof (EFI_IPv4_ADDRESS)); + } + + if (StationIp != NULL && SubnetMask != NULL) { + // + // Updated the route table. + // + Mode->RouteTableEntries = 1; + Mode->RouteTable[0].IpAddr.Addr[0] = StationIp->Addr[0] & SubnetMask->Addr[0]; + Mode->RouteTable[0].SubnetMask.Addr[0] = SubnetMask->Addr[0]; + Mode->RouteTable[0].GwAddr.Addr[0] = 0; + } + + if (StationIp != NULL || SubnetMask != NULL) { + // + // Reconfigure the Ip4 instance to capture background ICMP packets with new station Ip address. + // + Private->Ip4->Cancel (Private->Ip4, &Private->IcmpToken); + Private->Ip4->Configure (Private->Ip4, NULL); + + Status = Private->Ip4->Configure (Private->Ip4, &Private->Ip4CfgData); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = Private->Ip4->Receive (Private->Ip4, &Private->IcmpToken); + } + } + +ON_EXIT: + return Status; +} + + +/** + Notify the callback function when an event is triggered. + + @param[in] Event The triggered event. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +PxeBcCommonNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + *((BOOLEAN *) Context) = TRUE; +} + + +/** + Do arp resolution from arp cache in PxeBcMode. + + @param Mode The pointer to EFI_PXE_BASE_CODE_MODE. + @param Ip4Addr The Ip4 address for resolution. + @param MacAddress The resoluted MAC address if the resolution is successful. + The value is undefined if the resolution fails. + + @retval TRUE Found an matched entry. + @retval FALSE Did not find a matched entry. + +**/ +BOOLEAN +PxeBcCheckArpCache ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN EFI_IPv4_ADDRESS *Ip4Addr, + OUT EFI_MAC_ADDRESS *MacAddress + ) +{ + UINT32 Index; + + ASSERT (!Mode->UsingIpv6); + + // + // Check whether the current Arp cache in mode data contains this information or not. + // + for (Index = 0; Index < Mode->ArpCacheEntries; Index++) { + if (EFI_IP4_EQUAL (&Mode->ArpCache[Index].IpAddr.v4, Ip4Addr)) { + CopyMem ( + MacAddress, + &Mode->ArpCache[Index].MacAddr, + sizeof (EFI_MAC_ADDRESS) + ); + return TRUE; + } + } + + return FALSE; +} + + +/** + Update the arp cache periodically. + + @param Event The pointer to EFI_PXE_BC_PROTOCOL. + @param Context Context of the timer event. + +**/ +VOID +EFIAPI +PxeBcArpCacheUpdate ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_ARP_FIND_DATA *ArpEntry; + UINT32 EntryLength; + UINT32 EntryCount; + UINT32 Index; + EFI_STATUS Status; + + Private = (PXEBC_PRIVATE_DATA *) Context; + Mode = Private->PxeBc.Mode; + + ASSERT (!Mode->UsingIpv6); + + // + // Get the current Arp cache from Arp driver. + // + Status = Private->Arp->Find ( + Private->Arp, + TRUE, + NULL, + &EntryLength, + &EntryCount, + &ArpEntry, + TRUE + ); + if (EFI_ERROR (Status)) { + return; + } + + // + // Update the Arp cache in mode data. + // + Mode->ArpCacheEntries = MIN (EntryCount, EFI_PXE_BASE_CODE_MAX_ARP_ENTRIES); + + for (Index = 0; Index < Mode->ArpCacheEntries; Index++) { + CopyMem ( + &Mode->ArpCache[Index].IpAddr, + ArpEntry + 1, + ArpEntry->SwAddressLength + ); + CopyMem ( + &Mode->ArpCache[Index].MacAddr, + (UINT8 *) (ArpEntry + 1) + ArpEntry->SwAddressLength, + ArpEntry->HwAddressLength + ); + ArpEntry = (EFI_ARP_FIND_DATA *) ((UINT8 *) ArpEntry + EntryLength); + } +} + + +/** + Notify function to handle the received ICMP message in DPC. + + @param Context The PXEBC private data. + +**/ +VOID +EFIAPI +PxeBcIcmpErrorDpcHandle ( + IN VOID *Context + ) +{ + EFI_STATUS Status; + EFI_IP4_RECEIVE_DATA *RxData; + EFI_IP4_PROTOCOL *Ip4; + PXEBC_PRIVATE_DATA *Private; + EFI_PXE_BASE_CODE_MODE *Mode; + UINT8 Type; + UINTN Index; + UINT32 CopiedLen; + UINT8 *IcmpError; + + Private = (PXEBC_PRIVATE_DATA *) Context; + Mode = &Private->Mode; + Status = Private->IcmpToken.Status; + RxData = Private->IcmpToken.Packet.RxData; + Ip4 = Private->Ip4; + + ASSERT (!Mode->UsingIpv6); + + if (Status == EFI_ABORTED) { + // + // It's triggered by user cancellation. + // + return; + } + + if (RxData == NULL) { + goto ON_EXIT; + } + + if (Status != EFI_ICMP_ERROR) { + // + // The return status should be recognized as EFI_ICMP_ERROR. + // + goto ON_RECYCLE; + } + + if (EFI_IP4 (RxData->Header->SourceAddress) != 0 && + (NTOHL (Mode->SubnetMask.Addr[0]) != 0) && + IP4_NET_EQUAL (NTOHL(Mode->StationIp.Addr[0]), EFI_NTOHL (RxData->Header->SourceAddress), NTOHL (Mode->SubnetMask.Addr[0])) && + !NetIp4IsUnicast (EFI_NTOHL (RxData->Header->SourceAddress), NTOHL (Mode->SubnetMask.Addr[0]))) { + // + // The source address of the received packet should be a valid unicast address. + // + goto ON_RECYCLE; + } + + if (!EFI_IP4_EQUAL (&RxData->Header->DestinationAddress, &Mode->StationIp.v4)) { + // + // The destination address of the received packet should be equal to the host address. + // + goto ON_RECYCLE; + } + + // + // The protocol has been configured to only receive ICMP packet. + // + ASSERT (RxData->Header->Protocol == EFI_IP_PROTO_ICMP); + + Type = *((UINT8 *) RxData->FragmentTable[0].FragmentBuffer); + + if (Type != ICMP_DEST_UNREACHABLE && + Type != ICMP_SOURCE_QUENCH && + Type != ICMP_REDIRECT && + Type != ICMP_TIME_EXCEEDED && + Type != ICMP_PARAMETER_PROBLEM) { + // + // The type of the receveid ICMP message should be ICMP_ERROR_MESSAGE. + // + goto ON_RECYCLE; + } + + // + // Copy the right ICMP error message into mode data. + // + CopiedLen = 0; + IcmpError = (UINT8 *) &Mode->IcmpError; + + for (Index = 0; Index < RxData->FragmentCount; Index++) { + CopiedLen += RxData->FragmentTable[Index].FragmentLength; + if (CopiedLen <= sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR)) { + CopyMem ( + IcmpError, + RxData->FragmentTable[Index].FragmentBuffer, + RxData->FragmentTable[Index].FragmentLength + ); + } else { + CopyMem ( + IcmpError, + RxData->FragmentTable[Index].FragmentBuffer, + CopiedLen - sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR) + ); + } + IcmpError += CopiedLen; + } + +ON_RECYCLE: + gBS->SignalEvent (RxData->RecycleSignal); + +ON_EXIT: + Private->IcmpToken.Status = EFI_NOT_READY; + Ip4->Receive (Ip4, &Private->IcmpToken); +} + + +/** + Callback function to update the latest ICMP6 error message. + + @param Event The event signalled. + @param Context The context passed in using the event notifier. + +**/ +VOID +EFIAPI +PxeBcIcmpErrorUpdate ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + QueueDpc (TPL_CALLBACK, PxeBcIcmpErrorDpcHandle, Context); +} + + +/** + Notify function to handle the received ICMP6 message in DPC. + + @param Context The PXEBC private data. + +**/ +VOID +EFIAPI +PxeBcIcmp6ErrorDpcHandle ( + IN VOID *Context + ) +{ + PXEBC_PRIVATE_DATA *Private; + EFI_IP6_RECEIVE_DATA *RxData; + EFI_IP6_PROTOCOL *Ip6; + EFI_PXE_BASE_CODE_MODE *Mode; + EFI_STATUS Status; + UINTN Index; + UINT8 Type; + UINT32 CopiedLen; + UINT8 *Icmp6Error; + + Private = (PXEBC_PRIVATE_DATA *) Context; + Mode = &Private->Mode; + Status = Private->Icmp6Token.Status; + RxData = Private->Icmp6Token.Packet.RxData; + Ip6 = Private->Ip6; + + ASSERT (Mode->UsingIpv6); + + if (Status == EFI_ABORTED) { + // + // It's triggered by user cancellation. + // + return; + } + + if (RxData == NULL) { + goto ON_EXIT; + } + + if (Status != EFI_ICMP_ERROR) { + // + // The return status should be recognized as EFI_ICMP_ERROR. + // + goto ON_RECYCLE; + } + + if (!NetIp6IsValidUnicast (&RxData->Header->SourceAddress)) { + // + // The source address of the received packet should be a valid unicast address. + // + goto ON_RECYCLE; + } + + if (!NetIp6IsUnspecifiedAddr (&Mode->StationIp.v6) && + !EFI_IP6_EQUAL (&RxData->Header->DestinationAddress, &Mode->StationIp.v6)) { + // + // The destination address of the received packet should be equal to the host address. + // + goto ON_RECYCLE; + } + + // + // The protocol has been configured to only receive ICMP packet. + // + ASSERT (RxData->Header->NextHeader == IP6_ICMP); + + Type = *((UINT8 *) RxData->FragmentTable[0].FragmentBuffer); + + if (Type != ICMP_V6_DEST_UNREACHABLE && + Type != ICMP_V6_PACKET_TOO_BIG && + Type != ICMP_V6_TIME_EXCEEDED && + Type != ICMP_V6_PARAMETER_PROBLEM) { + // + // The type of the receveid packet should be an ICMP6 error message. + // + goto ON_RECYCLE; + } + + // + // Copy the right ICMP6 error message into mode data. + // + CopiedLen = 0; + Icmp6Error = (UINT8 *) &Mode->IcmpError; + + for (Index = 0; Index < RxData->FragmentCount; Index++) { + CopiedLen += RxData->FragmentTable[Index].FragmentLength; + if (CopiedLen <= sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR)) { + CopyMem ( + Icmp6Error, + RxData->FragmentTable[Index].FragmentBuffer, + RxData->FragmentTable[Index].FragmentLength + ); + } else { + CopyMem ( + Icmp6Error, + RxData->FragmentTable[Index].FragmentBuffer, + CopiedLen - sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR) + ); + } + Icmp6Error += CopiedLen; + } + +ON_RECYCLE: + gBS->SignalEvent (RxData->RecycleSignal); + +ON_EXIT: + Private->Icmp6Token.Status = EFI_NOT_READY; + Ip6->Receive (Ip6, &Private->Icmp6Token); +} + + +/** + Callback function to update the latest ICMP6 error message. + + @param Event The event signalled. + @param Context The context passed in using the event notifier. + +**/ +VOID +EFIAPI +PxeBcIcmp6ErrorUpdate ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + QueueDpc (TPL_CALLBACK, PxeBcIcmp6ErrorDpcHandle, Context); +} + + +/** + This function is to configure a UDPv4 instance for UdpWrite. + + @param[in] Udp4 The pointer to EFI_UDP4_PROTOCOL. + @param[in] StationIp The pointer to the station address. + @param[in] SubnetMask The pointer to the subnet mask. + @param[in] Gateway The pointer to the gateway address. + @param[in, out] SrcPort The pointer to the source port. + @param[in] DoNotFragment If TRUE, fragment is not enabled. + Otherwise, fragment is enabled. + @param[in] Ttl The time to live field of the IP header. + @param[in] ToS The type of service field of the IP header. + + @retval EFI_SUCCESS Successfully configured this instance. + @retval Others Failed to configure this instance. + +**/ +EFI_STATUS +PxeBcConfigUdp4Write ( + IN EFI_UDP4_PROTOCOL *Udp4, + IN EFI_IPv4_ADDRESS *StationIp, + IN EFI_IPv4_ADDRESS *SubnetMask, + IN EFI_IPv4_ADDRESS *Gateway, + IN OUT UINT16 *SrcPort, + IN BOOLEAN DoNotFragment, + IN UINT8 Ttl, + IN UINT8 ToS + ) +{ + EFI_UDP4_CONFIG_DATA Udp4CfgData; + EFI_STATUS Status; + + ZeroMem (&Udp4CfgData, sizeof (Udp4CfgData)); + + Udp4CfgData.TransmitTimeout = PXEBC_DEFAULT_LIFETIME; + Udp4CfgData.ReceiveTimeout = PXEBC_DEFAULT_LIFETIME; + Udp4CfgData.TypeOfService = ToS; + Udp4CfgData.TimeToLive = Ttl; + Udp4CfgData.AllowDuplicatePort = TRUE; + Udp4CfgData.DoNotFragment = DoNotFragment; + + CopyMem (&Udp4CfgData.StationAddress, StationIp, sizeof (*StationIp)); + CopyMem (&Udp4CfgData.SubnetMask, SubnetMask, sizeof (*SubnetMask)); + + Udp4CfgData.StationPort = *SrcPort; + + // + // Reset the UDPv4 instance. + // + Udp4->Configure (Udp4, NULL); + + Status = Udp4->Configure (Udp4, &Udp4CfgData); + if (!EFI_ERROR (Status) && !EFI_IP4_EQUAL (Gateway, &mZeroIp4Addr)) { + // + // The basic configuration is OK, need to add the default route entry + // + Status = Udp4->Routes (Udp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, Gateway); + if (EFI_ERROR (Status)) { + Udp4->Configure (Udp4, NULL); + } + } + + if (!EFI_ERROR (Status) && *SrcPort == 0) { + Udp4->GetModeData (Udp4, &Udp4CfgData, NULL, NULL, NULL); + *SrcPort = Udp4CfgData.StationPort; + } + + return Status; +} + + +/** + This function is to configure a UDPv6 instance for UdpWrite. + + @param[in] Udp6 The pointer to EFI_UDP6_PROTOCOL. + @param[in] StationIp The pointer to the station address. + @param[in, out] SrcPort The pointer to the source port. + + @retval EFI_SUCCESS Successfully configured this instance. + @retval Others Failed to configure this instance. + +**/ +EFI_STATUS +PxeBcConfigUdp6Write ( + IN EFI_UDP6_PROTOCOL *Udp6, + IN EFI_IPv6_ADDRESS *StationIp, + IN OUT UINT16 *SrcPort + ) +{ + EFI_UDP6_CONFIG_DATA CfgData; + EFI_STATUS Status; + + ZeroMem (&CfgData, sizeof (EFI_UDP6_CONFIG_DATA)); + + CfgData.ReceiveTimeout = PXEBC_DEFAULT_LIFETIME; + CfgData.TransmitTimeout = PXEBC_DEFAULT_LIFETIME; + CfgData.HopLimit = PXEBC_DEFAULT_HOPLIMIT; + CfgData.AllowDuplicatePort = TRUE; + CfgData.StationPort = *SrcPort; + + CopyMem (&CfgData.StationAddress, StationIp, sizeof (EFI_IPv6_ADDRESS)); + + // + // Reset the UDPv6 instance. + // + Udp6->Configure (Udp6, NULL); + + Status = Udp6->Configure (Udp6, &CfgData); + if (EFI_ERROR (Status)) { + return Status; + } + + if (!EFI_ERROR (Status) && *SrcPort == 0) { + Udp6->GetModeData (Udp6, &CfgData, NULL, NULL, NULL); + *SrcPort = CfgData.StationPort; + } + + return Status; +} + + +/** + This function is to configure a UDPv4 instance for UdpWrite. + + @param[in] Udp4 The pointer to EFI_UDP4_PROTOCOL. + @param[in] Session The pointer to the UDP4 session data. + @param[in] TimeoutEvent The event for timeout. + @param[in] Gateway The pointer to the gateway address. + @param[in] HeaderSize An optional field which may be set to the length of a header + at HeaderPtr to be prefixed to the data at BufferPtr. + @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be + prefixed to the data at BufferPtr. + @param[in] BufferSize A pointer to the size of the data at BufferPtr. + @param[in] BufferPtr A pointer to the data to be written. + + @retval EFI_SUCCESS Successfully send out data using Udp4Write. + @retval Others Failed to send out data. + +**/ +EFI_STATUS +PxeBcUdp4Write ( + IN EFI_UDP4_PROTOCOL *Udp4, + IN EFI_UDP4_SESSION_DATA *Session, + IN EFI_EVENT TimeoutEvent, + IN EFI_IPv4_ADDRESS *Gateway OPTIONAL, + IN UINTN *HeaderSize OPTIONAL, + IN VOID *HeaderPtr OPTIONAL, + IN UINTN *BufferSize, + IN VOID *BufferPtr + ) +{ + EFI_UDP4_COMPLETION_TOKEN Token; + EFI_UDP4_TRANSMIT_DATA *TxData; + UINT32 TxLength; + UINT32 FragCount; + UINT32 DataLength; + BOOLEAN IsDone; + EFI_STATUS Status; + + // + // Arrange one fragment buffer for data, and another fragment buffer for header if has. + // + FragCount = (HeaderSize != NULL) ? 2 : 1; + TxLength = sizeof (EFI_UDP4_TRANSMIT_DATA) + (FragCount - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA); + TxData = (EFI_UDP4_TRANSMIT_DATA *) AllocateZeroPool (TxLength); + if (TxData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + TxData->FragmentCount = FragCount; + TxData->FragmentTable[FragCount - 1].FragmentLength = (UINT32) *BufferSize; + TxData->FragmentTable[FragCount - 1].FragmentBuffer = BufferPtr; + DataLength = (UINT32) *BufferSize; + + if (HeaderSize != NULL) { + TxData->FragmentTable[0].FragmentLength = (UINT32) *HeaderSize; + TxData->FragmentTable[0].FragmentBuffer = HeaderPtr; + DataLength += (UINT32) *HeaderSize; + } + + if (Gateway != NULL) { + TxData->GatewayAddress = Gateway; + } + + TxData->UdpSessionData = Session; + TxData->DataLength = DataLength; + Token.Packet.TxData = TxData; + Token.Status = EFI_NOT_READY; + IsDone = FALSE; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + PxeBcCommonNotify, + &IsDone, + &Token.Event + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = Udp4->Transmit (Udp4, &Token); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Poll the UDPv6 read instance if no packet received and no timeout triggered. + // + while (!IsDone && + Token.Status == EFI_NOT_READY && + EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) { + Udp4->Poll (Udp4); + } + + Status = (Token.Status == EFI_NOT_READY) ? EFI_TIMEOUT : Token.Status; + +ON_EXIT: + if (Token.Event != NULL) { + gBS->CloseEvent (Token.Event); + } + FreePool (TxData); + + return Status; +} + + +/** + This function is to configure a UDPv4 instance for UdpWrite. + + @param[in] Udp6 The pointer to EFI_UDP6_PROTOCOL. + @param[in] Session The pointer to the UDP6 session data. + @param[in] TimeoutEvent The event for timeout. + @param[in] HeaderSize An optional field which may be set to the length of a header + at HeaderPtr to be prefixed to the data at BufferPtr. + @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be + prefixed to the data at BufferPtr. + @param[in] BufferSize A pointer to the size of the data at BufferPtr. + @param[in] BufferPtr A pointer to the data to be written. + + @retval EFI_SUCCESS Successfully sent out data using Udp6Write. + @retval Others Failed to send out data. + +**/ +EFI_STATUS +PxeBcUdp6Write ( + IN EFI_UDP6_PROTOCOL *Udp6, + IN EFI_UDP6_SESSION_DATA *Session, + IN EFI_EVENT TimeoutEvent, + IN UINTN *HeaderSize OPTIONAL, + IN VOID *HeaderPtr OPTIONAL, + IN UINTN *BufferSize, + IN VOID *BufferPtr + ) +{ + EFI_UDP6_COMPLETION_TOKEN Token; + EFI_UDP6_TRANSMIT_DATA *TxData; + UINT32 TxLength; + UINT32 FragCount; + UINT32 DataLength; + BOOLEAN IsDone; + EFI_STATUS Status; + + // + // Arrange one fragment buffer for data, and another fragment buffer for header if has. + // + FragCount = (HeaderSize != NULL) ? 2 : 1; + TxLength = sizeof (EFI_UDP6_TRANSMIT_DATA) + (FragCount - 1) * sizeof (EFI_UDP6_FRAGMENT_DATA); + TxData = (EFI_UDP6_TRANSMIT_DATA *) AllocateZeroPool (TxLength); + if (TxData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + TxData->FragmentCount = FragCount; + TxData->FragmentTable[FragCount - 1].FragmentLength = (UINT32) *BufferSize; + TxData->FragmentTable[FragCount - 1].FragmentBuffer = BufferPtr; + DataLength = (UINT32) *BufferSize; + + if (HeaderSize != NULL) { + TxData->FragmentTable[0].FragmentLength = (UINT32) *HeaderSize; + TxData->FragmentTable[0].FragmentBuffer = HeaderPtr; + DataLength += (UINT32) *HeaderSize; + } + + TxData->UdpSessionData = Session; + TxData->DataLength = DataLength; + Token.Packet.TxData = TxData; + Token.Status = EFI_NOT_READY; + IsDone = FALSE; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + PxeBcCommonNotify, + &IsDone, + &Token.Event + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = Udp6->Transmit (Udp6, &Token); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + // + // Poll the UDPv6 read instance if no packet received and no timeout triggered. + // + while (!IsDone && + Token.Status == EFI_NOT_READY && + EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) { + Udp6->Poll (Udp6); + } + + Status = (Token.Status == EFI_NOT_READY) ? EFI_TIMEOUT : Token.Status; + +ON_EXIT: + if (Token.Event != NULL) { + gBS->CloseEvent (Token.Event); + } + FreePool (TxData); + + return Status; +} + + +/** + Check the received packet using the Ip filter. + + @param[in] Mode The pointer to the mode data of PxeBc. + @param[in] Session The pointer to the current UDPv4 session. + @param[in] OpFlags Operation flag for UdpRead/UdpWrite. + + @retval TRUE Passed the Ip filter successfully. + @retval FALSE Failed to pass the Ip filter. + +**/ +BOOLEAN +PxeBcCheckByIpFilter ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN VOID *Session, + IN UINT16 OpFlags + ) +{ + EFI_IP_ADDRESS DestinationIp; + UINTN Index; + + if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER) == 0) { + return TRUE; + } + + if ((Mode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0) { + return TRUE; + } + + // + // Convert the destination address in session data to host order. + // + if (Mode->UsingIpv6) { + CopyMem ( + &DestinationIp, + &((EFI_UDP6_SESSION_DATA *) Session)->DestinationAddress, + sizeof (EFI_IPv6_ADDRESS) + ); + NTOHLLL (&DestinationIp.v6); + } else { + ZeroMem (&DestinationIp, sizeof (EFI_IP_ADDRESS)); + CopyMem ( + &DestinationIp, + &((EFI_UDP4_SESSION_DATA *) Session)->DestinationAddress, + sizeof (EFI_IPv4_ADDRESS) + ); + EFI_NTOHL (DestinationIp); + } + + if ((Mode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) != 0 && + (IP4_IS_MULTICAST (DestinationIp.Addr[0]) || + IP6_IS_MULTICAST (&DestinationIp))) { + return TRUE; + } + + if ((Mode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) != 0 && + IP4_IS_LOCAL_BROADCAST (DestinationIp.Addr[0])) { + ASSERT (!Mode->UsingIpv6); + return TRUE; + } + + if ((Mode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0 && + (EFI_IP4_EQUAL (&Mode->StationIp.v4, &DestinationIp) || + EFI_IP6_EQUAL (&Mode->StationIp.v6, &DestinationIp))) { + // + // Matched if the dest address is equal to the station address. + // + return TRUE; + } + + for (Index = 0; Index < Mode->IpFilter.IpCnt; Index++) { + ASSERT (Index < EFI_PXE_BASE_CODE_MAX_IPCNT); + if (EFI_IP4_EQUAL (&Mode->IpFilter.IpList[Index].v4, &DestinationIp) || + EFI_IP6_EQUAL (&Mode->IpFilter.IpList[Index].v6, &DestinationIp)) { + // + // Matched if the dest address is equal to any of address in the filter list. + // + return TRUE; + } + } + + return FALSE; +} + + +/** + Filter the received packet using the destination Ip. + + @param[in] Mode The pointer to the mode data of PxeBc. + @param[in] Session The pointer to the current UDPv4 session. + @param[in, out] DestIp The pointer to the destination Ip address. + @param[in] OpFlags Operation flag for UdpRead/UdpWrite. + + @retval TRUE Passed the IPv4 filter successfully. + @retval FALSE Failed to pass the IPv4 filter. + +**/ +BOOLEAN +PxeBcCheckByDestIp ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN VOID *Session, + IN OUT EFI_IP_ADDRESS *DestIp, + IN UINT16 OpFlags + ) +{ + if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP) != 0) { + // + // Copy the destination address from the received packet if accept any. + // + if (DestIp != NULL) { + if (Mode->UsingIpv6) { + CopyMem ( + DestIp, + &((EFI_UDP6_SESSION_DATA *)Session)->DestinationAddress, + sizeof (EFI_IPv6_ADDRESS) + ); + } else { + ZeroMem (DestIp, sizeof (EFI_IP_ADDRESS)); + CopyMem ( + DestIp, + &((EFI_UDP4_SESSION_DATA *)Session)->DestinationAddress, + sizeof (EFI_IPv4_ADDRESS) + ); + } + + } + return TRUE; + } else if (DestIp != NULL && + (EFI_IP4_EQUAL (DestIp, &((EFI_UDP4_SESSION_DATA *)Session)->DestinationAddress) || + EFI_IP6_EQUAL (DestIp, &((EFI_UDP6_SESSION_DATA *)Session)->DestinationAddress))) { + // + // The destination address in the received packet is matched if present. + // + return TRUE; + } else if (EFI_IP4_EQUAL (&Mode->StationIp, &((EFI_UDP4_SESSION_DATA *)Session)->DestinationAddress) || + EFI_IP6_EQUAL (&Mode->StationIp, &((EFI_UDP6_SESSION_DATA *)Session)->DestinationAddress)) { + // + // The destination address in the received packet is equal to the host address. + // + return TRUE; + } + + return FALSE; +} + + +/** + Check the received packet using the destination port. + + @param[in] Mode The pointer to the mode data of PxeBc. + @param[in] Session The pointer to the current UDPv4 session. + @param[in, out] DestPort The pointer to the destination port. + @param[in] OpFlags Operation flag for UdpRead/UdpWrite. + + @retval TRUE Passed the IPv4 filter successfully. + @retval FALSE Failed to pass the IPv4 filter. + +**/ +BOOLEAN +PxeBcCheckByDestPort ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN VOID *Session, + IN OUT UINT16 *DestPort, + IN UINT16 OpFlags + ) +{ + UINT16 Port; + + if (Mode->UsingIpv6) { + Port = ((EFI_UDP6_SESSION_DATA *) Session)->DestinationPort; + } else { + Port = ((EFI_UDP4_SESSION_DATA *) Session)->DestinationPort; + } + + if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) != 0) { + // + // Return the destination port in the received packet if accept any. + // + if (DestPort != NULL) { + *DestPort = Port; + } + return TRUE; + } else if (DestPort != NULL && *DestPort == Port) { + // + // The destination port in the received packet is matched if present. + // + return TRUE; + } + + return FALSE; +} + + +/** + Filter the received packet using the source Ip. + + @param[in] Mode The pointer to the mode data of PxeBc. + @param[in] Session The pointer to the current UDPv4 session. + @param[in, out] SrcIp The pointer to the source Ip address. + @param[in] OpFlags Operation flag for UdpRead/UdpWrite. + + @retval TRUE Passed the IPv4 filter successfully. + @retval FALSE Failed to pass the IPv4 filter. + +**/ +BOOLEAN +PxeBcFilterBySrcIp ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN VOID *Session, + IN OUT EFI_IP_ADDRESS *SrcIp, + IN UINT16 OpFlags + ) +{ + if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) != 0) { + // + // Copy the source address from the received packet if accept any. + // + if (SrcIp != NULL) { + if (Mode->UsingIpv6) { + CopyMem ( + SrcIp, + &((EFI_UDP6_SESSION_DATA *)Session)->SourceAddress, + sizeof (EFI_IPv6_ADDRESS) + ); + } else { + ZeroMem (SrcIp, sizeof (EFI_IP_ADDRESS)); + CopyMem ( + SrcIp, + &((EFI_UDP4_SESSION_DATA *)Session)->SourceAddress, + sizeof (EFI_IPv4_ADDRESS) + ); + } + + } + return TRUE; + } else if (SrcIp != NULL && + (EFI_IP4_EQUAL (SrcIp, &((EFI_UDP4_SESSION_DATA *)Session)->SourceAddress) || + EFI_IP6_EQUAL (SrcIp, &((EFI_UDP6_SESSION_DATA *)Session)->SourceAddress))) { + // + // The source address in the received packet is matched if present. + // + return TRUE; + } + + return FALSE; +} + + +/** + Filter the received packet using the source port. + + @param[in] Mode The pointer to the mode data of PxeBc. + @param[in] Session The pointer to the current UDPv4 session. + @param[in, out] SrcPort The pointer to the source port. + @param[in] OpFlags Operation flag for UdpRead/UdpWrite. + + @retval TRUE Passed the IPv4 filter successfully. + @retval FALSE Failed to pass the IPv4 filter. + +**/ +BOOLEAN +PxeBcFilterBySrcPort ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN VOID *Session, + IN OUT UINT16 *SrcPort, + IN UINT16 OpFlags + ) +{ + UINT16 Port; + + if (Mode->UsingIpv6) { + Port = ((EFI_UDP6_SESSION_DATA *) Session)->SourcePort; + } else { + Port = ((EFI_UDP4_SESSION_DATA *) Session)->SourcePort; + } + + if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) != 0) { + // + // Return the source port in the received packet if accept any. + // + if (SrcPort != NULL) { + *SrcPort = Port; + } + return TRUE; + } else if (SrcPort != NULL && *SrcPort == Port) { + // + // The source port in the received packet is matched if present. + // + return TRUE; + } + + return FALSE; +} + + +/** + This function is to receive packet using Udp4Read. + + @param[in] Udp4 The pointer to EFI_UDP4_PROTOCOL. + @param[in] Token The pointer to EFI_UDP4_COMPLETION_TOKEN. + @param[in] Mode The pointer to EFI_PXE_BASE_CODE_MODE. + @param[in] TimeoutEvent The event for timeout. + @param[in] OpFlags The UDP operation flags. + @param[in] IsDone The pointer to the IsDone flag. + @param[out] IsMatched The pointer to the IsMatched flag. + @param[in, out] DestIp The pointer to the destination address. + @param[in, out] DestPort The pointer to the destination port. + @param[in, out] SrcIp The pointer to the source address. + @param[in, out] SrcPort The pointer to the source port. + + @retval EFI_SUCCESS Successfully read the data using Udp4. + @retval Others Failed to send out data. + +**/ +EFI_STATUS +PxeBcUdp4Read ( + IN EFI_UDP4_PROTOCOL *Udp4, + IN EFI_UDP4_COMPLETION_TOKEN *Token, + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN EFI_EVENT TimeoutEvent, + IN UINT16 OpFlags, + IN BOOLEAN *IsDone, + OUT BOOLEAN *IsMatched, + IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL, + IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL, + IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL, + IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL + ) +{ + EFI_UDP4_RECEIVE_DATA *RxData; + EFI_UDP4_SESSION_DATA *Session; + EFI_STATUS Status; + + Token->Status = EFI_NOT_READY; + *IsDone = FALSE; + + Status = Udp4->Receive (Udp4, Token); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Poll the UDPv6 read instance if no packet received and no timeout triggered. + // + while (!(*IsDone) && + Token->Status == EFI_NOT_READY && + EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) { + // + // Poll the token utill reply/ICMPv6 error message received or timeout. + // + Udp4->Poll (Udp4); + if (Token->Status == EFI_ICMP_ERROR || + Token->Status == EFI_NETWORK_UNREACHABLE || + Token->Status == EFI_HOST_UNREACHABLE || + Token->Status == EFI_PROTOCOL_UNREACHABLE || + Token->Status == EFI_PORT_UNREACHABLE) { + break; + } + } + + Status = (Token->Status == EFI_NOT_READY) ? EFI_TIMEOUT : Token->Status; + + if (!EFI_ERROR (Status)) { + // + // check whether this packet matches the filters + // + RxData = Token->Packet.RxData; + Session = &RxData->UdpSession; + + *IsMatched = PxeBcCheckByIpFilter (Mode, Session, OpFlags); + + if (*IsMatched) { + *IsMatched = PxeBcCheckByDestIp (Mode, Session, DestIp, OpFlags); + } + + if (*IsMatched) { + *IsMatched = PxeBcCheckByDestPort (Mode, Session, DestPort, OpFlags); + } + + if (*IsMatched) { + *IsMatched = PxeBcFilterBySrcIp (Mode, Session, SrcIp, OpFlags); + } + + if (*IsMatched) { + *IsMatched = PxeBcFilterBySrcPort (Mode, Session, SrcPort, OpFlags); + } + + if (!(*IsMatched)) { + // + // Recycle the receiving buffer if not matched. + // + gBS->SignalEvent (RxData->RecycleSignal); + } + } + + return Status; +} + + +/** + This function is to receive packets using Udp6Read. + + @param[in] Udp6 The pointer to EFI_UDP6_PROTOCOL. + @param[in] Token The pointer to EFI_UDP6_COMPLETION_TOKEN. + @param[in] Mode The pointer to EFI_PXE_BASE_CODE_MODE. + @param[in] TimeoutEvent The event for timeout. + @param[in] OpFlags The UDP operation flags. + @param[in] IsDone The pointer to the IsDone flag. + @param[out] IsMatched The pointer to the IsMatched flag. + @param[in, out] DestIp The pointer to the destination address. + @param[in, out] DestPort The pointer to the destination port. + @param[in, out] SrcIp The pointer to the source address. + @param[in, out] SrcPort The pointer to the source port. + + @retval EFI_SUCCESS Successfully read data using Udp6. + @retval Others Failed to send out data. + +**/ +EFI_STATUS +PxeBcUdp6Read ( + IN EFI_UDP6_PROTOCOL *Udp6, + IN EFI_UDP6_COMPLETION_TOKEN *Token, + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN EFI_EVENT TimeoutEvent, + IN UINT16 OpFlags, + IN BOOLEAN *IsDone, + OUT BOOLEAN *IsMatched, + IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL, + IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL, + IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL, + IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL + ) +{ + EFI_UDP6_RECEIVE_DATA *RxData; + EFI_UDP6_SESSION_DATA *Session; + EFI_STATUS Status; + + Token->Status = EFI_NOT_READY; + *IsDone = FALSE; + + Status = Udp6->Receive (Udp6, Token); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Poll the UDPv6 read instance if no packet received and no timeout triggered. + // + while (!(*IsDone) && + Token->Status == EFI_NOT_READY && + EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) { + // + // Poll the token utill reply/ICMPv6 error message received or timeout. + // + Udp6->Poll (Udp6); + if (Token->Status == EFI_ICMP_ERROR || + Token->Status == EFI_NETWORK_UNREACHABLE || + Token->Status == EFI_HOST_UNREACHABLE || + Token->Status == EFI_PROTOCOL_UNREACHABLE || + Token->Status == EFI_PORT_UNREACHABLE) { + break; + } + } + + Status = (Token->Status == EFI_NOT_READY) ? EFI_TIMEOUT : Token->Status; + + if (!EFI_ERROR (Status)) { + // + // check whether this packet matches the filters + // + RxData = Token->Packet.RxData; + Session = &RxData->UdpSession; + + *IsMatched = PxeBcCheckByIpFilter (Mode, Session, OpFlags); + + if (*IsMatched) { + *IsMatched = PxeBcCheckByDestIp (Mode, Session, DestIp, OpFlags); + } + + if (*IsMatched) { + *IsMatched = PxeBcCheckByDestPort (Mode, Session, DestPort, OpFlags); + } + + if (*IsMatched) { + *IsMatched = PxeBcFilterBySrcIp (Mode, Session, SrcIp, OpFlags); + } + + if (*IsMatched) { + *IsMatched = PxeBcFilterBySrcPort (Mode, Session, SrcPort, OpFlags); + } + + if (!(*IsMatched)) { + // + // Recycle the receiving buffer if not matched. + // + gBS->SignalEvent (RxData->RecycleSignal); + } + } + + return Status; +} + + +/** + This function is to display the IPv4 address. + + @param[in] Ip The pointer to the IPv4 address. + +**/ +VOID +PxeBcShowIp4Addr ( + IN EFI_IPv4_ADDRESS *Ip + ) +{ + UINTN Index; + + for (Index = 0; Index < 4; Index++) { + AsciiPrint ("%d", Ip->Addr[Index]); + if (Index < 3) { + AsciiPrint ("."); + } + } +} + + +/** + This function is to display the IPv6 address. + + @param[in] Ip The pointer to the IPv6 address. + +**/ +VOID +PxeBcShowIp6Addr ( + IN EFI_IPv6_ADDRESS *Ip + ) +{ + UINTN Index; + + for (Index = 0; Index < 16; Index++) { + + if (Ip->Addr[Index] != 0) { + AsciiPrint ("%x", Ip->Addr[Index]); + } + Index++; + if (Index > 15) { + return; + } + if (((Ip->Addr[Index] & 0xf0) == 0) && (Ip->Addr[Index - 1] != 0)) { + AsciiPrint ("0"); + } + AsciiPrint ("%x", Ip->Addr[Index]); + if (Index < 15) { + AsciiPrint (":"); + } + } +} + + +/** + This function is to convert UINTN to ASCII string with the required formatting. + + @param[in] Number Numeric value to be converted. + @param[in] Buffer The pointer to the buffer for ASCII string. + @param[in] Length The length of the required format. + +**/ +VOID +PxeBcUintnToAscDecWithFormat ( + IN UINTN Number, + IN UINT8 *Buffer, + IN INTN Length + ) +{ + UINTN Remainder; + + for (; Length > 0; Length--) { + Remainder = Number % 10; + Number /= 10; + Buffer[Length - 1] = (UINT8) ('0' + Remainder); + } +} + + +/** + This function is to convert a UINTN to a ASCII string, and return the + actual length of the buffer. + + @param[in] Number Numeric value to be converted. + @param[in] Buffer The pointer to the buffer for ASCII string. + @param[in] BufferSize The maxsize of the buffer. + + @return Length The actual length of the ASCII string. + +**/ +UINTN +PxeBcUintnToAscDec ( + IN UINTN Number, + IN UINT8 *Buffer, + IN UINTN BufferSize + ) +{ + UINTN Index; + UINTN Length; + CHAR8 TempStr[64]; + + Index = 63; + TempStr[Index] = 0; + + do { + Index--; + TempStr[Index] = (CHAR8) ('0' + (Number % 10)); + Number = (UINTN) (Number / 10); + } while (Number != 0); + + AsciiStrCpyS ((CHAR8 *) Buffer, BufferSize, &TempStr[Index]); + + Length = AsciiStrLen ((CHAR8 *) Buffer); + + return Length; +} + + +/** + This function is to convert unicode hex number to a UINT8. + + @param[out] Digit The converted UINT8 for output. + @param[in] Char The unicode hex number to be converted. + + @retval EFI_SUCCESS Successfully converted the unicode hex. + @retval EFI_INVALID_PARAMETER Failed to convert the unicode hex. + +**/ +EFI_STATUS +PxeBcUniHexToUint8 ( + OUT UINT8 *Digit, + IN CHAR16 Char + ) +{ + if ((Char >= L'0') && (Char <= L'9')) { + *Digit = (UINT8) (Char - L'0'); + return EFI_SUCCESS; + } + + if ((Char >= L'A') && (Char <= L'F')) { + *Digit = (UINT8) (Char - L'A' + 0x0A); + return EFI_SUCCESS; + } + + if ((Char >= L'a') && (Char <= L'f')) { + *Digit = (UINT8) (Char - L'a' + 0x0A); + return EFI_SUCCESS; + } + + return EFI_INVALID_PARAMETER; +} + +/** + Calculate the elapsed time. + + @param[in] Private The pointer to PXE private data + +**/ +VOID +CalcElapsedTime ( + IN PXEBC_PRIVATE_DATA *Private + ) +{ + EFI_TIME Time; + UINT64 CurrentStamp; + UINT64 ElapsedTimeValue; + + // + // Generate a time stamp of the centiseconds from 1900/1/1, assume 30day/month. + // + ZeroMem (&Time, sizeof (EFI_TIME)); + gRT->GetTime (&Time, NULL); + CurrentStamp = MultU64x32 ( + ((((UINT32)(Time.Year - 1900) * 360 + (Time.Month - 1) * 30 + (Time.Day - 1)) * 24 + Time.Hour) * 60 + Time.Minute) * 60 + Time.Second, + 100 + ) + + DivU64x32 ( + Time.Nanosecond, + 10000000 + ); + + // + // Sentinel value of 0 means that this is the first DHCP packet that we are + // sending and that we need to initialize the value. First DHCP Solicit + // gets 0 elapsed-time. Otherwise, calculate based on StartTime. + // + if (Private->ElapsedTime == 0) { + Private->ElapsedTime = CurrentStamp; + } else { + ElapsedTimeValue = CurrentStamp - Private->ElapsedTime; + + // + // If elapsed time cannot fit in two bytes, set it to 0xffff. + // + if (ElapsedTimeValue > 0xffff) { + ElapsedTimeValue = 0xffff; + } + // + // Save the elapsed time + // + Private->ElapsedTime = ElapsedTimeValue; + } +} + diff --git a/NetworkPkg/UefiPxeBcDxe/PxeBcSupport.h b/NetworkPkg/UefiPxeBcDxe/PxeBcSupport.h new file mode 100644 index 000000000..9264cd92c --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/PxeBcSupport.h @@ -0,0 +1,513 @@ +/** @file + Support functions declaration for UefiPxeBc Driver. + + Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_PXEBC_SUPPORT_H__ +#define __EFI_PXEBC_SUPPORT_H__ + + +#define ICMP_DEST_UNREACHABLE 3 +#define ICMP_SOURCE_QUENCH 4 +#define ICMP_REDIRECT 5 +#define ICMP_ECHO_REQUEST 8 +#define ICMP_TIME_EXCEEDED 11 +#define ICMP_PARAMETER_PROBLEM 12 + + + +/** + Flush the previous configration using the new station Ip address. + + @param[in] Private Pointer to PxeBc private data. + @param[in] StationIp Pointer to the station Ip address. + @param[in] SubnetMask Pointer to the subnet mask address for v4. + + @retval EFI_SUCCESS Successfully flushed the previous config. + @retval Others Failed to flush using the new station Ip. + +**/ +EFI_STATUS +PxeBcFlushStationIp ( + PXEBC_PRIVATE_DATA *Private, + EFI_IP_ADDRESS *StationIp, OPTIONAL + EFI_IP_ADDRESS *SubnetMask OPTIONAL + ); + + +/** + Notify callback function when an event is triggered. + + @param[in] Event The triggered event. + @param[in] Context The opaque parameter to the function. + +**/ +VOID +EFIAPI +PxeBcCommonNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ); + + +/** + Perform arp resolution from the arp cache in PxeBcMode. + + @param Mode Pointer to EFI_PXE_BASE_CODE_MODE. + @param Ip4Addr The Ip4 address for resolution. + @param MacAddress The resoluted MAC address if the resolution is successful. + The value is undefined if resolution fails. + + @retval TRUE Found a matched entry. + @retval FALSE Did not find a matched entry. + +**/ +BOOLEAN +PxeBcCheckArpCache ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN EFI_IPv4_ADDRESS *Ip4Addr, + OUT EFI_MAC_ADDRESS *MacAddress + ); + + +/** + Update arp cache periodically. + + @param Event Pointer to EFI_PXE_BC_PROTOCOL. + @param Context Context of the timer event. + +**/ +VOID +EFIAPI +PxeBcArpCacheUpdate ( + IN EFI_EVENT Event, + IN VOID *Context + ); + + +/** + xxx + + @param Event The event signaled. + @param Context The context passed in by the event notifier. + +**/ +VOID +EFIAPI +PxeBcIcmpErrorUpdate ( + IN EFI_EVENT Event, + IN VOID *Context + ); + + +/** + xxx + + @param Event The event signaled. + @param Context The context passed in by the event notifier. + +**/ +VOID +EFIAPI +PxeBcIcmp6ErrorUpdate ( + IN EFI_EVENT Event, + IN VOID *Context + ); + + +/** + This function is to configure a UDPv4 instance for UdpWrite. + + @param[in] Udp4 The pointer to EFI_UDP4_PROTOCOL. + @param[in] StationIp The pointer to the station address. + @param[in] SubnetMask The pointer to the subnet mask. + @param[in] Gateway The pointer to the gateway address. + @param[in, out] SrcPort The pointer to the source port. + @param[in] DoNotFragment If TRUE, fragment is not enabled. + Otherwise, fragment is enabled. + @param[in] Ttl The time to live field of the IP header. + @param[in] ToS The type of service field of the IP header. + + @retval EFI_SUCCESS Successfully configured this instance. + @retval Others Failed to configure this instance. + +**/ +EFI_STATUS +PxeBcConfigUdp4Write ( + IN EFI_UDP4_PROTOCOL *Udp4, + IN EFI_IPv4_ADDRESS *StationIp, + IN EFI_IPv4_ADDRESS *SubnetMask, + IN EFI_IPv4_ADDRESS *Gateway, + IN OUT UINT16 *SrcPort, + IN BOOLEAN DoNotFragment, + IN UINT8 Ttl, + IN UINT8 ToS + ); + + +/** + This function is to configure a UDPv6 instance for UdpWrite. + + @param[in] Udp6 Pointer to EFI_UDP6_PROTOCOL. + @param[in] StationIp Pointer to the station address. + @param[in, out] SrcPort Pointer to the source port. + + @retval EFI_SUCCESS Successfuly configured this instance. + @retval Others Failed to configure this instance. + +**/ +EFI_STATUS +PxeBcConfigUdp6Write ( + IN EFI_UDP6_PROTOCOL *Udp6, + IN EFI_IPv6_ADDRESS *StationIp, + IN OUT UINT16 *SrcPort + ); + +/** + This function is to configure a UDPv4 instance for UdpWrite. + + @param[in] Udp4 Pointer to EFI_UDP4_PROTOCOL. + @param[in] Session Pointer to the UDP4 session data. + @param[in] TimeoutEvent The event for timeout. + @param[in] Gateway Pointer to the gateway address. + @param[in] HeaderSize An optional field which may be set to the length of a header + at HeaderPtr to be prefixed to the data at BufferPtr. + @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be + prefixed to the data at BufferPtr. + @param[in] BufferSize A pointer to the size of the data at BufferPtr. + @param[in] BufferPtr A pointer to the data to be written. + + @retval EFI_SUCCESS Successfully sent out data with Udp4Write. + @retval Others Failed to send out data. + +**/ +EFI_STATUS +PxeBcUdp4Write ( + IN EFI_UDP4_PROTOCOL *Udp4, + IN EFI_UDP4_SESSION_DATA *Session, + IN EFI_EVENT TimeoutEvent, + IN EFI_IPv4_ADDRESS *Gateway OPTIONAL, + IN UINTN *HeaderSize OPTIONAL, + IN VOID *HeaderPtr OPTIONAL, + IN UINTN *BufferSize, + IN VOID *BufferPtr + ); + + +/** + This function is to configure a UDPv6 instance for UdpWrite. + + @param[in] Udp6 Pointer to EFI_UDP6_PROTOCOL. + @param[in] Session Pointer to the UDP6 session data. + @param[in] TimeoutEvent The event for timeout. + @param[in] HeaderSize An optional field which may be set to the length of a header + at HeaderPtr to be prefixed to the data at BufferPtr. + @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be + prefixed to the data at BufferPtr. + @param[in] BufferSize A pointer to the size of the data at BufferPtr. + @param[in] BufferPtr A pointer to the data to be written. + + @retval EFI_SUCCESS Successfully to send out data with Udp6Write. + @retval Others Failed to send out data. + +**/ +EFI_STATUS +PxeBcUdp6Write ( + IN EFI_UDP6_PROTOCOL *Udp6, + IN EFI_UDP6_SESSION_DATA *Session, + IN EFI_EVENT TimeoutEvent, + IN UINTN *HeaderSize OPTIONAL, + IN VOID *HeaderPtr OPTIONAL, + IN UINTN *BufferSize, + IN VOID *BufferPtr + ); + + +/** + Check the received packet with the Ip filter. + + @param[in] Mode Pointer to mode data of PxeBc. + @param[in] Session Pointer to the current UDPv4 session. + @param[in] OpFlags Operation flag for UdpRead/UdpWrite. + + @retval TRUE Succesfully passed the Ip filter. + @retval FALSE Failed to pass the Ip filter. + +**/ +BOOLEAN +PxeBcCheckByIpFilter ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN VOID *Session, + IN UINT16 OpFlags + ); + + +/** + Filter the received packet with the destination Ip. + + @param[in] Mode Pointer to mode data of PxeBc. + @param[in] Session Pointer to the current UDPv4 session. + @param[in, out] DestIp Pointer to the dest Ip address. + @param[in] OpFlags Operation flag for UdpRead/UdpWrite. + + @retval TRUE Succesfully passed the IPv4 filter. + @retval FALSE Failed to pass the IPv4 filter. + +**/ +BOOLEAN +PxeBcCheckByDestIp ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN VOID *Session, + IN OUT EFI_IP_ADDRESS *DestIp, + IN UINT16 OpFlags + ); + + +/** + Check the received packet with the destination port. + + @param[in] Mode Pointer to mode data of PxeBc. + @param[in] Session Pointer to the current UDPv4 session. + @param[in, out] DestPort Pointer to the destination port. + @param[in] OpFlags Operation flag for UdpRead/UdpWrite. + + @retval TRUE Succesfully passed the IPv4 filter. + @retval FALSE Failed to pass the IPv4 filter. + +**/ +BOOLEAN +PxeBcCheckByDestPort ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN VOID *Session, + IN OUT UINT16 *DestPort, + IN UINT16 OpFlags + ); + + +/** + Filter the received packet with the source Ip. + + @param[in] Mode Pointer to mode data of PxeBc. + @param[in] Session Pointer to the current UDPv4 session. + @param[in, out] SrcIp Pointer to the source Ip address. + @param[in] OpFlags Operation flag for UdpRead/UdpWrite. + + @retval TRUE Succesfully passed the IPv4 filter. + @retval FALSE Failed to pass the IPv4 filter. + +**/ +BOOLEAN +PxeBcFilterBySrcIp ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN VOID *Session, + IN OUT EFI_IP_ADDRESS *SrcIp, + IN UINT16 OpFlags + ); + + +/** + Filter the received packet with the source port. + + @param[in] Mode Pointer to mode data of PxeBc. + @param[in] Session Pointer to the current UDPv4 session. + @param[in, out] SrcPort Pointer to the source port. + @param[in] OpFlags Operation flag for UdpRead/UdpWrite. + + @retval TRUE Succesfully passed the IPv4 filter. + @retval FALSE Failed to pass the IPv4 filter. + +**/ +BOOLEAN +PxeBcFilterBySrcPort ( + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN VOID *Session, + IN OUT UINT16 *SrcPort, + IN UINT16 OpFlags + ); + + +/** + This function is to receive packet with Udp4Read. + + @param[in] Udp4 Pointer to EFI_UDP4_PROTOCOL. + @param[in] Token Pointer to EFI_UDP4_COMPLETION_TOKEN. + @param[in] Mode Pointer to EFI_PXE_BASE_CODE_MODE. + @param[in] TimeoutEvent The event for timeout. + @param[in] OpFlags The UDP operation flags. + @param[in] IsDone Pointer to IsDone flag. + @param[out] IsMatched Pointer to IsMatched flag. + @param[in, out] DestIp Pointer to destination address. + @param[in, out] DestPort Pointer to destination port. + @param[in, out] SrcIp Pointer to source address. + @param[in, out] SrcPort Pointer to source port. + + @retval EFI_SUCCESS Successfully read data with Udp4. + @retval Others Failed to send out data. + +**/ +EFI_STATUS +PxeBcUdp4Read ( + IN EFI_UDP4_PROTOCOL *Udp4, + IN EFI_UDP4_COMPLETION_TOKEN *Token, + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN EFI_EVENT TimeoutEvent, + IN UINT16 OpFlags, + IN BOOLEAN *IsDone, + OUT BOOLEAN *IsMatched, + IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL, + IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL, + IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL, + IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL + ); + + +/** + This function is to receive packet with Udp6Read. + + @param[in] Udp6 Pointer to EFI_UDP6_PROTOCOL. + @param[in] Token Pointer to EFI_UDP6_COMPLETION_TOKEN. + @param[in] Mode Pointer to EFI_PXE_BASE_CODE_MODE. + @param[in] TimeoutEvent The event for timeout. + @param[in] OpFlags The UDP operation flags. + @param[in] IsDone Pointer to IsDone flag. + @param[out] IsMatched Pointer to IsMatched flag. + @param[in, out] DestIp Pointer to destination address. + @param[in, out] DestPort Pointer to destination port. + @param[in, out] SrcIp Pointer to source address. + @param[in, out] SrcPort Pointer to source port. + + @retval EFI_SUCCESS Successfully read data with Udp6. + @retval Others Failed to send out data. + +**/ +EFI_STATUS +PxeBcUdp6Read ( + IN EFI_UDP6_PROTOCOL *Udp6, + IN EFI_UDP6_COMPLETION_TOKEN *Token, + IN EFI_PXE_BASE_CODE_MODE *Mode, + IN EFI_EVENT TimeoutEvent, + IN UINT16 OpFlags, + IN BOOLEAN *IsDone, + OUT BOOLEAN *IsMatched, + IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL, + IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL, + IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL, + IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL + ); + + +/** + This function is to display the IPv4 address. + + @param[in] Ip Pointer to the IPv4 address. + +**/ +VOID +PxeBcShowIp4Addr ( + IN EFI_IPv4_ADDRESS *Ip + ); + + +/** + This function is to display the IPv6 address. + + @param[in] Ip Pointer to the IPv6 address. + +**/ +VOID +PxeBcShowIp6Addr ( + IN EFI_IPv6_ADDRESS *Ip + ); + + +/** + This function is to convert UINTN to ASCII string with required format. + + @param[in] Number Numeric value to be converted. + @param[in] Buffer Pointer to the buffer for ASCII string. + @param[in] Length Length of the required format. + +**/ +VOID +PxeBcUintnToAscDecWithFormat ( + IN UINTN Number, + IN UINT8 *Buffer, + IN INTN Length + ); + + +/** + This function is to convert a UINTN to a ASCII string, and return the + actual length of the buffer. + + @param[in] Number Numeric value to be converted. + @param[in] Buffer Pointer to the buffer for ASCII string. + @param[in] BufferSize The maxsize of the buffer. + + @return Length The actual length of the ASCII string. + +**/ +UINTN +PxeBcUintnToAscDec ( + IN UINTN Number, + IN UINT8 *Buffer, + IN UINTN BufferSize + ); + +/** + This function is to convert unicode hex number to a UINT8. + + @param[out] Digit The converted UINT8 for output. + @param[in] Char The unicode hex number to be converted. + + @retval EFI_SUCCESS Successfully converted the unicode hex. + @retval EFI_INVALID_PARAMETER Failed to convert the unicode hex. + +**/ +EFI_STATUS +PxeBcUniHexToUint8 ( + OUT UINT8 *Digit, + IN CHAR16 Char + ); + +/** + Calculate the elapsed time. + + @param[in] Private The pointer to PXE private data + +**/ +VOID +CalcElapsedTime ( + IN PXEBC_PRIVATE_DATA *Private + ); + +/** + Get the Nic handle using any child handle in the IPv4 stack. + + @param[in] ControllerHandle Pointer to child handle over IPv4. + + @return NicHandle The pointer to the Nic handle. + +**/ +EFI_HANDLE +PxeBcGetNicByIp4Children ( + IN EFI_HANDLE ControllerHandle + ); + +/** + Get the Nic handle using any child handle in the IPv6 stack. + + @param[in] ControllerHandle Pointer to child handle over IPv6. + + @return NicHandle The pointer to the Nic handle. + +**/ +EFI_HANDLE +PxeBcGetNicByIp6Children ( + IN EFI_HANDLE ControllerHandle + ); +#endif diff --git a/NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.inf b/NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.inf new file mode 100644 index 000000000..0341f1052 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.inf @@ -0,0 +1,109 @@ +## @file +# Access PXE-compatible devices for network access and network booting. +# +# This driver provides PXE Base Code Protocol which is used to accessing +# PXE-compatible device for network access or booting. This driver supports +# both IPv4 and IPv6 network stack. +# +# Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = UefiPxeBcDxe + FILE_GUID = B95E9FDA-26DE-48d2-8807-1F9107AC5E3A + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = PxeBcDriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload + MODULE_UNI_FILE = UefiPxeBcDxe.uni + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + ComponentName.c + PxeBcDriver.c + PxeBcDriver.h + PxeBcImpl.c + PxeBcImpl.h + PxeBcBoot.c + PxeBcBoot.h + PxeBcDhcp6.c + PxeBcDhcp6.h + PxeBcDhcp4.c + PxeBcDhcp4.h + PxeBcMtftp.c + PxeBcMtftp.h + PxeBcSupport.c + PxeBcSupport.h + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + + +[LibraryClasses] + BaseLib + UefiLib + UefiBootServicesTableLib + UefiDriverEntryPoint + BaseMemoryLib + MemoryAllocationLib + DebugLib + NetLib + DpcLib + DevicePathLib + PcdLib + +[Protocols] + ## TO_START + ## SOMETIMES_CONSUMES + gEfiDevicePathProtocolGuid + gEfiNetworkInterfaceIdentifierProtocolGuid_31 ## SOMETIMES_CONSUMES + gEfiArpServiceBindingProtocolGuid ## TO_START + gEfiArpProtocolGuid ## TO_START + gEfiIp4ServiceBindingProtocolGuid ## TO_START + gEfiIp4ProtocolGuid ## TO_START + gEfiIp4Config2ProtocolGuid ## TO_START + gEfiIp6ServiceBindingProtocolGuid ## TO_START + gEfiIp6ProtocolGuid ## TO_START + gEfiIp6ConfigProtocolGuid ## TO_START + gEfiUdp4ServiceBindingProtocolGuid ## TO_START + gEfiUdp4ProtocolGuid ## TO_START + gEfiMtftp4ServiceBindingProtocolGuid ## TO_START + gEfiMtftp4ProtocolGuid ## TO_START + gEfiDhcp4ServiceBindingProtocolGuid ## TO_START + gEfiDhcp4ProtocolGuid ## TO_START + gEfiUdp6ServiceBindingProtocolGuid ## TO_START + gEfiUdp6ProtocolGuid ## TO_START + gEfiMtftp6ServiceBindingProtocolGuid ## TO_START + gEfiMtftp6ProtocolGuid ## TO_START + gEfiDhcp6ServiceBindingProtocolGuid ## TO_START + gEfiDhcp6ProtocolGuid ## TO_START + gEfiDns6ServiceBindingProtocolGuid ## SOMETIMES_CONSUMES + gEfiDns6ProtocolGuid ## SOMETIMES_CONSUMES + gEfiPxeBaseCodeCallbackProtocolGuid ## SOMETIMES_PRODUCES + gEfiPxeBaseCodeProtocolGuid ## BY_START + gEfiLoadFileProtocolGuid ## BY_START + gEfiAdapterInformationProtocolGuid ## SOMETIMES_CONSUMES + +[Guids] + gEfiAdapterInfoUndiIpv6SupportGuid ## SOMETIMES_CONSUMES ## GUID + +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdTftpBlockSize ## SOMETIMES_CONSUMES + gEfiNetworkPkgTokenSpaceGuid.PcdPxeTftpWindowSize ## SOMETIMES_CONSUMES + gEfiNetworkPkgTokenSpaceGuid.PcdIPv4PXESupport ## CONSUMES + gEfiNetworkPkgTokenSpaceGuid.PcdIPv6PXESupport ## CONSUMES + +[UserExtensions.TianoCore."ExtraFiles"] + UefiPxeBcDxeExtra.uni diff --git a/NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.uni b/NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.uni new file mode 100644 index 000000000..a34308221 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxe.uni @@ -0,0 +1,18 @@ +// /** @file +// Access PXE-compatible devices for network access and network booting. +// +// This driver provides PXE Base Code Protocol which is used to accessing +// PXE-compatible device for network access or booting. It could work together +// with an IPv4 stack, an IPv6 stack or both. +// +// Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Access PXE-compatible devices for network access and network booting" + +#string STR_MODULE_DESCRIPTION #language en-US "This driver provides PXE Base Code Protocol which is used to accessing PXE-compatible device for network access or booting. It could work together with an IPv4 stack, an IPv6 stack or both." + diff --git a/NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxeExtra.uni b/NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxeExtra.uni new file mode 100644 index 000000000..fb694f637 --- /dev/null +++ b/NetworkPkg/UefiPxeBcDxe/UefiPxeBcDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// UefiPxeBcDxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"Uefi PXE DXE" + + diff --git a/NetworkPkg/VlanConfigDxe/ComponentName.c b/NetworkPkg/VlanConfigDxe/ComponentName.c new file mode 100644 index 000000000..f36d29f8b --- /dev/null +++ b/NetworkPkg/VlanConfigDxe/ComponentName.c @@ -0,0 +1,164 @@ +/** @file + UEFI Component Name(2) protocol implementation for VlanConfigDxe driver. + +Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "VlanConfigImpl.h" + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gVlanConfigComponentName = { + VlanConfigComponentNameGetDriverName, + VlanConfigComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gVlanConfigComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) VlanConfigComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) VlanConfigComponentNameGetControllerName, + "en" +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mVlanConfigDriverNameTable[] = { + { + "eng;en", + L"VLAN Configuration Driver" + }, + { + NULL, + NULL + } +}; + +// +// EFI Component Name Functions +// + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + @param DriverName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +VlanConfigComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mVlanConfigDriverNameTable, + DriverName, + (BOOLEAN)(This == &gVlanConfigComponentName) + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + @param ControllerHandle[in] The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + @param ChildHandle[in] The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + @param ControllerName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +VlanConfigComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + return EFI_UNSUPPORTED; +} diff --git a/NetworkPkg/VlanConfigDxe/VlanConfig.vfr b/NetworkPkg/VlanConfigDxe/VlanConfig.vfr new file mode 100644 index 000000000..e49332529 --- /dev/null +++ b/NetworkPkg/VlanConfigDxe/VlanConfig.vfr @@ -0,0 +1,72 @@ +///** @file +// VLAN configuration formset. +// +// Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.
+// SPDX-License-Identifier: BSD-2-Clause-Patent +// +//**/ + +#include "VlanConfigNvData.h" + +formset + guid = VLAN_CONFIG_FORM_SET_GUID, + title = STRING_TOKEN(STR_VLAN_FORM_SET_TITLE), + help = STRING_TOKEN(STR_VLAN_FORM_SET_TITLE_HELP), + classguid = EFI_HII_PLATFORM_SETUP_FORMSET_GUID, + + varstore VLAN_CONFIGURATION, + varid = VLAN_CONFIGURATION_VARSTORE_ID, + name = VlanNvData, + guid = VLAN_CONFIG_FORM_SET_GUID; + + form formid = VLAN_HEAD_FORM_ID, + title = STRING_TOKEN(STR_VLAN_FORM_TITLE); + + goto VLAN_CONFIGURATION_FORM_ID, + prompt = STRING_TOKEN (STR_GET_CURRENT_SETTING), + help = STRING_TOKEN (STR_GET_CURRENT_SETTING_HELP), + flags = INTERACTIVE, + key = VLAN_UPDATE_QUESTION_ID; + + endform; + + form formid = VLAN_CONFIGURATION_FORM_ID, + title = STRING_TOKEN(STR_VLAN_FORM_TITLE); + + subtitle text = STRING_TOKEN(STR_VLAN_CREATE_VLAN); + + numeric varid = VlanNvData.VlanId, + prompt = STRING_TOKEN(STR_VLAN_VID_PROMPT), + help = STRING_TOKEN(STR_VLAN_VID_HELP), + minimum = 0, + maximum = 4094, + endnumeric; + + numeric varid = VlanNvData.Priority, + prompt = STRING_TOKEN(STR_VLAN_PRIORITY_PROMPT), + help = STRING_TOKEN(STR_VLAN_PRIORITY_HELP), + minimum = 0, + maximum = 7, + endnumeric; + + text + help = STRING_TOKEN(STR_VLAN_ADD_VLAN_HELP), + text = STRING_TOKEN(STR_VLAN_ADD_VLAN_PROMPT), + flags = INTERACTIVE, + key = VLAN_ADD_QUESTION_ID; + + subtitle text = STRING_TOKEN(STR_VLAN_NULL_STRING); + subtitle text = STRING_TOKEN(STR_VLAN_VLAN_LIST); + + label LABEL_VLAN_LIST; + label LABEL_END; + + text + help = STRING_TOKEN(STR_VLAN_REMOVE_VLAN_HELP), + text = STRING_TOKEN(STR_VLAN_REMOVE_VLAN_PROMPT), + flags = INTERACTIVE, + key = VLAN_REMOVE_QUESTION_ID; + + endform; + +endformset; diff --git a/NetworkPkg/VlanConfigDxe/VlanConfigDriver.c b/NetworkPkg/VlanConfigDxe/VlanConfigDriver.c new file mode 100644 index 000000000..c717d9ea5 --- /dev/null +++ b/NetworkPkg/VlanConfigDxe/VlanConfigDriver.c @@ -0,0 +1,299 @@ +/** @file + The driver binding for VLAN configuration module. + +Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "VlanConfigImpl.h" + +EFI_DRIVER_BINDING_PROTOCOL gVlanConfigDriverBinding = { + VlanConfigDriverBindingSupported, + VlanConfigDriverBindingStart, + VlanConfigDriverBindingStop, + 0xa, + NULL, + NULL +}; + +/** + The entry point for IP4 config driver which install the driver + binding and component name protocol on its image. + + @param[in] ImageHandle The image handle of the driver. + @param[in] SystemTable The system table. + + @retval EFI_SUCCES All the related protocols are installed on the driver. + @retval Others Failed to install protocols. + +**/ +EFI_STATUS +EFIAPI +VlanConfigDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gVlanConfigDriverBinding, + ImageHandle, + &gVlanConfigComponentName, + &gVlanConfigComponentName2 + ); +} + + +/** + Test to see if this driver supports ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCES This driver supports this device + @retval EFI_ALREADY_STARTED This driver is already running on this device + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +VlanConfigDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiVlanConfigProtocolGuid, + (VOID **) &VlanConfig, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Close the VlanConfig protocol opened for supported test + // + gBS->CloseProtocol ( + ControllerHandle, + &gEfiVlanConfigProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + + return Status; +} + + +/** + Start this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCES This driver is added to ControllerHandle + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +VlanConfigDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + VLAN_CONFIG_PRIVATE_DATA *PrivateData; + + // + // Check for multiple start + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiCallerIdGuid, + (VOID **) &PrivateData, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + // + // Open VlanConfig protocol by driver + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiVlanConfigProtocolGuid, + (VOID **) &VlanConfig, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Get parent device path + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDevicePathProtocolGuid, + (VOID **) &DevicePath, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + goto ErrorExit; + } + + // + // Create a private data for this network device + // + PrivateData = AllocateCopyPool (sizeof (VLAN_CONFIG_PRIVATE_DATA), &mVlanConfigPrivateDateTemplate); + if (PrivateData == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ErrorExit; + } + + PrivateData->ImageHandle = This->DriverBindingHandle; + PrivateData->ControllerHandle = ControllerHandle; + PrivateData->VlanConfig = VlanConfig; + PrivateData->ParentDevicePath = DevicePath; + + // + // Install VLAN configuration form + // + Status = InstallVlanConfigForm (PrivateData); + if (EFI_ERROR (Status)) { + goto ErrorExit; + } + + // + // Install private GUID + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiCallerIdGuid, + PrivateData, + NULL + ); + if (EFI_ERROR (Status)) { + goto ErrorExit; + } + return Status; + +ErrorExit: + gBS->CloseProtocol ( + ControllerHandle, + &gEfiVlanConfigProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + + gBS->CloseProtocol ( + ControllerHandle, + &gEfiDevicePathProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + + if (PrivateData != NULL) { + UninstallVlanConfigForm (PrivateData); + FreePool (PrivateData); + } + + return Status; +} + + +/** + Stop this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number + of children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCES This driver is removed ControllerHandle + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +VlanConfigDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_STATUS Status; + VLAN_CONFIG_PRIVATE_DATA *PrivateData; + + // + // Retrieve the PrivateData from ControllerHandle + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiCallerIdGuid, + (VOID **) &PrivateData, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + ASSERT (PrivateData->Signature == VLAN_CONFIG_PRIVATE_DATA_SIGNATURE); + + if (NumberOfChildren != 0) { + if (NumberOfChildren != 1 || ChildHandleBuffer[0] != PrivateData->DriverHandle) { + return EFI_DEVICE_ERROR; + } + + return UninstallVlanConfigForm (PrivateData); + } + + // + // Uninstall the private GUID + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + ControllerHandle, + &gEfiCallerIdGuid, + PrivateData, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->CloseProtocol ( + ControllerHandle, + &gEfiVlanConfigProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + return Status; +} diff --git a/NetworkPkg/VlanConfigDxe/VlanConfigDxe.inf b/NetworkPkg/VlanConfigDxe/VlanConfigDxe.inf new file mode 100644 index 000000000..700cd881f --- /dev/null +++ b/NetworkPkg/VlanConfigDxe/VlanConfigDxe.inf @@ -0,0 +1,67 @@ +## @file +# This module provides one way to configurate VALN setting. +# +# This module produces EFI HII Configuration Access Protocol to provide one way to +# configurate VALN setting +# +# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = VlanConfigDxe + MODULE_UNI_FILE = VlanConfigDxe.uni + FILE_GUID = E4F61863-FE2C-4b56-A8F4-08519BC439DF + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = VlanConfigDriverEntryPoint + UNLOAD_IMAGE = NetLibDefaultUnload + + +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources] + ComponentName.c + VlanConfigDriver.c + VlanConfigImpl.c + VlanConfigImpl.h + VlanConfig.vfr + VlanConfigStrings.uni + VlanConfigNvData.h + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + MemoryAllocationLib + UefiLib + UefiBootServicesTableLib + UefiDriverEntryPoint + DebugLib + NetLib + HiiLib + +[Guids] + gEfiIfrTianoGuid ## SOMETIMES_PRODUCES ## UNDEFINED + ## SOMETIMES_CONSUMES ## UNDEFINED # HiiIsConfigHdrMatch mVlanStorageName + ## SOMETIMES_PRODUCES ## UNDEFINED # HiiConstructConfigHdr mVlanStorageName + ## SOMETIMES_PRODUCES ## UNDEFINED # HiiGetBrowserData mVlanStorageName + ## SOMETIMES_CONSUMES ## UNDEFINED # HiiSetBrowserData mVlanStorageName + ## SOMETIMES_CONSUMES ## HII + gVlanConfigFormSetGuid + +[Protocols] + gEfiHiiConfigAccessProtocolGuid ## BY_START + gEfiHiiConfigRoutingProtocolGuid ## CONSUMES + gEfiVlanConfigProtocolGuid ## TO_START + +[UserExtensions.TianoCore."ExtraFiles"] + VlanConfigDxeExtra.uni diff --git a/NetworkPkg/VlanConfigDxe/VlanConfigDxe.uni b/NetworkPkg/VlanConfigDxe/VlanConfigDxe.uni new file mode 100644 index 000000000..abc5a1ade --- /dev/null +++ b/NetworkPkg/VlanConfigDxe/VlanConfigDxe.uni @@ -0,0 +1,17 @@ +// /** @file +// This module provides one way to configurate VALN setting. +// +// This module produces EFI HII Configuration Access Protocol to provide one way to +// configurate VALN setting +// +// Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Provides a way to configure VLAN settings" + +#string STR_MODULE_DESCRIPTION #language en-US "This module produces EFI HII Configuration Access Protocol to provide a way to configure VLAN settings." + diff --git a/NetworkPkg/VlanConfigDxe/VlanConfigDxeExtra.uni b/NetworkPkg/VlanConfigDxe/VlanConfigDxeExtra.uni new file mode 100644 index 000000000..0c4eada24 --- /dev/null +++ b/NetworkPkg/VlanConfigDxe/VlanConfigDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file +// VlanConfigDxe Localized Strings and Content +// +// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_PROPERTIES_MODULE_NAME +#language en-US +"VLAN Configuration DXE Driver" + + diff --git a/NetworkPkg/VlanConfigDxe/VlanConfigImpl.c b/NetworkPkg/VlanConfigDxe/VlanConfigImpl.c new file mode 100644 index 000000000..24d844cbf --- /dev/null +++ b/NetworkPkg/VlanConfigDxe/VlanConfigImpl.c @@ -0,0 +1,664 @@ +/** @file + HII Config Access protocol implementation of VLAN configuration module. + +Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "VlanConfigImpl.h" + +CHAR16 mVlanStorageName[] = L"VlanNvData"; +EFI_HII_CONFIG_ROUTING_PROTOCOL *mHiiConfigRouting = NULL; + +VLAN_CONFIG_PRIVATE_DATA mVlanConfigPrivateDateTemplate = { + VLAN_CONFIG_PRIVATE_DATA_SIGNATURE, + { + VlanExtractConfig, + VlanRouteConfig, + VlanCallback + } +}; + +VENDOR_DEVICE_PATH mHiiVendorDevicePathNode = { + { + HARDWARE_DEVICE_PATH, + HW_VENDOR_DP, + { + (UINT8) (sizeof (VENDOR_DEVICE_PATH)), + (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8) + } + }, + VLAN_CONFIG_FORM_SET_GUID +}; + +/** + This function allows a caller to extract the current configuration for one + or more named elements from the target driver. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Request A null-terminated Unicode string in + format. + @param[out] Progress On return, points to a character in the Request + string. Points to the string's null terminator if + request was successful. Points to the most recent + '&' before the first failing name/value pair (or + the beginning of the string if the failure is in + the first name/value pair) if the request was not + successful. + @param[out] Results A null-terminated Unicode string in + format which has all values filled + in for the names in the Request string. String to + be allocated by the called function. + + @retval EFI_SUCCESS The Results is filled with the requested values. + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results. + @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name. + @retval EFI_NOT_FOUND Routing data doesn't match any storage in this + driver. + +**/ +EFI_STATUS +EFIAPI +VlanExtractConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Request, + OUT EFI_STRING *Progress, + OUT EFI_STRING *Results + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + VLAN_CONFIGURATION Configuration; + VLAN_CONFIG_PRIVATE_DATA *PrivateData; + EFI_STRING ConfigRequestHdr; + EFI_STRING ConfigRequest; + BOOLEAN AllocatedRequest; + UINTN Size; + + if (Progress == NULL || Results == NULL) { + return EFI_INVALID_PARAMETER; + } + + *Progress = Request; + if ((Request != NULL) && !HiiIsConfigHdrMatch (Request, &gVlanConfigFormSetGuid, mVlanStorageName)) { + return EFI_NOT_FOUND; + } + + ConfigRequestHdr = NULL; + ConfigRequest = NULL; + AllocatedRequest = FALSE; + Size = 0; + + // + // Retrieve the pointer to the UEFI HII Config Routing Protocol + // + if (mHiiConfigRouting == NULL) { + gBS->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid, NULL, (VOID **) &mHiiConfigRouting); + } + ASSERT (mHiiConfigRouting != NULL); + + // + // Convert buffer data to by helper function BlockToConfig() + // + PrivateData = VLAN_CONFIG_PRIVATE_DATA_FROM_THIS (This); + ZeroMem (&Configuration, sizeof (VLAN_CONFIGURATION)); + BufferSize = sizeof (Configuration); + ConfigRequest = Request; + if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) { + // + // Request has no request element, construct full request string. + // Allocate and fill a buffer large enough to hold the template + // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator + // + ConfigRequestHdr = HiiConstructConfigHdr (&gVlanConfigFormSetGuid, mVlanStorageName, PrivateData->DriverHandle); + Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16); + ConfigRequest = AllocateZeroPool (Size); + ASSERT (ConfigRequest != NULL); + AllocatedRequest = TRUE; + UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize); + FreePool (ConfigRequestHdr); + } + + Status = mHiiConfigRouting->BlockToConfig ( + mHiiConfigRouting, + ConfigRequest, + (UINT8 *) &Configuration, + BufferSize, + Results, + Progress + ); + // + // Free the allocated config request string. + // + if (AllocatedRequest) { + FreePool (ConfigRequest); + ConfigRequest = NULL; + } + // + // Set Progress string to the original request string. + // + if (Request == NULL) { + *Progress = NULL; + } else if (StrStr (Request, L"OFFSET") == NULL) { + *Progress = Request + StrLen (Request); + } + + return Status; +} + + +/** + This function processes the results of changes in configuration. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Configuration A null-terminated Unicode string in + format. + @param[out] Progress A pointer to a string filled in with the offset of + the most recent '&' before the first failing + name/value pair (or the beginning of the string if + the failure is in the first name/value pair) or + the terminating NULL if all was successful. + + @retval EFI_SUCCESS The Results is processed successfully. + @retval EFI_INVALID_PARAMETER Configuration is NULL. + @retval EFI_NOT_FOUND Routing data doesn't match any storage in this + driver. + +**/ +EFI_STATUS +EFIAPI +VlanRouteConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Configuration, + OUT EFI_STRING *Progress + ) +{ + if (Configuration == NULL || Progress == NULL) { + return EFI_INVALID_PARAMETER; + } + + *Progress = Configuration; + if (!HiiIsConfigHdrMatch (Configuration, &gVlanConfigFormSetGuid, mVlanStorageName)) { + return EFI_NOT_FOUND; + } + + *Progress = Configuration + StrLen (Configuration); + return EFI_SUCCESS; +} + +/** + This function processes the results of changes in configuration. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Action Specifies the type of action taken by the browser. + @param[in] QuestionId A unique value which is sent to the original + exporting driver so that it can identify the type + of data to expect. + @param[in] Type The type of value for the question. + @param[in] Value A pointer to the data being sent to the original + exporting driver. + @param[out] ActionRequest On return, points to the action requested by the + callback function. + + @retval EFI_SUCCESS The callback successfully handled the action. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the + variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be saved. + @retval EFI_UNSUPPORTED The specified Action is not supported by the + callback. + +**/ +EFI_STATUS +EFIAPI +VlanCallback ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN EFI_BROWSER_ACTION Action, + IN EFI_QUESTION_ID QuestionId, + IN UINT8 Type, + IN EFI_IFR_TYPE_VALUE *Value, + OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest + ) +{ + VLAN_CONFIG_PRIVATE_DATA *PrivateData; + VLAN_CONFIGURATION *Configuration; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + UINTN Index; + EFI_HANDLE VlanHandle; + + PrivateData = VLAN_CONFIG_PRIVATE_DATA_FROM_THIS (This); + + if ((Action == EFI_BROWSER_ACTION_FORM_OPEN) || (Action == EFI_BROWSER_ACTION_FORM_CLOSE)) { + return EFI_SUCCESS; + } + + if ((Action != EFI_BROWSER_ACTION_CHANGED) && (Action != EFI_BROWSER_ACTION_CHANGING)) { + // + // All other action return unsupported. + // + return EFI_UNSUPPORTED; + } + + // + // Get Browser data + // + Configuration = AllocateZeroPool (sizeof (VLAN_CONFIGURATION)); + ASSERT (Configuration != NULL); + HiiGetBrowserData (&gVlanConfigFormSetGuid, mVlanStorageName, sizeof (VLAN_CONFIGURATION), (UINT8 *) Configuration); + + VlanConfig = PrivateData->VlanConfig; + + if (Action == EFI_BROWSER_ACTION_CHANGED) { + switch (QuestionId) { + case VLAN_ADD_QUESTION_ID: + // + // Add a VLAN + // + VlanConfig->Set (VlanConfig, Configuration->VlanId, Configuration->Priority); + VlanUpdateForm (PrivateData); + + // + // Connect the newly created VLAN device + // + VlanHandle = NetLibGetVlanHandle (PrivateData->ControllerHandle, Configuration->VlanId); + if (VlanHandle == NULL) { + // + // There may be no child handle created for VLAN ID 0, connect the parent handle + // + VlanHandle = PrivateData->ControllerHandle; + } + gBS->ConnectController (VlanHandle, NULL, NULL, TRUE); + + // + // Clear UI data + // + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY; + Configuration->VlanId = 0; + Configuration->Priority = 0; + break; + + case VLAN_REMOVE_QUESTION_ID: + // + // Remove VLAN + // + ASSERT (PrivateData->NumberOfVlan <= MAX_VLAN_NUMBER); + for (Index = 0; Index < PrivateData->NumberOfVlan; Index++) { + if (Configuration->VlanList[Index] != 0) { + // + // Checkbox is selected, need remove this VLAN + // + VlanConfig->Remove (VlanConfig, PrivateData->VlanId[Index]); + } + } + + VlanUpdateForm (PrivateData); + if (PrivateData->NumberOfVlan == 0) { + // + // No VLAN device now, connect the physical NIC handle. + // Note: PrivateData->NumberOfVlan has been updated by VlanUpdateForm() + // + gBS->ConnectController (PrivateData->ControllerHandle, NULL, NULL, TRUE); + } + + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY; + ZeroMem (Configuration->VlanList, MAX_VLAN_NUMBER); + break; + + default: + break; + } + } else if (Action == EFI_BROWSER_ACTION_CHANGING) { + switch (QuestionId) { + case VLAN_UPDATE_QUESTION_ID: + // + // Update current VLAN list into Form. + // + VlanUpdateForm (PrivateData); + break; + + default: + break; + } + } + + HiiSetBrowserData (&gVlanConfigFormSetGuid, mVlanStorageName, sizeof (VLAN_CONFIGURATION), (UINT8 *) Configuration, NULL); + FreePool (Configuration); + return EFI_SUCCESS; +} + + +/** + This function update VLAN list in the VLAN configuration Form. + + @param[in, out] PrivateData Points to VLAN configuration private data. + +**/ +VOID +VlanUpdateForm ( + IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData + ) +{ + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + UINT16 NumberOfVlan; + UINTN Index; + EFI_VLAN_FIND_DATA *VlanData; + VOID *StartOpCodeHandle; + EFI_IFR_GUID_LABEL *StartLabel; + VOID *EndOpCodeHandle; + EFI_IFR_GUID_LABEL *EndLabel; + CHAR16 *String; + CHAR16 VlanStr[30]; + CHAR16 VlanIdStr[6]; + UINTN DigitalCount; + EFI_STRING_ID StringId; + + // + // Find current VLAN configuration + // + VlanData = NULL; + NumberOfVlan = 0; + VlanConfig = PrivateData->VlanConfig; + VlanConfig->Find (VlanConfig, NULL, &NumberOfVlan, &VlanData); + + // + // Update VLAN configuration in PrivateData + // + if (NumberOfVlan > MAX_VLAN_NUMBER) { + NumberOfVlan = MAX_VLAN_NUMBER; + } + PrivateData->NumberOfVlan = NumberOfVlan; + + // + // Init OpCode Handle + // + StartOpCodeHandle = HiiAllocateOpCodeHandle (); + ASSERT (StartOpCodeHandle != NULL); + + EndOpCodeHandle = HiiAllocateOpCodeHandle (); + ASSERT (EndOpCodeHandle != NULL); + + // + // Create Hii Extend Label OpCode as the start opcode + // + StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + StartOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + StartLabel->Number = LABEL_VLAN_LIST; + + // + // Create Hii Extend Label OpCode as the end opcode + // + EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + EndOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + EndLabel->Number = LABEL_END; + + ZeroMem (PrivateData->VlanId, MAX_VLAN_NUMBER); + for (Index = 0; Index < NumberOfVlan; Index++) { + String = VlanStr; + + StrCpyS (String, (sizeof (VlanStr) /sizeof (CHAR16)), L" VLAN ID:"); + String += 10; + // + // Pad VlanId string up to 4 characters with space + // + UnicodeValueToStringS (VlanIdStr, sizeof (VlanIdStr), 0, VlanData[Index].VlanId, 5); + DigitalCount = StrnLenS (VlanIdStr, ARRAY_SIZE (VlanIdStr)); + SetMem16 (String, (4 - DigitalCount) * sizeof (CHAR16), L' '); + StrCpyS (String + 4 - DigitalCount, (sizeof (VlanStr) /sizeof (CHAR16)) - 10 - (4 - DigitalCount), VlanIdStr); + String += 4; + + StrCpyS (String, (sizeof (VlanStr) /sizeof (CHAR16)) - 10 - (4 - DigitalCount) - 4, L", Priority:"); + String += 11; + UnicodeValueToStringS ( + String, + sizeof (VlanStr) - ((UINTN)String - (UINTN)VlanStr), + 0, + VlanData[Index].Priority, + 4 + ); + String += StrnLenS (String, ARRAY_SIZE (VlanStr) - ((UINTN)String - (UINTN)VlanStr) / sizeof (CHAR16)); + *String = 0; + + StringId = HiiSetString (PrivateData->HiiHandle, 0, VlanStr, NULL); + ASSERT (StringId != 0); + + HiiCreateCheckBoxOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (VLAN_LIST_VAR_OFFSET + Index), + VLAN_CONFIGURATION_VARSTORE_ID, + (UINT16) (VLAN_LIST_VAR_OFFSET + Index), + StringId, + STRING_TOKEN (STR_VLAN_VLAN_LIST_HELP), + 0, + 0, + NULL + ); + + // + // Save VLAN id to private data + // + PrivateData->VlanId[Index] = VlanData[Index].VlanId; + } + + HiiUpdateForm ( + PrivateData->HiiHandle, // HII handle + &gVlanConfigFormSetGuid, // Formset GUID + VLAN_CONFIGURATION_FORM_ID, // Form ID + StartOpCodeHandle, // Label for where to insert opcodes + EndOpCodeHandle // Replace data + ); + + HiiFreeOpCodeHandle (StartOpCodeHandle); + HiiFreeOpCodeHandle (EndOpCodeHandle); + + if (VlanData != NULL) { + FreePool (VlanData); + } +} + + +/** + This function publish the VLAN configuration Form for a network device. The + HII Config Access protocol will be installed on a child handle of the network + device. + + @param[in, out] PrivateData Points to VLAN configuration private data. + + @retval EFI_SUCCESS HII Form is installed for this network device. + @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +InstallVlanConfigForm ( + IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData + ) +{ + EFI_STATUS Status; + EFI_HII_HANDLE HiiHandle; + EFI_HANDLE DriverHandle; + CHAR16 Str[26 + sizeof (EFI_MAC_ADDRESS) * 2 + 1]; + CHAR16 *MacString; + EFI_DEVICE_PATH_PROTOCOL *ChildDevicePath; + EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + + // + // Create child handle and install HII Config Access Protocol + // + ChildDevicePath = AppendDevicePathNode ( + PrivateData->ParentDevicePath, + (CONST EFI_DEVICE_PATH_PROTOCOL *) &mHiiVendorDevicePathNode + ); + if (ChildDevicePath == NULL) { + return EFI_OUT_OF_RESOURCES; + } + PrivateData->ChildDevicePath = ChildDevicePath; + + DriverHandle = NULL; + ConfigAccess = &PrivateData->ConfigAccess; + Status = gBS->InstallMultipleProtocolInterfaces ( + &DriverHandle, + &gEfiDevicePathProtocolGuid, + ChildDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + ConfigAccess, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + PrivateData->DriverHandle = DriverHandle; + + // + // Establish the parent-child relationship between the new created + // child handle and the ControllerHandle. + // + Status = gBS->OpenProtocol ( + PrivateData->ControllerHandle, + &gEfiVlanConfigProtocolGuid, + (VOID **)&VlanConfig, + PrivateData->ImageHandle, + PrivateData->DriverHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Publish the HII package list + // + HiiHandle = HiiAddPackages ( + &gVlanConfigFormSetGuid, + DriverHandle, + VlanConfigDxeStrings, + VlanConfigBin, + NULL + ); + if (HiiHandle == NULL) { + return EFI_OUT_OF_RESOURCES; + } + PrivateData->HiiHandle = HiiHandle; + + // + // Update formset title help string. + // + MacString = NULL; + Status = NetLibGetMacString (PrivateData->ControllerHandle, PrivateData->ImageHandle, &MacString); + if (EFI_ERROR (Status)) { + return Status; + } + PrivateData->MacString = MacString; + + StrCpyS (Str, sizeof (Str) / sizeof (CHAR16), L"VLAN Configuration (MAC:"); + StrCatS (Str, sizeof (Str) / sizeof (CHAR16), MacString); + StrCatS (Str, sizeof (Str) / sizeof (CHAR16), L")"); + HiiSetString ( + HiiHandle, + STRING_TOKEN (STR_VLAN_FORM_SET_TITLE_HELP), + Str, + NULL + ); + + // + // Update form title help string. + // + HiiSetString ( + HiiHandle, + STRING_TOKEN (STR_VLAN_FORM_HELP), + Str, + NULL + ); + + return EFI_SUCCESS; +} + +/** + This function remove the VLAN configuration Form for a network device. The + child handle for HII Config Access protocol will be destroyed. + + @param[in, out] PrivateData Points to VLAN configuration private data. + + @retval EFI_SUCCESS HII Form has been uninstalled successfully. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +UninstallVlanConfigForm ( + IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData + ) +{ + EFI_STATUS Status; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + + // + // End the parent-child relationship. + // + Status = gBS->CloseProtocol ( + PrivateData->ControllerHandle, + &gEfiVlanConfigProtocolGuid, + PrivateData->ImageHandle, + PrivateData->DriverHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Uninstall HII Config Access Protocol + // + if (PrivateData->DriverHandle != NULL) { + Status = gBS->UninstallMultipleProtocolInterfaces ( + PrivateData->DriverHandle, + &gEfiDevicePathProtocolGuid, + PrivateData->ChildDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &PrivateData->ConfigAccess, + NULL + ); + if (EFI_ERROR (Status)) { + gBS->OpenProtocol ( + PrivateData->ControllerHandle, + &gEfiVlanConfigProtocolGuid, + (VOID **)&VlanConfig, + PrivateData->ImageHandle, + PrivateData->DriverHandle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + return Status; + } + PrivateData->DriverHandle = NULL; + + if (PrivateData->ChildDevicePath != NULL) { + FreePool (PrivateData->ChildDevicePath); + PrivateData->ChildDevicePath = NULL; + } + } + + // + // Free MAC string + // + if (PrivateData->MacString != NULL) { + FreePool (PrivateData->MacString); + PrivateData->MacString = NULL; + } + + // + // Uninstall HII package list + // + if (PrivateData->HiiHandle != NULL) { + HiiRemovePackages (PrivateData->HiiHandle); + PrivateData->HiiHandle = NULL; + } + return EFI_SUCCESS; +} diff --git a/NetworkPkg/VlanConfigDxe/VlanConfigImpl.h b/NetworkPkg/VlanConfigDxe/VlanConfigImpl.h new file mode 100644 index 000000000..14f99c03b --- /dev/null +++ b/NetworkPkg/VlanConfigDxe/VlanConfigImpl.h @@ -0,0 +1,381 @@ +/** @file + Header file for driver binding protocol and HII config access protocol. + +Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __VLAN_CONFIG_IMPL_H__ +#define __VLAN_CONFIG_IMPL_H__ + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "VlanConfigNvData.h" + +extern EFI_COMPONENT_NAME2_PROTOCOL gVlanConfigComponentName2; +extern EFI_COMPONENT_NAME_PROTOCOL gVlanConfigComponentName; + +// +// Tool generated IFR binary data and String package data +// +extern UINT8 VlanConfigBin[]; +extern UINT8 VlanConfigDxeStrings[]; + +#define VLAN_LIST_VAR_OFFSET ((UINT16) OFFSET_OF (VLAN_CONFIGURATION, VlanList)) + +typedef struct { + UINTN Signature; + + EFI_HII_CONFIG_ACCESS_PROTOCOL ConfigAccess; + EFI_HII_HANDLE HiiHandle; + EFI_HANDLE DriverHandle; + EFI_DEVICE_PATH_PROTOCOL *ChildDevicePath; + + EFI_HANDLE ControllerHandle; + EFI_HANDLE ImageHandle; + EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; + EFI_VLAN_CONFIG_PROTOCOL *VlanConfig; + CHAR16 *MacString; + + UINT16 NumberOfVlan; + UINT16 VlanId[MAX_VLAN_NUMBER]; +} VLAN_CONFIG_PRIVATE_DATA; + +#define VLAN_CONFIG_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('V', 'C', 'P', 'D') +#define VLAN_CONFIG_PRIVATE_DATA_FROM_THIS(a) CR (a, VLAN_CONFIG_PRIVATE_DATA, ConfigAccess, VLAN_CONFIG_PRIVATE_DATA_SIGNATURE) + +extern VLAN_CONFIG_PRIVATE_DATA mVlanConfigPrivateDateTemplate; + + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + @param DriverName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +VlanConfigComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + @param ControllerHandle[in] The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + @param ChildHandle[in] The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + @param ControllerName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +VlanConfigComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +/** + Test to see if this driver supports ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCES This driver supports this device + @retval EFI_ALREADY_STARTED This driver is already running on this device + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +VlanConfigDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to + @param[in] RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCES This driver is added to ControllerHandle + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +VlanConfigDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number + of children is zero stop the entire bus driver. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCES This driver is removed ControllerHandle + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +VlanConfigDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +/** + This function update VLAN list in the VLAN configuration Form. + + @param[in, out] PrivateData Points to VLAN configuration private data. + +**/ +VOID +VlanUpdateForm ( + IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData + ); + +/** + This function publish the VLAN configuration Form for a network device. The + HII Config Access protocol will be installed on a child handle of the network + device. + + @param[in, out] PrivateData Points to VLAN configuration private data. + + @retval EFI_SUCCESS HII Form is installed for this network device. + @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +InstallVlanConfigForm ( + IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData + ); + +/** + This function remove the VLAN configuration Form for a network device. The + child handle for HII Config Access protocol will be destroyed. + + @param[in, out] PrivateData Points to VLAN configuration private data. + + @retval EFI_SUCCESS HII Form has been uninstalled successfully. + @retval Others Other errors as indicated. + +**/ +EFI_STATUS +UninstallVlanConfigForm ( + IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData + ); + +/** + This function allows a caller to extract the current configuration for one + or more named elements from the target driver. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Request A null-terminated Unicode string in + format. + @param[out] Progress On return, points to a character in the Request + string. Points to the string's null terminator if + request was successful. Points to the most recent + '&' before the first failing name/value pair (or + the beginning of the string if the failure is in + the first name/value pair) if the request was not + successful. + @param[out] Results A null-terminated Unicode string in + format which has all values filled + in for the names in the Request string. String to + be allocated by the called function. + + @retval EFI_SUCCESS The Results is filled with the requested values. + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results. + @retval EFI_INVALID_PARAMETER Request is NULL, illegal syntax, or unknown name. + @retval EFI_NOT_FOUND Routing data doesn't match any storage in this + driver. + +**/ +EFI_STATUS +EFIAPI +VlanExtractConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Request, + OUT EFI_STRING *Progress, + OUT EFI_STRING *Results + ); + +/** + This function processes the results of changes in configuration. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Configuration A null-terminated Unicode string in + format. + @param[out] Progress A pointer to a string filled in with the offset of + the most recent '&' before the first failing + name/value pair (or the beginning of the string if + the failure is in the first name/value pair) or + the terminating NULL if all was successful. + + @retval EFI_SUCCESS The Results is processed successfully. + @retval EFI_INVALID_PARAMETER Configuration is NULL. + @retval EFI_NOT_FOUND Routing data doesn't match any storage in this + driver. + +**/ +EFI_STATUS +EFIAPI +VlanRouteConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Configuration, + OUT EFI_STRING *Progress + ); + +/** + This function processes the results of changes in configuration. + + @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param[in] Action Specifies the type of action taken by the browser. + @param[in] QuestionId A unique value which is sent to the original + exporting driver so that it can identify the type + of data to expect. + @param[in] Type The type of value for the question. + @param[in] Value A pointer to the data being sent to the original + exporting driver. + @param[out] ActionRequest On return, points to the action requested by the + callback function. + + @retval EFI_SUCCESS The callback successfully handled the action. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the + variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be saved. + @retval EFI_UNSUPPORTED The specified Action is not supported by the + callback. + +**/ +EFI_STATUS +EFIAPI +VlanCallback ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN EFI_BROWSER_ACTION Action, + IN EFI_QUESTION_ID QuestionId, + IN UINT8 Type, + IN EFI_IFR_TYPE_VALUE *Value, + OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest + ); + +#endif diff --git a/NetworkPkg/VlanConfigDxe/VlanConfigNvData.h b/NetworkPkg/VlanConfigDxe/VlanConfigNvData.h new file mode 100644 index 000000000..00b548690 --- /dev/null +++ b/NetworkPkg/VlanConfigDxe/VlanConfigNvData.h @@ -0,0 +1,40 @@ +/** @file + Header file for NV data structure definition. + +Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __VLAN_CONFIG_NV_DATA_H__ +#define __VLAN_CONFIG_NV_DATA_H__ + +#include +#include + +#define VLAN_CONFIGURATION_VARSTORE_ID 0x0001 +#define VLAN_CONFIGURATION_FORM_ID 0x0001 +#define VLAN_HEAD_FORM_ID 0x0002 + +#define VLAN_ADD_QUESTION_ID 0x1000 +#define VLAN_REMOVE_QUESTION_ID 0x2000 +#define VLAN_UPDATE_QUESTION_ID 0x3000 + +#define LABEL_VLAN_LIST 0x0001 +#define LABEL_END 0xffff + +// +// The maximum number of VLAN that will be displayed on the menu +// +#define MAX_VLAN_NUMBER 100 + +// +// Nv Data structure referenced by IFR +// +typedef struct { + UINT16 VlanId; + UINT8 Priority; + UINT8 VlanList[MAX_VLAN_NUMBER]; +} VLAN_CONFIGURATION; + +#endif diff --git a/NetworkPkg/VlanConfigDxe/VlanConfigStrings.uni b/NetworkPkg/VlanConfigDxe/VlanConfigStrings.uni new file mode 100644 index 000000000..1ccfd6001 --- /dev/null +++ b/NetworkPkg/VlanConfigDxe/VlanConfigStrings.uni @@ -0,0 +1,31 @@ +///** @file +// String definitions for VLAN Configuration Form. +// +// Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.
+// SPDX-License-Identifier: BSD-2-Clause-Patent +// +//**/ + +/=# + +#langdef en-US "English" + +#string STR_VLAN_FORM_SET_TITLE #language en-US "VLAN Configuration" +#string STR_VLAN_FORM_SET_TITLE_HELP #language en-US "VLAN configuration for this network device" +#string STR_VLAN_FORM_TITLE #language en-US "VLAN Configuration" +#string STR_VLAN_FORM_HELP #language en-US "VLAN Configuration Help" +#string STR_VLAN_CREATE_VLAN #language en-US "Create new VLAN" + +#string STR_VLAN_VID_PROMPT #language en-US " VLAN ID" +#string STR_VLAN_VID_HELP #language en-US "VLAN ID of new VLAN or existing VLAN, valid value is 0~4094" +#string STR_VLAN_PRIORITY_PROMPT #language en-US " Priority" +#string STR_VLAN_PRIORITY_HELP #language en-US "802.1Q Priority, valid value is 0~7" +#string STR_VLAN_ADD_VLAN_PROMPT #language en-US "Add VLAN" +#string STR_VLAN_ADD_VLAN_HELP #language en-US "Create a new VLAN or update existing VLAN" +#string STR_VLAN_VLAN_LIST #language en-US "Configured VLAN List" +#string STR_VLAN_VLAN_LIST_HELP #language en-US "Select for remove" +#string STR_VLAN_REMOVE_VLAN_PROMPT #language en-US "Remove VLAN" +#string STR_VLAN_REMOVE_VLAN_HELP #language en-US "Remove selected VLANs" +#string STR_VLAN_NULL_STRING #language en-US "" +#string STR_GET_CURRENT_SETTING #language en-US "Enter Configuration Menu" +#string STR_GET_CURRENT_SETTING_HELP #language en-US "Press ENTER to enter configuration menu for VLAN configuration." diff --git a/NetworkPkg/WifiConnectionManagerDxe/EapContext.h b/NetworkPkg/WifiConnectionManagerDxe/EapContext.h new file mode 100644 index 000000000..9b3801926 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/EapContext.h @@ -0,0 +1,22 @@ +/** @file + Eap configuration data structure definitions for EAP connections. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_WIFI_EAP_CONTEXT_H__ +#define __EFI_WIFI_EAP_CONTEXT_H__ + +typedef struct { + + BOOLEAN IsEncrypted; + CHAR16 EncryptPassword[PASSWORD_STORAGE_SIZE]; + UINTN KeySize; + UINT8 KeyData[1]; + +} EFI_EAP_PRIVATE_KEY; + +#endif diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxe.inf b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxe.inf new file mode 100644 index 000000000..4394b6f4b --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxe.inf @@ -0,0 +1,81 @@ +## @file +# WiFi Connection Manager. +# +# This module is an example of how to make use of UEFI WiFi connection capabilities. +# User can scan, connect and diconnect to networks through UI operations. +# +# Supported networks include: +# 1). Open Network +# 2). WPA2 Personal Network +# 3). EAP Networks (EAP-TLS, EAP-TTLS/MSCHAPv2 and PEAPv0/MSCHAPv2) +# +# Copyright (c) 2019, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = WifiConnectionManagerDxe + FILE_GUID = c6df98f2-5ec0-4a94-8c11-9a9828ef03f2 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 0.1 + ENTRY_POINT = WifiMgrDxeDriverEntryPoint + +[Sources] + WifiConnectionMgrDxe.h + WifiConnectionMgrDriverBinding.h + WifiConnectionMgrConfig.h + WifiConnectionMgrMisc.h + WifiConnectionMgrImpl.h + WifiConnectionMgrConfigNVDataStruct.h + WifiConnectionMgrHiiConfigAccess.h + WifiConnectionMgrComponentName.h + WifiConnectionMgrFileUtil.h + WifiConnectionMgrDriver.c + WifiConnectionMgrComponentName.c + WifiConnectionMgrMisc.c + WifiConnectionMgrHiiConfigAccess.c + WifiConnectionMgrImpl.c + WifiConnectionMgrFileUtil.c + WifiConnectionManagerDxeStrings.uni + WifiConnectionManagerDxe.vfr + EapContext.h + WifiConnectionMgrConfigHii.h + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec + +[LibraryClasses] + UefiDriverEntryPoint + UefiBootServicesTableLib + UefiRuntimeServicesTableLib + MemoryAllocationLib + BaseMemoryLib + BaseLib + UefiLib + DevicePathLib + DebugLib + HiiLib + PrintLib + UefiHiiServicesLib + NetLib + FileExplorerLib + +[Protocols] + gEfiHiiConfigAccessProtocolGuid ## PRODUCES + gEfiWiFi2ProtocolGuid ## TO_START + gEfiAdapterInformationProtocolGuid ## SOMETIMES_CONSUMES + gEfiSupplicantProtocolGuid ## SOMETIMES_CONSUMES + gEfiEapConfigurationProtocolGuid ## SOMETIMES_CONSUMES + +[Guids] + gWifiConfigGuid ## PRODUCES ## GUID + gEfiIfrTianoGuid ## CONSUMES ## GUID (Extended IFR Guid Opcode) + gEfiAdapterInfoMediaStateGuid ## SOMETIMES_CONSUMES ## GUID # Indicate the current media state status + +[Depex] + gEfiHiiConfigRoutingProtocolGuid diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxe.vfr b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxe.vfr new file mode 100644 index 000000000..b0ef18753 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxe.vfr @@ -0,0 +1,347 @@ +/** @file + Vfr files used in WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "WifiConnectionMgrConfigNVDataStruct.h" + +#define EFI_NETWORK_DEVICE_CLASS 0x04 + +formset + guid = WIFI_CONNECTION_MANAGER_CONFIG_GUID, + title = STRING_TOKEN(STR_WIFI_MGR_FORM_TITLE), + help = STRING_TOKEN(STR_WIFI_MGR_FORM_HELP), + class = EFI_NETWORK_DEVICE_CLASS, + subclass = 0x03, + + varstore WIFI_MANAGER_IFR_NVDATA, + varid = MANAGER_VARSTORE_ID, + name = WIFI_MANAGER_IFR_NVDATA, + guid = WIFI_CONNECTION_MANAGER_CONFIG_GUID; + + form formid = FORMID_MAC_SELECTION, + title = STRING_TOKEN(STR_WIFI_MAC_FORM_TITLE); + + suppressif TRUE; + text + help = STRING_TOKEN(STR_NULL_STRING), + text = STRING_TOKEN(STR_NULL_STRING), + flags = INTERACTIVE, + key = KEY_MAC_LIST; + endif; + + label LABEL_MAC_ENTRY; + label LABEL_END; + endform; + + form formid = FORMID_WIFI_MAINPAGE, + title = STRING_TOKEN(STR_NETWORK_MANAGEMENT_TITLE); + + text + help = STRING_TOKEN(STR_MAC_ADDRESS_HELP), // Help string + text = STRING_TOKEN(STR_MAC_ADDRESS_TITLE), // Prompt string + text = STRING_TOKEN(STR_MAC_ADDRESS); // TextTwo + + text + help = STRING_TOKEN(STR_NULL_STRING), // Help string + text = STRING_TOKEN(STR_CONNECTION_INFO), // Prompt string + text = STRING_TOKEN(STR_CONNECTED_SSID); // TextTwo; + + subtitle text = STRING_TOKEN(STR_NULL_STRING); + subtitle text = STRING_TOKEN(STR_NULL_STRING); + + goto FORMID_NETWORK_LIST, + prompt = STRING_TOKEN(STR_NETWORK_LIST), + help = STRING_TOKEN(STR_NETWORK_LIST_HELP), + flags = INTERACTIVE, + key = KEY_NETWORK_LIST; + + goto FORMID_WIFI_SETTINGS, + prompt = STRING_TOKEN(STR_WIFI_SETTINGS), + help = STRING_TOKEN(STR_WIFI_SETTINGS_HELP), + flags = INTERACTIVE, + key = KEY_WIFI_SETTINGS; + + action + questionid = KEY_REFRESH_TITLE_CONNECTION_STATUS, + prompt = STRING_TOKEN(STR_NULL_STRING), + help = STRING_TOKEN(STR_NULL_STRING), + flags = INTERACTIVE, + config = STRING_TOKEN(STR_NULL_STRING), + refreshguid = WIFI_CONFIG_MAIN_FORM_REFRESH_GUID, + endaction; + + endform; + + form formid = FORMID_NETWORK_LIST, + title = STRING_TOKEN(STR_NETWORK_LIST); + + numeric varid = WIFI_MANAGER_IFR_NVDATA.ProfileCount, + prompt = STRING_TOKEN(STR_REFRESH_NETWORK_COUNT), + help = STRING_TOKEN(STR_REFRESH_NETWORK_COUNT_HELP), + flags = INTERACTIVE | READ_ONLY, + key = KEY_REFRESH_NETWORK_LIST, + minimum = 0, + maximum = 0xffffffff, + step = 0, + default = 0, + refreshguid = WIFI_CONFIG_NETWORK_LIST_REFRESH_GUID, + endnumeric; + + subtitle text = STRING_TOKEN(STR_NULL_STRING); + + label LABEL_NETWORK_LIST_ENTRY; + label LABEL_END; + endform; + + form formid = FORMID_CONNECT_NETWORK, + title = STRING_TOKEN(STR_NETWORK_CONFIGURATION); + + subtitle text = STRING_TOKEN(STR_NULL_STRING); + + text + help = STRING_TOKEN(STR_CONNECT_STATUS_TITLE_HELP), // Help string + text = STRING_TOKEN(STR_CONNECT_STATUS_TITLE), // Prompt string + text = STRING_TOKEN(STR_CONNECT_STATUS); // TextTwo + + subtitle text = STRING_TOKEN(STR_NULL_STRING); + + text + help = STRING_TOKEN(STR_SSID_HELP), // Help string + text = STRING_TOKEN(STR_SSID_TITLE), // Prompt string + text = STRING_TOKEN(STR_SSID); // TextTwo + + text + help = STRING_TOKEN(STR_SECURITY_TYPE_HELP), // Help string + text = STRING_TOKEN(STR_SECURITY_TYPE_TITLE), // Prompt string + text = STRING_TOKEN(STR_SECURITY_TYPE); // TextTwo + + + suppressif NOT ideqval WIFI_MANAGER_IFR_NVDATA.SecurityType == SECURITY_TYPE_WPA2_PERSONAL; + password varid = WIFI_MANAGER_IFR_NVDATA.Password, + prompt = STRING_TOKEN(STR_PASSWORD), + help = STRING_TOKEN(STR_PASSWORD_HELP), + flags = INTERACTIVE, + key = KEY_PASSWORD_CONNECT_NETWORK, + minsize = PASSWORD_MIN_LEN, + maxsize = PASSWORD_MAX_LEN, + endpassword; + endif; + + suppressif NOT ideqval WIFI_MANAGER_IFR_NVDATA.SecurityType == SECURITY_TYPE_WPA2_ENTERPRISE; + + oneof varid = WIFI_MANAGER_IFR_NVDATA.EapAuthMethod, + questionid = KEY_EAP_AUTH_METHOD_CONNECT_NETWORK, + prompt = STRING_TOKEN(STR_EAP_AUTH_METHOD), + help = STRING_TOKEN(STR_EAP_AUTH_METHOD_HELP), + flags = INTERACTIVE, + option text = STRING_TOKEN(STR_EAP_AUTH_METHOD_TTLS), value = EAP_AUTH_METHOD_TTLS, flags = DEFAULT; + option text = STRING_TOKEN(STR_EAP_AUTH_METHOD_PEAP), value = EAP_AUTH_METHOD_PEAP, flags = 0; + option text = STRING_TOKEN(STR_EAP_AUTH_METHOD_TLS), value = EAP_AUTH_METHOD_TLS, flags = 0; + endoneof; + + suppressif NOT ideqvallist WIFI_MANAGER_IFR_NVDATA.EapAuthMethod == EAP_AUTH_METHOD_TLS + EAP_AUTH_METHOD_TTLS + EAP_AUTH_METHOD_PEAP; + + goto FORMID_ENROLL_CERT, + prompt = STRING_TOKEN(STR_EAP_ENROLL_CA_CERT), + help = STRING_TOKEN(STR_EAP_ENROLL_CA_CERT_HELP), + flags = INTERACTIVE, + key = KEY_ENROLL_CA_CERT_CONNECT_NETWORK; + + suppressif NOT ideqval WIFI_MANAGER_IFR_NVDATA.EapAuthMethod == EAP_AUTH_METHOD_TLS; + + goto FORMID_ENROLL_CERT, + prompt = STRING_TOKEN(STR_EAP_ENROLL_CLIENT_CERT), + help = STRING_TOKEN(STR_EAP_ENROLL_CLIENT_CERT_HELP), + flags = INTERACTIVE, + key = KEY_ENROLL_CLIENT_CERT_CONNECT_NETWORK; + + goto FORMID_ENROLL_PRIVATE_KEY, + prompt = STRING_TOKEN(STR_EAP_ENROLL_CLIENT_KEY), + help = STRING_TOKEN(STR_EAP_ENROLL_CLIENT_KEY_HELP), + flags = INTERACTIVE, + key = KEY_ENROLL_PRIVATE_KEY_CONNECT_NETWORK; + + endif; + + suppressif NOT ideqvallist WIFI_MANAGER_IFR_NVDATA.EapAuthMethod == EAP_AUTH_METHOD_TTLS + EAP_AUTH_METHOD_PEAP; + + oneof varid = WIFI_MANAGER_IFR_NVDATA.EapSecondAuthMethod, + questionid = KEY_EAP_SEAUTH_METHOD_CONNECT_NETWORK, + prompt = STRING_TOKEN(STR_EAP_SEAUTH_METHOD), + help = STRING_TOKEN(STR_EAP_SEAUTH_METHOD_HELP), + flags = INTERACTIVE, + option text = STRING_TOKEN(STR_EAP_SEAUTH_METHOD_MSCHAPV2), value = EAP_SEAUTH_METHOD_MSCHAPV2, flags = DEFAULT; + endoneof; + endif; + + string varid = WIFI_MANAGER_IFR_NVDATA.EapIdentity, + prompt = STRING_TOKEN(STR_EAP_IDENTITY), + help = STRING_TOKEN(STR_EAP_IDENTITY_HELP), + flags = INTERACTIVE, + key = KEY_EAP_IDENTITY_CONNECT_NETWORK, + minsize = 6, + maxsize = EAP_IDENTITY_LEN, + endstring; + + suppressif NOT ideqvallist WIFI_MANAGER_IFR_NVDATA.EapAuthMethod == EAP_AUTH_METHOD_TTLS + EAP_AUTH_METHOD_PEAP; + + password varid = WIFI_MANAGER_IFR_NVDATA.EapPassword, + prompt = STRING_TOKEN(STR_EAP_PASSWORD), + help = STRING_TOKEN(STR_EAP_PASSWORD_HELP), + flags = INTERACTIVE, + key = KEY_EAP_PASSWORD_CONNECT_NETWORK, + minsize = 0, + maxsize = PASSWORD_MAX_LEN, + endpassword; + endif; + endif; + endif; + + subtitle text = STRING_TOKEN(STR_NULL_STRING); + + text + help = STRING_TOKEN(STR_CONNECT_NOW_HELP), + text = STRING_TOKEN(STR_CONNECT_NOW), + flags = INTERACTIVE, + key = KEY_CONNECT_ACTION; + + action + questionid = KEY_REFRESH_CONNECT_CONFIGURATION, + prompt = STRING_TOKEN(STR_NULL_STRING), + help = STRING_TOKEN(STR_NULL_STRING), + flags = INTERACTIVE, + config = STRING_TOKEN(STR_NULL_STRING), + refreshguid = WIFI_CONFIG_CONNECT_FORM_REFRESH_GUID, + endaction; + + endform; + + form formid = FORMID_ENROLL_CERT, + title = STRING_TOKEN(STR_EAP_ENROLL_CERT); + + goto FORMID_ENROLL_CERT, + prompt = STRING_TOKEN(STR_EAP_ENROLL_CERT_FROM_FILE), + help = STRING_TOKEN(STR_EAP_ENROLL_CERT_FROM_FILE_HELP), + flags = INTERACTIVE, + key = KEY_EAP_ENROLL_CERT_FROM_FILE; + + text + help = STRING_TOKEN(STR_NULL_STRING), + text = STRING_TOKEN(STR_EAP_ENROLLED_CERT_NAME), + flags = INTERACTIVE, + key = KEY_ENROLLED_CERT_NAME; + + subtitle text = STRING_TOKEN(STR_NULL_STRING); + + text + help = STRING_TOKEN(STR_SAVE_EXIT_HELP), + text = STRING_TOKEN(STR_SAVE_EXIT), + flags = INTERACTIVE, + key = KEY_SAVE_CERT_TO_MEM; + + text + help = STRING_TOKEN(STR_NO_SAVE_EXIT_HELP), + text = STRING_TOKEN(STR_NO_SAVE_EXIT), + flags = INTERACTIVE, + key = KEY_NO_SAVE_CERT_TO_MEM; + + endform; + + form formid = FORMID_ENROLL_PRIVATE_KEY, + title = STRING_TOKEN(STR_EAP_ENROLL_CLIENT_KEY); + + goto FORMID_ENROLL_PRIVATE_KEY, + prompt = STRING_TOKEN(STR_EAP_ENROLL_KEY_FROM_FILE), + help = STRING_TOKEN(STR_EAP_ENROLL_KEY_FROM_FILE_HELP), + flags = INTERACTIVE, + key = KEY_EAP_ENROLL_PRIVATE_KEY_FROM_FILE; + + text + help = STRING_TOKEN(STR_NULL_STRING), + text = STRING_TOKEN(STR_EAP_ENROLLED_PRIVATE_KEY_NAME), + flags = INTERACTIVE, + key = KEY_ENROLLED_PRIVATE_KEY_NAME; + + subtitle text = STRING_TOKEN(STR_NULL_STRING); + + password varid = WIFI_MANAGER_IFR_NVDATA.PrivateKeyPassword, + prompt = STRING_TOKEN(STR_EAP_CLIENT_KEY_PASSWORD), + help = STRING_TOKEN(STR_NULL_STRING), + flags = INTERACTIVE, + key = KEY_PRIVATE_KEY_PASSWORD, + minsize = 0, + maxsize = PASSWORD_MAX_LEN, + endpassword; + + subtitle text = STRING_TOKEN(STR_NULL_STRING); + subtitle text = STRING_TOKEN(STR_NULL_STRING); + + text + help = STRING_TOKEN(STR_SAVE_EXIT_HELP), + text = STRING_TOKEN(STR_SAVE_EXIT), + flags = INTERACTIVE, + key = KEY_SAVE_PRIVATE_KEY_TO_MEM; + + text + help = STRING_TOKEN(STR_NO_SAVE_EXIT_HELP), + text = STRING_TOKEN(STR_NO_SAVE_EXIT), + flags = INTERACTIVE, + key = KEY_NO_SAVE_PRIVATE_KEY_TO_MEM; + + endform; + + form formid = FORMID_WIFI_SETTINGS, + title = STRING_TOKEN(STR_WIFI_SETTINGS_FORM_TITLE); + + subtitle text = STRING_TOKEN(STR_NULL_STRING); + + goto FORMID_HIDDEN_NETWORK_LIST, + prompt = STRING_TOKEN(STR_HIDDEN_NETWORK), + help = STRING_TOKEN(STR_HIDDEN_NETWORK_HELP), + flags = INTERACTIVE, + key = KEY_HIDDEN_NETWORK; + + endform; + + form formid = FORMID_HIDDEN_NETWORK_LIST, + title = STRING_TOKEN(STR_HIDDEN_NETWORK_FORM_TITLE); + + string + varid = WIFI_MANAGER_IFR_NVDATA.SSId, + prompt = STRING_TOKEN(STR_SSID_TITLE), + help = STRING_TOKEN(STR_SSID_HELP), + flags = INTERACTIVE, + minsize = SSID_MIN_LEN, + maxsize = SSID_MAX_LEN, + endstring; + + text + help = STRING_TOKEN(STR_ADD_HIDDEN_NETWORK_HELP), + text = STRING_TOKEN(STR_ADD_HIDDEN_NETWORK), + flags = INTERACTIVE, + key = KEY_ADD_HIDDEN_NETWORK; + + subtitle text = STRING_TOKEN(STR_NULL_STRING); + subtitle text = STRING_TOKEN(STR_HIDDEN_NETWORK_LIST); + + label LABEL_HIDDEN_NETWORK_ENTRY; + label LABEL_END; + + text + help = STRING_TOKEN(STR_REMOVE_HIDDEN_NETWORK_HELP), + text = STRING_TOKEN(STR_REMOVE_HIDDEN_NETWORK), + flags = INTERACTIVE, + key = KEY_REMOVE_HIDDEN_NETWORK; + + endform; + +endformset; diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxeStrings.uni b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxeStrings.uni new file mode 100644 index 000000000..3f2b068fb --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionManagerDxeStrings.uni @@ -0,0 +1,103 @@ +// /** @file +// String definitions for WiFi Connection Manager Forms. +// +// Copyright (c) 2019, Intel Corporation. All rights reserved.
+// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#langdef en-US "English" + +#string STR_WIFI_MGR_FORM_TITLE #language en-US "Wi-Fi Configuration" +#string STR_WIFI_MGR_FORM_HELP #language en-US "Configure the Wi-Fi parameters." + +#string STR_WIFI_MAC_FORM_TITLE #language en-US "MAC Selection" +#string STR_WIFI_MAC_FORM_HELP #language en-US "Select a Nic" + +#string STR_MAC_ADDRESS_TITLE #language en-US "MAC Address" +#string STR_MAC_ADDRESS #language en-US "88-88-88-88-88-87" +#string STR_MAC_ADDRESS_HELP #language en-US "MAC Address" +#string STR_CONNECTION_INFO #language en-US "Disconnected" +#string STR_CONNECTED_SSID #language en-US "" +#string STR_NULL_STRING #language en-US "" + +#string STR_NETWORK_MANAGEMENT_TITLE #language en-US "Wi-Fi Network Management" +#string STR_NETWORK_LIST #language en-US "Wi-Fi Network List" +#string STR_NETWORK_LIST_HELP #language en-US "Available Network List" +#string STR_NETWORK_CONFIGURATION #language en-US "Wi-Fi Network Configuration" + +#string STR_SSID_TITLE #language en-US "SSID" +#string STR_SSID #language en-US "" +#string STR_SSID_HELP #language en-US "SSID Length: 1 - 32 characters" +#string STR_SECURITY_TYPE_TITLE #language en-US "Security" +#string STR_SECURITY_TYPE #language en-US "" +#string STR_SECURITY_TYPE_HELP #language en-US "Network Security Type" + +#string STR_EAP_AUTH_METHOD #language en-US "EAP Authentication Method" +#string STR_EAP_AUTH_METHOD_HELP #language en-US "EAP Authentication Method" +#string STR_EAP_SEAUTH_METHOD #language en-US "EAP Second Authentication Method" +#string STR_EAP_SEAUTH_METHOD_HELP #language en-US "EAP Second Authentication Method" +#string STR_SECURITY_NONE #language en-US "Open" +#string STR_SECURITY_WEP #language en-US "WEP" +#string STR_SECURITY_WPA_PERSONAL #language en-US "WPA-Personal" +#string STR_SECURITY_WPA_ENTERPRISE #language en-US "WPA-Enterprise" +#string STR_SECURITY_WPA2_PERSONAL #language en-US "WPA2-Personal" +#string STR_SECURITY_WPA2_ENTERPRISE #language en-US "WPA2-Enterprise" +#string STR_SECURITY_UNKNOWN #language en-US "Unknown" +#string STR_EAP_AUTH_METHOD_TLS #language en-US "EAPTLS" +#string STR_EAP_AUTH_METHOD_TTLS #language en-US "TTLS" +#string STR_EAP_AUTH_METHOD_PEAP #language en-US "PEAP" +#string STR_EAP_SEAUTH_METHOD_MSCHAPV2 #language en-US "MSCHAPv2" +#string STR_EAP_SEAUTH_METHOD_GTC #language en-US "GTC" + +#string STR_PASSWORD #language en-US "Password" +#string STR_PASSWORD_HELP #language en-US "Password Length: 8 - 63 characters" +#string STR_CONNECT_STATUS_TITLE #language en-US "Connection Status:" +#string STR_CONNECT_STATUS_TITLE_HELP #language en-US "" +#string STR_CONNECT_STATUS #language en-US "" +#string STR_CONNECT_STATUS_HELP #language en-US "" + +#string STR_CONNECT_NOW #language en-US "Connect to network now" +#string STR_CONNECT_NOW_HELP #language en-US "" +#string STR_DISCONNECT_NOW #language en-US "Disconnect from this network" +#string STR_DISCONNECT_NOW_HELP #language en-US "" +#string STR_REFRESH_NETWORK_COUNT #language en-US "Number of Networks" +#string STR_REFRESH_NETWORK_COUNT_HELP #language en-US "The number of current available networks around" + +#string STR_EAP_IDENTITY #language en-US "Identity" +#string STR_EAP_IDENTITY_HELP #language en-US "It is used to query the identity of the peer." +#string STR_EAP_PASSWORD #language en-US "EAP Password" +#string STR_EAP_PASSWORD_HELP #language en-US "Password Length: 1 - 63 characters" + +#string STR_SAVE_EXIT #language en-US "Commit Changes and Exit" +#string STR_SAVE_EXIT_HELP #language en-US "" +#string STR_NO_SAVE_EXIT #language en-US "Discard Changes and Exit" +#string STR_NO_SAVE_EXIT_HELP #language en-US "" + +#string STR_EAP_ENROLL_CERT #language en-US "Enroll Certificate" +#string STR_EAP_ENROLL_CA_CERT #language en-US "Enroll CA Cert" +#string STR_EAP_ENROLL_CA_CERT_HELP #language en-US "" +#string STR_EAP_ENROLL_CLIENT_CERT #language en-US "Enroll Client Cert" +#string STR_EAP_ENROLL_CLIENT_CERT_HELP #language en-US "" +#string STR_EAP_ENROLL_CLIENT_KEY #language en-US "Enroll Client Private Key" +#string STR_EAP_ENROLL_CLIENT_KEY_HELP #language en-US "" +#string STR_EAP_ENROLL_CERT_FROM_FILE #language en-US "Enroll Cert Using File" +#string STR_EAP_ENROLL_CERT_FROM_FILE_HELP #language en-US "" +#string STR_EAP_ENROLL_KEY_FROM_FILE #language en-US "Enroll Private Key Using File" +#string STR_EAP_ENROLL_KEY_FROM_FILE_HELP #language en-US "" +#string STR_EAP_CLIENT_KEY_PASSWORD #language en-US "Client Private Key Password" +#string STR_EAP_ENROLLED_CERT_NAME #language en-US "" +#string STR_EAP_ENROLLED_PRIVATE_KEY_NAME #language en-US "" + +#string STR_WIFI_SETTINGS_FORM_TITLE #language en-US "Wi-Fi Settings" +#string STR_WIFI_SETTINGS #language en-US "Wi-Fi Settings" +#string STR_WIFI_SETTINGS_HELP #language en-US "" + +#string STR_HIDDEN_NETWORK_FORM_TITLE #language en-US "Hidden Network Configuration" +#string STR_HIDDEN_NETWORK #language en-US "Hidden Network Configuration" +#string STR_HIDDEN_NETWORK_HELP #language en-US "" +#string STR_ADD_HIDDEN_NETWORK_HELP #language en-US "Hidden Network List won't be saved in Storage, they will be cleaned after Reset!" +#string STR_ADD_HIDDEN_NETWORK #language en-US "Add Hidden Network" +#string STR_HIDDEN_NETWORK_LIST #language en-US "Hidden Network List" +#string STR_REMOVE_HIDDEN_NETWORK_HELP #language en-US "" +#string STR_REMOVE_HIDDEN_NETWORK #language en-US "Remove Hidden Network" \ No newline at end of file diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrComponentName.c b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrComponentName.c new file mode 100644 index 000000000..1bcd3daa2 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrComponentName.c @@ -0,0 +1,185 @@ +/** @file + UEFI Component Name(2) protocol implementation for WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "WifiConnectionMgrDxe.h" + +extern EFI_GUID mEfiWifiMgrPrivateGuid; + +/// +/// Component Name Protocol instance +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_COMPONENT_NAME_PROTOCOL gWifiMgrDxeComponentName = { + (EFI_COMPONENT_NAME_GET_DRIVER_NAME) WifiMgrDxeComponentNameGetDriverName, + (EFI_COMPONENT_NAME_GET_CONTROLLER_NAME) WifiMgrDxeComponentNameGetControllerName, + "eng" +}; + +/// +/// Component Name 2 Protocol instance +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_COMPONENT_NAME2_PROTOCOL gWifiMgrDxeComponentName2 = { + WifiMgrDxeComponentNameGetDriverName, + WifiMgrDxeComponentNameGetControllerName, + "en" +}; + +/// +/// Table of driver names +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_UNICODE_STRING_TABLE mWifiMgrDxeDriverNameTable[] = { + { + "eng;en", + L"UEFI WiFi Connection Manager" + }, + { + NULL, + NULL + } +}; + +/// +/// Table of controller names +/// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_UNICODE_STRING_TABLE mWifiMgrDxeControllerNameTable[] = { + { + "eng;en", + L"UEFI WiFi Connection Manager Controller" + }, + { + NULL, + NULL + } +}; + +/** + Retrieves a Unicode string that is the user-readable name of the EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param Language A pointer to a three-character ISO 639-2 language identifier. + This is the language of the driver name that that the caller + is requesting, and it must match one of the languages specified + in SupportedLanguages. The number of languages supported by a + driver is up to the driver writer. + @param DriverName A pointer to the Unicode string to return. This Unicode string + is the name of the driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by This + and the language specified by Language was returned + in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mWifiMgrDxeDriverNameTable, + DriverName, + (BOOLEAN)(This != &gWifiMgrDxeComponentName2) + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by an EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param ControllerHandle The handle of a controller that the driver specified by + This is managing. This handle specifies the controller + whose name is to be returned. + @param ChildHandle The handle of the child controller to retrieve the name + of. This is an optional parameter that may be NULL. It + will be NULL for device drivers. It will also be NULL + for a bus drivers that wish to retrieve the name of the + bus controller. It will not be NULL for a bus driver + that wishes to retrieve the name of a child controller. + @param Language A pointer to a three character ISO 639-2 language + identifier. This is the language of the controller name + that the caller is requesting, and it must match one + of the languages specified in SupportedLanguages. The + number of languages supported by a driver is up to the + driver writer. + @param ControllerName A pointer to the Unicode string to return. This Unicode + string is the name of the controller specified by + ControllerHandle and ChildHandle in the language specified + by Language, from the point of view of the driver specified + by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in the + language specified by Language for the driver + specified by This was returned in DriverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not currently managing + the controller specified by ControllerHandle and + ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + EFI_STATUS Status; + WIFI_MGR_PRIVATE_PROTOCOL *WifiMgrPrivate; + + // + // ChildHandle must be NULL for a Device Driver + // + if (ControllerHandle == NULL || ChildHandle != NULL) { + return EFI_UNSUPPORTED; + } + + // + // Check Controller's handle + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &mEfiWifiMgrPrivateGuid, + (VOID **) &WifiMgrPrivate, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mWifiMgrDxeControllerNameTable, + ControllerName, + (BOOLEAN)(This != &gWifiMgrDxeComponentName2) + ); +} diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrComponentName.h b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrComponentName.h new file mode 100644 index 000000000..c1865f8dc --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrComponentName.h @@ -0,0 +1,93 @@ +/** @file + UEFI Component Name(2) protocol implementation for WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_WIFI_COMPONENT_NAME__ +#define __EFI_WIFI_COMPONENT_NAME__ + +/** + Retrieves a Unicode string that is the user-readable name of the EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param Language A pointer to a three-character ISO 639-2 language identifier. + This is the language of the driver name that that the caller + is requesting, and it must match one of the languages specified + in SupportedLanguages. The number of languages supported by a + driver is up to the driver writer. + @param DriverName A pointer to the Unicode string to return. This Unicode string + is the name of the driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by This + and the language specified by Language was returned + in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by an EFI Driver. + + @param This A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance. + @param ControllerHandle The handle of a controller that the driver specified by + This is managing. This handle specifies the controller + whose name is to be returned. + @param ChildHandle The handle of the child controller to retrieve the name + of. This is an optional parameter that may be NULL. It + will be NULL for device drivers. It will also be NULL + for a bus drivers that wish to retrieve the name of the + bus controller. It will not be NULL for a bus driver + that wishes to retrieve the name of a child controller. + @param Language A pointer to a three character ISO 639-2 language + identifier. This is the language of the controller name + that the caller is requesting, and it must match one + of the languages specified in SupportedLanguages. The + number of languages supported by a driver is up to the + driver writer. + @param ControllerName A pointer to the Unicode string to return. This Unicode + string is the name of the controller specified by + ControllerHandle and ChildHandle in the language specified + by Language, from the point of view of the driver specified + by This. + + @retval EFI_SUCCESS The Unicode string for the user-readable name in the + language specified by Language for the driver + specified by This was returned in DriverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not currently managing + the controller specified by ControllerHandle and + ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does not support the + language specified by Language. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +#endif diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfig.h b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfig.h new file mode 100644 index 000000000..c0b440fd2 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfig.h @@ -0,0 +1,68 @@ +/** @file + Define network structure used by the WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _WIFI_MGR_CONFIG_H_ +#define _WIFI_MGR_CONFIG_H_ + +#include "WifiConnectionMgrConfigNVDataStruct.h" + +extern UINT8 WifiConnectionManagerDxeBin[]; +extern UINT8 WifiConnectionManagerDxeStrings[]; + +typedef struct { + UINT32 Signature; + + // + // Link to the current profile list in NIC device data (WIFI_MGR_DEVICE_DATA) + // + LIST_ENTRY Link; + + UINT32 NicIndex; + UINT32 ProfileIndex; // The unique identifier for network profile, starts from 1 + CHAR16 SSId[SSID_STORAGE_SIZE]; + CHAR16 Password[PASSWORD_STORAGE_SIZE]; + + UINT8 SecurityType; + UINT8 EapAuthMethod; + + CHAR16 CACertName[WIFI_FILENAME_STR_MAX_SIZE]; + VOID *CACertData; + UINTN CACertSize; + CHAR16 ClientCertName[WIFI_FILENAME_STR_MAX_SIZE]; + VOID *ClientCertData; + UINTN ClientCertSize; + CHAR16 PrivateKeyName[WIFI_FILENAME_STR_MAX_SIZE]; + VOID *PrivateKeyData; + UINTN PrivateKeyDataSize; + CHAR16 PrivateKeyPassword[PASSWORD_STORAGE_SIZE]; //Password to protect private key file + CHAR16 EapIdentity[EAP_IDENTITY_SIZE]; + CHAR16 EapPassword[PASSWORD_STORAGE_SIZE]; + UINT8 EapSecondAuthMethod; + + BOOLEAN AKMSuiteSupported; + BOOLEAN CipherSuiteSupported; + BOOLEAN IsAvailable; + EFI_80211_NETWORK Network; + UINT8 NetworkQuality; + EFI_STRING_ID TitleToken; +} WIFI_MGR_NETWORK_PROFILE; + +#define WIFI_MGR_PROFILE_SIGNATURE SIGNATURE_32 ('W','M','N','P') + +#pragma pack(1) +/// +/// HII specific Vendor Device Path definition. +/// +typedef struct { + VENDOR_DEVICE_PATH VendorDevicePath; + EFI_DEVICE_PATH_PROTOCOL End; +} HII_VENDOR_DEVICE_PATH; +#pragma pack() + +#endif diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfigHii.h b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfigHii.h new file mode 100644 index 000000000..45201f7d6 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfigHii.h @@ -0,0 +1,29 @@ +/** @file + Module level GUIDs used in WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __WIFI_CONFIG_HII_GUID_H__ +#define __WIFI_CONFIG_HII_GUID_H__ + +// {36AF7790-0C2F-4055-9D3D-DB72A44953CB} +#define WIFI_CONFIG_NETWORK_LIST_REFRESH_GUID \ + { \ + 0xc5f3c7f9, 0xfb9d, 0x49f1, { 0xbe, 0x67, 0x8b, 0xad, 0x20, 0xa7, 0xc6, 0xac } \ + } + +#define WIFI_CONFIG_CONNECT_FORM_REFRESH_GUID \ + { \ + 0xe5faf2b2, 0x5ecc, 0x44ac, { 0x91, 0x75, 0xfb, 0x78, 0xb2, 0x8a, 0x59, 0x6c } \ + } + +#define WIFI_CONFIG_MAIN_FORM_REFRESH_GUID \ + { \ + 0xde609972, 0xcbcc, 0x4e82, { 0x8b, 0x3e, 0x6a, 0xc5, 0xcf, 0x56, 0x73, 0x8d } \ + } + +#endif diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfigNVDataStruct.h b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfigNVDataStruct.h new file mode 100644 index 000000000..cffc51f59 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrConfigNVDataStruct.h @@ -0,0 +1,152 @@ +/** @file + Define IFR NVData structures used by the WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _WIFI_NVDATASTRUC_H_ +#define _WIFI_NVDATASTRUC_H_ + +#include +#include "WifiConnectionMgrConfigHii.h" + +#define MANAGER_VARSTORE_ID 0x0802 + +#define WIFI_STR_MAX_SIZE 224 +#define WIFI_FILENAME_STR_MAX_SIZE 224 +#define WIFI_MGR_MAX_MAC_STRING_LEN 96 + +#define SSID_MIN_LEN 1 +#define SSID_MAX_LEN 32 +#define SSID_STORAGE_SIZE 33 + +#define PASSWORD_MIN_LEN 8 +#define PASSWORD_MAX_LEN 63 +#define PASSWORD_STORAGE_SIZE 64 + +#define EAP_IDENTITY_LEN 63 +#define EAP_IDENTITY_SIZE 64 + +#define FORMID_NONE_FORM 0 +#define FORMID_MAC_SELECTION 1 +#define FORMID_WIFI_MAINPAGE 2 +#define FORMID_NETWORK_LIST 3 +#define FORMID_CONNECT_NETWORK 4 +#define FORMID_ENROLL_CERT 5 +#define FORMID_CA_LIST 6 +#define FORMID_ENROLL_PRIVATE_KEY 7 +#define FORMID_PRIVATE_KEY_LIST 8 +#define FORMID_WIFI_SETTINGS 9 +#define FORMID_HIDDEN_NETWORK_LIST 10 + +// +// Mac List Form Key +// +#define KEY_MAC_LIST 0x100 + +// +// Main Form Key +// +#define KEY_REFRESH_TITLE_CONNECTION_STATUS 0x101 + +// +// Network List Form Key +// +#define KEY_NETWORK_LIST 0x102 +#define KEY_REFRESH_NETWORK_LIST 0x103 +#define KEY_WIFI_SETTINGS 0x104 + +// +// Connect Network Form Key +// +#define KEY_PASSWORD_CONNECT_NETWORK 0x201 +#define KEY_CONNECT_ACTION 0x202 +#define KEY_REFRESH_CONNECT_CONFIGURATION 0x203 +#define KEY_EAP_AUTH_METHOD_CONNECT_NETWORK 0x204 +#define KEY_EAP_SEAUTH_METHOD_CONNECT_NETWORK 0x205 +#define KEY_ENROLL_CA_CERT_CONNECT_NETWORK 0x206 +#define KEY_ENROLL_CLIENT_CERT_CONNECT_NETWORK 0x207 +#define KEY_ENROLL_PRIVATE_KEY_CONNECT_NETWORK 0x208 +#define KEY_EAP_IDENTITY_CONNECT_NETWORK 0x209 +#define KEY_EAP_PASSWORD_CONNECT_NETWORK 0x210 + +// +//Cert Form And Private Key Form +// +#define KEY_EAP_ENROLL_CERT_FROM_FILE 0x301 +#define KEY_EAP_ENROLL_PRIVATE_KEY_FROM_FILE 0x302 +#define KEY_SAVE_CERT_TO_MEM 0x303 +#define KEY_NO_SAVE_CERT_TO_MEM 0x304 +#define KEY_SAVE_PRIVATE_KEY_TO_MEM 0x305 +#define KEY_NO_SAVE_PRIVATE_KEY_TO_MEM 0x306 +#define KEY_PRIVATE_KEY_PASSWORD 0x307 +#define KEY_ENROLLED_CERT_NAME 0x308 +#define KEY_ENROLLED_PRIVATE_KEY_NAME 0x309 + +// +// Hidden Network Configuration Form +// +#define KEY_HIDDEN_NETWORK 0x401 +#define KEY_ADD_HIDDEN_NETWORK 0x402 +#define KEY_REMOVE_HIDDEN_NETWORK 0x403 + +// +// Dynamic Lists +// +#define MAC_LIST_COUNT_MAX 255 +#define LABEL_MAC_ENTRY 0x1000 +#define KEY_MAC_ENTRY_BASE 0x1100 + +#define NETWORK_LIST_COUNT_MAX 4095 +#define LABEL_NETWORK_LIST_ENTRY 0x2000 +#define KEY_AVAILABLE_NETWORK_ENTRY_BASE 0x3000 + +#define HIDDEN_NETWORK_LIST_COUNT_MAX 255 +#define LABEL_HIDDEN_NETWORK_ENTRY 0x4000 +#define KEY_HIDDEN_NETWORK_ENTRY_BASE 0x4100 + +#define LABEL_END 0xffff + +// +// Network Security Type +// +#define SECURITY_TYPE_NONE 0 +#define SECURITY_TYPE_WPA_ENTERPRISE 1 +#define SECURITY_TYPE_WPA2_ENTERPRISE 2 +#define SECURITY_TYPE_WPA_PERSONAL 3 +#define SECURITY_TYPE_WPA2_PERSONAL 4 +#define SECURITY_TYPE_WEP 5 +#define SECURITY_TYPE_UNKNOWN 6 +#define SECURITY_TYPE_MAX 7 + +#define EAP_AUTH_METHOD_TTLS 0 +#define EAP_AUTH_METHOD_PEAP 1 +#define EAP_AUTH_METHOD_TLS 2 +#define EAP_AUTH_METHOD_MAX 3 + +#define EAP_SEAUTH_METHOD_MSCHAPV2 0 +#define EAP_SEAUTH_METHOD_MAX 1 + +#define HIDDEN_NETWORK_LIST_VAR_OFFSET ((UINT16) OFFSET_OF (WIFI_MANAGER_IFR_NVDATA, HiddenNetworkList)) + +#pragma pack(1) +typedef struct _WIFI_MANAGER_IFR_NVDATA { + + UINT32 ProfileCount; + CHAR16 SSId[SSID_STORAGE_SIZE]; + CHAR16 Password[PASSWORD_STORAGE_SIZE]; + CHAR16 PrivateKeyPassword[PASSWORD_STORAGE_SIZE]; + CHAR16 EapIdentity[EAP_IDENTITY_SIZE]; + CHAR16 EapPassword[PASSWORD_STORAGE_SIZE]; + UINT8 SecurityType; + UINT8 EapAuthMethod; + UINT8 EapSecondAuthMethod; + UINT8 HiddenNetworkList[HIDDEN_NETWORK_LIST_COUNT_MAX]; + +} WIFI_MANAGER_IFR_NVDATA; +#pragma pack() + +#endif diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDriver.c b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDriver.c new file mode 100644 index 000000000..841ff5258 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDriver.c @@ -0,0 +1,616 @@ +/** @file + The driver binding protocol for the WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "WifiConnectionMgrDxe.h" + +/// +/// Driver Binding Protocol instance +/// +EFI_DRIVER_BINDING_PROTOCOL gWifiMgrDxeDriverBinding = { + WifiMgrDxeDriverBindingSupported, + WifiMgrDxeDriverBindingStart, + WifiMgrDxeDriverBindingStop, + WIFI_MGR_DXE_VERSION, + NULL, + NULL +}; + +// +//The private global data for WiFi Connection Manager +// +WIFI_MGR_PRIVATE_DATA *mPrivate = NULL; + +// +//The private guid to identify WiFi Connection Manager +// +EFI_GUID mEfiWifiMgrPrivateGuid = EFI_WIFIMGR_PRIVATE_GUID; + +// +//The Hii config guids +// +EFI_GUID gWifiConfigFormSetGuid = WIFI_CONNECTION_MANAGER_CONFIG_GUID; +EFI_GUID mWifiConfigNetworkListRefreshGuid = WIFI_CONFIG_NETWORK_LIST_REFRESH_GUID; +EFI_GUID mWifiConfigConnectFormRefreshGuid = WIFI_CONFIG_CONNECT_FORM_REFRESH_GUID; +EFI_GUID mWifiConfigMainFormRefreshGuid = WIFI_CONFIG_MAIN_FORM_REFRESH_GUID; + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + + Status = gBS->OpenProtocol ( + ControllerHandle, + &mEfiWifiMgrPrivateGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + return EFI_ALREADY_STARTED; + } + + // + // Test for the wireless MAC connection 2 protocol + // + return gBS->OpenProtocol ( + ControllerHandle, + &gEfiWiFi2ProtocolGuid, + NULL, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); +} + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + UINTN AddressSize; + WIFI_MGR_DEVICE_DATA *Nic; + EFI_WIRELESS_MAC_CONNECTION_II_PROTOCOL *Wmp; + EFI_SUPPLICANT_PROTOCOL *Supplicant; + EFI_EAP_CONFIGURATION_PROTOCOL *EapConfig; + + Nic = NULL; + + // + //Open Protocols + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiWiFi2ProtocolGuid, + (VOID**) &Wmp, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiSupplicantProtocolGuid, + (VOID**) &Supplicant, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + Supplicant = NULL; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiEapConfigurationProtocolGuid, + (VOID**) &EapConfig, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + EapConfig = NULL; + } + + // + //Initialize Nic device data + // + Nic = AllocateZeroPool (sizeof (WIFI_MGR_DEVICE_DATA)); + if (Nic == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR1; + } + Nic->Signature = WIFI_MGR_DEVICE_DATA_SIGNATURE; + Nic->DriverHandle = This->DriverBindingHandle; + Nic->ControllerHandle = ControllerHandle; + Nic->Private = mPrivate; + Nic->Wmp = Wmp; + Nic->Supplicant = Supplicant; + Nic->EapConfig = EapConfig; + Nic->UserSelectedProfile = NULL; + Nic->OneTimeScanRequest = FALSE; + Nic->ScanTickTime = WIFI_SCAN_FREQUENCY; //Initialize the first scan + + if (Nic->Supplicant != NULL) { + WifiMgrGetSupportedSuites(Nic); + } + + InitializeListHead (&Nic->ProfileList); + + // + // Record the MAC address of the incoming NIC. + // + Status = NetLibGetMacAddress ( + ControllerHandle, + (EFI_MAC_ADDRESS*) &Nic->MacAddress, + &AddressSize + ); + if (EFI_ERROR (Status)) { + goto ERROR2; + } + + // + // Create and start the timer for the status check + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL | EVT_TIMER, + TPL_CALLBACK, + WifiMgrOnTimerTick, + Nic, + &Nic->TickTimer + ); + if (EFI_ERROR (Status)) { + goto ERROR2; + } + + Status = gBS->SetTimer (Nic->TickTimer, TimerPeriodic, EFI_TIMER_PERIOD_MILLISECONDS(500)); + if (EFI_ERROR (Status)) { + goto ERROR3; + } + + Nic->ConnectState = WifiMgrDisconnected; + Nic->ScanState = WifiMgrScanFinished; + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + InsertTailList (&mPrivate->NicList, &Nic->Link); + Nic->NicIndex = mPrivate->NicCount ++; + if (mPrivate->CurrentNic == NULL) { + mPrivate->CurrentNic = Nic; + } + gBS->RestoreTPL (OldTpl); + + Status = gBS->InstallProtocolInterface ( + &ControllerHandle, + &mEfiWifiMgrPrivateGuid, + EFI_NATIVE_INTERFACE, + &Nic->WifiMgrIdentifier + ); + if (EFI_ERROR (Status)) { + goto ERROR4; + } + + return EFI_SUCCESS; + +ERROR4: + + gBS->SetTimer (Nic->TickTimer, TimerCancel, 0); + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + RemoveEntryList (&Nic->Link); + mPrivate->NicCount--; + gBS->RestoreTPL (OldTpl); + +ERROR3: + + gBS->CloseEvent (Nic->TickTimer); + +ERROR2: + + if (Nic->Supplicant != NULL) { + if (Nic->SupportedSuites.SupportedAKMSuites != NULL) { + FreePool (Nic->SupportedSuites.SupportedAKMSuites); + } + if (Nic->SupportedSuites.SupportedSwCipherSuites != NULL) { + FreePool (Nic->SupportedSuites.SupportedSwCipherSuites); + } + if (Nic->SupportedSuites.SupportedHwCipherSuites != NULL) { + FreePool (Nic->SupportedSuites.SupportedHwCipherSuites); + } + } + FreePool (Nic); + +ERROR1: + + if (Supplicant != NULL) { + gBS->CloseProtocol ( + ControllerHandle, + &gEfiSupplicantProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + } + + if (EapConfig != NULL) { + gBS->CloseProtocol ( + ControllerHandle, + &gEfiEapConfigurationProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + } + + gBS->CloseProtocol ( + ControllerHandle, + &gEfiWiFi2ProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + + return Status; +} + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + WIFI_MGR_PRIVATE_PROTOCOL *WifiMgrIdentifier; + WIFI_MGR_DEVICE_DATA *Nic; + + Status = gBS->OpenProtocol ( + ControllerHandle, + &mEfiWifiMgrPrivateGuid, + (VOID **) &WifiMgrIdentifier, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + Nic = WIFI_MGR_DEVICE_DATA_FROM_IDENTIFIER (WifiMgrIdentifier); + if (Nic == NULL) { + return EFI_DEVICE_ERROR; + } + + // + // Close Event + // + gBS->SetTimer (Nic->TickTimer, TimerCancel, 0); + gBS->CloseEvent (Nic->TickTimer); + + // + // Clean Supported Suites + // + if (Nic->Supplicant != NULL) { + if (Nic->SupportedSuites.SupportedAKMSuites != NULL) { + FreePool (Nic->SupportedSuites.SupportedAKMSuites); + } + if (Nic->SupportedSuites.SupportedSwCipherSuites != NULL) { + FreePool (Nic->SupportedSuites.SupportedSwCipherSuites); + } + if (Nic->SupportedSuites.SupportedHwCipherSuites != NULL) { + FreePool (Nic->SupportedSuites.SupportedHwCipherSuites); + } + } + + // + // Close Protocols + // + Status = gBS->UninstallProtocolInterface ( + ControllerHandle, + &mEfiWifiMgrPrivateGuid, + &Nic->WifiMgrIdentifier + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->CloseProtocol ( + ControllerHandle, + &gEfiWiFi2ProtocolGuid, + Nic->DriverHandle, + Nic->ControllerHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Nic->Supplicant != NULL) { + Status = gBS->CloseProtocol ( + ControllerHandle, + &gEfiSupplicantProtocolGuid, + Nic->DriverHandle, + Nic->ControllerHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + if (Nic->EapConfig != NULL) { + Status = gBS->CloseProtocol ( + ControllerHandle, + &gEfiEapConfigurationProtocolGuid, + Nic->DriverHandle, + Nic->ControllerHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // Remove this Nic from Nic list + // + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + RemoveEntryList (&Nic->Link); + mPrivate->NicCount--; + if (mPrivate->CurrentNic == Nic) { + mPrivate->CurrentNic = NULL; + } + + gBS->RestoreTPL (OldTpl); + + WifiMgrFreeProfileList (&Nic->ProfileList); + FreePool (Nic); + + DEBUG ((DEBUG_INFO, "[WiFi Connection Manager] Device Controller has been Disconnected!\n")); + return EFI_SUCCESS; +} + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. + + @param ImageHandle The firmware allocated handle for the UEFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others An unexpected error occurred. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeDriverEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gWifiMgrDxeDriverBinding, + ImageHandle, + &gWifiMgrDxeComponentName, + &gWifiMgrDxeComponentName2 + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Initialize the global private data structure. + // + mPrivate = AllocateZeroPool (sizeof (WIFI_MGR_PRIVATE_DATA)); + if (mPrivate == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ERROR1; + } + mPrivate->Signature = WIFI_MGR_PRIVATE_DATA_SIGNATURE; + mPrivate->DriverHandle = ImageHandle; + InitializeListHead (&mPrivate->NicList); + mPrivate->NicCount = 0; + mPrivate->CurrentNic = NULL; + InitializeListHead (&mPrivate->HiddenNetworkList); + mPrivate->HiddenNetworkCount = 0; + + // + //Create events for page refresh + // + Status = gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + WifiMgrInternalEmptyFunction, + NULL, + &mWifiConfigNetworkListRefreshGuid, + &mPrivate->NetworkListRefreshEvent + ); + if (EFI_ERROR (Status)) { + goto ERROR2; + } + + Status = gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + WifiMgrInternalEmptyFunction, + NULL, + &mWifiConfigConnectFormRefreshGuid, + &mPrivate->ConnectFormRefreshEvent + ); + if (EFI_ERROR (Status)) { + goto ERROR3; + } + + Status = gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + WifiMgrInternalEmptyFunction, + NULL, + &mWifiConfigMainFormRefreshGuid, + &mPrivate->MainPageRefreshEvent + ); + if (EFI_ERROR (Status)) { + goto ERROR4; + } + + Status = WifiMgrDxeConfigFormInit (mPrivate); + if (EFI_ERROR (Status)) { + goto ERROR5; + } + + return Status; + +ERROR5: + gBS->CloseEvent (mPrivate->MainPageRefreshEvent); + +ERROR4: + gBS->CloseEvent (mPrivate->ConnectFormRefreshEvent); + +ERROR3: + gBS->CloseEvent (mPrivate->NetworkListRefreshEvent); + +ERROR2: + if (mPrivate != NULL) { + FreePool (mPrivate); + mPrivate = NULL; + } + +ERROR1: + gBS->UninstallMultipleProtocolInterfaces ( + ImageHandle, + &gEfiDriverBindingProtocolGuid, + &gWifiMgrDxeDriverBinding, + &gEfiComponentNameProtocolGuid, + &gWifiMgrDxeComponentName, + &gEfiComponentName2ProtocolGuid, + &gWifiMgrDxeComponentName2, + NULL + ); + + return Status; +} diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDriverBinding.h b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDriverBinding.h new file mode 100644 index 000000000..2420bb44e --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDriverBinding.h @@ -0,0 +1,142 @@ +/** @file + The driver binding protocol for the WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_WIFI_DRIVER_BINDING__ +#define __EFI_WIFI_DRIVER_BINDING__ + +/** + Tests to see if this driver supports a given controller. If a child device is provided, + it further tests to see if this driver supports creating a handle for the specified child device. + + This function checks to see if the driver specified by This supports the device specified by + ControllerHandle. Drivers will typically use the device path attached to + ControllerHandle and/or the services from the bus I/O abstraction attached to + ControllerHandle to determine if the driver supports ControllerHandle. This function + may be called many times during platform initialization. In order to reduce boot times, the tests + performed by this function must be very small, and take as little time as possible to execute. This + function must not change the state of any hardware devices, and this function must be aware that the + device specified by ControllerHandle may already be managed by the same driver or a + different driver. This function must match its calls to AllocatePages() with FreePages(), + AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol(). + Because ControllerHandle may have been previously started by the same driver, if a protocol is + already in the opened state, then it must not be closed with CloseProtocol(). This is required + to guarantee the state of ControllerHandle is not modified by this function. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to test. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For bus drivers, if this parameter is not NULL, then + the bus driver must determine if the bus controller specified + by ControllerHandle and the child controller specified + by RemainingDevicePath are both supported by this + bus driver. + + @retval EFI_SUCCESS The device specified by ControllerHandle and + RemainingDevicePath is supported by the driver specified by This. + @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by the driver + specified by This. + @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and + RemainingDevicePath is already being managed by a different + driver or an application that requires exclusive access. + Currently not implemented. + @retval EFI_UNSUPPORTED The device specified by ControllerHandle and + RemainingDevicePath is not supported by the driver specified by This. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Starts a device controller or a bus controller. + + The Start() function is designed to be invoked from the EFI boot service ConnectController(). + As a result, much of the error checking on the parameters to Start() has been moved into this + common boot service. It is legal to call Start() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE. + 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned + EFI_DEVICE_PATH_PROTOCOL. + 3. Prior to calling Start(), the Supported() function for the driver specified by This must + have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle The handle of the controller to start. This handle + must support a protocol interface that supplies + an I/O abstraction to the driver. + @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This + parameter is ignored by device drivers, and is optional for bus + drivers. For a bus driver, if this parameter is NULL, then handles + for all the children of Controller are created by this driver. + If this parameter is not NULL and the first Device Path Node is + not the End of Device Path Node, then only the handle for the + child device specified by the first Device Path Node of + RemainingDevicePath is created by this driver. + If the first Device Path Node of RemainingDevicePath is + the End of Device Path Node, no child handle is created by this + driver. + + @retval EFI_SUCCESS The device was started. + @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval Others The driver failded to start the device. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stops a device controller or a bus controller. + + The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). + As a result, much of the error checking on the parameters to Stop() has been moved + into this common boot service. It is legal to call Stop() from other locations, + but the following calling restrictions must be followed, or the system behavior will not be deterministic. + 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this + same driver's Start() function. + 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid + EFI_HANDLE. In addition, all of these handles must have been created in this driver's + Start() function, and the Start() function must have called OpenProtocol() on + ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. + + @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. + @param[in] ControllerHandle A handle to the device being stopped. The handle must + support a bus specific I/O protocol for the driver + to use to stop the device. + @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer. + @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL + if NumberOfChildren is 0. + + @retval EFI_SUCCESS The device was stopped. + @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ); + +#endif diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDxe.h b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDxe.h new file mode 100644 index 000000000..170991253 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrDxe.h @@ -0,0 +1,321 @@ +/** @file + The miscellaneous structure definitions for WiFi connection driver. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_WIFI_MGR_DXE_H__ +#define __EFI_WIFI_MGR_DXE_H__ + +#include + +// +// Libraries +// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +// UEFI Driver Model Protocols +// +#include +#include +#include +#include +#include + +// +// Consumed Protocols +// +#include +#include +#include +#include +#include +#include + +// +// Produced Protocols +// +#include + +// +// Guids +// +#include +#include +#include + +// +// NvData struct definition +// +#include "WifiConnectionMgrConfigNVDataStruct.h" +#include "WifiConnectionMgrConfig.h" +#include "EapContext.h" +#include "WifiConnectionMgrConfigHii.h" + +// +// Driver Version +// +#define WIFI_MGR_DXE_VERSION 0xb + +#define OUI_IEEE_80211I 0xAC0F00 + +typedef enum { + Ieee80211PairwiseCipherSuiteUseGroupCipherSuite = 0, + Ieee80211PairwiseCipherSuiteWEP40 = 1, + Ieee80211PairwiseCipherSuiteTKIP = 2, + Ieee80211PairwiseCipherSuiteCCMP = 4, + Ieee80211PairwiseCipherSuiteWEP104 = 5, + Ieee80211PairwiseCipherSuiteBIP = 6, + //... +} IEEE_80211_PAIRWISE_CIPHER_SUITE; + +#define IEEE_80211_PAIRWISE_CIPHER_SUITE_USE_GROUP (OUI_IEEE_80211I | (Ieee80211PairwiseCipherSuiteUseGroupCipherSuite << 24)) +#define IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP40 (OUI_IEEE_80211I | (Ieee80211PairwiseCipherSuiteWEP40 << 24)) +#define IEEE_80211_PAIRWISE_CIPHER_SUITE_TKIP (OUI_IEEE_80211I | (Ieee80211PairwiseCipherSuiteTKIP << 24)) +#define IEEE_80211_PAIRWISE_CIPHER_SUITE_CCMP (OUI_IEEE_80211I | (Ieee80211PairwiseCipherSuiteCCMP << 24)) +#define IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP104 (OUI_IEEE_80211I | (Ieee80211PairwiseCipherSuiteWEP104 << 24)) +#define IEEE_80211_PAIRWISE_CIPHER_SUITE_BIP (OUI_IEEE_80211I | (Ieee80211PairwiseCipherSuiteBIP << 24)) + +typedef enum { + Ieee80211AkmSuite8021XOrPMKSA = 1, + Ieee80211AkmSuitePSK = 2, + Ieee80211AkmSuite8021XOrPMKSASHA256 = 5, + Ieee80211AkmSuitePSKSHA256 = 6 + //... +} IEEE_80211_AKM_SUITE; + +#define IEEE_80211_AKM_SUITE_8021X_OR_PMKSA (OUI_IEEE_80211I | (Ieee80211AkmSuite8021XOrPMKSA << 24)) +#define IEEE_80211_AKM_SUITE_PSK (OUI_IEEE_80211I | (Ieee80211AkmSuitePSK << 24)) +#define IEEE_80211_AKM_SUITE_8021X_OR_PMKSA_SHA256 (OUI_IEEE_80211I | (Ieee80211AkmSuite8021XOrPMKSASHA256 << 24)) +#define IEEE_80211_AKM_SUITE_PSK_SHA256 (OUI_IEEE_80211I | (Ieee80211AkmSuitePSKSHA256 << 24)) + +// +// Protocol instances +// +extern EFI_DRIVER_BINDING_PROTOCOL gWifiMgrDxeDriverBinding; +extern EFI_COMPONENT_NAME2_PROTOCOL gWifiMgrDxeComponentName2; +extern EFI_COMPONENT_NAME_PROTOCOL gWifiMgrDxeComponentName; +extern EFI_HII_CONFIG_ACCESS_PROTOCOL gWifiMgrDxeHiiConfigAccess; + +// +// Private Context Data Structure +// +typedef enum { + WifiMgrDisconnected, + WifiMgrConnectingToAp, + WifiMgrConnectedToAp, + WifiMgrDisconnectingToAp, + WifiMgrConnectStateMaximum +} WIFI_MGR_CONNECT_STATE; + +typedef enum { + WifiMgrScanFinished, + WifiMgrScanning, + WifiMgrScanStateMaximum +} WIFI_MGR_SCAN_STATE; + +#define WIFI_SCAN_FREQUENCY 30 + +typedef struct _WIFI_MGR_SUPPORTED_SUITES { + EFI_80211_AKM_SUITE_SELECTOR *SupportedAKMSuites; + EFI_80211_CIPHER_SUITE_SELECTOR *SupportedSwCipherSuites; + EFI_80211_CIPHER_SUITE_SELECTOR *SupportedHwCipherSuites; +} WIFI_MGR_SUPPORTED_SUITES; + +#define EFI_WIFIMGR_PRIVATE_GUID \ + { \ + 0x99b7c019, 0x4789, 0x4829, { 0xa7, 0xbd, 0x0d, 0x4b, 0xaa, 0x62, 0x28, 0x72 } \ + } + +typedef struct _WIFI_MGR_PRIVATE_DATA WIFI_MGR_PRIVATE_DATA; + +typedef struct _WIFI_MGR_PRIVATE_PROTOCOL { + UINT32 Reserved; +} WIFI_MGR_PRIVATE_PROTOCOL; + +typedef struct _WIFI_MGR_FILE_CONTEXT { + EFI_FILE_HANDLE FHandle; + UINT16 *FileName; +} WIFI_MGR_FILE_CONTEXT; + +typedef enum { + FileTypeCACert, + FileTypeClientCert, + FileTypeMax +} WIFI_MGR_FILE_TYPE; + +typedef struct { + UINT32 Signature; + EFI_HANDLE DriverHandle; + EFI_HANDLE ControllerHandle; + EFI_EVENT TickTimer; + WIFI_MGR_PRIVATE_DATA *Private; + + // + // Pointers to consumed protocols + // + EFI_WIRELESS_MAC_CONNECTION_II_PROTOCOL *Wmp; + EFI_SUPPLICANT_PROTOCOL *Supplicant; + EFI_EAP_CONFIGURATION_PROTOCOL *EapConfig; + + // + // Produced protocols + // + WIFI_MGR_PRIVATE_PROTOCOL WifiMgrIdentifier; + + // + // Private functions and data fields + // + LIST_ENTRY Link; // Link to the NicList in global private data structure. + UINT32 NicIndex; + EFI_80211_MAC_ADDRESS MacAddress; + WIFI_MGR_SUPPORTED_SUITES SupportedSuites; + EFI_ADAPTER_INFO_MEDIA_STATE LastLinkState; + + // + // The network is currently connected, connecting or disconnecting. + // Only one network can be operated at one time. + // + WIFI_MGR_NETWORK_PROFILE *CurrentOperateNetwork; + WIFI_MGR_NETWORK_PROFILE *ConnectPendingNetwork; + BOOLEAN HasDisconnectPendingNetwork; + + // + //Profile related data fields + // + LIST_ENTRY ProfileList; // List of WIFI_MGR_NETWORK_PROFILE + UINT32 AvailableCount; + UINT32 MaxProfileIndex; + WIFI_MGR_NETWORK_PROFILE *UserSelectedProfile; + + // + // Data fields for Hii functionlity + // + BOOLEAN OneTimeScanRequest; + BOOLEAN OneTimeConnectRequest; + BOOLEAN OneTimeDisconnectRequest; + WIFI_MGR_SCAN_STATE ScanState; + UINTN ScanTickTime; + WIFI_MGR_CONNECT_STATE ConnectState; + BOOLEAN ConnectStateChanged; +} WIFI_MGR_DEVICE_DATA; + +#define WIFI_MGR_DEVICE_DATA_SIGNATURE SIGNATURE_32 ('W','M','D','D') + +#define WIFI_MGR_DEVICE_DATA_FROM_IDENTIFIER(Identifier) \ + CR ( \ + Identifier, \ + WIFI_MGR_DEVICE_DATA, \ + WifiMgrIdentifier, \ + WIFI_MGR_DEVICE_DATA_SIGNATURE \ + ) + +typedef struct { + UINT32 Signature; + LIST_ENTRY Link; + CHAR16 SSId[SSID_STORAGE_SIZE]; +} WIFI_HIDDEN_NETWORK_DATA; + +#define WIFI_MGR_HIDDEN_NETWORK_SIGNATURE SIGNATURE_32 ('W','M','H','N') + +#define WIFI_MGR_HIDDEN_NETWORK_FROM_IDENTIFIER(Identifier) \ + CR ( \ + Identifier, \ + WIFI_HIDDEN_NETWORK_DATA, \ + WifiMgrIdentifier, \ + WIFI_MGR_HIDDEN_NETWORK_SIGNATURE \ + ) + +// +// Global private data struct +// +struct _WIFI_MGR_PRIVATE_DATA { + + UINT32 Signature; + EFI_HANDLE DriverHandle; + EFI_HII_HANDLE RegisteredHandle; + EFI_HII_CONFIG_ACCESS_PROTOCOL ConfigAccess; + + UINT32 NicCount; + LIST_ENTRY NicList; + WIFI_MGR_DEVICE_DATA *CurrentNic; + + // + // Data fields for Hii functionlity + // + EFI_EVENT NetworkListRefreshEvent; // Event to refresh the network list form + EFI_EVENT ConnectFormRefreshEvent; // Event to refresh the connect form + EFI_EVENT MainPageRefreshEvent; // Event to refresh the main page + + // + //User Input Record + // + UINT8 SecurityType; + UINT8 EapAuthMethod; + UINT8 EapSecondAuthMethod; + CHAR16 EapIdentity[EAP_IDENTITY_SIZE]; + + WIFI_MGR_FILE_CONTEXT *FileContext; + WIFI_MGR_FILE_TYPE FileType; + + UINT32 HiddenNetworkCount; + LIST_ENTRY HiddenNetworkList; +}; + +#define WIFI_MGR_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('W','M','P','D') + +#define WIFI_MGR_PRIVATE_DATA_FROM_CONFIG_ACCESS(This) \ + CR ( \ + This, \ + WIFI_MGR_PRIVATE_DATA, \ + ConfigAccess, \ + WIFI_MGR_PRIVATE_DATA_SIGNATURE \ + ) +extern WIFI_MGR_PRIVATE_DATA *mPrivate; + +typedef enum { + TokenTypeGetNetworksToken, + TokenTypeConnectNetworkToken, + TokenTypeDisconnectNetworkToken, + TokenTypeMax, +} WIFI_MGR_MAC_CONFIG_TOKEN_TYPE; + +typedef union { + EFI_80211_GET_NETWORKS_TOKEN *GetNetworksToken; + EFI_80211_CONNECT_NETWORK_TOKEN *ConnectNetworkToken; + EFI_80211_DISCONNECT_NETWORK_TOKEN *DisconnectNetworkToken; +} MAC_CONNECTION2_ADAPTER_TOKEN; + +typedef struct { + WIFI_MGR_DEVICE_DATA *Nic; + WIFI_MGR_MAC_CONFIG_TOKEN_TYPE Type; + MAC_CONNECTION2_ADAPTER_TOKEN Token; +} WIFI_MGR_MAC_CONFIG_TOKEN; + +// +// Include files with function prototypes +// +#include "WifiConnectionMgrDriverBinding.h" +#include "WifiConnectionMgrImpl.h" +#include "WifiConnectionMgrComponentName.h" +#include "WifiConnectionMgrHiiConfigAccess.h" +#include "WifiConnectionMgrMisc.h" +#include "WifiConnectionMgrFileUtil.h" + +#endif diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrFileUtil.c b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrFileUtil.c new file mode 100644 index 000000000..c0e0de1b8 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrFileUtil.c @@ -0,0 +1,303 @@ +/** @file + The file operation functions for WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "WifiConnectionMgrFileUtil.h" + +CHAR16* mDerPemEncodedSuffix[] = { + L".cer", + L".der", + L".crt", + L".pem", + NULL +}; + +/** + This code checks if the FileSuffix is one of the possible DER/PEM-encoded certificate suffix. + + @param[in] FileSuffix The suffix of the input certificate file + + @retval TRUE It's a DER/PEM-encoded certificate. + @retval FALSE It's NOT a DER/PEM-encoded certificate. + +**/ +BOOLEAN +IsDerPemEncodeCertificate ( + IN CONST CHAR16 *FileSuffix +) +{ + UINTN Index; + for (Index = 0; mDerPemEncodedSuffix[Index] != NULL; Index++) { + if (StrCmp (FileSuffix, mDerPemEncodedSuffix[Index]) == 0) { + return TRUE; + } + } + return FALSE; +} + +/** + Read file content into BufferPtr, the size of the allocate buffer + is *FileSize plus AddtionAllocateSize. + + @param[in] FileHandle The file to be read. + @param[in, out] BufferPtr Pointers to the pointer of allocated buffer. + @param[out] FileSize Size of input file + @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated. + In case the buffer need to contain others besides the file content. + + @retval EFI_SUCCESS The file was read into the buffer. + @retval EFI_INVALID_PARAMETER A parameter was invalid. + @retval EFI_OUT_OF_RESOURCES A memory allocation failed. + @retval others Unexpected error. + +**/ +EFI_STATUS +ReadFileContent ( + IN EFI_FILE_HANDLE FileHandle, + IN OUT VOID **BufferPtr, + OUT UINTN *FileSize, + IN UINTN AddtionAllocateSize + ) +{ + UINTN BufferSize; + UINT64 SourceFileSize; + VOID *Buffer; + EFI_STATUS Status; + + if ((FileHandle == NULL) || (FileSize == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Buffer = NULL; + + // + // Get the file size + // + Status = FileHandle->SetPosition (FileHandle, (UINT64) -1); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = FileHandle->GetPosition (FileHandle, &SourceFileSize); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + Status = FileHandle->SetPosition (FileHandle, 0); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + BufferSize = (UINTN) SourceFileSize + AddtionAllocateSize; + Buffer = AllocateZeroPool(BufferSize); + if (Buffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + BufferSize = (UINTN) SourceFileSize; + *FileSize = BufferSize; + + Status = FileHandle->Read (FileHandle, &BufferSize, Buffer); + if (EFI_ERROR (Status) || BufferSize != *FileSize) { + FreePool (Buffer); + Buffer = NULL; + Status = EFI_BAD_BUFFER_SIZE; + goto ON_EXIT; + } + +ON_EXIT: + + *BufferPtr = Buffer; + return Status; +} + +/** + This function converts an input device structure to a Unicode string. + + @param[in] DevPath A pointer to the device path structure. + + @return A new allocated Unicode string that represents the device path. + +**/ +CHAR16 * +EFIAPI +DevicePathToStr ( + IN EFI_DEVICE_PATH_PROTOCOL *DevPath + ) +{ + return ConvertDevicePathToText ( + DevPath, + FALSE, + TRUE + ); +} + +/** + Extract filename from device path. The returned buffer is allocated using AllocateCopyPool. + The caller is responsible for freeing the allocated buffer using FreePool(). If return NULL + means not enough memory resource. + + @param DevicePath Device path. + + @retval NULL Not enough memory resourece for AllocateCopyPool. + @retval Other A new allocated string that represents the file name. + +**/ +CHAR16 * +ExtractFileNameFromDevicePath ( + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath + ) +{ + CHAR16 *String; + CHAR16 *MatchString; + CHAR16 *LastMatch; + CHAR16 *FileName; + UINTN Length; + + ASSERT(DevicePath != NULL); + + String = DevicePathToStr(DevicePath); + if (String == NULL) { + return NULL; + } + MatchString = String; + LastMatch = String; + FileName = NULL; + + while(MatchString != NULL){ + LastMatch = MatchString + 1; + MatchString = StrStr(LastMatch,L"\\"); + } + + Length = StrLen(LastMatch); + FileName = AllocateCopyPool ((Length + 1) * sizeof(CHAR16), LastMatch); + if (FileName != NULL) { + *(FileName + Length) = 0; + } + + FreePool(String); + + return FileName; +} + +/** + Update the form base on the selected file. + + @param[in] Private The pointer to the global private data structure. + @param[in] FilePath Point to the file path. + @param[in] FormId The form needs to display. + + @retval TRUE Exit caller function. + @retval FALSE Not exit caller function. + +**/ +BOOLEAN +UpdatePage( + IN WIFI_MGR_PRIVATE_DATA *Private, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath, + IN EFI_FORM_ID FormId + ) +{ + CHAR16 *FileName; + EFI_STATUS Status; + + FileName = NULL; + + if (FilePath != NULL) { + FileName = ExtractFileNameFromDevicePath(FilePath); + } + if (FileName == NULL) { + // + // FileName = NULL has two cases: + // 1. FilePath == NULL, not select file. + // 2. FilePath != NULL, but ExtractFileNameFromDevicePath return NULL not enough memory resource. + // In these two case, no need to update the form, and exit the caller function. + // + return TRUE; + } + + // + // Close the previous file handle before open a new one. + // + if (Private->FileContext->FHandle != NULL) { + Private->FileContext->FHandle->Close (Private->FileContext->FHandle); + } + Private->FileContext->FHandle = NULL; + + Status = EfiOpenFileByDevicePath ( + &FilePath, + &Private->FileContext->FHandle, + EFI_FILE_MODE_READ, + 0 + ); + if (EFI_ERROR (Status)) { + if (FormId == FORMID_ENROLL_CERT) { + HiiSetString (Private->RegisteredHandle, + STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME), L"", NULL); + } else if (FormId == FORMID_ENROLL_PRIVATE_KEY){ + HiiSetString (Private->RegisteredHandle, + STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME), L"", NULL); + } + } else { + + if (Private->FileContext->FileName != NULL) { + FreePool (Private->FileContext->FileName); + Private->FileContext->FileName = NULL; + } + Private->FileContext->FileName = FileName; + + if (FormId == FORMID_ENROLL_CERT) { + HiiSetString (Private->RegisteredHandle, + STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME), FileName, NULL); + } else if (FormId == FORMID_ENROLL_PRIVATE_KEY){ + HiiSetString (Private->RegisteredHandle, + STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME), FileName, NULL); + } + } + + return TRUE; +} + +/** + Update the CA form base on the input file path info. + + @param[in] Private The pointer to the global private data structure. + @param[in] FilePath Point to the file path. + + @retval TRUE Exit caller function. + @retval FALSE Not exit caller function. + +**/ +BOOLEAN +UpdateCAFromFile ( + IN WIFI_MGR_PRIVATE_DATA *Private, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath + ) +{ + return UpdatePage(Private, FilePath, FORMID_ENROLL_CERT); +} + +/** + Update the Private Key form base on the input file path info. + + @param[in] Private The pointer to the global private data structure. + @param[in] FilePath Point to the file path. + + @retval TRUE Exit caller function. + @retval FALSE Not exit caller function. + +**/ +BOOLEAN +UpdatePrivateKeyFromFile ( + IN WIFI_MGR_PRIVATE_DATA *Private, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath + ) +{ + return UpdatePage(Private, FilePath, FORMID_ENROLL_PRIVATE_KEY); +} + diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrFileUtil.h b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrFileUtil.h new file mode 100644 index 000000000..9d9e909d6 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrFileUtil.h @@ -0,0 +1,71 @@ +/** @file + The file operation functions for WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_WIFI_MGR_FILE_UTIL__ +#define __EFI_WIFI_MGR_FILE_UTIL__ + +#include "WifiConnectionMgrDxe.h" + +/** + Read file content into BufferPtr, the size of the allocate buffer + is *FileSize plus AddtionAllocateSize. + + @param[in] FileHandle The file to be read. + @param[in, out] BufferPtr Pointers to the pointer of allocated buffer. + @param[out] FileSize Size of input file + @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated. + In case the buffer need to contain others besides the file content. + + @retval EFI_SUCCESS The file was read into the buffer. + @retval EFI_INVALID_PARAMETER A parameter was invalid. + @retval EFI_OUT_OF_RESOURCES A memory allocation failed. + @retval others Unexpected error. + +**/ +EFI_STATUS +ReadFileContent ( + IN EFI_FILE_HANDLE FileHandle, + IN OUT VOID **BufferPtr, + OUT UINTN *FileSize, + IN UINTN AddtionAllocateSize + ); + +/** + Update the CA cert base on the input file path info. + + @param[in] Private The pointer to the global private data structure. + @param[in] FilePath Point to the file path. + + @retval TRUE Exit caller function. + @retval FALSE Not exit caller function. + +**/ +BOOLEAN +UpdateCAFromFile ( + IN WIFI_MGR_PRIVATE_DATA *Private, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath + ); + +/** + Update the Private Key base on the input file path info. + + @param[in] Private The pointer to the global private data structure. + @param[in] FilePath Point to the file path. + + @retval TRUE Exit caller function. + @retval FALSE Not exit caller function. + +**/ +BOOLEAN +UpdatePrivateKeyFromFile ( + IN WIFI_MGR_PRIVATE_DATA *Private, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath + ); + +#endif diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrHiiConfigAccess.c b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrHiiConfigAccess.c new file mode 100644 index 000000000..881592efd --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrHiiConfigAccess.c @@ -0,0 +1,2017 @@ +/** @file + The Hii functions for WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "WifiConnectionMgrDxe.h" + +CHAR16 mVendorStorageName[] = L"WIFI_MANAGER_IFR_NVDATA"; + +HII_VENDOR_DEVICE_PATH mWifiMgrDxeHiiVendorDevicePath = { + { + { + HARDWARE_DEVICE_PATH, + HW_VENDOR_DP, + { + (UINT8) (sizeof (VENDOR_DEVICE_PATH)), + (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8) + } + }, + WIFI_CONNECTION_MANAGER_CONFIG_GUID + }, + { + END_DEVICE_PATH_TYPE, + END_ENTIRE_DEVICE_PATH_SUBTYPE, + { + (UINT8) (END_DEVICE_PATH_LENGTH), + (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8) + } + } +}; + +// +// HII Config Access Protocol instance +// +GLOBAL_REMOVE_IF_UNREFERENCED +EFI_HII_CONFIG_ACCESS_PROTOCOL gWifiMgrDxeHiiConfigAccess = { + WifiMgrDxeHiiConfigAccessExtractConfig, + WifiMgrDxeHiiConfigAccessRouteConfig, + WifiMgrDxeHiiConfigAccessCallback +}; + +CHAR16* mSecurityType[] = { + L"OPEN ", + L"WPA-Enterprise ", + L"WPA2-Enterprise", + L"WPA-Personal ", + L"WPA2-Personal ", + L"WEP ", + L"UnKnown " +}; + +CHAR16* mSignalStrengthBar[] = { + L"[-----]", + L"[*----]", + L"[**---]", + L"[***--]", + L"[****-]", + L"[*****]" +}; + +#define RSSI_TO_SIGNAL_STRENGTH_BAR(Rssi) mSignalStrengthBar[((Rssi + 19)/20)] + +#define NET_LIST_FOR_EACH_FROM_NODE(Entry, Node, ListHead) \ + for(Entry = Node->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink) + +extern EFI_GUID gWifiConfigFormSetGuid; + +/** + Create Hii Extend Label OpCode as the start opcode and end opcode. + The caller is responsible for freeing the OpCode with HiiFreeOpCodeHandle(). + + @param[in] StartLabelNumber The number of start label. + @param[out] StartOpCodeHandle Points to the start opcode handle. + @param[out] EndOpCodeHandle Points to the end opcode handle. + + @retval EFI_OUT_OF_RESOURCES Do not have sufficient resource to finish this + operation. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_SUCCESS The operation is completed successfully. + @retval Other Errors Returned errors when updating the HII form. + +**/ +EFI_STATUS +WifiMgrCreateOpCode ( + IN UINT16 StartLabelNumber, + OUT VOID **StartOpCodeHandle, + OUT VOID **EndOpCodeHandle + ) +{ + EFI_STATUS Status; + EFI_IFR_GUID_LABEL *InternalStartLabel; + EFI_IFR_GUID_LABEL *InternalEndLabel; + + if (StartOpCodeHandle == NULL || EndOpCodeHandle == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = EFI_OUT_OF_RESOURCES; + *StartOpCodeHandle = NULL; + *EndOpCodeHandle = NULL; + + // + // Initialize the container for dynamic opcodes. + // + *StartOpCodeHandle = HiiAllocateOpCodeHandle (); + if (*StartOpCodeHandle == NULL) { + goto Exit; + } + *EndOpCodeHandle = HiiAllocateOpCodeHandle (); + if (*EndOpCodeHandle == NULL) { + goto Exit; + } + + // + // Create Hii Extend Label OpCode as the start opcode. + // + InternalStartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + *StartOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + if (InternalStartLabel == NULL) { + goto Exit; + } + InternalStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + InternalStartLabel->Number = StartLabelNumber; + + // + // Create Hii Extend Label OpCode as the end opcode. + // + InternalEndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode ( + *EndOpCodeHandle, + &gEfiIfrTianoGuid, + NULL, + sizeof (EFI_IFR_GUID_LABEL) + ); + if (InternalEndLabel == NULL) { + goto Exit; + } + InternalEndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL; + InternalEndLabel->Number = LABEL_END; + + return EFI_SUCCESS; + +Exit: + + if (*StartOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (*StartOpCodeHandle); + } + if (*EndOpCodeHandle != NULL) { + HiiFreeOpCodeHandle (*EndOpCodeHandle); + } + return Status; +} + +/** + Display the Nic list contains all available Nics. + + @param[in] Private The pointer to the global private data structure. + + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +WifiMgrShowNicList ( + IN WIFI_MGR_PRIVATE_DATA *Private +) +{ + EFI_STATUS Status; + CHAR16 MacString[WIFI_MGR_MAX_MAC_STRING_LEN]; + CHAR16 PortString[WIFI_STR_MAX_SIZE]; + EFI_STRING_ID PortTitleToken; + EFI_STRING_ID PortTitleHelpToken; + WIFI_MGR_DEVICE_DATA *Nic; + LIST_ENTRY *Entry; + VOID *StartOpCodeHandle; + VOID *EndOpCodeHandle; + + if (Private == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = WifiMgrCreateOpCode ( + LABEL_MAC_ENTRY, + &StartOpCodeHandle, + &EndOpCodeHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + NET_LIST_FOR_EACH (Entry, &Private->NicList) { + Nic = NET_LIST_USER_STRUCT_S (Entry, WIFI_MGR_DEVICE_DATA, Link, WIFI_MGR_DEVICE_DATA_SIGNATURE); + WifiMgrMacAddrToStr (&Nic->MacAddress, sizeof (MacString), MacString); + UnicodeSPrint (PortString, sizeof (PortString), L"MAC %s", MacString); + PortTitleToken = HiiSetString ( + Private->RegisteredHandle, + 0, + PortString, + NULL + ); + if (PortTitleToken == 0) { + Status = EFI_INVALID_PARAMETER; + goto Exit; + } + + UnicodeSPrint (PortString, sizeof (PortString), L"MAC Address"); + PortTitleHelpToken = HiiSetString ( + Private->RegisteredHandle, + 0, + PortString, + NULL + ); + if (PortTitleHelpToken == 0) { + Status = EFI_INVALID_PARAMETER; + goto Exit; + } + + HiiCreateGotoOpCode ( + StartOpCodeHandle, + FORMID_WIFI_MAINPAGE, + PortTitleToken, + PortTitleHelpToken, + EFI_IFR_FLAG_CALLBACK, + (UINT16) (KEY_MAC_ENTRY_BASE + Nic->NicIndex) + ); + } + + Status = HiiUpdateForm ( + Private->RegisteredHandle, // HII handle + &gWifiConfigFormSetGuid, // Formset GUID + FORMID_MAC_SELECTION, // Form ID + StartOpCodeHandle, // Label for where to insert opcodes + EndOpCodeHandle // Replace data + ); + +Exit: + + HiiFreeOpCodeHandle (StartOpCodeHandle); + HiiFreeOpCodeHandle (EndOpCodeHandle); + return Status; +} + +/** + Retreive the unicode string of the AKM Suite list of a profile. + The caller is responsible for freeing the string with FreePool(). + + @param[in] Profile The network profile contains a AKM suite list. + + @return the unicode string of AKM suite list or "None". + +**/ +CHAR16* +WifiMgrGetStrAKMList ( + IN WIFI_MGR_NETWORK_PROFILE *Profile +) +{ + UINT8 Index; + UINT16 AKMSuiteCount; + CHAR16 *AKMListDisplay; + + AKMListDisplay = NULL; + if (Profile == NULL || Profile->Network.AKMSuite == NULL) { + goto Exit; + } + + AKMSuiteCount = Profile->Network.AKMSuite->AKMSuiteCount; + if (AKMSuiteCount != 0) { + + // + // Current AKM Suite is between 1-9 + // + AKMListDisplay = (CHAR16 *) AllocateZeroPool(sizeof (CHAR16) * AKMSuiteCount * 2); + if (AKMListDisplay != NULL) { + for (Index = 0; Index < AKMSuiteCount; Index ++) { + UnicodeSPrint ( + AKMListDisplay + (Index * 2), + sizeof (CHAR16) * 2, + L"%d ", + Profile->Network.AKMSuite->AKMSuiteList[Index].SuiteType + ); + if (Index == AKMSuiteCount - 1) { + *(AKMListDisplay + (Index * 2 + 1)) = L'\0'; + } + } + } + } + +Exit: + + if (AKMListDisplay == NULL) { + AKMListDisplay = AllocateCopyPool (sizeof (L"None"), L"None"); + } + return AKMListDisplay; +} + +/** + Retreive the unicode string of the Cipher Suite list of a profile. + The caller is responsible for freeing the string with FreePool(). + + @param[in] Profile The network profile contains a Cipher suite list. + + @return the unicode string of Cipher suite list or "None". + +**/ +CHAR16* +WifiMgrGetStrCipherList ( + IN WIFI_MGR_NETWORK_PROFILE *Profile +) +{ + UINT8 Index; + UINT16 CipherSuiteCount; + CHAR16 *CipherListDisplay; + + CipherListDisplay = NULL; + if (Profile == NULL || Profile->Network.CipherSuite == NULL) { + goto Exit; + } + + CipherSuiteCount = Profile->Network.CipherSuite->CipherSuiteCount; + if (CipherSuiteCount != 0) { + + // + // Current Cipher Suite is between 1-9 + // + CipherListDisplay = (CHAR16 *) AllocateZeroPool(sizeof (CHAR16) * CipherSuiteCount * 2); + if (CipherListDisplay != NULL) { + for (Index = 0; Index < CipherSuiteCount; Index ++) { + UnicodeSPrint ( + CipherListDisplay + (Index * 2), + sizeof (CHAR16) * 2, + L"%d ", + Profile->Network.CipherSuite->CipherSuiteList[Index].SuiteType + ); + if (Index == CipherSuiteCount - 1) { + *(CipherListDisplay + (Index * 2 + 1)) = L'\0'; + } + } + } + } + +Exit: + + if (CipherListDisplay == NULL) { + CipherListDisplay = AllocateCopyPool (sizeof (L"None"), L"None"); + } + return CipherListDisplay; +} + +/** + Refresh the network list display of the current Nic. + + @param[in] Private The pointer to the global private data structure. + @param[out] IfrNvData The IFR NV data. + + @retval EFI_SUCCESS The operation is completed successfully. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Other Errors Returned errors when creating Opcodes or updating the + Hii form. + +**/ +EFI_STATUS +WifiMgrRefreshNetworkList ( + IN WIFI_MGR_PRIVATE_DATA *Private, + OUT WIFI_MANAGER_IFR_NVDATA *IfrNvData + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + UINT32 AvailableCount; + EFI_STRING_ID PortPromptToken; + EFI_STRING_ID PortTextToken; + EFI_STRING_ID PortHelpToken; + WIFI_MGR_NETWORK_PROFILE *Profile; + LIST_ENTRY *Entry; + VOID *StartOpCodeHandle; + VOID *EndOpCodeHandle; + CHAR16 *AKMListDisplay; + CHAR16 *CipherListDisplay; + CHAR16 PortString[WIFI_STR_MAX_SIZE]; + UINTN PortStringSize; + WIFI_MGR_NETWORK_PROFILE *ConnectedProfile; + + if (Private->CurrentNic == NULL) { + return EFI_SUCCESS; + } + + Status = WifiMgrCreateOpCode ( + LABEL_NETWORK_LIST_ENTRY, + &StartOpCodeHandle, + &EndOpCodeHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + AvailableCount = 0; + PortStringSize = sizeof (PortString); + ConnectedProfile = NULL; + AKMListDisplay = NULL; + CipherListDisplay = NULL; + + if (Private->CurrentNic->ConnectState == WifiMgrConnectedToAp) { + + // + // Display the current connected network. + // Find the current operate network under connected status. + // + if (Private->CurrentNic->CurrentOperateNetwork != NULL && + Private->CurrentNic->CurrentOperateNetwork->IsAvailable) { + + Profile = Private->CurrentNic->CurrentOperateNetwork; + AvailableCount ++; + + AKMListDisplay = WifiMgrGetStrAKMList (Profile); + if (AKMListDisplay == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + CipherListDisplay = WifiMgrGetStrCipherList(Profile); + if (CipherListDisplay == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + UnicodeSPrint (PortString, PortStringSize, L"%s (Connected)", Profile->SSId); + PortPromptToken = HiiSetString (Private->RegisteredHandle, 0, PortString, NULL); + + if (Profile->SecurityType == SECURITY_TYPE_NONE) { + PortHelpToken = 0; + } else { + UnicodeSPrint (PortString, PortStringSize, L"AKMSuite: %s CipherSuite: %s", AKMListDisplay, CipherListDisplay); + PortHelpToken = HiiSetString (Private->RegisteredHandle, 0, PortString, NULL); + } + FreePool (AKMListDisplay); + FreePool (CipherListDisplay); + AKMListDisplay = NULL; + CipherListDisplay = NULL; + + HiiCreateGotoOpCode ( + StartOpCodeHandle, + FORMID_CONNECT_NETWORK, + PortPromptToken, + PortHelpToken, + EFI_IFR_FLAG_CALLBACK, + (UINT16) (KEY_AVAILABLE_NETWORK_ENTRY_BASE + Profile->ProfileIndex) + ); + + UnicodeSPrint ( + PortString, + PortStringSize, + L"%s %s %s", + (Profile->SecurityType != SECURITY_TYPE_NONE ? L"Secured" : L"Open "), + mSecurityType[Profile->SecurityType], + RSSI_TO_SIGNAL_STRENGTH_BAR(Profile->NetworkQuality) + ); + PortTextToken = HiiSetString (Private->RegisteredHandle, 0, PortString, NULL); + + HiiCreateTextOpCode ( + StartOpCodeHandle, + PortTextToken, + 0, + 0 + ); + + ConnectedProfile = Profile; + } else { + Private->CurrentNic->HasDisconnectPendingNetwork = TRUE; + } + } + + // + // Display all supported available networks. + // + NET_LIST_FOR_EACH (Entry, &Private->CurrentNic->ProfileList) { + + Profile = NET_LIST_USER_STRUCT_S ( + Entry, + WIFI_MGR_NETWORK_PROFILE, + Link, + WIFI_MGR_PROFILE_SIGNATURE + ); + if (ConnectedProfile == Profile) { + continue; + } + if (Profile->IsAvailable && Profile->CipherSuiteSupported) { + + AvailableCount ++; + + AKMListDisplay = WifiMgrGetStrAKMList (Profile); + if (AKMListDisplay == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + CipherListDisplay = WifiMgrGetStrCipherList(Profile); + if (CipherListDisplay == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + PortPromptToken = HiiSetString (Private->RegisteredHandle, 0, Profile->SSId, NULL); + if (PortPromptToken == 0) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + if (Profile->SecurityType == SECURITY_TYPE_NONE) { + PortHelpToken = 0; + } else { + UnicodeSPrint ( + PortString, + PortStringSize, + L"AKMSuite: %s CipherSuite: %s", + AKMListDisplay, CipherListDisplay + ); + PortHelpToken = HiiSetString (Private->RegisteredHandle, 0, PortString, NULL); + if (PortHelpToken == 0) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + } + FreePool (AKMListDisplay); + FreePool (CipherListDisplay); + AKMListDisplay = NULL; + CipherListDisplay = NULL; + + HiiCreateGotoOpCode ( + StartOpCodeHandle, + FORMID_CONNECT_NETWORK, + PortPromptToken, + PortHelpToken, + EFI_IFR_FLAG_CALLBACK, + (UINT16) (KEY_AVAILABLE_NETWORK_ENTRY_BASE + Profile->ProfileIndex) + ); + + UnicodeSPrint ( + PortString, + PortStringSize, + L"%s %s %s", + (Profile->SecurityType != SECURITY_TYPE_NONE ? L"Secured" : L"Open "), + mSecurityType[Profile->SecurityType], + RSSI_TO_SIGNAL_STRENGTH_BAR(Profile->NetworkQuality) + ); + PortTextToken = HiiSetString (Private->RegisteredHandle, 0, PortString, NULL); + if (PortTextToken == 0) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + HiiCreateTextOpCode ( + StartOpCodeHandle, + PortTextToken, + 0, + 0 + ); + } + } + + // + // Display all Unsupported available networks. + // + NET_LIST_FOR_EACH (Entry, &Private->CurrentNic->ProfileList) { + + Profile = NET_LIST_USER_STRUCT_S ( + Entry, + WIFI_MGR_NETWORK_PROFILE, + Link, + WIFI_MGR_PROFILE_SIGNATURE + ); + if (ConnectedProfile == Profile) { + continue; + } + if (Profile->IsAvailable && !Profile->CipherSuiteSupported) { + + AvailableCount ++; + + AKMListDisplay = WifiMgrGetStrAKMList (Profile); + if (AKMListDisplay == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + CipherListDisplay = WifiMgrGetStrCipherList(Profile); + if (CipherListDisplay == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + PortPromptToken = HiiSetString (Private->RegisteredHandle, 0, Profile->SSId, NULL); + + if (Profile->AKMSuiteSupported) { + UnicodeSPrint ( + PortString, + PortStringSize, + L"AKMSuite: %s CipherSuite(UnSupported): %s", + AKMListDisplay, CipherListDisplay + ); + } else { + UnicodeSPrint ( + PortString, + PortStringSize, + L"AKMSuite(UnSupported): %s CipherSuite(UnSupported): %s", + AKMListDisplay, CipherListDisplay + ); + } + FreePool (AKMListDisplay); + FreePool (CipherListDisplay); + AKMListDisplay = NULL; + CipherListDisplay = NULL; + + PortHelpToken = HiiSetString (Private->RegisteredHandle, 0, PortString, NULL); + + HiiCreateGotoOpCode ( + StartOpCodeHandle, + FORMID_CONNECT_NETWORK, + PortPromptToken, + PortHelpToken, + EFI_IFR_FLAG_CALLBACK, + (UINT16) (KEY_AVAILABLE_NETWORK_ENTRY_BASE + Profile->ProfileIndex) + ); + + UnicodeSPrint ( + PortString, + PortStringSize, + L"%s %s %s", + L"UnSupported", + mSecurityType[Profile->SecurityType], + RSSI_TO_SIGNAL_STRENGTH_BAR(Profile->NetworkQuality) + ); + PortTextToken = HiiSetString (Private->RegisteredHandle, 0, PortString, NULL); + + HiiCreateTextOpCode ( + StartOpCodeHandle, + PortTextToken, + 0, + 0 + ); + } + } + + Status = HiiUpdateForm ( + Private->RegisteredHandle, // HII handle + &gWifiConfigFormSetGuid, // Formset GUID + FORMID_NETWORK_LIST, // Form ID + StartOpCodeHandle, // Label for where to insert opcodes + EndOpCodeHandle // Replace data + ); + +Exit: + + gBS->RestoreTPL (OldTpl); + + if (AKMListDisplay != NULL) { + FreePool (AKMListDisplay); + } + if (CipherListDisplay != NULL) { + FreePool (CipherListDisplay); + } + + HiiFreeOpCodeHandle (StartOpCodeHandle); + HiiFreeOpCodeHandle (EndOpCodeHandle); + + DEBUG ((DEBUG_INFO, "[WiFi Connection Manager] Network List is Refreshed!\n")); + return Status; +} + +/** + Refresh the hidden network list configured by user. + + @param[in] Private The pointer to the global private data structure. + + @retval EFI_SUCCESS The operation is completed successfully. + @retval Other Errors Returned errors when creating Opcodes or updating the + Hii form. +**/ +EFI_STATUS +WifiMgrRefreshHiddenList ( + IN WIFI_MGR_PRIVATE_DATA *Private + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + UINTN Index; + EFI_STRING_ID StringId; + VOID *StartOpCodeHandle; + VOID *EndOpCodeHandle; + WIFI_HIDDEN_NETWORK_DATA *HiddenNetwork; + LIST_ENTRY *Entry; + + if (Private == NULL) { + return EFI_SUCCESS; + } + + Status = WifiMgrCreateOpCode ( + LABEL_HIDDEN_NETWORK_ENTRY, + &StartOpCodeHandle, + &EndOpCodeHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Index = 0; + + NET_LIST_FOR_EACH (Entry, &Private->HiddenNetworkList) { + + HiddenNetwork = NET_LIST_USER_STRUCT_S ( + Entry, + WIFI_HIDDEN_NETWORK_DATA, + Link, + WIFI_MGR_HIDDEN_NETWORK_SIGNATURE + ); + StringId = HiiSetString (Private->RegisteredHandle, 0, HiddenNetwork->SSId, NULL); + + HiiCreateCheckBoxOpCode ( + StartOpCodeHandle, + (EFI_QUESTION_ID) (KEY_HIDDEN_NETWORK_ENTRY_BASE + Index), + MANAGER_VARSTORE_ID, + (UINT16) (HIDDEN_NETWORK_LIST_VAR_OFFSET + Index), + StringId, + 0, + 0, + 0, + NULL + ); + Index ++; + } + + Status = HiiUpdateForm ( + Private->RegisteredHandle, // HII handle + &gWifiConfigFormSetGuid, // Formset GUID + FORMID_HIDDEN_NETWORK_LIST, // Form ID + StartOpCodeHandle, // Label for where to insert opcodes + EndOpCodeHandle // Replace data + ); + + gBS->RestoreTPL (OldTpl); + HiiFreeOpCodeHandle (StartOpCodeHandle); + HiiFreeOpCodeHandle (EndOpCodeHandle); + return Status; +} + + +/** + Callback function for user to select a Nic. + + @param[in] Private The pointer to the global private data structure. + @param[in] KeyValue The key value received from HII input. + + @retval EFI_NOT_FOUND The corresponding Nic is not found. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +WifiMgrSelectNic ( + IN WIFI_MGR_PRIVATE_DATA *Private, + IN EFI_QUESTION_ID KeyValue + ) +{ + WIFI_MGR_DEVICE_DATA *Nic; + UINT32 NicIndex; + CHAR16 MacString[WIFI_MGR_MAX_MAC_STRING_LEN]; + + NicIndex = KeyValue - KEY_MAC_ENTRY_BASE; + Nic = WifiMgrGetNicByIndex (Private, NicIndex); + if (Nic == NULL) { + return EFI_NOT_FOUND; + } + Private->CurrentNic = Nic; + + WifiMgrMacAddrToStr (&Nic->MacAddress, sizeof (MacString), MacString); + HiiSetString (Private->RegisteredHandle, STRING_TOKEN(STR_MAC_ADDRESS), MacString, NULL); + return EFI_SUCCESS; +} + +/** + Restore the NV data to be default. + + @param[in] Private The pointer to the global private data structure. + @param[out] IfrNvData The IFR NV data. + +**/ +VOID +WifiMgrCleanUserInput ( + IN WIFI_MGR_PRIVATE_DATA *Private + ) +{ + Private->SecurityType = SECURITY_TYPE_NONE; + Private->EapAuthMethod = EAP_AUTH_METHOD_TTLS; + Private->EapSecondAuthMethod = EAP_SEAUTH_METHOD_MSCHAPV2; + Private->FileType = FileTypeMax; +} + +/** + UI handle function when user select a network to connect. + + @param[in] Private The pointer to the global private data structure. + @param[in] ProfileIndex The profile index user selected to connect. + + @retval EFI_INVALID_PARAMETER Nic is null. + @retval EFI_NOT_FOUND Profile could not be found. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +WifiMgrUserSelectProfileToConnect( + IN WIFI_MGR_PRIVATE_DATA *Private, + IN UINT32 ProfileIndex + ) +{ + WIFI_MGR_NETWORK_PROFILE *Profile; + WIFI_MGR_DEVICE_DATA *Nic; + + Nic = Private->CurrentNic; + if (Nic == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + //Initialize the connection page + // + WifiMgrCleanUserInput(Private); + + Profile = WifiMgrGetProfileByProfileIndex (ProfileIndex, &Nic->ProfileList); + if (Profile == NULL) { + return EFI_NOT_FOUND; + } + Private->CurrentNic->UserSelectedProfile = Profile; + + return EFI_SUCCESS; +} + +/** + Record password from a HII input string. + + @param[in] Private The pointer to the global private data structure. + @param[in] StringId The QuestionId received from HII input. + @param[in] StringBuffer The unicode string buffer to store password. + @param[in] StringBufferLen The len of unicode string buffer. + + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_NOT_FOUND The password string is not found or invalid. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +WifiMgrRecordPassword ( + IN WIFI_MGR_PRIVATE_DATA *Private, + IN EFI_STRING_ID StringId, + IN CHAR16 *StringBuffer, + IN UINTN StringBufferLen + ) +{ + CHAR16 *Password; + + if (StringId == 0 || StringBuffer == NULL || StringBufferLen <= 0) { + return EFI_INVALID_PARAMETER; + } + + Password = HiiGetString (Private->RegisteredHandle, StringId, NULL); + if (Password == NULL) { + return EFI_NOT_FOUND; + } + if (StrLen (Password) > StringBufferLen) { + FreePool (Password); + return EFI_NOT_FOUND; + } + StrnCpyS (StringBuffer, StringBufferLen, Password, StrLen (Password)); + ZeroMem (Password, (StrLen (Password) + 1) * sizeof (CHAR16)); + FreePool (Password); + + // + // Clean password in string package + // + HiiSetString (Private->RegisteredHandle, StringId, L"", NULL); + return EFI_SUCCESS; +} + +/** + Update connection message on connect configuration page, and trigger related form refresh. + + @param[in] Nic The related Nic for updating message. + @param[in] ConnectStateChanged The tag to tell if the connection state has been changed, only + when the connection changes from "Connected" or "Disconnecting" + to "Disconnected", or from "Disconnected" or "Connecting" to + "Connected", this tag can be set as TRUE. + @param[in] ConnectStatusMessage The message to show on connected status bar, if NULL, will + use default message. + +**/ +VOID +WifiMgrUpdateConnectMessage ( + IN WIFI_MGR_DEVICE_DATA *Nic, + IN BOOLEAN ConnectStateChanged, + IN EFI_STRING ConnectStatusMessage + ) +{ + CHAR16 ConnectStatusStr[WIFI_STR_MAX_SIZE]; + WIFI_MGR_PRIVATE_DATA *Private; + + Private = Nic->Private; + if (Private == NULL || Private->CurrentNic != Nic) { + return; + } + + // + // Update Connection Status Bar + // + if (ConnectStatusMessage != NULL) { + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_CONNECT_STATUS), ConnectStatusMessage, NULL); + } else { + if (Nic->ConnectState == WifiMgrConnectedToAp) { + + UnicodeSPrint (ConnectStatusStr, sizeof (ConnectStatusStr), L"Connected to %s", + Nic->CurrentOperateNetwork->SSId); + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_CONNECT_STATUS), ConnectStatusStr, NULL); + } else if (Nic->ConnectState == WifiMgrDisconnected) { + + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_CONNECT_STATUS), L"Disconnected", NULL); + } else if (Nic->ConnectState == WifiMgrConnectingToAp) { + + UnicodeSPrint (ConnectStatusStr, sizeof (ConnectStatusStr), L"Connecting to %s ...", + Nic->CurrentOperateNetwork->SSId); + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_CONNECT_STATUS), ConnectStatusStr, NULL); + } else if (Nic->ConnectState == WifiMgrDisconnectingToAp) { + + UnicodeSPrint (ConnectStatusStr, sizeof (ConnectStatusStr), L"Disconnecting from %s ...", + Nic->CurrentOperateNetwork->SSId); + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_CONNECT_STATUS), ConnectStatusStr, NULL); + } else { + return; + } + } + + // + // Update Connect Button + // + if (Nic->ConnectState == WifiMgrConnectedToAp && Nic->UserSelectedProfile == Nic->CurrentOperateNetwork) { + + HiiSetString (Private->RegisteredHandle, + STRING_TOKEN (STR_CONNECT_NOW), L"Disconnect from this Network", NULL); + } else { + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_CONNECT_NOW), L"Connect to this Network", NULL); + } + gBS->SignalEvent (Private->ConnectFormRefreshEvent); + + // + // Update Main Page and Network List + // + if (ConnectStateChanged) { + + if (Nic->ConnectState == WifiMgrConnectedToAp) { + + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_CONNECTION_INFO), L"Connected to", NULL); + HiiSetString (Private->RegisteredHandle, + STRING_TOKEN (STR_CONNECTED_SSID), Nic->CurrentOperateNetwork->SSId, NULL); + } else { + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_CONNECTION_INFO), L"Disconnected", NULL); + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_CONNECTED_SSID), L"", NULL); + } + + gBS->SignalEvent (Private->NetworkListRefreshEvent); + gBS->SignalEvent (Private->MainPageRefreshEvent); + } +} + +/** + Convert the driver configuration data into the IFR data. + + @param[in] Private The pointer to the global private data structure. + @param[out] IfrNvData The IFR NV data. + + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +WifiMgrConvertConfigDataToIfrNvData ( + IN WIFI_MGR_PRIVATE_DATA *Private, + OUT WIFI_MANAGER_IFR_NVDATA *IfrNvData + ) +{ + // + // Private shouldn't be NULL here, assert if Private is NULL. + // + ASSERT (Private != NULL); + + if (Private->CurrentNic != NULL) { + IfrNvData->ProfileCount = Private->CurrentNic->AvailableCount; + } else { + IfrNvData->ProfileCount = 0; + } + + return EFI_SUCCESS; +} + +/** + Convert the IFR data into the driver configuration data. + + @param[in] Private The pointer to the global private data structure. + @param[in, out] IfrNvData The IFR NV data. + + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +WifiMgrConvertIfrNvDataToConfigData ( + IN WIFI_MGR_PRIVATE_DATA *Private, + IN OUT WIFI_MANAGER_IFR_NVDATA *IfrNvData + ) +{ + return EFI_SUCCESS; +} + +/** + This function allows the caller to request the current + configuration for one or more named elements. The resulting + string is in format. Any and all alternative + configuration strings shall also be appended to the end of the + current configuration string. If they are, they must appear + after the current configuration. They must contain the same + routing (GUID, NAME, PATH) as the current configuration string. + They must have an additional description indicating the type of + alternative configuration the string represents, + "ALTCFG=". That (when + converted from Hex UNICODE to binary) is a reference to a + string in the associated string pack. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param Request A null-terminated Unicode string in + format. Note that this + includes the routing information as well as + the configurable name / value pairs. It is + invalid for this string to be in + format. + If a NULL is passed in for the Request field, + all of the settings being abstracted by this function + will be returned in the Results field. In addition, + if a ConfigHdr is passed in with no request elements, + all of the settings being abstracted for that particular + ConfigHdr reference will be returned in the Results Field. + + @param Progress On return, points to a character in the + Request string. Points to the string's null + terminator if request was successful. Points + to the most recent "&" before the first + failing name / value pair (or the beginning + of the string if the failure is in the first + name / value pair) if the request was not + successful. + + @param Results A null-terminated Unicode string in + format which has all values + filled in for the names in the Request string. + String to be allocated by the called function. + + @retval EFI_SUCCESS The Results string is filled with the + values corresponding to all requested + names. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_NOT_FOUND Routing data doesn't match any + known driver. Progress set to the + first character in the routing header. + Note: There is no requirement that the + driver validate the routing data. It + must skip the in order to + process the names. + + @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set + to most recent "&" before the + error or the beginning of the + string. + + @retval EFI_INVALID_PARAMETER Unknown name. Progress points + to the & before the name in + question. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeHiiConfigAccessExtractConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Request, + OUT EFI_STRING *Progress, + OUT EFI_STRING *Results + ) +{ + WIFI_MGR_PRIVATE_DATA *Private; + WIFI_MANAGER_IFR_NVDATA *IfrNvData; + EFI_STRING ConfigRequestHdr; + EFI_STRING ConfigRequest; + UINTN Size; + BOOLEAN AllocatedRequest; + UINTN BufferSize; + EFI_STATUS Status; + + if (This == NULL || Progress == NULL || Results == NULL) { + return EFI_INVALID_PARAMETER; + } + + *Progress = Request; + if ((Request != NULL) && + !HiiIsConfigHdrMatch (Request, &gWifiConfigFormSetGuid, mVendorStorageName)) { + return EFI_NOT_FOUND; + } + + ConfigRequestHdr = NULL; + ConfigRequest = NULL; + AllocatedRequest = FALSE; + Size = 0; + + Private = WIFI_MGR_PRIVATE_DATA_FROM_CONFIG_ACCESS (This); + + BufferSize = sizeof (WIFI_MANAGER_IFR_NVDATA); + IfrNvData = AllocateZeroPool (BufferSize); + if (IfrNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + WifiMgrConvertConfigDataToIfrNvData (Private, IfrNvData); + + ConfigRequest = Request; + if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) { + // + // Request has no request element, construct full request string. + // Allocate and fill a buffer large enough to hold the template + // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator. + // + ConfigRequestHdr = HiiConstructConfigHdr ( + &gWifiConfigFormSetGuid, + mVendorStorageName, + Private->DriverHandle); + if (ConfigRequestHdr == NULL) { + FreePool (IfrNvData); + return EFI_OUT_OF_RESOURCES; + } + + Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16); + ConfigRequest = AllocateZeroPool (Size); + if (ConfigRequest == NULL) { + + FreePool (IfrNvData); + FreePool (ConfigRequestHdr); + return EFI_OUT_OF_RESOURCES; + } + + AllocatedRequest = TRUE; + UnicodeSPrint ( + ConfigRequest, + Size, + L"%s&OFFSET=0&WIDTH=%016LX", + ConfigRequestHdr, + (UINT64) BufferSize + ); + FreePool (ConfigRequestHdr); + } + + // + // Convert buffer data to by helper function BlockToConfig() + // + Status = gHiiConfigRouting->BlockToConfig ( + gHiiConfigRouting, + ConfigRequest, + (UINT8 *) IfrNvData, + BufferSize, + Results, + Progress + ); + + FreePool (IfrNvData); + // + // Free the allocated config request string. + // + if (AllocatedRequest) { + FreePool (ConfigRequest); + ConfigRequest = NULL; + } + // + // Set Progress string to the original request string. + // + if (Request == NULL) { + *Progress = NULL; + } else if (StrStr (Request, L"OFFSET") == NULL) { + *Progress = Request + StrLen (Request); + } + + return Status; +} + +/** + This function applies changes in a driver's configuration. + Input is a Configuration, which has the routing data for this + driver followed by name / value configuration pairs. The driver + must apply those pairs to its configurable storage. If the + driver's configuration is stored in a linear block of data + and the driver's name / value pairs are in + format, it may use the ConfigToBlock helper function (above) to + simplify the job. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param Configuration A null-terminated Unicode string in + format. + + @param Progress A pointer to a string filled in with the + offset of the most recent '&' before the + first failing name / value pair (or the + beginn ing of the string if the failure + is in the first name / value pair) or + the terminating NULL if all was + successful. + + @retval EFI_SUCCESS The results have been distributed or are + awaiting distribution. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_INVALID_PARAMETERS Passing in a NULL for the + Results parameter would result + in this type of error. + + @retval EFI_NOT_FOUND Target for the specified routing data + was not found + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeHiiConfigAccessRouteConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Configuration, + OUT EFI_STRING *Progress + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + WIFI_MGR_PRIVATE_DATA *Private; + WIFI_MANAGER_IFR_NVDATA *IfrNvData; + + if (Configuration == NULL || Progress == NULL) { + return EFI_INVALID_PARAMETER; + } + + IfrNvData = NULL; + *Progress = Configuration; + BufferSize = sizeof (WIFI_MANAGER_IFR_NVDATA); + Private = WIFI_MGR_PRIVATE_DATA_FROM_CONFIG_ACCESS (This); + + if (!HiiIsConfigHdrMatch (Configuration, &gWifiConfigFormSetGuid, mVendorStorageName)) { + return EFI_NOT_FOUND; + } + + IfrNvData = AllocateZeroPool (BufferSize); + if (IfrNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + WifiMgrConvertConfigDataToIfrNvData (Private, IfrNvData); + + Status = gHiiConfigRouting->ConfigToBlock ( + gHiiConfigRouting, + Configuration, + (UINT8*) IfrNvData, + &BufferSize, + Progress + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = WifiMgrConvertIfrNvDataToConfigData (Private, IfrNvData); + ZeroMem (IfrNvData, sizeof (WIFI_MANAGER_IFR_NVDATA)); + FreePool (IfrNvData); + + return Status; +} + +/** + This function is called to provide results data to the driver. + This data consists of a unique key that is used to identify + which data is either being passed back or being asked for. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param Action Specifies the type of action taken by the browser. + @param QuestionId A unique value which is sent to the original + exporting driver so that it can identify the type + of data to expect. The format of the data tends to + vary based on the opcode that generated the callback. + @param Type The type of value for the question. + @param Value A pointer to the data being sent to the original + exporting driver. + @param ActionRequest On return, points to the action requested by the + callback function. + + @retval EFI_SUCCESS The callback successfully handled the action. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the + variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be saved. + @retval EFI_UNSUPPORTED The specified Action is not supported by the + callback. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeHiiConfigAccessCallback ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN EFI_BROWSER_ACTION Action, + IN EFI_QUESTION_ID QuestionId, + IN UINT8 Type, + IN OUT EFI_IFR_TYPE_VALUE *Value, + OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest + ) +{ + EFI_STATUS Status; + EFI_INPUT_KEY Key; + UINTN BufferSize; + WIFI_MGR_PRIVATE_DATA *Private; + WIFI_MANAGER_IFR_NVDATA *IfrNvData; + EFI_DEVICE_PATH_PROTOCOL *FilePath; + WIFI_MGR_NETWORK_PROFILE *Profile; + WIFI_MGR_NETWORK_PROFILE *ProfileToConnect; + WIFI_HIDDEN_NETWORK_DATA *HiddenNetwork; + UINTN TempDataSize; + VOID *TempData; + LIST_ENTRY *Entry; + UINT32 Index; + UINT32 RemoveCount; + CHAR16 *TempPassword; + CHAR16 *ErrorMessage; + + if (Action != EFI_BROWSER_ACTION_FORM_OPEN && + Action != EFI_BROWSER_ACTION_FORM_CLOSE && + Action != EFI_BROWSER_ACTION_CHANGING && + Action != EFI_BROWSER_ACTION_CHANGED && + Action != EFI_BROWSER_ACTION_RETRIEVE) { + + return EFI_UNSUPPORTED; + } + if ((Value == NULL) || (ActionRequest == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Status = EFI_SUCCESS; + Private = WIFI_MGR_PRIVATE_DATA_FROM_CONFIG_ACCESS (This); + if (Private->CurrentNic == NULL) { + return EFI_DEVICE_ERROR; + } + + // + // Retrieve uncommitted data from Browser + // + BufferSize = sizeof (WIFI_MANAGER_IFR_NVDATA); + IfrNvData = AllocateZeroPool (BufferSize); + if (IfrNvData == NULL) { + return EFI_OUT_OF_RESOURCES; + } + HiiGetBrowserData (&gWifiConfigFormSetGuid, mVendorStorageName, BufferSize, (UINT8 *) IfrNvData); + + if (Action == EFI_BROWSER_ACTION_FORM_OPEN) { + switch (QuestionId) { + + case KEY_MAC_LIST: + + Status = WifiMgrShowNicList (Private); + break; + + case KEY_REFRESH_NETWORK_LIST: + + if (Private->CurrentNic->UserSelectedProfile != NULL) { + + Profile = Private->CurrentNic->UserSelectedProfile; + + // + // Erase secrets since user has left Connection Page + // Connection Page may direct to Network List Page or Eap Configuration Page, + // secrets only need to be erased when head to Network List Page + // + WifiMgrCleanProfileSecrets (Profile); + + Private->CurrentNic->UserSelectedProfile = NULL; + } + + break; + + case KEY_CONNECT_ACTION: + + if (Private->CurrentNic->UserSelectedProfile == NULL) { + break; + } + Profile = Private->CurrentNic->UserSelectedProfile; + + // + //Enter the network connection configuration page + //Recovery from restored data + // + if (HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_SSID), Profile->SSId, NULL) == 0) { + return EFI_OUT_OF_RESOURCES; + } + IfrNvData->SecurityType = Profile->SecurityType; + if (HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_SECURITY_TYPE), + mSecurityType[IfrNvData->SecurityType], NULL) == 0) { + return EFI_OUT_OF_RESOURCES; + } + + if (IfrNvData->SecurityType == SECURITY_TYPE_WPA2_ENTERPRISE) { + + IfrNvData->EapAuthMethod = Profile->EapAuthMethod; + IfrNvData->EapSecondAuthMethod = Profile->EapSecondAuthMethod; + StrCpyS (IfrNvData->EapIdentity, EAP_IDENTITY_SIZE, Profile->EapIdentity); + } + + break; + + case KEY_ENROLLED_CERT_NAME: + + if (Private->CurrentNic->UserSelectedProfile == NULL) { + break; + } + Profile = Private->CurrentNic->UserSelectedProfile; + + // + //Enter the key enrollment page + //For TTLS and PEAP, only CA cert needs to be cared + // + if (Private->FileType == FileTypeCACert) { + + if (Profile->CACertData != NULL) { + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME), Profile->CACertName, NULL); + } else { + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME), L"", NULL); + } + } else if (Private->FileType == FileTypeClientCert) { + + if (Profile->ClientCertData != NULL) { + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME), Profile->ClientCertName, NULL); + } else { + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME), L"", NULL); + } + } + break; + + case KEY_ENROLLED_PRIVATE_KEY_NAME: + + if (Private->CurrentNic->UserSelectedProfile == NULL) { + break; + } + Profile = Private->CurrentNic->UserSelectedProfile; + + if (Profile->PrivateKeyData != NULL) { + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME), Profile->PrivateKeyName, NULL); + } else { + HiiSetString (Private->RegisteredHandle, STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME), L"", NULL); + } + break; + + default: + break; + } + } else if (Action == EFI_BROWSER_ACTION_FORM_CLOSE) { + switch (QuestionId) { + + case KEY_CONNECT_ACTION: + + if (Private->CurrentNic->UserSelectedProfile == NULL) { + break; + } + Profile = Private->CurrentNic->UserSelectedProfile; + + // + //Restore User Config Data for Page recovery + // + if (IfrNvData->SecurityType == SECURITY_TYPE_WPA2_ENTERPRISE) { + + Profile->EapAuthMethod = IfrNvData->EapAuthMethod; + Profile->EapSecondAuthMethod = IfrNvData->EapSecondAuthMethod; + StrCpyS (Profile->EapIdentity, EAP_IDENTITY_SIZE, IfrNvData->EapIdentity); + } + break; + + default: + break; + } + } else if (Action == EFI_BROWSER_ACTION_CHANGING) { + switch (QuestionId) { + + case KEY_NETWORK_LIST: + + // + //User triggered a scan process. + // + Private->CurrentNic->OneTimeScanRequest = TRUE; + break; + + case KEY_PASSWORD_CONNECT_NETWORK: + case KEY_EAP_PASSWORD_CONNECT_NETWORK: + case KEY_PRIVATE_KEY_PASSWORD: + + if (Private->CurrentNic->UserSelectedProfile == NULL) { + break; + } + Profile = Private->CurrentNic->UserSelectedProfile; + + if (QuestionId == KEY_PASSWORD_CONNECT_NETWORK) { + TempPassword = Profile->Password; + } else if (QuestionId == KEY_EAP_PASSWORD_CONNECT_NETWORK) { + TempPassword = Profile->EapPassword; + } else { + TempPassword = Profile->PrivateKeyPassword; + } + + Status = WifiMgrRecordPassword (Private, Value->string, TempPassword, PASSWORD_STORAGE_SIZE); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "[WiFi Connection Manager] Error: Failed to input password!")); + break; + } + + // + // This password is not a new created password, so no need to confirm. + // + Status = EFI_NOT_FOUND; + break; + + case KEY_CONNECT_ACTION: + + ErrorMessage = NULL; + ProfileToConnect = NULL; + + if (Private->CurrentNic->UserSelectedProfile == NULL) { + break; + } + Profile = Private->CurrentNic->UserSelectedProfile; + + if (Private->CurrentNic->ConnectState == WifiMgrDisconnected || + Profile != Private->CurrentNic->CurrentOperateNetwork) { + + // + // When this network is not currently connected, pend it to connect. + // + if (Profile->AKMSuiteSupported && Profile->CipherSuiteSupported) { + + if (Profile->SecurityType == SECURITY_TYPE_NONE || Profile->SecurityType == SECURITY_TYPE_WPA2_PERSONAL) { + + // + // For Open network, connect directly. + // + ProfileToConnect = Profile; + + } else if (Profile->SecurityType == SECURITY_TYPE_WPA2_ENTERPRISE) { + + // + // For WPA/WPA2-Enterprise network, conduct eap configuration first. + // Only EAP-TLS, TTLS and PEAP is supported now! + // + Profile->EapAuthMethod = IfrNvData->EapAuthMethod; + StrCpyS (Profile->EapIdentity, EAP_IDENTITY_SIZE, IfrNvData->EapIdentity); + + if (IfrNvData->EapAuthMethod == EAP_AUTH_METHOD_TTLS || IfrNvData->EapAuthMethod == EAP_AUTH_METHOD_PEAP) { + + Profile->EapSecondAuthMethod = IfrNvData->EapSecondAuthMethod; + ProfileToConnect = Profile; + } else if (IfrNvData->EapAuthMethod == EAP_AUTH_METHOD_TLS) { + ProfileToConnect = Profile; + } else { + ErrorMessage = L"ERROR: Only EAP-TLS, TTLS or PEAP is supported now!"; + } + } else { + ErrorMessage = L"ERROR: Can't connect to this network!"; + } + } else { + ErrorMessage = L"ERROR: This network is not supported!"; + } + + if (ErrorMessage != NULL) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + ErrorMessage, + NULL + ); + } + + if (ProfileToConnect != NULL) { + + Private->CurrentNic->OneTimeConnectRequest = TRUE; + Private->CurrentNic->ConnectPendingNetwork = ProfileToConnect; + } + } else if (Private->CurrentNic->ConnectState == WifiMgrConnectedToAp) { + + // + // This network is currently connected, just disconnect from it. + // + Private->CurrentNic->OneTimeDisconnectRequest = TRUE; + Private->CurrentNic->HasDisconnectPendingNetwork = TRUE; + } + break; + + case KEY_ENROLL_CA_CERT_CONNECT_NETWORK: + + Private->FileType = FileTypeCACert; + break; + + case KEY_ENROLL_CLIENT_CERT_CONNECT_NETWORK: + + Private->FileType = FileTypeClientCert; + break; + + case KEY_EAP_ENROLL_PRIVATE_KEY_FROM_FILE: + + FilePath = NULL; + ChooseFile (NULL, NULL, NULL, &FilePath); + + if (FilePath != NULL) { + + UpdatePrivateKeyFromFile(Private, FilePath); + FreePool (FilePath); + } + break; + + case KEY_EAP_ENROLL_CERT_FROM_FILE: + + // + //User will select a cert file from File Explore + // + FilePath = NULL; + ChooseFile( NULL, NULL, NULL, &FilePath); + + if (FilePath != NULL) { + + UpdateCAFromFile(Private, FilePath); + FreePool (FilePath); + } + break; + + case KEY_SAVE_PRIVATE_KEY_TO_MEM: + + if (Private->FileContext != NULL && Private->FileContext->FHandle != NULL && + Private->CurrentNic->UserSelectedProfile != NULL) { + + // + // Read Private Key file to Buffer + // + Profile = Private->CurrentNic->UserSelectedProfile; + if (Profile->PrivateKeyData != NULL) { + + ZeroMem (Profile->PrivateKeyData, Profile->PrivateKeyDataSize); + FreePool (Profile->PrivateKeyData); + Profile->PrivateKeyData = NULL; + } + + Status = WifiMgrReadFileToBuffer ( + Private->FileContext, + &TempData, + &TempDataSize + ); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"ERROR: Can't read this private key file!", + NULL + ); + } else { + + ASSERT (Private->FileContext->FileName != NULL); + + Profile->PrivateKeyData = TempData; + Profile->PrivateKeyDataSize = TempDataSize; + StrCpyS(Profile->PrivateKeyName, WIFI_FILENAME_STR_MAX_SIZE, Private->FileContext->FileName); + + DEBUG ((DEBUG_INFO, "[WiFi Connection Manager] Private Key: %s has been enrolled! Size: %d\n", + Profile->PrivateKeyName, Profile->PrivateKeyDataSize)); + } + } + break; + + case KEY_SAVE_CERT_TO_MEM: + + if (Private->FileContext != NULL && Private->FileContext->FHandle != NULL && + Private->CurrentNic->UserSelectedProfile != NULL) { + + // + // Read Cert file to Buffer + // + Profile = Private->CurrentNic->UserSelectedProfile; + + if (Private->FileType == FileTypeCACert) { + if (Profile->CACertData != NULL) { + + ZeroMem (Profile->CACertData, Profile->CACertSize); + FreePool (Profile->CACertData); + Profile->CACertData = NULL; + } + } else if (Private->FileType == FileTypeClientCert) { + if (Profile->ClientCertData != NULL) { + + ZeroMem (Profile->ClientCertData, Profile->ClientCertSize); + FreePool (Profile->ClientCertData); + Profile->ClientCertData = NULL; + } + } else { + break; + } + + Status = WifiMgrReadFileToBuffer ( + Private->FileContext, + &TempData, + &TempDataSize + ); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"ERROR: Can't read this certificate file!", + NULL + ); + } else { + + ASSERT (Private->FileContext->FileName != NULL); + if (Private->FileType == FileTypeCACert) { + + Profile->CACertData = TempData; + Profile->CACertSize = TempDataSize; + StrCpyS(Profile->CACertName, WIFI_FILENAME_STR_MAX_SIZE, Private->FileContext->FileName); + DEBUG ((DEBUG_INFO, "[WiFi Connection Manager] CA Cert: %s has been enrolled! Size: %d\n", + Profile->CACertName, Profile->CACertSize)); + } else { + + Profile->ClientCertData = TempData; + Profile->ClientCertSize = TempDataSize; + StrCpyS(Profile->ClientCertName, WIFI_FILENAME_STR_MAX_SIZE, Private->FileContext->FileName); + DEBUG ((DEBUG_INFO, "[WiFi Connection Manager] Client Cert: %s has been enrolled! Size: %d\n", + Profile->ClientCertName, Profile->ClientCertSize)); + } + } + } + break; + + case KEY_ADD_HIDDEN_NETWORK: + + // + // Add a Hidden Network + // + if (StrLen (IfrNvData->SSId) < SSID_MIN_LEN || + Private->HiddenNetworkCount >= HIDDEN_NETWORK_LIST_COUNT_MAX) { + + Status = EFI_ABORTED; + break; + } else { + + // + // Check if this SSId is already in Hidden Network List + // + NET_LIST_FOR_EACH (Entry, &Private->HiddenNetworkList) { + + HiddenNetwork = NET_LIST_USER_STRUCT_S (Entry, WIFI_HIDDEN_NETWORK_DATA, + Link, WIFI_MGR_HIDDEN_NETWORK_SIGNATURE); + if (StrCmp (HiddenNetwork->SSId, IfrNvData->SSId) == 0) { + + Status = EFI_ABORTED; + break; + } + } + } + + HiddenNetwork = (WIFI_HIDDEN_NETWORK_DATA *) AllocateZeroPool (sizeof (WIFI_HIDDEN_NETWORK_DATA)); + if (HiddenNetwork == NULL) { + + Status = EFI_OUT_OF_RESOURCES; + break; + } + HiddenNetwork->Signature = WIFI_MGR_HIDDEN_NETWORK_SIGNATURE; + StrCpyS (HiddenNetwork->SSId, SSID_STORAGE_SIZE, IfrNvData->SSId); + + InsertTailList (&Private->HiddenNetworkList, &HiddenNetwork->Link); + Private->HiddenNetworkCount ++; + + WifiMgrRefreshHiddenList (Private); + break; + + case KEY_REMOVE_HIDDEN_NETWORK: + + // + // Remove Hidden Networks + // + Entry = GetFirstNode (&Private->HiddenNetworkList); + RemoveCount = 0; + for (Index = 0; Index < Private->HiddenNetworkCount; Index ++) { + if (IfrNvData->HiddenNetworkList[Index] != 0) { + + HiddenNetwork = NET_LIST_USER_STRUCT_S (Entry, WIFI_HIDDEN_NETWORK_DATA, Link, WIFI_MGR_HIDDEN_NETWORK_SIGNATURE); + Entry = RemoveEntryList (Entry); + RemoveCount ++; + + FreePool (HiddenNetwork); + } else { + Entry = GetNextNode (&Private->HiddenNetworkList, Entry); + } + } + + Private->HiddenNetworkCount -= RemoveCount; + WifiMgrRefreshHiddenList (Private); + break; + + default: + + if (QuestionId >= KEY_MAC_ENTRY_BASE && QuestionId < KEY_MAC_ENTRY_BASE + Private->NicCount) { + // + // User selects a wireless NIC. + // + Status = WifiMgrSelectNic (Private, QuestionId); + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"ERROR: Fail to operate the wireless NIC!", + NULL + ); + } + } else if (Private->CurrentNic != NULL) { + if (QuestionId >= KEY_AVAILABLE_NETWORK_ENTRY_BASE && + QuestionId <= KEY_AVAILABLE_NETWORK_ENTRY_BASE + Private->CurrentNic->MaxProfileIndex) { + + Status = WifiMgrUserSelectProfileToConnect (Private, QuestionId - KEY_AVAILABLE_NETWORK_ENTRY_BASE); + if (!EFI_ERROR (Status)) { + WifiMgrUpdateConnectMessage(Private->CurrentNic, FALSE, NULL); + } + } + + if (EFI_ERROR (Status)) { + CreatePopUp ( + EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, + &Key, + L"ERROR: Fail to operate this profile!", + NULL + ); + } + } + + break; + } + } else if (Action == EFI_BROWSER_ACTION_CHANGED) { + switch (QuestionId) { + + case KEY_SAVE_CERT_TO_MEM: + case KEY_SAVE_PRIVATE_KEY_TO_MEM: + + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT; + break; + + case KEY_NO_SAVE_CERT_TO_MEM: + case KEY_NO_SAVE_PRIVATE_KEY_TO_MEM: + + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT; + break; + + default: + + *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY; + break; + } + } else if (Action == EFI_BROWSER_ACTION_RETRIEVE) { + + switch (QuestionId) { + + case KEY_REFRESH_NETWORK_LIST: + + WifiMgrRefreshNetworkList (Private, IfrNvData); + break; + + default: + break; + } + } + + if (!EFI_ERROR (Status)) { + // + // Pass changed uncommitted data back to Form Browser. + // + BufferSize = sizeof (WIFI_MANAGER_IFR_NVDATA); + HiiSetBrowserData (&gWifiConfigFormSetGuid, mVendorStorageName, BufferSize, (UINT8 *) IfrNvData, NULL); + } + + ZeroMem (IfrNvData, sizeof (WIFI_MANAGER_IFR_NVDATA)); + FreePool (IfrNvData); + return Status; +} + +/** + Initialize the WiFi configuration form. + + @param[in] Private The pointer to the global private data structure. + + @retval EFI_SUCCESS The configuration form is initialized. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval Other Erros Returned Errors when installing protocols. + +**/ +EFI_STATUS +WifiMgrDxeConfigFormInit ( + WIFI_MGR_PRIVATE_DATA *Private +) +{ + EFI_STATUS Status; + + if (Private == NULL) { + return EFI_INVALID_PARAMETER; + } + + Private->ConfigAccess.ExtractConfig = WifiMgrDxeHiiConfigAccessExtractConfig; + Private->ConfigAccess.RouteConfig = WifiMgrDxeHiiConfigAccessRouteConfig; + Private->ConfigAccess.Callback = WifiMgrDxeHiiConfigAccessCallback; + + // + // Install Device Path Protocol and Config Access protocol to driver handle. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &Private->DriverHandle, + &gEfiDevicePathProtocolGuid, + &mWifiMgrDxeHiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &Private->ConfigAccess, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Publish our HII data. + // + Private->RegisteredHandle = HiiAddPackages ( + &gWifiConfigFormSetGuid, + Private->DriverHandle, + WifiConnectionManagerDxeStrings, + WifiConnectionManagerDxeBin, + NULL + ); + if (Private->RegisteredHandle == NULL) { + gBS->UninstallMultipleProtocolInterfaces ( + Private->DriverHandle, + &gEfiDevicePathProtocolGuid, + &mWifiMgrDxeHiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &Private->ConfigAccess, + NULL + ); + return EFI_OUT_OF_RESOURCES; + } + + Private->FileContext = AllocateZeroPool (sizeof (WIFI_MGR_FILE_CONTEXT)); + if (Private->FileContext == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + return EFI_SUCCESS; +} + +/** + Unload the WiFi configuration form. + + @param[in] Private The pointer to the global private data structure. + + @retval EFI_SUCCESS The configuration form is unloaded successfully. + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval Other Errors Returned Erros when uninstalling protocols. + +**/ +EFI_STATUS +WifiMgrDxeConfigFormUnload ( + WIFI_MGR_PRIVATE_DATA *Private +) +{ + EFI_STATUS Status; + + if (Private == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (Private->FileContext != NULL) { + + if (Private->FileContext->FHandle != NULL) { + Private->FileContext->FHandle->Close (Private->FileContext->FHandle); + } + + if (Private->FileContext->FileName != NULL) { + FreePool (Private->FileContext->FileName); + } + FreePool (Private->FileContext); + } + + HiiRemovePackages(Private->RegisteredHandle); + + Status = gBS->UninstallMultipleProtocolInterfaces ( + Private->DriverHandle, + &gEfiDevicePathProtocolGuid, + &mWifiMgrDxeHiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &Private->ConfigAccess, + NULL + ); + + return Status; +} diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrHiiConfigAccess.h b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrHiiConfigAccess.h new file mode 100644 index 000000000..ebb18e1a9 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrHiiConfigAccess.h @@ -0,0 +1,241 @@ +/** @file + The Hii functions for WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_WIFI_MGR_HII_CONFIG_ACCESS__ +#define __EFI_WIFI_MGR_HII_CONFIG_ACCESS__ + +/** + Update connection message on connect configuration page, and trigger related form refresh. + + @param[in] Nic The related Nic for updating message. + @param[in] ConnectStateChanged The tag to tell if the connection state has been changed, only + when the connection changes from "Connected" or "Disconnecting" + to "Disconnected", or from "Disconnected" or "Connecting" to + "Connected", this tag can be set as TRUE. + @param[in] ConnectStatusMessage The message to show on connected status bar, if NULL, will + use default message. + +**/ +VOID +WifiMgrUpdateConnectMessage ( + IN WIFI_MGR_DEVICE_DATA *Nic, + IN BOOLEAN ConnectStateChanged, + IN EFI_STRING ConnectStatusMessage + ); + +/** + This function allows the caller to request the current + configuration for one or more named elements. The resulting + string is in format. Any and all alternative + configuration strings shall also be appended to the end of the + current configuration string. If they are, they must appear + after the current configuration. They must contain the same + routing (GUID, NAME, PATH) as the current configuration string. + They must have an additional description indicating the type of + alternative configuration the string represents, + "ALTCFG=". That (when + converted from Hex UNICODE to binary) is a reference to a + string in the associated string pack. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param Request A null-terminated Unicode string in + format. Note that this + includes the routing information as well as + the configurable name / value pairs. It is + invalid for this string to be in + format. + If a NULL is passed in for the Request field, + all of the settings being abstracted by this function + will be returned in the Results field. In addition, + if a ConfigHdr is passed in with no request elements, + all of the settings being abstracted for that particular + ConfigHdr reference will be returned in the Results Field. + + @param Progress On return, points to a character in the + Request string. Points to the string's null + terminator if request was successful. Points + to the most recent "&" before the first + failing name / value pair (or the beginning + of the string if the failure is in the first + name / value pair) if the request was not + successful. + + @param Results A null-terminated Unicode string in + format which has all values + filled in for the names in the Request string. + String to be allocated by the called function. + + @retval EFI_SUCCESS The Results string is filled with the + values corresponding to all requested + names. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_NOT_FOUND Routing data doesn't match any + known driver. Progress set to the + first character in the routing header. + Note: There is no requirement that the + driver validate the routing data. It + must skip the in order to + process the names. + + @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set + to most recent "&" before the + error or the beginning of the + string. + + @retval EFI_INVALID_PARAMETER Unknown name. Progress points + to the & before the name in + question. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeHiiConfigAccessExtractConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Request, + OUT EFI_STRING *Progress, + OUT EFI_STRING *Results + ); + +/** + This function applies changes in a driver's configuration. + Input is a Configuration, which has the routing data for this + driver followed by name / value configuration pairs. The driver + must apply those pairs to its configurable storage. If the + driver's configuration is stored in a linear block of data + and the driver's name / value pairs are in + format, it may use the ConfigToBlock helper function (above) to + simplify the job. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + + @param Configuration A null-terminated Unicode string in + format. + + @param Progress A pointer to a string filled in with the + offset of the most recent '&' before the + first failing name / value pair (or the + beginn ing of the string if the failure + is in the first name / value pair) or + the terminating NULL if all was + successful. + + @retval EFI_SUCCESS The results have been distributed or are + awaiting distribution. + + @retval EFI_OUT_OF_RESOURCES Not enough memory to store the + parts of the results that must be + stored awaiting possible future + protocols. + + @retval EFI_INVALID_PARAMETERS Passing in a NULL for the + Results parameter would result + in this type of error. + + @retval EFI_NOT_FOUND Target for the specified routing data + was not found + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeHiiConfigAccessRouteConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Configuration, + OUT EFI_STRING *Progress + ); + +/** + This function is called to provide results data to the driver. + This data consists of a unique key that is used to identify + which data is either being passed back or being asked for. + + @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL. + @param Action Specifies the type of action taken by the browser. + @param QuestionId A unique value which is sent to the original + exporting driver so that it can identify the type + of data to expect. The format of the data tends to + vary based on the opcode that generated the callback. + @param Type The type of value for the question. + @param Value A pointer to the data being sent to the original + exporting driver. + @param ActionRequest On return, points to the action requested by the + callback function. + + @retval EFI_SUCCESS The callback successfully handled the action. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the + variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be saved. + @retval EFI_UNSUPPORTED The specified Action is not supported by the + callback. + +**/ +EFI_STATUS +EFIAPI +WifiMgrDxeHiiConfigAccessCallback ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN EFI_BROWSER_ACTION Action, + IN EFI_QUESTION_ID QuestionId, + IN UINT8 Type, + IN OUT EFI_IFR_TYPE_VALUE *Value, + OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest + ); + +/** + Initialize the WiFi configuration form. + + @param[in] Private The pointer to the global private data structure. + + @retval EFI_SUCCESS The configuration form is initialized. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Other Erros Returned errors when installing protocols. + +**/ +EFI_STATUS +WifiMgrDxeConfigFormInit ( + WIFI_MGR_PRIVATE_DATA *Private + ); + +/** + Unload the WiFi configuration form. + + @param[in] Private The pointer to the global private data structure. + + @retval EFI_SUCCESS The configuration form is unloaded successfully. + @retval Other Errors Returned Erros when uninstalling protocols. + +**/ +EFI_STATUS +WifiMgrDxeConfigFormUnload ( + WIFI_MGR_PRIVATE_DATA *Private + ); + +/** + Refresh the network list display of the current Nic. + + @param[in] Private The pointer to the global private data structure. + @param[out] IfrNvData The IFR NV data. + + @retval EFI_SUCCESS The operation is completed successfully. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Other Errors Returned errors when creating Opcodes or updating the + Hii form. + +**/ +EFI_STATUS +WifiMgrRefreshNetworkList ( + IN WIFI_MGR_PRIVATE_DATA *Private, + OUT WIFI_MANAGER_IFR_NVDATA *IfrNvData + ); + +#endif diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrImpl.c b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrImpl.c new file mode 100644 index 000000000..2fbd3ebad --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrImpl.c @@ -0,0 +1,1285 @@ +/** @file + The Mac Connection2 Protocol adapter functions for WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "WifiConnectionMgrDxe.h" + +EFI_EAP_TYPE mEapAuthMethod[] = { + EFI_EAP_TYPE_TTLS, + EFI_EAP_TYPE_PEAP, + EFI_EAP_TYPE_EAPTLS +}; + +EFI_EAP_TYPE mEapSecondAuthMethod[] = { + EFI_EAP_TYPE_MSCHAPV2 +}; + +/** + The callback function for scan operation. This function updates networks + according to the latest scan result, and trigger UI refresh. + + ASSERT when errors occur in config token. + + @param[in] Event The GetNetworks token receive event. + @param[in] Context The context of the GetNetworks token. + +**/ +VOID +EFIAPI +WifiMgrOnScanFinished ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + WIFI_MGR_MAC_CONFIG_TOKEN *ConfigToken; + WIFI_MGR_DEVICE_DATA *Nic; + WIFI_MGR_NETWORK_PROFILE *Profile; + EFI_80211_NETWORK *Network; + UINTN DataSize; + EFI_80211_NETWORK_DESCRIPTION *NetworkDescription; + EFI_80211_GET_NETWORKS_RESULT *Result; + LIST_ENTRY *Entry; + UINT8 SecurityType; + BOOLEAN AKMSuiteSupported; + BOOLEAN CipherSuiteSupported; + CHAR8 *AsciiSSId; + UINTN Index; + + ASSERT (Context != NULL); + + ConfigToken = (WIFI_MGR_MAC_CONFIG_TOKEN *) Context; + ASSERT (ConfigToken->Nic != NULL); + ASSERT (ConfigToken->Type == TokenTypeGetNetworksToken); + + // + // It is the GetNetworks token, set scan state to "ScanFinished" + // + ConfigToken->Nic->ScanState = WifiMgrScanFinished; + + ASSERT (ConfigToken->Token.GetNetworksToken != NULL); + Result = ConfigToken->Token.GetNetworksToken->Result; + Nic = ConfigToken->Nic; + + // + // Clean previous result, and update network list according to the scan result + // + Nic->AvailableCount = 0; + + NET_LIST_FOR_EACH (Entry, &Nic->ProfileList) { + Profile = NET_LIST_USER_STRUCT_S (Entry, WIFI_MGR_NETWORK_PROFILE, + Link, WIFI_MGR_PROFILE_SIGNATURE); + Profile->IsAvailable = FALSE; + } + + if (Result == NULL) { + gBS->SignalEvent (Nic->Private->NetworkListRefreshEvent); + WifiMgrFreeToken(ConfigToken); + return; + } + + for (Index = 0; Index < Result->NumOfNetworkDesc; Index ++) { + + NetworkDescription = Result->NetworkDesc + Index; + if (NetworkDescription == NULL) { + continue; + } + + Network = &NetworkDescription->Network; + if (Network == NULL || Network->SSId.SSIdLen == 0) { + continue; + } + + Status = WifiMgrCheckRSN ( + Network->AKMSuite, + Network->CipherSuite, + Nic, + &SecurityType, + &AKMSuiteSupported, + &CipherSuiteSupported + ); + if (EFI_ERROR (Status)) { + + SecurityType = SECURITY_TYPE_UNKNOWN; + AKMSuiteSupported = FALSE; + CipherSuiteSupported = FALSE; + } + + AsciiSSId = (CHAR8*) AllocateZeroPool(sizeof (CHAR8) * (Network->SSId.SSIdLen + 1)); + if (AsciiSSId == NULL) { + continue; + } + CopyMem(AsciiSSId, (CHAR8 *) Network->SSId.SSId, sizeof (CHAR8) * Network->SSId.SSIdLen); + *(AsciiSSId + Network->SSId.SSIdLen) = '\0'; + + Profile = WifiMgrGetProfileByAsciiSSId (AsciiSSId, SecurityType, &Nic->ProfileList); + if (Profile == NULL) { + + if (Nic->MaxProfileIndex >= NETWORK_LIST_COUNT_MAX) { + FreePool (AsciiSSId); + continue; + } + + // + // Create a new profile + // + Profile = AllocateZeroPool (sizeof (WIFI_MGR_NETWORK_PROFILE)); + if (Profile == NULL) { + FreePool (AsciiSSId); + continue; + } + Profile->Signature = WIFI_MGR_PROFILE_SIGNATURE; + Profile->NicIndex = Nic->NicIndex; + Profile->ProfileIndex = Nic->MaxProfileIndex + 1; + AsciiStrToUnicodeStrS (AsciiSSId, Profile->SSId, SSID_STORAGE_SIZE); + InsertTailList (&Nic->ProfileList, &Profile->Link); + Nic->MaxProfileIndex ++; + } + FreePool (AsciiSSId); + + // + //May receive duplicate networks in scan results, check if it has already + //been processed. + // + if (!Profile->IsAvailable) { + + Profile->IsAvailable = TRUE; + Profile->SecurityType = SecurityType; + Profile->AKMSuiteSupported = AKMSuiteSupported; + Profile->CipherSuiteSupported = CipherSuiteSupported; + Profile->NetworkQuality = NetworkDescription->NetworkQuality; + Nic->AvailableCount ++; + + // + //Copy BSSType and SSId + // + CopyMem(&Profile->Network, Network, sizeof (EFI_80211_NETWORK)); + + // + //Copy AKMSuite list + // + if (Network->AKMSuite != NULL) { + + if (Network->AKMSuite->AKMSuiteCount == 0) { + DataSize = sizeof (EFI_80211_AKM_SUITE_SELECTOR); + } else { + DataSize = sizeof (EFI_80211_AKM_SUITE_SELECTOR) + sizeof (EFI_80211_SUITE_SELECTOR) + * (Network->AKMSuite->AKMSuiteCount - 1); + } + Profile->Network.AKMSuite = (EFI_80211_AKM_SUITE_SELECTOR *) AllocateZeroPool (DataSize); + if (Profile->Network.AKMSuite == NULL) { + continue; + } + CopyMem (Profile->Network.AKMSuite, Network->AKMSuite, DataSize); + } + + // + //Copy CipherSuite list + // + if (Network->CipherSuite != NULL) { + + if (Network->CipherSuite->CipherSuiteCount == 0) { + DataSize = sizeof (EFI_80211_CIPHER_SUITE_SELECTOR); + } else { + DataSize = sizeof (EFI_80211_CIPHER_SUITE_SELECTOR) + sizeof (EFI_80211_SUITE_SELECTOR) + * (Network->CipherSuite->CipherSuiteCount - 1); + } + Profile->Network.CipherSuite = (EFI_80211_CIPHER_SUITE_SELECTOR *) AllocateZeroPool (DataSize); + if (Profile->Network.CipherSuite == NULL) { + continue; + } + CopyMem (Profile->Network.CipherSuite, Network->CipherSuite, DataSize); + } + } else { + // + // A duplicate network, update signal quality + // + if (Profile->NetworkQuality < NetworkDescription->NetworkQuality) { + Profile->NetworkQuality = NetworkDescription->NetworkQuality; + } + continue; + } + } + + gBS->SignalEvent (Nic->Private->NetworkListRefreshEvent); + + // + // The current connected network should always be available until disconnection + // happens in Wifi FW layer, even when it is not in this time's scan result. + // + if (Nic->ConnectState == WifiMgrConnectedToAp && Nic->CurrentOperateNetwork != NULL) { + if (!Nic->CurrentOperateNetwork->IsAvailable) { + Nic->CurrentOperateNetwork->IsAvailable = TRUE; + Nic->AvailableCount ++; + } + } + + WifiMgrFreeToken(ConfigToken); +} + +/** + Start scan operation, and send out a token to collect available networks. + + @param[in] Nic Pointer to the device data of the selected NIC. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_ALREADY_STARTED A former scan operation is already ongoing. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Other Errors Return errors when getting networks from low layer. + +**/ +EFI_STATUS +WifiMgrStartScan ( + IN WIFI_MGR_DEVICE_DATA *Nic + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + WIFI_MGR_MAC_CONFIG_TOKEN *ConfigToken; + EFI_80211_GET_NETWORKS_TOKEN *GetNetworksToken; + UINT32 HiddenSSIdIndex; + UINT32 HiddenSSIdCount; + EFI_80211_SSID *HiddenSSIdList; + WIFI_HIDDEN_NETWORK_DATA *HiddenNetwork; + LIST_ENTRY *Entry; + + if (Nic == NULL || Nic->Wmp == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (Nic->ScanState == WifiMgrScanning) { + return EFI_ALREADY_STARTED; + } + + Nic->ScanState = WifiMgrScanning; + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Status = EFI_SUCCESS; + HiddenSSIdList = NULL; + HiddenSSIdCount = Nic->Private->HiddenNetworkCount; + HiddenSSIdIndex = 0; + + // + //create a new get network token + // + ConfigToken = AllocateZeroPool (sizeof (WIFI_MGR_MAC_CONFIG_TOKEN)); + if (ConfigToken == NULL) { + gBS->RestoreTPL (OldTpl); + return EFI_OUT_OF_RESOURCES; + } + + ConfigToken->Type = TokenTypeGetNetworksToken; + ConfigToken->Nic = Nic; + ConfigToken->Token.GetNetworksToken = AllocateZeroPool (sizeof (EFI_80211_GET_NETWORKS_TOKEN)); + if (ConfigToken->Token.GetNetworksToken == NULL) { + WifiMgrFreeToken(ConfigToken); + gBS->RestoreTPL (OldTpl); + return EFI_OUT_OF_RESOURCES; + } + GetNetworksToken = ConfigToken->Token.GetNetworksToken; + + // + // There are some hidden networks to scan, add them into scan list + // + if (HiddenSSIdCount > 0) { + HiddenSSIdList = AllocateZeroPool(HiddenSSIdCount * sizeof (EFI_80211_SSID)); + if (HiddenSSIdList == NULL) { + WifiMgrFreeToken(ConfigToken); + gBS->RestoreTPL (OldTpl); + return EFI_OUT_OF_RESOURCES; + } + + HiddenSSIdIndex = 0; + NET_LIST_FOR_EACH (Entry, &Nic->Private->HiddenNetworkList) { + + HiddenNetwork = NET_LIST_USER_STRUCT_S (Entry, WIFI_HIDDEN_NETWORK_DATA, + Link, WIFI_MGR_HIDDEN_NETWORK_SIGNATURE); + HiddenSSIdList[HiddenSSIdIndex].SSIdLen = (UINT8) StrLen (HiddenNetwork->SSId); + UnicodeStrToAsciiStrS(HiddenNetwork->SSId, + (CHAR8 *) HiddenSSIdList[HiddenSSIdIndex].SSId, SSID_STORAGE_SIZE); + HiddenSSIdIndex ++; + } + GetNetworksToken->Data = AllocateZeroPool (sizeof (EFI_80211_GET_NETWORKS_DATA) + + (HiddenSSIdCount - 1) * sizeof (EFI_80211_SSID)); + if (GetNetworksToken->Data == NULL) { + FreePool (HiddenSSIdList); + WifiMgrFreeToken(ConfigToken); + gBS->RestoreTPL (OldTpl); + return EFI_OUT_OF_RESOURCES; + } + GetNetworksToken->Data->NumOfSSID = HiddenSSIdCount; + CopyMem(GetNetworksToken->Data->SSIDList, HiddenSSIdList, HiddenSSIdCount * sizeof (EFI_80211_SSID)); + FreePool(HiddenSSIdList); + } else { + + GetNetworksToken->Data = AllocateZeroPool (sizeof (EFI_80211_GET_NETWORKS_DATA)); + if (GetNetworksToken->Data == NULL) { + WifiMgrFreeToken(ConfigToken); + gBS->RestoreTPL (OldTpl); + return EFI_OUT_OF_RESOURCES; + } + + GetNetworksToken->Data->NumOfSSID = 0; + } + + // + //Create a handle when scan process ends + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + WifiMgrOnScanFinished, + ConfigToken, + &GetNetworksToken->Event + ); + if (EFI_ERROR (Status)) { + WifiMgrFreeToken(ConfigToken); + gBS->RestoreTPL (OldTpl); + return Status; + } + + // + //Start scan ... + // + Status = Nic->Wmp->GetNetworks (Nic->Wmp, GetNetworksToken); + if (EFI_ERROR (Status)) { + Nic->ScanState = WifiMgrScanFinished; + WifiMgrFreeToken(ConfigToken); + gBS->RestoreTPL (OldTpl); + return Status; + } + + gBS->RestoreTPL (OldTpl); + return EFI_SUCCESS; +} + +/** + Configure password to supplicant before connecting to a secured network. + + @param[in] Nic Pointer to the device data of the selected NIC. + @param[in] Profile The target network to be connected. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_NOT_FOUND No valid password is found to configure. + @retval Other Errors Returned errors when setting data to supplicant. + +**/ +EFI_STATUS +WifiMgrConfigPassword ( + IN WIFI_MGR_DEVICE_DATA *Nic, + IN WIFI_MGR_NETWORK_PROFILE *Profile + ) +{ + EFI_STATUS Status; + EFI_SUPPLICANT_PROTOCOL *Supplicant; + EFI_80211_SSID SSId; + UINT8 *AsciiPassword; + + if (Nic == NULL || Nic->Supplicant == NULL || Profile == NULL) { + return EFI_INVALID_PARAMETER; + } + Supplicant = Nic->Supplicant; + // + //Set SSId to supplicant + // + SSId.SSIdLen = Profile->Network.SSId.SSIdLen; + CopyMem(SSId.SSId, Profile->Network.SSId.SSId, sizeof (Profile->Network.SSId.SSId)); + Status = Supplicant->SetData(Supplicant,EfiSupplicant80211TargetSSIDName, + (VOID *)&SSId, sizeof(EFI_80211_SSID)); + if (EFI_ERROR(Status)) { + return Status; + } + + // + //Set password to supplicant + // + if (StrLen (Profile->Password) < PASSWORD_MIN_LEN) { + return EFI_NOT_FOUND; + } + AsciiPassword = AllocateZeroPool ((StrLen(Profile->Password) + 1) * sizeof (UINT8)); + if (AsciiPassword == NULL) { + return EFI_OUT_OF_RESOURCES; + } + UnicodeStrToAsciiStrS (Profile->Password, (CHAR8 *) AsciiPassword, PASSWORD_STORAGE_SIZE); + Status = Supplicant->SetData (Supplicant, EfiSupplicant80211PskPassword, + AsciiPassword, (StrLen(Profile->Password) + 1) * sizeof (UINT8)); + ZeroMem (AsciiPassword, AsciiStrLen ((CHAR8 *) AsciiPassword) + 1); + FreePool(AsciiPassword); + + return Status; +} + +/** + Conduct EAP configuration to supplicant before connecting to a EAP network. + Current WiFi Connection Manager only supports three kinds of EAP networks: + 1). EAP-TLS (Two-Way Authentication is required in our implementation) + 2). EAP-TTLS/MSCHAPv2 (One-Way Authentication is required in our implementation) + 3). PEAPv0/MSCHAPv2 (One-Way Authentication is required in our implementation) + + @param[in] Nic Pointer to the device data of the selected NIC. + @param[in] Profile The target network to be connected. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED The expected EAP method is not supported. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Other Errors Returned errors when setting data to supplicant. + +**/ +EFI_STATUS +WifiMgrConfigEap ( + IN WIFI_MGR_DEVICE_DATA *Nic, + IN WIFI_MGR_NETWORK_PROFILE *Profile + ) +{ + EFI_STATUS Status; + EFI_EAP_CONFIGURATION_PROTOCOL *EapConfig; + EFI_EAP_TYPE EapAuthMethod; + EFI_EAP_TYPE EapSecondAuthMethod; + EFI_EAP_TYPE *AuthMethodList; + CHAR8 *Identity; + UINTN IdentitySize; + CHAR16 *Password; + UINTN PasswordSize; + UINTN EncryptPasswordLen; + CHAR8 *AsciiEncryptPassword; + UINTN AuthMethodListSize; + UINTN Index; + + if (Nic == NULL || Nic->EapConfig == NULL || Profile == NULL) { + return EFI_INVALID_PARAMETER; + } + EapConfig = Nic->EapConfig; + + if (Profile->EapAuthMethod >= EAP_AUTH_METHOD_MAX) { + return EFI_INVALID_PARAMETER; + } + EapAuthMethod = mEapAuthMethod[Profile->EapAuthMethod]; + + if (EapAuthMethod != EFI_EAP_TYPE_EAPTLS) { + if (Profile->EapSecondAuthMethod >= EAP_SEAUTH_METHOD_MAX) { + return EFI_INVALID_PARAMETER; + } + EapSecondAuthMethod = mEapSecondAuthMethod[Profile->EapSecondAuthMethod]; + } + + // + //The first time to get Supported Auth Method list, return the size. + // + AuthMethodListSize = 0; + AuthMethodList = NULL; + Status = EapConfig->GetData (EapConfig, EFI_EAP_TYPE_ATTRIBUTE, EfiEapConfigEapSupportedAuthMethod, + (VOID *) AuthMethodList, &AuthMethodListSize); + if (Status == EFI_SUCCESS) { + // + //No Supported Eap Auth Method + // + return EFI_UNSUPPORTED; + } else if (Status != EFI_BUFFER_TOO_SMALL) { + return Status; + } + + // + // The second time to get Supported Auth Method list, return the list. + // In current design, only EAPTLS, TTLS and PEAP are supported + // + AuthMethodList = (EFI_EAP_TYPE *) AllocateZeroPool(AuthMethodListSize); + if (AuthMethodList == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Status = EapConfig->GetData (EapConfig, EFI_EAP_TYPE_ATTRIBUTE, EfiEapConfigEapSupportedAuthMethod, + (VOID *) AuthMethodList, &AuthMethodListSize); + if (EFI_ERROR (Status)) { + FreePool (AuthMethodList); + return Status; + } + + // + //Check if EapAuthMethod is in supported Auth Method list, if found, skip the loop. + // + for (Index = 0; Index < AuthMethodListSize / sizeof (EFI_EAP_TYPE); Index ++) { + if (EapAuthMethod == AuthMethodList[Index]) { + break; + } + } + if (Index == AuthMethodListSize / sizeof (EFI_EAP_TYPE)) { + FreePool (AuthMethodList); + return EFI_UNSUPPORTED; + } + FreePool (AuthMethodList); + + // + // Set Identity to Eap peer, Mandatory field for PEAP and TTLS + // + if (StrLen (Profile->EapIdentity) > 0) { + + IdentitySize = sizeof(CHAR8) * (StrLen(Profile->EapIdentity) + 1); + Identity = AllocateZeroPool (IdentitySize); + if (Identity == NULL) { + return EFI_OUT_OF_RESOURCES; + } + UnicodeStrToAsciiStrS(Profile->EapIdentity, Identity, IdentitySize); + Status = EapConfig->SetData (EapConfig, EFI_EAP_TYPE_IDENTITY, EfiEapConfigIdentityString, + (VOID *) Identity, IdentitySize - 1); + if (EFI_ERROR(Status)) { + FreePool (Identity); + return Status; + } + FreePool (Identity); + } else { + if (EapAuthMethod != EFI_EAP_TYPE_EAPTLS) { + return EFI_INVALID_PARAMETER; + } + } + + // + //Set Auth Method to Eap peer, Mandatory field + // + Status = EapConfig->SetData (EapConfig, EFI_EAP_TYPE_ATTRIBUTE, EfiEapConfigEapAuthMethod, + (VOID *) &EapAuthMethod, sizeof (EapAuthMethod)); + if (EFI_ERROR(Status)) { + return Status; + } + + if (EapAuthMethod == EFI_EAP_TYPE_TTLS || EapAuthMethod == EFI_EAP_TYPE_PEAP) { + + Status = EapConfig->SetData (EapConfig, EapAuthMethod, EfiEapConfigEap2ndAuthMethod, + (VOID *) &EapSecondAuthMethod, sizeof (EapSecondAuthMethod)); + if (EFI_ERROR(Status)) { + return Status; + } + + // + // Set Password to Eap peer + // + if (StrLen (Profile->EapPassword) < PASSWORD_MIN_LEN) { + + DEBUG ((DEBUG_ERROR, "[WiFi Connection Manager] Error: No Eap Password for Network: %s.\n", Profile->SSId)); + return EFI_INVALID_PARAMETER; + } + + PasswordSize = sizeof (CHAR16) * (StrLen (Profile->EapPassword) + 1); + Password = AllocateZeroPool (PasswordSize); + if (Password == NULL) { + return EFI_OUT_OF_RESOURCES; + } + StrCpyS (Password, PasswordSize, Profile->EapPassword);; + Status = EapConfig->SetData (EapConfig, EFI_EAP_TYPE_MSCHAPV2, EfiEapConfigEapMSChapV2Password, + (VOID *) Password, PasswordSize); + ZeroMem (Password, PasswordSize); + FreePool (Password); + if (EFI_ERROR (Status)) { + return Status; + } + + // + //If CA cert is required, set it to Eap peer + // + if (Profile->CACertData != NULL) { + + Status = EapConfig->SetData (EapConfig, EapAuthMethod, EfiEapConfigEapTlsCACert, + Profile->CACertData, Profile->CACertSize); + if (EFI_ERROR(Status)) { + return Status; + } + } else { + return EFI_INVALID_PARAMETER; + } + } else if (EapAuthMethod == EFI_EAP_TYPE_EAPTLS) { + + // + //Set CA cert to Eap peer + // + if (Profile->CACertData == NULL) { + return EFI_INVALID_PARAMETER; + } + Status = EapConfig->SetData (EapConfig, EFI_EAP_TYPE_EAPTLS, EfiEapConfigEapTlsCACert, + Profile->CACertData, Profile->CACertSize); + if (EFI_ERROR(Status)) { + return Status; + } + + // + //Set Client cert to Eap peer + // + if (Profile->ClientCertData == NULL) { + return EFI_INVALID_PARAMETER; + } + Status = EapConfig->SetData (EapConfig, EFI_EAP_TYPE_EAPTLS, EfiEapConfigEapTlsClientCert, + Profile->ClientCertData, Profile->ClientCertSize); + if (EFI_ERROR(Status)) { + return Status; + } + + // + //Set Private key to Eap peer + // + if (Profile->PrivateKeyData == NULL) { + + DEBUG ((DEBUG_ERROR, "[WiFi Connection Manager] Error: No Private Key for Network: %s.\n", Profile->SSId)); + return EFI_INVALID_PARAMETER; + } + + Status = EapConfig->SetData (EapConfig, EFI_EAP_TYPE_EAPTLS, EfiEapConfigEapTlsClientPrivateKeyFile, + Profile->PrivateKeyData, Profile->PrivateKeyDataSize); + if (EFI_ERROR(Status)) { + return Status; + } + + if (StrLen (Profile->PrivateKeyPassword) > 0) { + + EncryptPasswordLen = StrLen (Profile->PrivateKeyPassword); + AsciiEncryptPassword = AllocateZeroPool(EncryptPasswordLen + 1); + if (AsciiEncryptPassword == NULL) { + return EFI_OUT_OF_RESOURCES; + } + UnicodeStrToAsciiStrS(Profile->PrivateKeyPassword, AsciiEncryptPassword, EncryptPasswordLen + 1); + Status = EapConfig->SetData(EapConfig, EFI_EAP_TYPE_EAPTLS, + EfiEapConfigEapTlsClientPrivateKeyFilePassword, + (VOID *) AsciiEncryptPassword, EncryptPasswordLen + 1); + if (EFI_ERROR(Status)) { + + ZeroMem (AsciiEncryptPassword, EncryptPasswordLen + 1); + FreePool (AsciiEncryptPassword); + return Status; + } + + ZeroMem (AsciiEncryptPassword, EncryptPasswordLen + 1); + FreePool (AsciiEncryptPassword); + } + } else { + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + +/** + Get current link state from low layer. + + @param[in] Nic Pointer to the device data of the selected NIC. + @param[out] LinkState The pointer to buffer to retrieve link state. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED Adapter information protocol is not supported. + @retval Other Errors Returned errors when retrieving link state from low layer. + +**/ +EFI_STATUS +WifiMgrGetLinkState ( + IN WIFI_MGR_DEVICE_DATA *Nic, + OUT EFI_ADAPTER_INFO_MEDIA_STATE *LinkState + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + UINTN DataSize; + EFI_ADAPTER_INFO_MEDIA_STATE *UndiState; + EFI_ADAPTER_INFORMATION_PROTOCOL *Aip; + + if (Nic == NULL || LinkState == NULL) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Status = gBS->OpenProtocol ( + Nic->ControllerHandle, + &gEfiAdapterInformationProtocolGuid, + (VOID**) &Aip, + Nic->DriverHandle, + Nic->ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + gBS->RestoreTPL (OldTpl); + return EFI_UNSUPPORTED; + } + + Status = Aip->GetInformation( + Aip, + &gEfiAdapterInfoMediaStateGuid, + (VOID **) &UndiState, + &DataSize + ); + if (EFI_ERROR (Status)) { + gBS->RestoreTPL (OldTpl); + return Status; + } + gBS->RestoreTPL (OldTpl); + + CopyMem (LinkState, UndiState, sizeof (EFI_ADAPTER_INFO_MEDIA_STATE)); + FreePool (UndiState); + return EFI_SUCCESS; +} + +/** + Prepare configuration work before connecting to the target network. + For WPA2 Personal networks, password should be checked; and for EAP networks, parameters + are different for different networks. + + @param[in] Nic Pointer to the device data of the selected NIC. + @param[in] Profile The target network to be connected. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_UNSUPPORTED This network is not supported. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + +**/ +EFI_STATUS +WifiMgrPrepareConnection ( + IN WIFI_MGR_DEVICE_DATA *Nic, + IN WIFI_MGR_NETWORK_PROFILE *Profile + ) +{ + EFI_STATUS Status; + UINT8 SecurityType; + BOOLEAN AKMSuiteSupported; + BOOLEAN CipherSuiteSupported; + + if (Profile == NULL || Nic == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = WifiMgrCheckRSN (Profile->Network.AKMSuite, Profile->Network.CipherSuite, + Nic, &SecurityType, &AKMSuiteSupported, &CipherSuiteSupported); + if (EFI_ERROR (Status)) { + return Status; + } + + if (AKMSuiteSupported && CipherSuiteSupported) { + switch (SecurityType) { + case SECURITY_TYPE_WPA2_PERSONAL: + + Status = WifiMgrConfigPassword (Nic, Profile); + if (EFI_ERROR (Status)) { + if (Status == EFI_NOT_FOUND) { + if (Nic->OneTimeConnectRequest) { + WifiMgrUpdateConnectMessage (Nic, FALSE, L"Connect Failed: Invalid Password!"); + } + } + return Status; + } + break; + + case SECURITY_TYPE_WPA2_ENTERPRISE: + + Status = WifiMgrConfigEap (Nic, Profile); + if (EFI_ERROR (Status)) { + if (Status == EFI_INVALID_PARAMETER) { + if (Nic->OneTimeConnectRequest) { + WifiMgrUpdateConnectMessage (Nic, FALSE, L"Connect Failed: Invalid Configuration!"); + } + } + return Status; + } + break; + + case SECURITY_TYPE_NONE: + break; + + default: + return EFI_UNSUPPORTED; + } + } else { + return EFI_UNSUPPORTED; + } + + return EFI_SUCCESS; +} + +/** + The callback function for connect operation. + + ASSERT when errors occur in config token. + + @param[in] Event The Connect token receive event. + @param[in] Context The context of the connect token. + +**/ +VOID +EFIAPI +WifiMgrOnConnectFinished ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + WIFI_MGR_MAC_CONFIG_TOKEN *ConfigToken; + WIFI_MGR_NETWORK_PROFILE *ConnectedProfile; + UINT8 SecurityType; + UINT8 SSIdLen; + CHAR8 *AsciiSSId; + + ASSERT (Context != NULL); + + ConnectedProfile = NULL; + ConfigToken = (WIFI_MGR_MAC_CONFIG_TOKEN*) Context; + ASSERT (ConfigToken->Nic != NULL); + + ConfigToken->Nic->ConnectState = WifiMgrDisconnected; + ASSERT (ConfigToken->Type == TokenTypeConnectNetworkToken); + + ASSERT (ConfigToken->Token.ConnectNetworkToken != NULL); + if (ConfigToken->Token.ConnectNetworkToken->Status != EFI_SUCCESS) { + + if (ConfigToken->Nic->OneTimeConnectRequest) { + // + // Only update message for user triggered connection + // + if (ConfigToken->Token.ConnectNetworkToken->Status == EFI_ACCESS_DENIED) { + + WifiMgrUpdateConnectMessage (ConfigToken->Nic, FALSE, L"Connect Failed: Permission Denied!"); + } else { + WifiMgrUpdateConnectMessage (ConfigToken->Nic, FALSE, L"Connect Failed!"); + } + ConfigToken->Nic->OneTimeConnectRequest = FALSE; + } + ConfigToken->Nic->CurrentOperateNetwork = NULL; + return; + } + + if (ConfigToken->Token.ConnectNetworkToken->ResultCode != ConnectSuccess) { + + if (ConfigToken->Nic->OneTimeConnectRequest) { + + if (ConfigToken->Token.ConnectNetworkToken->ResultCode == ConnectFailedReasonUnspecified) { + WifiMgrUpdateConnectMessage (ConfigToken->Nic, FALSE, L"Connect Failed: Wrong Password or Unexpected Error!"); + } else { + WifiMgrUpdateConnectMessage (ConfigToken->Nic, FALSE, L"Connect Failed!"); + } + } + goto Exit; + } + + if (ConfigToken->Token.ConnectNetworkToken->Data == NULL || + ConfigToken->Token.ConnectNetworkToken->Data->Network == NULL) { + + // + // An unexpected error occurs, tell low layer to perform a disconnect + // + ConfigToken->Nic->HasDisconnectPendingNetwork = TRUE; + WifiMgrUpdateConnectMessage (ConfigToken->Nic, FALSE, NULL); + goto Exit; + } + + // + // A correct connect token received, terminate the connection process + // + Status = WifiMgrCheckRSN(ConfigToken->Token.ConnectNetworkToken->Data->Network->AKMSuite, + ConfigToken->Token.ConnectNetworkToken->Data->Network->CipherSuite, + ConfigToken->Nic, &SecurityType, NULL, NULL); + if (EFI_ERROR(Status)) { + SecurityType = SECURITY_TYPE_UNKNOWN; + } + + SSIdLen = ConfigToken->Token.ConnectNetworkToken->Data->Network->SSId.SSIdLen; + AsciiSSId = (CHAR8*) AllocateZeroPool(sizeof (CHAR8) * (SSIdLen + 1)); + if (AsciiSSId == NULL) { + ConfigToken->Nic->HasDisconnectPendingNetwork = TRUE; + WifiMgrUpdateConnectMessage (ConfigToken->Nic, FALSE, NULL); + goto Exit; + } + + CopyMem(AsciiSSId, ConfigToken->Token.ConnectNetworkToken->Data->Network->SSId.SSId, SSIdLen); + *(AsciiSSId + SSIdLen) = '\0'; + + ConnectedProfile = WifiMgrGetProfileByAsciiSSId(AsciiSSId, SecurityType, &ConfigToken->Nic->ProfileList); + FreePool(AsciiSSId); + if (ConnectedProfile == NULL) { + ConfigToken->Nic->HasDisconnectPendingNetwork = TRUE; + WifiMgrUpdateConnectMessage (ConfigToken->Nic, FALSE, NULL); + goto Exit; + } + + ConfigToken->Nic->ConnectState = WifiMgrConnectedToAp; + WifiMgrUpdateConnectMessage (ConfigToken->Nic, TRUE, NULL); + +Exit: + + if (ConfigToken->Nic->ConnectState == WifiMgrDisconnected) { + ConfigToken->Nic->CurrentOperateNetwork = NULL; + } + ConfigToken->Nic->OneTimeConnectRequest = FALSE; + WifiMgrFreeToken(ConfigToken); +} + +/** + Start connect operation, and send out a token to connect to a target network. + + @param[in] Nic Pointer to the device data of the selected NIC. + @param[in] Profile The target network to be connected. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_ALREADY_STARTED Already in "connected" state, need to perform a disconnect + operation first. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Other Errors Return errors when connecting network on low layer. + +**/ +EFI_STATUS +WifiMgrConnectToNetwork ( + IN WIFI_MGR_DEVICE_DATA *Nic, + IN WIFI_MGR_NETWORK_PROFILE *Profile + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + EFI_ADAPTER_INFO_MEDIA_STATE LinkState; + WIFI_MGR_MAC_CONFIG_TOKEN *ConfigToken; + EFI_80211_CONNECT_NETWORK_TOKEN *ConnectToken; + + if (Nic == NULL || Nic->Wmp == NULL || Profile == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = WifiMgrGetLinkState (Nic, &LinkState); + if (EFI_ERROR (Status)) { + return Status; + } + if (LinkState.MediaState == EFI_SUCCESS) { + return EFI_ALREADY_STARTED; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Status = WifiMgrPrepareConnection (Nic, Profile); + if (EFI_ERROR (Status)) { + gBS->RestoreTPL (OldTpl); + return Status; + } + + // + // Create a new connect token + // + ConfigToken = AllocateZeroPool (sizeof (WIFI_MGR_MAC_CONFIG_TOKEN)); + if (ConfigToken == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Exit; + } + + ConfigToken->Type = TokenTypeConnectNetworkToken; + ConfigToken->Nic = Nic; + ConfigToken->Token.ConnectNetworkToken = AllocateZeroPool (sizeof (EFI_80211_CONNECT_NETWORK_TOKEN)); + if (ConfigToken->Token.ConnectNetworkToken == NULL) { + goto Exit; + } + + ConnectToken = ConfigToken->Token.ConnectNetworkToken; + ConnectToken->Data = AllocateZeroPool (sizeof (EFI_80211_CONNECT_NETWORK_DATA)); + if (ConnectToken->Data == NULL) { + goto Exit; + } + + ConnectToken->Data->Network = AllocateZeroPool (sizeof (EFI_80211_NETWORK)); + if (ConnectToken->Data->Network == NULL) { + goto Exit; + } + CopyMem(ConnectToken->Data->Network, &Profile->Network, sizeof (EFI_80211_NETWORK)); + + // + // Add event handle and start to connect + // + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + WifiMgrOnConnectFinished, + ConfigToken, + &ConnectToken->Event + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + + Nic->ConnectState = WifiMgrConnectingToAp; + Nic->CurrentOperateNetwork = Profile; + WifiMgrUpdateConnectMessage (Nic, FALSE, NULL); + + // + //Start Connecting ... + // + Status = Nic->Wmp->ConnectNetwork (Nic->Wmp, ConnectToken); + + // + // Erase secrets after connection is triggered + // + WifiMgrCleanProfileSecrets (Profile); + + if (EFI_ERROR (Status)) { + if (Status == EFI_ALREADY_STARTED) { + Nic->ConnectState = WifiMgrConnectedToAp; + WifiMgrUpdateConnectMessage (Nic, TRUE, NULL); + } else { + + Nic->ConnectState = WifiMgrDisconnected; + Nic->CurrentOperateNetwork = NULL; + + if (Nic->OneTimeConnectRequest) { + if (Status == EFI_NOT_FOUND) { + WifiMgrUpdateConnectMessage (Nic, FALSE, L"Connect Failed: Not Available!"); + } else { + WifiMgrUpdateConnectMessage (Nic, FALSE, L"Connect Failed: Unexpected Error!"); + } + } + } + goto Exit; + } + +Exit: + + if (EFI_ERROR (Status)) { + WifiMgrFreeToken (ConfigToken); + } + gBS->RestoreTPL (OldTpl); + + DEBUG ((DEBUG_INFO, "[WiFi Connection Manager] WifiMgrConnectToNetwork: %r\n", Status)); + return Status; +} + +/** + The callback function for disconnect operation. + + ASSERT when errors occur in config token. + + @param[in] Event The Disconnect token receive event. + @param[in] Context The context of the Disconnect token. + +**/ +VOID +EFIAPI +WifiMgrOnDisconnectFinished ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + WIFI_MGR_MAC_CONFIG_TOKEN *ConfigToken; + + ASSERT (Context != NULL); + + ConfigToken = (WIFI_MGR_MAC_CONFIG_TOKEN*) Context; + ASSERT (ConfigToken->Nic != NULL); + ASSERT (ConfigToken->Type == TokenTypeDisconnectNetworkToken); + + ASSERT (ConfigToken->Token.DisconnectNetworkToken != NULL); + if (ConfigToken->Token.DisconnectNetworkToken->Status != EFI_SUCCESS) { + ConfigToken->Nic->ConnectState = WifiMgrConnectedToAp; + WifiMgrUpdateConnectMessage (ConfigToken->Nic, FALSE, NULL); + ConfigToken->Nic->OneTimeDisconnectRequest = FALSE; + goto Exit; + } + + ConfigToken->Nic->ConnectState = WifiMgrDisconnected; + ConfigToken->Nic->CurrentOperateNetwork = NULL; + WifiMgrUpdateConnectMessage (ConfigToken->Nic, TRUE, NULL); + ConfigToken->Nic->OneTimeDisconnectRequest = FALSE; + + // + // Disconnected network may not be in network list now, trigger a scan again! + // + ConfigToken->Nic->OneTimeScanRequest = TRUE; + + Exit: + WifiMgrFreeToken(ConfigToken); + return; +} + +/** + Start disconnect operation, and send out a token to disconnect from current connected + network. + + @param[in] Nic Pointer to the device data of the selected NIC. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval Other Errors Return errors when disconnecting a network on low layer. + +**/ +EFI_STATUS +WifiMgrDisconnectToNetwork ( + IN WIFI_MGR_DEVICE_DATA *Nic + ) +{ + EFI_STATUS Status; + EFI_TPL OldTpl; + WIFI_MGR_MAC_CONFIG_TOKEN *ConfigToken; + EFI_80211_DISCONNECT_NETWORK_TOKEN *DisconnectToken; + + if (Nic == NULL) { + return EFI_INVALID_PARAMETER; + } + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + Status = EFI_SUCCESS; + ConfigToken = AllocateZeroPool (sizeof (WIFI_MGR_MAC_CONFIG_TOKEN)); + if (ConfigToken == NULL) { + gBS->RestoreTPL (OldTpl); + return EFI_OUT_OF_RESOURCES; + } + + ConfigToken->Type = TokenTypeDisconnectNetworkToken; + ConfigToken->Nic = Nic; + ConfigToken->Token.DisconnectNetworkToken = AllocateZeroPool (sizeof (EFI_80211_DISCONNECT_NETWORK_TOKEN)); + if (ConfigToken->Token.DisconnectNetworkToken == NULL) { + WifiMgrFreeToken(ConfigToken); + gBS->RestoreTPL (OldTpl); + return EFI_OUT_OF_RESOURCES; + } + + DisconnectToken = ConfigToken->Token.DisconnectNetworkToken; + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + WifiMgrOnDisconnectFinished, + ConfigToken, + &DisconnectToken->Event + ); + if (EFI_ERROR (Status)) { + WifiMgrFreeToken(ConfigToken); + gBS->RestoreTPL (OldTpl); + return Status; + } + + Nic->ConnectState = WifiMgrDisconnectingToAp; + WifiMgrUpdateConnectMessage (ConfigToken->Nic, FALSE, NULL); + + Status = Nic->Wmp->DisconnectNetwork (Nic->Wmp, DisconnectToken); + if (EFI_ERROR (Status)) { + if (Status == EFI_NOT_FOUND) { + + Nic->ConnectState = WifiMgrDisconnected; + Nic->CurrentOperateNetwork = NULL; + + // + // This network is not in network list now, trigger a scan again! + // + Nic->OneTimeScanRequest = TRUE; + + // + // State has been changed from Connected to Disconnected + // + WifiMgrUpdateConnectMessage (ConfigToken->Nic, TRUE, NULL); + Status = EFI_SUCCESS; + } else { + if (Nic->OneTimeDisconnectRequest) { + + WifiMgrUpdateConnectMessage (ConfigToken->Nic, FALSE, L"Disconnect Failed: Unexpected Error!"); + } + + Nic->ConnectState = WifiMgrConnectedToAp; + WifiMgrUpdateConnectMessage (ConfigToken->Nic, FALSE, NULL); + } + WifiMgrFreeToken(ConfigToken); + } + + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + The state machine of the connection manager, periodically check the state and + perform a corresponding operation. + + @param[in] Event The timer event to be triggered. + @param[in] Context The context of the Nic device data. + +**/ +VOID +EFIAPI +WifiMgrOnTimerTick ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + WIFI_MGR_DEVICE_DATA *Nic; + EFI_STATUS Status; + EFI_ADAPTER_INFO_MEDIA_STATE LinkState; + WIFI_MGR_NETWORK_PROFILE *Profile; + + if (Context == NULL) { + return; + } + + Nic = (WIFI_MGR_DEVICE_DATA*) Context; + NET_CHECK_SIGNATURE (Nic, WIFI_MGR_DEVICE_DATA_SIGNATURE); + + Status = WifiMgrGetLinkState (Nic, &LinkState); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "[WiFi Connection Manager] Error: Failed to get link state!\n")); + return; + } + + if (Nic->LastLinkState.MediaState != LinkState.MediaState) { + if (Nic->LastLinkState.MediaState == EFI_SUCCESS && LinkState.MediaState == EFI_NO_MEDIA) { + Nic->HasDisconnectPendingNetwork = TRUE; + } + Nic->LastLinkState.MediaState = LinkState.MediaState; + } + + Nic->ScanTickTime ++; + if ((Nic->ScanTickTime > WIFI_SCAN_FREQUENCY || Nic->OneTimeScanRequest) && + Nic->ScanState == WifiMgrScanFinished) { + + Nic->OneTimeScanRequest = FALSE; + Nic->ScanTickTime = 0; + + DEBUG ((DEBUG_INFO, "[WiFi Connection Manager] Scan is triggered.\n")); + WifiMgrStartScan (Nic); + } + + if (Nic->AvailableCount > 0 && Nic->ScanState == WifiMgrScanFinished) { + + switch (Nic->ConnectState) { + case WifiMgrDisconnected: + + if (Nic->HasDisconnectPendingNetwork) { + Nic->HasDisconnectPendingNetwork = FALSE; + } + + if (Nic->ConnectPendingNetwork != NULL) { + + Profile = Nic->ConnectPendingNetwork; + Status = WifiMgrConnectToNetwork(Nic, Profile); + Nic->ConnectPendingNetwork = NULL; + if (EFI_ERROR (Status)) { + // + // Some error happened, don't wait for a return connect token! + // + Nic->OneTimeConnectRequest = FALSE; + } + } + break; + + case WifiMgrConnectingToAp: + break; + + case WifiMgrDisconnectingToAp: + break; + + case WifiMgrConnectedToAp: + + if (Nic->ConnectPendingNetwork != NULL || Nic->HasDisconnectPendingNetwork) { + + Status = WifiMgrDisconnectToNetwork(Nic); + if (EFI_ERROR (Status)) { + // + // Some error happened, don't wait for a return disconnect token! + // + Nic->OneTimeDisconnectRequest = FALSE; + } + } + break; + + default: + break; + } + } +} diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrImpl.h b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrImpl.h new file mode 100644 index 000000000..d6b7ded8e --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrImpl.h @@ -0,0 +1,99 @@ +/** @file + The Mac Connection2 Protocol adapter functions for WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_WIFI_IMPL__ +#define __EFI_WIFI_IMPL__ + +/** + Start scan operation, and send out a token to collect available networks. + + @param[in] Nic Pointer to the device data of the selected NIC. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_ALREADY_STARTED A former scan operation is already ongoing. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Other Errors Return errors when getting networks from low layer. + +**/ +EFI_STATUS +WifiMgrStartScan ( + IN WIFI_MGR_DEVICE_DATA *Nic + ); + +/** + Get current link state from low layer. + + @param[in] Nic Pointer to the device data of the selected NIC. + @param[out] LinkState The pointer to buffer to retrieve link state. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_UNSUPPORTED Adapter information protocol is not supported. + @retval Other Errors Returned errors when retrieving link state from low layer. + +**/ +EFI_STATUS +WifiMgrGetLinkState ( + IN WIFI_MGR_DEVICE_DATA *Nic, + OUT EFI_ADAPTER_INFO_MEDIA_STATE *LinkState + ); + +/** + Start connect operation, and send out a token to connect to a target network. + + @param[in] Nic Pointer to the device data of the selected NIC. + @param[in] Profile The target network to be connected. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_ALREADY_STARTED Already in "connected" state, need to perform a disconnect + operation first. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Other Errors Return errors when connecting network on low layer. + +**/ +EFI_STATUS +WifiMgrConnectToNetwork ( + IN WIFI_MGR_DEVICE_DATA *Nic, + IN WIFI_MGR_NETWORK_PROFILE *Profile + ); + +/** + Start disconnect operation, and send out a token to disconnect from current connected + network. + + @param[in] Nic Pointer to the device data of the selected NIC. + + @retval EFI_SUCCESS The operation is completed. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory. + @retval Other Errors Return errors when disconnecting a network on low layer. + +**/ +EFI_STATUS +WifiMgrDisconnectToNetwork ( + IN WIFI_MGR_DEVICE_DATA *Nic + ); + +/** + The state machine of the connection manager, periodically check the state and + perform a corresponding operation. + + @param[in] Event The timer event to be triggered. + @param[in] Context The context of the Nic device data. + +**/ +VOID +EFIAPI +WifiMgrOnTimerTick ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +#endif diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrMisc.c b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrMisc.c new file mode 100644 index 000000000..9295b8b33 --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrMisc.c @@ -0,0 +1,744 @@ +/** @file + The Miscellaneous Routines for WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "WifiConnectionMgrDxe.h" + +/** + Empty function for event process function. + + @param Event The Event need to be process + @param Context The context of the event. + +**/ +VOID +EFIAPI +WifiMgrInternalEmptyFunction ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + return; +} + +/** + Convert the mac address into a hexadecimal encoded ":" seperated string. + + @param[in] Mac The mac address. + @param[in] StrSize The size, in bytes, of the output buffer specified by Str. + @param[out] Str The storage to return the mac string. + +**/ +VOID +WifiMgrMacAddrToStr ( + IN EFI_80211_MAC_ADDRESS *Mac, + IN UINT32 StrSize, + OUT CHAR16 *Str + ) +{ + if (Mac == NULL || Str == NULL) { + return; + } + + UnicodeSPrint ( + Str, + StrSize, + L"%02X:%02X:%02X:%02X:%02X:%02X", + Mac->Addr[0], Mac->Addr[1], Mac->Addr[2], + Mac->Addr[3], Mac->Addr[4], Mac->Addr[5] + ); +} + +/** + Read private key file to buffer. + + @param[in] FileContext The file context of private key file. + @param[out] PrivateKeyDataAddr The buffer address to restore private key file, should be + freed by caller. + @param[out] PrivateKeyDataSize The size of read private key file. + + @retval EFI_SUCCESS Successfully read the private key file. + @retval EFI_INVALID_PARAMETER One or more of the parameters is invalid. + +**/ +EFI_STATUS +WifiMgrReadFileToBuffer ( + IN WIFI_MGR_FILE_CONTEXT *FileContext, + OUT VOID **DataAddr, + OUT UINTN *DataSize + ) +{ + EFI_STATUS Status; + + if (FileContext != NULL && FileContext->FHandle != NULL) { + + Status = ReadFileContent ( + FileContext->FHandle, + DataAddr, + DataSize, + 0 + ); + + if (FileContext->FHandle != NULL) { + FileContext->FHandle->Close (FileContext->FHandle); + } + FileContext->FHandle = NULL; + return Status; + } + + return EFI_INVALID_PARAMETER; +} + +/** + Get the Nic data by the NicIndex. + + @param[in] Private The pointer to the global private data structure. + @param[in] NicIndex The index indicates the position of wireless NIC. + + @return Pointer to the Nic data, or NULL if not found. + +**/ +WIFI_MGR_DEVICE_DATA * +WifiMgrGetNicByIndex ( + IN WIFI_MGR_PRIVATE_DATA *Private, + IN UINT32 NicIndex + ) +{ + LIST_ENTRY *Entry; + WIFI_MGR_DEVICE_DATA *Nic; + + if (Private == NULL) { + return NULL; + } + + NET_LIST_FOR_EACH (Entry, &Private->NicList) { + Nic = NET_LIST_USER_STRUCT_S (Entry, WIFI_MGR_DEVICE_DATA, + Link, WIFI_MGR_DEVICE_DATA_SIGNATURE); + if (Nic->NicIndex == NicIndex) { + return Nic; + } + } + + return NULL; +} + +/** + Find a network profile through its' SSId and securit type, and the SSId is an unicode string. + + @param[in] SSId The target network's SSId. + @param[in] SecurityType The target network's security type. + @param[in] ProfileList The profile list on a Nic. + + @return Pointer to a network profile, or NULL if not found. + +**/ +WIFI_MGR_NETWORK_PROFILE * +WifiMgrGetProfileByUnicodeSSId ( + IN CHAR16 *SSId, + IN UINT8 SecurityType, + IN LIST_ENTRY *ProfileList + ) +{ + LIST_ENTRY *Entry; + WIFI_MGR_NETWORK_PROFILE *Profile; + + if (SSId == NULL || ProfileList == NULL) { + return NULL; + } + + NET_LIST_FOR_EACH (Entry, ProfileList) { + Profile = NET_LIST_USER_STRUCT_S (Entry, WIFI_MGR_NETWORK_PROFILE, + Link, WIFI_MGR_PROFILE_SIGNATURE); + if (StrCmp (SSId, Profile->SSId) == 0 && SecurityType == Profile->SecurityType) { + return Profile; + } + } + + return NULL; +} + +/** + Find a network profile through its' SSId and securit type, and the SSId is an ascii string. + + @param[in] SSId The target network's SSId. + @param[in] SecurityType The target network's security type. + @param[in] ProfileList The profile list on a Nic. + + @return Pointer to a network profile, or NULL if not found. + +**/ +WIFI_MGR_NETWORK_PROFILE * +WifiMgrGetProfileByAsciiSSId ( + IN CHAR8 *SSId, + IN UINT8 SecurityType, + IN LIST_ENTRY *ProfileList + ) +{ + CHAR16 SSIdUniCode[SSID_STORAGE_SIZE]; + + if (SSId == NULL) { + return NULL; + } + if (AsciiStrToUnicodeStrS (SSId, SSIdUniCode, SSID_STORAGE_SIZE) != RETURN_SUCCESS) { + return NULL; + } + + return WifiMgrGetProfileByUnicodeSSId (SSIdUniCode, SecurityType, ProfileList); +} + +/** + Find a network profile through its' profile index. + + @param[in] ProfileIndex The target network's profile index. + @param[in] ProfileList The profile list on a Nic. + + @return Pointer to a network profile, or NULL if not found. + +**/ +WIFI_MGR_NETWORK_PROFILE * +WifiMgrGetProfileByProfileIndex ( + IN UINT32 ProfileIndex, + IN LIST_ENTRY *ProfileList + ) +{ + WIFI_MGR_NETWORK_PROFILE *Profile; + LIST_ENTRY *Entry; + + if (ProfileList == NULL) { + return NULL; + } + NET_LIST_FOR_EACH (Entry, ProfileList) { + Profile = NET_LIST_USER_STRUCT_S (Entry, WIFI_MGR_NETWORK_PROFILE, + Link, WIFI_MGR_PROFILE_SIGNATURE); + if (Profile->ProfileIndex == ProfileIndex) { + return Profile; + } + } + return NULL; +} + +/** + To test if the AKMSuite is in supported AKMSuite list. + + @param[in] SupportedAKMSuiteCount The count of the supported AKMSuites. + @param[in] SupportedAKMSuiteList The supported AKMSuite list. + @param[in] AKMSuite The AKMSuite to be tested. + + @return True if this AKMSuite is supported, or False if not. + +**/ +BOOLEAN +WifiMgrSupportAKMSuite ( + IN UINT16 SupportedAKMSuiteCount, + IN UINT32 *SupportedAKMSuiteList, + IN UINT32 *AKMSuite + ) +{ + UINT16 Index; + + if (AKMSuite == NULL || SupportedAKMSuiteList == NULL || + SupportedAKMSuiteCount == 0) { + return FALSE; + } + + for (Index = 0; Index < SupportedAKMSuiteCount; Index ++) { + if (SupportedAKMSuiteList[Index] == *AKMSuite) { + return TRUE; + } + } + + return FALSE; +} + +/** + To check if the CipherSuite is in supported CipherSuite list. + + @param[in] SupportedCipherSuiteCount The count of the supported CipherSuites. + @param[in] SupportedCipherSuiteList The supported CipherSuite list. + @param[in] CipherSuite The CipherSuite to be tested. + + @return True if this CipherSuite is supported, or False if not. + +**/ +BOOLEAN +WifiMgrSupportCipherSuite ( + IN UINT16 SupportedCipherSuiteCount, + IN UINT32 *SupportedCipherSuiteList, + IN UINT32 *CipherSuite + ) +{ + UINT16 Index; + + if (CipherSuite == NULL || SupportedCipherSuiteCount == 0 || + SupportedCipherSuiteList == NULL) { + return FALSE; + } + + for (Index = 0; Index < SupportedCipherSuiteCount; Index ++) { + if (SupportedCipherSuiteList[Index] == *CipherSuite) { + return TRUE; + } + } + + return FALSE; +} + +/** + Check an AKM suite list and a Cipher suite list to see if one or more AKM suites or Cipher suites + are supported and find the matchable security type. + + @param[in] AKMList The target AKM suite list to be checked. + @param[in] CipherList The target Cipher suite list to be checked + @param[in] Nic The Nic to operate, contains the supported AKMSuite list + and supported CipherSuite list + @param[out] SecurityType To identify a security type from the AKM suite list and + Cipher suite list + @param[out] AKMSuiteSupported To identify if this security type is supported. If it is + NULL, overcome this field + @param[out] CipherSuiteSupported To identify if this security type is supported. If it is + NULL, overcome this field + + @retval EFI_SUCCESS This operation has completed successfully. + @retval EFI_INVALID_PARAMETER No Nic found or the suite list is null. + +**/ +EFI_STATUS +WifiMgrCheckRSN ( + IN EFI_80211_AKM_SUITE_SELECTOR *AKMList, + IN EFI_80211_CIPHER_SUITE_SELECTOR *CipherList, + IN WIFI_MGR_DEVICE_DATA *Nic, + OUT UINT8 *SecurityType, + OUT BOOLEAN *AKMSuiteSupported, + OUT BOOLEAN *CipherSuiteSupported + ) +{ + EFI_80211_AKM_SUITE_SELECTOR *SupportedAKMSuites; + EFI_80211_CIPHER_SUITE_SELECTOR *SupportedSwCipherSuites; + EFI_80211_CIPHER_SUITE_SELECTOR *SupportedHwCipherSuites; + EFI_80211_SUITE_SELECTOR *AKMSuite; + EFI_80211_SUITE_SELECTOR *CipherSuite; + UINT16 AKMIndex; + UINT16 CipherIndex; + + if (Nic == NULL || AKMList == NULL || CipherList == NULL|| SecurityType == NULL) { + return EFI_INVALID_PARAMETER; + } + + SupportedAKMSuites = Nic->SupportedSuites.SupportedAKMSuites; + SupportedSwCipherSuites = Nic->SupportedSuites.SupportedSwCipherSuites; + SupportedHwCipherSuites = Nic->SupportedSuites.SupportedHwCipherSuites; + + *SecurityType = SECURITY_TYPE_UNKNOWN; + if (AKMSuiteSupported != NULL && CipherSuiteSupported != NULL) { + *AKMSuiteSupported = FALSE; + *CipherSuiteSupported = FALSE; + } + + if (AKMList->AKMSuiteCount == 0) { + if (CipherList->CipherSuiteCount == 0) { + *SecurityType = SECURITY_TYPE_NONE; + if (AKMSuiteSupported != NULL && CipherSuiteSupported != NULL) { + *AKMSuiteSupported = TRUE; + *CipherSuiteSupported = TRUE; + } + } + + return EFI_SUCCESS; + } + + for (AKMIndex = 0; AKMIndex < AKMList->AKMSuiteCount; AKMIndex ++) { + + AKMSuite = AKMList->AKMSuiteList + AKMIndex; + if (WifiMgrSupportAKMSuite(SupportedAKMSuites->AKMSuiteCount, + (UINT32*) SupportedAKMSuites->AKMSuiteList, (UINT32*) AKMSuite)) { + + if (AKMSuiteSupported != NULL && CipherSuiteSupported != NULL) { + *AKMSuiteSupported = TRUE; + } + for (CipherIndex = 0; CipherIndex < CipherList->CipherSuiteCount; CipherIndex ++) { + + CipherSuite = CipherList->CipherSuiteList + CipherIndex; + + if (SupportedSwCipherSuites != NULL) { + + if (WifiMgrSupportCipherSuite(SupportedSwCipherSuites->CipherSuiteCount, + (UINT32*) SupportedSwCipherSuites->CipherSuiteList, (UINT32*) CipherSuite)) { + + *SecurityType = WifiMgrGetSecurityType ((UINT32*) AKMSuite, (UINT32*) CipherSuite); + + if (*SecurityType != SECURITY_TYPE_UNKNOWN) { + + if (AKMSuiteSupported != NULL && CipherSuiteSupported != NULL) { + *CipherSuiteSupported = TRUE; + } + return EFI_SUCCESS; + } + } + } + + if (SupportedHwCipherSuites != NULL) { + + if (WifiMgrSupportCipherSuite(SupportedHwCipherSuites->CipherSuiteCount, + (UINT32*) SupportedHwCipherSuites->CipherSuiteList, (UINT32*) CipherSuite)) { + + *SecurityType = WifiMgrGetSecurityType ((UINT32*) AKMSuite, (UINT32*) CipherSuite); + + if (*SecurityType != SECURITY_TYPE_UNKNOWN) { + + if (AKMSuiteSupported != NULL && CipherSuiteSupported != NULL) { + *CipherSuiteSupported = TRUE; + } + return EFI_SUCCESS; + } + } + } + } + } + } + + *SecurityType = WifiMgrGetSecurityType ((UINT32*) AKMList->AKMSuiteList, + (UINT32*) CipherList->CipherSuiteList); + + return EFI_SUCCESS; +} + +/** + Get the security type for a certain AKMSuite and CipherSuite. + + @param[in] AKMSuite An certain AKMSuite. + @param[in] CipherSuite An certain CipherSuite. + + @return a security type if found, or SECURITY_TYPE_UNKNOWN. + +**/ +UINT8 +WifiMgrGetSecurityType ( + IN UINT32 *AKMSuite, + IN UINT32 *CipherSuite + ) +{ + if (CipherSuite == NULL) { + + if (AKMSuite == NULL) { + return SECURITY_TYPE_NONE; + } else { + return SECURITY_TYPE_UNKNOWN; + } + } else if (*CipherSuite == IEEE_80211_PAIRWISE_CIPHER_SUITE_USE_GROUP) { + + if (AKMSuite == NULL) { + return SECURITY_TYPE_NONE; + } else { + return SECURITY_TYPE_UNKNOWN; + } + } else if (*CipherSuite == IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP40 || + *CipherSuite == IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP104) { + + return SECURITY_TYPE_WEP; + } else if (*CipherSuite == IEEE_80211_PAIRWISE_CIPHER_SUITE_CCMP) { + + if (AKMSuite == NULL) { + return SECURITY_TYPE_UNKNOWN; + } + + if (*AKMSuite == IEEE_80211_AKM_SUITE_8021X_OR_PMKSA || + *AKMSuite == IEEE_80211_AKM_SUITE_8021X_OR_PMKSA_SHA256) { + + return SECURITY_TYPE_WPA2_ENTERPRISE; + } else if (*AKMSuite == IEEE_80211_AKM_SUITE_PSK || + *AKMSuite == IEEE_80211_AKM_SUITE_PSK_SHA256){ + + return SECURITY_TYPE_WPA2_PERSONAL; + }else { + return SECURITY_TYPE_UNKNOWN; + } + } else if (*CipherSuite == IEEE_80211_PAIRWISE_CIPHER_SUITE_TKIP) { + + if (AKMSuite == NULL) { + return SECURITY_TYPE_UNKNOWN; + } + + if (*AKMSuite == IEEE_80211_AKM_SUITE_8021X_OR_PMKSA || + *AKMSuite == IEEE_80211_AKM_SUITE_8021X_OR_PMKSA_SHA256) { + + return SECURITY_TYPE_WPA_ENTERPRISE; + } else if (*AKMSuite == IEEE_80211_AKM_SUITE_PSK || + *AKMSuite == IEEE_80211_AKM_SUITE_PSK_SHA256){ + + return SECURITY_TYPE_WPA_PERSONAL; + }else { + return SECURITY_TYPE_UNKNOWN; + } + } else { + return SECURITY_TYPE_UNKNOWN; + } +} + +/** + Get supported AKMSuites and CipherSuites from supplicant for a Nic. + + @param[in] Nic The Nic to operate. + + @retval EFI_SUCCESS Get the supported suite list successfully. + @retval EFI_INVALID_PARAMETER No Nic found or supplicant is NULL. + +**/ +EFI_STATUS +WifiMgrGetSupportedSuites ( + IN WIFI_MGR_DEVICE_DATA *Nic + ) +{ + EFI_STATUS Status; + EFI_SUPPLICANT_PROTOCOL *Supplicant; + EFI_80211_AKM_SUITE_SELECTOR *SupportedAKMSuites; + EFI_80211_CIPHER_SUITE_SELECTOR *SupportedSwCipherSuites; + EFI_80211_CIPHER_SUITE_SELECTOR *SupportedHwCipherSuites; + UINTN DataSize; + + SupportedAKMSuites = NULL; + SupportedSwCipherSuites = NULL; + SupportedHwCipherSuites = NULL; + + if (Nic == NULL || Nic->Supplicant == NULL) { + return EFI_INVALID_PARAMETER; + } + + Supplicant = Nic->Supplicant; + + DataSize = 0; + Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedAKMSuites, NULL, &DataSize); + if (Status == EFI_BUFFER_TOO_SMALL && DataSize > 0) { + + SupportedAKMSuites = AllocateZeroPool(DataSize); + if (SupportedAKMSuites == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedAKMSuites, + (UINT8 *) SupportedAKMSuites, &DataSize); + if (!EFI_ERROR (Status)) { + Nic->SupportedSuites.SupportedAKMSuites = SupportedAKMSuites; + } else { + FreePool (SupportedAKMSuites); + } + } else { + SupportedAKMSuites = NULL; + } + + DataSize = 0; + Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedSoftwareCipherSuites, NULL, &DataSize); + if (Status == EFI_BUFFER_TOO_SMALL && DataSize > 0) { + + + SupportedSwCipherSuites = AllocateZeroPool(DataSize); + if (SupportedSwCipherSuites == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedSoftwareCipherSuites, + (UINT8 *) SupportedSwCipherSuites, &DataSize); + if (!EFI_ERROR (Status)) { + Nic->SupportedSuites.SupportedSwCipherSuites = SupportedSwCipherSuites; + } else { + FreePool (SupportedSwCipherSuites); + } + } else { + SupportedSwCipherSuites = NULL; + } + + DataSize = 0; + Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedHardwareCipherSuites, NULL, &DataSize); + if (Status == EFI_BUFFER_TOO_SMALL && DataSize > 0) { + + SupportedHwCipherSuites = AllocateZeroPool(DataSize); + if (SupportedHwCipherSuites == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Status = Supplicant->GetData (Supplicant, EfiSupplicant80211SupportedHardwareCipherSuites, + (UINT8 *) SupportedHwCipherSuites, &DataSize); + if (!EFI_ERROR (Status)) { + Nic->SupportedSuites.SupportedHwCipherSuites = SupportedHwCipherSuites; + } else { + FreePool (SupportedHwCipherSuites); + } + } else { + SupportedHwCipherSuites = NULL; + } + + return EFI_SUCCESS; +} + +/** + Clean secrets from a network profile. + + @param[in] Profile The profile to be cleanned. + +**/ +VOID +WifiMgrCleanProfileSecrets ( + IN WIFI_MGR_NETWORK_PROFILE *Profile + ) +{ + ZeroMem (Profile->Password, sizeof (CHAR16) * PASSWORD_STORAGE_SIZE); + ZeroMem (Profile->EapPassword, sizeof (CHAR16) * PASSWORD_STORAGE_SIZE); + ZeroMem (Profile->PrivateKeyPassword, sizeof (CHAR16) * PASSWORD_STORAGE_SIZE); + + if (Profile->CACertData != NULL) { + + ZeroMem (Profile->CACertData, Profile->CACertSize); + FreePool (Profile->CACertData); + } + Profile->CACertData = NULL; + Profile->CACertSize = 0; + + if (Profile->ClientCertData != NULL) { + + ZeroMem (Profile->ClientCertData, Profile->ClientCertSize); + FreePool (Profile->ClientCertData); + } + Profile->ClientCertData = NULL; + Profile->ClientCertSize = 0; + + if (Profile->PrivateKeyData != NULL) { + + ZeroMem (Profile->PrivateKeyData, Profile->PrivateKeyDataSize); + FreePool (Profile->PrivateKeyData); + } + Profile->PrivateKeyData = NULL; + Profile->PrivateKeyDataSize = 0; +} + +/** + Free all network profiles in a profile list. + + @param[in] ProfileList The profile list to be freed. + +**/ +VOID +WifiMgrFreeProfileList ( + IN LIST_ENTRY *ProfileList + ) +{ + WIFI_MGR_NETWORK_PROFILE *Profile; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + + if (ProfileList == NULL) { + return; + } + + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, ProfileList) { + + Profile = NET_LIST_USER_STRUCT_S (Entry, WIFI_MGR_NETWORK_PROFILE, + Link, WIFI_MGR_PROFILE_SIGNATURE); + + WifiMgrCleanProfileSecrets (Profile); + + if (Profile->Network.AKMSuite != NULL) { + FreePool(Profile->Network.AKMSuite); + } + + if (Profile->Network.CipherSuite != NULL) { + FreePool(Profile->Network.CipherSuite); + } + + FreePool (Profile); + } +} + +/** + Free user configured hidden network list. + + @param[in] HiddenList The hidden network list to be freed. + +**/ +VOID +WifiMgrFreeHiddenList ( + IN LIST_ENTRY *HiddenList + ) +{ + WIFI_HIDDEN_NETWORK_DATA *HiddenNetwork; + LIST_ENTRY *Entry; + LIST_ENTRY *NextEntry; + + if (HiddenList == NULL) { + return; + } + + NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, HiddenList) { + + HiddenNetwork = NET_LIST_USER_STRUCT_S (Entry, WIFI_HIDDEN_NETWORK_DATA, + Link, WIFI_MGR_HIDDEN_NETWORK_SIGNATURE); + FreePool (HiddenNetwork); + } +} + + +/** + Free the resources of a config token. + + @param[in] ConfigToken The config token to be freed. +**/ +VOID +WifiMgrFreeToken ( + IN WIFI_MGR_MAC_CONFIG_TOKEN *ConfigToken + ) +{ + EFI_80211_GET_NETWORKS_RESULT *Result; + + if (ConfigToken == NULL) { + return; + } + + switch (ConfigToken->Type) { + + case TokenTypeGetNetworksToken: + + if (ConfigToken->Token.GetNetworksToken != NULL) { + + gBS->CloseEvent (ConfigToken->Token.GetNetworksToken->Event); + if (ConfigToken->Token.GetNetworksToken->Data != NULL) { + FreePool(ConfigToken->Token.GetNetworksToken->Data); + } + + Result = ConfigToken->Token.GetNetworksToken->Result; + if (Result != NULL) { + FreePool (Result); + } + + FreePool(ConfigToken->Token.GetNetworksToken); + } + + FreePool (ConfigToken); + break; + + case TokenTypeConnectNetworkToken: + + if (ConfigToken->Token.ConnectNetworkToken != NULL) { + + gBS->CloseEvent (ConfigToken->Token.ConnectNetworkToken->Event); + if (ConfigToken->Token.ConnectNetworkToken->Data != NULL) { + FreePool(ConfigToken->Token.ConnectNetworkToken->Data); + } + FreePool(ConfigToken->Token.ConnectNetworkToken); + } + FreePool (ConfigToken); + break; + + case TokenTypeDisconnectNetworkToken: + + if (ConfigToken->Token.DisconnectNetworkToken != NULL) { + + FreePool(ConfigToken->Token.DisconnectNetworkToken); + } + + FreePool (ConfigToken); + break; + + default : + break; + } +} diff --git a/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrMisc.h b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrMisc.h new file mode 100644 index 000000000..ef5b9e5da --- /dev/null +++ b/NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrMisc.h @@ -0,0 +1,262 @@ +/** @file + The Miscellaneous Routines for WiFi Connection Manager. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_WIFI_MGR_MISC_H__ +#define __EFI_WIFI_MGR_MISC_H__ + +/** + Empty function for event process function. + + @param[in] Event The Event needs to be processed + @param[in] Context The context of the event + +**/ +VOID +EFIAPI +WifiMgrInternalEmptyFunction ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Convert the mac address into a hexadecimal encoded ":" seperated string. + + @param[in] Mac The mac address + @param[in] StrSize The size, in bytes, of the output buffer specified by Str + @param[out] Str The storage to return the mac string + +**/ +VOID +WifiMgrMacAddrToStr ( + IN EFI_80211_MAC_ADDRESS *Mac, + IN UINT32 StrSize, + OUT CHAR16 *Str + ); + +/** + Read private key file to buffer. + + @param[in] FileContext The file context of private key file. + @param[out] PrivateKeyDataAddr The buffer address to restore private key file, should be + freed by caller. + @param[out] PrivateKeyDataSize The size of read private key file. + + @retval EFI_SUCCESS Successfully read the private key file. + @retval EFI_INVALID_PARAMETER One or more of the parameters is invalid. + +**/ +EFI_STATUS +WifiMgrReadFileToBuffer ( + IN WIFI_MGR_FILE_CONTEXT *FileContext, + OUT VOID **PrivateKeyDataAddr, + OUT UINTN *PrivateKeyDataSize + ); + + +/** + Get the Nic data by the NicIndex. + + @param[in] Private The pointer to the global private data structure. + @param[in] NicIndex The index indicates the position of wireless NIC. + + @return Pointer to the Nic data, or NULL if not found. + +**/ +WIFI_MGR_DEVICE_DATA * +WifiMgrGetNicByIndex ( + IN WIFI_MGR_PRIVATE_DATA *Private, + IN UINT32 NicIndex + ); + +/** + Find a network profile through its' SSId and securit type, and the SSId is an unicode string. + + @param[in] SSId The target network's SSId. + @param[in] SecurityType The target network's security type. + @param[in] ProfileList The profile list on a Nic. + + @return Pointer to a network profile, or NULL if not found. + +**/ +WIFI_MGR_NETWORK_PROFILE * +WifiMgrGetProfileByUnicodeSSId ( + IN CHAR16 *SSId, + IN UINT8 SecurityType, + IN LIST_ENTRY *ProfileList + ); + +/** + Find a network profile through its' SSId and securit type, and the SSId is an ascii string. + + @param[in] SSId The target network's SSId. + @param[in] SecurityType The target network's security type. + @param[in] ProfileList The profile list on a Nic. + + @return Pointer to a network profile, or NULL if not found. + +**/ +WIFI_MGR_NETWORK_PROFILE * +WifiMgrGetProfileByAsciiSSId ( + IN CHAR8 *SSId, + IN UINT8 SecurityType, + IN LIST_ENTRY *ProfileList + ); + +/** + Find a network profile through its' profile index. + + @param[in] ProfileIndex The target network's profile index. + @param[in] ProfileList The profile list on a Nic. + + @return Pointer to a network profile, or NULL if not found. + +**/ +WIFI_MGR_NETWORK_PROFILE * +WifiMgrGetProfileByProfileIndex ( + IN UINT32 ProfileIndex, + IN LIST_ENTRY *ProfileList + ); + +/** + To test if the AKMSuite is in supported AKMSuite list. + + @param[in] SupportedAKMSuiteCount The count of the supported AKMSuites. + @param[in] SupportedAKMSuiteList The supported AKMSuite list. + @param[in] AKMSuite The AKMSuite to be tested. + + @return True if this AKMSuite is supported, or False if not. + +**/ +BOOLEAN +WifiMgrSupportAKMSuite ( + IN UINT16 SupportedAKMSuiteCount, + IN UINT32 *SupportedAKMSuiteList, + IN UINT32 *AKMSuite + ); + +/** + To check if the CipherSuite is in supported CipherSuite list. + + @param[in] SupportedCipherSuiteCount The count of the supported CipherSuites. + @param[in] SupportedCipherSuiteList The supported CipherSuite list. + @param[in] CipherSuite The CipherSuite to be tested. + + @return True if this CipherSuite is supported, or False if not. + +**/ +BOOLEAN +WifiMgrSupportCipherSuite ( + IN UINT16 SupportedCipherSuiteCount, + IN UINT32 *SupportedCipherSuiteList, + IN UINT32 *CipherSuite + ); + +/** + Check an AKM suite list and a Cipher suite list to see if one or more AKM suites or Cipher suites + are supported and find the matchable security type. + + @param[in] AKMList The target AKM suite list to be checked. + @param[in] CipherList The target Cipher suite list to be checked + @param[in] Nic The Nic to operate, contains the supported AKMSuite list + and supported CipherSuite list + @param[out] SecurityType To identify a security type from the AKM suite list and + Cipher suite list + @param[out] AKMSuiteSupported To identify if this security type is supported. If it is + NULL, overcome this field + @param[out] CipherSuiteSupported To identify if this security type is supported. If it is + NULL, overcome this field + + @retval EFI_SUCCESS This operation has completed successfully. + @retval EFI_INVALID_PARAMETER No Nic found or the suite list is null. + +**/ +EFI_STATUS +WifiMgrCheckRSN ( + IN EFI_80211_AKM_SUITE_SELECTOR *AKMList, + IN EFI_80211_CIPHER_SUITE_SELECTOR *CipherList, + IN WIFI_MGR_DEVICE_DATA *Nic, + OUT UINT8 *SecurityType, + OUT BOOLEAN *AKMSuiteSupported, + OUT BOOLEAN *CipherSuiteSupported + ); + +/** + To get the security type for a certain AKMSuite and CipherSuite. + + @param[in] AKMSuite An certain AKMSuite. + @param[in] CipherSuite An certain CipherSuite. + + @return a security type if found, or SECURITY_TYPE_UNKNOWN. + +**/ +UINT8 +WifiMgrGetSecurityType ( + IN UINT32 *AKMSuite, + IN UINT32 *CipherSuite + ); + +/** + Get supported AKMSuites and CipherSuites from supplicant. + + @param[in] Nic The Nic to operate. + + @retval EFI_SUCCESS Get the supported suite list successfully. + @retval EFI_INVALID_PARAMETER No Nic found or supplicant is NULL. + +**/ +EFI_STATUS +WifiMgrGetSupportedSuites ( + IN WIFI_MGR_DEVICE_DATA *Nic + ); + +/** + Clean secrets from a network profile. + + @param[in] Profile The profile to be cleanned. + +**/ +VOID +WifiMgrCleanProfileSecrets ( + IN WIFI_MGR_NETWORK_PROFILE *Profile + ); + +/** + Free all network profiles in a profile list. + + @param[in] ProfileList The profile list to be freed. + +**/ +VOID +WifiMgrFreeProfileList ( + IN LIST_ENTRY *ProfileList + ); + +/** + Free user configured hidden network list. + + @param[in] HiddenList The hidden network list to be freed. + +**/ +VOID +WifiMgrFreeHiddenList ( + IN LIST_ENTRY *HiddenList + ); + +/** + Free the resources of a config token. + + @param[in] ConfigToken The config token to be freed. + +**/ +VOID +WifiMgrFreeToken ( + IN WIFI_MGR_MAC_CONFIG_TOKEN *ConfigToken + ); + +#endif diff --git a/ShellPkg/Include/Library/ShellLib.h b/ShellPkg/Include/Library/ShellLib.h index 268a767b7..e40dcbb0c 100644 --- a/ShellPkg/Include/Library/ShellLib.h +++ b/ShellPkg/Include/Library/ShellLib.h @@ -128,7 +128,7 @@ ShellOpenFileByDevicePath( otherwise, the Filehandle is NULL. Attributes is valid only for EFI_FILE_MODE_CREATE. - @param[in] FilePath The pointer to file name. + @param[in] FileName The pointer to file name. @param[out] FileHandle The pointer to the file handle. @param[in] OpenMode The mode to open the file with. @param[in] Attributes The file's file attributes. @@ -153,7 +153,7 @@ ShellOpenFileByDevicePath( EFI_STATUS EFIAPI ShellOpenFileByName( - IN CONST CHAR16 *FilePath, + IN CONST CHAR16 *FileName, OUT SHELL_FILE_HANDLE *FileHandle, IN UINT64 OpenMode, IN UINT64 Attributes diff --git a/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c b/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c index 810328761..a81d5bc91 100644 --- a/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c +++ b/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c @@ -2321,23 +2321,6 @@ STATIC CONST GUID_INFO_BLOCK mGuidStringList[] = { {STRING_TOKEN(STR_FVB2), &gEfiFirmwareVolumeBlock2ProtocolGuid, NULL}, {STRING_TOKEN(STR_CPUIO2), &gEfiCpuIo2ProtocolGuid, NULL}, {STRING_TOKEN(STR_LEGACY_R2), &gEfiLegacyRegion2ProtocolGuid, NULL}, - {STRING_TOKEN(STR_SAL_MIP), &gEfiSalMcaInitPmiProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_BS), &gEfiExtendedSalBootServiceProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_BIO), &gEfiExtendedSalBaseIoServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_STALL), &gEfiExtendedSalStallServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_RTC), &gEfiExtendedSalRtcServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_VS), &gEfiExtendedSalVariableServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_MTC), &gEfiExtendedSalMtcServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_RESET), &gEfiExtendedSalResetServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_SC), &gEfiExtendedSalStatusCodeServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_FBS), &gEfiExtendedSalFvBlockServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_MP), &gEfiExtendedSalMpServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_PAL), &gEfiExtendedSalPalServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_BASE), &gEfiExtendedSalBaseServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_MCA), &gEfiExtendedSalMcaServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_PCI), &gEfiExtendedSalPciServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_CACHE), &gEfiExtendedSalCacheServicesProtocolGuid, NULL}, - {STRING_TOKEN(STR_ES_MCA_LOG), &gEfiExtendedSalMcaLogServicesProtocolGuid, NULL}, {STRING_TOKEN(STR_S2ARCH), &gEfiSecurity2ArchProtocolGuid, NULL}, {STRING_TOKEN(STR_EODXE), &gEfiSmmEndOfDxeProtocolGuid, NULL}, {STRING_TOKEN(STR_ISAHC), &gEfiIsaHcProtocolGuid, NULL}, diff --git a/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.inf b/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.inf index 04359f807..ef8508f67 100644 --- a/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.inf +++ b/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.inf @@ -234,23 +234,6 @@ gEfiFirmwareVolumeBlock2ProtocolGuid ## UNDEFINED gEfiCpuIo2ProtocolGuid ## UNDEFINED gEfiLegacyRegion2ProtocolGuid ## UNDEFINED - gEfiSalMcaInitPmiProtocolGuid ## UNDEFINED - gEfiExtendedSalBootServiceProtocolGuid ## UNDEFINED - gEfiExtendedSalBaseIoServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalStallServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalRtcServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalVariableServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalMtcServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalResetServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalStatusCodeServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalFvBlockServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalMpServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalPalServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalBaseServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalMcaServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalPciServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalCacheServicesProtocolGuid ## UNDEFINED - gEfiExtendedSalMcaLogServicesProtocolGuid ## UNDEFINED gEfiSecurity2ArchProtocolGuid ## UNDEFINED gEfiSmmEndOfDxeProtocolGuid ## UNDEFINED gEfiIsaHcProtocolGuid ## UNDEFINED diff --git a/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.uni b/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.uni index 15f391204..3238441b1 100644 --- a/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.uni +++ b/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.uni @@ -274,23 +274,6 @@ #string STR_FVB2 #language en-US "FirmwareVolumeBlock2" #string STR_CPUIO2 #language en-US "CpuIo2" #string STR_LEGACY_R2 #language en-US "LegacyRegion2" -#string STR_SAL_MIP #language en-US "SalMcaInitPmi" -#string STR_ES_BS #language en-US "ExtendedSalBootService" -#string STR_ES_BIO #language en-US "ExtendedSalBaseIoServices" -#string STR_ES_STALL #language en-US "ExtendedSalStallServices" -#string STR_ES_RTC #language en-US "ExtendedSalRtcServices" -#string STR_ES_VS #language en-US "ExtendedSalVariableServices" -#string STR_ES_MTC #language en-US "ExtendedSalMtcServices" -#string STR_ES_RESET #language en-US "ExtendedSalResetServices" -#string STR_ES_SC #language en-US "ExtendedSalStatusCodeServices" -#string STR_ES_FBS #language en-US "ExtendedSalFvBlockServices" -#string STR_ES_MP #language en-US "ExtendedSalMpServices" -#string STR_ES_PAL #language en-US "ExtendedSalPalServices" -#string STR_ES_BASE #language en-US "ExtendedSalBaseServices" -#string STR_ES_MCA #language en-US "ExtendedSalMcaServices" -#string STR_ES_PCI #language en-US "ExtendedSalPciServices" -#string STR_ES_CACHE #language en-US "ExtendedSalCacheServices" -#string STR_ES_MCA_LOG #language en-US "ExtendedSalMcaLogServices" #string STR_S2ARCH #language en-US "Security2Arch" #string STR_EODXE #language en-US "SmmEndOfDxe" #string STR_ISAHC #language en-US "IsaHc" diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Dmem.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Dmem.c index a53b17d5e..ee6d8cba3 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Dmem.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Dmem.c @@ -19,7 +19,7 @@ #include #include #include -#include + /** Make a printable character. @@ -190,10 +190,6 @@ ShellCommandRunDmem ( AcpiTableAddress = (UINT64)(UINTN)gST->ConfigurationTable[TableWalker].VendorTable; continue; } - if (CompareGuid(&gST->ConfigurationTable[TableWalker].VendorGuid, &gEfiSalSystemTableGuid)) { - SalTableAddress = (UINT64)(UINTN)gST->ConfigurationTable[TableWalker].VendorTable; - continue; - } if (CompareGuid(&gST->ConfigurationTable[TableWalker].VendorGuid, &gEfiSmbiosTableGuid)) { SmbiosTableAddress = (UINT64)(UINTN)gST->ConfigurationTable[TableWalker].VendorTable; continue; diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.inf b/ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.inf index 6b837b5fa..ff92f47bd 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.inf +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.inf @@ -133,7 +133,6 @@ gEfiSmbiosTableGuid ## SOMETIMES_CONSUMES ## SystemTable gEfiSmbios3TableGuid ## SOMETIMES_CONSUMES ## SystemTable gEfiMpsTableGuid ## SOMETIMES_CONSUMES ## SystemTable - gEfiSalSystemTableGuid ## SOMETIMES_CONSUMES ## SystemTable gEfiAcpi10TableGuid ## SOMETIMES_CONSUMES ## SystemTable gEfiAcpi20TableGuid ## SOMETIMES_CONSUMES ## SystemTable gShellDebug1HiiGuid ## SOMETIMES_CONSUMES ## HII diff --git a/ShellPkg/Library/UefiShellNetwork1CommandsLib/UefiShellNetwork1CommandsLib.inf b/ShellPkg/Library/UefiShellNetwork1CommandsLib/UefiShellNetwork1CommandsLib.inf index 4af9201cd..af48e2083 100644 --- a/ShellPkg/Library/UefiShellNetwork1CommandsLib/UefiShellNetwork1CommandsLib.inf +++ b/ShellPkg/Library/UefiShellNetwork1CommandsLib/UefiShellNetwork1CommandsLib.inf @@ -34,6 +34,7 @@ MdePkg/MdePkg.dec ShellPkg/ShellPkg.dec MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec [LibraryClasses] MemoryAllocationLib diff --git a/ShellPkg/Library/UefiShellNetwork2CommandsLib/UefiShellNetwork2CommandsLib.inf b/ShellPkg/Library/UefiShellNetwork2CommandsLib/UefiShellNetwork2CommandsLib.inf index 3502d2ae4..5400167bf 100644 --- a/ShellPkg/Library/UefiShellNetwork2CommandsLib/UefiShellNetwork2CommandsLib.inf +++ b/ShellPkg/Library/UefiShellNetwork2CommandsLib/UefiShellNetwork2CommandsLib.inf @@ -34,6 +34,7 @@ MdePkg/MdePkg.dec ShellPkg/ShellPkg.dec MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec [LibraryClasses] MemoryAllocationLib diff --git a/cbuild.bat b/cbuild.bat index 1ab106151..082f9fc4a 100644 --- a/cbuild.bat +++ b/cbuild.bat @@ -48,9 +48,9 @@ rem # get the current revision number :getrevision rem get svn revision number set F_VER_TXT=vers.txt - svnversion -n>%F_VER_TXT% +rem # svnversion -n>%F_VER_TXT% set /P s=<%F_VER_TXT% - del %F_VER_TXT% +rem # del %F_VER_TXT% set SVNREVISION= rem # get the current revision number @@ -143,6 +143,7 @@ rem # setup build if defined EDK_TOOLS_BIN ( set "BASETOOLS_DIR=%EDK_TOOLS_BIN%" ) + set "NASM_INC=-I%WORKSPACE%\MdePkg\Include\X64" rem # pass 1-call param if defined EDK_BUILDINFOS goto getEDKBuildInfos diff --git a/rEFIt_UEFI/refit.inf b/rEFIt_UEFI/refit.inf index 04f1bb4b5..a14eeeeda 100644 --- a/rEFIt_UEFI/refit.inf +++ b/rEFIt_UEFI/refit.inf @@ -166,6 +166,7 @@ CloverPkg.dec MdePkg/MdePkg.dec MdeModulePkg/MdeModulePkg.dec + NetworkPkg/NetworkPkg.dec IntelFrameworkPkg/IntelFrameworkPkg.dec IntelFrameworkModulePkg/IntelFrameworkModulePkg.dec