Get rid of an unnecessary include.
[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.43 2001/04/23 18:11:01 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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 #include "packet-wccp.h"
41 #include "in_cksum.h"
42 #include "etypes.h"
43 #include "greproto.h"
44 #include "ipproto.h"
45
46 static int proto_gre = -1;
47 static int hf_gre_proto = -1;
48
49 static gint ett_gre = -1;
50 static gint ett_gre_flags = -1;
51 static gint ett_gre_wccp2_redirect_header = -1;
52
53 static dissector_table_t gre_dissector_table;
54
55 /* bit positions for flags in header */
56 #define GH_B_C          0x8000
57 #define GH_B_R          0x4000
58 #define GH_B_K          0x2000
59 #define GH_B_S          0x1000
60 #define GH_B_s          0x0800
61 #define GH_B_RECUR      0x0700
62 #define GH_P_A          0x0080  /* only in special PPTPized GRE header */
63 #define GH_P_FLAGS      0x0078  /* only in special PPTPized GRE header */
64 #define GH_R_FLAGS      0x00F8
65 #define GH_B_VER        0x0007
66
67 static void add_flags_and_ver(proto_tree *, guint16, tvbuff_t *, int, int);
68 static void dissect_gre_wccp2_redirect_header(tvbuff_t *, int, proto_tree *);
69
70 static const value_string typevals[] = {
71         { ETHERTYPE_PPP,       "PPP" },
72         { ETHERTYPE_IP,        "IP" },
73         { GRE_WCCP,            "WCCP"},
74         { ETHERTYPE_IPX,       "IPX"},
75         { ETHERTYPE_ETHBRIDGE, "Transparent Ethernet bridging" },
76         { GRE_FR,              "Frame Relay"},
77         { 0,                   NULL }
78 };
79
80 static void
81 dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
82 {
83   int           offset = 0;
84   guint16       flags_and_ver;
85   guint16       type;
86   gboolean      is_ppp = FALSE;
87   gboolean      is_wccp2 = FALSE;
88   guint         len = 4;
89   proto_item    *ti;
90   proto_tree    *gre_tree = NULL;
91   guint16       sre_af;
92   guint8        sre_length;
93   tvbuff_t      *next_tvb;
94
95   flags_and_ver = tvb_get_ntohs(tvb, offset);
96   type = tvb_get_ntohs(tvb, offset + sizeof(flags_and_ver));
97
98   if (check_col(pinfo->fd, COL_PROTOCOL))
99     col_set_str(pinfo->fd, COL_PROTOCOL, "GRE");
100
101   if (check_col(pinfo->fd, COL_INFO)) {
102     col_add_fstr(pinfo->fd, COL_INFO, "Encapsulated %s",
103                  val_to_str(type, typevals, "0x%04X (unknown)"));
104   }
105
106   if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R)
107     len += 4;
108   if (flags_and_ver & GH_B_K)
109     len += 4;
110   if (flags_and_ver & GH_B_S)
111     len += 4;
112   switch (type) {
113
114   case ETHERTYPE_PPP:
115     if (flags_and_ver & GH_P_A)
116       len += 4;
117     is_ppp = TRUE;
118     break;
119
120   case GRE_WCCP:
121     /* WCCP2 puts an extra 4 octets into the header, but uses the same
122        encapsulation type; if it looks as if the first octet of the packet
123        isn't the beginning of an IPv4 header, assume it's WCCP2. */
124     if ((tvb_get_guint8(tvb, offset + sizeof(flags_and_ver) + sizeof(type)) & 0xF0) != 0x40) {
125       len += 4;
126       is_wccp2 = TRUE;
127     }
128     break;
129   }
130
131   if (tree) {
132     ti = proto_tree_add_protocol_format(tree, proto_gre, tvb, offset, len,
133       "Generic Routing Encapsulation (%s)",
134       val_to_str(type, typevals, "0x%04X - unknown"));
135     gre_tree = proto_item_add_subtree(ti, ett_gre);
136     add_flags_and_ver(gre_tree, flags_and_ver, tvb, offset, is_ppp);
137   }
138   offset += sizeof(flags_and_ver);
139
140   if (tree) {
141     proto_tree_add_uint(gre_tree, hf_gre_proto, tvb, offset, sizeof(type), type);
142   }
143   offset += sizeof(type);
144
145   if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
146     if (tree) {
147       guint length, reported_length;
148       vec_t cksum_vec[1];
149       guint16 cksum, computed_cksum;
150
151       cksum = tvb_get_ntohs(tvb, offset);
152       length = tvb_length(tvb);
153       reported_length = tvb_reported_length(tvb);
154       if ((flags_and_ver & GH_B_C) && !pinfo->fragmented
155                 && length >= reported_length) {
156         /* The Checksum Present bit is set, and the packet isn't part of a
157            fragmented datagram and isn't truncated, so we can checksum it. */
158
159         cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, reported_length);
160         cksum_vec[0].len = reported_length;
161         computed_cksum = in_cksum(cksum_vec, 1);
162         if (computed_cksum == 0) {
163           proto_tree_add_text(gre_tree, tvb, offset, 2,
164                         "Checksum: 0x%04x (correct)", cksum);
165         } else {
166           proto_tree_add_text(gre_tree, tvb, offset, 2,
167                         "Checksum: 0x%04x (incorrect, should be 0x%04x)",
168                         cksum, in_cksum_shouldbe(cksum, computed_cksum));
169         }
170       } else {
171         proto_tree_add_text(gre_tree, tvb, offset, 2,
172                           "Checksum: 0x%04x", cksum);
173       }
174     }
175     offset += 2;
176   }
177
178   if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
179     if (tree) {
180       proto_tree_add_text(gre_tree, tvb, offset, 2,
181                           "Offset: %u", tvb_get_ntohs(tvb, offset));
182     }
183     offset += 2;
184   }
185
186   if (flags_and_ver & GH_B_K) {
187     if (is_ppp) {
188       if (tree) {
189         proto_tree_add_text(gre_tree, tvb, offset, 2,
190                             "Payload length: %u", tvb_get_ntohs(tvb, offset));
191       }
192       offset += 2;
193       if (tree) {
194         proto_tree_add_text(gre_tree, tvb, offset, 2,
195                             "Call ID: %u", tvb_get_ntohs(tvb, offset));
196       }
197       offset += 2;
198     }
199     else {
200       if (tree) {
201         proto_tree_add_text(gre_tree, tvb, offset, 4,
202                             "Key: %u", tvb_get_ntohl(tvb, offset));
203       }
204       offset += 4;
205     }
206   }
207     
208   if (flags_and_ver & GH_B_S) {
209     if (tree) {
210       proto_tree_add_text(gre_tree, tvb, offset, 4,
211                           "Sequence number: %u", tvb_get_ntohl(tvb, offset));
212     }
213     offset += 4;
214   }
215
216   if (is_ppp && flags_and_ver & GH_P_A) {
217     if (tree) {
218       proto_tree_add_text(gre_tree, tvb, offset, 4,
219                           "Acknowledgement number: %u", tvb_get_ntohl(tvb, offset));
220     }
221     offset += 4;
222   }
223
224   if (flags_and_ver & GH_B_R) {
225     for (;;) {
226       sre_af = tvb_get_ntohs(tvb, offset);
227       if (tree) {
228         proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint16),
229                           "Address family: %u", sre_af);
230       }
231       offset += sizeof(guint16);
232       if (tree) {
233         proto_tree_add_text(gre_tree, tvb, offset, 1,
234                           "SRE offset: %u", tvb_get_guint8(tvb, offset));
235       }
236       offset += sizeof(guint8);
237       sre_length = tvb_get_guint8(tvb, offset);
238       if (tree) {
239         proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint8),
240                           "SRE length: %u", sre_length);
241       }
242       offset += sizeof(guint8);
243       if (sre_af == 0 && sre_length == 0)
244         break;
245       offset += sre_length;
246     }
247   }
248
249   if (type == GRE_WCCP) {
250     if (is_wccp2) {
251       if (tree)
252         dissect_gre_wccp2_redirect_header(tvb, offset, gre_tree);
253       offset += 4;
254     }
255   }
256   next_tvb = tvb_new_subset(tvb, offset, -1, -1);
257   if (!dissector_try_port(gre_dissector_table, type, next_tvb, pinfo, tree))
258     dissect_data(next_tvb, 0, pinfo, gre_tree);
259 }
260
261 static void
262 add_flags_and_ver(proto_tree *tree, guint16 flags_and_ver, tvbuff_t *tvb,
263     int offset, int is_ppp)
264 {
265   proto_item *  ti;
266   proto_tree *  fv_tree;
267   int           nbits = sizeof(flags_and_ver) * 8;
268   
269   ti = proto_tree_add_text(tree, tvb, offset, 2, 
270                            "Flags and version: %#04x", flags_and_ver);
271   fv_tree = proto_item_add_subtree(ti, ett_gre_flags);
272   
273   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
274                       decode_boolean_bitfield(flags_and_ver, GH_B_C, nbits,
275                                               "Checksum", "No checksum"));
276   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
277                       decode_boolean_bitfield(flags_and_ver, GH_B_R, nbits,
278                                               "Routing", "No routing"));
279   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
280                       decode_boolean_bitfield(flags_and_ver, GH_B_K, nbits,
281                                               "Key", "No key"));
282   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
283                       decode_boolean_bitfield(flags_and_ver, GH_B_S, nbits,
284                                               "Sequence number", "No sequence number"));
285   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
286                       decode_boolean_bitfield(flags_and_ver, GH_B_s, nbits,
287                                               "Strict source route", "No strict source route"));
288   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
289                       decode_numeric_bitfield(flags_and_ver, GH_B_RECUR, nbits,
290                                               "Recursion control: %u"));
291   if (is_ppp) {
292     proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
293                         decode_boolean_bitfield(flags_and_ver, GH_P_A, nbits,
294                                                 "Acknowledgment number", "No acknowledgment number"));
295     proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
296                         decode_numeric_bitfield(flags_and_ver, GH_P_FLAGS, nbits,
297                                                 "Flags: %u"));
298   }
299   else {
300     proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
301                         decode_numeric_bitfield(flags_and_ver, GH_R_FLAGS, nbits,
302                                                 "Flags: %u"));
303   }
304
305   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
306                       decode_numeric_bitfield(flags_and_ver, GH_B_VER, nbits,
307                                               "Version: %u"));
308  }
309
310 static void
311 dissect_gre_wccp2_redirect_header(tvbuff_t *tvb, int offset, proto_tree *tree)
312 {
313   proto_item *  ti;
314   proto_tree *  rh_tree;
315   guint8        rh_flags;
316   
317   ti = proto_tree_add_text(tree, tvb, offset, 4, "Redirect header");
318   rh_tree = proto_item_add_subtree(ti, ett_gre_wccp2_redirect_header);
319
320   rh_flags = tvb_get_guint8(tvb, offset);
321   proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
322                       decode_boolean_bitfield(rh_flags, 0x80, 8,
323                                       "Dynamic service", "Well-known service"));
324   proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
325                       decode_boolean_bitfield(rh_flags, 0x40, 8,
326                               "Alternative bucket used", "Alternative bucket not used"));
327
328   proto_tree_add_text(rh_tree, tvb, offset + 1, 1, "Service ID: %s",
329       val_to_str(tvb_get_guint8(tvb, offset + 1), service_id_vals, "Unknown (0x%02X)"));
330   if (rh_flags & 0x40)
331     proto_tree_add_text(rh_tree, tvb, offset + 2, 1, "Alternative bucket index: %u",
332                         tvb_get_guint8(tvb, offset + 2));
333   proto_tree_add_text(rh_tree, tvb, offset + 3, 1, "Primary bucket index: %u",
334                         tvb_get_guint8(tvb, offset + 3));
335 }
336  
337 void
338 proto_register_gre(void)
339 {
340         static hf_register_info hf[] = {
341                 { &hf_gre_proto,
342                         { "Protocol Type", "gre.proto", FT_UINT16, BASE_HEX, VALS(typevals), 0x0,
343                                 "The protocol that is GRE encapsulated"}
344                 },
345         };
346         static gint *ett[] = {
347                 &ett_gre,
348                 &ett_gre_flags,
349                 &ett_gre_wccp2_redirect_header,
350         };
351
352         proto_gre = proto_register_protocol("Generic Routing Encapsulation",
353             "GRE", "gre");
354         proto_register_field_array(proto_gre, hf, array_length(hf));
355         proto_register_subtree_array(ett, array_length(ett));
356
357         /* subdissector code */
358         gre_dissector_table = register_dissector_table("gre.proto");
359 }
360
361 void
362 proto_reg_handoff_gre(void)
363 {
364         dissector_add("ip.proto", IP_PROTO_GRE, dissect_gre, proto_gre);
365 }