nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0094-efinet-Setting-DNS-server-from-UEFI-protocol.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Michael Chang <mchang@suse.com>
8e15ce
Date: Thu, 14 Jul 2016 17:48:45 +0800
8e15ce
Subject: [PATCH] efinet: Setting DNS server from UEFI protocol
8e15ce
8e15ce
In the URI device path node, any name rahter than address can be used for
8e15ce
looking up the resources so that DNS service become needed to get answer of the
8e15ce
name's address. Unfortunately the DNS is not defined in any of the device path
8e15ce
nodes so that we use the EFI_IP4_CONFIG2_PROTOCOL and EFI_IP6_CONFIG_PROTOCOL
8e15ce
to obtain it.
8e15ce
8e15ce
These two protcols are defined the sections of UEFI specification.
8e15ce
8e15ce
 27.5 EFI IPv4 Configuration II Protocol
8e15ce
 27.7 EFI IPv6 Configuration Protocol
8e15ce
8e15ce
include/grub/efi/api.h:
8e15ce
Add new structure and protocol UUID of EFI_IP4_CONFIG2_PROTOCOL and
8e15ce
EFI_IP6_CONFIG_PROTOCOL.
8e15ce
8e15ce
grub-core/net/drivers/efi/efinet.c:
8e15ce
Use the EFI_IP4_CONFIG2_PROTOCOL and EFI_IP6_CONFIG_PROTOCOL to obtain the list
8e15ce
of DNS server address for IPv4 and IPv6 respectively. The address of DNS
8e15ce
servers is structured into DHCPACK packet and feed into the same DHCP packet
8e15ce
processing functions to ensure the network interface is setting up the same way
8e15ce
it used to be.
8e15ce
8e15ce
Signed-off-by: Michael Chang <mchang@suse.com>
8e15ce
Signed-off-by: Ken Lin <ken.lin@hpe.com>
8e15ce
---
8e15ce
 grub-core/net/drivers/efi/efinet.c | 163 +++++++++++++++++++++++++++++++++++++
8e15ce
 include/grub/efi/api.h             |  75 +++++++++++++++++
8e15ce
 2 files changed, 238 insertions(+)
8e15ce
8e15ce
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
8e15ce
index 8171ecaa5e4..715a6168d77 100644
8e15ce
--- a/grub-core/net/drivers/efi/efinet.c
8e15ce
+++ b/grub-core/net/drivers/efi/efinet.c
8e15ce
@@ -33,6 +33,8 @@ GRUB_MOD_LICENSE ("GPLv3+");
8e15ce
 /* GUID.  */
8e15ce
 static grub_efi_guid_t net_io_guid = GRUB_EFI_SIMPLE_NETWORK_GUID;
8e15ce
 static grub_efi_guid_t pxe_io_guid = GRUB_EFI_PXE_GUID;
8e15ce
+static grub_efi_guid_t ip4_config_guid = GRUB_EFI_IP4_CONFIG2_PROTOCOL_GUID;
8e15ce
+static grub_efi_guid_t ip6_config_guid = GRUB_EFI_IP6_CONFIG_PROTOCOL_GUID;
8e15ce
 
8e15ce
 static grub_err_t
8e15ce
 send_card_buffer (struct grub_net_card *dev,
8e15ce
@@ -332,6 +334,125 @@ grub_efinet_findcards (void)
8e15ce
   grub_free (handles);
8e15ce
 }
8e15ce
 
