Skip to contentSkip to conversation

Misc network boot patches

PATCH In review Matthew Garrett 4 participants 25 messages Jan 24, 2017 – Jan 29, 2017

lore
Share & tools
lore
#0 lore raw

Various patches that we're using to support network boot in our setup. Only number 3 is a bugfix.

#1 lore raw

PATCH 1/4 Allow non-default ports for HTTP requests

Add support for passing ports in HTTP requests. This takes the form of:
(http,serverip:portnum)/file

---
 grub-core/net/http.c |  8 ++++++--
 grub-core/net/net.c  | 10 +++++++++-
 include/grub/net.h   |  1 +
 3 files changed, 16 insertions(+), 3 deletions(-)
grub-core/net/http.c +6 -2
index 5aa4ad3..389a78e 100644
@@ -309,7 +309,7 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)
 {
   http_data_t data = file->data;
   grub_uint8_t *ptr;
-  int i;
+  int i, port;
   struct grub_net_buff *nb;
   grub_err_t err;
 
@@ -390,8 +390,12 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)
   grub_netbuff_put (nb, 2);
   grub_memcpy (ptr, "\r\n", 2);
 
+  if (file->device->net->port)
+    port = file->device->net->port;
+  else
+    port = HTTP_PORT;
   data->sock = grub_net_tcp_open (file->device->net->server,
-				  HTTP_PORT, http_receive,
+				  port, http_receive,
 				  http_err, http_err,
 				  file);
   if (!data->sock)
grub-core/net/net.c +9 -1
index 10773fc..585f4f7 100644
@@ -1261,7 +1261,7 @@ grub_net_open_real (const char *name)
   grub_net_app_level_t proto;
   const char *protname, *server;
   grub_size_t protnamelen;
