2 * Routines for IPsec/IPComp packet disassembly
4 * $Id: packet-ipsec.c,v 1.11 2000/02/15 21:02:19 gram Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
42 #include "packet-ipsec.h"
45 static int proto_ah = -1;
46 static int hf_ah_spi = -1;
47 static int hf_ah_sequence = -1;
48 static int proto_esp = -1;
49 static int hf_esp_spi = -1;
50 static int hf_esp_sequence = -1;
51 static int proto_ipcomp = -1;
52 static int hf_ipcomp_flags = -1;
53 static int hf_ipcomp_cpi = -1;
55 static gint ett_ah = -1;
56 static gint ett_esp = -1;
57 static gint ett_ipcomp = -1;
60 guint8 ah_nxt; /* Next Header */
61 guint8 ah_len; /* Length of data + 1, in 32bit */
62 guint16 ah_reserve; /* Reserved for future use */
63 guint32 ah_spi; /* Security parameter index */
64 guint32 ah_seq; /* Sequence number field */
65 /* variable size, 32bit bound*/ /* Authentication data */
69 guint32 esp_spi; /* ESP */
70 guint32 esp_seq; /* Sequence number */
71 /*variable size*/ /* (IV and) Payload data */
72 /*variable size*/ /* padding */
73 /*8bit*/ /* pad size */
74 /*8bit*/ /* next header */
75 /*8bit*/ /* next header */
76 /*variable size, 32bit bound*/ /* Authentication data */
80 guint8 comp_nxt; /* Next Header */
81 guint8 comp_flags; /* Must be zero */
82 guint16 comp_cpi; /* Compression parameter index */
85 /* well-known algorithm number (in CPI), from RFC2409 */
86 #define IPCOMP_OUI 1 /* vendor specific */
87 #define IPCOMP_DEFLATE 2 /* RFC2394 */
88 #define IPCOMP_LZS 3 /* RFC2395 */
91 static const value_string cpi2val[] = {
92 { IPCOMP_OUI, "OUI" },
93 { IPCOMP_DEFLATE, "DEFLATE" },
94 { IPCOMP_LZS, "LZS" },
99 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
103 dissect_ah(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
110 memcpy(&ah, (void *) &pd[offset], sizeof(ah));
111 advance = sizeof(ah) + ((ah.ah_len - 1) << 2);
113 if (check_col(fd, COL_PROTOCOL))
114 col_add_str(fd, COL_PROTOCOL, "AH");
115 if (check_col(fd, COL_INFO)) {
116 col_add_fstr(fd, COL_INFO, "AH (SPI=0x%08x)",
117 (guint32)ntohl(ah.ah_spi));
121 /* !!! specify length */
122 ti = proto_tree_add_item(tree, proto_ah, offset, advance, NULL);
123 ah_tree = proto_item_add_subtree(ti, ett_ah);
125 proto_tree_add_text(ah_tree, offset + offsetof(struct newah, ah_nxt), 1,
126 "Next Header: %s (0x%02x)", ipprotostr(ah.ah_nxt), ah.ah_nxt);
127 proto_tree_add_text(ah_tree, offset + offsetof(struct newah, ah_len), 1,
128 "Length: %d", ah.ah_len << 2);
129 proto_tree_add_item(ah_tree, hf_ah_spi,
130 offset + offsetof(struct newah, ah_spi), 4,
131 (guint32)ntohl(ah.ah_spi));
132 proto_tree_add_item(ah_tree, hf_ah_sequence,
133 offset + offsetof(struct newah, ah_seq), 4,
134 (guint32)ntohl(ah.ah_seq));
135 proto_tree_add_text(ah_tree, offset + sizeof(ah), (ah.ah_len - 1) << 2,
139 /* start of the new header (could be a extension header) */
144 dissect_esp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
146 proto_tree *esp_tree;
150 memcpy(&esp, (void *) &pd[offset], sizeof(esp));
153 * load the top pane info. This should be overwritten by
154 * the next protocol in the stack
156 if (check_col(fd, COL_PROTOCOL))
157 col_add_str(fd, COL_PROTOCOL, "ESP");
158 if (check_col(fd, COL_INFO)) {
159 col_add_fstr(fd, COL_INFO, "ESP (SPI=0x%08x)",
160 (guint32)ntohl(esp.esp_spi));
164 * populate a tree in the second pane with the status of the link layer
168 ti = proto_tree_add_item(tree, proto_esp, offset, END_OF_FRAME, NULL);
169 esp_tree = proto_item_add_subtree(ti, ett_esp);
170 proto_tree_add_item(esp_tree, hf_esp_spi,
171 offset + offsetof(struct newesp, esp_spi), 4,
172 (guint32)ntohl(esp.esp_spi));
173 proto_tree_add_item(esp_tree, hf_esp_sequence,
174 offset + offsetof(struct newesp, esp_seq), 4,
175 (guint32)ntohl(esp.esp_seq));
176 dissect_data(pd, offset + sizeof(struct newesp), fd, esp_tree);
181 dissect_ipcomp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
183 proto_tree *ipcomp_tree;
185 struct ipcomp ipcomp;
188 memcpy(&ipcomp, (void *) &pd[offset], sizeof(ipcomp));
191 * load the top pane info. This should be overwritten by
192 * the next protocol in the stack
194 if (check_col(fd, COL_PROTOCOL))
195 col_add_str(fd, COL_PROTOCOL, "IPComp");
196 if (check_col(fd, COL_INFO)) {
197 p = val_to_str(ntohs(ipcomp.comp_cpi), cpi2val, "");
199 col_add_fstr(fd, COL_INFO, "IPComp (CPI=0x%04x)",
200 ntohs(ipcomp.comp_cpi));
202 col_add_fstr(fd, COL_INFO, "IPComp (CPI=%s)", p);
206 * populate a tree in the second pane with the status of the link layer
210 ti = proto_tree_add_item(tree, proto_ipcomp, offset, END_OF_FRAME,
212 ipcomp_tree = proto_item_add_subtree(ti, ett_ipcomp);
214 proto_tree_add_text(ipcomp_tree,
215 offset + offsetof(struct ipcomp, comp_nxt), 1,
216 "Next Header: %s (0x%02x)",
217 ipprotostr(ipcomp.comp_nxt), ipcomp.comp_nxt);
218 proto_tree_add_item(ipcomp_tree, hf_ipcomp_flags,
219 offset + offsetof(struct ipcomp, comp_flags), 1,
221 p = val_to_str(ntohs(ipcomp.comp_cpi), cpi2val, "");
223 proto_tree_add_item(ipcomp_tree, hf_ipcomp_cpi,
224 offset + offsetof(struct ipcomp, comp_cpi), 2,
225 ntohs(ipcomp.comp_cpi));
227 proto_tree_add_item_format(ipcomp_tree, hf_ipcomp_cpi,
228 offset + offsetof(struct ipcomp, comp_cpi), 2,
229 ntohs(ipcomp.comp_cpi),
231 p, ntohs(ipcomp.comp_cpi));
233 dissect_data(pd, offset + sizeof(struct ipcomp), fd, ipcomp_tree);
238 proto_register_ipsec(void)
241 static hf_register_info hf_ah[] = {
243 { "SPI", "ah.spi", FT_UINT32, BASE_HEX, NULL, 0x0,
246 { "Sequence", "ah.sequence", FT_UINT32, BASE_HEX, NULL, 0x0,
250 static hf_register_info hf_esp[] = {
252 { "SPI", "esp.spi", FT_UINT32, BASE_HEX, NULL, 0x0,
255 { "Sequence", "esp.sequence", FT_UINT32, BASE_HEX, NULL, 0x0,
259 static hf_register_info hf_ipcomp[] = {
261 { "Flags", "ipcomp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
264 { "CPI", "ipcomp.cpi", FT_UINT16, BASE_HEX, NULL, 0x0,
267 static gint *ett[] = {
273 proto_ah = proto_register_protocol("Authentication Header", "ah");
274 proto_register_field_array(proto_ah, hf_ah, array_length(hf_ah));
276 proto_esp = proto_register_protocol("Encapsulated Security Payload", "esp");
277 proto_register_field_array(proto_esp, hf_esp, array_length(hf_esp));
279 proto_ipcomp = proto_register_protocol("IP Payload Compression", "ipcomp");
280 proto_register_field_array(proto_ipcomp, hf_ipcomp, array_length(hf_ipcomp));
282 proto_register_subtree_array(ett, array_length(ett));