Blame SOURCES/0169-bootp-New-net_bootp6-command.patch

d9d99f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d9d99f
From: Michael Chang <mchang@suse.com>
d9d99f
Date: Sun, 10 Jul 2016 23:46:06 +0800
d9d99f
Subject: [PATCH] bootp: New net_bootp6 command
d9d99f
d9d99f
Implement new net_bootp6 command for IPv6 network auto configuration via the
d9d99f
DHCPv6 protocol (RFC3315).
d9d99f
d9d99f
Signed-off-by: Michael Chang <mchang@suse.com>
d9d99f
Signed-off-by: Ken Lin <ken.lin@hpe.com>
d9d99f
---
d9d99f
 grub-core/net/bootp.c              | 1048 ++++++++++++++++++++++++++++++------
d9d99f
 grub-core/net/drivers/efi/efinet.c |   20 +-
d9d99f
 grub-core/net/ip.c                 |   39 ++
d9d99f
 include/grub/efi/api.h             |    2 +-
d9d99f
 include/grub/net.h                 |   91 ++--
d9d99f
 5 files changed, 994 insertions(+), 206 deletions(-)
d9d99f
d9d99f
diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c
d9d99f
index 4e55adc557b..ff1d7776e7f 100644
d9d99f
--- a/grub-core/net/bootp.c
d9d99f
+++ b/grub-core/net/bootp.c
d9d99f
@@ -25,6 +25,98 @@
d9d99f
 #include <grub/net/udp.h>
d9d99f
 #include <grub/net/url.h>
d9d99f
 #include <grub/datetime.h>
d9d99f
+#include <grub/time.h>
d9d99f
+#include <grub/list.h>
d9d99f
+
d9d99f
+static int
d9d99f
+dissect_url (const char *url, char **proto, char **host, char **path)
d9d99f
+{
d9d99f
+  const char *p, *ps;
d9d99f
+  grub_size_t l;
d9d99f
+
d9d99f
+  *proto = *host = *path = NULL;
d9d99f
+  ps = p = url;
d9d99f
+
d9d99f
+  while ((p = grub_strchr (p, ':')))
d9d99f
+    {
d9d99f
+      if (grub_strlen (p) < sizeof ("://") - 1)
d9d99f
+	break;
d9d99f
+      if (grub_memcmp (p, "://", sizeof ("://") - 1) == 0)
d9d99f
+	{
d9d99f
+	  l = p - ps;
d9d99f
+	  *proto = grub_malloc (l + 1);
d9d99f
+	  if (!*proto)
d9d99f
+	    {
d9d99f
+	      grub_print_error ();
d9d99f
+	      return 0;
d9d99f
+	    }
d9d99f
+
d9d99f
+	  grub_memcpy (*proto, ps, l);
d9d99f
+	  (*proto)[l] = '\0';
d9d99f
+	  p +=  sizeof ("://") - 1;
d9d99f
+	  break;
d9d99f
+	}
d9d99f
+      ++p;
d9d99f
+    }
d9d99f
+
d9d99f
+  if (!*proto)
d9d99f
+    {
d9d99f
+      grub_dprintf ("bootp", "url: %s is not valid, protocol not found\n", url);
d9d99f
+      return 0;
d9d99f
+    }
d9d99f
+
d9d99f
+  ps = p;
d9d99f
+  p = grub_strchr (p, '/');
d9d99f
+
d9d99f
+  if (!p)
d9d99f
+    {
d9d99f
+      grub_dprintf ("bootp", "url: %s is not valid, host/path not found\n", url);
d9d99f
+      grub_free (*proto);
d9d99f
+      *proto = NULL;
d9d99f
+      return 0;
d9d99f
+    }
d9d99f
+
d9d99f
+  l = p - ps;
d9d99f
+
d9d99f
+  if (l > 2 && ps[0] == '[' && ps[l - 1] == ']')
d9d99f
+    {
d9d99f
+      *host = grub_malloc (l - 1);
d9d99f
+      if (!*host)
d9d99f
+	{
d9d99f
+	  grub_print_error ();
d9d99f
+	  grub_free (*proto);
d9d99f
+	  *proto = NULL;
d9d99f
+	  return 0;
d9d99f
+	}
d9d99f
+      grub_memcpy (*host, ps + 1, l - 2);
d9d99f
+      (*host)[l - 2] = 0;
d9d99f
+    }
d9d99f
+  else
d9d99f
+    {
d9d99f
+      *host = grub_malloc (l + 1);
d9d99f
+      if (!*host)
d9d99f
+	{
d9d99f
+	  grub_print_error ();
d9d99f
+	  grub_free (*proto);
d9d99f
+	  *proto = NULL;
d9d99f
+	  return 0;
d9d99f
+	}
d9d99f
+      grub_memcpy (*host, ps, l);
d9d99f
+      (*host)[l] = 0;
d9d99f
+    }
d9d99f
+
d9d99f
+  *path = grub_strdup (p);
d9d99f
+  if (!*path)
d9d99f
+    {
d9d99f
+      grub_print_error ();
d9d99f
+      grub_free (*host);
d9d99f
+      grub_free (*proto);
d9d99f
+      *host = NULL;
d9d99f
+      *proto = NULL;
d9d99f
+      return 0;
d9d99f
+    }
d9d99f
+  return 1;
d9d99f
+}
d9d99f
 
d9d99f
 static char *
