All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Kiper <dkiper@net-space.pl>
To: Javier Martinez Canillas <javierm@redhat.com>
Cc: grub-devel@gnu.org, Vladimir Serbinenko <phcoder@gmail.com>,
	Daniel Kiper <daniel.kiper@oracle.com>,
	Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
Subject: Re: [PATCH 1/3] Set net_<interface>_client{id, uuid} variables from DHCP options
Date: Tue, 15 Oct 2019 16:19:31 +0200	[thread overview]
Message-ID: <20191015141931.auoezcg5d632o3uf@tomti.i.net-space.pl> (raw)
In-Reply-To: <20191004224427.22480-2-javierm@redhat.com>

On Sat, Oct 05, 2019 at 12:44:25AM +0200, Javier Martinez Canillas wrote:
> From: Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
>
> This patch sets a net_<interface>_clientid and net_<interface>_clientuuid
> GRUB environment variables, using the DHCP client ID and UUID options if
> these are found.
>
> In the same way than net_<interface>_<option> variables are set for other
> options such domain name, boot file, next server, etc.
>
> Signed-off-by: Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
> Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>

In general "Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>"
but a few nit picks below...

> ---
>
>  grub-core/net/bootp.c | 85 +++++++++++++++++++++++++++++++++++++++----
>  include/grub/net.h    |  2 +
>  2 files changed, 79 insertions(+), 8 deletions(-)
>
> diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c
> index 04cfbb04504..0e6e41a1699 100644
> --- a/grub-core/net/bootp.c
> +++ b/grub-core/net/bootp.c
> @@ -95,6 +95,49 @@ enum
>  /* Max timeout when waiting for BOOTP/DHCP reply */
>  #define GRUB_DHCP_MAX_PACKET_TIMEOUT 32
>
> +static char *
> +grub_env_write_readonly (struct grub_env_var *var __attribute__ ((unused)),
> +                         const char *val __attribute__ ((unused)))

s/grub_env_write_readonly/grub_env_ro_write/?

> +{
> +  return NULL;
> +}
> +
> +static void
> +set_env_limn_ro (const char *intername, const char *suffix,
> +                 const char *value, grub_size_t len)

I have hard time reading this. What does "limn" stand for?
Could we make the name of this function less cryptic?

