2 * Routines for IPsec/IPComp packet disassembly
4 * $Id: packet-ipsec.c,v 1.44 2003/08/16 00:11:07 gerald Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #include <epan/packet.h>
34 #include "packet-ipsec.h"
35 #include <epan/resolv.h>
39 /* Place AH payload in sub tree */
40 static gboolean g_ah_payload_in_subtree = FALSE;
42 static int proto_ah = -1;
43 static int hf_ah_spi = -1;
44 static int hf_ah_sequence = -1;
45 static int proto_esp = -1;
46 static int hf_esp_spi = -1;
47 static int hf_esp_sequence = -1;
48 static int proto_ipcomp = -1;
49 static int hf_ipcomp_flags = -1;
50 static int hf_ipcomp_cpi = -1;
52 static gint ett_ah = -1;
53 static gint ett_esp = -1;
54 static gint ett_ipcomp = -1;
56 static dissector_handle_t data_handle;
58 static dissector_table_t ip_dissector_table;
61 guint8 ah_nxt; /* Next Header */
62 guint8 ah_len; /* Length of data + 1, in 32bit */
63 guint16 ah_reserve; /* Reserved for future use */
64 guint32 ah_spi; /* Security parameter index */
65 guint32 ah_seq; /* Sequence number field */
66 /* variable size, 32bit bound*/ /* Authentication data */
70 guint32 esp_spi; /* ESP */
71 guint32 esp_seq; /* Sequence number */
72 /*variable size*/ /* (IV and) Payload data */
73 /*variable size*/ /* padding */
74 /*8bit*/ /* pad size */
75 /*8bit*/ /* next header */
76 /*8bit*/ /* next header */
77 /*variable size, 32bit bound*/ /* Authentication data */
81 guint8 comp_nxt; /* Next Header */
82 guint8 comp_flags; /* Must be zero */
83 guint16 comp_cpi; /* Compression parameter index */
86 /* well-known algorithm number (in CPI), from RFC2409 */
87 #define IPCOMP_OUI 1 /* vendor specific */
88 #define IPCOMP_DEFLATE 2 /* RFC2394 */
89 #define IPCOMP_LZS 3 /* RFC2395 */
92 static const value_string cpi2val[] = {
93 { IPCOMP_OUI, "OUI" },
94 { IPCOMP_DEFLATE, "DEFLATE" },
95 { IPCOMP_LZS, "LZS" },
100 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
104 dissect_ah(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
106 proto_tree *next_tree;
111 advance = dissect_ah_header(tvb, pinfo, tree, &nxt, &next_tree);
112 next_tvb = tvb_new_subset(tvb, advance, -1, -1);
114 if (g_ah_payload_in_subtree) {
115 col_set_writable(pinfo->cinfo, FALSE);
118 /* do lookup with the subdissector table */
119 if (!dissector_try_port(ip_dissector_table, nxt, next_tvb, pinfo, next_tree)) {
120 call_dissector(data_handle,next_tvb, pinfo, next_tree);
125 dissect_ah_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
126 guint8 *nxt_p, proto_tree **next_tree_p)
133 if (check_col(pinfo->cinfo, COL_PROTOCOL))
134 col_set_str(pinfo->cinfo, COL_PROTOCOL, "AH");
135 if (check_col(pinfo->cinfo, COL_INFO))
136 col_clear(pinfo->cinfo, COL_INFO);
138 tvb_memcpy(tvb, (guint8 *)&ah, 0, sizeof(ah));
139 advance = sizeof(ah) + ((ah.ah_len - 1) << 2);
141 if (check_col(pinfo->cinfo, COL_INFO)) {
142 col_add_fstr(pinfo->cinfo, COL_INFO, "AH (SPI=0x%08x)",
143 (guint32)g_ntohl(ah.ah_spi));
147 /* !!! specify length */
148 ti = proto_tree_add_item(tree, proto_ah, tvb, 0, advance, FALSE);
149 ah_tree = proto_item_add_subtree(ti, ett_ah);
151 proto_tree_add_text(ah_tree, tvb,
152 offsetof(struct newah, ah_nxt), 1,
153 "Next Header: %s (0x%02x)",
154 ipprotostr(ah.ah_nxt), ah.ah_nxt);
155 proto_tree_add_text(ah_tree, tvb,
156 offsetof(struct newah, ah_len), 1,
157 "Length: %u", (ah.ah_len + 2) << 2);
158 proto_tree_add_uint(ah_tree, hf_ah_spi, tvb,
159 offsetof(struct newah, ah_spi), 4,
160 (guint32)g_ntohl(ah.ah_spi));
161 proto_tree_add_uint(ah_tree, hf_ah_sequence, tvb,
162 offsetof(struct newah, ah_seq), 4,
163 (guint32)g_ntohl(ah.ah_seq));
164 proto_tree_add_text(ah_tree, tvb,
165 sizeof(ah), (ah.ah_len - 1) << 2,
168 if (next_tree_p != NULL) {
169 /* Decide where to place next protocol decode */
170 if (g_ah_payload_in_subtree) {
171 *next_tree_p = ah_tree;
178 if (next_tree_p != NULL)
185 /* start of the new header (could be a extension header) */
190 dissect_esp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
192 proto_tree *esp_tree;
197 * load the top pane info. This should be overwritten by
198 * the next protocol in the stack
200 if (check_col(pinfo->cinfo, COL_PROTOCOL))
201 col_set_str(pinfo->cinfo, COL_PROTOCOL, "ESP");
202 if (check_col(pinfo->cinfo, COL_INFO))
203 col_clear(pinfo->cinfo, COL_INFO);
205 tvb_memcpy(tvb, (guint8 *)&esp, 0, sizeof(esp));
207 if (check_col(pinfo->cinfo, COL_INFO)) {
208 col_add_fstr(pinfo->cinfo, COL_INFO, "ESP (SPI=0x%08x)",
209 (guint32)g_ntohl(esp.esp_spi));
213 * populate a tree in the second pane with the status of the link layer
217 ti = proto_tree_add_item(tree, proto_esp, tvb, 0, -1, FALSE);
218 esp_tree = proto_item_add_subtree(ti, ett_esp);
219 proto_tree_add_uint(esp_tree, hf_esp_spi, tvb,
220 offsetof(struct newesp, esp_spi), 4,
221 (guint32)g_ntohl(esp.esp_spi));
222 proto_tree_add_uint(esp_tree, hf_esp_sequence, tvb,
223 offsetof(struct newesp, esp_seq), 4,
224 (guint32)g_ntohl(esp.esp_seq));
225 call_dissector(data_handle,
226 tvb_new_subset(tvb, sizeof(struct newesp), -1, -1),
232 dissect_ipcomp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
234 proto_tree *ipcomp_tree;
236 struct ipcomp ipcomp;
240 * load the top pane info. This should be overwritten by
241 * the next protocol in the stack
243 if (check_col(pinfo->cinfo, COL_PROTOCOL))
244 col_set_str(pinfo->cinfo, COL_PROTOCOL, "IPComp");
245 if (check_col(pinfo->cinfo, COL_INFO))
246 col_clear(pinfo->cinfo, COL_INFO);
248 tvb_memcpy(tvb, (guint8 *)&ipcomp, 0, sizeof(ipcomp));
250 if (check_col(pinfo->cinfo, COL_INFO)) {
251 p = match_strval(g_ntohs(ipcomp.comp_cpi), cpi2val);
253 col_add_fstr(pinfo->cinfo, COL_INFO, "IPComp (CPI=0x%04x)",
254 g_ntohs(ipcomp.comp_cpi));
256 col_add_fstr(pinfo->cinfo, COL_INFO, "IPComp (CPI=%s)", p);
260 * populate a tree in the second pane with the status of the link layer
264 ti = proto_tree_add_item(tree, proto_ipcomp, tvb, 0, -1, FALSE);
265 ipcomp_tree = proto_item_add_subtree(ti, ett_ipcomp);
267 proto_tree_add_text(ipcomp_tree, tvb,
268 offsetof(struct ipcomp, comp_nxt), 1,
269 "Next Header: %s (0x%02x)",
270 ipprotostr(ipcomp.comp_nxt), ipcomp.comp_nxt);
271 proto_tree_add_uint(ipcomp_tree, hf_ipcomp_flags, tvb,
272 offsetof(struct ipcomp, comp_flags), 1,
274 proto_tree_add_uint(ipcomp_tree, hf_ipcomp_cpi, tvb,
275 offsetof(struct ipcomp, comp_cpi), 2,
276 g_ntohs(ipcomp.comp_cpi));
277 call_dissector(data_handle,
278 tvb_new_subset(tvb, sizeof(struct ipcomp), -1, -1), pinfo,
284 proto_register_ipsec(void)
287 static hf_register_info hf_ah[] = {
289 { "SPI", "ah.spi", FT_UINT32, BASE_HEX, NULL, 0x0,
292 { "Sequence", "ah.sequence", FT_UINT32, BASE_DEC, NULL, 0x0,
296 static hf_register_info hf_esp[] = {
298 { "SPI", "esp.spi", FT_UINT32, BASE_HEX, NULL, 0x0,
301 { "Sequence", "esp.sequence", FT_UINT32, BASE_DEC, NULL, 0x0,
305 static hf_register_info hf_ipcomp[] = {
307 { "Flags", "ipcomp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
310 { "CPI", "ipcomp.cpi", FT_UINT16, BASE_HEX,
311 VALS(cpi2val), 0x0, "", HFILL }},
313 static gint *ett[] = {
321 proto_ah = proto_register_protocol("Authentication Header", "AH", "ah");
322 proto_register_field_array(proto_ah, hf_ah, array_length(hf_ah));
324 proto_esp = proto_register_protocol("Encapsulating Security Payload",
326 proto_register_field_array(proto_esp, hf_esp, array_length(hf_esp));
328 proto_ipcomp = proto_register_protocol("IP Payload Compression",
330 proto_register_field_array(proto_ipcomp, hf_ipcomp, array_length(hf_ipcomp));
332 proto_register_subtree_array(ett, array_length(ett));
334 /* Register a configuration option for placement of AH payload dissection */
335 ah_module = prefs_register_protocol(proto_ah, NULL);
336 prefs_register_bool_preference(ah_module, "place_ah_payload_in_subtree",
337 "Place AH payload in subtree",
338 "Whether the AH payload decode should be placed in a subtree",
339 &g_ah_payload_in_subtree);
341 register_dissector("esp", dissect_esp, proto_esp);
342 register_dissector("ah", dissect_ah, proto_ah);
346 proto_reg_handoff_ipsec(void)
348 dissector_handle_t esp_handle, ah_handle, ipcomp_handle;
350 data_handle = find_dissector("data");
351 ah_handle = find_dissector("ah");
352 dissector_add("ip.proto", IP_PROTO_AH, ah_handle);
353 esp_handle = find_dissector("esp");
354 dissector_add("ip.proto", IP_PROTO_ESP, esp_handle);
355 ipcomp_handle = create_dissector_handle(dissect_ipcomp, proto_ipcomp);
356 dissector_add("ip.proto", IP_PROTO_IPCOMP, ipcomp_handle);
358 ip_dissector_table = find_dissector_table("ip.proto");