dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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