Call subdissectors even if we're not building a protocol tree.
[obnox/wireshark/wip.git] / packet-ipsec-udp.c
1 /*
2  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * $Id: packet-ipsec-udp.c,v 1.4 2003/10/02 22:44:19 guy Exp $
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <string.h>
31 #include <glib.h>
32 #include <epan/packet.h>
33
34 static int proto_udpencap = -1;
35 static gint ett_udpencap = -1;
36
37 static dissector_handle_t esp_handle;
38 static dissector_handle_t isakmp_handle;
39
40 /*
41  * UDP Encapsulation of IPsec Packets   
42  * draft-ietf-ipsec-udp-encaps-06.txt
43  */
44 static void
45 dissect_udpencap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
46 {
47   tvbuff_t *next_tvb;
48   proto_tree *udpencap_tree = NULL;
49   proto_item *ti = NULL;
50   guint32 spi;
51
52   if (check_col(pinfo->cinfo, COL_PROTOCOL))
53     col_set_str(pinfo->cinfo, COL_PROTOCOL, "UDPENCAP");
54   if (check_col(pinfo->cinfo, COL_INFO))
55     col_clear(pinfo->cinfo, COL_INFO);
56
57   if (tree) {
58     ti = proto_tree_add_item(tree, proto_udpencap, tvb, 0, -1, FALSE);
59     udpencap_tree = proto_item_add_subtree(ti, ett_udpencap);
60   }
61
62   /* First byte of 0 iindicates NAT-keepalive */
63   if (tvb_get_guint8(tvb, 0) == 0xff) {
64     if (tree)
65       proto_tree_add_text(udpencap_tree, tvb, 0, 1, "NAT-keepalive packet");
66   } else {
67     /* SPI of zero indicates IKE traffic, otherwise it's ESP */
68     tvb_memcpy(tvb, (guint8 *)&spi, 0, sizeof(spi));
69     if (spi == 0) {
70       if (tree) {
71         proto_tree_add_text(udpencap_tree, tvb, 0, sizeof(spi),
72                 "Non-ESP Marker");
73         proto_item_set_len(ti, sizeof(spi));
74       }
75       next_tvb = tvb_new_subset(tvb, sizeof(spi), -1, -1);
76       call_dissector(isakmp_handle, next_tvb, pinfo, tree);
77     } else {
78       if (tree)
79         proto_item_set_len(ti, 0);
80       call_dissector(esp_handle, tvb, pinfo, tree);
81     }
82   }
83 }
84
85 void
86 proto_register_udpencap(void)
87 {
88   static gint *ett[] = {
89     &ett_udpencap,
90   };
91
92   proto_udpencap = proto_register_protocol(
93         "UDP Encapsulation of IPsec Packets", "UDPENCAP", "udpencap");
94   proto_register_subtree_array(ett, array_length(ett));
95 }
96
97 void
98 proto_reg_handoff_udpencap(void)
99 {
100   dissector_handle_t udpencap_handle;
101
102   esp_handle = find_dissector("esp");
103   isakmp_handle = find_dissector("isakmp");
104
105   udpencap_handle = create_dissector_handle(dissect_udpencap, proto_udpencap);
106   dissector_add("udp.port", 4500, udpencap_handle);
107 }