2 * Routines for the Generic Routing Encapsulation (GRE) protocol
3 * Brad Robel-Forrest <brad.robel-forrest@watchguard.com>
5 * $Id: packet-gre.c,v 1.34 2001/01/07 22:08:31 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@zing.org>
9 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
40 #include "packet-ip.h"
41 #include "packet-ipx.h"
42 #include "packet-wccp.h"
45 static int proto_gre = -1;
46 static int hf_gre_proto = -1;
48 static gint ett_gre = -1;
49 static gint ett_gre_flags = -1;
50 static gint ett_gre_wccp2_redirect_header = -1;
52 /* bit positions for flags in header */
58 #define GH_B_RECUR 0x0700
59 #define GH_P_A 0x0080 /* only in special PPTPized GRE header */
60 #define GH_P_FLAGS 0x0078 /* only in special PPTPized GRE header */
61 #define GH_R_FLAGS 0x00F8
62 #define GH_B_VER 0x0007
64 #define GRE_PPP 0x880B
66 #define GRE_WCCP 0x883E
67 #define GRE_IPX 0x8137
70 static void add_flags_and_ver(proto_tree *, guint16, tvbuff_t *, int, int);
71 static void dissect_gre_wccp2_redirect_header(tvbuff_t *, int, proto_tree *);
73 static const value_string typevals[] = {
82 static dissector_handle_t ip_handle;
83 static dissector_handle_t ppp_handle;
84 static dissector_handle_t fr_handle;
87 dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
90 guint16 flags_and_ver;
92 gboolean is_ppp = FALSE;
93 gboolean is_wccp2 = FALSE;
96 proto_tree *gre_tree = NULL;
101 CHECK_DISPLAY_AS_DATA(proto_gre, tvb, pinfo, tree);
103 pinfo->current_proto = "GRE";
105 flags_and_ver = tvb_get_ntohs(tvb, offset);
106 type = tvb_get_ntohs(tvb, offset + sizeof(flags_and_ver));
108 if (check_col(pinfo->fd, COL_PROTOCOL))
109 col_set_str(pinfo->fd, COL_PROTOCOL, "GRE");
111 if (check_col(pinfo->fd, COL_INFO)) {
112 col_add_fstr(pinfo->fd, COL_INFO, "Encapsulated %s",
113 val_to_str(type, typevals, "0x%04X (unknown)"));
116 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R)
118 if (flags_and_ver & GH_B_K)
120 if (flags_and_ver & GH_B_S)
125 if (flags_and_ver & GH_P_A)
131 /* WCCP2 puts an extra 4 octets into the header, but uses the same
132 encapsulation type; if it looks as if the first octet of the packet
133 isn't the beginning of an IPv4 header, assume it's WCCP2. */
134 if ((tvb_get_guint8(tvb, offset + sizeof(flags_and_ver) + sizeof(type)) & 0xF0) != 0x40) {
142 ti = proto_tree_add_protocol_format(tree, proto_gre, tvb, offset, len,
143 "Generic Routing Encapsulation (%s)",
144 val_to_str(type, typevals, "0x%04X - unknown"));
145 gre_tree = proto_item_add_subtree(ti, ett_gre);
146 add_flags_and_ver(gre_tree, flags_and_ver, tvb, offset, is_ppp);
148 offset += sizeof(flags_and_ver);
151 proto_tree_add_uint(gre_tree, hf_gre_proto, tvb, offset, sizeof(type), type);
153 offset += sizeof(type);
155 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
157 guint length, reported_length;
159 guint16 cksum, computed_cksum;
161 cksum = tvb_get_ntohs(tvb, offset);
162 length = tvb_length(tvb);
163 reported_length = tvb_reported_length(tvb);
164 if ((flags_and_ver & GH_B_C) && !pinfo->fragmented
165 && length >= reported_length) {
166 /* The Checksum Present bit is set, and the packet isn't part of a
167 fragmented datagram and isn't truncated, so we can checksum it. */
169 cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, reported_length);
170 cksum_vec[0].len = reported_length;
171 computed_cksum = in_cksum(cksum_vec, 1);
172 if (computed_cksum == 0) {
173 proto_tree_add_text(gre_tree, tvb, offset, 2,
174 "Checksum: 0x%04x (correct)", cksum);
176 proto_tree_add_text(gre_tree, tvb, offset, 2,
177 "Checksum: 0x%04x (incorrect, should be 0x%04x)",
178 cksum, in_cksum_shouldbe(cksum, computed_cksum));
181 proto_tree_add_text(gre_tree, tvb, offset, 2,
182 "Checksum: 0x%04x", cksum);
188 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
190 proto_tree_add_text(gre_tree, tvb, offset, 2,
191 "Offset: %u", tvb_get_ntohs(tvb, offset));
196 if (flags_and_ver & GH_B_K) {
199 proto_tree_add_text(gre_tree, tvb, offset, 2,
200 "Payload length: %u", tvb_get_ntohs(tvb, offset));
204 proto_tree_add_text(gre_tree, tvb, offset, 2,
205 "Call ID: %u", tvb_get_ntohs(tvb, offset));
211 proto_tree_add_text(gre_tree, tvb, offset, 4,
212 "Key: %u", tvb_get_ntohl(tvb, offset));
218 if (flags_and_ver & GH_B_S) {
220 proto_tree_add_text(gre_tree, tvb, offset, 4,
221 "Sequence number: %u", tvb_get_ntohl(tvb, offset));
226 if (is_ppp && flags_and_ver & GH_P_A) {
228 proto_tree_add_text(gre_tree, tvb, offset, 4,
229 "Acknowledgement number: %u", tvb_get_ntohl(tvb, offset));
234 if (flags_and_ver & GH_B_R) {
236 sre_af = tvb_get_ntohs(tvb, offset);
238 proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint16),
239 "Address family: %u", sre_af);
241 offset += sizeof(guint16);
243 proto_tree_add_text(gre_tree, tvb, offset, 1,
244 "SRE offset: %u", tvb_get_guint8(tvb, offset));
246 offset += sizeof(guint8);
247 sre_length = tvb_get_guint8(tvb, offset);
249 proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint8),
250 "SRE length: %u", sre_length);
252 offset += sizeof(guint8);
253 if (sre_af == 0 && sre_length == 0)
255 offset += sre_length;
259 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
262 call_dissector(ppp_handle, next_tvb, pinfo, tree);
265 call_dissector(ip_handle, next_tvb, pinfo, tree);
270 dissect_gre_wccp2_redirect_header(tvb, offset, gre_tree);
273 call_dissector(ip_handle, next_tvb, pinfo, tree);
276 dissect_ipx(next_tvb, pinfo, tree);
279 call_dissector(fr_handle, next_tvb, pinfo, tree);
282 dissect_data(next_tvb, 0, pinfo, gre_tree);
288 add_flags_and_ver(proto_tree *tree, guint16 flags_and_ver, tvbuff_t *tvb,
289 int offset, int is_ppp)
292 proto_tree * fv_tree;
293 int nbits = sizeof(flags_and_ver) * 8;
295 ti = proto_tree_add_text(tree, tvb, offset, 2,
296 "Flags and version: %#04x", flags_and_ver);
297 fv_tree = proto_item_add_subtree(ti, ett_gre_flags);
299 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
300 decode_boolean_bitfield(flags_and_ver, GH_B_C, nbits,
301 "Checksum", "No checksum"));
302 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
303 decode_boolean_bitfield(flags_and_ver, GH_B_R, nbits,
304 "Routing", "No routing"));
305 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
306 decode_boolean_bitfield(flags_and_ver, GH_B_K, nbits,
308 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
309 decode_boolean_bitfield(flags_and_ver, GH_B_S, nbits,
310 "Sequence number", "No sequence number"));
311 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
312 decode_boolean_bitfield(flags_and_ver, GH_B_s, nbits,
313 "Strict source route", "No strict source route"));
314 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
315 decode_numeric_bitfield(flags_and_ver, GH_B_RECUR, nbits,
316 "Recursion control: %u"));
318 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
319 decode_boolean_bitfield(flags_and_ver, GH_P_A, nbits,
320 "Acknowledgment number", "No acknowledgment number"));
321 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
322 decode_numeric_bitfield(flags_and_ver, GH_P_FLAGS, nbits,
326 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
327 decode_numeric_bitfield(flags_and_ver, GH_R_FLAGS, nbits,
331 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
332 decode_numeric_bitfield(flags_and_ver, GH_B_VER, nbits,
337 dissect_gre_wccp2_redirect_header(tvbuff_t *tvb, int offset, proto_tree *tree)
340 proto_tree * rh_tree;
343 ti = proto_tree_add_text(tree, tvb, offset, 4, "Redirect header");
344 rh_tree = proto_item_add_subtree(ti, ett_gre_wccp2_redirect_header);
346 rh_flags = tvb_get_guint8(tvb, offset);
347 proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
348 decode_boolean_bitfield(rh_flags, 0x80, 8,
349 "Dynamic service", "Well-known service"));
350 proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
351 decode_boolean_bitfield(rh_flags, 0x40, 8,
352 "Alternative bucket used", "Alternative bucket not used"));
354 proto_tree_add_text(rh_tree, tvb, offset + 1, 1, "Service ID: %s",
355 val_to_str(tvb_get_guint8(tvb, offset + 1), service_id_vals, "Unknown (0x%02X)"));
357 proto_tree_add_text(rh_tree, tvb, offset + 2, 1, "Alternative bucket index: %u",
358 tvb_get_guint8(tvb, offset + 2));
359 proto_tree_add_text(rh_tree, tvb, offset + 3, 1, "Primary bucket index: %u",
360 tvb_get_guint8(tvb, offset + 3));
364 proto_register_gre(void)
366 static hf_register_info hf[] = {
368 { "Protocol Type", "gre.proto", FT_UINT16, BASE_HEX, VALS(typevals), 0x0,
369 "The protocol that is GRE encapsulated"}
372 static gint *ett[] = {
375 &ett_gre_wccp2_redirect_header,
378 proto_gre = proto_register_protocol("Generic Routing Encapsulation",
380 proto_register_field_array(proto_gre, hf, array_length(hf));
381 proto_register_subtree_array(ett, array_length(ett));
385 proto_reg_handoff_gre(void)
387 dissector_add("ip.proto", IP_PROTO_GRE, dissect_gre);
390 * Get handles for the IP, PPP, and Frame Relay dissectors.
392 ip_handle = find_dissector("ip");
393 ppp_handle = find_dissector("ppp");
394 fr_handle = find_dissector("fr");