Neels Hofmeyr | da6853a | 2016-05-20 21:38:32 +0200 | [diff] [blame] | 1 | /* Common parts of IuCS and IuPS interfaces implementation */ |
| 2 | |
| 3 | /* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 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 <stdint.h> |
| 22 | #include <unistd.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <stdbool.h> |
| 27 | |
| 28 | #include <osmocom/core/select.h> |
| 29 | #include <osmocom/core/prim.h> |
| 30 | #include <osmocom/core/talloc.h> |
| 31 | #include <osmocom/core/logging.h> |
| 32 | #include <osmocom/core/application.h> |
| 33 | #include <osmocom/vty/logging.h> |
| 34 | |
| 35 | #include <osmocom/gsm/gsm48.h> |
| 36 | #include <osmocom/gprs/gprs_msgb.h> |
| 37 | |
| 38 | #include <osmocom/sigtran/sua.h> |
| 39 | #include <osmocom/sigtran/sccp_sap.h> |
| 40 | #include <osmocom/sigtran/sccp_helpers.h> |
| 41 | |
| 42 | #include <openbsc/gprs_sgsn.h> |
| 43 | #include <openbsc/iu.h> |
| 44 | #include <openbsc/debug.h> |
| 45 | |
| 46 | #include <pdp.h> |
| 47 | |
| 48 | #include <osmocom/ranap/ranap_ies_defs.h> |
| 49 | #include <osmocom/ranap/ranap_common.h> |
| 50 | #include <osmocom/ranap/ranap_common_cn.h> |
| 51 | #include <osmocom/ranap/ranap_msg_factory.h> |
| 52 | |
| 53 | #include <asn1c/asn1helpers.h> |
| 54 | |
| 55 | /* Parsed global RNC id. See also struct RANAP_GlobalRNC_ID, and note that the |
| 56 | * PLMN identity is a BCD representation of the MCC and MNC. |
| 57 | * See iu_grnc_id_parse(). */ |
| 58 | struct iu_grnc_id { |
| 59 | uint16_t mcc; |
| 60 | uint16_t mnc; |
| 61 | uint16_t rnc_id; |
| 62 | }; |
| 63 | |
| 64 | /* A remote RNC (Radio Network Controller, like BSC but for UMTS) that has |
| 65 | * called us and is currently reachable at the given osmo_sccp_link. So, when we |
| 66 | * know a LAC for a subscriber, we can page it at the RNC matching that LAC or |
| 67 | * RAC. An HNB-GW typically presents itself as if it were a single RNC, even |
| 68 | * though it may have several RNCs in hNodeBs connected to it. Those will then |
| 69 | * share the same RNC id, which they actually receive and adopt from the HNB-GW |
| 70 | * in the HNBAP HNB REGISTER ACCEPT message. */ |
| 71 | struct iu_rnc { |
| 72 | struct llist_head entry; |
| 73 | |
| 74 | uint16_t rnc_id; |
| 75 | uint16_t lac; /* Location Area Code (used for CS and PS) */ |
| 76 | uint8_t rac; /* Routing Area Code (used for PS only) */ |
| 77 | struct osmo_sccp_link *link; |
| 78 | }; |
| 79 | |
| 80 | void *talloc_iu_ctx; |
| 81 | |
| 82 | int asn1_xer_print = 1; |
| 83 | void *talloc_asn1_ctx; |
| 84 | |
| 85 | iu_recv_cb_t global_iu_recv_cb = NULL; |
| 86 | iu_event_cb_t global_iu_event_cb = NULL; |
| 87 | |
| 88 | static LLIST_HEAD(ue_conn_ctx_list); |
| 89 | static LLIST_HEAD(rnc_list); |
| 90 | |
| 91 | const struct value_string iu_event_type_names[] = { |
| 92 | #define IU_EVT_STR(X) { X, #X } |
| 93 | IU_EVT_STR(IU_EVENT_RAB_ASSIGN), |
| 94 | IU_EVT_STR(IU_EVENT_SECURITY_MODE_COMPLETE), |
| 95 | IU_EVT_STR(IU_EVENT_IU_RELEASE), |
| 96 | IU_EVT_STR(IU_EVENT_LINK_INVALIDATED), |
| 97 | #undef IU_EVT_STR |
| 98 | }; |
| 99 | |
| 100 | struct ue_conn_ctx *ue_conn_ctx_alloc(struct osmo_sccp_link *link, uint32_t conn_id) |
| 101 | { |
| 102 | struct ue_conn_ctx *ctx = talloc_zero(talloc_iu_ctx, struct ue_conn_ctx); |
| 103 | |
| 104 | ctx->link = link; |
| 105 | ctx->conn_id = conn_id; |
| 106 | llist_add(&ctx->list, &ue_conn_ctx_list); |
| 107 | |
| 108 | return ctx; |
| 109 | } |
| 110 | |
| 111 | struct ue_conn_ctx *ue_conn_ctx_find(struct osmo_sccp_link *link, |
| 112 | uint32_t conn_id) |
| 113 | { |
| 114 | struct ue_conn_ctx *ctx; |
| 115 | |
| 116 | llist_for_each_entry(ctx, &ue_conn_ctx_list, list) { |
| 117 | if (ctx->link == link && ctx->conn_id == conn_id) |
| 118 | return ctx; |
| 119 | } |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | static struct iu_rnc *iu_rnc_alloc(uint16_t rnc_id, uint16_t lac, uint8_t rac, |
| 124 | struct osmo_sccp_link *link) |
| 125 | { |
| 126 | struct iu_rnc *rnc = talloc_zero(talloc_iu_ctx, struct iu_rnc); |
| 127 | |
| 128 | rnc->rnc_id = rnc_id; |
| 129 | rnc->lac = lac; |
| 130 | rnc->rac = rac; |
| 131 | rnc->link = link; |
| 132 | llist_add(&rnc->entry, &rnc_list); |
| 133 | |
| 134 | LOGP(DRANAP, LOGL_NOTICE, "New RNC %d (LAC=%d RAC=%d)\n", |
| 135 | rnc->rnc_id, rnc->lac, rnc->rac); |
| 136 | |
| 137 | return rnc; |
| 138 | } |
| 139 | |
| 140 | static struct iu_rnc *iu_rnc_register(uint16_t rnc_id, uint16_t lac, |
| 141 | uint8_t rac, struct osmo_sccp_link *link) |
| 142 | { |
| 143 | struct iu_rnc *rnc; |
| 144 | llist_for_each_entry(rnc, &rnc_list, entry) { |
| 145 | if (rnc->rnc_id != rnc_id) |
| 146 | continue; |
| 147 | |
| 148 | /* We have this RNC Id registered already. Make sure that the |
| 149 | * details match. */ |
| 150 | |
| 151 | /* TODO should a mismatch be an error? */ |
| 152 | if (rnc->lac != lac || rnc->rac != rac) |
| 153 | LOGP(DRANAP, LOGL_NOTICE, "RNC %d changes its details:" |
| 154 | " LAC=%d RAC=%d --> LAC=%d RAC=%d\n", |
| 155 | rnc->rnc_id, rnc->lac, rnc->rac, |
| 156 | lac, rac); |
| 157 | rnc->lac = lac; |
| 158 | rnc->rac = rac; |
| 159 | |
| 160 | if (link && rnc->link != link) |
| 161 | LOGP(DRANAP, LOGL_NOTICE, "RNC %d on new link" |
| 162 | " (LAC=%d RAC=%d)\n", |
| 163 | rnc->rnc_id, rnc->lac, rnc->rac); |
| 164 | rnc->link = link; |
| 165 | return rnc; |
| 166 | } |
| 167 | |
| 168 | /* Not found, make a new one. */ |
| 169 | return iu_rnc_alloc(rnc_id, lac, rac, link); |
| 170 | } |
| 171 | |
| 172 | /* Discard/invalidate all ue_conn_ctx and iu_rnc entries that reference the |
| 173 | * given link, since this link is invalid and about to be deallocated. For |
| 174 | * each ue_conn_ctx, invoke the iu_event_cb_t with IU_EVENT_LINK_INVALIDATED. |
| 175 | */ |
| 176 | void iu_link_del(struct osmo_sccp_link *link) |
| 177 | { |
| 178 | struct iu_rnc *rnc, *rnc_next; |
| 179 | llist_for_each_entry_safe(rnc, rnc_next, &rnc_list, entry) { |
| 180 | if (!rnc->link) |
| 181 | continue; |
| 182 | if (rnc->link != link) |
| 183 | continue; |
| 184 | rnc->link = NULL; |
| 185 | llist_del(&rnc->entry); |
| 186 | talloc_free(rnc); |
| 187 | } |
| 188 | |
| 189 | struct ue_conn_ctx *uec, *uec_next; |
| 190 | llist_for_each_entry_safe(uec, uec_next, &ue_conn_ctx_list, list) { |
| 191 | if (uec->link != link) |
| 192 | continue; |
| 193 | uec->link = NULL; |
| 194 | global_iu_event_cb(uec, IU_EVENT_LINK_INVALIDATED, NULL); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /*********************************************************************** |
| 199 | * RANAP handling |
| 200 | ***********************************************************************/ |
| 201 | |
| 202 | int iu_rab_act(struct ue_conn_ctx *ue_ctx, struct msgb *msg) |
| 203 | { |
| 204 | struct osmo_scu_prim *prim; |
| 205 | |
| 206 | /* wrap RANAP message in SCCP N-DATA.req */ |
| 207 | prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); |
| 208 | prim->u.data.conn_id = ue_ctx->conn_id; |
| 209 | osmo_prim_init(&prim->oph, |
| 210 | SCCP_SAP_USER, |
| 211 | OSMO_SCU_PRIM_N_DATA, |
| 212 | PRIM_OP_REQUEST, |
| 213 | msg); |
| 214 | return osmo_sua_user_link_down(ue_ctx->link, &prim->oph); |
| 215 | } |
| 216 | |
| 217 | int iu_rab_deact(struct ue_conn_ctx *ue_ctx, uint8_t rab_id) |
| 218 | { |
| 219 | /* FIXME */ |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | int iu_tx_sec_mode_cmd(struct ue_conn_ctx *uectx, struct gsm_auth_tuple *tp, |
| 224 | int send_ck, int new_key) |
| 225 | { |
| 226 | struct osmo_scu_prim *prim; |
| 227 | struct msgb *msg; |
| 228 | uint8_t ik[16]; |
| 229 | uint8_t ck[16]; |
| 230 | unsigned int i; |
| 231 | |
| 232 | /* C5 function to derive IK from Kc */ |
| 233 | for (i = 0; i < 4; i++) |
| 234 | ik[i] = tp->vec.kc[i] ^ tp->vec.kc[i+4]; |
| 235 | memcpy(ik+4, tp->vec.kc, 8); |
| 236 | for (i = 12; i < 16; i++) |
| 237 | ik[i] = ik[i-12]; |
| 238 | |
| 239 | if (send_ck) { |
| 240 | /* C4 function to derive CK from Kc */ |
| 241 | memcpy(ck, tp->vec.kc, 8); |
| 242 | memcpy(ck+8, tp->vec.kc, 8); |
| 243 | } |
| 244 | |
| 245 | /* create RANAP message */ |
| 246 | msg = ranap_new_msg_sec_mod_cmd(ik, send_ck? ck : NULL, new_key ? RANAP_KeyStatus_new : RANAP_KeyStatus_old); |
| 247 | msg->l2h = msg->data; |
| 248 | /* wrap RANAP message in SCCP N-DATA.req */ |
| 249 | prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); |
| 250 | prim->u.data.conn_id = uectx->conn_id; |
| 251 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 252 | OSMO_SCU_PRIM_N_DATA, |
| 253 | PRIM_OP_REQUEST, msg); |
| 254 | osmo_sua_user_link_down(uectx->link, &prim->oph); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int iu_grnc_id_parse(struct iu_grnc_id *dst, |
| 260 | struct RANAP_GlobalRNC_ID *src) |
| 261 | { |
| 262 | /* The size is coming from arbitrary sender, check it gracefully */ |
| 263 | if (src->pLMNidentity.size != 3) { |
| 264 | LOGP(DRANAP, LOGL_ERROR, "Invalid PLMN Identity size:" |
| 265 | " should be 3, is %d\n", src->pLMNidentity.size); |
| 266 | return -1; |
| 267 | } |
| 268 | gsm48_mcc_mnc_from_bcd(&src->pLMNidentity.buf[0], |
| 269 | &dst->mcc, &dst->mnc); |
| 270 | dst->rnc_id = (uint16_t)src->rNC_ID; |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | #if 0 |
| 275 | -- not used at present -- |
| 276 | static int iu_grnc_id_compose(struct iu_grnc_id *src, |
| 277 | struct RANAP_GlobalRNC_ID *dst) |
| 278 | { |
| 279 | /* The caller must ensure proper size */ |
| 280 | OSMO_ASSERT(dst->pLMNidentity.size == 3); |
| 281 | gsm48_mcc_mnc_to_bcd(&dst->pLMNidentity.buf[0], |
| 282 | src->mcc, src->mnc); |
| 283 | dst->rNC_ID = src->rnc_id; |
| 284 | return 0; |
| 285 | } |
| 286 | #endif |
| 287 | |
| 288 | static int ranap_handle_co_initial_ue(void *ctx, RANAP_InitialUE_MessageIEs_t *ies) |
| 289 | { |
| 290 | struct ue_conn_ctx *ue_conn = ctx; |
| 291 | struct gprs_ra_id ra_id; |
| 292 | struct iu_grnc_id grnc_id; |
| 293 | uint16_t sai; |
| 294 | struct msgb *msg = msgb_alloc(256, "RANAP->NAS"); |
| 295 | |
| 296 | if (ranap_parse_lai(&ra_id, &ies->lai) != 0) { |
| 297 | LOGP(DRANAP, LOGL_ERROR, "Failed to parse RANAP LAI IE\n"); |
| 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | if (ies->presenceMask & INITIALUE_MESSAGEIES_RANAP_RAC_PRESENT) { |
| 302 | ra_id.rac = asn1str_to_u8(&ies->rac); |
| 303 | } |
| 304 | |
| 305 | if (iu_grnc_id_parse(&grnc_id, &ies->globalRNC_ID) != 0) { |
| 306 | LOGP(DRANAP, LOGL_ERROR, |
| 307 | "Failed to parse RANAP Global-RNC-ID IE\n"); |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | sai = asn1str_to_u16(&ies->sai.sAC); |
| 312 | msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size); |
| 313 | memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size); |
| 314 | |
| 315 | /* Make sure we know the RNC Id and LAC+RAC coming in on this connection. */ |
| 316 | iu_rnc_register(grnc_id.rnc_id, ra_id.lac, ra_id.rac, ue_conn->link); |
| 317 | ue_conn->ra_id = ra_id; |
| 318 | |
| 319 | /* Feed into the MM layer */ |
| 320 | msg->dst = ctx; |
| 321 | global_iu_recv_cb(msg, &ra_id, &sai); |
| 322 | |
| 323 | msgb_free(msg); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies) |
| 329 | { |
| 330 | struct gprs_ra_id _ra_id, *ra_id = NULL; |
| 331 | uint16_t _sai, *sai = NULL; |
| 332 | struct msgb *msg = msgb_alloc(256, "RANAP->NAS"); |
| 333 | |
| 334 | if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) { |
| 335 | if (ranap_parse_lai(&_ra_id, &ies->lai) != 0) { |
| 336 | LOGP(DRANAP, LOGL_ERROR, "Failed to parse RANAP LAI IE\n"); |
| 337 | return -1; |
| 338 | } |
| 339 | ra_id = &_ra_id; |
| 340 | if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) { |
| 341 | _ra_id.rac = asn1str_to_u8(&ies->rac); |
| 342 | } |
| 343 | if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) { |
| 344 | _sai = asn1str_to_u16(&ies->sai.sAC); |
| 345 | sai = &_sai; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size); |
| 350 | memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size); |
| 351 | |
| 352 | /* Feed into the MM/CC/SMS-CP layer */ |
| 353 | msg->dst = ctx; |
| 354 | global_iu_recv_cb(msg, ra_id, sai); |
| 355 | |
| 356 | msgb_free(msg); |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies) |
| 362 | { |
| 363 | if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT) |
| 364 | LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n", |
| 365 | ranap_cause_str(&ies->cause)); |
| 366 | else |
| 367 | LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n"); |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | int iu_tx(struct msgb *msg_nas, uint8_t sapi) |
| 373 | { |
| 374 | struct ue_conn_ctx *uectx = msg_nas->dst; |
| 375 | struct msgb *msg; |
| 376 | struct osmo_scu_prim *prim; |
| 377 | |
| 378 | LOGP(DRANAP, LOGL_INFO, "Transmitting L3 Message as RANAP DT (SUA link %p conn_id %u)\n", |
| 379 | uectx->link, uectx->conn_id); |
| 380 | |
| 381 | msg = ranap_new_msg_dt(sapi, msg_nas->data, msgb_length(msg_nas)); |
| 382 | msgb_free(msg_nas); |
| 383 | msg->l2h = msg->data; |
| 384 | prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); |
| 385 | prim->u.data.conn_id = uectx->conn_id; |
| 386 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 387 | OSMO_SCU_PRIM_N_DATA, |
| 388 | PRIM_OP_REQUEST, msg); |
| 389 | osmo_sua_user_link_down(uectx->link, &prim->oph); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int ranap_handle_co_iu_rel_req(struct ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies) |
| 394 | { |
| 395 | struct msgb *msg; |
| 396 | struct osmo_scu_prim *prim; |
| 397 | |
| 398 | LOGP(DRANAP, LOGL_INFO, "Received Iu Release Request, Sending Release Command\n"); |
| 399 | msg = ranap_new_msg_iu_rel_cmd(&ies->cause); |
| 400 | msg->l2h = msg->data; |
| 401 | prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); |
| 402 | prim->u.data.conn_id = ctx->conn_id; |
| 403 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 404 | OSMO_SCU_PRIM_N_DATA, |
| 405 | PRIM_OP_REQUEST, msg); |
| 406 | osmo_sua_user_link_down(ctx->link, &prim->oph); |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int ranap_handle_co_rab_ass_resp(struct ue_conn_ctx *ctx, RANAP_RAB_AssignmentResponseIEs_t *ies) |
| 411 | { |
| 412 | int rc = -1; |
| 413 | |
Neels Hofmeyr | 9e5cf3d | 2016-10-18 20:19:50 +0200 | [diff] [blame] | 414 | LOGP(DRANAP, LOGL_INFO, |
| 415 | "Rx RAB Assignment Response for UE conn_id %u\n", ctx->conn_id); |
Neels Hofmeyr | da6853a | 2016-05-20 21:38:32 +0200 | [diff] [blame] | 416 | if (ies->presenceMask & RAB_ASSIGNMENTRESPONSEIES_RANAP_RAB_SETUPORMODIFIEDLIST_PRESENT) { |
| 417 | /* TODO: Iterate over list of SetupOrModifiedList IEs and handle each one */ |
| 418 | RANAP_IE_t *ranap_ie = ies->raB_SetupOrModifiedList.raB_SetupOrModifiedList_ies.list.array[0]; |
| 419 | RANAP_RAB_SetupOrModifiedItemIEs_t setup_ies; |
| 420 | |
| 421 | rc = ranap_decode_rab_setupormodifieditemies_fromlist(&setup_ies, &ranap_ie->value); |
| 422 | if (rc) { |
| 423 | LOGP(DRANAP, LOGL_ERROR, "Error in ranap_decode_rab_setupormodifieditemies()\n"); |
| 424 | return rc; |
| 425 | } |
| 426 | |
| 427 | rc = global_iu_event_cb(ctx, IU_EVENT_RAB_ASSIGN, &setup_ies); |
| 428 | |
| 429 | ranap_free_rab_setupormodifieditemies(&setup_ies); |
| 430 | } |
| 431 | |
Neels Hofmeyr | da6853a | 2016-05-20 21:38:32 +0200 | [diff] [blame] | 432 | return rc; |
| 433 | } |
| 434 | |
| 435 | /* Entry point for connection-oriented RANAP message */ |
| 436 | static void cn_ranap_handle_co(void *ctx, ranap_message *message) |
| 437 | { |
| 438 | int rc; |
| 439 | |
| 440 | LOGP(DRANAP, LOGL_NOTICE, "handle_co(dir=%u, proc=%u)\n", message->direction, message->procedureCode); |
| 441 | |
| 442 | switch (message->direction) { |
| 443 | case RANAP_RANAP_PDU_PR_initiatingMessage: |
| 444 | switch (message->procedureCode) { |
| 445 | case RANAP_ProcedureCode_id_InitialUE_Message: |
| 446 | rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs); |
| 447 | break; |
| 448 | case RANAP_ProcedureCode_id_DirectTransfer: |
| 449 | rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs); |
| 450 | break; |
| 451 | case RANAP_ProcedureCode_id_ErrorIndication: |
| 452 | rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs); |
| 453 | break; |
| 454 | case RANAP_ProcedureCode_id_Iu_ReleaseRequest: |
| 455 | /* Iu Release Request */ |
| 456 | rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs); |
| 457 | break; |
| 458 | default: |
| 459 | LOGP(DRANAP, LOGL_ERROR, "Received Initiating Message: unknown Procedure Code %d\n", |
| 460 | message->procedureCode); |
| 461 | rc = -1; |
| 462 | break; |
| 463 | } |
| 464 | break; |
| 465 | case RANAP_RANAP_PDU_PR_successfulOutcome: |
| 466 | switch (message->procedureCode) { |
| 467 | case RANAP_ProcedureCode_id_SecurityModeControl: |
| 468 | /* Security Mode Complete */ |
| 469 | rc = global_iu_event_cb(ctx, IU_EVENT_SECURITY_MODE_COMPLETE, NULL); |
| 470 | break; |
| 471 | case RANAP_ProcedureCode_id_Iu_Release: |
| 472 | /* Iu Release Complete */ |
| 473 | rc = global_iu_event_cb(ctx, IU_EVENT_IU_RELEASE, NULL); |
| 474 | if (rc) { |
| 475 | LOGP(DRANAP, LOGL_ERROR, "Iu Release event: Iu Event callback returned %d\n", |
| 476 | rc); |
| 477 | } |
| 478 | break; |
| 479 | default: |
| 480 | LOGP(DRANAP, LOGL_ERROR, "Received Successful Outcome: unknown Procedure Code %d\n", |
| 481 | message->procedureCode); |
| 482 | rc = -1; |
| 483 | break; |
| 484 | } |
| 485 | break; |
| 486 | case RANAP_RANAP_PDU_PR_outcome: |
| 487 | switch (message->procedureCode) { |
| 488 | case RANAP_ProcedureCode_id_RAB_Assignment: |
| 489 | /* RAB Assignment Response */ |
| 490 | rc = ranap_handle_co_rab_ass_resp(ctx, &message->msg.raB_AssignmentResponseIEs); |
| 491 | break; |
| 492 | default: |
| 493 | LOGP(DRANAP, LOGL_ERROR, "Received Outcome: unknown Procedure Code %d\n", |
| 494 | message->procedureCode); |
| 495 | rc = -1; |
| 496 | break; |
| 497 | } |
| 498 | break; |
| 499 | case RANAP_RANAP_PDU_PR_unsuccessfulOutcome: |
| 500 | default: |
| 501 | LOGP(DRANAP, LOGL_ERROR, "Received Unsuccessful Outcome: Procedure Code %d\n", |
| 502 | message->procedureCode); |
| 503 | rc = -1; |
| 504 | break; |
| 505 | } |
| 506 | |
| 507 | if (rc) { |
| 508 | LOGP(DRANAP, LOGL_ERROR, "Error in cn_ranap_handle_co (%d)\n", |
| 509 | rc); |
| 510 | /* TODO handling of the error? */ |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies) |
| 515 | { |
| 516 | /* FIXME: send reset response */ |
| 517 | return -1; |
| 518 | } |
| 519 | |
| 520 | static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies) |
| 521 | { |
| 522 | if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT) |
| 523 | LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n", |
| 524 | ranap_cause_str(&ies->cause)); |
| 525 | else |
| 526 | LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n"); |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | /* Entry point for connection-less RANAP message */ |
| 532 | static void cn_ranap_handle_cl(void *ctx, ranap_message *message) |
| 533 | { |
| 534 | int rc; |
| 535 | |
| 536 | switch (message->direction) { |
| 537 | case RANAP_RANAP_PDU_PR_initiatingMessage: |
| 538 | switch (message->procedureCode) { |
| 539 | case RANAP_ProcedureCode_id_Reset: |
| 540 | /* received reset.req, send reset.resp */ |
| 541 | rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs); |
| 542 | break; |
| 543 | case RANAP_ProcedureCode_id_ErrorIndication: |
| 544 | rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs); |
| 545 | break; |
| 546 | default: |
| 547 | rc = -1; |
| 548 | break; |
| 549 | } |
| 550 | break; |
| 551 | case RANAP_RANAP_PDU_PR_successfulOutcome: |
| 552 | case RANAP_RANAP_PDU_PR_unsuccessfulOutcome: |
| 553 | case RANAP_RANAP_PDU_PR_outcome: |
| 554 | default: |
| 555 | rc = -1; |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | if (rc) { |
| 560 | LOGP(DRANAP, LOGL_ERROR, "Error in cn_ranap_handle_cl (%d)\n", |
| 561 | rc); |
| 562 | /* TODO handling of the error? */ |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /*********************************************************************** |
| 567 | * Paging |
| 568 | ***********************************************************************/ |
| 569 | |
| 570 | /* Send a paging command down a given SUA link. tmsi and paging_cause are |
| 571 | * optional and may be passed NULL and 0, respectively, to disable their use. |
| 572 | * See enum RANAP_PagingCause. |
| 573 | * |
| 574 | * If TMSI is given, the IMSI is not sent over the air interface. Nevertheless, |
| 575 | * the IMSI is still required for resolution in the HNB-GW and/or(?) RNC. */ |
| 576 | static int iu_tx_paging_cmd(struct osmo_sccp_link *link, |
| 577 | const char *imsi, const uint32_t *tmsi, |
| 578 | bool is_ps, uint32_t paging_cause) |
| 579 | { |
| 580 | struct msgb *msg; |
| 581 | msg = ranap_new_msg_paging_cmd(imsi, tmsi, is_ps? 1 : 0, paging_cause); |
| 582 | msg->l2h = msg->data; |
| 583 | return osmo_sccp_tx_unitdata_ranap(link, 1, 2, msg->data, |
| 584 | msgb_length(msg)); |
| 585 | } |
| 586 | |
| 587 | static int iu_page(const char *imsi, const uint32_t *tmsi_or_ptimsi, |
| 588 | uint16_t lac, uint8_t rac, bool is_ps) |
| 589 | { |
| 590 | struct iu_rnc *rnc; |
| 591 | int pagings_sent = 0; |
| 592 | |
| 593 | if (tmsi_or_ptimsi) { |
| 594 | LOGP(DRANAP, LOGL_DEBUG, "%s: Looking for RNCs to page for IMSI %s" |
| 595 | " (paging will use %s %x)\n", |
| 596 | is_ps? "IuPS" : "IuCS", |
| 597 | imsi, |
| 598 | is_ps? "PTMSI" : "TMSI", |
| 599 | *tmsi_or_ptimsi); |
| 600 | } else { |
| 601 | LOGP(DRANAP, LOGL_DEBUG, "%s: Looking for RNCs to page for IMSI %s" |
| 602 | " (paging will use IMSI)\n", |
| 603 | is_ps? "IuPS" : "IuCS", |
| 604 | imsi |
| 605 | ); |
| 606 | } |
| 607 | |
| 608 | llist_for_each_entry(rnc, &rnc_list, entry) { |
| 609 | if (!rnc->link) { |
| 610 | /* Not actually connected, don't count it. */ |
| 611 | continue; |
| 612 | } |
| 613 | if (rnc->lac != lac) |
| 614 | continue; |
| 615 | if (is_ps && rnc->rac != rac) |
| 616 | continue; |
| 617 | |
| 618 | /* Found a match! */ |
| 619 | if (iu_tx_paging_cmd(rnc->link, imsi, tmsi_or_ptimsi, is_ps, 0) |
| 620 | == 0) { |
| 621 | LOGP(DRANAP, LOGL_DEBUG, |
| 622 | "%s: Paged for IMSI %s on RNC %d, on SUA link %p\n", |
| 623 | is_ps? "IuPS" : "IuCS", |
| 624 | imsi, rnc->rnc_id, rnc->link); |
| 625 | pagings_sent ++; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /* Some logging... */ |
| 630 | if (pagings_sent > 0) { |
| 631 | LOGP(DRANAP, LOGL_DEBUG, |
| 632 | "%s: %d RNCs were paged for IMSI %s.\n", |
| 633 | is_ps? "IuPS" : "IuCS", |
| 634 | pagings_sent, imsi); |
| 635 | } |
| 636 | else { |
| 637 | if (is_ps) { |
| 638 | LOGP(DRANAP, LOGL_ERROR, "IuPS: Found no RNC to page for" |
| 639 | " LAC %d RAC %d (would have paged IMSI %s)\n", |
| 640 | lac, rac, imsi); |
| 641 | } |
| 642 | else { |
| 643 | LOGP(DRANAP, LOGL_ERROR, "IuCS: Found no RNC to page for" |
| 644 | " LAC %d (would have paged IMSI %s)\n", |
| 645 | lac, imsi); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | return pagings_sent; |
| 650 | } |
| 651 | |
| 652 | int iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac) |
| 653 | { |
| 654 | return iu_page(imsi, tmsi, lac, 0, false); |
| 655 | } |
| 656 | |
| 657 | int iu_page_ps(const char *imsi, const uint32_t *ptmsi, uint16_t lac, uint8_t rac) |
| 658 | { |
| 659 | return iu_page(imsi, ptmsi, lac, rac, true); |
| 660 | } |
| 661 | |
| 662 | |
| 663 | /*********************************************************************** |
| 664 | * |
| 665 | ***********************************************************************/ |
| 666 | |
| 667 | int tx_unitdata(struct osmo_sccp_link *link); |
| 668 | int tx_conn_req(struct osmo_sccp_link *link, uint32_t conn_id); |
| 669 | |
| 670 | struct osmo_prim_hdr *make_conn_req(uint32_t conn_id); |
| 671 | struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len); |
| 672 | |
| 673 | struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param) |
| 674 | { |
| 675 | struct msgb *msg = msgb_alloc(1024, "conn_resp"); |
| 676 | struct osmo_scu_prim *prim; |
| 677 | |
| 678 | prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); |
| 679 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 680 | OSMO_SCU_PRIM_N_CONNECT, |
| 681 | PRIM_OP_RESPONSE, msg); |
| 682 | memcpy(&prim->u.connect, param, sizeof(prim->u.connect)); |
| 683 | return &prim->oph; |
| 684 | } |
| 685 | |
| 686 | static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link) |
| 687 | { |
| 688 | struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph; |
| 689 | struct osmo_prim_hdr *resp = NULL; |
| 690 | int rc; |
| 691 | struct ue_conn_ctx *ue; |
| 692 | |
| 693 | DEBUGP(DRANAP, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph)); |
| 694 | |
| 695 | switch (OSMO_PRIM_HDR(oph)) { |
| 696 | case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM): |
| 697 | /* confirmation of outbound connection */ |
| 698 | rc = -1; |
| 699 | break; |
| 700 | case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION): |
| 701 | /* indication of new inbound connection request*/ |
| 702 | DEBUGP(DRANAP, "N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id); |
| 703 | if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */ |
| 704 | !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) { |
| 705 | LOGP(DRANAP, LOGL_NOTICE, |
| 706 | "Received invalid N-CONNECT.ind\n"); |
| 707 | return 0; |
| 708 | } |
| 709 | ue = ue_conn_ctx_alloc(link, prim->u.connect.conn_id); |
| 710 | /* first ensure the local SUA/SCCP socket is ACTIVE */ |
| 711 | resp = make_conn_resp(&prim->u.connect); |
| 712 | osmo_sua_user_link_down(link, resp); |
| 713 | /* then handle the RANAP payload */ |
| 714 | rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg)); |
| 715 | break; |
| 716 | case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION): |
| 717 | /* indication of disconnect */ |
| 718 | DEBUGP(DRANAP, "N-DISCONNECT.ind(%u)\n", |
| 719 | prim->u.disconnect.conn_id); |
| 720 | ue = ue_conn_ctx_find(link, prim->u.disconnect.conn_id); |
| 721 | rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg)); |
| 722 | break; |
| 723 | case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION): |
| 724 | /* connection-oriented data received */ |
| 725 | DEBUGP(DRANAP, "N-DATA.ind(%u, %s)\n", prim->u.data.conn_id, |
| 726 | osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); |
| 727 | /* resolve UE context */ |
| 728 | ue = ue_conn_ctx_find(link, prim->u.data.conn_id); |
| 729 | rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg)); |
| 730 | break; |
| 731 | case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION): |
| 732 | /* connection-less data received */ |
| 733 | DEBUGP(DRANAP, "N-UNITDATA.ind(%s)\n", |
| 734 | osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); |
| 735 | rc = ranap_cn_rx_cl(cn_ranap_handle_cl, link, msgb_l2(oph->msg), msgb_l2len(oph->msg)); |
| 736 | break; |
| 737 | default: |
| 738 | rc = -1; |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | msgb_free(oph->msg); |
| 743 | return rc; |
| 744 | } |
| 745 | |
| 746 | int iu_init(void *ctx, const char *listen_addr, uint16_t listen_port, |
| 747 | iu_recv_cb_t iu_recv_cb, iu_event_cb_t iu_event_cb) |
| 748 | { |
| 749 | struct osmo_sccp_user *user; |
| 750 | talloc_iu_ctx = talloc_named_const(ctx, 1, "iu"); |
| 751 | talloc_asn1_ctx = talloc_named_const(talloc_iu_ctx, 1, "asn1"); |
| 752 | |
| 753 | global_iu_recv_cb = iu_recv_cb; |
| 754 | global_iu_event_cb = iu_event_cb; |
| 755 | osmo_sua_set_log_area(DSUA); |
| 756 | user = osmo_sua_user_create(talloc_iu_ctx, sccp_sap_up, talloc_iu_ctx); |
| 757 | return osmo_sua_server_listen(user, listen_addr, listen_port); |
| 758 | } |
| 759 | |