> +{
> +  char *varname, *varvalue;
> +  char *ptr;

Please add empty line here.

> +  varname = grub_xasprintf ("net_%s_%s", intername, suffix);
> +  if (!varname)
> +    return;
> +  for (ptr = varname; *ptr; ptr++)
> +    if (*ptr == ':')
> +      *ptr = '_';
> +  varvalue = grub_malloc (len + 1);

grub_zalloc() and then you can...

> +  if (!varvalue)
> +    {
> +      grub_free (varname);
> +      return;
> +    }
> +
> +  grub_memcpy (varvalue, value, len);
> +  varvalue[len] = 0;

...drop this...

Daniel

> +  grub_env_set (varname, varvalue);
> +  grub_register_variable_hook (varname, 0, grub_env_write_readonly);
> +  grub_env_export (varname);
> +  grub_free (varname);
> +  grub_free (varvalue);
> +}
> +
> +static char
> +hexdigit (grub_uint8_t val)
> +{
> +  if (val < 10)
> +    return val + '0';
> +  return val + 'a' - 10;
> +}
> +
>  static const void *
>  find_dhcp_option (const struct grub_net_bootp_packet *bp, grub_size_t size,
>  		  grub_uint8_t opt_code, grub_uint8_t *opt_len)
> @@ -152,6 +195,9 @@ again:
>        if (i + taglength >= size)
>  	return NULL;
>
> +      grub_dprintf("net", "DHCP option %u (0x%02x) found with length %u.\n",
> +                   tagtype, tagtype, taglength);
> +
>        /* FIXME RFC 3396 options concatentation */
>        if (tagtype == opt_code)
>  	{
> @@ -354,6 +400,37 @@ grub_net_configure_by_dhcp_ack (const char *name,
>      }
>    grub_net_add_ipv4_local (inter, mask);
>
> +  opt = find_dhcp_option (bp, size, GRUB_NET_BOOTP_CLIENT_ID, &opt_len);
> +  if (opt)
> +    {
> +      set_env_limn_ro (name, "clientid", (char *) opt, opt_len);
> +    }
> +
> +  opt = find_dhcp_option (bp, size, GRUB_NET_BOOTP_CLIENT_UUID, &opt_len);
> +  if (opt && opt_len == 17)
> +    {
> +      /* The format is 9cfe245e-d0c8-bd45-a79f-54ea5fbd3d97 */
> +
> +      opt += 1;
> +      opt_len -= 1;
> +
> +      char *val = grub_malloc (2 * opt_len + 4 + 1);
> +      int i = 0;
> +      int j = 0;
> +      for (i = 0; i < opt_len; i++)
> +        {
> +          val[2 * i + j] = hexdigit (opt[i] >> 4);
> +          val[2 * i + 1 + j] = hexdigit (opt[i] & 0xf);
> +
> +          if ((i == 3) || (i == 5) || (i == 7) || (i == 9))
> +            {
> +              j++;
> +              val[2 * i + 1+ j] = '-';
> +            }
> +        }
> +      set_env_limn_ro (name, "clientuuid", (char *) val, 2 * opt_len + 4);
> +    }
> +
>    /* We do not implement dead gateway detection and the first entry SHOULD
>       be preferred one */
>    opt = find_dhcp_option (bp, size, GRUB_NET_BOOTP_ROUTER, &opt_len);
> @@ -631,14 +708,6 @@ grub_net_process_dhcp (struct grub_net_buff *nb,
>      }
>  }
>
> -static char
> -hexdigit (grub_uint8_t val)
> -{
> -  if (val < 10)
> -    return val + '0';
> -  return val + 'a' - 10;
> -}
> -
>  static grub_err_t
>  grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)),
>  		  int argc, char **args)
> diff --git a/include/grub/net.h b/include/grub/net.h
> index 4a9069a1474..556c54e579f 100644
> --- a/include/grub/net.h
> +++ b/include/grub/net.h
> @@ -462,6 +462,8 @@ enum
>      GRUB_NET_BOOTP_DOMAIN = 0x0f,
>      GRUB_NET_BOOTP_ROOT_PATH = 0x11,
>      GRUB_NET_BOOTP_EXTENSIONS_PATH = 0x12,
> +    GRUB_NET_BOOTP_CLIENT_ID = 0x3d,
> +    GRUB_NET_BOOTP_CLIENT_UUID = 0x61,
>      GRUB_NET_DHCP_REQUESTED_IP_ADDRESS = 50,
>      GRUB_NET_DHCP_OVERLOAD = 52,
>      GRUB_NET_DHCP_MESSAGE_TYPE = 53,
> --
> 2.21.0


  reply	other threads:[~2019-10-15 15:07 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-04 22:44 [PATCH 0/3] Search for specific config files using UUID, MAC and IP Javier Martinez Canillas
2019-10-04 22:44 ` [PATCH 1/3] Set net_<interface>_client{id, uuid} variables from DHCP options Javier Martinez Canillas
2019-10-15 14:19   ` Daniel Kiper [this message]
2019-10-18  7:46     ` Javier Martinez Canillas
2019-10-04 22:44 ` [PATCH 2/3] Add %X option to printf functions Javier Martinez Canillas
2019-10-05  1:47   ` Steve McIntyre
2019-10-05  7:15     ` Javier Martinez Canillas
2019-10-15 14:26     ` Daniel Kiper
2019-10-15 14:24   ` Daniel Kiper
2019-10-04 22:44 ` [PATCH 3/3] Search for specific config files for netboot Javier Martinez Canillas
2019-10-15 14:35   ` Daniel Kiper
2019-10-15 14:37 ` [PATCH 0/3] Search for specific config files using UUID, MAC and IP Daniel Kiper
2019-10-18  8:00   ` Javier Martinez Canillas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191015141931.auoezcg5d632o3uf@tomti.i.net-space.pl \
    --to=dkiper@net-space.pl \
    --cc=daniel.kiper@oracle.com \
    --cc=grub-devel@gnu.org \
    --cc=javierm@redhat.com \
    --cc=pfsmorigo@br.ibm.com \
    --cc=phcoder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.