Removed trailing whitespaces from .h and .c files using the
[obnox/wireshark/wip.git] / packet-gre.c
index fc79ce9405e8a5abf99ee75e6dd9cc7b19b21559..b82aa03ff447fa36b0ca35e9725c9fe501f0f584 100644 (file)
@@ -2,12 +2,11 @@
  * Routines for the Generic Routing Encapsulation (GRE) protocol
  * Brad Robel-Forrest <brad.robel-forrest@watchguard.com>
  *
  * Routines for the Generic Routing Encapsulation (GRE) protocol
  * Brad Robel-Forrest <brad.robel-forrest@watchguard.com>
  *
- * $Id: packet-gre.c,v 1.37 2001/01/09 09:59:28 guy Exp $
+ * $Id: packet-gre.c,v 1.51 2002/08/02 23:35:49 jmayer Exp $
  *
  * Ethereal - Network traffic analyzer
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
  * 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
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 # include "config.h"
 #endif
 
 # 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 <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 "packet-wccp.h"
 #include "in_cksum.h"
+#include "etypes.h"
+#include "greproto.h"
+#include "ipproto.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;
 
 static int proto_gre = -1;
 static int hf_gre_proto = -1;
@@ -49,6 +50,9 @@ static gint ett_gre = -1;
 static gint ett_gre_flags = -1;
 static gint ett_gre_wccp2_redirect_header = -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
 /* bit positions for flags in header */
 #define GH_B_C         0x8000
 #define GH_B_R         0x4000
@@ -61,29 +65,19 @@ static gint ett_gre_wccp2_redirect_header = -1;
 #define GH_R_FLAGS     0x00F8
 #define GH_B_VER       0x0007
 
 #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[] = {
 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" },
+       { 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 ipx_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)
 {
 static void
 dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
@@ -99,18 +93,14 @@ dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
   guint8       sre_length;
   tvbuff_t     *next_tvb;
 
   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));
 
   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)"));
   }
 
                 val_to_str(type, typevals, "0x%04X (unknown)"));
   }
 
@@ -122,7 +112,7 @@ dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
     len += 4;
   switch (type) {
 
     len += 4;
   switch (type) {
 
-  case GRE_PPP:
+  case ETHERTYPE_PPP:
     if (flags_and_ver & GH_P_A)
       len += 4;
     is_ppp = TRUE;
     if (flags_and_ver & GH_P_A)
       len += 4;
     is_ppp = TRUE;
@@ -257,32 +247,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:
-      call_dissector(ipx_handle, 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
 }
 
 static void
@@ -367,7 +353,7 @@ proto_register_gre(void)
        static hf_register_info hf[] = {
                { &hf_gre_proto,
                        { "Protocol Type", "gre.proto", FT_UINT16, BASE_HEX, VALS(typevals), 0x0,
        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[] = {
                },
        };
        static gint *ett[] = {
@@ -380,18 +366,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));
            "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)
 {
 }
 
 void
 proto_reg_handoff_gre(void)
 {
-       dissector_add("ip.proto", IP_PROTO_GRE, dissect_gre, proto_gre);
-
-       /*
-        * Get handles for the IP, IPX, PPP, and Frame Relay dissectors.
-        */
-       ip_handle = find_dissector("ip");
-       ipx_handle = find_dissector("ipx");
-       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");
 }
 }