From Lars Ruoff (who also contributed the previous change; my apologies
[obnox/wireshark/wip.git] / packet-gre.c
index 95ecca0ddb1918597032c2534e34cd135aad3797..1b15b48a38de3e9529c39f0e504ad7217cfa078b 100644 (file)
@@ -2,23 +2,22 @@
  * Routines for the Generic Routing Encapsulation (GRE) protocol
  * Brad Robel-Forrest <brad.robel-forrest@watchguard.com>
  *
- * $Id: packet-gre.c,v 1.35 2001/01/07 22:35:21 guy Exp $
+ * $Id: packet-gre.c,v 1.54 2003/06/10 05:38:52 guy Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
  *
- * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 # include "config.h"
 #endif
 
-#ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-
-#ifdef HAVE_NETINET_IN_H
-#include <netinet/in.h>
-#endif
 #include <glib.h>
-#include "packet.h"
-#include "packet-ip.h"
-#include "packet-ipx.h"
+#include <epan/packet.h>
 #include "packet-wccp.h"
 #include "in_cksum.h"
+#include "etypes.h"
+#include "greproto.h"
+#include "ipproto.h"
+#include "llcsaps.h"
+
+/*
+ * See RFC 1701 "Generic Routing Encapsulation (GRE)", RFC 1702
+ * "Generic Routing Encapsulation over IPv4 networks", RFC 2637
+ * "Point-to-Point Tunneling Protocol (PPTP)", RFC 2784 "Generic
+ * Routing Encapsulation (GRE)", and RFC 2890 "Key and Sequence
+ * Number Extensions to GRE".
+ */
 
 static int proto_gre = -1;
 static int hf_gre_proto = -1;
@@ -49,6 +51,9 @@ static gint ett_gre = -1;
 static gint ett_gre_flags = -1;
 static gint ett_gre_wccp2_redirect_header = -1;
 
+static dissector_table_t gre_dissector_table;
+static dissector_handle_t data_handle;
+
 /* bit positions for flags in header */
 #define GH_B_C         0x8000
 #define GH_B_R         0x4000
@@ -61,28 +66,20 @@ static gint ett_gre_wccp2_redirect_header = -1;
 #define GH_R_FLAGS     0x00F8
 #define GH_B_VER       0x0007
 
-#define GRE_PPP                0x880B
-#define        GRE_IP          0x0800
-#define GRE_WCCP       0x883E
-#define GRE_IPX                0x8137
-#define GRE_FR         0x6559
-
 static void add_flags_and_ver(proto_tree *, guint16, tvbuff_t *, int, int);
 static void dissect_gre_wccp2_redirect_header(tvbuff_t *, int, proto_tree *);
 
 static const value_string typevals[] = {
-       { GRE_PPP,  "PPP" },
-       { GRE_IP,   "IP" },
-       { GRE_WCCP, "WCCP"},
-       { GRE_IPX,  "IPX"},
-        { GRE_FR,   "FR"},
-       { 0,        NULL  }
+       { ETHERTYPE_PPP,       "PPP" },
+       { ETHERTYPE_IP,        "IP" },
+       { SAP_OSINL5,          "OSI"},
+       { GRE_WCCP,            "WCCP"},
+       { ETHERTYPE_IPX,       "IPX"},
+       { ETHERTYPE_ETHBRIDGE, "Transparent Ethernet bridging" },
+       { GRE_FR,              "Frame Relay"},
+       { 0,                   NULL }
 };
 
-static dissector_handle_t ip_handle;
-static dissector_handle_t ppp_handle;
-static dissector_handle_t fr_handle;
-
 static void
 dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
@@ -98,18 +95,14 @@ dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
   guint8       sre_length;
   tvbuff_t     *next_tvb;
 
-  CHECK_DISPLAY_AS_DATA(proto_gre, tvb, pinfo, tree);
-
-  pinfo->current_proto = "GRE";
-
   flags_and_ver = tvb_get_ntohs(tvb, offset);
   type = tvb_get_ntohs(tvb, offset + sizeof(flags_and_ver));
 
-  if (check_col(pinfo->fd, COL_PROTOCOL))
-    col_set_str(pinfo->fd, COL_PROTOCOL, "GRE");
+  if (check_col(pinfo->cinfo, COL_PROTOCOL))
+    col_set_str(pinfo->cinfo, COL_PROTOCOL, "GRE");
 
-  if (check_col(pinfo->fd, COL_INFO)) {
-    col_add_fstr(pinfo->fd, COL_INFO, "Encapsulated %s",
+  if (check_col(pinfo->cinfo, COL_INFO)) {
+    col_add_fstr(pinfo->cinfo, COL_INFO, "Encapsulated %s",
                 val_to_str(type, typevals, "0x%04X (unknown)"));
   }
 
@@ -121,7 +114,7 @@ dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
     len += 4;
   switch (type) {
 
-  case GRE_PPP:
+  case ETHERTYPE_PPP:
     if (flags_and_ver & GH_P_A)
       len += 4;
     is_ppp = TRUE;
@@ -214,7 +207,7 @@ dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
       offset += 4;
     }
   }
-    
+
   if (flags_and_ver & GH_B_S) {
     if (tree) {
       proto_tree_add_text(gre_tree, tvb, offset, 4,
@@ -256,32 +249,28 @@ dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
     }
   }
 
-  next_tvb = tvb_new_subset(tvb, offset, -1, -1);
-  switch (type) {
-    case GRE_PPP:
-      call_dissector(ppp_handle, next_tvb, pinfo, tree);
-      break;
-    case GRE_IP:
-      call_dissector(ip_handle, next_tvb, pinfo, tree);
-      break;
-    case GRE_WCCP:
-      if (is_wccp2) {
-       if (tree)
-          dissect_gre_wccp2_redirect_header(tvb, offset, gre_tree);
-        offset += 4;
-      }
-      call_dissector(ip_handle, next_tvb, pinfo, tree);
-      break;
-    case GRE_IPX:
-      dissect_ipx(next_tvb, pinfo, tree);
-      break;
-    case GRE_FR:
-      call_dissector(fr_handle, next_tvb, pinfo, tree);
-      break;        
-    default:
-      dissect_data(next_tvb, 0, pinfo, gre_tree);
-      break;
+  if (type == GRE_WCCP) {
+    if (is_wccp2) {
+      if (tree)
+        dissect_gre_wccp2_redirect_header(tvb, offset, gre_tree);
+      offset += 4;
+    }
   }
+
+  /* If the S bit is not set, this packet might not have a payload, so
+     check whether there's any data left, first.
+
+     XXX - the S bit isn't in RFC 2784, which deprecates that bit
+     and some other bits in RFC 1701 and says that they should be
+     zero for RFC 2784-compliant GRE; as such, the absence of the
+     S bit doesn't necessarily mean there's no payload.  */
+  if (!(flags_and_ver & GH_B_S)) {
+    if (tvb_reported_length_remaining(tvb, offset) <= 0)
+      return;  /* no payload */
+  }
+  next_tvb = tvb_new_subset(tvb, offset, -1, -1);
+  if (!dissector_try_port(gre_dissector_table, type, next_tvb, pinfo, tree))
+    call_dissector(data_handle,next_tvb, pinfo, gre_tree);
 }
 
 static void
@@ -291,11 +280,11 @@ add_flags_and_ver(proto_tree *tree, guint16 flags_and_ver, tvbuff_t *tvb,
   proto_item * ti;
   proto_tree * fv_tree;
   int          nbits = sizeof(flags_and_ver) * 8;
-  
-  ti = proto_tree_add_text(tree, tvb, offset, 2, 
+
+  ti = proto_tree_add_text(tree, tvb, offset, 2,
                           "Flags and version: %#04x", flags_and_ver);
   fv_tree = proto_item_add_subtree(ti, ett_gre_flags);
-  
+
   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
                      decode_boolean_bitfield(flags_and_ver, GH_B_C, nbits,
                                              "Checksum", "No checksum"));
@@ -339,7 +328,7 @@ dissect_gre_wccp2_redirect_header(tvbuff_t *tvb, int offset, proto_tree *tree)
   proto_item * ti;
   proto_tree * rh_tree;
   guint8       rh_flags;
-  
+
   ti = proto_tree_add_text(tree, tvb, offset, 4, "Redirect header");
   rh_tree = proto_item_add_subtree(ti, ett_gre_wccp2_redirect_header);
 
@@ -359,14 +348,14 @@ dissect_gre_wccp2_redirect_header(tvbuff_t *tvb, int offset, proto_tree *tree)
   proto_tree_add_text(rh_tree, tvb, offset + 3, 1, "Primary bucket index: %u",
                        tvb_get_guint8(tvb, offset + 3));
 }
+
 void
 proto_register_gre(void)
 {
        static hf_register_info hf[] = {
                { &hf_gre_proto,
                        { "Protocol Type", "gre.proto", FT_UINT16, BASE_HEX, VALS(typevals), 0x0,
-                               "The protocol that is GRE encapsulated"}
+                               "The protocol that is GRE encapsulated", HFILL }
                },
        };
        static gint *ett[] = {
@@ -379,17 +368,18 @@ proto_register_gre(void)
            "GRE", "gre");
         proto_register_field_array(proto_gre, hf, array_length(hf));
        proto_register_subtree_array(ett, array_length(ett));
+
+       /* subdissector code */
+       gre_dissector_table = register_dissector_table("gre.proto",
+           "GRE protocol type", FT_UINT16, BASE_HEX);
 }
 
 void
 proto_reg_handoff_gre(void)
 {
-       dissector_add("ip.proto", IP_PROTO_GRE, dissect_gre);
-
-       /*
-        * Get handles for the IP, PPP, and Frame Relay dissectors.
-        */
-       ip_handle = find_dissector("ip");
-       ppp_handle = find_dissector("ppp");
-       fr_handle = find_dissector("fr");
+       dissector_handle_t gre_handle;
+
+       gre_handle = create_dissector_handle(dissect_gre, proto_gre);
+       dissector_add("ip.proto", IP_PROTO_GRE, gre_handle);
+       data_handle = find_dissector("data");
 }