blob: 46203e025e42c0314b15cb088fc0c32278c4bc7d [file] [log] [blame]
Harald Welte77847ad2015-10-06 22:07:04 +02001/* common RANAP code */
2
3/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
Harald Welteac9c0242015-09-10 21:18:16 +020021#include <stdint.h>
22
23#include <osmocom/core/msgb.h>
Harald Welte37704d92015-12-26 08:42:31 +010024#include <osmocom/gsm/gsm48.h>
Harald Welteac9c0242015-09-10 21:18:16 +020025
Neels Hofmeyr96979af2016-01-05 15:19:44 +010026#include <osmocom/ranap/ranap_common.h>
Neels Hofmeyrf898d6b2017-12-19 03:12:08 +010027#include <asn1c/asn1helpers.h>
Harald Welteac9c0242015-09-10 21:18:16 +020028
29extern int asn1_xer_print;
Harald Weltebdf3fd12016-01-03 17:04:09 +010030int _ranap_DRANAP = 0;
31#define DRANAP _ranap_DRANAP
Harald Welteac9c0242015-09-10 21:18:16 +020032
Harald Welteefef6fb2015-12-25 15:35:01 +010033const struct value_string ranap_presence_vals[5] = {
34 { RANAP_RANAP_PDU_PR_initiatingMessage, "Initiating" },
35 { RANAP_RANAP_PDU_PR_successfulOutcome, "Successful Outcome" },
36 { RANAP_RANAP_PDU_PR_unsuccessfulOutcome, "Unsuccessful Outcome" },
37 { RANAP_RANAP_PDU_PR_outcome, "(Intermediate) Outcome" },
38 { 0, NULL }
39};
40
41const struct value_string ranap_procedure_code_vals[48] = {
42 { RANAP_ProcedureCode_id_RAB_Assignment, "RAB Assignment" },
43 { RANAP_ProcedureCode_id_Iu_Release, "Iu Release" },
44 { RANAP_ProcedureCode_id_RelocationPreparation, "Relocation Preparation" },
45 { RANAP_ProcedureCode_id_RelocationResourceAllocation, "Relocation Resource Allocation" },
46 { RANAP_ProcedureCode_id_RelocationCancel, "Relocation Cancel" },
47 { RANAP_ProcedureCode_id_SRNS_ContextTransfer, "SRNS Context Transfer" },
48 { RANAP_ProcedureCode_id_SecurityModeControl, "Security Mode Control" },
49 { RANAP_ProcedureCode_id_DataVolumeReport, "Data Volume Report" },
50 { RANAP_ProcedureCode_id_Reset, "Reset" },
51 { RANAP_ProcedureCode_id_RAB_ReleaseRequest, "RAB Release Request" },
52 { RANAP_ProcedureCode_id_Iu_ReleaseRequest, "Iu Release Request" },
53 { RANAP_ProcedureCode_id_RelocationDetect, "Relocation Detect" },
54 { RANAP_ProcedureCode_id_RelocationComplete, "Relocation Complete" },
55 { RANAP_ProcedureCode_id_Paging, "Paging" },
56 { RANAP_ProcedureCode_id_CommonID, "Common ID" },
57 { RANAP_ProcedureCode_id_CN_InvokeTrace, "CN Invoke Trace" },
58 { RANAP_ProcedureCode_id_LocationReportingControl, "Location Reporting Control" },
59 { RANAP_ProcedureCode_id_LocationReport, "Location Report" },
60 { RANAP_ProcedureCode_id_InitialUE_Message, "Initial UE Message" },
61 { RANAP_ProcedureCode_id_DirectTransfer, "Direct Transfer" },
62 { RANAP_ProcedureCode_id_OverloadControl, "Overload Control" },
63 { RANAP_ProcedureCode_id_ErrorIndication, "Error Indication" },
64 { RANAP_ProcedureCode_id_SRNS_DataForward, "SRNS Data Forward" },
65 { RANAP_ProcedureCode_id_ForwardSRNS_Context, "Forward SRNS Context" },
66 { RANAP_ProcedureCode_id_privateMessage, "Private Message" },
67 { RANAP_ProcedureCode_id_CN_DeactivateTrace, "CN Deactivate Trace" },
68 { RANAP_ProcedureCode_id_ResetResource, "Reset Resource" },
69 { RANAP_ProcedureCode_id_RANAP_Relocation, "RANAP Relocation" },
70 { RANAP_ProcedureCode_id_RAB_ModifyRequest, "RAB Modify Request" },
71 { RANAP_ProcedureCode_id_LocationRelatedData, "Location Related Data" },
72 { RANAP_ProcedureCode_id_InformationTransfer, "Information Transfer" },
73 { RANAP_ProcedureCode_id_UESpecificInformation, "UE Specific Information" },
74 { RANAP_ProcedureCode_id_UplinkInformationExchange, "Uplink Information Transfer" },
75 { RANAP_ProcedureCode_id_DirectInformationTransfer, "Direct Information Transfer" },
76 { RANAP_ProcedureCode_id_MBMSSessionStart, "MBMS Session Start" },
77 { RANAP_ProcedureCode_id_MBMSSessionUpdate, "MBMS Session Update" },
78 { RANAP_ProcedureCode_id_MBMSSessionStop, "MBMS Session Stop" },
79 { RANAP_ProcedureCode_id_MBMSUELinking, "MBMS UE Linking" },
80 { RANAP_ProcedureCode_id_MBMSRegistration, "MBMS Registration" },
81 { RANAP_ProcedureCode_id_MBMSCNDe_Registration_Procedure, "MBMS CN De-Registration" },
82 { RANAP_ProcedureCode_id_MBMSRABEstablishmentIndication, "MBMS RAB Establishment Indication" },
83 { RANAP_ProcedureCode_id_MBMSRABRelease, "MBMS RAB Release" },
84 { RANAP_ProcedureCode_id_enhancedRelocationComplete, "Enhanced Relocation Complete" },
85 { RANAP_ProcedureCode_id_enhancedRelocationCompleteConfirm, "Enhanced Relocation Complete Confirm" },
86 { RANAP_ProcedureCode_id_RANAPenhancedRelocation, "RANAP Enhanced Relocation" },
87 { RANAP_ProcedureCode_id_SRVCCPreparation, "SRVCC Preparation" },
88 { RANAP_ProcedureCode_id_UeRadioCapabilityMatch, "UE Radio Capability Match" },
89 { 0, NULL }
90};
91
Harald Welte3d39f842015-12-25 09:40:07 +010092static const struct value_string ranap_cause_radio_vals[] = {
93 { RANAP_CauseRadioNetwork_rab_pre_empted, "RAB pre-empted" },
94 { RANAP_CauseRadioNetwork_trelocoverall_expiry, "Treloc_overall expiry" },
95 { RANAP_CauseRadioNetwork_trelocprep_expiry, "Treloc_prep expiry" },
96 { RANAP_CauseRadioNetwork_treloccomplete_expiry,"Treloc_complete expiry" },
97 { RANAP_CauseRadioNetwork_tqueing_expiry, "Tqueueing expiry" },
98 { RANAP_CauseRadioNetwork_relocation_triggered, "Relocation triggered" },
99 { RANAP_CauseRadioNetwork_trellocalloc_expiry, "Treloc_alloc expiry" },
100 { RANAP_CauseRadioNetwork_unable_to_establish_during_relocation,
101 "unable to establish during relocation" },
102 { RANAP_CauseRadioNetwork_unknown_target_rnc,
103 "unknown target RNC" },
104 { RANAP_CauseRadioNetwork_relocation_cancelled, "Relocation cancelled" },
105 { RANAP_CauseRadioNetwork_successful_relocation, "Successful relocation" },
106 { RANAP_CauseRadioNetwork_requested_ciphering_and_or_integrity_protection_algorithms_not_supported,
107 "requested ciph. and/or int. prot. algorithms not supported" },
108 { RANAP_CauseRadioNetwork_conflict_with_already_existing_integrity_protection_and_or_ciphering_information,
109 "conflict with existing int. prot. and/or ciph. information" },
110 { RANAP_CauseRadioNetwork_failure_in_the_radio_interface_procedure,
111 "failure in the radio interface procedure" },
112 { RANAP_CauseRadioNetwork_release_due_to_utran_generated_reason,
113 "release due to UTRAN generated reason" },
114 { RANAP_CauseRadioNetwork_user_inactivity, "user inactivity" },
115 { RANAP_CauseRadioNetwork_time_critical_relocation,
116 "time critical relocation" },
117 { RANAP_CauseRadioNetwork_requested_traffic_class_not_available,
118 "requested traffic class not available" },
119 { RANAP_CauseRadioNetwork_invalid_rab_parameters_value,
120 "invalid RAB parameters value" },
121 { RANAP_CauseRadioNetwork_requested_maximum_bit_rate_not_available,
122 "requested max. bit-rate not available" },
123 { RANAP_CauseRadioNetwork_requested_guaranteed_bit_rate_not_available,
124 "requested guaranteed bit-rate not available" },
125 { RANAP_CauseRadioNetwork_requested_transfer_delay_not_achievable,
126 "requested transfer delay not achievable" },
127 { RANAP_CauseRadioNetwork_invalid_rab_parameters_combination,
128 "invalid RAB parameters combination" },
129 { RANAP_CauseRadioNetwork_condition_violation_for_sdu_parameters,
130 "condition violation for SDU parameters" },
131 { RANAP_CauseRadioNetwork_condition_violation_for_traffic_handling_priority,
132 "condition violation for traffic handling priority" },
133 { RANAP_CauseRadioNetwork_condition_violation_for_guaranteed_bit_rate,
134 "condition violation for guaranteed bit-rate" },
135 { RANAP_CauseRadioNetwork_user_plane_versions_not_supported,
136 "user-plane versions not supported" },
137 { RANAP_CauseRadioNetwork_iu_up_failure, "Iu-UP failure" },
138 { RANAP_CauseRadioNetwork_relocation_failure_in_target_CN_RNC_or_target_system,
139 "relocation failure in target CN/RNC or target system" },
140 { RANAP_CauseRadioNetwork_invalid_RAB_ID, "Invalid RAB ID" },
141 { RANAP_CauseRadioNetwork_no_remaining_rab, "No remaining RAB" },
142 { RANAP_CauseRadioNetwork_interaction_with_other_procedure,
143 "interaction with other procedure" },
144 { RANAP_CauseRadioNetwork_requested_maximum_bit_rate_for_dl_not_available,
145 "requested maximum bit-rate for DL not available" },
146 { RANAP_CauseRadioNetwork_requested_maximum_bit_rate_for_ul_not_available,
147 "requested maximum bit-rate for UL not available" },
148 { RANAP_CauseRadioNetwork_requested_guaranteed_bit_rate_for_dl_not_available,
149 "requested guaranteed bit-rate for DL not available" },
150 { RANAP_CauseRadioNetwork_requested_guaranteed_bit_rate_for_ul_not_available,
151 "requested guaranteed bit-rate for UL not available" },
152 { RANAP_CauseRadioNetwork_repeated_integrity_checking_failure,
153 "repeated integrity checking failure" },
154 { RANAP_CauseRadioNetwork_requested_request_type_not_supported,
155 "requested request type not supported" },
156 { RANAP_CauseRadioNetwork_request_superseded, "request superseded" },
157 { RANAP_CauseRadioNetwork_release_due_to_UE_generated_signalling_connection_release,
158 "release due to UE-generated signalling connection release" },
159 { RANAP_CauseRadioNetwork_resource_optimisation_relocation,
160 "resource optimisation relocation" },
161 { RANAP_CauseRadioNetwork_requested_information_not_available,
162 "requested information not available" },
163 { RANAP_CauseRadioNetwork_relocation_desirable_for_radio_reasons,
164 "relocation desirable for radio reasons" },
165 { RANAP_CauseRadioNetwork_relocation_not_supported_in_target_RNC_or_target_system,
166 "relocation not supported in target RNC or target system" },
167 { RANAP_CauseRadioNetwork_directed_retry, "directed retry" },
168 { RANAP_CauseRadioNetwork_radio_connection_with_UE_Lost,
169 "radio connection with UE lost" },
170 { RANAP_CauseRadioNetwork_rNC_unable_to_establish_all_RFCs,
171 "RNC unable to establish al RFCs" },
172 { RANAP_CauseRadioNetwork_deciphering_keys_not_available,
173 "de-ciphering keys not available" },
174 { RANAP_CauseRadioNetwork_dedicated_assistance_data_not_available,
175 "dedicated assistance data not available" },
176 { RANAP_CauseRadioNetwork_relocation_target_not_allowed,
177 "relocation target not allowed" },
178 { RANAP_CauseRadioNetwork_location_reporting_congestion,
179 "relocation reporting congestion" },
180 { RANAP_CauseRadioNetwork_reduce_load_in_serving_cell,
181 "reduce load in serving cell" },
182 { RANAP_CauseRadioNetwork_no_radio_resources_available_in_target_cell,
183 "no radio resources available in target cell" },
184 { RANAP_CauseRadioNetwork_gERAN_Iumode_failure,
185 "GERAN Iu-Mode failure" },
186 { RANAP_CauseRadioNetwork_access_restricted_due_to_shared_networks,
187 "access restricted due to shared networks" },
188 { RANAP_CauseRadioNetwork_incoming_relocation_not_supported_due_to_PUESBINE_feature,
189 "incoming relocation not supported due to PUESBINE feature" },
190 { RANAP_CauseRadioNetwork_traffic_load_in_the_target_cell_higher_than_in_the_source_cell,
191 "traffic load in the target cell higher than in the source cell" },
192 { RANAP_CauseRadioNetwork_mBMS_no_multicast_service_for_this_UE,
193 "MBMS: no multicast service for this UE" },
194 { RANAP_CauseRadioNetwork_mBMS_unknown_UE_ID, "MBMS: unknown UE ID" },
195 { RANAP_CauseRadioNetwork_successful_MBMS_session_start_no_data_bearer_necessary,
196 "successful MBMS session start; no data bearer necessarry" },
197 { RANAP_CauseRadioNetwork_mBMS_superseded_due_to_NNSF,
198 "MBMS superseded due to NNSF" },
199 { RANAP_CauseRadioNetwork_mBMS_UE_linking_already_done,
200 "MBMS: UE linking already done" },
201 { RANAP_CauseRadioNetwork_mBMS_UE_de_linking_failure_no_existing_UE_linking,
202 "MBMS: UE de-linking failure; no existing UE linking" },
203 { RANAP_CauseRadioNetwork_tMGI_unknown, "TMGI unknown" },
204 { 0, NULL }
205};
206
207static const struct value_string ranap_cause_transm_vals[] = {
208 { RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure,
209 "signalling transport resource failure" },
210 { RANAP_CauseTransmissionNetwork_iu_transport_connection_failed_to_establish,
211 "Iu transport connection failed to establish" },
212 { 0, NULL }
213};
214
215static const struct value_string ranap_cause_nas_vals[] = {
216 { RANAP_CauseNAS_user_restriction_start_indication,
217 "user restriction; start indication" },
218 { RANAP_CauseNAS_user_restriction_end_indication,
219 "user restriction; stop indication" },
220 { RANAP_CauseNAS_normal_release,
221 "normal release" },
222 { RANAP_CauseNAS_csg_subscription_expiry,
223 "CSG subscription expiry" },
224 { 0, NULL }
225};
226
227static const struct value_string ranap_cause_prot_vals[] = {
228 { RANAP_CauseProtocol_transfer_syntax_error,
229 "transfer sytax error" },
230 { RANAP_CauseProtocol_semantic_error,
231 "semantic error" },
232 { RANAP_CauseProtocol_message_not_compatible_with_receiver_state,
233 "message not compatible with receiver state" },
234 { RANAP_CauseProtocol_abstract_syntax_error_reject,
235 "syntax error: reject" },
236 { RANAP_CauseProtocol_abstract_syntax_error_ignore_and_notify,
237 "syntax error: ignore and notify" },
238 { RANAP_CauseProtocol_abstract_syntax_error_falsely_constructed_message,
239 "syntax error: falsely constructed message" },
240 { 0, NULL }
241};
242
243static const struct value_string ranap_cause_misc_vals[] = {
244 { RANAP_CauseMisc_om_intervention, "OAM intervention" },
245 { RANAP_CauseMisc_no_resource_available, "no resource available" },
246 { RANAP_CauseMisc_unspecified_failure, "unspecified failure" },
247 { RANAP_CauseMisc_network_optimisation, "network optimisation" },
248 { 0, NULL }
249};
250
251char *ranap_cause_str(const RANAP_Cause_t *cause)
252{
253 static char buf[128];
254
255 switch (cause->present) {
256 case RANAP_Cause_PR_radioNetwork:
257 snprintf(buf, sizeof(buf), "radio(%s)",
258 get_value_string(ranap_cause_radio_vals,
259 cause->choice.radioNetwork));
260 break;
261 case RANAP_Cause_PR_transmissionNetwork:
262 snprintf(buf, sizeof(buf), "transmission(%s)",
263 get_value_string(ranap_cause_transm_vals,
264 cause->choice.transmissionNetwork));
265 break;
266 case RANAP_Cause_PR_nAS:
267 snprintf(buf, sizeof(buf), "nas(%s)",
268 get_value_string(ranap_cause_nas_vals,
269 cause->choice.nAS));
270 break;
271 case RANAP_Cause_PR_protocol:
272 snprintf(buf, sizeof(buf), "protocol(%s)",
273 get_value_string(ranap_cause_prot_vals,
274 cause->choice.protocol));
275 break;
276 case RANAP_Cause_PR_misc:
277 snprintf(buf, sizeof(buf), "misc(%s)",
278 get_value_string(ranap_cause_misc_vals,
279 cause->choice.misc));
280 break;
281 case RANAP_Cause_PR_non_Standard:
Neels Hofmeyrf898d6b2017-12-19 03:12:08 +0100282 snprintf(buf, sizeof(buf), "non-standard(%ld)",
Harald Welte3d39f842015-12-25 09:40:07 +0100283 cause->choice.non_Standard);
284 break;
285 default:
286 strcpy(buf, "unknown");
287 break;
288 }
289 return buf;
290}
291
292
Harald Welteac9c0242015-09-10 21:18:16 +0200293static struct msgb *ranap_msgb_alloc(void)
294{
Neels Hofmeyr3b426582017-07-03 14:40:11 +0200295 return msgb_alloc_headroom(1024+512, 512, "RANAP Tx");
Harald Welteac9c0242015-09-10 21:18:16 +0200296}
297
Harald Welte8dacb072015-12-16 20:27:14 +0100298static struct msgb *_ranap_gen_msg(RANAP_RANAP_PDU_t *pdu)
Harald Welteac9c0242015-09-10 21:18:16 +0200299{
Harald Weltecbaaeef2015-12-16 20:17:26 +0100300 struct msgb *msg = ranap_msgb_alloc();
301 asn_enc_rval_t rval;
Harald Welteac9c0242015-09-10 21:18:16 +0200302
Harald Welte8dacb072015-12-16 20:27:14 +0100303 if (!msg)
Harald Weltecbaaeef2015-12-16 20:17:26 +0100304 return NULL;
Harald Welteac9c0242015-09-10 21:18:16 +0200305
Harald Welte8dacb072015-12-16 20:27:14 +0100306 rval = aper_encode_to_buffer(&asn_DEF_RANAP_RANAP_PDU, pdu,
Harald Weltecbaaeef2015-12-16 20:17:26 +0100307 msg->data, msgb_tailroom(msg));
308 if (rval.encoded < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100309 LOGP(DRANAP, LOGL_ERROR, "Error encoding type: %s\n",
Harald Weltecbaaeef2015-12-16 20:17:26 +0100310 rval.failed_type->name);
311
312 }
313
314 msgb_put(msg, rval.encoded/8);
315
316 return msg;
Harald Welteac9c0242015-09-10 21:18:16 +0200317}
Harald Welteac9c0242015-09-10 21:18:16 +0200318
Harald Welte8dacb072015-12-16 20:27:14 +0100319struct msgb *ranap_generate_initiating_message(e_RANAP_ProcedureCode procedureCode,
320 RANAP_Criticality_t criticality,
321 asn_TYPE_descriptor_t *td, void *sptr)
322{
323 RANAP_RANAP_PDU_t pdu;
Harald Welte26765542015-12-18 13:33:00 +0100324 struct msgb *msg;
Harald Welte8dacb072015-12-16 20:27:14 +0100325 int rc;
326
327 memset(&pdu, 0, sizeof(pdu));
328
329 pdu.present = RANAP_RANAP_PDU_PR_initiatingMessage;
330 pdu.choice.initiatingMessage.procedureCode = procedureCode;
331 pdu.choice.initiatingMessage.criticality = criticality;
332 rc = ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr);
333 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100334 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welte8dacb072015-12-16 20:27:14 +0100335 return NULL;
336 }
337
Harald Welte26765542015-12-18 13:33:00 +0100338 msg = _ranap_gen_msg(&pdu);
339 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu);
340
341 return msg;
Harald Welte8dacb072015-12-16 20:27:14 +0100342}
343
Harald Welteac9c0242015-09-10 21:18:16 +0200344struct msgb *ranap_generate_successful_outcome(
345 e_RANAP_ProcedureCode procedureCode,
346 RANAP_Criticality_t criticality,
347 asn_TYPE_descriptor_t * td,
348 void *sptr)
349{
Harald Welteac9c0242015-09-10 21:18:16 +0200350 RANAP_RANAP_PDU_t pdu;
Harald Welte26765542015-12-18 13:33:00 +0100351 struct msgb *msg;
Harald Welteac9c0242015-09-10 21:18:16 +0200352 int rc;
353
354 memset(&pdu, 0, sizeof(pdu));
Harald Welte8dacb072015-12-16 20:27:14 +0100355
Harald Welteac9c0242015-09-10 21:18:16 +0200356 pdu.present = RANAP_RANAP_PDU_PR_successfulOutcome;
357 pdu.choice.successfulOutcome.procedureCode = procedureCode;
358 pdu.choice.successfulOutcome.criticality = criticality;
359 rc = ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr);
360 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100361 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welteac9c0242015-09-10 21:18:16 +0200362 return NULL;
363 }
364
Harald Welte26765542015-12-18 13:33:00 +0100365 msg = _ranap_gen_msg(&pdu);
366 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu);
367
368 return msg;
Harald Welteac9c0242015-09-10 21:18:16 +0200369}
370
Harald Weltecbaaeef2015-12-16 20:17:26 +0100371struct msgb *ranap_generate_unsuccessful_outcome(
Harald Welteac9c0242015-09-10 21:18:16 +0200372 e_RANAP_ProcedureCode procedureCode,
373 RANAP_Criticality_t criticality,
374 asn_TYPE_descriptor_t * td,
375 void *sptr)
376{
Harald Welteac9c0242015-09-10 21:18:16 +0200377 RANAP_RANAP_PDU_t pdu;
Harald Welte26765542015-12-18 13:33:00 +0100378 struct msgb *msg;
Harald Weltecbaaeef2015-12-16 20:17:26 +0100379 int rc;
Harald Welteac9c0242015-09-10 21:18:16 +0200380
381 memset(&pdu, 0, sizeof(pdu));
382
383 pdu.present = RANAP_RANAP_PDU_PR_unsuccessfulOutcome;
Harald Weltec16117a2015-12-16 20:30:11 +0100384 pdu.choice.unsuccessfulOutcome.procedureCode = procedureCode;
385 pdu.choice.unsuccessfulOutcome.criticality = criticality;
386 rc = ANY_fromType_aper(&pdu.choice.unsuccessfulOutcome.value, td, sptr);
Harald Weltecbaaeef2015-12-16 20:17:26 +0100387 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100388 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Weltecbaaeef2015-12-16 20:17:26 +0100389 return NULL;
Harald Welteac9c0242015-09-10 21:18:16 +0200390 }
391
Harald Welte26765542015-12-18 13:33:00 +0100392 msg = _ranap_gen_msg(&pdu);
393 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu);
394
395 return msg;
Harald Welteac9c0242015-09-10 21:18:16 +0200396}
Harald Welteac9c0242015-09-10 21:18:16 +0200397
Harald Weltec16117a2015-12-16 20:30:11 +0100398struct msgb *ranap_generate_outcome(
399 e_RANAP_ProcedureCode procedureCode,
400 RANAP_Criticality_t criticality,
401 asn_TYPE_descriptor_t * td,
402 void *sptr)
403{
404 RANAP_RANAP_PDU_t pdu;
Harald Welte26765542015-12-18 13:33:00 +0100405 struct msgb *msg;
Harald Weltec16117a2015-12-16 20:30:11 +0100406 int rc;
407
408 memset(&pdu, 0, sizeof(pdu));
409
410 pdu.present = RANAP_RANAP_PDU_PR_outcome;
411 pdu.choice.outcome.procedureCode = procedureCode;
412 pdu.choice.outcome.criticality = criticality;
413 rc = ANY_fromType_aper(&pdu.choice.outcome.value, td, sptr);
414 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100415 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Weltec16117a2015-12-16 20:30:11 +0100416 return NULL;
417 }
418
Harald Welte26765542015-12-18 13:33:00 +0100419 msg = _ranap_gen_msg(&pdu);
420 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu);
421
422 return msg;
Harald Weltec16117a2015-12-16 20:30:11 +0100423}
424
425
Harald Welteac9c0242015-09-10 21:18:16 +0200426RANAP_IE_t *ranap_new_ie(RANAP_ProtocolIE_ID_t id,
427 RANAP_Criticality_t criticality,
428 asn_TYPE_descriptor_t * type, void *sptr)
429{
Harald Welteac9c0242015-09-10 21:18:16 +0200430 RANAP_IE_t *buff;
Harald Welte04329dc2015-12-18 15:17:21 +0100431 int rc;
Harald Welteac9c0242015-09-10 21:18:16 +0200432
Harald Welte9c397d42015-12-18 13:33:20 +0100433 if ((buff = CALLOC(1, sizeof(*buff))) == NULL) {
Harald Welteac9c0242015-09-10 21:18:16 +0200434 // Possible error on malloc
435 return NULL;
436 }
Harald Welteac9c0242015-09-10 21:18:16 +0200437
438 buff->id = id;
439 buff->criticality = criticality;
440
Harald Welte04329dc2015-12-18 15:17:21 +0100441 rc = ANY_fromType_aper(&buff->value, type, sptr);
442 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100443 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welte04329dc2015-12-18 15:17:21 +0100444 FREEMEM(buff);
445 return NULL;
446 }
Harald Welteac9c0242015-09-10 21:18:16 +0200447
448 if (asn1_xer_print)
449 if (xer_fprint(stdout, &asn_DEF_RANAP_IE, buff) < 0) {
Harald Welte62939132015-12-18 14:57:04 +0100450 FREEMEM(buff);
Harald Welteac9c0242015-09-10 21:18:16 +0200451 return NULL;
452 }
453
454 return buff;
455}
Harald Welteace1d242015-12-16 23:07:19 +0100456
457RANAP_ProtocolIE_FieldPair_t *ranap_new_ie_pair(RANAP_ProtocolIE_ID_t id,
458 RANAP_Criticality_t criticality1,
459 asn_TYPE_descriptor_t *type1, void *sptr1,
460 RANAP_Criticality_t criticality2,
461 asn_TYPE_descriptor_t *type2, void *sptr2)
462{
Harald Welteace1d242015-12-16 23:07:19 +0100463 RANAP_ProtocolIE_FieldPair_t *buff;
Harald Welte04329dc2015-12-18 15:17:21 +0100464 int rc;
Harald Welteace1d242015-12-16 23:07:19 +0100465
Harald Welte9c397d42015-12-18 13:33:20 +0100466 if ((buff = CALLOC(1, sizeof(*buff))) == NULL) {
Harald Welteace1d242015-12-16 23:07:19 +0100467 // Possible error on malloc
468 return NULL;
469 }
Harald Welteace1d242015-12-16 23:07:19 +0100470
471 buff->id = id;
472 buff->firstCriticality = criticality1;
473 buff->secondCriticality = criticality2;
474
Harald Welte04329dc2015-12-18 15:17:21 +0100475 rc = ANY_fromType_aper(&buff->firstValue, type1, sptr1);
476 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100477 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welte04329dc2015-12-18 15:17:21 +0100478 FREEMEM(buff);
479 return NULL;
480 }
481
482 rc = ANY_fromType_aper(&buff->secondValue, type2, sptr2);
483 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100484 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welte16232782015-12-18 17:22:04 +0100485 ASN_STRUCT_FREE(asn_DEF_RANAP_ProtocolIE_FieldPair, buff);
Harald Welte04329dc2015-12-18 15:17:21 +0100486 return NULL;
487 }
Harald Welteace1d242015-12-16 23:07:19 +0100488
489 if (asn1_xer_print)
Harald Welte62939132015-12-18 14:57:04 +0100490 if (xer_fprint(stdout, &asn_DEF_RANAP_ProtocolIE_FieldPair, buff) < 0) {
Harald Welte16232782015-12-18 17:22:04 +0100491 ASN_STRUCT_FREE(asn_DEF_RANAP_ProtocolIE_FieldPair, buff);
Harald Welteace1d242015-12-16 23:07:19 +0100492 return NULL;
493 }
494
495 return buff;
496}
Harald Welte37704d92015-12-26 08:42:31 +0100497
498int ranap_parse_lai(struct gprs_ra_id *ra_id, const RANAP_LAI_t *lai)
499{
500 uint8_t *ptr = lai->pLMNidentity.buf;
501
Neels Hofmeyrf6e56052016-04-25 13:12:58 +0200502 /* Avoid unitialized mem for parse failures, and set rac to zero (which
503 * isn't being parsed). */
504 *ra_id = (struct gprs_ra_id){ .lac = 0xfffe };
505
Harald Welte37704d92015-12-26 08:42:31 +0100506 /* TS 25.413 9.2.3.55 */
Neels Hofmeyr8f4cd862016-04-06 13:49:40 +0200507 if (lai->pLMNidentity.size != 3) {
508 LOGP(DRANAP, LOGL_ERROR, "Invalid PLMN Identity size:"
509 " should be 3, is %d\n", lai->pLMNidentity.size);
Harald Welte37704d92015-12-26 08:42:31 +0100510 return -1;
Neels Hofmeyr8f4cd862016-04-06 13:49:40 +0200511 }
Harald Welte37704d92015-12-26 08:42:31 +0100512
513 ra_id->mcc = (ptr[0] & 0xF) * 100 +
514 (ptr[0] >> 4) * 10 +
515 (ptr[1] & 0xF);
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200516 ra_id->mnc = (ptr[2] & 0xF) * 10 +
517 (ptr[2] >> 4);
Harald Welte37704d92015-12-26 08:42:31 +0100518 if ((ptr[1] >> 4) != 0xF)
519 ra_id->mnc += (ptr[1] >> 4) * 100;
520
Neels Hofmeyr8f4cd862016-04-06 13:49:40 +0200521 if (lai->lAC.size != 2) {
522 LOGP(DRANAP, LOGL_ERROR, "Invalid LAC size:"
523 " should be 2, is %d\n", lai->lAC.size);
524 return -1;
525 }
526
Harald Welte37704d92015-12-26 08:42:31 +0100527 ra_id->lac = asn1str_to_u16(&lai->lAC);
528
529 /* TS 25.413 9.2.3.6 */
Neels Hofmeyr8f4cd862016-04-06 13:49:40 +0200530 if (ra_id->lac == 0 || ra_id->lac == 0xfffe) {
531 LOGP(DRANAP, LOGL_ERROR, "Invalid LAC: 0x%hx\n",
532 ra_id->lac);
Harald Welte37704d92015-12-26 08:42:31 +0100533 return -1;
Neels Hofmeyr8f4cd862016-04-06 13:49:40 +0200534 }
Harald Welte37704d92015-12-26 08:42:31 +0100535
536 return 0;
537}
Harald Weltebdf3fd12016-01-03 17:04:09 +0100538
539void ranap_set_log_area(int log_area)
540{
541 _ranap_DRANAP = log_area;
542}
Daniel Willmann4870b992016-02-04 18:00:36 +0100543
544int ranap_ip_from_transp_layer_addr(const BIT_STRING_t *in, uint32_t *ip)
545{
Daniel Willmann4870b992016-02-04 18:00:36 +0100546 uint8_t x213[] = {0x35, 0x00, 0x01};
547
548 /* Only support IPv4 for now - plain and with x213 encapsulation */
549 if (in->size == 4)
550 *ip = *(uint32_t *)in->buf;
551 else if ((in->size == 7) && !memcmp(in->buf, x213, sizeof(x213)))
552 *ip = htonl(*(uint32_t *)&in->buf[3]);
553 else
554 return -1;
555
556 return 0;
557
558}
Daniel Willmann635fac02016-02-18 13:27:36 +0100559
560int ranap_decode_rab_setupormodifieditemies_fromlist(
561 RANAP_RAB_SetupOrModifiedItemIEs_t *raB_SetupOrModifiedItemIEs,
562 ANY_t *any_p) {
563
564 RANAP_RAB_SetupOrModifiedItem_t *ranaP_RABSetupOrModifiedItem_p = NULL;
565 int decoded = 0;
566 assert(any_p != NULL);
567 assert(raB_SetupOrModifiedItemIEs != NULL);
568
569 memset(raB_SetupOrModifiedItemIEs, 0, sizeof(RANAP_RAB_SetupOrModifiedItemIEs_t));
570 RANAP_DEBUG("Decoding message RANAP_RAB_SetupOrModifiedItemIEs (%s:%d)\n", __FILE__, __LINE__);
571 decoded = ANY_to_type_aper(any_p, &asn_DEF_RANAP_RAB_SetupOrModifiedItem, (void**)&ranaP_RABSetupOrModifiedItem_p);
572 if (decoded < 0) {
573 RANAP_DEBUG("Decoding of IE raB_SetupOrModifiedItem failed\n");
574 return -1;
575 }
576 if (asn1_xer_print)
577 xer_fprint(stdout, &asn_DEF_RANAP_RAB_SetupOrModifiedItem, ranaP_RABSetupOrModifiedItem_p);
578 memcpy(&raB_SetupOrModifiedItemIEs->raB_SetupOrModifiedItem, ranaP_RABSetupOrModifiedItem_p, sizeof(RANAP_RAB_SetupOrModifiedItem_t));
579 FREEMEM(ranaP_RABSetupOrModifiedItem_p);
580
581 return decoded;
582}
583