8e15ce
+static grub_efi_handle_t
8e15ce
+grub_efi_locate_device_path (grub_efi_guid_t *protocol, grub_efi_device_path_t *device_path,
8e15ce
+			    grub_efi_device_path_t **r_device_path)
8e15ce
+{
8e15ce
+  grub_efi_handle_t handle;
8e15ce
+  grub_efi_status_t status;
8e15ce
+
8e15ce
+  status = efi_call_3 (grub_efi_system_table->boot_services->locate_device_path,
8e15ce
+		      protocol, &device_path, &handle);
8e15ce
+
8e15ce
+  if (status != GRUB_EFI_SUCCESS)
8e15ce
+    return 0;
8e15ce
+
8e15ce
+  if (r_device_path)
8e15ce
+    *r_device_path = device_path;
8e15ce
+
8e15ce
+  return handle;
8e15ce
+}
8e15ce
+
8e15ce
+static grub_efi_ipv4_address_t *
8e15ce
+grub_dns_server_ip4_address (grub_efi_device_path_t *dp, grub_efi_uintn_t *num_dns)
8e15ce
+{
8e15ce
+  grub_efi_handle_t hnd;
8e15ce
+  grub_efi_status_t status;
8e15ce
+  grub_efi_ip4_config2_protocol_t *conf;
8e15ce
+  grub_efi_ipv4_address_t *addrs;
8e15ce
+  grub_efi_uintn_t data_size = 1 * sizeof (grub_efi_ipv4_address_t);
8e15ce
+
8e15ce
+  hnd = grub_efi_locate_device_path (&ip4_config_guid, dp, NULL);
8e15ce
+
8e15ce
+  if (!hnd)
8e15ce
+    return 0;
8e15ce
+
8e15ce
+  conf = grub_efi_open_protocol (hnd, &ip4_config_guid,
8e15ce
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
8e15ce
+
8e15ce
+  if (!conf)
8e15ce
+    return 0;
8e15ce
+
8e15ce
+  addrs  = grub_malloc (data_size);
8e15ce
+  if (!addrs)
8e15ce
+    return 0;
8e15ce
+
8e15ce
+  status = efi_call_4 (conf->get_data, conf,
8e15ce
+		      GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
8e15ce
+		      &data_size, addrs);
8e15ce
+
8e15ce
+  if (status == GRUB_EFI_BUFFER_TOO_SMALL)
8e15ce
+    {
8e15ce
+      grub_free (addrs);
8e15ce
+      addrs  = grub_malloc (data_size);
8e15ce
+      if (!addrs)
8e15ce
+	return 0;
8e15ce
+
8e15ce
+      status = efi_call_4 (conf->get_data,  conf,
8e15ce
+			  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
8e15ce
+			  &data_size, addrs);
8e15ce
+    }
8e15ce
+
8e15ce
+  if (status != GRUB_EFI_SUCCESS)
8e15ce
+    {
8e15ce
+      grub_free (addrs);
8e15ce
+      return 0;
8e15ce
+    }
8e15ce
+
8e15ce
+  *num_dns = data_size / sizeof (grub_efi_ipv4_address_t);
8e15ce
+  return addrs;
8e15ce
+}
8e15ce
+
8e15ce
+static grub_efi_ipv6_address_t *
8e15ce
+grub_dns_server_ip6_address (grub_efi_device_path_t *dp, grub_efi_uintn_t *num_dns)
8e15ce
+{
8e15ce
+  grub_efi_handle_t hnd;
8e15ce
+  grub_efi_status_t status;
8e15ce
+  grub_efi_ip6_config_protocol_t *conf;
8e15ce
+  grub_efi_ipv6_address_t *addrs;
8e15ce
+  grub_efi_uintn_t data_size = 1 * sizeof (grub_efi_ipv6_address_t);
8e15ce
+
8e15ce
+  hnd = grub_efi_locate_device_path (&ip6_config_guid, dp, NULL);
8e15ce
+
8e15ce
+  if (!hnd)
8e15ce
+    return 0;
8e15ce
+
8e15ce
+  conf = grub_efi_open_protocol (hnd, &ip6_config_guid,
8e15ce
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
8e15ce
+
8e15ce
+  if (!conf)
8e15ce
+    return 0;
8e15ce
+
8e15ce
+  addrs  = grub_malloc (data_size);
8e15ce
+  if (!addrs)
8e15ce
+    return 0;
8e15ce
+
8e15ce
+  status = efi_call_4 (conf->get_data, conf,
8e15ce
+		      GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
8e15ce
+		      &data_size, addrs);
8e15ce
+
8e15ce
+  if (status == GRUB_EFI_BUFFER_TOO_SMALL)
8e15ce
+    {
8e15ce
+      grub_free (addrs);
8e15ce
+      addrs  = grub_malloc (data_size);
8e15ce
+      if (!addrs)
8e15ce
+	return 0;
8e15ce
+
8e15ce
+      status = efi_call_4 (conf->get_data,  conf,
8e15ce
+			  GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
8e15ce
+			  &data_size, addrs);
8e15ce
+    }
8e15ce
+
8e15ce
+  if (status != GRUB_EFI_SUCCESS)
8e15ce
+    {
8e15ce
+      grub_free (addrs);
8e15ce
+      return 0;
8e15ce
+    }
8e15ce
+
8e15ce
+  *num_dns = data_size / sizeof (grub_efi_ipv6_address_t);
8e15ce
+  return addrs;
8e15ce
+}
8e15ce
+
8e15ce
 static struct grub_net_buff *
8e15ce
 grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *use_ipv6)
8e15ce
 {
8e15ce
@@ -390,6 +511,8 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
8e15ce
       grub_efi_ipv4_device_path_t *ipv4 = (grub_efi_ipv4_device_path_t *) ldp;
8e15ce
       struct grub_net_bootp_packet *bp;
8e15ce
       grub_uint8_t *ptr;
8e15ce
+      grub_efi_ipv4_address_t *dns;
8e15ce
+      grub_efi_uintn_t num_dns;
8e15ce
 
8e15ce
       bp = (struct grub_net_bootp_packet *) nb->tail;
8e15ce
       err = grub_netbuff_put (nb, sizeof (*bp) + 4);
8e15ce
@@ -451,6 +574,25 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
8e15ce
       *ptr++ = sizeof ("HTTPClient") - 1;
8e15ce
       grub_memcpy (ptr, "HTTPClient", sizeof ("HTTPClient") - 1);
8e15ce
 
8e15ce
+      dns = grub_dns_server_ip4_address (dp, &num_dns);
8e15ce
+      if (dns)
8e15ce
+	{
8e15ce
+	  grub_efi_uintn_t size_dns = sizeof (*dns) * num_dns;
8e15ce
+
8e15ce
+	  ptr = nb->tail;
8e15ce
+	  err = grub_netbuff_put (nb, size_dns + 2);
8e15ce
+	  if (err)
8e15ce
+	    {
8e15ce
+	      grub_free (ddp);
8e15ce
+	      grub_netbuff_free (nb);
8e15ce
+	      return NULL;
8e15ce
+	    }
8e15ce
+	  *ptr++ = GRUB_NET_BOOTP_DNS;
8e15ce
+	  *ptr++ = size_dns;
8e15ce
+	  grub_memcpy (ptr, dns, size_dns);
8e15ce
+	  grub_free (dns);
8e15ce
+	}
8e15ce
+
8e15ce
       ptr = nb->tail;
8e15ce
       err = grub_netbuff_put (nb, 1);
8e15ce
       if (err)
8e15ce
@@ -483,6 +625,8 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
8e15ce
       struct grub_net_dhcp6_option *opt;
8e15ce
       struct grub_net_dhcp6_option_iana *iana;
8e15ce
       struct grub_net_dhcp6_option_iaaddr *iaaddr;
8e15ce
+      grub_efi_ipv6_address_t *dns;
8e15ce
+      grub_efi_uintn_t num_dns;
8e15ce
 
8e15ce
       d6p = (struct grub_net_dhcp6_packet *)nb->tail;
8e15ce
       err = grub_netbuff_put (nb, sizeof(*d6p));
8e15ce
@@ -546,6 +690,25 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
8e15ce
       opt->len = grub_cpu_to_be16 (uri_len);
8e15ce
       grub_memcpy (opt->data, uri_dp->uri, uri_len);
8e15ce
 
8e15ce
+      dns = grub_dns_server_ip6_address (dp, &num_dns);
8e15ce
+      if (dns)
8e15ce
+	{
8e15ce
+	  grub_efi_uintn_t size_dns = sizeof (*dns) * num_dns;
8e15ce
+
8e15ce
+	  opt = (struct grub_net_dhcp6_option *)nb->tail;
8e15ce
+	  err = grub_netbuff_put (nb, sizeof(*opt) + size_dns);
8e15ce
+	  if (err)
8e15ce
+	  {
8e15ce
+	    grub_free (ddp);
8e15ce
+	    grub_netbuff_free (nb);
8e15ce
+	    return NULL;
8e15ce
+	  }
8e15ce
+	  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_DNS_SERVERS);
8e15ce
+	  opt->len = grub_cpu_to_be16 (size_dns);
8e15ce
+	  grub_memcpy (opt->data, dns, size_dns);
8e15ce
+	  grub_free (dns);
8e15ce
+	}
8e15ce
+
8e15ce
       *use_ipv6 = 1;
8e15ce
     }
