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