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