Blame SOURCES/0248-Add-support-for-non-Ethernet-network-cards.patch

f731ee
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f731ee
From: Andrzej Kacprowski <andrzej.kacprowski@intel.com>
f731ee
Date: Fri, 21 Apr 2017 10:06:20 +0200
f731ee
Subject: [PATCH] Add support for non-Ethernet network cards
f731ee
f731ee
This patch replaces fixed 6-byte link layer address with
f731ee
up to 32-byte variable sized address.
f731ee
This allows supporting Infiniband and Omni-Path fabric
f731ee
which use 20-byte address, but other network card types
f731ee
can also take advantage of this change.
f731ee
The network card driver is responsible for replacing L2
f731ee
header provided by grub2 if needed.
f731ee
This approach is compatible with UEFI network stack which
f731ee
also allows up to 32-byte variable size link address.
f731ee
f731ee
The BOOTP/DHCP packet format is limited to 16 byte client
f731ee
hardware address, if link address is more that 16-bytes
f731ee
then chaddr field in BOOTP it will be set to 0 as per rfc4390.
f731ee
f731ee
Resolves: rhbz#1370642
f731ee
f731ee
Signed-off-by: Andrzej Kacprowski <andrzej.kacprowski@intel.com>
f731ee
f731ee
Conflicts:
f731ee
	grub-core/net/ip.c
f731ee
---
f731ee
 grub-core/net/arp.c                    | 157 +++++++++++++++++++++------------
f731ee
 grub-core/net/bootp.c                  |  14 ++-
f731ee
 grub-core/net/drivers/efi/efinet.c     |   8 +-
f731ee
 grub-core/net/drivers/emu/emunet.c     |   1 +
f731ee
 grub-core/net/drivers/i386/pc/pxe.c    |  13 +--
f731ee
 grub-core/net/drivers/ieee1275/ofnet.c |   2 +
f731ee
 grub-core/net/drivers/uboot/ubootnet.c |   1 +
f731ee
 grub-core/net/ethernet.c               |  83 +++++++++--------
f731ee
 grub-core/net/icmp6.c                  |  15 ++--
f731ee
 grub-core/net/ip.c                     |   4 +-
f731ee
 grub-core/net/net.c                    |  48 +++++-----
f731ee
 include/grub/net.h                     |  19 ++--
f731ee
 12 files changed, 213 insertions(+), 152 deletions(-)
f731ee
f731ee
diff --git a/grub-core/net/arp.c b/grub-core/net/arp.c
f731ee
index 996473e1091..badd2d3e684 100644
f731ee
--- a/grub-core/net/arp.c
f731ee
+++ b/grub-core/net/arp.c
f731ee
@@ -31,22 +31,12 @@ enum
f731ee
     ARP_REPLY = 2
f731ee
   };
f731ee
 
