cb01f18ce7985e4a9bf01841edceb90a3371625b
[obnox/wireshark/wip.git] / asn1 / s1ap / packet-s1ap-template.c
1 /* packet-s1ap.c
2  * Routines for E-UTRAN S1 Application Protocol (S1AP) packet dissection
3  * Copyright 2007-2009, Anders Broman <anders.broman@ericsson.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  *
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.
20  *
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.
24  *
25  * Based on the RANAP dissector
26  *
27  * References: 3GPP TS 36.413 V8.5.0 (2009-03)
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <glib.h>
35 #include <epan/packet.h>
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <epan/strutil.h>
41 #include <epan/asn1.h>
42 #include <epan/prefs.h>
43 #include <epan/sctpppids.h>
44
45 #include "packet-ber.h"
46 #include "packet-per.h"
47 #include "packet-e212.h"
48 #include "packet-sccp.h"
49
50 #ifdef _MSC_VER
51 /* disable: "warning C4146: unary minus operator applied to unsigned type, result still unsigned" */
52 #pragma warning(disable:4146)
53 #endif
54
55 #define PNAME  "S1 Application Protocol "
56 #define PSNAME "S1AP"
57 #define PFNAME "s1ap"
58
59 /* No SCTP port registered with IANA for S1AP yet */
60 #define SCTP_PORT_S1AP  0
61
62 static dissector_handle_t nas_eps_handle;
63
64 #include "packet-s1ap-val.h"
65
66 /* Initialize the protocol and registered fields */
67 static int proto_s1ap = -1;
68
69 static int hf_s1ap_transportLayerAddressIPv4 = -1;
70 static int hf_s1ap_transportLayerAddressIPv6 = -1;
71 #include "packet-s1ap-hf.c"
72
73 /* Initialize the subtree pointers */
74 static int ett_s1ap = -1;
75 static int ett_s1ap_TransportLayerAddress = -1;
76
77 #include "packet-s1ap-ett.c"
78
79 /* Global variables */
80 static guint32 ProcedureCode;
81 static guint32 ProtocolIE_ID;
82 static guint32 ProtocolExtensionID;
83 static guint gbl_s1apSctpPort=SCTP_PORT_S1AP;
84
85 /* Dissector tables */
86 static dissector_table_t s1ap_ies_dissector_table;
87 static dissector_table_t s1ap_ies_p1_dissector_table;
88 static dissector_table_t s1ap_ies_p2_dissector_table;
89 static dissector_table_t s1ap_extension_dissector_table;
90 static dissector_table_t s1ap_proc_imsg_dissector_table;
91 static dissector_table_t s1ap_proc_sout_dissector_table;
92 static dissector_table_t s1ap_proc_uout_dissector_table;
93
94 static int dissect_ProtocolIEFieldValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
95 /* Currently not used
96 static int dissect_ProtocolIEFieldPairFirstValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
97 static int dissect_ProtocolIEFieldPairSecondValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
98 */
99 static int dissect_ProtocolExtensionFieldExtensionValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
100 static int dissect_InitiatingMessageValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
101 static int dissect_SuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
102 static int dissect_UnsuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
103
104 #include "packet-s1ap-fn.c"
105
106 static int dissect_ProtocolIEFieldValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
107 {
108   return (dissector_try_port(s1ap_ies_dissector_table, ProtocolIE_ID, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
109 }
110 /* Currently not used
111 static int dissect_ProtocolIEFieldPairFirstValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
112 {
113   return (dissector_try_port(s1ap_ies_p1_dissector_table, ProtocolIE_ID, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
114 }
115
116 static int dissect_ProtocolIEFieldPairSecondValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
117 {
118   return (dissector_try_port(s1ap_ies_p2_dissector_table, ProtocolIE_ID, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
119 }
120 */
121
122 static int dissect_ProtocolExtensionFieldExtensionValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
123 {
124   return (dissector_try_port(s1ap_extension_dissector_table, ProtocolExtensionID, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
125 }
126
127 static int dissect_InitiatingMessageValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
128 {
129   return (dissector_try_port(s1ap_proc_imsg_dissector_table, ProcedureCode, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
130 }
131
132 static int dissect_SuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
133 {
134   return (dissector_try_port(s1ap_proc_sout_dissector_table, ProcedureCode, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
135 }
136
137 static int dissect_UnsuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
138 {
139   return (dissector_try_port(s1ap_proc_uout_dissector_table, ProcedureCode, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
140 }
141
142
143 static void
144 dissect_s1ap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
145 {
146         proto_item      *s1ap_item = NULL;
147         proto_tree      *s1ap_tree = NULL;
148
149         /* make entry in the Protocol column on summary display */
150         if (check_col(pinfo->cinfo, COL_PROTOCOL))
151                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "S1AP");
152
153         /* create the s1ap protocol tree */
154         s1ap_item = proto_tree_add_item(tree, proto_s1ap, tvb, 0, -1, FALSE);
155         s1ap_tree = proto_item_add_subtree(s1ap_item, ett_s1ap);
156         
157         dissect_S1AP_PDU_PDU(tvb, pinfo, s1ap_tree);
158 }
159
160 /*--- proto_reg_handoff_s1ap ---------------------------------------*/
161 void
162 proto_reg_handoff_s1ap(void)
163 {
164         static gboolean Initialized=FALSE;
165         static dissector_handle_t s1ap_handle;
166         static guint SctpPort;
167
168         if (!Initialized) {
169                 s1ap_handle = find_dissector("s1ap");
170                 nas_eps_handle = find_dissector("nas-eps");
171                 dissector_add_handle("sctp.port", s1ap_handle);   /* for "decode-as"  */
172                 dissector_add("sctp.ppi", S1AP_PAYLOAD_PROTOCOL_ID,   s1ap_handle);
173                 Initialized=TRUE;
174 #include "packet-s1ap-dis-tab.c"
175         } else {
176                 if (SctpPort != 0) {
177                         dissector_delete("sctp.port", SctpPort, s1ap_handle);
178                 }
179         }
180
181         SctpPort=gbl_s1apSctpPort;
182         if (SctpPort != 0) {
183                 dissector_add("sctp.port", SctpPort, s1ap_handle);
184         }
185 }
186
187 /*--- proto_register_s1ap -------------------------------------------*/
188 void proto_register_s1ap(void) {
189
190   /* List of fields */
191
192   static hf_register_info hf[] = {
193     { &hf_s1ap_transportLayerAddressIPv4,
194       { "transportLayerAddress(IPv4)", "s1ap.transportLayerAddressIPv4",
195         FT_IPv4, BASE_NONE, NULL, 0,
196         NULL, HFILL }},
197     { &hf_s1ap_transportLayerAddressIPv6,
198       { "transportLayerAddress(IPv6)", "s1ap.transportLayerAddressIPv6",
199         FT_IPv4, BASE_NONE, NULL, 0,
200         NULL, HFILL }},
201
202 #include "packet-s1ap-hfarr.c"
203   };
204
205   /* List of subtrees */
206   static gint *ett[] = {
207                   &ett_s1ap,
208                   &ett_s1ap_TransportLayerAddress,
209 #include "packet-s1ap-ettarr.c"
210   };
211
212   module_t *s1ap_module;
213
214   /* Register protocol */
215   proto_s1ap = proto_register_protocol(PNAME, PSNAME, PFNAME);
216   /* Register fields and subtrees */
217   proto_register_field_array(proto_s1ap, hf, array_length(hf));
218   proto_register_subtree_array(ett, array_length(ett));
219  
220   /* Register dissector */
221   register_dissector("s1ap", dissect_s1ap, proto_s1ap);
222
223   /* Register dissector tables */
224   s1ap_ies_dissector_table = register_dissector_table("s1ap.ies", "S1AP-PROTOCOL-IES", FT_UINT32, BASE_DEC);
225   s1ap_ies_p1_dissector_table = register_dissector_table("s1ap.ies.pair.first", "S1AP-PROTOCOL-IES-PAIR FirstValue", FT_UINT32, BASE_DEC);
226   s1ap_ies_p2_dissector_table = register_dissector_table("s1ap.ies.pair.second", "S1AP-PROTOCOL-IES-PAIR SecondValue", FT_UINT32, BASE_DEC);
227   s1ap_extension_dissector_table = register_dissector_table("s1ap.extension", "S1AP-PROTOCOL-EXTENSION", FT_UINT32, BASE_DEC);
228   s1ap_proc_imsg_dissector_table = register_dissector_table("s1ap.proc.imsg", "S1AP-ELEMENTARY-PROCEDURE InitiatingMessage", FT_UINT32, BASE_DEC);
229   s1ap_proc_sout_dissector_table = register_dissector_table("s1ap.proc.sout", "S1AP-ELEMENTARY-PROCEDURE SuccessfulOutcome", FT_UINT32, BASE_DEC);
230   s1ap_proc_uout_dissector_table = register_dissector_table("s1ap.proc.uout", "S1AP-ELEMENTARY-PROCEDURE UnsuccessfulOutcome", FT_UINT32, BASE_DEC);
231   
232   /* Register configuration options for ports */
233   s1ap_module = prefs_register_protocol(proto_s1ap, proto_reg_handoff_s1ap);
234
235   prefs_register_uint_preference(s1ap_module, "sctp.port",
236                                  "S1AP SCTP Port",
237                                  "Set the SCTP port for S1AP messages",
238                                  10,
239                                  &gbl_s1apSctpPort);
240
241 }
242
243
244
245
246