File 0002-Add-Virtual-LAN-support.patch of Package grub2.14515

From d17c80ca8de35f8cb06175965d428bdabbda3c19 Mon Sep 17 00:00:00 2001
From: Paulo Flabiano Smorigo <pfsmorigo@linux.vnet.ibm.com>
Date: Mon, 30 Jun 2014 10:37:08 -0300
Subject: [PATCH 2/2] Add Virtual LAN support.

This patch adds support for virtual LAN (VLAN) tagging. VLAN tagging allows
multiple VLANs in a bridged network to share the same physical network link
but maintain isolation:

http://en.wikipedia.org/wiki/IEEE_802.1Q
---
 ChangeLog                              | 17 ++++++++++++
 grub-core/net/arp.c                    | 10 +++++--
 grub-core/net/drivers/ieee1275/ofnet.c | 20 +++++++++++++-
 grub-core/net/ethernet.c               | 49 ++++++++++++++++++++++++++++++----
 grub-core/net/ip.c                     | 31 ++++++++++++++-------
 include/grub/net.h                     |  9 +++++++
 include/grub/net/arp.h                 |  5 ++--
 include/grub/net/ip.h                  |  3 ++-
 8 files changed, 124 insertions(+), 20 deletions(-)

Index: grub-2.02~beta3/grub-core/net/arp.c
===================================================================
--- grub-2.02~beta3.orig/grub-core/net/arp.c	2016-02-28 19:23:27.060877621 +0300
+++ grub-2.02~beta3/grub-core/net/arp.c	2016-02-28 19:23:27.056877621 +0300
@@ -111,8 +111,8 @@
 }
 
 grub_err_t
-grub_net_arp_receive (struct grub_net_buff *nb,
-		      struct grub_net_card *card)
+grub_net_arp_receive (struct grub_net_buff *nb, struct grub_net_card *card,
+                      grub_uint16_t vlantag_vid)
 {
   struct arppkt *arp_packet = (struct arppkt *) nb->data;
   grub_net_network_level_address_t sender_addr, target_addr;
@@ -138,6 +138,12 @@
 
   FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
   {
+    /* Check vlantag id */
+    if (inf->card == card &&
+        ((inf->vlantag.set && vlantag_vid != inf->vlantag.vid) ||
+         (!inf->vlantag.set && vlantag_vid != 0)))
+        continue;
+
     /* Am I the protocol address target? */
     if (grub_net_addr_cmp (&inf->address, &target_addr) == 0
 	&& arp_packet->op == grub_cpu_to_be16_compile_time (ARP_REQUEST))
Index: grub-2.02~beta3/grub-core/net/drivers/ieee1275/ofnet.c
===================================================================
--- grub-2.02~beta3.orig/grub-core/net/drivers/ieee1275/ofnet.c	2016-02-28 19:23:27.060877621 +0300
+++ grub-2.02~beta3/grub-core/net/drivers/ieee1275/ofnet.c	2016-02-28 19:24:47.004878566 +0300
@@ -147,11 +147,11 @@
   char *comma_char = 0;
   char *equal_char = 0;
   grub_size_t field_counter = 0;
-
   grub_net_network_level_address_t client_addr, gateway_addr, subnet_mask;
   grub_net_link_level_address_t hw_addr;
   grub_net_interface_flags_t flags = 0;
   struct grub_net_network_level_interface *inter = NULL;
+  grub_uint32_t vlantag = 0;
 
   hw_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
 
@@ -169,6 +169,18 @@
           *equal_char = 0;
           grub_env_set_net_property ((*card)->name, args, equal_char + 1,
                                      grub_strlen(equal_char + 1));
+
+          if ((grub_strcmp (args, "vtag") == 0) &&
+              (grub_strlen (equal_char + 1) > 4))
+            {
+            vlantag = grub_strtoul (equal_char + 1, 0, 16) & 0xffff;
+            if (grub_errno == GRUB_ERR_BAD_NUMBER)
+              {
+                vlantag = 0;
+                grub_errno = GRUB_ERR_NONE;
+              }
+            }
+
           *equal_char = '=';
         }
       else
@@ -207,6 +219,12 @@
                                   hw_addr.mac, sizeof(hw_addr.mac), 0);
       inter = grub_net_add_addr ((*card)->name, *card, &client_addr, &hw_addr,
                                  flags);
