blob: 5ca7d96dd6ba2b0044017c4f94e5f0a8ab139861 [file] [log] [blame]
Harald Weltef8db61b2015-12-18 17:29:59 +01001/* high-level RANAP messsage generation 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
21#include <osmocom/core/utils.h>
22#include <osmocom/core/msgb.h>
23
24#include "asn1helpers.h"
Neels Hofmeyr96979af2016-01-05 15:19:44 +010025#include <osmocom/ranap/iu_helpers.h>
Harald Weltef8db61b2015-12-18 17:29:59 +010026
Neels Hofmeyr96979af2016-01-05 15:19:44 +010027#include <osmocom/ranap/ranap_common.h>
28#include <osmocom/ranap/ranap_ies_defs.h>
29#include <osmocom/ranap/ranap_msg_factory.h>
Harald Weltef8db61b2015-12-18 17:29:59 +010030
Harald Weltebdf3fd12016-01-03 17:04:09 +010031#define DRANAP _ranap_DRANAP
Harald Weltef8db61b2015-12-18 17:29:59 +010032
33/*! \brief allocate a new long and assing a value to it */
34static long *new_long(long in)
35{
36 long *out = CALLOC(1, sizeof(long));
37 *out = in;
38 return out;
39}
40
Harald Weltec4338de2015-12-24 00:40:52 +010041/*! \brief generate RANAP RESET message */
42struct msgb *ranap_new_msg_reset(RANAP_CN_DomainIndicator_t domain,
Harald Welte1cdb81d2016-01-01 16:21:05 +010043 const RANAP_Cause_t *cause)
Harald Weltec4338de2015-12-24 00:40:52 +010044{
45 RANAP_ResetIEs_t ies;
46 RANAP_Reset_t out;
47 struct msgb *msg;
48 int rc;
49
50 memset(&ies, 0, sizeof(ies));
51 ies.cN_DomainIndicator = domain;
52 if (cause)
53 memcpy(&ies.cause, cause, sizeof(ies.cause));
54
55 memset(&out, 0, sizeof(out));
56 rc = ranap_encode_reseties(&out, &ies);
57 if (rc < 0) {
58 LOGP(DRANAP, LOGL_ERROR, "error encoding reset IEs: %d\n", rc);
59 return NULL;
60 }
61
62 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_Reset,
63 RANAP_Criticality_reject,
64 &asn_DEF_RANAP_Reset,
65 &out);
66
67 /* release dynamic allocations attached to dt */
68 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_Reset, &out);
69
70 return msg;
71}
72
73/*! \brief generate RANAP RESET ACK message */
74struct msgb *ranap_new_msg_reset_ack(RANAP_CN_DomainIndicator_t domain,
75 RANAP_GlobalRNC_ID_t *rnc_id)
76{
77 RANAP_ResetAcknowledgeIEs_t ies;
78 RANAP_ResetAcknowledge_t out;
79 struct msgb *msg;
80 int rc;
81
82 memset(&ies, 0, sizeof(ies));
83 ies.cN_DomainIndicator = domain;
84
85 /* The RNC shall include the globalRNC_ID in the RESET
86 * ACKNOWLEDGE message to the CN */
87 if (rnc_id) {
88 ies.presenceMask = RESETACKNOWLEDGEIES_RANAP_GLOBALRNC_ID_PRESENT;
Harald Welte74157f62016-01-01 16:43:59 +010089 OCTET_STRING_noalloc(&ies.globalRNC_ID.pLMNidentity,
Harald Welteea98b6f2015-12-24 15:09:06 +010090 rnc_id->pLMNidentity.buf,
91 rnc_id->pLMNidentity.size);
Harald Weltec4338de2015-12-24 00:40:52 +010092 ies.globalRNC_ID.rNC_ID = rnc_id->rNC_ID;
93 }
94
95 /* FIXME: Do we need criticalityDiagnostics */
96
97 memset(&out, 0, sizeof(out));
98 rc = ranap_encode_resetacknowledgeies(&out, &ies);
99 if (rc < 0) {
100 LOGP(DRANAP, LOGL_ERROR, "error encoding reset ack IEs: %d\n", rc);
101 return NULL;
102 }
103
104 msg = ranap_generate_successful_outcome(RANAP_ProcedureCode_id_Reset,
105 RANAP_Criticality_reject,
106 &asn_DEF_RANAP_ResetAcknowledge,
107 &out);
108
109 /* release dynamic allocations attached to dt */
110 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_ResetAcknowledge, &out);
111
112 return msg;
113}
114
Harald Welteea98b6f2015-12-24 15:09:06 +0100115/*! \brief generate RANAP INITIAL UE message */
116struct msgb *ranap_new_msg_initial_ue(uint32_t conn_id, int is_ps,
117 RANAP_GlobalRNC_ID_t *rnc_id,
118 uint8_t *nas_pdu, unsigned int nas_len)
119{
120 RANAP_InitialUE_MessageIEs_t ies;
121 RANAP_InitialUE_Message_t out;
122 struct msgb *msg;
123 uint32_t ctxidbuf;
124 int rc;
125 uint16_t buf0 = 0x2342;
126
127 memset(&ies, 0, sizeof(ies));
128 if (is_ps)
129 ies.cN_DomainIndicator = RANAP_CN_DomainIndicator_ps_domain;
130 else
131 ies.cN_DomainIndicator = RANAP_CN_DomainIndicator_cs_domain;
132
Harald Welte74157f62016-01-01 16:43:59 +0100133 OCTET_STRING_noalloc(&ies.lai.pLMNidentity, rnc_id->pLMNidentity.buf, rnc_id->pLMNidentity.size);
134 OCTET_STRING_noalloc(&ies.lai.lAC, (uint8_t *)&buf0, sizeof(buf0));
Harald Welteea98b6f2015-12-24 15:09:06 +0100135
Harald Welte74157f62016-01-01 16:43:59 +0100136 OCTET_STRING_noalloc(&ies.sai.pLMNidentity, rnc_id->pLMNidentity.buf, rnc_id->pLMNidentity.size);
137 OCTET_STRING_noalloc(&ies.sai.lAC, (uint8_t *)&buf0, sizeof(buf0));
138 OCTET_STRING_noalloc(&ies.sai.sAC, (uint8_t *)&buf0, sizeof(buf0));
Harald Welteea98b6f2015-12-24 15:09:06 +0100139
Harald Welte74157f62016-01-01 16:43:59 +0100140 OCTET_STRING_noalloc(&ies.nas_pdu, nas_pdu, nas_len);
Harald Welteea98b6f2015-12-24 15:09:06 +0100141 asn1_u24_to_bitstring(&ies.iuSigConId, &ctxidbuf, conn_id);
Harald Welte74157f62016-01-01 16:43:59 +0100142 OCTET_STRING_noalloc(&ies.globalRNC_ID.pLMNidentity, rnc_id->pLMNidentity.buf, rnc_id->pLMNidentity.size);
Harald Welteea98b6f2015-12-24 15:09:06 +0100143 ies.globalRNC_ID.rNC_ID = rnc_id->rNC_ID;
144
145 memset(&out, 0, sizeof(out));
146 rc = ranap_encode_initialue_messageies(&out, &ies);
147 if (rc < 0) {
148 LOGP(DRANAP, LOGL_ERROR, "error encoding initial UE IEs: %d\n", rc);
149 return NULL;
150 }
151
152 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_InitialUE_Message,
153 RANAP_Criticality_reject,
154 &asn_DEF_RANAP_InitialUE_Message,
155 &out);
156
157 /* release dynamic allocations attached to dt */
158 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_InitialUE_Message, &out);
159
160 return msg;
161}
162
163
Harald Weltef8db61b2015-12-18 17:29:59 +0100164/*! \brief generate RANAP DIRECT TRANSFER message */
165struct msgb *ranap_new_msg_dt(uint8_t sapi, const uint8_t *nas, unsigned int nas_len)
166{
167 RANAP_DirectTransferIEs_t ies;
168 RANAP_DirectTransfer_t dt;
169 struct msgb *msg;
170 int rc;
171
172 memset(&ies, 0, sizeof(ies));
173 memset(&dt, 0, sizeof(dt));
174
175 /* only SAPI optional field shall be present for CN->RNC */
176 ies.presenceMask = DIRECTTRANSFERIES_RANAP_SAPI_PRESENT;
177
178 if (sapi == 3)
179 ies.sapi = RANAP_SAPI_sapi_3;
180 else
181 ies.sapi = RANAP_SAPI_sapi_0;
182
Harald Welte74157f62016-01-01 16:43:59 +0100183 /* Avoid copying + later freeing of OCTET STRING */
184 OCTET_STRING_noalloc(&ies.nas_pdu, nas, nas_len);
Harald Weltef8db61b2015-12-18 17:29:59 +0100185
186 /* ies -> dt */
187 rc = ranap_encode_directtransferies(&dt, &ies);
188
189 /* dt -> msg */
190 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_DirectTransfer,
191 RANAP_Criticality_reject,
192 &asn_DEF_RANAP_DirectTransfer,
193 &dt);
194
195 /* release dynamic allocations attached to dt */
196 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_DirectTransfer, &dt);
197
198 return msg;
199}
200
201static const enum RANAP_IntegrityProtectionAlgorithm ip_alg[2] = {
202 RANAP_IntegrityProtectionAlgorithm_standard_UMTS_integrity_algorithm_UIA1,
203 RANAP_IntegrityProtectionAlgorithm_standard_UMTS_integrity_algorithm_UIA2,
204};
205
206static const RANAP_EncryptionAlgorithm_t enc_alg[2] = {
207 RANAP_EncryptionAlgorithm_standard_UMTS_encryption_algorith_UEA1,
208 RANAP_EncryptionAlgorithm_standard_UMTS_encryption_algorithm_UEA2,
209};
210
211/*! \brief generate RANAP SECURITY MODE COMMAND message */
212struct msgb *ranap_new_msg_sec_mod_cmd(const uint8_t *ik, const uint8_t *ck)
213{
214 RANAP_SecurityModeCommandIEs_t ies;
215 RANAP_SecurityModeCommand_t out;
216 struct msgb *msg;
217 int i, rc;
218
219 memset(&ies, 0, sizeof(ies));
220 memset(&out, 0, sizeof(out));
221
Harald Weltef8db61b2015-12-18 17:29:59 +0100222 for (i = 0; i < ARRAY_SIZE(ip_alg); i++) {
223 /* needs to be dynamically allocated, as
224 * SET_OF_free() will call FREEMEM() on it */
225 RANAP_IntegrityProtectionAlgorithm_t *alg = CALLOC(1, sizeof(*alg));
226 *alg = ip_alg[i];
227 ASN_SEQUENCE_ADD(&ies.integrityProtectionInformation.permittedAlgorithms, alg);
228 }
229
230 BIT_STRING_fromBuf(&ies.integrityProtectionInformation.key, ik, 16*8);
231
232 if (ck) {
Harald Welte2cf0d8f2015-12-28 13:13:47 +0100233 ies.presenceMask = SECURITYMODECOMMANDIES_RANAP_ENCRYPTIONINFORMATION_PRESENT;
Harald Weltef8db61b2015-12-18 17:29:59 +0100234 for (i = 0; i < ARRAY_SIZE(ip_alg); i++) {
235 /* needs to be dynamically allocated, as
236 * SET_OF_free() will call FREEMEM() on it */
237 RANAP_EncryptionAlgorithm_t *alg = CALLOC(1, sizeof(*alg));
238 *alg = enc_alg[i];
239 ASN_SEQUENCE_ADD(&ies.encryptionInformation.permittedAlgorithms, alg);
240 }
241 BIT_STRING_fromBuf(&ies.encryptionInformation.key, ck, 16*8);
242 }
243
244 ies.keyStatus = RANAP_KeyStatus_new; /* FIXME */
245
246 /* ies -> out */
247 rc = ranap_encode_securitymodecommandies(&out, &ies);
248
249 /* release dynamic allocations attached to ies */
250 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_IntegrityProtectionInformation, &ies.integrityProtectionInformation);
Harald Welte2cf0d8f2015-12-28 13:13:47 +0100251 if (ck)
252 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_EncryptionInformation, &ies.encryptionInformation);
Harald Weltef8db61b2015-12-18 17:29:59 +0100253
254 /* out -> msg */
255 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_SecurityModeControl,
256 RANAP_Criticality_reject,
257 &asn_DEF_RANAP_SecurityModeCommand,
258 &out);
259
260 /* release dynamic allocations attached to out */
261 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_SecurityModeCommand, &out);
262
263 return msg;
264}
265
Neels Hofmeyra9f55662016-04-19 01:21:41 +0200266struct msgb *ranap_new_msg_sec_mod_compl(
267 RANAP_ChosenIntegrityProtectionAlgorithm_t chosen_ip_alg,
268 RANAP_ChosenEncryptionAlgorithm_t chosen_enc_alg)
269{
270 RANAP_SecurityModeCompleteIEs_t ies;
271 RANAP_SecurityModeComplete_t out;
272 struct msgb *msg;
273 int i, rc;
274
275 memset(&ies, 0, sizeof(ies));
276 memset(&out, 0, sizeof(out));
277
278 ies.presenceMask = SECURITYMODECOMPLETEIES_RANAP_CHOSENENCRYPTIONALGORITHM_PRESENT;
279 ies.chosenIntegrityProtectionAlgorithm = chosen_ip_alg;
280 ies.chosenEncryptionAlgorithm = chosen_enc_alg;
281
282 /* ies -> out */
283 rc = ranap_encode_securitymodecompleteies(&out, &ies);
284
285 /* out -> msg */
286 msg = ranap_generate_successful_outcome(RANAP_ProcedureCode_id_SecurityModeControl,
287 RANAP_Criticality_reject,
288 &asn_DEF_RANAP_SecurityModeComplete,
289 &out);
290
291 /* release dynamic allocations attached to out */
292 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_SecurityModeComplete, &out);
293
294 return msg;
295}
296
Harald Weltef8db61b2015-12-18 17:29:59 +0100297/*! \brief generate RANAP COMMON ID message */
298struct msgb *ranap_new_msg_common_id(const char *imsi)
299{
300 RANAP_CommonID_IEs_t ies;
301 RANAP_CommonID_t out;
302 struct msgb *msg;
303 int rc;
304
305 memset(&ies, 0, sizeof(ies));
306 memset(&out, 0, sizeof(out));
307
308 if (imsi) {
309 uint8_t *imsi_buf = CALLOC(1, 16);
Harald Welte056984f2016-01-03 16:31:31 +0100310 rc = ranap_imsi_encode(imsi_buf, 16, imsi);
Harald Weltef8db61b2015-12-18 17:29:59 +0100311 ies.permanentNAS_UE_ID.present = RANAP_PermanentNAS_UE_ID_PR_iMSI;
312 ies.permanentNAS_UE_ID.choice.iMSI.buf = imsi_buf;
313 ies.permanentNAS_UE_ID.choice.iMSI.size = rc;
314 } else
315 ies.permanentNAS_UE_ID.present = RANAP_PermanentNAS_UE_ID_PR_NOTHING;
316
317 /* ies -> out */
318 rc = ranap_encode_commonid_ies(&out, &ies);
319 /* release dynamic allocations attached to ies */
320 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_PermanentNAS_UE_ID, &ies.permanentNAS_UE_ID);
321 if (rc < 0)
322 return NULL;
323
324 /* out -> msg */
325 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_CommonID,
326 RANAP_Criticality_ignore,
327 &asn_DEF_RANAP_CommonID,
328 &out);
329
330 /* release dynamic allocations attached to out */
331 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_CommonID, &out);
332
333 return msg;
334}
335
336/*! \brief generate RANAP IU RELEASE COMMAND message */
337struct msgb *ranap_new_msg_iu_rel_cmd(const RANAP_Cause_t *cause_in)
338{
339 RANAP_Iu_ReleaseCommandIEs_t ies;
340 RANAP_Iu_ReleaseCommand_t out;
341 struct msgb *msg;
342 int rc;
343
344 memset(&ies, 0, sizeof(ies));
345 memset(&out, 0, sizeof(out));
346
347 memcpy(&ies.cause, cause_in, sizeof(ies.cause));
348
349 /* ies -> out */
350 rc = ranap_encode_iu_releasecommandies(&out, &ies);
351 if (rc < 0)
352 return NULL;
353
354 /* out -> msg */
355 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_Iu_Release,
356 RANAP_Criticality_reject,
357 &asn_DEF_RANAP_Iu_ReleaseCommand,
358 &out);
359
360 /* release dynamic allocations attached to out */
361 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_Iu_ReleaseCommand, &out);
362
363 return msg;
364}
365
366/*! \brief generate RANAP PAGING COMMAND message */
367struct msgb *ranap_new_msg_paging_cmd(const char *imsi, const uint32_t *tmsi, int is_ps, uint32_t cause)
368{
369 RANAP_PagingIEs_t ies;
370 RANAP_Paging_t out;
371 struct msgb *msg;
372 uint8_t *imsi_buf = CALLOC(1, 16);
373 int rc;
374
375 memset(&ies, 0, sizeof(ies));
376 memset(&out, 0, sizeof(out));
377
378 /* put together the 'ies' */
379 if (is_ps)
380 ies.cN_DomainIndicator = RANAP_CN_DomainIndicator_ps_domain;
381 else
382 ies.cN_DomainIndicator = RANAP_CN_DomainIndicator_cs_domain;
383
Harald Welte056984f2016-01-03 16:31:31 +0100384 rc = ranap_imsi_encode(imsi_buf, 16, imsi);
Harald Weltef8db61b2015-12-18 17:29:59 +0100385 ies.permanentNAS_UE_ID.present = RANAP_PermanentNAS_UE_ID_PR_iMSI;
386 ies.permanentNAS_UE_ID.choice.iMSI.buf = imsi_buf;
387 ies.permanentNAS_UE_ID.choice.iMSI.size = rc;
388
389 if (tmsi) {
390 uint32_t *tmsi_buf = CALLOC(1, sizeof(*tmsi_buf));
391 *tmsi_buf = *tmsi;
392 ies.presenceMask |= PAGINGIES_RANAP_TEMPORARYUE_ID_PRESENT;
393 if (is_ps) {
394 ies.temporaryUE_ID.present = RANAP_TemporaryUE_ID_PR_p_TMSI;
Harald Weltef9c9aa52015-12-24 15:39:00 +0100395 ies.temporaryUE_ID.choice.tMSI.buf = (uint8_t *) tmsi_buf;
Harald Weltef8db61b2015-12-18 17:29:59 +0100396 ies.temporaryUE_ID.choice.tMSI.size = sizeof(*tmsi_buf);
397 } else {
398 ies.temporaryUE_ID.present = RANAP_TemporaryUE_ID_PR_tMSI;
Harald Weltef9c9aa52015-12-24 15:39:00 +0100399 ies.temporaryUE_ID.choice.p_TMSI.buf = (uint8_t *) tmsi_buf;
Harald Weltef8db61b2015-12-18 17:29:59 +0100400 ies.temporaryUE_ID.choice.p_TMSI.size = sizeof(*tmsi_buf);
401 }
402 }
403
404 if (cause) {
405 ies.presenceMask |= PAGINGIES_RANAP_PAGINGCAUSE_PRESENT;
406 ies.pagingCause = cause;
407 }
408
409 /* ies -> out */
410 rc = ranap_encode_pagingies(&out, &ies);
411 /* release dynamic allocation attached to ies */
412 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_PermanentNAS_UE_ID, &ies.permanentNAS_UE_ID);
413 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_TemporaryUE_ID, &ies.temporaryUE_ID);
414 if (rc < 0)
415 return NULL;
416
417 /* out -> msg */
418 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_Paging,
419 RANAP_Criticality_reject,
420 &asn_DEF_RANAP_Paging,
421 &out);
422
423 /* release dynamic allocations attached to out */
424 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_Paging, &out);
425
426 return msg;
427}
428
429static RANAP_SDU_ErrorRatio_t *new_sdu_error_ratio(long mantissa, long exponent)
430{
431 RANAP_SDU_ErrorRatio_t *err = CALLOC(1, sizeof(*err));
432
433 err->mantissa = mantissa;
434 err->exponent = exponent;
435
436 return err;
437}
438
439
440static RANAP_SDU_FormatInformationParameterItem_t *
441new_format_info_pars(long sdu_size)
442{
443 RANAP_SDU_FormatInformationParameterItem_t *fmti = CALLOC(1, sizeof(*fmti));
444 fmti->subflowSDU_Size = new_long(sdu_size);
445 return fmti;
446}
447
448enum sdu_par_profile {
449 SDUPAR_P_VOICE0,
450 SDUPAR_P_VOICE1,
451 SDUPAR_P_VOICE2,
452 SDUPAR_P_DATA,
453};
454
455/* See Chapter 5 of TS 26.102 */
456static RANAP_SDU_ParameterItem_t *new_sdu_par_item(enum sdu_par_profile profile)
457{
458 RANAP_SDU_ParameterItem_t *sdui = CALLOC(1, sizeof(*sdui));
459 RANAP_SDU_FormatInformationParameters_t *fmtip = CALLOC(1, sizeof(*fmtip));
460 RANAP_SDU_FormatInformationParameterItem_t *fmti;
461
462 switch (profile) {
463 case SDUPAR_P_VOICE0:
464 sdui->sDU_ErrorRatio = new_sdu_error_ratio(1, 5);
465 sdui->residualBitErrorRatio.mantissa = 1;
466 sdui->residualBitErrorRatio.exponent = 6;
467 sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_yes;
468 sdui->sDU_FormatInformationParameters = fmtip;
469 fmti = new_format_info_pars(81);
470 ASN_SEQUENCE_ADD(fmtip, fmti);
471 fmti = new_format_info_pars(39);
472 ASN_SEQUENCE_ADD(fmtip, fmti);
473 /* FIXME: could be 10 SDU descriptors for AMR! */
474 break;
475 case SDUPAR_P_VOICE1:
476 sdui->residualBitErrorRatio.mantissa = 1;
477 sdui->residualBitErrorRatio.exponent = 3;
478 sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_no_error_detection_consideration;
479 sdui->sDU_FormatInformationParameters = fmtip;
480 fmti = new_format_info_pars(103);
481 ASN_SEQUENCE_ADD(fmtip, fmti);
482 fmti = new_format_info_pars(0);
483 ASN_SEQUENCE_ADD(fmtip, fmti);
484 /* FIXME: could be 10 SDU descriptors for AMR! */
485 break;
486 case SDUPAR_P_VOICE2:
487 sdui->residualBitErrorRatio.mantissa = 5;
488 sdui->residualBitErrorRatio.exponent = 3;
489 sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_no_error_detection_consideration;
490 sdui->sDU_FormatInformationParameters = fmtip;
491 fmti = new_format_info_pars(60);
492 ASN_SEQUENCE_ADD(fmtip, fmti);
493 fmti = new_format_info_pars(0);
494 ASN_SEQUENCE_ADD(fmtip, fmti);
495 /* FIXME: could be 10 SDU descriptors for AMR! */
496 break;
497 case SDUPAR_P_DATA:
498 sdui->sDU_ErrorRatio = new_sdu_error_ratio(1, 4);
499 sdui->residualBitErrorRatio.mantissa = 1;
500 sdui->residualBitErrorRatio.exponent = 5;
501 sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_no;
502 FREEMEM(fmtip);
503 break;
504 }
505
506 return sdui;
507}
508
509static RANAP_AllocationOrRetentionPriority_t *
510new_alloc_ret_prio(RANAP_PriorityLevel_t level, int capability, int vulnerability,
511 int queueing_allowed)
512{
513 RANAP_AllocationOrRetentionPriority_t *arp = CALLOC(1, sizeof(*arp));
514
515 arp->priorityLevel = level;
516
517 if (capability)
518 arp->pre_emptionCapability = RANAP_Pre_emptionCapability_may_trigger_pre_emption;
519 else
520 arp->pre_emptionCapability = RANAP_Pre_emptionCapability_shall_not_trigger_pre_emption;
521
522 if (vulnerability)
523 arp->pre_emptionVulnerability = RANAP_Pre_emptionVulnerability_pre_emptable;
524 else
525 arp->pre_emptionVulnerability = RANAP_Pre_emptionVulnerability_not_pre_emptable;
526
527 if (queueing_allowed)
528 arp->queuingAllowed = RANAP_QueuingAllowed_queueing_allowed;
529 else
530 arp->queuingAllowed = RANAP_QueuingAllowed_queueing_not_allowed;
531
532 return arp;
533}
534
535/* See Chapter 5 of TS 26.102 */
536static RANAP_RAB_Parameters_t *new_rab_par_voice(void)
537{
538 RANAP_RAB_Parameters_t *rab = CALLOC(1, sizeof(*rab));
539 RANAP_SDU_ParameterItem_t *sdui;
540
541 rab->trafficClass = RANAP_TrafficClass_conversational;
542 rab->rAB_AsymmetryIndicator = RANAP_RAB_AsymmetryIndicator_symmetric_bidirectional;
543
544 ASN_SEQUENCE_ADD(&rab->maxBitrate.list, new_long(12200));
545 rab->guaranteedBitRate = CALLOC(1, sizeof(*rab->guaranteedBitRate));
546 ASN_SEQUENCE_ADD(rab->guaranteedBitRate, new_long(12200));
547 rab->deliveryOrder = RANAP_DeliveryOrder_delivery_order_requested;
548 rab->maxSDU_Size = 244;
549
550 sdui = new_sdu_par_item(SDUPAR_P_VOICE0);
551 ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui);
552 sdui = new_sdu_par_item(SDUPAR_P_VOICE1);
553 ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui);
554 sdui = new_sdu_par_item(SDUPAR_P_VOICE2);
555 ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui);
556
557 rab->transferDelay = new_long(80);
558 rab->allocationOrRetentionPriority = new_alloc_ret_prio(RANAP_PriorityLevel_no_priority, 0, 1, 0);
559
560 rab->sourceStatisticsDescriptor = new_long(RANAP_SourceStatisticsDescriptor_speech);
561
562 return rab;
563}
564
Harald Welte05ac6772015-12-29 19:09:52 +0100565static RANAP_RAB_Parameters_t *new_rab_par_data(uint32_t dl_max_bitrate, uint32_t ul_max_bitrate)
Harald Weltef8db61b2015-12-18 17:29:59 +0100566{
567 RANAP_RAB_Parameters_t *rab = CALLOC(1, sizeof(*rab));
568 RANAP_SDU_ParameterItem_t *sdui;
569
570 rab->trafficClass = RANAP_TrafficClass_background;
571 rab->rAB_AsymmetryIndicator = RANAP_RAB_AsymmetryIndicator_asymmetric_bidirectional;
572
Harald Welte05ac6772015-12-29 19:09:52 +0100573 ASN_SEQUENCE_ADD(&rab->maxBitrate.list, new_long(dl_max_bitrate));
574 ASN_SEQUENCE_ADD(&rab->maxBitrate.list, new_long(ul_max_bitrate));
Harald Weltef8db61b2015-12-18 17:29:59 +0100575 rab->deliveryOrder = RANAP_DeliveryOrder_delivery_order_requested;
576 rab->maxSDU_Size = 8000;
577
578 sdui = new_sdu_par_item(SDUPAR_P_DATA);
579 ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui);
580
581 rab->allocationOrRetentionPriority = new_alloc_ret_prio(RANAP_PriorityLevel_no_priority, 0, 0, 0);
582
583 /* FIXME: RAB-Parameter-ExtendedMaxBitrateList for 42Mbps? */
584
585 return rab;
586}
587
Harald Weltebfe49a22015-12-28 13:14:52 +0100588static void new_transp_layer_addr(BIT_STRING_t *out, uint32_t ip, int use_x213_nsap)
589{
590 uint8_t *buf;
591 unsigned int len;
592
593 if (use_x213_nsap) {
594 len = 7;
595 buf = CALLOC(len, sizeof(uint8_t));
596 buf[0] = 0x35; /* AFI For IANA ICP */
597 buf[1] = 0x00; /* See A.5.2.1.2.7 of X.213 */
598 buf[2] = 0x01;
599 *(uint32_t *)&buf[3] = ntohl(ip);
600 } else {
601 len = 4;
602 buf = CALLOC(len, sizeof(uint8_t));
603 *(uint32_t *)buf = ip;
604 }
605 out->buf = buf;
606 out->size = len;
607 out->bits_unused = 0;
608}
609
Harald Weltef8db61b2015-12-18 17:29:59 +0100610static RANAP_TransportLayerInformation_t *new_transp_info_rtp(uint32_t ip, uint16_t port)
611{
612 RANAP_TransportLayerInformation_t *tli = CALLOC(1, sizeof(*tli));
Harald Weltef8db61b2015-12-18 17:29:59 +0100613 uint8_t binding_id[4];
614
Harald Welte37223d82016-01-01 16:18:55 +0100615#if 0
Harald Weltef8db61b2015-12-18 17:29:59 +0100616 binding_id[0] = port >> 8;
617 binding_id[1] = port & 0xff;
618 binding_id[2] = binding_id[3] = 0;
Harald Welte37223d82016-01-01 16:18:55 +0100619#else
620 binding_id[0] = binding_id[1] = binding_id[2] = 0;
621 binding_id[3] = 1;
622#endif
Harald Weltef8db61b2015-12-18 17:29:59 +0100623
Harald Weltebfe49a22015-12-28 13:14:52 +0100624 new_transp_layer_addr(&tli->transportLayerAddress, ip, 1);
Harald Weltef8db61b2015-12-18 17:29:59 +0100625 tli->iuTransportAssociation.present = RANAP_IuTransportAssociation_PR_bindingID;
626 OCTET_STRING_fromBuf(&tli->iuTransportAssociation.choice.bindingID,
627 (const char *) binding_id, sizeof(binding_id));
628
629 return tli;
630}
631
632static RANAP_TransportLayerInformation_t *new_transp_info_gtp(uint32_t ip, uint32_t tei)
633{
634 RANAP_TransportLayerInformation_t *tli = CALLOC(1, sizeof(*tli));
Harald Welte01de8d72015-12-29 19:10:48 +0100635 uint32_t binding_buf = htonl(tei);
Harald Weltef8db61b2015-12-18 17:29:59 +0100636
Harald Weltebfe49a22015-12-28 13:14:52 +0100637 new_transp_layer_addr(&tli->transportLayerAddress, ip, 1);
Harald Weltef8db61b2015-12-18 17:29:59 +0100638 tli->iuTransportAssociation.present = RANAP_IuTransportAssociation_PR_gTP_TEI;
Harald Welte01de8d72015-12-29 19:10:48 +0100639 OCTET_STRING_fromBuf(&tli->iuTransportAssociation.choice.gTP_TEI,
Harald Weltef8db61b2015-12-18 17:29:59 +0100640 (const char *) &binding_buf, sizeof(binding_buf));
641
642 return tli;
643}
644
645static RANAP_UserPlaneInformation_t *new_upi(long mode, uint8_t mode_versions)
646{
647 RANAP_UserPlaneInformation_t *upi = CALLOC(1, sizeof(*upi));
648 uint16_t *buf = CALLOC(1, sizeof(*buf));
649
Daniel Willmann49f99cd2016-01-26 09:37:22 +0100650 *buf = ntohs(mode_versions);
Harald Weltef8db61b2015-12-18 17:29:59 +0100651
652 upi->userPlaneMode = mode;
Harald Weltef9c9aa52015-12-24 15:39:00 +0100653 upi->uP_ModeVersions.buf = (uint8_t *) buf;
Harald Weltef8db61b2015-12-18 17:29:59 +0100654 upi->uP_ModeVersions.size = sizeof(*buf);
655 upi->uP_ModeVersions.bits_unused = 0;
656
657 return upi;
658}
659
660
661static void assign_new_ra_id(RANAP_RAB_ID_t *id, uint8_t rab_id)
662{
663 uint8_t *buf = CALLOC(1, sizeof(*buf));
Harald Welte0a3eafe2015-12-19 02:38:09 +0100664 *buf = rab_id;
Harald Weltef8db61b2015-12-18 17:29:59 +0100665
666 id->buf = buf;
667 id->size = 1;
668 id->bits_unused = 0;
669}
670
671/*! \brief generate RANAP RAB ASSIGNMENT REQUEST message for CS (voice) */
672struct msgb *ranap_new_msg_rab_assign_voice(uint8_t rab_id, uint32_t rtp_ip, uint16_t rtp_port)
673{
674 RANAP_ProtocolIE_FieldPair_t *pair;
675 RANAP_RAB_AssignmentRequestIEs_t ies;
676 RANAP_RAB_AssignmentRequest_t out;
677 struct msgb *msg;
678 int rc;
679
680 memset(&ies, 0, sizeof(ies));
681 memset(&out, 0, sizeof(out));
682
683 /* only assingnment is present, no release */
684 ies.presenceMask = RAB_ASSIGNMENTREQUESTIES_RANAP_RAB_SETUPORMODIFYLIST_PRESENT;
685
686 /* put together the 'First' part */
687 RANAP_RAB_SetupOrModifyItemFirst_t first;
688 memset(&first, 0, sizeof(first));
689 assign_new_ra_id(&first.rAB_ID, rab_id);
690 //first.nAS_SynchronisationIndicator = FIXME;
691 first.rAB_Parameters = new_rab_par_voice();
692 first.userPlaneInformation = new_upi(RANAP_UserPlaneMode_support_mode_for_predefined_SDU_sizes, 1); /* 2? */
693 first.transportLayerInformation = new_transp_info_rtp(rtp_ip, rtp_port);
694
695 /* put together the 'Second' part */
696 RANAP_RAB_SetupOrModifyItemSecond_t second;
697 memset(&second, 0, sizeof(second));
698
699 /* Build an IE Pair out of first and second part:
700 * (first, second) -> pair */
701 pair = ranap_new_ie_pair(RANAP_ProtocolIE_ID_id_RAB_SetupOrModifyItem,
702 RANAP_Criticality_reject,
703 &asn_DEF_RANAP_RAB_SetupOrModifyItemFirst, &first,
704 RANAP_Criticality_ignore,
705 &asn_DEF_RANAP_RAB_SetupOrModifyItemSecond, &second);
706
707 /* the pair has been made, we can release any of its elements */
708 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemFirst, &first);
709 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemSecond, &second);
710
Harald Welteb7f67c42015-12-19 02:36:52 +0100711 RANAP_ProtocolIE_ContainerPair_t *container_pair = CALLOC(1, sizeof(*container_pair));
Harald Weltef8db61b2015-12-18 17:29:59 +0100712 /* Add the pair to the list of IEs of the RAB ass.req */
Harald Welteb7f67c42015-12-19 02:36:52 +0100713 ASN_SEQUENCE_ADD(container_pair, pair);
714 ASN_SEQUENCE_ADD(&ies.raB_SetupOrModifyList.list, container_pair);
Harald Weltef8db61b2015-12-18 17:29:59 +0100715
716 /* encode the IEs into the actual assignment request:
717 * ies -> out */
718 rc = ranap_encode_rab_assignmentrequesties(&out, &ies);
719 /* 'out' has been generated, we can now release the input */
720 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyList,
721 &ies.raB_SetupOrModifyList);
722 if (rc < 0)
723 return NULL;
724
725 /* generate an Initiating Mesasage: out -> msg */
726 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_RAB_Assignment,
727 RANAP_Criticality_reject,
728 &asn_DEF_RANAP_RAB_AssignmentRequest, &out);
729
730 /* 'msg' has been generated, we cann now release the input 'out' */
731 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_AssignmentRequest, &out);
732
733 return msg;
734}
735
736/*! \brief generate RANAP RAB ASSIGNMENT REQUEST message for PS (data) */
737struct msgb *ranap_new_msg_rab_assign_data(uint8_t rab_id, uint32_t gtp_ip, uint32_t gtp_tei)
738{
739 RANAP_ProtocolIE_FieldPair_t *pair;
740 RANAP_RAB_AssignmentRequestIEs_t ies;
741 RANAP_RAB_AssignmentRequest_t out;
742 RANAP_DataVolumeReportingIndication_t *dat_vol_ind;
743 struct msgb *msg;
744 int rc;
745
746 memset(&ies, 0, sizeof(ies));
747 memset(&out, 0, sizeof(out));
748
749 /* only assingnment is present, no release */
750 ies.presenceMask = RAB_ASSIGNMENTREQUESTIES_RANAP_RAB_SETUPORMODIFYLIST_PRESENT;
751
752 /* put together the 'First' part */
753 RANAP_RAB_SetupOrModifyItemFirst_t first;
754 memset(&first, 0, sizeof(first));
755 assign_new_ra_id(&first.rAB_ID, rab_id);
756 //first.nAS_SynchronisationIndicator = FIXME;
Harald Welte05ac6772015-12-29 19:09:52 +0100757
758 first.rAB_Parameters = new_rab_par_data(1600000, 800000);
Harald Weltef8db61b2015-12-18 17:29:59 +0100759 first.userPlaneInformation = new_upi(RANAP_UserPlaneMode_transparent_mode, 1);
Harald Welte01de8d72015-12-29 19:10:48 +0100760 first.transportLayerInformation = new_transp_info_gtp(gtp_ip, gtp_tei);
Harald Weltef8db61b2015-12-18 17:29:59 +0100761
762 /* put together the 'Second' part */
763 RANAP_RAB_SetupOrModifyItemSecond_t second;
764 memset(&second, 0, sizeof(second));
765 second.pDP_TypeInformation = CALLOC(1, sizeof(*second.pDP_TypeInformation));
766 ASN_SEQUENCE_ADD(second.pDP_TypeInformation, new_long(RANAP_PDP_Type_ipv4));
767 dat_vol_ind = CALLOC(1, sizeof(*dat_vol_ind));
768 *dat_vol_ind = RANAP_DataVolumeReportingIndication_do_not_report;
769 second.dataVolumeReportingIndication = dat_vol_ind;
770 second.dl_GTP_PDU_SequenceNumber = new_long(0);
771 second.ul_GTP_PDU_SequenceNumber = new_long(0);
772
773 /* Build an IE Pair out of first and second part:
774 * (first, second) -> pair */
775 pair = ranap_new_ie_pair(RANAP_ProtocolIE_ID_id_RAB_SetupOrModifyItem,
776 RANAP_Criticality_reject,
777 &asn_DEF_RANAP_RAB_SetupOrModifyItemFirst,
778 &first, RANAP_Criticality_ignore,
779 &asn_DEF_RANAP_RAB_SetupOrModifyItemSecond,
780 &second);
781
782 /* the pair has been made, we can release any of its elements */
783 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemFirst, &first);
784 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemSecond, &second);
785
Harald Welteb7f67c42015-12-19 02:36:52 +0100786 RANAP_ProtocolIE_ContainerPair_t *container_pair = CALLOC(1, sizeof(*container_pair));
Harald Weltef8db61b2015-12-18 17:29:59 +0100787 /* Add the pair to the list of IEs of the RAB ass.req */
Harald Welteb7f67c42015-12-19 02:36:52 +0100788 ASN_SEQUENCE_ADD(container_pair, pair);
789 /* Add the pair to the list of IEs of the RAB ass.req */
790 ASN_SEQUENCE_ADD(&ies.raB_SetupOrModifyList.list, container_pair);
Harald Weltef8db61b2015-12-18 17:29:59 +0100791
792 /* encode the IEs into the actual assignment request:
793 * ies -> out */
794 rc = ranap_encode_rab_assignmentrequesties(&out, &ies);
795 /* 'out' has been generated, we can now release the input */
796 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyList,
797 &ies.raB_SetupOrModifyList);
798 if (rc < 0)
799 return NULL;
800
801 /* generate an Initiating Mesasage: out -> msg */
802 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_RAB_Assignment,
803 RANAP_Criticality_reject,
804 &asn_DEF_RANAP_RAB_AssignmentRequest, &out);
805
806 /* 'msg' has been generated, we cann now release the input 'out' */
807 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_AssignmentRequest, &out);
808
809 return msg;
810}
Harald Welte37223d82016-01-01 16:18:55 +0100811
812struct msgb *ranap_new_msg_iu_rel_req(const RANAP_Cause_t *cause)
813{
814 RANAP_Iu_ReleaseRequestIEs_t ies;
815 RANAP_Iu_ReleaseRequest_t out;
816 struct msgb *msg;
817 int rc;
818
819 memset(&ies, 0, sizeof(ies));
820 memset(&out, 0, sizeof(out));
821
822 memcpy(&ies.cause, cause, sizeof(ies.cause));
823
824 rc = ranap_encode_iu_releaserequesties(&out, &ies);
825 if (rc < 0)
826 return NULL;
827
828 /* encode the output into the msgb */
829 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_Iu_ReleaseRequest,
830 RANAP_Criticality_reject,
831 &asn_DEF_RANAP_Iu_ReleaseRequest, &out);
832 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_Iu_ReleaseRequest, &out);
833
834 return msg;
835}
836
837struct msgb *ranap_new_msg_rab_rel_req(uint8_t rab_id, const RANAP_Cause_t *cause)
838{
839 RANAP_RAB_ReleaseItemIEs_t item_ies;
840 RANAP_RAB_ReleaseRequestIEs_t ies;
841 RANAP_RAB_ReleaseRequest_t out;
842 struct msgb *msg;
843 int rc;
844
845 memset(&item_ies, 0, sizeof(item_ies));
846 memset(&ies, 0, sizeof(ies));
847 memset(&out, 0, sizeof(out));
848
849 /* put together the ReleaseItem */
850 assign_new_ra_id(&item_ies.raB_ReleaseItem.rAB_ID, rab_id);
851 memcpy(&item_ies.raB_ReleaseItem.cause, cause, sizeof(item_ies.raB_ReleaseItem.cause));
852
853 /* add to the list */
854 rc = ranap_encode_rab_releaseitemies(&ies.raB_ReleaseList, &item_ies);
855 if (rc < 0)
856 return NULL;
857 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_ReleaseItem, &item_ies.raB_ReleaseItem);
858
859 /* encoe the list IEs into the output */
860 rc = ranap_encode_rab_releaserequesties(&out, &ies);
861 if (rc < 0)
862 return NULL;
863 /* 'out' has been generated, we can release the input */
864 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_ReleaseList, &ies.raB_ReleaseList);
865
866 /* encode the output into the msgb */
867 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_RAB_ReleaseRequest,
868 RANAP_Criticality_reject,
869 &asn_DEF_RANAP_RAB_ReleaseRequest, &out);
870
871 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_ReleaseRequest, &out);
872
873 return msg;
874}