f731ee
-enum
f731ee
-  {
f731ee
-    /* IANA ARP constant to define hardware type as ethernet. */
f731ee
-    GRUB_NET_ARPHRD_ETHERNET = 1
f731ee
-  };
f731ee
-
f731ee
-struct arppkt {
f731ee
+struct arphdr {
f731ee
   grub_uint16_t hrd;
f731ee
   grub_uint16_t pro;
f731ee
   grub_uint8_t hln;
f731ee
   grub_uint8_t pln;
f731ee
   grub_uint16_t op;
f731ee
-  grub_uint8_t sender_mac[6];
f731ee
-  grub_uint32_t sender_ip;
f731ee
-  grub_uint8_t recv_mac[6];
f731ee
-  grub_uint32_t recv_ip;
f731ee
 } GRUB_PACKED;
f731ee
 
f731ee
 static int have_pending;
f731ee
@@ -57,12 +47,16 @@ grub_net_arp_send_request (struct grub_net_network_level_interface *inf,
f731ee
 			   const grub_net_network_level_address_t *proto_addr)
f731ee
 {
f731ee
   struct grub_net_buff nb;
f731ee
-  struct arppkt *arp_packet;
f731ee
+  struct arphdr *arp_header;
f731ee
   grub_net_link_level_address_t target_mac_addr;
f731ee
   grub_err_t err;
f731ee
   int i;
f731ee
   grub_uint8_t *nbd;
f731ee
   grub_uint8_t arp_data[128];
f731ee
+  grub_uint8_t hln;
f731ee
+  grub_uint8_t pln;
f731ee
+  grub_uint8_t arp_packet_len;
f731ee
+  grub_uint8_t *tmp_ptr;
f731ee
 
f731ee
   if (proto_addr->type != GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4)
f731ee
     return grub_error (GRUB_ERR_BUG, "unsupported address family");
f731ee
@@ -73,25 +67,39 @@ grub_net_arp_send_request (struct grub_net_network_level_interface *inf,
f731ee
   grub_netbuff_clear (&nb);
f731ee
   grub_netbuff_reserve (&nb, 128);
f731ee
 
f731ee
-  err = grub_netbuff_push (&nb, sizeof (*arp_packet));
f731ee
+  hln = inf->card->default_address.len;
f731ee
+  pln = sizeof (proto_addr->ipv4);
f731ee
+  arp_packet_len = sizeof (*arp_header) + 2 * (hln + pln);
f731ee
+
f731ee
+  err = grub_netbuff_push (&nb, arp_packet_len);
f731ee
   if (err)
f731ee
     return err;
f731ee
 
f731ee
-  arp_packet = (struct arppkt *) nb.data;
f731ee
-  arp_packet->hrd = grub_cpu_to_be16_compile_time (GRUB_NET_ARPHRD_ETHERNET);
f731ee
-  arp_packet->hln = 6;
f731ee
-  arp_packet->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
f731ee
-  arp_packet->pln = 4;
f731ee
-  arp_packet->op = grub_cpu_to_be16_compile_time (ARP_REQUEST);
f731ee
-  /* Sender hardware address.  */
f731ee
-  grub_memcpy (aux, &inf->hwaddress.mac, 6);
f731ee
-
f731ee
-  grub_memcpy (arp_packet->sender_mac, &inf->hwaddress.mac, 6);
f731ee
-  arp_packet->sender_ip = inf->address.ipv4;
f731ee
-  grub_memset (arp_packet->recv_mac, 0, 6);
f731ee
-  arp_packet->recv_ip = proto_addr->ipv4;
f731ee
-  /* Target protocol address */
f731ee
-  grub_memset (&target_mac_addr.mac, 0xff, 6);
f731ee
+  arp_header = (struct arphdr *) nb.data;
f731ee
+  arp_header->hrd = grub_cpu_to_be16 (inf->card->default_address.type);
f731ee
+  arp_header->hln = hln;
f731ee
+  arp_header->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
f731ee
+  arp_header->pln = pln;
f731ee
+  arp_header->op = grub_cpu_to_be16_compile_time (ARP_REQUEST);
f731ee
+  tmp_ptr = nb.data + sizeof (*arp_header);
f731ee
+
f731ee
+  /* The source hardware address. */
f731ee
+  grub_memcpy (tmp_ptr, inf->hwaddress.mac, hln);
f731ee
+  tmp_ptr += hln;
f731ee
+
f731ee
+  /* The source protocol address. */
f731ee
+  grub_memcpy (tmp_ptr, &inf->address.ipv4, pln);
f731ee
+  tmp_ptr += pln;
f731ee
+
f731ee
+  /* The target hardware address. */
f731ee
+  grub_memset (tmp_ptr, 0, hln);
f731ee
+  tmp_ptr += hln;
f731ee
+
f731ee
+  /* The target protocol address */
f731ee
+  grub_memcpy (tmp_ptr, &proto_addr->ipv4, pln);
f731ee
+  tmp_ptr += pln;
f731ee
+
f731ee
+  grub_memset (&target_mac_addr.mac, 0xff, hln);
f731ee
 
f731ee
   nbd = nb.data;
f731ee
   send_ethernet_packet (inf, &nb, target_mac_addr, GRUB_NET_ETHERTYPE_ARP);
f731ee
@@ -116,37 +124,62 @@ grub_err_t
f731ee
 grub_net_arp_receive (struct grub_net_buff *nb,
f731ee
 		      struct grub_net_card *card)
f731ee
 {
f731ee
-  struct arppkt *arp_packet = (struct arppkt *) nb->data;
f731ee
+  struct arphdr *arp_header = (struct arphdr *) nb->data;
f731ee
   grub_net_network_level_address_t sender_addr, target_addr;
f731ee
   grub_net_link_level_address_t sender_mac_addr;
f731ee
   struct grub_net_network_level_interface *inf;
f731ee
+  grub_uint16_t hw_type;
f731ee
+  grub_uint8_t hln;
f731ee
+  grub_uint8_t pln;
f731ee
+  grub_uint8_t arp_packet_len;
f731ee
+  grub_uint8_t *tmp_ptr;
f731ee
 
f731ee
-  if (arp_packet->pro != grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP)
f731ee
-      || arp_packet->pln != 4 || arp_packet->hln != 6
f731ee
-      || nb->tail - nb->data < (int) sizeof (*arp_packet))
f731ee
+  hw_type = card->default_address.type;
f731ee
+  hln = card->default_address.len;
f731ee
+  pln = sizeof(sender_addr.ipv4);
f731ee
+  arp_packet_len = sizeof (*arp_header) + 2 * (pln + hln);
f731ee
+
f731ee
+  if (arp_header->pro != grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP)
f731ee
+      || arp_header->hrd != grub_cpu_to_be16 (hw_type)
f731ee
+      || arp_header->hln != hln || arp_header->pln != pln
f731ee
+      || nb->tail - nb->data < (int) arp_packet_len) {
f731ee
     return GRUB_ERR_NONE;
f731ee
+  }
f731ee
 
f731ee
+  tmp_ptr =  nb->data + sizeof (*arp_header);
f731ee
+
f731ee
+  /* The source hardware address. */
f731ee
+  sender_mac_addr.type = hw_type;
f731ee
+  sender_mac_addr.len = hln;
f731ee
+  grub_memcpy (sender_mac_addr.mac, tmp_ptr, hln);
f731ee
+  tmp_ptr += hln;
f731ee
+
f731ee
+  /* The source protocol address. */
f731ee
   sender_addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
f731ee
+  grub_memcpy(&sender_addr.ipv4, tmp_ptr, pln);
f731ee
+  tmp_ptr += pln;
f731ee
+
f731ee
+  grub_net_link_layer_add_address (card, &sender_addr, &sender_mac_addr, 1);
f731ee
+
f731ee
+  /* The target hardware address. */
f731ee
+  tmp_ptr += hln;
f731ee
+
f731ee
+  /* The target protocol address. */
f731ee
   target_addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
f731ee
-  sender_addr.ipv4 = arp_packet->sender_ip;
f731ee
-  target_addr.ipv4 = arp_packet->recv_ip;
f731ee
-  if (arp_packet->sender_ip == pending_req)
f731ee
+  grub_memcpy(&target_addr.ipv4, tmp_ptr, pln);
f731ee
+
f731ee
+  if (sender_addr.ipv4 == pending_req)
f731ee
     have_pending = 1;
f731ee
 
f731ee
-  sender_mac_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
-  grub_memcpy (sender_mac_addr.mac, arp_packet->sender_mac,
f731ee
-	       sizeof (sender_mac_addr.mac));
f731ee
-  grub_net_link_layer_add_address (card, &sender_addr, &sender_mac_addr, 1);
f731ee
-
f731ee
   FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
f731ee
   {
f731ee
     /* Am I the protocol address target? */
f731ee
     if (grub_net_addr_cmp (&inf->address, &target_addr) == 0
f731ee
-	&& arp_packet->op == grub_cpu_to_be16_compile_time (ARP_REQUEST))
f731ee
+	&& arp_header->op == grub_cpu_to_be16_compile_time (ARP_REQUEST))
f731ee
       {
f731ee
 	grub_net_link_level_address_t target;
f731ee
 	struct grub_net_buff nb_reply;
f731ee
-	struct arppkt *arp_reply;
f731ee
+	struct arphdr *arp_reply;
f731ee
 	grub_uint8_t arp_data[128];
f731ee
 	grub_err_t err;
f731ee
 
f731ee
@@ -155,25 +188,39 @@ grub_net_arp_receive (struct grub_net_buff *nb,
f731ee
 	grub_netbuff_clear (&nb_reply);
f731ee
 	grub_netbuff_reserve (&nb_reply, 128);
f731ee
 
f731ee
-	err = grub_netbuff_push (&nb_reply, sizeof (*arp_packet));
f731ee
+	err = grub_netbuff_push (&nb_reply, arp_packet_len);
f731ee
 	if (err)
f731ee
 	  return err;
f731ee
 
f731ee
-	arp_reply = (struct arppkt *) nb_reply.data;
f731ee
+	arp_reply = (struct arphdr *) nb_reply.data;
f731ee
 
f731ee
-	arp_reply->hrd = grub_cpu_to_be16_compile_time (GRUB_NET_ARPHRD_ETHERNET);
f731ee
+	arp_reply->hrd = grub_cpu_to_be16 (hw_type);
f731ee
 	arp_reply->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
f731ee
-	arp_reply->pln = 4;
f731ee
-	arp_reply->hln = 6;
f731ee
+	arp_reply->pln = pln;
f731ee
+	arp_reply->hln = hln;
f731ee
 	arp_reply->op = grub_cpu_to_be16_compile_time (ARP_REPLY);
f731ee
-	arp_reply->sender_ip = arp_packet->recv_ip;
f731ee
-	arp_reply->recv_ip = arp_packet->sender_ip;
f731ee
-	arp_reply->hln = 6;
f731ee
-
f731ee
-	target.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
-	grub_memcpy (target.mac, arp_packet->sender_mac, 6);
f731ee
-	grub_memcpy (arp_reply->sender_mac, inf->hwaddress.mac, 6);
f731ee
-	grub_memcpy (arp_reply->recv_mac, arp_packet->sender_mac, 6);
f731ee
+
f731ee
+	tmp_ptr = nb_reply.data + sizeof (*arp_reply);
f731ee
+
f731ee
+	/* The source hardware address. */
f731ee
+	grub_memcpy (tmp_ptr, inf->hwaddress.mac, hln);
f731ee
+	tmp_ptr += hln;
f731ee
+
f731ee
+	/* The source protocol address. */
f731ee
+	grub_memcpy (tmp_ptr, &target_addr.ipv4, pln);
f731ee
+	tmp_ptr += pln;
f731ee
+
f731ee
+	/* The target hardware address. */
f731ee
+	grub_memcpy (tmp_ptr, sender_mac_addr.mac, hln);
f731ee
+	tmp_ptr += hln;
f731ee
+
f731ee
+	/* The target protocol address */
f731ee
+	grub_memcpy (tmp_ptr, &sender_addr.ipv4, pln);
f731ee
+	tmp_ptr += pln;
f731ee
+
f731ee
+	target.type = hw_type;
f731ee
+	target.len = hln;
f731ee
+	grub_memcpy (target.mac, sender_mac_addr.mac, hln);
f731ee
 
f731ee
 	/* Change operation to REPLY and send packet */
f731ee
 	send_ethernet_packet (inf, &nb_reply, target, GRUB_NET_ETHERTYPE_ARP);
f731ee
diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c
f731ee
index dfdf7e08d90..0a2d11b8820 100644
f731ee
--- a/grub-core/net/bootp.c
f731ee
+++ b/grub-core/net/bootp.c
f731ee
@@ -219,7 +219,6 @@ grub_net_configure_by_dhcp_ack (const char *name,
f731ee
 				int is_def, char **device, char **path)
f731ee
 {
f731ee
   grub_net_network_level_address_t addr;
f731ee
-  grub_net_link_level_address_t hwaddr;
f731ee
   struct grub_net_network_level_interface *inter;
f731ee
   int mask = -1;
f731ee
 
f731ee
@@ -231,12 +230,8 @@ grub_net_configure_by_dhcp_ack (const char *name,
f731ee
   if (path)
f731ee
     *path = 0;
f731ee
 
f731ee
-  grub_memcpy (hwaddr.mac, bp->mac_addr,
f731ee
-	       bp->hw_len < sizeof (hwaddr.mac) ? bp->hw_len
f731ee
-	       : sizeof (hwaddr.mac));
f731ee
-  hwaddr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
-
f731ee
-  inter = grub_net_add_addr (name, card, &addr, &hwaddr, flags);
f731ee
+  grub_dprintf("dhcp", "configuring dhcp for %s\n", name);
f731ee
+  inter = grub_net_add_addr (name, card, &addr, &card->default_address, flags);
f731ee
   if (!inter)
f731ee
     return 0;
f731ee
 
f731ee
@@ -762,7 +757,8 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
f731ee
 	  grub_memset (pack, 0, sizeof (*pack) + 64);
f731ee
 	  pack->opcode = 1;
f731ee
 	  pack->hw_type = 1;
f731ee
-	  pack->hw_len = 6;
f731ee
+	  pack->hw_len = ifaces[j].hwaddress.len > 16 ? 0
f731ee
+						      : ifaces[j].hwaddress.len;
f731ee
 	  err = grub_get_datetime (&date);
f731ee
 	  if (err || !grub_datetime2unixtime (&date, &t))
f731ee
 	    {
f731ee
@@ -773,7 +769,7 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
f731ee
 	  ifaces[j].dhcp_xid = pack->xid;
f731ee
 	  pack->seconds = grub_cpu_to_be16 (t);
f731ee
 
f731ee
-	  grub_memcpy (&pack->mac_addr, &ifaces[j].hwaddress.mac, 6); 
f731ee
+	  grub_memcpy (&pack->mac_addr, &ifaces[j].hwaddress.mac, pack->hw_len);
f731ee
 
f731ee
 	  grub_netbuff_push (nb, sizeof (*udph));
f731ee
 
f731ee
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
f731ee
index 69b0fc7a8fe..b9ed13fcad1 100644
f731ee
--- a/grub-core/net/drivers/efi/efinet.c
f731ee
+++ b/grub-core/net/drivers/efi/efinet.c
f731ee
@@ -290,6 +290,9 @@ grub_efinet_findcards (void)
f731ee
 	/* This should not happen... Why?  */
f731ee
 	continue;
f731ee
 
f731ee
+      if (net->mode->hwaddr_size > GRUB_NET_MAX_LINK_ADDRESS_SIZE)
f731ee
+	continue;
f731ee
+
f731ee
       if (net->mode->state == GRUB_EFI_NETWORK_STOPPED
f731ee
 	  && efi_call_1 (net->start, net) != GRUB_EFI_SUCCESS)
f731ee
 	continue;
f731ee
@@ -326,10 +329,11 @@ grub_efinet_findcards (void)
f731ee
       card->name = grub_xasprintf ("efinet%d", i++);
f731ee
       card->driver = &efidriver;
f731ee
       card->flags = 0;
f731ee
-      card->default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
+      card->default_address.type = net->mode->if_type;
f731ee
+      card->default_address.len = net->mode->hwaddr_size;
f731ee
       grub_memcpy (card->default_address.mac,
f731ee
 		   net->mode->current_address,
f731ee
-		   sizeof (card->default_address.mac));
f731ee
+		   net->mode->hwaddr_size);
f731ee
       card->efi_net = net;
f731ee
       card->efi_handle = *handle;
f731ee
 
f731ee
diff --git a/grub-core/net/drivers/emu/emunet.c b/grub-core/net/drivers/emu/emunet.c
f731ee
index 7c977cd52ca..962b0c86d2b 100644
f731ee
--- a/grub-core/net/drivers/emu/emunet.c
f731ee
+++ b/grub-core/net/drivers/emu/emunet.c
f731ee
@@ -46,6 +46,7 @@ static struct grub_net_card emucard =
f731ee
     .mtu = 1500,
f731ee
     .default_address = {
f731ee
 			 .type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET,
f731ee
+			 . len = 6,
f731ee
 			 {.mac = {0, 1, 2, 3, 4, 5}}
f731ee
 		       },
f731ee
     .flags = 0
f731ee
diff --git a/grub-core/net/drivers/i386/pc/pxe.c b/grub-core/net/drivers/i386/pc/pxe.c
f731ee
index e8c0b22e294..5660149503d 100644
f731ee
--- a/grub-core/net/drivers/i386/pc/pxe.c
f731ee
+++ b/grub-core/net/drivers/i386/pc/pxe.c
f731ee
@@ -387,20 +387,21 @@ GRUB_MOD_INIT(pxe)
f731ee
   grub_memset (ui, 0, sizeof (*ui));
f731ee
   grub_pxe_call (GRUB_PXENV_UNDI_GET_INFORMATION, ui, pxe_rm_entry);
f731ee
 
f731ee
+  grub_pxe_card.default_address.len = 6;
f731ee
   grub_memcpy (grub_pxe_card.default_address.mac, ui->current_addr,
f731ee
-	       sizeof (grub_pxe_card.default_address.mac));
f731ee
-  for (i = 0; i < sizeof (grub_pxe_card.default_address.mac); i++)
f731ee
+	       grub_pxe_card.default_address.len);
f731ee
+  for (i = 0; i < grub_pxe_card.default_address.len; i++)
f731ee
     if (grub_pxe_card.default_address.mac[i] != 0)
f731ee
       break;
f731ee
-  if (i != sizeof (grub_pxe_card.default_address.mac))
f731ee
+  if (i != grub_pxe_card.default_address.len)
f731ee
     {
f731ee
-      for (i = 0; i < sizeof (grub_pxe_card.default_address.mac); i++)
f731ee
+      for (i = 0; i < grub_pxe_card.default_address.len; i++)
f731ee
 	if (grub_pxe_card.default_address.mac[i] != 0xff)
f731ee
 	  break;
f731ee
     }
f731ee
-  if (i == sizeof (grub_pxe_card.default_address.mac))
f731ee
+  if (i == grub_pxe_card.default_address.len)
f731ee
     grub_memcpy (grub_pxe_card.default_address.mac, ui->permanent_addr,
f731ee
-		 sizeof (grub_pxe_card.default_address.mac));
f731ee
+		 grub_pxe_card.default_address.len);
f731ee
   grub_pxe_card.mtu = ui->mtu;
f731ee
 
f731ee
   grub_pxe_card.default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
diff --git a/grub-core/net/drivers/ieee1275/ofnet.c b/grub-core/net/drivers/ieee1275/ofnet.c
f731ee
index cd24ddc99c5..5a199bcaa75 100644
f731ee
--- a/grub-core/net/drivers/ieee1275/ofnet.c
f731ee
+++ b/grub-core/net/drivers/ieee1275/ofnet.c
f731ee
@@ -154,6 +154,7 @@ grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
f731ee
   struct grub_net_network_level_interface *inter;
f731ee
 
f731ee
   hw_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
+  hw_addr.len = 6;
f731ee
 
f731ee
   args = bootpath + grub_strlen (devpath) + 1;
f731ee
   do
f731ee
@@ -369,6 +370,7 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
f731ee
     grub_memcpy (&lla.mac, pprop, 6);
f731ee
 
f731ee
   lla.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
+  lla.len = 6;
f731ee
   card->default_address = lla;
f731ee
 
f731ee
   card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
f731ee
diff --git a/grub-core/net/drivers/uboot/ubootnet.c b/grub-core/net/drivers/uboot/ubootnet.c
f731ee
index 056052e40d5..22ebcbf211e 100644
f731ee
--- a/grub-core/net/drivers/uboot/ubootnet.c
f731ee
+++ b/grub-core/net/drivers/uboot/ubootnet.c
f731ee
@@ -131,6 +131,7 @@ GRUB_MOD_INIT (ubootnet)
f731ee
 
f731ee
       grub_memcpy (&(card->default_address.mac), &devinfo->di_net.hwaddr, 6);
f731ee
       card->default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
+      card->default_address.len = 6;
f731ee
 
f731ee
       card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
f731ee
       card->txbuf = grub_zalloc (card->txbufsize);
f731ee
diff --git a/grub-core/net/ethernet.c b/grub-core/net/ethernet.c
f731ee
index faaca67c50e..1b479d3cadc 100644
f731ee
--- a/grub-core/net/ethernet.c
f731ee
+++ b/grub-core/net/ethernet.c
f731ee
@@ -29,13 +29,6 @@
f731ee
 
f731ee
 #define LLCADDRMASK 0x7f
f731ee
 
f731ee
-struct etherhdr
f731ee
-{
f731ee
-  grub_uint8_t dst[6];
f731ee
-  grub_uint8_t src[6];
f731ee
-  grub_uint16_t type;
f731ee
-} GRUB_PACKED;
f731ee
-
f731ee
 struct llchdr
f731ee
 {
f731ee
   grub_uint8_t dsap;
f731ee
@@ -55,13 +48,15 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
f731ee
 		      grub_net_link_level_address_t target_addr,
f731ee
 		      grub_net_ethertype_t ethertype)
f731ee
 {
f731ee
-  struct etherhdr *eth;
f731ee
+  grub_uint8_t *eth;
f731ee
   grub_err_t err;
f731ee
   grub_uint32_t vlantag = 0;
f731ee
-  grub_uint8_t etherhdr_size;
f731ee
+  grub_uint8_t hw_addr_len = inf->card->default_address.len;
f731ee
+  grub_uint8_t etherhdr_size = 2 * hw_addr_len + 2;
f731ee
 
f731ee
-  etherhdr_size = sizeof (*eth);
f731ee
-  COMPILE_TIME_ASSERT (sizeof (*eth) + 4 < GRUB_NET_MAX_LINK_HEADER_SIZE);
f731ee
+  /* Source and destination link addresses + ethertype + vlan tag */
f731ee
+  COMPILE_TIME_ASSERT ((GRUB_NET_MAX_LINK_ADDRESS_SIZE * 2 + 2 + 4) <
f731ee
+		       GRUB_NET_MAX_LINK_HEADER_SIZE);
f731ee
 
f731ee
   const char *vlantag_text = grub_env_get ("vlan-tag");
f731ee
   if (vlantag_text != 0) {
f731ee
@@ -72,11 +67,22 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
f731ee
   err = grub_netbuff_push (nb, etherhdr_size);
f731ee
   if (err)
f731ee
     return err;
f731ee
-  eth = (struct etherhdr *) nb->data;
f731ee
-  grub_memcpy (eth->dst, target_addr.mac, 6);
f731ee
-  grub_memcpy (eth->src, inf->hwaddress.mac, 6);
f731ee
+  eth = nb->data;
f731ee
+  grub_memcpy (eth, target_addr.mac, hw_addr_len);
f731ee
+  eth += hw_addr_len;
f731ee
+  grub_memcpy (eth, inf->hwaddress.mac, hw_addr_len);
f731ee
+  eth += hw_addr_len;
f731ee
+
f731ee
+  /* Check if a vlan-tag is present. */
f731ee
+  if (vlantag != 0)
f731ee
+    {
f731ee
+      *((grub_uint32_t *)eth) = grub_cpu_to_be32 (vlantag);
f731ee
+      eth += sizeof (vlantag);
f731ee
+    }
f731ee
+
f731ee
+  /* Write ethertype */
f731ee
+  *((grub_uint16_t*) eth) = grub_cpu_to_be16 (ethertype);
f731ee
 
f731ee
-  eth->type = grub_cpu_to_be16 (ethertype);
f731ee
   if (!inf->card->opened)
f731ee
     {
f731ee
       err = GRUB_ERR_NONE;
f731ee
@@ -87,18 +93,6 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
f731ee
       inf->card->opened = 1;
f731ee
     }
f731ee
 
f731ee
-  /* Check if a vlan-tag is needed. */
f731ee
-  if (vlantag != 0)
f731ee
-    {
f731ee
-      /* Move eth type to the right */
f731ee
-      grub_memcpy((char *) nb->data + etherhdr_size - 2,
f731ee
-                  (char *) nb->data + etherhdr_size - 6, 2);
f731ee
-
f731ee
-      /* Add the tag in the middle */
f731ee
-      grub_memcpy((char *) nb->data + etherhdr_size - 6,
f731ee
-                  &vlantag, 4);
f731ee
-    }
f731ee
-
f731ee
   return inf->card->driver->send (inf->card, nb);
f731ee
 }
f731ee
 
f731ee
@@ -106,29 +100,37 @@ grub_err_t
f731ee
 grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
f731ee
 			       struct grub_net_card *card)
f731ee
 {
f731ee
-  struct etherhdr *eth;
f731ee
+  grub_uint8_t *eth;
f731ee
   struct llchdr *llch;
f731ee
   struct snaphdr *snaph;
f731ee
   grub_net_ethertype_t type;
f731ee
   grub_net_link_level_address_t hwaddress;
f731ee
   grub_net_link_level_address_t src_hwaddress;
f731ee
   grub_err_t err;
f731ee
-  grub_uint8_t etherhdr_size = sizeof (*eth);
f731ee
+  grub_uint8_t hw_addr_len = card->default_address.len;
f731ee
+  grub_uint8_t etherhdr_size = 2 * hw_addr_len + 2;
f731ee
 
f731ee
-  grub_uint16_t vlantag_identifier = 0;
f731ee
-  grub_memcpy (&vlantag_identifier, nb->data + etherhdr_size - 2, 2);
f731ee
+  eth = nb->data;
f731ee
 
f731ee
-  /* Check if a vlan-tag is present. */
f731ee
-  if (vlantag_identifier == VLANTAG_IDENTIFIER)
f731ee
+  hwaddress.type = card->default_address.type;
f731ee
+  hwaddress.len = hw_addr_len;
f731ee
+  grub_memcpy (hwaddress.mac, eth, hw_addr_len);
f731ee
+  eth += hw_addr_len;
f731ee
+
f731ee
+  src_hwaddress.type = card->default_address.type;
f731ee
+  src_hwaddress.len = hw_addr_len;
f731ee
+  grub_memcpy (src_hwaddress.mac, eth, hw_addr_len);
f731ee
+  eth += hw_addr_len;
f731ee
+
f731ee
+  type = grub_be_to_cpu16 (*(grub_uint16_t*)(eth));
f731ee
+  if (type == VLANTAG_IDENTIFIER)
f731ee
     {
f731ee
+      /* Skip vlan tag */
f731ee
       etherhdr_size += 4;
f731ee
-      /* Move eth type to the original position */
f731ee
-      grub_memcpy((char *) nb->data + etherhdr_size - 6,
f731ee
-                  (char *) nb->data + etherhdr_size - 2, 2);
f731ee
+      eth += 4;
f731ee
+      type = grub_be_to_cpu16 (*(grub_uint16_t*)(eth));
f731ee
     }
f731ee
 
f731ee
-  eth = (struct etherhdr *) nb->data;
f731ee
-  type = grub_be_to_cpu16 (eth->type);
f731ee
   err = grub_netbuff_pull (nb, etherhdr_size);
f731ee
   if (err)
f731ee
     return err;
f731ee
@@ -148,11 +150,6 @@ grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
f731ee
 	}
f731ee
     }
f731ee
 
f731ee
-  hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
-  grub_memcpy (hwaddress.mac, eth->dst, sizeof (hwaddress.mac));
f731ee
-  src_hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
-  grub_memcpy (src_hwaddress.mac, eth->src, sizeof (src_hwaddress.mac));
f731ee
-
f731ee
   switch (type)
f731ee
     {
f731ee
       /* ARP packet. */
f731ee
diff --git a/grub-core/net/icmp6.c b/grub-core/net/icmp6.c
f731ee
index 7953e68ecfa..7e7a6bcbd68 100644
f731ee
--- a/grub-core/net/icmp6.c
f731ee
+++ b/grub-core/net/icmp6.c
f731ee
@@ -230,8 +230,9 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
f731ee
 		&& ohdr->len == 1)
f731ee
 	      {
f731ee
 		grub_net_link_level_address_t ll_address;
f731ee
-		ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
-		grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
f731ee
+		ll_address.type = card->default_address.type;
f731ee
+		ll_address.len = card->default_address.len;
f731ee
+		grub_memcpy (ll_address.mac, ohdr + 1, ll_address.len);
f731ee
 		grub_net_link_layer_add_address (card, source, &ll_address, 0);
f731ee
 	      }
f731ee
 	  }
f731ee
@@ -334,8 +335,9 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
f731ee
 		&& ohdr->len == 1)
f731ee
 	      {
f731ee
 		grub_net_link_level_address_t ll_address;
f731ee
-		ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
-		grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
f731ee
+		ll_address.type = card->default_address.type;
f731ee
+		ll_address.len = card->default_address.len;
f731ee
+		grub_memcpy (ll_address.mac, ohdr + 1, ll_address.len);
f731ee
 		grub_net_link_layer_add_address (card, source, &ll_address, 0);
f731ee
 	      }
f731ee
 	  }
