MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-ipsec-tcp.c
blobb42caafb6a6a3a635ff91a31027948e7e1061990
1 /*
2 * Routines for the disassembly of the proprietary Cisco IPSEC in
3 * TCP encapsulation protocol
5 * $Id$
7 * Copyright 2007 Joerg Mayer (see AUTHORS file)
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 /* TODO:
29 * - Find out the meaning of the (unknown) trailer
30 * - UDP checksum is wrong
31 * - Currently doesn't handle AH (lack of sample trace)
34 #include "config.h"
36 #include <glib.h>
37 #include <epan/packet.h>
38 #include <epan/prefs.h>
39 #include "packet-ndmp.h"
41 static int hf_tcpencap_unknown = -1;
42 static int hf_tcpencap_zero = -1;
43 static int hf_tcpencap_seq = -1;
44 static int hf_tcpencap_ike_direction = -1;
45 static int hf_tcpencap_esp_zero = -1;
46 static int hf_tcpencap_magic = -1;
47 static int hf_tcpencap_proto = -1;
48 static int hf_tcpencap_magic2 = -1;
50 static int proto_tcpencap = -1;
51 static gint ett_tcpencap = -1;
52 static gint ett_tcpencap_unknown = -1;
54 static const value_string tcpencap_ikedir_vals[] = {
55 { 0x0000, "Server to client" },
56 { 0x4000, "Client to server" },
58 { 0, NULL }
61 static const value_string tcpencap_proto_vals[] = {
62 { 0x11, "ISAKMP" },
63 { 0x32, "ESP" },
65 { 0, NULL }
68 #define TRAILERLENGTH 16
69 #define TCP_CISCO_IPSEC 10000
70 /* Another case of several companies creating protocols and
71 choosing an easy-to-remember port. Playing tonight: Cisco vs NDMP.
72 Since NDMP has officially registered port 10000 with IANA, it should be the default
74 static guint global_tcpencap_tcp_port = 0;
76 static dissector_handle_t esp_handle;
77 static dissector_handle_t udp_handle;
79 #define TCP_ENCAP_P_ESP 1
80 #define TCP_ENCAP_P_UDP 2
83 static int
84 packet_is_tcpencap(tvbuff_t *tvb, packet_info *pinfo, guint32 offset)
86 if ( /* Must be zero */
87 tvb_get_ntohl(tvb, offset + 0) != 0 ||
88 /* Lower 12 bits must be zero */
89 (tvb_get_ntohs(tvb, offset + 6) & 0xfff) != 0 ||
90 /* Protocol must be UDP or ESP */
91 (tvb_get_guint8(tvb, offset + 13) != 17 &&
92 tvb_get_guint8(tvb, offset + 13) != 50)
93 ) {
94 return FALSE;
97 if(check_if_ndmp(tvb, pinfo)){
98 return FALSE;
101 return TRUE;
105 * TCP Encapsulation of IPsec Packets
106 * as supported by the cisco vpn3000 concentrator series
108 static int
109 dissect_tcpencap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
111 proto_tree *tcpencap_tree = NULL;
112 proto_tree *tcpencap_unknown_tree = NULL;
114 proto_item *tree_item = NULL;
115 proto_item *unknown_item = NULL;
116 tvbuff_t *next_tvb;
117 guint32 reported_length = tvb_reported_length(tvb);
118 guint32 offset;
119 guint8 protocol;
121 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TCPENCAP");
122 col_clear(pinfo->cinfo, COL_INFO);
124 /* If the first 4 bytes are 0x01f401f4 (udp src and dst port = 500)
125 we most likely have UDP (isakmp) traffic */
127 if (tvb_get_ntohl(tvb, 0) == 0x01f401f4) { /* UDP means ISAKMP */
128 protocol = TCP_ENCAP_P_UDP;
129 } else { /* Hopefully ESP */
130 protocol = TCP_ENCAP_P_ESP;
133 if (tree) {
134 tree_item = proto_tree_add_item(tree, proto_tcpencap, tvb, 0, -1, ENC_NA);
135 tcpencap_tree = proto_item_add_subtree(tree_item, ett_tcpencap);
137 /* Dissect the trailer following the encapsulated IPSEC/ISAKMP packet */
138 offset = reported_length - TRAILERLENGTH;
139 unknown_item = proto_tree_add_item(tcpencap_tree, hf_tcpencap_unknown, tvb,
140 offset, TRAILERLENGTH, ENC_NA);
141 /* Try to guess the contents of the trailer */
142 tcpencap_unknown_tree = proto_item_add_subtree(unknown_item, ett_tcpencap_unknown);
143 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_zero, tvb, offset + 0, 4, ENC_NA);
144 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_seq, tvb, offset + 4, 2, ENC_BIG_ENDIAN);
145 if (protocol == TCP_ENCAP_P_UDP) {
146 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_ike_direction, tvb, offset + 6, 2, ENC_BIG_ENDIAN);
147 } else {
148 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_esp_zero, tvb, offset + 6, 2, ENC_BIG_ENDIAN);
150 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_magic, tvb, offset + 8, 5, ENC_NA);
151 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_proto, tvb, offset + 13, 1, ENC_BIG_ENDIAN);
152 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_magic2, tvb, offset + 14, 2, ENC_NA);
155 /* Create the tvbuffer for the next dissector */
156 next_tvb = tvb_new_subset(tvb, 0, reported_length - TRAILERLENGTH , -1);
157 if (protocol == TCP_ENCAP_P_UDP) {
158 call_dissector(udp_handle, next_tvb, pinfo, tree);
159 } else { /* Hopefully ESP */
160 call_dissector(esp_handle, next_tvb, pinfo, tree);
163 return tvb_length(tvb);
166 static gboolean
167 dissect_tcpencap_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
169 guint32 reported_length = tvb_reported_length(tvb);
170 guint32 length = tvb_length(tvb);
172 if (reported_length <= TRAILERLENGTH + 8 ||
173 /* Ensure we have enough bytes for packet_is_tcpencap analysis */
174 (reported_length - length) > (TRAILERLENGTH - 13) ||
175 !packet_is_tcpencap(tvb, pinfo, reported_length - TRAILERLENGTH) ) {
176 return FALSE;
179 dissect_tcpencap(tvb, pinfo, tree, data);
180 return TRUE;
183 void
184 proto_register_tcpencap(void)
186 static hf_register_info hf[] = {
188 { &hf_tcpencap_unknown,
189 { "Unknown trailer", "tcpencap.unknown", FT_BYTES, BASE_NONE, NULL,
190 0x0, NULL, HFILL }},
192 { &hf_tcpencap_zero,
193 { "All zero", "tcpencap.zero", FT_BYTES, BASE_NONE, NULL,
194 0x0, NULL, HFILL }},
196 { &hf_tcpencap_seq,
197 { "Sequence number", "tcpencap.seq", FT_UINT16, BASE_HEX, NULL,
198 0x0, NULL, HFILL }},
200 { &hf_tcpencap_esp_zero,
201 { "ESP zero", "tcpencap.espzero", FT_UINT16, BASE_HEX, NULL,
202 0x0, NULL, HFILL }},
204 { &hf_tcpencap_ike_direction,
205 { "ISAKMP traffic direction", "tcpencap.ikedirection", FT_UINT16, BASE_HEX, VALS(tcpencap_ikedir_vals),
206 0x0, NULL, HFILL }},
208 { &hf_tcpencap_magic,
209 { "Magic number", "tcpencap.magic", FT_BYTES, BASE_NONE, NULL,
210 0x0, NULL, HFILL }},
212 { &hf_tcpencap_proto,
213 { "Protocol", "tcpencap.proto", FT_UINT8, BASE_HEX, VALS(tcpencap_proto_vals),
214 0x0, NULL, HFILL }},
216 { &hf_tcpencap_magic2,
217 { "Magic 2", "tcpencap.magic2", FT_BYTES, BASE_NONE, NULL,
218 0x0, NULL, HFILL }},
222 static gint *ett[] = {
223 &ett_tcpencap,
224 &ett_tcpencap_unknown,
227 module_t *tcpencap_module;
229 void proto_reg_handoff_tcpencap(void);
231 proto_tcpencap = proto_register_protocol(
232 "TCP Encapsulation of IPsec Packets", "TCPENCAP", "tcpencap");
233 proto_register_field_array(proto_tcpencap, hf, array_length(hf));
234 proto_register_subtree_array(ett, array_length(ett));
235 tcpencap_module = prefs_register_protocol(proto_tcpencap, proto_reg_handoff_tcpencap);
236 prefs_register_uint_preference(tcpencap_module, "tcp.port", "IPSEC TCP Port",
237 "Set the port for IPSEC/ISAKMP messages (typically 10000)",
238 10, &global_tcpencap_tcp_port);
241 void
242 proto_reg_handoff_tcpencap(void)
244 static dissector_handle_t tcpencap_handle;
245 static gboolean initialized = FALSE;
246 static guint tcpencap_tcp_port = 0;
248 if (!initialized) {
249 tcpencap_handle = new_create_dissector_handle(dissect_tcpencap, proto_tcpencap);
250 esp_handle = find_dissector("esp");
251 udp_handle = find_dissector("udp");
253 heur_dissector_add("tcp", dissect_tcpencap_heur, proto_tcpencap);
255 initialized = TRUE;
258 /* Register TCP port for dissection */
259 if(tcpencap_tcp_port != 0 && tcpencap_tcp_port != global_tcpencap_tcp_port){
260 dissector_delete_uint("tcp.port", tcpencap_tcp_port, tcpencap_handle);
263 if(global_tcpencap_tcp_port != 0 && tcpencap_tcp_port != global_tcpencap_tcp_port) {
264 dissector_add_uint("tcp.port", global_tcpencap_tcp_port, tcpencap_handle);