8e15ce
 
8e15ce
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
8e15ce
index 4a51667adb1..0b490195ad9 100644
8e15ce
--- a/include/grub/efi/api.h
8e15ce
+++ b/include/grub/efi/api.h
8e15ce
@@ -352,6 +352,15 @@
8e15ce
 #define GRUB_EFI_RNG_PROTOCOL_GUID \
8e15ce
   { 0x3152bca5, 0xeade, 0x433d, \
8e15ce
     { 0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44 } \
8e15ce
+
8e15ce
+#define GRUB_EFI_IP4_CONFIG2_PROTOCOL_GUID \
8e15ce
+  { 0x5b446ed1, 0xe30b, 0x4faa, \
8e15ce
+      { 0x87, 0x1a, 0x36, 0x54, 0xec, 0xa3, 0x60, 0x80 } \
8e15ce
+  }
8e15ce
+
8e15ce
+#define GRUB_EFI_IP6_CONFIG_PROTOCOL_GUID \
8e15ce
+  { 0x937fe521, 0x95ae, 0x4d1a, \
8e15ce
+      { 0x89, 0x29, 0x48, 0xbc, 0xd9, 0x0a, 0xd3, 0x1a } \
8e15ce
   }
8e15ce
 
8e15ce
 struct grub_efi_sal_system_table
8e15ce
@@ -1883,6 +1892,72 @@ struct grub_efi_rng_protocol
8e15ce
 };
