Move /asn1 to /epan/dissectors
[metze/wireshark/wip.git] / epan / dissectors / asn1 / s1ap / packet-s1ap-template.c
1 /* packet-s1ap.c
2  * Routines for E-UTRAN S1 Application Protocol (S1AP) packet dissection
3  * Copyright 2007-2010, Anders Broman <anders.broman@ericsson.com>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * Based on the RANAP dissector
24  *
25  * References: 3GPP TS 36.413 V9.2.0 (2010-03)
26  */
27
28 #include "config.h"
29
30 #include <epan/packet.h>
31
32 #include <epan/strutil.h>
33 #include <epan/asn1.h>
34 #include <epan/prefs.h>
35 #include <epan/sctpppids.h>
36
37 #include "packet-ber.h"
38 #include "packet-per.h"
39 #include "packet-e212.h"
40 #include "packet-sccp.h"
41 #include "packet-lte-rrc.h"
42 #include "packet-ranap.h"
43 #include "packet-bssgp.h"
44 #include "packet-s1ap.h"
45 #include "packet-a21.h"
46
47 #ifdef _MSC_VER
48 /* disable: "warning C4146: unary minus operator applied to unsigned type, result still unsigned" */
49 #pragma warning(disable:4146)
50 #endif
51
52 #define PNAME  "S1 Application Protocol"
53 #define PSNAME "S1AP"
54 #define PFNAME "s1ap"
55
56 /* Dissector will use SCTP PPID 18 or SCTP port. IANA assigned port = 36412 */
57 #define SCTP_PORT_S1AP  36412
58
59 void proto_register_s1ap(void);
60 void proto_reg_handoff_s1ap(void);
61
62 static dissector_handle_t nas_eps_handle;
63 static dissector_handle_t lppa_handle;
64 static dissector_handle_t bssgp_handle;
65
66 #include "packet-s1ap-val.h"
67
68 /* Initialize the protocol and registered fields */
69 static int proto_s1ap = -1;
70
71 static int hf_s1ap_transportLayerAddressIPv4 = -1;
72 static int hf_s1ap_transportLayerAddressIPv6 = -1;
73 #include "packet-s1ap-hf.c"
74
75 /* Initialize the subtree pointers */
76 static int ett_s1ap = -1;
77 static int ett_s1ap_TransportLayerAddress = -1;
78 static int ett_s1ap_ToTargetTransparentContainer = -1;
79 static int ett_s1ap_ToSourceTransparentContainer = -1;
80 static int ett_s1ap_RRCContainer = -1;
81 static int ett_s1ap_UERadioCapability = -1;
82 static int ett_s1ap_RIMInformation = -1;
83 static int ett_s1ap_Cdma2000PDU = -1;
84 static int ett_s1ap_Cdma2000SectorID = -1;
85
86 #include "packet-s1ap-ett.c"
87
88 enum{
89         INITIATING_MESSAGE,
90         SUCCESSFUL_OUTCOME,
91         UNSUCCESSFUL_OUTCOME
92 };
93
94
95 /* Global variables */
96 static guint32 ProcedureCode;
97 static guint32 ProtocolIE_ID;
98 static guint32 ProtocolExtensionID;
99 static guint gbl_s1apSctpPort=SCTP_PORT_S1AP;
100 static guint32 handover_type_value;
101 static guint32 message_type;
102 static gboolean g_s1ap_dissect_container = TRUE;
103 static const char *obj_id = NULL;
104
105 static dissector_handle_t gcsna_handle = NULL;
106
107 /* Dissector tables */
108 static dissector_table_t s1ap_ies_dissector_table;
109 static dissector_table_t s1ap_ies_p1_dissector_table;
110 static dissector_table_t s1ap_ies_p2_dissector_table;
111 static dissector_table_t s1ap_extension_dissector_table;
112 static dissector_table_t s1ap_proc_imsg_dissector_table;
113 static dissector_table_t s1ap_proc_sout_dissector_table;
114 static dissector_table_t s1ap_proc_uout_dissector_table;
115
116 static int dissect_ProtocolIEFieldValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
117 /* Currently not used
118 static int dissect_ProtocolIEFieldPairFirstValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
119 static int dissect_ProtocolIEFieldPairSecondValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
120 */
121 static int dissect_ProtocolExtensionFieldExtensionValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
122 static int dissect_InitiatingMessageValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
123 static int dissect_SuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
124 static int dissect_UnsuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
125
126 static int dissect_SourceeNB_ToTargeteNB_TransparentContainer_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
127 static int dissect_TargeteNB_ToSourceeNB_TransparentContainer_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
128 #if 0
129 static int dissect_SourceRNC_ToTargetRNC_TransparentContainer_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
130 static int dissect_TargetRNC_ToSourceRNC_TransparentContainer_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
131 static int dissect_SourceBSS_ToTargetBSS_TransparentContainer_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
132 static int dissect_TargetBSS_ToSourceBSS_TransparentContainer_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
133 #endif
134
135 #include "packet-s1ap-fn.c"
136
137 static int dissect_ProtocolIEFieldValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
138 {
139   s1ap_ctx_t s1ap_ctx;
140
141   s1ap_ctx.message_type        = message_type;
142   s1ap_ctx.ProcedureCode       = ProcedureCode;
143   s1ap_ctx.ProtocolIE_ID       = ProtocolIE_ID;
144   s1ap_ctx.ProtocolExtensionID = ProtocolExtensionID;
145
146   return (dissector_try_uint_new(s1ap_ies_dissector_table, ProtocolIE_ID, tvb, pinfo, tree, TRUE, &s1ap_ctx)) ? tvb_captured_length(tvb) : 0;
147 }
148 /* Currently not used
149 static int dissect_ProtocolIEFieldPairFirstValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
150 {
151   return (dissector_try_uint(s1ap_ies_p1_dissector_table, ProtocolIE_ID, tvb, pinfo, tree)) ? tvb_captured_length(tvb) : 0;
152 }
153
154 static int dissect_ProtocolIEFieldPairSecondValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
155 {
156   return (dissector_try_uint(s1ap_ies_p2_dissector_table, ProtocolIE_ID, tvb, pinfo, tree)) ? tvb_captured_length(tvb) : 0;
157 }
158 */
159
160 static int dissect_ProtocolExtensionFieldExtensionValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
161 {
162   s1ap_ctx_t s1ap_ctx;
163
164   s1ap_ctx.message_type        = message_type;
165   s1ap_ctx.ProcedureCode       = ProcedureCode;
166   s1ap_ctx.ProtocolIE_ID       = ProtocolIE_ID;
167   s1ap_ctx.ProtocolExtensionID = ProtocolExtensionID;
168
169   return (dissector_try_uint_new(s1ap_extension_dissector_table, ProtocolExtensionID, tvb, pinfo, tree, TRUE, &s1ap_ctx)) ? tvb_captured_length(tvb) : 0;
170 }
171
172 static int dissect_InitiatingMessageValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
173 {
174   return (dissector_try_uint_new(s1ap_proc_imsg_dissector_table, ProcedureCode, tvb, pinfo, tree, TRUE, data)) ? tvb_captured_length(tvb) : 0;
175 }
176
177 static int dissect_SuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
178 {
179   return (dissector_try_uint_new(s1ap_proc_sout_dissector_table, ProcedureCode, tvb, pinfo, tree, TRUE, data)) ? tvb_captured_length(tvb) : 0;
180 }
181
182 static int dissect_UnsuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
183 {
184   return (dissector_try_uint_new(s1ap_proc_uout_dissector_table, ProcedureCode, tvb, pinfo, tree, TRUE, data)) ? tvb_captured_length(tvb) : 0;
185 }
186
187
188 static int
189 dissect_s1ap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
190 {
191         proto_item      *s1ap_item = NULL;
192         proto_tree      *s1ap_tree = NULL;
193
194         /* make entry in the Protocol column on summary display */
195         col_set_str(pinfo->cinfo, COL_PROTOCOL, "S1AP");
196
197         /* create the s1ap protocol tree */
198         s1ap_item = proto_tree_add_item(tree, proto_s1ap, tvb, 0, -1, ENC_NA);
199         s1ap_tree = proto_item_add_subtree(s1ap_item, ett_s1ap);
200
201         dissect_S1AP_PDU_PDU(tvb, pinfo, s1ap_tree, NULL);
202         return tvb_captured_length(tvb);
203 }
204
205 /*--- proto_reg_handoff_s1ap ---------------------------------------*/
206 void
207 proto_reg_handoff_s1ap(void)
208 {
209         static gboolean Initialized=FALSE;
210         static dissector_handle_t s1ap_handle;
211         static guint SctpPort;
212
213         s1ap_handle = find_dissector("s1ap");
214     gcsna_handle = find_dissector("gcsna");
215
216         if (!Initialized) {
217                 nas_eps_handle = find_dissector("nas-eps");
218                 lppa_handle = find_dissector("lppa");
219                 bssgp_handle = find_dissector("bssgp");
220                 dissector_add_for_decode_as("sctp.port", s1ap_handle);
221                 dissector_add_uint("sctp.ppi", S1AP_PAYLOAD_PROTOCOL_ID,   s1ap_handle);
222                 Initialized=TRUE;
223 #include "packet-s1ap-dis-tab.c"
224         } else {
225                 if (SctpPort != 0) {
226                         dissector_delete_uint("sctp.port", SctpPort, s1ap_handle);
227                 }
228         }
229
230         SctpPort=gbl_s1apSctpPort;
231         if (SctpPort != 0) {
232                 dissector_add_uint("sctp.port", SctpPort, s1ap_handle);
233         }
234 }
235
236 /*--- proto_register_s1ap -------------------------------------------*/
237 void proto_register_s1ap(void) {
238
239   /* List of fields */
240
241   static hf_register_info hf[] = {
242     { &hf_s1ap_transportLayerAddressIPv4,
243       { "transportLayerAddress(IPv4)", "s1ap.transportLayerAddressIPv4",
244         FT_IPv4, BASE_NONE, NULL, 0,
245         NULL, HFILL }},
246     { &hf_s1ap_transportLayerAddressIPv6,
247       { "transportLayerAddress(IPv6)", "s1ap.transportLayerAddressIPv6",
248         FT_IPv6, BASE_NONE, NULL, 0,
249         NULL, HFILL }},
250
251 #include "packet-s1ap-hfarr.c"
252   };
253
254   /* List of subtrees */
255   static gint *ett[] = {
256                   &ett_s1ap,
257                   &ett_s1ap_TransportLayerAddress,
258                   &ett_s1ap_ToTargetTransparentContainer,
259                   &ett_s1ap_ToSourceTransparentContainer,
260                   &ett_s1ap_RRCContainer,
261                   &ett_s1ap_UERadioCapability,
262                   &ett_s1ap_RIMInformation,
263           &ett_s1ap_Cdma2000PDU,
264           &ett_s1ap_Cdma2000SectorID,
265 #include "packet-s1ap-ettarr.c"
266   };
267
268   module_t *s1ap_module;
269
270   /* Register protocol */
271   proto_s1ap = proto_register_protocol(PNAME, PSNAME, PFNAME);
272   /* Register fields and subtrees */
273   proto_register_field_array(proto_s1ap, hf, array_length(hf));
274   proto_register_subtree_array(ett, array_length(ett));
275
276   /* Register dissector */
277   register_dissector("s1ap", dissect_s1ap, proto_s1ap);
278
279   /* Register dissector tables */
280   s1ap_ies_dissector_table = register_dissector_table("s1ap.ies", "S1AP-PROTOCOL-IES", FT_UINT32, BASE_DEC, DISSECTOR_TABLE_ALLOW_DUPLICATE);
281   s1ap_ies_p1_dissector_table = register_dissector_table("s1ap.ies.pair.first", "S1AP-PROTOCOL-IES-PAIR FirstValue", FT_UINT32, BASE_DEC, DISSECTOR_TABLE_ALLOW_DUPLICATE);
282   s1ap_ies_p2_dissector_table = register_dissector_table("s1ap.ies.pair.second", "S1AP-PROTOCOL-IES-PAIR SecondValue", FT_UINT32, BASE_DEC, DISSECTOR_TABLE_ALLOW_DUPLICATE);
283   s1ap_extension_dissector_table = register_dissector_table("s1ap.extension", "S1AP-PROTOCOL-EXTENSION", FT_UINT32, BASE_DEC, DISSECTOR_TABLE_ALLOW_DUPLICATE);
284   s1ap_proc_imsg_dissector_table = register_dissector_table("s1ap.proc.imsg", "S1AP-ELEMENTARY-PROCEDURE InitiatingMessage", FT_UINT32, BASE_DEC, DISSECTOR_TABLE_ALLOW_DUPLICATE);
285   s1ap_proc_sout_dissector_table = register_dissector_table("s1ap.proc.sout", "S1AP-ELEMENTARY-PROCEDURE SuccessfulOutcome", FT_UINT32, BASE_DEC, DISSECTOR_TABLE_ALLOW_DUPLICATE);
286   s1ap_proc_uout_dissector_table = register_dissector_table("s1ap.proc.uout", "S1AP-ELEMENTARY-PROCEDURE UnsuccessfulOutcome", FT_UINT32, BASE_DEC, DISSECTOR_TABLE_ALLOW_DUPLICATE);
287
288   /* Register configuration options for ports */
289   s1ap_module = prefs_register_protocol(proto_s1ap, proto_reg_handoff_s1ap);
290
291   prefs_register_uint_preference(s1ap_module, "sctp.port",
292                                  "S1AP SCTP Port",
293                                  "Set the SCTP port for S1AP messages",
294                                  10,
295                                  &gbl_s1apSctpPort);
296   prefs_register_bool_preference(s1ap_module, "dissect_container", "Dissect TransparentContainer", "Dissect TransparentContainers that are opaque to S1AP", &g_s1ap_dissect_container);
297
298 }
299
300
301
302
303