-  int try;
+  int try, port = 0;
 
   if (grub_strncmp (name, "pxe:", sizeof ("pxe:") - 1) == 0)
     {
@@ -1278,7 +1278,14 @@ grub_net_open_real (const char *name)
   else
     {
       const char *comma;
+      char *colon;
       comma = grub_strchr (name, ',');
+      colon = grub_strchr (name, ':');
+      if (colon)
+	{
+	  port = (int) grub_strtol(colon+1, NULL, 10);
+	  *colon = '\0';
+	}
       if (comma)
 	{
 	  protnamelen = comma - name;
@@ -1310,6 +1317,7 @@ grub_net_open_real (const char *name)
 	    if (!ret)
 	      return NULL;
 	    ret->protocol = proto;
+	    ret->port = port;
 	    ret->server = grub_strdup (server);
 	    if (!ret->server)
 	      {
include/grub/net.h +1 -0
index 2192fa1..c517509 100644
@@ -276,6 +276,7 @@ typedef struct grub_net
   grub_fs_t fs;
   int eof;
   int stall;
+  int port;
 } *grub_net_t;
 
 extern grub_net_t (*EXPORT_VAR (grub_net_open)) (const char *name);
Signature · 1 line
2.9.3
#3 lore raw

PATCH 2/4 Send a user class identifier in bootp requests and tag it as DHCP discover

In reply to Matthew Garrett

It's helpful to determine that a request was sent by grub in order to permit the server to provide different information at different stages of the boot process. Send GRUB2 as a type 77 DHCP option when sending bootp packets in order to make this possible and tag the request as a DHCP discover to convince servers to pay attention to it. Add the client architecture for good measure.

---
 grub-core/net/bootp.c | 42 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)
grub-core/net/bootp.c +41 -1
index 9e2fdb7..0da8e24 100644
@@ -25,6 +25,24 @@
 #include <grub/net/udp.h>
 #include <grub/datetime.h>
 
+#if !defined(GRUB_MACHINE_EFI) && (defined(__i386__) || defined(__x86_64__))
+#define GRUB_NET_BOOTP_ARCH 0x0000
+#elif defined(GRUB_MACHINE_EFI) && defined(__x86_64__)
+#define GRUB_NET_BOOTP_ARCH 0x0007
+#elif defined(GRUB_MACHINE_EFI) && defined(__aarch64__)
+#define GRUB_NET_BOOTP_ARCH 0x000B
+#else
+#error "unknown bootp architecture"
+#endif
+
+static grub_uint8_t dhcp_option_header[] = {GRUB_NET_BOOTP_RFC1048_MAGIC_0,
+					    GRUB_NET_BOOTP_RFC1048_MAGIC_1,
+					    GRUB_NET_BOOTP_RFC1048_MAGIC_2,
+					    GRUB_NET_BOOTP_RFC1048_MAGIC_3};
+static grub_uint8_t grub_userclass[] = {0x4D, 0x06, 0x05, 'G', 'R', 'U', 'B', '2'};
+static grub_uint8_t grub_dhcpdiscover[] = {0x35, 0x01, 0x01};
+static grub_uint8_t grub_dhcptime[] = {0x33, 0x04, 0x00, 0x00, 0x0e, 0x10};
+
 static void
 parse_dhcp_vendor (const char *name, const void *vend, int limit, int *mask)
 {
@@ -499,10 +517,14 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
 	  struct udphdr *udph;
 	  grub_net_network_level_address_t target;
 	  grub_net_link_level_address_t ll_target;
+	  grub_uint8_t *offset;
 
 	  if (!ifaces[j].prev)
 	    continue;
-	  nb = grub_netbuff_alloc (sizeof (*pack) + 64 + 128);
+	  nb = grub_netbuff_alloc (sizeof (*pack) + sizeof(dhcp_option_header)
+				   + sizeof(grub_userclass)
+				   + sizeof(grub_dhcpdiscover)
+				   + sizeof(grub_dhcptime) + 64 + 128);
 	  if (!nb)
 	    {
 	      grub_netbuff_free (nb);
@@ -536,6 +558,24 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
 	  pack->seconds = grub_cpu_to_be16 (t);
 
 	  grub_memcpy (&pack->mac_addr, &ifaces[j].hwaddress.mac, 6); 
+	  offset = (grub_uint8_t *)&pack->vendor;
+	  grub_memcpy (offset, dhcp_option_header, sizeof(dhcp_option_header));
+	  offset += sizeof(dhcp_option_header);
+	  grub_memcpy (offset, grub_dhcpdiscover, sizeof(grub_dhcpdiscover));
+	  offset += sizeof(grub_dhcpdiscover);
+	  grub_memcpy (offset, grub_userclass, sizeof(grub_userclass));
+	  offset += sizeof(grub_userclass);
+	  grub_memcpy (offset, grub_dhcptime, sizeof(grub_dhcptime));
+
+	  /* insert Client System Architecture (option 93) */
+	  offset += sizeof(grub_dhcptime);
+	  offset[0] = 93;
+	  offset[1] = 2;
+	  offset[2] = (GRUB_NET_BOOTP_ARCH >> 8);
+	  offset[3] = (GRUB_NET_BOOTP_ARCH & 0xFF);
+
+	  /* option terminator */
+	  offset[4] = 255;
 
 	  grub_netbuff_push (nb, sizeof (*udph));
 
Signature · 1 line
2.9.3
#5 lore raw

PATCH 3/4 Don't allocate a new address buffer if we receive multiple DNS responses

In reply to Matthew Garrett

The current logic in the DNS resolution code allocates an address buffer based on the number of addresses in the response packet. If we receive multiple response packets in response to a single query packet, this means that we will reallocate a new buffer large enough for only the addresses in that specific packet, discarding any previous results in the process. Worse, we still keep track of the *total* number of addresses resolved in response to this query, not merely the number in the packet being currently processed. Use realloc() rather than malloc() to avoid overwriting the existing data, and allocate a buffer large enough for the total set of addresses rather than merely the number in this specific response.

---
 grub-core/net/dns.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/net/dns.c +2 -2
index 5d9afe0..5deb1ef 100644
@@ -285,8 +285,8 @@ recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)),
       ptr++;
       ptr += 4;
     }
-  *data->addresses = grub_malloc (sizeof ((*data->addresses)[0])
-				 * grub_be_to_cpu16 (head->ancount));
+  *data->addresses = grub_realloc (*data->addresses, sizeof ((*data->addresses)[0])
+		     * (grub_be_to_cpu16 (head->ancount) + *data->naddresses));
   if (!*data->addresses)
     {
       grub_errno = GRUB_ERR_NONE;
Signature · 1 line
2.9.3
#8 lore raw

PATCH 4/4 Allow protocol to be separated from host with a semicolon

In reply to Matthew Garrett

Some DHCP servers (such as dnsmasq) tokenise parameters with commas, making it impossible to pass boot files with commas in them. Allow using a semicolon to separate the protocol from host if a comma wasn't found.

---
 grub-core/net/net.c | 4 ++++
 1 file changed, 4 insertions(+)
grub-core/net/net.c +4 -0
index 585f4f7..efacb30 100644
@@ -1280,6 +1280,10 @@ grub_net_open_real (const char *name)
       const char *comma;
       char *colon;
       comma = grub_strchr (name, ',');
+      if (!comma)
+	{
+	  comma = grub_strchr (name, ';');
+	}
       colon = grub_strchr (name, ':');
       if (colon)
 	{
Signature · 1 line
2.9.3
#6 lore raw

Re: [PATCH 3/4] Don't allocate a new address buffer if we receive multiple DNS responses

In reply to Matthew Garrett

24.01.2017 03:36, Matthew Garrett пишет:

Quoted 19 lines

The current logic in the DNS resolution code allocates an address buffer based on the number of addresses in the response packet. If we receive multiple response packets in response to a single query packet, this means that we will reallocate a new buffer large enough for only the addresses in that specific packet, discarding any previous results in the process. Worse, we still keep track of the *total* number of addresses resolved in response to this query, not merely the number in the packet being currently processed. Use realloc() rather than malloc() to avoid overwriting the existing data, and allocate a buffer large enough for the total set of addresses rather than merely the number in this specific response.

---
 grub-core/net/dns.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/net/dns.c +0 -0
index 5d9afe0..5deb1ef 100644

grub-core/net/dns.c recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)), · line 289

Quoted 7 lines
grub-core/net/dns.c +2 -2
@@ -285,8 +285,8 @@ recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)),
       ptr++;
       ptr += 4;
     }
