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

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