2 * Routines for the disassembly of the proprietary Cisco IPSEC in
3 * TCP encapsulation protocol
5 * Copyright 2007 Joerg Mayer (see AUTHORS file)
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * - Find out the meaning of the (unknown) trailer
28 * - UDP checksum is wrong
29 * - Currently doesn't handle AH (lack of sample trace)
35 #include <epan/packet.h>
36 #include <epan/prefs.h>
37 #include "packet-ndmp.h"
39 void proto_register_tcpencap(void);
40 void proto_reg_handoff_tcpencap(void);
42 static int hf_tcpencap_unknown = -1;
43 static int hf_tcpencap_zero = -1;
44 static int hf_tcpencap_seq = -1;
45 static int hf_tcpencap_ike_direction = -1;
46 static int hf_tcpencap_esp_zero = -1;
47 static int hf_tcpencap_magic = -1;
48 static int hf_tcpencap_proto = -1;
49 static int hf_tcpencap_magic2 = -1;
51 static int proto_tcpencap = -1;
52 static gint ett_tcpencap = -1;
53 static gint ett_tcpencap_unknown = -1;
55 static const value_string tcpencap_ikedir_vals[] = {
56 { 0x0000, "Server to client" },
57 { 0x4000, "Client to server" },
62 static const value_string tcpencap_proto_vals[] = {
69 #define TRAILERLENGTH 16
70 #define TCP_CISCO_IPSEC 10000
71 /* Another case of several companies creating protocols and
72 choosing an easy-to-remember port. Playing tonight: Cisco vs NDMP.
73 Since NDMP has officially registered port 10000 with IANA, it should be the default
75 static guint global_tcpencap_tcp_port = 0;
77 static dissector_handle_t esp_handle;
78 static dissector_handle_t udp_handle;
80 #define TCP_ENCAP_P_ESP 1
81 #define TCP_ENCAP_P_UDP 2
85 packet_is_tcpencap(tvbuff_t *tvb, packet_info *pinfo, guint32 offset)
87 if ( /* Must be zero */
88 tvb_get_ntohl(tvb, offset + 0) != 0 ||
89 /* Lower 12 bits must be zero */
90 (tvb_get_ntohs(tvb, offset + 6) & 0xfff) != 0 ||
91 /* Protocol must be UDP or ESP */
92 (tvb_get_guint8(tvb, offset + 13) != 17 &&
93 tvb_get_guint8(tvb, offset + 13) != 50)
98 if(check_if_ndmp(tvb, pinfo)){
106 * TCP Encapsulation of IPsec Packets
107 * as supported by the cisco vpn3000 concentrator series
110 dissect_tcpencap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
112 proto_tree *tcpencap_tree = NULL;
113 proto_tree *tcpencap_unknown_tree = NULL;
115 proto_item *tree_item = NULL;
116 proto_item *unknown_item = NULL;
118 guint32 reported_length = tvb_reported_length(tvb);
122 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TCPENCAP");
123 col_clear(pinfo->cinfo, COL_INFO);
125 /* If the first 4 bytes are 0x01f401f4 (udp src and dst port = 500)
126 we most likely have UDP (isakmp) traffic */
128 if (tvb_get_ntohl(tvb, 0) == 0x01f401f4) { /* UDP means ISAKMP */
129 protocol = TCP_ENCAP_P_UDP;
130 } else { /* Hopefully ESP */
131 protocol = TCP_ENCAP_P_ESP;
135 tree_item = proto_tree_add_item(tree, proto_tcpencap, tvb, 0, -1, ENC_NA);
136 tcpencap_tree = proto_item_add_subtree(tree_item, ett_tcpencap);
138 /* Dissect the trailer following the encapsulated IPSEC/ISAKMP packet */
139 offset = reported_length - TRAILERLENGTH;
140 unknown_item = proto_tree_add_item(tcpencap_tree, hf_tcpencap_unknown, tvb,
141 offset, TRAILERLENGTH, ENC_NA);
142 /* Try to guess the contents of the trailer */
143 tcpencap_unknown_tree = proto_item_add_subtree(unknown_item, ett_tcpencap_unknown);
144 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_zero, tvb, offset + 0, 4, ENC_NA);
145 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_seq, tvb, offset + 4, 2, ENC_BIG_ENDIAN);
146 if (protocol == TCP_ENCAP_P_UDP) {
147 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_ike_direction, tvb, offset + 6, 2, ENC_BIG_ENDIAN);
149 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_esp_zero, tvb, offset + 6, 2, ENC_BIG_ENDIAN);
151 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_magic, tvb, offset + 8, 5, ENC_NA);
152 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_proto, tvb, offset + 13, 1, ENC_BIG_ENDIAN);
153 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_magic2, tvb, offset + 14, 2, ENC_NA);
156 /* Create the tvbuffer for the next dissector */
157 next_tvb = tvb_new_subset(tvb, 0, reported_length - TRAILERLENGTH , -1);
158 if (protocol == TCP_ENCAP_P_UDP) {
159 call_dissector(udp_handle, next_tvb, pinfo, tree);
160 } else { /* Hopefully ESP */
161 call_dissector(esp_handle, next_tvb, pinfo, tree);
164 return tvb_length(tvb);
168 dissect_tcpencap_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
170 guint32 reported_length = tvb_reported_length(tvb);
171 guint32 length = tvb_length(tvb);
173 if (reported_length <= TRAILERLENGTH + 8 ||
174 /* Ensure we have enough bytes for packet_is_tcpencap analysis */
175 (reported_length - length) > (TRAILERLENGTH - 13) ||
176 !packet_is_tcpencap(tvb, pinfo, reported_length - TRAILERLENGTH) ) {
180 dissect_tcpencap(tvb, pinfo, tree, data);
185 proto_register_tcpencap(void)
187 static hf_register_info hf[] = {
189 { &hf_tcpencap_unknown,
190 { "Unknown trailer", "tcpencap.unknown", FT_BYTES, BASE_NONE, NULL,
194 { "All zero", "tcpencap.zero", FT_BYTES, BASE_NONE, NULL,
198 { "Sequence number", "tcpencap.seq", FT_UINT16, BASE_HEX, NULL,
201 { &hf_tcpencap_esp_zero,
202 { "ESP zero", "tcpencap.espzero", FT_UINT16, BASE_HEX, NULL,
205 { &hf_tcpencap_ike_direction,
206 { "ISAKMP traffic direction", "tcpencap.ikedirection", FT_UINT16, BASE_HEX, VALS(tcpencap_ikedir_vals),
209 { &hf_tcpencap_magic,
210 { "Magic number", "tcpencap.magic", FT_BYTES, BASE_NONE, NULL,
213 { &hf_tcpencap_proto,
214 { "Protocol", "tcpencap.proto", FT_UINT8, BASE_HEX, VALS(tcpencap_proto_vals),
217 { &hf_tcpencap_magic2,
218 { "Magic 2", "tcpencap.magic2", FT_BYTES, BASE_NONE, NULL,
223 static gint *ett[] = {
225 &ett_tcpencap_unknown,
228 module_t *tcpencap_module;
230 proto_tcpencap = proto_register_protocol(
231 "TCP Encapsulation of IPsec Packets", "TCPENCAP", "tcpencap");
232 proto_register_field_array(proto_tcpencap, hf, array_length(hf));
233 proto_register_subtree_array(ett, array_length(ett));
234 tcpencap_module = prefs_register_protocol(proto_tcpencap, proto_reg_handoff_tcpencap);
235 prefs_register_uint_preference(tcpencap_module, "tcp.port", "IPSEC TCP Port",
236 "Set the port for IPSEC/ISAKMP messages (typically 10000)",
237 10, &global_tcpencap_tcp_port);
241 proto_reg_handoff_tcpencap(void)
243 static dissector_handle_t tcpencap_handle;
244 static gboolean initialized = FALSE;
245 static guint tcpencap_tcp_port = 0;
248 tcpencap_handle = new_create_dissector_handle(dissect_tcpencap, proto_tcpencap);
249 esp_handle = find_dissector("esp");
250 udp_handle = find_dissector("udp");
252 heur_dissector_add("tcp", dissect_tcpencap_heur, proto_tcpencap);
257 /* Register TCP port for dissection */
258 if(tcpencap_tcp_port != 0 && tcpencap_tcp_port != global_tcpencap_tcp_port){
259 dissector_delete_uint("tcp.port", tcpencap_tcp_port, tcpencap_handle);
262 if(global_tcpencap_tcp_port != 0 && tcpencap_tcp_port != global_tcpencap_tcp_port) {
263 dissector_add_uint("tcp.port", global_tcpencap_tcp_port, tcpencap_handle);