+      if (vlantag > 0)
+        {
+          inter->vlantag.set = 1;
+          inter->vlantag.vid = vlantag & 0xfff;
+        }
+
       grub_net_add_ipv4_local (inter,
                           __builtin_ctz (~grub_le_to_cpu32 (subnet_mask.ipv4)));
     }
Index: grub-2.02~beta3/grub-core/net/ethernet.c
===================================================================
--- grub-2.02~beta3.orig/grub-core/net/ethernet.c	2016-02-28 19:23:27.060877621 +0300
+++ grub-2.02~beta3/grub-core/net/ethernet.c	2016-02-28 19:23:27.056877621 +0300
@@ -18,6 +18,7 @@
 
 #include <grub/misc.h>
 #include <grub/mm.h>
+#include <grub/env.h>
 #include <grub/net/ethernet.h>
 #include <grub/net/ip.h>
 #include <grub/net/arp.h>
@@ -56,10 +57,16 @@
 {
   struct etherhdr *eth;
   grub_err_t err;
+  grub_uint8_t etherhdr_size;
 
-  COMPILE_TIME_ASSERT (sizeof (*eth) < GRUB_NET_MAX_LINK_HEADER_SIZE);
+  etherhdr_size = sizeof (*eth);
+  COMPILE_TIME_ASSERT (sizeof (*eth) + 4 < GRUB_NET_MAX_LINK_HEADER_SIZE);
 
-  err = grub_netbuff_push (nb, sizeof (*eth));
+  /* Increase ethernet header in case of vlantag */
+  if (inf->vlantag.set)
+    etherhdr_size += 4;
+
+  err = grub_netbuff_push (nb, etherhdr_size);
   if (err)
     return err;
   eth = (struct etherhdr *) nb->data;
@@ -76,6 +83,21 @@
 	return err;
       inf->card->opened = 1;
     }
+
+  /* Check and add a vlan-tag if needed. */
+  if (inf->vlantag.set)
+    {
+      grub_uint32_t vlantag;
+      vlantag = grub_cpu_to_be32 ((VLANTAG_IDENTIFIER << 16) | inf->vlantag.vid);
+
+      /* Move eth type to the right */
+      grub_memcpy ((char *) nb->data + etherhdr_size - 2,
+                   (char *) nb->data + etherhdr_size - 6, 2);
+
+      /* Add the tag in the middle */
+      grub_memcpy ((char *) nb->data + etherhdr_size - 6, &vlantag, 4);
+    }
+
   return inf->card->driver->send (inf->card, nb);
 }
 
@@ -90,10 +112,26 @@
   grub_net_link_level_address_t hwaddress;
   grub_net_link_level_address_t src_hwaddress;
   grub_err_t err;
+  grub_uint8_t etherhdr_size = sizeof (*eth);
+  grub_uint16_t vlantag_vid = 0;
+
+  /* Check if a vlan-tag is present. If so, the ethernet header is 4 bytes */
+  /* longer than the original one. The vlantag id is extracted and the header */
+  /* is reseted to the original size. */
+  if (grub_get_unaligned16 (nb->data + etherhdr_size - 2) ==
+      grub_cpu_to_be16_compile_time (VLANTAG_IDENTIFIER))
+    {
+      vlantag_vid = grub_be_to_cpu16 (grub_get_unaligned16 (nb->data +
+                                                      etherhdr_size)) & 0x1fff;
+      etherhdr_size += 4;
+      /* Move eth type to the original position */
+      grub_memcpy((char *) nb->data + etherhdr_size - 6,
+                  (char *) nb->data + etherhdr_size - 2, 2);
+    }
 
   eth = (struct etherhdr *) nb->data;
   type = grub_be_to_cpu16 (eth->type);
-  err = grub_netbuff_pull (nb, sizeof (*eth));
+  err = grub_netbuff_pull (nb, etherhdr_size);
   if (err)
     return err;
 
