Received: from list by lists.gnu.org with archive (Exim 4.71) id 1h1qO3-0004cn-AX for mharc-grub-devel@gnu.org; Thu, 07 Mar 2019 05:36:27 -0500 Received: from eggs.gnu.org ([209.51.188.92]:51095) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h1qO0-0004ar-P2 for grub-devel@gnu.org; Thu, 07 Mar 2019 05:36:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h1qNz-0004fw-O5 for grub-devel@gnu.org; Thu, 07 Mar 2019 05:36:24 -0500 Received: from foss.arm.com ([217.140.101.70]:40152) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h1qNz-0004bM-9a for grub-devel@gnu.org; Thu, 07 Mar 2019 05:36:23 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C71D3EBD; Thu, 7 Mar 2019 02:36:20 -0800 (PST) Received: from donnerap.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 920833F706; Thu, 7 Mar 2019 02:36:19 -0800 (PST) Date: Thu, 7 Mar 2019 10:36:12 +0000 From: Andre Przywara To: Daniel Kiper Cc: Daniel Kiper , Vladimir Serbinenko , Andrei Borzenkov , Mark Rutland , grub-devel@gnu.org Subject: Re: [PATCH v2 6/9] net: dhcp: use DHCP options for name and bootfile Message-ID: <20190307103612.14cdc5d9@donnerap.cambridge.arm.com> In-Reply-To: <20190221184908.tqkvqt3bseydcnk6@tomti.i.net-space.pl> References: <20190212174700.184741-1-andre.przywara@arm.com> <20190212174700.184741-7-andre.przywara@arm.com> <20190221184908.tqkvqt3bseydcnk6@tomti.i.net-space.pl> Organization: ARM X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; aarch64-unknown-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/1tXYgWpmB5VEudshMz/C0hx" X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 217.140.101.70 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 Mar 2019 10:36:26 -0000 --MP_/1tXYgWpmB5VEudshMz/C0hx Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline On Thu, 21 Feb 2019 19:49:08 +0100 Daniel Kiper wrote: > On Tue, Feb 12, 2019 at 05:46:57PM +0000, Andre Przywara wrote: > > From: Andrei Borzenkov > > > > The BOOTP RFC describes the boot file name and the server name as being > > part of the integral BOOTP data structure, with some limits on the size > > of them. > > DHCP extends this by allowing them to be separate DHCP options, which is > > more flexible. > > > > Teach the code dealing with those fields to check for those DHCP options > > first and use this information, if provided. We fall back to using the > > BOOTP information if those options are not used. > > > > Signed-off-by: Andre Przywara > > --- > > grub-core/net/bootp.c | 72 +++++++++++++++++++++++++++++++------------ > > include/grub/net.h | 2 ++ > > 2 files changed, 54 insertions(+), 20 deletions(-) > > > > diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c > > index 42117b72d..0c6756ea0 100644 > > --- a/grub-core/net/bootp.c > > +++ b/grub-core/net/bootp.c > > @@ -169,7 +169,9 @@ grub_net_configure_by_dhcp_ack (const char *name, > > int mask = -1; > > char server_ip[sizeof ("xxx.xxx.xxx.xxx")]; > > const grub_uint8_t *opt; > > - grub_uint8_t opt_len; > > + grub_uint8_t opt_len, overload = 0; > > + const char *boot_file = 0, *server_name = 0; > > + grub_size_t boot_file_len, server_name_len; > > > > addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4; > > addr.ipv4 = bp->your_ip; > > @@ -188,9 +190,36 @@ grub_net_configure_by_dhcp_ack (const char *name, > > if (!inter) > > return 0; > > > > - if (size > OFFSET_OF (boot_file, bp)) > > - grub_env_set_net_property (name, "boot_file", bp->boot_file, > > - sizeof (bp->boot_file)); > > + opt = find_dhcp_option (bp, size, GRUB_NET_DHCP_OVERLOAD, &opt_len); > > + if (opt && opt_len == 1) > > + overload = *opt; > > + > > + opt = find_dhcp_option (bp, size, GRUB_NET_DHCP_TFTP_SERVER_NAME, &opt_len); > > + if (opt && opt_len) > > + { > > + server_name = (const char *) opt; > > + server_name_len = opt_len; > > + } > > + else if (size > OFFSET_OF (server_name, bp) && !(overload & GRUB_DHCP_OPT_OVERLOAD_SNAME) && > > + bp->server_name[0]) > > + { > > + server_name = bp->server_name; > > + server_name_len = sizeof (bp->server_name); > > + } > > + > > + opt = find_dhcp_option (bp, size, GRUB_NET_DHCP_BOOTFILE_NAME, &opt_len); > > + if (opt && opt_len) > > + { > > + boot_file = (const char *) opt; > > + boot_file_len = opt_len; > > + } > > + else if (size > OFFSET_OF (boot_file, bp) && !(overload && GRUB_DHCP_OPT_OVERLOAD_FILE) && > > + bp->boot_file[0]) > > + { > > + boot_file = bp->boot_file; > > + boot_file_len = sizeof (bp->boot_file); > > + } > > + > > if (bp->server_ip) > > { > > grub_snprintf (server_ip, sizeof (server_ip), "%d.%d.%d.%d", > > @@ -221,35 +250,38 @@ grub_net_configure_by_dhcp_ack (const char *name, > > *device = grub_xasprintf ("tftp,%s", server_ip); > > grub_print_error (); > > } > > - if (size > OFFSET_OF (server_name, bp) > > - && bp->server_name[0]) > > + > > + if (server_name) > > { > > - grub_env_set_net_property (name, "dhcp_server_name", bp->server_name, > > - sizeof (bp->server_name)); > > + grub_env_set_net_property (name, "dhcp_server_name", server_name, server_name_len); > > if (is_def && !grub_net_default_server) > > { > > - grub_net_default_server = grub_strdup (bp->server_name); > > + grub_net_default_server = grub_strdup (server_name); > > grub_print_error (); > > } > > if (device && !*device) > > { > > - *device = grub_xasprintf ("tftp,%s", bp->server_name); > > + *device = grub_xasprintf ("tftp,%s", server_name); > > grub_print_error (); > > } > > } > > > > - if (size > OFFSET_OF (boot_file, bp) && path) > > + if (boot_file) > > { > > - *path = grub_strndup (bp->boot_file, sizeof (bp->boot_file)); > > - grub_print_error (); > > - if (*path) > > + grub_env_set_net_property (name, "boot_file", boot_file, boot_file_len); > > + if (path) > > { > > - char *slash; > > - slash = grub_strrchr (*path, '/'); > > - if (slash) > > - *slash = 0; > > - else > > - **path = 0; > > + *path = grub_strndup (boot_file, boot_file_len); > > + grub_print_error (); > > + if (*path) > > + { > > + char *slash; > > + slash = grub_strrchr (*path, '/'); > > + if (slash) > > + *slash = 0; > > + else > > + **path = 0; > > + } > > I am not sure why you are changing the code starting from > "if (size > OFFSET_OF (boot_file, bp) && path)". Could you > enlighten me? The diff is indeed somewhat confusing. So several things are changed here: - The "boot file" could be specified via *two* ways now, as a DHCP option or using the legacy BOOTP structure field. This is detected at separate places, that's why we change the first line to check for a valid boot_file pointer, instead of hardcoding the BOOTP structure field way here. - grub_env_set_net_property() was called before when detecting the BOOTP structure field (see above), we do it now here, to cover both ways. - There is an additional molly guard to check that "path" is not a NULL pointer. I am attaching a slightly modified wide diff (diff -yb) to make this clearer. Cheers, Andre. --MP_/1tXYgWpmB5VEudshMz/C0hx Content-Type: text/x-patch Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=boot_file.diff if (size > OFFSET_OF (boot_file, bp) && path) | if (boot_file) { { > if (path) > { > grub_env_set_net_property (name, "boot_file", boot_file *path = grub_strndup (bp->boot_file, sizeof (bp->boot_f ~ *path = grub_strndup (boot_file, boot_file_len); grub_print_error (); grub_print_error (); if (*path) if (*path) { { char *slash; char *slash; slash = grub_strrchr (*path, '/'); slash = grub_strrchr (*path, '/'); if (slash) if (slash) *slash = 0; *slash = 0; else else **path = 0; **path = 0; > } } } } } --MP_/1tXYgWpmB5VEudshMz/C0hx--