From Olivier Biot: have a separate subtree ett_ value for concatenated
[obnox/wireshark/wip.git] / packet-ipsec.c
1 /* packet-ipsec.c
2  * Routines for IPsec/IPComp packet disassembly
3  *
4  * $Id: packet-ipsec.c,v 1.44 2003/08/16 00:11:07 gerald Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #include <string.h>
32 #include <glib.h>
33 #include <epan/packet.h>
34 #include "packet-ipsec.h"
35 #include <epan/resolv.h>
36 #include "ipproto.h"
37 #include "prefs.h"
38
39 /* Place AH payload in sub tree */
40 static gboolean g_ah_payload_in_subtree = FALSE;
41
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;
51
52 static gint ett_ah = -1;
53 static gint ett_esp = -1;
54 static gint ett_ipcomp = -1;
55
56 static dissector_handle_t data_handle;
57
58 static dissector_table_t ip_dissector_table;
59
60 struct newah {
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 */
67 };
68
69 struct newesp {
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 */
78 };
79
80 struct ipcomp {
81         guint8 comp_nxt;        /* Next Header */
82         guint8 comp_flags;      /* Must be zero */
83         guint16 comp_cpi;       /* Compression parameter index */
84 };
85
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 */
90 #define IPCOMP_MAX      4
91
92 static const value_string cpi2val[] = {
93     { IPCOMP_OUI, "OUI" },
94     { IPCOMP_DEFLATE, "DEFLATE" },
95     { IPCOMP_LZS, "LZS" },
96     { 0, NULL },
97 };
98
99 #ifndef offsetof
100 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
101 #endif
102
103 static void
104 dissect_ah(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
105 {
106     proto_tree *next_tree;
107     guint8 nxt;
108     tvbuff_t *next_tvb;
109     int advance;
110
111     advance = dissect_ah_header(tvb, pinfo, tree, &nxt, &next_tree);
112     next_tvb = tvb_new_subset(tvb, advance, -1, -1);
113
114     if (g_ah_payload_in_subtree) {
115         col_set_writable(pinfo->cinfo, FALSE);
116     }
117
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);
121     }
122 }
123
124 int
125 dissect_ah_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
126                   guint8 *nxt_p, proto_tree **next_tree_p)
127 {
128     proto_tree *ah_tree;
129     proto_item *ti;
130     struct newah ah;
131     int advance;
132
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);
137
138     tvb_memcpy(tvb, (guint8 *)&ah, 0, sizeof(ah));
139     advance = sizeof(ah) + ((ah.ah_len - 1) << 2);
140
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));
144     }
145
146     if (tree) {
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);
150
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,
166                             "ICV");
167
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;
172             }
173             else {
174                 *next_tree_p = tree;
175             }
176         }
177     } else {
178         if (next_tree_p != NULL)
179             *next_tree_p = NULL;
180     }
181
182     if (nxt_p != NULL)
183         *nxt_p = ah.ah_nxt;
184
185     /* start of the new header (could be a extension header) */
186     return advance;
187 }
188
189 static void
190 dissect_esp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
191 {
192     proto_tree *esp_tree;
193     proto_item *ti;
194     struct newesp esp;
195
196     /*
197      * load the top pane info. This should be overwritten by
198      * the next protocol in the stack
199      */
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);
204
205     tvb_memcpy(tvb, (guint8 *)&esp, 0, sizeof(esp));
206
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));
210     }
211
212     /*
213      * populate a tree in the second pane with the status of the link layer
214      * (ie none)
215      */
216     if(tree) {
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),
227             pinfo, esp_tree);
228     }
229 }
230
231 static void
232 dissect_ipcomp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
233 {
234     proto_tree *ipcomp_tree;
235     proto_item *ti;
236     struct ipcomp ipcomp;
237     char *p;
238
239     /*
240      * load the top pane info. This should be overwritten by
241      * the next protocol in the stack
242      */
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);
247
248     tvb_memcpy(tvb, (guint8 *)&ipcomp, 0, sizeof(ipcomp));
249
250     if (check_col(pinfo->cinfo, COL_INFO)) {
251         p = match_strval(g_ntohs(ipcomp.comp_cpi), cpi2val);
252         if (p == NULL) {
253             col_add_fstr(pinfo->cinfo, COL_INFO, "IPComp (CPI=0x%04x)",
254                 g_ntohs(ipcomp.comp_cpi));
255         } else
256             col_add_fstr(pinfo->cinfo, COL_INFO, "IPComp (CPI=%s)", p);
257     }
258
259     /*
260      * populate a tree in the second pane with the status of the link layer
261      * (ie none)
262      */
263     if (tree) {
264         ti = proto_tree_add_item(tree, proto_ipcomp, tvb, 0, -1, FALSE);
265         ipcomp_tree = proto_item_add_subtree(ti, ett_ipcomp);
266
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,
273             ipcomp.comp_flags);
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,
279             ipcomp_tree);
280     }
281 }
282
283 void
284 proto_register_ipsec(void)
285 {
286
287   static hf_register_info hf_ah[] = {
288     { &hf_ah_spi,
289       { "SPI",          "ah.spi",       FT_UINT32,      BASE_HEX, NULL, 0x0,
290         "", HFILL }},
291     { &hf_ah_sequence,
292       { "Sequence",     "ah.sequence",  FT_UINT32,      BASE_DEC, NULL, 0x0,
293         "", HFILL }}
294   };
295
296   static hf_register_info hf_esp[] = {
297     { &hf_esp_spi,
298       { "SPI",          "esp.spi",      FT_UINT32,      BASE_HEX, NULL, 0x0,
299         "", HFILL }},
300     { &hf_esp_sequence,
301       { "Sequence",     "esp.sequence", FT_UINT32,      BASE_DEC, NULL, 0x0,
302         "", HFILL }}
303   };
304
305   static hf_register_info hf_ipcomp[] = {
306     { &hf_ipcomp_flags,
307       { "Flags",        "ipcomp.flags", FT_UINT8,       BASE_HEX, NULL, 0x0,
308         "", HFILL }},
309     { &hf_ipcomp_cpi,
310       { "CPI",          "ipcomp.cpi",   FT_UINT16,      BASE_HEX,
311         VALS(cpi2val),  0x0,            "", HFILL }},
312   };
313   static gint *ett[] = {
314     &ett_ah,
315     &ett_esp,
316     &ett_ipcomp,
317   };
318
319   module_t *ah_module;
320
321   proto_ah = proto_register_protocol("Authentication Header", "AH", "ah");
322   proto_register_field_array(proto_ah, hf_ah, array_length(hf_ah));
323
324   proto_esp = proto_register_protocol("Encapsulating Security Payload",
325                                       "ESP", "esp");
326   proto_register_field_array(proto_esp, hf_esp, array_length(hf_esp));
327
328   proto_ipcomp = proto_register_protocol("IP Payload Compression",
329                                          "IPComp", "ipcomp");
330   proto_register_field_array(proto_ipcomp, hf_ipcomp, array_length(hf_ipcomp));
331
332   proto_register_subtree_array(ett, array_length(ett));
333
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);
340
341   register_dissector("esp", dissect_esp, proto_esp);
342   register_dissector("ah", dissect_ah, proto_ah);
343 }
344
345 void
346 proto_reg_handoff_ipsec(void)
347 {
348   dissector_handle_t esp_handle, ah_handle, ipcomp_handle;
349
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);
357
358   ip_dissector_table = find_dissector_table("ip.proto");
359 }