So many people who love to use easy to remember ports.
[obnox/wireshark/wip.git] / epan / dissectors / packet-ipsec-tcp.c
1 /*
2  * Routines for the disassembly of the proprietary Cisco IPSEC in
3  * TCP encapsulation protocol
4  *
5  * $Id$
6  *
7  * Copyright 2007 Joerg Mayer (see AUTHORS file)
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
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.
17  *
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.
22  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
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)
32  */
33
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <glib.h>
39 #include <epan/packet.h>
40 #include <epan/prefs.h>
41 #include "packet-ndmp.h"
42
43 static int hf_tcpencap_unknown = -1;
44 static int hf_tcpencap_zero = -1;
45 static int hf_tcpencap_seq = -1;
46 static int hf_tcpencap_ike_direction = -1;
47 static int hf_tcpencap_esp_zero = -1;
48 static int hf_tcpencap_magic = -1;
49 static int hf_tcpencap_proto = -1;
50 static int hf_tcpencap_magic2 = -1;
51
52 static int proto_tcpencap = -1;
53 static gint ett_tcpencap = -1;
54 static gint ett_tcpencap_unknown = -1;
55
56 static const value_string tcpencap_ikedir_vals[] = {
57         { 0x0000,       "Server to client" },
58         { 0x4000,       "Client to server" },
59
60         { 0,    NULL }
61 };
62
63 static const value_string tcpencap_proto_vals[] = {
64         { 0x11, "ISAKMP" },
65         { 0x32, "ESP" },
66
67         { 0,    NULL }
68 };
69
70 #define TRAILERLENGTH 16
71 #define TCP_CISCO_IPSEC 10000
72 static guint global_tcpencap_tcp_port = TCP_CISCO_IPSEC;
73
74 static dissector_handle_t esp_handle;
75 static dissector_handle_t udp_handle;
76
77 #define TCP_ENCAP_P_ESP 1
78 #define TCP_ENCAP_P_UDP 2
79
80
81 /* Another case of several companies creating protocols and
82    choosing an easy-to-remember port. Playing tonight: Cisco vs NDMP.
83 */
84 static int
85 packet_is_tcpencap(tvbuff_t *tvb, packet_info *pinfo, guint32 offset)
86 {
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)
94         ) {
95                 return FALSE;
96         }
97
98         if(check_if_ndmp(tvb, pinfo)){
99                 return FALSE;
100         }
101
102         return TRUE;
103 }
104
105 /*
106  * TCP Encapsulation of IPsec Packets   
107  * as supported by the cisco vpn3000 concentrator series
108  */
109 static int
110 dissect_tcpencap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
111 {
112         proto_tree *tcpencap_tree = NULL;
113         proto_tree *tcpencap_unknown_tree = NULL;
114
115         proto_item *tree_item = NULL;
116         proto_item *unknown_item = NULL;
117         tvbuff_t *next_tvb;
118         guint32 reported_length = tvb_reported_length(tvb);
119         guint32 offset;
120         guint8  protocol;
121
122         /* verify that this looks like a tcpencap packet */
123         if (reported_length <= TRAILERLENGTH + 8 ||
124            !packet_is_tcpencap(tvb, pinfo, reported_length - TRAILERLENGTH) ) {
125                 return 0;
126         }
127
128         col_set_str(pinfo->cinfo, COL_PROTOCOL, "TCPENCAP");
129         col_clear(pinfo->cinfo, COL_INFO);
130
131         /* If the first 4 bytes are 0x01f401f4 (udp src and dst port = 500)
132            we most likely have UDP (isakmp) traffic */
133         
134         if (tvb_get_ntohl(tvb, 0) == 0x01f401f4) { /* UDP means ISAKMP */
135                 protocol = TCP_ENCAP_P_UDP;
136         } else { /* Hopefully ESP */
137                 protocol = TCP_ENCAP_P_ESP;
138         }
139
140         if (tree) {
141                 tree_item = proto_tree_add_item(tree, proto_tcpencap, tvb, 0, -1, FALSE);
142                 tcpencap_tree = proto_item_add_subtree(tree_item, ett_tcpencap);
143
144                 /* Dissect the trailer following the encapsulated IPSEC/ISAKMP packet */
145                 offset = reported_length - TRAILERLENGTH;
146                 unknown_item = proto_tree_add_item(tcpencap_tree, hf_tcpencap_unknown, tvb,
147                         offset, TRAILERLENGTH, FALSE);
148                 /* Try to guess the contents of the trailer */
149                 tcpencap_unknown_tree = proto_item_add_subtree(unknown_item, ett_tcpencap_unknown);
150                 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_zero, tvb, offset + 0, 4, FALSE);
151                 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_seq, tvb, offset + 4, 2, FALSE);
152                 if (protocol == TCP_ENCAP_P_UDP) {
153                         proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_ike_direction, tvb, offset + 6, 2, FALSE);
154                 } else {
155                         proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_esp_zero, tvb, offset + 6, 2, FALSE);
156                 }
157                 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_magic, tvb, offset + 8, 5, FALSE);
158                 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_proto, tvb, offset + 13, 1, FALSE);
159                 proto_tree_add_item(tcpencap_unknown_tree, hf_tcpencap_magic2, tvb, offset + 14, 2, FALSE);
160         }
161
162         /* Create the tvbuffer for the next dissector */
163         next_tvb = tvb_new_subset(tvb, 0, reported_length - TRAILERLENGTH , -1);
164         if (protocol == TCP_ENCAP_P_UDP) {
165                 call_dissector(udp_handle, next_tvb, pinfo, tree);
166         } else { /* Hopefully ESP */
167                 call_dissector(esp_handle, next_tvb, pinfo, tree);
168         }
169
170         return tvb_length(tvb);
171 }
172
173 void
174 proto_register_tcpencap(void)
175 {
176         static hf_register_info hf[] = {
177
178                 { &hf_tcpencap_unknown,
179                 { "Unknown trailer",      "tcpencap.unknown", FT_BYTES, BASE_NONE, NULL,
180                         0x0, NULL, HFILL }},
181
182                 { &hf_tcpencap_zero,
183                 { "All zero",      "tcpencap.zero", FT_BYTES, BASE_NONE, NULL,
184                         0x0, NULL, HFILL }},
185
186                 { &hf_tcpencap_seq,
187                 { "Sequence number",      "tcpencap.seq", FT_UINT16, BASE_HEX, NULL,
188                         0x0, NULL, HFILL }},
189
190                 { &hf_tcpencap_esp_zero,
191                 { "ESP zero",      "tcpencap.espzero", FT_UINT16, BASE_HEX, NULL,
192                         0x0, NULL, HFILL }},
193
194                 { &hf_tcpencap_ike_direction,
195                 { "ISAKMP traffic direction",      "tcpencap.ikedirection", FT_UINT16, BASE_HEX, VALS(tcpencap_ikedir_vals),
196                         0x0, NULL, HFILL }},
197
198                 { &hf_tcpencap_magic,
199                 { "Magic number",      "tcpencap.magic", FT_BYTES, BASE_NONE, NULL,
200                         0x0, NULL, HFILL }},
201
202                 { &hf_tcpencap_proto,
203                 { "Protocol",      "tcpencap.proto", FT_UINT8, BASE_HEX, VALS(tcpencap_proto_vals),
204                         0x0, NULL, HFILL }},
205
206                 { &hf_tcpencap_magic2,
207                 { "Magic 2",      "tcpencap.magic2", FT_BYTES, BASE_NONE, NULL,
208                         0x0, NULL, HFILL }},
209
210         };
211
212         static gint *ett[] = {
213                 &ett_tcpencap,
214                 &ett_tcpencap_unknown,
215         };
216
217         module_t *tcpencap_module;
218
219         void proto_reg_handoff_tcpencap(void);
220
221         proto_tcpencap = proto_register_protocol(
222                 "TCP Encapsulation of IPsec Packets", "TCPENCAP", "tcpencap");
223         proto_register_field_array(proto_tcpencap, hf, array_length(hf));
224         proto_register_subtree_array(ett, array_length(ett));
225         tcpencap_module = prefs_register_protocol(proto_tcpencap, proto_reg_handoff_tcpencap);
226         prefs_register_uint_preference(tcpencap_module, "tcp.port", "IPSEC TCP Port",
227                 "Set the port for IPSEC/ISAKMP messages"
228                 "If other than the default of 10000)",
229                 10, &global_tcpencap_tcp_port);
230 }
231
232 void
233 proto_reg_handoff_tcpencap(void)
234 {
235         static dissector_handle_t tcpencap_handle;
236         static gboolean initialized = FALSE;
237         static guint tcpencap_tcp_port;
238
239         if (!initialized) {
240                 tcpencap_handle = new_create_dissector_handle(dissect_tcpencap, proto_tcpencap);
241                 esp_handle = find_dissector("esp");
242                 udp_handle = find_dissector("udp");
243                 initialized = TRUE;
244         } else {
245                 dissector_delete("tcp.port", tcpencap_tcp_port, tcpencap_handle);
246         }
247
248         tcpencap_tcp_port = global_tcpencap_tcp_port;
249         dissector_add("tcp.port", global_tcpencap_tcp_port, tcpencap_handle);
250 }
251