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