blob: eb7fc937774a8d5f980710148409f785e8cd636c [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* RANAP encoding and decoding for MSC */
2/*
3 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010019 */
20
21#include <asn1c/asn1helpers.h>
22
23#include <osmocom/core/prim.h>
24#include <osmocom/core/byteswap.h>
25#include <osmocom/crypt/auth.h>
Alexander Couzens8b7d7852021-11-05 01:52:05 +010026#include <osmocom/crypt/utran_cipher.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010027#include <osmocom/gsm/gsm48.h>
28
29#include <osmocom/ranap/ranap_common_cn.h>
30#include <osmocom/ranap/ranap_msg_factory.h>
31#include <osmocom/ranap/iu_helpers.h>
32
33#include <osmocom/msc/debug.h>
34#include <osmocom/msc/msc_common.h>
35#include <osmocom/msc/sccp_ran.h>
36#include <osmocom/msc/ran_msg_iu.h>
Vadim Yanitskiy1c500442019-06-20 06:34:49 +070037#include <osmocom/msc/gsm_04_11.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010038
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010039#define LOG_RAN_IU_DEC(RAN_DEC, level, fmt, args...) \
40 LOG_RAN_DEC(RAN_DEC, DIUCS, level, "RANAP: " fmt, ## args)
41
42#define LOG_RAN_IU_ENC(FI, level, fmt, args...) \
43 LOG_RAN_ENC(FI, DIUCS, level, "RANAP: " fmt, ## args)
44
45static void ran_iu_decode_l3_initial(struct ran_dec *ran_iu_decode, const RANAP_InitialUE_MessageIEs_t *ies, const char *msg_name)
46{
47 struct msgb *ran = msgb_alloc(256, msg_name);
48 struct ran_msg ran_dec_msg;
49
50 struct osmo_plmn_id plmn;
51
52 if (ies->lai.pLMNidentity.size < 3) {
53 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_ERROR, "Too short PLMNidentity in RANAP InitialUE message\n");
54 return;
55 }
56 osmo_plmn_from_bcd(ies->lai.pLMNidentity.buf, &plmn);
57
58 struct gsm0808_cell_id cid = {
59 .id_discr = CELL_IDENT_LAI,
60 .id.lai_and_lac = {
61 .plmn = plmn,
62 .lac = asn1str_to_u16(&ies->lai.lAC),
63 },
64 };
65
66 /* TODO: really necessary to copy the RAN PDU?? */
67 ran->l3h = msgb_put(ran, ies->nas_pdu.size);
68 memcpy(ran->l3h, ies->nas_pdu.buf, ies->nas_pdu.size);
69
70 ran_dec_msg = (struct ran_msg){
71 .msg_type = RAN_MSG_COMPL_L3,
72 .msg_name = msg_name,
73 .compl_l3 = {
74 .cell_id = &cid,
75 .msg = ran,
76 },
77 };
78 ran_decoded(ran_iu_decode, &ran_dec_msg);
79
80 msgb_free(ran);
81}
82
Vadim Yanitskiy1c500442019-06-20 06:34:49 +070083static void ran_iu_decode_l3(struct ran_dec *ran_iu_decode,
84 const RANAP_DirectTransferIEs_t *ies,
85 const char *msg_name)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010086{
Vadim Yanitskiy1c500442019-06-20 06:34:49 +070087 const RANAP_NAS_PDU_t *nas_pdu = &ies->nas_pdu;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010088 struct msgb *ran = msgb_alloc(256, msg_name);
89 struct ran_msg ran_dec_msg;
90
91 /* TODO: really necessary to copy the RAN PDU?? */
92 ran->l3h = msgb_put(ran, nas_pdu->size);
93 memcpy(ran->l3h, nas_pdu->buf, nas_pdu->size);
94
Vadim Yanitskiy1c500442019-06-20 06:34:49 +070095 /* Handle optional SAPI IE */
96 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAPI_PRESENT) {
97 if (ies->sapi == RANAP_SAPI_sapi_3)
98 OMSC_LINKID_CB(ran) = UM_SAPI_SMS;
99 }
100
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100101 ran_dec_msg = (struct ran_msg){
102 .msg_type = RAN_MSG_DTAP,
103 .msg_name = msg_name,
104 .dtap = ran,
105 };
106 ran_decoded(ran_iu_decode, &ran_dec_msg);
107
108 msgb_free(ran);
109}
110
111static void ran_iu_decode_err(struct ran_dec *ran_iu_decode, const RANAP_ErrorIndicationIEs_t *ies)
112{
113 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_ERROR, "Rx Error Indication (%s)\n",
114 (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)?
115 ranap_cause_str(&ies->cause) : "no cause specified");
116}
117
118static int ran_iu_decode_rab_assignment_response_decode_setup_ies(struct ran_dec *ran_iu_decode,
119 struct ran_msg *ran_dec_msg,
120 const RANAP_RAB_SetupOrModifiedItemIEs_t *setup_ies)
121{
122 const RANAP_RAB_SetupOrModifiedItem_t *item;
123 const RANAP_TransportLayerAddress_t *transp_layer_addr;
124 const RANAP_IuTransportAssociation_t *transp_assoc;
125 uint16_t port = 0;
126 char addr[INET_ADDRSTRLEN];
127 uint8_t rab_id;
128
129 item = &setup_ies->raB_SetupOrModifiedItem;
130
131 rab_id = item->rAB_ID.buf[0];
132 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_DEBUG, "Received RAB assignment response for rab_id=%d\n", rab_id);
133
134 if (!(item->iuTransportAssociation && item->transportLayerAddress)) {
135 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_ERROR, "RAB Assignment Response does not contain RAB information\n");
136 return -1;
137 }
138
139 transp_layer_addr = item->transportLayerAddress;
140 transp_assoc = item->iuTransportAssociation;
141
142 if (ranap_transp_assoc_decode(&port, transp_assoc)) {
143 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_ERROR, "Unable to decode RTP port in RAB Assignment Response\n");
144 return -1;
145 }
146
147 if (ranap_transp_layer_addr_decode(addr, sizeof(addr), transp_layer_addr)) {
148 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_ERROR, "Unable to decode IP-Address in RAB Assignment Response\n");
149 return -1;
150 }
151
152 *ran_dec_msg = (struct ran_msg){
153 .msg_type = RAN_MSG_ASSIGNMENT_COMPLETE,
154 .msg_name = "RANAP RAB Assignment Response",
155 .assignment_complete = {
Neels Hofmeyrcec51b32023-03-01 03:47:45 +0100156 /* For codec compatibility resolution, indicate AMR-FR */
Pau Espin Pedrold7955312022-01-07 18:45:50 +0100157 .codec_present = true,
Neels Hofmeyrcec51b32023-03-01 03:47:45 +0100158 .codec = {
159 .fi = true,
160 .type = GSM0808_SCT_FR3,
161 .cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR,
162 },
163 /* Indicate that (at least) the first MGW endpoint towards RAN needs to expect VND.3GPP.IUFP
164 * that encapsulates the AMR-FR RTP payload. */
165 .codec_with_iuup = true,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100166 },
167 };
168 if (osmo_sockaddr_str_from_str(&ran_dec_msg->assignment_complete.remote_rtp, addr, port)) {
169 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_ERROR, "Assignment Complete: unable to decode remote RTP IP address %s\n",
170 osmo_quote_str(addr, -1));
171 return -1;
172 }
173 return 0;
174}
175
176static void ran_iu_decode_rab_assignment_response(struct ran_dec *ran_iu_decode, const RANAP_RAB_AssignmentResponseIEs_t *ies)
177{
178 int rc;
179 RANAP_IE_t *ranap_ie;
180 RANAP_RAB_SetupOrModifiedItemIEs_t setup_ies;
181 struct ran_msg ran_dec_msg;
182 bool free_ies = false;
183
184 if (!(ies->presenceMask & RAB_ASSIGNMENTRESPONSEIES_RANAP_RAB_SETUPORMODIFIEDLIST_PRESENT)) {
185 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_ERROR, "RAB Assignment Response does not contain RAB information\n");
186 goto failure;
187 }
188
189 /* So far we assign a single RAB at a time, so it should not be necessary to iterate over the list of
190 * SetupOrModifiedList IEs and handle each one. */
191 ranap_ie = ies->raB_SetupOrModifiedList.raB_SetupOrModifiedList_ies.list.array[0];
192
193 rc = ranap_decode_rab_setupormodifieditemies_fromlist(&setup_ies, &ranap_ie->value);
194 if (rc) {
195 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_ERROR, "Error in ranap_decode_rab_setupormodifieditemies(): rc=%d\n", rc);
196 goto failure;
197 }
198 free_ies = true;
199
200 if (!ran_iu_decode_rab_assignment_response_decode_setup_ies(ran_iu_decode, &ran_dec_msg, &setup_ies))
201 goto success;
202
203failure:
204 ran_dec_msg = (struct ran_msg){
205 .msg_type = RAN_MSG_ASSIGNMENT_FAILURE,
206 .msg_name = "RANAP RAB Assignment Response: Failure",
207 .assignment_failure = {
208 .bssap_cause = RAN_MSG_BSSAP_CAUSE_UNSET,
209 .rr_cause = GSM48_RR_CAUSE_ABNORMAL_UNSPEC,
210 },
211 };
212
213success:
214 ran_decoded(ran_iu_decode, &ran_dec_msg);
215
216 if (free_ies)
217 ranap_free_rab_setupormodifieditemies(&setup_ies);
218}
219
Alexander Couzens2aaff752021-10-19 17:09:11 +0200220static void ran_iu_decode_security_mode_complete(struct ran_dec *ran_iu_decode, const RANAP_SecurityModeCompleteIEs_t *ies)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100221{
222 struct ran_msg ran_dec_msg = {
223 .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE,
224 .msg_name = "RANAP SecurityModeControl successfulOutcome",
Alexander Couzens2aaff752021-10-19 17:09:11 +0200225 .cipher_mode_complete = {
226 .utran_integrity = ies->chosenIntegrityProtectionAlgorithm,
227 .utran_encryption = -1,
228 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100229 };
Alexander Couzens2aaff752021-10-19 17:09:11 +0200230
231 if (ies->presenceMask & SECURITYMODECOMPLETEIES_RANAP_CHOSENENCRYPTIONALGORITHM_PRESENT)
232 ran_dec_msg.cipher_mode_complete.utran_encryption = ies->chosenEncryptionAlgorithm;
233
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100234 ran_decoded(ran_iu_decode, &ran_dec_msg);
235}
236
237static void ran_iu_decode_security_mode_reject(struct ran_dec *ran_iu_decode)
238{
239 struct ran_msg ran_dec_msg = {
240 .msg_type = RAN_MSG_CIPHER_MODE_REJECT,
241 .msg_name = "RANAP SecurityModeControl unsuccessfulOutcome",
242 .cipher_mode_reject = {
243 .bssap_cause = RAN_MSG_BSSAP_CAUSE_UNSET,
244 },
245 };
246 ran_decoded(ran_iu_decode, &ran_dec_msg);
247}
248
249static void ran_iu_decode_release_request(struct ran_dec *ran_iu_decode)
250{
251 struct ran_msg ran_dec_msg = {
252 .msg_type = RAN_MSG_CLEAR_REQUEST,
253 .msg_name = "RANAP Iu ReleaseRequest",
254 .clear_request = {
255 .bssap_cause = RAN_MSG_BSSAP_CAUSE_UNSET,
256 },
257 };
258 ran_decoded(ran_iu_decode, &ran_dec_msg);
259}
260
261static void ran_iu_decode_release_complete(struct ran_dec *ran_iu_decode)
262{
263 struct ran_msg ran_dec_msg = {
264 .msg_type = RAN_MSG_CLEAR_COMPLETE,
265 .msg_name = "RANAP Iu Release successfulOutcome",
266 };
267 ran_decoded(ran_iu_decode, &ran_dec_msg);
268}
269
270static void ran_iu_decode_ranap_msg(void *_ran_dec, ranap_message *message)
271{
272 struct ran_dec *ran_iu_decode = _ran_dec;
273
274 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_DEBUG, "dir=%u proc=%u\n", message->direction, message->procedureCode);
275
276 switch (message->procedureCode) {
277
278 case RANAP_ProcedureCode_id_InitialUE_Message:
279 ran_iu_decode_l3_initial(ran_iu_decode, &message->msg.initialUE_MessageIEs, "RANAP InitialUE RAN PDU");
280 return;
281
282 case RANAP_ProcedureCode_id_DirectTransfer:
Vadim Yanitskiy1c500442019-06-20 06:34:49 +0700283 ran_iu_decode_l3(ran_iu_decode, &message->msg.directTransferIEs, "RANAP DirectTransfer RAN PDU");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100284 return;
285
286 case RANAP_ProcedureCode_id_SecurityModeControl:
287 switch (message->direction) {
288 case RANAP_RANAP_PDU_PR_successfulOutcome:
Alexander Couzens2aaff752021-10-19 17:09:11 +0200289 ran_iu_decode_security_mode_complete(ran_iu_decode, &message->msg.securityModeCompleteIEs);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100290 return;
291 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
292 ran_iu_decode_security_mode_reject(ran_iu_decode);
293 return;
294 default:
295 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_ERROR,
296 "Received SecurityModeControl: unexpected RANAP ProcedureCode: %d\n",
297 message->direction);
298 return;
299 }
300
301 case RANAP_ProcedureCode_id_RAB_Assignment:
302 /* This should always be a RANAP_RANAP_PDU_PR_outcome. No need to check for that. */
303 ran_iu_decode_rab_assignment_response(ran_iu_decode, &message->msg.raB_AssignmentResponseIEs);
304 return;
305
306 case RANAP_ProcedureCode_id_Iu_ReleaseRequest:
307 ran_iu_decode_release_request(ran_iu_decode);
308 return;
309
310 case RANAP_ProcedureCode_id_Iu_Release:
311 if (message->direction != RANAP_RANAP_PDU_PR_successfulOutcome) {
312 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_ERROR, "Received Iu_Release: expected successfulOutcome, got %d\n",
313 message->direction);
314 return;
315 }
316 ran_iu_decode_release_complete(ran_iu_decode);
317 return;
318
319 case RANAP_ProcedureCode_id_ErrorIndication:
320 ran_iu_decode_err(ran_iu_decode, &message->msg.errorIndicationIEs);
321 return;
322
323 default:
324 LOG_RAN_IU_DEC(ran_iu_decode, LOGL_ERROR, "Received unhandled RANAP Procedure Code %d\n", message->procedureCode);
325 return;
326 }
327}
328
329int ran_iu_decode_l2(struct ran_dec *ran_iu_decode, struct msgb *ranap)
330{
331 return ranap_cn_rx_co(ran_iu_decode_ranap_msg, ran_iu_decode, msgb_l2(ranap), msgb_l2len(ranap));
332}
333
334/* Create a RANAP Initiating DirectTransfer message containing the given DTAP as RAN PDU, and return the resulting
335 * AN-APDU to be forwarded via E-interface. */
336static struct msgb *ran_iu_wrap_dtap(struct msgb *dtap)
337{
338 struct msgb *an_apdu;
Harald Welte4dd150a2019-05-10 23:54:15 +0200339 uint8_t sapi = OMSC_LINKID_CB(dtap);
340
341 an_apdu = ranap_new_msg_dt(sapi, dtap->data, msgb_length(dtap));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100342 an_apdu->l2h = an_apdu->data;
343 msgb_free(dtap);
344 return an_apdu;
345}
346
347static struct msgb *ran_iu_make_rab_assignment(struct osmo_fsm_inst *caller_fi, const struct ran_assignment_command *ac)
348{
349 struct msgb *msg;
350 bool use_x213_nsap;
351 uint32_t cn_rtp_ip;
352 static uint8_t next_rab_id = 1;
353 uint8_t rab_id = next_rab_id;
354
355 next_rab_id ++;
356 if (!next_rab_id)
357 next_rab_id = 1;
358
359 cn_rtp_ip = osmo_htonl(inet_addr(ac->cn_rtp->ip));
360
361 if (cn_rtp_ip == INADDR_NONE) {
362 LOG_RAN_IU_ENC(caller_fi, LOGL_ERROR, "Error during RAB Assignment: invalid RTP IP-Address\n");
363 return NULL;
364 }
365 if (ac->cn_rtp->port == 0) {
366 LOG_RAN_IU_ENC(caller_fi, LOGL_ERROR, "Error during RAB Assignment: invalid RTP port\n");
367 return NULL;
368 }
369
370 use_x213_nsap = (ac->rab_assign_addr_enc == NSAP_ADDR_ENC_X213);
371 LOG_RAN_IU_ENC(caller_fi, LOGL_DEBUG, "RAB Assignment: rab_id=%d, rtp=" OSMO_SOCKADDR_STR_FMT ", use_x213_nsap=%d\n",
372 rab_id, OSMO_SOCKADDR_STR_FMT_ARGS(ac->cn_rtp), use_x213_nsap);
373
374 msg = ranap_new_msg_rab_assign_voice(rab_id, cn_rtp_ip, ac->cn_rtp->port, use_x213_nsap);
375 msg->l2h = msg->data;
376
377 return msg;
378}
379
380static struct msgb *ran_iu_make_security_mode_command(struct osmo_fsm_inst *caller_fi,
381 const struct ran_cipher_mode_command *cm)
382{
Vadim Yanitskiyd405bad2021-11-29 00:58:02 +0300383 /* TODO: make the choice of available UIA algorithms configurable */
384 const uint8_t uia_mask = (1 << OSMO_UTRAN_UIA1) | (1 << OSMO_UTRAN_UIA2);
Alexander Couzense400b112021-11-26 23:57:10 +0100385 const uint8_t uea_mask = cm->utran.uea_encryption_mask & ~(1 << OSMO_UTRAN_UEA0);
386 bool use_encryption = uea_mask != 0x00;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100387
Harald Welte505a94a2021-02-06 17:12:20 +0100388 LOG_RAN_IU_ENC(caller_fi, LOGL_DEBUG, "Tx RANAP SECURITY MODE COMMAND to RNC, IK=%s, CK=%s\n",
389 osmo_hexdump_nospc(cm->vec->ik, 16),
Vadim Yanitskiy35db1462021-11-29 00:54:11 +0300390 use_encryption ? osmo_hexdump_nospc(cm->vec->ck, 16) : "NONE");
Harald Welte505a94a2021-02-06 17:12:20 +0100391 /* TODO: Do we need to check if the UE supports all of the algorithms and build an intersection like
392 * in the case of A5? */
Vadim Yanitskiy35db1462021-11-29 00:54:11 +0300393 return ranap_new_msg_sec_mod_cmd2(cm->vec->ik,
394 use_encryption ? cm->vec->ck : NULL,
Vadim Yanitskiyd405bad2021-11-29 00:58:02 +0300395 RANAP_KeyStatus_new,
396 (uia_mask << 1), /* API treats LSB as UIA0 */
Alexander Couzense400b112021-11-26 23:57:10 +0100397 uea_mask);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100398}
399
400
401static struct msgb *ran_iu_make_release_command(struct osmo_fsm_inst *caller_fi,
402 const struct ran_clear_command *ccmd)
403{
404 static const struct RANAP_Cause cause = {
405 .present = RANAP_Cause_PR_radioNetwork,
406 .choice.radioNetwork = RANAP_CauseRadioNetwork_release_due_to_utran_generated_reason,
407 /* TODO: set various causes depending on the ran_clear_command cause value */
408 };
409 return ranap_new_msg_iu_rel_cmd(&cause);
410}
411
412struct msgb *ran_iu_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
413{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100414 switch (ran_enc_msg->msg_type) {
415
416 case RAN_MSG_DTAP:
Neels Hofmeyrfe5ae8e2019-10-08 06:25:06 +0200417 LOG_RAN_IU_ENC(caller_fi, LOGL_DEBUG, "DirectTransfer\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100418 return ran_iu_wrap_dtap(ran_enc_msg->dtap);
419
420 // TODO: RAN_MSG_CLASSMARK_REQUEST ??
421
422 case RAN_MSG_CIPHER_MODE_COMMAND:
Neels Hofmeyrfe5ae8e2019-10-08 06:25:06 +0200423 LOG_RAN_IU_ENC(caller_fi, LOGL_DEBUG, "SecurityModeCommand\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100424 return ran_iu_make_security_mode_command(caller_fi, &ran_enc_msg->cipher_mode_command);
425
426 case RAN_MSG_ASSIGNMENT_COMMAND:
Neels Hofmeyrfe5ae8e2019-10-08 06:25:06 +0200427 LOG_RAN_IU_ENC(caller_fi, LOGL_DEBUG, "RAB AssignmentRequest\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100428 return ran_iu_make_rab_assignment(caller_fi, &ran_enc_msg->assignment_command);
429
430 case RAN_MSG_COMMON_ID:
Neels Hofmeyrfe5ae8e2019-10-08 06:25:06 +0200431 LOG_RAN_IU_ENC(caller_fi, LOGL_DEBUG, "CommonId\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100432 return ranap_new_msg_common_id(ran_enc_msg->common_id.imsi);
433
434 case RAN_MSG_CLEAR_COMMAND:
Neels Hofmeyrfe5ae8e2019-10-08 06:25:06 +0200435 LOG_RAN_IU_ENC(caller_fi, LOGL_DEBUG, "Iu Release\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100436 return ran_iu_make_release_command(caller_fi, &ran_enc_msg->clear_command);
437
438 default:
439 LOG_RAN_IU_ENC(caller_fi, LOGL_ERROR, "Message type not implemented: %s\n",
440 ran_msg_type_name(ran_enc_msg->msg_type));
441 return NULL;
442 }
443}
444
445/* Entry point for connection-less RANAP message */
446static void ranap_handle_cl(void *ctx, ranap_message *message)
447{
448 int *rc = ctx;
449 *rc = SCCP_RAN_MSG_NON_RESET;
450
451 if (message->procedureCode != RANAP_ProcedureCode_id_Reset)
452 return;
453
454 switch (message->direction) {
455 case RANAP_RANAP_PDU_PR_initiatingMessage:
456 *rc = SCCP_RAN_MSG_RESET;
457 return;
458 case RANAP_RANAP_PDU_PR_successfulOutcome:
459 *rc = SCCP_RAN_MSG_RESET_ACK;
460 return;
461 default:
462 return;
463 }
464}
465
Neels Hofmeyrb6972742020-06-26 15:20:51 +0200466enum reset_msg_type ranap_is_reset_msg(const struct sccp_ran_inst *sri, struct osmo_fsm_inst *log_fi,
467 struct msgb *l2, int *supports_osmux)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100468{
469 int ret = SCCP_RAN_MSG_NON_RESET;
470 int rc;
471
Neels Hofmeyrb6972742020-06-26 15:20:51 +0200472 if (supports_osmux != NULL)
473 *supports_osmux = -1;
474
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100475 rc = ranap_cn_rx_cl(ranap_handle_cl, &ret, msgb_l2(l2), msgb_l2len(l2));
476 if (rc)
477 return 0;
478 return ret;
479}
480
481struct msgb *ranap_make_reset_msg(const struct sccp_ran_inst *sri, enum reset_msg_type type)
482{
483 const RANAP_Cause_t cause = {
484 .present = RANAP_Cause_PR_protocol,
485 .choice = {
486 .protocol = RANAP_CauseProtocol_message_not_compatible_with_receiver_state,
487 },
488 };
489 switch (type) {
490 case SCCP_RAN_MSG_RESET:
491 return ranap_new_msg_reset(RANAP_CN_DomainIndicator_cs_domain, &cause);
492 case SCCP_RAN_MSG_RESET_ACK:
493 return ranap_new_msg_reset_ack(RANAP_CN_DomainIndicator_cs_domain, NULL);
494 default:
495 return NULL;
496 }
497}
498
499static e_RANAP_PagingCause ranap_paging_cause_from_msc(enum paging_cause cause)
500{
501 switch (cause) {
502 default:
503 case PAGING_CAUSE_UNSPECIFIED:
504 case PAGING_CAUSE_CALL_CONVERSATIONAL:
505 return RANAP_PagingCause_terminating_conversational_call;
506 case PAGING_CAUSE_CALL_STREAMING:
507 return RANAP_PagingCause_terminating_streaming_call;
508 case PAGING_CAUSE_CALL_INTERACTIVE:
509 return RANAP_PagingCause_terminating_interactive_call;
510 case PAGING_CAUSE_CALL_BACKGROUND:
511 return RANAP_PagingCause_terminating_background_call;
512 case PAGING_CAUSE_SIGNALLING_LOW_PRIO:
513 return RANAP_PagingCause_terminating_low_priority_signalling;
514 case PAGING_CAUSE_SIGNALLING_HIGH_PRIO:
515 return RANAP_PagingCause_terminating_high_priority_signalling;
516 }
517}
518
519struct msgb *ranap_make_paging_msg(const struct sccp_ran_inst *sri, const struct gsm0808_cell_id *page_cell_id,
520 const char *imsi, uint32_t tmsi, enum paging_cause cause)
521{
522 return ranap_new_msg_paging_cmd(imsi, tmsi == GSM_RESERVED_TMSI ? NULL : &tmsi, false,
523 ranap_paging_cause_from_msc(cause));
524}
525
526const char *ranap_msg_name(const struct sccp_ran_inst *sri, const struct msgb *l2)
527{
528 uint8_t msgt;
529 uint8_t procedure;
530 static char buf[32];
531 if (!l2->l2h)
532 return "?";
533
534 msgt = l2->l2h[0];
535 procedure = l2->l2h[1];
536
537 snprintf(buf, sizeof(buf), "type %u procedureCode %u", msgt, procedure);
538 return buf;
539}