Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 1 | /* 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 Hofmeyr | 96979af | 2016-01-05 15:19:44 +0100 | [diff] [blame] | 25 | #include <osmocom/ranap/iu_helpers.h> |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 26 | |
Neels Hofmeyr | 96979af | 2016-01-05 15:19:44 +0100 | [diff] [blame] | 27 | #include <osmocom/ranap/ranap_common.h> |
| 28 | #include <osmocom/ranap/ranap_ies_defs.h> |
| 29 | #include <osmocom/ranap/ranap_msg_factory.h> |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 30 | |
Harald Welte | bdf3fd1 | 2016-01-03 17:04:09 +0100 | [diff] [blame] | 31 | #define DRANAP _ranap_DRANAP |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 32 | |
| 33 | /*! \brief allocate a new long and assing a value to it */ |
| 34 | static long *new_long(long in) |
| 35 | { |
| 36 | long *out = CALLOC(1, sizeof(long)); |
| 37 | *out = in; |
| 38 | return out; |
| 39 | } |
| 40 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 41 | /*! \brief generate RANAP RESET message */ |
| 42 | struct msgb *ranap_new_msg_reset(RANAP_CN_DomainIndicator_t domain, |
Harald Welte | 1cdb81d | 2016-01-01 16:21:05 +0100 | [diff] [blame] | 43 | const RANAP_Cause_t *cause) |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 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 */ |
| 74 | struct 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 Welte | 74157f6 | 2016-01-01 16:43:59 +0100 | [diff] [blame] | 89 | OCTET_STRING_noalloc(&ies.globalRNC_ID.pLMNidentity, |
Harald Welte | ea98b6f | 2015-12-24 15:09:06 +0100 | [diff] [blame] | 90 | rnc_id->pLMNidentity.buf, |
| 91 | rnc_id->pLMNidentity.size); |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 92 | 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 Welte | ea98b6f | 2015-12-24 15:09:06 +0100 | [diff] [blame] | 115 | /*! \brief generate RANAP INITIAL UE message */ |
| 116 | struct 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 Welte | 74157f6 | 2016-01-01 16:43:59 +0100 | [diff] [blame] | 133 | 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 Welte | ea98b6f | 2015-12-24 15:09:06 +0100 | [diff] [blame] | 135 | |
Harald Welte | 74157f6 | 2016-01-01 16:43:59 +0100 | [diff] [blame] | 136 | 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 Welte | ea98b6f | 2015-12-24 15:09:06 +0100 | [diff] [blame] | 139 | |
Harald Welte | 74157f6 | 2016-01-01 16:43:59 +0100 | [diff] [blame] | 140 | OCTET_STRING_noalloc(&ies.nas_pdu, nas_pdu, nas_len); |
Harald Welte | ea98b6f | 2015-12-24 15:09:06 +0100 | [diff] [blame] | 141 | asn1_u24_to_bitstring(&ies.iuSigConId, &ctxidbuf, conn_id); |
Harald Welte | 74157f6 | 2016-01-01 16:43:59 +0100 | [diff] [blame] | 142 | OCTET_STRING_noalloc(&ies.globalRNC_ID.pLMNidentity, rnc_id->pLMNidentity.buf, rnc_id->pLMNidentity.size); |
Harald Welte | ea98b6f | 2015-12-24 15:09:06 +0100 | [diff] [blame] | 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 Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 164 | /*! \brief generate RANAP DIRECT TRANSFER message */ |
| 165 | struct 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 Welte | 74157f6 | 2016-01-01 16:43:59 +0100 | [diff] [blame] | 183 | /* Avoid copying + later freeing of OCTET STRING */ |
| 184 | OCTET_STRING_noalloc(&ies.nas_pdu, nas, nas_len); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 185 | |
| 186 | /* ies -> dt */ |
| 187 | rc = ranap_encode_directtransferies(&dt, &ies); |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 188 | if (rc < 0) { |
| 189 | LOGP(DRANAP, LOGL_ERROR, "error encoding direct transfer IEs: %d\n", rc); |
| 190 | return NULL; |
| 191 | } |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 192 | |
| 193 | /* dt -> msg */ |
| 194 | msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_DirectTransfer, |
| 195 | RANAP_Criticality_reject, |
| 196 | &asn_DEF_RANAP_DirectTransfer, |
| 197 | &dt); |
| 198 | |
| 199 | /* release dynamic allocations attached to dt */ |
| 200 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_DirectTransfer, &dt); |
| 201 | |
| 202 | return msg; |
| 203 | } |
| 204 | |
| 205 | static const enum RANAP_IntegrityProtectionAlgorithm ip_alg[2] = { |
| 206 | RANAP_IntegrityProtectionAlgorithm_standard_UMTS_integrity_algorithm_UIA1, |
| 207 | RANAP_IntegrityProtectionAlgorithm_standard_UMTS_integrity_algorithm_UIA2, |
| 208 | }; |
| 209 | |
| 210 | static const RANAP_EncryptionAlgorithm_t enc_alg[2] = { |
| 211 | RANAP_EncryptionAlgorithm_standard_UMTS_encryption_algorith_UEA1, |
| 212 | RANAP_EncryptionAlgorithm_standard_UMTS_encryption_algorithm_UEA2, |
| 213 | }; |
| 214 | |
| 215 | /*! \brief generate RANAP SECURITY MODE COMMAND message */ |
Daniel Willmann | f44d12c | 2016-04-20 10:16:37 +0200 | [diff] [blame] | 216 | struct msgb *ranap_new_msg_sec_mod_cmd(const uint8_t *ik, const uint8_t *ck, enum RANAP_KeyStatus status) |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 217 | { |
| 218 | RANAP_SecurityModeCommandIEs_t ies; |
| 219 | RANAP_SecurityModeCommand_t out; |
| 220 | struct msgb *msg; |
| 221 | int i, rc; |
| 222 | |
| 223 | memset(&ies, 0, sizeof(ies)); |
| 224 | memset(&out, 0, sizeof(out)); |
| 225 | |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 226 | for (i = 0; i < ARRAY_SIZE(ip_alg); i++) { |
| 227 | /* needs to be dynamically allocated, as |
| 228 | * SET_OF_free() will call FREEMEM() on it */ |
| 229 | RANAP_IntegrityProtectionAlgorithm_t *alg = CALLOC(1, sizeof(*alg)); |
| 230 | *alg = ip_alg[i]; |
| 231 | ASN_SEQUENCE_ADD(&ies.integrityProtectionInformation.permittedAlgorithms, alg); |
| 232 | } |
| 233 | |
| 234 | BIT_STRING_fromBuf(&ies.integrityProtectionInformation.key, ik, 16*8); |
| 235 | |
| 236 | if (ck) { |
Harald Welte | 2cf0d8f | 2015-12-28 13:13:47 +0100 | [diff] [blame] | 237 | ies.presenceMask = SECURITYMODECOMMANDIES_RANAP_ENCRYPTIONINFORMATION_PRESENT; |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 238 | for (i = 0; i < ARRAY_SIZE(ip_alg); i++) { |
| 239 | /* needs to be dynamically allocated, as |
| 240 | * SET_OF_free() will call FREEMEM() on it */ |
| 241 | RANAP_EncryptionAlgorithm_t *alg = CALLOC(1, sizeof(*alg)); |
| 242 | *alg = enc_alg[i]; |
| 243 | ASN_SEQUENCE_ADD(&ies.encryptionInformation.permittedAlgorithms, alg); |
| 244 | } |
| 245 | BIT_STRING_fromBuf(&ies.encryptionInformation.key, ck, 16*8); |
| 246 | } |
| 247 | |
Daniel Willmann | f44d12c | 2016-04-20 10:16:37 +0200 | [diff] [blame] | 248 | ies.keyStatus = status; |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 249 | |
| 250 | /* ies -> out */ |
| 251 | rc = ranap_encode_securitymodecommandies(&out, &ies); |
| 252 | |
| 253 | /* release dynamic allocations attached to ies */ |
| 254 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_IntegrityProtectionInformation, &ies.integrityProtectionInformation); |
Harald Welte | 2cf0d8f | 2015-12-28 13:13:47 +0100 | [diff] [blame] | 255 | if (ck) |
| 256 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_EncryptionInformation, &ies.encryptionInformation); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 257 | |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 258 | if (rc < 0) { |
| 259 | LOGP(DRANAP, LOGL_ERROR, "error encoding security mode command IEs: %d\n", rc); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 263 | /* out -> msg */ |
| 264 | msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_SecurityModeControl, |
| 265 | RANAP_Criticality_reject, |
| 266 | &asn_DEF_RANAP_SecurityModeCommand, |
| 267 | &out); |
| 268 | |
| 269 | /* release dynamic allocations attached to out */ |
| 270 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_SecurityModeCommand, &out); |
| 271 | |
| 272 | return msg; |
| 273 | } |
| 274 | |
Neels Hofmeyr | 7e760acc | 2016-04-19 01:41:17 +0200 | [diff] [blame] | 275 | /*! \brief generate RANAP SECURITY MODE COMPLETE message */ |
Neels Hofmeyr | a9f5566 | 2016-04-19 01:21:41 +0200 | [diff] [blame] | 276 | struct msgb *ranap_new_msg_sec_mod_compl( |
| 277 | RANAP_ChosenIntegrityProtectionAlgorithm_t chosen_ip_alg, |
| 278 | RANAP_ChosenEncryptionAlgorithm_t chosen_enc_alg) |
| 279 | { |
| 280 | RANAP_SecurityModeCompleteIEs_t ies; |
| 281 | RANAP_SecurityModeComplete_t out; |
| 282 | struct msgb *msg; |
Philipp Maier | aa8d48c | 2017-12-19 17:48:38 +0100 | [diff] [blame] | 283 | int rc; |
Neels Hofmeyr | a9f5566 | 2016-04-19 01:21:41 +0200 | [diff] [blame] | 284 | |
| 285 | memset(&ies, 0, sizeof(ies)); |
| 286 | memset(&out, 0, sizeof(out)); |
| 287 | |
| 288 | ies.presenceMask = SECURITYMODECOMPLETEIES_RANAP_CHOSENENCRYPTIONALGORITHM_PRESENT; |
| 289 | ies.chosenIntegrityProtectionAlgorithm = chosen_ip_alg; |
| 290 | ies.chosenEncryptionAlgorithm = chosen_enc_alg; |
| 291 | |
| 292 | /* ies -> out */ |
| 293 | rc = ranap_encode_securitymodecompleteies(&out, &ies); |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 294 | if (rc < 0) { |
| 295 | LOGP(DRANAP, LOGL_ERROR, "error encoding security mode complete IEs: %d\n", rc); |
| 296 | return NULL; |
| 297 | } |
Neels Hofmeyr | a9f5566 | 2016-04-19 01:21:41 +0200 | [diff] [blame] | 298 | |
| 299 | /* out -> msg */ |
| 300 | msg = ranap_generate_successful_outcome(RANAP_ProcedureCode_id_SecurityModeControl, |
| 301 | RANAP_Criticality_reject, |
| 302 | &asn_DEF_RANAP_SecurityModeComplete, |
| 303 | &out); |
| 304 | |
| 305 | /* release dynamic allocations attached to out */ |
| 306 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_SecurityModeComplete, &out); |
| 307 | |
| 308 | return msg; |
| 309 | } |
| 310 | |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 311 | /*! \brief generate RANAP COMMON ID message */ |
| 312 | struct msgb *ranap_new_msg_common_id(const char *imsi) |
| 313 | { |
| 314 | RANAP_CommonID_IEs_t ies; |
| 315 | RANAP_CommonID_t out; |
| 316 | struct msgb *msg; |
| 317 | int rc; |
| 318 | |
| 319 | memset(&ies, 0, sizeof(ies)); |
| 320 | memset(&out, 0, sizeof(out)); |
| 321 | |
| 322 | if (imsi) { |
| 323 | uint8_t *imsi_buf = CALLOC(1, 16); |
Harald Welte | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 324 | rc = ranap_imsi_encode(imsi_buf, 16, imsi); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 325 | ies.permanentNAS_UE_ID.present = RANAP_PermanentNAS_UE_ID_PR_iMSI; |
| 326 | ies.permanentNAS_UE_ID.choice.iMSI.buf = imsi_buf; |
| 327 | ies.permanentNAS_UE_ID.choice.iMSI.size = rc; |
| 328 | } else |
| 329 | ies.permanentNAS_UE_ID.present = RANAP_PermanentNAS_UE_ID_PR_NOTHING; |
| 330 | |
| 331 | /* ies -> out */ |
| 332 | rc = ranap_encode_commonid_ies(&out, &ies); |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 333 | |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 334 | /* release dynamic allocations attached to ies */ |
| 335 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_PermanentNAS_UE_ID, &ies.permanentNAS_UE_ID); |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 336 | |
| 337 | if (rc < 0) { |
| 338 | LOGP(DRANAP, LOGL_ERROR, "error encoding common id IEs: %d\n", rc); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 339 | return NULL; |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 340 | } |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 341 | |
| 342 | /* out -> msg */ |
| 343 | msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_CommonID, |
| 344 | RANAP_Criticality_ignore, |
| 345 | &asn_DEF_RANAP_CommonID, |
| 346 | &out); |
| 347 | |
| 348 | /* release dynamic allocations attached to out */ |
| 349 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_CommonID, &out); |
| 350 | |
| 351 | return msg; |
| 352 | } |
| 353 | |
| 354 | /*! \brief generate RANAP IU RELEASE COMMAND message */ |
| 355 | struct msgb *ranap_new_msg_iu_rel_cmd(const RANAP_Cause_t *cause_in) |
| 356 | { |
| 357 | RANAP_Iu_ReleaseCommandIEs_t ies; |
| 358 | RANAP_Iu_ReleaseCommand_t out; |
| 359 | struct msgb *msg; |
| 360 | int rc; |
| 361 | |
| 362 | memset(&ies, 0, sizeof(ies)); |
| 363 | memset(&out, 0, sizeof(out)); |
| 364 | |
| 365 | memcpy(&ies.cause, cause_in, sizeof(ies.cause)); |
| 366 | |
| 367 | /* ies -> out */ |
| 368 | rc = ranap_encode_iu_releasecommandies(&out, &ies); |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 369 | if (rc < 0) { |
| 370 | LOGP(DRANAP, LOGL_ERROR, "error encoding release command IEs: %d\n", rc); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 371 | return NULL; |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 372 | } |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 373 | |
| 374 | /* out -> msg */ |
| 375 | msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_Iu_Release, |
| 376 | RANAP_Criticality_reject, |
| 377 | &asn_DEF_RANAP_Iu_ReleaseCommand, |
| 378 | &out); |
| 379 | |
| 380 | /* release dynamic allocations attached to out */ |
| 381 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_Iu_ReleaseCommand, &out); |
| 382 | |
| 383 | return msg; |
| 384 | } |
| 385 | |
Neels Hofmeyr | f6e16b7 | 2016-04-19 02:32:05 +0200 | [diff] [blame] | 386 | /*! \brief generate RAPAP IU RELEASE COMPLETE message */ |
| 387 | struct msgb *ranap_new_msg_iu_rel_compl(void) |
| 388 | { |
| 389 | RANAP_Iu_ReleaseCompleteIEs_t ies; |
| 390 | RANAP_Iu_ReleaseComplete_t out; |
| 391 | struct msgb *msg; |
| 392 | int rc; |
| 393 | |
| 394 | memset(&ies, 0, sizeof(ies)); |
| 395 | memset(&out, 0, sizeof(out)); |
| 396 | |
| 397 | /* ies -> out */ |
| 398 | rc = ranap_encode_iu_releasecompleteies(&out, &ies); |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 399 | if (rc < 0) { |
| 400 | LOGP(DRANAP, LOGL_ERROR, "error encoding release complete IEs: %d\n", rc); |
Neels Hofmeyr | f6e16b7 | 2016-04-19 02:32:05 +0200 | [diff] [blame] | 401 | return NULL; |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 402 | } |
Neels Hofmeyr | f6e16b7 | 2016-04-19 02:32:05 +0200 | [diff] [blame] | 403 | |
| 404 | /* out -> msg */ |
| 405 | msg = ranap_generate_successful_outcome(RANAP_ProcedureCode_id_Iu_Release, |
| 406 | RANAP_Criticality_reject, |
| 407 | &asn_DEF_RANAP_Iu_ReleaseComplete, |
| 408 | &out); |
| 409 | |
| 410 | /* release dynamic allocations attached to out */ |
| 411 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_Iu_ReleaseComplete, &out); |
| 412 | |
| 413 | return msg; |
| 414 | } |
| 415 | |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 416 | /*! \brief generate RANAP PAGING COMMAND message */ |
| 417 | struct msgb *ranap_new_msg_paging_cmd(const char *imsi, const uint32_t *tmsi, int is_ps, uint32_t cause) |
| 418 | { |
| 419 | RANAP_PagingIEs_t ies; |
| 420 | RANAP_Paging_t out; |
| 421 | struct msgb *msg; |
| 422 | uint8_t *imsi_buf = CALLOC(1, 16); |
| 423 | int rc; |
| 424 | |
| 425 | memset(&ies, 0, sizeof(ies)); |
| 426 | memset(&out, 0, sizeof(out)); |
| 427 | |
| 428 | /* put together the 'ies' */ |
| 429 | if (is_ps) |
| 430 | ies.cN_DomainIndicator = RANAP_CN_DomainIndicator_ps_domain; |
| 431 | else |
| 432 | ies.cN_DomainIndicator = RANAP_CN_DomainIndicator_cs_domain; |
| 433 | |
Harald Welte | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 434 | rc = ranap_imsi_encode(imsi_buf, 16, imsi); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 435 | ies.permanentNAS_UE_ID.present = RANAP_PermanentNAS_UE_ID_PR_iMSI; |
| 436 | ies.permanentNAS_UE_ID.choice.iMSI.buf = imsi_buf; |
| 437 | ies.permanentNAS_UE_ID.choice.iMSI.size = rc; |
| 438 | |
| 439 | if (tmsi) { |
| 440 | uint32_t *tmsi_buf = CALLOC(1, sizeof(*tmsi_buf)); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 441 | ies.presenceMask |= PAGINGIES_RANAP_TEMPORARYUE_ID_PRESENT; |
| 442 | if (is_ps) { |
| 443 | ies.temporaryUE_ID.present = RANAP_TemporaryUE_ID_PR_p_TMSI; |
Daniel Willmann | a9cf70f | 2016-05-04 13:50:43 +0200 | [diff] [blame] | 444 | asn1_u32_to_str(&ies.temporaryUE_ID.choice.tMSI, tmsi_buf, *tmsi); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 445 | } else { |
| 446 | ies.temporaryUE_ID.present = RANAP_TemporaryUE_ID_PR_tMSI; |
Daniel Willmann | a9cf70f | 2016-05-04 13:50:43 +0200 | [diff] [blame] | 447 | asn1_u32_to_str(&ies.temporaryUE_ID.choice.p_TMSI, tmsi_buf, *tmsi); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | if (cause) { |
| 452 | ies.presenceMask |= PAGINGIES_RANAP_PAGINGCAUSE_PRESENT; |
| 453 | ies.pagingCause = cause; |
| 454 | } |
| 455 | |
| 456 | /* ies -> out */ |
| 457 | rc = ranap_encode_pagingies(&out, &ies); |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 458 | |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 459 | /* release dynamic allocation attached to ies */ |
| 460 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_PermanentNAS_UE_ID, &ies.permanentNAS_UE_ID); |
| 461 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_TemporaryUE_ID, &ies.temporaryUE_ID); |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 462 | |
| 463 | if (rc < 0) { |
| 464 | LOGP(DRANAP, LOGL_ERROR, "error encoding paging IEs: %d\n", rc); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 465 | return NULL; |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 466 | } |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 467 | |
| 468 | /* out -> msg */ |
| 469 | msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_Paging, |
| 470 | RANAP_Criticality_reject, |
| 471 | &asn_DEF_RANAP_Paging, |
| 472 | &out); |
| 473 | |
| 474 | /* release dynamic allocations attached to out */ |
| 475 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_Paging, &out); |
| 476 | |
| 477 | return msg; |
| 478 | } |
| 479 | |
| 480 | static RANAP_SDU_ErrorRatio_t *new_sdu_error_ratio(long mantissa, long exponent) |
| 481 | { |
| 482 | RANAP_SDU_ErrorRatio_t *err = CALLOC(1, sizeof(*err)); |
| 483 | |
| 484 | err->mantissa = mantissa; |
| 485 | err->exponent = exponent; |
| 486 | |
| 487 | return err; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | static RANAP_SDU_FormatInformationParameterItem_t * |
| 492 | new_format_info_pars(long sdu_size) |
| 493 | { |
| 494 | RANAP_SDU_FormatInformationParameterItem_t *fmti = CALLOC(1, sizeof(*fmti)); |
| 495 | fmti->subflowSDU_Size = new_long(sdu_size); |
| 496 | return fmti; |
| 497 | } |
| 498 | |
| 499 | enum sdu_par_profile { |
| 500 | SDUPAR_P_VOICE0, |
| 501 | SDUPAR_P_VOICE1, |
| 502 | SDUPAR_P_VOICE2, |
| 503 | SDUPAR_P_DATA, |
| 504 | }; |
| 505 | |
| 506 | /* See Chapter 5 of TS 26.102 */ |
| 507 | static RANAP_SDU_ParameterItem_t *new_sdu_par_item(enum sdu_par_profile profile) |
| 508 | { |
| 509 | RANAP_SDU_ParameterItem_t *sdui = CALLOC(1, sizeof(*sdui)); |
| 510 | RANAP_SDU_FormatInformationParameters_t *fmtip = CALLOC(1, sizeof(*fmtip)); |
| 511 | RANAP_SDU_FormatInformationParameterItem_t *fmti; |
| 512 | |
| 513 | switch (profile) { |
| 514 | case SDUPAR_P_VOICE0: |
| 515 | sdui->sDU_ErrorRatio = new_sdu_error_ratio(1, 5); |
| 516 | sdui->residualBitErrorRatio.mantissa = 1; |
| 517 | sdui->residualBitErrorRatio.exponent = 6; |
| 518 | sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_yes; |
| 519 | sdui->sDU_FormatInformationParameters = fmtip; |
| 520 | fmti = new_format_info_pars(81); |
| 521 | ASN_SEQUENCE_ADD(fmtip, fmti); |
| 522 | fmti = new_format_info_pars(39); |
| 523 | ASN_SEQUENCE_ADD(fmtip, fmti); |
| 524 | /* FIXME: could be 10 SDU descriptors for AMR! */ |
| 525 | break; |
| 526 | case SDUPAR_P_VOICE1: |
| 527 | sdui->residualBitErrorRatio.mantissa = 1; |
| 528 | sdui->residualBitErrorRatio.exponent = 3; |
| 529 | sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_no_error_detection_consideration; |
| 530 | sdui->sDU_FormatInformationParameters = fmtip; |
| 531 | fmti = new_format_info_pars(103); |
| 532 | ASN_SEQUENCE_ADD(fmtip, fmti); |
| 533 | fmti = new_format_info_pars(0); |
| 534 | ASN_SEQUENCE_ADD(fmtip, fmti); |
| 535 | /* FIXME: could be 10 SDU descriptors for AMR! */ |
| 536 | break; |
| 537 | case SDUPAR_P_VOICE2: |
| 538 | sdui->residualBitErrorRatio.mantissa = 5; |
| 539 | sdui->residualBitErrorRatio.exponent = 3; |
| 540 | sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_no_error_detection_consideration; |
| 541 | sdui->sDU_FormatInformationParameters = fmtip; |
| 542 | fmti = new_format_info_pars(60); |
| 543 | ASN_SEQUENCE_ADD(fmtip, fmti); |
| 544 | fmti = new_format_info_pars(0); |
| 545 | ASN_SEQUENCE_ADD(fmtip, fmti); |
| 546 | /* FIXME: could be 10 SDU descriptors for AMR! */ |
| 547 | break; |
| 548 | case SDUPAR_P_DATA: |
| 549 | sdui->sDU_ErrorRatio = new_sdu_error_ratio(1, 4); |
| 550 | sdui->residualBitErrorRatio.mantissa = 1; |
| 551 | sdui->residualBitErrorRatio.exponent = 5; |
| 552 | sdui->deliveryOfErroneousSDU = RANAP_DeliveryOfErroneousSDU_no; |
| 553 | FREEMEM(fmtip); |
| 554 | break; |
| 555 | } |
| 556 | |
| 557 | return sdui; |
| 558 | } |
| 559 | |
| 560 | static RANAP_AllocationOrRetentionPriority_t * |
| 561 | new_alloc_ret_prio(RANAP_PriorityLevel_t level, int capability, int vulnerability, |
| 562 | int queueing_allowed) |
| 563 | { |
| 564 | RANAP_AllocationOrRetentionPriority_t *arp = CALLOC(1, sizeof(*arp)); |
| 565 | |
| 566 | arp->priorityLevel = level; |
| 567 | |
| 568 | if (capability) |
| 569 | arp->pre_emptionCapability = RANAP_Pre_emptionCapability_may_trigger_pre_emption; |
| 570 | else |
| 571 | arp->pre_emptionCapability = RANAP_Pre_emptionCapability_shall_not_trigger_pre_emption; |
| 572 | |
| 573 | if (vulnerability) |
| 574 | arp->pre_emptionVulnerability = RANAP_Pre_emptionVulnerability_pre_emptable; |
| 575 | else |
| 576 | arp->pre_emptionVulnerability = RANAP_Pre_emptionVulnerability_not_pre_emptable; |
| 577 | |
| 578 | if (queueing_allowed) |
| 579 | arp->queuingAllowed = RANAP_QueuingAllowed_queueing_allowed; |
| 580 | else |
| 581 | arp->queuingAllowed = RANAP_QueuingAllowed_queueing_not_allowed; |
| 582 | |
| 583 | return arp; |
| 584 | } |
| 585 | |
| 586 | /* See Chapter 5 of TS 26.102 */ |
Neels Hofmeyr | 8e29b23 | 2017-01-23 16:36:11 +0100 | [diff] [blame] | 587 | static RANAP_RAB_Parameters_t *new_rab_par_voice(long bitrate_guaranteed, |
| 588 | long bitrate_max) |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 589 | { |
| 590 | RANAP_RAB_Parameters_t *rab = CALLOC(1, sizeof(*rab)); |
| 591 | RANAP_SDU_ParameterItem_t *sdui; |
| 592 | |
| 593 | rab->trafficClass = RANAP_TrafficClass_conversational; |
| 594 | rab->rAB_AsymmetryIndicator = RANAP_RAB_AsymmetryIndicator_symmetric_bidirectional; |
| 595 | |
Neels Hofmeyr | 8e29b23 | 2017-01-23 16:36:11 +0100 | [diff] [blame] | 596 | ASN_SEQUENCE_ADD(&rab->maxBitrate.list, new_long(bitrate_max)); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 597 | rab->guaranteedBitRate = CALLOC(1, sizeof(*rab->guaranteedBitRate)); |
Neels Hofmeyr | 8e29b23 | 2017-01-23 16:36:11 +0100 | [diff] [blame] | 598 | ASN_SEQUENCE_ADD(rab->guaranteedBitRate, new_long(bitrate_guaranteed)); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 599 | rab->deliveryOrder = RANAP_DeliveryOrder_delivery_order_requested; |
| 600 | rab->maxSDU_Size = 244; |
| 601 | |
| 602 | sdui = new_sdu_par_item(SDUPAR_P_VOICE0); |
| 603 | ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui); |
| 604 | sdui = new_sdu_par_item(SDUPAR_P_VOICE1); |
| 605 | ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui); |
| 606 | sdui = new_sdu_par_item(SDUPAR_P_VOICE2); |
| 607 | ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui); |
| 608 | |
| 609 | rab->transferDelay = new_long(80); |
| 610 | rab->allocationOrRetentionPriority = new_alloc_ret_prio(RANAP_PriorityLevel_no_priority, 0, 1, 0); |
| 611 | |
| 612 | rab->sourceStatisticsDescriptor = new_long(RANAP_SourceStatisticsDescriptor_speech); |
| 613 | |
| 614 | return rab; |
| 615 | } |
| 616 | |
Neels Hofmeyr | ad14ff9 | 2017-01-23 15:06:40 +0100 | [diff] [blame] | 617 | static RANAP_NAS_SynchronisationIndicator_t *new_rab_nas_sync_ind(int val) |
| 618 | { |
| 619 | uint8_t val_buf = (val / 10) << 4; |
| 620 | RANAP_NAS_SynchronisationIndicator_t *nsi = CALLOC(1, sizeof(*nsi)); |
| 621 | BIT_STRING_fromBuf(nsi, &val_buf, 4); |
| 622 | return nsi; |
| 623 | } |
| 624 | |
Harald Welte | 05ac677 | 2015-12-29 19:09:52 +0100 | [diff] [blame] | 625 | static RANAP_RAB_Parameters_t *new_rab_par_data(uint32_t dl_max_bitrate, uint32_t ul_max_bitrate) |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 626 | { |
| 627 | RANAP_RAB_Parameters_t *rab = CALLOC(1, sizeof(*rab)); |
| 628 | RANAP_SDU_ParameterItem_t *sdui; |
| 629 | |
| 630 | rab->trafficClass = RANAP_TrafficClass_background; |
| 631 | rab->rAB_AsymmetryIndicator = RANAP_RAB_AsymmetryIndicator_asymmetric_bidirectional; |
| 632 | |
Harald Welte | 05ac677 | 2015-12-29 19:09:52 +0100 | [diff] [blame] | 633 | ASN_SEQUENCE_ADD(&rab->maxBitrate.list, new_long(dl_max_bitrate)); |
| 634 | ASN_SEQUENCE_ADD(&rab->maxBitrate.list, new_long(ul_max_bitrate)); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 635 | rab->deliveryOrder = RANAP_DeliveryOrder_delivery_order_requested; |
| 636 | rab->maxSDU_Size = 8000; |
| 637 | |
| 638 | sdui = new_sdu_par_item(SDUPAR_P_DATA); |
| 639 | ASN_SEQUENCE_ADD(&rab->sDU_Parameters, sdui); |
| 640 | |
| 641 | rab->allocationOrRetentionPriority = new_alloc_ret_prio(RANAP_PriorityLevel_no_priority, 0, 0, 0); |
| 642 | |
Harald Welte | bb289e3 | 2016-05-01 15:20:56 +0200 | [diff] [blame] | 643 | RANAP_ProtocolExtensionField_t *pxf = CALLOC(1, sizeof(*pxf)); |
| 644 | pxf->id = RANAP_ProtocolIE_ID_id_RAB_Parameter_ExtendedMaxBitrateList; |
| 645 | pxf->criticality = RANAP_Criticality_ignore; |
| 646 | |
| 647 | RANAP_RAB_Parameter_ExtendedMaxBitrateList_t *rab_mbrlist = CALLOC(1, sizeof(*rab_mbrlist)); |
| 648 | RANAP_ExtendedMaxBitrate_t *xmbr = CALLOC(1, sizeof(*xmbr)); |
| 649 | *xmbr = 42000000; |
| 650 | ASN_SEQUENCE_ADD(&rab_mbrlist->list, xmbr); |
| 651 | |
| 652 | ANY_fromType_aper(&pxf->value, &asn_DEF_RANAP_RAB_Parameter_ExtendedMaxBitrateList, rab_mbrlist); |
| 653 | |
| 654 | ASN_STRUCT_FREE(asn_DEF_RANAP_RAB_Parameter_ExtendedMaxBitrateList, rab_mbrlist); |
| 655 | |
| 656 | rab->iE_Extensions = CALLOC(1, sizeof(*rab->iE_Extensions)); |
| 657 | ASN_SEQUENCE_ADD(&rab->iE_Extensions->list, pxf); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 658 | |
| 659 | return rab; |
| 660 | } |
| 661 | |
Neels Hofmeyr | edf1367 | 2016-04-23 13:50:46 +0200 | [diff] [blame] | 662 | static void new_transp_layer_addr(BIT_STRING_t *out, uint32_t ip, bool use_x213_nsap) |
Harald Welte | bfe49a2 | 2015-12-28 13:14:52 +0100 | [diff] [blame] | 663 | { |
| 664 | uint8_t *buf; |
| 665 | unsigned int len; |
Neels Hofmeyr | 54df0a1 | 2017-11-18 21:00:41 +0100 | [diff] [blame] | 666 | uint32_t ip_h = ntohl(ip); |
Harald Welte | bfe49a2 | 2015-12-28 13:14:52 +0100 | [diff] [blame] | 667 | |
| 668 | if (use_x213_nsap) { |
Neels Hofmeyr | 135bc06 | 2017-01-23 16:49:20 +0100 | [diff] [blame] | 669 | len = 160/8; |
Harald Welte | bfe49a2 | 2015-12-28 13:14:52 +0100 | [diff] [blame] | 670 | buf = CALLOC(len, sizeof(uint8_t)); |
| 671 | buf[0] = 0x35; /* AFI For IANA ICP */ |
| 672 | buf[1] = 0x00; /* See A.5.2.1.2.7 of X.213 */ |
| 673 | buf[2] = 0x01; |
Neels Hofmeyr | 54df0a1 | 2017-11-18 21:00:41 +0100 | [diff] [blame] | 674 | memcpy(&buf[3], &ip_h, sizeof(ip_h)); |
Harald Welte | bfe49a2 | 2015-12-28 13:14:52 +0100 | [diff] [blame] | 675 | } else { |
Neels Hofmeyr | 54df0a1 | 2017-11-18 21:00:41 +0100 | [diff] [blame] | 676 | len = sizeof(ip_h); |
Harald Welte | bfe49a2 | 2015-12-28 13:14:52 +0100 | [diff] [blame] | 677 | buf = CALLOC(len, sizeof(uint8_t)); |
Neels Hofmeyr | 54df0a1 | 2017-11-18 21:00:41 +0100 | [diff] [blame] | 678 | memcpy(buf, &ip_h, sizeof(ip_h)); |
Harald Welte | bfe49a2 | 2015-12-28 13:14:52 +0100 | [diff] [blame] | 679 | } |
| 680 | out->buf = buf; |
| 681 | out->size = len; |
| 682 | out->bits_unused = 0; |
| 683 | } |
| 684 | |
Neels Hofmeyr | f6673b7 | 2016-09-08 15:39:18 +0200 | [diff] [blame] | 685 | static RANAP_TransportLayerInformation_t *new_transp_info_rtp(uint32_t ip, uint16_t port, |
| 686 | bool use_x213_nsap) |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 687 | { |
| 688 | RANAP_TransportLayerInformation_t *tli = CALLOC(1, sizeof(*tli)); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 689 | uint8_t binding_id[4]; |
| 690 | |
| 691 | binding_id[0] = port >> 8; |
| 692 | binding_id[1] = port & 0xff; |
| 693 | binding_id[2] = binding_id[3] = 0; |
| 694 | |
Neels Hofmeyr | f6673b7 | 2016-09-08 15:39:18 +0200 | [diff] [blame] | 695 | new_transp_layer_addr(&tli->transportLayerAddress, ip, use_x213_nsap); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 696 | tli->iuTransportAssociation.present = RANAP_IuTransportAssociation_PR_bindingID; |
| 697 | OCTET_STRING_fromBuf(&tli->iuTransportAssociation.choice.bindingID, |
| 698 | (const char *) binding_id, sizeof(binding_id)); |
| 699 | |
| 700 | return tli; |
| 701 | } |
| 702 | |
Neels Hofmeyr | edf1367 | 2016-04-23 13:50:46 +0200 | [diff] [blame] | 703 | static RANAP_TransportLayerInformation_t *new_transp_info_gtp(uint32_t ip, uint32_t tei, |
| 704 | bool use_x213_nsap) |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 705 | { |
| 706 | RANAP_TransportLayerInformation_t *tli = CALLOC(1, sizeof(*tli)); |
Harald Welte | 01de8d7 | 2015-12-29 19:10:48 +0100 | [diff] [blame] | 707 | uint32_t binding_buf = htonl(tei); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 708 | |
Neels Hofmeyr | edf1367 | 2016-04-23 13:50:46 +0200 | [diff] [blame] | 709 | new_transp_layer_addr(&tli->transportLayerAddress, ip, use_x213_nsap); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 710 | tli->iuTransportAssociation.present = RANAP_IuTransportAssociation_PR_gTP_TEI; |
Harald Welte | 01de8d7 | 2015-12-29 19:10:48 +0100 | [diff] [blame] | 711 | OCTET_STRING_fromBuf(&tli->iuTransportAssociation.choice.gTP_TEI, |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 712 | (const char *) &binding_buf, sizeof(binding_buf)); |
| 713 | |
| 714 | return tli; |
| 715 | } |
| 716 | |
| 717 | static RANAP_UserPlaneInformation_t *new_upi(long mode, uint8_t mode_versions) |
| 718 | { |
| 719 | RANAP_UserPlaneInformation_t *upi = CALLOC(1, sizeof(*upi)); |
| 720 | uint16_t *buf = CALLOC(1, sizeof(*buf)); |
| 721 | |
Daniel Willmann | 49f99cd | 2016-01-26 09:37:22 +0100 | [diff] [blame] | 722 | *buf = ntohs(mode_versions); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 723 | |
| 724 | upi->userPlaneMode = mode; |
Harald Welte | f9c9aa5 | 2015-12-24 15:39:00 +0100 | [diff] [blame] | 725 | upi->uP_ModeVersions.buf = (uint8_t *) buf; |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 726 | upi->uP_ModeVersions.size = sizeof(*buf); |
| 727 | upi->uP_ModeVersions.bits_unused = 0; |
| 728 | |
| 729 | return upi; |
| 730 | } |
| 731 | |
| 732 | |
| 733 | static void assign_new_ra_id(RANAP_RAB_ID_t *id, uint8_t rab_id) |
| 734 | { |
| 735 | uint8_t *buf = CALLOC(1, sizeof(*buf)); |
Harald Welte | 0a3eafe | 2015-12-19 02:38:09 +0100 | [diff] [blame] | 736 | *buf = rab_id; |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 737 | |
| 738 | id->buf = buf; |
| 739 | id->size = 1; |
| 740 | id->bits_unused = 0; |
| 741 | } |
| 742 | |
Neels Hofmeyr | 74b0565 | 2016-09-09 01:25:22 +0200 | [diff] [blame] | 743 | /*! \brief generate RANAP RAB ASSIGNMENT REQUEST message for CS (voice). |
| 744 | * See 3GPP TS 25.413 8.2. |
Neels Hofmeyr | e90f13e | 2017-11-20 17:18:03 +0100 | [diff] [blame] | 745 | * RAB ID: 3GPP TS 25.413 9.2.1.2. |
| 746 | * \param rtp_ip MGW's RTP IPv4 address in *network* byte order. |
Neels Hofmeyr | 74b0565 | 2016-09-09 01:25:22 +0200 | [diff] [blame] | 747 | */ |
Neels Hofmeyr | f6673b7 | 2016-09-08 15:39:18 +0200 | [diff] [blame] | 748 | struct msgb *ranap_new_msg_rab_assign_voice(uint8_t rab_id, uint32_t rtp_ip, |
| 749 | uint16_t rtp_port, |
| 750 | bool use_x213_nsap) |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 751 | { |
| 752 | RANAP_ProtocolIE_FieldPair_t *pair; |
| 753 | RANAP_RAB_AssignmentRequestIEs_t ies; |
| 754 | RANAP_RAB_AssignmentRequest_t out; |
| 755 | struct msgb *msg; |
| 756 | int rc; |
| 757 | |
| 758 | memset(&ies, 0, sizeof(ies)); |
| 759 | memset(&out, 0, sizeof(out)); |
| 760 | |
| 761 | /* only assingnment is present, no release */ |
| 762 | ies.presenceMask = RAB_ASSIGNMENTREQUESTIES_RANAP_RAB_SETUPORMODIFYLIST_PRESENT; |
| 763 | |
| 764 | /* put together the 'First' part */ |
| 765 | RANAP_RAB_SetupOrModifyItemFirst_t first; |
| 766 | memset(&first, 0, sizeof(first)); |
| 767 | assign_new_ra_id(&first.rAB_ID, rab_id); |
Neels Hofmeyr | ad14ff9 | 2017-01-23 15:06:40 +0100 | [diff] [blame] | 768 | first.nAS_SynchronisationIndicator = new_rab_nas_sync_ind(60); |
Neels Hofmeyr | 8e29b23 | 2017-01-23 16:36:11 +0100 | [diff] [blame] | 769 | first.rAB_Parameters = new_rab_par_voice(6700, 12200); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 770 | first.userPlaneInformation = new_upi(RANAP_UserPlaneMode_support_mode_for_predefined_SDU_sizes, 1); /* 2? */ |
Neels Hofmeyr | f6673b7 | 2016-09-08 15:39:18 +0200 | [diff] [blame] | 771 | first.transportLayerInformation = new_transp_info_rtp(rtp_ip, rtp_port, |
| 772 | use_x213_nsap); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 773 | |
| 774 | /* put together the 'Second' part */ |
| 775 | RANAP_RAB_SetupOrModifyItemSecond_t second; |
| 776 | memset(&second, 0, sizeof(second)); |
| 777 | |
| 778 | /* Build an IE Pair out of first and second part: |
| 779 | * (first, second) -> pair */ |
| 780 | pair = ranap_new_ie_pair(RANAP_ProtocolIE_ID_id_RAB_SetupOrModifyItem, |
| 781 | RANAP_Criticality_reject, |
| 782 | &asn_DEF_RANAP_RAB_SetupOrModifyItemFirst, &first, |
| 783 | RANAP_Criticality_ignore, |
| 784 | &asn_DEF_RANAP_RAB_SetupOrModifyItemSecond, &second); |
| 785 | |
| 786 | /* the pair has been made, we can release any of its elements */ |
| 787 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemFirst, &first); |
| 788 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemSecond, &second); |
| 789 | |
Harald Welte | b7f67c4 | 2015-12-19 02:36:52 +0100 | [diff] [blame] | 790 | RANAP_ProtocolIE_ContainerPair_t *container_pair = CALLOC(1, sizeof(*container_pair)); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 791 | /* Add the pair to the list of IEs of the RAB ass.req */ |
Harald Welte | b7f67c4 | 2015-12-19 02:36:52 +0100 | [diff] [blame] | 792 | ASN_SEQUENCE_ADD(container_pair, pair); |
| 793 | ASN_SEQUENCE_ADD(&ies.raB_SetupOrModifyList.list, container_pair); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 794 | |
| 795 | /* encode the IEs into the actual assignment request: |
| 796 | * ies -> out */ |
| 797 | rc = ranap_encode_rab_assignmentrequesties(&out, &ies); |
| 798 | /* 'out' has been generated, we can now release the input */ |
| 799 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyList, |
| 800 | &ies.raB_SetupOrModifyList); |
| 801 | if (rc < 0) |
| 802 | return NULL; |
| 803 | |
| 804 | /* generate an Initiating Mesasage: out -> msg */ |
| 805 | msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_RAB_Assignment, |
| 806 | RANAP_Criticality_reject, |
| 807 | &asn_DEF_RANAP_RAB_AssignmentRequest, &out); |
| 808 | |
| 809 | /* 'msg' has been generated, we cann now release the input 'out' */ |
| 810 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_AssignmentRequest, &out); |
| 811 | |
| 812 | return msg; |
| 813 | } |
| 814 | |
Neels Hofmeyr | e90f13e | 2017-11-20 17:18:03 +0100 | [diff] [blame] | 815 | /*! \brief generate RANAP RAB ASSIGNMENT REQUEST message for PS (data) |
| 816 | * \param gtp_ip SGSN's GTP IPv4 address in *network* byte order. */ |
Neels Hofmeyr | edf1367 | 2016-04-23 13:50:46 +0200 | [diff] [blame] | 817 | struct msgb *ranap_new_msg_rab_assign_data(uint8_t rab_id, uint32_t gtp_ip, |
| 818 | uint32_t gtp_tei, bool use_x213_nsap) |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 819 | { |
| 820 | RANAP_ProtocolIE_FieldPair_t *pair; |
| 821 | RANAP_RAB_AssignmentRequestIEs_t ies; |
| 822 | RANAP_RAB_AssignmentRequest_t out; |
| 823 | RANAP_DataVolumeReportingIndication_t *dat_vol_ind; |
| 824 | struct msgb *msg; |
| 825 | int rc; |
| 826 | |
| 827 | memset(&ies, 0, sizeof(ies)); |
| 828 | memset(&out, 0, sizeof(out)); |
| 829 | |
| 830 | /* only assingnment is present, no release */ |
| 831 | ies.presenceMask = RAB_ASSIGNMENTREQUESTIES_RANAP_RAB_SETUPORMODIFYLIST_PRESENT; |
| 832 | |
| 833 | /* put together the 'First' part */ |
| 834 | RANAP_RAB_SetupOrModifyItemFirst_t first; |
| 835 | memset(&first, 0, sizeof(first)); |
| 836 | assign_new_ra_id(&first.rAB_ID, rab_id); |
| 837 | //first.nAS_SynchronisationIndicator = FIXME; |
Harald Welte | 05ac677 | 2015-12-29 19:09:52 +0100 | [diff] [blame] | 838 | |
| 839 | first.rAB_Parameters = new_rab_par_data(1600000, 800000); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 840 | first.userPlaneInformation = new_upi(RANAP_UserPlaneMode_transparent_mode, 1); |
Neels Hofmeyr | edf1367 | 2016-04-23 13:50:46 +0200 | [diff] [blame] | 841 | first.transportLayerInformation = new_transp_info_gtp(gtp_ip, gtp_tei, |
| 842 | use_x213_nsap); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 843 | |
| 844 | /* put together the 'Second' part */ |
| 845 | RANAP_RAB_SetupOrModifyItemSecond_t second; |
| 846 | memset(&second, 0, sizeof(second)); |
| 847 | second.pDP_TypeInformation = CALLOC(1, sizeof(*second.pDP_TypeInformation)); |
| 848 | ASN_SEQUENCE_ADD(second.pDP_TypeInformation, new_long(RANAP_PDP_Type_ipv4)); |
| 849 | dat_vol_ind = CALLOC(1, sizeof(*dat_vol_ind)); |
| 850 | *dat_vol_ind = RANAP_DataVolumeReportingIndication_do_not_report; |
| 851 | second.dataVolumeReportingIndication = dat_vol_ind; |
| 852 | second.dl_GTP_PDU_SequenceNumber = new_long(0); |
| 853 | second.ul_GTP_PDU_SequenceNumber = new_long(0); |
| 854 | |
| 855 | /* Build an IE Pair out of first and second part: |
| 856 | * (first, second) -> pair */ |
| 857 | pair = ranap_new_ie_pair(RANAP_ProtocolIE_ID_id_RAB_SetupOrModifyItem, |
| 858 | RANAP_Criticality_reject, |
| 859 | &asn_DEF_RANAP_RAB_SetupOrModifyItemFirst, |
| 860 | &first, RANAP_Criticality_ignore, |
| 861 | &asn_DEF_RANAP_RAB_SetupOrModifyItemSecond, |
| 862 | &second); |
| 863 | |
| 864 | /* the pair has been made, we can release any of its elements */ |
| 865 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemFirst, &first); |
| 866 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyItemSecond, &second); |
| 867 | |
Harald Welte | b7f67c4 | 2015-12-19 02:36:52 +0100 | [diff] [blame] | 868 | RANAP_ProtocolIE_ContainerPair_t *container_pair = CALLOC(1, sizeof(*container_pair)); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 869 | /* Add the pair to the list of IEs of the RAB ass.req */ |
Neels Hofmeyr | 9246cc9 | 2016-04-25 14:47:26 +0200 | [diff] [blame] | 870 | ASN_SEQUENCE_ADD(&container_pair->list, pair); |
Harald Welte | b7f67c4 | 2015-12-19 02:36:52 +0100 | [diff] [blame] | 871 | /* Add the pair to the list of IEs of the RAB ass.req */ |
| 872 | ASN_SEQUENCE_ADD(&ies.raB_SetupOrModifyList.list, container_pair); |
Harald Welte | f8db61b | 2015-12-18 17:29:59 +0100 | [diff] [blame] | 873 | |
| 874 | /* encode the IEs into the actual assignment request: |
| 875 | * ies -> out */ |
| 876 | rc = ranap_encode_rab_assignmentrequesties(&out, &ies); |
| 877 | /* 'out' has been generated, we can now release the input */ |
| 878 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_SetupOrModifyList, |
| 879 | &ies.raB_SetupOrModifyList); |
| 880 | if (rc < 0) |
| 881 | return NULL; |
| 882 | |
| 883 | /* generate an Initiating Mesasage: out -> msg */ |
| 884 | msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_RAB_Assignment, |
| 885 | RANAP_Criticality_reject, |
| 886 | &asn_DEF_RANAP_RAB_AssignmentRequest, &out); |
| 887 | |
| 888 | /* 'msg' has been generated, we cann now release the input 'out' */ |
| 889 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_AssignmentRequest, &out); |
| 890 | |
| 891 | return msg; |
| 892 | } |
Harald Welte | 37223d8 | 2016-01-01 16:18:55 +0100 | [diff] [blame] | 893 | |
| 894 | struct msgb *ranap_new_msg_iu_rel_req(const RANAP_Cause_t *cause) |
| 895 | { |
| 896 | RANAP_Iu_ReleaseRequestIEs_t ies; |
| 897 | RANAP_Iu_ReleaseRequest_t out; |
| 898 | struct msgb *msg; |
| 899 | int rc; |
| 900 | |
| 901 | memset(&ies, 0, sizeof(ies)); |
| 902 | memset(&out, 0, sizeof(out)); |
| 903 | |
| 904 | memcpy(&ies.cause, cause, sizeof(ies.cause)); |
| 905 | |
| 906 | rc = ranap_encode_iu_releaserequesties(&out, &ies); |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 907 | if (rc < 0) { |
| 908 | LOGP(DRANAP, LOGL_ERROR, "error encoding release request IEs: %d\n", rc); |
| 909 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_Iu_ReleaseRequest, &out); |
Harald Welte | 37223d8 | 2016-01-01 16:18:55 +0100 | [diff] [blame] | 910 | return NULL; |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 911 | } |
Harald Welte | 37223d8 | 2016-01-01 16:18:55 +0100 | [diff] [blame] | 912 | |
| 913 | /* encode the output into the msgb */ |
| 914 | msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_Iu_ReleaseRequest, |
| 915 | RANAP_Criticality_reject, |
| 916 | &asn_DEF_RANAP_Iu_ReleaseRequest, &out); |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 917 | |
Harald Welte | 37223d8 | 2016-01-01 16:18:55 +0100 | [diff] [blame] | 918 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_Iu_ReleaseRequest, &out); |
| 919 | |
| 920 | return msg; |
| 921 | } |
| 922 | |
| 923 | struct msgb *ranap_new_msg_rab_rel_req(uint8_t rab_id, const RANAP_Cause_t *cause) |
| 924 | { |
| 925 | RANAP_RAB_ReleaseItemIEs_t item_ies; |
| 926 | RANAP_RAB_ReleaseRequestIEs_t ies; |
| 927 | RANAP_RAB_ReleaseRequest_t out; |
| 928 | struct msgb *msg; |
| 929 | int rc; |
| 930 | |
| 931 | memset(&item_ies, 0, sizeof(item_ies)); |
| 932 | memset(&ies, 0, sizeof(ies)); |
| 933 | memset(&out, 0, sizeof(out)); |
| 934 | |
| 935 | /* put together the ReleaseItem */ |
| 936 | assign_new_ra_id(&item_ies.raB_ReleaseItem.rAB_ID, rab_id); |
| 937 | memcpy(&item_ies.raB_ReleaseItem.cause, cause, sizeof(item_ies.raB_ReleaseItem.cause)); |
| 938 | |
| 939 | /* add to the list */ |
| 940 | rc = ranap_encode_rab_releaseitemies(&ies.raB_ReleaseList, &item_ies); |
| 941 | if (rc < 0) |
| 942 | return NULL; |
| 943 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_ReleaseItem, &item_ies.raB_ReleaseItem); |
| 944 | |
| 945 | /* encoe the list IEs into the output */ |
| 946 | rc = ranap_encode_rab_releaserequesties(&out, &ies); |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 947 | |
Harald Welte | 37223d8 | 2016-01-01 16:18:55 +0100 | [diff] [blame] | 948 | /* 'out' has been generated, we can release the input */ |
| 949 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_ReleaseList, &ies.raB_ReleaseList); |
| 950 | |
Philipp Maier | 1eb19cf | 2017-12-19 17:46:36 +0100 | [diff] [blame] | 951 | if (rc < 0) { |
| 952 | LOGP(DRANAP, LOGL_ERROR, "error encoding release request IEs: %d\n", rc); |
| 953 | return NULL; |
| 954 | } |
| 955 | |
Harald Welte | 37223d8 | 2016-01-01 16:18:55 +0100 | [diff] [blame] | 956 | /* encode the output into the msgb */ |
| 957 | msg = ranap_generate_initiating_message(RANAP_ProcedureCode_id_RAB_ReleaseRequest, |
| 958 | RANAP_Criticality_reject, |
| 959 | &asn_DEF_RANAP_RAB_ReleaseRequest, &out); |
| 960 | |
| 961 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RAB_ReleaseRequest, &out); |
| 962 | |
| 963 | return msg; |
| 964 | } |