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.56 2004/05/15 19:54:10 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include <epan/packet.h>
32 #include "packet-wccp.h"
40 * See RFC 1701 "Generic Routing Encapsulation (GRE)", RFC 1702
41 * "Generic Routing Encapsulation over IPv4 networks", RFC 2637
42 * "Point-to-Point Tunneling Protocol (PPTP)", RFC 2784 "Generic
43 * Routing Encapsulation (GRE)", RFC 2890 "Key and Sequence
44 * Number Extensions to GRE" and draft-ietf-mpls-in-ip-or-gre-07.txt
45 * "Encapsulating MPLS in IP or Generic Routing Encapsulation (GRE)".
48 static int proto_gre = -1;
49 static int hf_gre_proto = -1;
51 static gint ett_gre = -1;
52 static gint ett_gre_flags = -1;
53 static gint ett_gre_wccp2_redirect_header = -1;
55 static dissector_table_t gre_dissector_table;
56 static dissector_handle_t data_handle;
58 /* bit positions for flags in header */
64 #define GH_B_RECUR 0x0700
65 #define GH_P_A 0x0080 /* only in special PPTPized GRE header */
66 #define GH_P_FLAGS 0x0078 /* only in special PPTPized GRE header */
67 #define GH_R_FLAGS 0x00F8
68 #define GH_B_VER 0x0007
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[] = {
74 { ETHERTYPE_PPP, "PPP" },
75 { ETHERTYPE_IP, "IP" },
78 { ETHERTYPE_IPX, "IPX"},
79 { ETHERTYPE_ETHBRIDGE, "Transparent Ethernet bridging" },
80 { GRE_FR, "Frame Relay"},
81 { ETHERTYPE_IPv6, "IPv6" },
82 { ETHERTYPE_MPLS, "MPLS label switched packet" },
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 flags_and_ver = tvb_get_ntohs(tvb, offset);
102 type = tvb_get_ntohs(tvb, offset + sizeof(flags_and_ver));
104 if (check_col(pinfo->cinfo, COL_PROTOCOL))
105 col_set_str(pinfo->cinfo, COL_PROTOCOL, "GRE");
107 if (check_col(pinfo->cinfo, COL_INFO)) {
108 col_add_fstr(pinfo->cinfo, COL_INFO, "Encapsulated %s",
109 val_to_str(type, typevals, "0x%04X (unknown)"));
112 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R)
114 if (flags_and_ver & GH_B_K)
116 if (flags_and_ver & GH_B_S)
121 if (flags_and_ver & GH_P_A)
127 /* WCCP2 puts an extra 4 octets into the header, but uses the same
128 encapsulation type; if it looks as if the first octet of the packet
129 isn't the beginning of an IPv4 header, assume it's WCCP2. */
130 if ((tvb_get_guint8(tvb, offset + sizeof(flags_and_ver) + sizeof(type)) & 0xF0) != 0x40) {
138 ti = proto_tree_add_protocol_format(tree, proto_gre, tvb, offset, len,
139 "Generic Routing Encapsulation (%s)",
140 val_to_str(type, typevals, "0x%04X - unknown"));
141 gre_tree = proto_item_add_subtree(ti, ett_gre);
142 add_flags_and_ver(gre_tree, flags_and_ver, tvb, offset, is_ppp);
144 offset += sizeof(flags_and_ver);
147 proto_tree_add_uint(gre_tree, hf_gre_proto, tvb, offset, sizeof(type), type);
149 offset += sizeof(type);
151 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
153 guint length, reported_length;
155 guint16 cksum, computed_cksum;
157 cksum = tvb_get_ntohs(tvb, offset);
158 length = tvb_length(tvb);
159 reported_length = tvb_reported_length(tvb);
160 if ((flags_and_ver & GH_B_C) && !pinfo->fragmented
161 && length >= reported_length) {
162 /* The Checksum Present bit is set, and the packet isn't part of a
163 fragmented datagram and isn't truncated, so we can checksum it. */
165 cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, reported_length);
166 cksum_vec[0].len = reported_length;
167 computed_cksum = in_cksum(cksum_vec, 1);
168 if (computed_cksum == 0) {
169 proto_tree_add_text(gre_tree, tvb, offset, 2,
170 "Checksum: 0x%04x (correct)", cksum);
172 proto_tree_add_text(gre_tree, tvb, offset, 2,
173 "Checksum: 0x%04x (incorrect, should be 0x%04x)",
174 cksum, in_cksum_shouldbe(cksum, computed_cksum));
177 proto_tree_add_text(gre_tree, tvb, offset, 2,
178 "Checksum: 0x%04x", cksum);
184 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
186 proto_tree_add_text(gre_tree, tvb, offset, 2,
187 "Offset: %u", tvb_get_ntohs(tvb, offset));
192 if (flags_and_ver & GH_B_K) {
195 proto_tree_add_text(gre_tree, tvb, offset, 2,
196 "Payload length: %u", tvb_get_ntohs(tvb, offset));
200 proto_tree_add_text(gre_tree, tvb, offset, 2,
201 "Call ID: %u", tvb_get_ntohs(tvb, offset));
207 proto_tree_add_text(gre_tree, tvb, offset, 4,
208 "Key: %u", tvb_get_ntohl(tvb, offset));
214 if (flags_and_ver & GH_B_S) {
216 proto_tree_add_text(gre_tree, tvb, offset, 4,
217 "Sequence number: %u", tvb_get_ntohl(tvb, offset));
222 if (is_ppp && flags_and_ver & GH_P_A) {
224 proto_tree_add_text(gre_tree, tvb, offset, 4,
225 "Acknowledgement number: %u", tvb_get_ntohl(tvb, offset));
230 if (flags_and_ver & GH_B_R) {
232 sre_af = tvb_get_ntohs(tvb, offset);
234 proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint16),
235 "Address family: %u", sre_af);
237 offset += sizeof(guint16);
239 proto_tree_add_text(gre_tree, tvb, offset, 1,
240 "SRE offset: %u", tvb_get_guint8(tvb, offset));
242 offset += sizeof(guint8);
243 sre_length = tvb_get_guint8(tvb, offset);
245 proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint8),
246 "SRE length: %u", sre_length);
248 offset += sizeof(guint8);
249 if (sre_af == 0 && sre_length == 0)
251 offset += sre_length;
255 if (type == GRE_WCCP) {
258 dissect_gre_wccp2_redirect_header(tvb, offset, gre_tree);
263 /* If the S bit is not set, this packet might not have a payload, so
264 check whether there's any data left, first.
266 XXX - the S bit isn't in RFC 2784, which deprecates that bit
267 and some other bits in RFC 1701 and says that they should be
268 zero for RFC 2784-compliant GRE; as such, the absence of the
269 S bit doesn't necessarily mean there's no payload. */
270 if (!(flags_and_ver & GH_B_S)) {
271 if (tvb_reported_length_remaining(tvb, offset) <= 0)
272 return; /* no payload */
274 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
275 if (!dissector_try_port(gre_dissector_table, type, next_tvb, pinfo, tree))
276 call_dissector(data_handle,next_tvb, pinfo, gre_tree);
280 add_flags_and_ver(proto_tree *tree, guint16 flags_and_ver, tvbuff_t *tvb,
281 int offset, int is_ppp)
284 proto_tree * fv_tree;
285 int nbits = sizeof(flags_and_ver) * 8;
287 ti = proto_tree_add_text(tree, tvb, offset, 2,
288 "Flags and version: %#04x", flags_and_ver);
289 fv_tree = proto_item_add_subtree(ti, ett_gre_flags);
291 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
292 decode_boolean_bitfield(flags_and_ver, GH_B_C, nbits,
293 "Checksum", "No checksum"));
294 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
295 decode_boolean_bitfield(flags_and_ver, GH_B_R, nbits,
296 "Routing", "No routing"));
297 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
298 decode_boolean_bitfield(flags_and_ver, GH_B_K, nbits,
300 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
301 decode_boolean_bitfield(flags_and_ver, GH_B_S, nbits,
302 "Sequence number", "No sequence number"));
303 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
304 decode_boolean_bitfield(flags_and_ver, GH_B_s, nbits,
305 "Strict source route", "No strict source route"));
306 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
307 decode_numeric_bitfield(flags_and_ver, GH_B_RECUR, nbits,
308 "Recursion control: %u"));
310 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
311 decode_boolean_bitfield(flags_and_ver, GH_P_A, nbits,
312 "Acknowledgment number", "No acknowledgment number"));
313 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
314 decode_numeric_bitfield(flags_and_ver, GH_P_FLAGS, nbits,
318 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
319 decode_numeric_bitfield(flags_and_ver, GH_R_FLAGS, nbits,
323 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
324 decode_numeric_bitfield(flags_and_ver, GH_B_VER, nbits,
329 dissect_gre_wccp2_redirect_header(tvbuff_t *tvb, int offset, proto_tree *tree)
332 proto_tree * rh_tree;
335 ti = proto_tree_add_text(tree, tvb, offset, 4, "Redirect header");
336 rh_tree = proto_item_add_subtree(ti, ett_gre_wccp2_redirect_header);
338 rh_flags = tvb_get_guint8(tvb, offset);
339 proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
340 decode_boolean_bitfield(rh_flags, 0x80, 8,
341 "Dynamic service", "Well-known service"));
342 proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
343 decode_boolean_bitfield(rh_flags, 0x40, 8,
344 "Alternative bucket used", "Alternative bucket not used"));
346 proto_tree_add_text(rh_tree, tvb, offset + 1, 1, "Service ID: %s",
347 val_to_str(tvb_get_guint8(tvb, offset + 1), service_id_vals, "Unknown (0x%02X)"));
349 proto_tree_add_text(rh_tree, tvb, offset + 2, 1, "Alternative bucket index: %u",
350 tvb_get_guint8(tvb, offset + 2));
351 proto_tree_add_text(rh_tree, tvb, offset + 3, 1, "Primary bucket index: %u",
352 tvb_get_guint8(tvb, offset + 3));
356 proto_register_gre(void)
358 static hf_register_info hf[] = {
360 { "Protocol Type", "gre.proto", FT_UINT16, BASE_HEX, VALS(typevals), 0x0,
361 "The protocol that is GRE encapsulated", HFILL }
364 static gint *ett[] = {
367 &ett_gre_wccp2_redirect_header,
370 proto_gre = proto_register_protocol("Generic Routing Encapsulation",
372 proto_register_field_array(proto_gre, hf, array_length(hf));
373 proto_register_subtree_array(ett, array_length(ett));
375 /* subdissector code */
376 gre_dissector_table = register_dissector_table("gre.proto",
377 "GRE protocol type", FT_UINT16, BASE_HEX);
381 proto_reg_handoff_gre(void)
383 dissector_handle_t gre_handle;
385 gre_handle = create_dissector_handle(dissect_gre, proto_gre);
386 dissector_add("ip.proto", IP_PROTO_GRE, gre_handle);
387 data_handle = find_dissector("data");