Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1kD5SO-0001rB-TD for mharc-grub-devel@gnu.org; Tue, 01 Sep 2020 08:32:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58676) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kD5SN-0001pG-UU for grub-devel@gnu.org; Tue, 01 Sep 2020 08:32:12 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:51315 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kD5SL-0008Dm-3Z for grub-devel@gnu.org; Tue, 01 Sep 2020 08:32:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598963527; 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=RPkou/evpDdTSruwS/U6y0ONVcEuPmI8Ja3vjqrQiPg=; b=Yh02aPEo046+h55JWk2fNVED7VVHS4JFx2uePbhwycj4BUKpBuIS2eLKFikDMjQSWN7SxW XD8ICVIPy0f1st23z3y/CFaOBsp+mLuki6tpjUjI2ixypd4Dt9c9178jpeycL/d/WyA9mF 28t19sxZo/WaxAMSlUCEdKVpqQsLL5U= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-243-2Y89GApnOQyMI_N1rwoa0A-1; Tue, 01 Sep 2020 08:30:55 -0400 X-MC-Unique: 2Y89GApnOQyMI_N1rwoa0A-1 Received: by mail-wr1-f72.google.com with SMTP id n15so510203wrv.23 for ; Tue, 01 Sep 2020 05:30:55 -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=RPkou/evpDdTSruwS/U6y0ONVcEuPmI8Ja3vjqrQiPg=; b=Z54wah6dE7Ev5x126KC24BtxRJB0Faa2ZrdWVu4DdKjBKGWGQ1fbgWQmAvosCDD+B7 oWJHNXbD2mBWvFwxEY9J66Gf0EbUvVSZ9H5V3mhvoCORtCgW7VVFIudgGNagI3C/VEre L+5mPa8BARpAipnP6qfMgGqVi29VzKmQeR3pfW4Up0Gl0yEM7mH3SLxva3WFAM6hpZkl p8hdcbQb2Hj4WekDzJTqRZVbpSvURVvbd9tPVGz2TipKexi1R7CVP49OJTvC+hkMmQOr z/GfA6OC8QzmvHtef0pX5lg4nPI4KdjOSSUVsibUERk114bFPUXlPdqVykszxrDBbGlR +heg== X-Gm-Message-State: AOAM531XqWitSjaNjnKM6gfJIa0T7ARbqlHhAgTedrdlYD2LbnLizWig ZqS7ItapVAwzxccpOnPipHdXfoILyID7z8ohJdWdjmRzw6dYzYJJmzOEQcuIR0lrtsY2DX/3UmJ QRW9Jb9Bm/0k= X-Received: by 2002:a1c:f60e:: with SMTP id w14mr1497275wmc.19.1598963453288; Tue, 01 Sep 2020 05:30:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyAdngoMtTCQRGOzB+NdqAlK17Hl85s8JWsgLckfzP6NeiWq4Yr6cwHJHmevFVbU734vd19Hg== X-Received: by 2002:a1c:f60e:: with SMTP id w14mr1497254wmc.19.1598963452959; Tue, 01 Sep 2020 05:30:52 -0700 (PDT) Received: from fedora.redhat.com ([92.176.231.106]) by smtp.gmail.com with ESMTPSA id h8sm2091801wrw.68.2020.09.01.05.30.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Sep 2020 05:30:52 -0700 (PDT) From: Javier Martinez Canillas To: grub-devel@gnu.org Cc: Javier Martinez Canillas , Peter Jones , Alexey Makhalov , Daniel Kiper Subject: [PATCH] tftp: roll-over block counter to prevent data packets timeouts Date: Tue, 1 Sep 2020 14:30:35 +0200 Message-Id: <20200901123035.1048013-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=207.211.31.81; envelope-from=javierm@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/01 02:08:15 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_H2=-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: Tue, 01 Sep 2020 12:32:12 -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 --- grub-core/net/tftp.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/grub-core/net/tftp.c b/grub-core/net/tftp.c index 644135caf46..ba90c2bc45a 100644 --- a/grub-core/net/tftp.c +++ b/grub-core/net/tftp.c @@ -29,6 +29,8 @@ GRUB_MOD_LICENSE ("GPLv3+"); +#define BLK_MASK ((1 << 16) - 1) + /* IP port for the MTFTP server used for Intel's PXE */ enum { @@ -183,8 +185,19 @@ 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) < ((data->block + 1) & BLK_MASK)) 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) -- 2.28.0