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.14 2000/01/07 22:05: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>
41 static int proto_gre = -1;
42 static int hf_gre_proto = -1;
44 static gint ett_gre = -1;
45 static gint ett_gre_flags = -1;
47 /* bit positions for flags in header */
53 #define GH_B_RECUR 0x0700
54 #define GH_P_A 0x0080 /* only in special PPTPized GRE header */
55 #define GH_P_FLAGS 0x0078 /* only in special PPTPized GRE header */
56 #define GH_R_FLAGS 0x00F8
57 #define GH_B_VER 0x0007
59 #define GRE_PPP 0x880B
61 #define GRE_WCCP 0x883E
63 static void add_flags_and_ver(proto_tree *, guint16, int, int);
65 static const value_string typevals[] = {
73 dissect_gre(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
75 guint16 flags_and_ver = pntohs(pd + offset);
76 guint16 type = pntohs(pd + offset + sizeof(flags_and_ver));
80 if (check_col(fd, COL_PROTOCOL))
81 col_add_str(fd, COL_PROTOCOL, "GRE");
83 if (check_col(fd, COL_INFO)) {
84 col_add_fstr(fd, COL_INFO, "Encapsulated %s",
85 val_to_str(type, typevals, "0x%04X (unknown)"));
88 if (IS_DATA_IN_FRAME(offset) && tree) {
89 gboolean is_ppp = FALSE;
90 gboolean is_wccp2 = FALSE;
92 proto_tree * gre_tree;
95 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R)
97 if (flags_and_ver & GH_B_K)
99 if (flags_and_ver & GH_B_S)
104 if (flags_and_ver & GH_P_A)
110 /* WCCP2 apparently puts an extra 4 octets into the header, but uses
111 the same encapsulation type; if it looks as if the first octet of
112 the packet isn't the beginning of an IPv4 header, assume it's
114 if ((pd[offset + sizeof(flags_and_ver) + sizeof(type)] & 0xF0) != 0x40) {
121 ti = proto_tree_add_item_format(tree, proto_gre, offset, len, NULL,
122 "Generic Routing Encapsulation (%s)",
123 val_to_str(type, typevals, "0x%04X - unknown"));
124 gre_tree = proto_item_add_subtree(ti, ett_gre);
125 add_flags_and_ver(gre_tree, flags_and_ver, offset, is_ppp);
127 offset += sizeof(flags_and_ver);
129 proto_tree_add_item(gre_tree, hf_gre_proto, offset, sizeof(type), type);
130 offset += sizeof(type);
132 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
133 guint16 checksum = pntohs(pd + offset);
134 proto_tree_add_text(gre_tree, offset, sizeof(checksum),
135 "Checksum: %u", checksum);
136 offset += sizeof(checksum);
139 if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
140 guint16 rtoffset = pntohs(pd + offset);
141 proto_tree_add_text(gre_tree, offset, sizeof(rtoffset),
142 "Offset: %u", rtoffset);
143 offset += sizeof(rtoffset);
146 if (flags_and_ver & GH_B_K) {
151 paylen = pntohs(pd + offset);
152 proto_tree_add_text(gre_tree, offset, sizeof(paylen),
153 "Payload length: %u", paylen);
154 offset += sizeof(paylen);
156 callid = pntohs(pd + offset);
157 proto_tree_add_text(gre_tree, offset, sizeof(callid),
158 "Call ID: %u", callid);
159 offset += sizeof(callid);
162 guint32 key = pntohl(pd + offset);
163 proto_tree_add_text(gre_tree, offset, sizeof(key),
165 offset += sizeof(key);
169 if (flags_and_ver & GH_B_S) {
170 guint32 seqnum = pntohl(pd + offset);
171 proto_tree_add_text(gre_tree, offset, sizeof(seqnum),
172 "Sequence number: %u", seqnum);
173 offset += sizeof(seqnum);
176 if (is_ppp && flags_and_ver & GH_P_A) {
177 guint32 acknum = pntohl(pd + offset);
178 proto_tree_add_text(gre_tree, offset, sizeof(acknum),
179 "Acknowledgement number: %u", acknum);
180 offset += sizeof(acknum);
183 if (flags_and_ver & GH_B_R) {
185 sre_af = pntohs(pd + offset);
186 proto_tree_add_text(gre_tree, offset, sizeof(guint16),
187 "Address family: %u", sre_af);
188 offset += sizeof(guint16);
189 proto_tree_add_text(gre_tree, offset, 1,
190 "SRE offset: %u", pd[offset++]);
191 sre_length = pd[offset];
192 proto_tree_add_text(gre_tree, offset, sizeof(guint8),
193 "SRE length: %u", sre_length);
194 offset += sizeof(guint8);
195 if (sre_af == 0 && sre_length == 0)
197 offset += sre_length;
203 dissect_payload_ppp(pd, offset, fd, tree);
206 dissect_ip(pd, offset, fd, tree);
210 proto_tree_add_text(gre_tree, offset, sizeof(guint32), "WCCPv2 Data");
213 dissect_ip(pd, offset, fd, tree);
216 dissect_data(pd, offset, fd, gre_tree);
223 add_flags_and_ver(proto_tree *tree, guint16 flags_and_ver, int offset, int is_ppp) {
226 proto_tree * fv_tree;
227 int nbits = sizeof(flags_and_ver) * 8;
229 ti = proto_tree_add_text(tree, offset, 2,
230 "Flags and version: %#04x", flags_and_ver);
231 fv_tree = proto_item_add_subtree(ti, ett_gre_flags);
233 proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
234 decode_boolean_bitfield(flags_and_ver, GH_B_C, nbits,
235 "Checksum", "No checksum"));
236 proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
237 decode_boolean_bitfield(flags_and_ver, GH_B_R, nbits,
238 "Routing", "No routing"));
239 proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
240 decode_boolean_bitfield(flags_and_ver, GH_B_K, nbits,
242 proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
243 decode_boolean_bitfield(flags_and_ver, GH_B_S, nbits,
244 "Sequence number", "No sequence number"));
245 proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
246 decode_boolean_bitfield(flags_and_ver, GH_B_s, nbits,
247 "Strict source route", "No strict source route"));
248 proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
249 decode_numeric_bitfield(flags_and_ver, GH_B_RECUR, nbits,
250 "Recursion control: %u"));
252 proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
253 decode_boolean_bitfield(flags_and_ver, GH_P_A, nbits,
254 "Acknowledgment number", "No acknowledgment number"));
255 proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
256 decode_numeric_bitfield(flags_and_ver, GH_P_FLAGS, nbits,
260 proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
261 decode_numeric_bitfield(flags_and_ver, GH_R_FLAGS, nbits,
265 proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
266 decode_numeric_bitfield(flags_and_ver, GH_B_VER, nbits,
271 proto_register_gre(void)
273 static hf_register_info hf[] = {
275 { "Protocol Type", "gre.proto", FT_UINT16, BASE_HEX, VALS(typevals), 0x0,
276 "The protocol that is GRE encapsulated"}
279 static gint *ett[] = {
284 proto_gre = proto_register_protocol("Generic Routing Encapsulation", "gre");
285 proto_register_field_array(proto_gre, hf, array_length(hf));
286 proto_register_subtree_array(ett, array_length(ett));