From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f194.google.com (mail-qt1-f194.google.com [209.85.160.194]) by mx.groups.io with SMTP id smtpd.web10.7694.1589439752989196050 for ; Thu, 14 May 2020 00:02:33 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=JSYd76cG; spf=pass (domain: gmail.com, ip: 209.85.160.194, mailfrom: andrey.warkentin@gmail.com) Received: by mail-qt1-f194.google.com with SMTP id i68so2045638qtb.5 for ; Thu, 14 May 2020 00:02:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=h4yy6bM17Vs4G96fEpbvT+ztxAQVQOglPV+SDB2vQIc=; b=JSYd76cGHJa2WDWvZrzJmAFwzzY/fADRnChIKU8piIHnsClkg7wXQyeKQuz//9E7Xe 2ijcVJBHZHAKOFT+gIO1JpL5Rmr9BmB6gRcjgWwCTkzoK4BeTmRX2RLpI4Tf1bLd/aLz lKUznMR8AWGb2Ma9zNoK5++4Pz3zLuV06lp4IHTEopzExCVPIOQmNOo29WdAxPtAETlO dfEIiCB0V3OY50xXf6kMhESl+f550zbzB84+6R6QH9xsrFCoPbBx3QHS38Bv+o6dyfaO GvRdwPOD1niT/k6k0NEey5cHseMDjahC1YNTkwD17NqGRTtGgN/VtAK+avIVB5QYHC+q q9Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=h4yy6bM17Vs4G96fEpbvT+ztxAQVQOglPV+SDB2vQIc=; b=dftmA8+OUbnAmc1yeNDcl3sNyWvDBxO5QTBEHQ7bS+id+FgbunHJHOgUHNIoc8sdZv V4K6ULs4D8/hNEc+XXkTbfCqF3f0kUGPbBuQTBjP45mhpAyMsuEpMpjoKGJiqZiRyvkI xHBIVnpmTOoFtYJJNd5fwe3a6OZmmVhspRpHyuXUIIR1YPP/b3K+j+Wnzdvn+Tr5HLoY BQBN6UfZUDa/A2eXoIKBMXDfOSwRw4157e2s+tgL/4ufEdzLSRqkQ5hB+5Hqc8BSUlO3 D4IX9xgD2YEArL5FuhnA5tvyyJviruOWkxdXJaljR/TQjscZ3HrcsNzmMDDsZe09tFq5 4oIA== X-Gm-Message-State: AOAM530dVUPsaonrr/hJUPwwZTVPjzvtFV8PUkazPDesFejW3nP5inQQ oFV91ee4ZnMipENeY7gmUlU9t4CNBIY= X-Google-Smtp-Source: ABdhPJz9mFQWom6tJcQ0nrVDTxmknqUUqj3/8SnqFhekl/H3kjGJ0eH1LAMy4folEsVHYm/l9Kh2cg== X-Received: by 2002:ac8:70d:: with SMTP id g13mr2824165qth.91.1589439751834; Thu, 14 May 2020 00:02:31 -0700 (PDT) Return-Path: Received: from localhost.localdomain (c-98-214-99-181.hsd1.il.comcast.net. [98.214.99.181]) by smtp.gmail.com with ESMTPSA id j90sm1904018qte.20.2020.05.14.00.02.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 May 2020 00:02:31 -0700 (PDT) From: "Andrei Warkentin" To: devel@edk2.groups.io Cc: maciej.rabeda@linux.intel.com, jiaxin.wu@intel.com, siyuan.fu@intel.com Subject: [edk2][PATCH 1/1] HttpBoot: handle servers which may FIN after file sizing in HttpBootLoadFile Date: Thu, 14 May 2020 00:02:23 -0700 Message-Id: <20200514070223.48194-1-andrey.warkentin@gmail.com> X-Mailer: git-send-email 2.17.1 Python http.server seems to FIN after the first HEAD request to size the loaded file is completed and ACKed. What happens next is interesting. On low latency connections, the GET request to download may get sent after the server sends the FIN but before the client has a chance to process it. The net result is: - Server ignores GET - HttpBootLoadFile returns EFI_CONNECTION_FIN. Boot fails. In the other case, client handles the FIN before attempting the GET, so there's a proper three-way handshake as part of GET. The solution is to retry HttpBootLoadFile 2 times if it returns EFI_CONNECTION_FIN. This is because HttpBootLoadFile may issue up to 3 requests: HEAD/GET to get size and final GET to load. Some servers may send a FIN after each request. The first request (HEAD) is not supposed to fail this way, so only the two subsequent GET request may result in a FIN. Fixes https://bugzilla.tianocore.org/show_bug.cgi?id=2720 Signed-off-by: Andrei Warkentin --- NetworkPkg/HttpBootDxe/HttpBootImpl.c | 27 +++++++++++++++++++- NetworkPkg/HttpDxe/HttpImpl.c | 5 ++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/NetworkPkg/HttpBootDxe/HttpBootImpl.c b/NetworkPkg/HttpBootDxe/HttpBootImpl.c index 4a51f35cdd..2d74d5f293 100644 --- a/NetworkPkg/HttpBootDxe/HttpBootImpl.c +++ b/NetworkPkg/HttpBootDxe/HttpBootImpl.c @@ -288,6 +288,7 @@ HttpBootDhcp ( @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_CONNECTION_FIN Server had closed the connection while we were waiting downloading. @retval EFI_DEVICE_ERROR An unexpected network error occurred. @retval Others Other errors as indicated. @@ -535,6 +536,7 @@ HttpBootStop ( @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 EFI_CONNECTION_FIN Server had closed the connection while we were waiting for a response. **/ EFI_STATUS @@ -553,6 +555,7 @@ HttpBootDxeLoadFile ( BOOLEAN UsingIpv6; EFI_STATUS Status; HTTP_BOOT_IMAGE_TYPE ImageType; + UINTN MaxTries; if (This == NULL || BufferSize == NULL || FilePath == NULL) { return EFI_INVALID_PARAMETER; @@ -598,7 +601,29 @@ HttpBootDxeLoadFile ( // Load the boot file. // ImageType = ImageTypeMax; - Status = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType); + // + // HttpBootLoadFile may issue up to 2 requests: HEAD/GET to get + // size and final GET to load. Some servers may send a FIN after + // each request. The first request (HEAD) is not supposed to + // fail this way, so only the two possible GETs need the special + // handling. + // + MaxTries = 2; + do { + Status = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType); + if (Status == EFI_CONNECTION_FIN) { + if (Private->HttpCreated) { + // + // Tear down HTTP/TCP state entirely. Http->Configure (NULL) is not + // sufficient (EFI_ACCESS_DENIED from TCP stack on subsequent + // HttpBootLoadFile. + // + HttpIoDestroyIo (&Private->HttpIo); + Private->HttpCreated = FALSE; + } + } + } while (MaxTries-- && Status == EFI_CONNECTION_FIN); + if (EFI_ERROR (Status)) { if (Status == EFI_BUFFER_TOO_SMALL && (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk)) { Status = EFI_WARN_FILE_SYSTEM; diff --git a/NetworkPkg/HttpDxe/HttpImpl.c b/NetworkPkg/HttpDxe/HttpImpl.c index 5a6ecbc9d9..34a33b09f7 100644 --- a/NetworkPkg/HttpDxe/HttpImpl.c +++ b/NetworkPkg/HttpDxe/HttpImpl.c @@ -959,6 +959,8 @@ HttpBodyParserCallback ( @retval EFI_OUT_OF_RESOURCES Failed to complete the operation 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. + @retval EFI_CONNECTION_FIN Server had closed the connection while we were waiting for + a response. **/ EFI_STATUS @@ -1528,6 +1530,9 @@ Error: @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. + + @retval EFI_CONNECTION_FIN Server had closed the connection while we were waiting for + a response. **/ EFI_STATUS EFIAPI -- 2.17.1