1999-06-11 15:30:55 +00:00
|
|
|
/* packet-gre.c
|
|
|
|
* Routines for the Generic Routing Encapsulation (GRE) protocol
|
|
|
|
* Brad Robel-Forrest <brad.robel-forrest@watchguard.com>
|
|
|
|
*
|
1999-12-10 21:27:13 +00:00
|
|
|
* $Id: packet-gre.c,v 1.11 1999/12/10 21:27:13 guy Exp $
|
1999-06-11 15:30:55 +00:00
|
|
|
*
|
|
|
|
* Ethereal - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@unicom.net>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
1999-07-13 02:53:26 +00:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
1999-06-11 15:30:55 +00:00
|
|
|
#include <netinet/in.h>
|
1999-07-13 02:53:26 +00:00
|
|
|
#endif
|
1999-06-11 15:30:55 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include "packet.h"
|
|
|
|
|
1999-07-29 05:47:07 +00:00
|
|
|
static int proto_gre = -1;
|
|
|
|
|
1999-11-16 11:44:20 +00:00
|
|
|
static gint ett_gre = -1;
|
|
|
|
static gint ett_gre_flags = -1;
|
|
|
|
|
1999-06-11 15:30:55 +00:00
|
|
|
/* bit positions for flags in header */
|
|
|
|
#define GH_B_C 0x8000
|
|
|
|
#define GH_B_R 0x4000
|
|
|
|
#define GH_B_K 0x2000
|
|
|
|
#define GH_B_S 0x1000
|
|
|
|
#define GH_B_s 0x0800
|
|
|
|
#define GH_B_RECUR 0x0700
|
|
|
|
#define GH_P_A 0x0080 /* only in special PPTPized GRE header */
|
|
|
|
#define GH_P_FLAGS 0x0078 /* only in special PPTPized GRE header */
|
|
|
|
#define GH_R_FLAGS 0x00F8
|
|
|
|
#define GH_B_VER 0x0007
|
|
|
|
|
|
|
|
#define GRE_PPP 0x880B
|
1999-12-09 02:53:33 +00:00
|
|
|
#define GRE_IP 0x0800
|
1999-06-11 15:30:55 +00:00
|
|
|
|
|
|
|
static int calc_len(guint16, int);
|
|
|
|
static void add_flags_and_ver(proto_tree *, guint16, int, int);
|
|
|
|
|
|
|
|
void
|
|
|
|
dissect_gre(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
|
|
|
|
|
|
|
|
guint16 flags_and_ver = pntohs(pd + offset);
|
|
|
|
guint16 type = pntohs(pd + offset + sizeof(flags_and_ver));
|
|
|
|
static const value_string typevals[] = {
|
|
|
|
{ GRE_PPP, "PPP" },
|
1999-12-09 02:53:33 +00:00
|
|
|
{ GRE_IP, "IP" },
|
1999-06-11 15:30:55 +00:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|
1999-12-09 02:53:33 +00:00
|
|
|
guint16 sre_af;
|
|
|
|
guint8 sre_length;
|
1999-06-11 15:30:55 +00:00
|
|
|
|
|
|
|
if (check_col(fd, COL_PROTOCOL))
|
|
|
|
col_add_str(fd, COL_PROTOCOL, "GRE");
|
|
|
|
|
|
|
|
if (check_col(fd, COL_INFO)) {
|
1999-12-09 02:53:33 +00:00
|
|
|
col_add_fstr(fd, COL_INFO, "Encapsulated %s",
|
|
|
|
val_to_str(type, typevals, "0x%04X (unknown)"));
|
1999-06-11 15:30:55 +00:00
|
|
|
}
|
|
|
|
|
1999-09-17 05:56:58 +00:00
|
|
|
if (IS_DATA_IN_FRAME(offset) && tree) {
|
1999-12-10 21:27:13 +00:00
|
|
|
gboolean is_ppp;
|
1999-06-11 15:30:55 +00:00
|
|
|
proto_item * ti;
|
|
|
|
proto_tree * gre_tree;
|
|
|
|
|
1999-12-10 21:27:13 +00:00
|
|
|
is_ppp = (type == GRE_PPP);
|
|
|
|
|
|
|
|
ti = proto_tree_add_item_format(tree, proto_gre, offset,
|
|
|
|
calc_len(flags_and_ver, type), NULL,
|
|
|
|
"Generic Routing Encapsulation (%s)",
|
|
|
|
val_to_str(type, typevals, "0x%04X - unknown"));
|
|
|
|
gre_tree = proto_item_add_subtree(ti, ett_gre);
|
|
|
|
add_flags_and_ver(gre_tree, flags_and_ver, offset, is_ppp);
|
1999-06-11 15:30:55 +00:00
|
|
|
|
|
|
|
offset += sizeof(flags_and_ver);
|
|
|
|
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(gre_tree, offset, sizeof(type),
|
1999-06-11 15:30:55 +00:00
|
|
|
"Protocol Type: %s (%#04x)",
|
|
|
|
val_to_str(type, typevals, "Unknown"), type);
|
|
|
|
offset += sizeof(type);
|
|
|
|
|
|
|
|
if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
|
|
|
|
guint16 checksum = pntohs(pd + offset);
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(gre_tree, offset, sizeof(checksum),
|
1999-06-11 15:30:55 +00:00
|
|
|
"Checksum: %u", checksum);
|
|
|
|
offset += sizeof(checksum);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
|
|
|
|
guint16 rtoffset = pntohs(pd + offset);
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(gre_tree, offset, sizeof(rtoffset),
|
1999-06-11 15:30:55 +00:00
|
|
|
"Offset: %u", rtoffset);
|
|
|
|
offset += sizeof(rtoffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags_and_ver & GH_B_K) {
|
|
|
|
if (is_ppp) {
|
|
|
|
guint16 paylen;
|
|
|
|
guint16 callid;
|
|
|
|
|
|
|
|
paylen = pntohs(pd + offset);
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(gre_tree, offset, sizeof(paylen),
|
1999-06-11 15:30:55 +00:00
|
|
|
"Payload length: %u", paylen);
|
|
|
|
offset += sizeof(paylen);
|
|
|
|
|
|
|
|
callid = pntohs(pd + offset);
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(gre_tree, offset, sizeof(callid),
|
1999-06-11 15:30:55 +00:00
|
|
|
"Call ID: %u", callid);
|
|
|
|
offset += sizeof(callid);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
guint32 key = pntohl(pd + offset);
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(gre_tree, offset, sizeof(key),
|
1999-06-11 15:30:55 +00:00
|
|
|
"Key: %u", key);
|
|
|
|
offset += sizeof(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags_and_ver & GH_B_S) {
|
|
|
|
guint32 seqnum = pntohl(pd + offset);
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(gre_tree, offset, sizeof(seqnum),
|
1999-06-11 15:30:55 +00:00
|
|
|
"Sequence number: %u", seqnum);
|
|
|
|
offset += sizeof(seqnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_ppp && flags_and_ver & GH_P_A) {
|
|
|
|
guint32 acknum = pntohl(pd + offset);
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(gre_tree, offset, sizeof(acknum),
|
1999-06-11 15:30:55 +00:00
|
|
|
"Acknowledgement number: %u", acknum);
|
|
|
|
offset += sizeof(acknum);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags_and_ver & GH_B_R) {
|
1999-12-09 02:53:33 +00:00
|
|
|
for (;;) {
|
|
|
|
sre_af = pntohs(pd + offset);
|
|
|
|
proto_tree_add_text(gre_tree, offset, sizeof(guint16),
|
|
|
|
"Address family: %u", sre_af);
|
|
|
|
offset += sizeof(guint16);
|
|
|
|
proto_tree_add_text(gre_tree, offset, 1,
|
1999-06-11 15:30:55 +00:00
|
|
|
"SRE offset: %u", pd[offset++]);
|
1999-12-09 02:53:33 +00:00
|
|
|
sre_length = pd[offset];
|
|
|
|
proto_tree_add_text(gre_tree, offset, sizeof(guint8),
|
|
|
|
"SRE length: %u", sre_length);
|
|
|
|
offset += sizeof(guint8);
|
|
|
|
if (sre_af == 0 && sre_length == 0)
|
|
|
|
break;
|
|
|
|
offset += sre_length;
|
|
|
|
}
|
1999-06-11 15:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
1999-12-10 21:27:13 +00:00
|
|
|
case GRE_PPP:
|
1999-06-11 15:30:55 +00:00
|
|
|
dissect_payload_ppp(pd, offset, fd, tree);
|
|
|
|
break;
|
1999-12-10 21:27:13 +00:00
|
|
|
case GRE_IP:
|
1999-12-09 02:53:33 +00:00
|
|
|
dissect_ip(pd, offset, fd, tree);
|
|
|
|
break;
|
1999-06-11 15:30:55 +00:00
|
|
|
default:
|
|
|
|
dissect_data(pd, offset, fd, gre_tree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-12-10 21:27:13 +00:00
|
|
|
calc_len(guint16 flags_and_ver, int type)
|
|
|
|
{
|
1999-06-11 15:30:55 +00:00
|
|
|
int len = 4;
|
|
|
|
|
1999-12-10 21:27:13 +00:00
|
|
|
if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R)
|
|
|
|
len += 4;
|
|
|
|
if (flags_and_ver & GH_B_K)
|
|
|
|
len += 4;
|
|
|
|
if (flags_and_ver & GH_B_S)
|
|
|
|
len += 4;
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case GRE_PPP:
|
|
|
|
if (flags_and_ver & GH_P_A)
|
|
|
|
len += 4;
|
|
|
|
break;
|
|
|
|
}
|
1999-06-11 15:30:55 +00:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_flags_and_ver(proto_tree *tree, guint16 flags_and_ver, int offset, int is_ppp) {
|
|
|
|
|
|
|
|
proto_item * ti;
|
|
|
|
proto_tree * fv_tree;
|
|
|
|
int nbits = sizeof(flags_and_ver) * 8;
|
|
|
|
|
1999-07-07 22:52:57 +00:00
|
|
|
ti = proto_tree_add_text(tree, offset, 2,
|
1999-12-10 02:29:48 +00:00
|
|
|
"Flags and version: %#04x", flags_and_ver);
|
1999-11-16 11:44:20 +00:00
|
|
|
fv_tree = proto_item_add_subtree(ti, ett_gre_flags);
|
1999-06-11 15:30:55 +00:00
|
|
|
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
|
1999-06-11 15:30:55 +00:00
|
|
|
decode_boolean_bitfield(flags_and_ver, GH_B_C, nbits,
|
|
|
|
"Checksum", "No checksum"));
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
|
1999-06-11 15:30:55 +00:00
|
|
|
decode_boolean_bitfield(flags_and_ver, GH_B_R, nbits,
|
|
|
|
"Routing", "No routing"));
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
|
1999-06-11 15:30:55 +00:00
|
|
|
decode_boolean_bitfield(flags_and_ver, GH_B_K, nbits,
|
|
|
|
"Key", "No key"));
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
|
1999-06-11 15:30:55 +00:00
|
|
|
decode_boolean_bitfield(flags_and_ver, GH_B_S, nbits,
|
|
|
|
"Sequence number", "No sequence number"));
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
|
1999-06-11 15:30:55 +00:00
|
|
|
decode_boolean_bitfield(flags_and_ver, GH_B_s, nbits,
|
|
|
|
"Strict source route", "No strict source route"));
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
|
1999-06-11 15:30:55 +00:00
|
|
|
decode_numeric_bitfield(flags_and_ver, GH_B_RECUR, nbits,
|
|
|
|
"Recursion control: %u"));
|
|
|
|
if (is_ppp) {
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
|
1999-06-11 15:30:55 +00:00
|
|
|
decode_boolean_bitfield(flags_and_ver, GH_P_A, nbits,
|
|
|
|
"Acknowledgment number", "No acknowledgment number"));
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
|
1999-06-11 15:30:55 +00:00
|
|
|
decode_numeric_bitfield(flags_and_ver, GH_P_FLAGS, nbits,
|
|
|
|
"Flags: %u"));
|
|
|
|
}
|
|
|
|
else {
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
|
1999-06-11 15:30:55 +00:00
|
|
|
decode_numeric_bitfield(flags_and_ver, GH_R_FLAGS, nbits,
|
|
|
|
"Flags: %u"));
|
|
|
|
}
|
|
|
|
|
1999-07-07 22:52:57 +00:00
|
|
|
proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
|
1999-06-11 15:30:55 +00:00
|
|
|
decode_numeric_bitfield(flags_and_ver, GH_B_VER, nbits,
|
|
|
|
"Version: %u"));
|
|
|
|
}
|
|
|
|
|
1999-07-29 05:47:07 +00:00
|
|
|
void
|
|
|
|
proto_register_gre(void)
|
|
|
|
{
|
|
|
|
/* static hf_register_info hf[] = {
|
|
|
|
{ &variable,
|
|
|
|
{ "Name", "gre.abbreviation", TYPE, VALS_POINTER }},
|
|
|
|
};*/
|
1999-11-16 11:44:20 +00:00
|
|
|
static gint *ett[] = {
|
|
|
|
&ett_gre,
|
1999-12-08 21:38:14 +00:00
|
|
|
&ett_gre_flags,
|
1999-11-16 11:44:20 +00:00
|
|
|
};
|
1999-07-29 05:47:07 +00:00
|
|
|
|
|
|
|
proto_gre = proto_register_protocol("Generic Routing Encapsulation", "gre");
|
|
|
|
/* proto_register_field_array(proto_gre, hf, array_length(hf));*/
|
1999-11-16 11:44:20 +00:00
|
|
|
proto_register_subtree_array(ett, array_length(ett));
|
1999-07-29 05:47:07 +00:00
|
|
|
}
|