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

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