f731ee
@@ -366,8 +368,9 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
f731ee
 		&& ohdr->len == 1)
f731ee
 	      {
f731ee
 		grub_net_link_level_address_t ll_address;
f731ee
-		ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
-		grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
f731ee
+		ll_address.type = card->default_address.type;
f731ee
+		ll_address.len = card->default_address.len;
f731ee
+		grub_memcpy (ll_address.mac, ohdr + 1, ll_address.len);
f731ee
 		grub_net_link_layer_add_address (card, source, &ll_address, 0);
f731ee
 	      }
f731ee
 	    if (ohdr->type == OPTION_PREFIX && ohdr->len == 4)
f731ee
diff --git a/grub-core/net/ip.c b/grub-core/net/ip.c
f731ee
index 9e7930caa03..311213feec1 100644
f731ee
--- a/grub-core/net/ip.c
f731ee
+++ b/grub-core/net/ip.c
f731ee
@@ -273,8 +273,8 @@ handle_dgram (struct grub_net_buff *nb,
f731ee
 	      && inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV
f731ee
 	      && inf->dhcp_xid == bootp->xid
f731ee
 	      && inf->hwaddress.type == GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
f731ee
-	      && grub_memcmp (inf->hwaddress.mac, &bootp->mac_addr,
f731ee
-			      sizeof (inf->hwaddress.mac)) == 0)
f731ee
+	      && (grub_memcmp (inf->hwaddress.mac, &bootp->mac_addr,
f731ee
+			       bootp->hw_len) == 0 || bootp->hw_len == 0))
f731ee
 	    {
f731ee
 	      grub_net_process_dhcp (nb, inf->card);
f731ee
 	      grub_netbuff_free (nb);
f731ee
diff --git a/grub-core/net/net.c b/grub-core/net/net.c
f731ee
index fc6f714bf7f..b3a80ba2954 100644
f731ee
--- a/grub-core/net/net.c
f731ee
+++ b/grub-core/net/net.c
f731ee
@@ -143,8 +143,9 @@ grub_net_link_layer_resolve (struct grub_net_network_level_interface *inf,
f731ee
 								   << 48)
f731ee
 	  && proto_addr->ipv6[1] == (grub_be_to_cpu64_compile_time (1))))
