There's no need to keep a "FILE *" for the file being printed to in a
[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.54 2003/06/10 05:38:52 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include "packet-wccp.h"
33 #include "in_cksum.h"
34 #include "etypes.h"
35 #include "greproto.h"
36 #include "ipproto.h"
37 #include "llcsaps.h"
38
39 /*
40  * See RFC 1701 "Generic Routing Encapsulation (GRE)", RFC 1702
41  * "Generic Routing Encapsulation over IPv4 networks", RFC 2637
42  * "Point-to-Point Tunneling Protocol (PPTP)", RFC 2784 "Generic
43  * Routing Encapsulation (GRE)", and RFC 2890 "Key and Sequence
44  * Number Extensions to GRE".
45  */
46
47 static int proto_gre = -1;
48 static int hf_gre_proto = -1;
49
50 static gint ett_gre = -1;
51 static gint ett_gre_flags = -1;
52 static gint ett_gre_wccp2_redirect_header = -1;
53
54 static dissector_table_t gre_dissector_table;
55 static dissector_handle_t data_handle;
56
57 /* bit positions for flags in header */
58 #define GH_B_C          0x8000
59 #define GH_B_R          0x4000
60 #define GH_B_K          0x2000
61 #define GH_B_S          0x1000
62 #define GH_B_s          0x0800
63 #define GH_B_RECUR      0x0700
64 #define GH_P_A          0x0080  /* only in special PPTPized GRE header */
65 #define GH_P_FLAGS      0x0078  /* only in special PPTPized GRE header */
66 #define GH_R_FLAGS      0x00F8
67 #define GH_B_VER        0x0007
68
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 *);
71
72 static const value_string typevals[] = {
73         { ETHERTYPE_PPP,       "PPP" },
74         { ETHERTYPE_IP,        "IP" },
75         { SAP_OSINL5,          "OSI"},
76         { GRE_WCCP,            "WCCP"},
77         { ETHERTYPE_IPX,       "IPX"},
78         { ETHERTYPE_ETHBRIDGE, "Transparent Ethernet bridging" },
79         { GRE_FR,              "Frame Relay"},
80         { 0,                   NULL }
81 };
82
83 static void
84 dissect_gre(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
85 {
86   int           offset = 0;
87   guint16       flags_and_ver;
88   guint16       type;
89   gboolean      is_ppp = FALSE;
90   gboolean      is_wccp2 = FALSE;
91   guint         len = 4;
92   proto_item    *ti;
93   proto_tree    *gre_tree = NULL;
94   guint16       sre_af;
95   guint8        sre_length;
96   tvbuff_t      *next_tvb;
97
98   flags_and_ver = tvb_get_ntohs(tvb, offset);
99   type = tvb_get_ntohs(tvb, offset + sizeof(flags_and_ver));
100
101   if (check_col(pinfo->cinfo, COL_PROTOCOL))
102     col_set_str(pinfo->cinfo, COL_PROTOCOL, "GRE");
103
104   if (check_col(pinfo->cinfo, COL_INFO)) {
105     col_add_fstr(pinfo->cinfo, COL_INFO, "Encapsulated %s",
106                  val_to_str(type, typevals, "0x%04X (unknown)"));
107   }
108
109   if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R)
110     len += 4;
111   if (flags_and_ver & GH_B_K)
112     len += 4;
113   if (flags_and_ver & GH_B_S)
114     len += 4;
115   switch (type) {
116
117   case ETHERTYPE_PPP:
118     if (flags_and_ver & GH_P_A)
119       len += 4;
120     is_ppp = TRUE;
121     break;
122
123   case GRE_WCCP:
124     /* WCCP2 puts an extra 4 octets into the header, but uses the same
125        encapsulation type; if it looks as if the first octet of the packet
126        isn't the beginning of an IPv4 header, assume it's WCCP2. */
127     if ((tvb_get_guint8(tvb, offset + sizeof(flags_and_ver) + sizeof(type)) & 0xF0) != 0x40) {
128       len += 4;
129       is_wccp2 = TRUE;
130     }
131     break;
132   }
133
134   if (tree) {
135     ti = proto_tree_add_protocol_format(tree, proto_gre, tvb, offset, len,
136       "Generic Routing Encapsulation (%s)",
137       val_to_str(type, typevals, "0x%04X - unknown"));
138     gre_tree = proto_item_add_subtree(ti, ett_gre);
139     add_flags_and_ver(gre_tree, flags_and_ver, tvb, offset, is_ppp);
140   }
141   offset += sizeof(flags_and_ver);
142
143   if (tree) {
144     proto_tree_add_uint(gre_tree, hf_gre_proto, tvb, offset, sizeof(type), type);
145   }
146   offset += sizeof(type);
147
148   if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
149     if (tree) {
150       guint length, reported_length;
151       vec_t cksum_vec[1];
152       guint16 cksum, computed_cksum;
153
154       cksum = tvb_get_ntohs(tvb, offset);
155       length = tvb_length(tvb);
156       reported_length = tvb_reported_length(tvb);
157       if ((flags_and_ver & GH_B_C) && !pinfo->fragmented
158                 && length >= reported_length) {
159         /* The Checksum Present bit is set, and the packet isn't part of a
160            fragmented datagram and isn't truncated, so we can checksum it. */
161
162         cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, reported_length);
163         cksum_vec[0].len = reported_length;
164         computed_cksum = in_cksum(cksum_vec, 1);
165         if (computed_cksum == 0) {
166           proto_tree_add_text(gre_tree, tvb, offset, 2,
167                         "Checksum: 0x%04x (correct)", cksum);
168         } else {
169           proto_tree_add_text(gre_tree, tvb, offset, 2,
170                         "Checksum: 0x%04x (incorrect, should be 0x%04x)",
171                         cksum, in_cksum_shouldbe(cksum, computed_cksum));
172         }
173       } else {
174         proto_tree_add_text(gre_tree, tvb, offset, 2,
175                           "Checksum: 0x%04x", cksum);
176       }
177     }
178     offset += 2;
179   }
180
181   if (flags_and_ver & GH_B_C || flags_and_ver & GH_B_R) {
182     if (tree) {
183       proto_tree_add_text(gre_tree, tvb, offset, 2,
184                           "Offset: %u", tvb_get_ntohs(tvb, offset));
185     }
186     offset += 2;
187   }
188
189   if (flags_and_ver & GH_B_K) {
190     if (is_ppp) {
191       if (tree) {
192         proto_tree_add_text(gre_tree, tvb, offset, 2,
193                             "Payload length: %u", tvb_get_ntohs(tvb, offset));
194       }
195       offset += 2;
196       if (tree) {
197         proto_tree_add_text(gre_tree, tvb, offset, 2,
198                             "Call ID: %u", tvb_get_ntohs(tvb, offset));
199       }
200       offset += 2;
201     }
202     else {
203       if (tree) {
204         proto_tree_add_text(gre_tree, tvb, offset, 4,
205                             "Key: %u", tvb_get_ntohl(tvb, offset));
206       }
207       offset += 4;
208     }
209   }
210
211   if (flags_and_ver & GH_B_S) {
212     if (tree) {
213       proto_tree_add_text(gre_tree, tvb, offset, 4,
214                           "Sequence number: %u", tvb_get_ntohl(tvb, offset));
215     }
216     offset += 4;
217   }
218
219   if (is_ppp && flags_and_ver & GH_P_A) {
220     if (tree) {
221       proto_tree_add_text(gre_tree, tvb, offset, 4,
222                           "Acknowledgement number: %u", tvb_get_ntohl(tvb, offset));
223     }
224     offset += 4;
225   }
226
227   if (flags_and_ver & GH_B_R) {
228     for (;;) {
229       sre_af = tvb_get_ntohs(tvb, offset);
230       if (tree) {
231         proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint16),
232                           "Address family: %u", sre_af);
233       }
234       offset += sizeof(guint16);
235       if (tree) {
236         proto_tree_add_text(gre_tree, tvb, offset, 1,
237                           "SRE offset: %u", tvb_get_guint8(tvb, offset));
238       }
239       offset += sizeof(guint8);
240       sre_length = tvb_get_guint8(tvb, offset);
241       if (tree) {
242         proto_tree_add_text(gre_tree, tvb, offset, sizeof(guint8),
243                           "SRE length: %u", sre_length);
244       }
245       offset += sizeof(guint8);
246       if (sre_af == 0 && sre_length == 0)
247         break;
248       offset += sre_length;
249     }
250   }
251
252   if (type == GRE_WCCP) {
253     if (is_wccp2) {
254       if (tree)
255         dissect_gre_wccp2_redirect_header(tvb, offset, gre_tree);
256       offset += 4;
257     }
258   }
259
260   /* If the S bit is not set, this packet might not have a payload, so
261      check whether there's any data left, first.
262
263      XXX - the S bit isn't in RFC 2784, which deprecates that bit
264      and some other bits in RFC 1701 and says that they should be
265      zero for RFC 2784-compliant GRE; as such, the absence of the
266      S bit doesn't necessarily mean there's no payload.  */
267   if (!(flags_and_ver & GH_B_S)) {
268     if (tvb_reported_length_remaining(tvb, offset) <= 0)
269       return;   /* no payload */
270   }
271   next_tvb = tvb_new_subset(tvb, offset, -1, -1);
272   if (!dissector_try_port(gre_dissector_table, type, next_tvb, pinfo, tree))
273     call_dissector(data_handle,next_tvb, pinfo, gre_tree);
274 }
275
276 static void
277 add_flags_and_ver(proto_tree *tree, guint16 flags_and_ver, tvbuff_t *tvb,
278     int offset, int is_ppp)
279 {
280   proto_item *  ti;
281   proto_tree *  fv_tree;
282   int           nbits = sizeof(flags_and_ver) * 8;
283
284   ti = proto_tree_add_text(tree, tvb, offset, 2,
285                            "Flags and version: %#04x", flags_and_ver);
286   fv_tree = proto_item_add_subtree(ti, ett_gre_flags);
287
288   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
289                       decode_boolean_bitfield(flags_and_ver, GH_B_C, nbits,
290                                               "Checksum", "No checksum"));
291   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
292                       decode_boolean_bitfield(flags_and_ver, GH_B_R, nbits,
293                                               "Routing", "No routing"));
294   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
295                       decode_boolean_bitfield(flags_and_ver, GH_B_K, nbits,
296                                               "Key", "No key"));
297   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
298                       decode_boolean_bitfield(flags_and_ver, GH_B_S, nbits,
299                                               "Sequence number", "No sequence number"));
300   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
301                       decode_boolean_bitfield(flags_and_ver, GH_B_s, nbits,
302                                               "Strict source route", "No strict source route"));
303   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
304                       decode_numeric_bitfield(flags_and_ver, GH_B_RECUR, nbits,
305                                               "Recursion control: %u"));
306   if (is_ppp) {
307     proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
308                         decode_boolean_bitfield(flags_and_ver, GH_P_A, nbits,
309                                                 "Acknowledgment number", "No acknowledgment number"));
310     proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
311                         decode_numeric_bitfield(flags_and_ver, GH_P_FLAGS, nbits,
312                                                 "Flags: %u"));
313   }
314   else {
315     proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
316                         decode_numeric_bitfield(flags_and_ver, GH_R_FLAGS, nbits,
317                                                 "Flags: %u"));
318   }
319
320   proto_tree_add_text(fv_tree, tvb, offset, sizeof(flags_and_ver), "%s",
321                       decode_numeric_bitfield(flags_and_ver, GH_B_VER, nbits,
322                                               "Version: %u"));
323  }
324
325 static void
326 dissect_gre_wccp2_redirect_header(tvbuff_t *tvb, int offset, proto_tree *tree)
327 {
328   proto_item *  ti;
329   proto_tree *  rh_tree;
330   guint8        rh_flags;
331
332   ti = proto_tree_add_text(tree, tvb, offset, 4, "Redirect header");
333   rh_tree = proto_item_add_subtree(ti, ett_gre_wccp2_redirect_header);
334
335   rh_flags = tvb_get_guint8(tvb, offset);
336   proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
337                       decode_boolean_bitfield(rh_flags, 0x80, 8,
338                                       "Dynamic service", "Well-known service"));
339   proto_tree_add_text(rh_tree, tvb, offset, 1, "%s",
340                       decode_boolean_bitfield(rh_flags, 0x40, 8,
341                               "Alternative bucket used", "Alternative bucket not used"));
342
343   proto_tree_add_text(rh_tree, tvb, offset + 1, 1, "Service ID: %s",
344       val_to_str(tvb_get_guint8(tvb, offset + 1), service_id_vals, "Unknown (0x%02X)"));
345   if (rh_flags & 0x40)
346     proto_tree_add_text(rh_tree, tvb, offset + 2, 1, "Alternative bucket index: %u",
347                         tvb_get_guint8(tvb, offset + 2));
348   proto_tree_add_text(rh_tree, tvb, offset + 3, 1, "Primary bucket index: %u",
349                         tvb_get_guint8(tvb, offset + 3));
350 }
351
352 void
353 proto_register_gre(void)
354 {
355         static hf_register_info hf[] = {
356                 { &hf_gre_proto,
357                         { "Protocol Type", "gre.proto", FT_UINT16, BASE_HEX, VALS(typevals), 0x0,
358                                 "The protocol that is GRE encapsulated", HFILL }
359                 },
360         };
361         static gint *ett[] = {
362                 &ett_gre,
363                 &ett_gre_flags,
364                 &ett_gre_wccp2_redirect_header,
365         };
366
367         proto_gre = proto_register_protocol("Generic Routing Encapsulation",
368             "GRE", "gre");
369         proto_register_field_array(proto_gre, hf, array_length(hf));
370         proto_register_subtree_array(ett, array_length(ett));
371
372         /* subdissector code */
373         gre_dissector_table = register_dissector_table("gre.proto",
374             "GRE protocol type", FT_UINT16, BASE_HEX);
375 }
376
377 void
378 proto_reg_handoff_gre(void)
379 {
380         dissector_handle_t gre_handle;
381
382         gre_handle = create_dissector_handle(dissect_gre, proto_gre);
383         dissector_add("ip.proto", IP_PROTO_GRE, gre_handle);
384         data_handle = find_dissector("data");
385 }