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