blob: c7ed341fc39d8d0c782fbcad922bd7472121780e [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
112static struct ranap_ue_conn_ctx *ue_conn_ctx_alloc(struct ranap_iu_rnc *rnc, uint32_t conn_id)
113{
114 struct ranap_ue_conn_ctx *ctx = talloc_zero(talloc_iu_ctx, struct ranap_ue_conn_ctx);
115
116 ctx->rnc = rnc;
117 ctx->conn_id = conn_id;
118 llist_add(&ctx->list, &ue_conn_ctx_list);
119
120 return ctx;
121}
122
123static struct ranap_ue_conn_ctx *ue_conn_ctx_find(uint32_t conn_id)
124{
125 struct ranap_ue_conn_ctx *ctx;
126
127 llist_for_each_entry(ctx, &ue_conn_ctx_list, list) {
128 if (ctx->conn_id == conn_id)
129 return ctx;
130 }
131 return NULL;
132}
133
Alexander Couzens609994d2019-08-12 16:48:56 +0200134void ranap_iu_free_ue(struct ranap_ue_conn_ctx *ue_ctx)
135{
136 if (!ue_ctx)
137 return;
138
139 osmo_sccp_tx_disconn(g_scu, ue_ctx->conn_id, NULL, 0);
140 llist_del(&ue_ctx->list);
141 talloc_free(ue_ctx);
142}
143
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100144static struct ranap_iu_rnc *iu_rnc_alloc(uint16_t rnc_id, struct osmo_sccp_addr *addr)
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200145{
146 struct ranap_iu_rnc *rnc = talloc_zero(talloc_iu_ctx, struct ranap_iu_rnc);
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100147 OSMO_ASSERT(rnc);
148
149 INIT_LLIST_HEAD(&rnc->lac_rac_list);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200150
151 rnc->rnc_id = rnc_id;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200152 rnc->sccp_addr = *addr;
153 llist_add(&rnc->entry, &rnc_list);
154
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100155 LOGPIU(LOGL_NOTICE, "New RNC %d at %s\n", rnc->rnc_id, osmo_sccp_addr_dump(addr));
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200156
157 return rnc;
158}
159
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100160/* Find a match for the given LAC (and RAC). For CS, pass rac as 0.
161 * If rnc and lre pointers are not NULL, *rnc / *lre are set to NULL if no match is found, or to the
162 * match if a match is found. Return true if a match is found. */
163static bool iu_rnc_lac_rac_find(struct ranap_iu_rnc **rnc, struct iu_lac_rac_entry **lre,
164 uint16_t lac, uint8_t rac)
165{
166 struct ranap_iu_rnc *r;
167 struct iu_lac_rac_entry *e;
168
169 if (rnc)
170 *rnc = NULL;
171 if (lre)
172 *lre = NULL;
173
174 llist_for_each_entry(r, &rnc_list, entry) {
175 llist_for_each_entry(e, &r->lac_rac_list, entry) {
176 if (e->lac == lac && e->rac == rac) {
177 if (rnc)
178 *rnc = r;
179 if (lre)
180 *lre = e;
181 return true;
182 }
183 }
184 }
185 return false;
186}
187
188static struct ranap_iu_rnc *iu_rnc_id_find(uint16_t rnc_id)
189{
190 struct ranap_iu_rnc *rnc;
191 llist_for_each_entry(rnc, &rnc_list, entry) {
192 if (rnc->rnc_id == rnc_id)
193 return rnc;
194 }
195 return NULL;
196}
197
198static bool same_sccp_addr(struct osmo_sccp_addr *a, struct osmo_sccp_addr *b)
199{
200 char buf[256];
201 osmo_strlcpy(buf, osmo_sccp_addr_dump(a), sizeof(buf));
202 return !strcmp(buf, osmo_sccp_addr_dump(b));
203}
204
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200205static struct ranap_iu_rnc *iu_rnc_register(uint16_t rnc_id, uint16_t lac,
206 uint8_t rac, struct osmo_sccp_addr *addr)
207{
208 struct ranap_iu_rnc *rnc;
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100209 struct ranap_iu_rnc *old_rnc;
210 struct iu_lac_rac_entry *lre;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200211
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100212 /* Make sure we know this rnc_id and that this SCCP address is in our records */
213 rnc = iu_rnc_id_find(rnc_id);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200214
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100215 if (rnc) {
216 if (!same_sccp_addr(&rnc->sccp_addr, addr)) {
217 LOGPIU(LOGL_NOTICE, "RNC %u changed its SCCP addr to %s (LAC=%u RAC=%u)\n",
218 rnc_id, osmo_sccp_addr_dump(addr), lac, rac);
219 rnc->sccp_addr = *addr;
220 }
221 } else
222 rnc = iu_rnc_alloc(rnc_id, addr);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200223
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100224 /* Detect whether the LAC,RAC is already recorded in another RNC */
225 iu_rnc_lac_rac_find(&old_rnc, &lre, lac, rac);
226
227 if (old_rnc && old_rnc != rnc) {
228 /* LAC,RAC already exists in a different RNC */
229 LOGPIU(LOGL_NOTICE, "LAC %u RAC %u moved from RNC %u %s",
230 lac, rac, old_rnc->rnc_id, osmo_sccp_addr_dump(&old_rnc->sccp_addr));
231 LOGPIUC(LOGL_NOTICE, " to RNC %u %s\n",
232 rnc->rnc_id, osmo_sccp_addr_dump(&rnc->sccp_addr));
233
234 llist_del(&lre->entry);
235 llist_add(&lre->entry, &rnc->lac_rac_list);
236 } else if (!old_rnc) {
237 /* LAC,RAC not recorded yet */
238 LOGPIU(LOGL_NOTICE, "RNC %u: new LAC %u RAC %u\n", rnc_id, lac, rac);
239 lre = talloc_zero(rnc, struct iu_lac_rac_entry);
240 lre->lac = lac;
241 lre->rac = rac;
242 llist_add(&lre->entry, &rnc->lac_rac_list);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200243 }
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100244 /* else, LAC,RAC already recorded with the current RNC. */
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200245
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100246 return rnc;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200247}
248
249/***********************************************************************
250 * RANAP handling
251 ***********************************************************************/
252
253int ranap_iu_rab_act(struct ranap_ue_conn_ctx *ue_ctx, struct msgb *msg)
254{
255 struct osmo_scu_prim *prim;
256
257 /* wrap RANAP message in SCCP N-DATA.req */
258 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
259 prim->u.data.conn_id = ue_ctx->conn_id;
260 osmo_prim_init(&prim->oph,
261 SCCP_SAP_USER,
262 OSMO_SCU_PRIM_N_DATA,
263 PRIM_OP_REQUEST,
264 msg);
265 return osmo_sccp_user_sap_down(g_scu, &prim->oph);
266}
267
268int ranap_iu_rab_deact(struct ranap_ue_conn_ctx *ue_ctx, uint8_t rab_id)
269{
270 /* FIXME */
271 return -1;
272}
273
274int ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
275 int send_ck, int new_key)
276{
277 struct osmo_scu_prim *prim;
278 struct msgb *msg;
279
280 /* create RANAP message */
281 msg = ranap_new_msg_sec_mod_cmd(vec->ik, send_ck? vec->ck : NULL,
282 new_key ? RANAP_KeyStatus_new : RANAP_KeyStatus_old);
283 msg->l2h = msg->data;
284 /* wrap RANAP message in SCCP N-DATA.req */
285 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
286 prim->u.data.conn_id = uectx->conn_id;
287 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
288 OSMO_SCU_PRIM_N_DATA,
289 PRIM_OP_REQUEST, msg);
290 osmo_sccp_user_sap_down(g_scu, &prim->oph);
291
292 return 0;
293}
294
295int ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *uectx, const char *imsi)
296{
297 struct msgb *msg;
298 struct osmo_scu_prim *prim;
299
300 LOGPIU(LOGL_INFO, "Transmitting RANAP CommonID (SCCP conn_id %u)\n",
301 uectx->conn_id);
302
303 msg = ranap_new_msg_common_id(imsi);
304 msg->l2h = msg->data;
305 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
306 prim->u.data.conn_id = uectx->conn_id;
307 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
308 OSMO_SCU_PRIM_N_DATA,
309 PRIM_OP_REQUEST, msg);
310 osmo_sccp_user_sap_down(g_scu, &prim->oph);
311 return 0;
312}
313
314static int iu_grnc_id_parse(struct iu_grnc_id *dst, struct RANAP_GlobalRNC_ID *src)
315{
316 /* The size is coming from arbitrary sender, check it gracefully */
317 if (src->pLMNidentity.size != 3) {
318 LOGPIU(LOGL_ERROR, "Invalid PLMN Identity size:"
319 " should be 3, is %d\n", src->pLMNidentity.size);
320 return -1;
321 }
Neels Hofmeyrcf5fbea2018-03-21 15:50:41 +0100322 osmo_plmn_from_bcd(&src->pLMNidentity.buf[0], &dst->plmn);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200323 dst->rnc_id = (uint16_t)src->rNC_ID;
324 return 0;
325}
326
327#if 0
328 -- not used at present --
329static int iu_grnc_id_compose(struct iu_grnc_id *src, struct RANAP_GlobalRNC_ID *dst)
330{
331 /* The caller must ensure proper size */
332 OSMO_ASSERT(dst->pLMNidentity.size == 3);
333 gsm48_mcc_mnc_to_bcd(&dst->pLMNidentity.buf[0],
334 src->mcc, src->mnc);
335 dst->rNC_ID = src->rnc_id;
336 return 0;
337}
338#endif
339
340struct new_ue_conn_ctx {
341 struct osmo_sccp_addr sccp_addr;
342 uint32_t conn_id;
343};
344
345static int ranap_handle_co_initial_ue(void *ctx, RANAP_InitialUE_MessageIEs_t *ies)
346{
347 struct new_ue_conn_ctx *new_ctx = ctx;
348 struct gprs_ra_id ra_id;
349 struct iu_grnc_id grnc_id;
350 uint16_t sai;
351 struct ranap_ue_conn_ctx *ue;
352 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
353 struct ranap_iu_rnc *rnc;
354
355 if (ranap_parse_lai(&ra_id, &ies->lai) != 0) {
356 LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
357 return -1;
358 }
359
360 if (ies->presenceMask & INITIALUE_MESSAGEIES_RANAP_RAC_PRESENT) {
361 ra_id.rac = asn1str_to_u8(&ies->rac);
362 }
363
364 if (iu_grnc_id_parse(&grnc_id, &ies->globalRNC_ID) != 0) {
365 LOGPIU(LOGL_ERROR,
366 "Failed to parse RANAP Global-RNC-ID IE\n");
367 return -1;
368 }
369
370 sai = asn1str_to_u16(&ies->sai.sAC);
371 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
372 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
373
374 /* Make sure we know the RNC Id and LAC+RAC coming in on this connection. */
375 rnc = iu_rnc_register(grnc_id.rnc_id, ra_id.lac, ra_id.rac, &new_ctx->sccp_addr);
376
377 ue = ue_conn_ctx_alloc(rnc, new_ctx->conn_id);
378 OSMO_ASSERT(ue);
379 ue->ra_id = ra_id;
380
381 /* Feed into the MM layer */
382 msg->dst = ue;
383 global_iu_recv_cb(msg, &ra_id, &sai);
384
385 msgb_free(msg);
386
387 return 0;
388}
389
390static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies)
391{
392 struct gprs_ra_id _ra_id, *ra_id = NULL;
393 uint16_t _sai, *sai = NULL;
394 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
395
396 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) {
397 if (ranap_parse_lai(&_ra_id, &ies->lai) != 0) {
398 LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
399 return -1;
400 }
401 ra_id = &_ra_id;
402 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) {
403 _ra_id.rac = asn1str_to_u8(&ies->rac);
404 }
405 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) {
406 _sai = asn1str_to_u16(&ies->sai.sAC);
407 sai = &_sai;
408 }
409 }
410
411 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
412 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
413
414 /* Feed into the MM/CC/SMS-CP layer */
415 msg->dst = ctx;
416 global_iu_recv_cb(msg, ra_id, sai);
417
418 msgb_free(msg);
419
420 return 0;
421}
422
423static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
424{
425 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
426 LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n",
427 ranap_cause_str(&ies->cause));
428 else
429 LOGPIU(LOGL_ERROR, "Rx Error Indication\n");
430
431 return 0;
432}
433
434int ranap_iu_tx(struct msgb *msg_nas, uint8_t sapi)
435{
436 struct ranap_ue_conn_ctx *uectx = msg_nas->dst;
437 struct msgb *msg;
438 struct osmo_scu_prim *prim;
439
440 LOGPIU(LOGL_INFO, "Transmitting L3 Message as RANAP DT (SCCP conn_id %u)\n",
441 uectx->conn_id);
442
443 msg = ranap_new_msg_dt(sapi, msg_nas->data, msgb_length(msg_nas));
444 msgb_free(msg_nas);
445 msg->l2h = msg->data;
446 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
447 prim->u.data.conn_id = uectx->conn_id;
448 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
449 OSMO_SCU_PRIM_N_DATA,
450 PRIM_OP_REQUEST, msg);
451 osmo_sccp_user_sap_down(g_scu, &prim->oph);
452 return 0;
453}
454
455/* Send Iu Release for the given UE connection.
456 * If cause is NULL, the standard "No remaining RAB" cause is sent, otherwise
457 * the provided cause. */
458int ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
459{
460 struct msgb *msg;
461 struct osmo_scu_prim *prim;
462 static const struct RANAP_Cause default_cause = {
463 .present = RANAP_Cause_PR_radioNetwork,
464 .choice.radioNetwork = RANAP_CauseRadioNetwork_no_remaining_rab,
465 };
466
467 if (!cause)
468 cause = &default_cause;
469
470 msg = ranap_new_msg_iu_rel_cmd(cause);
471 msg->l2h = msg->data;
472 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
473 prim->u.data.conn_id = ctx->conn_id;
474 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
475 OSMO_SCU_PRIM_N_DATA,
476 PRIM_OP_REQUEST, msg);
477 return osmo_sccp_user_sap_down(g_scu, &prim->oph);
478}
479
480static int ranap_handle_co_iu_rel_req(struct ranap_ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies)
481{
482 LOGPIU(LOGL_INFO, "Received Iu Release Request, Sending Release Command\n");
483 ranap_iu_tx_release(ctx, &ies->cause);
484 return 0;
485}
486
487static int ranap_handle_co_rab_ass_resp(struct ranap_ue_conn_ctx *ctx, RANAP_RAB_AssignmentResponseIEs_t *ies)
488{
489 int rc = -1;
490
491 LOGPIU(LOGL_INFO,
492 "Rx RAB Assignment Response for UE conn_id %u\n", ctx->conn_id);
493 if (ies->presenceMask & RAB_ASSIGNMENTRESPONSEIES_RANAP_RAB_SETUPORMODIFIEDLIST_PRESENT) {
494 /* TODO: Iterate over list of SetupOrModifiedList IEs and handle each one */
495 RANAP_IE_t *ranap_ie = ies->raB_SetupOrModifiedList.raB_SetupOrModifiedList_ies.list.array[0];
496 RANAP_RAB_SetupOrModifiedItemIEs_t setup_ies;
497
498 rc = ranap_decode_rab_setupormodifieditemies_fromlist(&setup_ies, &ranap_ie->value);
499 if (rc) {
500 LOGPIU(LOGL_ERROR, "Error in ranap_decode_rab_setupormodifieditemies()\n");
501 return rc;
502 }
503
504 rc = global_iu_event_cb(ctx, RANAP_IU_EVENT_RAB_ASSIGN, &setup_ies);
505
506 ranap_free_rab_setupormodifieditemies(&setup_ies);
507 }
508 /* FIXME: handle RAB Ass failure? */
509
510 return rc;
511}
512
513static void cn_ranap_handle_co_initial(void *ctx, ranap_message *message)
514{
515 int rc;
516
517 LOGPIU(LOGL_NOTICE, "handle_co_initial(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
518
519 if (message->direction != RANAP_RANAP_PDU_PR_initiatingMessage
520 || message->procedureCode != RANAP_ProcedureCode_id_InitialUE_Message) {
521 LOGPIU(LOGL_ERROR, "Expected direction 'InitiatingMessage',"
522 " procedureCode 'InitialUE_Message', instead got %u and %u\n",
523 message->direction, message->procedureCode);
524 rc = -1;
525 }
526 else
527 rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs);
528
529 if (rc) {
530 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
531 /* TODO handling of the error? */
532 }
533}
534
535/* Entry point for connection-oriented RANAP message */
536static void cn_ranap_handle_co(void *ctx, ranap_message *message)
537{
538 int rc;
539
540 LOGPIU(LOGL_NOTICE, "handle_co(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
541
542 switch (message->direction) {
543 case RANAP_RANAP_PDU_PR_initiatingMessage:
544 switch (message->procedureCode) {
545 case RANAP_ProcedureCode_id_InitialUE_Message:
546 LOGPIU(LOGL_ERROR, "Got InitialUE_Message but this is not a new conn\n");
547 rc = -1;
548 break;
549 case RANAP_ProcedureCode_id_DirectTransfer:
550 rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs);
551 break;
552 case RANAP_ProcedureCode_id_ErrorIndication:
553 rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs);
554 break;
555 case RANAP_ProcedureCode_id_Iu_ReleaseRequest:
556 /* Iu Release Request */
557 rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs);
558 break;
559 default:
560 LOGPIU(LOGL_ERROR, "Received Initiating Message: unknown Procedure Code %d\n",
561 message->procedureCode);
562 rc = -1;
563 break;
564 }
565 break;
566 case RANAP_RANAP_PDU_PR_successfulOutcome:
567 switch (message->procedureCode) {
568 case RANAP_ProcedureCode_id_SecurityModeControl:
569 /* Security Mode Complete */
570 rc = global_iu_event_cb(ctx, RANAP_IU_EVENT_SECURITY_MODE_COMPLETE, NULL);
571 break;
572 case RANAP_ProcedureCode_id_Iu_Release:
573 /* Iu Release Complete */
574 rc = global_iu_event_cb(ctx, RANAP_IU_EVENT_IU_RELEASE, NULL);
575 if (rc) {
576 LOGPIU(LOGL_ERROR, "Iu Release event: Iu Event callback returned %d\n",
577 rc);
578 }
579 break;
580 default:
581 LOGPIU(LOGL_ERROR, "Received Successful Outcome: unknown Procedure Code %d\n",
582 message->procedureCode);
583 rc = -1;
584 break;
585 }
586 break;
587 case RANAP_RANAP_PDU_PR_outcome:
588 switch (message->procedureCode) {
589 case RANAP_ProcedureCode_id_RAB_Assignment:
590 /* RAB Assignment Response */
591 rc = ranap_handle_co_rab_ass_resp(ctx, &message->msg.raB_AssignmentResponseIEs);
592 break;
593 default:
594 LOGPIU(LOGL_ERROR, "Received Outcome: unknown Procedure Code %d\n",
595 message->procedureCode);
596 rc = -1;
597 break;
598 }
599 break;
600 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
601 default:
602 LOGPIU(LOGL_ERROR, "Received Unsuccessful Outcome: Procedure Code %d\n",
603 message->procedureCode);
604 rc = -1;
605 break;
606 }
607
608 if (rc) {
609 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
610 /* TODO handling of the error? */
611 }
612}
613
614static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies)
615{
Harald Welte8fa35b82019-04-20 20:13:31 +0200616 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) ctx;
617 struct osmo_scu_unitdata_param *ud_prim = &prim->u.unitdata;
618 RANAP_GlobalRNC_ID_t *grnc_id = NULL;
619 struct msgb *resp;
620
621 OSMO_ASSERT(prim->oph.primitive == OSMO_SCU_PRIM_N_UNITDATA);
622
623 /* FIXME: verify ies.cN_DomainIndicator */
624
625 if (ies->presenceMask & RESETIES_RANAP_GLOBALRNC_ID_PRESENT)
626 grnc_id = &ies->globalRNC_ID;
627
628 /* send reset response */
629 resp = ranap_new_msg_reset_ack(ies->cN_DomainIndicator, grnc_id);
630 if (!resp)
631 return -ENOMEM;
632 resp->l2h = resp->data;
633 return osmo_sccp_tx_unitdata_msg(g_scu, &g_local_sccp_addr, &ud_prim->calling_addr, resp);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200634}
635
636static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
637{
638 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
639 LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n",
640 ranap_cause_str(&ies->cause));
641 else
642 LOGPIU(LOGL_ERROR, "Rx Error Indication\n");
643
644 return 0;
645}
646
647/* Entry point for connection-less RANAP message */
648static void cn_ranap_handle_cl(void *ctx, ranap_message *message)
649{
650 int rc;
651
652 switch (message->direction) {
653 case RANAP_RANAP_PDU_PR_initiatingMessage:
654 switch (message->procedureCode) {
655 case RANAP_ProcedureCode_id_Reset:
656 /* received reset.req, send reset.resp */
657 rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs);
658 break;
659 case RANAP_ProcedureCode_id_ErrorIndication:
660 rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs);
661 break;
662 default:
663 rc = -1;
664 break;
665 }
666 break;
667 case RANAP_RANAP_PDU_PR_successfulOutcome:
668 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
669 case RANAP_RANAP_PDU_PR_outcome:
670 default:
671 rc = -1;
672 break;
673 }
674
675 if (rc) {
676 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
677 /* TODO handling of the error? */
678 }
679}
680
681/***********************************************************************
682 * Paging
683 ***********************************************************************/
684
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200685/* Send a paging command down a given SCCP User. tmsi and paging_cause are
686 * optional and may be passed NULL and 0, respectively, to disable their use.
687 * See enum RANAP_PagingCause.
688 *
689 * If TMSI is given, the IMSI is not sent over the air interface. Nevertheless,
690 * the IMSI is still required for resolution in the HNB-GW and/or(?) RNC. */
691static int iu_tx_paging_cmd(struct osmo_sccp_addr *called_addr,
692 const char *imsi, const uint32_t *tmsi,
693 bool is_ps, uint32_t paging_cause)
694{
695 struct msgb *msg;
696 msg = ranap_new_msg_paging_cmd(imsi, tmsi, is_ps? 1 : 0, paging_cause);
697 msg->l2h = msg->data;
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100698 return osmo_sccp_tx_unitdata_msg(g_scu, &g_local_sccp_addr, called_addr, msg);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200699}
700
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100701static int iu_page(const char *imsi, const uint32_t *tmsi_or_ptmsi,
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200702 uint16_t lac, uint8_t rac, bool is_ps)
703{
704 struct ranap_iu_rnc *rnc;
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100705 const char *log_msg;
706 int log_level;
707 int paged = 0;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200708
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100709 iu_rnc_lac_rac_find(&rnc, NULL, lac, rac);
710 if (rnc) {
711 if (iu_tx_paging_cmd(&rnc->sccp_addr, imsi, tmsi_or_ptmsi, is_ps, 0) == 0) {
712 log_msg = "Paging";
713 log_level = LOGL_DEBUG;
714 paged = 1;
715 } else {
716 log_msg = "Paging failed";
717 log_level = LOGL_ERROR;
718 }
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200719 } else {
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100720 log_msg = "Found no RNC to Page";
721 log_level = LOGL_ERROR;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200722 }
723
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100724 if (is_ps)
725 LOGPIU(log_level, "IuPS: %s on LAC %d RAC %d", log_msg, lac, rac);
726 else
727 LOGPIU(log_level, "IuCS: %s on LAC %d", log_msg, lac);
728 if (rnc)
729 LOGPIUC(log_level, " at SCCP-addr %s", osmo_sccp_addr_dump(&rnc->sccp_addr));
730 if (tmsi_or_ptmsi)
731 LOGPIUC(log_level, ", for %s %08x\n", is_ps? "PTMSI" : "TMSI", *tmsi_or_ptmsi);
732 else
733 LOGPIUC(log_level, ", for IMSI %s\n", imsi);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200734
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100735 return paged;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200736}
737
738int ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
739{
740 return iu_page(imsi, tmsi, lac, 0, false);
741}
742
743int ranap_iu_page_ps(const char *imsi, const uint32_t *ptmsi, uint16_t lac, uint8_t rac)
744{
745 return iu_page(imsi, ptmsi, lac, rac, true);
746}
747
748
749/***********************************************************************
750 *
751 ***********************************************************************/
752
753int tx_unitdata(struct osmo_sccp_user *scu);
754int tx_conn_req(struct osmo_sccp_user *scu, uint32_t conn_id);
755
756struct osmo_prim_hdr *make_conn_req(uint32_t conn_id);
757struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len);
758
759static struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param)
760{
761 struct msgb *msg = msgb_alloc(1024, "conn_resp");
762 struct osmo_scu_prim *prim;
763
764 prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim));
765 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
766 OSMO_SCU_PRIM_N_CONNECT,
767 PRIM_OP_RESPONSE, msg);
768 memcpy(&prim->u.connect, param, sizeof(prim->u.connect));
769 return &prim->oph;
770}
771
772static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
773{
774 struct osmo_sccp_user *scu = _scu;
775 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
776 struct osmo_prim_hdr *resp = NULL;
Alexander Couzens80307922019-08-13 17:13:59 +0200777 int rc = -1;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200778 struct ranap_ue_conn_ctx *ue;
779 struct new_ue_conn_ctx new_ctx = {};
780
781 LOGPIU(LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
782
783 switch (OSMO_PRIM_HDR(oph)) {
784 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
785 /* confirmation of outbound connection */
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200786 break;
787 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
788 /* indication of new inbound connection request*/
789 LOGPIU(LOGL_DEBUG, "N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id);
790 if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */
791 !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) {
792 LOGPIU(LOGL_NOTICE,
793 "Received invalid N-CONNECT.ind\n");
794 return 0;
795 }
796 new_ctx.sccp_addr = prim->u.connect.calling_addr;
797 new_ctx.conn_id = prim->u.connect.conn_id;
798 /* first ensure the local SCCP socket is ACTIVE */
799 resp = make_conn_resp(&prim->u.connect);
800 osmo_sccp_user_sap_down(scu, resp);
801 /* then handle the RANAP payload */
802 rc = ranap_cn_rx_co(cn_ranap_handle_co_initial, &new_ctx, msgb_l2(oph->msg), msgb_l2len(oph->msg));
803 break;
804 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
805 /* indication of disconnect */
806 LOGPIU(LOGL_DEBUG, "N-DISCONNECT.ind(%u)\n",
807 prim->u.disconnect.conn_id);
808 ue = ue_conn_ctx_find(prim->u.disconnect.conn_id);
Alexander Couzens87482b32019-08-13 17:06:17 +0200809 if (!ue)
810 break;
811
Alexander Couzens4ae261b2019-08-13 17:14:31 +0200812 rc = 0;
813 if (msgb_l2len(oph->msg) > 0)
814 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200815 break;
816 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
817 /* connection-oriented data received */
818 LOGPIU(LOGL_DEBUG, "N-DATA.ind(%u, %s)\n", prim->u.data.conn_id,
819 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
820 /* resolve UE context */
821 ue = ue_conn_ctx_find(prim->u.data.conn_id);
Alexander Couzens87482b32019-08-13 17:06:17 +0200822 if (!ue)
823 break;
824
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200825 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
826 break;
827 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
828 /* connection-less data received */
829 LOGPIU(LOGL_DEBUG, "N-UNITDATA.ind(%s)\n",
830 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Harald Welte8fa35b82019-04-20 20:13:31 +0200831 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 +0200832 break;
833 default:
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200834 break;
835 }
836
837 msgb_free(oph->msg);
838 return rc;
839}
840
841int ranap_iu_init(void *ctx, int log_subsystem, const char *sccp_user_name, struct osmo_sccp_instance *sccp,
842 ranap_iu_recv_cb_t iu_recv_cb, ranap_iu_event_cb_t iu_event_cb)
843{
844 iu_log_subsystem = log_subsystem;
845 talloc_iu_ctx = talloc_named_const(ctx, 1, "iu");
846 talloc_asn1_ctx = talloc_named_const(talloc_iu_ctx, 1, "asn1");
847
848 global_iu_recv_cb = iu_recv_cb;
849 global_iu_event_cb = iu_event_cb;
850 g_sccp = sccp;
Neels Hofmeyr1aef9a62017-08-12 18:02:44 +0200851 osmo_sccp_local_addr_by_instance(&g_local_sccp_addr, sccp, OSMO_SCCP_SSN_RANAP);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200852 g_scu = osmo_sccp_user_bind(g_sccp, sccp_user_name, sccp_sap_up, OSMO_SCCP_SSN_RANAP);
853
854 return 0;
855}