nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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