8e15ce
 typedef struct grub_efi_rng_protocol grub_efi_rng_protocol_t;
8e15ce
 
8e15ce
+enum grub_efi_ip4_config2_data_type {
8e15ce
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_INTERFACEINFO,
8e15ce
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_POLICY,
8e15ce
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_MANUAL_ADDRESS,
8e15ce
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_GATEWAY,
8e15ce
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
8e15ce
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_MAXIMUM
8e15ce
+};
8e15ce
+typedef enum grub_efi_ip4_config2_data_type grub_efi_ip4_config2_data_type_t;
8e15ce
+
8e15ce
+struct grub_efi_ip4_config2_protocol
8e15ce
+{
8e15ce
+  grub_efi_status_t (*set_data) (struct grub_efi_ip4_config2_protocol *this,
8e15ce
+				 grub_efi_ip4_config2_data_type_t data_type,
8e15ce
+				 grub_efi_uintn_t data_size,
8e15ce
+				 void *data);
8e15ce
+
8e15ce
+  grub_efi_status_t (*get_data) (struct grub_efi_ip4_config2_protocol *this,
8e15ce
+				 grub_efi_ip4_config2_data_type_t data_type,
8e15ce
+				 grub_efi_uintn_t *data_size,
8e15ce
+				 void *data);
8e15ce
+
8e15ce
+  grub_efi_status_t (*register_data_notify) (struct grub_efi_ip4_config2_protocol *this,
8e15ce
+					     grub_efi_ip4_config2_data_type_t data_type,
8e15ce
+					     grub_efi_event_t event);
8e15ce
+
8e15ce
+  grub_efi_status_t (*unregister_datanotify) (struct grub_efi_ip4_config2_protocol *this,
8e15ce
+					     grub_efi_ip4_config2_data_type_t data_type,
8e15ce
+					     grub_efi_event_t event);
8e15ce
+};
8e15ce
+typedef struct grub_efi_ip4_config2_protocol grub_efi_ip4_config2_protocol_t;
8e15ce
+
8e15ce
+enum grub_efi_ip6_config_data_type {
8e15ce
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_INTERFACEINFO,
8e15ce
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_ALT_INTERFACEID,
8e15ce
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_POLICY,
8e15ce
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_DUP_ADDR_DETECT_TRANSMITS,
8e15ce
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_MANUAL_ADDRESS,
8e15ce
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_GATEWAY,
8e15ce
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
8e15ce
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_MAXIMUM
8e15ce
+};
8e15ce
+typedef enum grub_efi_ip6_config_data_type grub_efi_ip6_config_data_type_t;
8e15ce
+
8e15ce
+struct grub_efi_ip6_config_protocol
8e15ce
+{
8e15ce
+  grub_efi_status_t (*set_data) (struct grub_efi_ip6_config_protocol *this,
8e15ce
+				 grub_efi_ip6_config_data_type_t data_type,
8e15ce
+				 grub_efi_uintn_t data_size,
8e15ce
+				 void *data);
8e15ce
+
8e15ce
+  grub_efi_status_t (*get_data) (struct grub_efi_ip6_config_protocol *this,
8e15ce
+				 grub_efi_ip6_config_data_type_t data_type,
8e15ce
+				 grub_efi_uintn_t *data_size,
8e15ce
+				 void *data);
8e15ce
+
8e15ce
+  grub_efi_status_t (*register_data_notify) (struct grub_efi_ip6_config_protocol *this,
8e15ce
+					     grub_efi_ip6_config_data_type_t data_type,
8e15ce
+					     grub_efi_event_t event);
8e15ce
+
8e15ce
+  grub_efi_status_t (*unregister_datanotify) (struct grub_efi_ip6_config_protocol *this,
8e15ce
+					     grub_efi_ip6_config_data_type_t data_type,
8e15ce
+					     grub_efi_event_t event);
8e15ce
+};
8e15ce
+typedef struct grub_efi_ip6_config_protocol grub_efi_ip6_config_protocol_t;
8e15ce
+
8e15ce
 #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \
8e15ce
   || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__) \
8e15ce
   || defined(__riscv)