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" },
79 { ETHERTYPE_IPX, "IPX"},
80 { ETHERTYPE_ETHBRIDGE, "Transparent Ethernet bridging" },
81 { ETHERTYPE_RAW_FR, "Frame Relay"},
82 { ETHERTYPE_IPv6, "IPv6" },
83 { ETHERTYPE_MPLS, "MPLS label switched packet" },
84 { ETHERTYPE_CDMA2000_A10_UBS,"CDMA2000 A10 Unstructured byte stream" },
89 dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
92 guint16 flags_and_ver;
94 gboolean is_ppp = FALSE;
95 gboolean is_wccp2 = FALSE;
98 proto_tree *gre_tree = NULL;
103 flags_and_ver = tvb_get_ntohs(tvb, offset);
104 type = tvb_get_ntohs(tvb, offset + sizeof(flags_and_ver));
106 if (check_col(pinfo->cinfo, COL_PROTOCOL))
107 col_set_str(pinfo->cinfo, COL_PROTOCOL, "GRE");
109 if (check_col(pinfo->cinfo, COL_INFO)) {
110 col_add_fstr(pinfo->cinfo, COL_INFO, "Encapsulated %s",
111 val_to_str(type, typevals, "0x%04X (unknown)"));
114 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R)
116 if (flags_and_ver & GH_B_K)
118 if (flags_and_ver & GH_B_S)
123 if (flags_and_ver & GH_P_A)
127 case ETHERTYPE_CDMA2000_A10_UBS:
128 if (flags_and_ver & GH_P_A)
134 /* WCCP2 puts an extra 4 octets into the header, but uses the same
135 encapsulation type; if it looks as if the first octet of the packet
136 isn't the beginning of an IPv4 header, assume it's WCCP2. */
137 if ((tvb_get_guint8(tvb, offset + sizeof(flags_and_ver) + sizeof(type)) & 0xF0) != 0x40) {
145 ti = proto_tree_add_protocol_format(tree, proto_gre, tvb, offset, len,
146 "Generic Routing Encapsulation (%s)",
147 val_to_str(type, typevals, "0x%04X - unknown"));
148 gre_tree = proto_item_add_subtree(ti, ett_gre);
149 add_flags_and_ver(gre_tree, flags_and_ver, tvb, offset, is_ppp);
151 offset += sizeof(flags_and_ver);
154 proto_tree_add_uint(gre_tree, hf_gre_proto, tvb, offset, sizeof(type), type);
156 offset += sizeof(type);
158 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
160 guint length, reported_length;
162 guint16 cksum, computed_cksum;
164 cksum = tvb_get_ntohs(tvb, offset);
165 length = tvb_length(tvb);
166 reported_length = tvb_reported_length(tvb);
167 if ((flags_and_ver & GH_B_C) && !pinfo->fragmented
168 && length >= reported_length) {
169 /* The Checksum Present bit is set, and the packet isn't part of a
170 fragmented datagram and isn't truncated, so we can checksum it. */
172 cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, reported_length);
173 cksum_vec[0].len = reported_length;
174 computed_cksum = in_cksum(cksum_vec, 1);
175 if (computed_cksum == 0) {
176 proto_tree_add_text(gre_tree, tvb, offset, 2,
177 "Checksum: 0x%04x [correct]", cksum);
179 proto_tree_add_text(gre_tree, tvb, offset, 2,
180 "Checksum: 0x%04x [incorrect, should be 0x%04x]",
181 cksum, in_cksum_shouldbe(cksum, computed_cksum));
184 proto_tree_add_text(gre_tree, tvb, offset, 2,
185 "Checksum: 0x%04x", cksum);
191 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
193 proto_tree_add_text(gre_tree, tvb, offset, 2,
194 "Offset: %u", tvb_get_ntohs(tvb, offset));
199 if (flags_and_ver & GH_B_K) {
200 if (is_ppp && type!=ETHERTYPE_CDMA2000_A10_UBS) {
202 proto_tree_add_text(gre_tree, tvb, offset, 2,
203 "Payload length: %u", tvb_get_ntohs(tvb, offset));
207 proto_tree_add_text(gre_tree, tvb, offset, 2,
208 "Call ID: %u", tvb_get_ntohs(tvb, offset));
214 proto_tree_add_item(gre_tree, hf_gre_key, tvb, offset, 4, FALSE);
219 if (flags_and_ver & GH_B_S) {
221 proto_tree_add_text(gre_tree, tvb, offset, 4,
222 "Sequence number: %u", tvb_get_ntohl(tvb, offset));
227 if (is_ppp && flags_and_ver & GH_P_A) {
229 proto_tree_add_text(gre_tree, tvb, offset, 4,
230 "Acknowledgement number: %u", tvb_get_ntohl(tvb, offset));
235 if (flags_and_ver & GH_B_R) {
237 sre_af = tvb_get_ntohs(tvb, offset);
239 proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint16),
240 "Address family: %u", sre_af);
242 offset += sizeof(guint16);
244 proto_tree_add_text(gre_tree, tvb, offset, 1,
245 "SRE offset: %u", tvb_get_guint8(tvb, offset));
247 offset += sizeof(guint8);
248 sre_length = tvb_get_guint8(tvb, offset);
250 proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint8),
251 "SRE length: %u", sre_length);
253 offset += sizeof(guint8);
254 if (sre_af == 0 && sre_length == 0)
256 offset += sre_length;
260 if (type == GRE_WCCP) {
263 dissect_gre_wccp2_redirect_header(tvb, offset, gre_tree);
268 /* If the S bit is not set, this packet might not have a payload, so
269 check whether there's any data left, first.
271 XXX - the S bit isn't in RFC 2784, which deprecates that bit
272 and some other bits in RFC 1701 and says that they should be
273 zero for RFC 2784-compliant GRE; as such, the absence of the
274 S bit doesn't necessarily mean there's no payload. */
275 if (!(flags_and_ver & GH_B_S)) {
276 if (tvb_reported_length_remaining(tvb, offset) <= 0)
277 return; /* no payload */
279 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
280 if (!dissector_try_port(gre_dissector_table, type, next_tvb, pinfo, tree))
281 call_dissector(data_handle,next_tvb, pinfo, gre_tree);
285 add_flags_and_ver(proto_tree *tree, guint16 flags_and_ver, tvbuff_t *tvb,
286 int offset, int is_ppp)
289 proto_tree * fv_tree;
290 int nbits = sizeof(flags_and_ver) * 8;
292 ti = proto_tree_add_text(tree, tvb, offset, 2,
293 "Flags and version: %#04x", flags_and_ver);
294 fv_tree = proto_item_add_subtree(ti, ett_gre_flags);
296 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
297 decode_boolean_bitfield(flags_and_ver, GH_B_C, nbits,
298 "Checksum", "No checksum"));
299 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
300 decode_boolean_bitfield(flags_and_ver, GH_B_R, nbits,
301 "Routing", "No routing"));
302 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
303 decode_boolean_bitfield(flags_and_ver, GH_B_K, nbits,
305 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
306 decode_boolean_bitfield(flags_and_ver, GH_B_S, nbits,
307 "Sequence number", "No sequence number"));
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 "Strict source route", "No strict source route"));
311 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
312 decode_numeric_bitfield(flags_and_ver, GH_B_RECUR, nbits,
313 "Recursion control: %u"));
315 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
316 decode_boolean_bitfield(flags_and_ver, GH_P_A, nbits,
317 "Acknowledgment number", "No acknowledgment number"));
318 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
319 decode_numeric_bitfield(flags_and_ver, GH_P_FLAGS, nbits,
323 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
324 decode_numeric_bitfield(flags_and_ver, GH_R_FLAGS, nbits,
328 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
329 decode_numeric_bitfield(flags_and_ver, GH_B_VER, nbits,
334 dissect_gre_wccp2_redirect_header(tvbuff_t *tvb, int offset, proto_tree *tree)
337 proto_tree * rh_tree;
340 ti = proto_tree_add_text(tree, tvb, offset, 4, "Redirect header");
341 rh_tree = proto_item_add_subtree(ti, ett_gre_wccp2_redirect_header);
343 rh_flags = tvb_get_guint8(tvb, offset);
344 proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
345 decode_boolean_bitfield(rh_flags, 0x80, 8,
346 "Dynamic service", "Well-known service"));
347 proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
348 decode_boolean_bitfield(rh_flags, 0x40, 8,
349 "Alternative bucket used", "Alternative bucket not used"));
351 proto_tree_add_text(rh_tree, tvb, offset + 1, 1, "Service ID: %s",
352 val_to_str(tvb_get_guint8(tvb, offset + 1), service_id_vals, "Unknown (0x%02X)"));
354 proto_tree_add_text(rh_tree, tvb, offset + 2, 1, "Alternative bucket index: %u",
355 tvb_get_guint8(tvb, offset + 2));
356 proto_tree_add_text(rh_tree, tvb, offset + 3, 1, "Primary bucket index: %u",
357 tvb_get_guint8(tvb, offset + 3));
361 proto_register_gre(void)
363 static hf_register_info hf[] = {
365 { "Protocol Type", "gre.proto", FT_UINT16, BASE_HEX, VALS(typevals), 0x0,
366 "The protocol that is GRE encapsulated", HFILL }
369 { "GRE Key", "gre.key", FT_UINT32, BASE_HEX, NULL, 0x0,
373 static gint *ett[] = {
376 &ett_gre_wccp2_redirect_header,
379 proto_gre = proto_register_protocol("Generic Routing Encapsulation",
381 proto_register_field_array(proto_gre, hf, array_length(hf));
382 proto_register_subtree_array(ett, array_length(ett));
384 /* subdissector code */
385 gre_dissector_table = register_dissector_table("gre.proto",
386 "GRE protocol type", FT_UINT16, BASE_HEX);
390 proto_reg_handoff_gre(void)
392 dissector_handle_t gre_handle;
394 gre_handle = create_dissector_handle(dissect_gre, proto_gre);
395 dissector_add("ip.proto", IP_PROTO_GRE, gre_handle);
396 data_handle = find_dissector("data");