d9d99f
 grub_env_write_readonly (struct grub_env_var *var __attribute__ ((unused)),
d9d99f
@@ -345,178 +437,578 @@ grub_net_configure_by_dhcp_ack (const char *name,
d9d99f
   return inter;
d9d99f
 }
d9d99f
 
d9d99f
-struct grub_net_network_level_interface *
d9d99f
-grub_net_configure_by_dhcpv6_ack (const char *name,
d9d99f
-				  struct grub_net_card *card,
d9d99f
-				  grub_net_interface_flags_t flags
d9d99f
-				    __attribute__((__unused__)),
d9d99f
-				  const grub_net_link_level_address_t *hwaddr,
d9d99f
-				  const struct grub_net_dhcpv6_packet *packet,
d9d99f
-				  int is_def, char **device, char **path)
d9d99f
-{
d9d99f
-  struct grub_net_network_level_interface *inter = NULL;
d9d99f
-  struct grub_net_network_level_address addr;
d9d99f
-  int mask = -1;
d9d99f
-
d9d99f
-  if (!device || !path)
d9d99f
+/* The default netbuff size for sending DHCPv6 packets which should be
d9d99f
+   large enough to hold the information */
d9d99f
+#define GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE 512
d9d99f
+
d9d99f
+struct grub_dhcp6_options
d9d99f
+{
d9d99f
+  grub_uint8_t *client_duid;
d9d99f
+  grub_uint16_t client_duid_len;
d9d99f
+  grub_uint8_t *server_duid;
d9d99f
+  grub_uint16_t server_duid_len;
d9d99f
+  grub_uint32_t iaid;
d9d99f
+  grub_uint32_t t1;
d9d99f
+  grub_uint32_t t2;
d9d99f
+  grub_net_network_level_address_t *ia_addr;
d9d99f
+  grub_uint32_t preferred_lifetime;
d9d99f
+  grub_uint32_t valid_lifetime;
d9d99f
+  grub_net_network_level_address_t *dns_server_addrs;
d9d99f
+  grub_uint16_t num_dns_server;
d9d99f
+  char *boot_file_proto;
d9d99f
+  char *boot_file_server_ip;
d9d99f
+  char *boot_file_path;
d9d99f
+};
d9d99f
+
d9d99f
+typedef struct grub_dhcp6_options *grub_dhcp6_options_t;
d9d99f
+
d9d99f
+struct grub_dhcp6_session
d9d99f
+{
d9d99f
+  struct grub_dhcp6_session *next;
d9d99f
+  struct grub_dhcp6_session **prev;
d9d99f
+  grub_uint32_t iaid;
d9d99f
+  grub_uint32_t transaction_id:24;
d9d99f
+  grub_uint64_t start_time;
d9d99f
+  struct grub_net_dhcp6_option_duid_ll duid;
d9d99f
+  struct grub_net_network_level_interface *iface;
d9d99f
+
d9d99f
+  /* The associated dhcpv6 options */
d9d99f
+  grub_dhcp6_options_t adv;
d9d99f
+  grub_dhcp6_options_t reply;
d9d99f
+};
d9d99f
+
d9d99f
+typedef struct grub_dhcp6_session *grub_dhcp6_session_t;
d9d99f
+
d9d99f
+typedef void (*dhcp6_option_hook_fn) (const struct grub_net_dhcp6_option *opt, void *data);
d9d99f
+
d9d99f
+static void
d9d99f
+foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt, grub_size_t size,
d9d99f
+		      dhcp6_option_hook_fn hook, void *hook_data);
d9d99f
+
d9d99f
+static void
d9d99f
+parse_dhcp6_iaaddr (const struct grub_net_dhcp6_option *opt, void *data)
d9d99f
+{
d9d99f
+  grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t )data;
d9d99f
+
d9d99f
+  grub_uint16_t code = grub_be_to_cpu16 (opt->code);
d9d99f
+  grub_uint16_t len = grub_be_to_cpu16 (opt->len);
d9d99f
+
d9d99f
+  if (code == GRUB_NET_DHCP6_OPTION_IAADDR)
d9d99f
+    {
d9d99f
+      const struct grub_net_dhcp6_option_iaaddr *iaaddr;
d9d99f
+      iaaddr = (const struct grub_net_dhcp6_option_iaaddr *)opt->data;
d9d99f
+
d9d99f
+      if (len < sizeof (*iaaddr))
d9d99f
+	{
d9d99f
+	  grub_dprintf ("bootp", "DHCPv6: code %u with insufficient length %u\n", code, len);
d9d99f
+	  return;
d9d99f
+	}
d9d99f
+      if (!dhcp6->ia_addr)
d9d99f
+	{
d9d99f
+	  dhcp6->ia_addr = grub_malloc (sizeof(*dhcp6->ia_addr));
d9d99f
+	  dhcp6->ia_addr->type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
d9d99f
+	  dhcp6->ia_addr->ipv6[0] = grub_get_unaligned64 (iaaddr->addr);
d9d99f
+	  dhcp6->ia_addr->ipv6[1] = grub_get_unaligned64 (iaaddr->addr + 8);
d9d99f
+	  dhcp6->preferred_lifetime = grub_be_to_cpu32 (iaaddr->preferred_lifetime);
d9d99f
+	  dhcp6->valid_lifetime = grub_be_to_cpu32 (iaaddr->valid_lifetime);
d9d99f
+	}
d9d99f
+    }
d9d99f
+}
d9d99f
+
d9d99f
+static void
d9d99f
+parse_dhcp6_option (const struct grub_net_dhcp6_option *opt, void *data)
d9d99f
+{
d9d99f
+  grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t)data;
d9d99f
+  grub_uint16_t code = grub_be_to_cpu16 (opt->code);
d9d99f
+  grub_uint16_t len = grub_be_to_cpu16 (opt->len);
d9d99f
+
d9d99f
+  switch (code)
d9d99f
+    {
d9d99f
+      case GRUB_NET_DHCP6_OPTION_CLIENTID:
d9d99f
+
d9d99f
+	if (dhcp6->client_duid || !len)
d9d99f
+	  {
d9d99f
+	    grub_dprintf ("bootp", "Skipped DHCPv6 CLIENTID with length %u\n", len);
d9d99f
+	    break;
d9d99f
+	  }
d9d99f
+	dhcp6->client_duid = grub_malloc (len);
d9d99f
+	grub_memcpy (dhcp6->client_duid, opt->data, len);
d9d99f
+	dhcp6->client_duid_len = len;
d9d99f
+	break;
d9d99f
+
d9d99f
+      case GRUB_NET_DHCP6_OPTION_SERVERID:
d9d99f
+
d9d99f
+	if (dhcp6->server_duid || !len)
d9d99f
+	  {
d9d99f
+	    grub_dprintf ("bootp", "Skipped DHCPv6 SERVERID with length %u\n", len);
d9d99f
+	    break;
d9d99f
+	  }
d9d99f
+	dhcp6->server_duid = grub_malloc (len);
d9d99f
+	grub_memcpy (dhcp6->server_duid, opt->data, len);
d9d99f
+	dhcp6->server_duid_len = len;
d9d99f
+	break;
d9d99f
+
d9d99f
+      case GRUB_NET_DHCP6_OPTION_IA_NA:
d9d99f
+	{
d9d99f
+	  const struct grub_net_dhcp6_option_iana *ia_na;
d9d99f
+	  grub_uint16_t data_len;
d9d99f
+
d9d99f
+	  if (dhcp6->iaid || len < sizeof (*ia_na))
d9d99f
+	    {
d9d99f
+	      grub_dprintf ("bootp", "Skipped DHCPv6 IA_NA with length %u\n", len);
d9d99f
+	      break;
d9d99f
+	    }
d9d99f
+	  ia_na = (const struct grub_net_dhcp6_option_iana *)opt->data;
d9d99f
+	  dhcp6->iaid = grub_be_to_cpu32 (ia_na->iaid);
d9d99f
+	  dhcp6->t1 = grub_be_to_cpu32 (ia_na->t1);
d9d99f
+	  dhcp6->t2 = grub_be_to_cpu32 (ia_na->t2);
d9d99f
+
d9d99f
+	  data_len = len - sizeof (*ia_na);
d9d99f
+	  if (data_len)
d9d99f
+	    foreach_dhcp6_option ((const struct grub_net_dhcp6_option *)ia_na->data, data_len, parse_dhcp6_iaaddr, dhcp6);
d9d99f
+	}
d9d99f
+	break;
d9d99f
+
d9d99f
+      case GRUB_NET_DHCP6_OPTION_DNS_SERVERS:
d9d99f
+	{
d9d99f
+	  const grub_uint8_t *po;
d9d99f
+	  grub_uint16_t ln;
d9d99f
+	  grub_net_network_level_address_t *la;
d9d99f
+
d9d99f
+	  if (!len || len & 0xf)
d9d99f
+	    {
d9d99f
+	      grub_dprintf ("bootp", "Skip invalid length DHCPv6 DNS_SERVERS \n");
d9d99f
+	      break;
d9d99f
+	    }
d9d99f
+	  dhcp6->num_dns_server = ln = len >> 4;
d9d99f
+	  dhcp6->dns_server_addrs = la = grub_zalloc (ln * sizeof (*la));
d9d99f
+
d9d99f
+	  for (po = opt->data; ln > 0; po += 0x10, la++, ln--)
d9d99f
+	    {
d9d99f
+	      la->type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
d9d99f
+	      la->ipv6[0] = grub_get_unaligned64 (po);
d9d99f
+	      la->ipv6[1] = grub_get_unaligned64 (po + 8);
d9d99f
+	      la->option = DNS_OPTION_PREFER_IPV6;
d9d99f
+	    }
d9d99f
+	}
d9d99f
+	break;
d9d99f
+
d9d99f
+      case GRUB_NET_DHCP6_OPTION_BOOTFILE_URL:
d9d99f
+	dissect_url ((const char *)opt->data,
d9d99f
+		      &dhcp6->boot_file_proto,
d9d99f
+		      &dhcp6->boot_file_server_ip,
d9d99f
+		      &dhcp6->boot_file_path);
d9d99f
+	break;
d9d99f
+
d9d99f
+      default:
d9d99f
+	break;
d9d99f
+    }
d9d99f
+}
d9d99f
+
d9d99f
+static void
d9d99f
+foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt, grub_size_t size, dhcp6_option_hook_fn hook, void *hook_data)
d9d99f
+{
d9d99f
+  while (size)
d9d99f
+    {
d9d99f
+      grub_uint16_t code, len;
d9d99f
+
d9d99f
+      if (size < sizeof (*opt))
d9d99f
+	{
d9d99f
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped with remaining size %" PRIxGRUB_SIZE "\n", size);
d9d99f
+	  break;
d9d99f
+	}
d9d99f
+      size -= sizeof (*opt);
d9d99f
+      len = grub_be_to_cpu16 (opt->len);
d9d99f
+      code = grub_be_to_cpu16 (opt->code);
d9d99f
+      if (size < len)
d9d99f
+	{
d9d99f
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped at out of bound length %u for option %u\n", len, code);
d9d99f
+	  break;
d9d99f
+	}
d9d99f
+      if (!len)
d9d99f
+	{
d9d99f
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped at zero length option %u\n", code);
d9d99f
+	  break;
d9d99f
+	}
d9d99f
+      else
d9d99f
+	{
d9d99f
+	  if (hook)
d9d99f
+	    hook (opt, hook_data);
d9d99f
+	  size -= len;
d9d99f
+	  opt = (const struct grub_net_dhcp6_option *)((grub_uint8_t *)opt + len + sizeof (*opt));
d9d99f
+	}
d9d99f
+    }
d9d99f
+}
d9d99f
+
d9d99f
+static grub_dhcp6_options_t
d9d99f
+grub_dhcp6_options_get (const struct grub_net_dhcp6_packet *v6h,
d9d99f
+			grub_size_t size)
d9d99f
+{
d9d99f
+  grub_dhcp6_options_t options;
d9d99f
+
d9d99f
+  if (size < sizeof (*v6h))
d9d99f
+    {
d9d99f
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("DHCPv6 packet size too small"));
d9d99f
+      return NULL;
d9d99f
+    }
d9d99f
+
d9d99f
+  options = grub_zalloc (sizeof(*options));
d9d99f
+  if (!options)
d9d99f
     return NULL;
