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