Fix a bunch of dissectors to use "pi.captured_len" rather than
[obnox/wireshark/wip.git] / packet-gre.c
1 /* packet-gre.c
2  * Routines for the Generic Routing Encapsulation (GRE) protocol
3  * Brad Robel-Forrest <brad.robel-forrest@watchguard.com>
4  *
5  * $Id: packet-gre.c,v 1.14 2000/01/07 22:05:31 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * 
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.
16  * 
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.
21  * 
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.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
37 #endif
38 #include <glib.h>
39 #include "packet.h"
40
41 static int proto_gre = -1;
42 static int hf_gre_proto = -1;
43
44 static gint ett_gre = -1;
45 static gint ett_gre_flags = -1;
46
47 /* bit positions for flags in header */
48 #define GH_B_C          0x8000
49 #define GH_B_R          0x4000
50 #define GH_B_K          0x2000
51 #define GH_B_S          0x1000
52 #define GH_B_s          0x0800
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
58
59 #define GRE_PPP         0x880B
60 #define GRE_IP          0x0800
61 #define GRE_WCCP        0x883E
62
63 static void add_flags_and_ver(proto_tree *, guint16, int, int);
64
65 static const value_string typevals[] = {
66         { GRE_PPP,  "PPP" },
67         { GRE_IP,   "IP" },
68         { GRE_WCCP, "WCCP"},
69         { 0,        NULL  }
70 };
71
72 void
73 dissect_gre(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
74   
75   guint16       flags_and_ver = pntohs(pd + offset);
76   guint16       type          = pntohs(pd + offset + sizeof(flags_and_ver));
77   guint16       sre_af;
78   guint8        sre_length;
79
80   if (check_col(fd, COL_PROTOCOL))
81     col_add_str(fd, COL_PROTOCOL, "GRE");
82         
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)"));
86   }
87                 
88   if (IS_DATA_IN_FRAME(offset) && tree) {
89     gboolean            is_ppp = FALSE;
90     gboolean            is_wccp2 = FALSE;
91     proto_item *        ti;
92     proto_tree *        gre_tree;
93     guint               len = 4;
94
95     if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R)
96       len += 4;
97     if (flags_and_ver & GH_B_K)
98       len += 4;
99     if (flags_and_ver & GH_B_S)
100       len += 4;
101     switch (type) {
102
103     case GRE_PPP:
104       if (flags_and_ver & GH_P_A)
105         len += 4;
106       is_ppp = TRUE;
107       break;
108
109     case GRE_WCCP:
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
113          WCCP2. */
114       if ((pd[offset + sizeof(flags_and_ver) + sizeof(type)] & 0xF0) != 0x40) {
115         len += 4;
116         is_wccp2 = TRUE;
117       }
118       break;
119     }
120
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);
126
127     offset += sizeof(flags_and_ver);
128
129     proto_tree_add_item(gre_tree, hf_gre_proto, offset, sizeof(type), type);
130     offset += sizeof(type);
131
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);
137     }
138     
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);
144     }
145
146     if (flags_and_ver & GH_B_K) {
147       if (is_ppp) {
148         guint16 paylen;
149         guint16 callid;
150         
151         paylen = pntohs(pd + offset);
152         proto_tree_add_text(gre_tree, offset, sizeof(paylen),
153                             "Payload length: %u", paylen);
154         offset += sizeof(paylen);
155
156         callid = pntohs(pd + offset);
157         proto_tree_add_text(gre_tree, offset, sizeof(callid),
158                             "Call ID: %u", callid);
159         offset += sizeof(callid);
160       }
161       else {
162         guint32 key = pntohl(pd + offset);
163         proto_tree_add_text(gre_tree, offset, sizeof(key),
164                             "Key: %u", key);
165         offset += sizeof(key);
166       }
167     }
168     
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);
174     }
175
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);
181     }
182
183     if (flags_and_ver & GH_B_R) {
184       for (;;) {
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)
196           break;
197         offset += sre_length;
198       }
199     }
200
201     switch (type) {
202       case GRE_PPP:
203         dissect_payload_ppp(pd, offset, fd, tree);
204         break;
205       case GRE_IP:
206         dissect_ip(pd, offset, fd, tree);
207         break;
208       case GRE_WCCP:
209         if (is_wccp2) {
210           proto_tree_add_text(gre_tree, offset, sizeof(guint32), "WCCPv2 Data");
211           offset += 4;
212         }
213         dissect_ip(pd, offset, fd, tree);
214         break;
215       default:
216         dissect_data(pd, offset, fd, gre_tree);
217         break;
218     }
219   }
220 }
221
222 static void
223 add_flags_and_ver(proto_tree *tree, guint16 flags_and_ver, int offset, int is_ppp) {
224
225   proto_item *  ti;
226   proto_tree *  fv_tree;
227   int           nbits = sizeof(flags_and_ver) * 8;
228   
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);
232   
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,
241                                               "Key", "No key"));
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"));
251   if (is_ppp) {
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,
257                                                 "Flags: %u"));
258   }
259   else {
260     proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
261                         decode_numeric_bitfield(flags_and_ver, GH_R_FLAGS, nbits,
262                                                 "Flags: %u"));
263   }
264
265   proto_tree_add_text(fv_tree, offset, sizeof(flags_and_ver), "%s",
266                       decode_numeric_bitfield(flags_and_ver, GH_B_VER, nbits,
267                                               "Version: %u"));
268  }
269  
270 void
271 proto_register_gre(void)
272 {
273         static hf_register_info hf[] = {
274                 { &hf_gre_proto,
275                         { "Protocol Type", "gre.proto", FT_UINT16, BASE_HEX, VALS(typevals), 0x0,
276                                 "The protocol that is GRE encapsulated"}
277                 },
278         };
279         static gint *ett[] = {
280                 &ett_gre,
281                 &ett_gre_flags,
282         };
283
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));
287 }