b1302e553db5b6cb1d3f5052f62269ec96b40232
[obnox/wireshark/wip.git] / asn1 / ranap / packet-ranap-template.c
1 /* packet-ranap.c
2  * Routines for UMTS Node B Application Part(RANAP) packet dissection
3  * Copyright 2005 - 2010, Anders Broman <anders.broman[AT]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  * References: 3GPP TS 25.413 version 6.6.0 Release
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <glib.h>
33 #include <epan/packet.h>
34
35 #include <epan/emem.h>
36 #include <epan/strutil.h>
37 #include <epan/asn1.h>
38 #include <epan/prefs.h>
39
40 #include "packet-ber.h"
41 #include "packet-per.h"
42 #include "packet-gsm_map.h"
43 #include "packet-ranap.h"
44 #include "packet-e212.h"
45 #include "packet-sccp.h"
46 #include "packet-gsm_a_common.h"
47
48 #ifdef _MSC_VER
49 /* disable: "warning C4146: unary minus operator applied to unsigned type, result still unsigned" */
50 #pragma warning(disable:4146)
51 #endif
52
53 #define SCCP_SSN_RANAP 142
54
55 #define PNAME  "Radio Access Network Application Part"
56 #define PSNAME "RANAP"
57 #define PFNAME "ranap"
58
59 /* Higest Ranap_ProcedureCode_value, use in heuristics */
60 #define RANAP_MAX_PC  45 /* id_RANAPenhancedRelocation =  45 */
61
62 #include "packet-ranap-val.h"
63
64 /* Initialize the protocol and registered fields */
65 static int proto_ranap = -1;
66
67 /* initialise sub-dissector handles */
68 static dissector_handle_t rrc_s_to_trnc_handle = NULL;
69 static dissector_handle_t rrc_ho_to_utran_cmd = NULL;
70
71 static int hf_ranap_imsi_digits = -1;
72 static int hf_ranap_transportLayerAddress_ipv4 = -1;
73 static int hf_ranap_transportLayerAddress_ipv6 = -1;
74
75 #include "packet-ranap-hf.c"
76
77 /* Initialize the subtree pointers */
78 static int ett_ranap = -1;
79 static int ett_ranap_TransportLayerAddress = -1;
80
81 #include "packet-ranap-ett.c"
82
83 /* Global variables */
84 static guint32 ProcedureCode;
85 static guint32 ProtocolIE_ID;
86 static guint32 ProtocolExtensionID;
87
88 /* Some IE:s identities uses the same value for different IE:s
89  * depending on PDU type:
90  * InitiatingMessage
91  * SuccessfulOutcome
92  * UnsuccessfulOutcome
93  * Outcome
94  * As a workarond a value is added to the IE:id in the .cnf file.
95  * Example:
96  * ResetResourceList                N rnsap.ies IMSG||id-IuSigConIdList  # no spaces are allowed in value as a space is delimiter
97  * PDU type is stored in a global variable and can is used in the IE decoding section.
98  */
99 /*
100  *      &InitiatingMessage                              ,
101  *      &SuccessfulOutcome                              OPTIONAL,
102  *      &UnsuccessfulOutcome                            OPTIONAL,
103  *      &Outcome                                        OPTIONAL,
104  *
105  * Only these two needed currently 
106  */
107 #define IMSG (1<<16)
108 #define SOUT (2<<16)
109 #define SPECIAL (4<<16)
110
111 int pdu_type = 0; /* 0 means wildcard */
112
113 /* Initialise the Preferences */
114 static gint global_ranap_sccp_ssn = SCCP_SSN_RANAP;
115
116 /* Dissector tables */
117 static dissector_table_t ranap_ies_dissector_table;
118 static dissector_table_t ranap_ies_p1_dissector_table;
119 static dissector_table_t ranap_ies_p2_dissector_table;
120 static dissector_table_t ranap_extension_dissector_table;
121 static dissector_table_t ranap_proc_imsg_dissector_table;
122 static dissector_table_t ranap_proc_sout_dissector_table;
123 static dissector_table_t ranap_proc_uout_dissector_table;
124 static dissector_table_t ranap_proc_out_dissector_table;
125 static dissector_table_t nas_pdu_dissector_table;
126
127 static int dissect_ProtocolIEFieldValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
128 static int dissect_ProtocolIEFieldPairFirstValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
129 static int dissect_ProtocolIEFieldPairSecondValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
130 static int dissect_ProtocolExtensionFieldExtensionValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
131 static int dissect_InitiatingMessageValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
132 static int dissect_SuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
133 static int dissect_UnsuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
134 static int dissect_OutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
135
136 static int dissect_ranap_SourceRNC_ToTargetRNC_TransparentContainer(tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index);
137 static int dissect_ranap_TargetRNC_ToSourceRNC_TransparentContainer(tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index);
138
139 void proto_reg_handoff_ranap(void);
140
141 #include "packet-ranap-fn.c"
142
143 static int dissect_ProtocolIEFieldValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
144 {
145
146   int ret = 0;
147   int key;
148
149   /* Special handling, same ID used for different IE's depending on signal */
150   switch(ProcedureCode){
151           case id_RelocationPreparation:
152                   if((ProtocolIE_ID == id_Source_ToTarget_TransparentContainer)||(ProtocolIE_ID == id_Target_ToSource_TransparentContainer)){
153                           key = SPECIAL | ProtocolIE_ID;
154                           ret = (dissector_try_port_new(ranap_ies_dissector_table, key, tvb, pinfo, tree, FALSE)) ? tvb_length(tvb) : 0;
155                           break;
156                   }
157                   /* Fall trough */
158           default:
159                   /* no special handling */
160                   ret = (dissector_try_port_new(ranap_ies_dissector_table, ProtocolIE_ID, tvb, pinfo, tree, FALSE)) ? tvb_length(tvb) : 0;
161                   if (ret == 0) {
162                           key = pdu_type | ProtocolIE_ID;
163                           ret = (dissector_try_port_new(ranap_ies_dissector_table, key, tvb, pinfo, tree, FALSE)) ? tvb_length(tvb) : 0;
164                   }
165                   break;
166   }
167   return ret;
168 }
169
170 static int dissect_ProtocolIEFieldPairFirstValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
171 {
172   return (dissector_try_port_new(ranap_ies_p1_dissector_table, ProtocolIE_ID, tvb, pinfo, tree, FALSE)) ? tvb_length(tvb) : 0;
173 }
174
175 static int dissect_ProtocolIEFieldPairSecondValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
176 {
177   return (dissector_try_port_new(ranap_ies_p2_dissector_table, ProtocolIE_ID, tvb, pinfo, tree, FALSE)) ? tvb_length(tvb) : 0;
178 }
179
180 static int dissect_ProtocolExtensionFieldExtensionValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
181 {
182   return (dissector_try_port_new(ranap_extension_dissector_table, ProtocolExtensionID, tvb, pinfo, tree, FALSE)) ? tvb_length(tvb) : 0;
183 }
184
185 static int dissect_InitiatingMessageValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
186 {
187   pdu_type = IMSG;
188   return (dissector_try_port_new(ranap_proc_imsg_dissector_table, ProcedureCode, tvb, pinfo, tree, FALSE)) ? tvb_length(tvb) : 0;
189   pdu_type = 0;
190 }
191
192 static int dissect_SuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
193 {
194   pdu_type = SOUT;
195   return (dissector_try_port_new(ranap_proc_sout_dissector_table, ProcedureCode, tvb, pinfo, tree, FALSE)) ? tvb_length(tvb) : 0;
196   pdu_type = 0;
197 }
198
199 static int dissect_UnsuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
200 {
201   return (dissector_try_port_new(ranap_proc_uout_dissector_table, ProcedureCode, tvb, pinfo, tree, FALSE)) ? tvb_length(tvb) : 0;
202 }
203
204 static int dissect_OutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
205 {
206   return (dissector_try_port_new(ranap_proc_out_dissector_table, ProcedureCode, tvb, pinfo, tree, FALSE)) ? tvb_length(tvb) : 0;
207 }
208
209 static void
210 dissect_ranap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
211 {
212         proto_item      *ranap_item = NULL;
213         proto_tree      *ranap_tree = NULL;
214
215         pdu_type = 0;
216         ProtocolIE_ID = 0;
217
218         /* make entry in the Protocol column on summary display */
219         col_set_str(pinfo->cinfo, COL_PROTOCOL, "RANAP");
220
221         /* create the ranap protocol tree */
222         ranap_item = proto_tree_add_item(tree, proto_ranap, tvb, 0, -1, FALSE);
223         ranap_tree = proto_item_add_subtree(ranap_item, ett_ranap);
224
225         dissect_RANAP_PDU_PDU(tvb, pinfo, ranap_tree);
226         if (pinfo->sccp_info) {
227                 sccp_msg_info_t* sccp_msg_lcl = pinfo->sccp_info;
228
229                 if (sccp_msg_lcl->data.co.assoc)
230                         sccp_msg_lcl->data.co.assoc->payload = SCCP_PLOAD_RANAP;
231
232                 if (! sccp_msg_lcl->data.co.label && ProcedureCode != 0xFFFFFFFF) {
233                         const gchar* str = val_to_str(ProcedureCode, ranap_ProcedureCode_vals,"Unknown RANAP");
234                         sccp_msg_lcl->data.co.label = se_strdup(str);
235                 }
236         }
237 }
238
239 static gboolean
240 dissect_sccp_ranap_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
241 {
242     guint8 temp;
243         asn1_ctx_t asn1_ctx;
244         guint length;
245         int offset;
246
247         asn1_ctx_init(&asn1_ctx, ASN1_ENC_PER, TRUE, pinfo);
248
249     /* Is it a ranap packet?
250      *
251      * 4th octet should be the length of the rest of the message.
252      * 2nd octet is the message-type e Z[0, 28]
253      * (obviously there must be at least four octets)
254      *
255      * If both hold true we'll assume its RANAP
256      */
257
258     #define LENGTH_OFFSET 3
259     #define MSG_TYPE_OFFSET 1
260     if (tvb_length(tvb) < 4) { return FALSE; }
261     /*if (tvb_get_guint8(tvb, LENGTH_OFFSET) != (tvb_length(tvb) - 4)) { return FALSE; }*/
262         /* Read the length NOTE offset in bits */
263         offset = dissect_per_length_determinant(tvb, LENGTH_OFFSET<<3, &asn1_ctx, tree, -1, &length);
264         offset = offset>>3;
265         if (length!= (tvb_length(tvb) - offset)){
266                 return FALSE; 
267         }
268
269     temp = tvb_get_guint8(tvb, MSG_TYPE_OFFSET);
270     if (temp > RANAP_MAX_PC) { return FALSE; }
271
272     dissect_ranap(tvb, pinfo, tree);
273
274     return TRUE;
275 }
276
277 /*--- proto_register_ranap -------------------------------------------*/
278 void proto_register_ranap(void) {
279   module_t *ranap_module;
280
281   /* List of fields */
282
283   static hf_register_info hf[] = {
284         { &hf_ranap_imsi_digits,
285       { "IMSI digits", "ranap.imsi_digits",
286         FT_STRING, BASE_NONE, NULL, 0,
287         NULL, HFILL }},
288     { &hf_ranap_transportLayerAddress_ipv4,
289       { "transportLayerAddress IPv4", "ranap.transportLayerAddress_ipv4",
290         FT_IPv4, BASE_NONE, NULL, 0,
291         NULL, HFILL }},
292     { &hf_ranap_transportLayerAddress_ipv6,
293       { "transportLayerAddress IPv6", "ranap.transportLayerAddress_ipv6",
294         FT_IPv6, BASE_NONE, NULL, 0,
295         NULL, HFILL }},
296
297
298 #include "packet-ranap-hfarr.c"
299   };
300
301   /* List of subtrees */
302   static gint *ett[] = {
303                   &ett_ranap,
304                   &ett_ranap_TransportLayerAddress,
305 #include "packet-ranap-ettarr.c"
306   };
307
308
309   /* Register protocol */
310   proto_ranap = proto_register_protocol(PNAME, PSNAME, PFNAME);
311   /* Register fields and subtrees */
312   proto_register_field_array(proto_ranap, hf, array_length(hf));
313   proto_register_subtree_array(ett, array_length(ett));
314
315   /* Register dissector */
316   register_dissector("ranap", dissect_ranap, proto_ranap);
317
318   /* Register dissector tables */
319   ranap_ies_dissector_table = register_dissector_table("ranap.ies", "RANAP-PROTOCOL-IES", FT_UINT32, BASE_DEC);
320   ranap_ies_p1_dissector_table = register_dissector_table("ranap.ies.pair.first", "RANAP-PROTOCOL-IES-PAIR FirstValue", FT_UINT32, BASE_DEC);
321   ranap_ies_p2_dissector_table = register_dissector_table("ranap.ies.pair.second", "RANAP-PROTOCOL-IES-PAIR SecondValue", FT_UINT32, BASE_DEC);
322   ranap_extension_dissector_table = register_dissector_table("ranap.extension", "RANAP-PROTOCOL-EXTENSION", FT_UINT32, BASE_DEC);
323   ranap_proc_imsg_dissector_table = register_dissector_table("ranap.proc.imsg", "RANAP-ELEMENTARY-PROCEDURE InitiatingMessage", FT_UINT32, BASE_DEC);
324   ranap_proc_sout_dissector_table = register_dissector_table("ranap.proc.sout", "RANAP-ELEMENTARY-PROCEDURE SuccessfulOutcome", FT_UINT32, BASE_DEC);
325   ranap_proc_uout_dissector_table = register_dissector_table("ranap.proc.uout", "RANAP-ELEMENTARY-PROCEDURE UnsuccessfulOutcome", FT_UINT32, BASE_DEC);
326   ranap_proc_out_dissector_table = register_dissector_table("ranap.proc.out", "RANAP-ELEMENTARY-PROCEDURE Outcome", FT_UINT32, BASE_DEC);
327
328   nas_pdu_dissector_table = register_dissector_table("ranap.nas_pdu", "RANAP NAS PDU", FT_UINT8, BASE_DEC);
329
330   ranap_module = prefs_register_protocol(proto_ranap, proto_reg_handoff_ranap);
331   prefs_register_uint_preference(ranap_module, "sccp_ssn", "SCCP SSN for RANAP",
332                                  "The SCCP SubSystem Number for RANAP (default 142)", 10,
333                                  &global_ranap_sccp_ssn);
334 }
335
336
337 /*--- proto_reg_handoff_ranap ---------------------------------------*/
338 void
339 proto_reg_handoff_ranap(void)
340 {
341         static gboolean initialized = FALSE;
342         static dissector_handle_t ranap_handle;
343         static gint local_ranap_sccp_ssn;
344
345         if (!initialized) {
346                 ranap_handle = find_dissector("ranap");
347                 rrc_s_to_trnc_handle = find_dissector("rrc.s_to_trnc_cont");
348                 rrc_ho_to_utran_cmd = find_dissector("rrc.irat.ho_to_utran_cmd");
349                 initialized = TRUE;
350 #include "packet-ranap-dis-tab.c"
351         } else {
352                 dissector_delete("sccp.ssn", local_ranap_sccp_ssn, ranap_handle);
353         }
354
355         dissector_add("sccp.ssn", global_ranap_sccp_ssn, ranap_handle);
356         local_ranap_sccp_ssn = global_ranap_sccp_ssn;
357         /* Add heuristic dissector
358         * Perhaps we want a preference whether the heuristic dissector
359         * is or isn't enabled
360         */
361         heur_dissector_add("sccp", dissect_sccp_ranap_heur, proto_ranap); 
362         heur_dissector_add("sua", dissect_sccp_ranap_heur, proto_ranap); 
363 }
364
365