d9d99f
 
d9d99f
-  *device = 0;
d9d99f
-  *path = 0;
d9d99f
-
d9d99f
-  grub_dprintf ("net", "mac address is %02x:%02x:%02x:%02x:%02x:%02x\n",
d9d99f
-		hwaddr->mac[0], hwaddr->mac[1], hwaddr->mac[2],
d9d99f
-		hwaddr->mac[3], hwaddr->mac[4], hwaddr->mac[5]);
d9d99f
-
d9d99f
-  if (is_def)
d9d99f
-    grub_net_default_server = 0;
d9d99f
-
d9d99f
-  if (is_def && !grub_net_default_server && packet)
d9d99f
+  foreach_dhcp6_option ((const struct grub_net_dhcp6_option *)v6h->dhcp_options,
d9d99f
+		       size - sizeof (*v6h), parse_dhcp6_option, options);
d9d99f
+
d9d99f
+  return options;
d9d99f
+}
d9d99f
+
d9d99f
+static void
d9d99f
+grub_dhcp6_options_free (grub_dhcp6_options_t options)
d9d99f
+{
d9d99f
+  if (options->client_duid)
d9d99f
+    grub_free (options->client_duid);
d9d99f
+  if (options->server_duid)
d9d99f
+    grub_free (options->server_duid);
d9d99f
+  if (options->ia_addr)
d9d99f
+    grub_free (options->ia_addr);
d9d99f
+  if (options->dns_server_addrs)
d9d99f
+    grub_free (options->dns_server_addrs);
d9d99f
+  if (options->boot_file_proto)
d9d99f
+    grub_free (options->boot_file_proto);
d9d99f
+  if (options->boot_file_server_ip)
d9d99f
+    grub_free (options->boot_file_server_ip);
d9d99f
+  if (options->boot_file_path)
d9d99f
+    grub_free (options->boot_file_path);
d9d99f
+
d9d99f
+  grub_free (options);
d9d99f
+}
d9d99f
+
d9d99f
+static grub_dhcp6_session_t grub_dhcp6_sessions;
d9d99f
+#define FOR_DHCP6_SESSIONS_SAFE(var, next) FOR_LIST_ELEMENTS_SAFE (var, next, grub_dhcp6_sessions)
d9d99f
+#define FOR_DHCP6_SESSIONS(var) FOR_LIST_ELEMENTS (var, grub_dhcp6_sessions)
d9d99f
+
d9d99f
+static void
d9d99f
+grub_net_configure_by_dhcp6_info (const char *name,
d9d99f
+	  struct grub_net_card *card,
d9d99f
+	  grub_dhcp6_options_t dhcp6,
d9d99f
+	  int is_def,
d9d99f
+	  int flags,
d9d99f
+	  struct grub_net_network_level_interface **ret_inf)
d9d99f
+{
d9d99f
+  grub_net_network_level_netaddress_t netaddr;
d9d99f
+  struct grub_net_network_level_interface *inf;
d9d99f
+
d9d99f
+  if (dhcp6->ia_addr)
d9d99f
     {
d9d99f
-      const grub_uint8_t *options = packet->dhcp_options;
d9d99f
-      unsigned int option_max = 1024 - OFFSET_OF (dhcp_options, packet);
d9d99f
-      unsigned int i;
d9d99f
-
d9d99f
-      for (i = 0; i < option_max - sizeof (grub_net_dhcpv6_option_t); )
d9d99f
-	{
d9d99f
-	  grub_uint16_t num, len;
d9d99f
-	  grub_net_dhcpv6_option_t *opt =
d9d99f
-	    (grub_net_dhcpv6_option_t *)(options + i);
d9d99f
-
d9d99f
-	  num = grub_be_to_cpu16(opt->option_num);
d9d99f
-	  len = grub_be_to_cpu16(opt->option_len);
d9d99f
-
d9d99f
-	  grub_dprintf ("net", "got dhcpv6 option %d len %d\n", num, len);
d9d99f
-
d9d99f
-	  if (len == 0)
d9d99f
-	    break;
d9d99f
-
d9d99f
-	  if (len + i > 1024)
d9d99f
-	    break;
d9d99f
-
d9d99f
-	  if (num == GRUB_NET_DHCP6_BOOTFILE_URL)
d9d99f
-	    {
d9d99f
-	      char *scheme, *userinfo, *host, *file;
d9d99f
-	      char *tmp;
d9d99f
-	      int hostlen;
d9d99f
-	      int port;
d9d99f
-	      int rc = extract_url_info ((const char *)opt->option_data,
d9d99f
-					 (grub_size_t)len,
d9d99f
-					 &scheme, &userinfo, &host, &port,
d9d99f
-					 &file;;
d9d99f
-	      if (rc < 0)
d9d99f
-		continue;
d9d99f
-
d9d99f
-	      /* right now this only handles tftp. */
d9d99f
-	      if (grub_strcmp("tftp", scheme))
d9d99f
-		{
d9d99f
-		  grub_free (scheme);
d9d99f
-		  grub_free (userinfo);
d9d99f
-		  grub_free (host);
d9d99f
-		  grub_free (file);
d9d99f
-		  continue;
d9d99f
-		}
d9d99f
-	      grub_free (userinfo);
d9d99f
-
d9d99f
-	      hostlen = grub_strlen (host);
d9d99f
-	      if (hostlen > 2 && host[0] == '[' && host[hostlen-1] == ']')
d9d99f
-		{
d9d99f
-		  tmp = host+1;
d9d99f
-		  host[hostlen-1] = '\0';
d9d99f
-		}
d9d99f
-	      else
d9d99f
-		tmp = host;
d9d99f
+      inf = grub_net_add_addr (name, card, dhcp6->ia_addr, &card->default_address, flags);
d9d99f
 
d9d99f
-	      *device = grub_xasprintf ("%s,%s", scheme, tmp);
d9d99f
-	      grub_free (scheme);
d9d99f
-	      grub_free (host);
d9d99f
+      netaddr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
d9d99f
+      netaddr.ipv6.base[0] = dhcp6->ia_addr->ipv6[0];
d9d99f
+      netaddr.ipv6.base[1] = 0;
d9d99f
+      netaddr.ipv6.masksize = 64;
d9d99f
+      grub_net_add_route (name, netaddr, inf);
d9d99f
 
d9d99f
-	      if (file && *file)
d9d99f
-		{
d9d99f
-		  tmp = grub_strrchr (file, '/');
d9d99f
-		  if (tmp)
d9d99f
-		    *(tmp+1) = '\0';
d9d99f
-		  else
d9d99f
-		    file[0] = '\0';
d9d99f
-		}
d9d99f
-	      else if (!file)
d9d99f
-		file = grub_strdup ("");
d9d99f
-
d9d99f
-	      if (file[0] == '/')
d9d99f
-		{
d9d99f
-		  *path = grub_strdup (file+1);
d9d99f
-		  grub_free (file);
d9d99f
-		}
d9d99f
-	      else
d9d99f
-		*path = file;
d9d99f
-	    }
d9d99f
-	  else if (num == GRUB_NET_DHCP6_IA_NA)
d9d99f
-	    {
d9d99f
-	      const grub_net_dhcpv6_option_t *ia_na_opt;
d9d99f
-	      const grub_net_dhcpv6_opt_ia_na_t *ia_na =
d9d99f
-		(const grub_net_dhcpv6_opt_ia_na_t *)opt;
d9d99f
-	      unsigned int left = len - OFFSET_OF (options, ia_na);
d9d99f
-	      unsigned int j;
d9d99f
-
d9d99f
-	      if ((grub_uint8_t *)ia_na + left >
d9d99f
-		  (grub_uint8_t *)options + option_max)
d9d99f
-		left -= ((grub_uint8_t *)ia_na + left)
d9d99f
-		        - ((grub_uint8_t *)options + option_max);
d9d99f
-
d9d99f
-	      if (len < OFFSET_OF (option_data, opt)
d9d99f
-			+ sizeof (grub_net_dhcpv6_option_t))
d9d99f
-		{
d9d99f
-		  grub_dprintf ("net",
d9d99f
-				"found dhcpv6 ia_na option with no address\n");
d9d99f
-		  continue;
d9d99f
-		}
d9d99f
-
d9d99f
-	      for (j = 0; left > sizeof (grub_net_dhcpv6_option_t); )
d9d99f
-		{
d9d99f
-		  ia_na_opt = (const grub_net_dhcpv6_option_t *)
d9d99f
-			       (ia_na->options + j);
d9d99f
-		  grub_uint16_t ia_na_opt_num, ia_na_opt_len;
d9d99f
-
d9d99f
-		  ia_na_opt_num = grub_be_to_cpu16 (ia_na_opt->option_num);
d9d99f
-		  ia_na_opt_len = grub_be_to_cpu16 (ia_na_opt->option_len);
d9d99f
-		  if (ia_na_opt_len == 0)
d9d99f
-		    break;
d9d99f
-		  if (j + ia_na_opt_len > left)
d9d99f
-		    break;
d9d99f
-		  if (ia_na_opt_num == GRUB_NET_DHCP6_IA_ADDRESS)
d9d99f
-		    {
d9d99f
-		      const grub_net_dhcpv6_opt_ia_address_t *ia_addr;
d9d99f
-
d9d99f
-		      ia_addr = (const grub_net_dhcpv6_opt_ia_address_t *)
d9d99f
-				 ia_na_opt;
d9d99f
-		      addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
d9d99f
-		      grub_memcpy(addr.ipv6, ia_addr->ipv6_address,
d9d99f
-				  sizeof (ia_addr->ipv6_address));
d9d99f
-		      inter = grub_net_add_addr (name, card, &addr, hwaddr, 0);
d9d99f
-		    }
d9d99f
-
d9d99f
-		  j += ia_na_opt_len;
d9d99f
-		  left -= ia_na_opt_len;
d9d99f
-		}
d9d99f
-	    }
d9d99f
+      if (ret_inf)
d9d99f
+	*ret_inf = inf;
d9d99f
+    }
d9d99f
 
d9d99f
-	  i += len + 4;
d9d99f
-	}
d9d99f
+  if (dhcp6->dns_server_addrs)
d9d99f
+    {
d9d99f
+      grub_uint16_t i;
d9d99f
 
d9d99f
-      grub_print_error ();
d9d99f
+      for (i = 0; i < dhcp6->num_dns_server; ++i)
d9d99f
+	grub_net_add_dns_server (dhcp6->dns_server_addrs + i);
d9d99f
     }
d9d99f
 
d9d99f
-  if (is_def)
d9d99f
+  if (dhcp6->boot_file_path)
d9d99f
+    grub_env_set_net_property (name, "boot_file", dhcp6->boot_file_path,
d9d99f
+			  grub_strlen (dhcp6->boot_file_path));
d9d99f
+
d9d99f
+  if (is_def && dhcp6->boot_file_server_ip)
d9d99f
     {
d9d99f
+      grub_net_default_server = grub_strdup (dhcp6->boot_file_server_ip);
d9d99f
       grub_env_set ("net_default_interface", name);
d9d99f
       grub_env_export ("net_default_interface");
d9d99f
     }
d9d99f
+}
d9d99f
+
d9d99f
+static void
d9d99f
+grub_dhcp6_session_add (struct grub_net_network_level_interface *iface,
d9d99f
+			grub_uint32_t iaid)
d9d99f
+{
d9d99f
+  grub_dhcp6_session_t se;
d9d99f
+  struct grub_datetime date;
d9d99f
+  grub_err_t err;
d9d99f
+  grub_int32_t t = 0;
d9d99f
+
d9d99f
+  se = grub_malloc (sizeof (*se));
d9d99f
+
d9d99f
+  err = grub_get_datetime (&date);
d9d99f
+  if (err || !grub_datetime2unixtime (&date, &t))
d9d99f
+    {
d9d99f
+      grub_errno = GRUB_ERR_NONE;
d9d99f
+      t = 0;
d9d99f
+    }
d9d99f
+
d9d99f
+  se->iface = iface;
d9d99f
+  se->iaid = iaid;
d9d99f
+  se->transaction_id = t;
d9d99f
+  se->start_time = grub_get_time_ms ();
d9d99f
+  se->duid.type = grub_cpu_to_be16_compile_time (3) ;
d9d99f
+  se->duid.hw_type = grub_cpu_to_be16_compile_time (1);
d9d99f
+  grub_memcpy (&se->duid.hwaddr, &iface->hwaddress.mac, sizeof (se->duid.hwaddr));
d9d99f
+  se->adv = NULL;
d9d99f
+  se->reply = NULL;
d9d99f
+  grub_list_push (GRUB_AS_LIST_P (&grub_dhcp6_sessions), GRUB_AS_LIST (se));
d9d99f
+}
d9d99f
+
d9d99f
+static void
d9d99f
+grub_dhcp6_session_remove (grub_dhcp6_session_t se)
d9d99f
+{
d9d99f
+  grub_list_remove (GRUB_AS_LIST (se));
d9d99f
+  if (se->adv)
d9d99f
+    grub_dhcp6_options_free (se->adv);
d9d99f
+  if (se->reply)
d9d99f
+    grub_dhcp6_options_free (se->reply);
d9d99f
+  grub_free (se);
d9d99f
+}
d9d99f
+
d9d99f
+static void
d9d99f
+grub_dhcp6_session_remove_all (void)
d9d99f
+{
d9d99f
+  grub_dhcp6_session_t se, next;
d9d99f
+
d9d99f
+  FOR_DHCP6_SESSIONS_SAFE (se, next)
d9d99f
+    {
d9d99f
+      grub_dhcp6_session_remove (se);
d9d99f
+    }
d9d99f
+  grub_dhcp6_sessions = NULL;
d9d99f
+}
d9d99f
+
d9d99f
+static grub_err_t
d9d99f
+grub_dhcp6_session_configure_network (grub_dhcp6_session_t se)
d9d99f
+{
d9d99f
+  char *name;
d9d99f
 
d9d99f
-    if (inter)
d9d99f
-      grub_net_add_ipv6_local (inter, mask);
d9d99f
-    return inter;
d9d99f
+  name = grub_xasprintf ("%s:dhcp6", se->iface->card->name);
d9d99f
+  if (!name)
d9d99f
+    return grub_errno;
d9d99f
+
d9d99f
+  grub_net_configure_by_dhcp6_info (name, se->iface->card, se->reply, 1, 0, 0);
d9d99f
+  grub_free (name);
d9d99f
+
d9d99f
+  return GRUB_ERR_NONE;
d9d99f
 }
d9d99f
 
d9d99f
+static grub_err_t
d9d99f
+grub_dhcp6_session_send_request (grub_dhcp6_session_t se)
d9d99f
+{
d9d99f
+  struct grub_net_buff *nb;
d9d99f
+  struct grub_net_dhcp6_option *opt;
d9d99f
+  struct grub_net_dhcp6_packet *v6h;
d9d99f
+  struct grub_net_dhcp6_option_iana *ia_na;
d9d99f
+  struct grub_net_dhcp6_option_iaaddr *iaaddr;
d9d99f
+  struct udphdr *udph;
d9d99f
+  grub_net_network_level_address_t multicast;
d9d99f
+  grub_net_link_level_address_t ll_multicast;
d9d99f
+  grub_uint64_t elapsed;
d9d99f
+  struct grub_net_network_level_interface *inf = se->iface;
d9d99f
+  grub_dhcp6_options_t dhcp6 = se->adv;
d9d99f
+  grub_err_t err = GRUB_ERR_NONE;
d9d99f
+
d9d99f
+  multicast.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
d9d99f
+  multicast.ipv6[0] = grub_cpu_to_be64_compile_time (0xff02ULL << 48);
d9d99f
+  multicast.ipv6[1] = grub_cpu_to_be64_compile_time (0x10002ULL);
d9d99f
+
d9d99f
+  err = grub_net_link_layer_resolve (inf, &multicast, &ll_multicast);
d9d99f
+  if (err)
d9d99f
+    return err;
d9d99f
+
d9d99f
+  nb = grub_netbuff_alloc (GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
d9d99f
+
d9d99f
+  if (!nb)
d9d99f
+    return grub_errno;
d9d99f
+
d9d99f
+  err = grub_netbuff_reserve (nb, GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
d9d99f
+  if (err)
d9d99f
+    {
d9d99f
+      grub_netbuff_free (nb);
d9d99f
+      return err;
d9d99f
+    }
d9d99f
+
d9d99f
+  err = grub_netbuff_push (nb, dhcp6->client_duid_len + sizeof (*opt));
d9d99f
+  if (err)
d9d99f
+    {
d9d99f
+      grub_netbuff_free (nb);
d9d99f
+      return err;
d9d99f
+    }
d9d99f
+  opt = (struct grub_net_dhcp6_option *)nb->data;
d9d99f
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_CLIENTID);
d9d99f
+  opt->len = grub_cpu_to_be16 (dhcp6->client_duid_len);
d9d99f
+  grub_memcpy (opt->data, dhcp6->client_duid , dhcp6->client_duid_len);
d9d99f
+
d9d99f
+  err = grub_netbuff_push (nb, dhcp6->server_duid_len + sizeof (*opt));
d9d99f
+  if (err)
d9d99f
+    {
d9d99f
+      grub_netbuff_free (nb);
d9d99f
+      return err;
d9d99f
+    }
d9d99f
+  opt = (struct grub_net_dhcp6_option *)nb->data;
d9d99f
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_SERVERID);
d9d99f
+  opt->len = grub_cpu_to_be16 (dhcp6->server_duid_len);
d9d99f
+  grub_memcpy (opt->data, dhcp6->server_duid , dhcp6->server_duid_len);
d9d99f
+
d9d99f
+  err = grub_netbuff_push (nb, sizeof (*ia_na) + sizeof (*opt));
d9d99f
+  if (err)
d9d99f
+    {
d9d99f
+      grub_netbuff_free (nb);
d9d99f
+      return err;
d9d99f
+    }
d9d99f
+
d9d99f
+  if (dhcp6->ia_addr)
d9d99f
+    {
d9d99f
+      err = grub_netbuff_push (nb, sizeof(*iaaddr) + sizeof (*opt));
d9d99f
+      if (err)
d9d99f
+	{
d9d99f
+	  grub_netbuff_free (nb);
d9d99f
+	  return err;
d9d99f
+	}
d9d99f
+    }
d9d99f
+  opt = (struct grub_net_dhcp6_option *)nb->data;
d9d99f
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA);
d9d99f
+  opt->len = grub_cpu_to_be16 (sizeof (*ia_na));
d9d99f
+  if (dhcp6->ia_addr)
d9d99f
+    opt->len += grub_cpu_to_be16 (sizeof(*iaaddr) + sizeof (*opt));
d9d99f
+
d9d99f
+  ia_na = (struct grub_net_dhcp6_option_iana *)opt->data;
d9d99f
+  ia_na->iaid = grub_cpu_to_be32 (dhcp6->iaid);
d9d99f
+
d9d99f
+  ia_na->t1 = grub_cpu_to_be32 (dhcp6->t1);
d9d99f
+  ia_na->t2 = grub_cpu_to_be32 (dhcp6->t2);
d9d99f
+
d9d99f
+  if (dhcp6->ia_addr)
d9d99f
+    {
d9d99f
+      opt = (struct grub_net_dhcp6_option *)ia_na->data;
d9d99f
+      opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IAADDR);
d9d99f
+      opt->len = grub_cpu_to_be16 (sizeof (*iaaddr));
d9d99f
+      iaaddr = (struct grub_net_dhcp6_option_iaaddr *)opt->data;
d9d99f
+      grub_set_unaligned64 (iaaddr->addr, dhcp6->ia_addr->ipv6[0]);
d9d99f
+      grub_set_unaligned64 (iaaddr->addr + 8, dhcp6->ia_addr->ipv6[1]);
d9d99f
+
d9d99f
+      iaaddr->preferred_lifetime = grub_cpu_to_be32 (dhcp6->preferred_lifetime);
d9d99f
+      iaaddr->valid_lifetime = grub_cpu_to_be32 (dhcp6->valid_lifetime);
d9d99f
+    }
d9d99f
+
d9d99f
+  err = grub_netbuff_push (nb, sizeof (*opt) + 2 * sizeof (grub_uint16_t));
d9d99f
+  if (err)
d9d99f
+    {
d9d99f
+      grub_netbuff_free (nb);
d9d99f
+      return err;
d9d99f
+    }
d9d99f
+
d9d99f
+  opt = (struct grub_net_dhcp6_option*) nb->data;
d9d99f
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ORO);
d9d99f
+  opt->len = grub_cpu_to_be16_compile_time (2 * sizeof (grub_uint16_t));
d9d99f
+  grub_set_unaligned16 (opt->data, grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_BOOTFILE_URL));
d9d99f
+  grub_set_unaligned16 (opt->data + 2, grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_DNS_SERVERS));
d9d99f
+
d9d99f
+  err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (grub_uint16_t));
d9d99f
+  if (err)
d9d99f
+    {
d9d99f
+      grub_netbuff_free (nb);
d9d99f
+      return err;
d9d99f
+    }
d9d99f
+  opt = (struct grub_net_dhcp6_option*) nb->data;
d9d99f
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ELAPSED_TIME);
d9d99f
+  opt->len = grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t));
d9d99f
+
d9d99f
+  /* the time is expressed in hundredths of a second */
d9d99f
+  elapsed = grub_divmod64 (grub_get_time_ms () - se->start_time, 10, 0);
d9d99f
+
d9d99f
+  if (elapsed > 0xffff)
d9d99f
+    elapsed = 0xffff;
d9d99f
+
d9d99f
+  grub_set_unaligned16 (opt->data,  grub_cpu_to_be16 ((grub_uint16_t)elapsed));
d9d99f
+
d9d99f
+  err = grub_netbuff_push (nb, sizeof (*v6h));
d9d99f
+  if (err)
d9d99f
+    {
d9d99f
+      grub_netbuff_free (nb);
d9d99f
+      return err;
d9d99f
+    }
d9d99f
+
d9d99f
+  v6h = (struct grub_net_dhcp6_packet *) nb->data;
d9d99f
+  v6h->message_type = GRUB_NET_DHCP6_REQUEST;
d9d99f
+  v6h->transaction_id = se->transaction_id;
d9d99f
+
d9d99f
+  err = grub_netbuff_push (nb, sizeof (*udph));
d9d99f
+  if (err)
d9d99f
+    {
d9d99f
+      grub_netbuff_free (nb);
d9d99f
+      return err;
d9d99f
+    }
d9d99f
+
d9d99f
+  udph = (struct udphdr *) nb->data;
d9d99f
+  udph->src = grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT);
d9d99f
+  udph->dst = grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT);
d9d99f
+  udph->chksum = 0;
d9d99f
+  udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
d9d99f
+
d9d99f
+  udph->chksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
d9d99f
+						 &inf->address,
d9d99f
+						 &multicast);
d9d99f
+  err = grub_net_send_ip_packet (inf, &multicast, &ll_multicast, nb,
d9d99f
+				 GRUB_NET_IP_UDP);
d9d99f
+
d9d99f
+  grub_netbuff_free (nb);
d9d99f
+
d9d99f
+  return err;
d9d99f
+}
d9d99f
+
d9d99f
+struct grub_net_network_level_interface *
d9d99f
+grub_net_configure_by_dhcpv6_reply (const char *name,
d9d99f
+	struct grub_net_card *card,
d9d99f
+	grub_net_interface_flags_t flags,
d9d99f
+	const struct grub_net_dhcp6_packet *v6h,
d9d99f
+	grub_size_t size,
d9d99f
+	int is_def,
d9d99f
+	char **device, char **path)
d9d99f
+{
d9d99f
+  struct grub_net_network_level_interface *inf;
d9d99f
+  grub_dhcp6_options_t dhcp6;
d9d99f
+
d9d99f
+  dhcp6 = grub_dhcp6_options_get (v6h, size);
d9d99f
+  if (!dhcp6)
d9d99f
+    {
d9d99f
+      grub_print_error ();
d9d99f
+      return NULL;
d9d99f
+    }
d9d99f
+
d9d99f
+  grub_net_configure_by_dhcp6_info (name, card, dhcp6, is_def, flags, &inf);
d9d99f
+
d9d99f
+  if (device && dhcp6->boot_file_proto && dhcp6->boot_file_server_ip)
d9d99f
+    {
d9d99f
+      *device = grub_xasprintf ("%s,%s", dhcp6->boot_file_proto, dhcp6->boot_file_server_ip);
d9d99f
+      grub_print_error ();
d9d99f
+    }
d9d99f
+  if (path && dhcp6->boot_file_path)
d9d99f
+    {
d9d99f
+      *path = grub_strdup (dhcp6->boot_file_path);
d9d99f
+      grub_print_error ();
d9d99f
+      if (*path)
d9d99f
+	{
d9d99f
+	  char *slash;
d9d99f
+	  slash = grub_strrchr (*path, '/');
d9d99f
+	  if (slash)
d9d99f
+	    *slash = 0;
d9d99f
+	  else
d9d99f
+	    **path = 0;
d9d99f
+	}
d9d99f
+    }
d9d99f
+
d9d99f
+  grub_dhcp6_options_free (dhcp6);
d9d99f
+  return inf;
d9d99f
+}
d9d99f
 
