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