@@ -121,13 +159,14 @@
     {
       /* ARP packet. */
     case GRUB_NET_ETHERTYPE_ARP:
-      grub_net_arp_receive (nb, card);
+      grub_net_arp_receive (nb, card, vlantag_vid);
       grub_netbuff_free (nb);
       return GRUB_ERR_NONE;
       /* IP packet.  */
     case GRUB_NET_ETHERTYPE_IP:
     case GRUB_NET_ETHERTYPE_IP6:
-      return grub_net_recv_ip_packets (nb, card, &hwaddress, &src_hwaddress);
+      return grub_net_recv_ip_packets (nb, card, &hwaddress, &src_hwaddress,
+                                       vlantag_vid);
     }
   grub_netbuff_free (nb);
   return GRUB_ERR_NONE;
Index: grub-2.02~beta3/grub-core/net/ip.c
===================================================================
--- grub-2.02~beta3.orig/grub-core/net/ip.c	2016-02-28 19:23:27.060877621 +0300
+++ grub-2.02~beta3/grub-core/net/ip.c	2016-02-28 19:23:27.056877621 +0300
@@ -228,12 +228,13 @@
 	      grub_net_ip_protocol_t proto,
 	      const grub_net_network_level_address_t *source,
 	      const grub_net_network_level_address_t *dest,
+              grub_uint16_t vlantag_vid,
 	      grub_uint8_t ttl)
 {
   struct grub_net_network_level_interface *inf = NULL;
   grub_err_t err;
   int multicast = 0;
-  
+
   /* DHCP needs special treatment since we don't know IP yet.  */
   {
     struct udphdr *udph;
@@ -293,6 +294,13 @@
 	&& grub_net_addr_cmp (&inf->address, dest) == 0
 	&& grub_net_hwaddr_cmp (&inf->hwaddress, hwaddress) == 0)
       break;
+
+    /* Check vlantag id */
+    if (inf->card == card &&
+        ((inf->vlantag.set && vlantag_vid != inf->vlantag.vid) ||
+         (!inf->vlantag.set && vlantag_vid != 0)))
+        continue;
+
     /* Solicited node multicast.  */
     if (inf->card == card
 	&& inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
@@ -381,7 +389,8 @@
 grub_net_recv_ip4_packets (struct grub_net_buff *nb,
 			   struct grub_net_card *card,
 			   const grub_net_link_level_address_t *hwaddress,
-			   const grub_net_link_level_address_t *src_hwaddress)
+			   const grub_net_link_level_address_t *src_hwaddress,
+                           grub_uint16_t vlantag_vid)
 {
   struct iphdr *iph = (struct iphdr *) nb->data;
   grub_err_t err;
@@ -456,7 +465,7 @@
       dest.ipv4 = iph->dest;
 
       return handle_dgram (nb, card, src_hwaddress, hwaddress, iph->protocol,
-			   &source, &dest, iph->ttl);
+			   &source, &dest, vlantag_vid, iph->ttl);
     }
 
   for (prev = &reassembles, rsm = *prev; rsm; prev = &rsm->next, rsm = *prev)
@@ -592,7 +601,7 @@
       dest.ipv4 = dst;
 
       return handle_dgram (ret, card, src_hwaddress,
-			   hwaddress, proto, &source, &dest,
+			   hwaddress, proto, &source, &dest, vlantag_vid,
 			   ttl);
     }
 }
