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.33 2001/01/03 06:55:28 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
69 static void add_flags_and_ver(proto_tree *, guint16, tvbuff_t *, int, int);
70 static void dissect_gre_wccp2_redirect_header(tvbuff_t *, int, proto_tree *);
72 static const value_string typevals[] = {
80 static dissector_handle_t ip_handle;
81 static dissector_handle_t ppp_handle;
84 dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
87 guint16 flags_and_ver;
93 CHECK_DISPLAY_AS_DATA(proto_gre, tvb, pinfo, tree);
95 pinfo->current_proto = "GRE";
97 flags_and_ver = tvb_get_ntohs(tvb, offset);
98 type = tvb_get_ntohs(tvb, offset + sizeof(flags_and_ver));
100 if (check_col(pinfo->fd, COL_PROTOCOL))
101 col_set_str(pinfo->fd, COL_PROTOCOL, "GRE");
103 if (check_col(pinfo->fd, COL_INFO)) {
104 col_add_fstr(pinfo->fd, COL_INFO, "Encapsulated %s",
105 val_to_str(type, typevals, "0x%04X (unknown)"));
109 gboolean is_ppp = FALSE;
110 gboolean is_wccp2 = FALSE;
112 proto_tree * gre_tree;
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)
130 /* WCCP2 puts an extra 4 octets into the header, but uses the same
131 encapsulation type; if it looks as if the first octet of the packet
132 isn't the beginning of an IPv4 header, assume it's WCCP2. */
133 if ((tvb_get_guint8(tvb, offset + sizeof(flags_and_ver) + sizeof(type)) & 0xF0) != 0x40) {
140 ti = proto_tree_add_protocol_format(tree, proto_gre, tvb, offset, len,
141 "Generic Routing Encapsulation (%s)",
142 val_to_str(type, typevals, "0x%04X - unknown"));
143 gre_tree = proto_item_add_subtree(ti, ett_gre);
144 add_flags_and_ver(gre_tree, flags_and_ver, tvb, offset, is_ppp);
145 offset += sizeof(flags_and_ver);
147 proto_tree_add_uint(gre_tree, hf_gre_proto, tvb, offset, sizeof(type), type);
148 offset += sizeof(type);
150 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
151 guint length, reported_length;
153 guint16 cksum, computed_cksum;
155 cksum = tvb_get_ntohs(tvb, offset);
156 length = tvb_length(tvb);
157 reported_length = tvb_reported_length(tvb);
158 if ((flags_and_ver & GH_B_C) && !pinfo->fragmented
159 && length >= reported_length) {
160 /* The Checksum Present bit is set, and the packet isn't part of a
161 fragmented datagram and isn't truncated, so we can checksum it. */
163 cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, reported_length);
164 cksum_vec[0].len = reported_length;
165 computed_cksum = in_cksum(cksum_vec, 1);
166 if (computed_cksum == 0) {
167 proto_tree_add_text(gre_tree, tvb, offset, sizeof(cksum),
168 "Checksum: 0x%04x (correct)", cksum);
170 proto_tree_add_text(gre_tree, tvb, offset, sizeof(cksum),
171 "Checksum: 0x%04x (incorrect, should be 0x%04x)",
172 cksum, in_cksum_shouldbe(cksum, computed_cksum));
175 proto_tree_add_text(gre_tree, tvb, offset, sizeof(cksum),
176 "Checksum: 0x%04x", cksum);
178 offset += sizeof(cksum);
181 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
182 guint16 rtoffset = tvb_get_ntohs(tvb, offset);
183 proto_tree_add_text(gre_tree, tvb, offset, sizeof(rtoffset),
184 "Offset: %u", rtoffset);
185 offset += sizeof(rtoffset);
188 if (flags_and_ver & GH_B_K) {
193 paylen = tvb_get_ntohs(tvb, offset);
194 proto_tree_add_text(gre_tree, tvb, offset, sizeof(paylen),
195 "Payload length: %u", paylen);
196 offset += sizeof(paylen);
198 callid = tvb_get_ntohs(tvb, offset);
199 proto_tree_add_text(gre_tree, tvb, offset, sizeof(callid),
200 "Call ID: %u", callid);
201 offset += sizeof(callid);
204 guint32 key = tvb_get_ntohl(tvb, offset);
205 proto_tree_add_text(gre_tree, tvb, offset, sizeof(key),
207 offset += sizeof(key);
211 if (flags_and_ver & GH_B_S) {
212 guint32 seqnum = tvb_get_ntohl(tvb, offset);
213 proto_tree_add_text(gre_tree, tvb, offset, sizeof(seqnum),
214 "Sequence number: %u", seqnum);
215 offset += sizeof(seqnum);
218 if (is_ppp && flags_and_ver & GH_P_A) {
219 guint32 acknum = tvb_get_ntohl(tvb, offset);
220 proto_tree_add_text(gre_tree, tvb, offset, sizeof(acknum),
221 "Acknowledgement number: %u", acknum);
222 offset += sizeof(acknum);
225 if (flags_and_ver & GH_B_R) {
227 sre_af = tvb_get_ntohs(tvb, offset);
228 proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint16),
229 "Address family: %u", sre_af);
230 offset += sizeof(guint16);
231 proto_tree_add_text(gre_tree, tvb, offset, 1,
232 "SRE offset: %u", tvb_get_guint8(tvb, offset));
233 offset += sizeof(guint8);
234 sre_length = tvb_get_guint8(tvb, offset);
235 proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint8),
236 "SRE length: %u", sre_length);
237 offset += sizeof(guint8);
238 if (sre_af == 0 && sre_length == 0)
240 offset += sre_length;
244 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
247 call_dissector(ppp_handle, next_tvb, pinfo, tree);
250 call_dissector(ip_handle, next_tvb, pinfo, tree);
254 dissect_gre_wccp2_redirect_header(tvb, offset, gre_tree);
257 call_dissector(ip_handle, next_tvb, pinfo, tree);
260 dissect_ipx(next_tvb, pinfo, tree);
263 dissect_data(next_tvb, 0, pinfo, gre_tree);
270 add_flags_and_ver(proto_tree *tree, guint16 flags_and_ver, tvbuff_t *tvb,
271 int offset, int is_ppp)
274 proto_tree * fv_tree;
275 int nbits = sizeof(flags_and_ver) * 8;
277 ti = proto_tree_add_text(tree, tvb, offset, 2,
278 "Flags and version: %#04x", flags_and_ver);
279 fv_tree = proto_item_add_subtree(ti, ett_gre_flags);
281 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
282 decode_boolean_bitfield(flags_and_ver, GH_B_C, nbits,
283 "Checksum", "No checksum"));
284 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
285 decode_boolean_bitfield(flags_and_ver, GH_B_R, nbits,
286 "Routing", "No routing"));
287 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
288 decode_boolean_bitfield(flags_and_ver, GH_B_K, nbits,
290 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
291 decode_boolean_bitfield(flags_and_ver, GH_B_S, nbits,
292 "Sequence number", "No sequence number"));
293 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
294 decode_boolean_bitfield(flags_and_ver, GH_B_s, nbits,
295 "Strict source route", "No strict source route"));
296 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
297 decode_numeric_bitfield(flags_and_ver, GH_B_RECUR, nbits,
298 "Recursion control: %u"));
300 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
301 decode_boolean_bitfield(flags_and_ver, GH_P_A, nbits,
302 "Acknowledgment number", "No acknowledgment number"));
303 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
304 decode_numeric_bitfield(flags_and_ver, GH_P_FLAGS, nbits,
308 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
309 decode_numeric_bitfield(flags_and_ver, GH_R_FLAGS, nbits,
313 proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
314 decode_numeric_bitfield(flags_and_ver, GH_B_VER, nbits,
319 dissect_gre_wccp2_redirect_header(tvbuff_t *tvb, int offset, proto_tree *tree)
322 proto_tree * rh_tree;
325 ti = proto_tree_add_text(tree, tvb, offset, 4, "Redirect header");
326 rh_tree = proto_item_add_subtree(ti, ett_gre_wccp2_redirect_header);
328 rh_flags = tvb_get_guint8(tvb, offset);
329 proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
330 decode_boolean_bitfield(rh_flags, 0x80, 8,
331 "Dynamic service", "Well-known service"));
332 proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
333 decode_boolean_bitfield(rh_flags, 0x40, 8,
334 "Alternative bucket used", "Alternative bucket not used"));
336 proto_tree_add_text(rh_tree, tvb, offset + 1, 1, "Service ID: %s",
337 val_to_str(tvb_get_guint8(tvb, offset + 1), service_id_vals, "Unknown (0x%02X)"));
339 proto_tree_add_text(rh_tree, tvb, offset + 2, 1, "Alternative bucket index: %u",
340 tvb_get_guint8(tvb, offset + 2));
341 proto_tree_add_text(rh_tree, tvb, offset + 3, 1, "Primary bucket index: %u",
342 tvb_get_guint8(tvb, offset + 3));
346 proto_register_gre(void)
348 static hf_register_info hf[] = {
350 { "Protocol Type", "gre.proto", FT_UINT16, BASE_HEX, VALS(typevals), 0x0,
351 "The protocol that is GRE encapsulated"}
354 static gint *ett[] = {
357 &ett_gre_wccp2_redirect_header,
360 proto_gre = proto_register_protocol("Generic Routing Encapsulation",
362 proto_register_field_array(proto_gre, hf, array_length(hf));
363 proto_register_subtree_array(ett, array_length(ett));
367 proto_reg_handoff_gre(void)
369 dissector_add("ip.proto", IP_PROTO_GRE, dissect_gre);
372 * Get handles for the IP and PPP dissectors.
374 ip_handle = find_dissector("ip");
375 ppp_handle = find_dissector("ppp");