2 * Routines for the Generic Routing Encapsulation (GRE) protocol
3 * Brad Robel-Forrest <brad.robel-forrest@watchguard.com>
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"
33 #include <epan/in_cksum.h>
34 #include <epan/etypes.h>
35 #include <epan/greproto.h>
36 #include <epan/ipproto.h>
37 #include <epan/llcsaps.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;
50 static int hf_gre_key = -1;
52 static gint ett_gre = -1;
53 static gint ett_gre_flags = -1;
54 static gint ett_gre_wccp2_redirect_header = -1;
56 static dissector_table_t gre_dissector_table;
57 static dissector_handle_t data_handle;
59 /* bit positions for flags in header */
65 #define GH_B_RECUR 0x0700
66 #define GH_P_A 0x0080 /* only in special PPTPized GRE header */
67 #define GH_P_FLAGS 0x0078 /* only in special PPTPized GRE header */
68 #define GH_R_FLAGS 0x00F8
69 #define GH_B_VER 0x0007
71 static void add_flags_and_ver(proto_tree *, guint16, tvbuff_t *, int, int);
72 static void dissect_gre_wccp2_redirect_header(tvbuff_t *, int, proto_tree *);
74 static const value_string typevals[] = {
75 { ETHERTYPE_PPP, "PPP" },
76 { ETHERTYPE_IP, "IP" },
80 { ETHERTYPE_IPX, "IPX"},
81 { ETHERTYPE_ETHBRIDGE, "Transparent Ethernet bridging" },
82 { ETHERTYPE_RAW_FR, "Frame Relay"},
83 { ETHERTYPE_IPv6, "IPv6" },
84 { ETHERTYPE_MPLS, "MPLS label switched packet" },
85 { ETHERTYPE_CDMA2000_A10_UBS,"CDMA2000 A10 Unstructured byte stream" },
90 dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
93 guint16 flags_and_ver;
95 gboolean is_ppp = FALSE;
96 gboolean is_wccp2 = FALSE;
99 proto_tree *gre_tree = NULL;
104 flags_and_ver = tvb_get_ntohs(tvb, offset);
105 type = tvb_get_ntohs(tvb, offset + sizeof(flags_and_ver));
107 if (check_col(pinfo->cinfo, COL_PROTOCOL))
108 col_set_str(pinfo->cinfo, COL_PROTOCOL, "GRE");
110 if (check_col(pinfo->cinfo, COL_INFO)) {
111 col_add_fstr(pinfo->cinfo, COL_INFO, "Encapsulated %s",
112 val_to_str(type, typevals, "0x%04X (unknown)"));
115 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R)
117 if (flags_and_ver & GH_B_K)
119 if (flags_and_ver & GH_B_S)
124 if (flags_and_ver & GH_P_A)
128 case ETHERTYPE_CDMA2000_A10_UBS:
129 if (flags_and_ver & GH_P_A)
135 /* WCCP2 puts an extra 4 octets into the header, but uses the same
136 encapsulation type; if it looks as if the first octet of the packet
137 isn't the beginning of an IPv4 header, assume it's WCCP2. */
138 if ((tvb_get_guint8(tvb, offset + sizeof(flags_and_ver) + sizeof(type)) & 0xF0) != 0x40) {
146 ti = proto_tree_add_protocol_format(tree, proto_gre, tvb, offset, len,
147 "Generic Routing Encapsulation (%s)",
148 val_to_str(type, typevals, "0x%04X - unknown"));
149 gre_tree = proto_item_add_subtree(ti, ett_gre);
150 add_flags_and_ver(gre_tree, flags_and_ver, tvb, offset, is_ppp);
152 offset += sizeof(flags_and_ver);
155 proto_tree_add_uint(gre_tree, hf_gre_proto, tvb, offset, sizeof(type), type);
157 offset += sizeof(type);
159 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
161 guint length, reported_length;
163 guint16 cksum, computed_cksum;
165 cksum = tvb_get_ntohs(tvb, offset);
166 length = tvb_length(tvb);
167 reported_length = tvb_reported_length(tvb);
168 if ((flags_and_ver & GH_B_C) && !pinfo->fragmented
169 && length >= reported_length) {
170 /* The Checksum Present bit is set, and the packet isn't part of a
171 fragmented datagram and isn't truncated, so we can checksum it. */
173 cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, reported_length);
174 cksum_vec[0].len = reported_length;
175 computed_cksum = in_cksum(cksum_vec, 1);
176 if (computed_cksum == 0) {
177 proto_tree_add_text(gre_tree, tvb, offset, 2,
178 "Checksum: 0x%04x [correct]", cksum);
180 proto_tree_add_text(gre_tree, tvb, offset, 2,
181 "Checksum: 0x%04x [incorrect, should be 0x%04x]",
182 cksum, in_cksum_shouldbe(cksum, computed_cksum));
185 proto_tree_add_text(gre_tree, tvb, offset, 2,
186 "Checksum: 0x%04x", cksum);
192 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
194 proto_tree_add_text(gre_tree, tvb, offset, 2,
195 "Offset: %u", tvb_get_ntohs(tvb, offset));
200 if (flags_and_ver & GH_B_K) {
201 if (is_ppp && type!=ETHERTYPE_CDMA2000_A10_UBS) {
203 proto_tree_add_text(gre_tree, tvb, offset, 2,
204 "Payload length: %u", tvb_get_ntohs(tvb, offset));
208 proto_tree_add_text(gre_tree, tvb, offset, 2,
209 "Call ID: %u", tvb_get_ntohs(tvb, offset));
215 proto_tree_add_item(gre_tree, hf_gre_key, tvb, offset, 4, FALSE);
220 if (flags_and_ver & GH_B_S) {
222 proto_tree_add_text(gre_tree, tvb, offset, 4,
223 "Sequence number: %u", tvb_get_ntohl(tvb, offset));
228 if (is_ppp && flags_and_ver & GH_P_A) {
230 proto_tree_add_text(gre_tree, tvb, offset, 4,
231 "Acknowledgement number: %u", tvb_get_ntohl(tvb, offset));
236 if (flags_and_ver & GH_B_R) {
238 sre_af = tvb_get_ntohs(tvb, offset);
240 proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint16),
241 "Address family: %u", sre_af);
243 offset += sizeof(guint16);
245 proto_tree_add_text(gre_tree, tvb, offset, 1,
246 "SRE offset: %u", tvb_get_guint8(tvb, offset));
248 offset += sizeof(guint8);
249 sre_length = tvb_get_guint8(tvb, offset);
251 proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint8),
252 "SRE length: %u", sre_length);
254 offset += sizeof(guint8);
255 if (sre_af == 0 && sre_length == 0)
257 offset += sre_length;
261 if (type == GRE_WCCP) {
264 dissect_gre_wccp2_redirect_header(tvb, offset, gre_tree);
269 /* If the S bit is not set, this packet might not have a payload, so
270 check whether there's any data left, first.
272 XXX - the S bit isn't in RFC 2784, which deprecates that bit
273 and some other bits in RFC 1701 and says that they should be
274 zero for RFC 2784-compliant GRE; as such, the absence of the
275 S bit doesn't necessarily mean there's no payload. */
276 if (!(flags_and_ver & GH_B_S)) {
277 if (tvb_reported_length_remaining(tvb, offset) <= 0)
278 return; /* no payload */
280 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
281 if (!dissector_try_port(gre_dissector_table, type, next_tvb, pinfo, tree))
282 call_dissector(data_handle,next_tvb, pinfo, gre_tree);
286 add_flags_and_ver(proto_tree *tree, guint16 flags_and_ver, tvbuff_t *tvb,
287 int offset, int is_ppp)
290 proto_tree * fv_tree;
291 int nbits = sizeof(flags_and_ver) * 8;
293 ti = proto_tree_add_text(tree, tvb, offset, 2,
294 "Flags and version: %#04x", flags_and_ver);
295 fv_tree = proto_item_add_subtree(ti, ett_gre_flags);
297 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
298 decode_boolean_bitfield(flags_and_ver, GH_B_C, nbits,
299 "Checksum", "No checksum"));
300 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
301 decode_boolean_bitfield(flags_and_ver, GH_B_R, nbits,
302 "Routing", "No routing"));
303 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
304 decode_boolean_bitfield(flags_and_ver, GH_B_K, nbits,
306 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
307 decode_boolean_bitfield(flags_and_ver, GH_B_S, nbits,
308 "Sequence number", "No sequence number"));
309 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
310 decode_boolean_bitfield(flags_and_ver, GH_B_s, nbits,
311 "Strict source route", "No strict source route"));
312 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
313 decode_numeric_bitfield(flags_and_ver, GH_B_RECUR, nbits,
314 "Recursion control: %u"));
316 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
317 decode_boolean_bitfield(flags_and_ver, GH_P_A, nbits,
318 "Acknowledgment number", "No acknowledgment number"));
319 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
320 decode_numeric_bitfield(flags_and_ver, GH_P_FLAGS, nbits,
324 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
325 decode_numeric_bitfield(flags_and_ver, GH_R_FLAGS, nbits,
329 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
330 decode_numeric_bitfield(flags_and_ver, GH_B_VER, nbits,
335 dissect_gre_wccp2_redirect_header(tvbuff_t *tvb, int offset, proto_tree *tree)
338 proto_tree * rh_tree;
341 ti = proto_tree_add_text(tree, tvb, offset, 4, "Redirect header");
342 rh_tree = proto_item_add_subtree(ti, ett_gre_wccp2_redirect_header);
344 rh_flags = tvb_get_guint8(tvb, offset);
345 proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
346 decode_boolean_bitfield(rh_flags, 0x80, 8,
347 "Dynamic service", "Well-known service"));
348 proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
349 decode_boolean_bitfield(rh_flags, 0x40, 8,
350 "Alternative bucket used", "Alternative bucket not used"));
352 proto_tree_add_text(rh_tree, tvb, offset + 1, 1, "Service ID: %s",
353 val_to_str(tvb_get_guint8(tvb, offset + 1), service_id_vals, "Unknown (0x%02X)"));
355 proto_tree_add_text(rh_tree, tvb, offset + 2, 1, "Alternative bucket index: %u",
356 tvb_get_guint8(tvb, offset + 2));
357 proto_tree_add_text(rh_tree, tvb, offset + 3, 1, "Primary bucket index: %u",
358 tvb_get_guint8(tvb, offset + 3));
362 proto_register_gre(void)
364 static hf_register_info hf[] = {
366 { "Protocol Type", "gre.proto", FT_UINT16, BASE_HEX, VALS(typevals), 0x0,
367 "The protocol that is GRE encapsulated", HFILL }
370 { "GRE Key", "gre.key", FT_UINT32, BASE_HEX, NULL, 0x0,
374 static gint *ett[] = {
377 &ett_gre_wccp2_redirect_header,
380 proto_gre = proto_register_protocol("Generic Routing Encapsulation",
382 proto_register_field_array(proto_gre, hf, array_length(hf));
383 proto_register_subtree_array(ett, array_length(ett));
385 /* subdissector code */
386 gre_dissector_table = register_dissector_table("gre.proto",
387 "GRE protocol type", FT_UINT16, BASE_HEX);
391 proto_reg_handoff_gre(void)
393 dissector_handle_t gre_handle;
395 gre_handle = create_dissector_handle(dissect_gre, proto_gre);
396 dissector_add("ip.proto", IP_PROTO_GRE, gre_handle);
397 data_handle = find_dissector("data");