d9d99f
 void
d9d99f
 grub_net_process_dhcp (struct grub_net_buff *nb,
d9d99f
@@ -550,6 +1042,77 @@ grub_net_process_dhcp (struct grub_net_buff *nb,
d9d99f
     }
d9d99f
 }
d9d99f
 
d9d99f
+grub_err_t
d9d99f
+grub_net_process_dhcp6 (struct grub_net_buff *nb,
d9d99f
+			struct grub_net_card *card __attribute__ ((unused)))
d9d99f
+{
d9d99f
+  const struct grub_net_dhcp6_packet *v6h;
d9d99f
+  grub_dhcp6_session_t se;
d9d99f
+  grub_size_t size;
d9d99f
+  grub_dhcp6_options_t options;
d9d99f
+
d9d99f
+  v6h = (const struct grub_net_dhcp6_packet *) nb->data;
d9d99f
+  size = nb->tail - nb->data;
d9d99f
+
d9d99f
+  options = grub_dhcp6_options_get (v6h, size);
d9d99f
+  if (!options)
d9d99f
+    return grub_errno;
d9d99f
+
d9d99f
+  if (!options->client_duid || !options->server_duid || !options->ia_addr)
d9d99f
+    {
d9d99f
+      grub_dhcp6_options_free (options);
d9d99f
+      return grub_error (GRUB_ERR_BAD_ARGUMENT, "Bad DHCPv6 Packet");
d9d99f
+    }
d9d99f
+
d9d99f
+  FOR_DHCP6_SESSIONS (se)
d9d99f
+    {
d9d99f
+      if (se->transaction_id == v6h->transaction_id &&
d9d99f
+	  grub_memcmp (options->client_duid, &se->duid, sizeof (se->duid)) == 0 &&
d9d99f
+	  se->iaid == options->iaid)
d9d99f
+	break;
d9d99f
+    }
d9d99f
+
d9d99f
+  if (!se)
d9d99f
+    {
d9d99f
+      grub_dprintf ("bootp", "DHCPv6 session not found\n");
d9d99f
+      grub_dhcp6_options_free (options);
d9d99f
+      return GRUB_ERR_NONE;
d9d99f
+    }
d9d99f
+
d9d99f
+  if (v6h->message_type == GRUB_NET_DHCP6_ADVERTISE)
d9d99f
+    {
d9d99f
+      if (se->adv)
d9d99f
+	{
d9d99f
+	  grub_dprintf ("bootp", "Skipped DHCPv6 Advertised .. \n");
d9d99f
+	  grub_dhcp6_options_free (options);
d9d99f
+	  return GRUB_ERR_NONE;
d9d99f
+	}
d9d99f
+
d9d99f
+      se->adv = options;
d9d99f
+      return grub_dhcp6_session_send_request (se);
d9d99f
+    }
d9d99f
+  else if (v6h->message_type == GRUB_NET_DHCP6_REPLY)
d9d99f
+    {
d9d99f
+      if (!se->adv)
d9d99f
+	{
d9d99f
+	  grub_dprintf ("bootp", "Skipped DHCPv6 Reply .. \n");
d9d99f
+	  grub_dhcp6_options_free (options);
d9d99f
+	  return GRUB_ERR_NONE;
d9d99f
+	}
d9d99f
+
d9d99f
+      se->reply = options;
d9d99f
+      grub_dhcp6_session_configure_network (se);
d9d99f
+      grub_dhcp6_session_remove (se);
d9d99f
+      return GRUB_ERR_NONE;
d9d99f
+    }
d9d99f
+  else
d9d99f
+    {
d9d99f
+      grub_dhcp6_options_free (options);
d9d99f
+    }
d9d99f
+
d9d99f
+  return GRUB_ERR_NONE;
d9d99f
+}
d9d99f
+
d9d99f
 static grub_err_t
