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

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