Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1kGOL4-0000yE-57 for mharc-grub-devel@gnu.org; Thu, 10 Sep 2020 11:18:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39110) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kGOL1-0000xx-6e for grub-devel@gnu.org; Thu, 10 Sep 2020 11:18:15 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:35836) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kGOKz-0007O2-1A for grub-devel@gnu.org; Thu, 10 Sep 2020 11:18:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1599751091; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=fSXaQaZ3pjwhNJcc7oShaX+vuhLi65RhBaJJ2F1a5sU=; b=VG+HfdH1b/xIebEO722NxfNX1FpfOTIyIV2TnIAqzfn+qBXXH69r+rsX5uAyIMtlybkgVC UAlGnJOf1dG31bjMZD7smusd0Y+UidK1lAMBUsBO1K1Sf/FZKcaDYKmoSS7GOAG/JeyjJS sNVbFv4LboNuiSITQGjskoK6FMJtarU= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-588-TNWXCaz0PWyl8-wByjBSTw-1; Thu, 10 Sep 2020 11:18:03 -0400 X-MC-Unique: TNWXCaz0PWyl8-wByjBSTw-1 Received: by mail-wr1-f69.google.com with SMTP id a12so2377443wrg.13 for ; Thu, 10 Sep 2020 08:18:03 -0700 (PDT) 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:mime-version :content-transfer-encoding; bh=fSXaQaZ3pjwhNJcc7oShaX+vuhLi65RhBaJJ2F1a5sU=; b=EdSI7ZneKPjRhETqRTgFzgUrJjShgQsnTrPyypC2MJBaANpQJZQILfpMt2UoTOuRHT Sf2RxvmRSjnFbM4iO/aBrxNKDmNYlHLuZBwtyQHDWtPhJSoLuNiVdmAx5K2C1Y1+1xri 0oqUb/ThatRB4AZKda21Supck2TKnTDGtjdh6kk7clApRcILquwevX5LUPhl2ql/24EA MTHA7rE7sm1qEE8xqIO43/6nplo7bX3vH/9RIUojy/UW6k2Inld3Xg+92PAae/1xtiKf M4sMBZeOD4zUEoDmhVMB/220jFIb7NY51A76s1EvnbcOHdNbLCIz3UWPiI/JvL06c253 nREA== X-Gm-Message-State: AOAM5325Tb7DAYTTaA8PMNjN7kvnd7+v19andXRwCelVK0G/HNnmB8ma HtqivfcTxu8RipgXFHeBrtwryLDqfhUlXXe+4B3ESWYS8DsAcVKKlyaGwz1NaV82OkRR9l5+f5O HT40BfeD10q0= X-Received: by 2002:a5d:6311:: with SMTP id i17mr8982537wru.243.1599751081765; Thu, 10 Sep 2020 08:18:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwecZExJcPgl0C4anUa5VziJbC7rvnjcbzYbGHR3I05cM44vaePrO2vMZPXlkPa2izY0k/WEw== X-Received: by 2002:a5d:6311:: with SMTP id i17mr8982515wru.243.1599751081517; Thu, 10 Sep 2020 08:18:01 -0700 (PDT) Received: from minerva.redhat.com ([92.176.231.106]) by smtp.gmail.com with ESMTPSA id h17sm10132941wro.27.2020.09.10.08.18.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 08:18:00 -0700 (PDT) From: Javier Martinez Canillas To: grub-devel@gnu.org Cc: Javier Martinez Canillas , Peter Jones , Alexey Makhalov , Daniel Kiper Subject: [PATCH v2] tftp: roll-over block counter to prevent data packets timeouts Date: Thu, 10 Sep 2020 17:17:57 +0200 Message-Id: <20200910151757.250274-1-javierm@redhat.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=javierm@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=216.205.24.124; envelope-from=javierm@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/10 08:38:09 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Sep 2020 15:18:15 -0000 Commit 781b3e5efc3 (tftp: Do not use priority queue) caused a regression when fetching files over TFTP whose size is bigger than 65535 * block size. grub> linux /images/pxeboot/vmlinuz grub> echo $? 0 grub> initrd /images/pxeboot/initrd.img error: timeout reading '/images/pxeboot/initrd.img'. grub> echo $? 28 It is caused by the block number counter being a 16-bit field, which leads to a maximum file size of ((1 << 16) - 1) * block size. Because GRUB sets the block size to 1024 octets (by using the TFTP Blocksize Option from RFC 2348 [0]), the maximum file size that can be transferred is 67107840 bytes. The TFTP PROTOCOL (REVISION 2) RFC 1350 [1] does not mention what a client should do when a file size is bigger than the maximum, but most TFTP hosts support the block number counter to be rolled over. That is, acking a data packet with a block number of 0 is taken as if the 65356th block was acked. It was working before because the block counter roll-over was happening due an overflow. But that got fixed by the mentioned commit, which led to the regression when attempting to fetch files larger than the maximum size. To allow TFTP file transfers of unlimited size again, re-introduce a block counter roll-over so the data packets are acked preventing the timeouts. [0]: https://tools.ietf.org/html/rfc2348 [1]: https://tools.ietf.org/html/rfc1350 Fixes: 781b3e5efc3 (tftp: Do not use priority queue) Suggested-by: Peter Jones Signed-off-by: Javier Martinez Canillas --- Changes in v2: - Remove unnecessary quotes when referring to a commit. - Use type casting instead of a mask to roll-over. grub-core/net/tftp.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/grub-core/net/tftp.c b/grub-core/net/tftp.c index 644135caf46..ec85fce3b1b 100644 --- a/grub-core/net/tftp.c +++ b/grub-core/net/tftp.c @@ -183,11 +183,22 @@ tftp_receive (grub_net_udp_socket_t sock __attribute__ ((unused)), return GRUB_ERR_NONE; } - /* Ack old/retransmitted block. */ - if (grub_be_to_cpu16 (tftph->u.data.block) < data->block + 1) + /* + * Ack old/retransmitted block. + * + * The block number is a 16-bit counter, thus the maximum file size that + * could be transfered is 65535 * block size. Most TFTP hosts support to + * roll-over the block counter to allow unlimited transfer file size. + * + * This behavior is not defined in the RFC 1350 [0] but is implemented by + * most TFTP clients and hosts. + * + * [0]: https://tools.ietf.org/html/rfc1350 + */ + if (grub_be_to_cpu16 (tftph->u.data.block) < ((grub_uint16_t)(data->block + 1))) ack (data, grub_be_to_cpu16 (tftph->u.data.block)); /* Ignore unexpected block. */ - else if (grub_be_to_cpu16 (tftph->u.data.block) > data->block + 1) + else if (grub_be_to_cpu16 (tftph->u.data.block) > ((grub_uint16_t)(data->block + 1))) grub_dprintf ("tftp", "TFTP unexpected block # %d\n", tftph->u.data.block); else { -- 2.28.0