d9d99f
 grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)),
d9d99f
 		  int argc, char **args)
d9d99f
@@ -824,7 +1387,174 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
d9d99f
   return err;
d9d99f
 }
d9d99f
 
d9d99f
-static grub_command_t cmd_getdhcp, cmd_bootp;
d9d99f
+static grub_err_t
d9d99f
+grub_cmd_bootp6 (struct grub_command *cmd __attribute__ ((unused)),
d9d99f
+		  int argc, char **args)
d9d99f
+{
d9d99f
+  struct grub_net_card *card;
d9d99f
+  grub_uint32_t iaid = 0;
d9d99f
+  int interval;
d9d99f
+  grub_err_t err;
d9d99f
+  grub_dhcp6_session_t se;
d9d99f
+
d9d99f
+  err = GRUB_ERR_NONE;
d9d99f
+
d9d99f
+  FOR_NET_CARDS (card)
d9d99f
+  {
d9d99f
+    struct grub_net_network_level_interface *iface;
d9d99f
+
d9d99f
+    if (argc > 0 && grub_strcmp (card->name, args[0]) != 0)
d9d99f
+      continue;
d9d99f
+
d9d99f
+    iface = grub_net_ipv6_get_link_local (card, &card->default_address);
d9d99f
+    if (!iface)
d9d99f
+      {
d9d99f
+	grub_dhcp6_session_remove_all ();
d9d99f
+	return grub_errno;
d9d99f
+      }
d9d99f
+
d9d99f
+    grub_dhcp6_session_add (iface, iaid++);
d9d99f
+  }
d9d99f
+
d9d99f
+  for (interval = 200; interval < 10000; interval *= 2)
d9d99f
+    {
d9d99f
+      int done = 1;
d9d99f
+
d9d99f
+      FOR_DHCP6_SESSIONS (se)
d9d99f
+	{
d9d99f
+	  struct grub_net_buff *nb;
d9d99f
+	  struct grub_net_dhcp6_option *opt;
d9d99f
+	  struct grub_net_dhcp6_packet *v6h;
d9d99f
+	  struct grub_net_dhcp6_option_duid_ll *duid;
d9d99f
+	  struct grub_net_dhcp6_option_iana *ia_na;
d9d99f
+	  grub_net_network_level_address_t multicast;
d9d99f
+	  grub_net_link_level_address_t ll_multicast;
d9d99f
+	  struct udphdr *udph;
d9d99f
+
d9d99f
+	  multicast.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
d9d99f
+	  multicast.ipv6[0] = grub_cpu_to_be64_compile_time (0xff02ULL << 48);
d9d99f
+	  multicast.ipv6[1] = grub_cpu_to_be64_compile_time (0x10002ULL);
d9d99f
+
d9d99f
+	  err = grub_net_link_layer_resolve (se->iface,
d9d99f
+		    &multicast, &ll_multicast);
d9d99f
+	  if (err)
d9d99f
+	    {
d9d99f
+	      grub_dhcp6_session_remove_all ();
d9d99f
+	      return err;
d9d99f
+	    }
d9d99f
+
d9d99f
+	  nb = grub_netbuff_alloc (GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
d9d99f
+
d9d99f
+	  if (!nb)
d9d99f
+	    {
d9d99f
+	      grub_dhcp6_session_remove_all ();
d9d99f
+	      return grub_errno;
d9d99f
+	    }
d9d99f
+
d9d99f
+	  err = grub_netbuff_reserve (nb, GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
d9d99f
+	  if (err)
d9d99f
+	    {
d9d99f
+	      grub_dhcp6_session_remove_all ();
d9d99f
+	      grub_netbuff_free (nb);
d9d99f
+	      return err;
d9d99f
+	    }
d9d99f
+
d9d99f
+	  err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (grub_uint16_t));
d9d99f
+	  if (err)
d9d99f
+	    {
d9d99f
+	      grub_dhcp6_session_remove_all ();
d9d99f
+	      grub_netbuff_free (nb);
d9d99f
+	      return err;
d9d99f
+	    }
d9d99f
+
d9d99f
+	  opt = (struct grub_net_dhcp6_option *)nb->data;
d9d99f
+	  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ELAPSED_TIME);
d9d99f
+	  opt->len = grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t));
d9d99f
+	  grub_set_unaligned16 (opt->data, 0);
d9d99f
+
d9d99f
+	  err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (*duid));
d9d99f
+	  if (err)
d9d99f
+	    {
d9d99f
+	      grub_dhcp6_session_remove_all ();
d9d99f
+	      grub_netbuff_free (nb);
d9d99f
+	      return err;
d9d99f
+	    }
d9d99f
+
d9d99f
+	  opt = (struct grub_net_dhcp6_option *)nb->data;
d9d99f
+	  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_CLIENTID);
d9d99f
+	  opt->len = grub_cpu_to_be16 (sizeof (*duid));
d9d99f
+
d9d99f
+	  duid = (struct grub_net_dhcp6_option_duid_ll *) opt->data;
d9d99f
+	  grub_memcpy (duid, &se->duid, sizeof (*duid));
d9d99f
+
d9d99f
+	  err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (*ia_na));
d9d99f
+	  if (err)
d9d99f
+	    {
d9d99f
+	      grub_dhcp6_session_remove_all ();
d9d99f
+	      grub_netbuff_free (nb);
d9d99f
+	      return err;
d9d99f
+	    }
d9d99f
+
d9d99f
+	  opt = (struct grub_net_dhcp6_option *)nb->data;
d9d99f
+	  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA);
d9d99f
+	  opt->len = grub_cpu_to_be16 (sizeof (*ia_na));
d9d99f
+	  ia_na = (struct grub_net_dhcp6_option_iana *)opt->data;
d9d99f
+	  ia_na->iaid = grub_cpu_to_be32 (se->iaid);
d9d99f
+	  ia_na->t1 = 0;
d9d99f
+	  ia_na->t2 = 0;
d9d99f
+
d9d99f
+	  err = grub_netbuff_push (nb, sizeof (*v6h));
d9d99f
+	  if (err)
d9d99f
+	    {
d9d99f
+	      grub_dhcp6_session_remove_all ();
d9d99f
+	      grub_netbuff_free (nb);
d9d99f
+	      return err;
d9d99f
+	    }
d9d99f
+
d9d99f
+	  v6h = (struct grub_net_dhcp6_packet *)nb->data;
d9d99f
+	  v6h->message_type = GRUB_NET_DHCP6_SOLICIT;
d9d99f
+	  v6h->transaction_id = se->transaction_id;
d9d99f
+
d9d99f
+	  grub_netbuff_push (nb, sizeof (*udph));
d9d99f
+
d9d99f
+	  udph = (struct udphdr *) nb->data;
d9d99f
+	  udph->src = grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT);
d9d99f
+	  udph->dst = grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT);
d9d99f
+	  udph->chksum = 0;
d9d99f
+	  udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
d9d99f
+
d9d99f
+	  udph->chksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
d9d99f
+			    &se->iface->address, &multicast);
d9d99f
+
d9d99f
+	  err = grub_net_send_ip_packet (se->iface, &multicast,
d9d99f
+		    &ll_multicast, nb, GRUB_NET_IP_UDP);
d9d99f
+	  done = 0;
d9d99f
+	  grub_netbuff_free (nb);
d9d99f
+
d9d99f
+	  if (err)
d9d99f
+	    {
d9d99f
+	      grub_dhcp6_session_remove_all ();
d9d99f
+	      return err;
d9d99f
+	    }
d9d99f
+	}
d9d99f
+      if (!done)
d9d99f
+	grub_net_poll_cards (interval, 0);
d9d99f
+    }
d9d99f
+
d9d99f
+  FOR_DHCP6_SESSIONS (se)
d9d99f
+    {
d9d99f
+      grub_error_push ();
d9d99f
+      err = grub_error (GRUB_ERR_FILE_NOT_FOUND,
d9d99f
+			N_("couldn't autoconfigure %s"),
d9d99f
+			se->iface->card->name);
d9d99f
+    }
d9d99f
+
d9d99f
+  grub_dhcp6_session_remove_all ();
d9d99f
+
d9d99f
+  return err;
d9d99f
+}
d9d99f
+
d9d99f
+static grub_command_t cmd_getdhcp, cmd_bootp, cmd_bootp6;
d9d99f
 
