6239b099f0e8763819ee95c946ca68d342b5cb39
[metze/wireshark/wip.git] / epan / dissectors / 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
25 #include "config.h"
26
27 #include <epan/packet.h>
28
29 void proto_register_udpencap(void);
30 void proto_reg_handoff_udpencap(void);
31
32 static int proto_udpencap = -1;
33
34 static int hf_nat_keepalive = -1;
35 static int hf_non_esp_marker = -1;
36
37 static gint ett_udpencap = -1;
38
39 static dissector_handle_t esp_handle;
40 static dissector_handle_t isakmp_handle;
41
42 /*
43  * UDP Encapsulation of IPsec Packets
44  * draft-ietf-ipsec-udp-encaps-06.txt
45  */
46 static void
47 dissect_udpencap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
48 {
49   tvbuff_t   *next_tvb;
50   proto_tree *udpencap_tree;
51   proto_item *ti;
52   guint32     spi;
53
54   col_set_str(pinfo->cinfo, COL_PROTOCOL, "UDPENCAP");
55   col_clear(pinfo->cinfo, COL_INFO);
56
57   ti = proto_tree_add_item(tree, proto_udpencap, tvb, 0, -1, ENC_NA);
58   udpencap_tree = proto_item_add_subtree(ti, ett_udpencap);
59
60   /* 1 byte of 0xFF indicates NAT-keepalive */
61   if ((tvb_captured_length(tvb) == 1) && (tvb_get_guint8(tvb, 0) == 0xff)) {
62     col_set_str(pinfo->cinfo, COL_INFO, "NAT-keepalive");
63     proto_tree_add_item(udpencap_tree, hf_nat_keepalive, tvb, 0, 1, ENC_NA);
64   } else {
65     /* SPI of zero indicates IKE traffic, otherwise it's ESP */
66     spi = tvb_get_ntohl(tvb, 0);
67     if (spi == 0) {
68       col_set_str(pinfo->cinfo, COL_INFO, "ISAKMP");
69       proto_tree_add_item(udpencap_tree, hf_non_esp_marker, tvb, 0, 4, ENC_NA);
70       proto_item_set_len(ti, 4);
71       next_tvb = tvb_new_subset_remaining(tvb, 4);
72       call_dissector(isakmp_handle, next_tvb, pinfo, tree);
73     } else {
74       col_set_str(pinfo->cinfo, COL_INFO, "ESP");
75       proto_item_set_len(ti, 0);
76       call_dissector(esp_handle, tvb, pinfo, tree);
77     }
78   }
79 }
80
81 void
82 proto_register_udpencap(void)
83 {
84   static hf_register_info hf[] = {
85     { &hf_nat_keepalive, { "NAT-keepalive packet", "udpencap.nat_keepalive",
86           FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL }},
87     { &hf_non_esp_marker, { "Non-ESP Marker", "udpencap.non_esp_marker",
88           FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL }},
89   };
90
91   static gint *ett[] = {
92     &ett_udpencap,
93   };
94
95   proto_udpencap = proto_register_protocol(
96         "UDP Encapsulation of IPsec Packets", "UDPENCAP", "udpencap");
97   proto_register_field_array(proto_udpencap, hf, array_length(hf));
98   proto_register_subtree_array(ett, array_length(ett));
99 }
100
101 void
102 proto_reg_handoff_udpencap(void)
103 {
104   dissector_handle_t udpencap_handle;
105
106   esp_handle = find_dissector("esp");
107   isakmp_handle = find_dissector("isakmp");
108
109   udpencap_handle = create_dissector_handle(dissect_udpencap, proto_udpencap);
110   dissector_add_uint("udp.port", 4500, udpencap_handle);
111 }
112
113 /*
114  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
115  *
116  * Local Variables:
117  * c-basic-offset: 2
118  * tab-width: 8
119  * indent-tabs-mode: nil
120  * End:
121  *
122  * ex: set shiftwidth=2 tabstop=8 expandtab:
123  * :indentSize=2:tabSize=8:noTabs=true:
124  */