f731ee
     {
f731ee
-      hw_addr->type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
-      grub_memset (hw_addr->mac, -1, 6);
f731ee
+      hw_addr->type = inf->card->default_address.type;
f731ee
+      hw_addr->len = inf->card->default_address.len;
f731ee
+      grub_memset (hw_addr->mac, -1, hw_addr->len);
f731ee
       return GRUB_ERR_NONE;
f731ee
     }
f731ee
 
f731ee
@@ -152,6 +153,7 @@ grub_net_link_layer_resolve (struct grub_net_network_level_interface *inf,
f731ee
       && ((grub_be_to_cpu64 (proto_addr->ipv6[0]) >> 56) == 0xff))
f731ee
     {
f731ee
       hw_addr->type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f731ee
+      hw_addr->len = inf->card->default_address.len;
f731ee
       hw_addr->mac[0] = 0x33;
f731ee
       hw_addr->mac[1] = 0x33;
f731ee
       hw_addr->mac[2] = ((grub_be_to_cpu64 (proto_addr->ipv6[1]) >> 24) & 0xff);
f731ee
@@ -771,23 +773,21 @@ grub_net_addr_to_str (const grub_net_network_level_address_t *target, char *buf)
f731ee
 void
f731ee
 grub_net_hwaddr_to_str (const grub_net_link_level_address_t *addr, char *str)
f731ee
 {
f731ee
-  str[0] = 0;
f731ee
-  switch (addr->type)
f731ee
+  char *ptr;
f731ee
+  unsigned i;
f731ee
+
f731ee
+  if (addr->len > GRUB_NET_MAX_LINK_ADDRESS_SIZE)
f731ee
     {
f731ee
-    case GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET:
f731ee
-      {
f731ee
-	char *ptr;
f731ee
-	unsigned i;
f731ee
-	for (ptr = str, i = 0; i < ARRAY_SIZE (addr->mac); i++)
f731ee
-	  {
f731ee
-	    grub_snprintf (ptr, GRUB_NET_MAX_STR_HWADDR_LEN - (ptr - str),
f731ee
-			   "%02x:", addr->mac[i] & 0xff);
f731ee
-	    ptr += (sizeof ("XX:") - 1);
f731ee
-	  }
f731ee
-      return;
f731ee
-      }
f731ee
+       str[0] = 0;
f731ee
+       grub_printf (_("Unsupported hw address type %d len %d\n"),
f731ee
+		    addr->type, addr->len);
f731ee
+       return;
f731ee
+    }
f731ee
+  for (ptr = str, i = 0; i < addr->len; i++)
f731ee
+    {
f731ee
+      ptr += grub_snprintf (ptr, GRUB_NET_MAX_STR_HWADDR_LEN - (ptr - str),
f731ee
+		     "%02x:", addr->mac[i] & 0xff);
f731ee
     }
f731ee
-  grub_printf (_("Unsupported hw address type %d\n"), addr->type);
f731ee
 }
f731ee
 
f731ee
 int
f731ee
@@ -798,13 +798,17 @@ grub_net_hwaddr_cmp (const grub_net_link_level_address_t *a,
f731ee
     return -1;
f731ee
   if (a->type > b->type)
f731ee
     return +1;
f731ee
-  switch (a->type)
f731ee
+  if (a->len < b->len)
f731ee
+    return -1;
f731ee
+  if (a->len > b->len)
f731ee
+    return +1;
f731ee
+  if (a->len > GRUB_NET_MAX_LINK_ADDRESS_SIZE)
f731ee
     {
f731ee
-    case GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET:
f731ee
-      return grub_memcmp (a->mac, b->mac, sizeof (a->mac));
f731ee
+      grub_printf (_("Unsupported hw address type %d len %d\n"),
f731ee
+		   a->type, a->len);
f731ee
+      return + 1;
f731ee
     }
f731ee
-  grub_printf (_("Unsupported hw address type %d\n"), a->type);
f731ee
-  return 1;
f731ee
+  return grub_memcmp (a->mac, b->mac, a->len);
f731ee
 }
f731ee
 
f731ee
 int
f731ee
diff --git a/include/grub/net.h b/include/grub/net.h
f731ee
index b1bc23048f1..0d9213d6759 100644
f731ee
--- a/include/grub/net.h
f731ee
+++ b/include/grub/net.h
f731ee
@@ -29,7 +29,8 @@
f731ee
 
f731ee
 enum
f731ee
   {
f731ee
-    GRUB_NET_MAX_LINK_HEADER_SIZE = 64,
f731ee
+    GRUB_NET_MAX_LINK_HEADER_SIZE = 96,
f731ee
+    GRUB_NET_MAX_LINK_ADDRESS_SIZE = 32,
f731ee
     GRUB_NET_UDP_HEADER_SIZE = 8,
f731ee
     GRUB_NET_TCP_HEADER_SIZE = 20,
f731ee
     GRUB_NET_OUR_IPV4_HEADER_SIZE = 20,
f731ee
@@ -42,15 +43,17 @@ enum
f731ee
 
f731ee
 typedef enum grub_link_level_protocol_id 
f731ee
 {
f731ee
-  GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
f731ee
+  /* IANA ARP constant to define hardware type. */
f731ee
+  GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET = 1,
f731ee
 } grub_link_level_protocol_id_t;
f731ee
 
f731ee
 typedef struct grub_net_link_level_address
f731ee
 {
f731ee
   grub_link_level_protocol_id_t type;
f731ee
+  grub_uint8_t len;
f731ee
   union
f731ee
   {
f731ee
-    grub_uint8_t mac[6];
f731ee
+    grub_uint8_t mac[GRUB_NET_MAX_LINK_ADDRESS_SIZE];
f731ee
   };
f731ee
 } grub_net_link_level_address_t;
f731ee
 
f731ee
@@ -531,11 +534,13 @@ grub_net_addr_cmp (const grub_net_network_level_address_t *a,
f731ee
 #define GRUB_NET_MAX_STR_ADDR_LEN sizeof ("XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX")
f731ee
 
f731ee
 /*
f731ee
-  Currently suppoerted adresses:
f731ee
-  ethernet:   XX:XX:XX:XX:XX:XX
f731ee
+  Up to 32 byte hardware address supported, see GRUB_NET_MAX_LINK_ADDRESS_SIZE
f731ee
  */
f731ee
-
f731ee
-#define GRUB_NET_MAX_STR_HWADDR_LEN (sizeof ("XX:XX:XX:XX:XX:XX"))
f731ee
+#define GRUB_NET_MAX_STR_HWADDR_LEN (sizeof (\
f731ee
+	"XX:XX:XX:XX:XX:XX:XX:XX:"\
f731ee
+	"XX:XX:XX:XX:XX:XX:XX:XX:"\
f731ee
+	"XX:XX:XX:XX:XX:XX:XX:XX:"\
f731ee
+	"XX:XX:XX:XX:XX:XX:XX:XX"))
f731ee
 
f731ee
 void
f731ee
 grub_net_addr_to_str (const grub_net_network_level_address_t *target,