As the gtk2 directory is no longer needed (GTK1 and 2 are using the same sources...
[obnox/wireshark/wip.git] / epan / dissectors / packet-enc.c
1 /*
2  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
3  *
4  * $Id$
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <string.h>
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include "etypes.h"
34 #include <epan/resolv.h>
35 #include "packet-ip.h"
36 #include "packet-ipv6.h"
37
38 #ifndef offsetof
39 /* Can't trust stddef.h to be there for us */
40 # define offsetof(type, member) ((size_t)(&((type *)0)->member))
41 #endif
42
43 /* The header in OpenBSD Encapsulating Interface files. */
44
45 struct enchdr {
46   guint32 af;
47   guint32 spi;
48   guint32 flags;
49 };
50 #define ENC_HDRLEN    sizeof(struct enchdr)
51
52 # define BSD_ENC_INET    2
53 # define BSD_ENC_INET6   24
54
55 # define BSD_ENC_M_CONF          0x0400  /* payload encrypted */
56 # define BSD_ENC_M_AUTH          0x0800  /* payload authenticated */
57 # define BSD_ENC_M_COMP          0x1000  /* payload compressed */
58 # define BSD_ENC_M_AUTH_AH       0x2000  /* header authenticated */
59
60 static dissector_handle_t  data_handle, ip_handle, ipv6_handle;
61
62 /* header fields */
63 static unsigned int proto_enc = -1;
64 static unsigned int hf_enc_af = -1;
65 static unsigned int hf_enc_spi = -1;
66 static unsigned int hf_enc_flags = -1;
67
68 static gint ett_enc = -1;
69
70 void
71 capture_enc(const guchar *pd, int offset, int len, packet_counts *ld)
72 {
73   struct enchdr ench;
74
75   if (!BYTES_ARE_IN_FRAME(offset, len, (int)ENC_HDRLEN)) {
76     ld->other++;
77     return;
78   }
79
80   offset += ENC_HDRLEN;
81
82   /* Copy out the enc header to insure alignment */
83   memcpy(&ench, pd, sizeof(ench));
84   ench.af = g_ntohl(ench.af);
85
86   switch (ench.af) {
87
88   case BSD_ENC_INET:
89     capture_ip(pd, offset, len, ld);
90     break;
91
92 #ifdef notyet
93   case BSD_ENC_INET6:
94     capture_ipv6(pd, offset, len, ld);
95     break;
96 #endif
97
98   default:
99     ld->other++;
100     break;
101   }
102 }
103
104 static const value_string af_vals[] = {
105   { BSD_ENC_INET,  "IPv4" },
106   { BSD_ENC_INET6, "IPv6" },
107   { 0,            NULL }
108 };
109
110 static void
111 dissect_enc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
112 {
113   struct enchdr ench;
114   tvbuff_t *next_tvb;
115   proto_tree *enc_tree;
116   proto_item *ti;
117
118   if (check_col(pinfo->cinfo, COL_PROTOCOL))
119     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ENC");
120
121   /* Copy out the enc header to insure alignment */
122   tvb_memcpy(tvb, (guint8 *)&ench, 0, sizeof(ench));
123
124   /* Byteswap the header now */
125   ench.spi = g_ntohl(ench.spi);
126   /* ench.af = g_ntohl(ench.af); */
127   /* ench.flags = g_ntohl(ench.flags); */
128
129   if (tree) {
130     ti = proto_tree_add_protocol_format(tree, proto_enc, tvb, 0,
131              ENC_HDRLEN,
132              "Enc %s, SPI 0x%8.8x, %s%s%s%s",
133              val_to_str(ench.af, af_vals, "unknown (%u)"),
134              ench.spi,
135              ench.flags ? "" : "unprotected",
136              ench.flags & BSD_ENC_M_AUTH ? "authentic" : "",
137              (ench.flags & (BSD_ENC_M_AUTH|BSD_ENC_M_CONF)) ==
138                 (BSD_ENC_M_AUTH|BSD_ENC_M_CONF) ? ", " : "",
139              ench.flags & BSD_ENC_M_CONF ? "confidential" : ""
140              );
141     enc_tree = proto_item_add_subtree(ti, ett_enc);
142
143     proto_tree_add_uint(enc_tree, hf_enc_af, tvb,
144              offsetof(struct enchdr, af), sizeof(ench.af),
145              ench.af);
146     proto_tree_add_uint(enc_tree, hf_enc_spi, tvb,
147              offsetof(struct enchdr, spi), sizeof(ench.spi),
148              ench.spi);
149     proto_tree_add_uint(enc_tree, hf_enc_flags, tvb,
150              offsetof(struct enchdr, flags), sizeof(ench.flags),
151              ench.flags);
152   }
153
154   /* Set the tvbuff for the payload after the header */
155   next_tvb = tvb_new_subset(tvb, ENC_HDRLEN, -1, -1);
156
157   switch (ench.af) {
158
159   case BSD_ENC_INET:
160     call_dissector(ip_handle, next_tvb, pinfo, tree);
161     break;
162
163   case BSD_ENC_INET6:
164     call_dissector(ipv6_handle, next_tvb, pinfo, tree);
165     break;
166
167   default:
168     call_dissector(data_handle, next_tvb, pinfo, tree);
169     break;
170   }
171 }
172
173 void
174 proto_register_enc(void)
175 {
176   static hf_register_info hf[] = {
177     { &hf_enc_af,
178       { "Address Family", "enc.af", FT_UINT32, BASE_DEC, VALS(af_vals), 0x0,
179         "Protocol (IPv4 vs IPv6)", HFILL }},
180     { &hf_enc_spi,
181       { "SPI", "enc.spi", FT_UINT32, BASE_HEX, NULL, 0x0,
182         "Security Parameter Index", HFILL }},
183     { &hf_enc_flags,
184       { "Flags", "enc.flags", FT_UINT32, BASE_HEX, NULL, 0x0,
185         "ENC flags", HFILL }},
186   };
187   static gint *ett[] = { &ett_enc };
188
189   proto_enc = proto_register_protocol("OpenBSD Encapsulating device",
190                                       "ENC", "enc");
191   proto_register_field_array(proto_enc, hf, array_length(hf));
192   proto_register_subtree_array(ett, array_length(ett));
193 }
194
195 void
196 proto_reg_handoff_enc(void)
197 {
198   dissector_handle_t enc_handle;
199
200   ip_handle = find_dissector("ip");
201   ipv6_handle = find_dissector("ipv6");
202   data_handle = find_dissector("data");
203
204   enc_handle = create_dissector_handle(dissect_enc, proto_enc);
205   dissector_add("wtap_encap", WTAP_ENCAP_ENC, enc_handle);
206 }