Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 1 | /* Common parts of IuCS and IuPS interfaces implementation */ |
| 2 | |
| 3 | /* (C) 2016-2017 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 | |
Harald Welte | da86fe5 | 2017-11-21 08:14:37 +0100 | [diff] [blame] | 28 | #include <asn1c/asn1helpers.h> |
| 29 | |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 30 | #include <osmocom/ranap/iu_client.h> |
| 31 | |
| 32 | #include <osmocom/core/logging.h> |
| 33 | #include <osmocom/crypt/auth.h> |
| 34 | #include <osmocom/gprs/gprs_msgb.h> |
| 35 | #include <osmocom/sigtran/sccp_sap.h> |
Harald Welte | da86fe5 | 2017-11-21 08:14:37 +0100 | [diff] [blame] | 36 | #include <osmocom/sigtran/sccp_helpers.h> |
| 37 | #include <osmocom/ranap/ranap_common_cn.h> |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 38 | #include <osmocom/ranap/ranap_ies_defs.h> |
| 39 | #include <osmocom/ranap/ranap_msg_factory.h> |
| 40 | |
| 41 | /* Parsed global RNC id. See also struct RANAP_GlobalRNC_ID, and note that the |
| 42 | * PLMN identity is a BCD representation of the MCC and MNC. |
| 43 | * See iu_grnc_id_parse(). */ |
| 44 | struct iu_grnc_id { |
Neels Hofmeyr | cf5fbea | 2018-03-21 15:50:41 +0100 | [diff] [blame] | 45 | struct osmo_plmn_id plmn; |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 46 | uint16_t rnc_id; |
| 47 | }; |
| 48 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 49 | struct iu_lac_rac_entry { |
| 50 | struct llist_head entry; |
| 51 | |
| 52 | /* LAC: Location Area Code (for CS and PS) */ |
| 53 | uint16_t lac; |
| 54 | /* RAC: Routing Area Code (for PS only) */ |
| 55 | uint8_t rac; |
| 56 | }; |
| 57 | |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 58 | /* A remote RNC (Radio Network Controller, like BSC but for UMTS) that has |
| 59 | * called us and is currently reachable at the given osmo_sccp_addr. So, when we |
| 60 | * know a LAC for a subscriber, we can page it at the RNC matching that LAC or |
| 61 | * RAC. An HNB-GW typically presents itself as if it were a single RNC, even |
| 62 | * though it may have several RNCs in hNodeBs connected to it. Those will then |
| 63 | * share the same RNC id, which they actually receive and adopt from the HNB-GW |
| 64 | * in the HNBAP HNB REGISTER ACCEPT message. */ |
| 65 | struct ranap_iu_rnc { |
| 66 | struct llist_head entry; |
| 67 | |
| 68 | uint16_t rnc_id; |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 69 | struct osmo_sccp_addr sccp_addr; |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 70 | |
| 71 | /* A list of struct iu_lac_rac_entry */ |
| 72 | struct llist_head lac_rac_list; |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | void *talloc_iu_ctx; |
| 76 | void *talloc_asn1_ctx; |
| 77 | |
| 78 | /* Implement the extern asn_debug from libasn1c to indicate whether to print |
| 79 | * asn.1 debug messages (see libasn1c). */ |
| 80 | int asn_debug = 0; |
| 81 | |
| 82 | /* Implement the extern asn1_xer_print to indicate whether the ASN.1 binary |
| 83 | * code decoded and encoded during Iu communication should be logged to stderr |
| 84 | * (see asn.1 generated code in osmo-iuh). */ |
| 85 | int asn1_xer_print = 0; |
| 86 | |
| 87 | ranap_iu_recv_cb_t global_iu_recv_cb = NULL; |
| 88 | ranap_iu_event_cb_t global_iu_event_cb = NULL; |
| 89 | int iu_log_subsystem = 0; |
| 90 | |
| 91 | #define LOGPIU(level, fmt, args...) \ |
| 92 | LOGP(iu_log_subsystem, level, fmt, ## args) |
| 93 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 94 | #define LOGPIUC(level, fmt, args...) \ |
| 95 | LOGPC(iu_log_subsystem, level, fmt, ## args) |
| 96 | |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 97 | static LLIST_HEAD(ue_conn_ctx_list); |
| 98 | static LLIST_HEAD(rnc_list); |
| 99 | |
| 100 | static struct osmo_sccp_instance *g_sccp; |
| 101 | static struct osmo_sccp_user *g_scu; |
Neels Hofmeyr | 1aef9a6 | 2017-08-12 18:02:44 +0200 | [diff] [blame] | 102 | static struct osmo_sccp_addr g_local_sccp_addr; |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 103 | |
| 104 | const struct value_string ranap_iu_event_type_names[] = { |
| 105 | OSMO_VALUE_STRING(RANAP_IU_EVENT_RAB_ASSIGN), |
| 106 | OSMO_VALUE_STRING(RANAP_IU_EVENT_SECURITY_MODE_COMPLETE), |
| 107 | OSMO_VALUE_STRING(RANAP_IU_EVENT_IU_RELEASE), |
| 108 | OSMO_VALUE_STRING(RANAP_IU_EVENT_LINK_INVALIDATED), |
| 109 | { 0, NULL } |
| 110 | }; |
| 111 | |
Alexander Couzens | 790212f | 2019-09-10 18:23:37 +0200 | [diff] [blame] | 112 | static int global_iu_event(struct ranap_ue_conn_ctx *ue_ctx, |
| 113 | enum ranap_iu_event_type type, |
| 114 | void *data) |
| 115 | { |
| 116 | if (!global_iu_event_cb) |
| 117 | return 0; |
| 118 | |
Alexander Couzens | 8e2a0ee | 2019-09-10 18:28:49 +0200 | [diff] [blame] | 119 | if (!ue_ctx->notification) |
| 120 | return 0; |
| 121 | |
Alexander Couzens | 790212f | 2019-09-10 18:23:37 +0200 | [diff] [blame] | 122 | return global_iu_event_cb(ue_ctx, type, data); |
| 123 | } |
| 124 | |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 125 | static struct ranap_ue_conn_ctx *ue_conn_ctx_alloc(struct ranap_iu_rnc *rnc, uint32_t conn_id) |
| 126 | { |
| 127 | struct ranap_ue_conn_ctx *ctx = talloc_zero(talloc_iu_ctx, struct ranap_ue_conn_ctx); |
| 128 | |
| 129 | ctx->rnc = rnc; |
| 130 | ctx->conn_id = conn_id; |
Alexander Couzens | 8e2a0ee | 2019-09-10 18:28:49 +0200 | [diff] [blame] | 131 | ctx->notification = true; |
Alexander Couzens | 32fc82a | 2019-09-10 19:09:30 +0200 | [diff] [blame] | 132 | ctx->free_on_release = false; |
Alexander Couzens | ff79699 | 2019-09-10 17:46:54 +0200 | [diff] [blame] | 133 | osmo_timer_setup(&ctx->release_timeout, |
| 134 | (void *)(void *) ranap_iu_free_ue, |
| 135 | ctx); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 136 | llist_add(&ctx->list, &ue_conn_ctx_list); |
| 137 | |
| 138 | return ctx; |
| 139 | } |
| 140 | |
| 141 | static struct ranap_ue_conn_ctx *ue_conn_ctx_find(uint32_t conn_id) |
| 142 | { |
| 143 | struct ranap_ue_conn_ctx *ctx; |
| 144 | |
| 145 | llist_for_each_entry(ctx, &ue_conn_ctx_list, list) { |
| 146 | if (ctx->conn_id == conn_id) |
| 147 | return ctx; |
| 148 | } |
| 149 | return NULL; |
| 150 | } |
| 151 | |
Alexander Couzens | 609994d | 2019-08-12 16:48:56 +0200 | [diff] [blame] | 152 | void ranap_iu_free_ue(struct ranap_ue_conn_ctx *ue_ctx) |
| 153 | { |
| 154 | if (!ue_ctx) |
| 155 | return; |
| 156 | |
Alexander Couzens | ff79699 | 2019-09-10 17:46:54 +0200 | [diff] [blame] | 157 | osmo_timer_del(&ue_ctx->release_timeout); |
Alexander Couzens | 609994d | 2019-08-12 16:48:56 +0200 | [diff] [blame] | 158 | osmo_sccp_tx_disconn(g_scu, ue_ctx->conn_id, NULL, 0); |
| 159 | llist_del(&ue_ctx->list); |
| 160 | talloc_free(ue_ctx); |
| 161 | } |
| 162 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 163 | static struct ranap_iu_rnc *iu_rnc_alloc(uint16_t rnc_id, struct osmo_sccp_addr *addr) |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 164 | { |
| 165 | struct ranap_iu_rnc *rnc = talloc_zero(talloc_iu_ctx, struct ranap_iu_rnc); |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 166 | OSMO_ASSERT(rnc); |
| 167 | |
| 168 | INIT_LLIST_HEAD(&rnc->lac_rac_list); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 169 | |
| 170 | rnc->rnc_id = rnc_id; |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 171 | rnc->sccp_addr = *addr; |
| 172 | llist_add(&rnc->entry, &rnc_list); |
| 173 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 174 | LOGPIU(LOGL_NOTICE, "New RNC %d at %s\n", rnc->rnc_id, osmo_sccp_addr_dump(addr)); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 175 | |
| 176 | return rnc; |
| 177 | } |
| 178 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 179 | /* Find a match for the given LAC (and RAC). For CS, pass rac as 0. |
| 180 | * If rnc and lre pointers are not NULL, *rnc / *lre are set to NULL if no match is found, or to the |
| 181 | * match if a match is found. Return true if a match is found. */ |
| 182 | static bool iu_rnc_lac_rac_find(struct ranap_iu_rnc **rnc, struct iu_lac_rac_entry **lre, |
| 183 | uint16_t lac, uint8_t rac) |
| 184 | { |
| 185 | struct ranap_iu_rnc *r; |
| 186 | struct iu_lac_rac_entry *e; |
| 187 | |
| 188 | if (rnc) |
| 189 | *rnc = NULL; |
| 190 | if (lre) |
| 191 | *lre = NULL; |
| 192 | |
| 193 | llist_for_each_entry(r, &rnc_list, entry) { |
| 194 | llist_for_each_entry(e, &r->lac_rac_list, entry) { |
| 195 | if (e->lac == lac && e->rac == rac) { |
| 196 | if (rnc) |
| 197 | *rnc = r; |
| 198 | if (lre) |
| 199 | *lre = e; |
| 200 | return true; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | static struct ranap_iu_rnc *iu_rnc_id_find(uint16_t rnc_id) |
| 208 | { |
| 209 | struct ranap_iu_rnc *rnc; |
| 210 | llist_for_each_entry(rnc, &rnc_list, entry) { |
| 211 | if (rnc->rnc_id == rnc_id) |
| 212 | return rnc; |
| 213 | } |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | static bool same_sccp_addr(struct osmo_sccp_addr *a, struct osmo_sccp_addr *b) |
| 218 | { |
| 219 | char buf[256]; |
| 220 | osmo_strlcpy(buf, osmo_sccp_addr_dump(a), sizeof(buf)); |
| 221 | return !strcmp(buf, osmo_sccp_addr_dump(b)); |
| 222 | } |
| 223 | |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 224 | static struct ranap_iu_rnc *iu_rnc_register(uint16_t rnc_id, uint16_t lac, |
| 225 | uint8_t rac, struct osmo_sccp_addr *addr) |
| 226 | { |
| 227 | struct ranap_iu_rnc *rnc; |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 228 | struct ranap_iu_rnc *old_rnc; |
| 229 | struct iu_lac_rac_entry *lre; |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 230 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 231 | /* Make sure we know this rnc_id and that this SCCP address is in our records */ |
| 232 | rnc = iu_rnc_id_find(rnc_id); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 233 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 234 | if (rnc) { |
| 235 | if (!same_sccp_addr(&rnc->sccp_addr, addr)) { |
| 236 | LOGPIU(LOGL_NOTICE, "RNC %u changed its SCCP addr to %s (LAC=%u RAC=%u)\n", |
| 237 | rnc_id, osmo_sccp_addr_dump(addr), lac, rac); |
| 238 | rnc->sccp_addr = *addr; |
| 239 | } |
| 240 | } else |
| 241 | rnc = iu_rnc_alloc(rnc_id, addr); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 242 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 243 | /* Detect whether the LAC,RAC is already recorded in another RNC */ |
| 244 | iu_rnc_lac_rac_find(&old_rnc, &lre, lac, rac); |
| 245 | |
| 246 | if (old_rnc && old_rnc != rnc) { |
| 247 | /* LAC,RAC already exists in a different RNC */ |
| 248 | LOGPIU(LOGL_NOTICE, "LAC %u RAC %u moved from RNC %u %s", |
| 249 | lac, rac, old_rnc->rnc_id, osmo_sccp_addr_dump(&old_rnc->sccp_addr)); |
| 250 | LOGPIUC(LOGL_NOTICE, " to RNC %u %s\n", |
| 251 | rnc->rnc_id, osmo_sccp_addr_dump(&rnc->sccp_addr)); |
| 252 | |
| 253 | llist_del(&lre->entry); |
| 254 | llist_add(&lre->entry, &rnc->lac_rac_list); |
| 255 | } else if (!old_rnc) { |
| 256 | /* LAC,RAC not recorded yet */ |
| 257 | LOGPIU(LOGL_NOTICE, "RNC %u: new LAC %u RAC %u\n", rnc_id, lac, rac); |
| 258 | lre = talloc_zero(rnc, struct iu_lac_rac_entry); |
| 259 | lre->lac = lac; |
| 260 | lre->rac = rac; |
| 261 | llist_add(&lre->entry, &rnc->lac_rac_list); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 262 | } |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 263 | /* else, LAC,RAC already recorded with the current RNC. */ |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 264 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 265 | return rnc; |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /*********************************************************************** |
| 269 | * RANAP handling |
| 270 | ***********************************************************************/ |
| 271 | |
| 272 | int ranap_iu_rab_act(struct ranap_ue_conn_ctx *ue_ctx, struct msgb *msg) |
| 273 | { |
| 274 | struct osmo_scu_prim *prim; |
| 275 | |
| 276 | /* wrap RANAP message in SCCP N-DATA.req */ |
| 277 | prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); |
| 278 | prim->u.data.conn_id = ue_ctx->conn_id; |
| 279 | osmo_prim_init(&prim->oph, |
| 280 | SCCP_SAP_USER, |
| 281 | OSMO_SCU_PRIM_N_DATA, |
| 282 | PRIM_OP_REQUEST, |
| 283 | msg); |
| 284 | return osmo_sccp_user_sap_down(g_scu, &prim->oph); |
| 285 | } |
| 286 | |
| 287 | int ranap_iu_rab_deact(struct ranap_ue_conn_ctx *ue_ctx, uint8_t rab_id) |
| 288 | { |
| 289 | /* FIXME */ |
| 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | int ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec, |
| 294 | int send_ck, int new_key) |
| 295 | { |
| 296 | struct osmo_scu_prim *prim; |
| 297 | struct msgb *msg; |
| 298 | |
| 299 | /* create RANAP message */ |
| 300 | msg = ranap_new_msg_sec_mod_cmd(vec->ik, send_ck? vec->ck : NULL, |
| 301 | new_key ? RANAP_KeyStatus_new : RANAP_KeyStatus_old); |
| 302 | msg->l2h = msg->data; |
| 303 | /* wrap RANAP message in SCCP N-DATA.req */ |
| 304 | prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); |
| 305 | prim->u.data.conn_id = uectx->conn_id; |
| 306 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 307 | OSMO_SCU_PRIM_N_DATA, |
| 308 | PRIM_OP_REQUEST, msg); |
Alexander Couzens | a6eb09d | 2019-09-17 16:20:40 +0200 | [diff] [blame] | 309 | return osmo_sccp_user_sap_down(g_scu, &prim->oph); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | int ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *uectx, const char *imsi) |
| 313 | { |
| 314 | struct msgb *msg; |
| 315 | struct osmo_scu_prim *prim; |
| 316 | |
| 317 | LOGPIU(LOGL_INFO, "Transmitting RANAP CommonID (SCCP conn_id %u)\n", |
| 318 | uectx->conn_id); |
| 319 | |
| 320 | msg = ranap_new_msg_common_id(imsi); |
| 321 | msg->l2h = msg->data; |
| 322 | prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); |
| 323 | prim->u.data.conn_id = uectx->conn_id; |
| 324 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 325 | OSMO_SCU_PRIM_N_DATA, |
| 326 | PRIM_OP_REQUEST, msg); |
Alexander Couzens | a6eb09d | 2019-09-17 16:20:40 +0200 | [diff] [blame] | 327 | return osmo_sccp_user_sap_down(g_scu, &prim->oph); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static int iu_grnc_id_parse(struct iu_grnc_id *dst, struct RANAP_GlobalRNC_ID *src) |
| 331 | { |
| 332 | /* The size is coming from arbitrary sender, check it gracefully */ |
| 333 | if (src->pLMNidentity.size != 3) { |
| 334 | LOGPIU(LOGL_ERROR, "Invalid PLMN Identity size:" |
| 335 | " should be 3, is %d\n", src->pLMNidentity.size); |
| 336 | return -1; |
| 337 | } |
Neels Hofmeyr | cf5fbea | 2018-03-21 15:50:41 +0100 | [diff] [blame] | 338 | osmo_plmn_from_bcd(&src->pLMNidentity.buf[0], &dst->plmn); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 339 | dst->rnc_id = (uint16_t)src->rNC_ID; |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | #if 0 |
| 344 | -- not used at present -- |
| 345 | static int iu_grnc_id_compose(struct iu_grnc_id *src, struct RANAP_GlobalRNC_ID *dst) |
| 346 | { |
| 347 | /* The caller must ensure proper size */ |
| 348 | OSMO_ASSERT(dst->pLMNidentity.size == 3); |
| 349 | gsm48_mcc_mnc_to_bcd(&dst->pLMNidentity.buf[0], |
| 350 | src->mcc, src->mnc); |
| 351 | dst->rNC_ID = src->rnc_id; |
| 352 | return 0; |
| 353 | } |
| 354 | #endif |
| 355 | |
| 356 | struct new_ue_conn_ctx { |
| 357 | struct osmo_sccp_addr sccp_addr; |
| 358 | uint32_t conn_id; |
| 359 | }; |
| 360 | |
| 361 | static int ranap_handle_co_initial_ue(void *ctx, RANAP_InitialUE_MessageIEs_t *ies) |
| 362 | { |
| 363 | struct new_ue_conn_ctx *new_ctx = ctx; |
| 364 | struct gprs_ra_id ra_id; |
| 365 | struct iu_grnc_id grnc_id; |
| 366 | uint16_t sai; |
| 367 | struct ranap_ue_conn_ctx *ue; |
| 368 | struct msgb *msg = msgb_alloc(256, "RANAP->NAS"); |
| 369 | struct ranap_iu_rnc *rnc; |
| 370 | |
| 371 | if (ranap_parse_lai(&ra_id, &ies->lai) != 0) { |
| 372 | LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n"); |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | if (ies->presenceMask & INITIALUE_MESSAGEIES_RANAP_RAC_PRESENT) { |
| 377 | ra_id.rac = asn1str_to_u8(&ies->rac); |
| 378 | } |
| 379 | |
| 380 | if (iu_grnc_id_parse(&grnc_id, &ies->globalRNC_ID) != 0) { |
| 381 | LOGPIU(LOGL_ERROR, |
| 382 | "Failed to parse RANAP Global-RNC-ID IE\n"); |
| 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | sai = asn1str_to_u16(&ies->sai.sAC); |
| 387 | msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size); |
| 388 | memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size); |
| 389 | |
| 390 | /* Make sure we know the RNC Id and LAC+RAC coming in on this connection. */ |
| 391 | rnc = iu_rnc_register(grnc_id.rnc_id, ra_id.lac, ra_id.rac, &new_ctx->sccp_addr); |
| 392 | |
| 393 | ue = ue_conn_ctx_alloc(rnc, new_ctx->conn_id); |
| 394 | OSMO_ASSERT(ue); |
| 395 | ue->ra_id = ra_id; |
| 396 | |
| 397 | /* Feed into the MM layer */ |
| 398 | msg->dst = ue; |
| 399 | global_iu_recv_cb(msg, &ra_id, &sai); |
| 400 | |
| 401 | msgb_free(msg); |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies) |
| 407 | { |
| 408 | struct gprs_ra_id _ra_id, *ra_id = NULL; |
| 409 | uint16_t _sai, *sai = NULL; |
| 410 | struct msgb *msg = msgb_alloc(256, "RANAP->NAS"); |
| 411 | |
| 412 | if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) { |
| 413 | if (ranap_parse_lai(&_ra_id, &ies->lai) != 0) { |
| 414 | LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n"); |
| 415 | return -1; |
| 416 | } |
| 417 | ra_id = &_ra_id; |
| 418 | if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) { |
| 419 | _ra_id.rac = asn1str_to_u8(&ies->rac); |
| 420 | } |
| 421 | if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) { |
| 422 | _sai = asn1str_to_u16(&ies->sai.sAC); |
| 423 | sai = &_sai; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size); |
| 428 | memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size); |
| 429 | |
| 430 | /* Feed into the MM/CC/SMS-CP layer */ |
| 431 | msg->dst = ctx; |
| 432 | global_iu_recv_cb(msg, ra_id, sai); |
| 433 | |
| 434 | msgb_free(msg); |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies) |
| 440 | { |
| 441 | if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT) |
| 442 | LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n", |
| 443 | ranap_cause_str(&ies->cause)); |
| 444 | else |
| 445 | LOGPIU(LOGL_ERROR, "Rx Error Indication\n"); |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | int ranap_iu_tx(struct msgb *msg_nas, uint8_t sapi) |
| 451 | { |
| 452 | struct ranap_ue_conn_ctx *uectx = msg_nas->dst; |
| 453 | struct msgb *msg; |
| 454 | struct osmo_scu_prim *prim; |
| 455 | |
| 456 | LOGPIU(LOGL_INFO, "Transmitting L3 Message as RANAP DT (SCCP conn_id %u)\n", |
| 457 | uectx->conn_id); |
| 458 | |
| 459 | msg = ranap_new_msg_dt(sapi, msg_nas->data, msgb_length(msg_nas)); |
| 460 | msgb_free(msg_nas); |
| 461 | msg->l2h = msg->data; |
| 462 | prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); |
| 463 | prim->u.data.conn_id = uectx->conn_id; |
| 464 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 465 | OSMO_SCU_PRIM_N_DATA, |
| 466 | PRIM_OP_REQUEST, msg); |
Alexander Couzens | a6eb09d | 2019-09-17 16:20:40 +0200 | [diff] [blame] | 467 | return osmo_sccp_user_sap_down(g_scu, &prim->oph); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* Send Iu Release for the given UE connection. |
Alexander Couzens | 9fb8cac | 2019-10-03 20:45:52 +0200 | [diff] [blame^] | 471 | * If cause is NULL, Normal Release cause is sent, otherwise |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 472 | * the provided cause. */ |
| 473 | int ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause) |
| 474 | { |
| 475 | struct msgb *msg; |
| 476 | struct osmo_scu_prim *prim; |
| 477 | static const struct RANAP_Cause default_cause = { |
Alexander Couzens | 9fb8cac | 2019-10-03 20:45:52 +0200 | [diff] [blame^] | 478 | .present = RANAP_Cause_PR_nAS, |
| 479 | .choice.radioNetwork = RANAP_CauseNAS_normal_release, |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 480 | }; |
| 481 | |
| 482 | if (!cause) |
| 483 | cause = &default_cause; |
| 484 | |
| 485 | msg = ranap_new_msg_iu_rel_cmd(cause); |
| 486 | msg->l2h = msg->data; |
| 487 | prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); |
| 488 | prim->u.data.conn_id = ctx->conn_id; |
| 489 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 490 | OSMO_SCU_PRIM_N_DATA, |
| 491 | PRIM_OP_REQUEST, msg); |
| 492 | return osmo_sccp_user_sap_down(g_scu, &prim->oph); |
| 493 | } |
| 494 | |
Alexander Couzens | ff79699 | 2019-09-10 17:46:54 +0200 | [diff] [blame] | 495 | void ranap_iu_tx_release_free(struct ranap_ue_conn_ctx *ctx, |
| 496 | const struct RANAP_Cause *cause, |
| 497 | int timeout) |
| 498 | { |
| 499 | ctx->notification = false; |
Alexander Couzens | 32fc82a | 2019-09-10 19:09:30 +0200 | [diff] [blame] | 500 | ctx->free_on_release = true; |
Alexander Couzens | ff79699 | 2019-09-10 17:46:54 +0200 | [diff] [blame] | 501 | int ret = ranap_iu_tx_release(ctx, cause); |
| 502 | if (ret) { |
| 503 | ranap_iu_free_ue(ctx); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | osmo_timer_schedule(&ctx->release_timeout, timeout, 0); |
| 508 | } |
| 509 | |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 510 | static int ranap_handle_co_iu_rel_req(struct ranap_ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies) |
| 511 | { |
| 512 | LOGPIU(LOGL_INFO, "Received Iu Release Request, Sending Release Command\n"); |
| 513 | ranap_iu_tx_release(ctx, &ies->cause); |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int ranap_handle_co_rab_ass_resp(struct ranap_ue_conn_ctx *ctx, RANAP_RAB_AssignmentResponseIEs_t *ies) |
| 518 | { |
| 519 | int rc = -1; |
| 520 | |
| 521 | LOGPIU(LOGL_INFO, |
| 522 | "Rx RAB Assignment Response for UE conn_id %u\n", ctx->conn_id); |
| 523 | if (ies->presenceMask & RAB_ASSIGNMENTRESPONSEIES_RANAP_RAB_SETUPORMODIFIEDLIST_PRESENT) { |
| 524 | /* TODO: Iterate over list of SetupOrModifiedList IEs and handle each one */ |
| 525 | RANAP_IE_t *ranap_ie = ies->raB_SetupOrModifiedList.raB_SetupOrModifiedList_ies.list.array[0]; |
| 526 | RANAP_RAB_SetupOrModifiedItemIEs_t setup_ies; |
| 527 | |
| 528 | rc = ranap_decode_rab_setupormodifieditemies_fromlist(&setup_ies, &ranap_ie->value); |
| 529 | if (rc) { |
| 530 | LOGPIU(LOGL_ERROR, "Error in ranap_decode_rab_setupormodifieditemies()\n"); |
| 531 | return rc; |
| 532 | } |
| 533 | |
Alexander Couzens | 790212f | 2019-09-10 18:23:37 +0200 | [diff] [blame] | 534 | rc = global_iu_event(ctx, RANAP_IU_EVENT_RAB_ASSIGN, &setup_ies); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 535 | |
| 536 | ranap_free_rab_setupormodifieditemies(&setup_ies); |
| 537 | } |
| 538 | /* FIXME: handle RAB Ass failure? */ |
| 539 | |
| 540 | return rc; |
| 541 | } |
| 542 | |
| 543 | static void cn_ranap_handle_co_initial(void *ctx, ranap_message *message) |
| 544 | { |
| 545 | int rc; |
| 546 | |
| 547 | LOGPIU(LOGL_NOTICE, "handle_co_initial(dir=%u, proc=%u)\n", message->direction, message->procedureCode); |
| 548 | |
| 549 | if (message->direction != RANAP_RANAP_PDU_PR_initiatingMessage |
| 550 | || message->procedureCode != RANAP_ProcedureCode_id_InitialUE_Message) { |
| 551 | LOGPIU(LOGL_ERROR, "Expected direction 'InitiatingMessage'," |
| 552 | " procedureCode 'InitialUE_Message', instead got %u and %u\n", |
| 553 | message->direction, message->procedureCode); |
| 554 | rc = -1; |
| 555 | } |
| 556 | else |
| 557 | rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs); |
| 558 | |
| 559 | if (rc) { |
| 560 | LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc); |
| 561 | /* TODO handling of the error? */ |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /* Entry point for connection-oriented RANAP message */ |
| 566 | static void cn_ranap_handle_co(void *ctx, ranap_message *message) |
| 567 | { |
| 568 | int rc; |
| 569 | |
| 570 | LOGPIU(LOGL_NOTICE, "handle_co(dir=%u, proc=%u)\n", message->direction, message->procedureCode); |
| 571 | |
| 572 | switch (message->direction) { |
| 573 | case RANAP_RANAP_PDU_PR_initiatingMessage: |
| 574 | switch (message->procedureCode) { |
| 575 | case RANAP_ProcedureCode_id_InitialUE_Message: |
| 576 | LOGPIU(LOGL_ERROR, "Got InitialUE_Message but this is not a new conn\n"); |
| 577 | rc = -1; |
| 578 | break; |
| 579 | case RANAP_ProcedureCode_id_DirectTransfer: |
| 580 | rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs); |
| 581 | break; |
| 582 | case RANAP_ProcedureCode_id_ErrorIndication: |
| 583 | rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs); |
| 584 | break; |
| 585 | case RANAP_ProcedureCode_id_Iu_ReleaseRequest: |
| 586 | /* Iu Release Request */ |
| 587 | rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs); |
| 588 | break; |
| 589 | default: |
| 590 | LOGPIU(LOGL_ERROR, "Received Initiating Message: unknown Procedure Code %d\n", |
| 591 | message->procedureCode); |
| 592 | rc = -1; |
| 593 | break; |
| 594 | } |
| 595 | break; |
| 596 | case RANAP_RANAP_PDU_PR_successfulOutcome: |
| 597 | switch (message->procedureCode) { |
| 598 | case RANAP_ProcedureCode_id_SecurityModeControl: |
| 599 | /* Security Mode Complete */ |
Alexander Couzens | 790212f | 2019-09-10 18:23:37 +0200 | [diff] [blame] | 600 | rc = global_iu_event(ctx, RANAP_IU_EVENT_SECURITY_MODE_COMPLETE, NULL); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 601 | break; |
| 602 | case RANAP_ProcedureCode_id_Iu_Release: |
| 603 | /* Iu Release Complete */ |
Alexander Couzens | 790212f | 2019-09-10 18:23:37 +0200 | [diff] [blame] | 604 | rc = global_iu_event(ctx, RANAP_IU_EVENT_IU_RELEASE, NULL); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 605 | if (rc) { |
| 606 | LOGPIU(LOGL_ERROR, "Iu Release event: Iu Event callback returned %d\n", |
| 607 | rc); |
| 608 | } |
| 609 | break; |
| 610 | default: |
| 611 | LOGPIU(LOGL_ERROR, "Received Successful Outcome: unknown Procedure Code %d\n", |
| 612 | message->procedureCode); |
| 613 | rc = -1; |
| 614 | break; |
| 615 | } |
| 616 | break; |
| 617 | case RANAP_RANAP_PDU_PR_outcome: |
| 618 | switch (message->procedureCode) { |
| 619 | case RANAP_ProcedureCode_id_RAB_Assignment: |
| 620 | /* RAB Assignment Response */ |
| 621 | rc = ranap_handle_co_rab_ass_resp(ctx, &message->msg.raB_AssignmentResponseIEs); |
| 622 | break; |
| 623 | default: |
| 624 | LOGPIU(LOGL_ERROR, "Received Outcome: unknown Procedure Code %d\n", |
| 625 | message->procedureCode); |
| 626 | rc = -1; |
| 627 | break; |
| 628 | } |
| 629 | break; |
| 630 | case RANAP_RANAP_PDU_PR_unsuccessfulOutcome: |
| 631 | default: |
| 632 | LOGPIU(LOGL_ERROR, "Received Unsuccessful Outcome: Procedure Code %d\n", |
| 633 | message->procedureCode); |
| 634 | rc = -1; |
| 635 | break; |
| 636 | } |
| 637 | |
| 638 | if (rc) { |
| 639 | LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc); |
| 640 | /* TODO handling of the error? */ |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies) |
| 645 | { |
Harald Welte | 8fa35b8 | 2019-04-20 20:13:31 +0200 | [diff] [blame] | 646 | struct osmo_scu_prim *prim = (struct osmo_scu_prim *) ctx; |
| 647 | struct osmo_scu_unitdata_param *ud_prim = &prim->u.unitdata; |
| 648 | RANAP_GlobalRNC_ID_t *grnc_id = NULL; |
| 649 | struct msgb *resp; |
| 650 | |
| 651 | OSMO_ASSERT(prim->oph.primitive == OSMO_SCU_PRIM_N_UNITDATA); |
| 652 | |
| 653 | /* FIXME: verify ies.cN_DomainIndicator */ |
| 654 | |
| 655 | if (ies->presenceMask & RESETIES_RANAP_GLOBALRNC_ID_PRESENT) |
| 656 | grnc_id = &ies->globalRNC_ID; |
| 657 | |
| 658 | /* send reset response */ |
| 659 | resp = ranap_new_msg_reset_ack(ies->cN_DomainIndicator, grnc_id); |
| 660 | if (!resp) |
| 661 | return -ENOMEM; |
| 662 | resp->l2h = resp->data; |
| 663 | return osmo_sccp_tx_unitdata_msg(g_scu, &g_local_sccp_addr, &ud_prim->calling_addr, resp); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies) |
| 667 | { |
| 668 | if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT) |
| 669 | LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n", |
| 670 | ranap_cause_str(&ies->cause)); |
| 671 | else |
| 672 | LOGPIU(LOGL_ERROR, "Rx Error Indication\n"); |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /* Entry point for connection-less RANAP message */ |
| 678 | static void cn_ranap_handle_cl(void *ctx, ranap_message *message) |
| 679 | { |
| 680 | int rc; |
| 681 | |
| 682 | switch (message->direction) { |
| 683 | case RANAP_RANAP_PDU_PR_initiatingMessage: |
| 684 | switch (message->procedureCode) { |
| 685 | case RANAP_ProcedureCode_id_Reset: |
| 686 | /* received reset.req, send reset.resp */ |
| 687 | rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs); |
| 688 | break; |
| 689 | case RANAP_ProcedureCode_id_ErrorIndication: |
| 690 | rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs); |
| 691 | break; |
| 692 | default: |
| 693 | rc = -1; |
| 694 | break; |
| 695 | } |
| 696 | break; |
| 697 | case RANAP_RANAP_PDU_PR_successfulOutcome: |
| 698 | case RANAP_RANAP_PDU_PR_unsuccessfulOutcome: |
| 699 | case RANAP_RANAP_PDU_PR_outcome: |
| 700 | default: |
| 701 | rc = -1; |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | if (rc) { |
| 706 | LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc); |
| 707 | /* TODO handling of the error? */ |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | /*********************************************************************** |
| 712 | * Paging |
| 713 | ***********************************************************************/ |
| 714 | |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 715 | /* Send a paging command down a given SCCP User. tmsi and paging_cause are |
| 716 | * optional and may be passed NULL and 0, respectively, to disable their use. |
| 717 | * See enum RANAP_PagingCause. |
| 718 | * |
| 719 | * If TMSI is given, the IMSI is not sent over the air interface. Nevertheless, |
| 720 | * the IMSI is still required for resolution in the HNB-GW and/or(?) RNC. */ |
| 721 | static int iu_tx_paging_cmd(struct osmo_sccp_addr *called_addr, |
| 722 | const char *imsi, const uint32_t *tmsi, |
| 723 | bool is_ps, uint32_t paging_cause) |
| 724 | { |
| 725 | struct msgb *msg; |
| 726 | msg = ranap_new_msg_paging_cmd(imsi, tmsi, is_ps? 1 : 0, paging_cause); |
| 727 | msg->l2h = msg->data; |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 728 | return osmo_sccp_tx_unitdata_msg(g_scu, &g_local_sccp_addr, called_addr, msg); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 729 | } |
| 730 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 731 | static int iu_page(const char *imsi, const uint32_t *tmsi_or_ptmsi, |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 732 | uint16_t lac, uint8_t rac, bool is_ps) |
| 733 | { |
| 734 | struct ranap_iu_rnc *rnc; |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 735 | const char *log_msg; |
| 736 | int log_level; |
| 737 | int paged = 0; |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 738 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 739 | iu_rnc_lac_rac_find(&rnc, NULL, lac, rac); |
| 740 | if (rnc) { |
| 741 | if (iu_tx_paging_cmd(&rnc->sccp_addr, imsi, tmsi_or_ptmsi, is_ps, 0) == 0) { |
| 742 | log_msg = "Paging"; |
| 743 | log_level = LOGL_DEBUG; |
| 744 | paged = 1; |
| 745 | } else { |
| 746 | log_msg = "Paging failed"; |
| 747 | log_level = LOGL_ERROR; |
| 748 | } |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 749 | } else { |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 750 | log_msg = "Found no RNC to Page"; |
| 751 | log_level = LOGL_ERROR; |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 752 | } |
| 753 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 754 | if (is_ps) |
| 755 | LOGPIU(log_level, "IuPS: %s on LAC %d RAC %d", log_msg, lac, rac); |
| 756 | else |
| 757 | LOGPIU(log_level, "IuCS: %s on LAC %d", log_msg, lac); |
| 758 | if (rnc) |
| 759 | LOGPIUC(log_level, " at SCCP-addr %s", osmo_sccp_addr_dump(&rnc->sccp_addr)); |
| 760 | if (tmsi_or_ptmsi) |
| 761 | LOGPIUC(log_level, ", for %s %08x\n", is_ps? "PTMSI" : "TMSI", *tmsi_or_ptmsi); |
| 762 | else |
| 763 | LOGPIUC(log_level, ", for IMSI %s\n", imsi); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 764 | |
Neels Hofmeyr | 69888c8 | 2017-12-15 02:54:45 +0100 | [diff] [blame] | 765 | return paged; |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | int ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac) |
| 769 | { |
| 770 | return iu_page(imsi, tmsi, lac, 0, false); |
| 771 | } |
| 772 | |
| 773 | int ranap_iu_page_ps(const char *imsi, const uint32_t *ptmsi, uint16_t lac, uint8_t rac) |
| 774 | { |
| 775 | return iu_page(imsi, ptmsi, lac, rac, true); |
| 776 | } |
| 777 | |
| 778 | |
| 779 | /*********************************************************************** |
| 780 | * |
| 781 | ***********************************************************************/ |
| 782 | |
| 783 | int tx_unitdata(struct osmo_sccp_user *scu); |
| 784 | int tx_conn_req(struct osmo_sccp_user *scu, uint32_t conn_id); |
| 785 | |
| 786 | struct osmo_prim_hdr *make_conn_req(uint32_t conn_id); |
| 787 | struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len); |
| 788 | |
| 789 | static struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param) |
| 790 | { |
| 791 | struct msgb *msg = msgb_alloc(1024, "conn_resp"); |
| 792 | struct osmo_scu_prim *prim; |
| 793 | |
| 794 | prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); |
| 795 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 796 | OSMO_SCU_PRIM_N_CONNECT, |
| 797 | PRIM_OP_RESPONSE, msg); |
| 798 | memcpy(&prim->u.connect, param, sizeof(prim->u.connect)); |
| 799 | return &prim->oph; |
| 800 | } |
| 801 | |
| 802 | static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu) |
| 803 | { |
| 804 | struct osmo_sccp_user *scu = _scu; |
| 805 | struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph; |
| 806 | struct osmo_prim_hdr *resp = NULL; |
Alexander Couzens | 8030792 | 2019-08-13 17:13:59 +0200 | [diff] [blame] | 807 | int rc = -1; |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 808 | struct ranap_ue_conn_ctx *ue; |
| 809 | struct new_ue_conn_ctx new_ctx = {}; |
| 810 | |
| 811 | LOGPIU(LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph)); |
| 812 | |
| 813 | switch (OSMO_PRIM_HDR(oph)) { |
| 814 | case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM): |
| 815 | /* confirmation of outbound connection */ |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 816 | break; |
| 817 | case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION): |
| 818 | /* indication of new inbound connection request*/ |
| 819 | LOGPIU(LOGL_DEBUG, "N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id); |
| 820 | if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */ |
| 821 | !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) { |
| 822 | LOGPIU(LOGL_NOTICE, |
| 823 | "Received invalid N-CONNECT.ind\n"); |
| 824 | return 0; |
| 825 | } |
| 826 | new_ctx.sccp_addr = prim->u.connect.calling_addr; |
| 827 | new_ctx.conn_id = prim->u.connect.conn_id; |
| 828 | /* first ensure the local SCCP socket is ACTIVE */ |
| 829 | resp = make_conn_resp(&prim->u.connect); |
| 830 | osmo_sccp_user_sap_down(scu, resp); |
| 831 | /* then handle the RANAP payload */ |
| 832 | rc = ranap_cn_rx_co(cn_ranap_handle_co_initial, &new_ctx, msgb_l2(oph->msg), msgb_l2len(oph->msg)); |
| 833 | break; |
| 834 | case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION): |
| 835 | /* indication of disconnect */ |
| 836 | LOGPIU(LOGL_DEBUG, "N-DISCONNECT.ind(%u)\n", |
| 837 | prim->u.disconnect.conn_id); |
| 838 | ue = ue_conn_ctx_find(prim->u.disconnect.conn_id); |
Alexander Couzens | 87482b3 | 2019-08-13 17:06:17 +0200 | [diff] [blame] | 839 | if (!ue) |
| 840 | break; |
| 841 | |
Alexander Couzens | 4ae261b | 2019-08-13 17:14:31 +0200 | [diff] [blame] | 842 | rc = 0; |
| 843 | if (msgb_l2len(oph->msg) > 0) |
| 844 | rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg)); |
Alexander Couzens | efdd80d | 2019-08-13 17:42:50 +0200 | [diff] [blame] | 845 | |
| 846 | /* A Iu Release event might be used to free the UE in cn_ranap_handle_co. */ |
| 847 | ue = ue_conn_ctx_find(prim->u.disconnect.conn_id); |
| 848 | if (!ue) |
| 849 | break; |
| 850 | |
Alexander Couzens | 790212f | 2019-09-10 18:23:37 +0200 | [diff] [blame] | 851 | global_iu_event(ue, RANAP_IU_EVENT_LINK_INVALIDATED, NULL); |
Alexander Couzens | 32fc82a | 2019-09-10 19:09:30 +0200 | [diff] [blame] | 852 | |
| 853 | /* A RANAP_IU_EVENT_LINK_INVALIDATED, can lead to a free */ |
| 854 | ue = ue_conn_ctx_find(prim->u.disconnect.conn_id); |
| 855 | if (!ue) |
| 856 | break; |
| 857 | if (ue->free_on_release) |
| 858 | ranap_iu_free_ue(ue); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 859 | break; |
| 860 | case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION): |
| 861 | /* connection-oriented data received */ |
| 862 | LOGPIU(LOGL_DEBUG, "N-DATA.ind(%u, %s)\n", prim->u.data.conn_id, |
| 863 | osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); |
| 864 | /* resolve UE context */ |
| 865 | ue = ue_conn_ctx_find(prim->u.data.conn_id); |
Alexander Couzens | 87482b3 | 2019-08-13 17:06:17 +0200 | [diff] [blame] | 866 | if (!ue) |
| 867 | break; |
| 868 | |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 869 | rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg)); |
| 870 | break; |
| 871 | case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION): |
| 872 | /* connection-less data received */ |
| 873 | LOGPIU(LOGL_DEBUG, "N-UNITDATA.ind(%s)\n", |
| 874 | osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); |
Harald Welte | 8fa35b8 | 2019-04-20 20:13:31 +0200 | [diff] [blame] | 875 | rc = ranap_cn_rx_cl(cn_ranap_handle_cl, prim, msgb_l2(oph->msg), msgb_l2len(oph->msg)); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 876 | break; |
| 877 | default: |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 878 | break; |
| 879 | } |
| 880 | |
| 881 | msgb_free(oph->msg); |
| 882 | return rc; |
| 883 | } |
| 884 | |
| 885 | int ranap_iu_init(void *ctx, int log_subsystem, const char *sccp_user_name, struct osmo_sccp_instance *sccp, |
| 886 | ranap_iu_recv_cb_t iu_recv_cb, ranap_iu_event_cb_t iu_event_cb) |
| 887 | { |
| 888 | iu_log_subsystem = log_subsystem; |
| 889 | talloc_iu_ctx = talloc_named_const(ctx, 1, "iu"); |
| 890 | talloc_asn1_ctx = talloc_named_const(talloc_iu_ctx, 1, "asn1"); |
| 891 | |
| 892 | global_iu_recv_cb = iu_recv_cb; |
| 893 | global_iu_event_cb = iu_event_cb; |
| 894 | g_sccp = sccp; |
Neels Hofmeyr | 1aef9a6 | 2017-08-12 18:02:44 +0200 | [diff] [blame] | 895 | osmo_sccp_local_addr_by_instance(&g_local_sccp_addr, sccp, OSMO_SCCP_SSN_RANAP); |
Neels Hofmeyr | aae68b2 | 2017-07-05 14:38:52 +0200 | [diff] [blame] | 896 | g_scu = osmo_sccp_user_bind(g_sccp, sccp_user_name, sccp_sap_up, OSMO_SCCP_SSN_RANAP); |
| 897 | |
| 898 | return 0; |
| 899 | } |