-  *data->addresses = grub_malloc (sizeof ((*data->addresses)[0])
-				 * grub_be_to_cpu16 (head->ancount));
+  *data->addresses = grub_realloc (*data->addresses, sizeof ((*data->addresses)[0])
+		     * (grub_be_to_cpu16 (head->ancount) + *data->naddresses));

If *data->addresses was not NULL, we should not reach this code.

  /* Code apparently assumed that only one packet is received as response.
     We may get multiple responses due to network condition, so check here
     and quit early. */
  if (*data->addresses)
    {
      grub_netbuff_free (nb);
      return GRUB_ERR_NONE;
    }

This was noted previously by Josef, we discussed it and my position is that resolver code requires redesign to correctly merge multiple answers and prioritize A vs AAAA requests.

Do you get actual errors with current master? If yes, could you provide more information what this patch fixes?

Quoted 4 lines
grub-core/net/dns.c +0 -0
   if (!*data->addresses)
     {
       grub_errno = GRUB_ERR_NONE;
#9 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

In reply to Matthew Garrett

24.01.2017 03:36, Matthew Garrett пишет:

Quoted 2 lines

Some DHCP servers (such as dnsmasq) tokenise parameters with commas, making it impossible to pass boot files with commas in them. Allow using a

grub_net_open() operates on devices, not files. Please give more details about your problem.

Quoted 21 lines

semicolon to separate the protocol from host if a comma wasn't found.

---
 grub-core/net/net.c | 4 ++++
 1 file changed, 4 insertions(+)
grub-core/net/net.c +4 -0
index 585f4f7..efacb30 100644
@@ -1280,6 +1280,10 @@ grub_net_open_real (const char *name)
       const char *comma;
       char *colon;
       comma = grub_strchr (name, ',');
+      if (!comma)
+	{
+	  comma = grub_strchr (name, ';');
+	}
       colon = grub_strchr (name, ':');
       if (colon)
 	{
#4 lore raw

Re: [PATCH 2/4] Send a user class identifier in bootp requests and tag it as DHCP discover

In reply to Matthew Garrett
Quoted 17 lines · Matthew Garrett

It's helpful to determine that a request was sent by grub in order to permit the server to provide different information at different stages of the boot process. Send GRUB2 as a type 77 DHCP option when sending bootp packets in order to make this possible and tag the request as a DHCP discover to convince servers to pay attention to it. Add the client architecture for good measure.

---
 grub-core/net/bootp.c | 42 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)
grub-core/net/bootp.c +0 -0
index 9e2fdb7..0da8e24 100644
@@ -25,6 +25,24 @@
 #include <grub/net/udp.h>
 #include <grub/datetime.h>

grub-core/net/bootp.c line 36

Quoted 10 lines
grub-core/net/bootp.c +9 -0
 
+#if !defined(GRUB_MACHINE_EFI) && (defined(__i386__) || defined(__x86_64__))
+#define GRUB_NET_BOOTP_ARCH 0x0000
+#elif defined(GRUB_MACHINE_EFI) && defined(__x86_64__)
+#define GRUB_NET_BOOTP_ARCH 0x0007
+#elif defined(GRUB_MACHINE_EFI) && defined(__aarch64__)
+#define GRUB_NET_BOOTP_ARCH 0x000B
+#else
+#error "unknown bootp architecture"
+#endif

This list is woefully incomplete and there aren't even allocated values for every platform that grub supports so adding the architecture option will need to be optional.

Quoted 15 lines
grub-core/net/bootp.c +9 -0
+
+static grub_uint8_t dhcp_option_header[] = {GRUB_NET_BOOTP_RFC1048_MAGIC_0,
+                                           GRUB_NET_BOOTP_RFC1048_MAGIC_1,
+                                           GRUB_NET_BOOTP_RFC1048_MAGIC_2,
+                                           GRUB_NET_BOOTP_RFC1048_MAGIC_3};
+static grub_uint8_t grub_userclass[] = {0x4D, 0x06, 0x05, 'G', 'R', 'U', 'B', '2'};
+static grub_uint8_t grub_dhcpdiscover[] = {0x35, 0x01, 0x01};
+static grub_uint8_t grub_dhcptime[] = {0x33, 0x04, 0x00, 0x00, 0x0e, 0x10};
+
 static void
 parse_dhcp_vendor (const char *name, const void *vend, int limit, int *mask)
 {
@@ -499,10 +517,14 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
          struct udphdr *udph;
          grub_net_network_level_address_t target;

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), · line 527

Quoted 10 lines
grub-core/net/bootp.c +5 -1
          grub_net_link_level_address_t ll_target;
+         grub_uint8_t *offset;
 
          if (!ifaces[j].prev)
            continue;
-         nb = grub_netbuff_alloc (sizeof (*pack) + 64 + 128);
+         nb = grub_netbuff_alloc (sizeof (*pack) + sizeof(dhcp_option_header)
+                                  + sizeof(grub_userclass)
+                                  + sizeof(grub_dhcpdiscover)
+                                  + sizeof(grub_dhcptime) + 64 + 128);

Increasing the allocation without also updating grub_netbuff_reserve and grub_netbuff_push calls doesn't actually influence the amount of space in the packet. grub_netbuff_alloc and grub_netbuff_reserve should be given the total size of the packet: ethernet+ip+udp+bootp while grub_netbuff_push is called for each of those layers. As-is this happens to work because the existing code included 64 bytes of zeros at the end of the bootp packet. The new options use 26 of those bytes. The extra 128 bytes of the reservation is for the lower protocol headers (ethernet+ip+udp).

I don't now why the original code included 64 bytes of trailing zeros or allocates 128 for the lower protocols when only 42 will ever be used I don't know, that seems to be the state of the code on initial check-in to git.

Quoted 36 lines
grub-core/net/bootp.c +18 -1
          if (!nb)
            {
              grub_netbuff_free (nb);
@@ -536,6 +558,24 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
          pack->seconds = grub_cpu_to_be16 (t);
 
          grub_memcpy (&pack->mac_addr, &ifaces[j].hwaddress.mac, 6);
+         offset = (grub_uint8_t *)&pack->vendor;
+         grub_memcpy (offset, dhcp_option_header, sizeof(dhcp_option_header));
+         offset += sizeof(dhcp_option_header);
+         grub_memcpy (offset, grub_dhcpdiscover, sizeof(grub_dhcpdiscover));
+         offset += sizeof(grub_dhcpdiscover);
+         grub_memcpy (offset, grub_userclass, sizeof(grub_userclass));
+         offset += sizeof(grub_userclass);
+         grub_memcpy (offset, grub_dhcptime, sizeof(grub_dhcptime));
+
+         /* insert Client System Architecture (option 93) */
+         offset += sizeof(grub_dhcptime);
+         offset[0] = 93;
+         offset[1] = 2;
+         offset[2] = (GRUB_NET_BOOTP_ARCH >> 8);
+         offset[3] = (GRUB_NET_BOOTP_ARCH & 0xFF);
+
+         /* option terminator */
+         offset[4] = 255;
 
          grub_netbuff_push (nb, sizeof (*udph));
 
--

2.9.3

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#10 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

In reply to Andrei Borzenkov
Quoted 6 lines · Andrei Borzenkov

24.01.2017 03:36, Matthew Garrett пишет:

Quoted 2 lines

Some DHCP servers (such as dnsmasq) tokenise parameters with commas, making it impossible to pass boot files with commas in them. Allow using a

grub_net_open() operates on devices, not files. Please give more details about your problem.

The DHCP server will return a string in the boot_file field. If you want to indicate that this file should be obtained over http, the easiest way to handle this is to provide a boot file in the form (http,host)filename. Unfortunately dnsmasq uses commas to tokenise its configuration parameters and there appears to be no way to override that, which makes it impossible to provide a boot file in this form. Allowing the use of an alternative character avoids this problem.

#7 lore raw

Re: [PATCH 3/4] Don't allocate a new address buffer if we receive multiple DNS responses

In reply to Andrei Borzenkov
Quoted 6 lines · Andrei Borzenkov

This was noted previously by Josef, we discussed it and my position is that resolver code requires redesign to correctly merge multiple answers and prioritize A vs AAAA requests.

Do you get actual errors with current master? If yes, could you provide more information what this patch fixes?

Ah, yeah, it looks like this is unnecessary with the current state of the code. I'll drop it, thanks!

#11 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

In reply to Matthew Garrett

24.01.2017 23:50, Matthew Garrett пишет:

Quoted 8 lines · Andrei Borzenkov
Quoted 6 lines

24.01.2017 03:36, Matthew Garrett пишет:

Quoted 2 lines

Some DHCP servers (such as dnsmasq) tokenise parameters with commas, making it impossible to pass boot files with commas in them. Allow using a

grub_net_open() operates on devices, not files. Please give more details about your problem.

Quoted 8 lines

The DHCP server will return a string in the boot_file field. If you want to indicate that this file should be obtained over http, the easiest way to handle this is to provide a boot file in the form (http,host)filename. Unfortunately dnsmasq uses commas to tokenise its configuration parameters and there appears to be no way to override that, which makes it impossible to provide a boot file in this form. Allowing the use of an alternative character avoids this problem.

This won't work because (http,host) will never be interpreted as device in current code so you need to support this first before this patch can even be considered. Also I am not convinced that arbitrary changing syntax is good idea.

You already can set $prefix when generating image. Why is it not enough?

#12 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

Quoted 18 lines · Andrei Borzenkov

24.01.2017 23:50, Matthew Garrett пишет:

Quoted 8 lines · Andrei Borzenkov
Quoted 6 lines

24.01.2017 03:36, Matthew Garrett пишет:

Quoted 2 lines

Some DHCP servers (such as dnsmasq) tokenise parameters with commas, making it impossible to pass boot files with commas in them. Allow using a

grub_net_open() operates on devices, not files. Please give more details about your problem.

Quoted 8 lines

The DHCP server will return a string in the boot_file field. If you want to indicate that this file should be obtained over http, the easiest way to handle this is to provide a boot file in the form (http,host)filename. Unfortunately dnsmasq uses commas to tokenise its configuration parameters and there appears to be no way to override that, which makes it impossible to provide a boot file in this form. Allowing the use of an alternative character avoids this problem.

Quoted 4 lines

This won't work because (http,host) will never be interpreted as device in current code so you need to support this first before this patch can even be considered. Also I am not convinced that arbitrary changing syntax is good idea.

I don't understand - grub_net_open_real() already handles this case:

  else
    {
      const char *comma;
      comma = grub_strchr (name, ',');
      if (comma)
        {
          protnamelen = comma - name;
          server = comma + 1;
          protname = name;
        }
      else
        {
          protnamelen = grub_strlen (name);
          server = grub_net_default_server;
          protname = name;
        }
Quoted 1 line

You already can set $prefix when generating image. Why is it not enough?

Because we ship prebuilt images but don't know what IP addresses users will be using for their deployment servers.

#13 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

25.01.2017 07:06, Matthew Garrett пишет:

Quoted 19 lines · Andrei Borzenkov
Quoted 18 lines

24.01.2017 23:50, Matthew Garrett пишет:

Quoted 16 lines · Andrei Borzenkov
Quoted 6 lines

24.01.2017 03:36, Matthew Garrett пишет:

Quoted 2 lines

Some DHCP servers (such as dnsmasq) tokenise parameters with commas, making it impossible to pass boot files with commas in them. Allow using a

grub_net_open() operates on devices, not files. Please give more details about your problem.

The DHCP server will return a string in the boot_file field. If you want to indicate that this file should be obtained over http, the easiest way to handle this is to provide a boot file in the form (http,host)filename. Unfortunately dnsmasq uses commas to tokenise its configuration parameters and there appears to be no way to override that, which makes it impossible to provide a boot file in this form. Allowing the use of an alternative character avoids this problem.

Quoted 6 lines
Quoted 4 lines

This won't work because (http,host) will never be interpreted as device in current code so you need to support this first before this patch can even be considered. Also I am not convinced that arbitrary changing syntax is good idea.

I don't understand - grub_net_open_real() already handles this case:

Because bootfile from DHCP packet is not used to set device part of $prefix. Setting bootfile to (http,host)filename will end up with full prefix "(tftp,$next_ip)(http,host)base-name-of-filename". If you mean something different, please explain.

Quoted 19 lines
  else
    {
      const char *comma;
      comma = grub_strchr (name, ',');
      if (comma)
        {
          protnamelen = comma - name;
          server = comma + 1;
          protname = name;
        }
      else
        {
          protnamelen = grub_strlen (name);
          server = grub_net_default_server;
          protname = name;
        }
Quoted 5 lines
Quoted 1 line

You already can set $prefix when generating image. Why is it not enough?

Because we ship prebuilt images but don't know what IP addresses users will be using for their deployment servers.

I can think about implementing URI parsing (somewhat in line with UEFI HTTPboot spec) and/or supporting partial $prefix that sets only protocol, something like "grub-mknetdir --prefix=(http,)", where server part is filled from DHCP ACK.

#14 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

Quoted 7 lines · Andrei Borzenkov

25.01.2017 07:06, Matthew Garrett пишет:

Quoted 1 line

I don't understand - grub_net_open_real() already handles this case:

Because bootfile from DHCP packet is not used to set device part of $prefix. Setting bootfile to (http,host)filename will end up with full prefix "(tftp,$next_ip)(http,host)base-name-of-filename". If you mean something different, please explain.

If prefix isn't set then won't bootfile be interpreted as the device plus file?

Quoted 8 lines
Quoted 3 lines

Because we ship prebuilt images but don't know what IP addresses users will be using for their deployment servers.

I can think about implementing URI parsing (somewhat in line with UEFI HTTPboot spec) and/or supporting partial $prefix that sets only protocol, something like "grub-mknetdir --prefix=(http,)", where server part is filled from DHCP ACK.

We need the ability to pass port as well, so that would still be insufficient.

#19 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

In reply to Matthew Garrett
Quoted 8 lines · Matthew Garrett
Quoted 6 lines · Andrei Borzenkov

24.01.2017 03:36, Matthew Garrett пишет:

Quoted 2 lines

Some DHCP servers (such as dnsmasq) tokenise parameters with commas, making it impossible to pass boot files with commas in them. Allow using a

grub_net_open() operates on devices, not files. Please give more details about your problem.

Quoted 7 lines

The DHCP server will return a string in the boot_file field. If you want to indicate that this file should be obtained over http, the easiest way to handle this is to provide a boot file in the form (http,host)filename. Unfortunately dnsmasq uses commas to tokenise its configuration parameters and there appears to be no way to override that, which makes it impossible to provide a boot file in this form. Allowing the use of an alternative character avoids this problem.

To my understanding, you can (and have to) use the url form in boot-file and specify vendor-class-identifier to "HTTPClient" to let the UEFI firmware knows this as a response to HTTP boot request and handle that boot-file accordingly, then continue with that url loaded and run as NBP.

Here the article provide some examples.

https://en.opensuse.org/UEFI_HTTPBoot_with_OVMF

I suppose you may be running the bootp for setting up the client network interface and wanted it to have work with http hosting the os images in some way ? Anyway the config seems not to work with what UEFI prosposed for HTTP Boot and I'd suggest to check that first.

Thanks,
Michael

Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#20 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

Quoted 9 lines · Michael Chang
Quoted 7 lines · Matthew Garrett

The DHCP server will return a string in the boot_file field. If you want to indicate that this file should be obtained over http, the easiest way to handle this is to provide a boot file in the form (http,host)filename. Unfortunately dnsmasq uses commas to tokenise its configuration parameters and there appears to be no way to override that, which makes it impossible to provide a boot file in this form. Allowing the use of an alternative character avoids this problem.

Quoted 4 lines

To my understanding, you can (and have to) use the url form in boot-file and specify vendor-class-identifier to "HTTPClient" to let the UEFI firmware knows this as a response to HTTP boot request and handle that boot-file accordingly, then continue with that url loaded and run as NBP.

We're passing the bootfile to grub in order to obtain further configuration, so the firmware isn't relevant here.

#15 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

In reply to Matthew Garrett
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · Andrei Borzenkov

25.01.2017 07:06, Matthew Garrett пишет:

Quoted 1 line

I don't understand - grub_net_open_real() already handles this case:

Because bootfile from DHCP packet is not used to set device part of $prefix. Setting bootfile to (http,host)filename will end up with full prefix "(tftp,$next_ip)(http,host)base-name-of-filename". If you mean something different, please explain.

If prefix isn't set then won't bootfile be interpreted as the device plus file?

No. That would mean "parsing URI" that I mentioned.

Quoted 10 lines
Quoted 8 lines
Quoted 3 lines

Because we ship prebuilt images but don't know what IP addresses users will be using for their deployment servers.

I can think about implementing URI parsing (somewhat in line with UEFI HTTPboot spec) and/or supporting partial $prefix that sets only protocol, something like "grub-mknetdir --prefix=(http,)", where server part is filled from DHCP ACK.

We need the ability to pass port as well, so that would still be insufficient.

Good. So start with design proposal that is extensible enough.

But TBH - all of this can already be implemented using grub scripting. Use custom DHCP options or parse bootfile using regex. No code changes is needed - we support generic scripting for a reason.

#16 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

Quoted 5 lines · Andrei Borzenkov
Quoted 2 lines · Matthew Garrett

If prefix isn't set then won't bootfile be interpreted as the device plus file?

No. That would mean "parsing URI" that I mentioned.

My experience is that configfile (http,example.com)grub/config works as you'd expect it to, and that set endpoint=$net_efinet0_dhcp_boot_file; configfile $endpoint does the same. Am I hitting some corner case where things are being incorrectly parsed and so giving me unintended functionality?

Quoted 7 lines
Quoted 1 line

We need the ability to pass port as well, so that would still be insufficient.

Good. So start with design proposal that is extensible enough.

But TBH - all of this can already be implemented using grub scripting. Use custom DHCP options or parse bootfile using regex. No code changes is needed - we support generic scripting for a reason.

How are custom DHCP options handled? I can't find anything in the documentation about them being interpreted, and a quick look at the code only shows it setting known variables.

#17 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

Quoted 5 lines · Matthew Garrett
Quoted 4 lines · Andrei Borzenkov
Quoted 2 lines · Matthew Garrett

If prefix isn't set then won't bootfile be interpreted as the device plus file?

Quoted 8 lines
Quoted 1 line

No. That would mean "parsing URI" that I mentioned.

My experience is that configfile (http,example.com)grub/config works as you'd expect it to, and that set endpoint=$net_efinet0_dhcp_boot_file; configfile $endpoint does the same. Am I hitting some corner case where things are being incorrectly parsed and so giving me unintended functionality?

When grub starts it tries to determine device and path it was booted from. For network boot it currently means examining DHCP ACK packet that is normally provided by firmware and setting device to tftp,$next_ip and path to $bootfile. There is no provision to set protocol to anything else nor to parse $bootfile to extract protocol/server.

If you speak about "configfile something" you are past this point so DHCP $bootfile option is not relevant at all.

Quoted 11 lines
Quoted 7 lines
Quoted 1 line

We need the ability to pass port as well, so that would still be insufficient.

Good. So start with design proposal that is extensible enough.

But TBH - all of this can already be implemented using grub scripting. Use custom DHCP options or parse bootfile using regex. No code changes is needed - we support generic scripting for a reason.

How are custom DHCP options handled? I can't find anything in the documentation about them being interpreted, and a quick look at the code only shows it setting known variables.

You have net_get_dhcp_option to fetch arbitrary option value from DHCP ACK packet and put it in variable for further processing. I'm definitely open to improving this command to make it more useful if it turns out lacking something.

#21 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

In reply to Matthew Garrett
Quoted 10 lines · Matthew Garrett
Quoted 9 lines · Michael Chang
Quoted 7 lines · Matthew Garrett

The DHCP server will return a string in the boot_file field. If you want to indicate that this file should be obtained over http, the easiest way to handle this is to provide a boot file in the form (http,host)filename. Unfortunately dnsmasq uses commas to tokenise its configuration parameters and there appears to be no way to override that, which makes it impossible to provide a boot file in this form. Allowing the use of an alternative character avoids this problem.

Quoted 7 lines
Quoted 4 lines

To my understanding, you can (and have to) use the url form in boot-file and specify vendor-class-identifier to "HTTPClient" to let the UEFI firmware knows this as a response to HTTP boot request and handle that boot-file accordingly, then continue with that url loaded and run as NBP.

We're passing the bootfile to grub in order to obtain further configuration, so the firmware isn't relevant here.

I mean that firmware may not be involved for now but it may be helpful someday to carry that very same config booted by firmware as is.

And firmware did have implications to $prefix, if it's not been set, the value would be the booted device and path pass by firmware. To keep from ABI conflicts in modules loaded from elsewhere, you should not change it (in that further configration) unless you know the modules will always be compatible in new $prefix location.

Thanks,
Michael

Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#18 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

In reply to Andrei Borzenkov
Quoted 15 lines · Andrei Borzenkov
Quoted 6 lines · Matthew Garrett

My experience is that configfile (http,example.com)grub/config works as you'd expect it to, and that set endpoint=$net_efinet0_dhcp_boot_file; configfile $endpoint does the same. Am I hitting some corner case where things are being incorrectly parsed and so giving me unintended functionality?

When grub starts it tries to determine device and path it was booted from. For network boot it currently means examining DHCP ACK packet that is normally provided by firmware and setting device to tftp,$next_ip and path to $bootfile. There is no provision to set protocol to anything else nor to parse $bootfile to extract protocol/server.

Quoted 2 lines

If you speak about "configfile something" you are past this point so DHCP $bootfile option is not relevant at all.

The variable is accessible to any configuration file that was built into the grub binary, so it's still very relevant. Our flow is to have the firmware DHCP and obtain grub, then have grub send a new DHCP request with a different user agent (see previous patch) and obtain a new bootfile.

Quoted 8 lines
Quoted 3 lines

How are custom DHCP options handled? I can't find anything in the documentation about them being interpreted, and a quick look at the code only shows it setting known variables.

You have net_get_dhcp_option to fetch arbitrary option value from DHCP ACK packet and put it in variable for further processing. I'm definitely open to improving this command to make it more useful if it turns out lacking something.

Hm. In theory sticking everything in option 43 and parsing it out in the config is possible - in practice the only reason that would be necessary right now is that grub uses the same separator as dnsmasq, and a trivial patch to grub would make it much easier to provide configuration on the dnsmasq command line.

#22 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

In reply to Michael Chang
Quoted 6 lines · Michael Chang
Quoted 2 lines · Matthew Garrett

We're passing the bootfile to grub in order to obtain further configuration, so the firmware isn't relevant here.

I mean that firmware may not be involved for now but it may be helpful someday to carry that very same config booted by firmware as is.

The information is there. If local configuration doesn't choose to do something that overrides it, it will stay there.

Quoted 5 lines

And firmware did have implications to $prefix, if it's not been set, the value would be the booted device and path pass by firmware. To keep from ABI conflicts in modules loaded from elsewhere, you should not change it (in that further configration) unless you know the modules will always be compatible in new $prefix location.

The DHCP server provided the initial copy of grub, so it should ensure that it only provides links to configuration that will provide compatible modules. This is no worse than supporting reading config fragments off disk that might redefine prefix.

#23 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

In reply to Matthew Garrett

24.01.2017 23:50, Matthew Garrett пишет:

Quoted 8 lines · Andrei Borzenkov
Quoted 6 lines

24.01.2017 03:36, Matthew Garrett пишет:

Quoted 2 lines

Some DHCP servers (such as dnsmasq) tokenise parameters with commas, making it impossible to pass boot files with commas in them. Allow using a

grub_net_open() operates on devices, not files. Please give more details about your problem.

Quoted 6 lines

The DHCP server will return a string in the boot_file field. If you want to indicate that this file should be obtained over http, the easiest way to handle this is to provide a boot file in the form (http,host)filename. Unfortunately dnsmasq uses commas to tokenise its configuration parameters and there appears to be no way to override that, which makes it impossible to provide a boot file in this form.

Really?

dnsmasq -d -z --dhcp-range=192.168.11.20,192.168.11.30 --dhcp-option option:bootfile-name,'(http,1.2.3.4)/foo/bar' -i eth1

Bootstrap Protocol (ACK)
...

    Option: (67) Bootfile name
        Length: 23
        Bootfile name: (http,1.2.3.4)/foo/bar
    Option: (255) End
        Option End: 255
#24 lore raw

Re: [PATCH 4/4] Allow protocol to be separated from host with a semicolon

Quoted 14 lines · Andrei Borzenkov

24.01.2017 23:50, Matthew Garrett пишет:

Quoted 6 lines

The DHCP server will return a string in the boot_file field. If you want to indicate that this file should be obtained over http, the easiest way to handle this is to provide a boot file in the form (http,host)filename. Unfortunately dnsmasq uses commas to tokenise its configuration parameters and there appears to be no way to override that, which makes it impossible to provide a boot file in this form.

Really?

dnsmasq -d -z --dhcp-range=192.168.11.20,192.168.11.30 --dhcp-option option:bootfile-name,'(http,1.2.3.4)/foo/bar' -i eth1

Quoted 7 lines

Bootstrap Protocol (ACK)
...

    Option: (67) Bootfile name
        Length: 23
        Bootfile name: (http,1.2.3.4)/foo/bar
    Option: (255) End
        Option End: 255

Huh. Apparently I was very confused. Sorry about the waste of time!

#2 lore raw

Re: [PATCH 1/4] Allow non-default ports for HTTP requests

In reply to Matthew Garrett

24.01.2017 03:35, Matthew Garrett пишет:

Quoted 22 lines

Add support for passing ports in HTTP requests. This takes the form of:
(http,serverip:portnum)/file

---
 grub-core/net/http.c |  8 ++++++--
 grub-core/net/net.c  | 10 +++++++++-
 include/grub/net.h   |  1 +
 3 files changed, 16 insertions(+), 3 deletions(-)
grub-core/net/http.c +1 -1
index 5aa4ad3..389a78e 100644
@@ -309,7 +309,7 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)
 {
   http_data_t data = file->data;
   grub_uint8_t *ptr;
-  int i;
+  int i, port;
   struct grub_net_buff *nb;
   grub_err_t err;
 

grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 393

Quoted 4 lines
grub-core/net/http.c +1 -0
@@ -390,8 +390,12 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)
   grub_netbuff_put (nb, 2);
   grub_memcpy (ptr, "\r\n", 2);
 
+  if (file->device->net->port)

0 is valid port number (at least I am not aware of any RFC that prohibits its use).

Quoted 23 lines
grub-core/net/http.c +4 -1
+    port = file->device->net->port;
+  else
+    port = HTTP_PORT;
   data->sock = grub_net_tcp_open (file->device->net->server,
-				  HTTP_PORT, http_receive,
+				  port, http_receive,
 				  http_err, http_err,
 				  file);
   if (!data->sock)
grub-core/net/net.c +1 -1
index 10773fc..585f4f7 100644
@@ -1261,7 +1261,7 @@ grub_net_open_real (const char *name)
   grub_net_app_level_t proto;
   const char *protname, *server;
   grub_size_t protnamelen;
-  int try;
+  int try, port = 0;
 
   if (grub_strncmp (name, "pxe:", sizeof ("pxe:") - 1) == 0)
     {

grub-core/net/net.c grub_net_open_real (const char *name) · line 1283

Quoted 6 lines
grub-core/net/net.c +2 -0
@@ -1278,7 +1278,14 @@ grub_net_open_real (const char *name)
   else
     {
       const char *comma;
+      char *colon;
       comma = grub_strchr (name, ',');
+      colon = grub_strchr (name, ':');

This conflicts with IPv6 addresses. Similar patches were already posted. I suggested using (proto,server,port). This also allows empty server and still using non-standard port.

More general at some point we should hit authentication, and so may need to stuff user/password there. So we need to come up with extensible framework.

Putting everything in "server" string may work, but then we need to support IPv6 literals here - [fe80::1]:444 - at least, this is the only way to use server:port I see. Also, we probably need to support escaping of colons. Finally, speaking about user/password, we may need to support

user:password@[fe80::1]:456

May be using (net,proto=http,server=xxx,port=yyy,user=uuu,...) which allows adding arbitrary keywords at any time.