@@ -650,7 +659,8 @@
 grub_net_recv_ip6_packets (struct grub_net_buff *nb,
 			   struct grub_net_card *card,
 			   const grub_net_link_level_address_t *hwaddress,
-			   const grub_net_link_level_address_t *src_hwaddress)
+			   const grub_net_link_level_address_t *src_hwaddress,
+                           grub_uint16_t vlantag_vid)
 {
   struct ip6hdr *iph = (struct ip6hdr *) nb->data;
   grub_err_t err;
@@ -701,21 +711,24 @@
   grub_memcpy (dest.ipv6, &iph->dest, sizeof (dest.ipv6));
 
   return handle_dgram (nb, card, src_hwaddress, hwaddress, iph->protocol,
-		       &source, &dest, iph->ttl);
+		       &source, &dest, vlantag_vid, iph->ttl);
 }
 
 grub_err_t
 grub_net_recv_ip_packets (struct grub_net_buff *nb,
 			  struct grub_net_card *card,
 			  const grub_net_link_level_address_t *hwaddress,
-			  const grub_net_link_level_address_t *src_hwaddress)
+			  const grub_net_link_level_address_t *src_hwaddress,
+                          grub_uint16_t vlantag_vid)
 {
   struct iphdr *iph = (struct iphdr *) nb->data;
 
   if ((iph->verhdrlen >> 4) == 4)
-    return grub_net_recv_ip4_packets (nb, card, hwaddress, src_hwaddress);
+    return grub_net_recv_ip4_packets (nb, card, hwaddress, src_hwaddress,
+                                      vlantag_vid);
   if ((iph->verhdrlen >> 4) == 6)
-    return grub_net_recv_ip6_packets (nb, card, hwaddress, src_hwaddress);
+    return grub_net_recv_ip6_packets (nb, card, hwaddress, src_hwaddress,
+                                      vlantag_vid);
   grub_dprintf ("net", "Bad IP version: %d\n", (iph->verhdrlen >> 4));
   grub_netbuff_free (nb);
   return GRUB_ERR_NONE;
Index: grub-2.02~beta3/include/grub/net.h
===================================================================
--- grub-2.02~beta3.orig/include/grub/net.h	2016-02-28 19:23:27.060877621 +0300
+++ grub-2.02~beta3/include/grub/net.h	2016-02-28 19:23:27.056877621 +0300
@@ -280,6 +280,12 @@
 
 extern grub_net_t (*EXPORT_VAR (grub_net_open)) (const char *name);
 
+struct grub_net_vlantag
+{
+  grub_uint8_t set;
+  grub_uint16_t vid;
+};
+
 struct grub_net_network_level_interface
 {
   struct grub_net_network_level_interface *next;
@@ -291,6 +297,7 @@
   grub_net_interface_flags_t flags;
   struct grub_net_bootp_packet *dhcp_ack;
   grub_size_t dhcp_acklen;
+  struct grub_net_vlantag vlantag;
   void *data;
 };
 
@@ -561,4 +568,6 @@
 #define GRUB_NET_INTERVAL 400
 #define GRUB_NET_INTERVAL_ADDITION 20
 
+#define VLANTAG_IDENTIFIER 0x8100
+
 #endif /* ! GRUB_NET_HEADER */
Index: grub-2.02~beta3/include/grub/net/arp.h
===================================================================
--- grub-2.02~beta3.orig/include/grub/net/arp.h	2016-02-28 19:23:27.060877621 +0300
+++ grub-2.02~beta3/include/grub/net/arp.h	2016-02-28 19:23:27.056877621 +0300
@@ -22,10 +22,11 @@
 #include <grub/net.h>
 
 extern grub_err_t grub_net_arp_receive (struct grub_net_buff *nb,
-					struct grub_net_card *card);
+                                        struct grub_net_card *card,
+                                        grub_uint16_t vlantag_vid);
 
 grub_err_t
 grub_net_arp_send_request (struct grub_net_network_level_interface *inf,
-			   const grub_net_network_level_address_t *proto_addr);
+                           const grub_net_network_level_address_t *proto_addr);
 
 #endif 
Index: grub-2.02~beta3/include/grub/net/ip.h
===================================================================
--- grub-2.02~beta3.orig/include/grub/net/ip.h	2016-02-28 19:23:27.060877621 +0300
+++ grub-2.02~beta3/include/grub/net/ip.h	2016-02-28 19:23:27.056877621 +0300
@@ -48,7 +48,8 @@
 grub_net_recv_ip_packets (struct grub_net_buff *nb,
 			  struct grub_net_card *card,
 			  const grub_net_link_level_address_t *hwaddress,
-			  const grub_net_link_level_address_t *src_hwaddress);
+			  const grub_net_link_level_address_t *src_hwaddress,
+                          grub_uint16_t vlantag_vid);
 
 grub_err_t
 grub_net_send_ip_packet (struct grub_net_network_level_interface *inf,
openSUSE Build Service is sponsored by