d9d99f
 void
d9d99f
 grub_bootp_init (void)
d9d99f
@@ -835,6 +1565,9 @@ grub_bootp_init (void)
d9d99f
   cmd_getdhcp = grub_register_command ("net_get_dhcp_option", grub_cmd_dhcpopt,
d9d99f
 				       N_("VAR INTERFACE NUMBER DESCRIPTION"),
d9d99f
 				       N_("retrieve DHCP option and save it into VAR. If VAR is - then print the value."));
d9d99f
+  cmd_bootp6 = grub_register_command ("net_bootp6", grub_cmd_bootp6,
d9d99f
+				     N_("[CARD]"),
d9d99f
+				     N_("perform a DHCPv6 autoconfiguration"));
d9d99f
 }
d9d99f
 
d9d99f
 void
d9d99f
@@ -842,4 +1575,5 @@ grub_bootp_fini (void)
d9d99f
 {
d9d99f
   grub_unregister_command (cmd_getdhcp);
d9d99f
   grub_unregister_command (cmd_bootp);
d9d99f
+  grub_unregister_command (cmd_bootp6);
d9d99f
 }
d9d99f
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
d9d99f
index cd6dba79f63..6fd0a4f4998 100644
d9d99f
--- a/grub-core/net/drivers/efi/efinet.c
d9d99f
+++ b/grub-core/net/drivers/efi/efinet.c
d9d99f
@@ -393,9 +393,6 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
d9d99f
     pxe_mode = pxe->mode;
