blob: c309ce4c377158478ccf979ceafb98f93452dd89 [file] [log] [blame]
Neels Hofmeyraae68b22017-07-05 14:38:52 +02001/* 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 Welteda86fe52017-11-21 08:14:37 +010028#include <asn1c/asn1helpers.h>
29
Neels Hofmeyraae68b22017-07-05 14:38:52 +020030#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 Welteda86fe52017-11-21 08:14:37 +010036#include <osmocom/sigtran/sccp_helpers.h>
37#include <osmocom/ranap/ranap_common_cn.h>
Neels Hofmeyraae68b22017-07-05 14:38:52 +020038#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(). */
44struct iu_grnc_id {
Neels Hofmeyrcf5fbea2018-03-21 15:50:41 +010045 struct osmo_plmn_id plmn;
Neels Hofmeyraae68b22017-07-05 14:38:52 +020046 uint16_t rnc_id;
47};
48
Neels Hofmeyr69888c82017-12-15 02:54:45 +010049struct 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 Hofmeyraae68b22017-07-05 14:38:52 +020058/* 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. */
65struct ranap_iu_rnc {
66 struct llist_head entry;
67
68 uint16_t rnc_id;
Neels Hofmeyraae68b22017-07-05 14:38:52 +020069 struct osmo_sccp_addr sccp_addr;
Neels Hofmeyr69888c82017-12-15 02:54:45 +010070
71 /* A list of struct iu_lac_rac_entry */
72 struct llist_head lac_rac_list;
Neels Hofmeyraae68b22017-07-05 14:38:52 +020073};
74
75void *talloc_iu_ctx;
76void *talloc_asn1_ctx;
77
78/* Implement the extern asn_debug from libasn1c to indicate whether to print
79 * asn.1 debug messages (see libasn1c). */
80int 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). */
85int asn1_xer_print = 0;
86
87ranap_iu_recv_cb_t global_iu_recv_cb = NULL;
88ranap_iu_event_cb_t global_iu_event_cb = NULL;
89int iu_log_subsystem = 0;
90
91#define LOGPIU(level, fmt, args...) \
92 LOGP(iu_log_subsystem, level, fmt, ## args)
93
Neels Hofmeyr69888c82017-12-15 02:54:45 +010094#define LOGPIUC(level, fmt, args...) \
95 LOGPC(iu_log_subsystem, level, fmt, ## args)
96
Neels Hofmeyraae68b22017-07-05 14:38:52 +020097static LLIST_HEAD(ue_conn_ctx_list);
98static LLIST_HEAD(rnc_list);
99
100static struct osmo_sccp_instance *g_sccp;
101static struct osmo_sccp_user *g_scu;
Neels Hofmeyr1aef9a62017-08-12 18:02:44 +0200102static struct osmo_sccp_addr g_local_sccp_addr;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200103
104const 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 Couzens790212f2019-09-10 18:23:37 +0200112static 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 Couzens8e2a0ee2019-09-10 18:28:49 +0200119 if (!ue_ctx->notification)
120 return 0;
121
Alexander Couzens790212f2019-09-10 18:23:37 +0200122 return global_iu_event_cb(ue_ctx, type, data);
123}
124
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200125static 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 Couzens8e2a0ee2019-09-10 18:28:49 +0200131 ctx->notification = true;
Alexander Couzens32fc82a2019-09-10 19:09:30 +0200132 ctx->free_on_release = false;
Alexander Couzensff796992019-09-10 17:46:54 +0200133 osmo_timer_setup(&ctx->release_timeout,
134 (void *)(void *) ranap_iu_free_ue,
135 ctx);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200136 llist_add(&ctx->list, &ue_conn_ctx_list);
137
138 return ctx;
139}
140
141static 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 Couzens609994d2019-08-12 16:48:56 +0200152void ranap_iu_free_ue(struct ranap_ue_conn_ctx *ue_ctx)
153{
154 if (!ue_ctx)
155 return;
156
Alexander Couzensff796992019-09-10 17:46:54 +0200157 osmo_timer_del(&ue_ctx->release_timeout);
Alexander Couzens609994d2019-08-12 16:48:56 +0200158 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 Hofmeyr69888c82017-12-15 02:54:45 +0100163static struct ranap_iu_rnc *iu_rnc_alloc(uint16_t rnc_id, struct osmo_sccp_addr *addr)
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200164{
165 struct ranap_iu_rnc *rnc = talloc_zero(talloc_iu_ctx, struct ranap_iu_rnc);
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100166 OSMO_ASSERT(rnc);
167
168 INIT_LLIST_HEAD(&rnc->lac_rac_list);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200169
170 rnc->rnc_id = rnc_id;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200171 rnc->sccp_addr = *addr;
172 llist_add(&rnc->entry, &rnc_list);
173
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100174 LOGPIU(LOGL_NOTICE, "New RNC %d at %s\n", rnc->rnc_id, osmo_sccp_addr_dump(addr));
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200175
176 return rnc;
177}
178
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100179/* 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. */
182static 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
207static 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
217static 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 Hofmeyraae68b22017-07-05 14:38:52 +0200224static 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 Hofmeyr69888c82017-12-15 02:54:45 +0100228 struct ranap_iu_rnc *old_rnc;
229 struct iu_lac_rac_entry *lre;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200230
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100231 /* 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 Hofmeyraae68b22017-07-05 14:38:52 +0200233
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100234 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 Hofmeyraae68b22017-07-05 14:38:52 +0200242
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100243 /* 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 Hofmeyraae68b22017-07-05 14:38:52 +0200262 }
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100263 /* else, LAC,RAC already recorded with the current RNC. */
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200264
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100265 return rnc;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200266}
267
268/***********************************************************************
269 * RANAP handling
270 ***********************************************************************/
271
272int 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
287int ranap_iu_rab_deact(struct ranap_ue_conn_ctx *ue_ctx, uint8_t rab_id)
288{
289 /* FIXME */
290 return -1;
291}
292
293int 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);
309 osmo_sccp_user_sap_down(g_scu, &prim->oph);
310
311 return 0;
312}
313
314int ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *uectx, const char *imsi)
315{
316 struct msgb *msg;
317 struct osmo_scu_prim *prim;
318
319 LOGPIU(LOGL_INFO, "Transmitting RANAP CommonID (SCCP conn_id %u)\n",
320 uectx->conn_id);
321
322 msg = ranap_new_msg_common_id(imsi);
323 msg->l2h = msg->data;
324 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
325 prim->u.data.conn_id = uectx->conn_id;
326 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
327 OSMO_SCU_PRIM_N_DATA,
328 PRIM_OP_REQUEST, msg);
329 osmo_sccp_user_sap_down(g_scu, &prim->oph);
330 return 0;
331}
332
333static int iu_grnc_id_parse(struct iu_grnc_id *dst, struct RANAP_GlobalRNC_ID *src)
334{
335 /* The size is coming from arbitrary sender, check it gracefully */
336 if (src->pLMNidentity.size != 3) {
337 LOGPIU(LOGL_ERROR, "Invalid PLMN Identity size:"
338 " should be 3, is %d\n", src->pLMNidentity.size);
339 return -1;
340 }
Neels Hofmeyrcf5fbea2018-03-21 15:50:41 +0100341 osmo_plmn_from_bcd(&src->pLMNidentity.buf[0], &dst->plmn);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200342 dst->rnc_id = (uint16_t)src->rNC_ID;
343 return 0;
344}
345
346#if 0
347 -- not used at present --
348static int iu_grnc_id_compose(struct iu_grnc_id *src, struct RANAP_GlobalRNC_ID *dst)
349{
350 /* The caller must ensure proper size */
351 OSMO_ASSERT(dst->pLMNidentity.size == 3);
352 gsm48_mcc_mnc_to_bcd(&dst->pLMNidentity.buf[0],
353 src->mcc, src->mnc);
354 dst->rNC_ID = src->rnc_id;
355 return 0;
356}
357#endif
358
359struct new_ue_conn_ctx {
360 struct osmo_sccp_addr sccp_addr;
361 uint32_t conn_id;
362};
363
364static int ranap_handle_co_initial_ue(void *ctx, RANAP_InitialUE_MessageIEs_t *ies)
365{
366 struct new_ue_conn_ctx *new_ctx = ctx;
367 struct gprs_ra_id ra_id;
368 struct iu_grnc_id grnc_id;
369 uint16_t sai;
370 struct ranap_ue_conn_ctx *ue;
371 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
372 struct ranap_iu_rnc *rnc;
373
374 if (ranap_parse_lai(&ra_id, &ies->lai) != 0) {
375 LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
376 return -1;
377 }
378
379 if (ies->presenceMask & INITIALUE_MESSAGEIES_RANAP_RAC_PRESENT) {
380 ra_id.rac = asn1str_to_u8(&ies->rac);
381 }
382
383 if (iu_grnc_id_parse(&grnc_id, &ies->globalRNC_ID) != 0) {
384 LOGPIU(LOGL_ERROR,
385 "Failed to parse RANAP Global-RNC-ID IE\n");
386 return -1;
387 }
388
389 sai = asn1str_to_u16(&ies->sai.sAC);
390 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
391 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
392
393 /* Make sure we know the RNC Id and LAC+RAC coming in on this connection. */
394 rnc = iu_rnc_register(grnc_id.rnc_id, ra_id.lac, ra_id.rac, &new_ctx->sccp_addr);
395
396 ue = ue_conn_ctx_alloc(rnc, new_ctx->conn_id);
397 OSMO_ASSERT(ue);
398 ue->ra_id = ra_id;
399
400 /* Feed into the MM layer */
401 msg->dst = ue;
402 global_iu_recv_cb(msg, &ra_id, &sai);
403
404 msgb_free(msg);
405
406 return 0;
407}
408
409static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies)
410{
411 struct gprs_ra_id _ra_id, *ra_id = NULL;
412 uint16_t _sai, *sai = NULL;
413 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
414
415 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) {
416 if (ranap_parse_lai(&_ra_id, &ies->lai) != 0) {
417 LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
418 return -1;
419 }
420 ra_id = &_ra_id;
421 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) {
422 _ra_id.rac = asn1str_to_u8(&ies->rac);
423 }
424 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) {
425 _sai = asn1str_to_u16(&ies->sai.sAC);
426 sai = &_sai;
427 }
428 }
429
430 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
431 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
432
433 /* Feed into the MM/CC/SMS-CP layer */
434 msg->dst = ctx;
435 global_iu_recv_cb(msg, ra_id, sai);
436
437 msgb_free(msg);
438
439 return 0;
440}
441
442static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
443{
444 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
445 LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n",
446 ranap_cause_str(&ies->cause));
447 else
448 LOGPIU(LOGL_ERROR, "Rx Error Indication\n");
449
450 return 0;
451}
452
453int ranap_iu_tx(struct msgb *msg_nas, uint8_t sapi)
454{
455 struct ranap_ue_conn_ctx *uectx = msg_nas->dst;
456 struct msgb *msg;
457 struct osmo_scu_prim *prim;
458
459 LOGPIU(LOGL_INFO, "Transmitting L3 Message as RANAP DT (SCCP conn_id %u)\n",
460 uectx->conn_id);
461
462 msg = ranap_new_msg_dt(sapi, msg_nas->data, msgb_length(msg_nas));
463 msgb_free(msg_nas);
464 msg->l2h = msg->data;
465 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
466 prim->u.data.conn_id = uectx->conn_id;
467 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
468 OSMO_SCU_PRIM_N_DATA,
469 PRIM_OP_REQUEST, msg);
470 osmo_sccp_user_sap_down(g_scu, &prim->oph);
471 return 0;
472}
473
474/* Send Iu Release for the given UE connection.
475 * If cause is NULL, the standard "No remaining RAB" cause is sent, otherwise
476 * the provided cause. */
477int ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
478{
479 struct msgb *msg;
480 struct osmo_scu_prim *prim;
481 static const struct RANAP_Cause default_cause = {
482 .present = RANAP_Cause_PR_radioNetwork,
483 .choice.radioNetwork = RANAP_CauseRadioNetwork_no_remaining_rab,
484 };
485
486 if (!cause)
487 cause = &default_cause;
488
489 msg = ranap_new_msg_iu_rel_cmd(cause);
490 msg->l2h = msg->data;
491 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
492 prim->u.data.conn_id = ctx->conn_id;
493 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
494 OSMO_SCU_PRIM_N_DATA,
495 PRIM_OP_REQUEST, msg);
496 return osmo_sccp_user_sap_down(g_scu, &prim->oph);
497}
498
Alexander Couzensff796992019-09-10 17:46:54 +0200499void ranap_iu_tx_release_free(struct ranap_ue_conn_ctx *ctx,
500 const struct RANAP_Cause *cause,
501 int timeout)
502{
503 ctx->notification = false;
Alexander Couzens32fc82a2019-09-10 19:09:30 +0200504 ctx->free_on_release = true;
Alexander Couzensff796992019-09-10 17:46:54 +0200505 int ret = ranap_iu_tx_release(ctx, cause);
506 if (ret) {
507 ranap_iu_free_ue(ctx);
508 return;
509 }
510
511 osmo_timer_schedule(&ctx->release_timeout, timeout, 0);
512}
513
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200514static int ranap_handle_co_iu_rel_req(struct ranap_ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies)
515{
516 LOGPIU(LOGL_INFO, "Received Iu Release Request, Sending Release Command\n");
517 ranap_iu_tx_release(ctx, &ies->cause);
518 return 0;
519}
520
521static int ranap_handle_co_rab_ass_resp(struct ranap_ue_conn_ctx *ctx, RANAP_RAB_AssignmentResponseIEs_t *ies)
522{
523 int rc = -1;
524
525 LOGPIU(LOGL_INFO,
526 "Rx RAB Assignment Response for UE conn_id %u\n", ctx->conn_id);
527 if (ies->presenceMask & RAB_ASSIGNMENTRESPONSEIES_RANAP_RAB_SETUPORMODIFIEDLIST_PRESENT) {
528 /* TODO: Iterate over list of SetupOrModifiedList IEs and handle each one */
529 RANAP_IE_t *ranap_ie = ies->raB_SetupOrModifiedList.raB_SetupOrModifiedList_ies.list.array[0];
530 RANAP_RAB_SetupOrModifiedItemIEs_t setup_ies;
531
532 rc = ranap_decode_rab_setupormodifieditemies_fromlist(&setup_ies, &ranap_ie->value);
533 if (rc) {
534 LOGPIU(LOGL_ERROR, "Error in ranap_decode_rab_setupormodifieditemies()\n");
535 return rc;
536 }
537
Alexander Couzens790212f2019-09-10 18:23:37 +0200538 rc = global_iu_event(ctx, RANAP_IU_EVENT_RAB_ASSIGN, &setup_ies);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200539
540 ranap_free_rab_setupormodifieditemies(&setup_ies);
541 }
542 /* FIXME: handle RAB Ass failure? */
543
544 return rc;
545}
546
547static void cn_ranap_handle_co_initial(void *ctx, ranap_message *message)
548{
549 int rc;
550
551 LOGPIU(LOGL_NOTICE, "handle_co_initial(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
552
553 if (message->direction != RANAP_RANAP_PDU_PR_initiatingMessage
554 || message->procedureCode != RANAP_ProcedureCode_id_InitialUE_Message) {
555 LOGPIU(LOGL_ERROR, "Expected direction 'InitiatingMessage',"
556 " procedureCode 'InitialUE_Message', instead got %u and %u\n",
557 message->direction, message->procedureCode);
558 rc = -1;
559 }
560 else
561 rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs);
562
563 if (rc) {
564 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
565 /* TODO handling of the error? */
566 }
567}
568
569/* Entry point for connection-oriented RANAP message */
570static void cn_ranap_handle_co(void *ctx, ranap_message *message)
571{
572 int rc;
573
574 LOGPIU(LOGL_NOTICE, "handle_co(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
575
576 switch (message->direction) {
577 case RANAP_RANAP_PDU_PR_initiatingMessage:
578 switch (message->procedureCode) {
579 case RANAP_ProcedureCode_id_InitialUE_Message:
580 LOGPIU(LOGL_ERROR, "Got InitialUE_Message but this is not a new conn\n");
581 rc = -1;
582 break;
583 case RANAP_ProcedureCode_id_DirectTransfer:
584 rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs);
585 break;
586 case RANAP_ProcedureCode_id_ErrorIndication:
587 rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs);
588 break;
589 case RANAP_ProcedureCode_id_Iu_ReleaseRequest:
590 /* Iu Release Request */
591 rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs);
592 break;
593 default:
594 LOGPIU(LOGL_ERROR, "Received Initiating Message: unknown Procedure Code %d\n",
595 message->procedureCode);
596 rc = -1;
597 break;
598 }
599 break;
600 case RANAP_RANAP_PDU_PR_successfulOutcome:
601 switch (message->procedureCode) {
602 case RANAP_ProcedureCode_id_SecurityModeControl:
603 /* Security Mode Complete */
Alexander Couzens790212f2019-09-10 18:23:37 +0200604 rc = global_iu_event(ctx, RANAP_IU_EVENT_SECURITY_MODE_COMPLETE, NULL);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200605 break;
606 case RANAP_ProcedureCode_id_Iu_Release:
607 /* Iu Release Complete */
Alexander Couzens790212f2019-09-10 18:23:37 +0200608 rc = global_iu_event(ctx, RANAP_IU_EVENT_IU_RELEASE, NULL);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200609 if (rc) {
610 LOGPIU(LOGL_ERROR, "Iu Release event: Iu Event callback returned %d\n",
611 rc);
612 }
613 break;
614 default:
615 LOGPIU(LOGL_ERROR, "Received Successful Outcome: unknown Procedure Code %d\n",
616 message->procedureCode);
617 rc = -1;
618 break;
619 }
620 break;
621 case RANAP_RANAP_PDU_PR_outcome:
622 switch (message->procedureCode) {
623 case RANAP_ProcedureCode_id_RAB_Assignment:
624 /* RAB Assignment Response */
625 rc = ranap_handle_co_rab_ass_resp(ctx, &message->msg.raB_AssignmentResponseIEs);
626 break;
627 default:
628 LOGPIU(LOGL_ERROR, "Received Outcome: unknown Procedure Code %d\n",
629 message->procedureCode);
630 rc = -1;
631 break;
632 }
633 break;
634 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
635 default:
636 LOGPIU(LOGL_ERROR, "Received Unsuccessful Outcome: Procedure Code %d\n",
637 message->procedureCode);
638 rc = -1;
639 break;
640 }
641
642 if (rc) {
643 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
644 /* TODO handling of the error? */
645 }
646}
647
648static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies)
649{
Harald Welte8fa35b82019-04-20 20:13:31 +0200650 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) ctx;
651 struct osmo_scu_unitdata_param *ud_prim = &prim->u.unitdata;
652 RANAP_GlobalRNC_ID_t *grnc_id = NULL;
653 struct msgb *resp;
654
655 OSMO_ASSERT(prim->oph.primitive == OSMO_SCU_PRIM_N_UNITDATA);
656
657 /* FIXME: verify ies.cN_DomainIndicator */
658
659 if (ies->presenceMask & RESETIES_RANAP_GLOBALRNC_ID_PRESENT)
660 grnc_id = &ies->globalRNC_ID;
661
662 /* send reset response */
663 resp = ranap_new_msg_reset_ack(ies->cN_DomainIndicator, grnc_id);
664 if (!resp)
665 return -ENOMEM;
666 resp->l2h = resp->data;
667 return osmo_sccp_tx_unitdata_msg(g_scu, &g_local_sccp_addr, &ud_prim->calling_addr, resp);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200668}
669
670static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
671{
672 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
673 LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n",
674 ranap_cause_str(&ies->cause));
675 else
676 LOGPIU(LOGL_ERROR, "Rx Error Indication\n");
677
678 return 0;
679}
680
681/* Entry point for connection-less RANAP message */
682static void cn_ranap_handle_cl(void *ctx, ranap_message *message)
683{
684 int rc;
685
686 switch (message->direction) {
687 case RANAP_RANAP_PDU_PR_initiatingMessage:
688 switch (message->procedureCode) {
689 case RANAP_ProcedureCode_id_Reset:
690 /* received reset.req, send reset.resp */
691 rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs);
692 break;
693 case RANAP_ProcedureCode_id_ErrorIndication:
694 rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs);
695 break;
696 default:
697 rc = -1;
698 break;
699 }
700 break;
701 case RANAP_RANAP_PDU_PR_successfulOutcome:
702 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
703 case RANAP_RANAP_PDU_PR_outcome:
704 default:
705 rc = -1;
706 break;
707 }
708
709 if (rc) {
710 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
711 /* TODO handling of the error? */
712 }
713}
714
715/***********************************************************************
716 * Paging
717 ***********************************************************************/
718
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200719/* Send a paging command down a given SCCP User. tmsi and paging_cause are
720 * optional and may be passed NULL and 0, respectively, to disable their use.
721 * See enum RANAP_PagingCause.
722 *
723 * If TMSI is given, the IMSI is not sent over the air interface. Nevertheless,
724 * the IMSI is still required for resolution in the HNB-GW and/or(?) RNC. */
725static int iu_tx_paging_cmd(struct osmo_sccp_addr *called_addr,
726 const char *imsi, const uint32_t *tmsi,
727 bool is_ps, uint32_t paging_cause)
728{
729 struct msgb *msg;
730 msg = ranap_new_msg_paging_cmd(imsi, tmsi, is_ps? 1 : 0, paging_cause);
731 msg->l2h = msg->data;
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100732 return osmo_sccp_tx_unitdata_msg(g_scu, &g_local_sccp_addr, called_addr, msg);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200733}
734
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100735static int iu_page(const char *imsi, const uint32_t *tmsi_or_ptmsi,
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200736 uint16_t lac, uint8_t rac, bool is_ps)
737{
738 struct ranap_iu_rnc *rnc;
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100739 const char *log_msg;
740 int log_level;
741 int paged = 0;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200742
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100743 iu_rnc_lac_rac_find(&rnc, NULL, lac, rac);
744 if (rnc) {
745 if (iu_tx_paging_cmd(&rnc->sccp_addr, imsi, tmsi_or_ptmsi, is_ps, 0) == 0) {
746 log_msg = "Paging";
747 log_level = LOGL_DEBUG;
748 paged = 1;
749 } else {
750 log_msg = "Paging failed";
751 log_level = LOGL_ERROR;
752 }
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200753 } else {
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100754 log_msg = "Found no RNC to Page";
755 log_level = LOGL_ERROR;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200756 }
757
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100758 if (is_ps)
759 LOGPIU(log_level, "IuPS: %s on LAC %d RAC %d", log_msg, lac, rac);
760 else
761 LOGPIU(log_level, "IuCS: %s on LAC %d", log_msg, lac);
762 if (rnc)
763 LOGPIUC(log_level, " at SCCP-addr %s", osmo_sccp_addr_dump(&rnc->sccp_addr));
764 if (tmsi_or_ptmsi)
765 LOGPIUC(log_level, ", for %s %08x\n", is_ps? "PTMSI" : "TMSI", *tmsi_or_ptmsi);
766 else
767 LOGPIUC(log_level, ", for IMSI %s\n", imsi);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200768
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100769 return paged;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200770}
771
772int ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
773{
774 return iu_page(imsi, tmsi, lac, 0, false);
775}
776
777int ranap_iu_page_ps(const char *imsi, const uint32_t *ptmsi, uint16_t lac, uint8_t rac)
778{
779 return iu_page(imsi, ptmsi, lac, rac, true);
780}
781
782
783/***********************************************************************
784 *
785 ***********************************************************************/
786
787int tx_unitdata(struct osmo_sccp_user *scu);
788int tx_conn_req(struct osmo_sccp_user *scu, uint32_t conn_id);
789
790struct osmo_prim_hdr *make_conn_req(uint32_t conn_id);
791struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len);
792
793static struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param)
794{
795 struct msgb *msg = msgb_alloc(1024, "conn_resp");
796 struct osmo_scu_prim *prim;
797
798 prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim));
799 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
800 OSMO_SCU_PRIM_N_CONNECT,
801 PRIM_OP_RESPONSE, msg);
802 memcpy(&prim->u.connect, param, sizeof(prim->u.connect));
803 return &prim->oph;
804}
805
806static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
807{
808 struct osmo_sccp_user *scu = _scu;
809 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
810 struct osmo_prim_hdr *resp = NULL;
Alexander Couzens80307922019-08-13 17:13:59 +0200811 int rc = -1;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200812 struct ranap_ue_conn_ctx *ue;
813 struct new_ue_conn_ctx new_ctx = {};
814
815 LOGPIU(LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
816
817 switch (OSMO_PRIM_HDR(oph)) {
818 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
819 /* confirmation of outbound connection */
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200820 break;
821 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
822 /* indication of new inbound connection request*/
823 LOGPIU(LOGL_DEBUG, "N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id);
824 if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */
825 !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) {
826 LOGPIU(LOGL_NOTICE,
827 "Received invalid N-CONNECT.ind\n");
828 return 0;
829 }
830 new_ctx.sccp_addr = prim->u.connect.calling_addr;
831 new_ctx.conn_id = prim->u.connect.conn_id;
832 /* first ensure the local SCCP socket is ACTIVE */
833 resp = make_conn_resp(&prim->u.connect);
834 osmo_sccp_user_sap_down(scu, resp);
835 /* then handle the RANAP payload */
836 rc = ranap_cn_rx_co(cn_ranap_handle_co_initial, &new_ctx, msgb_l2(oph->msg), msgb_l2len(oph->msg));
837 break;
838 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
839 /* indication of disconnect */
840 LOGPIU(LOGL_DEBUG, "N-DISCONNECT.ind(%u)\n",
841 prim->u.disconnect.conn_id);
842 ue = ue_conn_ctx_find(prim->u.disconnect.conn_id);
Alexander Couzens87482b32019-08-13 17:06:17 +0200843 if (!ue)
844 break;
845
Alexander Couzens4ae261b2019-08-13 17:14:31 +0200846 rc = 0;
847 if (msgb_l2len(oph->msg) > 0)
848 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
Alexander Couzensefdd80d2019-08-13 17:42:50 +0200849
850 /* A Iu Release event might be used to free the UE in cn_ranap_handle_co. */
851 ue = ue_conn_ctx_find(prim->u.disconnect.conn_id);
852 if (!ue)
853 break;
854
Alexander Couzens790212f2019-09-10 18:23:37 +0200855 global_iu_event(ue, RANAP_IU_EVENT_LINK_INVALIDATED, NULL);
Alexander Couzens32fc82a2019-09-10 19:09:30 +0200856
857 /* A RANAP_IU_EVENT_LINK_INVALIDATED, can lead to a free */
858 ue = ue_conn_ctx_find(prim->u.disconnect.conn_id);
859 if (!ue)
860 break;
861 if (ue->free_on_release)
862 ranap_iu_free_ue(ue);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200863 break;
864 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
865 /* connection-oriented data received */
866 LOGPIU(LOGL_DEBUG, "N-DATA.ind(%u, %s)\n", prim->u.data.conn_id,
867 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
868 /* resolve UE context */
869 ue = ue_conn_ctx_find(prim->u.data.conn_id);
Alexander Couzens87482b32019-08-13 17:06:17 +0200870 if (!ue)
871 break;
872
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200873 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
874 break;
875 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
876 /* connection-less data received */
877 LOGPIU(LOGL_DEBUG, "N-UNITDATA.ind(%s)\n",
878 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Harald Welte8fa35b82019-04-20 20:13:31 +0200879 rc = ranap_cn_rx_cl(cn_ranap_handle_cl, prim, msgb_l2(oph->msg), msgb_l2len(oph->msg));
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200880 break;
881 default:
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200882 break;
883 }
884
885 msgb_free(oph->msg);
886 return rc;
887}
888
889int ranap_iu_init(void *ctx, int log_subsystem, const char *sccp_user_name, struct osmo_sccp_instance *sccp,
890 ranap_iu_recv_cb_t iu_recv_cb, ranap_iu_event_cb_t iu_event_cb)
891{
892 iu_log_subsystem = log_subsystem;
893 talloc_iu_ctx = talloc_named_const(ctx, 1, "iu");
894 talloc_asn1_ctx = talloc_named_const(talloc_iu_ctx, 1, "asn1");
895
896 global_iu_recv_cb = iu_recv_cb;
897 global_iu_event_cb = iu_event_cb;
898 g_sccp = sccp;
Neels Hofmeyr1aef9a62017-08-12 18:02:44 +0200899 osmo_sccp_local_addr_by_instance(&g_local_sccp_addr, sccp, OSMO_SCCP_SSN_RANAP);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200900 g_scu = osmo_sccp_user_bind(g_sccp, sccp_user_name, sccp_sap_up, OSMO_SCCP_SSN_RANAP);
901
902 return 0;
903}