zbyszek / rpms / grub2

Forked from rpms/grub2 5 years ago
Clone
011fe81
From c987623113e2d03e80f0ba1e108bf0a800628f85 Mon Sep 17 00:00:00 2001
481bf3b
From: Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
481bf3b
Date: Tue, 30 Oct 2012 15:19:39 -0200
58fe9aa
Subject: [PATCH 097/152] Add vlan-tag support
481bf3b
481bf3b
This patch adds support for virtual LAN (VLAN) tagging. VLAN tagging allows
481bf3b
multiple VLANs in a bridged network to share the same physical network link but
481bf3b
maintain isolation:
481bf3b
481bf3b
http://en.wikipedia.org/wiki/IEEE_802.1Q
481bf3b
481bf3b
This patch should fix this bugzilla:
481bf3b
https://bugzilla.redhat.com/show_bug.cgi?id=871563
481bf3b
---
481bf3b
 grub-core/kern/ieee1275/init.c   |  1 +
481bf3b
 grub-core/kern/ieee1275/openfw.c | 30 ++++++++++++++++++++++++++++
481bf3b
 grub-core/net/ethernet.c         | 42 +++++++++++++++++++++++++++++++++++++---
481bf3b
 include/grub/ieee1275/ieee1275.h |  1 +
481bf3b
 include/grub/net.h               |  2 ++
481bf3b
 5 files changed, 73 insertions(+), 3 deletions(-)
481bf3b
481bf3b
diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c
78a3d7d
index d5bd74d..8191f8c 100644
481bf3b
--- a/grub-core/kern/ieee1275/init.c
481bf3b
+++ b/grub-core/kern/ieee1275/init.c
78a3d7d
@@ -117,6 +117,7 @@ grub_machine_get_bootlocation (char **device, char **path)
481bf3b
       char *dev, *canon;
481bf3b
       char *ptr;
481bf3b
       dev = grub_ieee1275_get_aliasdevname (bootpath);
481bf3b
+      grub_ieee1275_parse_net_options (bootpath);
481bf3b
       canon = grub_ieee1275_canonicalise_devname (dev);
481bf3b
       ptr = canon + grub_strlen (canon) - 1;
481bf3b
       while (ptr > canon && (*ptr == ',' || *ptr == ':'))
481bf3b
diff --git a/grub-core/kern/ieee1275/openfw.c b/grub-core/kern/ieee1275/openfw.c
8c7f759
index 6db8b98..81276fa 100644
481bf3b
--- a/grub-core/kern/ieee1275/openfw.c
481bf3b
+++ b/grub-core/kern/ieee1275/openfw.c
481bf3b
@@ -23,6 +23,7 @@
481bf3b
 #include <grub/mm.h>
481bf3b
 #include <grub/ieee1275/ieee1275.h>
481bf3b
 #include <grub/net.h>
481bf3b
+#include <grub/env.h>
481bf3b
 
481bf3b
 enum grub_ieee1275_parse_type
481bf3b
 {
481bf3b
@@ -451,6 +452,35 @@ fail:
481bf3b
   return ret;
481bf3b
 }
481bf3b
 
481bf3b
+int
481bf3b
+grub_ieee1275_parse_net_options (const char *path)
481bf3b
+{
481bf3b
+  char *comma;
481bf3b
+  char *args;
481bf3b
+  char *option = 0;
481bf3b
+
481bf3b
+  args = grub_ieee1275_get_devargs (path);
481bf3b
+  if (!args)
481bf3b
+    /* There is no option.  */
481bf3b
+    return -1;
481bf3b
+
481bf3b
+  do
481bf3b
+    {
481bf3b
+      comma = grub_strchr (args, ',');
481bf3b
+      if (! comma)
481bf3b
+        option = grub_strdup (args);
481bf3b
+      else
481bf3b
+        option = grub_strndup (args, (grub_size_t)(comma - args));
481bf3b
+      args = comma + 1;
481bf3b
+
481bf3b
+      if (! grub_strncmp(option, "vtag", 4))
481bf3b
+          grub_env_set ("vlan-tag", option + grub_strlen("vtag="));
481bf3b
+
481bf3b
+    } while (comma);
481bf3b
+
481bf3b
+  return 0;
481bf3b
+}
481bf3b
+
481bf3b
 char *
481bf3b
 grub_ieee1275_get_device_type (const char *path)