d9d99f
     if (pxe_mode->using_ipv6)
d9d99f
       {
d9d99f
-	grub_net_link_level_address_t hwaddr;
d9d99f
-	struct grub_net_network_level_interface *intf;
d9d99f
-
d9d99f
 	grub_dprintf ("efinet", "using ipv6 and dhcpv6\n");
d9d99f
 	grub_dprintf ("efinet", "dhcp_ack_received: %s%s\n",
d9d99f
 		      pxe_mode->dhcp_ack_received ? "yes" : "no",
d9d99f
@@ -403,15 +400,14 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
d9d99f
 	if (!pxe_mode->dhcp_ack_received)
d9d99f
 	  continue;
d9d99f
 
d9d99f
-	hwaddr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
d9d99f
-	grub_memcpy (hwaddr.mac,
d9d99f
-		     card->efi_net->mode->current_address,
d9d99f
-		     sizeof (hwaddr.mac));
d9d99f
-
d9d99f
-	intf = grub_net_configure_by_dhcpv6_ack (card->name, card, 0, &hwaddr,
d9d99f
-	      (const struct grub_net_dhcpv6_packet *)&pxe_mode->dhcp_ack.dhcpv6,
d9d99f
-	      1, device, path);
d9d99f
-	if (intf && device && path)
d9d99f
+	grub_net_configure_by_dhcpv6_reply (card->name, card, 0,
d9d99f
+					    (struct grub_net_dhcp6_packet *)
d9d99f
+					    &pxe_mode->dhcp_ack,
d9d99f
+					    sizeof (pxe_mode->dhcp_ack),
d9d99f
+					    1, device, path);
d9d99f
+	if (grub_errno)
d9d99f
+	  grub_print_error ();
d9d99f
+	if (device && path)
d9d99f
 	  grub_dprintf ("efinet", "device: `%s' path: `%s'\n", *device, *path);
d9d99f
       }
d9d99f
     else
d9d99f
diff --git a/grub-core/net/ip.c b/grub-core/net/ip.c
d9d99f
index b2ca74b6eb1..9a4e589aa39 100644
d9d99f
--- a/grub-core/net/ip.c
d9d99f
+++ b/grub-core/net/ip.c
d9d99f
@@ -239,6 +239,45 @@ handle_dgram (struct grub_net_buff *nb,
d9d99f
   {
d9d99f
     struct udphdr *udph;
d9d99f
     udph = (struct udphdr *) nb->data;
d9d99f
+
d9d99f
+    if (proto == GRUB_NET_IP_UDP && udph->dst == grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT))
d9d99f
+      {
d9d99f
+	if (udph->chksum)
d9d99f
+	  {
d9d99f
+	    grub_uint16_t chk, expected;
d9d99f
+	    chk = udph->chksum;
d9d99f
+	    udph->chksum = 0;
d9d99f
+	    expected = grub_net_ip_transport_checksum (nb,
d9d99f
+						       GRUB_NET_IP_UDP,
d9d99f
+						       source,
d9d99f
+						       dest);
d9d99f
+	    if (expected != chk)
d9d99f
+	      {
d9d99f
+		grub_dprintf ("net", "Invalid UDP checksum. "
d9d99f
+			      "Expected %x, got %x\n",
d9d99f
+			      grub_be_to_cpu16 (expected),
d9d99f
+			      grub_be_to_cpu16 (chk));
d9d99f
+		grub_netbuff_free (nb);
d9d99f
+		return GRUB_ERR_NONE;
d9d99f
+	      }
d9d99f
+	    udph->chksum = chk;
d9d99f
+	  }
d9d99f
+
d9d99f
+	err = grub_netbuff_pull (nb, sizeof (*udph));
d9d99f
+	if (err)
d9d99f
+	  {
d9d99f
+	    grub_netbuff_free (nb);
d9d99f
+	    return err;
d9d99f
+	  }
d9d99f
+
d9d99f
+	err = grub_net_process_dhcp6 (nb, card);
d9d99f
+	if (err)
d9d99f
+	  grub_print_error ();
d9d99f
+
d9d99f
+	grub_netbuff_free (nb);
d9d99f
+	return GRUB_ERR_NONE;
d9d99f
+      }
d9d99f
+
d9d99f
     if (proto == GRUB_NET_IP_UDP && grub_be_to_cpu16 (udph->dst) == 68)
d9d99f
       {
d9d99f
 	const struct grub_net_bootp_packet *bootp;
d9d99f
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
d9d99f
index e5b521bd9be..1250d493e25 100644
d9d99f
--- a/include/grub/efi/api.h
d9d99f
+++ b/include/grub/efi/api.h
d9d99f
@@ -1507,7 +1507,7 @@ typedef struct grub_efi_pxe_ip_filter
d9d99f
 {
d9d99f
   grub_efi_uint8_t filters;
d9d99f
   grub_efi_uint8_t ip_count;
d9d99f
-  grub_efi_uint8_t reserved;
d9d99f
+  grub_efi_uint16_t reserved;
d9d99f
   grub_efi_ip_address_t ip_list[GRUB_EFI_PXE_MAX_IPCNT];
d9d99f
 } grub_efi_pxe_ip_filter_t;
d9d99f
 
d9d99f
diff --git a/include/grub/net.h b/include/grub/net.h
d9d99f
index f4cd86e582f..5f78b22e109 100644
d9d99f
--- a/include/grub/net.h
d9d99f
+++ b/include/grub/net.h
d9d99f
@@ -447,50 +447,65 @@ struct grub_net_bootp_packet
d9d99f
   grub_uint8_t vendor[0];
d9d99f
 } GRUB_PACKED;
d9d99f
 
d9d99f
-enum
d9d99f
-  {
d9d99f
-    GRUB_NET_DHCP6_IA_NA = 3,
d9d99f
-    GRUB_NET_DHCP6_IA_ADDRESS = 5,
d9d99f
-    GRUB_NET_DHCP6_BOOTFILE_URL = 59,
d9d99f
-  };
d9d99f
-
d9d99f
-struct grub_net_dhcpv6_option
d9d99f
+struct grub_net_dhcp6_packet
d9d99f
 {
d9d99f
-  grub_uint16_t option_num;
d9d99f
-  grub_uint16_t option_len;
d9d99f
-  grub_uint8_t option_data[];
d9d99f
+  grub_uint32_t message_type:8;
d9d99f
+  grub_uint32_t transaction_id:24;
d9d99f
+  grub_uint8_t dhcp_options[0];
d9d99f
 } GRUB_PACKED;
d9d99f
-typedef struct grub_net_dhcpv6_option grub_net_dhcpv6_option_t;
d9d99f
 
d9d99f
-struct grub_net_dhcpv6_opt_ia_na
d9d99f
-{
d9d99f
-  grub_uint16_t option_num;
d9d99f
-  grub_uint16_t option_len;
d9d99f
+struct grub_net_dhcp6_option {
d9d99f
+  grub_uint16_t code;
d9d99f
+  grub_uint16_t len;
d9d99f
+  grub_uint8_t data[0];
d9d99f
+} GRUB_PACKED;
d9d99f
+
d9d99f
+struct grub_net_dhcp6_option_iana {
d9d99f
   grub_uint32_t iaid;
d9d99f
   grub_uint32_t t1;
d9d99f
   grub_uint32_t t2;
d9d99f
-  grub_uint8_t options[];
d9d99f
+  grub_uint8_t data[0];
d9d99f
 } GRUB_PACKED;
d9d99f
-typedef struct grub_net_dhcpv6_opt_ia_na grub_net_dhcpv6_opt_ia_na_t;
d9d99f
 
d9d99f
-struct grub_net_dhcpv6_opt_ia_address
d9d99f
-{
d9d99f
-  grub_uint16_t option_num;
d9d99f
-  grub_uint16_t option_len;
d9d99f
-  grub_uint64_t ipv6_address[2];
d9d99f
+struct grub_net_dhcp6_option_iaaddr {
d9d99f
+  grub_uint8_t addr[16];
d9d99f
   grub_uint32_t preferred_lifetime;
d9d99f
   grub_uint32_t valid_lifetime;
d9d99f
-  grub_uint8_t options[];
d9d99f
+  grub_uint8_t data[0];
d9d99f
 } GRUB_PACKED;
d9d99f
-typedef struct grub_net_dhcpv6_opt_ia_address grub_net_dhcpv6_opt_ia_address_t;
d9d99f
 
d9d99f
-struct grub_net_dhcpv6_packet
d9d99f
+struct grub_net_dhcp6_option_duid_ll
d9d99f
 {
d9d99f
-  grub_uint32_t message_type:8;
d9d99f
-  grub_uint32_t transaction_id:24;
d9d99f
-  grub_uint8_t dhcp_options[1024];
d9d99f
+  grub_uint16_t type;
d9d99f
+  grub_uint16_t hw_type;
d9d99f
+  grub_uint8_t hwaddr[6];
d9d99f
 } GRUB_PACKED;
d9d99f
-typedef struct grub_net_dhcpv6_packet grub_net_dhcpv6_packet_t;
d9d99f
+
d9d99f
+enum
d9d99f
+  {
d9d99f
+    GRUB_NET_DHCP6_SOLICIT = 1,
d9d99f
+    GRUB_NET_DHCP6_ADVERTISE = 2,
d9d99f
+    GRUB_NET_DHCP6_REQUEST = 3,
d9d99f
+    GRUB_NET_DHCP6_REPLY = 7
d9d99f
+  };
d9d99f
+
d9d99f
+enum
d9d99f
+  {
d9d99f
+    DHCP6_CLIENT_PORT = 546,
d9d99f
+    DHCP6_SERVER_PORT = 547
d9d99f
+  };
d9d99f
+
d9d99f
+enum
d9d99f
+  {
d9d99f
+    GRUB_NET_DHCP6_OPTION_CLIENTID = 1,
d9d99f
+    GRUB_NET_DHCP6_OPTION_SERVERID = 2,
d9d99f
+    GRUB_NET_DHCP6_OPTION_IA_NA = 3,
d9d99f
+    GRUB_NET_DHCP6_OPTION_IAADDR = 5,
d9d99f
+    GRUB_NET_DHCP6_OPTION_ORO = 6,
d9d99f
+    GRUB_NET_DHCP6_OPTION_ELAPSED_TIME = 8,
d9d99f
+    GRUB_NET_DHCP6_OPTION_DNS_SERVERS = 23,
d9d99f
+    GRUB_NET_DHCP6_OPTION_BOOTFILE_URL = 59
d9d99f
+  };
d9d99f
 
d9d99f
 #define	GRUB_NET_BOOTP_RFC1048_MAGIC_0	0x63
d9d99f
 #define	GRUB_NET_BOOTP_RFC1048_MAGIC_1	0x82
d9d99f
@@ -521,12 +536,12 @@ grub_net_configure_by_dhcp_ack (const char *name,
d9d99f
 				int is_def, char **device, char **path);
d9d99f
 
d9d99f
 struct grub_net_network_level_interface *
d9d99f
-grub_net_configure_by_dhcpv6_ack (const char *name,
d9d99f
-				 struct grub_net_card *card,
d9d99f
-				 grub_net_interface_flags_t flags,
d9d99f
-				 const grub_net_link_level_address_t *hwaddr,
d9d99f
-				 const struct grub_net_dhcpv6_packet *packet,
d9d99f
-				 int is_def, char **device, char **path);
d9d99f
+grub_net_configure_by_dhcpv6_reply (const char *name,
d9d99f
+				    struct grub_net_card *card,
d9d99f
+				    grub_net_interface_flags_t flags,
d9d99f
+				    const struct grub_net_dhcp6_packet *v6,
d9d99f
+				    grub_size_t size,
d9d99f
+				    int is_def, char **device, char **path);
d9d99f
 
d9d99f
 int
d9d99f
 grub_ipv6_get_masksize(grub_uint16_t *mask);
d9d99f
@@ -543,6 +558,10 @@ void
d9d99f
 grub_net_process_dhcp (struct grub_net_buff *nb,
d9d99f
 		       struct grub_net_card *card);
d9d99f
 
d9d99f
+grub_err_t
d9d99f
+grub_net_process_dhcp6 (struct grub_net_buff *nb,
d9d99f
+			struct grub_net_card *card);
d9d99f
+
d9d99f
 int
d9d99f
 grub_net_hwaddr_cmp (const grub_net_link_level_address_t *a,
d9d99f
 		     const grub_net_link_level_address_t *b);