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