dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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