blob: e0ff42badb2f94ab40c72496e546aef040b316b1 [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"
25#include "iu_helpers.h"
26
27#include "ranap_common.h"
28#include "ranap_ies_defs.h"
29#include "ranap_msg_factory.h"
30
31#include "hnbgw.h"
32
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,
43 RANAP_Cause_t *cause)
44{
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 Welteea98b6f2015-12-24 15:09:06 +010089 OCTET_STRING_fromBuf(&ies.globalRNC_ID.pLMNidentity,
90 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
133 OCTET_STRING_fromBuf(&ies.lai.pLMNidentity, rnc_id->pLMNidentity.buf, rnc_id->pLMNidentity.size);
134 OCTET_STRING_fromBuf(&ies.lai.lAC, &buf0, sizeof(buf0));
135
136 OCTET_STRING_fromBuf(&ies.sai.pLMNidentity, rnc_id->pLMNidentity.buf, rnc_id->pLMNidentity.size);
137 OCTET_STRING_fromBuf(&ies.sai.lAC, &buf0, sizeof(buf0));
138 OCTET_STRING_fromBuf(&ies.sai.sAC, &buf0, sizeof(buf0));
139
140 OCTET_STRING_fromBuf(&ies.nas_pdu, nas_pdu, nas_len);
141 asn1_u24_to_bitstring(&ies.iuSigConId, &ctxidbuf, conn_id);
142 OCTET_STRING_fromBuf(&ies.globalRNC_ID.pLMNidentity, rnc_id->pLMNidentity.buf, rnc_id->pLMNidentity.size);
143 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
183 ies.nas_pdu.buf = (uint8_t *) nas;
184 ies.nas_pdu.size = nas_len;
185
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
222 ies.presenceMask = SECURITYMODECOMMANDIES_RANAP_ENCRYPTIONINFORMATION_PRESENT;
223
224 for (i = 0; i < ARRAY_SIZE(ip_alg); i++) {
225 /* needs to be dynamically allocated, as
226 * SET_OF_free() will call FREEMEM() on it */
227 RANAP_IntegrityProtectionAlgorithm_t *alg = CALLOC(1, sizeof(*alg));
228 *alg = ip_alg[i];
229 ASN_SEQUENCE_ADD(&ies.integrityProtectionInformation.permittedAlgorithms, alg);
230 }
231
232 BIT_STRING_fromBuf(&ies.integrityProtectionInformation.key, ik, 16*8);
233
234 if (ck) {
235 for (i = 0; i < ARRAY_SIZE(ip_alg); i++) {
236 /* needs to be dynamically allocated, as
237 * SET_OF_free() will call FREEMEM() on it */
238 RANAP_EncryptionAlgorithm_t *alg = CALLOC(1, sizeof(*alg));
239 *alg = enc_alg[i];
240 ASN_SEQUENCE_ADD(&ies.encryptionInformation.permittedAlgorithms, alg);
241 }
242 BIT_STRING_fromBuf(&ies.encryptionInformation.key, ck, 16*8);
243 }
244
245 ies.keyStatus = RANAP_KeyStatus_new; /* FIXME */
246
247 /* ies -> out */
248 rc = ranap_encode_securitymodecommandies(&out, &ies);
249
250 /* release dynamic allocations attached to ies */
251 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_IntegrityProtectionInformation, &ies.integrityProtectionInformation);
252 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_EncryptionInformation, &ies.encryptionInformation);
253
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
266/*! \brief generate RANAP COMMON ID message */
267struct msgb *ranap_new_msg_common_id(const char *imsi)
268{
269 RANAP_CommonID_IEs_t ies;
270 RANAP_CommonID_t out;
271 struct msgb *msg;
272 int rc;
273
274 memset(&ies, 0, sizeof(ies));
275 memset(&out, 0, sizeof(out));
276
277 if (imsi) {
278 uint8_t *imsi_buf = CALLOC(1, 16);
279 rc = encode_iu_imsi(imsi_buf, 16, imsi);
280 ies.permanentNAS_UE_ID.present = RANAP_PermanentNAS_UE_ID_PR_iMSI;
281 ies.permanentNAS_UE_ID.choice.iMSI.buf = imsi_buf;
282 ies.permanentNAS_UE_ID.choice.iMSI.size = rc;
283 } else
284 ies.permanentNAS_UE_ID.present = RANAP_PermanentNAS_UE_ID_PR_NOTHING;
285
286 /* ies -> out */
287 rc = ranap_encode_commonid_ies(&out, &ies);
288 /* release dynamic allocations attached to ies */
289 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_PermanentNAS_UE_ID, &ies.permanentNAS_UE_ID);
290 if (rc < 0)
291 return NULL;
292
293 /* out -> msg */
294 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_CommonID,
295 RANAP_Criticality_ignore,
296 &asn_DEF_RANAP_CommonID,
297 &out);
298
299 /* release dynamic allocations attached to out */
300 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_CommonID, &out);
301
302 return msg;
303}
304
305/*! \brief generate RANAP IU RELEASE COMMAND message */
306struct msgb *ranap_new_msg_iu_rel_cmd(const RANAP_Cause_t *cause_in)
307{
308 RANAP_Iu_ReleaseCommandIEs_t ies;
309 RANAP_Iu_ReleaseCommand_t out;
310 struct msgb *msg;
311 int rc;
312
313 memset(&ies, 0, sizeof(ies));
314 memset(&out, 0, sizeof(out));
315
316 memcpy(&ies.cause, cause_in, sizeof(ies.cause));
317
318 /* ies -> out */
319 rc = ranap_encode_iu_releasecommandies(&out, &ies);
320 if (rc < 0)
321 return NULL;
322
323 /* out -> msg */
324 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_Iu_Release,
325 RANAP_Criticality_reject,
326 &asn_DEF_RANAP_Iu_ReleaseCommand,
327 &out);
328
329 /* release dynamic allocations attached to out */
330 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_Iu_ReleaseCommand, &out);
331
332 return msg;
333}
334
335/*! \brief generate RANAP PAGING COMMAND message */
336struct msgb *ranap_new_msg_paging_cmd(const char *imsi, const uint32_t *tmsi, int is_ps, uint32_t cause)
337{
338 RANAP_PagingIEs_t ies;
339 RANAP_Paging_t out;
340 struct msgb *msg;
341 uint8_t *imsi_buf = CALLOC(1, 16);
342 int rc;
343
344 memset(&ies, 0, sizeof(ies));
345 memset(&out, 0, sizeof(out));
346
347 /* put together the 'ies' */
348 if (is_ps)
349 ies.cN_DomainIndicator = RANAP_CN_DomainIndicator_ps_domain;
350 else
351 ies.cN_DomainIndicator = RANAP_CN_DomainIndicator_cs_domain;
352
353 rc = encode_iu_imsi(imsi_buf, 16, imsi);
354 ies.permanentNAS_UE_ID.present = RANAP_PermanentNAS_UE_ID_PR_iMSI;
355 ies.permanentNAS_UE_ID.choice.iMSI.buf = imsi_buf;
356 ies.permanentNAS_UE_ID.choice.iMSI.size = rc;
357
358 if (tmsi) {
359 uint32_t *tmsi_buf = CALLOC(1, sizeof(*tmsi_buf));
360 *tmsi_buf = *tmsi;
361 ies.presenceMask |= PAGINGIES_RANAP_TEMPORARYUE_ID_PRESENT;
362 if (is_ps) {
363 ies.temporaryUE_ID.present = RANAP_TemporaryUE_ID_PR_p_TMSI;
364 ies.temporaryUE_ID.choice.tMSI.buf = tmsi_buf;
365 ies.temporaryUE_ID.choice.tMSI.size = sizeof(*tmsi_buf);
366 } else {
367 ies.temporaryUE_ID.present = RANAP_TemporaryUE_ID_PR_tMSI;
368 ies.temporaryUE_ID.choice.p_TMSI.buf = tmsi_buf;
369 ies.temporaryUE_ID.choice.p_TMSI.size = sizeof(*tmsi_buf);
370 }
371 }
372
373 if (cause) {
374 ies.presenceMask |= PAGINGIES_RANAP_PAGINGCAUSE_PRESENT;
375 ies.pagingCause = cause;
376 }
377
378 /* ies -> out */
379 rc = ranap_encode_pagingies(&out, &ies);
380 /* release dynamic allocation attached to ies */
381 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_PermanentNAS_UE_ID, &ies.permanentNAS_UE_ID);
382 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_TemporaryUE_ID, &ies.temporaryUE_ID);
383 if (rc < 0)
384 return NULL;
385
386 /* out -> msg */
387 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_Paging,
388 RANAP_Criticality_reject,
389 &asn_DEF_RANAP_Paging,
390 &out);
391
392 /* release dynamic allocations attached to out */
393 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_Paging, &out);
394
395 return msg;
396}
397
398static RANAP_SDU_ErrorRatio_t *new_sdu_error_ratio(long mantissa, long exponent)
399{
400 RANAP_SDU_ErrorRatio_t *err = CALLOC(1, sizeof(*err));
401
402 err->mantissa = mantissa;
403 err->exponent = exponent;
404
405 return err;
406}
407
408
409static RANAP_SDU_FormatInformationParameterItem_t *
410new_format_info_pars(long sdu_size)
411{
412 RANAP_SDU_FormatInformationParameterItem_t *fmti = CALLOC(1, sizeof(*fmti));
413 fmti->subflowSDU_Size = new_long(sdu_size);
414 return fmti;
415}
416
417enum sdu_par_profile {
418 SDUPAR_P_VOICE0,
419 SDUPAR_P_VOICE1,
420 SDUPAR_P_VOICE2,
421 SDUPAR_P_DATA,
422};
423
424/* See Chapter 5 of TS 26.102 */
425static RANAP_SDU_ParameterItem_t *new_sdu_par_item(enum sdu_par_profile profile)
426{
427 RANAP_SDU_ParameterItem_t *sdui = CALLOC(1, sizeof(*sdui));
428 RANAP_SDU_FormatInformationParameters_t *fmtip = CALLOC(1, sizeof(*fmtip));
429 RANAP_SDU_FormatInformationParameterItem_t *fmti;
430
431 switch (profile) {
432 case SDUPAR_P_VOICE0:
433 sdui->sDU_ErrorRatio = new_sdu_error_ratio(1, 5);
434 sdui->residualBitErrorRatio.mantissa = 1;
435 sdui->residualBitErrorRatio.exponent = 6;
436 sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_yes;
437 sdui->sDU_FormatInformationParameters = fmtip;
438 fmti = new_format_info_pars(81);
439 ASN_SEQUENCE_ADD(fmtip, fmti);
440 fmti = new_format_info_pars(39);
441 ASN_SEQUENCE_ADD(fmtip, fmti);
442 /* FIXME: could be 10 SDU descriptors for AMR! */
443 break;
444 case SDUPAR_P_VOICE1:
445 sdui->residualBitErrorRatio.mantissa = 1;
446 sdui->residualBitErrorRatio.exponent = 3;
447 sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_no_error_detection_consideration;
448 sdui->sDU_FormatInformationParameters = fmtip;
449 fmti = new_format_info_pars(103);
450 ASN_SEQUENCE_ADD(fmtip, fmti);
451 fmti = new_format_info_pars(0);
452 ASN_SEQUENCE_ADD(fmtip, fmti);
453 /* FIXME: could be 10 SDU descriptors for AMR! */
454 break;
455 case SDUPAR_P_VOICE2:
456 sdui->residualBitErrorRatio.mantissa = 5;
457 sdui->residualBitErrorRatio.exponent = 3;
458 sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_no_error_detection_consideration;
459 sdui->sDU_FormatInformationParameters = fmtip;
460 fmti = new_format_info_pars(60);
461 ASN_SEQUENCE_ADD(fmtip, fmti);
462 fmti = new_format_info_pars(0);
463 ASN_SEQUENCE_ADD(fmtip, fmti);
464 /* FIXME: could be 10 SDU descriptors for AMR! */
465 break;
466 case SDUPAR_P_DATA:
467 sdui->sDU_ErrorRatio = new_sdu_error_ratio(1, 4);
468 sdui->residualBitErrorRatio.mantissa = 1;
469 sdui->residualBitErrorRatio.exponent = 5;
470 sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_no;
471 FREEMEM(fmtip);
472 break;
473 }
474
475 return sdui;
476}
477
478static RANAP_AllocationOrRetentionPriority_t *
479new_alloc_ret_prio(RANAP_PriorityLevel_t level, int capability, int vulnerability,
480 int queueing_allowed)
481{
482 RANAP_AllocationOrRetentionPriority_t *arp = CALLOC(1, sizeof(*arp));
483
484 arp->priorityLevel = level;
485
486 if (capability)
487 arp->pre_emptionCapability = RANAP_Pre_emptionCapability_may_trigger_pre_emption;
488 else
489 arp->pre_emptionCapability = RANAP_Pre_emptionCapability_shall_not_trigger_pre_emption;
490
491 if (vulnerability)
492 arp->pre_emptionVulnerability = RANAP_Pre_emptionVulnerability_pre_emptable;
493 else
494 arp->pre_emptionVulnerability = RANAP_Pre_emptionVulnerability_not_pre_emptable;
495
496 if (queueing_allowed)
497 arp->queuingAllowed = RANAP_QueuingAllowed_queueing_allowed;
498 else
499 arp->queuingAllowed = RANAP_QueuingAllowed_queueing_not_allowed;
500
501 return arp;
502}
503
504/* See Chapter 5 of TS 26.102 */
505static RANAP_RAB_Parameters_t *new_rab_par_voice(void)
506{
507 RANAP_RAB_Parameters_t *rab = CALLOC(1, sizeof(*rab));
508 RANAP_SDU_ParameterItem_t *sdui;
509
510 rab->trafficClass = RANAP_TrafficClass_conversational;
511 rab->rAB_AsymmetryIndicator = RANAP_RAB_AsymmetryIndicator_symmetric_bidirectional;
512
513 ASN_SEQUENCE_ADD(&rab->maxBitrate.list, new_long(12200));
514 rab->guaranteedBitRate = CALLOC(1, sizeof(*rab->guaranteedBitRate));
515 ASN_SEQUENCE_ADD(rab->guaranteedBitRate, new_long(12200));
516 rab->deliveryOrder = RANAP_DeliveryOrder_delivery_order_requested;
517 rab->maxSDU_Size = 244;
518
519 sdui = new_sdu_par_item(SDUPAR_P_VOICE0);
520 ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui);
521 sdui = new_sdu_par_item(SDUPAR_P_VOICE1);
522 ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui);
523 sdui = new_sdu_par_item(SDUPAR_P_VOICE2);
524 ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui);
525
526 rab->transferDelay = new_long(80);
527 rab->allocationOrRetentionPriority = new_alloc_ret_prio(RANAP_PriorityLevel_no_priority, 0, 1, 0);
528
529 rab->sourceStatisticsDescriptor = new_long(RANAP_SourceStatisticsDescriptor_speech);
530
531 return rab;
532}
533
534static RANAP_RAB_Parameters_t *new_rab_par_data(void)
535{
536 RANAP_RAB_Parameters_t *rab = CALLOC(1, sizeof(*rab));
537 RANAP_SDU_ParameterItem_t *sdui;
538
539 rab->trafficClass = RANAP_TrafficClass_background;
540 rab->rAB_AsymmetryIndicator = RANAP_RAB_AsymmetryIndicator_asymmetric_bidirectional;
541
542 ASN_SEQUENCE_ADD(&rab->maxBitrate.list, new_long(16000000));
543 ASN_SEQUENCE_ADD(&rab->maxBitrate.list, new_long(8000000));
544 rab->deliveryOrder = RANAP_DeliveryOrder_delivery_order_requested;
545 rab->maxSDU_Size = 8000;
546
547 sdui = new_sdu_par_item(SDUPAR_P_DATA);
548 ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui);
549
550 rab->allocationOrRetentionPriority = new_alloc_ret_prio(RANAP_PriorityLevel_no_priority, 0, 0, 0);
551
552 /* FIXME: RAB-Parameter-ExtendedMaxBitrateList for 42Mbps? */
553
554 return rab;
555}
556
557static RANAP_TransportLayerInformation_t *new_transp_info_rtp(uint32_t ip, uint16_t port)
558{
559 RANAP_TransportLayerInformation_t *tli = CALLOC(1, sizeof(*tli));
560 uint32_t *ipbuf = CALLOC(1, sizeof(*ipbuf));
561 uint8_t binding_id[4];
562
563 binding_id[0] = port >> 8;
564 binding_id[1] = port & 0xff;
565 binding_id[2] = binding_id[3] = 0;
566
Harald Welte94a62d52015-12-19 02:37:48 +0100567 asn1_u32_to_bitstring(&tli->transportLayerAddress, ipbuf, ip);
Harald Weltef8db61b2015-12-18 17:29:59 +0100568 tli->iuTransportAssociation.present = RANAP_IuTransportAssociation_PR_bindingID;
569 OCTET_STRING_fromBuf(&tli->iuTransportAssociation.choice.bindingID,
570 (const char *) binding_id, sizeof(binding_id));
571
572 return tli;
573}
574
575static RANAP_TransportLayerInformation_t *new_transp_info_gtp(uint32_t ip, uint32_t tei)
576{
577 RANAP_TransportLayerInformation_t *tli = CALLOC(1, sizeof(*tli));
578 uint32_t *ipbuf = CALLOC(1, sizeof(*ipbuf));
Harald Welte94a62d52015-12-19 02:37:48 +0100579 uint32_t binding_buf = tei;
Harald Weltef8db61b2015-12-18 17:29:59 +0100580
Harald Welte94a62d52015-12-19 02:37:48 +0100581 asn1_u32_to_bitstring(&tli->transportLayerAddress, ipbuf, ip);
Harald Weltef8db61b2015-12-18 17:29:59 +0100582 tli->iuTransportAssociation.present = RANAP_IuTransportAssociation_PR_gTP_TEI;
583 OCTET_STRING_fromBuf(&tli->iuTransportAssociation.choice.bindingID,
584 (const char *) &binding_buf, sizeof(binding_buf));
585
586 return tli;
587}
588
589static RANAP_UserPlaneInformation_t *new_upi(long mode, uint8_t mode_versions)
590{
591 RANAP_UserPlaneInformation_t *upi = CALLOC(1, sizeof(*upi));
592 uint16_t *buf = CALLOC(1, sizeof(*buf));
593
594 *buf = mode_versions;
595
596 upi->userPlaneMode = mode;
597 upi->uP_ModeVersions.buf = buf;
598 upi->uP_ModeVersions.size = sizeof(*buf);
599 upi->uP_ModeVersions.bits_unused = 0;
600
601 return upi;
602}
603
604
605static void assign_new_ra_id(RANAP_RAB_ID_t *id, uint8_t rab_id)
606{
607 uint8_t *buf = CALLOC(1, sizeof(*buf));
Harald Welte0a3eafe2015-12-19 02:38:09 +0100608 *buf = rab_id;
Harald Weltef8db61b2015-12-18 17:29:59 +0100609
610 id->buf = buf;
611 id->size = 1;
612 id->bits_unused = 0;
613}
614
615/*! \brief generate RANAP RAB ASSIGNMENT REQUEST message for CS (voice) */
616struct msgb *ranap_new_msg_rab_assign_voice(uint8_t rab_id, uint32_t rtp_ip, uint16_t rtp_port)
617{
618 RANAP_ProtocolIE_FieldPair_t *pair;
619 RANAP_RAB_AssignmentRequestIEs_t ies;
620 RANAP_RAB_AssignmentRequest_t out;
621 struct msgb *msg;
622 int rc;
623
624 memset(&ies, 0, sizeof(ies));
625 memset(&out, 0, sizeof(out));
626
627 /* only assingnment is present, no release */
628 ies.presenceMask = RAB_ASSIGNMENTREQUESTIES_RANAP_RAB_SETUPORMODIFYLIST_PRESENT;
629
630 /* put together the 'First' part */
631 RANAP_RAB_SetupOrModifyItemFirst_t first;
632 memset(&first, 0, sizeof(first));
633 assign_new_ra_id(&first.rAB_ID, rab_id);
634 //first.nAS_SynchronisationIndicator = FIXME;
635 first.rAB_Parameters = new_rab_par_voice();
636 first.userPlaneInformation = new_upi(RANAP_UserPlaneMode_support_mode_for_predefined_SDU_sizes, 1); /* 2? */
637 first.transportLayerInformation = new_transp_info_rtp(rtp_ip, rtp_port);
638
639 /* put together the 'Second' part */
640 RANAP_RAB_SetupOrModifyItemSecond_t second;
641 memset(&second, 0, sizeof(second));
642
643 /* Build an IE Pair out of first and second part:
644 * (first, second) -> pair */
645 pair = ranap_new_ie_pair(RANAP_ProtocolIE_ID_id_RAB_SetupOrModifyItem,
646 RANAP_Criticality_reject,
647 &asn_DEF_RANAP_RAB_SetupOrModifyItemFirst, &first,
648 RANAP_Criticality_ignore,
649 &asn_DEF_RANAP_RAB_SetupOrModifyItemSecond, &second);
650
651 /* the pair has been made, we can release any of its elements */
652 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemFirst, &first);
653 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemSecond, &second);
654
Harald Welteb7f67c42015-12-19 02:36:52 +0100655 RANAP_ProtocolIE_ContainerPair_t *container_pair = CALLOC(1, sizeof(*container_pair));
Harald Weltef8db61b2015-12-18 17:29:59 +0100656 /* Add the pair to the list of IEs of the RAB ass.req */
Harald Welteb7f67c42015-12-19 02:36:52 +0100657 ASN_SEQUENCE_ADD(container_pair, pair);
658 ASN_SEQUENCE_ADD(&ies.raB_SetupOrModifyList.list, container_pair);
Harald Weltef8db61b2015-12-18 17:29:59 +0100659
660 /* encode the IEs into the actual assignment request:
661 * ies -> out */
662 rc = ranap_encode_rab_assignmentrequesties(&out, &ies);
663 /* 'out' has been generated, we can now release the input */
664 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyList,
665 &ies.raB_SetupOrModifyList);
666 if (rc < 0)
667 return NULL;
668
669 /* generate an Initiating Mesasage: out -> msg */
670 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_RAB_Assignment,
671 RANAP_Criticality_reject,
672 &asn_DEF_RANAP_RAB_AssignmentRequest, &out);
673
674 /* 'msg' has been generated, we cann now release the input 'out' */
675 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_AssignmentRequest, &out);
676
677 return msg;
678}
679
680/*! \brief generate RANAP RAB ASSIGNMENT REQUEST message for PS (data) */
681struct msgb *ranap_new_msg_rab_assign_data(uint8_t rab_id, uint32_t gtp_ip, uint32_t gtp_tei)
682{
683 RANAP_ProtocolIE_FieldPair_t *pair;
684 RANAP_RAB_AssignmentRequestIEs_t ies;
685 RANAP_RAB_AssignmentRequest_t out;
686 RANAP_DataVolumeReportingIndication_t *dat_vol_ind;
687 struct msgb *msg;
688 int rc;
689
690 memset(&ies, 0, sizeof(ies));
691 memset(&out, 0, sizeof(out));
692
693 /* only assingnment is present, no release */
694 ies.presenceMask = RAB_ASSIGNMENTREQUESTIES_RANAP_RAB_SETUPORMODIFYLIST_PRESENT;
695
696 /* put together the 'First' part */
697 RANAP_RAB_SetupOrModifyItemFirst_t first;
698 memset(&first, 0, sizeof(first));
699 assign_new_ra_id(&first.rAB_ID, rab_id);
700 //first.nAS_SynchronisationIndicator = FIXME;
701 first.rAB_Parameters = new_rab_par_data();
702 first.userPlaneInformation = new_upi(RANAP_UserPlaneMode_transparent_mode, 1);
703 first.transportLayerInformation = new_transp_info_rtp(gtp_ip, gtp_tei);
704
705 /* put together the 'Second' part */
706 RANAP_RAB_SetupOrModifyItemSecond_t second;
707 memset(&second, 0, sizeof(second));
708 second.pDP_TypeInformation = CALLOC(1, sizeof(*second.pDP_TypeInformation));
709 ASN_SEQUENCE_ADD(second.pDP_TypeInformation, new_long(RANAP_PDP_Type_ipv4));
710 dat_vol_ind = CALLOC(1, sizeof(*dat_vol_ind));
711 *dat_vol_ind = RANAP_DataVolumeReportingIndication_do_not_report;
712 second.dataVolumeReportingIndication = dat_vol_ind;
713 second.dl_GTP_PDU_SequenceNumber = new_long(0);
714 second.ul_GTP_PDU_SequenceNumber = new_long(0);
715
716 /* Build an IE Pair out of first and second part:
717 * (first, second) -> pair */
718 pair = ranap_new_ie_pair(RANAP_ProtocolIE_ID_id_RAB_SetupOrModifyItem,
719 RANAP_Criticality_reject,
720 &asn_DEF_RANAP_RAB_SetupOrModifyItemFirst,
721 &first, RANAP_Criticality_ignore,
722 &asn_DEF_RANAP_RAB_SetupOrModifyItemSecond,
723 &second);
724
725 /* the pair has been made, we can release any of its elements */
726 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemFirst, &first);
727 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemSecond, &second);
728
Harald Welteb7f67c42015-12-19 02:36:52 +0100729 RANAP_ProtocolIE_ContainerPair_t *container_pair = CALLOC(1, sizeof(*container_pair));
Harald Weltef8db61b2015-12-18 17:29:59 +0100730 /* Add the pair to the list of IEs of the RAB ass.req */
Harald Welteb7f67c42015-12-19 02:36:52 +0100731 ASN_SEQUENCE_ADD(container_pair, pair);
732 /* Add the pair to the list of IEs of the RAB ass.req */
733 ASN_SEQUENCE_ADD(&ies.raB_SetupOrModifyList.list, container_pair);
Harald Weltef8db61b2015-12-18 17:29:59 +0100734
735 /* encode the IEs into the actual assignment request:
736 * ies -> out */
737 rc = ranap_encode_rab_assignmentrequesties(&out, &ies);
738 /* 'out' has been generated, we can now release the input */
739 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyList,
740 &ies.raB_SetupOrModifyList);
741 if (rc < 0)
742 return NULL;
743
744 /* generate an Initiating Mesasage: out -> msg */
745 msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_RAB_Assignment,
746 RANAP_Criticality_reject,
747 &asn_DEF_RANAP_RAB_AssignmentRequest, &out);
748
749 /* 'msg' has been generated, we cann now release the input 'out' */
750 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_AssignmentRequest, &out);
751
752 return msg;
753}