nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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