481bf3b
 {
481bf3b
diff --git a/grub-core/net/ethernet.c b/grub-core/net/ethernet.c
481bf3b
index c397b1b..faaca67 100644
481bf3b
--- a/grub-core/net/ethernet.c
481bf3b
+++ b/grub-core/net/ethernet.c
481bf3b
@@ -23,6 +23,7 @@
481bf3b
 #include <grub/net/arp.h>
481bf3b
 #include <grub/net/netbuff.h>
481bf3b
 #include <grub/net.h>
481bf3b
+#include <grub/env.h>
481bf3b
 #include <grub/time.h>
481bf3b
 #include <grub/net/arp.h>
481bf3b
 
481bf3b
@@ -56,10 +57,19 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
481bf3b
 {
481bf3b
   struct etherhdr *eth;
481bf3b
   grub_err_t err;
481bf3b
+  grub_uint32_t vlantag = 0;
481bf3b
+  grub_uint8_t etherhdr_size;
481bf3b
 
481bf3b
-  COMPILE_TIME_ASSERT (sizeof (*eth) < GRUB_NET_MAX_LINK_HEADER_SIZE);
481bf3b
+  etherhdr_size = sizeof (*eth);
481bf3b
+  COMPILE_TIME_ASSERT (sizeof (*eth) + 4 < GRUB_NET_MAX_LINK_HEADER_SIZE);
481bf3b
 
481bf3b
-  err = grub_netbuff_push (nb, sizeof (*eth));
481bf3b
+  const char *vlantag_text = grub_env_get ("vlan-tag");
481bf3b
+  if (vlantag_text != 0) {
481bf3b
+      etherhdr_size += 4;
481bf3b
+      vlantag = grub_strtoul (vlantag_text, 0, 16);
481bf3b
+  }
481bf3b
+
481bf3b
+  err = grub_netbuff_push (nb, etherhdr_size);
481bf3b
   if (err)
481bf3b
     return err;
481bf3b
   eth = (struct etherhdr *) nb->data;
481bf3b
@@ -76,6 +86,19 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
481bf3b
 	return err;
481bf3b
       inf->card->opened = 1;
481bf3b
     }
481bf3b
+
481bf3b
+  /* Check if a vlan-tag is needed. */
481bf3b
+  if (vlantag != 0)
481bf3b
+    {
481bf3b
+      /* Move eth type to the right */
481bf3b
+      grub_memcpy((char *) nb->data + etherhdr_size - 2,
481bf3b
+                  (char *) nb->data + etherhdr_size - 6, 2);
481bf3b
+
481bf3b
+      /* Add the tag in the middle */
481bf3b
+      grub_memcpy((char *) nb->data + etherhdr_size - 6,
481bf3b
+                  &vlantag, 4);
481bf3b
+    }
481bf3b
+
481bf3b
   return inf->card->driver->send (inf->card, nb);
481bf3b
 }
481bf3b
 
481bf3b
@@ -90,10 +113,23 @@ grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
481bf3b
   grub_net_link_level_address_t hwaddress;
481bf3b
   grub_net_link_level_address_t src_hwaddress;
481bf3b
   grub_err_t err;
481bf3b
+  grub_uint8_t etherhdr_size = sizeof (*eth);
481bf3b
+
481bf3b
+  grub_uint16_t vlantag_identifier = 0;
481bf3b
+  grub_memcpy (&vlantag_identifier, nb->data + etherhdr_size - 2, 2);
481bf3b
+
481bf3b
+  /* Check if a vlan-tag is present. */
481bf3b
+  if (vlantag_identifier == VLANTAG_IDENTIFIER)
481bf3b
+    {
481bf3b
+      etherhdr_size += 4;
481bf3b
+      /* Move eth type to the original position */
481bf3b
+      grub_memcpy((char *) nb->data + etherhdr_size - 6,
481bf3b
+                  (char *) nb->data + etherhdr_size - 2, 2);
481bf3b
+    }
481bf3b
 
481bf3b
   eth = (struct etherhdr *) nb->data;
481bf3b
   type = grub_be_to_cpu16 (eth->type);
481bf3b
-  err = grub_netbuff_pull (nb, sizeof (*eth));
481bf3b
+  err = grub_netbuff_pull (nb, etherhdr_size);
481bf3b
   if (err)
481bf3b
     return err;
481bf3b
 
481bf3b
diff --git a/include/grub/ieee1275/ieee1275.h b/include/grub/ieee1275/ieee1275.h
78a3d7d
index 9f26c69..6a21f5d 100644
481bf3b
--- a/include/grub/ieee1275/ieee1275.h
481bf3b
+++ b/include/grub/ieee1275/ieee1275.h
78a3d7d
@@ -236,6 +236,7 @@ void EXPORT_FUNC(grub_ieee1275_children_first) (const char *devpath,
481bf3b
 						struct grub_ieee1275_devalias *alias);
481bf3b
 int EXPORT_FUNC(grub_ieee1275_cas_reboot) (char *script);
481bf3b
 int EXPORT_FUNC(grub_ieee1275_set_boot_last_label) (const char *text);
481bf3b
+int EXPORT_FUNC(grub_ieee1275_parse_net_options) (const char *path);
481bf3b
 
481bf3b
 #define FOR_IEEE1275_DEVALIASES(alias) for (grub_ieee1275_devalias_init_iterator (&(alias)); grub_ieee1275_devalias_next (&(alias));)
481bf3b
 
481bf3b
diff --git a/include/grub/net.h b/include/grub/net.h
78a3d7d
index 538baa3..a799e6b 100644
481bf3b
--- a/include/grub/net.h
481bf3b
+++ b/include/grub/net.h
78a3d7d
@@ -538,4 +538,6 @@ extern char *grub_net_default_server;
481bf3b
 #define GRUB_NET_INTERVAL 400
78a3d7d
 #define GRUB_NET_INTERVAL_ADDITION 20
481bf3b
 
481bf3b
+#define VLANTAG_IDENTIFIER 0x8100
481bf3b
+
481bf3b
 #endif /* ! GRUB_NET_HEADER */
481bf3b
-- 
37b39b7
1.9.3
481bf3b